fix: zero-initialize msghdr first for musl lib build compatibility #2224
+22
−18
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.
Hey there!
fixes #2222
Like @strophy pointed out, there's an issue where msghdr can't be created directly on musl systems. The problem is that the musl libc has a couple extra private padding fields in the struct that aren't accessible.
Fix
This PR uses std::mem::zeroed() to initialize
msghdrby just setting everything to0. This way quiche builds fine on musl-based distros.If we take a look at the msghdr struct, it's basically just raw pointers and numbers. By zeroing everything out (including those hidden padding fields), we are able to satisfy the kernel.
The original code manually set many fields to null pointers and
0. Withzeroed()doing this automatically, those manual assignments were commented out. (kept for reference)Safety
Now,
std::mem::zeroed()can be dangerous if used on the wrong types - like anything that can't be null or zero because that creates undefined behavior.But as mentioned before it's fine here since
msghdris just raw pointers and numeric types, which can all safely be0.Performance
Ran some benchmarks with criterion.rs to compare how fast safe vs unsafe initialization of msghdr struct are.
Optimizations stayed enabled since the goal was to test under realistic conditions.
The safe constructor (which doesn't work with musl) was indeed slower, but the difference wasn't as big as mentioned here.
On average the unsafe approach is about 1.5-2% faster than the version without
zeroed().Between having redundant assignments vs just using
zeroed(), the performance is virtually identical.