forked from SteveSandersonMS/DotNetIsolator
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b38ab1c
commit 805563e
Showing
16 changed files
with
319 additions
and
8 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 |
---|---|---|
|
@@ -3,3 +3,4 @@ obj/ | |
bin/ | ||
*.suo | ||
*.csproj.user | ||
artifacts/ |
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,10 @@ | ||
<Project> | ||
<PropertyGroup> | ||
<PackageOutputPath>$(MSBuildThisFileDirectory)../artifacts</PackageOutputPath> | ||
<VersionPrefix>0.1.0</VersionPrefix> | ||
<VersionSuffix>dev</VersionSuffix> | ||
<Authors>Steve Sanderson</Authors> | ||
<Company>Microsoft</Company> | ||
<PackageLicenseExpression>MIT</PackageLicenseExpression> | ||
</PropertyGroup> | ||
</Project> |
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,14 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="MessagePack" Version="2.5.103" /> | ||
</ItemGroup> | ||
|
||
</Project> |
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,38 @@ | ||
using DotNetIsolator.Guest; | ||
using MessagePack.Resolvers; | ||
using MessagePack; | ||
using System.Text; | ||
|
||
namespace DotNetIsolator; | ||
|
||
public static class DotNetIsolatorHost | ||
{ | ||
public static unsafe void Invoke(string callbackName, params object[] args) | ||
=> Invoke<object>(callbackName, args); | ||
|
||
public static unsafe T Invoke<T>(string callbackName, params object[] args) | ||
{ | ||
var argsSerialized = args.Select(a => a is null | ||
? null | ||
: MessagePackSerializer.Serialize(a.GetType(), a, ContractlessStandardResolverAllowPrivate.Options)) | ||
.ToArray(); | ||
|
||
var callInfo = new GuestToHostCall { CallbackName = callbackName, ArgsSerialized = argsSerialized }; | ||
var callInfoBytes = MessagePackSerializer.Serialize(callInfo, ContractlessStandardResolverAllowPrivate.Options); | ||
|
||
fixed (void* callInfoPtr = callInfoBytes) | ||
{ | ||
var success = Interop.CallHost(callInfoPtr, callInfoBytes.Length, out var resultPtr, out var resultLength); | ||
var result = (int)resultPtr == 0 ? null : new Span<byte>(resultPtr, resultLength); | ||
if (success) | ||
{ | ||
return (int)resultPtr == 0 ? default! : MessagePackSerializer.Deserialize<T>(result.ToArray(), ContractlessStandardResolverAllowPrivate.Options); | ||
} | ||
else | ||
{ | ||
var errorString = Encoding.UTF8.GetString(result); | ||
throw new InvalidOperationException($"Call to host failed: {errorString}"); | ||
} | ||
} | ||
} | ||
} |
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,9 @@ | ||
namespace DotNetIsolator; | ||
|
||
#pragma warning disable CS0649 | ||
internal struct GuestToHostCall | ||
{ | ||
public string CallbackName; | ||
public byte[]?[] ArgsSerialized; | ||
} | ||
#pragma warning restore CS0649 |
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,9 @@ | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace DotNetIsolator.Guest; | ||
|
||
internal static class Interop | ||
{ | ||
[MethodImpl(MethodImplOptions.InternalCall)] | ||
public static unsafe extern bool CallHost(void* invocationPtr, int invocationLength, out void* result, out int resultLength); | ||
} |
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,10 @@ | ||
#include <stdio.h> | ||
#include <mono-wasi/driver.h> | ||
|
||
__attribute__((import_module("dotnetisolator"))) | ||
__attribute__((import_name("call_host"))) | ||
int dotnetisolator_call_host(void* invocation, int invocation_length, void** result, int* result_length); | ||
|
||
void dotnetisolator_add_host_callback_internal_calls() { | ||
mono_add_internal_call("DotNetIsolator.Guest.Interop::CallHost", dotnetisolator_call_host); | ||
} |
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
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.