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.
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
Introduced a PRIVATE(member) macro #4409
Introduced a PRIVATE(member) macro #4409
Changes from all commits
be9a842
888c16c
38b3ff4
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is likely to confuse source code navigation. I don't know if this really matters: do we often look up field names (as opposed to function or type names) when working on the library?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can't speak for others, but I never look up field names. So I don't think the disruption to source code navigation is a big issue here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changing the name of struct members is not guaranteed to result in compatible object files. I expect this works with most compilers when invoked in an ordinary way, but does it work with link-time optimization? And even if it does, are we comfortable taking the risk that it will break in some environments, perhaps in subtle ways resulting in access to the wrong field (although most likely breaking with a link error)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you elaborate how changing the name of a struct member could, possibly and/or realistically, result in incompatible object files?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In theory, as far as I can tell from reading the C standard (i.e. I haven't found a provision to the contrary), a compiler could base padding on the field names and not just on the field types. (Not the order: a field always has to be at a higher address than the previous field.) In other words, given
it's possible, but “exotic”, that
offsetof(struct private, private_b) != offsetof(struct public, private_b)
. The C standard only guarantees thatoffsetof(…a) == 0
andoffsetof(…b) >= sizeof(a_t)
andoffsetof(…c) >= offsetof(…b) + sizeof(b_t)
.I can't think of a plausible reason why a compiler would do this, but it could. Maybe some build instrumented for runtime debugging that includes the field name after each field?
For bit-fields, N1256 states “The order of allocation of bit-fields within a unit (high-order to low-order or low-order to high-order) is implementation-defined”. So something like ordering field names by hash value would not be permitted, but basing the choice between low-to-high or high-to-low on parity of the total number of letters in field names would be as long as it's documented (but of course it would be really bizarre).
With link-time optimization, the link-time optimizer may try to access struct members and error out because a field doesn't exist. I have not checked any compiler's LTO.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've now checked LTO with GCC 10 and Clang 10. This PR passes the unit tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I had a similar concern, but then thought (1) I think it's unlikely to matter in practice for common compilers with common options, and (2) even if it happens with some compiler/option, I think people using those exotic settings can always define
MBEDTLS_ALLOW_PRIVATE_ACCESS
on the command line to get rid of the renaming.Again, the point of this is not to fully prevent users from accessing private fields, but to help them notice if they do it inadvertently. I think it's OK for people using hypothetical exotic build settings to not have access to that layer of protection. (And actually, they could still in their CI do an extra build with more standard settings to catch any unintentional uses of private fields, since this would be compile-time error.)
Thanks for checking what the standard says and how it works with LTO.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just a short note for now, later on when this task is fully implemented, this will need more in-depth explanation