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 {
571571 GlobalLinkageIdLinkOnce,
572572};
573573
574enum SymbolVisibilityId {
575 SymbolVisibilityIdDefault,
576 SymbolVisibilityIdHidden,
577 SymbolVisibilityIdProtected,
578};
579
580574enum TldId {
581575 TldIdVar,
582576 TldIdFn,
......@@ -1660,7 +1654,6 @@ enum FnAnalState {
16601654struct GlobalExport {
16611655 Buf name;
16621656 GlobalLinkageId linkage;
1663 SymbolVisibilityId visibility;
16641657};
16651658
16661659struct ZigFn {
src/stage1/analyze.cpp+5-7
......@@ -3717,15 +3717,14 @@ ZigType *get_test_fn_type(CodeGen *g) {
37173717 return g->test_fn_type;
37183718}
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) {
37213721 GlobalExport *global_export = var->export_list.add_one();
37223722 memset(global_export, 0, sizeof(GlobalExport));
37233723 buf_init_from_str(&global_export->name, symbol_name);
37243724 global_export->linkage = linkage;
3725 global_export->visibility = visibility;
37263725}
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) {
37293728 CallingConvention winapi_cc = g->zig_target->arch == ZigLLVM_x86
37303729 ? CallingConventionStdcall
37313730 : CallingConventionC;
......@@ -3750,7 +3749,6 @@ void add_fn_export(CodeGen *g, ZigFn *fn_table_entry, const char *symbol_name, G
37503749 memset(fn_export, 0, sizeof(GlobalExport));
37513750 buf_init_from_str(&fn_export->name, symbol_name);
37523751 fn_export->linkage = linkage;
3753 fn_export->visibility = visibility;
37543752}
37553753
37563754static void resolve_decl_fn(CodeGen *g, TldFn *tld_fn) {
......@@ -3854,13 +3852,13 @@ static void resolve_decl_fn(CodeGen *g, TldFn *tld_fn) {
38543852 case CallingConventionWin64:
38553853 case CallingConventionPtxKernel:
38563854 add_fn_export(g, fn_table_entry, buf_ptr(&fn_table_entry->symbol_name),
3857 GlobalLinkageIdStrong, SymbolVisibilityIdDefault, fn_cc);
3855 GlobalLinkageIdStrong, fn_cc);
38583856 break;
38593857 case CallingConventionUnspecified:
38603858 // An exported function without a specific calling
38613859 // convention defaults to C
38623860 add_fn_export(g, fn_table_entry, buf_ptr(&fn_table_entry->symbol_name),
3863 GlobalLinkageIdStrong, SymbolVisibilityIdDefault, CallingConventionC);
3861 GlobalLinkageIdStrong, CallingConventionC);
38643862 break;
38653863 }
38663864 }
......@@ -4323,7 +4321,7 @@ static void resolve_decl_var(CodeGen *g, TldVar *tld_var, bool allow_lazy) {
43234321
43244322 if (is_export) {
43254323 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);
43274325 }
43284326
43294327 if (is_extern) {
src/stage1/analyze.hpp+2-2
......@@ -221,8 +221,8 @@ ZigType *get_align_amt_type(CodeGen *g);
221221ZigPackage *new_anonymous_package(void);
222222
223223Buf *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);
225void add_var_export(CodeGen *g, ZigVar *fn_table_entry, const char *symbol_name, GlobalLinkageId linkage, SymbolVisibilityId visibility);
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);
226226
227227
228228ZigValue *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) {
242242 zig_unreachable();
243243}
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
257245struct CalcLLVMFieldIndex {
258246 uint32_t offset;
259247 uint32_t field_index;
......@@ -412,7 +400,6 @@ static LLVMValueRef make_fn_llvm_value(CodeGen *g, ZigFn *fn) {
412400 const char *unmangled_name = buf_ptr(&fn->symbol_name);
413401 const char *symbol_name;
414402 GlobalLinkageId linkage;
415 SymbolVisibilityId visibility = SymbolVisibilityIdDefault;
416403 if (fn->body_node == nullptr) {
417404 symbol_name = unmangled_name;
418405 linkage = GlobalLinkageIdStrong;
......@@ -423,7 +410,6 @@ static LLVMValueRef make_fn_llvm_value(CodeGen *g, ZigFn *fn) {
423410 GlobalExport *fn_export = &fn->export_list.items[0];
424411 symbol_name = buf_ptr(&fn_export->name);
425412 linkage = fn_export->linkage;
426 visibility = fn_export->visibility;
427413 }
428414
429415 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) {
546532 LLVMSetUnnamedAddr(llvm_fn, true);
547533 }
548534
549 LLVMSetVisibility(llvm_fn, to_llvm_visibility(visibility));
550
551535 ZigType *return_type = fn_type->data.fn.fn_type_id.return_type;
552536 if (return_type->id == ZigTypeIdUnreachable) {
553537 addLLVMFnAttr(llvm_fn, "noreturn");
......@@ -8967,7 +8951,6 @@ static void do_code_gen(CodeGen *g) {
89678951 assert(var->decl_node);
89688952
89698953 GlobalLinkageId linkage;
8970 SymbolVisibilityId visibility = SymbolVisibilityIdDefault;
89718954 const char *unmangled_name = var->name;
89728955 const char *symbol_name;
89738956 if (var->export_list.length == 0) {
......@@ -8982,7 +8965,6 @@ static void do_code_gen(CodeGen *g) {
89828965 GlobalExport *global_export = &var->export_list.items[0];
89838966 symbol_name = buf_ptr(&global_export->name);
89848967 linkage = global_export->linkage;
8985 visibility = global_export->visibility;
89868968 }
89878969
89888970 LLVMValueRef global_value;
......@@ -9028,8 +9010,6 @@ static void do_code_gen(CodeGen *g) {
90289010 set_global_tls(g, var, global_value);
90299011 }
90309012
9031 LLVMSetVisibility(global_value, to_llvm_visibility(visibility));
9032
90339013 var->value_ref = global_value;
90349014
90359015 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
86358635 return true;
86368636}
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
86578638static bool ir_resolve_float_mode(IrAnalyze *ira, Stage1AirInst *value, FloatMode *out) {
86588639 if (type_is_invalid(value->value->type))
86598640 return false;
......@@ -11680,12 +11661,6 @@ static Stage1AirInst *ir_analyze_instruction_export(IrAnalyze *ira, Stage1ZirIns
1168011661 if (type_is_invalid(section_inst->value->type))
1168111662 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
1168911664 // The `section` field is optional, we have to unwrap it first
1169011665 Stage1AirInst *non_null_check = ir_analyze_test_non_null(ira, instruction->base.scope, instruction->base.source_node, section_inst);
1169111666 bool is_non_null;
......@@ -11714,10 +11689,6 @@ static Stage1AirInst *ir_analyze_instruction_export(IrAnalyze *ira, Stage1ZirIns
1171411689 if (!ir_resolve_global_linkage(ira, linkage_inst, &global_linkage_id))
1171511690 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
1172111692 Buf *section_name = nullptr;
1172211693 if (section_str_inst != nullptr && !(section_name = ir_resolve_str(ira, section_str_inst)))
1172311694 return ira->codegen->invalid_inst_gen;
......@@ -11780,7 +11751,7 @@ static Stage1AirInst *ir_analyze_instruction_export(IrAnalyze *ira, Stage1ZirIns
1178011751 case CallingConventionSysV:
1178111752 case CallingConventionWin64:
1178211753 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);
1178411755 fn_entry->section_name = section_name;
1178511756 break;
1178611757 }
......@@ -11927,7 +11898,7 @@ static Stage1AirInst *ir_analyze_instruction_export(IrAnalyze *ira, Stage1ZirIns
1192711898 if (load_ptr->ptr->id == Stage1AirInstIdVarPtr) {
1192811899 Stage1AirInstVarPtr *var_ptr = reinterpret_cast<Stage1AirInstVarPtr *>(load_ptr->ptr);
1192911900 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);
1193111902 var->section_name = section_name;
1193211903 }
1193311904 }