| author | |
| committer | |
| log | 1ff9a18cd327027164073f1ebf9c2cca6c3de876 |
| tree | 97cc523ac0c060e892f4df9cac6d62beb07d01e4 |
| parent | c4c5020f0267758c7eb127689177cf1a70fb6d97 |
```
$ 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 | 571 | GlobalLinkageIdLinkOnce, |
| 572 | 572 | }; |
| 573 | 573 | |
| 574 | enum SymbolVisibilityId { | |
| 575 | SymbolVisibilityIdDefault, | |
| 576 | SymbolVisibilityIdHidden, | |
| 577 | SymbolVisibilityIdProtected, | |
| 578 | }; | |
| 579 | ||
| 580 | 574 | enum TldId { |
| 581 | 575 | TldIdVar, |
| 582 | 576 | TldIdFn, |
| ... | ... | @@ -1660,7 +1654,6 @@ enum FnAnalState { |
| 1660 | 1654 | struct GlobalExport { |
| 1661 | 1655 | Buf name; |
| 1662 | 1656 | GlobalLinkageId linkage; |
| 1663 | SymbolVisibilityId visibility; | |
| 1664 | 1657 | }; |
| 1665 | 1658 | |
| 1666 | 1659 | struct ZigFn { |
src/stage1/analyze.cpp+5-7| ... | ... | @@ -3717,15 +3717,14 @@ ZigType *get_test_fn_type(CodeGen *g) { |
| 3717 | 3717 | return g->test_fn_type; |
| 3718 | 3718 | } |
| 3719 | 3719 | |
| 3720 | void add_var_export(CodeGen *g, ZigVar *var, const char *symbol_name, GlobalLinkageId linkage, SymbolVisibilityId visibility) { | |
| 3720 | void add_var_export(CodeGen *g, ZigVar *var, const char *symbol_name, GlobalLinkageId linkage) { | |
| 3721 | 3721 | GlobalExport *global_export = var->export_list.add_one(); |
| 3722 | 3722 | memset(global_export, 0, sizeof(GlobalExport)); |
| 3723 | 3723 | buf_init_from_str(&global_export->name, symbol_name); |
| 3724 | 3724 | global_export->linkage = linkage; |
| 3725 | global_export->visibility = visibility; | |
| 3726 | 3725 | } |
| 3727 | 3726 | |
| 3728 | void add_fn_export(CodeGen *g, ZigFn *fn_table_entry, const char *symbol_name, GlobalLinkageId linkage, SymbolVisibilityId visibility, CallingConvention cc) { | |
| 3727 | void add_fn_export(CodeGen *g, ZigFn *fn_table_entry, const char *symbol_name, GlobalLinkageId linkage, CallingConvention cc) { | |
| 3729 | 3728 | CallingConvention winapi_cc = g->zig_target->arch == ZigLLVM_x86 |
| 3730 | 3729 | ? CallingConventionStdcall |
| 3731 | 3730 | : CallingConventionC; |
| ... | ... | @@ -3750,7 +3749,6 @@ void add_fn_export(CodeGen *g, ZigFn *fn_table_entry, const char *symbol_name, G |
| 3750 | 3749 | memset(fn_export, 0, sizeof(GlobalExport)); |
| 3751 | 3750 | buf_init_from_str(&fn_export->name, symbol_name); |
| 3752 | 3751 | fn_export->linkage = linkage; |
| 3753 | fn_export->visibility = visibility; | |
| 3754 | 3752 | } |
| 3755 | 3753 | |
| 3756 | 3754 | 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 | 3852 | case CallingConventionWin64: |
| 3855 | 3853 | case CallingConventionPtxKernel: |
| 3856 | 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 | 3856 | break; |
| 3859 | 3857 | case CallingConventionUnspecified: |
| 3860 | 3858 | // An exported function without a specific calling |
| 3861 | 3859 | // convention defaults to C |
| 3862 | 3860 | add_fn_export(g, fn_table_entry, buf_ptr(&fn_table_entry->symbol_name), |
| 3863 | GlobalLinkageIdStrong, SymbolVisibilityIdDefault, CallingConventionC); | |
| 3861 | GlobalLinkageIdStrong, CallingConventionC); | |
| 3864 | 3862 | break; |
| 3865 | 3863 | } |
| 3866 | 3864 | } |
| ... | ... | @@ -4323,7 +4321,7 @@ static void resolve_decl_var(CodeGen *g, TldVar *tld_var, bool allow_lazy) { |
| 4323 | 4321 | |
| 4324 | 4322 | if (is_export) { |
| 4325 | 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 | } |
| 4328 | 4326 | |
| 4329 | 4327 | if (is_extern) { |
src/stage1/analyze.hpp+2-2| ... | ... | @@ -221,8 +221,8 @@ ZigType *get_align_amt_type(CodeGen *g); |
| 221 | 221 | ZigPackage *new_anonymous_package(void); |
| 222 | 222 | |
| 223 | 223 | Buf *const_value_to_buffer(ZigValue *const_val); |
| 224 | void add_fn_export(CodeGen *g, ZigFn *fn_table_entry, const char *symbol_name, GlobalLinkageId linkage, SymbolVisibilityId visibility, CallingConvention cc); | |
| 225 | void add_var_export(CodeGen *g, ZigVar *fn_table_entry, const char *symbol_name, GlobalLinkageId linkage, SymbolVisibilityId visibility); | |
| 224 | void add_fn_export(CodeGen *g, ZigFn *fn_table_entry, const char *symbol_name, GlobalLinkageId linkage, CallingConvention cc); | |
| 225 | void add_var_export(CodeGen *g, ZigVar *fn_table_entry, const char *symbol_name, GlobalLinkageId linkage); | |
| 226 | 226 | |
| 227 | 227 | |
| 228 | 228 | ZigValue *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 | 242 | zig_unreachable(); |
| 243 | 243 | } |
| 244 | 244 | |
| 245 | static 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 | ||
| 257 | 245 | struct CalcLLVMFieldIndex { |
| 258 | 246 | uint32_t offset; |
| 259 | 247 | uint32_t field_index; |
| ... | ... | @@ -412,7 +400,6 @@ static LLVMValueRef make_fn_llvm_value(CodeGen *g, ZigFn *fn) { |
| 412 | 400 | const char *unmangled_name = buf_ptr(&fn->symbol_name); |
| 413 | 401 | const char *symbol_name; |
| 414 | 402 | GlobalLinkageId linkage; |
| 415 | SymbolVisibilityId visibility = SymbolVisibilityIdDefault; | |
| 416 | 403 | if (fn->body_node == nullptr) { |
| 417 | 404 | symbol_name = unmangled_name; |
| 418 | 405 | linkage = GlobalLinkageIdStrong; |
| ... | ... | @@ -423,7 +410,6 @@ static LLVMValueRef make_fn_llvm_value(CodeGen *g, ZigFn *fn) { |
| 423 | 410 | GlobalExport *fn_export = &fn->export_list.items[0]; |
| 424 | 411 | symbol_name = buf_ptr(&fn_export->name); |
| 425 | 412 | linkage = fn_export->linkage; |
| 426 | visibility = fn_export->visibility; | |
| 427 | 413 | } |
| 428 | 414 | |
| 429 | 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 | 532 | LLVMSetUnnamedAddr(llvm_fn, true); |
| 547 | 533 | } |
| 548 | 534 | |
| 549 | LLVMSetVisibility(llvm_fn, to_llvm_visibility(visibility)); | |
| 550 | ||
| 551 | 535 | ZigType *return_type = fn_type->data.fn.fn_type_id.return_type; |
| 552 | 536 | if (return_type->id == ZigTypeIdUnreachable) { |
| 553 | 537 | addLLVMFnAttr(llvm_fn, "noreturn"); |
| ... | ... | @@ -8967,7 +8951,6 @@ static void do_code_gen(CodeGen *g) { |
| 8967 | 8951 | assert(var->decl_node); |
| 8968 | 8952 | |
| 8969 | 8953 | GlobalLinkageId linkage; |
| 8970 | SymbolVisibilityId visibility = SymbolVisibilityIdDefault; | |
| 8971 | 8954 | const char *unmangled_name = var->name; |
| 8972 | 8955 | const char *symbol_name; |
| 8973 | 8956 | if (var->export_list.length == 0) { |
| ... | ... | @@ -8982,7 +8965,6 @@ static void do_code_gen(CodeGen *g) { |
| 8982 | 8965 | GlobalExport *global_export = &var->export_list.items[0]; |
| 8983 | 8966 | symbol_name = buf_ptr(&global_export->name); |
| 8984 | 8967 | linkage = global_export->linkage; |
| 8985 | visibility = global_export->visibility; | |
| 8986 | 8968 | } |
| 8987 | 8969 | |
| 8988 | 8970 | LLVMValueRef global_value; |
| ... | ... | @@ -9028,8 +9010,6 @@ static void do_code_gen(CodeGen *g) { |
| 9028 | 9010 | set_global_tls(g, var, global_value); |
| 9029 | 9011 | } |
| 9030 | 9012 | |
| 9031 | LLVMSetVisibility(global_value, to_llvm_visibility(visibility)); | |
| 9032 | ||
| 9033 | 9013 | var->value_ref = global_value; |
| 9034 | 9014 | |
| 9035 | 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 | 8635 | return true; |
| 8636 | 8636 | } |
| 8637 | 8637 | |
| 8638 | static 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 | ||
| 8657 | 8638 | static bool ir_resolve_float_mode(IrAnalyze *ira, Stage1AirInst *value, FloatMode *out) { |
| 8658 | 8639 | if (type_is_invalid(value->value->type)) |
| 8659 | 8640 | return false; |
| ... | ... | @@ -11680,12 +11661,6 @@ static Stage1AirInst *ir_analyze_instruction_export(IrAnalyze *ira, Stage1ZirIns |
| 11680 | 11661 | if (type_is_invalid(section_inst->value->type)) |
| 11681 | 11662 | return ira->codegen->invalid_inst_gen; |
| 11682 | 11663 | |
| 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 | 11664 | // The `section` field is optional, we have to unwrap it first |
| 11690 | 11665 | Stage1AirInst *non_null_check = ir_analyze_test_non_null(ira, instruction->base.scope, instruction->base.source_node, section_inst); |
| 11691 | 11666 | bool is_non_null; |
| ... | ... | @@ -11714,10 +11689,6 @@ static Stage1AirInst *ir_analyze_instruction_export(IrAnalyze *ira, Stage1ZirIns |
| 11714 | 11689 | if (!ir_resolve_global_linkage(ira, linkage_inst, &global_linkage_id)) |
| 11715 | 11690 | return ira->codegen->invalid_inst_gen; |
| 11716 | 11691 | |
| 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 | 11692 | Buf *section_name = nullptr; |
| 11722 | 11693 | if (section_str_inst != nullptr && !(section_name = ir_resolve_str(ira, section_str_inst))) |
| 11723 | 11694 | return ira->codegen->invalid_inst_gen; |
| ... | ... | @@ -11780,7 +11751,7 @@ static Stage1AirInst *ir_analyze_instruction_export(IrAnalyze *ira, Stage1ZirIns |
| 11780 | 11751 | case CallingConventionSysV: |
| 11781 | 11752 | case CallingConventionWin64: |
| 11782 | 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 | 11755 | fn_entry->section_name = section_name; |
| 11785 | 11756 | break; |
| 11786 | 11757 | } |
| ... | ... | @@ -11927,7 +11898,7 @@ static Stage1AirInst *ir_analyze_instruction_export(IrAnalyze *ira, Stage1ZirIns |
| 11927 | 11898 | if (load_ptr->ptr->id == Stage1AirInstIdVarPtr) { |
| 11928 | 11899 | Stage1AirInstVarPtr *var_ptr = reinterpret_cast<Stage1AirInstVarPtr *>(load_ptr->ptr); |
| 11929 | 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 | 11902 | var->section_name = section_name; |
| 11932 | 11903 | } |
| 11933 | 11904 | } |