Align all memory used by the system symbol table #297
Merged
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.
Issue #, if available: N/A
Description of changes:
Rarely, the compiler/linker will decide to put
gSystemSymbolMemory
on a smaller alignment boundary (e.g. 4 bytes) than what is required for the data it contains.gSystemSymbolMemory
is declared as achar[]
, but is really being used as generic backing memory for the system symbol table and any other data structures associated with it. Notably, the very first data structure placed in thegSystemSymbolMemory
is anION_ALLOCATION_CHAIN
, which may need a wider alignment.With the current behavior, an
ION_ALLOCATION_CHAIN
could be allocated at a smaller alignment than needed. TheION_ALLOC_BLOCK_TO_USER_PTR
macro then gets an aligned offset into the memory chunk associated with theION_ALLOCATION_CHAIN
. When another allocation needs to be performed, theION_ALLOC_USER_PTR_TO_BLOCK
macro is used to convert the user data pointer into anION_ALLOCATION_CHAIN
, but this is where the problems start happening. BecauseION_ALLOC_BLOCK_TO_USER_PTR
aligns the user data pointer, an unknown amount of padding exists between the end of theION_ALLOCATION_CHAIN
and the user data pointer (up toALLOC_ALIGNMENT
bytes).ION_ALLOC_USER_PTR_TO_BLOCK
doesn't account for any alignment done to the user data pointer (and there's no way it can really), meaning it might produce a pointer that is offset from the actualION_ALLOCATION_CHAIN
by the amount of padding that exists. This usually leads to a crash, since the data being read is now scrambled.This commit addresses this issue by introducing an
ALIGN_AS
macro that forces a data structure to be aligned to a wider alignment than the type requires. This is applied to thegSystemSymbolMemory
array, so that it is always located at a correctly aligned address. This also adds a few debug assertions to the system symbol table allocation routines and gets rid of the pointer aligning behavior in theION_ALLOC
converter macros (an offset from a pointer should be aligned if you start with an aligned pointer and add an aligned offset to it; in the case that you start with a misaligned pointer, you'll at least get the right pointer back with a round trip conversion, rather than a misaligned one).By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.