Making sure everything is aligned correctly. Succeeder of #141#233
Merged
apoelstra merged 4 commits intorust-bitcoin:masterfrom Dec 21, 2020
Merged
Making sure everything is aligned correctly. Succeeder of #141#233apoelstra merged 4 commits intorust-bitcoin:masterfrom
apoelstra merged 4 commits intorust-bitcoin:masterfrom
Conversation
This was referenced Aug 28, 2020
1b73d4e to
2249cbf
Compare
2249cbf to
0638107
Compare
Member
Author
|
Rebased |
apoelstra
approved these changes
Dec 21, 2020
Merged
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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_tdirectly: 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
16as alignment.And use
#[repr(align(16))] struct AlignType([u8; 16])for the preallocated buffer API.Alternatives
Vec<AlignType>for allocations.unsafewith 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