diff --git a/Makefile b/Makefile index d2940c28196f95..bf4a6c98644e64 100644 --- a/Makefile +++ b/Makefile @@ -1301,6 +1301,7 @@ jsc-build-mac-compile-lto: .PHONY: jsc-build-mac-compile-debug jsc-build-mac-compile-debug: mkdir -p $(WEBKIT_DEBUG_DIR) $(WEBKIT_DIR); + # to disable asan, remove -DENABLE_SANITIZERS=address and add -DENABLE_MALLOC_HEAP_BREAKDOWN=ON cd $(WEBKIT_DEBUG_DIR) && \ ICU_INCLUDE_DIRS="$(HOMEBREW_PREFIX)opt/icu4c/include" \ cmake \ @@ -1309,7 +1310,6 @@ jsc-build-mac-compile-debug: -DCMAKE_BUILD_TYPE=Debug \ -DUSE_THIN_ARCHIVES=OFF \ -DENABLE_FTL_JIT=ON \ - -DENABLE_MALLOC_HEAP_BREAKDOWN=ON \ -DCMAKE_EXPORT_COMPILE_COMMANDS=ON \ -DUSE_BUN_JSC_ADDITIONS=ON \ -DUSE_BUN_EVENT_LOOP=ON \ @@ -1321,6 +1321,7 @@ jsc-build-mac-compile-debug: -DUSE_PTHREAD_JIT_PERMISSIONS_API=ON \ -DENABLE_REMOTE_INSPECTOR=ON \ -DUSE_VISIBILITY_ATTRIBUTE=1 \ + -DENABLE_SANITIZERS=address \ $(WEBKIT_DIR) \ $(WEBKIT_DEBUG_DIR) && \ CFLAGS="$(CFLAGS) -ffat-lto-objects" CXXFLAGS="$(CXXFLAGS) -ffat-lto-objects" \ diff --git a/build.zig b/build.zig index a487dc66239ee7..054fc4dd944395 100644 --- a/build.zig +++ b/build.zig @@ -46,6 +46,7 @@ const BunBuildOptions = struct { sha: []const u8, /// enable debug logs in release builds enable_logs: bool = false, + enable_asan: bool, tracy_callstack_depth: u16, reported_nodejs_version: Version, /// To make iterating on some '@embedFile's faster, we load them at runtime @@ -275,6 +276,7 @@ pub fn build(b: *Build) !void { .tracy_callstack_depth = b.option(u16, "tracy_callstack_depth", "") orelse 10, .enable_logs = b.option(bool, "enable_logs", "Enable logs in release") orelse false, + .enable_asan = b.option(bool, "enable_asan", "Enable asan") orelse false, }; // zig build obj @@ -393,6 +395,7 @@ pub fn addMultiCheck( .reported_nodejs_version = root_build_options.reported_nodejs_version, .codegen_path = root_build_options.codegen_path, .no_llvm = root_build_options.no_llvm, + .enable_asan = root_build_options.enable_asan, }; var obj = addBunObject(b, &options); @@ -440,6 +443,14 @@ pub fn addBunObject(b: *Build, opts: *BunBuildOptions) *Compile { .omit_frame_pointer = false, .strip = false, // stripped at the end }); + if (opts.enable_asan) { + if (@hasField(Build.Module, "sanitize_address")) { + obj.root_module.sanitize_address = true; + } else { + const fail_step = b.addFail("asan is not supported on this platform"); + obj.step.dependOn(&fail_step.step); + } + } obj.bundle_compiler_rt = false; obj.root_module.omit_frame_pointer = false; diff --git a/cmake/CompilerFlags.cmake b/cmake/CompilerFlags.cmake index 847b365ddae467..9f61034af8f094 100644 --- a/cmake/CompilerFlags.cmake +++ b/cmake/CompilerFlags.cmake @@ -44,6 +44,13 @@ if(WIN32) ) endif() +if(ENABLE_ASAN) + register_compiler_flags( + DESCRIPTION "Enable AddressSanitizer" + -fsanitize=address + ) +endif() + # --- Optimization level --- if(DEBUG) register_compiler_flags( diff --git a/cmake/Options.cmake b/cmake/Options.cmake index fe3219c2687686..5874cf644e4649 100644 --- a/cmake/Options.cmake +++ b/cmake/Options.cmake @@ -86,6 +86,11 @@ optionx(ENABLE_LTO BOOL "If LTO (link-time optimization) should be used" DEFAULT if(LINUX) optionx(ENABLE_VALGRIND BOOL "If Valgrind support should be enabled" DEFAULT OFF) endif() +if(DEBUG AND APPLE AND CMAKE_SYSTEM_PROCESSOR MATCHES "arm64|aarch64") + optionx(ENABLE_ASAN BOOL "If ASAN support should be enabled" DEFAULT ON) +else() + optionx(ENABLE_ASAN BOOL "If ASAN support should be enabled" DEFAULT OFF) +endif() optionx(ENABLE_PRETTIER BOOL "If prettier should be ran" DEFAULT OFF) diff --git a/cmake/scripts/DownloadZig.cmake b/cmake/scripts/DownloadZig.cmake index 590a443f444009..42c6b3c990ffd9 100644 --- a/cmake/scripts/DownloadZig.cmake +++ b/cmake/scripts/DownloadZig.cmake @@ -28,7 +28,12 @@ else() message(FATAL_ERROR "Unsupported architecture: ${CMAKE_HOST_SYSTEM_PROCESSOR}") endif() -set(ZIG_NAME zig-${ZIG_OS}-${ZIG_ARCH}-${ZIG_VERSION}) +set(ZIG_ASAN "") +if(ENABLE_ASAN) + set(ZIG_ASAN "+asan") +endif() + +set(ZIG_NAME zig-${ZIG_OS}-${ZIG_ARCH}-${ZIG_VERSION}${ZIG_ASAN}) if(CMAKE_HOST_WIN32) set(ZIG_EXE "zig.exe") diff --git a/cmake/targets/BuildBun.cmake b/cmake/targets/BuildBun.cmake index ad1ce07f33ddc6..81b8c8bac6a47b 100644 --- a/cmake/targets/BuildBun.cmake +++ b/cmake/targets/BuildBun.cmake @@ -565,6 +565,7 @@ register_command( -Dcanary=${CANARY_REVISION} -Dcodegen_path=${CODEGEN_PATH} -Dcodegen_embed=$,true,false> + -Denable_asan=$,true,false> --prominent-compile-errors ${ZIG_FLAGS_BUN} ARTIFACTS @@ -827,6 +828,15 @@ if(NOT WIN32) ) endif() + if (ENABLE_ASAN) + target_compile_options(${bun} PUBLIC + -fsanitize=address + ) + target_link_libraries(${bun} PUBLIC + -fsanitize=address + ) + endif() + target_compile_options(${bun} PUBLIC -Werror=return-type -Werror=return-stack-address diff --git a/cmake/targets/BuildMimalloc.cmake b/cmake/targets/BuildMimalloc.cmake index 1e88a1a5f0e7f9..3ce366a03a1361 100644 --- a/cmake/targets/BuildMimalloc.cmake +++ b/cmake/targets/BuildMimalloc.cmake @@ -4,7 +4,7 @@ register_repository( REPOSITORY oven-sh/mimalloc COMMIT - 82b2c2277a4d570187c07b376557dc5bde81d848 + 1beadf9651a7bfdec6b5367c380ecc3fe1c40d1a ) set(MIMALLOC_CMAKE_ARGS @@ -19,6 +19,10 @@ set(MIMALLOC_CMAKE_ARGS -DMI_SKIP_COLLECT_ON_EXIT=ON ) +if(ENABLE_ASAN) + list(APPEND MIMALLOC_CMAKE_ARGS -DMI_TRACK_ASAN=ON) +endif() + if(DEBUG) list(APPEND MIMALLOC_CMAKE_ARGS -DMI_DEBUG_FULL=ON) endif() diff --git a/cmake/tools/SetupWebKit.cmake b/cmake/tools/SetupWebKit.cmake index 6c3f8a9619c8ab..417b0289a4ca06 100644 --- a/cmake/tools/SetupWebKit.cmake +++ b/cmake/tools/SetupWebKit.cmake @@ -2,7 +2,7 @@ option(WEBKIT_VERSION "The version of WebKit to use") option(WEBKIT_LOCAL "If a local version of WebKit should be used instead of downloading") if(NOT WEBKIT_VERSION) - set(WEBKIT_VERSION e32c6356625cfacebff0c61d182f759abf6f508a) + set(WEBKIT_VERSION 851aabf42b06ba583cc0485ff9088e3f84c22f3d) endif() string(SUBSTRING ${WEBKIT_VERSION} 0 16 WEBKIT_VERSION_PREFIX) @@ -79,6 +79,10 @@ else() set(WEBKIT_SUFFIX "${WEBKIT_SUFFIX}") endif() +if(ENABLE_ASAN) + set(WEBKIT_SUFFIX "${WEBKIT_SUFFIX}-asan") +endif() + set(WEBKIT_NAME bun-webkit-${WEBKIT_OS}-${WEBKIT_ARCH}${WEBKIT_SUFFIX}) set(WEBKIT_FILENAME ${WEBKIT_NAME}.tar.gz) setx(WEBKIT_DOWNLOAD_URL https://github.com/oven-sh/WebKit/releases/download/autobuild-${WEBKIT_VERSION}/${WEBKIT_FILENAME}) diff --git a/cmake/tools/SetupZig.cmake b/cmake/tools/SetupZig.cmake index 00cab1c61bffd7..9825b7831aae7d 100644 --- a/cmake/tools/SetupZig.cmake +++ b/cmake/tools/SetupZig.cmake @@ -21,7 +21,7 @@ else() endif() optionx(ZIG_VERSION STRING "The zig version of the compiler to download" DEFAULT "0.14.0-dev.2987+183bb8b08") -optionx(ZIG_COMMIT STRING "The zig commit to use in oven-sh/zig" DEFAULT "568a19ea4b811a5580bbf869cdaf6071244b9bb2") +optionx(ZIG_COMMIT STRING "The zig commit to use in oven-sh/zig" DEFAULT "63f8ed52c011beafde83216efba766492491ef4b") optionx(ZIG_TARGET STRING "The zig target to use" DEFAULT ${DEFAULT_ZIG_TARGET}) if(CMAKE_BUILD_TYPE STREQUAL "Release") @@ -79,6 +79,7 @@ register_command( -DZIG_PATH=${ZIG_PATH} -DZIG_VERSION=${ZIG_VERSION} -DZIG_COMMIT=${ZIG_COMMIT} + -DENABLE_ASAN=${ENABLE_ASAN} -P ${CWD}/cmake/scripts/DownloadZig.cmake SOURCES ${CWD}/cmake/scripts/DownloadZig.cmake diff --git a/src/bun.js/bindings/bindings.cpp b/src/bun.js/bindings/bindings.cpp index f123454b52193c..c7ae9f663dd6b0 100644 --- a/src/bun.js/bindings/bindings.cpp +++ b/src/bun.js/bindings/bindings.cpp @@ -133,10 +133,12 @@ #if OS(DARWIN) #if BUN_DEBUG +#if !__has_feature(address_sanitizer) #include #define IS_MALLOC_DEBUGGING_ENABLED 1 #endif #endif +#endif static WTF::StringView StringView_slice(WTF::StringView sv, unsigned start, unsigned end) { diff --git a/src/bun.js/modules/BunJSCModule.h b/src/bun.js/modules/BunJSCModule.h index 1246400d237436..3ed93771345f19 100644 --- a/src/bun.js/modules/BunJSCModule.h +++ b/src/bun.js/modules/BunJSCModule.h @@ -50,10 +50,12 @@ #if OS(DARWIN) #if BUN_DEBUG +#if !__has_feature(address_sanitizer) #include #define IS_MALLOC_DEBUGGING_ENABLED 1 #endif #endif +#endif using namespace JSC; using namespace WTF;