Skip to content

[NativeAOT/ARM] Prevent misoptimization by clang - #97811

Merged
jkotas merged 3 commits into
dotnet:mainfrom
filipnavara:naot-arm-misopt2
Feb 2, 2024
Merged

jkotas merged 3 commits into
dotnet:mainfrom
filipnavara:naot-arm-misopt2

Conversation

@filipnavara

Copy link
Copy Markdown
Member

Optimized builds by clang assume that variable addresses are already aligned and ignore the & ~1 masking:

printf("%x %x %x\r\n", (uintptr_t)&RhpAssignRefAVLocation, (uintptr_t)&RhpAssignRefAVLocation & ~1, faultingIP);
// prints "4b9539 4b9539 4b9538"

Avoid using non-aligned labels in the first place.

Contributes to #97729

@ghost ghost added area-NativeAOT-coreclr community-contribution Indicates that the PR has been added by a community member labels Feb 1, 2024
@ghost

ghost commented Feb 1, 2024

Copy link
Copy Markdown

Tagging subscribers to this area: @agocke, @MichalStrehovsky, @jkotas
See info in area-owners.md if you want to be subscribed.

Issue Details

Optimized builds by clang assume that variable addresses are already aligned and ignore the & ~1 masking:

printf("%x %x %x\r\n", (uintptr_t)&RhpAssignRefAVLocation, (uintptr_t)&RhpAssignRefAVLocation & ~1, faultingIP);
// prints "4b9539 4b9539 4b9538"

Avoid using non-aligned labels in the first place.

Contributes to #97729

Author: filipnavara
Assignees: -
Labels:

area-NativeAOT-coreclr

Milestone: -

@jkotas

jkotas commented Feb 1, 2024

Copy link
Copy Markdown
Member

Isn't the root cause of the problem that these code labels are declared as void* variables

EXTERN_C void * RhpAssignRefAVLocation;
? The code labels are not guaranteed to have void* alignment on any architecture supported by native AOT. There is still opportunity for mis-optimization. We have seen cases where clang/llvm is very creative in using the alignment guarantees for optimizations. Should these code labels be declared as byte variables instead to tell the compiler that there are no alignment guarantees?

@filipnavara

filipnavara commented Feb 1, 2024 •

Copy link
Copy Markdown
Member Author

Generally, we should unify on either using ALTERNATE_ENTRY (or GLOBAL_LABEL in this PR) or EXPORT_POINTER_TO_ADDRESS. Both mechanisms serve the same purpose but with the indirection done at different point in time (C vs ASM).

Taking an address of a void * variable on ARM is guaranteed to be aligned, which is the Clang assumption. These are not variables on the assembly side though, they are labels or (before this PR) function entrypoints which have different semantics. Changing to uint8_t * may work too but it's still misrepresenting the purpose.

I'm leaning slightly in the direction of EXPORT_POINTER_TO_ADDRESS but doing that is a bigger change across all platforms.

@jkotas

jkotas commented Feb 1, 2024

Copy link
Copy Markdown
Member

we should unify on either using ALTERNATE_ENTRY (or GLOBAL_LABEL in this PR) or EXPORT_POINTER_TO_ADDRESS

+1. The unification does not need to be done in this PR. We should agree on direction.

with the indirection done at different point in time (C vs ASM).

The difference between these two mechanisms is in number of indirections. EXPORT_POINTER_TO_ADDRESS has an extra indirection and it is a bit less efficient. My guess is that this extra indirection was needed for DACization of redhawk stackwalker in the past. We do not have functional DACized builds for native AOT. If we need an out-of-proc stackwalker for native AOT again, we would do this differently. The extra indirection serves no purpose.

Changing to uint8_t * may work too but it's still misrepresenting the purpose.

uint8_t * would have the same alignment problem as void*. It would need to be changed to uint8_t. We can define a type for it to make it clear that it is not an ordinary uint8_t.

@filipnavara

Copy link
Copy Markdown
Member Author

We can define a type for it to make it clear that it is not an ordinary uint8_t.

Any suggestion for a name?

@jkotas

jkotas commented Feb 1, 2024

Copy link
Copy Markdown
Member

The AV labels are named XYZLocation, so the type can be named ASSEMBLY_CODE_LOCATION or CODE_LOCATION.

@filipnavara

Copy link
Copy Markdown
Member Author

I’ll update the PR later today.

EXTERN_C void* RhpInterfaceDispatchAVLocation16;
EXTERN_C void* RhpInterfaceDispatchAVLocation32;
EXTERN_C void* RhpInterfaceDispatchAVLocation64;
EXTERN_C CODE_LOCATION RhpInitialInterfaceDispatch;

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Side-node: This is broken on ARM. Firstly, it is function entrypoint and not AV location. Secondly, we don't do the access in the assembly that would cause the AV.

I'll probably submit the fix separately since I cannot test it on device now.

@jkotas jkotas left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

@jkotas
jkotas merged commit 53d8003 into dotnet:main Feb 2, 2024
@github-actions github-actions Bot locked and limited conversation to collaborators Mar 3, 2024
@filipnavara
filipnavara deleted the naot-arm-misopt2 branch June 5, 2025 07:32
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

arch-arm32 area-NativeAOT-coreclr community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants