Repository: dotnet/runtime
File: src/coreclr/tools/Common/Compiler/ObjectWriter/WasmObjectWriter.cs (C#)

Code before:
            SectionWriter writer = GetOrCreateSection(WasmObjectNodeSection.FunctionSection);

            WasmFuncType signature = WasmAbiContext.GetSignature(desc);
            if (!_uniqueSignatures.TryGetValue(signature, out int signatureIndex))
            {
                throw new InvalidOperationException($"Signature index not found for function: {desc.GetName()}");
            }

            writer.WriteULEB128((ulong)signatureIndex);
        }


        private int _numImports;
        /// <summary>
        /// Writes the common prefix for an import entry, which includes the module name, import name, and kind.
        /// </summary>
        private SectionWriter WriteImport(WasmImport import)
        {
            SectionWriter writer = GetOrCreateSection(WasmObjectNodeSection.ImportSection);
            writer.WriteUtf8WithLength(import.Module);
            writer.WriteUtf8WithLength(import.Name);
            writer.WriteByte((byte)import.Kind);

            int size = import.EncodeSize();
            import.Encode(writer.Buffer.GetSpan(size));
            writer.Buffer.Advance((int)size);

            _numImports++;
            return writer;
        }

        /// <summary>
        /// WebAssembly export descriptor kinds per the spec.
        /// </summary>
        internal enum WasmExportKind : byte
        {
            Function = 0x00,
            Table = 0x01,
            Memory = 0x02,
            Global = 0x03
        }

        private int _numExports;
        private void WriteExport(string name, WasmExportKind kind, int index)
        {
            SectionWriter writer = GetOrCreateSection(WasmObjectNodeSection.ExportSection);
            int length = Encoding.UTF8.GetByteCount(name);
            writer.WriteULEB128((ulong)length);
            writer.WriteUtf8StringNoNull(name);
            writer.WriteByte((byte)kind);
            writer.WriteULEB128((ulong)index);

Review comment (bug):
`WriteImport` advances the section buffer by `EncodeSize()` rather than by the actual byte count returned from `Encode(...)`. If `EncodeSize()` and `Encode()` ever diverge (or a future implementation writes fewer bytes), this will either leave garbage bytes in the section or corrupt subsequent writes. Capture the return value from `Encode(...)`, assert it matches the expected size, and advance by the encoded byte count.
```suggestion
            int expectedSize = import.EncodeSize();
            Span<byte> buffer = writer.Buffer.GetSpan(expectedSize);
            int encodedSize = import.Encode(buffer);
            Debug.Assert(encodedSize == expectedSize, $"Encoded import size mismatch in {nameof(WriteImport)}.");
            writer.Buffer.Advance(encodedSize);
```

Code after:
        {
            SectionWriter writer = GetOrCreateSection(WasmObjectNodeSection.FunctionSection);

            WasmFuncType signature = WasmAbiContext.GetSignature(desc);
            if (!_uniqueSignatures.TryGetValue(signature, out int signatureIndex))
            {
                throw new InvalidOperationException($"Signature index not found for function: {desc.GetName()}");
            }

            writer.WriteULEB128((ulong)signatureIndex);
        }


        private int _numImports;
        /// <summary>
        /// Writes the given import entry, including its prefix (module/name/kind) and body (external ref).
        /// </summary>
        private SectionWriter WriteImport(WasmImport import)
        {
            SectionWriter writer = GetOrCreateSection(WasmObjectNodeSection.ImportSection);
            writer.WriteUtf8WithLength(import.Module);
            writer.WriteUtf8WithLength(import.Name);
            writer.WriteByte((byte)import.Kind);

            int encodeSize = import.EncodeSize();
            int bytesWritten = import.Encode(writer.Buffer.GetSpan(encodeSize));
            Debug.Assert(bytesWritten == encodeSize);
            writer.Buffer.Advance((int)bytesWritten);

            _numImports++;
            return writer;
        }

        /// <summary>
        /// WebAssembly export descriptor kinds per the spec.
        /// </summary>
        internal enum WasmExportKind : byte
        {
            Function = 0x00,
            Table = 0x01,
            Memory = 0x02,
            Global = 0x03
        }

        private int _numExports;
        private void WriteExport(string name, WasmExportKind kind, int index)
        {
            SectionWriter writer = GetOrCreateSection(WasmObjectNodeSection.ExportSection);
            int length = Encoding.UTF8.GetByteCount(name);
            writer.WriteULEB128((ulong)length);
            writer.WriteUtf8StringNoNull(name);