Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

On Mac, zig build fails with SIGSEGV when including Chipmunk headers #4250

Closed
jpochyla opened this issue Jan 20, 2020 · 3 comments · Fixed by #4253
Closed

On Mac, zig build fails with SIGSEGV when including Chipmunk headers #4250

jpochyla opened this issue Jan 20, 2020 · 3 comments · Fixed by #4253

Comments

@jpochyla
Copy link

jpochyla commented Jan 20, 2020

main.zig:

const c = @cImport({
    @cInclude("chipmunk/chipmunk.h");
});

pub fn main() anyerror!void {
	// commenting out following line fixes the crash
    const space: *c.cpSpace = undefined;
}

build.zig:

const Builder = @import("std").build.Builder;

pub fn build(b: *Builder) void {
    const mode = b.standardReleaseOptions();
    const exe = b.addExecutable("testcase", "src/main.zig");
    exe.setBuildMode(mode);

	// required by chipmunk/chipmunk_types.h 
    exe.linkFramework("ApplicationServices");
    exe.addFrameworkDir("/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks");

	// homebrew Chipmunk headers
    exe.addIncludeDir("/usr/local/include");

    exe.install();

    const run_cmd = exe.run();
    run_cmd.step.dependOn(b.getInstallStep());

    const run_step = b.step("run", "Run the app");
    run_step.dependOn(&run_cmd.step);
}

Compiling:

❯ /usr/local/Cellar/zig/HEAD-c522699/bin/zig build-exe /Users/jpochyla/Projects/jpochyla/testcase/src/main.zig --cache-dir /Users/jpochyla/Projects/jpochyla/testcase/zig-cache --name testcase -I /usr/local/include -F /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks -framework ApplicationServices --cache on
fish: '/usr/local/Cellar/zig/HEAD-c522…' terminated by signal SIGSEGV (Address boundary error)

Platform: MacOS 10.14.6
Zig: 0.5.0+c522699
Chipmunk: 7.0.3 from Homebrew

@LemonBoy
Copy link
Contributor

I cannot reproduce the problem on linux, can you get a backtrace using gdb / lldb ?

@mikdusan
Copy link
Member

mikdusan commented Jan 20, 2020

reproduced on macOS 10.15.2, zig 0.5.0+c522699f2, chipmunk 7.0.3:

(lldb) target create "zig"
Current executable set to 'zig' (x86_64).
(lldb) settings set -- target.run-args  "build-obj" "main.zig" "-I" "Chipmunk-7.0.3/include" "-F" "/Applications/Xcode_11.3.1.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks"
Process 29126 launched: '/Users/mike/project/zig/work/_build/zig' (x86_64)
Process 29126 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x16e07cb)
    frame #0: 0x00000001043f72d3 zig`llvm::APInt::countLeadingZerosSlowCase() const + 35
