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

Bus error on simple code #1217

Closed
tiehuis opened this issue Jul 11, 2018 · 3 comments
Closed

Bus error on simple code #1217

tiehuis opened this issue Jul 11, 2018 · 3 comments
Labels
bug Observed behavior contradicts documented or intended behavior os-freebsd
Milestone

Comments

@tiehuis
Copy link
Member

tiehuis commented Jul 11, 2018

The following code runs and compiles fine under Linux, but fails with a SIGBUS on FreeBSD.

const std = @import("std");

const AvailableOption = struct {
    name: []const u8,
    p0: u128,
};

pub fn option(name: []const u8) AvailableOption {
    return AvailableOption{
        .name = name,
        .p0 = 0,
    };
}

pub fn main() void {
    var a = option("string");
}

A few notes:

  • This only fails under debug builds, all other build settings work fine. Although, I'm not sure if this just a result of this reproduction as I still get this error when running the zig build --build-file ../build.zig test command when specifying a release setting. Is the zig build build runner always compiled in debug mode?
  • The p0 must be present for this to fail and further, it appears to need to be a single type larger than 64-bits. If p0 is builtin.TypeId this still fails, but if it is a u64 this code works fine. This is unrelated to the size of the total struct, as 4 individual u32 fields for example will work fine.
  • The .name field must be set from an argument and not directly via a literal, else this will compile fine.

Assembly

Assembly between Linux and FreeBSD are practically identical so maybe there is some ABI difference that we are missing?

FreeBSD option

main:
.Lfunc_begin290:
	.file	22 "/usr/home/me/src/zig/build/t.zig"
	.loc	22 16 0
	.cfi_startproc
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset %rbp, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register %rbp
	subq	$64, %rsp
	leaq	-32(%rbp), %rdi
	leaq	__unnamed_180(%rip), %rsi
.Ltmp2109:
	.loc	22 17 19 prologue_end
	callq	option
	.loc	22 17 5 is_stmt 0
	movq	-32(%rbp), %rsi
	movq	%rsi, -64(%rbp)
	movq	-24(%rbp), %rsi
	movq	%rsi, -56(%rbp)
	movq	-16(%rbp), %rsi
	movq	%rsi, -48(%rbp)
	movq	-8(%rbp), %rsi
	movq	%rsi, -40(%rbp)
.Ltmp2110:
	.loc	22 16 20 is_stmt 1
	addq	$64, %rsp
	popq	%rbp
	retq

option:
.Lfunc_begin291:
	.loc	22 9 0
	.cfi_startproc
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset %rbp, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register %rbp
	movq	%rdi, %rax
.Ltmp2112:
	.loc	22 10 28 prologue_end
	vmovdqu	(%rsi), %xmm0
	vmovdqa	%xmm0, -32(%rbp)
	movq	$0, -8(%rbp)
	movq	$0, -16(%rbp)
	.loc	22 10 5 is_stmt 0
	movq	-32(%rbp), %rsi
	movq	%rsi, (%rdi)
	movq	-24(%rbp), %rsi
	movq	%rsi, 8(%rdi)
	movq	-16(%rbp), %rsi
	movq	%rsi, 16(%rdi)
	movq	-8(%rbp), %rsi
	movq	%rsi, 24(%rdi)
	popq	%rbp
	retq

Will try and dig into this a bit more over the next few days.

@tiehuis tiehuis added os-freebsd bug Observed behavior contradicts documented or intended behavior labels Jul 11, 2018
@andrewrk
Copy link
Member

Is it a build error or a crash at runtime of the generated binary?

If it's a build error, it might be a out of memory error during LLD linking: https://groups.google.com/forum/#!topic/llvm-dev/A6xFrwz0OUo

SIGBUS is what happens when you run out of memory due to reading files with mmap instead of read.

Is the zig build build runner always compiled in debug mode?

Yes

@andrewrk andrewrk added this to the 0.4.0 milestone Jul 11, 2018
@tiehuis
Copy link
Member Author

tiehuis commented Jul 11, 2018

It is a runtime error so I don't believe it is that issue.

@andrewrk
Copy link
Member

Looks like this is solved on master branch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior os-freebsd
Projects
None yet
Development

No branches or pull requests

2 participants