diff options
author | Eric Fiselier <eric@efcs.ca> | 2016-04-20 02:21:33 +0000 |
---|---|---|
committer | Eric Fiselier <eric@efcs.ca> | 2016-04-20 02:21:33 +0000 |
commit | e08afaf8df5ff65a4d89d85dc923456bc5131151 (patch) | |
tree | 9c018998ce7382e4456aa788492b4d55ccaf7c4b /libcxx/test/std/thread/thread.threads | |
parent | 3e8f1e483ce5a25e7904c22d238bf3661cad9229 (diff) | |
download | bcm5719-llvm-e08afaf8df5ff65a4d89d85dc923456bc5131151.tar.gz bcm5719-llvm-e08afaf8df5ff65a4d89d85dc923456bc5131151.zip |
[libcxx] Fix PR15638 - Only allocate in parent when starting a thread to prevent calling terminate.
Summary:
Hi,
When creating a new thread libc++ performs at least 2 allocations. The first allocates a tuple of args and the functor that will be passed to the new thread. The second allocation is for the thread local storage needed internally by libc++. Currently the second allocation happens in the child thread, meaning that if it throws the program will terminate with an uncaught bad alloc.
The solution to this is to allocate ALL memory in the parent thread and then pass it to the child.
See https://llvm.org/bugs/show_bug.cgi?id=15638
Reviewers: mclow.lists, danalbert, jroelofs, EricWF
Subscribers: cfe-commits
Differential Revision: http://reviews.llvm.org/D13748
llvm-svn: 266851
Diffstat (limited to 'libcxx/test/std/thread/thread.threads')
-rw-r--r-- | libcxx/test/std/thread/thread.threads/thread.thread.class/thread.thread.constr/F.pass.cpp | 66 |
1 files changed, 51 insertions, 15 deletions
diff --git a/libcxx/test/std/thread/thread.threads/thread.thread.class/thread.thread.constr/F.pass.cpp b/libcxx/test/std/thread/thread.threads/thread.thread.class/thread.thread.constr/F.pass.cpp index 3de15af8929..2489bfc972c 100644 --- a/libcxx/test/std/thread/thread.threads/thread.thread.class/thread.thread.constr/F.pass.cpp +++ b/libcxx/test/std/thread/thread.threads/thread.thread.class/thread.thread.constr/F.pass.cpp @@ -20,23 +20,28 @@ #include <thread> #include <new> +#include <atomic> #include <cstdlib> #include <cassert> #include "test_macros.h" -unsigned throw_one = 0xFFFF; +std::atomic<unsigned> throw_one(0xFFFF); +std::atomic<unsigned> outstanding_new(0); + void* operator new(std::size_t s) throw(std::bad_alloc) { if (throw_one == 0) throw std::bad_alloc(); --throw_one; + ++outstanding_new; return std::malloc(s); } void operator delete(void* p) throw() { + --outstanding_new; std::free(p); } @@ -94,27 +99,58 @@ public: #endif -int main() -{ +// 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::threads constructor should properly handle exceptions and not leak +// memory. +// Plan: +// 1 Create a thread and count the number of allocations, 'N', 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 'N+1' +// allocations. +void test_throwing_new_during_thread_creation() { + throw_one = 0xFFF; { std::thread t(f); t.join(); - assert(f_run == true); } - f_run = false; - { - try - { - throw_one = 0; + const int 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(false); - } - catch (...) - { - throw_one = 0xFFFF; - assert(!f_run); + 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(old_outstanding == outstanding_new); // (2.3) + } + f_run = false; + throw_one = 0xFFF; +} + +int main() +{ + 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); |