Fix strict aliasing violations#519
Open
heshpdx wants to merge 1 commit into
Open
Conversation
The cast to char** is a violation of strict aliasing rules. Compilers may generate incorrect code due to this issue. Using memcpy to avoid the issue. Not expecting perf difference.
cmuellner
reviewed
Jun 16, 2026
| // allocate new memory page | ||
| nBytes = sizeof(Hop_Obj_t) * (1<<IVY_PAGE_SIZE) + 64; | ||
| pMemory = ABC_ALLOC( char, nBytes ); | ||
| PMemAlign = PMemAlign + 64 - (((int)(ABC_PTRUINT_T)PMemAlign) & 63); |
There was a problem hiding this comment.
PMemAlign is initialized to zero at this point, so this is incorrect.
That's likely also the reason why the CI is failing.
The strict-aliasing issues are only in the type-punning accesses:
*((Hop_Obj_t **)pTemp)
*((char **)pMemory)The surrounding allocation/alignment code can and should stay the same.
Proposed change:
char * pMemory;
Hop_Obj_t * pEntry, * pNext;
...
pMemory = ABC_ALLOC( char, nBytes );
Vec_PtrPush( p->vChunks, pMemory );
pMemory = pMemory + 64 - (((int)(ABC_PTRUINT_T)pMemory) & 63);
Vec_PtrPush( p->vPages, pMemory );
pEntry = (Hop_Obj_t *)pMemory;
p->pListFree = pEntry;
for ( i = 1; i <= IVY_PAGE_MASK; i++ )
{
pNext = pEntry + 1;
memcpy( pEntry, &pNext, sizeof(Hop_Obj_t *) );
pEntry++;
}
pNext = NULL;
memcpy( pEntry, &pNext, sizeof(Hop_Obj_t *) );
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.
The cast to char** is a violation of strict aliasing rules. Compilers may generate incorrect code due to this issue. Using memcpy to avoid the issue. Not expecting perf difference.
This was found via testing in SPEC CPU's 729.abc_r.