| 1 | #cxx . . $ `<file>/src/include/mallocrecursionguard.hpp`/pragma once |
| 2 | #ifndef MALLOCRECURSIONGUARD_H |
| 3 | #define MALLOCRECURSIONGUARD_Hcxx . . $ `src/include/mallocrecursionguard.hpp:3:9`! |
| 4 | |
| 5 | #include <pthread.h> |
| 6 | |
| 7 | #include <mutex> |
| 8 | |
| 9 | #include "common.hpp"cxx . . $ `<file>/src/include/common.hpp`/ |
| 10 | |
| 11 | /** |
| 12 | * Implements a thread-specific flag to guard against inadvertent recursions |
| 13 | * when interposing heap functions. |
| 14 | */ |
| 15 | class MallocRecursionGuardcxx . . $ MallocRecursionGuard# { |
| 16 | static pthread_key_t* getKeycxx . . $ MallocRecursionGuard#getKey(ac695d451f96e4aa).() { |
| 17 | static pthread_key_t _inMallocKey; |
| 18 | return &_inMallocKey; |
| 19 | } |
| 20 | |
| 21 | enumcxx . . $ MallocRecursionGuard#$anonymous_type_0# { NEEDS_KEYcxx . . $ MallocRecursionGuard#NEEDS_KEY. = 0, CREATING_KEYcxx . . $ MallocRecursionGuard#CREATING_KEY. = 1, DONEcxx . . $ MallocRecursionGuard#DONE. = 2 }; |
| 22 | |
| 23 | static inline bool isInMalloccxx . . $ MallocRecursionGuard#isInMalloc(c9857a73d68e59ba).() { |
| 24 | // modified double-checked locking pattern |
| 25 | // (https://en.wikipedia.org/wiki/Double-checked_locking) |
| 26 | static stdcxx . . $ std/::recursive_mutexcxx . . $ std/recursive_mutex# mcxx . . $ std/recursive_mutex#recursive_mutex(49f6e7a06ebc5aa8).; |
| 27 | static int inMallocKeyState{NEEDS_KEYcxx . . $ MallocRecursionGuard#NEEDS_KEY.}; |
| 28 | |
| 29 | // We create the thread-specific data store the pthread way because the C++ |
| 30 | // language based ones all seem to fail when interposing on malloc et al., |
| 31 | // as they are invoked early from within library initialization. |
| 32 | |
| 33 | auto state = __atomic_load_n(&inMallocKeyState, __ATOMIC_ACQUIREcxx . . $ `<built-in>:16:9`!); |
| 34 | if (state != DONEcxx . . $ MallocRecursionGuard#DONE.) { |
| 35 | if (slowPathInMalloccxx . . $ MallocRecursionGuard#slowPathInMalloc(5fda1cc0fd880211).(m, inMallocKeyState) == CREATING_KEYcxx . . $ MallocRecursionGuard#CREATING_KEY.) { |
| 36 | // this happens IFF pthread_key_create allocates memory |
| 37 | return true; |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | return pthread_getspecific(*getKeycxx . . $ MallocRecursionGuard#getKey(ac695d451f96e4aa).()) != 0; |
| 42 | } |
| 43 | |
| 44 | static int slowPathInMalloccxx . . $ MallocRecursionGuard#slowPathInMalloc(5fda1cc0fd880211).(stdcxx . . $ std/::recursive_mutexcxx . . $ std/recursive_mutex#& m, int& inMallocKeyState) { |
| 45 | stdcxx . . $ std/::lock_guardcxx . . $ std/lock_guard#<decltype(m)> gcxx . . $ std/lock_guard#lock_guard(8e935d9c2e576e3a).{m}; |
| 46 | |
| 47 | auto state = __atomic_load_n(&inMallocKeyState, __ATOMIC_RELAXEDcxx . . $ `<built-in>:14:9`!); |
| 48 | |
| 49 | if (unlikelycxx . . $ `src/include/common.hpp:8:9`!(state == NEEDS_KEY)) { |
| 50 | __atomic_store_n(&inMallocKeyState, CREATING_KEYcxx . . $ MallocRecursionGuard#CREATING_KEY., __ATOMIC_RELAXEDcxx . . $ `<built-in>:14:9`!); |
| 51 | if (pthread_key_create(getKeycxx . . $ MallocRecursionGuard#getKey(ac695d451f96e4aa).(), 0) != 0) { // may call [cm]alloc |
| 52 | abortcxx . . $ abort(8df32a62017fbbf6).(); |
| 53 | } |
| 54 | __atomic_store_n(&inMallocKeyState, DONEcxx . . $ MallocRecursionGuard#DONE., __ATOMIC_RELEASEcxx . . $ `<built-in>:17:9`!); |
| 55 | return DONEcxx . . $ MallocRecursionGuard#DONE.; |
| 56 | } |
| 57 | |
| 58 | return state; |
| 59 | } |
| 60 | |
| 61 | static inline void setInMalloccxx . . $ MallocRecursionGuard#setInMalloc(b45ddcbc8423892c).(bool state) { |
| 62 | pthread_setspecific(*getKeycxx . . $ MallocRecursionGuard#getKey(ac695d451f96e4aa).(), state ? (void*)1 : (void*)0); |
| 63 | } |
| 64 | |
| 65 | bool _wasInMalloccxx . . $ MallocRecursionGuard#_wasInMalloc.; |
| 66 | |
| 67 | MallocRecursionGuardcxx . . $ MallocRecursionGuard#MallocRecursionGuard(98456dd656beb6b2).(const MallocRecursionGuardcxx . . $ MallocRecursionGuard#&) = delete; |
| 68 | MallocRecursionGuardcxx . . $ MallocRecursionGuard#& operatorcxx . . $ MallocRecursionGuard#`operator=`(7cac9077f41ea7f3).=(const MallocRecursionGuardcxx . . $ MallocRecursionGuard#&) = delete; |
| 69 | |
| 70 | public: |
| 71 | inline MallocRecursionGuardcxx . . $ MallocRecursionGuard#MallocRecursionGuard(49f6e7a06ebc5aa8).() { |
| 72 | if (!(_wasInMalloccxx . . $ MallocRecursionGuard#_wasInMalloc. = isInMalloccxx . . $ MallocRecursionGuard#isInMalloc(c9857a73d68e59ba).())) { |
| 73 | setInMalloccxx . . $ MallocRecursionGuard#setInMalloc(b45ddcbc8423892c).(true); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | inline ~cxx . . $ MallocRecursionGuard#`~MallocRecursionGuard`(49f6e7a06ebc5aa8).MallocRecursionGuardcxx . . $ MallocRecursionGuard#() { |
| 78 | if (!_wasInMalloccxx . . $ MallocRecursionGuard#_wasInMalloc.) { |
| 79 | setInMalloccxx . . $ MallocRecursionGuard#setInMalloc(b45ddcbc8423892c).(false); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | inline bool wasInMalloccxx . . $ MallocRecursionGuard#wasInMalloc(50ce9a9e25b4a850).() const { return _wasInMalloccxx . . $ MallocRecursionGuard#_wasInMalloc.; } |
| 84 | }; |
| 85 | |
| 86 | #endif |