Skip to content

Commit

Permalink
Support disassembly of generic methods
Browse files Browse the repository at this point in the history
  • Loading branch information
KubaZ2 committed Aug 12, 2024
1 parent 66bfade commit a660fab
Show file tree
Hide file tree
Showing 13 changed files with 141 additions and 39 deletions.
1 change: 1 addition & 0 deletions Backend/Sandbox/Asm/Asm.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

<ItemGroup>
<ProjectReference Include="..\Shared\Shared.csproj" />
<ProjectReference Include="..\..\..\Sharp.Runtime\Sharp.Runtime.csproj" />
</ItemGroup>

<PropertyGroup>
Expand Down
20 changes: 12 additions & 8 deletions Backend/Sandbox/Asm/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,20 @@ ARG MINUS_PREFIXED_PLATFORM_IF_NOT_AMD64
FROM mcr.microsoft.com/dotnet/sdk:$DOTNET_SDK_VERSION AS build-env
WORKDIR /src

COPY Sandbox/Shared/Shared.csproj Sandbox/Shared/Shared.csproj
RUN dotnet restore Sandbox/Shared/Shared.csproj
COPY Sharp.Runtime/Sharp.Runtime.csproj Sharp.Runtime/Sharp.Runtime.csproj
RUN dotnet restore Sharp.Runtime/Sharp.Runtime.csproj

COPY Sandbox/Asm/Asm.csproj Sandbox/Asm/Asm.csproj
RUN dotnet restore Sandbox/Asm/Asm.csproj
COPY Backend/Sandbox/Shared/Shared.csproj Backend/Sandbox/Shared/Shared.csproj
RUN dotnet restore Backend/Sandbox/Shared/Shared.csproj

COPY Sandbox/Shared Sandbox/Shared
COPY Sandbox/Asm Sandbox/Asm
COPY Backend/Sandbox/Asm/Asm.csproj Backend/Sandbox/Asm/Asm.csproj
RUN dotnet restore Backend/Sandbox/Asm/Asm.csproj

RUN dotnet publish Sandbox/Asm/Asm.csproj -c Release -o /app
COPY Sharp.Runtime Sharp.Runtime
COPY Backend/Sandbox/Shared Backend/Sandbox/Shared
COPY Backend/Sandbox/Asm Backend/Sandbox/Asm

RUN dotnet publish Backend/Sandbox/Asm/Asm.csproj -c Release -o /app

FROM mcr.microsoft.com/dotnet-buildtools/prereqs:$OS-$OS_VERSION$MINUS_PREFIXED_PLATFORM_IF_NOT_AMD64 AS runtime-build-env
WORKDIR /runtime
Expand All @@ -42,7 +46,7 @@ FROM pwn.red/jail

COPY --from=runtime / /srv

COPY Sandbox/Asm/start.sh /srv/app/run
COPY Backend/Sandbox/Asm/start.sh /srv/app/run

ENV JAIL_TIME=10
ENV JAIL_PIDS=100
Expand Down
76 changes: 67 additions & 9 deletions Backend/Sandbox/Asm/Program.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,76 @@
using System.Reflection;
using System.Collections.Immutable;
using System.Reflection;
using System.Runtime.CompilerServices;

using Sharp;
using Sharp.Backend.Sandbox.Shared;

var assembly = Utils.LoadAssembly();

const BindingFlags Flags = BindingFlags.DeclaredOnly
| BindingFlags.Instance
| BindingFlags.Static
| BindingFlags.Public
| BindingFlags.NonPublic;

foreach (var type in assembly.DefinedTypes)
{
const BindingFlags Flags = BindingFlags.DeclaredOnly
| BindingFlags.Instance
| BindingFlags.Static
| BindingFlags.Public
| BindingFlags.NonPublic;

foreach (var method in type.GetMembers(Flags).OfType<MethodBase>())
RuntimeHelpers.PrepareMethod(method.MethodHandle);
if (type.IsNested)
continue;

PrepareType(type, []);
}

static void PrepareType(Type type, ImmutableList<RuntimeTypeHandle> baseGenericTypes)
{
if (type.IsGenericType)
{
foreach (var jitGenericAttribute in type.GetCustomAttributes<JitGenericAttribute>())
PrepareTypeMembers(type, baseGenericTypes.AddRange(jitGenericAttribute.Types.Select(t => t.TypeHandle)));
}
else
PrepareTypeMembers(type, baseGenericTypes);
}

static void PrepareMethod(MethodBase method, ImmutableList<RuntimeTypeHandle> genericTypes)
{
if (method.IsGenericMethod)
{
foreach (var jitGenericAttribute in method.GetCustomAttributes<JitGenericAttribute>())
{
try
{
RuntimeHelpers.PrepareMethod(method.MethodHandle, [.. genericTypes, .. jitGenericAttribute.Types.Select(t => t.TypeHandle)]);
}
catch
{
}
}
}
else
{
try
{
RuntimeHelpers.PrepareMethod(method.MethodHandle, [.. genericTypes]);
}
catch
{
}
}
}

