Segmentation fault when running check-clang-python with UBSan
| Bugzilla Link | 39559 |
| Resolution | WORKSFORME |
| Resolved on | Jan 25, 2021 15:06 |
| Version | trunk |
| OS | Linux |
| CC | @vabridgers |
Extended Description
When running check-clang-python on trunk (r346128) with a LLVM_USE_SANITIZER=Undefined build, I see a segmentation fault.
$ env CLANG_LIBRARY_PATH=../../../../build-sanu/lib /usr/bin/python2.7 -m unittest discover -v testConstantArray (tests.cindex.test_type.TestType) ... ERROR test_a_struct (tests.cindex.test_type.TestType) ... Segmentation fault (core dumped)
LLVM was built, and the test was run, using:
export LLVM_ROOT=/path/to/llvm-7.0/build/
mkdir build-sanu
pushd build-sanu
cmake -G Ninja
-DCMAKE_BUILD_TYPE=Release
-DLLVM_ENABLE_ASSERTIONS=ON
-DCMAKE_C_COMPILER=$LLVM_ROOT/bin/clang
-DCMAKE_CXX_COMPILER=$LLVM_ROOT/bin/clang++
-DLLVM_USE_SANITIZER=Undefined
../.
popd
ninja -C build-sanu check-clang-python
The crash seems to occur in the following call to getenv() in clang_createIndex():
3247 // We use crash recovery to make some of our APIs more reliable, implicitly 3248 // enable it.
3249 if (!getenv("LIBCLANG_DISABLE_CRASH_RECOVERY")) 3250 llvm::CrashRecoveryContext::Enable();
Backtrace (before stepi that results in segmentation fault):
(gdb) bt #0 0x00007fffee590d80 in getenv@plt () from /llvm/build-sanu/lib/libclang.so #1 0x00007fffee5f9491 in clang_createIndex () at ../tools/clang/tools/libclang/CIndex.cpp:3249 #2 0x00007ffff65dfc7c in ffi_call_unix64 () from /usr/lib/x86_64-linux-gnu/libffi.so.6 #3 0x00007ffff65df5ac in ffi_call () from /usr/lib/x86_64-linux-gnu/libffi.so.6 #4 0x00007ffff67f65fe in _ctypes_callproc () from /usr/lib/python2.7/lib-dynload/_ctypes.x86_64-linux-gnu.so #5 0x00007ffff67f7f9e in ?? () from /usr/lib/python2.7/lib-dynload/_ctypes.x86_64-linux-gnu.so #6 0x00000000004c84a5 in PyEval_EvalFrameEx ()
The segmentation fault seems to be due to the PLT entry for getenv() being broken (?):
(gdb) disas $pc Dump of assembler code for function getenv@plt: => 0x00007fffee590d80 <+0>: jmpq *0x62e2292(%rip) # 0x7ffff4873018 <GLOBAL_OFFSET_TABLE+48> 0x00007fffee590d86 <+6>: pushq $0x3 0x00007fffee590d8b <+11>: jmpq 0x7fffee590d40 End of assembler dump.
(gdb) x (void *) 0x7ffff4873018 0x7ffff4873018 <GLOBAL_OFFSET_TABLE+48>: 0x026a7d86
(gdb) x 0x026a7d86 0x26a7d86: Cannot access memory at address 0x26a7d86
This was seen when running on a Ubuntu 14.04.5 LTS installation. I have unfortunately not been able to reproduce this on another machine yet.
I have tried building LLVM with a Clang 5.0 and a Clang 7.0 installation, with the same results.
I looked into this, and am not able to repro under currently documented build environment. I was able to repro under very particular build/run environments, so I suspect David encountered one or the other issue that leads to this segfault. This problem can occur due to 1 of the following reasons:
- LLVM compiler built with LLVM_ENABLE_PIC=on. There was a patch created to disable Python bindings under these conditions at some point. Perhaps this was the problem at the time.
- Perhaps a problem with the Python version used - I'm guessing this was the problem in the original report.
This problem has been reported a few times, and there have been different approaches to addressing this. If a reader comes across this issue looking for a solution, please look at those and also check out the following references.
References …
Python binding tests disabled when LLVM_ENABLE_PIC=OFF https://lists.llvm.org/pipermail/cfe-commits/Week-of-Mon-20190114/257720.html
Appears to be caused by a regression in Python. See https://bugs.python.org/issue37140