You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When calling async, concurrent or std.Io.Group.async on std.Io.Threaded with a function that has parameters or a return value with extra alignment, it may fail because the internal async closure did not properly create well aligned storage for them.
conststd=@import("std");
pubfnmain() !void {
// Depending on which allocator is used, the code may work "by accident". // Using a fixed buffer allocator did consistently reproduce the issue for mevarbuffer: [1024]u8align(64) =undefined;
varfba: std.heap.FixedBufferAllocator= .init(buffer[1..]);
varthreaded: std.Io.Threaded= .init(fba.allocator());
deferthreaded.deinit();
constio=threaded.io();
varfuture=io.async(paramWithExtraAlignment, .{.{}});
future.await(io);
}
fnparamWithExtraAlignment(unused: Align64) void {
_=unused;
}
constAlign64=struct {
unused: u8align(64) =0,
};
zig run sample.zig thread 5824 panic: incorrect alignment
/home/techatrix/repos/zig/lib/std/Io.zig:1534:46: 0x1163409 in start (std.zig)
const args_casted: *const Args = @ptrCast(@alignCast(context));
^
/home/techatrix/repos/zig/lib/std/Io/Threaded.zig:405:16: 0x106af5e in start (std.zig)
ac.func(ac.contextPointer(), ac.resultPointer());
^
/home/techatrix/repos/zig/lib/std/Io/Threaded.zig:188:26: 0x10d9b06 in worker (std.zig)
closure.start(closure);
^
/home/techatrix/repos/zig/lib/std/Thread.zig:558:13: 0x10b3100 in callFn__anon_16055 (std.zig)
@call(.auto, f, args);
^
/home/techatrix/repos/zig/lib/std/Thread.zig:1534:30: 0x108f070 in entryFn (std.zig)
return callFn(f, self.fn_args);
^
/home/techatrix/repos/zig/lib/std/os/linux/x86_64.zig:105:5: 0x10b31b5 in clone (std.zig)
asm volatile (
^
Aborted (core dumped)
The reason will be displayed to describe this comment to others. Learn more.
Aside from the problem I point out, the differences between this PR and #25817 is that this PR is slightly more efficient with the sizes of closure fields (2 * u8 + 2 * usize vs 3 * usize) but allocates a few more wasted bytes when the context alignment is <= the closure alignemnt.
#25817 also deduplicates the allocation code to a common init() function to avoid having the code paths going out of sync.
I could try to adapt the test to my PR if you think it'd be useful.
The reason will be displayed to describe this comment to others. Learn more.
This looks wrong to me, the code is not aware of the context length. resultPointer() will return a pointer into the middle of the context. AsyncClosure needs to remember either the computed result offset, or the context alignment + context length + result alignment to recompute it.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When calling
async,concurrentorstd.Io.Group.asynconstd.Io.Threadedwith a function that has parameters or a return value with extra alignment, it may fail because the internal async closure did not properly create well aligned storage for them.