0

Derived from llvm error when attempt to build libcxx instrumented with memory sanitizer

I am attempting to build LLVM with the libcxx and libcxxabi projects instrumented with MemorySanitizer (Msan) for debugging uninitialized memory usage in my C++ applications. However, I'm encountering errors during the build process.

Steps Taken

First, I executed the following CMake command successfully:

cmake -GNinja ../llvm \
    -DCMAKE_BUILD_TYPE=Release \
    -DLLVM_ENABLE_PROJECTS="libcxx;libcxxabi" \
    -DCMAKE_C_COMPILER=clang \
    -DCMAKE_CXX_COMPILER=clang++ \
    -DLLVM_USE_SANITIZER=MemoryWithOrigins

After running the command, I attempted to build LLVM using Ninja:

ninja -C ~/llvm-project/build -j2

Error Output Perhaps, I received the following MemorySanitizer error output:

#7 0x555f2def5c9c in llvm::RecordKeeper::addClass(std::unique_ptr<llvm::Record, std::default_delete<llvm::Record> >) /home/j/llvm-project/llvm/include/llvm/TableGen/Record.h:2027:24
#8 0x555f2def3f4 in llvm::TGParser::ParseClass() /home/j/llvm-project/llvm/lib/TableGen/TGParser.cpp:4008:13
#9 0x555f2def0bd7 in llvm::TGParser::ParseObject(llvm::MultiClass*) /home/j/llvm-project/llvm/lib/TableGen/TGParser.cpp:4377:12
#10 0x555f2df00c6a in ParseObjectList /home/j/llvm-project/llvm/lib/TableGen/TGParser.cpp:4389:9
#11 0x555f2df00c6a in llvm::TGParser::ParseFile() /home/j/llvm-project/llvm/lib/TableGen/TGParser.cpp:4398:7
#12 0x555f2ddb4772 in llvm::TableGenMain(char const*, std::function<bool (llvm::raw_ostream&, llvm::RecordKeeper const&)>) /home/j/llvm-project/llvm/lib/TableGen/Main.cpp:127:14
#13 0x555f2db78c32 in main /home/j/llvm-project/llvm/utils/TableGen/TableGen.cpp:81:10
#14 0x7ff357429d8f in __libc_start_call_main csu/../sysdeps/nptl/libc_start_call_main.h:58:16

SUMMARY: MemorySanitizer: use-of-uninitialized-value /usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/bits/stl_tree.h:1336:4 in _M_lower_bound_tr<llvm::StringRef, void>
Exiting
ninja: build stopped: subcommand failed.

Purpose of Building with MemorySanitizer

The only reason I am building LLVM with the line -DLLVM_USE_SANITIZER=MemoryWithOrigins is to fix the MemorySanitizer issue, as it throws an error for the use of uninitialized values. According to the MemorySanitizer GitHub page, building LLVM with this flag is necessary.

When I disable this flag, the build completes successfully, but I lose the ability to diagnose uninitialized memory issues.

Question

How can I successfully build LLVM with MemorySanitizer enabled without encountering these errors? Are there any specific flags or configurations I should try?

4
  • Re. "I'm encountering errors during the build process": what errors? Please show them in your question as text verbatim. Commented Oct 27, 2024 at 20:28
  • G.M, Msan errors in a body of initial question Commented Oct 27, 2024 at 20:31
  • G.M basically I get exactly same error I get if I use my current MSan build with my C++ applications, but according to MSan GitHub, flag 'DLLVM_USE_SANITIZER=MemoryWithOrigins ' is needed to fix MSan issues Commented Oct 27, 2024 at 20:36
  • after couple of hours with Docker just ended up with exact same errors 23.14 c++: error: unrecognized argument to '-fsanitize=' option: 'memory' 23.14 c++: error: unrecognized command line option '-fsanitize-memory-track-origins' 23.14 [2/5169] Building CXX object lib/Support/CMakeFiles/LLVMSupport.dir/ABIBreak.cpp.o Commented Oct 29, 2024 at 4:38

1 Answer 1

0

What has worked for me after hours of hard trying:

1.Freshly installed Ubuntu Jammy

2.All binaries built as requested by LLVM

 apt-get update && apt-get install -y \
            build-essential \
            ninja-build \
            clang \
            libunwind-dev \
            git \
            wget \
            && rm -rf /var/lib/apt/lists/*
    
    cd /tmp
    wget https://github.com/Kitware/CMake/releases/download/v3.24.0/cmake-3.24.0-linux-x86_64.tar.gz && \
    tar -zxvf cmake-3.24.0-linux-x86_64.tar.gz --strip-components=1 -C /usr/local 
    
    cd /llvm-project
    git clone --depth=1 https://github.com/llvm/llvm-project.git .
    
    cd /llvm-project/build
    RUN mkdir -p /llvm-project/build && cd /llvm-project/build && \
        CC=clang CXX=clang++ cmake -G Ninja \
        -DLLVM_ENABLE_PROJECTS="clang" \
        -DLLVM_ENABLE_RUNTIMES="libcxx;libcxxabi;libunwind" \
        -DLLVM_ENABLE_LIBCXX=ON \
        -DLLVM_ENABLE_LIBUNWIND=ON \
        -DCMAKE_BUILD_TYPE=Release \
        ../llvm && \
        ninja

3.All components must comply with specific llvm->clang needed. In my case v15 for both.

Also, guides at MemorySanitizer's GitHub page and LLVM main page are missing important part:

CC=clang CXX=clang++ which is quite game changer! After I've added that part, false positive errors thrown by MSan were eliminated.

Happy debugging!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.