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

when a coroutine returns it accesses freed memory #1164

Closed
andrewrk opened this Issue Jun 27, 2018 · 1 comment

Comments

Projects
None yet
1 participant
@andrewrk
Member

andrewrk commented Jun 27, 2018

Here's a passing coroutine test:

var await_a_promise: promise = undefined;
var await_final_result: i32 = 0;
test "coroutine await" {
await_seq('a');
const p = async<std.debug.global_allocator> await_amain() catch unreachable;
await_seq('f');
resume await_a_promise;
await_seq('i');
assert(await_final_result == 1234);
assert(std.mem.eql(u8, await_points, "abcdefghi"));
}
async fn await_amain() void {
await_seq('b');
const p = async await_another() catch unreachable;
await_seq('e');
await_final_result = await p;
await_seq('h');
}
async fn await_another() i32 {
await_seq('c');
suspend |p| {
await_seq('d');
await_a_promise = p;
}
await_seq('g');
return 1234;
}
var await_points = []u8{0} ** "abcdefghi".len;
var await_seq_index: usize = 0;
fn await_seq(c: u8) void {
await_points[await_seq_index] = c;
await_seq_index += 1;
}

but it fails if you replace std.debug.global_allocator with std.heap.DirectAllocator:

    var da = std.heap.DirectAllocator.init();
    defer da.deinit();

    const allocator = &da.allocator;

Because it's accessing the coroutine frame after it has been destroyed.

@andrewrk andrewrk added the bug label Jun 27, 2018

@andrewrk andrewrk added this to the 0.3.0 milestone Jun 27, 2018

@andrewrk andrewrk closed this in 2fa588e Jun 27, 2018

@andrewrk

This comment has been minimized.

Member

andrewrk commented Jun 27, 2018

@Hejsil ^ I got bit for breaking my own rule about not using std.debug.global_allocator in tests :)

(specifically, although DirectAllocator is the slowest allocator, it usually causes segfaults on access of freed memory, which is nice for testing)

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