Fix ASan stack range registration for main task#3298
Open
chenBright wants to merge 1 commit into
Open
Conversation
…requested __asan_handle_no_return"
On Linux, `pthread_attr_getstack(3)` returns the lowest address of the
stack, but brpc treats `StackStorage::bottom` as the highest address
(see `bthread/stack.h`). `TaskGroup::init` was registering the main
task's stack with the lowest address as `bottom`, so every per-jump
`BTHREAD_SCOPED_ASAN_FIBER_SWITCHER` told ASan a stack range
`stacksize` bytes below the real one. The first
`__asan_handle_no_return` (e.g. the C++ unwinder fired by
`throw bthread::ExitException` inside `bthread_exit`) then prints
WARNING: ASan is ignoring requested __asan_handle_no_return:
stack type: default top: ...; bottom ...; size: 0x62bdb000 (~1.5 GiB)
False positive error reports may follow
Fix `PthreadAttrGetStack` to translate the lowest address to the
highest address before returning, matching `StackStorage::bottom`.
ASan-only path; non-ASan builds are unaffected.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What problem does this PR solve?
Issue Number: resolve
Problem Summary:
Under ASan, every
bthread_exitfrom a non-main task (i.e. thethrow bthread::ExitException(retval)branch) reliably emits thefollowing warning to stderr, polluting test output and confusing future
ASan reports:
(
top - bottom ≈ 1.5 GiBis impossible for any real stack: brpc'sbiggest bthread stack is
STACK_TYPE_LARGE, an order-of-magnitudesmaller, and a default pthread stack is only a few MB.)
Root cause: brpc's
StackStorage::bottomis, by convention, thehighest address of the stack (see
bthread/stack.hcomment "Assumestack grows upwards"; stack.cpp confirms with
s->bottom = (char*)mem + stacksize). However, in the ASan-only branchof
TaskGroup::init, the value passed tostk->storage.bottomfor themain task came straight from
PthreadAttrGetStack, which on Linux usespthread_attr_getstack(3)— and that returns the lowest address ofthe region. The convention was therefore violated for the main task.
internal::StartSwitchFibercomputesasan_stack_bottom = (char*)storage.bottom - storage.stacksizeandpasses it to
__sanitizer_start_switch_fiber. With the conventionviolated, this address was
stacksizebytes below the real pthreadstack, so the per-jump
BTHREAD_SCOPED_ASAN_FIBER_SWITCHERtold ASanthat the main task lives in nonsensical memory. From that point on,
ASan's per-thread fake-stack tracking was inconsistent with reality.
The first time the C++ unwinder fired (e.g. from
throw bthread::ExitException),__asan_handle_no_returnsaw a stackrange that straddles the bthread stack and the bogus "main" stack and
bailed out with the warning above.
What is changed and the side effects?
Changed:
PthreadAttrGetStacknow translatesthe lowest address returned by
pthread_attr_getstack(3)into ahighest address before returning, so callers can keep treating the
output as a
StackStorage::bottom. macOS already returns the stackbase (highest address) via
pthread_get_stackaddr_np(3)and isunchanged. A comment is added describing the convention.
Before fix:
After fix:
Side effects:
Performance effects:
Breaking backward compatibility:
Check List: