Repository navigation
Fix incorrect operand size for long-form IL local/argument instructions in InstructionEncoder - #128279
Merged
MichalStrehovsky merged 2 commits intoJun 15, 2026
Conversation
Contributor
|
Tagging subscribers to this area: @dotnet/area-system-reflection-metadata |
… instructions Long-form local/argument opcodes (ldloc, stloc, ldloca, ldarg, ldarga, starg) take a 2-byte unsigned short operand per ECMA-335, not a 4-byte int32. - Change WriteInt32 to WriteUInt16 in all six methods: LoadLocal, StoreLocal, LoadLocalAddress, LoadArgument, LoadArgumentAddress, StoreArgument - Tighten range check: reject indexes > ushort.MaxValue with ArgumentOutOfRangeException - Update XML doc comments to reflect the new upper bound - Fix regression tests to expect correct 4-byte sequences and verify that ushort.MaxValue succeeds while ushort.MaxValue+1 and int.MaxValue throw Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/b47df461-9f6f-4067-a596-9f4b69ec5d4d Co-authored-by: MichalStrehovsky <[email protected]>
Copilot
AI
changed the title
[WIP] Fix incorrect operand encoding for long-form IL instructions
Fix incorrect operand size for long-form IL local/argument instructions in InstructionEncoder
May 16, 2026
This was referenced May 16, 2026
MichalStrehovsky
marked this pull request as ready for review
May 17, 2026 05:07
Contributor
There was a problem hiding this comment.
Pull request overview
This PR corrects InstructionEncoder’s encoding of long-form local and argument IL instructions so their operands are emitted as 2-byte unsigned indexes, matching IL encoding rules.
Changes:
- Writes
ushortoperands for long-formldloc,stloc,ldloca,ldarg,ldarga, andstarg. - Rejects indexes above
ushort.MaxValuewhile preserving short/dedicated opcode encodings. - Updates tests to verify corrected byte sequences and upper-bound failures.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
src/libraries/System.Reflection.Metadata/src/System/Reflection/Metadata/Ecma335/Encoding/InstructionEncoder.cs |
Fixes operand size and range validation for local/argument instruction encoders. |
src/libraries/System.Reflection.Metadata/tests/Metadata/Ecma335/Encoding/InstructionEncoderTests.cs |
Updates expected encodings and adds out-of-range coverage for affected methods. |
Member
|
@dotnet/area-system-reflection-metadata who would be the best to review this? It should be trivial enough, the original code was just plain wrong. |
Member
|
@jeffhandley could you help me find a reviewer for this please? |
Member
|
@tmat is this something you could have a look at please? |
tmat
approved these changes
Jun 15, 2026
MichalStrehovsky
deleted the
copilot/fix-operand-encoding-il-instructions
branch
June 15, 2026 23:38
eiriktsarpalis
pushed a commit
that referenced
this pull request
Jul 15, 2026
…ns in InstructionEncoder (#128279) `InstructionEncoder` was emitting a 4-byte `int32` operand for long-form local/argument opcodes (`ldloc`, `stloc`, `ldloca`, `ldarg`, `ldarga`, `starg`). Per ECMA-335, these opcodes take a **2-byte unsigned short** operand. For example, `StoreLocal(256)` was emitting `FE 0E 00 01 00 00` instead of the correct `FE 0E 00 01`. ## Changes - **`InstructionEncoder.cs`** — all six `Load`/`Store` local/argument methods: - `WriteInt32(slotIndex)` → `WriteUInt16((ushort)slotIndex)` for the long-form branch - Range guard changed from `slotIndex > 0` to `unchecked((uint)slotIndex) <= ushort.MaxValue`, so indexes above 65535 now throw `ArgumentOutOfRangeException` - XML doc `<exception>` comments updated to reflect the new upper bound - **`InstructionEncoderTests.cs`** — all six corresponding test methods: - `il.XXX(int.MaxValue)` call replaced with `il.XXX(ushort.MaxValue)` (now a valid, in-range input) - Expected byte arrays corrected from 6-byte to 4-byte sequences (e.g. `0xFE, 0x0E, 0xFF, 0xFF` instead of `0xFE, 0x0E, 0xFF, 0xFF, 0xFF, 0x7F`) - Added `Assert.Throws<ArgumentOutOfRangeException>` for `ushort.MaxValue + 1` and `int.MaxValue` <!-- START COPILOT ORIGINAL PROMPT --> <details> <summary>Original prompt</summary> > Fix incorrect operand encoding for long-form IL local/argument instructions in `InstructionEncoder` and add regression coverage. > > Context: > - Repository: `dotnet/runtime` > - File: `src/libraries/System.Reflection.Metadata/src/System/Reflection/Metadata/Ecma335/Encoding/InstructionEncoder.cs` > - Commit/URL referenced by user: `https://github.com/dotnet/runtime/blame/88dbd1173bde444926a06aeb14f507b04b451aa8/src/libraries/System.Reflection.Metadata/src/System/Reflection/Metadata/Ecma335/Encoding/InstructionEncoder.cs#L230` > > Problem: > `InstructionEncoder` currently emits a 4-byte operand for several long-form local/argument opcodes, for example: > > ```csharp > else if (slotIndex > 0) > { > OpCode(ILOpCode.Stloc); > CodeBuilder.WriteInt32(slotIndex); > } > ``` > > This is incorrect for ECMA-335 short-inline variable operands. Long-form local/argument opcodes such as `ldloc`, `stloc`, `ldloca`, `ldarg`, `ldarga`, and `starg` use a 2-byte unsigned local/argument index operand, not a 4-byte int32 operand. The `_s` forms use a 1-byte operand, and the dedicated `_0` through `_3` forms have no operand. > > Tasks: > 1. Audit `InstructionEncoder.cs` for all similar local/argument encoding patterns, especially: > - `LoadLocal` > - `StoreLocal` > - `LoadLocalAddress` > - `LoadArgument` > - `LoadArgumentAddress` > - `StoreArgument` > 2. Fix long-form local/argument operand emission to write a 2-byte unsigned operand (`ushort`) instead of 4 bytes. > 3. Adjust validation/range checks so indexes greater than `ushort.MaxValue` are rejected with `ArgumentOutOfRangeException`, and negative indexes continue to be rejected. > 4. Preserve optimal encodings where applicable: > - local load/store indexes 0-3 should still use dedicated single-byte opcodes > - indexes <= `byte.MaxValue` should still use `_s` forms where currently used > - indexes between 256 and 65535 should use the non-short form with a 2-byte operand > 5. Add regression tests in the System.Reflection.Metadata test suite that verify exact bytes emitted for the relevant methods/opcodes, including at least: > - `StoreLocal(256)` emits `FE 0E 00 01` (opcode `stloc`, ushort operand 256 little-endian), not six bytes > - similar tests for `LoadLocal(256)`, `LoadLocalAddress(256)`, `LoadArgument(256)`, `LoadArgumentAddress(256)`, and `StoreArgument(256)` > - boundary tests for `ushort.MaxValue` succeeding and `ushort.MaxValue + 1` throwing, if consistent with the existing test style > 6. Run or document relevant test commands if possible. > > Please create a pull request with the fix and regression tests. > > The following is the prior conversation context from the user's chat exploration (may be truncated): > > User: is there a bug around ILOpCode.Stloc writing? > > Assistant: Yes — **`InstructionEncoder.StoreLocal` appears to write the wrong operand size for the long-form `stloc` opcode**. > > In `StoreLocal(int slotIndex)`, for `slotIndex > byte.MaxValue`, it emits: > > ```csharp name=InstructionEncoder.cs url=https://github.com/dotnet/runtime/blob/88dbd1173bde444926a06aeb14f507b04b451aa8/src/libraries/System.Reflection.Metadata/src/System/Reflection/Metadata/Ecma335/Encoding/InstructionEncoder.cs#L264-L268 > else if (slotIndex > 0) > { > OpCode(ILOpCode.Stloc); > CodeBuilder.WriteInt32(slotIndex); > } > ``` > > But `ILOpCode.Stloc` is `0xfe0e`, i.e. the long local-store form. Per IL encoding, `stloc` takes an **unsigned int16** local index operand, not int32. The corresponding long local load/address/arg forms have the same issue pattern in this file too: > > - `ldloc` writes `Int32` > - `stloc` writes `Int32` > - `ldloca` writes `Int32` > - `ldarg`, `ldarga`, `starg` write `Int32` > > For `stloc`, this means encoding local slot 256 would produce: > > ```text > FE 0E 00 01 00 00 > ``` > > instead of the expected: > > ```text > FE 0E 00 01 > ``` > > assuming little-endian operand bytes. > > So yes, around `ILOpCode.Stloc` writing, the likely bug is that `CodeBuilder.WriteInt32(slotIndex)` should be `WriteUInt16((ushort)slotIndex)` or equivalent, and the accepted range should probably be `ushort.MaxValue`, not any positive `int`. > > User: make a PR that fixes the bug and adds a regression test. look for similar patterns elsewhere (e.g ldloc/ldloca/ldarg/etc.) > > Assistant: I’m waiting for your response to the confirmation dialog so I can continue creating the PR. > > </details> <!-- START COPILOT CODING AGENT SUFFIX --> *This pull request was created from Copilot chat.* > --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: MichalStrehovsky <[email protected]>
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 subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
InstructionEncoderwas emitting a 4-byteint32operand for long-form local/argument opcodes (ldloc,stloc,ldloca,ldarg,ldarga,starg). Per ECMA-335, these opcodes take a 2-byte unsigned short operand. For example,StoreLocal(256)was emittingFE 0E 00 01 00 00instead of the correctFE 0E 00 01.Changes
InstructionEncoder.cs— all sixLoad/Storelocal/argument methods:WriteInt32(slotIndex)→WriteUInt16((ushort)slotIndex)for the long-form branchslotIndex > 0tounchecked((uint)slotIndex) <= ushort.MaxValue, so indexes above 65535 now throwArgumentOutOfRangeException<exception>comments updated to reflect the new upper boundInstructionEncoderTests.cs— all six corresponding test methods:il.XXX(int.MaxValue)call replaced withil.XXX(ushort.MaxValue)(now a valid, in-range input)0xFE, 0x0E, 0xFF, 0xFFinstead of0xFE, 0x0E, 0xFF, 0xFF, 0xFF, 0x7F)Assert.Throws<ArgumentOutOfRangeException>forushort.MaxValue + 1andint.MaxValueOriginal prompt
This pull request was created from Copilot chat.