Skip to content
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
30 changes: 30 additions & 0 deletions eng/vendored-files.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,36 @@
}
]
},
{
"id": "msbuild-dotnet-muxer-locator-windows",
"local_path": "src/Platform/Microsoft.Testing.Platform.MSBuild/Tasks/DotnetMuxerLocator.Windows.cs",
"notes": "Adapted from vstest's DotnetHostHelper. Windows PE header parsing, split out of DotnetMuxerLocator.cs.",
"sources": [
{
"repo": "microsoft/vstest",
"ref": "main",
"path": "src/Microsoft.TestPlatform.CoreUtilities/Helpers/DotnetHostHelper.cs",
"baseline_ref_sha": "d5a7e3b67a2ccb6dc5b3b81255f3e6571c318b78",
"baseline_blob_sha": "d9bc7043f4612d95b7415f3512cbb2b188e825ec",
"scope": "GetMuxerArchitectureByPEHeaderOnWin"
}
]
},
{
"id": "msbuild-dotnet-muxer-locator-macos",
"local_path": "src/Platform/Microsoft.Testing.Platform.MSBuild/Tasks/DotnetMuxerLocator.MacOs.cs",
"notes": "Adapted from vstest's DotnetHostHelper. Mach-O header parsing, split out of DotnetMuxerLocator.cs.",
"sources": [
{
"repo": "microsoft/vstest",
"ref": "main",
"path": "src/Microsoft.TestPlatform.CoreUtilities/Helpers/DotnetHostHelper.cs",
"baseline_ref_sha": "d5a7e3b67a2ccb6dc5b3b81255f3e6571c318b78",
"baseline_blob_sha": "d9bc7043f4612d95b7415f3512cbb2b188e825ec",
"scope": "GetMuxerArchitectureByMachoOnMac and Mach-O constants"
}
]
},
{
"id": "platform-env-config-provider",
"local_path": "src/Platform/Microsoft.Testing.Platform/Configurations/EnvironmentVariablesConfigurationProvider.cs",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

// Adapted from https://github.com/microsoft/vstest/blob/main/src/Microsoft.TestPlatform.CoreUtilities/Helpers/DotnetHostHelper.cs
using System.Buffers.Binary;

namespace Microsoft.Testing.Platform.MSBuild.Tasks;

internal sealed partial class DotnetMuxerLocator
Comment thread
Evangelink marked this conversation as resolved.
Resolved
{
// Mach-O magic numbers from https://en.wikipedia.org/wiki/Mach-O
private const uint MachOMagic32BigEndian = 0xfeedface; // 32-bit big-endian
private const uint MachOMagic64BigEndian = 0xfeedfacf; // 64-bit big-endian
private const uint MachOMagic32LittleEndian = 0xcefaedfe; // 32-bit little-endian
private const uint MachOMagic64LittleEndian = 0xcffaedfe; // 64-bit little-endian
private const uint MachOMagicFatBigEndian = 0xcafebabe; // Multi-architecture big-endian

// See https://opensource.apple.com/source/xnu/xnu-2050.18.24/EXTERNAL_HEADERS/mach-o/loader.h
// https://opensource.apple.com/source/xnu/xnu-4570.41.2/osfmk/mach/machine.h.auto.html
// https://opensource.apple.com/source/xnu/xnu-4570.41.2/EXTERNAL_HEADERS/mach-o/fat.h.auto.html
private PlatformArchitecture? GetMuxerArchitectureByMachoOnMac(string path)
{
try
{
using var headerReader = new FileStream(path, FileMode.Open, FileAccess.Read);
uint magic = BinaryPrimitives.ReadUInt32BigEndian(ReadFourBytes(headerReader));

// Validate magic bytes to ensure this is a valid Mach-O binary
if (magic is not (MachOMagic32BigEndian or MachOMagic64BigEndian or MachOMagic32LittleEndian or MachOMagic64LittleEndian or MachOMagicFatBigEndian))
{
_resolutionLog($"DotnetHostHelper.GetMuxerArchitectureByMachoOnMac: Invalid Mach-O magic bytes: 0x{magic:X8}");
return null;
}

if (magic == MachOMagicFatBigEndian)
{
// A fat (multi-architecture) header is followed by 'nfat_arch' (4 bytes) and then
// one or more 'fat_arch' entries. The cputype we care about is the first field of
// the first 'fat_arch' entry, i.e. at offset 8 (magic + nfat_arch), not offset 4.
ReadFourBytes(headerReader);
}

byte[] cpuInfoBytes = ReadFourBytes(headerReader);
uint cpuInfo = magic is MachOMagic32LittleEndian or MachOMagic64LittleEndian
? BinaryPrimitives.ReadUInt32LittleEndian(cpuInfoBytes)
: BinaryPrimitives.ReadUInt32BigEndian(cpuInfoBytes);
PlatformArchitecture? architecture = (MacOsCpuType)cpuInfo switch
{
MacOsCpuType.Arm64Magic or MacOsCpuType.Arm64Cigam => PlatformArchitecture.ARM64,
MacOsCpuType.X64Magic or MacOsCpuType.X64Cigam => PlatformArchitecture.X64,
MacOsCpuType.X86Magic or MacOsCpuType.X86Cigam => PlatformArchitecture.X86,
Comment thread
Evangelink marked this conversation as resolved.
Resolved
_ => null,
};

return architecture;
}
catch (Exception ex)
{
// In case of failure during header reading we must fallback to the next place(default installation path)
_resolutionLog($"DotnetHostHelper.GetMuxerArchitectureByMachoOnMac: Failed to get architecture from Mach-O for '{path}'\n{ex}");
}

return null;
}

private static byte[] ReadFourBytes(FileStream stream)
{
byte[] buffer = new byte[4];
#pragma warning disable CA2022 // Avoid inexact read with 'Stream.Read'
stream.Read(buffer, 0, buffer.Length);
#pragma warning restore CA2022 // Avoid inexact read with 'Stream.Read'
return buffer;
}

internal enum MacOsCpuType : uint
{
/// <summary>
/// Arm64Magic.
/// </summary>
Arm64Magic = 0x0100000c,

/// <summary>
/// Arm64Cigam.
/// </summary>
Arm64Cigam = 0x0c000001,

/// <summary>
/// X64Magic.
/// </summary>
X64Magic = 0x01000007,

/// <summary>
/// X64Cigam.
/// </summary>
X64Cigam = 0x07000001,

/// <summary>
/// X86Magic.
/// </summary>
X86Magic = 0x00000007,

/// <summary>
/// X86Cigam.
/// </summary>
X86Cigam = 0x07000000,
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

// Adapted from https://github.com/microsoft/vstest/blob/main/src/Microsoft.TestPlatform.CoreUtilities/Helpers/DotnetHostHelper.cs
namespace Microsoft.Testing.Platform.MSBuild.Tasks;

internal sealed partial class DotnetMuxerLocator
Comment thread
Evangelink marked this conversation as resolved.
Resolved
{
private static PlatformArchitecture? GetMuxerArchitectureByPEHeaderOnWin(string path, Action<string> resolutionLog)
{
// For details refer to below code available on MSDN.
// https://code.msdn.microsoft.com/windowsapps/CSCheckExeType-aab06100#content
PlatformArchitecture? archType = null;
ushort machine = 0;

uint peHeader;
const int imageFileMachineAmd64 = 0x8664;
const int imageFileMachineIa64 = 0x200;
const int imageFileMachineI386 = 0x14c;
const int imageFileMachineArm = 0x01c0; // ARM Little-Endian
const int imageFileMachineThumb = 0x01c2; // ARM Thumb/Thumb-2 Little-Endian
const int imageFileMachineArmnt = 0x01c4; // ARM Thumb-2 Little-Endian
const int imageFileMachineArm64 = 0xAA64;

// get the input stream
using Stream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
using var reader = new BinaryReader(fs);
bool validImage = true;

// PE Header starts @ 0x3C (60). Its a 4 byte header.
fs.Position = 0x3C;
Comment thread
Evangelink marked this conversation as resolved.
Resolved
peHeader = reader.ReadUInt32();

// Check if the offset is invalid
if (peHeader > fs.Length - 5)
{
resolutionLog("[GetMuxerArchitectureByPEHeaderOnWin]Invalid offset");
validImage = false;
}

if (validImage)
{
// Moving to PE Header start location...
fs.Position = peHeader;

// peHeaderSignature
// 0x00004550 is the letters "PE" followed by two terminating zeros.
if (reader.ReadUInt32() != 0x00004550)
{
validImage = false;
resolutionLog("[GetMuxerArchitectureByPEHeaderOnWin]Missing PE signature");
}

if (validImage)
{
// Read the image file header.
machine = reader.ReadUInt16();
reader.ReadUInt16(); // NumberOfSections
reader.ReadUInt32(); // TimeDateStamp
reader.ReadUInt32(); // PointerToSymbolTable
reader.ReadUInt32(); // NumberOfSymbols
reader.ReadUInt16(); // SizeOfOptionalHeader
reader.ReadUInt16(); // Characteristics

// magic number.32bit or 64bit assembly.
ushort magic = reader.ReadUInt16();
if (magic is not 0x010B and not 0x020B)
{
validImage = false;
}
}

if (validImage)
{
switch (machine)
{
case imageFileMachineI386:
archType = PlatformArchitecture.X86;
break;

case imageFileMachineAmd64:
case imageFileMachineIa64:
archType = PlatformArchitecture.X64;
break;

case imageFileMachineArm64:
archType = PlatformArchitecture.ARM64;
break;

case imageFileMachineArm:
case imageFileMachineThumb:
case imageFileMachineArmnt:
archType = PlatformArchitecture.ARM;
break;
}
}
}

return archType ?? throw new InvalidOperationException($"Could not determine the CPU architecture from the PE (Portable Executable) file at '{path}'. The file may be corrupt or contain an unrecognized machine type.");
}
}
Loading
Loading