Skip to content
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

[wiki] Outdated instructions on how to build libcxx instructed with MSAN #1685

Open
Wentzell opened this issue Sep 20, 2023 · 7 comments
Open

Comments

@Wentzell
Copy link

The wiki instructions on how to build libcxx instructed with MSAN are outdated.
The current article still suggests using LLVM_ENABLE_PROJECTS which is no longer available.

# clone LLVM
git clone --depth=1 https://github.com/llvm/llvm-project
cd llvm-project
mkdir build; cd build
# configure cmake
cmake -GNinja ../llvm \
	-DCMAKE_BUILD_TYPE=Release \
	-DLLVM_ENABLE_PROJECTS="libcxx;libcxxabi" \
	-DCMAKE_C_COMPILER=clang \
	-DCMAKE_CXX_COMPILER=clang++ \
	-DLLVM_USE_SANITIZER=MemoryWithOrigins
# build the libraries
cmake --build . -- cxx cxxabi

Using instead LLVM_ENABLE_RUNTIMES has not been successful for me.
Could you please update the article page for the latest releases?

@yugr
Copy link

yugr commented Sep 20, 2023

Just in case, this gist has worked for me a year ago.

@Wentzell
Copy link
Author

Thank you @yugr for the suggestion. Unfortunately this does not work for me.
See my setup at https://github.com/TRIQS/triqs/blob/DEV_MSAN/packaging/Dockerfile.msan

The libcxx build fails with a MemorySanitizer positive

110.5 [234/1473] Building IntrinsicEnums.inc...
110.5 FAILED: include/llvm/IR/IntrinsicEnums.inc /tmp/llvm_build/include/llvm/IR/IntrinsicEnums.inc
110.5 cd /tmp/llvm_build && /tmp/llvm_build/bin/llvm-min-tblgen -gen-intrinsic-enums -I /tmp/llvm_src/llvm/include/llvm/IR -I/tmp/llvm_build/include -I/tmp/llvm_src/llvm/include /tmp/llvm_src/llvm/include/llvm/IR/Intrinsics.td --wr
ite-if-changed -o include/llvm/IR/IntrinsicEnums.inc -d include/llvm/IR/IntrinsicEnums.inc.d
110.5 ==1974==WARNING: MemorySanitizer: use-of-uninitialized-value
110.5     #0 0x560ccd4a15a8 in std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>::_M_is_local() const /usr/bin/../lib/gcc/x86_64-linux-gnu/12/../../../../include/c++/12/bits/basic_string.h:275:6
110.5     #1 0x560ccd4a15a8 in std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>::operator=(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>&&) /usr/bin/../lib/gcc/x86_64-l
inux-gnu/12/../../../../include/c++/12/bits/basic_string.h:867:12
110.5     #2 0x560ccd4a15a8 in (anonymous namespace)::CommandLineParser::ParseCommandLineOptions(int, char const* const*, llvm::StringRef, llvm::raw_ostream*, bool) /tmp/llvm_src/llvm/lib/Support/CommandLine.cpp:1520:15
110.5     #3 0x560ccd4a15a8 in llvm::cl::ParseCommandLineOptions(int, char const* const*, llvm::StringRef, llvm::raw_ostream*, char const*, bool) /tmp/llvm_src/llvm/lib/Support/CommandLine.cpp:1469:24
110.5     #4 0x560ccd484cce in main /tmp/llvm_src/llvm/utils/TableGen/TableGen.cpp:82:3
110.5     #5 0x7f86b7615a8f  (/lib/x86_64-linux-gnu/libc.so.6+0x23a8f) (BuildId: bdb8aa3b1b60f9d43e1c70ba98158e05f765efdc)
110.5     #6 0x7f86b7615b48 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x23b48) (BuildId: bdb8aa3b1b60f9d43e1c70ba98158e05f765efdc)
110.5     #7 0x560ccd36a844 in _start (/tmp/llvm_build/bin/llvm-min-tblgen+0x42844) (BuildId: edc733bed5cf109b1f4aa4060fc31e5c4b9d77d1)
110.5
110.5   Uninitialized value was created by an allocation of 'ref.tmp12.i' in the stack frame
110.5     #0 0x560ccd4983a1 in (anonymous namespace)::CommandLineParser::ParseCommandLineOptions(int, char const* const*, llvm::StringRef, llvm::raw_ostream*, bool) /tmp/llvm_src/llvm/lib/Support/CommandLine.cpp:1520:17
110.5     #1 0x560ccd4983a1 in llvm::cl::ParseCommandLineOptions(int, char const* const*, llvm::StringRef, llvm::raw_ostream*, char const*, bool) /tmp/llvm_src/llvm/lib/Support/CommandLine.cpp:1469:24
110.5
110.5 SUMMARY: MemorySanitizer: use-of-uninitialized-value /usr/bin/../lib/gcc/x86_64-linux-gnu/12/../../../../include/c++/12/bits/basic_string.h:275:6 in std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char
>>::_M_is_local() const

