Skip to content

Commit

Permalink
[wasm] Endian fix for Webcil (#92274)
Browse files Browse the repository at this point in the history
* [wasm] Endian fix for Webcil

'dotnet new blazorwasm' command failed on s390x and was throwing a not implemented exception

The issue was with with the WebCil writer and reader, specific endianness conversions relating to the webcil payload were not implemented for big endian machines.

We considered fixing the generic implementation, but there were only two structures in use: WebcilHeader and WebcilSectionHeader, so it was easier to handle them explicitly.


---------

Co-authored-by: Aleksey Kliger <[email protected]>
  • Loading branch information
saitama951 and lambdageek authored Sep 22, 2023
1 parent 913231a commit b82b1d9
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

<ItemGroup>
<!-- we need to keep the version of System.Reflection.Metadata in sync with dotnet/msbuild and dotnet/sdk -->
<PackageReference Include="System.Memory" Version="$(SystemMemoryVersion)" />
<PackageReference Include="System.Reflection.Metadata" Version="$(SystemReflectionMetadataVersion)" />
<PackageReference Include="System.Collections.Immutable" Version="$(SystemCollectionsImmutableVersion)" />
</ItemGroup>
Expand Down
34 changes: 23 additions & 11 deletions src/tasks/Microsoft.NET.WebAssembly.Webcil/WebcilConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Buffers.Binary;
using System.IO;
using System.Collections.Immutable;
using System.Reflection.PortableExecutable;
Expand Down Expand Up @@ -174,16 +175,23 @@ public unsafe void GatherInfo(PEReader peReader, out WCFileInfo wcInfo, out PEFi
SectionStart: firstWCSection);
}

private static void WriteHeader(Stream s, WebcilHeader header)
private static void WriteHeader(Stream s, WebcilHeader webcilHeader)
{
WriteStructure(s, header);
if (!BitConverter.IsLittleEndian)
{
webcilHeader.version_major = BinaryPrimitives.ReverseEndianness(webcilHeader.version_major);
webcilHeader.version_minor = BinaryPrimitives.ReverseEndianness(webcilHeader.version_minor);
webcilHeader.coff_sections = BinaryPrimitives.ReverseEndianness(webcilHeader.coff_sections);
webcilHeader.pe_cli_header_rva = BinaryPrimitives.ReverseEndianness(webcilHeader.pe_cli_header_rva);
webcilHeader.pe_cli_header_size = BinaryPrimitives.ReverseEndianness(webcilHeader.pe_cli_header_size);
webcilHeader.pe_debug_rva = BinaryPrimitives.ReverseEndianness(webcilHeader.pe_debug_rva);
webcilHeader.pe_debug_size = BinaryPrimitives.ReverseEndianness(webcilHeader.pe_debug_size);
}
WriteStructure(s, webcilHeader);
}

private static void WriteSectionHeaders(Stream s, ImmutableArray<WebcilSectionHeader> sectionsHeaders)
{
// FIXME: fixup endianness
if (!BitConverter.IsLittleEndian)
throw new NotImplementedException();
foreach (var sectionHeader in sectionsHeaders)
{
WriteSectionHeader(s, sectionHeader);
Expand All @@ -192,16 +200,23 @@ private static void WriteSectionHeaders(Stream s, ImmutableArray<WebcilSectionHe

private static void WriteSectionHeader(Stream s, WebcilSectionHeader sectionHeader)
{
if (!BitConverter.IsLittleEndian)
{
sectionHeader = new WebcilSectionHeader
(
virtualSize: BinaryPrimitives.ReverseEndianness(sectionHeader.VirtualSize),
virtualAddress: BinaryPrimitives.ReverseEndianness(sectionHeader.VirtualAddress),
sizeOfRawData: BinaryPrimitives.ReverseEndianness(sectionHeader.SizeOfRawData),
pointerToRawData: BinaryPrimitives.ReverseEndianness(sectionHeader.PointerToRawData)
);
}
WriteStructure(s, sectionHeader);
}

#if NETCOREAPP2_1_OR_GREATER
private static void WriteStructure<T>(Stream s, T structure)
where T : unmanaged
{
// FIXME: fixup endianness
if (!BitConverter.IsLittleEndian)
throw new NotImplementedException();
unsafe
{
byte* p = (byte*)&structure;
Expand All @@ -212,9 +227,6 @@ private static void WriteStructure<T>(Stream s, T structure)
private static void WriteStructure<T>(Stream s, T structure)
where T : unmanaged
{
// FIXME: fixup endianness
if (!BitConverter.IsLittleEndian)
throw new NotImplementedException();
int size = Marshal.SizeOf<T>();
byte[] buffer = new byte[size];
IntPtr ptr = IntPtr.Zero;
Expand Down
37 changes: 30 additions & 7 deletions src/tasks/Microsoft.NET.WebAssembly.Webcil/WebcilReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;

using System.Buffers.Binary;
using System.Reflection.Metadata;
using System.Reflection.PortableExecutable;

Expand Down Expand Up @@ -63,14 +63,20 @@ private unsafe bool ReadHeader()
{
return false;
}
if (!BitConverter.IsLittleEndian)
{
throw new NotImplementedException("TODO: implement big endian support");
}
fixed (byte* p = buffer)
{
header = *(WebcilHeader*)p;
}
if (!BitConverter.IsLittleEndian)
{
header.version_major = BinaryPrimitives.ReverseEndianness(header.version_major);
header.version_minor = BinaryPrimitives.ReverseEndianness(header.version_minor);
header.coff_sections = BinaryPrimitives.ReverseEndianness(header.coff_sections);
header.pe_cli_header_rva = BinaryPrimitives.ReverseEndianness(header.pe_cli_header_rva);
header.pe_cli_header_size = BinaryPrimitives.ReverseEndianness(header.pe_cli_header_size);
header.pe_debug_rva = BinaryPrimitives.ReverseEndianness(header.pe_debug_rva);
header.pe_debug_rva = BinaryPrimitives.ReverseEndianness(header.pe_debug_size);
}
if (header.id[0] != 'W' || header.id[1] != 'b'
|| header.id[2] != 'I' || header.id[3] != 'L'
|| header.version_major != Internal.Constants.WC_VERSION_MAJOR
Expand Down Expand Up @@ -346,6 +352,7 @@ private long TranslateRVA(uint rva)

private unsafe ImmutableArray<WebcilSectionHeader> ReadSections()
{
WebcilSectionHeader secheader;
var sections = ImmutableArray.CreateBuilder<WebcilSectionHeader>(_header.coff_sections);
var buffer = new byte[Marshal.SizeOf<WebcilSectionHeader>()];
_stream.Seek(SectionDirectoryOffset + _webcilInWasmOffset, SeekOrigin.Begin);
Expand All @@ -357,8 +364,24 @@ private unsafe ImmutableArray<WebcilSectionHeader> ReadSections()
}
fixed (byte* p = buffer)
{
// FIXME endianness
sections.Add(*(WebcilSectionHeader*)p);
secheader = (*(WebcilSectionHeader*)p);
}
if (!BitConverter.IsLittleEndian)
{
sections.Add
(
new WebcilSectionHeader
(
virtualSize: BinaryPrimitives.ReverseEndianness(secheader.VirtualSize),
virtualAddress: BinaryPrimitives.ReverseEndianness(secheader.VirtualAddress),
sizeOfRawData: BinaryPrimitives.ReverseEndianness(secheader.SizeOfRawData),
pointerToRawData: BinaryPrimitives.ReverseEndianness(secheader.PointerToRawData)
)
);
}
else
{
sections.Add(secheader);
}
}
return sections.MoveToImmutable();
Expand Down

0 comments on commit b82b1d9

Please sign in to comment.