Skip to content

Conversation

@gbaraldi
Copy link
Member

This commit improves AddressSanitizer (ASAN) support with several fixes:

  1. Fix race condition in gc_scrub_record_task (gc-debug.c)

    • Multiple GC threads could concurrently call gc_scrub_record_task
      during the mark phase, causing data races on jl_gc_debug_tasks
    • Added pthread_mutex_t with static initialization to protect the
      arraylist operations
  2. Add jl_is_asanbuild() API function (jlapi.c, julia.h)

    • New function to detect at runtime if Julia was built with ASAN
    • Exported for use in Julia code
  3. Add ASAN library path detection for pkgimage linking (linking.jl)

    • On macOS, pkgimages need to link against libclang_rt.asan_osx_dynamic
    • Uses Libdl.dlopen with RTLD_NOLOAD to find the already-loaded ASAN
      runtime library path without requiring environment variables
  4. Add automatic ASAN options for LLVM compatibility (init.c, Make.inc)

    • When Julia is built with ASAN but LLVM is not (common with
      BinaryBuilder LLVM), std::vector container overflow detection
      causes false positives due to mixing instrumented and
      non-instrumented code
    • Added __asan_default_options() to automatically disable
      detect_container_overflow when LLVM lacks ASAN
    • Added LLVM_SANITIZE=1 build option to indicate LLVM has sanitizers

Co-Authored-By: Claude Opus 4.5 [email protected]

@gbaraldi gbaraldi requested review from Keno and xal-0 and removed request for d-netto, qinsoon and udesou January 16, 2026 18:57
@xal-0
Copy link
Member

xal-0 commented Jan 16, 2026

Does this build on your machine?

@gbaraldi
Copy link
Member Author

After the push it does

@gbaraldi gbaraldi force-pushed the gb/macos-asan branch 2 times, most recently from f3872b8 to 46ab024 Compare January 16, 2026 20:12
@IanButterworth
Copy link
Member

Add a job to the buildkite CI?

@gbaraldi
Copy link
Member Author

That's the plan

fi

make -C "$TOOLCHAIN/deps" install-clang install-llvm-tools install-patchelf
make -C "$TOOLCHAIN/deps" install-clang install-llvm-tools install-patchelf install-zstd
Copy link
Member

Choose a reason for hiding this comment

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

We should install-zlib too, since our LLVM links against it.

Comment on lines +549 to +562
/**
* @brief Check if Julia is built with AddressSanitizer (ASAN) enabled.
*
* @return Returns 1 if Julia is built with ASAN, 0 otherwise.
*/
JL_DLLEXPORT int jl_is_asanbuild(void) JL_NOTSAFEPOINT
{
#ifdef _COMPILER_ASAN_ENABLED_
return 1;
#else
return 0;
#endif
}

Copy link
Member

Choose a reason for hiding this comment

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

Should use jl_options.target_sanitize_address?

This commit improves AddressSanitizer (ASAN) support with several fixes:

1. Fix race condition in gc_scrub_record_task (gc-debug.c)
   - Multiple GC threads could concurrently call gc_scrub_record_task
     during the mark phase, causing data races on jl_gc_debug_tasks
   - Added pthread_mutex_t with static initialization to protect the
     arraylist operations

2. Add jl_is_asanbuild() API function (jlapi.c, julia.h)
   - New function to detect at runtime if Julia was built with ASAN
   - Exported for use in Julia code

3. Add ASAN library path detection for pkgimage linking (linking.jl)
   - On macOS, pkgimages need to link against libclang_rt.asan_osx_dynamic
   - Uses Libdl.dlopen with RTLD_NOLOAD to find the already-loaded ASAN
     runtime library path without requiring environment variables

4. Add automatic ASAN options for LLVM compatibility (init.c, Make.inc)
   - When Julia is built with ASAN but LLVM is not (common with
     BinaryBuilder LLVM), std::vector container overflow detection
     causes false positives due to mixing instrumented and
     non-instrumented code
   - Added __asan_default_options() to automatically disable
     detect_container_overflow when LLVM lacks ASAN
   - Added LLVM_SANITIZE=1 build option to indicate LLVM has sanitizers

5. Add __GLIBC__ guard for ELF-specific sanitizer code (dlload.c)
   - The link_map struct and related code is glibc-specific

6. Fix flisp Makefile for Darwin (flisp/Makefile)
   - Static AddressSanitizer runtime is not supported on Darwin
   - Skip -static-libsan flag on macOS

7. Add jmpbuf_sp implementation for macOS aarch64 (julia_internal.h)
   - Required for ASAN stack unpoisoning on Apple Silicon
   - Implements pointer demangling using Apple's TSD mechanism

8. Disable ASAN global ctor/dtor for JITLink compatibility (pipeline.cpp)
   - ASAN module constructors/destructors that register globals are not
     compatible with JITLink on ARM64 due to Page21 relocation range limits
   - Stack/heap/use-after-free detection still works

Co-Authored-By: Claude Opus 4.5 <[email protected]>
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.

4 participants