Skip to content

Update compiler-rt to LLVM 17.0.4 #20708

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 12 commits into from
Nov 16, 2023

Conversation

aheejin
Copy link
Member

@aheejin aheejin commented Nov 14, 2023

On top of the main library code changes, each fix that was necessary for Emscripten was committed separately with its own commit message.

`crtbegin.c` and `crtend.c` were originally in `compiler-rt/lib/crt`,
the directory we don't maintain, and moved to `compiler-rt/lib/builtins`
recently. So they were included in the `excludes` list.
This was introduced in
llvm/llvm-project@0a71e25,
which makes Wasm backend error out.
- lsan_common_emscripten.cpp: Function name changes
- lsan_common.cpp:
  The function structure has changed. Previously we had separate
  `ProcessRootRegion` and `ProcessRootRegions`, and Emscripten modified
  `ProcessRootRegion`. But in LLVM 17 `ProcessRootRegion` was deleted
  and merged into `ProcessRootRegions`. This fixes the code according to
  the new semantics.
These interceptors was added in LLVM 17, but it looks they have a wrong
return type. I submitted llvm/llvm-project#71253
to fix that upstream, but in the meantime we should fix this to pass our
tests.
We need these after `pthread_exit` LSan interceptor was added in
llvm/llvm-project@da7943b.
`__sanitizer::internal_mprotect` symbol produces a link-time error in
`MprotectReadWrite`, which was added in LLVM 17.
While I am not very familiar with this part of the code, it looks we're
already avoiding running it like these in the same file:
https://github.com/emscripten-core/emscripten/blob/8ecbdb3fc694f659aadb85a00d80777b20477281/system/lib/compiler-rt/lib/sanitizer_common/sanitizer_posix.cpp#L148-L152
https://github.com/emscripten-core/emscripten/blob/8ecbdb3fc694f659aadb85a00d80777b20477281/system/lib/compiler-rt/lib/sanitizer_common/sanitizer_posix.cpp#L156-L160
This does the same thing for `MprotectReadWrite`.
In LLVM 16, in `pthread_create` LSan interceptor, if `attr` is NULL, it
calls `pthread_attr_init` and initializes the `attr` with it, and then
calls `pthread_attr_getdetachstate`:
https://github.com/llvm/llvm-project/blob/7cbf1a2591520c2491aa35339f227775f4d3adf6/compiler-rt/lib/lsan/lsan_interceptors.cpp#L450-L456

In Emscripten, emscripten-core#15099 changes the `if` condition so that even if `attr`
is not NULL, if it is `__ATTRP_C11_THREAD`, we call `pthread_attr_init`.
`__ATTRP_C11_THREAD` looks like something used from musl, and is defined
as -1.
https://github.com/emscripten-core/emscripten/blob/5ce75b8828e3f50494c956d42f2bac301e41253b/system/lib/compiler-rt/lib/lsan/lsan_interceptors.cpp#L465

In LLVM 17, somehow the order of the `pthread_attr_init` and
`pthread_attr_getdetachstate` has swapped:
https://github.com/llvm/llvm-project/blob/309d55140c46384b6de7a7573206cbeba3f7077f/compiler-rt/lib/lsan/lsan_interceptors.cpp#L444-L453
So we don't get to call `pthread_attr_init` before calling
`pthread_attr_getdetachstate`. Even if the new code calls
`pthread_attr_getdetachstate` only when `attr` is not NULL, in our case
it didn't help because our `attr` was not NULL but `__ATTRP_C11_THREAD`.

This swaps the code order back to what it was in LLVM 16. This is
necessary to pass `lsan.test_pthread_c11_threads*`.

Drive-by fix: This also guards
`if (!attr || attr == __ATTRP_C11_THREAD)` condition with
`SANITIZER_EMSCRIPTEN`, which was an Emscripten-specific fix added
before.
@aheejin aheejin requested a review from sbc100 November 14, 2023 05:53
@aheejin
Copy link
Member Author

aheejin commented Nov 16, 2023

Ping 😀

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.

lgtm % comment

@@ -453,17 +453,32 @@ INTERCEPTOR(int, pthread_create, void *th, void *attr,
ENSURE_LSAN_INITED;
EnsureMainThreadIDIsCorrect();

#if !SANITIZER_EMSCRIPTEN
Copy link
Collaborator

Choose a reason for hiding this comment

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

I wonder if could just have a single if at the top of this function?

#if !SANITIZER_EMSCRIPTEN
// Treat __ATTRP_C11_THREAD like the null attr
if (attr == __ATTRP_C11_THREAD) attr = nullptr;
#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.

That's a good idea. Btw you meant #if SANITIZER_EMSCRIPTEN (without !), right?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yup

@aheejin aheejin merged commit 0e3e9b5 into emscripten-core:main Nov 16, 2023
@aheejin aheejin deleted the llvm_17_compiler_rt branch November 16, 2023 20:34
@sbc100
Copy link
Collaborator

sbc100 commented Nov 16, 2023

Oh, I forgot to say we should add something to the ChangeLog for this (and the libc++ change).

@aheejin
Copy link
Member Author

aheejin commented Nov 16, 2023

Oh, I forgot to say we should add something to the ChangeLog for this (and the libc++ change).

Done in #20736

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.

2 participants