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. git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@273107 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
@@ -11,6 +11,7 @@
|
|||||||
#include "algorithm"
|
#include "algorithm"
|
||||||
#include "climits"
|
#include "climits"
|
||||||
#include "cstring"
|
#include "cstring"
|
||||||
|
#include "__debug"
|
||||||
|
|
||||||
_LIBCPP_BEGIN_NAMESPACE_STD
|
_LIBCPP_BEGIN_NAMESPACE_STD
|
||||||
|
|
||||||
@@ -167,7 +168,10 @@ strstreambuf::overflow(int_type __c)
|
|||||||
buf = new char[new_size];
|
buf = new char[new_size];
|
||||||
if (buf == nullptr)
|
if (buf == nullptr)
|
||||||
return int_type(EOF);
|
return int_type(EOF);
|
||||||
|
if (old_size != 0) {
|
||||||
|
_LIBCPP_ASSERT(eback(), "overflow copying from NULL");
|
||||||
memcpy(buf, eback(), static_cast<size_t>(old_size));
|
memcpy(buf, eback(), static_cast<size_t>(old_size));
|
||||||
|
}
|
||||||
ptrdiff_t ninp = gptr() - eback();
|
ptrdiff_t ninp = gptr() - eback();
|
||||||
ptrdiff_t einp = egptr() - eback();
|
ptrdiff_t einp = egptr() - eback();
|
||||||
ptrdiff_t nout = pptr() - pbase();
|
ptrdiff_t nout = pptr() - pbase();
|
||||||
|
|||||||
@@ -620,12 +620,13 @@ class Configuration(object):
|
|||||||
blacklist = os.path.join(self.libcxx_src_root,
|
blacklist = os.path.join(self.libcxx_src_root,
|
||||||
'test/ubsan_blacklist.txt')
|
'test/ubsan_blacklist.txt')
|
||||||
self.cxx.flags += ['-fsanitize=undefined',
|
self.cxx.flags += ['-fsanitize=undefined',
|
||||||
'-fno-sanitize=vptr,function',
|
'-fno-sanitize=vptr,function,float-divide-by-zero',
|
||||||
'-fno-sanitize-recover=all',
|
'-fno-sanitize-recover=all',
|
||||||
'-fsanitize-blacklist=' + blacklist]
|
'-fsanitize-blacklist=' + blacklist]
|
||||||
self.cxx.compile_flags += ['-O3']
|
self.cxx.compile_flags += ['-O3']
|
||||||
self.env['UBSAN_OPTIONS'] = 'print_stacktrace=1'
|
self.env['UBSAN_OPTIONS'] = 'print_stacktrace=1'
|
||||||
self.config.available_features.add('ubsan')
|
self.config.available_features.add('ubsan')
|
||||||
|
self.config.available_features.add('sanitizer-new-delete')
|
||||||
elif san == 'Thread':
|
elif san == 'Thread':
|
||||||
self.cxx.flags += ['-fsanitize=thread']
|
self.cxx.flags += ['-fsanitize=thread']
|
||||||
self.config.available_features.add('tsan')
|
self.config.available_features.add('tsan')
|
||||||
|
|||||||
@@ -23,12 +23,14 @@ int main()
|
|||||||
{
|
{
|
||||||
{
|
{
|
||||||
std::atomic_flag f;
|
std::atomic_flag f;
|
||||||
|
f.clear();
|
||||||
f.test_and_set();
|
f.test_and_set();
|
||||||
atomic_flag_clear(&f);
|
atomic_flag_clear(&f);
|
||||||
assert(f.test_and_set() == 0);
|
assert(f.test_and_set() == 0);
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
volatile std::atomic_flag f;
|
volatile std::atomic_flag f;
|
||||||
|
f.clear();
|
||||||
f.test_and_set();
|
f.test_and_set();
|
||||||
atomic_flag_clear(&f);
|
atomic_flag_clear(&f);
|
||||||
assert(f.test_and_set() == 0);
|
assert(f.test_and_set() == 0);
|
||||||
|
|||||||
@@ -22,38 +22,44 @@
|
|||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
{
|
{
|
||||||
std::atomic_flag f;
|
std::atomic_flag f; // uninitialized first
|
||||||
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);
|
atomic_flag_clear_explicit(&f, std::memory_order_relaxed);
|
||||||
assert(f.test_and_set() == 0);
|
assert(f.test_and_set() == 0);
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
std::atomic_flag f;
|
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);
|
atomic_flag_clear_explicit(&f, std::memory_order_release);
|
||||||
assert(f.test_and_set() == 0);
|
assert(f.test_and_set() == 0);
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
std::atomic_flag f;
|
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);
|
atomic_flag_clear_explicit(&f, std::memory_order_seq_cst);
|
||||||
assert(f.test_and_set() == 0);
|
assert(f.test_and_set() == 0);
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
volatile std::atomic_flag f;
|
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);
|
atomic_flag_clear_explicit(&f, std::memory_order_relaxed);
|
||||||
assert(f.test_and_set() == 0);
|
assert(f.test_and_set() == 0);
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
volatile std::atomic_flag f;
|
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);
|
atomic_flag_clear_explicit(&f, std::memory_order_release);
|
||||||
assert(f.test_and_set() == 0);
|
assert(f.test_and_set() == 0);
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
volatile std::atomic_flag f;
|
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);
|
atomic_flag_clear_explicit(&f, std::memory_order_seq_cst);
|
||||||
assert(f.test_and_set() == 0);
|
assert(f.test_and_set() == 0);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,50 +22,58 @@
|
|||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
{
|
{
|
||||||
std::atomic_flag f;
|
std::atomic_flag f; // uninitialized
|
||||||
f.test_and_set();
|
f.clear();
|
||||||
|
assert(f.test_and_set() == 0);
|
||||||
f.clear();
|
f.clear();
|
||||||
assert(f.test_and_set() == 0);
|
assert(f.test_and_set() == 0);
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
std::atomic_flag f;
|
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);
|
f.clear(std::memory_order_relaxed);
|
||||||
assert(f.test_and_set() == 0);
|
assert(f.test_and_set() == 0);
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
std::atomic_flag f;
|
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);
|
f.clear(std::memory_order_release);
|
||||||
assert(f.test_and_set() == 0);
|
assert(f.test_and_set() == 0);
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
std::atomic_flag f;
|
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);
|
f.clear(std::memory_order_seq_cst);
|
||||||
assert(f.test_and_set() == 0);
|
assert(f.test_and_set() == 0);
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
volatile std::atomic_flag f;
|
volatile std::atomic_flag f;
|
||||||
f.test_and_set();
|
f.clear();
|
||||||
|
assert(f.test_and_set() == 0);
|
||||||
f.clear();
|
f.clear();
|
||||||
assert(f.test_and_set() == 0);
|
assert(f.test_and_set() == 0);
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
volatile std::atomic_flag f;
|
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);
|
f.clear(std::memory_order_relaxed);
|
||||||
assert(f.test_and_set() == 0);
|
assert(f.test_and_set() == 0);
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
volatile std::atomic_flag f;
|
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);
|
f.clear(std::memory_order_release);
|
||||||
assert(f.test_and_set() == 0);
|
assert(f.test_and_set() == 0);
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
volatile std::atomic_flag f;
|
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);
|
f.clear(std::memory_order_seq_cst);
|
||||||
assert(f.test_and_set() == 0);
|
assert(f.test_and_set() == 0);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -64,12 +64,12 @@ void test() {
|
|||||||
end = std::experimental::sample(PopulationIterator(ia),
|
end = std::experimental::sample(PopulationIterator(ia),
|
||||||
PopulationIterator(ia + is),
|
PopulationIterator(ia + is),
|
||||||
SampleIterator(oa), os, g);
|
SampleIterator(oa), os, g);
|
||||||
assert(&*end - oa == std::min(os, is));
|
assert(end.base() - oa == std::min(os, is));
|
||||||
assert(std::equal(oa, oa + os, oa1));
|
assert(std::equal(oa, oa + os, oa1));
|
||||||
end = std::experimental::sample(PopulationIterator(ia),
|
end = std::experimental::sample(PopulationIterator(ia),
|
||||||
PopulationIterator(ia + is),
|
PopulationIterator(ia + is),
|
||||||
SampleIterator(oa), os, g);
|
SampleIterator(oa), os, g);
|
||||||
assert(&*end - oa == std::min(os, is));
|
assert(end.base() - oa == std::min(os, is));
|
||||||
assert(std::equal(oa, oa + os, oa2));
|
assert(std::equal(oa, oa + os, oa2));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -85,7 +85,7 @@ void test_empty_population() {
|
|||||||
SampleIterator end =
|
SampleIterator end =
|
||||||
std::experimental::sample(PopulationIterator(ia), PopulationIterator(ia),
|
std::experimental::sample(PopulationIterator(ia), PopulationIterator(ia),
|
||||||
SampleIterator(oa), os, g);
|
SampleIterator(oa), os, g);
|
||||||
assert(&*end == oa);
|
assert(end.base() == oa);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <template<class> class PopulationIteratorType, class PopulationItem,
|
template <template<class> class PopulationIteratorType, class PopulationItem,
|
||||||
@@ -100,7 +100,7 @@ void test_empty_sample() {
|
|||||||
SampleIterator end =
|
SampleIterator end =
|
||||||
std::experimental::sample(PopulationIterator(ia), PopulationIterator(ia + is),
|
std::experimental::sample(PopulationIterator(ia), PopulationIterator(ia + is),
|
||||||
SampleIterator(oa), 0, g);
|
SampleIterator(oa), 0, g);
|
||||||
assert(&*end == oa);
|
assert(end.base() == oa);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <template<class> class PopulationIteratorType, class PopulationItem,
|
template <template<class> class PopulationIteratorType, class PopulationItem,
|
||||||
@@ -119,8 +119,8 @@ void test_small_population() {
|
|||||||
end = std::experimental::sample(PopulationIterator(ia),
|
end = std::experimental::sample(PopulationIterator(ia),
|
||||||
PopulationIterator(ia + is),
|
PopulationIterator(ia + is),
|
||||||
SampleIterator(oa), os, g);
|
SampleIterator(oa), os, g);
|
||||||
assert(&*end - oa == std::min(os, is));
|
assert(end.base() - oa == std::min(os, is));
|
||||||
assert(std::equal(oa, &*end, oa1));
|
assert(std::equal(oa, end.base(), oa1));
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
|
|||||||
Reference in New Issue
Block a user