From 990952b6644e79da08c6925bddedff7c3b3ebe8a Mon Sep 17 00:00:00 2001 From: Eric Fiselier Date: Sun, 19 Jun 2016 07:08:27 +0000 Subject: Fix various undefined behavior found by UBSan. * Fix non-null violation in strstream.cpp Overflow was calling memcpy with a null parameter and a size of 0. * Fix std/atomics/atomics.flag/ tests: a.test_and_set() was reading from an uninitialized atomic, but wasn't using the value. The tests now clear the flag before performing the first test_and_set. This allows UBSAN to test that clear doesn't read an invalid value. * Fix std/experimental/algorithms/alg.random.sample/sample.pass.cpp The tests were dereferencing a past-the-end pointer to an array so that they could do pointer arithmetic with it. Instead of dereference the iterator I changed the tests to use the special 'base()' test iterator method. * Add -fno-sanitize=float-divide-by-zero to suppress division by zero UBSAN diagnostics. The tests that cause float division by zero are explicitly aware that they are doing that. Since this is well defined for IEEE floats suppress the warnings for now. llvm-svn: 273107 --- .../atomics.flag/atomic_flag_clear.pass.cpp | 2 ++ .../atomic_flag_clear_explicit.pass.cpp | 20 +++++++++++------ .../test/std/atomics/atomics.flag/clear.pass.cpp | 26 ++++++++++++++-------- 3 files changed, 32 insertions(+), 16 deletions(-) (limited to 'libcxx/test/std/atomics') diff --git a/libcxx/test/std/atomics/atomics.flag/atomic_flag_clear.pass.cpp b/libcxx/test/std/atomics/atomics.flag/atomic_flag_clear.pass.cpp index 64093d639e4..22bbbd6af53 100644 --- a/libcxx/test/std/atomics/atomics.flag/atomic_flag_clear.pass.cpp +++ b/libcxx/test/std/atomics/atomics.flag/atomic_flag_clear.pass.cpp @@ -23,12 +23,14 @@ int main() { { std::atomic_flag f; + f.clear(); f.test_and_set(); atomic_flag_clear(&f); assert(f.test_and_set() == 0); } { volatile std::atomic_flag f; + f.clear(); f.test_and_set(); atomic_flag_clear(&f); assert(f.test_and_set() == 0); diff --git a/libcxx/test/std/atomics/atomics.flag/atomic_flag_clear_explicit.pass.cpp b/libcxx/test/std/atomics/atomics.flag/atomic_flag_clear_explicit.pass.cpp index e1a9349c939..1a212c6f352 100644 --- a/libcxx/test/std/atomics/atomics.flag/atomic_flag_clear_explicit.pass.cpp +++ b/libcxx/test/std/atomics/atomics.flag/atomic_flag_clear_explicit.pass.cpp @@ -22,38 +22,44 @@ int main() { { - std::atomic_flag f; - f.test_and_set(); + std::atomic_flag f; // uninitialized first + atomic_flag_clear_explicit(&f, std::memory_order_relaxed); + assert(f.test_and_set() == 0); atomic_flag_clear_explicit(&f, std::memory_order_relaxed); assert(f.test_and_set() == 0); } { std::atomic_flag f; - f.test_and_set(); + atomic_flag_clear_explicit(&f, std::memory_order_release); + assert(f.test_and_set() == 0); atomic_flag_clear_explicit(&f, std::memory_order_release); assert(f.test_and_set() == 0); } { std::atomic_flag f; - f.test_and_set(); + atomic_flag_clear_explicit(&f, std::memory_order_seq_cst); + assert(f.test_and_set() == 0); atomic_flag_clear_explicit(&f, std::memory_order_seq_cst); assert(f.test_and_set() == 0); } { volatile std::atomic_flag f; - f.test_and_set(); + atomic_flag_clear_explicit(&f, std::memory_order_relaxed); + assert(f.test_and_set() == 0); atomic_flag_clear_explicit(&f, std::memory_order_relaxed); assert(f.test_and_set() == 0); } { volatile std::atomic_flag f; - f.test_and_set(); + atomic_flag_clear_explicit(&f, std::memory_order_release); + assert(f.test_and_set() == 0); atomic_flag_clear_explicit(&f, std::memory_order_release); assert(f.test_and_set() == 0); } { volatile std::atomic_flag f; - f.test_and_set(); + atomic_flag_clear_explicit(&f, std::memory_order_seq_cst); + assert(f.test_and_set() == 0); atomic_flag_clear_explicit(&f, std::memory_order_seq_cst); assert(f.test_and_set() == 0); } diff --git a/libcxx/test/std/atomics/atomics.flag/clear.pass.cpp b/libcxx/test/std/atomics/atomics.flag/clear.pass.cpp index 65051af790d..255af8f176e 100644 --- a/libcxx/test/std/atomics/atomics.flag/clear.pass.cpp +++ b/libcxx/test/std/atomics/atomics.flag/clear.pass.cpp @@ -22,50 +22,58 @@ int main() { { - std::atomic_flag f; - f.test_and_set(); + std::atomic_flag f; // uninitialized + f.clear(); + assert(f.test_and_set() == 0); f.clear(); assert(f.test_and_set() == 0); } { std::atomic_flag f; - f.test_and_set(); + f.clear(std::memory_order_relaxed); + assert(f.test_and_set() == 0); f.clear(std::memory_order_relaxed); assert(f.test_and_set() == 0); } { std::atomic_flag f; - f.test_and_set(); + f.clear(std::memory_order_release); + assert(f.test_and_set() == 0); f.clear(std::memory_order_release); assert(f.test_and_set() == 0); } { std::atomic_flag f; - f.test_and_set(); + f.clear(std::memory_order_seq_cst); + assert(f.test_and_set() == 0); f.clear(std::memory_order_seq_cst); assert(f.test_and_set() == 0); } { volatile std::atomic_flag f; - f.test_and_set(); + f.clear(); + assert(f.test_and_set() == 0); f.clear(); assert(f.test_and_set() == 0); } { volatile std::atomic_flag f; - f.test_and_set(); + f.clear(std::memory_order_relaxed); + assert(f.test_and_set() == 0); f.clear(std::memory_order_relaxed); assert(f.test_and_set() == 0); } { volatile std::atomic_flag f; - f.test_and_set(); + f.clear(std::memory_order_release); + assert(f.test_and_set() == 0); f.clear(std::memory_order_release); assert(f.test_and_set() == 0); } { volatile std::atomic_flag f; - f.test_and_set(); + f.clear(std::memory_order_seq_cst); + assert(f.test_and_set() == 0); f.clear(std::memory_order_seq_cst); assert(f.test_and_set() == 0); } -- cgit v1.2.3