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

Fix ASAN-enabled build #35364

Merged
merged 2 commits into from
Apr 8, 2020
Merged

Fix ASAN-enabled build #35364

merged 2 commits into from
Apr 8, 2020

Conversation

tkf
Copy link
Member

@tkf tkf commented Apr 5, 2020

I have no idea if it is the right fix (esp. NDEBUG) but this is what I needed to do to build julia with ASAN #35341. I'm sending a patch just in case it's an OK fix.

With this patch, I can run build julia with: ASAN_OPTIONS=detect_leaks=0:allow_user_segv_handler=1 make CC=$PWD/usr/tools/clang CXX=$PWD/usr/tools/clang LLVM_CONFIG=$PWD/usr/tools/llvm-config USECLANG=1 SANITIZE=1

cc @vchuravy @maleadt @Keno

close #35338

Make.inc Outdated Show resolved Hide resolved
@tkf
Copy link
Member Author

tkf commented Apr 5, 2020

By the way, it'd be nice if ASAN build (and some core tests with it?) is run via buildbot. It may find a bug like #35338 (comment)

@maleadt
Copy link
Member

maleadt commented Apr 6, 2020

I don't get the link error, are you sure your LLVM versions are matched up? FWIW, I just tried using a pretty hacky script of mine that uses Julia's build system to get an identical LLVM toolchain (this is from before BB, so maybe that step is redundant now, but It Works so I haven't updated it yet):

#!/bin/bash -uxe

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
ROOT="$DIR/.."

rm -rf $ROOT/deps/srccache/*/

## toolchain

BUILD="$DIR/sanitize_toolchain"

if [[ ! -d "${BUILD}" ]]; then

FORCE=1 make -C $ROOT configure O=$BUILD

cat <<EOD | sed 's/^ *//' >$BUILD/Make.user
    USE_BINARYBUILDER_LLVM=0

    # autotools doesn't build sanitizers
    override LLVM_USE_CMAKE=1

    # LLVM bug #23649
    USE_LLVM_SHLIB=0

    BUILD_LLVM_CLANG=1

    CMAKE_GENERATOR=Ninja
EOD
make -C $BUILD/deps install-llvm "$@"
mv $BUILD/usr/bin/* $BUILD/usr/tools

fi


## julia

BUILD="$DIR/sanitize"

rm -rf $BUILD
FORCE=1 make -C $ROOT configure O=$BUILD

cat <<'EOD' | sed 's/^ *//' >$BUILD/Make.user
    TOOLCHAIN=$(BUILDROOT)/../sanitize_toolchain/usr/tools

    # use our new toolchain
    USECLANG=1
    override CC=$(TOOLCHAIN)/clang
    override CXX=$(TOOLCHAIN)/clang++
    export ASAN_SYMBOLIZER_PATH=$(TOOLCHAIN)/llvm-symbolizer

    USE_BINARYBUILDER_LLVM=0

    # enable ASAN
    override LLVM_SANITIZE=1
    override SANITIZE=1

    # autotools doesn't have a self-sanitize mode
    override LLVM_USE_CMAKE=1

    # make the GC use regular malloc/frees, which are hooked by ASAN
    override WITH_GC_DEBUG_ENV=1

    # default to a debug build for better line number reporting
    override JULIA_BUILD_MODE=debug

    # enable LLVM assertions (which also enables pass debug output)
    override LLVM_ASSERTIONS=1

    # make ASAN consume less memory
    export ASAN_OPTIONS=detect_leaks=0:fast_unwind_on_malloc=0:allow_user_segv_handler=1:malloc_context_size=2

    CMAKE_GENERATOR=Ninja

    JULIA_PRECOMPILE=0
EOD
make -C $BUILD "$@"

Script expects to be put in and run from a build subdirectory in your Julia repo.

@Keno
Copy link
Member

Keno commented Apr 7, 2020

@tkf If you want to update this to drop the -DNDEBUG, I'll merge this and we can figure out the LLVM issue separately.

@tkf
Copy link
Member Author

tkf commented Apr 7, 2020

-DNDEBUG removed

@tkf
Copy link
Member Author

tkf commented Apr 7, 2020

@maleadt I get

...
    LINK usr/bin/julia
clang-9: warning: argument unused during compilation: '-mllvm -asan-stack=0' [-Wunused-command-line-argument]
/julia-ndebug/usr/lib/libjulia.so: undefined reference to `llvm::cfg::Update<llvm::BasicBlock*>::dump() const'
clang-9: error: linker command failed with exit code 1 (use -v to see invocation)
Makefile:86: recipe for target '/julia-ndebug/usr/bin/julia' failed
make[1]: *** [/julia-ndebug/usr/bin/julia] Error 1
Makefile:78: recipe for target 'julia-ui-release' failed
make: *** [julia-ui-release] Error 2

with

make -C deps install-llvm
tooldir=$PWD/usr/tools
ASAN_OPTIONS=detect_leaks=0:allow_user_segv_handler=1 PATH="$tooldir:$PATH" \
    make USECLANG=1 SANITIZE=1 CXX=$tooldir/clang

and

root@919704e44cd5:/julia-ndebug# git log -n1 --oneline
922891ccbf (HEAD -> ndebug, origin/master, origin/HEAD) Version `UnicodeData.txt` filename (#35383)
root@919704e44cd5:/julia-ndebug# git diff
diff --git a/src/aotcompile.cpp b/src/aotcompile.cpp
index 15623efe5a..c33010ccb0 100644
--- a/src/aotcompile.cpp
+++ b/src/aotcompile.cpp
@@ -23,6 +23,7 @@
 #include <llvm/Transforms/Vectorize.h>
 #if defined(JL_ASAN_ENABLED)
 #include <llvm/Transforms/Instrumentation.h>
+#include <llvm/Transforms/Instrumentation/AddressSanitizer.h>
 #endif
 #include <llvm/Transforms/Scalar/GVN.h>
 #include <llvm/Transforms/IPO/AlwaysInliner.h>
root@919704e44cd5:/julia-ndebug# cat Make.user
cat: Make.user: No such file or directory

Copy link
Member

@vchuravy vchuravy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got this as well:

/home/vchuravy/julia/src/aotcompile.cpp:586:13: error: use of undeclared identifier 'createAddressSanitizerFunctionPass'
    PM->add(createAddressSanitizerFunctionPass());
            ^

@Keno Keno merged commit 03dbe08 into JuliaLang:master Apr 8, 2020
ztultrebor pushed a commit to ztultrebor/julia that referenced this pull request Apr 17, 2020
* Fix ASAN-enabled build

* Remove -DNDEBUG
staticfloat pushed a commit that referenced this pull request Apr 21, 2020
* Fix ASAN-enabled build

* Remove -DNDEBUG
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

Successfully merging this pull request may close these issues.

ASAN build is broken in 1.5.0-DEV
4 participants