Summary: detect_stack_use_after_return enables fake stack logic. Previous implementation was relying on fiber releasing its fake stack before switch, which is not safe, because that fake stack may actually be in use. This diff implements a hacky workaround by building a freeFakeStack function, which can release fake stack outside of the fiber. Ideally this function should be provided by ASAN itself.
Reviewed By: yfeldblum
Differential Revision:
D4631962
fbshipit-source-id:
6213e2a6b3cbc35fae3793fcf86b0614dd8ff825
Fiber::~Fiber() {
#ifdef FOLLY_SANITIZE_ADDRESS
+ if (asanFakeStack_ != nullptr) {
+ fiberManager_.freeFakeStack(asanFakeStack_);
+ }
fiberManager_.unpoisonFiberStack(this);
#endif
fiberManager_.stackAllocator_.deallocate(fiberStackLimit_, fiberStackSize_);
}
}
+void FiberManager::freeFakeStack(void* fakeStack) {
+ static AsanStartSwitchStackFuncPtr fnStart = getStartSwitchStackFunc();
+ static AsanFinishSwitchStackFuncPtr fnFinish = getFinishSwitchStackFunc();
+ if (fnStart == nullptr || fnFinish == nullptr) {
+ LOG(FATAL) << "The version of ASAN in use doesn't support fibers";
+ }
+
+ void* saveFakeStack;
+ const void* stackBottom;
+ size_t stackSize;
+ fnStart(&saveFakeStack, nullptr, 0);
+ fnFinish(fakeStack, &stackBottom, &stackSize);
+ fnStart(nullptr, stackBottom, stackSize);
+ fnFinish(saveFakeStack, nullptr, nullptr);
+}
+
void FiberManager::unpoisonFiberStack(const Fiber* fiber) {
auto stack = fiber->getStack();
DCHECK(fiber->asanMainStackBase_);
DCHECK(fiber->asanMainStackSize_);
- // Release fake stack if fiber is completed
- auto saveFakeStackPtr =
- fiber->state_ == Fiber::INVALID ? nullptr : &fiber->asanFakeStack_;
registerStartSwitchStackWithAsan(
- saveFakeStackPtr, fiber->asanMainStackBase_, fiber->asanMainStackSize_);
+ &fiber->asanFakeStack_,
+ fiber->asanMainStackBase_,
+ fiber->asanMainStackSize_);
SCOPE_EXIT {
registerFinishSwitchStackWithAsan(
fiber->asanFakeStack_,
void* fakeStack,
const void** saveStackBase,
size_t* saveStackSize);
+ void freeFakeStack(void* fakeStack);
void unpoisonFiberStack(const Fiber* fiber);
#endif // FOLLY_SANITIZE_ADDRESS