You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
InstructionEncoder produces corrupted IL bodies when its CodeBuilder (BlobBuilder) crosses chunk boundaries (default 256 bytes). The corruption occurs when mixing InstructionEncoder.OpCode() with direct CodeBuilder.WriteByte() / CodeBuilder.WriteInt32() calls for instruction operands.
This is the expected usage pattern for instructions like ldarg.s where OpCode(ILOpCode.Ldarg_s) emits the opcode and CodeBuilder.WriteByte(index) emits the operand — there is no dedicated InstructionEncoder API for these operands.
Impact
Any consumer of InstructionEncoder that writes operand bytes directly to CodeBuilder (rather than using high-level APIs like Branch, LoadString, Token) will produce corrupted IL when the method body exceeds 256 bytes.
Workaround
Use new BlobBuilder(capacity) with a capacity larger than the expected IL body size to avoid multi-chunk operation:
Description
InstructionEncoderproduces corrupted IL bodies when itsCodeBuilder(BlobBuilder) crosses chunk boundaries (default 256 bytes). The corruption occurs when mixingInstructionEncoder.OpCode()with directCodeBuilder.WriteByte()/CodeBuilder.WriteInt32()calls for instruction operands.This is the expected usage pattern for instructions like
ldarg.swhereOpCode(ILOpCode.Ldarg_s)emits the opcode andCodeBuilder.WriteByte(index)emits the operand — there is no dedicatedInstructionEncoderAPI for these operands.Impact
Any consumer of
InstructionEncoderthat writes operand bytes directly toCodeBuilder(rather than using high-level APIs likeBranch,LoadString,Token) will produce corrupted IL when the method body exceeds 256 bytes.Workaround
Use
new BlobBuilder(capacity)with a capacity larger than the expected IL body size to avoid multi-chunk operation:Reproduction
See the standalone repro in the comment below. Reproduces on .NET 9 and .NET 11.
Note
This issue was created with the assistance of Copilot.