Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[release/8.0] [wasm] Endian fix for Webcil #92495

Merged
merged 3 commits into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading