diff options
author | Eric Fiselier <eric@efcs.ca> | 2017-02-08 00:10:10 +0000 |
---|---|---|
committer | Eric Fiselier <eric@efcs.ca> | 2017-02-08 00:10:10 +0000 |
commit | 25f28d0c9ec4f42f3eabbc5c6bd9c53485c325c3 (patch) | |
tree | f6f1ad71dacbeb4c518357203d013c8dd6f13a5d /test | |
parent | fecf0579406081010195d87cd5a74608c33d5004 (diff) | |
download | external_libcxx-25f28d0c9ec4f42f3eabbc5c6bd9c53485c325c3.tar.gz external_libcxx-25f28d0c9ec4f42f3eabbc5c6bd9c53485c325c3.tar.bz2 external_libcxx-25f28d0c9ec4f42f3eabbc5c6bd9c53485c325c3.zip |
Prevent UBSAN from generating unsigned overflow diagnostics in the hashing internals
git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@294391 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'test')
-rw-r--r-- | test/libcxx/utilities/function.objects/unord.hash/murmur2_or_cityhash_ubsan_unsigned_overflow_ignored.pass.cpp | 41 | ||||
-rw-r--r-- | test/support/test_macros.h | 12 |
2 files changed, 53 insertions, 0 deletions
diff --git a/test/libcxx/utilities/function.objects/unord.hash/murmur2_or_cityhash_ubsan_unsigned_overflow_ignored.pass.cpp b/test/libcxx/utilities/function.objects/unord.hash/murmur2_or_cityhash_ubsan_unsigned_overflow_ignored.pass.cpp new file mode 100644 index 000000000..319a78b05 --- /dev/null +++ b/test/libcxx/utilities/function.objects/unord.hash/murmur2_or_cityhash_ubsan_unsigned_overflow_ignored.pass.cpp @@ -0,0 +1,41 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// Test that UBSAN doesn't generate unsigned integer overflow diagnostics +// from within the hashing internals. + +#include <utility> +#include <cstdint> +#include <limits> +#include <string> + +#include "test_macros.h" + +typedef std::__murmur2_or_cityhash<uint32_t> Hash32; +typedef std::__murmur2_or_cityhash<uint64_t> Hash64; + +void test(const void* key, int len) { + for (int i=1; i <= len; ++i) { + Hash32 h1; + Hash64 h2; + DoNotOptimize(h1(key, i)); + DoNotOptimize(h2(key, i)); + } +} + +int main() { + const std::string TestCases[] = { + "abcdaoeuaoeclaoeoaeuaoeuaousaotehu]+}sthoasuthaoesutahoesutaohesutaoeusaoetuhasoetuhaoseutaoseuthaoesutaohes" + "00000000000000000000000000000000000000000000000000000000000000000000000", + "1237546895+54+4554985416849484213464984765465464654564565645645646546456546546" + }; + const size_t NumCases = sizeof(TestCases)/sizeof(TestCases[0]); + for (size_t i=0; i < NumCases; ++i) + test(TestCases[i].data(), TestCases[i].length()); +} diff --git a/test/support/test_macros.h b/test/support/test_macros.h index 431ca8a3e..cec5f5a5e 100644 --- a/test/support/test_macros.h +++ b/test/support/test_macros.h @@ -182,6 +182,18 @@ struct is_same<T, T> { enum {value = 1}; }; #endif #endif +#if defined(__GNUC__) || defined(__clang__) +template <class Tp> +inline void DoNotOptimize(Tp const& value) { + asm volatile("" : : "g"(value) : "memory"); +} +#else +template <class Tp> +inline void DoNotOptimize(Tp const&) { + // FIXME: Do something here... +} +#endif + #if defined(__GNUC__) #pragma GCC diagnostic pop #endif |