authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-05-27 20:59:19-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-05-27 20:59:19-04:00
log2c0280ba085893984007706fb40c7b291f43074d
tree49c477831d259ef6a5ce994508818e16480cce4e
parent3fccc0747903f0726d6cc8ee73832cb62f1304bb
signaturelock-open Commit is signed but in an unrecognized format.

improve the stack check CLI options

See #2526

6 files changed, 47 insertions(+), 23 deletions(-)

src/all_types.hpp+12-5
......@@ -1633,6 +1633,12 @@ enum WantPIC {
16331633 WantPICEnabled,
16341634};
16351635
1636enum WantStackCheck {
1637 WantStackCheckAuto,
1638 WantStackCheckDisabled,
1639 WantStackCheckEnabled,
1640};
1641
16361642struct CFile {
16371643 ZigList<const char *> args;
16381644 const char *source_path;
......@@ -1790,6 +1796,8 @@ struct CodeGen {
17901796 TldFn *panic_tld_fn;
17911797 AstNode *root_export_decl;
17921798
1799 WantPIC want_pic;
1800 WantStackCheck want_stack_check;
17931801 CacheHash cache_hash;
17941802 ErrColor err_color;
17951803 uint32_t next_unresolved_index;
......@@ -1807,8 +1815,6 @@ struct CodeGen {
18071815 bool have_dllmain_crt_startup;
18081816 bool have_pub_panic;
18091817 bool have_err_ret_tracing;
1810 bool have_pic;
1811 bool have_dynamic_link; // this is whether the final thing will be dynamically linked. see also is_dynamic
18121818 bool c_want_stdint;
18131819 bool c_want_stdbool;
18141820 bool verbose_tokenize;
......@@ -1824,6 +1830,7 @@ struct CodeGen {
18241830 bool enable_time_report;
18251831 bool system_linker_hack;
18261832 bool reported_bad_link_libc_error;
1833 bool is_dynamic; // shared library rather than static library. dynamic musl rather than static musl.
18271834
18281835 //////////////////////////// Participates in Input Parameter Cache Hash
18291836 /////// Note: there is a separate cache hash for builtin.zig, when adding fields,
......@@ -1852,8 +1859,6 @@ struct CodeGen {
18521859 const ZigTarget *zig_target;
18531860 TargetSubsystem subsystem;
18541861 ValgrindSupport valgrind_support;
1855 WantPIC want_pic;
1856 bool is_dynamic; // shared library rather than static library. dynamic musl rather than static musl.
18571862 bool strip_debug_symbols;
18581863 bool is_test_build;
18591864 bool is_single_threaded;
......@@ -1863,7 +1868,9 @@ struct CodeGen {
18631868 bool is_dummy_so;
18641869 bool disable_gen_h;
18651870 bool bundle_compiler_rt;
1866 bool disable_stack_probing;
1871 bool have_pic;
1872 bool have_dynamic_link; // this is whether the final thing will be dynamically linked. see also is_dynamic
1873 bool have_stack_probing;
18671874
18681875 Buf *mmacosx_version_min;
18691876 Buf *mios_version_min;
src/codegen.cpp+20-12
......@@ -399,15 +399,6 @@ static void add_uwtable_attr(CodeGen *g, LLVMValueRef fn_val) {
399399 }
400400}
401401
402static void add_probe_stack_attr(CodeGen *g, LLVMValueRef fn_val) {
403 // Windows already emits its own stack probes
404 if (!g->disable_stack_probing && g->zig_target->os != OsWindows &&
405 (g->zig_target->arch == ZigLLVM_x86 ||
406 g->zig_target->arch == ZigLLVM_x86_64)) {
407 addLLVMFnAttrStr(fn_val, "probe-stack", "__zig_probe_stack");
408 }
409}
410
411402static LLVMLinkage to_llvm_linkage(GlobalLinkageId id) {
412403 switch (id) {
413404 case GlobalLinkageIdInternal:
......@@ -596,8 +587,9 @@ static LLVMValueRef fn_llvm_value(CodeGen *g, ZigFn *fn_table_entry) {
596587 addLLVMFnAttr(fn_table_entry->llvm_value, "sspstrong");
597588 addLLVMFnAttrStr(fn_table_entry->llvm_value, "stack-protector-buffer-size", "4");
598589 }
599
600 add_probe_stack_attr(g, fn_table_entry->llvm_value);
590 }
591 if (g->have_stack_probing && !fn_table_entry->def_scope->safety_off) {
592 addLLVMFnAttrStr(fn_table_entry->llvm_value, "probe-stack", "__zig_probe_stack");
601593 }
602594 } else {
603595 maybe_import_dll(g, fn_table_entry->llvm_value, linkage);
......@@ -7458,6 +7450,20 @@ static bool detect_pic(CodeGen *g) {
74587450 zig_unreachable();
74597451}
74607452
7453static bool detect_stack_probing(CodeGen *g) {
7454 if (!target_supports_stack_probing(g->zig_target))
7455 return false;
7456 switch (g->want_stack_check) {
7457 case WantStackCheckDisabled:
7458 return false;
7459 case WantStackCheckEnabled:
7460 return true;
7461 case WantStackCheckAuto:
7462 return g->build_mode == BuildModeSafeRelease || g->build_mode == BuildModeDebug;
7463 }
7464 zig_unreachable();
7465}
7466
74617467static bool detect_single_threaded(CodeGen *g) {
74627468 if (g->want_single_threaded)
74637469 return true;
......@@ -7476,6 +7482,7 @@ static bool detect_err_ret_tracing(CodeGen *g) {
74767482Buf *codegen_generate_builtin_source(CodeGen *g) {
74777483 g->have_dynamic_link = detect_dynamic_link(g);
74787484 g->have_pic = detect_pic(g);
7485 g->have_stack_probing = detect_stack_probing(g);
74797486 g->is_single_threaded = detect_single_threaded(g);
74807487 g->have_err_ret_tracing = detect_err_ret_tracing(g);
74817488
......@@ -7982,6 +7989,7 @@ static void init(CodeGen *g) {
79827989
79837990 g->have_dynamic_link = detect_dynamic_link(g);
79847991 g->have_pic = detect_pic(g);
7992 g->have_stack_probing = detect_stack_probing(g);
79857993 g->is_single_threaded = detect_single_threaded(g);
79867994 g->have_err_ret_tracing = detect_err_ret_tracing(g);
79877995
......@@ -9351,10 +9359,10 @@ static Error check_cache(CodeGen *g, Buf *manifest_dir, Buf *digest) {
93519359 cache_bool(ch, g->each_lib_rpath);
93529360 cache_bool(ch, g->disable_gen_h);
93539361 cache_bool(ch, g->bundle_compiler_rt);
9354 cache_bool(ch, g->disable_stack_probing);
93559362 cache_bool(ch, want_valgrind_support(g));
93569363 cache_bool(ch, g->have_pic);
93579364 cache_bool(ch, g->have_dynamic_link);
9365 cache_bool(ch, g->have_stack_probing);
93589366 cache_bool(ch, g->is_dummy_so);
93599367 cache_buf_opt(ch, g->mmacosx_version_min);
93609368 cache_buf_opt(ch, g->mios_version_min);
src/link.cpp+1-1
......@@ -25,7 +25,7 @@ static CodeGen *create_child_codegen(CodeGen *parent_gen, Buf *root_src_path, Ou
2525 CodeGen *child_gen = codegen_create(nullptr, root_src_path, parent_gen->zig_target, out_type,
2626 parent_gen->build_mode, parent_gen->zig_lib_dir, parent_gen->zig_std_dir, libc, get_stage1_cache_path());
2727 child_gen->disable_gen_h = true;
28 child_gen->disable_stack_probing = true;
28 child_gen->want_stack_check = WantStackCheckDisabled;
2929 child_gen->verbose_tokenize = parent_gen->verbose_tokenize;
3030 child_gen->verbose_ast = parent_gen->verbose_ast;
3131 child_gen->verbose_link = parent_gen->verbose_link;
src/main.cpp+9-5
......@@ -55,7 +55,8 @@ static int print_full_usage(const char *arg0, FILE *file, int return_code) {
5555 " --disable-gen-h do not generate a C header file (.h)\n"
5656 " --disable-valgrind omit valgrind client requests in debug builds\n"
5757 " --enable-valgrind include valgrind client requests release builds\n"
58 " --disable-stack-probing workaround for macosx\n"
58 " -fstack-check enable stack probing in unsafe builds\n"
59 " -fno-stack-check disable stack probing in safe builds\n"
5960 " --emit [asm|bin|llvm-ir] emit a specific file format as compilation output\n"
6061 " -fPIC enable Position Independent Code\n"
6162 " -fno-PIC disable Position Independent Code\n"
......@@ -443,12 +444,12 @@ int main(int argc, char **argv) {
443444 bool want_single_threaded = false;
444445 bool disable_gen_h = false;
445446 bool bundle_compiler_rt = false;
446 bool disable_stack_probing = false;
447447 Buf *override_std_dir = nullptr;
448448 Buf *override_lib_dir = nullptr;
449449 Buf *main_pkg_path = nullptr;
450450 ValgrindSupport valgrind_support = ValgrindSupportAuto;
451451 WantPIC want_pic = WantPICAuto;
452 WantStackCheck want_stack_check = WantStackCheckAuto;
452453
453454 ZigList<const char *> llvm_argv = {0};
454455 llvm_argv.append("zig (LLVM option parsing)");
......@@ -648,6 +649,10 @@ int main(int argc, char **argv) {
648649 want_pic = WantPICEnabled;
649650 } else if (strcmp(arg, "-fno-PIC") == 0) {
650651 want_pic = WantPICDisabled;
652 } else if (strcmp(arg, "-fstack-check") == 0) {
653 want_stack_check = WantStackCheckEnabled;
654 } else if (strcmp(arg, "-fno-stack-check") == 0) {
655 want_stack_check = WantStackCheckDisabled;
651656 } else if (strcmp(arg, "--system-linker-hack") == 0) {
652657 system_linker_hack = true;
653658 } else if (strcmp(arg, "--single-threaded") == 0) {
......@@ -656,8 +661,6 @@ int main(int argc, char **argv) {
656661 disable_gen_h = true;
657662 } else if (strcmp(arg, "--bundle-compiler-rt") == 0) {
658663 bundle_compiler_rt = true;
659 } else if (strcmp(arg, "--disable-stack-probing") == 0) {
660 disable_stack_probing = true;
661664 } else if (strcmp(arg, "--test-cmd-bin") == 0) {
662665 test_exec_args.append(nullptr);
663666 } else if (arg[1] == 'L' && arg[2] != 0) {
......@@ -953,6 +956,7 @@ int main(int argc, char **argv) {
953956 out_type, build_mode, override_lib_dir, override_std_dir, nullptr, nullptr);
954957 g->valgrind_support = valgrind_support;
955958 g->want_pic = want_pic;
959 g->want_stack_check = want_stack_check;
956960 g->want_single_threaded = want_single_threaded;
957961 Buf *builtin_source = codegen_generate_builtin_source(g);
958962 if (fwrite(buf_ptr(builtin_source), 1, buf_len(builtin_source), stdout) != buf_len(builtin_source)) {
......@@ -1048,6 +1052,7 @@ int main(int argc, char **argv) {
10481052 if (llvm_argv.length >= 2) codegen_set_llvm_argv(g, llvm_argv.items + 1, llvm_argv.length - 2);
10491053 g->valgrind_support = valgrind_support;
10501054 g->want_pic = want_pic;
1055 g->want_stack_check = want_stack_check;
10511056 g->subsystem = subsystem;
10521057
10531058 g->enable_time_report = timing_info;
......@@ -1074,7 +1079,6 @@ int main(int argc, char **argv) {
10741079 g->output_dir = output_dir;
10751080 g->disable_gen_h = disable_gen_h;
10761081 g->bundle_compiler_rt = bundle_compiler_rt;
1077 g->disable_stack_probing = disable_stack_probing;
10781082 codegen_set_errmsg_color(g, color);
10791083 g->system_linker_hack = system_linker_hack;
10801084
src/target.cpp+4
......@@ -1356,6 +1356,10 @@ bool target_supports_fpic(const ZigTarget *target) {
13561356 return target->os != OsWindows;
13571357}
13581358
1359bool target_supports_stack_probing(const ZigTarget *target) {
1360 return target->os != OsWindows && (target->arch == ZigLLVM_x86 || target->arch == ZigLLVM_x86_64);
1361}
1362
13591363bool target_requires_pic(const ZigTarget *target, bool linking_libc) {
13601364 // This function returns whether non-pic code is completely invalid on the given target.
13611365 return target->os == OsWindows || target_os_requires_libc(target->os) ||
src/target.hpp+1
......@@ -172,6 +172,7 @@ bool target_is_glibc(const ZigTarget *target);
172172bool target_is_musl(const ZigTarget *target);
173173bool target_is_wasm(const ZigTarget *target);
174174bool target_is_single_threaded(const ZigTarget *target);
175bool target_supports_stack_probing(const ZigTarget *target);
175176
176177uint32_t target_arch_pointer_bit_width(ZigLLVM_ArchType arch);
177178