Skip to content
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

Add comments and test for 64-bit field alignment #418

Merged
merged 3 commits into from
Jan 6, 2020

Conversation

MrAlias
Copy link
Contributor

@MrAlias MrAlias commented Jan 3, 2020

All values that are passed to 64-bit operations in the atomic package need to be arranged for 64-bit alignment:

On ARM, x86-32, and 32-bit MIPS, it is the caller's responsibility to arrange for 64-bit alignment of 64-bit words accessed atomically. The first word in a variable or in an allocated struct, array, or slice can be relied upon to be 64-bit aligned.

This requirement has led to opaque test failures in the past. The modification of a structs field ordering would result in fields requiring this alignment to no longer have it. However, instead of a useful error message being presented, all the developer received was a runtime panic raised for anil pointer dereference.

This PR is intended to proactively notify developers of the critical ordering of certain struct fields as well as add tests to check alignment and give useful error messages when incorrect modification is committed. To do this the code-base was cataloged and all (current) variables passed to 64-bit atomic operations were determined. Any of these variables that ended up as a field in a struct was then explicitly identified with a comment for that field, moved to the beginning of the struct to standardize on alignment, and included in a unit test. The tests added use a common set of testing utilities and validate (in the preprocessor) that the field offset is 64-bit aligned prior to any other testing being run.

Important to note is what this PR is not. Ideally, the validation of variables passed to the 64-bit atomic operations would be dynamic instead of static. However, this would require a compiled Go program to parse a syntax tree (which it is a part of) and then evaluate the field offset of discovered structs. As explained in the linked issue, this is not possible. Evaluation of constant values is possible, but the desired offset value (a variable value) is not. Therefore, these tests are static and not dynamic.

This trade-off for static tests is one that is currently acceptable, but it is made with the understanding that it might not be in the future. At that future point it is expected these test will be modified or removed.

Resolves #341

Tyler Yahn added 2 commits January 2, 2020 15:32
Add comment about alignment requirements to all struct fields who's
values are passed to 64-bit atomic operations.

Update any struct's field ordering if one or more of those fields has
alignment requirements to support 64-bit atomic operations.
Most `struct` that have field alignment requirements are now statically
validated prior to testing. The only `struct`s not validated that have
these requirements are ones defined in tests themselves where multiple
`TestMain` functions would be needed to test them. Given the fields are
already identified with comments specifying the alignment requirements
and they are in the test themselves, this seems like an OK omission.
@lizthegrey lizthegrey merged commit 91091b4 into open-telemetry:master Jan 6, 2020
lizthegrey added a commit that referenced this pull request Jan 6, 2020
@MrAlias MrAlias mentioned this pull request Feb 11, 2020
6 tasks
@MrAlias MrAlias deleted the 64-bit-align branch September 2, 2020 19:33
@pellared pellared added this to the untracked milestone Nov 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add unsafe.Alignof() tests for code that depends on 64-bit atomics
4 participants