xref: /openbsd-src/gnu/llvm/compiler-rt/lib/gwp_asan/tests/enable_disable.cpp (revision d89ec533011f513df1010f142a111086a0785f09)
11f9cb04fSpatrick //===-- enable_disable.cpp --------------------------------------*- C++ -*-===//
21f9cb04fSpatrick //
31f9cb04fSpatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
41f9cb04fSpatrick // See https://llvm.org/LICENSE.txt for license information.
51f9cb04fSpatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
61f9cb04fSpatrick //
71f9cb04fSpatrick //===----------------------------------------------------------------------===//
81f9cb04fSpatrick 
91f9cb04fSpatrick #include "gwp_asan/tests/harness.h"
101f9cb04fSpatrick 
111f9cb04fSpatrick constexpr size_t Size = 100;
121f9cb04fSpatrick 
TEST_F(DefaultGuardedPoolAllocatorDeathTest,Fork)13*d89ec533Spatrick TEST_F(DefaultGuardedPoolAllocatorDeathTest, Fork) {
141f9cb04fSpatrick   void *P;
151f9cb04fSpatrick   pid_t Pid = fork();
161f9cb04fSpatrick   EXPECT_GE(Pid, 0);
171f9cb04fSpatrick   if (Pid == 0) {
181f9cb04fSpatrick     P = GPA.allocate(Size);
191f9cb04fSpatrick     EXPECT_NE(P, nullptr);
201f9cb04fSpatrick     memset(P, 0x42, Size);
211f9cb04fSpatrick     GPA.deallocate(P);
221f9cb04fSpatrick     _exit(0);
231f9cb04fSpatrick   }
241f9cb04fSpatrick   waitpid(Pid, nullptr, 0);
251f9cb04fSpatrick   P = GPA.allocate(Size);
261f9cb04fSpatrick   EXPECT_NE(P, nullptr);
271f9cb04fSpatrick   memset(P, 0x42, Size);
281f9cb04fSpatrick   GPA.deallocate(P);
291f9cb04fSpatrick 
301f9cb04fSpatrick   // fork should stall if the allocator has been disabled.
311f9cb04fSpatrick   EXPECT_DEATH(
321f9cb04fSpatrick       {
331f9cb04fSpatrick         GPA.disable();
341f9cb04fSpatrick         alarm(1);
351f9cb04fSpatrick         Pid = fork();
361f9cb04fSpatrick         EXPECT_GE(Pid, 0);
371f9cb04fSpatrick       },
381f9cb04fSpatrick       "");
391f9cb04fSpatrick }
401f9cb04fSpatrick 
411f9cb04fSpatrick namespace {
421f9cb04fSpatrick pthread_mutex_t Mutex;
431f9cb04fSpatrick pthread_cond_t Conditional = PTHREAD_COND_INITIALIZER;
441f9cb04fSpatrick bool ThreadReady = false;
451f9cb04fSpatrick 
enableMalloc(void * arg)461f9cb04fSpatrick void *enableMalloc(void *arg) {
471f9cb04fSpatrick   auto &GPA = *reinterpret_cast<gwp_asan::GuardedPoolAllocator *>(arg);
481f9cb04fSpatrick 
491f9cb04fSpatrick   // Signal the main thread we are ready.
501f9cb04fSpatrick   pthread_mutex_lock(&Mutex);
511f9cb04fSpatrick   ThreadReady = true;
521f9cb04fSpatrick   pthread_cond_signal(&Conditional);
531f9cb04fSpatrick   pthread_mutex_unlock(&Mutex);
541f9cb04fSpatrick 
551f9cb04fSpatrick   // Wait for the malloc_disable & fork, then enable the allocator again.
561f9cb04fSpatrick   sleep(1);
571f9cb04fSpatrick   GPA.enable();
581f9cb04fSpatrick 
591f9cb04fSpatrick   return nullptr;
601f9cb04fSpatrick }
611f9cb04fSpatrick 
TEST_F(DefaultGuardedPoolAllocator,DisableForkEnable)621f9cb04fSpatrick TEST_F(DefaultGuardedPoolAllocator, DisableForkEnable) {
631f9cb04fSpatrick   pthread_t ThreadId;
641f9cb04fSpatrick   EXPECT_EQ(pthread_create(&ThreadId, nullptr, &enableMalloc, &GPA), 0);
651f9cb04fSpatrick 
661f9cb04fSpatrick   // Do not lock the allocator right away, the other thread may need it to start
671f9cb04fSpatrick   // up.
681f9cb04fSpatrick   pthread_mutex_lock(&Mutex);
691f9cb04fSpatrick   while (!ThreadReady)
701f9cb04fSpatrick     pthread_cond_wait(&Conditional, &Mutex);
711f9cb04fSpatrick   pthread_mutex_unlock(&Mutex);
721f9cb04fSpatrick 
731f9cb04fSpatrick   // Disable the allocator and fork. fork should succeed after malloc_enable.
741f9cb04fSpatrick   GPA.disable();
751f9cb04fSpatrick   pid_t Pid = fork();
761f9cb04fSpatrick   EXPECT_GE(Pid, 0);
771f9cb04fSpatrick   if (Pid == 0) {
781f9cb04fSpatrick     void *P = GPA.allocate(Size);
791f9cb04fSpatrick     EXPECT_NE(P, nullptr);
801f9cb04fSpatrick     GPA.deallocate(P);
811f9cb04fSpatrick     _exit(0);
821f9cb04fSpatrick   }
831f9cb04fSpatrick   waitpid(Pid, nullptr, 0);
841f9cb04fSpatrick   EXPECT_EQ(pthread_join(ThreadId, 0), 0);
851f9cb04fSpatrick }
861f9cb04fSpatrick } // namespace
87