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 {...@@ -1633,6 +1633,12 @@ enum WantPIC {
1633 WantPICEnabled,1633 WantPICEnabled,
1634};1634};
16351635
1636enum WantStackCheck {
1637 WantStackCheckAuto,
1638 WantStackCheckDisabled,
1639 WantStackCheckEnabled,
1640};
1641
1636struct CFile {1642struct CFile {
1637 ZigList<const char *> args;1643 ZigList<const char *> args;
1638 const char *source_path;1644 const char *source_path;
...@@ -1790,6 +1796,8 @@ struct CodeGen {...@@ -1790,6 +1796,8 @@ struct CodeGen {
1790 TldFn *panic_tld_fn;1796 TldFn *panic_tld_fn;
1791 AstNode *root_export_decl;1797 AstNode *root_export_decl;
17921798
1799 WantPIC want_pic;
1800 WantStackCheck want_stack_check;
1793 CacheHash cache_hash;1801 CacheHash cache_hash;
1794 ErrColor err_color;1802 ErrColor err_color;
1795 uint32_t next_unresolved_index;1803 uint32_t next_unresolved_index;
...@@ -1807,8 +1815,6 @@ struct CodeGen {...@@ -1807,8 +1815,6 @@ struct CodeGen {
1807 bool have_dllmain_crt_startup;1815 bool have_dllmain_crt_startup;
1808 bool have_pub_panic;1816 bool have_pub_panic;
1809 bool have_err_ret_tracing;1817 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
1812 bool c_want_stdint;1818 bool c_want_stdint;
1813 bool c_want_stdbool;1819 bool c_want_stdbool;
1814 bool verbose_tokenize;1820 bool verbose_tokenize;
...@@ -1824,6 +1830,7 @@ struct CodeGen {...@@ -1824,6 +1830,7 @@ struct CodeGen {
1824 bool enable_time_report;1830 bool enable_time_report;
1825 bool system_linker_hack;1831 bool system_linker_hack;
1826 bool reported_bad_link_libc_error;1832 bool reported_bad_link_libc_error;
1833 bool is_dynamic; // shared library rather than static library. dynamic musl rather than static musl.
18271834
1828 //////////////////////////// Participates in Input Parameter Cache Hash1835 //////////////////////////// Participates in Input Parameter Cache Hash
1829 /////// Note: there is a separate cache hash for builtin.zig, when adding fields,1836 /////// Note: there is a separate cache hash for builtin.zig, when adding fields,
...@@ -1852,8 +1859,6 @@ struct CodeGen {...@@ -1852,8 +1859,6 @@ struct CodeGen {
1852 const ZigTarget *zig_target;1859 const ZigTarget *zig_target;
1853 TargetSubsystem subsystem;1860 TargetSubsystem subsystem;
1854 ValgrindSupport valgrind_support;1861 ValgrindSupport valgrind_support;
1855 WantPIC want_pic;
1856 bool is_dynamic; // shared library rather than static library. dynamic musl rather than static musl.
1857 bool strip_debug_symbols;1862 bool strip_debug_symbols;
1858 bool is_test_build;1863 bool is_test_build;
1859 bool is_single_threaded;1864 bool is_single_threaded;
...@@ -1863,7 +1868,9 @@ struct CodeGen {...@@ -1863,7 +1868,9 @@ struct CodeGen {
1863 bool is_dummy_so;1868 bool is_dummy_so;
1864 bool disable_gen_h;1869 bool disable_gen_h;
1865 bool bundle_compiler_rt;1870 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
1868 Buf *mmacosx_version_min;1875 Buf *mmacosx_version_min;
1869 Buf *mios_version_min;1876 Buf *mios_version_min;
src/codegen.cpp+20-12
...@@ -399,15 +399,6 @@ static void add_uwtable_attr(CodeGen *g, LLVMValueRef fn_val) {...@@ -399,15 +399,6 @@ static void add_uwtable_attr(CodeGen *g, LLVMValueRef fn_val) {
399 }399 }
400}400}
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
411static LLVMLinkage to_llvm_linkage(GlobalLinkageId id) {402static LLVMLinkage to_llvm_linkage(GlobalLinkageId id) {
412 switch (id) {403 switch (id) {
413 case GlobalLinkageIdInternal:404 case GlobalLinkageIdInternal:
...@@ -596,8 +587,9 @@ static LLVMValueRef fn_llvm_value(CodeGen *g, ZigFn *fn_table_entry) {...@@ -596,8 +587,9 @@ static LLVMValueRef fn_llvm_value(CodeGen *g, ZigFn *fn_table_entry) {
596 addLLVMFnAttr(fn_table_entry->llvm_value, "sspstrong");587 addLLVMFnAttr(fn_table_entry->llvm_value, "sspstrong");
597 addLLVMFnAttrStr(fn_table_entry->llvm_value, "stack-protector-buffer-size", "4");588 addLLVMFnAttrStr(fn_table_entry->llvm_value, "stack-protector-buffer-size", "4");
598 }589 }
599590 }
600 add_probe_stack_attr(g, fn_table_entry->llvm_value);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");
601 }593 }
602 } else {594 } else {
603 maybe_import_dll(g, fn_table_entry->llvm_value, linkage);595 maybe_import_dll(g, fn_table_entry->llvm_value, linkage);
...@@ -7458,6 +7450,20 @@ static bool detect_pic(CodeGen *g) {...@@ -7458,6 +7450,20 @@ static bool detect_pic(CodeGen *g) {
7458 zig_unreachable();7450 zig_unreachable();
7459}7451}
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
7461static bool detect_single_threaded(CodeGen *g) {7467static bool detect_single_threaded(CodeGen *g) {
7462 if (g->want_single_threaded)7468 if (g->want_single_threaded)
7463 return true;7469 return true;
...@@ -7476,6 +7482,7 @@ static bool detect_err_ret_tracing(CodeGen *g) {...@@ -7476,6 +7482,7 @@ static bool detect_err_ret_tracing(CodeGen *g) {
7476Buf *codegen_generate_builtin_source(CodeGen *g) {7482Buf *codegen_generate_builtin_source(CodeGen *g) {
7477 g->have_dynamic_link = detect_dynamic_link(g);7483 g->have_dynamic_link = detect_dynamic_link(g);
7478 g->have_pic = detect_pic(g);7484 g->have_pic = detect_pic(g);
7485 g->have_stack_probing = detect_stack_probing(g);
7479 g->is_single_threaded = detect_single_threaded(g);7486 g->is_single_threaded = detect_single_threaded(g);
7480 g->have_err_ret_tracing = detect_err_ret_tracing(g);7487 g->have_err_ret_tracing = detect_err_ret_tracing(g);
74817488
...@@ -7982,6 +7989,7 @@ static void init(CodeGen *g) {...@@ -7982,6 +7989,7 @@ static void init(CodeGen *g) {
79827989
7983 g->have_dynamic_link = detect_dynamic_link(g);7990 g->have_dynamic_link = detect_dynamic_link(g);
7984 g->have_pic = detect_pic(g);7991 g->have_pic = detect_pic(g);
7992 g->have_stack_probing = detect_stack_probing(g);
7985 g->is_single_threaded = detect_single_threaded(g);7993 g->is_single_threaded = detect_single_threaded(g);
7986 g->have_err_ret_tracing = detect_err_ret_tracing(g);7994 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) {...@@ -9351,10 +9359,10 @@ static Error check_cache(CodeGen *g, Buf *manifest_dir, Buf *digest) {
9351 cache_bool(ch, g->each_lib_rpath);9359 cache_bool(ch, g->each_lib_rpath);
9352 cache_bool(ch, g->disable_gen_h);9360 cache_bool(ch, g->disable_gen_h);
9353 cache_bool(ch, g->bundle_compiler_rt);9361 cache_bool(ch, g->bundle_compiler_rt);
9354 cache_bool(ch, g->disable_stack_probing);
9355 cache_bool(ch, want_valgrind_support(g));9362 cache_bool(ch, want_valgrind_support(g));
9356 cache_bool(ch, g->have_pic);9363 cache_bool(ch, g->have_pic);
9357 cache_bool(ch, g->have_dynamic_link);9364 cache_bool(ch, g->have_dynamic_link);
9365 cache_bool(ch, g->have_stack_probing);
9358 cache_bool(ch, g->is_dummy_so);9366 cache_bool(ch, g->is_dummy_so);
9359 cache_buf_opt(ch, g->mmacosx_version_min);9367 cache_buf_opt(ch, g->mmacosx_version_min);
9360 cache_buf_opt(ch, g->mios_version_min);9368 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...@@ -25,7 +25,7 @@ static CodeGen *create_child_codegen(CodeGen *parent_gen, Buf *root_src_path, Ou
25 CodeGen *child_gen = codegen_create(nullptr, root_src_path, parent_gen->zig_target, out_type,25 CodeGen *child_gen = codegen_create(nullptr, root_src_path, parent_gen->zig_target, out_type,
26 parent_gen->build_mode, parent_gen->zig_lib_dir, parent_gen->zig_std_dir, libc, get_stage1_cache_path());26 parent_gen->build_mode, parent_gen->zig_lib_dir, parent_gen->zig_std_dir, libc, get_stage1_cache_path());
27 child_gen->disable_gen_h = true;27 child_gen->disable_gen_h = true;
28 child_gen->disable_stack_probing = true;28 child_gen->want_stack_check = WantStackCheckDisabled;
29 child_gen->verbose_tokenize = parent_gen->verbose_tokenize;29 child_gen->verbose_tokenize = parent_gen->verbose_tokenize;
30 child_gen->verbose_ast = parent_gen->verbose_ast;30 child_gen->verbose_ast = parent_gen->verbose_ast;
31 child_gen->verbose_link = parent_gen->verbose_link;31 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) {...@@ -55,7 +55,8 @@ static int print_full_usage(const char *arg0, FILE *file, int return_code) {
55 " --disable-gen-h do not generate a C header file (.h)\n"55 " --disable-gen-h do not generate a C header file (.h)\n"
56 " --disable-valgrind omit valgrind client requests in debug builds\n"56 " --disable-valgrind omit valgrind client requests in debug builds\n"
57 " --enable-valgrind include valgrind client requests release builds\n"57 " --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"
59 " --emit [asm|bin|llvm-ir] emit a specific file format as compilation output\n"60 " --emit [asm|bin|llvm-ir] emit a specific file format as compilation output\n"
60 " -fPIC enable Position Independent Code\n"61 " -fPIC enable Position Independent Code\n"
61 " -fno-PIC disable Position Independent Code\n"62 " -fno-PIC disable Position Independent Code\n"
...@@ -443,12 +444,12 @@ int main(int argc, char **argv) {...@@ -443,12 +444,12 @@ int main(int argc, char **argv) {
443 bool want_single_threaded = false;444 bool want_single_threaded = false;
444 bool disable_gen_h = false;445 bool disable_gen_h = false;
445 bool bundle_compiler_rt = false;446 bool bundle_compiler_rt = false;
446 bool disable_stack_probing = false;
447 Buf *override_std_dir = nullptr;447 Buf *override_std_dir = nullptr;
448 Buf *override_lib_dir = nullptr;448 Buf *override_lib_dir = nullptr;
449 Buf *main_pkg_path = nullptr;449 Buf *main_pkg_path = nullptr;
450 ValgrindSupport valgrind_support = ValgrindSupportAuto;450 ValgrindSupport valgrind_support = ValgrindSupportAuto;
451 WantPIC want_pic = WantPICAuto;451 WantPIC want_pic = WantPICAuto;
452 WantStackCheck want_stack_check = WantStackCheckAuto;
452453
453 ZigList<const char *> llvm_argv = {0};454 ZigList<const char *> llvm_argv = {0};
454 llvm_argv.append("zig (LLVM option parsing)");455 llvm_argv.append("zig (LLVM option parsing)");
...@@ -648,6 +649,10 @@ int main(int argc, char **argv) {...@@ -648,6 +649,10 @@ int main(int argc, char **argv) {
648 want_pic = WantPICEnabled;649 want_pic = WantPICEnabled;
649 } else if (strcmp(arg, "-fno-PIC") == 0) {650 } else if (strcmp(arg, "-fno-PIC") == 0) {
650 want_pic = WantPICDisabled;651 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;
651 } else if (strcmp(arg, "--system-linker-hack") == 0) {656 } else if (strcmp(arg, "--system-linker-hack") == 0) {
652 system_linker_hack = true;657 system_linker_hack = true;
653 } else if (strcmp(arg, "--single-threaded") == 0) {658 } else if (strcmp(arg, "--single-threaded") == 0) {
...@@ -656,8 +661,6 @@ int main(int argc, char **argv) {...@@ -656,8 +661,6 @@ int main(int argc, char **argv) {
656 disable_gen_h = true;661 disable_gen_h = true;
657 } else if (strcmp(arg, "--bundle-compiler-rt") == 0) {662 } else if (strcmp(arg, "--bundle-compiler-rt") == 0) {
658 bundle_compiler_rt = true;663 bundle_compiler_rt = true;
659 } else if (strcmp(arg, "--disable-stack-probing") == 0) {
660 disable_stack_probing = true;
661 } else if (strcmp(arg, "--test-cmd-bin") == 0) {664 } else if (strcmp(arg, "--test-cmd-bin") == 0) {
662 test_exec_args.append(nullptr);665 test_exec_args.append(nullptr);
663 } else if (arg[1] == 'L' && arg[2] != 0) {666 } else if (arg[1] == 'L' && arg[2] != 0) {
...@@ -953,6 +956,7 @@ int main(int argc, char **argv) {...@@ -953,6 +956,7 @@ int main(int argc, char **argv) {
953 out_type, build_mode, override_lib_dir, override_std_dir, nullptr, nullptr);956 out_type, build_mode, override_lib_dir, override_std_dir, nullptr, nullptr);
954 g->valgrind_support = valgrind_support;957 g->valgrind_support = valgrind_support;
955 g->want_pic = want_pic;958 g->want_pic = want_pic;
959 g->want_stack_check = want_stack_check;
956 g->want_single_threaded = want_single_threaded;960 g->want_single_threaded = want_single_threaded;
957 Buf *builtin_source = codegen_generate_builtin_source(g);961 Buf *builtin_source = codegen_generate_builtin_source(g);
958 if (fwrite(buf_ptr(builtin_source), 1, buf_len(builtin_source), stdout) != buf_len(builtin_source)) {962 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) {...@@ -1048,6 +1052,7 @@ int main(int argc, char **argv) {
1048 if (llvm_argv.length >= 2) codegen_set_llvm_argv(g, llvm_argv.items + 1, llvm_argv.length - 2);1052 if (llvm_argv.length >= 2) codegen_set_llvm_argv(g, llvm_argv.items + 1, llvm_argv.length - 2);
1049 g->valgrind_support = valgrind_support;1053 g->valgrind_support = valgrind_support;
1050 g->want_pic = want_pic;1054 g->want_pic = want_pic;
1055 g->want_stack_check = want_stack_check;
1051 g->subsystem = subsystem;1056 g->subsystem = subsystem;
10521057
1053 g->enable_time_report = timing_info;1058 g->enable_time_report = timing_info;
...@@ -1074,7 +1079,6 @@ int main(int argc, char **argv) {...@@ -1074,7 +1079,6 @@ int main(int argc, char **argv) {
1074 g->output_dir = output_dir;1079 g->output_dir = output_dir;
1075 g->disable_gen_h = disable_gen_h;1080 g->disable_gen_h = disable_gen_h;
1076 g->bundle_compiler_rt = bundle_compiler_rt;1081 g->bundle_compiler_rt = bundle_compiler_rt;
1077 g->disable_stack_probing = disable_stack_probing;
1078 codegen_set_errmsg_color(g, color);1082 codegen_set_errmsg_color(g, color);
1079 g->system_linker_hack = system_linker_hack;1083 g->system_linker_hack = system_linker_hack;
10801084
src/target.cpp+4
...@@ -1356,6 +1356,10 @@ bool target_supports_fpic(const ZigTarget *target) {...@@ -1356,6 +1356,10 @@ bool target_supports_fpic(const ZigTarget *target) {
1356 return target->os != OsWindows;1356 return target->os != OsWindows;
1357}1357}
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
1359bool target_requires_pic(const ZigTarget *target, bool linking_libc) {1363bool target_requires_pic(const ZigTarget *target, bool linking_libc) {
1360 // This function returns whether non-pic code is completely invalid on the given target.1364 // This function returns whether non-pic code is completely invalid on the given target.
1361 return target->os == OsWindows || target_os_requires_libc(target->os) ||1365 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);...@@ -172,6 +172,7 @@ bool target_is_glibc(const ZigTarget *target);
172bool target_is_musl(const ZigTarget *target);172bool target_is_musl(const ZigTarget *target);
173bool target_is_wasm(const ZigTarget *target);173bool target_is_wasm(const ZigTarget *target);
174bool target_is_single_threaded(const ZigTarget *target);174bool target_is_single_threaded(const ZigTarget *target);
175bool target_supports_stack_probing(const ZigTarget *target);
175176
176uint32_t target_arch_pointer_bit_width(ZigLLVM_ArchType arch);177uint32_t target_arch_pointer_bit_width(ZigLLVM_ArchType arch);
177178