From 7390478f194a1655bc919664928217d811ae4c68 Mon Sep 17 00:00:00 2001 From: Nick Kralevich Date: Wed, 26 Aug 2015 10:40:00 -0700 Subject: [PATCH] libcutils: cleanups for -fsanitize=integer Hash functions rely on overflow behavior, so whitelist them. ATRACE_TAG_NOT_READY: use an unsigned constant when shifting bits. Otherwise, the value overflows on shift. The users of this constant assign it to a uint64_t variable. Change-Id: I21c437ce2083525e906c3ead3259ec34a1ef4b66 --- include/cutils/trace.h | 2 +- libcutils/hashmap.c | 7 +++++++ libcutils/str_parms.c | 3 +++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/include/cutils/trace.h b/include/cutils/trace.h index 6d9b3bc90..9c077d62a 100644 --- a/include/cutils/trace.h +++ b/include/cutils/trace.h @@ -71,7 +71,7 @@ __BEGIN_DECLS #define ATRACE_TAG_LAST ATRACE_TAG_PACKAGE_MANAGER // Reserved for initialization. -#define ATRACE_TAG_NOT_READY (1LL<<63) +#define ATRACE_TAG_NOT_READY (1ULL<<63) #define ATRACE_TAG_VALID_MASK ((ATRACE_TAG_LAST - 1) | ATRACE_TAG_LAST) diff --git a/libcutils/hashmap.c b/libcutils/hashmap.c index 65539ea30..ede3b981d 100644 --- a/libcutils/hashmap.c +++ b/libcutils/hashmap.c @@ -77,6 +77,9 @@ Hashmap* hashmapCreate(size_t initialCapacity, /** * Hashes the given key. */ +#ifdef __clang__ +__attribute__((no_sanitize("integer"))) +#endif static inline int hashKey(Hashmap* map, void* key) { int h = map->hash(key); @@ -152,6 +155,10 @@ void hashmapFree(Hashmap* map) { free(map); } +#ifdef __clang__ +__attribute__((no_sanitize("integer"))) +#endif +/* FIXME: relies on signed integer overflow, which is undefined behavior */ int hashmapHash(void* key, size_t keySize) { int h = keySize; char* data = (char*) key; diff --git a/libcutils/str_parms.c b/libcutils/str_parms.c index 924289aa9..4f23d09c5 100644 --- a/libcutils/str_parms.c +++ b/libcutils/str_parms.c @@ -42,6 +42,9 @@ static bool str_eq(void *key_a, void *key_b) } /* use djb hash unless we find it inadequate */ +#ifdef __clang__ +__attribute__((no_sanitize("integer"))) +#endif static int str_hash_fn(void *str) { uint32_t hash = 5381;