-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
[Clang-REPL] Fix crash during __run_exit_handlers
with dynamic libraries.
#117475
Conversation
…aries
@llvm/pr-subscribers-clang Author: None (SahilPatidar) ChangesApply the fix suggested by Lang Hames to address a crash in Clang-REPL that occurs during the execution of Full diff: https://github.com/llvm/llvm-project/pull/117475.diff 1 Files Affected:
diff --git a/clang/lib/Interpreter/Interpreter.cpp b/clang/lib/Interpreter/Interpreter.cpp
index 5dc67f6375098f..71299f7176e2fe 100644
--- a/clang/lib/Interpreter/Interpreter.cpp
+++ b/clang/lib/Interpreter/Interpreter.cpp
@@ -719,7 +719,9 @@ llvm::Error Interpreter::LoadDynamicLibrary(const char *name) {
if (auto DLSG = llvm::orc::DynamicLibrarySearchGenerator::Load(
name, DL.getGlobalPrefix()))
- EE->getMainJITDylib().addGenerator(std::move(*DLSG));
+ // FIXME: Eventually we should put each library in its own JITDylib and
+ // turn off process symbols by default.
+ EE->getProcessSymbolsJITDylib()->addGenerator(std::move(*DLSG));
else
return DLSG.takeError();
|
It is worth adding a test here. |
I have added a test, but it should only run on Darwin (macOS) platforms. // REQUIRES: host-supports-jit
// RUN: cat %s | env SDKROOT=$(xcrun --show-sdk-path) DYLD_LIBRARY_PATH=%S/Inputs:$DYLD_LIBRARY_PATH clang-repl
%lib vec.dylib
#include <vector>
std::vector<int> v;
%quit
|
In this case we will need an extra requires clause. Also I do not think we need the xcrun. There was some way to make a library out of yaml, you can look in the list of tests what it was. Maybe that’s useful. |
I came across the following setup: // REQUIRES: host-supports-jit
// RUN: rm -rf %t
// RUN: mkdir -p %t
//
// RUN: split-file %s %t
//
// RUN: %clang++ -std=c++20 -fPIC -c %t/vec.cpp -o %t/vec.o
// RUN: %clang++ -shared %t/vec.o -o %t/vec.dylib
//
// RUN: cat %t/Test.cpp | DYLD_LIBRARY_PATH=%t:$DYLD_LIBRARY_PATH clang-repl
//--- vec.cpp
#include <vector>
//--- Test.cpp
%lib vec.dylib
#include <vector>
std::vector<int> v;
%quit |
Hm, okay, would that work on linux? If so, then I guess we can follow that pattern. |
Would it be okay if I push the test for the Linux modification and check the results here? I don’t have access to a Linux x86 setup for testing. |
Yes, that's what the pre-merge checks are for ;) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
clang/test/Interpreter/crash.cpp
Outdated
#include <vector> | ||
|
||
//--- Test.cpp | ||
%lib vec.dylib |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
%lib vec.dylib | |
%lib vec.so |
This blocks some CI jobs on our end because we don't have a systemwide libc++ or libstdc++ installed. As a general rule, you can't just You can see how this is done in edbc6e9. You have to add a file like this: https://github.com/llvm/llvm-project/blob/main/clang/test/Modules/Inputs/PR28752/vector and pass something like |
Also |
30ad53b introduced a new test that includes `<vector>` from the system include path without honoring environment variables that may provide the path to C++ standard library. This is not supported in some CI systems because we don't always have the C++ library in the standard location. The conventional way of doing includes in the test is through `Inputs` directory and pass it as an include path. The `vector` file included in this patch has been shortened, but I have verified that it works with this test. i.e. the clang repl crashes on this test in the same way if the fix in #117475 is reverted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I landed my previous fix but I found that this test to be more problematic than I originally anticipated. I left some comments. I may send more patches to fix it until I am satisfied.
// RUN: split-file %s %t | ||
// | ||
// RUN: %clang++ -std=c++20 -fPIC -c %t/vec.cpp -o %t/vec.o | ||
// RUN: %clang++ -shared %t/vec.o -o %t/vec.so |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This invokes the system linker. From the build system's point of view, it's almost never correct to invoke the system linker without the environment variables $LDFLAGS
and $LIBS
. In this case, you expect to make the shared library to link against the default libs.
I am thinking of changing this line to read
// RUN: %clang++ -shared %t/vec.o -o %t/vec.so $LDFLAGS $LIBS
Not particularly satisfied with this approach though.
I also think that your vec.o
and vec.so
are both empty after my previous fix. This suggests that the two steps here may not even be needed at all for the sake of testing this fix.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps we should use this yaml-to-so tool to build the library.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps we should use this yaml-to-so tool to build the library.
Can you point me to the tool? A naive search returned nothing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If there's no quick solution, I would propose to revert the testing portion of this patch. This has been blocking CI on our end for a couple of days. No other tests in llvm repo invokes system linker like this one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The tools is called yaml2obj
. Alternatively we can upload the binary file which is my least favorite but still should get you going...
// RUN: %clang++ -std=c++20 -fPIC -c %t/vec.cpp -o %t/vec.o | ||
// RUN: %clang++ -shared %t/vec.o -o %t/vec.so | ||
// | ||
// RUN: cat %t/Test.cpp | LD_LIBRARY_PATH=%t:$LD_LIBRARY_PATH clang-repl |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How does clang-repl
respond to #include <vector>
if the path to the library is not provided on the command line? IIUC, this will continue to search for a systemwide installation of the C++ standard library. If that's the case, this continues to be wrong.
@SahilPatidar, could you help us with this? |
Can we temporarily disable this test when @SahilPatidar is looking for a solution? I am fine with a solution that doesn't invoke the system ld. |
Given the fix forward didn't work, I reverted this to unblock
as it seems to need some amount of work. Feel free to ping us so we can try this patch once you have something ready to confirm it fixes the build. |
Sorry for the delay. We'll come up with a solution, or perhaps we can use a direct binary as suggested by @vgvassilev, since we want to avoid invoking the system linker. |
Apply the fix suggested by Lang Hames to address a crash in Clang-REPL that occurs during the execution of
__run_exit_handlers
when using dynamic libraries.