Making sure everything is aligned correctly. Succeeder of #141 #233
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.
Introduction
libsecp256k1 expects thing to be "suitably aligned to hold an object of any type"[0] which is the same jargon C89 uses for malloc:
which translated to C11 jargon means "suitably aligned for any fundamental type" :
Which is:
Rust holds a table of all these alignments for the supported platforms: https://github.com/rust-lang/rust/blob/2c31b45ae878b821975c4ebd94cc1e49f6073fd0/library/std/src/sys_common/alloc.rs
And rust-lang/libc implements
max_align_t
directly: https://docs.rs/libc/0.2.76/libc/struct.max_align_t.htmlSo I took the bigger alignment any rust architecture supports and used that as our default alignment, and also added a test that it is bigger than
max_align_t
(we could also test that in the build.rs if we really want).Implementation
Use alloc() and dealloc() whenever we allocate, with
16
as alignment.And use
#[repr(align(16))] struct AlignType([u8; 16])
for the preallocated buffer API.Alternatives
Vec<AlignType>
for allocations.unsafe
with s.t. the caller now needs to make sure he upholds the invariantsCloses #138
[0] https://github.com/bitcoin-core/secp256k1/blob/670cdd3f8be25f81472b2d16dcd228b0d24a5c45/include/secp256k1_preallocated.h#L42
[1] http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf 7.20.3
[2] http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1548.pdf 7.22.3
[3] http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1548.pdf 6.2.8p2