Skip to content

Building TinyUSDZ with Sanitizers

Syoyo Fujita edited this page Jan 6, 2025 · 4 revisions

ASAN(Address sanitizer)

Specify

-DSANITIZE_ADDRESS=1

to cmake args

UBSan(Undefined sanitizer)

Specify

-DSANITIZE_UNDEFINED=1

to cmake args

TSAN(Thread sanitizer)

Specify

-DSANITIZE_THREAD=1

to cmake args

MSAN(Memory Sanitizer)

Building TinyUSDZ with MSAN(MemorySanitizer) support requires extra work. We need to compile all source code with MSAN support, including C++ STL(libcxx).

Requirements

  • clang(17 or later required. Otherwise libcxx cannot be compiled(at least for llvm v19.1.0))
  • llvm runtime source codes(libcxx, libcxxabi, libunwind)
    • 19.1.0

References

Install clang(17 or later)

Install clang. In ubuntu you may be able to install clang by

$ sudo apt install clang-18 libclang-18-dev

Build libcxx

Handy build script is provided: https://github.com/lighttransport/tinyusdz/blob/dev/scripts/build_msan_libcxx.sh

$ cd <tinyusdz-repo>
$ ./scripts/build_libcxx_msan.sh

Details of the script.

Checkout llvm-project source code(version 19.1.0)

git clone --depth=1 https://github.com/llvm/llvm-project -b llvmorg-19.1.0

Bootstrap libcxx, libcxxabi and libunwind with cmake (Specify which runtime module to build through LLVM_ENABLE_RUNTIMES)

CXX=clang++-18 CC=clang-18 cmake -G Ninja -B ${build_dir} -S llvm-project/runtimes/ \
  -DCMAKE_INSTALL_PREFIX=${dist_dir} \
  -DLLVM_ENABLE_RUNTIMES="libcxx;libcxxabi;libunwind" \
  -DLLVM_USE_SANITIZER=MemoryFromOrigins \
  -DCMAKE_BUILD_TYPE=Release

Then build & install libcxx with Ninja.

Build TinyUSDZ with msan-enabled libcxx

See https://github.com/lighttransport/tinyusdz/blob/dev/scripts/bootstrap-cmake-linux-msan.sh for the script.

Set CXX and CC as follows:

export CXX="clang++-18 -fsanitize=memory -fsanitize-memory-track-origins -fno-omit-frame-pointer "
export CC="clang-18 -fsanitize=memory -fsanitize-memory-track-origins -fno-omit-frame-pointer "

Use CMAKE_CXX_FLAGS to specify msan sanitizer(-DSANITIZER_MEMORY=1 does not work) and set include path to msan-enabled libcxx

-DCMAKE_CXX_FLAGS="-fsanitize=memory -fno-omit-frame-pointer -fsanitize-memory-track-origins -isystem ${LIBCXX_MSAN_DIR}/include -isystem ${LIBCXX_MSAN_DIR}/include/c++/v1 -nostdinc++ 

And use CMAKE_EXE_LINKER_FLAGS to specify lib path and libraries to be linked.

-DCMAKE_EXE_LINKER_FLAGS="-Wl,-rpath,${LIBCXX_MSAN_DIR}/lib -lc++ -lc++abi -lunwind "

Also set -DTINYUSDZ_MSAN=1 recommended.


Clone this wiki locally