Skip to content

[EH] Use _UA_SEARCH_PHASE in personality function (NFC) #17991

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 5, 2022

Conversation

aheejin
Copy link
Member

@aheejin aheejin commented Oct 5, 2022

In two-phase unwinding, the first phase is the search phase (_UA_SEARCH_PHASE) and the second one is the cleanup phase (_UA_CLEANUP_PHASE). The search phase searches up the stack to see if there is a matching catch handler, and if it finds one, it caches the result. And in the second cleanup phase, it retrieves the cached result (to avoid doing same work twice) and unwinds the stack.

Wasm does not do the two-phase unwinding; it only has a single phase. We used _UA_CLEANUP_PHASE for this single phase, so in Wasm the cleanup phase is supposed to the search. So we several many custom #ifdefs to use the code guarded by _UA_SEARCH_PHASE, for example: https://github.com/aheejin/emscripten/blob/d57db5bea1719319a680699c50b91fa3d88fa0ec/system/lib/libcxxabi/src/cxa_personality.cpp#L771-L776 https://github.com/aheejin/emscripten/blob/d57db5bea1719319a680699c50b91fa3d88fa0ec/system/lib/libcxxabi/src/cxa_personality.cpp#L850-L855

These are apparently gone in #14288, which replaced many ifs with asserts. This in effect removed our special handling for _UA_CLEANUP_PHASE; there are several asserts that asserts the current phase is _UA_SEARCH_PHASE, while Wasm is in _UA_CLEANUP_PHASE. But this has not caused problems so far because we have built libc++abi with -NDEBUG, so all assertions were no-op.

cflags.append('-DNDEBUG')
But this is now a problem because #17979 adds a debug build of libc++abi, which enables assertions.

Come to think of it, I'm not sure why I decided to use _UA_CLEANUP_PHASE for our single phase in the first place. If we use _UA_SEARCH_PHASE, we can remove more our custom code and reduce the difference between our port and the upstream library.

In two-phase unwinding, the first phase is the search phase
(`_UA_SEARCH_PHASE`) and the second one is the cleanup phase
(`_UA_CLEANUP_PHASE`). The search phase searches up the stack to see if
there is a matching catch handler, and if it finds one, it caches the
result. And in the second cleanup phase, it retrieves the cached result
(to avoid doing same work twice) and unwinds the stack.

Wasm does not do the two-phase unwinding; it only has a single phase. We
used `_UA_CLEANUP_PHASE` for this single phase, so in Wasm the cleanup
phase is supposed to the search. So we several many custom `#ifdef`s to
use the code guarded by `_UA_SEARCH_PHASE`, for example:
https://github.com/aheejin/emscripten/blob/d57db5bea1719319a680699c50b91fa3d88fa0ec/system/lib/libcxxabi/src/cxa_personality.cpp#L771-L776
https://github.com/aheejin/emscripten/blob/d57db5bea1719319a680699c50b91fa3d88fa0ec/system/lib/libcxxabi/src/cxa_personality.cpp#L850-L855

These are apparently gone in emscripten-core#14288, which replaced many `if`s with
`assert`s. This in effect removed our special handling for
`_UA_CLEANUP_PHASE`; there are several `assert`s that asserts the
current phase is `_UA_SEARCH_PHASE`, while Wasm is in
`_UA_CLEANUP_PHASE`. But this has not caused problems so far because we
have built libc++abi with `-NDEBUG`, so all assertions were no-op.
https://github.com/emscripten-core/emscripten/blob/40fb7d2071e439f1de614898b88518df582faa94/tools/system_libs.py#L1366
But this is now a problem because emscripten-core#17979 adds a debug build of
libc++abi, which enables assertions.

Come to think of it, I'm not sure why I decided to use
`_UA_CLEANUP_PHASE` for our single phase in the first place. If we use
`_UA_SEARCH_PHASE`, we can remove more our custom code and reduce the
difference between our port and the upstream library.
@aheejin aheejin requested review from sbc100 and dschuff October 5, 2022 02:09
Copy link
Collaborator

@sbc100 sbc100 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't fully understand the code but less code sounds good to me!

Comment on lines +969 to +973
#ifdef __USING_WASM_EXCEPTIONS__
// Wasm only uses a single phase (_UA_SEARCH_PHASE), so save the
// results here.
set_registers(unwind_exception, context, results);
#endif
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is to save our selector computation result.

In the upstream version, the search phase searches for a matching catch handler and caches the result, and the second cleanup phase retrieves the result and sets registers with the final selector value. (In Wasm's libunwind, they are not registers but just a field in a struct.). They do it in the second cleanup phase:

set_registers(unwind_exception, context, results);

But now we only use the search phase, we do it here. Instead, we are able to remove our duplicated caching code in line 984-993 below.

Comment on lines -984 to -993
#ifdef __USING_WASM_EXCEPTIONS__
// Wasm uses only one phase in _UA_CLEANUP_PHASE, so we should set
// these here.
__cxa_exception* exception_header = (__cxa_exception*)(unwind_exception+1) - 1;
exception_header->handlerSwitchValue = static_cast<int>(results.ttypeIndex);
exception_header->actionRecord = results.actionRecord;
exception_header->languageSpecificData = results.languageSpecificData;
exception_header->catchTemp = reinterpret_cast<void*>(results.landingPad);
exception_header->adjustedPtr = results.adjustedPtr;
#endif
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is what the upstream code does in the first search phase, but we used only the cleanup phase, we duplicated the code here. Now we can remove it.

Comment on lines +801 to +802
assert(actions & _UA_SEARCH_PHASE);
results.ttypeIndex = ttypeIndex;
Copy link
Member Author

@aheejin aheejin Oct 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not related to this PR, but I compared our cxa_personality.cpp with llvm 14.0.0 version (on which our current libc++abi is based on), this part is missing. I guess it was dropped when we upgraded the library. I don't think Wasm is using this part of the code though, reducing difference from the upstream code seems better.

Copy link
Member

@dschuff dschuff left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM too.

@aheejin aheejin merged commit aba07b5 into emscripten-core:main Oct 5, 2022
@aheejin aheejin deleted the eh_search_phase branch October 5, 2022 22:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants