authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-05-10 16:43:51-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-05-10 16:43:51-07:00
log1ff9a18cd327027164073f1ebf9c2cca6c3de876
tree97cc523ac0c060e892f4df9cac6d62beb07d01e4
parentc4c5020f0267758c7eb127689177cf1a70fb6d97

stage1: back out the broken visibility changes

``` $ valgrind ./zig test ../test/behavior.zig -target powerpc-linux-musl -lc -I../test ==2828778== Invalid read of size 1 ==2828778== at 0x6EA0265: LLVMSetVisibility (in /home/andy/Downloads/zig/build/zig) ==2828778== by 0x1BCE60B: do_code_gen(CodeGen*) (codegen.cpp:9031) ==2828778== by 0x1BD51E2: codegen_build_object(CodeGen*) (codegen.cpp:10610) ==2828778== by 0x1BA5C17: zig_stage1_build_object (stage1.cpp:132) ==2828778== by 0xE61E24: Module.build_object (stage1.zig:149) ==2828778== by 0xC3D4CE: Compilation.updateStage1Module (Compilation.zig:5025) ==2828778== by 0xC3117E: Compilation.performAllTheWork (Compilation.zig:2691) ==2828778== by 0xC2A3ED: Compilation.update (Compilation.zig:2098) ==2828778== by 0xBB9D1F: main.updateModule (main.zig:3104) ==2828778== by 0xB16B75: main.buildOutputType (main.zig:2793) ==2828778== by 0xAD0526: main.mainArgs (main.zig:225) ==2828778== by 0xACFCB9: main (stage1.zig:48) ``` Since the plan is to ship stage3 for Zig 0.10.0, the stage1 implementation of this hardly matters.

5 files changed, 9 insertions(+), 67 deletions(-)

src/stage1/all_types.hpp-7
...@@ -571,12 +571,6 @@ enum GlobalLinkageId {...@@ -571,12 +571,6 @@ enum GlobalLinkageId {
571 GlobalLinkageIdLinkOnce,571 GlobalLinkageIdLinkOnce,
572};572};
573573
574enum SymbolVisibilityId {
575 SymbolVisibilityIdDefault,
576 SymbolVisibilityIdHidden,
577 SymbolVisibilityIdProtected,
578};
579
580enum TldId {574enum TldId {
581 TldIdVar,575 TldIdVar,
582 TldIdFn,576 TldIdFn,
...@@ -1660,7 +1654,6 @@ enum FnAnalState {...@@ -1660,7 +1654,6 @@ enum FnAnalState {
1660struct GlobalExport {1654struct GlobalExport {
1661 Buf name;1655 Buf name;
1662 GlobalLinkageId linkage;1656 GlobalLinkageId linkage;
1663 SymbolVisibilityId visibility;
1664};1657};
16651658
1666struct ZigFn {1659struct ZigFn {
src/stage1/analyze.cpp+5-7
...@@ -3717,15 +3717,14 @@ ZigType *get_test_fn_type(CodeGen *g) {...@@ -3717,15 +3717,14 @@ ZigType *get_test_fn_type(CodeGen *g) {
3717 return g->test_fn_type;3717 return g->test_fn_type;
3718}3718}
37193719
3720void add_var_export(CodeGen *g, ZigVar *var, const char *symbol_name, GlobalLinkageId linkage, SymbolVisibilityId visibility) {3720void add_var_export(CodeGen *g, ZigVar *var, const char *symbol_name, GlobalLinkageId linkage) {
3721 GlobalExport *global_export = var->export_list.add_one();3721 GlobalExport *global_export = var->export_list.add_one();
3722 memset(global_export, 0, sizeof(GlobalExport));3722 memset(global_export, 0, sizeof(GlobalExport));
3723 buf_init_from_str(&global_export->name, symbol_name);3723 buf_init_from_str(&global_export->name, symbol_name);
3724 global_export->linkage = linkage;3724 global_export->linkage = linkage;
3725 global_export->visibility = visibility;
3726}3725}
37273726
3728void add_fn_export(CodeGen *g, ZigFn *fn_table_entry, const char *symbol_name, GlobalLinkageId linkage, SymbolVisibilityId visibility, CallingConvention cc) {3727void add_fn_export(CodeGen *g, ZigFn *fn_table_entry, const char *symbol_name, GlobalLinkageId linkage, CallingConvention cc) {
3729 CallingConvention winapi_cc = g->zig_target->arch == ZigLLVM_x863728 CallingConvention winapi_cc = g->zig_target->arch == ZigLLVM_x86
3730 ? CallingConventionStdcall3729 ? CallingConventionStdcall
3731 : CallingConventionC;3730 : CallingConventionC;
...@@ -3750,7 +3749,6 @@ void add_fn_export(CodeGen *g, ZigFn *fn_table_entry, const char *symbol_name, G...@@ -3750,7 +3749,6 @@ void add_fn_export(CodeGen *g, ZigFn *fn_table_entry, const char *symbol_name, G
3750 memset(fn_export, 0, sizeof(GlobalExport));3749 memset(fn_export, 0, sizeof(GlobalExport));
3751 buf_init_from_str(&fn_export->name, symbol_name);3750 buf_init_from_str(&fn_export->name, symbol_name);
3752 fn_export->linkage = linkage;3751 fn_export->linkage = linkage;
3753 fn_export->visibility = visibility;
3754}3752}
37553753
3756static void resolve_decl_fn(CodeGen *g, TldFn *tld_fn) {3754static void resolve_decl_fn(CodeGen *g, TldFn *tld_fn) {
...@@ -3854,13 +3852,13 @@ static void resolve_decl_fn(CodeGen *g, TldFn *tld_fn) {...@@ -3854,13 +3852,13 @@ static void resolve_decl_fn(CodeGen *g, TldFn *tld_fn) {
3854 case CallingConventionWin64:3852 case CallingConventionWin64:
3855 case CallingConventionPtxKernel:3853 case CallingConventionPtxKernel:
3856 add_fn_export(g, fn_table_entry, buf_ptr(&fn_table_entry->symbol_name),3854 add_fn_export(g, fn_table_entry, buf_ptr(&fn_table_entry->symbol_name),
3857 GlobalLinkageIdStrong, SymbolVisibilityIdDefault, fn_cc);3855 GlobalLinkageIdStrong, fn_cc);
3858 break;3856 break;
3859 case CallingConventionUnspecified:3857 case CallingConventionUnspecified:
3860 // An exported function without a specific calling3858 // An exported function without a specific calling
3861 // convention defaults to C3859 // convention defaults to C
3862 add_fn_export(g, fn_table_entry, buf_ptr(&fn_table_entry->symbol_name),3860 add_fn_export(g, fn_table_entry, buf_ptr(&fn_table_entry->symbol_name),
3863 GlobalLinkageIdStrong, SymbolVisibilityIdDefault, CallingConventionC);3861 GlobalLinkageIdStrong, CallingConventionC);
3864 break;3862 break;
3865 }3863 }
3866 }3864 }
...@@ -4323,7 +4321,7 @@ static void resolve_decl_var(CodeGen *g, TldVar *tld_var, bool allow_lazy) {...@@ -4323,7 +4321,7 @@ static void resolve_decl_var(CodeGen *g, TldVar *tld_var, bool allow_lazy) {
43234321
4324 if (is_export) {4322 if (is_export) {
4325 validate_export_var_type(g, type, source_node);4323 validate_export_var_type(g, type, source_node);
4326 add_var_export(g, tld_var->var, tld_var->var->name, GlobalLinkageIdStrong, SymbolVisibilityIdDefault);4324 add_var_export(g, tld_var->var, tld_var->var->name, GlobalLinkageIdStrong);
4327 }4325 }
43284326
4329 if (is_extern) {4327 if (is_extern) {
src/stage1/analyze.hpp+2-2
...@@ -221,8 +221,8 @@ ZigType *get_align_amt_type(CodeGen *g);...@@ -221,8 +221,8 @@ ZigType *get_align_amt_type(CodeGen *g);
221ZigPackage *new_anonymous_package(void);221ZigPackage *new_anonymous_package(void);
222222
223Buf *const_value_to_buffer(ZigValue *const_val);223Buf *const_value_to_buffer(ZigValue *const_val);
224void add_fn_export(CodeGen *g, ZigFn *fn_table_entry, const char *symbol_name, GlobalLinkageId linkage, SymbolVisibilityId visibility, CallingConvention cc);224void add_fn_export(CodeGen *g, ZigFn *fn_table_entry, const char *symbol_name, GlobalLinkageId linkage, CallingConvention cc);
225void add_var_export(CodeGen *g, ZigVar *fn_table_entry, const char *symbol_name, GlobalLinkageId linkage, SymbolVisibilityId visibility);225void add_var_export(CodeGen *g, ZigVar *fn_table_entry, const char *symbol_name, GlobalLinkageId linkage);
226226
227227
228ZigValue *get_builtin_value(CodeGen *codegen, const char *name);228ZigValue *get_builtin_value(CodeGen *codegen, const char *name);
src/stage1/codegen.cpp-20
...@@ -242,18 +242,6 @@ static LLVMLinkage to_llvm_linkage(GlobalLinkageId id, bool is_extern) {...@@ -242,18 +242,6 @@ static LLVMLinkage to_llvm_linkage(GlobalLinkageId id, bool is_extern) {
242 zig_unreachable();242 zig_unreachable();
243}243}
244244
245static LLVMVisibility to_llvm_visibility(SymbolVisibilityId id) {
246 switch (id) {
247 case SymbolVisibilityIdDefault:
248 return LLVMDefaultVisibility;
249 case SymbolVisibilityIdHidden:
250 return LLVMHiddenVisibility;
251 case SymbolVisibilityIdProtected:
252 return LLVMProtectedVisibility;
253 }
254 zig_unreachable();
255}
256
257struct CalcLLVMFieldIndex {245struct CalcLLVMFieldIndex {
258 uint32_t offset;246 uint32_t offset;
259 uint32_t field_index;247 uint32_t field_index;
...@@ -412,7 +400,6 @@ static LLVMValueRef make_fn_llvm_value(CodeGen *g, ZigFn *fn) {...@@ -412,7 +400,6 @@ static LLVMValueRef make_fn_llvm_value(CodeGen *g, ZigFn *fn) {
412 const char *unmangled_name = buf_ptr(&fn->symbol_name);400 const char *unmangled_name = buf_ptr(&fn->symbol_name);
413 const char *symbol_name;401 const char *symbol_name;
414 GlobalLinkageId linkage;402 GlobalLinkageId linkage;
415 SymbolVisibilityId visibility = SymbolVisibilityIdDefault;
416 if (fn->body_node == nullptr) {403 if (fn->body_node == nullptr) {
417 symbol_name = unmangled_name;404 symbol_name = unmangled_name;
418 linkage = GlobalLinkageIdStrong;405 linkage = GlobalLinkageIdStrong;
...@@ -423,7 +410,6 @@ static LLVMValueRef make_fn_llvm_value(CodeGen *g, ZigFn *fn) {...@@ -423,7 +410,6 @@ static LLVMValueRef make_fn_llvm_value(CodeGen *g, ZigFn *fn) {
423 GlobalExport *fn_export = &fn->export_list.items[0];410 GlobalExport *fn_export = &fn->export_list.items[0];
424 symbol_name = buf_ptr(&fn_export->name);411 symbol_name = buf_ptr(&fn_export->name);
425 linkage = fn_export->linkage;412 linkage = fn_export->linkage;
426 visibility = fn_export->visibility;
427 }413 }
428414
429 CallingConvention cc = fn->type_entry->data.fn.fn_type_id.cc;415 CallingConvention cc = fn->type_entry->data.fn.fn_type_id.cc;
...@@ -546,8 +532,6 @@ static LLVMValueRef make_fn_llvm_value(CodeGen *g, ZigFn *fn) {...@@ -546,8 +532,6 @@ static LLVMValueRef make_fn_llvm_value(CodeGen *g, ZigFn *fn) {
546 LLVMSetUnnamedAddr(llvm_fn, true);532 LLVMSetUnnamedAddr(llvm_fn, true);
547 }533 }
548534
549 LLVMSetVisibility(llvm_fn, to_llvm_visibility(visibility));
550
551 ZigType *return_type = fn_type->data.fn.fn_type_id.return_type;535 ZigType *return_type = fn_type->data.fn.fn_type_id.return_type;
552 if (return_type->id == ZigTypeIdUnreachable) {536 if (return_type->id == ZigTypeIdUnreachable) {
553 addLLVMFnAttr(llvm_fn, "noreturn");537 addLLVMFnAttr(llvm_fn, "noreturn");
...@@ -8967,7 +8951,6 @@ static void do_code_gen(CodeGen *g) {...@@ -8967,7 +8951,6 @@ static void do_code_gen(CodeGen *g) {
8967 assert(var->decl_node);8951 assert(var->decl_node);
89688952
8969 GlobalLinkageId linkage;8953 GlobalLinkageId linkage;
8970 SymbolVisibilityId visibility = SymbolVisibilityIdDefault;
8971 const char *unmangled_name = var->name;8954 const char *unmangled_name = var->name;
8972 const char *symbol_name;8955 const char *symbol_name;
8973 if (var->export_list.length == 0) {8956 if (var->export_list.length == 0) {
...@@ -8982,7 +8965,6 @@ static void do_code_gen(CodeGen *g) {...@@ -8982,7 +8965,6 @@ static void do_code_gen(CodeGen *g) {
8982 GlobalExport *global_export = &var->export_list.items[0];8965 GlobalExport *global_export = &var->export_list.items[0];
8983 symbol_name = buf_ptr(&global_export->name);8966 symbol_name = buf_ptr(&global_export->name);
8984 linkage = global_export->linkage;8967 linkage = global_export->linkage;
8985 visibility = global_export->visibility;
8986 }8968 }
89878969
8988 LLVMValueRef global_value;8970 LLVMValueRef global_value;
...@@ -9028,8 +9010,6 @@ static void do_code_gen(CodeGen *g) {...@@ -9028,8 +9010,6 @@ static void do_code_gen(CodeGen *g) {
9028 set_global_tls(g, var, global_value);9010 set_global_tls(g, var, global_value);
9029 }9011 }
90309012
9031 LLVMSetVisibility(global_value, to_llvm_visibility(visibility));
9032
9033 var->value_ref = global_value;9013 var->value_ref = global_value;
90349014
9035 for (size_t export_i = 1; export_i < var->export_list.length; export_i += 1) {9015 for (size_t export_i = 1; export_i < var->export_list.length; export_i += 1) {
src/stage1/ir.cpp+2-31
...@@ -8635,25 +8635,6 @@ static bool ir_resolve_global_linkage(IrAnalyze *ira, Stage1AirInst *value, Glob...@@ -8635,25 +8635,6 @@ static bool ir_resolve_global_linkage(IrAnalyze *ira, Stage1AirInst *value, Glob
8635 return true;8635 return true;
8636}8636}
86378637
8638static bool ir_resolve_global_visibility(IrAnalyze *ira, Stage1AirInst *value, SymbolVisibilityId *out) {
8639 if (type_is_invalid(value->value->type))
8640 return false;
8641
8642 ZigType *global_visibility_type = get_builtin_type(ira->codegen, "SymbolVisibility");
8643
8644 Stage1AirInst *casted_value = ir_implicit_cast(ira, value, global_visibility_type);
8645 if (type_is_invalid(casted_value->value->type))
8646 return false;
8647
8648 ZigValue *const_val = ir_resolve_const(ira, casted_value, UndefBad);
8649 if (!const_val)
8650 return false;
8651
8652 *out = (SymbolVisibilityId)bigint_as_u32(&const_val->data.x_enum_tag);
8653 return true;
8654}
8655
8656
8657static bool ir_resolve_float_mode(IrAnalyze *ira, Stage1AirInst *value, FloatMode *out) {8638static bool ir_resolve_float_mode(IrAnalyze *ira, Stage1AirInst *value, FloatMode *out) {
8658 if (type_is_invalid(value->value->type))8639 if (type_is_invalid(value->value->type))
8659 return false;8640 return false;
...@@ -11680,12 +11661,6 @@ static Stage1AirInst *ir_analyze_instruction_export(IrAnalyze *ira, Stage1ZirIns...@@ -11680,12 +11661,6 @@ static Stage1AirInst *ir_analyze_instruction_export(IrAnalyze *ira, Stage1ZirIns
11680 if (type_is_invalid(section_inst->value->type))11661 if (type_is_invalid(section_inst->value->type))
11681 return ira->codegen->invalid_inst_gen;11662 return ira->codegen->invalid_inst_gen;
1168211663
11683 TypeStructField *visibility_field = find_struct_type_field(options_type, buf_create_from_str("visibility"));
11684 src_assert(visibility_field != nullptr, instruction->base.source_node);
11685 Stage1AirInst *visibility_inst = ir_analyze_struct_value_field_value(ira, instruction->base.scope, instruction->base.source_node, options, visibility_field);
11686 if (type_is_invalid(visibility_inst->value->type))
11687 return ira->codegen->invalid_inst_gen;
11688
11689 // The `section` field is optional, we have to unwrap it first11664 // The `section` field is optional, we have to unwrap it first
11690 Stage1AirInst *non_null_check = ir_analyze_test_non_null(ira, instruction->base.scope, instruction->base.source_node, section_inst);11665 Stage1AirInst *non_null_check = ir_analyze_test_non_null(ira, instruction->base.scope, instruction->base.source_node, section_inst);
11691 bool is_non_null;11666 bool is_non_null;
...@@ -11714,10 +11689,6 @@ static Stage1AirInst *ir_analyze_instruction_export(IrAnalyze *ira, Stage1ZirIns...@@ -11714,10 +11689,6 @@ static Stage1AirInst *ir_analyze_instruction_export(IrAnalyze *ira, Stage1ZirIns
11714 if (!ir_resolve_global_linkage(ira, linkage_inst, &global_linkage_id))11689 if (!ir_resolve_global_linkage(ira, linkage_inst, &global_linkage_id))
11715 return ira->codegen->invalid_inst_gen;11690 return ira->codegen->invalid_inst_gen;
1171611691
11717 SymbolVisibilityId global_visibility_id;
11718 if (!ir_resolve_global_visibility(ira, visibility_inst, &global_visibility_id))
11719 return ira->codegen->invalid_inst_gen;
11720
11721 Buf *section_name = nullptr;11692 Buf *section_name = nullptr;
11722 if (section_str_inst != nullptr && !(section_name = ir_resolve_str(ira, section_str_inst)))11693 if (section_str_inst != nullptr && !(section_name = ir_resolve_str(ira, section_str_inst)))
11723 return ira->codegen->invalid_inst_gen;11694 return ira->codegen->invalid_inst_gen;
...@@ -11780,7 +11751,7 @@ static Stage1AirInst *ir_analyze_instruction_export(IrAnalyze *ira, Stage1ZirIns...@@ -11780,7 +11751,7 @@ static Stage1AirInst *ir_analyze_instruction_export(IrAnalyze *ira, Stage1ZirIns
11780 case CallingConventionSysV:11751 case CallingConventionSysV:
11781 case CallingConventionWin64:11752 case CallingConventionWin64:
11782 case CallingConventionPtxKernel:11753 case CallingConventionPtxKernel:
11783 add_fn_export(ira->codegen, fn_entry, buf_ptr(symbol_name), global_linkage_id, global_visibility_id, cc);11754 add_fn_export(ira->codegen, fn_entry, buf_ptr(symbol_name), global_linkage_id, cc);
11784 fn_entry->section_name = section_name;11755 fn_entry->section_name = section_name;
11785 break;11756 break;
11786 }11757 }
...@@ -11927,7 +11898,7 @@ static Stage1AirInst *ir_analyze_instruction_export(IrAnalyze *ira, Stage1ZirIns...@@ -11927,7 +11898,7 @@ static Stage1AirInst *ir_analyze_instruction_export(IrAnalyze *ira, Stage1ZirIns
11927 if (load_ptr->ptr->id == Stage1AirInstIdVarPtr) {11898 if (load_ptr->ptr->id == Stage1AirInstIdVarPtr) {
11928 Stage1AirInstVarPtr *var_ptr = reinterpret_cast<Stage1AirInstVarPtr *>(load_ptr->ptr);11899 Stage1AirInstVarPtr *var_ptr = reinterpret_cast<Stage1AirInstVarPtr *>(load_ptr->ptr);
11929 ZigVar *var = var_ptr->var;11900 ZigVar *var = var_ptr->var;
11930 add_var_export(ira->codegen, var, buf_ptr(symbol_name), global_linkage_id, global_visibility_id);11901 add_var_export(ira->codegen, var, buf_ptr(symbol_name), global_linkage_id);
11931 var->section_name = section_name;11902 var->section_name = section_name;
11932 }11903 }
11933 }11904 }