Skip to content

Segmentation fault referencing values in anonymous tuple. #7878

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

Closed
lithdew opened this issue Jan 25, 2021 · 3 comments
Closed

Segmentation fault referencing values in anonymous tuple. #7878

lithdew opened this issue Jan 25, 2021 · 3 comments
Labels
bug Observed behavior contradicts documented or intended behavior stage1 The process of building from source via WebAssembly and the C backend.
Milestone

Comments

@lithdew
Copy link
Contributor

lithdew commented Jan 25, 2021

If I un-encapsulate the heap-allocated string out of a struct, the test case passes. EDIT: as @LemonBoy has mentioned, the problem appears to be with printing out structs declared inside an anonymous tuple.

$ zig run test.zig
Segmentation fault at address 0x202b20
/home/lith/Desktop/tests/test.zig:13:49: 0x22d46a in main (test)
    std.debug.print("{s}\n", .{Header{ .value = value }});
                                                ^
/home/lith/Desktop/zig/lib/std/start.zig:345:37: 0x204e44 in std.start.posixCallMainAndExit (test)
            const result = root.main() catch |err| {
                                    ^
/home/lith/Desktop/zig/lib/std/start.zig:163:5: 0x204ce2 in std.start._start (test)
    @call(.{ .modifier = .never_inline }, posixCallMainAndExit, .{});
    ^
Aborted (core dumped)
const std = @import("std");

pub fn main() !void {
    const Header = struct {
        value: []const u8,
    };

    const allocator = std.heap.page_allocator;

    var value = try allocator.dupe(u8, "test");
    defer allocator.free(value);

    std.debug.print("{s}\n", .{Header{ .value = value }});
}
@lithdew lithdew changed the title std.fmt: segmentation fault printing heap-allocated strings std.fmt: segmentation fault printing heap-allocated string(s) in struct Jan 25, 2021
@LemonBoy
Copy link
Contributor

The crash has nothing to do with std.fmt nor with printing strings nor with heap allocations.
Pull Header{ .value = value } out of the anonymous tuple and the code runs just fine, the problem is with the anonymous thing.

const std = @import("std");

pub fn main() !void {
    const Header = struct {
        value: []const u8,
    };

    const allocator = std.heap.page_allocator;

    var value = try allocator.dupe(u8, "test");
    defer allocator.free(value);

    var hdr = Header{ .value = value };
    std.debug.print("{s}\n", .{hdr111});
}

@faraazahmad
Copy link

I've noticed this is LLVM behavior. I was implementing print function in my own language and generating LLVM IR, I've noticed that passing anonymous "values" (I don't have a better word) causes segfaults. I was passing in a string without first declaring it, hence the segfault. I then declared the string separately and then passed its pointer in it, and it worked.

I've looked at LLVM IR generated by clang and that's how it does it too. I feel like programmers shouldn't have to worry about this internal implementation detail, but I guess this method also forces the programmer to write better code, i.e. easier to read (when it gets bigger and complicated), easier to catch errors, etc.

@lithdew lithdew changed the title std.fmt: segmentation fault printing heap-allocated string(s) in struct std.fmt: segmentation fault printing out anonymous structs Jan 29, 2021
@lithdew lithdew changed the title std.fmt: segmentation fault printing out anonymous structs Segmentation fault referencing values in anonymous tuple. Jan 29, 2021
@SpexGuy SpexGuy added bug Observed behavior contradicts documented or intended behavior stage1 The process of building from source via WebAssembly and the C backend. labels Mar 17, 2021
@Vexu Vexu added this to the 0.9.0 milestone Mar 19, 2021
@andrewrk andrewrk modified the milestones: 0.9.0, 0.10.0 May 19, 2021
@mikdusan
Copy link
Member

mikdusan commented Jan 6, 2023

  • unable to repro with latest compiler
  • unable to repro with godbolt, back to 0.6.0
  • closing because the anon tuple input is correctly output
repro.zig
const std = @import("std");

pub fn main() !void {
    const Header = struct {
        value: []const u8,
    };

    const allocator = std.heap.page_allocator;

    var value = try allocator.dupe(u8, "test");
    defer allocator.free(value);

    std.debug.print("{}\n", .{Header{ .value = value }});
}
$ archlinux▸ uname -a
Linux archlinux 6.1.2-arch1-1 #1 SMP PREEMPT_DYNAMIC Sat, 31 Dec 2022 17:40:35 +0000 x86_64 GNU/Linux

$ zig version
0.11.0-dev.1239+7a2d7ff62

$ zig run repro.zig
repro.main.Header{ .value = { 116, 101, 115, 116 } }

@mikdusan mikdusan closed this as completed Jan 6, 2023
@andrewrk andrewrk modified the milestones: 0.12.0, 0.11.0 Jan 8, 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 stage1 The process of building from source via WebAssembly and the C backend.
Projects
None yet
Development

No branches or pull requests

7 participants