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

segfault when mutating comptime created memory at runtime #13735

Open
travisstaloch opened this issue Dec 1, 2022 · 3 comments
Open

segfault when mutating comptime created memory at runtime #13735

travisstaloch opened this issue Dec 1, 2022 · 3 comments
Labels
bug Observed behavior contradicts documented or intended behavior frontend Tokenization, parsing, AstGen, Sema, and Liveness.
Milestone

Comments

@travisstaloch
Copy link
Contributor

Zig Version

0.10.0

Steps to Reproduce and Observed Behavior

This was found while doing a comptime only advent of code day 1 solution. Seems like the array created in parse() can't be mutated at runtime when trying to sort it. The error goes away if sorting is also done at comptime.

// bug.zig
const std = @import("std");

const input_raw = "1\n2\n3\n4\n5";

pub fn main() !void {
    const input = comptime try parse(input_raw);
    std.sort.sort(usize, input, {}, std.sort.desc(usize));
}

fn parse(comptime input: []const u8) ![]usize {
    @setEvalBranchQuota(input.len * 20);

    const len = std.mem.count(u8, input, "\n") + 1;
    var totals: [len]usize = undefined;
    var i: usize = 0;
    var it = std.mem.split(u8, input, "\n");
    while (it.next()) |line| : (i += 1) {
        if (line.len == 0) continue;
        totals[i] = try std.fmt.parseInt(usize, line, 10);
    }
    return &totals;
}
$ zig run bug.zig 
Segmentation fault at address 0x2099b0
~/zig/zig/download/0.10.0/files/lib/std/mem.zig:2856:5: 0x21daba in swap__anon_4059 (bug)
    a.* = b.*;
    ^
~/zig/zig/download/0.10.0/files/lib/std/sort.zig:1153:17: 0x21e090 in swap__anon_4071 (bug)
        mem.swap(T, &items[x], &items[y]);
                ^
~/zig/zig/download/0.10.0/files/lib/std/sort.zig:287:21: 0x212c7e in sort__anon_3483 (bug)
                swap(T, sliced_items, context, lessThan, &order, 0, 1);
                    ^
./bug.zig:7:18: 0x211db7 in main (bug)
    std.sort.sort(usize, input, {}, std.sort.desc(usize));
                 ^
~/zig/zig/download/0.10.0/files/lib/std/start.zig:606:37: 0x211997 in posixCallMainAndExit (bug)
            const result = root.main() catch |err| {
                                    ^
~/zig/zig/download/0.10.0/files/lib/std/start.zig:368:5: 0x211421 in _start (bug)
    @call(.{ .modifier = .never_inline }, posixCallMainAndExit, .{});
    ^
Aborted

Expected Behavior

I expected the sort to succeed.

@travisstaloch travisstaloch added the bug Observed behavior contradicts documented or intended behavior label Dec 1, 2022
@travisstaloch
Copy link
Contributor Author

travisstaloch commented Dec 1, 2022

as pointed out by @cryptocode on discord, the error seems to go away in ReleaseFast. here is a demo of that https://zigbin.io/ff38fd. if you click run you'll see that it works.

@travisstaloch
Copy link
Contributor Author

here is a smaller repro. it seems that bug is dependent the memory being mutated (see the comment).

const std = @import("std");

pub fn main() !void {
    const input = comptime parse();
    std.sort.sort(usize, input, {}, std.sort.desc(usize));
}

fn parse() []usize {
    var result = [1]usize{1} ** 10;
    for (result) |*n, i| n.* = i; // commenting out this line prevents the segfault
    return &result;
}

@Vexu
Copy link
Member

Vexu commented Dec 5, 2022

I think that slice should not be mutable per #7396

@Vexu Vexu added frontend Tokenization, parsing, AstGen, Sema, and Liveness. stage1 The process of building from source via WebAssembly and the C backend. labels Dec 5, 2022
@Vexu Vexu added this to the 0.11.0 milestone Dec 5, 2022
@Vexu Vexu removed the stage1 The process of building from source via WebAssembly and the C backend. label Dec 8, 2022
@Vexu Vexu modified the milestones: 0.11.0, 0.12.0 Apr 23, 2023
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 frontend Tokenization, parsing, AstGen, Sema, and Liveness.
Projects
None yet
Development

No branches or pull requests

2 participants