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

making var comptime yields SIGBUS (Misaligned address error) during runtime #3996

Closed
benmkw opened this issue Dec 29, 2019 · 6 comments
Closed
Labels
bug Observed behavior contradicts documented or intended behavior stage1 The process of building from source via WebAssembly and the C backend.
Milestone

Comments

@benmkw
Copy link

benmkw commented Dec 29, 2019

I am attempting to create a trie during compile time and use it for string matching (similar to the perfekt hashing approach from Andrews blog post) but ran into this problem.

source

on line 158 changing the var to be comptime results in runtime crash, while omitting the comptime works fine (whole program just executed at runtime). comptime var nodes = [_]TrieNode{TrieNode{}} ** size;

The crash happens on line 54 child.*.?.value = .{ .char = character };

I need nodes to be comptime in order to make const trie = create_trie_from(&input, &trie_node_pool); comptime and the idea is that LLVM can do something useful with that information.

zig version 0.5.0+bda355d97 (current master)

@andrewrk andrewrk added bug Observed behavior contradicts documented or intended behavior stage1 The process of building from source via WebAssembly and the C backend. labels Dec 31, 2019
@andrewrk andrewrk added this to the 0.6.0 milestone Dec 31, 2019
@benmkw
Copy link
Author

benmkw commented Jan 14, 2020

I played a bit with creduce and created this minimal example (some manual editing was done but creduce did some really cool things as well)

const s = struct {
    p: ?*void = null,
};

pub fn main() void {
    comptime var a = s{}; // remove this comptime and it works
    var c = &a; // change this to const and it works
    c.p = null; // a.p = null works
}
}

'zig run 5reduce.zig' terminated by signal SIGBUS (Misaligned address error)

I also tried the ir-clean-up-vars branch at 01c2eba where this issue remains

creduce without help

for reference, this is what creduce achieves all on its own. After this, some manual inlining helps it to make some more progress.

const a = struct {
    b: [128]?*a = [_]?*a{null} ** 128,
};
const c = struct {
    d: []a,
    e: usize,
    f: usize,
    fn allocate(g: c) *a {
        return &g.d[g.e];
    }
};
fn h(i: *a, j: []const u8, k: *c) void {
    var l = i;
    for (l.b) |*child| child.* = k.allocate();
}
fn m(inputs: [][]u8, k: *c) *a {
    var i = k.allocate();
    for (inputs) |j| h(i, j, k);
    return i;
}
pub fn main() void {
    var input = [_][]u8{""};
    const o = 0;
    comptime var d = [_]a{a{}};
    var k = c{
        .d = &d,
        .f = o,
        .e = 0,
    };
    const p = m(&input, &k);
}

@andrewrk
Copy link
Member

That's good to know that creduce can create zig test cases! Do you think you would be willing to write a tutorial on how to use creduce with zig?

@benmkw
Copy link
Author

benmkw commented Jan 14, 2020

I don't have a website or such, feel free to use/ distriubute as you want, I basically just did the following after looking at this, (the official website would somehow not load for me):

creduce  --not-c ./reduce.rb main.zig # yeah really, don't like bash, could use anything here, e.g. zig

where reduce.rb is

#! /usr/bin/env ruby

ret = `zig run main.zig 2>&1`

# or 
# ret = `zig fmt main.zig && zig run main.zig 2>&1`


status =  $?.exitstatus

if status == 138 # SIGBUS
    exit 0 # zero means, continue reducing 
else
    exit 1 # means, this is a branch which lost the bug we wanted to find
end

It's really fun to watch main.zig in your editor while creduce works on it.

I had some issues where creduce would reduce to an empty file after just a few seconds but my return value checks were wrong in these cases, writing the right reduce.rb script was actually a bit tricky although it seems easy now.

for the--not-c see here

I think if creduce would understand functions and could inline them that would help a lot (I presume the c version would be able to do this), or if zig could preprocess code and inline functions (not recursive ones) that may also be an (easier) option. The reason for this is that it seems as if it can't really delete function arguments at the moment so these are a barrier which stops propagation of deletions.

@andrewrk
Copy link
Member

Thank you so much! This is really useful. I'll compile this information and put it into either the wiki or CONTRIBUTING.md.

@benmkw
Copy link
Author

benmkw commented Jan 14, 2020

Your welcome!

Maybe you could add to it by converting the ruby script to zig and adding the option/ an example to search stdout/ stderr such that its really easy to combine textual things and return value. I find myself spending a bit of time figuring these things out every time they reoccur.

@Vexu
Copy link
Member

Vexu commented Dec 28, 2022

Duplicate of #1487

@Vexu Vexu marked this as a duplicate of #1487 Dec 28, 2022
@Vexu Vexu closed this as completed Dec 28, 2022
@andrewrk andrewrk modified the milestones: 0.12.0, 0.11.0 Dec 28, 2022
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 stage1 The process of building from source via WebAssembly and the C backend.
Projects
None yet
Development

No branches or pull requests

3 participants