Skip to content

Commit

Permalink
[System.Runtime.InteropServices] Detect Arm/Arm64 in RuntimeInformati…
Browse files Browse the repository at this point in the history
…on (#10088)

* For ProcessArchitecture, use the DllMap arch of the runtime.

* For OSArchitecture, make an educated guess combining ProcessArchitecture and Environment.Is64BitOperatingSystem.

* For non-Intel/ARM architectures, (supported by Mono and not on the chopping block are PPC32/64, z, and WebAssembly) please dogpile onto dotnet/corefx#30706.
  • Loading branch information
NattyNarwhal authored and akoeplinger committed Aug 20, 2018
1 parent ddda306 commit 3965597
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 4 deletions.
Expand Up @@ -30,11 +30,19 @@

using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;

namespace System.Runtime.InteropServices
{
public static class RuntimeInformation
{
/* gets the runtime's arch from the value it uses for DllMap */
static extern string RuntimeArchitecture
{
[MethodImpl (MethodImplOptions.InternalCall)]
get;
}

public static string FrameworkDescription {
get {
return "Mono " + Mono.Runtime.GetDisplayName ();
Expand Down Expand Up @@ -75,17 +83,40 @@ public static Architecture OSArchitecture
{
get
{
// TODO: very barebones implementation, doesn't respect ARM
return Environment.Is64BitOperatingSystem ? Architecture.X64 : Architecture.X86;
switch (RuntimeArchitecture) {
case "arm":
case "armv8":
return Environment.Is64BitOperatingSystem ? Architecture.Arm64 : Architecture.Arm;
case "x86":
case "x86-64":
// upstream only has these values; try to pretend we're x86 if nothing matches
// want more? bug: https://github.com/dotnet/corefx/issues/30706
default:
return Environment.Is64BitOperatingSystem ? Architecture.X64 : Architecture.X86;
}
}
}

public static Architecture ProcessArchitecture
{
get
{
// TODO: very barebones implementation, doesn't respect ARM
return Environment.Is64BitProcess ? Architecture.X64 : Architecture.X86;
// we can use the runtime's compiled config options for DllMaps here
// process architecure for us is runtime architecture (OS is much harder)
// see for values: mono-config.c
switch (RuntimeArchitecture) {
case "x86":
return Architecture.X86;
case "x86-64":
return Architecture.X64;
case "arm":
return Architecture.Arm;
case "armv8":
return Architecture.Arm64;
// see comment in OSArchiteture default case
default:
return Environment.Is64BitProcess ? Architecture.X64 : Architecture.X86;
}
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions mono/metadata/icall-def.h
Expand Up @@ -734,6 +734,10 @@ HANDLES(ICALL(MARSHAL_34, "StructureToPtr", ves_icall_System_Runtime_InteropServ
ICALL(MARSHAL_35, "UnsafeAddrOfPinnedArrayElement", ves_icall_System_Runtime_InteropServices_Marshal_UnsafeAddrOfPinnedArrayElement)
HANDLES(ICALL(MARSHAL_41, "copy_from_unmanaged_fixed", ves_icall_System_Runtime_InteropServices_Marshal_copy_from_unmanaged))
HANDLES(ICALL(MARSHAL_42, "copy_to_unmanaged_fixed", ves_icall_System_Runtime_InteropServices_Marshal_copy_to_unmanaged))

ICALL_TYPE(RUNTIMEINFO, "System.Runtime.InteropServices.RuntimeInformation", RUNTIMEINFO_1)
HANDLES(ICALL(RUNTIMEINFO_1, "get_RuntimeArchitecture", ves_icall_System_Runtime_InteropServices_RuntimeInformation_get_RuntimeArchitecture))

#ifndef DISABLE_COM
ICALL_TYPE(WINDOWSRUNTIME_UNM, "System.Runtime.InteropServices.WindowsRuntime.UnsafeNativeMethods", WINDOWSRUNTIME_UNM_0)
HANDLES(ICALL(WINDOWSRUNTIME_UNM_0, "GetRestrictedErrorInfo", ves_icall_System_Runtime_InteropServices_WindowsRuntime_UnsafeNativeMethods_GetRestrictedErrorInfo))
Expand Down
11 changes: 11 additions & 0 deletions mono/metadata/icall.c
Expand Up @@ -7719,6 +7719,17 @@ ves_icall_System_Runtime_InteropServices_Marshal_PrelinkAll (MonoReflectionTypeH
}
}

/*
* used by System.Runtime.InteropServices.RuntimeInformation.(OS|Process)Architecture;
* which use them in different ways for filling in an enum
*/
ICALL_EXPORT MonoStringHandle
ves_icall_System_Runtime_InteropServices_RuntimeInformation_get_RuntimeArchitecture (MonoError *error)
{
error_init (error);
return mono_string_new_handle (mono_domain_get (), mono_config_get_cpu (), error);
}

ICALL_EXPORT int
ves_icall_Interop_Sys_DoubleToString(double value, char *format, char *buffer, int bufferLength)
{
Expand Down

0 comments on commit 3965597

Please sign in to comment.