-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
std.Io.Threaded: fix incorrect alignment of trailing data in closure #25868
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
base: master
Are you sure you want to change the base?
Conversation
castholm
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
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.
| fn resultPointer(ac: *AsyncClosure) [*]u8 { | ||
| const base: [*]u8 = @ptrCast(ac); | ||
| return base + ac.result_offset; | ||
| const closure_size_with_padding = @sizeOf(AsyncClosure) + ac.context_alignment.forward(@alignOf(AsyncClosure)); | ||
| return @ptrFromInt(ac.result_alignment.forward(@intFromPtr(base + closure_size_with_padding))); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch. I adjusted it to result offset instead of the total allocated memory for the closure.
50f3eed to
a00a915
Compare
Feel free to reuse the tests in your PR. |
a00a915 to
f90001d
Compare
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.