|
26 | 26 | #include <folly/experimental/fibers/Fiber.h> |
27 | 27 | #include <folly/experimental/fibers/LoopController.h> |
28 | 28 |
|
| 29 | +#ifdef FOLLY_SANITIZE_ADDRESS |
| 30 | + |
| 31 | +#include <dlfcn.h> |
| 32 | + |
| 33 | +static void __asan_enter_fiber_weak( |
| 34 | + void const* fiber_stack_base, |
| 35 | + size_t fiber_stack_extent) |
| 36 | + __attribute__((__weakref__("__asan_enter_fiber"))); |
| 37 | +static void __asan_exit_fiber_weak() |
| 38 | + __attribute__((__weakref__("__asan_exit_fiber"))); |
| 39 | + |
| 40 | +typedef void (*AsanEnterFiberFuncPtr)(void const*, size_t); |
| 41 | +typedef void (*AsanExitFiberFuncPtr)(); |
| 42 | + |
| 43 | +namespace folly { namespace fibers { |
| 44 | + |
| 45 | +static AsanEnterFiberFuncPtr getEnterFiberFunc(); |
| 46 | +static AsanExitFiberFuncPtr getExitFiberFunc(); |
| 47 | + |
| 48 | +}} |
| 49 | + |
| 50 | +#endif |
| 51 | + |
29 | 52 | namespace folly { namespace fibers { |
30 | 53 |
|
31 | 54 | FOLLY_TLS FiberManager* FiberManager::currentFiberManager_ = nullptr; |
@@ -88,7 +111,6 @@ Fiber* FiberManager::getFiber() { |
88 | 111 | ++fiberId_; |
89 | 112 | bool recordStack = (options_.recordStackEvery != 0) && |
90 | 113 | (fiberId_ % options_.recordStackEvery == 0); |
91 | | - fiber->init(recordStack); |
92 | 114 | return fiber; |
93 | 115 | } |
94 | 116 |
|
@@ -139,12 +161,78 @@ void FiberManager::doFibersPoolResizing() { |
139 | 161 | maxFibersActiveLastPeriod_ = fibersActive_; |
140 | 162 | } |
141 | 163 |
|
142 | | -void FiberManager::FiberManager::FibersPoolResizer::operator()() { |
| 164 | +void FiberManager::FibersPoolResizer::operator()() { |
143 | 165 | fiberManager_.doFibersPoolResizing(); |
144 | 166 | fiberManager_.timeoutManager_->registerTimeout( |
145 | 167 | *this, |
146 | 168 | std::chrono::milliseconds( |
147 | 169 | fiberManager_.options_.fibersPoolResizePeriodMs)); |
148 | 170 | } |
149 | 171 |
|
| 172 | +#ifdef FOLLY_SANITIZE_ADDRESS |
| 173 | + |
| 174 | +void FiberManager::registerFiberActivationWithAsan(Fiber* fiber) { |
| 175 | + auto context = &fiber->fcontext_; |
| 176 | + void* top = context->stackBase(); |
| 177 | + void* bottom = context->stackLimit(); |
| 178 | + size_t extent = static_cast<char*>(top) - static_cast<char*>(bottom); |
| 179 | + |
| 180 | + // Check if we can find a fiber enter function and call it if we find one |
| 181 | + static AsanEnterFiberFuncPtr fn = getEnterFiberFunc(); |
| 182 | + if (fn == nullptr) { |
| 183 | + LOG(FATAL) << "The version of ASAN in use doesn't support fibers"; |
| 184 | + } else { |
| 185 | + fn(bottom, extent); |
| 186 | + } |
| 187 | +} |
| 188 | + |
| 189 | +void FiberManager::registerFiberDeactivationWithAsan(Fiber* fiber) { |
| 190 | + (void)fiber; // currently unused |
| 191 | + |
| 192 | + // Check if we can find a fiber exit function and call it if we find one |
| 193 | + static AsanExitFiberFuncPtr fn = getExitFiberFunc(); |
| 194 | + if (fn == nullptr) { |
| 195 | + LOG(FATAL) << "The version of ASAN in use doesn't support fibers"; |
| 196 | + } else { |
| 197 | + fn(); |
| 198 | + } |
| 199 | +} |
| 200 | + |
| 201 | +static AsanEnterFiberFuncPtr getEnterFiberFunc() { |
| 202 | + AsanEnterFiberFuncPtr fn{nullptr}; |
| 203 | + |
| 204 | + // Check whether weak reference points to statically linked enter function |
| 205 | + if (nullptr != (fn = &::__asan_enter_fiber_weak)) { |
| 206 | + return fn; |
| 207 | + } |
| 208 | + |
| 209 | + // Check whether we can find a dynamically linked enter function |
| 210 | + if (nullptr != |
| 211 | + (fn = (AsanEnterFiberFuncPtr)dlsym(RTLD_DEFAULT, "__asan_enter_fiber"))) { |
| 212 | + return fn; |
| 213 | + } |
| 214 | + |
| 215 | + // Couldn't find the function at all |
| 216 | + return nullptr; |
| 217 | +} |
| 218 | + |
| 219 | +static AsanExitFiberFuncPtr getExitFiberFunc() { |
| 220 | + AsanExitFiberFuncPtr fn{nullptr}; |
| 221 | + |
| 222 | + // Check whether weak reference points to statically linked exit function |
| 223 | + if (nullptr != (fn = &::__asan_exit_fiber_weak)) { |
| 224 | + return fn; |
| 225 | + } |
| 226 | + |
| 227 | + // Check whether we can find a dynamically linked enter function |
| 228 | + if (nullptr != |
| 229 | + (fn = (AsanExitFiberFuncPtr)dlsym(RTLD_DEFAULT, "__asan_exit_fiber"))) { |
| 230 | + return fn; |
| 231 | + } |
| 232 | + |
| 233 | + // Couldn't find the function at all |
| 234 | + return nullptr; |
| 235 | +} |
| 236 | + |
| 237 | +#endif // FOLLY_SANITIZE_ADDRESS |
150 | 238 | }} |
0 commit comments