Conservative-Clang: An early LLVM pass which removes most of UB-implying information.
If you are fighting with C/C++ undefined behavior (or clang miscompilation bugs), please try this tool :)
Before using the pass, please try with the following compilation flags in clang-flags.txt:
-fwrapv -fno-check-new -fno-delete-null-pointer-checks -fno-strict-aliasing -fno-strict-enums -fno-strict-float-cast-overflow -fno-strict-return -fno-strict-vtable-pointers -fno-struct-path-tbaa -fno-pointer-tbaa -fno-assume-nothrow-exception-dtor -fno-assume-sane-operator-new -fno-assume-unique-vtables -fno-assumptions -fno-finite-loops -fno-common -mno-global-merge -fno-merge-all-constants
If this "magical formula" does not work, please try the LLVM pass.
This work is inspired by Daniel J. Bernstein's blog. I hope this work can unblock your project at the prototype stage.
sudo apt install llvm-20-dev # Depends on your clang version
git clone https://github.com/dtcxzyw/conservative-clang.git
cd conservative-clang
mkdir -p build && cd build
cmake .. -DCMAKE_BUILD_TYPE=Release
cmake --build . -j
export CFLAGS=-fpass-plugin=<path to conservative-clang>/build/cclang.so
export CXXFLAGS=-fpass-plugin=<path to conservative-clang>/build/cclang.so
# Configure & build your project
If you are using CMake, please pass -DCMAKE_C_FLAGS=-fpass-plugin=<path to conservative-clang>/build/cclang.so -DCMAKE_CXX_FLAGS=-fpass-plugin=<path to conservative-clang>/build/cclang.so
to CMake.
I did some performance evaluation on llvm test-suite. This pass can result in ~10% performance degradation on average.
This project is licensed under the Apache License 2.0. Please see the LICENSE for details.