Skip to content

[NativeAOT] Read the byte-packed LSDA offsets without a misaligned load - #134051

Merged
MichalStrehovsky merged 2 commits into
dotnet:mainfrom
maximmenshikov:maximmenshikov/misaligned
Sep 16, 2026
Merged

MichalStrehovsky merged 2 commits into
dotnet:mainfrom
maximmenshikov:maximmenshikov/misaligned

Conversation

@maximmenshikov

Copy link
Copy Markdown
Contributor

A method's LSDA is byte-packed with no padding: a one-byte flags field followed by 32-bit offsets. UnixNativeCodeManager and RuntimeInstance read those offsets with *dac_cast<PTR_int32_t>(p), so the load lands on a 4-byte-aligned address only by chance. The cast also tells the compiler that the address is aligned, which the format does not promise.

x64 and arm64 do not care. A RISC-V execution environment may support misaligned access in hardware, emulate it, or reject it, and the runtime cannot tell which, so it should not ask for it here. Build flags do not help: with -mstrict-align the compiler still emits a single lw, because the cast promised the alignment.

Read the offsets a byte at a time through NativePrimitiveDecoder, which already reads these blobs. It gets a non-mutating PeekInt32, and ReadInt32/ReadUInt32 now use it instead of repeating the byte assembly. Widening each byte before the shift also removes the signed overflow the old expressions had in the top byte.

Nothing is lost where misaligned access is available: with clang 22.1.8 at -O2 the affected paths still compile to one ldrsw on arm64 and one movslq on x64.

@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Sep 16, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
13 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @agocke, @dotnet/ilc-contrib
See info in area-owners.md if you want to be subscribed.

@maximmenshikov
maximmenshikov force-pushed the maximmenshikov/misaligned branch from 4e24216 to cd863bb Compare September 16, 2026 12:41
A method's LSDA is byte-packed with no padding: a one-byte flags field
followed by 32-bit offsets. UnixNativeCodeManager and RuntimeInstance read
those offsets with *dac_cast<PTR_int32_t>(p), so the load lands on a
4-byte-aligned address only by chance. The cast also tells the compiler
that the address is aligned, which the format does not promise.

x64 and arm64 do not care. A RISC-V execution environment may support
misaligned access in hardware, emulate it, or reject it, and the runtime
cannot tell which, so it should not ask for it here. Build flags do not
help: with -mstrict-align the compiler still emits a single lw, because the
cast promised the alignment.

Read the offsets through memcpy instead, in the decoder that already reads
these blobs. NativePrimitiveDecoder gets a non-mutating PeekInt32 and
PeekUInt32, and ReadInt32/ReadUInt32 use them rather than assembling the
value out of byte loads by hand. That keeps every read native-endian, as it
was before, and drops the signed overflow the old expressions had in the
top byte.

Nothing is lost where misaligned access is available: with clang 22.1.8 at
-O2 the affected paths still compile to one ldrsw on arm64 and one movslq
on x64.

Signed-off-by: Maxim Menshikov <[email protected]>
@jkotas jkotas added the arch-riscv Related to the RISC-V architecture label Sep 16, 2026
@maximmenshikov
maximmenshikov force-pushed the maximmenshikov/misaligned branch from cd863bb to 89a1ed8 Compare September 16, 2026 13:24
@maximmenshikov
maximmenshikov marked this pull request as ready for review September 16, 2026 13:28
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
13 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@MichalStrehovsky
MichalStrehovsky merged commit 0342213 into dotnet:main Sep 16, 2026
110 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Sep 17, 2026
jtschuster pushed a commit to jtschuster/runtime that referenced this pull request Sep 18, 2026
…ad (dotnet#134051)

A method's LSDA is byte-packed with no padding: a one-byte flags field
followed by 32-bit offsets. UnixNativeCodeManager and RuntimeInstance
read those offsets with *dac_cast<PTR_int32_t>(p), so the load lands on
a 4-byte-aligned address only by chance. The cast also tells the
compiler that the address is aligned, which the format does not promise.

x64 and arm64 do not care. A RISC-V execution environment may support
misaligned access in hardware, emulate it, or reject it, and the runtime
cannot tell which, so it should not ask for it here. Build flags do not
help: with -mstrict-align the compiler still emits a single lw, because
the cast promised the alignment.

Read the offsets a byte at a time through NativePrimitiveDecoder, which
already reads these blobs. It gets a non-mutating PeekInt32, and
ReadInt32/ReadUInt32 now use it instead of repeating the byte assembly.
Widening each byte before the shift also removes the signed overflow the
old expressions had in the top byte.

Nothing is lost where misaligned access is available: with clang 22.1.8
at -O2 the affected paths still compile to one ldrsw on arm64 and one
movslq on x64.

Signed-off-by: Maxim Menshikov <[email protected]>
@github-actions github-actions Bot locked and limited conversation to collaborators Oct 17, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

arch-riscv Related to the RISC-V architecture 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.

4 participants