Repository navigation
Split DotnetMuxerLocator into platform-focused modules #11534
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
Merged
Amaury Levé (Evangelink)
merged 4 commits into
main
from
copilot/refactor-dotnetmuxerlocator
Sep 27, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
src/Platform/Microsoft.Testing.Platform.MSBuild/Tasks/DotnetMuxerLocator.MacOs.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| { | ||
| // 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, | ||
|
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, | ||
| } | ||
| } | ||
101 changes: 101 additions & 0 deletions
101
src/Platform/Microsoft.Testing.Platform.MSBuild/Tasks/DotnetMuxerLocator.Windows.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
|
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; | ||
|
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."); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.