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 {
24042404 };
24052405
24062406 os.sigaction(os.SIGSEGV, &act, null);
2407 os.sigaction(os.SIGILL, &act, null);
24072408}
24082409
24092410fn resetSegfaultHandler() void {
......@@ -2420,6 +2421,7 @@ fn resetSegfaultHandler() void {
24202421 .flags = 0,
24212422 };
24222423 os.sigaction(os.SIGSEGV, &act, null);
2424 os.sigaction(os.SIGILL, &act, null);
24232425}
24242426
24252427extern 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
24292431 resetSegfaultHandler();
24302432
24312433 const addr = @ptrToInt(info.fields.sigfault.addr);
2432 std.debug.warn("Segmentation fault at address 0x{x}\n", .{addr});
2433
2434 switch (sig) {
2435 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 }
24342439 switch (builtin.arch) {
24352440 .i386 => {
24362441 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
89738973
89748974 switch (g->build_mode) {
89758975 case BuildModeDebug:
8976 args.append("-fsanitize=undefined");
8977 args.append("-fsanitize-trap=undefined");
8978
89768979 // windows c runtime requires -D_DEBUG if using debug libraries
89778980 args.append("-D_DEBUG");
89788981
......@@ -8985,6 +8988,9 @@ void add_cc_args(CodeGen *g, ZigList<const char *> &args, const char *out_dep_pa
89858988 }
89868989 break;
89878990 case BuildModeSafeRelease:
8991 args.append("-fsanitize=undefined");
8992 args.append("-fsanitize-trap=undefined");
8993
89888994 // See the comment in the BuildModeFastRelease case for why we pass -O2 rather
89898995 // than -O3 here.
89908996 args.append("-O2");