Skip to content

Commit 6cbeaed

Browse files
danielsnfeliperodri
authored andcommitted
check inputs to hash functions
1 parent 61d5409 commit 6cbeaed

File tree

8 files changed

+84
-5
lines changed

8 files changed

+84
-5
lines changed

.cbmc-batch/jobs/Makefile.common

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ CBMCFLAGS += --pointer-check
3737
CBMCFLAGS += --pointer-overflow-check
3838
CBMCFLAGS += --signed-overflow-check
3939
CBMCFLAGS += --undefined-shift-check
40-
CBMCFLAGS += --unsigned-overflow-check
40+
#CBMCFLAGS += --unsigned-overflow-check
4141
CBMCFLAGS += --unwind 1
4242
CBMCFLAGS += --unwinding-assertions
4343
CBMCFLAGS += $(CBMC_UNWINDSET)

.cbmc-batch/jobs/aws_hash_c_string/Makefile

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,16 @@
1212
# limitations under the License.
1313

1414
###########
15-
MAX_STRING_SIZE ?= 4
15+
#32: 4s
16+
#64: 5s
17+
#128 6s
18+
#256 11s
19+
#1024: 2m 30s
20+
MAX_STRING_SIZE ?= 256
1621
DEFINES += -DMAX_STRING_SIZE=$(MAX_STRING_SIZE)
1722

1823
UNWINDSET += strlen.0:$(shell echo $$(( $(MAX_STRING_SIZE) + 1)))
19-
24+
UNWINDSET += hashlittle2.2:$(shell echo $$(( $$(( $(MAX_STRING_SIZE) / 12 )) +1 )) )
2025

2126
CBMCFLAGS +=
2227

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
jobos: ubuntu16
2-
cbmcflags: "--bounds-check;--div-by-zero-check;--float-overflow-check;--nan-check;--pointer-check;--pointer-overflow-check;--signed-overflow-check;--undefined-shift-check;--unsigned-overflow-check;--unwind;1;--unwinding-assertions;--unwindset;strlen.0:5;--object-bits;8"
2+
cbmcflags: "--bounds-check;--div-by-zero-check;--float-overflow-check;--nan-check;--pointer-check;--pointer-overflow-check;--signed-overflow-check;--undefined-shift-check;--unwind;1;--unwinding-assertions;--unwindset;strlen.0:257,hashlittle2.2:22;--object-bits;8"
33
goto: aws_hash_c_string_harness.goto
44
expected: "SUCCESSFUL"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
jobos: ubuntu16
2-
cbmcflags: "--bounds-check;--div-by-zero-check;--float-overflow-check;--nan-check;--pointer-check;--pointer-overflow-check;--signed-overflow-check;--undefined-shift-check;--unsigned-overflow-check;--unwind;1;--unwinding-assertions;--object-bits;8"
2+
cbmcflags: "--bounds-check;--div-by-zero-check;--float-overflow-check;--nan-check;--pointer-check;--pointer-overflow-check;--signed-overflow-check;--undefined-shift-check;--unwind;1;--unwinding-assertions;--object-bits;8"
33
goto: aws_hash_ptr_harness.goto
44
expected: "SUCCESSFUL"
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License"). You may not use
4+
# this file except in compliance with the License. A copy of the License is
5+
# located at
6+
#
7+
# http://aws.amazon.com/apache2.0/
8+
#
9+
# or in the "license" file accompanying this file. This file is distributed on an
10+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
11+
# implied. See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
14+
###########
15+
#32: 4s
16+
#64: 5s
17+
#128 6s
18+
#256 11s
19+
#1024: 2m 30s
20+
MAX_STRING_SIZE ?= 256
21+
DEFINES += -DMAX_STRING_SIZE=$(MAX_STRING_SIZE)
22+
23+
UNWINDSET += hashlittle2.2:$(shell echo $$(( $$(( $(MAX_STRING_SIZE) / 12 )) +1 )) )
24+
25+
CBMCFLAGS +=
26+
27+
DEPENDENCIES += $(HELPERDIR)/source/make_common_data_structures.c
28+
DEPENDENCIES += $(HELPERDIR)/source/proof_allocators.c
29+
DEPENDENCIES += $(HELPERDIR)/source/utils.c
30+
DEPENDENCIES += $(HELPERDIR)/stubs/error.c
31+
DEPENDENCIES += $(SRCDIR)/source/common.c
32+
DEPENDENCIES += $(SRCDIR)/source/hash_table.c
33+
34+
ENTRY = aws_hash_string_harness
35+
###########
36+
37+
include ../Makefile.common
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use
5+
* this file except in compliance with the License. A copy of the License is
6+
* located at
7+
*
8+
* http://aws.amazon.com/apache2.0/
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed on an
11+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12+
* implied. See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
#include <aws/common/hash_table.h>
17+
#include <aws/common/private/hash_table_impl.h>
18+
#include <proof_helpers/make_common_data_structures.h>
19+
#include <proof_helpers/proof_allocators.h>
20+
#include <proof_helpers/utils.h>
21+
22+
void aws_hash_string_harness() {
23+
struct aws_string *str =
24+
make_arbitrary_aws_string_nondet_len_with_max(nondet_bool() ? NULL : can_fail_allocator(), MAX_STRING_SIZE);
25+
/* This function has no pre or post conditions */
26+
uint64_t rval = aws_hash_string(str);
27+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
jobos: ubuntu16
2+
cbmcflags: "--bounds-check;--div-by-zero-check;--float-overflow-check;--nan-check;--pointer-check;--pointer-overflow-check;--signed-overflow-check;--undefined-shift-check;--unwind;1;--unwinding-assertions;--unwindset;hashlittle2.2:22;--object-bits;8"
3+
goto: aws_hash_string_harness.goto
4+
expected: "SUCCESSFUL"

source/hash_table.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -972,16 +972,19 @@ void aws_hash_table_clear(struct aws_hash_table *map) {
972972
}
973973

974974
uint64_t aws_hash_c_string(const void *item) {
975+
AWS_PRECONDITION(aws_c_string_is_valid(item));
975976
const char *str = item;
976977

977978
/* first digits of pi in hex */
978979
uint32_t b = 0x3243F6A8, c = 0x885A308D;
979980
hashlittle2(str, strlen(str), &c, &b);
980981

982+
AWS_POSTCONDITION(aws_c_string_is_valid(item));
981983
return ((uint64_t)b << 32) | c;
982984
}
983985

984986
uint64_t aws_hash_string(const void *item) {
987+
AWS_PRECONDITION(aws_string_is_valid(item));
985988
const struct aws_string *str = item;
986989

987990
/* first digits of pi in hex */
@@ -992,16 +995,19 @@ uint64_t aws_hash_string(const void *item) {
992995
}
993996

994997
uint64_t aws_hash_byte_cursor_ptr(const void *item) {
998+
AWS_PRECONDITION(aws_byte_cursor_is_valid(item));
995999
const struct aws_byte_cursor *cur = item;
9961000

9971001
/* first digits of pi in hex */
9981002
uint32_t b = 0x3243F6A8, c = 0x885A308D;
9991003
hashlittle2(cur->ptr, cur->len, &c, &b);
10001004

1005+
AWS_POSTCONDITION(aws_byte_cursor_is_valid(item));
10011006
return ((uint64_t)b << 32) | c;
10021007
}
10031008

10041009
uint64_t aws_hash_ptr(const void *item) {
1010+
/* Since the numberic value of the pointer is considered, not the memory behind it, 0 is an acceptable value */
10051011
/* first digits of e in hex
10061012
* 2.b7e 1516 28ae d2a6 */
10071013
uint32_t b = 0x2b7e1516, c = 0x28aed2a6;

0 commit comments

Comments
 (0)