static void PrepareTypeMembers(Type type, ImmutableList<RuntimeTypeHandle> genericTypes)
{
foreach (var member in type.GetMembers(Flags))
{
if (member is MethodBase method)
{
if (method.IsAbstract)
continue;

PrepareMethod(method, genericTypes);
}
else if (member is Type nestedType)
PrepareType(nestedType, genericTypes);
}
}
22 changes: 12 additions & 10 deletions Backend/Sandbox/Runner/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,20 @@ ARG DOTNET_RUNTIME_VERSION=8.0.7
FROM mcr.microsoft.com/dotnet/sdk:$DOTNET_SDK_VERSION AS build-env
WORKDIR /src

COPY Sandbox/Shared/Shared.csproj Sandbox/Shared/Shared.csproj
RUN dotnet restore Sandbox/Shared/Shared.csproj
COPY Sharp.Runtime/Sharp.Runtime.csproj Sharp.Runtime/Sharp.Runtime.csproj
RUN dotnet restore Sharp.Runtime/Sharp.Runtime.csproj

COPY Sandbox/Runner/Runner.csproj Sandbox/Runner/Runner.csproj
RUN dotnet restore Sandbox/Runner/Runner.csproj
COPY Backend/Sandbox/Shared/Shared.csproj Backend/Sandbox/Shared/Shared.csproj
RUN dotnet restore Backend/Sandbox/Shared/Shared.csproj

COPY Sandbox/Shared Sandbox/Shared
COPY Sandbox/Runner Sandbox/Runner
COPY Backend/Sandbox/Runner/Runner.csproj Backend/Sandbox/Runner/Runner.csproj
RUN dotnet restore Backend/Sandbox/Runner/Runner.csproj

RUN dotnet publish Sandbox/Runner/Runner.csproj -c Release -o /app
COPY Sharp.Runtime Sharp.Runtime
COPY Backend/Sandbox/Shared Backend/Sandbox/Shared
COPY Backend/Sandbox/Runner Backend/Sandbox/Runner

RUN dotnet publish Backend/Sandbox/Runner/Runner.csproj -c Release -o /app

FROM mcr.microsoft.com/dotnet/runtime:$DOTNET_RUNTIME_VERSION AS runtime

Expand All @@ -23,9 +27,7 @@ FROM pwn.red/jail

COPY --from=runtime / /srv

# RUN ln -s /app/Sharp.Backend.Sandbox.Runner /srv/app/run

COPY Sandbox/Runner/start.sh /srv/app/run
COPY Backend/Sandbox/Runner/start.sh /srv/app/run

ENV JAIL_TIME=10
ENV JAIL_PIDS=100
Expand Down
1 change: 1 addition & 0 deletions Backend/Sandbox/Runner/Runner.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

<ItemGroup>
<ProjectReference Include="..\Shared\Shared.csproj" />
<ProjectReference Include="..\..\..\Sharp.Runtime\Sharp.Runtime.csproj" />
</ItemGroup>

<PropertyGroup>
Expand Down
6 changes: 4 additions & 2 deletions Backend/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,17 @@ services:

runner:
build:
dockerfile: Sandbox/Runner/Dockerfile
context: ..
dockerfile: Backend/Sandbox/Runner/Dockerfile
networks:
- runner
privileged: true
restart: always

asm:
build:
dockerfile: Sandbox/Asm/Dockerfile
context: ..
dockerfile: Backend/Sandbox/Asm/Dockerfile
networks:
- asm
privileged: true
Expand Down
5 changes: 3 additions & 2 deletions Bot/Sharp/Compilation/CSharpCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,16 @@ private static CSharpCompilationOptions GetOptions(SyntaxTree syntaxTree, Compil
return syntaxTree.GetRoot().ChildNodes().Any(node => node.IsKind(SyntaxKind.GlobalStatement)) ? _executableOptions : _libraryOptions;
}

private static readonly MetadataReference[] _references = [.. Net80.References.All];
private static readonly MetadataReference[] _references = [.. Net80.References.All, MetadataReference.CreateFromFile(typeof(JitGenericAttribute).Assembly.Location)];

private static readonly SyntaxTree _globalUsingsSyntaxTree = CreateGlobalUsingsSyntaxTree("System",
"System.Collections.Generic",
"System.IO",
"System.Linq",
"System.Net.Http",
"System.Threading",
"System.Threading.Tasks");
"System.Threading.Tasks",
"Sharp");

private static SyntaxTree CreateGlobalUsingsSyntaxTree(params string[] namespaces)
{
Expand Down
18 changes: 11 additions & 7 deletions Bot/Sharp/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build-env
WORKDIR /src

COPY Mobius.ILasm/Mobius.ILASM/Mobius.ILasm.csproj Mobius.ILasm/Mobius.ILASM/Mobius.ILasm.csproj
RUN dotnet restore Mobius.ILasm/Mobius.ILASM/Mobius.ILasm.csproj
COPY Sharp.Runtime/Sharp.Runtime.csproj Sharp.Runtime/Sharp.Runtime.csproj
RUN dotnet restore Sharp.Runtime/Sharp.Runtime.csproj

COPY Sharp/Sharp.csproj Sharp/Sharp.csproj
RUN dotnet restore Sharp/Sharp.csproj
COPY Bot/Mobius.ILasm/Mobius.ILASM/Mobius.ILasm.csproj Bot/Mobius.ILasm/Mobius.ILASM/Mobius.ILasm.csproj
RUN dotnet restore Bot/Mobius.ILasm/Mobius.ILASM/Mobius.ILasm.csproj

COPY Mobius.ILasm/Mobius.ILASM Mobius.ILasm/Mobius.ILASM
COPY Sharp Sharp
COPY Bot/Sharp/Sharp.csproj Bot/Sharp/Sharp.csproj
RUN dotnet restore Bot/Sharp/Sharp.csproj

RUN dotnet publish Sharp/Sharp.csproj -c Release -o /app
COPY Sharp.Runtime Sharp.Runtime
COPY Bot/Mobius.ILasm/Mobius.ILASM Bot/Mobius.ILasm/Mobius.ILASM
COPY Bot/Sharp Bot/Sharp

RUN dotnet publish Bot/Sharp/Sharp.csproj -c Release -o /app

FROM mcr.microsoft.com/dotnet/runtime:8.0
WORKDIR /app
Expand Down
1 change: 1 addition & 0 deletions Bot/Sharp/Sharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

<ItemGroup>
<ProjectReference Include="..\Mobius.ILasm\Mobius.ILASM\Mobius.ILasm.csproj" />
<ProjectReference Include="..\..\Sharp.Runtime\Sharp.Runtime.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
3 changes: 2 additions & 1 deletion Bot/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
services:
bot:
build:
dockerfile: Sharp/Dockerfile
context: ..
dockerfile: Bot/Sharp/Dockerfile
restart: always
7 changes: 7 additions & 0 deletions Sharp.Runtime/JitGenericAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Sharp;

[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Interface)]
public sealed class JitGenericAttribute(params Type[] types) : Attribute
{
public IReadOnlyList<Type> Types { get; } = types;
}
10 changes: 10 additions & 0 deletions Sharp.Runtime/Sharp.Runtime.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RootNamespace>Sharp</RootNamespace>
</PropertyGroup>

</Project>
10 changes: 10 additions & 0 deletions Sharp.sln
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sharp", "Bot\Sharp\Sharp.cs
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Mobius.ILasm", "Bot\Mobius.ILasm\Mobius.ILASM\Mobius.ILasm.csproj", "{65B5DBEB-7DCD-440D-86C9-477BC5D9C4BD}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sharp.Runtime", "Sharp.Runtime\Sharp.Runtime.csproj", "{AF8C0407-4D7C-4DAE-9155-D4A784A621BA}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -88,6 +90,14 @@ Global
{65B5DBEB-7DCD-440D-86C9-477BC5D9C4BD}.Release|Any CPU.Build.0 = Release|Any CPU
{65B5DBEB-7DCD-440D-86C9-477BC5D9C4BD}.Release|x86.ActiveCfg = Release|Any CPU
{65B5DBEB-7DCD-440D-86C9-477BC5D9C4BD}.Release|x86.Build.0 = Release|Any CPU
{AF8C0407-4D7C-4DAE-9155-D4A784A621BA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AF8C0407-4D7C-4DAE-9155-D4A784A621BA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AF8C0407-4D7C-4DAE-9155-D4A784A621BA}.Debug|x86.ActiveCfg = Debug|Any CPU
{AF8C0407-4D7C-4DAE-9155-D4A784A621BA}.Debug|x86.Build.0 = Debug|Any CPU
{AF8C0407-4D7C-4DAE-9155-D4A784A621BA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AF8C0407-4D7C-4DAE-9155-D4A784A621BA}.Release|Any CPU.Build.0 = Release|Any CPU
{AF8C0407-4D7C-4DAE-9155-D4A784A621BA}.Release|x86.ActiveCfg = Release|Any CPU
{AF8C0407-4D7C-4DAE-9155-D4A784A621BA}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down

0 comments on commit a660fab

Please sign in to comment.