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

design flaw: direct parameter passing to coroutines #1592

Open
andrewrk opened this Issue Sep 26, 2018 · 0 comments

Comments

Projects
None yet
1 participant
@andrewrk
Member

andrewrk commented Sep 26, 2018

Directly passing a parameter is not guaranteed to be a copy; neither is it guaranteed to be a reference. This is a footgun when combined with coroutines, where accessing a reference across a suspend point could be undefined behavior:

const std = @import("std");
const assert = std.debug.assert;

const Foo = struct {
    x: i32,
    y: i32,
};

async fn callee(foo: Foo) void {
    suspend;
    assert(foo.y == 5678);
}

test "pass directly" {
    const h = bar();
    blowUpStack(10);
    resume h;
}

fn bar() promise {
    var x: i32 = 1234;
    var y: i32 = 5678;
    var foo = Foo{ .x = x, .y = y };
    return async<std.debug.global_allocator> callee(foo) catch unreachable;
}

fn blowUpStack(x: u32) void {
    if (x == 0) return;
    blowUpStack(x - 1);
}
Test 1/1 pass directly...assertion failure
/home/andy/dev/zig/build/lib/zig/std/debug/index.zig:118:13: 0x2055a9 in ??? (test)
            @panic("assertion failure");
            ^
/home/andy/dev/zig/build/test.zig:11:11: 0x2247c9 in ??? (test)
    assert(foo.y == 5678);
          ^
/home/andy/dev/zig/build/test.zig:17:5: 0x205657 in ??? (test)
    resume h;
    ^
/home/andy/dev/zig/build/lib/zig/std/special/test_runner.zig:13:25: 0x22368a in ??? (test)
        if (test_fn.func()) |_| {
                        ^
/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:96:22: 0x22343b in ??? (test)
            root.main() catch |err| {
                     ^
/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:70:20: 0x2233b5 in ??? (test)
    return callMain();
                   ^
/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:64:39: 0x223218 in ??? (test)
    std.os.posix.exit(callMainWithArgs(argc, argv, envp));
                                      ^
/home/andy/dev/zig/build/lib/zig/std/special/bootstrap.zig:37:5: 0x2230d0 in ??? (test)
    @noInlineCall(posixCallMainAndExit);
    ^

Tests failed. Use the following command to reproduce the failure:
/home/andy/dev/zig/build/zig-cache/test

@andrewrk andrewrk added this to the 0.4.0 milestone Sep 26, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment