-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add mechanism for runtime to query host for information (#78798)
- Loading branch information
1 parent
360d405
commit 45f5e85
Showing
14 changed files
with
335 additions
and
29 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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,16 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
#ifndef _HOSTINFORMATION_H_ | ||
#define _HOSTINFORMATION_H_ | ||
|
||
#include <corehost/host_runtime_contract.h> | ||
|
||
class HostInformation | ||
{ | ||
public: | ||
static void SetContract(_In_ host_runtime_contract* hostContract); | ||
static bool GetProperty(_In_z_ const char* name, SString& value); | ||
}; | ||
|
||
#endif // _HOSTINFORMATION_H_ |
This file contains 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
This file contains 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
This file contains 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,42 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
#include "common.h" | ||
#include "hostinformation.h" | ||
|
||
namespace | ||
{ | ||
host_runtime_contract* s_hostContract = nullptr; | ||
} | ||
|
||
void HostInformation::SetContract(_In_ host_runtime_contract* hostContract) | ||
{ | ||
_ASSERTE(s_hostContract == nullptr); | ||
s_hostContract = hostContract; | ||
} | ||
|
||
bool HostInformation::GetProperty(_In_z_ const char* name, SString& value) | ||
{ | ||
if (s_hostContract == nullptr || s_hostContract->get_runtime_property == nullptr) | ||
return false; | ||
|
||
size_t len = MAX_PATH + 1; | ||
char* dest = value.OpenUTF8Buffer(static_cast<COUNT_T>(len)); | ||
size_t lenActual = s_hostContract->get_runtime_property(name, dest, len, s_hostContract->context); | ||
value.CloseBuffer(); | ||
|
||
// Doesn't exist or failed to get property | ||
if (lenActual == (size_t)-1 || lenActual == 0) | ||
return false; | ||
|
||
if (lenActual <= len) | ||
return true; | ||
|
||
// Buffer was not large enough | ||
len = lenActual; | ||
dest = value.OpenUTF8Buffer(static_cast<COUNT_T>(len)); | ||
lenActual = s_hostContract->get_runtime_property(name, dest, len, s_hostContract->context); | ||
value.CloseBuffer(); | ||
|
||
return lenActual > 0 && lenActual <= len; | ||
} |
This file contains 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
86 changes: 86 additions & 0 deletions
86
src/installer/tests/Assets/TestProjects/HostApiInvokerApp/HostRuntimeContract.cs
This file contains 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,86 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.InteropServices; | ||
using System.Text; | ||
|
||
namespace HostApiInvokerApp | ||
{ | ||
public static unsafe class HostRuntimeContract | ||
{ | ||
internal struct host_runtime_contract | ||
{ | ||
public nint size; | ||
public void* context; | ||
public delegate* unmanaged[Stdcall]<byte*, byte*, nint, void*, nint> get_runtime_property; | ||
public IntPtr bundle_probe; | ||
public IntPtr pinvoke_override; | ||
} | ||
|
||
private static host_runtime_contract GetContract() | ||
{ | ||
string contractString = (string)AppContext.GetData("HOST_RUNTIME_CONTRACT"); | ||
if (string.IsNullOrEmpty(contractString)) | ||
throw new Exception("HOST_RUNTIME_CONTRACT not found"); | ||
|
||
host_runtime_contract* contract = (host_runtime_contract*)Convert.ToUInt64(contractString, 16); | ||
if (contract->size != sizeof(host_runtime_contract)) | ||
throw new Exception($"Unexpected contract size {contract->size}. Expected: {sizeof(host_runtime_contract)}"); | ||
|
||
return *contract; | ||
} | ||
|
||
private static void Test_get_runtime_property(string[] args) | ||
{ | ||
host_runtime_contract contract = GetContract(); | ||
|
||
foreach (string name in args) | ||
{ | ||
string value = GetProperty(name, contract); | ||
Console.WriteLine($"{nameof(host_runtime_contract.get_runtime_property)}: {name} = {(value == null ? "<none>" : value)}"); | ||
} | ||
|
||
static string GetProperty(string name, host_runtime_contract contract) | ||
{ | ||
Span<byte> nameSpan = stackalloc byte[Encoding.UTF8.GetMaxByteCount(name.Length)]; | ||
byte* namePtr = (byte*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(nameSpan)); | ||
int nameLen = Encoding.UTF8.GetBytes(name, nameSpan); | ||
nameSpan[nameLen] = 0; | ||
|
||
nint len = 256; | ||
byte* buffer = stackalloc byte[(int)len]; | ||
nint lenActual = contract.get_runtime_property(namePtr, buffer, len, contract.context); | ||
if (lenActual <= 0) | ||
{ | ||
Console.WriteLine($"No value for {name} - {nameof(host_runtime_contract.get_runtime_property)} returned {lenActual}"); | ||
return null; | ||
} | ||
|
||
if (lenActual <= len) | ||
return Encoding.UTF8.GetString(buffer, (int)lenActual); | ||
|
||
len = lenActual; | ||
byte* expandedBuffer = stackalloc byte[(int)len]; | ||
lenActual = contract.get_runtime_property(namePtr, expandedBuffer, len, contract.context); | ||
return Encoding.UTF8.GetString(expandedBuffer, (int)lenActual); | ||
} | ||
} | ||
|
||
public static bool RunTest(string apiToTest, string[] args) | ||
{ | ||
switch (apiToTest) | ||
{ | ||
case $"{nameof(host_runtime_contract)}.{nameof(host_runtime_contract.get_runtime_property)}": | ||
Test_get_runtime_property(args); | ||
break; | ||
default: | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
} | ||
|
||
} |
This file contains 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
This file contains 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
Oops, something went wrong.