1 //===-- enable_disable.cpp --------------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "gwp_asan/tests/harness.h"
11 constexpr size_t Size
= 100;
13 TEST_F(DefaultGuardedPoolAllocatorDeathTest
, Fork
) {
18 P
= GPA
.allocate(Size
);
19 EXPECT_NE(P
, nullptr);
20 memset(P
, 0x42, Size
);
24 waitpid(Pid
, nullptr, 0);
25 P
= GPA
.allocate(Size
);
26 EXPECT_NE(P
, nullptr);
27 memset(P
, 0x42, Size
);
30 // fork should stall if the allocator has been disabled.
42 pthread_mutex_t Mutex
;
43 pthread_cond_t Conditional
= PTHREAD_COND_INITIALIZER
;
44 bool ThreadReady
= false;
46 void *enableMalloc(void *arg
) {
47 auto &GPA
= *reinterpret_cast<gwp_asan::GuardedPoolAllocator
*>(arg
);
49 // Signal the main thread we are ready.
50 pthread_mutex_lock(&Mutex
);
52 pthread_cond_signal(&Conditional
);
53 pthread_mutex_unlock(&Mutex
);
55 // Wait for the malloc_disable & fork, then enable the allocator again.
62 TEST_F(DefaultGuardedPoolAllocator
, DisableForkEnable
) {
64 EXPECT_EQ(pthread_create(&ThreadId
, nullptr, &enableMalloc
, &GPA
), 0);
66 // Do not lock the allocator right away, the other thread may need it to start
68 pthread_mutex_lock(&Mutex
);
70 pthread_cond_wait(&Conditional
, &Mutex
);
71 pthread_mutex_unlock(&Mutex
);
73 // Disable the allocator and fork. fork should succeed after malloc_enable.
78 void *P
= GPA
.allocate(Size
);
79 EXPECT_NE(P
, nullptr);
83 waitpid(Pid
, nullptr, 0);
84 EXPECT_EQ(pthread_join(ThreadId
, 0), 0);