authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-10-31 17:24:22-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-12-16 13:06:02-05:00
logf8cd981c04e60530b99c9f360c3e79041d75ca96
treeb5d355aa94f0d64e0cd9b8f237f056b3dce75030
parentcb5a5ebb200c6e7f87178e26dac453e8c91948d7
signature Commit is signed but in an unrecognized format.

use -fsanitize=undefined for C code in safe build modes

closes #3569

2 files changed, 13 insertions(+), 2 deletions(-)

lib/std/debug.zig+7-2
...@@ -2404,6 +2404,7 @@ pub fn attachSegfaultHandler() void {...@@ -2404,6 +2404,7 @@ pub fn attachSegfaultHandler() void {
2404 };2404 };
24052405
2406 os.sigaction(os.SIGSEGV, &act, null);2406 os.sigaction(os.SIGSEGV, &act, null);
2407 os.sigaction(os.SIGILL, &act, null);
2407}2408}
24082409
2409fn resetSegfaultHandler() void {2410fn resetSegfaultHandler() void {
...@@ -2420,6 +2421,7 @@ fn resetSegfaultHandler() void {...@@ -2420,6 +2421,7 @@ fn resetSegfaultHandler() void {
2420 .flags = 0,2421 .flags = 0,
2421 };2422 };
2422 os.sigaction(os.SIGSEGV, &act, null);2423 os.sigaction(os.SIGSEGV, &act, null);
2424 os.sigaction(os.SIGILL, &act, null);
2423}2425}
24242426
2425extern fn handleSegfaultLinux(sig: i32, info: *const os.siginfo_t, ctx_ptr: *const c_void) noreturn {2427extern fn handleSegfaultLinux(sig: i32, info: *const os.siginfo_t, ctx_ptr: *const c_void) noreturn {
...@@ -2429,8 +2431,11 @@ extern fn handleSegfaultLinux(sig: i32, info: *const os.siginfo_t, ctx_ptr: *con...@@ -2429,8 +2431,11 @@ extern fn handleSegfaultLinux(sig: i32, info: *const os.siginfo_t, ctx_ptr: *con
2429 resetSegfaultHandler();2431 resetSegfaultHandler();
24302432
2431 const addr = @ptrToInt(info.fields.sigfault.addr);2433 const addr = @ptrToInt(info.fields.sigfault.addr);
2432 std.debug.warn("Segmentation fault at address 0x{x}\n", .{addr});2434 switch (sig) {
24332435 os.SIGSEGV => std.debug.warn("Segmentation fault at address 0x{x}\n", .{addr}),
2436 os.SIGILL => std.debug.warn("Illegal instruction at address 0x{x}\n", .{addr}),
2437 else => unreachable,
2438 }
2434 switch (builtin.arch) {2439 switch (builtin.arch) {
2435 .i386 => {2440 .i386 => {
2436 const ctx = @ptrCast(*const os.ucontext_t, @alignCast(@alignOf(os.ucontext_t), ctx_ptr));2441 const ctx = @ptrCast(*const os.ucontext_t, @alignCast(@alignOf(os.ucontext_t), ctx_ptr));
src/codegen.cpp+6
...@@ -8973,6 +8973,9 @@ void add_cc_args(CodeGen *g, ZigList<const char *> &args, const char *out_dep_pa...@@ -8973,6 +8973,9 @@ void add_cc_args(CodeGen *g, ZigList<const char *> &args, const char *out_dep_pa
89738973
8974 switch (g->build_mode) {8974 switch (g->build_mode) {
8975 case BuildModeDebug:8975 case BuildModeDebug:
8976 args.append("-fsanitize=undefined");
8977 args.append("-fsanitize-trap=undefined");
8978
8976 // windows c runtime requires -D_DEBUG if using debug libraries8979 // windows c runtime requires -D_DEBUG if using debug libraries
8977 args.append("-D_DEBUG");8980 args.append("-D_DEBUG");
89788981
...@@ -8985,6 +8988,9 @@ void add_cc_args(CodeGen *g, ZigList<const char *> &args, const char *out_dep_pa...@@ -8985,6 +8988,9 @@ void add_cc_args(CodeGen *g, ZigList<const char *> &args, const char *out_dep_pa
8985 }8988 }
8986 break;8989 break;
8987 case BuildModeSafeRelease:8990 case BuildModeSafeRelease:
8991 args.append("-fsanitize=undefined");
8992 args.append("-fsanitize-trap=undefined");
8993
8988 // See the comment in the BuildModeFastRelease case for why we pass -O2 rather8994 // See the comment in the BuildModeFastRelease case for why we pass -O2 rather
8989 // than -O3 here.8995 // than -O3 here.
8990 args.append("-O2");8996 args.append("-O2");