@yugr
Copy link

yugr commented Sep 23, 2023

So what happens here is that build instructions which I provided perform a bootstrap compile - i.e. they first build clang and then use it to build the libraries. Compiler (and its build tools, llvm-tblgen in this case) are memory-sanitized but linked against unsanitized host STL library which results in spurious errors from Msan when compiler/tools are run.

It seems this is a known problem: https://stackoverflow.com/a/73827100/2170527 . Following SO instructions I was able to successfully build libcxx/libcxxabi (needed to install clang-15 first):

$ CC=clang-15 CXX=clang++-15 cmake -G Ninja -S runtimes -B msan_out -DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_RUNTIMES="libcxx;libcxxabi" -DLLVM_USE_SANITIZER=MemoryWithOrigins
$ ninja -C msan_out

@vitalybuka
Copy link
Contributor

There is buildbot https://lab.llvm.org/buildbot/#/builders/sanitizer-x86_64-linux-bootstrap-msan

stage2 is libcxx and full clang instrumented and tested with msan

This is how you ca reproduce the bot on your workstation/containter: https://github.com/google/sanitizers/wiki/SanitizerBotReproduceBuild

Or you can try to extract cmake/ninja commands from the bot output https://lab.llvm.org/buildbot/#/builders/74/builds/22184/steps/2/logs/stdio

@yugr
Copy link

yugr commented Sep 23, 2023

Or you can try to extract cmake/ninja commands from the bot output https://lab.llvm.org/buildbot/#/builders/74/builds/22184/steps/2/logs/stdio

Looks like it matches the SO answer I linked:

cmake -DLLVM_APPEND_VC_REV=OFF -GNinja -DCMAKE_BUILD_TYPE=Release -DLLVM_CCACHE_BUILD=ON -DLLVM_USE_LINKER=lld -DLLVM_ENABLE_ASSERTIONS=ON -DLLVM_ENABLE_PER_TARGET_RUNTIME_DIR=OFF -DCMAKE_C_COMPILER=/b/sanitizer-x86_64-linux-bootstrap-msan/build/llvm_build0/bin/clang -DCMAKE_CXX_COMPILER=/b/sanitizer-x86_64-linux-bootstrap-msan/build/llvm_build0/bin/clang++ '-DLLVM_ENABLE_RUNTIMES=libcxx;libcxxabi' -DLLVM_USE_SANITIZER=Memory '-DCMAKE_C_FLAGS=-fsanitize=memory  ' '-DCMAKE_CXX_FLAGS=-fsanitize=memory  ' /b/sanitizer-x86_64-linux-bootstrap-msan/build/llvm-project/llvm/../runtimes

@Wentzell
Copy link
Author

So what happens here is that build instructions which I provided perform a bootstrap compile - i.e. they first build clang and then use it to build the libraries. Compiler (and its build tools, llvm-tblgen in this case) are memory-sanitized but linked against unsanitized host STL library which results in spurious errors from Msan when compiler/tools are run.

It seems this is a known problem: https://stackoverflow.com/a/73827100/2170527 . Following SO instructions I was able to successfully build libcxx/libcxxabi (needed to install clang-15 first):

$ CC=clang-15 CXX=clang++-15 cmake -G Ninja -S runtimes -B msan_out -DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_RUNTIMES="libcxx;libcxxabi" -DLLVM_USE_SANITIZER=MemoryWithOrigins
$ ninja -C msan_out

Thank you for pointing out the SO post. Indeed I was only missing the -S runtimes to avoid bootstrapping the compiler.
It would be great if the wiki could be updated accordingly.

@yugr
Copy link

yugr commented Sep 25, 2023

It would be great if the wiki could be updated accordingly.

@kcc @ramosian-glider @eugenis FYI

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

No branches or pull requests

3 participants