Skip to content

Commit 86cf38d

Browse files
committed
check inputs to hash functions
1 parent f549c5f commit 86cf38d

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
@@ -809,16 +809,19 @@ void aws_hash_table_clear(struct aws_hash_table *map) {
809809
}
810810

811811
uint64_t aws_hash_c_string(const void *item) {
812+
AWS_PRECONDITION(aws_c_string_is_valid(item));
812813
const char *str = item;
813814

814815
/* first digits of pi in hex */
815816
uint32_t b = 0x3243F6A8, c = 0x885A308D;
816817
hashlittle2(str, strlen(str), &c, &b);
817818

819+
AWS_POSTCONDITION(aws_c_string_is_valid(item));
818820
return ((uint64_t)b << 32) | c;
819821
}
820822

821823
uint64_t aws_hash_string(const void *item) {
824+
AWS_PRECONDITION(aws_string_is_valid(item));
822825
const struct aws_string *str = item;
823826

824827
/* first digits of pi in hex */
@@ -829,16 +832,19 @@ uint64_t aws_hash_string(const void *item) {
829832
}
830833

831834
uint64_t aws_hash_byte_cursor_ptr(const void *item) {
835+
AWS_PRECONDITION(aws_byte_cursor_is_valid(item));
832836
const struct aws_byte_cursor *cur = item;
833837

834838
/* first digits of pi in hex */
835839
uint32_t b = 0x3243F6A8, c = 0x885A308D;
836840
hashlittle2(cur->ptr, cur->len, &c, &b);
837841

842+
AWS_POSTCONDITION(aws_byte_cursor_is_valid(item));
838843
return ((uint64_t)b << 32) | c;
839844
}
840845

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

0 commit comments

Comments
 (0)