-
-
Notifications
You must be signed in to change notification settings - Fork 14.7k
Stop using mem::zeroed for FFI #136737
Copy link
Copy link
Closed
Labels
A-FFIArea: Foreign function interface (FFI)Area: Foreign function interface (FFI)E-hardCall for participation: Hard difficulty. Experience needed to fix: A lot.Call for participation: Hard difficulty. Experience needed to fix: A lot.E-help-wantedCall for participation: Help is requested to fix this issue.Call for participation: Help is requested to fix this issue.E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.T-libsRelevant to the library team, which will review and decide on the PR/issue.Relevant to the library team, which will review and decide on the PR/issue.
Metadata
Metadata
Assignees
Labels
A-FFIArea: Foreign function interface (FFI)Area: Foreign function interface (FFI)E-hardCall for participation: Hard difficulty. Experience needed to fix: A lot.Call for participation: Hard difficulty. Experience needed to fix: A lot.E-help-wantedCall for participation: Help is requested to fix this issue.Call for participation: Help is requested to fix this issue.E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.T-libsRelevant to the library team, which will review and decide on the PR/issue.Relevant to the library team, which will review and decide on the PR/issue.
Type
Fields
Give feedbackNo fields configured for issues without a type.
Quite a lot of
stds FFI code usesmem::zeroedto create empty structures that are to be filled by FFI. E.g.:rust/library/std/src/sys/pal/windows/time.rs
Lines 68 to 72 in d2f335d
This is unnecessary, since the C code does not require the structures to be initialized (you wouldn't zero out structures in C either). Thus, this pattern just reduces performance, as it results in the initialization of potentially very large structures such as
sockaddr_storage. We should get rid of this pattern and replace it with proper handling of uninitialized data throughMaybeUninit.Edit (after discussion below): In some instances one might decide to keep the zero-initialization behaviour, but I think this should still go through
MaybeUninit::zeroedinstead ofmem::zeroedto make the point of initialization explicit (by introducing.assume_init()calls in the right places.I'll probably do the network code myself (I want to clean some things up there anyway), but I'm happy to mentor you if you'd like to help with other instances such as the filesystem code (
library/std/src/sys/pal/*/fs.rs). Just contact me here or on Zulip. The best way to find the pattern is probably by searching formem::zeroedinlibrary/std/src/sys.