zig`llvm::APInt::countLeadingZerosSlowCase:
->  0x1043f72d3 <+35>: movq   (%rsi,%rdi,8), %rdi
    0x1043f72d7 <+39>: testq  %rdi, %rdi
    0x1043f72da <+42>: jne    0x1043f72e9               ; <+57>
    0x1043f72dc <+44>: addl   $0x40, %eax
Target 0: (zig) stopped.
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x16e07cb)
  * frame #0: 0x00000001043f72d3 zig`llvm::APInt::countLeadingZerosSlowCase() const + 35
    frame #1: 0x000000010032b266 zig`llvm::APInt::countLeadingZeros(this=0x000000010a5600b8) const at APInt.h:1600:12
    frame #2: 0x000000010032b1fb zig`llvm::APInt::getActiveBits(this=0x000000010a5600b8) const at APInt.h:1532:54
    frame #3: 0x000000010032b101 zig`llvm::APInt::ugt(this=0x000000010a5600b8, RHS=18446744073709551615) const at APInt.h:1264:32
    frame #4: 0x00000001003227bf zig`llvm::APInt::getLimitedValue(this=0x000000010a5600b8, Limit=18446744073709551615) const at APInt.h:482:12
    frame #5: 0x0000000100322785 zig`::ZigClangAPInt_getLimitedValue(self=0x000000010a5600b8, limit=18446744073709551615) at zig_clang.cpp:2055:20
    frame #6: 0x00000001002ba367 zig`translate_c.transInitListExprArray(rp=translate_c.RestorePoint @ 0x00007ffeefbfd108, scope=0x000000010b9b8ef8, loc=<unavailable>, expr=0x00000001095432b0, ty=0x000000010a560090, used=used) at translate_c.zig:1987:52
    frame #7: 0x000000010027af22 zig`translate_c.transInitListExpr(rp=translate_c.RestorePoint @ 0x00007ffeefbfd108, scope=0x000000010b9b8ef8, expr=0x00000001095432b0, used=used) at translate_c.zig:2062:38
    frame #8: 0x000000010020521c zig`translate_c.transStmt(rp=translate_c.RestorePoint @ 0x00007ffeefbfd108, scope=0x000000010b9b8ef8, stmt=0x00000001095432b0, result_used=used, lrvalue=r_value) at translate_c.zig:1026:55
    frame #9: 0x0000000100216819 zig`translate_c.transExpr(rp=translate_c.RestorePoint @ 0x00007ffeefbfd108, scope=0x000000010b9b8ef8, expr=0x00000001095432b0, used=used, lrvalue=r_value) at translate_c.zig:1877:21
    frame #10: 0x00000001001c067e zig`translate_c.transExprCoercing(rp=translate_c.RestorePoint @ 0x00007ffeefbfd108, scope=0x000000010b9b8ef8, expr=0x00000001095432b0, used=used, lrvalue=r_value) at translate_c.zig:1904:21
    frame #11: 0x000000010016b6fe zig`translate_c.visitVarDecl(c=0x00007ffeefbfd820, var_decl=0x0000000109542f48) at translate_c.zig:580:30
    frame #12: 0x000000010014bf6e zig`translate_c.declVisitor(c=0x00007ffeefbfd820, decl=0x0000000109542f48) at translate_c.zig:405:32
    frame #13: 0x0000000100147b61 zig`translate_c.declVisitorC(context=0x00007ffeefbfd820, decl=0x0000000109542f48) at translate_c.zig:376:16
    frame #14: 0x00000001006bfb69 zig`clang::ASTUnit::visitLocalTopLevelDecls(void*, bool (*)(void*, clang::Decl const*)) + 265
    frame #15: 0x000000010031fd00 zig`::ZigClangASTUnit_visitLocalTopLevelDecls(self=0x000000010a066c00, context=0x00007ffeefbfd820, Fn=(zig`translate_c.declVisitorC at translate_c.zig:374))(void *, const ZigClangDecl *)) at zig_clang.cpp:1524:54
    frame #16: 0x0000000100140600 zig`translate_c.translate(backing_allocator=0x00000001059f77c8, args_begin=0x0000000109c2f510, args_end=0x0000000109c2f5f8, errors=0x00007ffeefbfda00, resources_path="/Users/mike/project/zig/work/lib/include") at translate_c.zig:315:49
    frame #17: 0x000000010013fb9c zig`stage2_translate_c(out_ast=0x00007ffeefbfddd0, out_errors_ptr=0x00007ffeefbfdde0, out_errors_len=0x00007ffeefbfddd8, args_begin=0x0000000109c2f510, args_end=0x0000000109c2f5f8, resources_path="/Users/mike/project/zig/work/lib/include") at stage1.zig:99:38
    frame #18: 0x00000001000c3de7 zig`ir_analyze_instruction_c_import(ira=0x0000000109c2ea60, instruction=0x000000010989fee0) at ir.cpp:23945:20
    frame #19: 0x0000000100085337 zig`ir_analyze_instruction_base(ira=0x0000000109c2ea60, instruction=0x000000010989fee0) at ir.cpp:28776:20
    frame #20: 0x0000000100083ecb zig`ir_analyze(codegen=0x000000010a01e400, old_exec=0x0000000109a1ae30, new_exec=0x0000000109a19bf0, expected_type=0x0000000000000000, expected_type_source_node=0x0000000000000000) at ir.cpp:29027:42
    frame #21: 0x00000001000824df zig`ir_eval_const_value(codegen=0x000000010a01e400, scope=0x0000000109829ee0, node=0x000000010982a330, expected_type=0x0000000000000000, backward_branch_count=0x00007ffeefbfe180, backward_branch_quota=0x00007ffeefbfe178, fn_entry=0x0000000000000000, c_import_buf=0x0000000000000000, source_node=0x000000010982a330, exec_name=0x000000010a04ae68, parent_exec=0x0000000000000000, expected_type_source_node=0x0000000000000000, undef_allowed=LazyOk) at ir.cpp:12097:28
    frame #22: 0x0000000100010f21 zig`analyze_const_value(g=0x000000010a01e400, scope=0x0000000109829ee0, node=0x000000010982a330, type_entry=0x0000000000000000, type_name=0x000000010a04ae68, undef=LazyOk) at analyze.cpp:1107:12
    frame #23: 0x0000000100014afe zig`resolve_decl_var(g=0x000000010a01e400, tld_var=0x000000010982ad10, allow_lazy=true) at analyze.cpp:3936:22
    frame #24: 0x0000000100012f5f zig`resolve_top_level_decl(g=0x000000010a01e400, tld=0x000000010982ad10, source_node=0x000000010982a870, allow_lazy=true) at analyze.cpp:4128:17
    frame #25: 0x00000001000f5cb1 zig`ir_analyze_decl_ref(ira=0x0000000109a1b780, source_instruction=0x00000001098b1a50, tld=0x000000010982ad10) at ir.cpp:20371:5
    frame #26: 0x00000001000cd325 zig`ir_analyze_instruction_decl_ref(ira=0x0000000109a1b780, instruction=0x00000001098b1a50) at ir.cpp:27400:38
    frame #27: 0x0000000100085751 zig`ir_analyze_instruction_base(ira=0x0000000109a1b780, instruction=0x00000001098b1a50) at ir.cpp:28860:20
    frame #28: 0x0000000100083ecb zig`ir_analyze(codegen=0x000000010a01e400, old_exec=0x00000001098b89a0, new_exec=0x00000001098bd7e0, expected_type=0x00000001098beb30, expected_type_source_node=0x000000010982a5f0) at ir.cpp:29027:42
    frame #29: 0x0000000100027ae1 zig`analyze_fn_ir(g=0x000000010a01e400, fn=0x00000001098bd760, return_type_node=0x000000010982a5f0) at analyze.cpp:4721:34
    frame #30: 0x0000000100016d43 zig`analyze_fn_body(g=0x000000010a01e400, fn_table_entry=0x00000001098bd760) at analyze.cpp:4834:5
    frame #31: 0x0000000100017dfe zig`semantic_analyze(g=0x000000010a01e400) at analyze.cpp:4946:13
    frame #32: 0x000000010004ae3f zig`gen_root_source(g=0x000000010a01e400) at codegen.cpp:9511:9
    frame #33: 0x0000000100049101 zig`codegen_build_and_link(g=0x000000010a01e400) at codegen.cpp:10517:13
    frame #34: 0x0000000100006eb5 zig`main(argc=7, argv=0x00007ffeefbff830) at main.cpp:1293:17
    frame #35: 0x00007fff722187fd libdyld.dylib`start + 1

@mikdusan
Copy link
Member

mikdusan commented Jan 20, 2020

Here's a reproduction:

typedef int uuid_t[16];
static const uuid_t UUID_NULL __attribute__ ((unused)) = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

LemonBoy added a commit to LemonBoy/zig that referenced this issue Jan 20, 2020
LemonBoy added a commit to LemonBoy/zig that referenced this issue Jan 21, 2020
andrewrk pushed a commit that referenced this issue Jan 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants