
Found while running libc++'s tests with MSVC's STL.
*
`libcxx/test/std/algorithms/alg.sorting/alg.heap.operations/sort.heap/ranges_sort_heap.pass.cpp`
+ Fix Clang `-Wunused-variable`, because `LIBCPP_ASSERT` expands to
nothing for MSVC's STL.
+ This is the same "always void-cast" change that #73437 applied to the
neighboring `complexity.pass.cpp`. I missed that
`ranges_sort_heap.pass.cpp` was also affected because we had disabled
this test.
*
`libcxx/test/std/input.output/file.streams/fstreams/ifstream.members/buffered_reads.pass.cpp`
*
`libcxx/test/std/input.output/file.streams/fstreams/ofstream.members/buffered_writes.pass.cpp`
+ Fix MSVC "warning C4244: '`=`': conversion from '`__int64`' to
'`_Ty`', possible loss of data".
+ This is a valid warning, possibly the best one that MSVC found in this
entire saga. We're accumulating a `std::vector<std::streamsize>` and
storing the result in `std::streamsize total_size` but we actually have
to start with `std::streamsize{0}` or we'll truncate.
*
`libcxx/test/std/input.output/filesystems/fs.enum/enum.path.format.pass.cpp`
+ Fix Clang `-Wunused-local-typedef` because the following usage is
libc++-only.
+ I'm just expanding it at the point of use, and using the dedicated
`LIBCPP_STATIC_ASSERT` to keep the line length down.
*
`libcxx/test/std/input.output/syncstream/syncbuf/syncstream.syncbuf.assign/swap.pass.cpp`
+ Fix MSVC "warning C4242: 'argument': conversion from '`int`' to
'`const _Elem`', possible loss of data".
+ This is a valid warning (possibly the second-best) as `sputc()`
returns `int_type`. If `sputc()` returns something unexpected, we want
to know, so we should separately say `expected.push_back(CharT('B'))`.
*
`libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.single/new.size_align_nothrow.pass.cpp`
*
`libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.single/new.size_nothrow.pass.cpp`
+ Fix MSVC "warning C6001: Using uninitialized memory '`x`'."
+ [N4964](https://wg21.link/N4964) \[new.delete.single\]/12:
> *Effects:* The deallocation functions
(\[basic.stc.dynamic.deallocation\]) called by a *delete-expression*
(\[expr.delete\]) to render the value of `ptr` invalid.
+ \[basic.stc.general\]/4:
> When the end of the duration of a region of storage is reached, the
values of all pointers representing the address of any part of that
region of storage become invalid pointer values (\[basic.compound\]).
Indirection through an invalid pointer value and passing an invalid
pointer value to a deallocation function have undefined behavior. Any
other use of an invalid pointer value has implementation-defined
behavior.
+ In certain configurations, after `delete x;` MSVC will consider `x` to
be radioactive (and in other configurations, it'll physically null out
`x` as a safety measure). We can copy it into `old_x` before deletion,
which the implementation finds acceptable.
*
`libcxx/test/std/ranges/range.adaptors/range.elements/general.pass.cpp`
*
`libcxx/test/std/ranges/range.adaptors/range.elements/iterator/deref.pass.cpp`
+ Fix MSVC "warning C4242: 'initializing': conversion from '`_Ty`' to
'`_Ty`', possible loss of data".
+ This was being emitted in `pair` and `tuple`'s perfect forwarding
constructors. Passing `short{1}` allows MSVC to see that no truncation
is happening.
*
`libcxx/test/std/ranges/range.adaptors/range.elements/iterator/member_types.compile.pass.cpp`
+ Fix MSVC "warning C4242: 'initializing': conversion from '`_Ty`' to
'`_Ty2`', possible loss of data".
+ Similarly, this was being emitted in `pair`'s perfect forwarding
constructor. After passing `short{1}`, I reduced repetition by relying
on CTAD. (I can undo that cleanup if it's stylistically undesirable.)
*
`libcxx/test/std/utilities/function.objects/refwrap/refwrap.const/type_conv_ctor.pass.cpp`
+ Fix MSVC "warning C4930: '`std::reference_wrapper<int> purr(void)`':
prototyped function not called (was a variable definition intended?)".
+ There's no reason for `purr()` to be locally declared (aside from
isolating it to a narrow scope, which has minimal benefits); it can be
declared like `meow()` above. 😸
*
`libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.create/make_shared_for_overwrite.pass.cpp`
*
`libcxx/test/std/utilities/smartptr/unique.ptr/unique.ptr.create/make_unique_for_overwrite.default_init.pass.cpp`
+ Fix MSVC static analysis warnings when replacing `operator new`:
```
warning C28196: The requirement that '(_Param_(1)>0)?(return!=0):(1)' is
not satisfied. (The expression does not evaluate to true.)
warning C6387: 'return' could be '0': this does not adhere to the
specification for the function 'new'.
warning C6011: Dereferencing NULL pointer 'reinterpret_cast<char
*>ptr+i'.
```
+ All we need is a null check, which appears in other `operator new`
replacements:
b85f1f9b18/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.single/new.size.replace.pass.cpp (L27-L28)
219 lines
5.2 KiB
C++
219 lines
5.2 KiB
C++
//===----------------------------------------------------------------------===//
|
|
//
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// <thread>
|
|
|
|
// class thread
|
|
|
|
// template <class F, class ...Args> thread(F&& f, Args&&... args);
|
|
|
|
// UNSUPPORTED: no-threads
|
|
// UNSUPPORTED: sanitizer-new-delete
|
|
|
|
#include <thread>
|
|
#include <new>
|
|
#include <atomic>
|
|
#include <cstdlib>
|
|
#include <cassert>
|
|
#include <vector>
|
|
|
|
#include "test_macros.h"
|
|
|
|
std::atomic<unsigned> throw_one(0xFFFF);
|
|
std::atomic<unsigned> outstanding_new(0);
|
|
|
|
|
|
void* operator new(std::size_t s) TEST_THROW_SPEC(std::bad_alloc)
|
|
{
|
|
unsigned expected = throw_one;
|
|
do {
|
|
if (expected == 0) TEST_THROW(std::bad_alloc());
|
|
} while (!throw_one.compare_exchange_weak(expected, expected - 1));
|
|
++outstanding_new;
|
|
void* ret = std::malloc(s);
|
|
if (!ret) {
|
|
std::abort(); // placate MSVC's unchecked malloc warning (assert() won't silence it)
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
void operator delete(void* p) TEST_NOEXCEPT
|
|
{
|
|
if (!p) return;
|
|
--outstanding_new;
|
|
std::free(p);
|
|
}
|
|
|
|
bool f_run = false;
|
|
|
|
struct F {
|
|
std::vector<int> v_; // so f's copy-ctor calls operator new
|
|
explicit F() : v_(10) {}
|
|
void operator()() const { f_run = true; }
|
|
};
|
|
F f;
|
|
|
|
class G
|
|
{
|
|
int alive_;
|
|
public:
|
|
static int n_alive;
|
|
static bool op_run;
|
|
|
|
G() : alive_(1) {++n_alive;}
|
|
G(const G& g) : alive_(g.alive_) {++n_alive;}
|
|
~G() {alive_ = 0; --n_alive;}
|
|
|
|
void operator()()
|
|
{
|
|
assert(alive_ == 1);
|
|
assert(n_alive >= 1);
|
|
op_run = true;
|
|
}
|
|
|
|
void operator()(int i, double j)
|
|
{
|
|
assert(alive_ == 1);
|
|
assert(n_alive >= 1);
|
|
assert(i == 5);
|
|
assert(j == 5.5);
|
|
op_run = true;
|
|
}
|
|
};
|
|
|
|
int G::n_alive = 0;
|
|
bool G::op_run = false;
|
|
|
|
#if TEST_STD_VER >= 11
|
|
|
|
class MoveOnly
|
|
{
|
|
MoveOnly(const MoveOnly&);
|
|
public:
|
|
MoveOnly() {}
|
|
MoveOnly(MoveOnly&&) {}
|
|
|
|
void operator()(MoveOnly&&)
|
|
{
|
|
}
|
|
};
|
|
|
|
#endif
|
|
|
|
// Test throwing std::bad_alloc
|
|
//-----------------------------
|
|
// Concerns:
|
|
// A Each allocation performed during thread construction should be performed
|
|
// in the parent thread so that std::terminate is not called if
|
|
// std::bad_alloc is thrown by new.
|
|
// B std::thread's constructor should properly handle exceptions and not leak
|
|
// memory.
|
|
// Plan:
|
|
// 1 Create a thread and count the number of allocations, 'numAllocs', it
|
|
// performs.
|
|
// 2 For each allocation performed run a test where that allocation throws.
|
|
// 2.1 check that the exception can be caught in the parent thread.
|
|
// 2.2 Check that the functor has not been called.
|
|
// 2.3 Check that no memory allocated by the creation of the thread is leaked.
|
|
// 3 Finally check that a thread runs successfully if we throw after
|
|
// 'numAllocs + 1' allocations.
|
|
|
|
int numAllocs;
|
|
|
|
void test_throwing_new_during_thread_creation() {
|
|
#ifndef TEST_HAS_NO_EXCEPTIONS
|
|
throw_one = 0xFFF;
|
|
{
|
|
std::thread t(f);
|
|
t.join();
|
|
}
|
|
numAllocs = 0xFFF - throw_one;
|
|
// i <= numAllocs means the last iteration is expected not to throw.
|
|
for (int i=0; i <= numAllocs; ++i) {
|
|
throw_one = i;
|
|
f_run = false;
|
|
unsigned old_outstanding = outstanding_new;
|
|
try {
|
|
std::thread t(f);
|
|
assert(i == numAllocs); // Only final iteration will not throw.
|
|
t.join();
|
|
assert(f_run);
|
|
} catch (std::bad_alloc const&) {
|
|
assert(i < numAllocs);
|
|
assert(!f_run); // (2.2)
|
|
}
|
|
ASSERT_WITH_LIBRARY_INTERNAL_ALLOCATIONS(old_outstanding == outstanding_new); // (2.3)
|
|
}
|
|
f_run = false;
|
|
throw_one = 0xFFF;
|
|
#endif
|
|
}
|
|
|
|
int main(int, char**)
|
|
{
|
|
test_throwing_new_during_thread_creation();
|
|
{
|
|
std::thread t(f);
|
|
t.join();
|
|
assert(f_run == true);
|
|
}
|
|
|
|
{
|
|
assert(G::n_alive == 0);
|
|
assert(!G::op_run);
|
|
{
|
|
G g;
|
|
std::thread t(g);
|
|
t.join();
|
|
}
|
|
assert(G::n_alive == 0);
|
|
assert(G::op_run);
|
|
}
|
|
G::op_run = false;
|
|
#ifndef TEST_HAS_NO_EXCEPTIONS
|
|
// The test below expects `std::thread` to call `new`, which may not be the
|
|
// case for all implementations.
|
|
LIBCPP_ASSERT(numAllocs > 0); // libc++ should call new.
|
|
if (numAllocs > 0) {
|
|
try
|
|
{
|
|
throw_one = 0;
|
|
assert(G::n_alive == 0);
|
|
assert(!G::op_run);
|
|
std::thread t((G()));
|
|
assert(false);
|
|
}
|
|
catch (std::bad_alloc const&)
|
|
{
|
|
throw_one = 0xFFFF;
|
|
assert(G::n_alive == 0);
|
|
assert(!G::op_run);
|
|
}
|
|
}
|
|
#endif
|
|
#if TEST_STD_VER >= 11
|
|
{
|
|
assert(G::n_alive == 0);
|
|
assert(!G::op_run);
|
|
{
|
|
G g;
|
|
std::thread t(g, 5, 5.5);
|
|
t.join();
|
|
}
|
|
assert(G::n_alive == 0);
|
|
assert(G::op_run);
|
|
}
|
|
{
|
|
std::thread t = std::thread(MoveOnly(), MoveOnly());
|
|
t.join();
|
|
}
|
|
#endif
|
|
|
|
return 0;
|
|
}
|