authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-12-16 16:17:52-05:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2019-12-16 16:17:52-05:00
log13cdc137e6389af83f225ab851bf8ef768ebcc69
tree34675041bfef45efd3c294f7df9fb244674c6dbf
parentde0d8885b4623dab14c379fc844ae0b18d9f8405
parent839b3a61ad51b385ac28a0123b6cc63d90ef83f8
signature Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #3570 from ziglang/c-sanitize-undef

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

7 files changed, 59 insertions(+), 2 deletions(-)

lib/std/build.zig+5
......@@ -1034,6 +1034,7 @@ pub const LibExeObjStep = struct {
10341034 disable_gen_h: bool,
10351035 bundle_compiler_rt: bool,
10361036 disable_stack_probing: bool,
1037 disable_sanitize_c: bool,
10371038 c_std: Builder.CStd,
10381039 override_lib_dir: ?[]const u8,
10391040 main_pkg_path: ?[]const u8,
......@@ -1183,6 +1184,7 @@ pub const LibExeObjStep = struct {
11831184 .disable_gen_h = false,
11841185 .bundle_compiler_rt = false,
11851186 .disable_stack_probing = false,
1187 .disable_sanitize_c = false,
11861188 .output_dir = null,
11871189 .need_system_paths = false,
11881190 .single_threaded = false,
......@@ -1822,6 +1824,9 @@ pub const LibExeObjStep = struct {
18221824 if (self.disable_stack_probing) {
18231825 try zig_args.append("-fno-stack-check");
18241826 }
1827 if (self.disable_sanitize_c) {
1828 try zig_args.append("-fno-sanitize-c");
1829 }
18251830
18261831 switch (self.target) {
18271832 .Native => {},
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/all_types.hpp+8
......@@ -1928,6 +1928,12 @@ enum WantStackCheck {
19281928 WantStackCheckEnabled,
19291929};
19301930
1931enum WantCSanitize {
1932 WantCSanitizeAuto,
1933 WantCSanitizeDisabled,
1934 WantCSanitizeEnabled,
1935};
1936
19311937struct CFile {
19321938 ZigList<const char *> args;
19331939 const char *source_path;
......@@ -2096,6 +2102,7 @@ struct CodeGen {
20962102
20972103 WantPIC want_pic;
20982104 WantStackCheck want_stack_check;
2105 WantCSanitize want_sanitize_c;
20992106 CacheHash cache_hash;
21002107 ErrColor err_color;
21012108 uint32_t next_unresolved_index;
......@@ -2170,6 +2177,7 @@ struct CodeGen {
21702177 bool have_pic;
21712178 bool have_dynamic_link; // this is whether the final thing will be dynamically linked. see also is_dynamic
21722179 bool have_stack_probing;
2180 bool have_sanitize_c;
21732181 bool function_sections;
21742182 bool enable_dump_analysis;
21752183 bool enable_doc_generation;
src/codegen.cpp+25
......@@ -8261,6 +8261,20 @@ static bool detect_stack_probing(CodeGen *g) {
82618261 zig_unreachable();
82628262}
82638263
8264static bool detect_sanitize_c(CodeGen *g) {
8265 if (!target_supports_sanitize_c(g->zig_target))
8266 return false;
8267 switch (g->want_sanitize_c) {
8268 case WantCSanitizeDisabled:
8269 return false;
8270 case WantCSanitizeEnabled:
8271 return true;
8272 case WantCSanitizeAuto:
8273 return g->build_mode == BuildModeSafeRelease || g->build_mode == BuildModeDebug;
8274 }
8275 zig_unreachable();
8276}
8277
82648278// Returns TargetSubsystemAuto to mean "no subsystem"
82658279TargetSubsystem detect_subsystem(CodeGen *g) {
82668280 if (g->subsystem != TargetSubsystemAuto)
......@@ -8297,6 +8311,7 @@ Buf *codegen_generate_builtin_source(CodeGen *g) {
82978311 g->have_dynamic_link = detect_dynamic_link(g);
82988312 g->have_pic = detect_pic(g);
82998313 g->have_stack_probing = detect_stack_probing(g);
8314 g->have_sanitize_c = detect_sanitize_c(g);
83008315 g->is_single_threaded = detect_single_threaded(g);
83018316 g->have_err_ret_tracing = detect_err_ret_tracing(g);
83028317
......@@ -8582,6 +8597,7 @@ static void init(CodeGen *g) {
85828597 g->have_dynamic_link = detect_dynamic_link(g);
85838598 g->have_pic = detect_pic(g);
85848599 g->have_stack_probing = detect_stack_probing(g);
8600 g->have_sanitize_c = detect_sanitize_c(g);
85858601 g->is_single_threaded = detect_single_threaded(g);
85868602 g->have_err_ret_tracing = detect_err_ret_tracing(g);
85878603
......@@ -8971,6 +8987,11 @@ void add_cc_args(CodeGen *g, ZigList<const char *> &args, const char *out_dep_pa
89718987 args.append("-fomit-frame-pointer");
89728988 }
89738989
8990 if (g->have_sanitize_c) {
8991 args.append("-fsanitize=undefined");
8992 args.append("-fsanitize-trap=undefined");
8993 }
8994
89748995 switch (g->build_mode) {
89758996 case BuildModeDebug:
89768997 // windows c runtime requires -D_DEBUG if using debug libraries
......@@ -9306,6 +9327,7 @@ Error create_c_object_cache(CodeGen *g, CacheHash **out_cache_hash, bool verbose
93069327 cache_bool(cache_hash, g->strip_debug_symbols);
93079328 cache_int(cache_hash, g->build_mode);
93089329 cache_bool(cache_hash, g->have_pic);
9330 cache_bool(cache_hash, g->have_sanitize_c);
93099331 cache_bool(cache_hash, want_valgrind_support(g));
93109332 cache_bool(cache_hash, g->function_sections);
93119333 for (size_t arg_i = 0; arg_i < g->clang_argv_len; arg_i += 1) {
......@@ -10084,6 +10106,7 @@ static Error check_cache(CodeGen *g, Buf *manifest_dir, Buf *digest) {
1008410106 cache_bool(ch, g->have_pic);
1008510107 cache_bool(ch, g->have_dynamic_link);
1008610108 cache_bool(ch, g->have_stack_probing);
10109 cache_bool(ch, g->have_sanitize_c);
1008710110 cache_bool(ch, g->is_dummy_so);
1008810111 cache_bool(ch, g->function_sections);
1008910112 cache_bool(ch, g->enable_dump_analysis);
......@@ -10204,6 +10227,7 @@ void codegen_build_and_link(CodeGen *g) {
1020410227 g->have_pic = detect_pic(g);
1020510228 g->is_single_threaded = detect_single_threaded(g);
1020610229 g->have_err_ret_tracing = detect_err_ret_tracing(g);
10230 g->have_sanitize_c = detect_sanitize_c(g);
1020710231 detect_libc(g);
1020810232 detect_dynamic_linker(g);
1020910233
......@@ -10392,6 +10416,7 @@ CodeGen *create_child_codegen(CodeGen *parent_gen, Buf *root_src_path, OutType o
1039210416 child_gen->root_out_name = buf_create_from_str(name);
1039310417 child_gen->disable_gen_h = true;
1039410418 child_gen->want_stack_check = WantStackCheckDisabled;
10419 child_gen->want_sanitize_c = WantCSanitizeDisabled;
1039510420 child_gen->verbose_tokenize = parent_gen->verbose_tokenize;
1039610421 child_gen->verbose_ast = parent_gen->verbose_ast;
1039710422 child_gen->verbose_link = parent_gen->verbose_link;
src/main.cpp+9
......@@ -59,6 +59,8 @@ static int print_full_usage(const char *arg0, FILE *file, int return_code) {
5959 " --enable-valgrind include valgrind client requests release builds\n"
6060 " -fstack-check enable stack probing in unsafe builds\n"
6161 " -fno-stack-check disable stack probing in safe builds\n"
62 " -fsanitize-c enable C undefined behavior detection in unsafe builds\n"
63 " -fno-sanitize-c disable C undefined behavior detection in safe builds\n"
6264 " --emit [asm|bin|llvm-ir] emit a specific file format as compilation output\n"
6365 " -fPIC enable Position Independent Code\n"
6466 " -fno-PIC disable Position Independent Code\n"
......@@ -524,6 +526,7 @@ int main(int argc, char **argv) {
524526 ValgrindSupport valgrind_support = ValgrindSupportAuto;
525527 WantPIC want_pic = WantPICAuto;
526528 WantStackCheck want_stack_check = WantStackCheckAuto;
529 WantCSanitize want_sanitize_c = WantCSanitizeAuto;
527530 bool function_sections = false;
528531
529532 ZigList<const char *> llvm_argv = {0};
......@@ -722,6 +725,10 @@ int main(int argc, char **argv) {
722725 want_stack_check = WantStackCheckEnabled;
723726 } else if (strcmp(arg, "-fno-stack-check") == 0) {
724727 want_stack_check = WantStackCheckDisabled;
728 } else if (strcmp(arg, "-fsanitize-c") == 0) {
729 want_sanitize_c = WantCSanitizeEnabled;
730 } else if (strcmp(arg, "-fno-sanitize-c") == 0) {
731 want_sanitize_c = WantCSanitizeDisabled;
725732 } else if (strcmp(arg, "--system-linker-hack") == 0) {
726733 system_linker_hack = true;
727734 } else if (strcmp(arg, "--single-threaded") == 0) {
......@@ -1093,6 +1100,7 @@ int main(int argc, char **argv) {
10931100 g->valgrind_support = valgrind_support;
10941101 g->want_pic = want_pic;
10951102 g->want_stack_check = want_stack_check;
1103 g->want_sanitize_c = want_sanitize_c;
10961104 g->want_single_threaded = want_single_threaded;
10971105 Buf *builtin_source = codegen_generate_builtin_source(g);
10981106 if (fwrite(buf_ptr(builtin_source), 1, buf_len(builtin_source), stdout) != buf_len(builtin_source)) {
......@@ -1192,6 +1200,7 @@ int main(int argc, char **argv) {
11921200 g->valgrind_support = valgrind_support;
11931201 g->want_pic = want_pic;
11941202 g->want_stack_check = want_stack_check;
1203 g->want_sanitize_c = want_sanitize_c;
11951204 g->subsystem = subsystem;
11961205
11971206 g->enable_time_report = timing_info;
src/target.cpp+4
......@@ -1606,6 +1606,10 @@ bool target_supports_stack_probing(const ZigTarget *target) {
16061606 return target->os != OsWindows && target->os != OsUefi && (target->arch == ZigLLVM_x86 || target->arch == ZigLLVM_x86_64);
16071607}
16081608
1609bool target_supports_sanitize_c(const ZigTarget *target) {
1610 return true;
1611}
1612
16091613bool target_requires_pic(const ZigTarget *target, bool linking_libc) {
16101614 // This function returns whether non-pic code is completely invalid on the given target.
16111615 return target_is_android(target) || target->os == OsWindows || target->os == OsUefi || target_os_requires_libc(target->os) ||
src/target.hpp+1
......@@ -194,6 +194,7 @@ bool target_is_riscv(const ZigTarget *target);
194194bool target_is_android(const ZigTarget *target);
195195bool target_is_single_threaded(const ZigTarget *target);
196196bool target_supports_stack_probing(const ZigTarget *target);
197bool target_supports_sanitize_c(const ZigTarget *target);
197198bool target_has_debug_info(const ZigTarget *target);
198199const char *target_arch_musl_name(ZigLLVM_ArchType arch);
199200bool target_supports_libunwind(const ZigTarget *target);