Skip to content

Commit

Permalink
[gh-1307] Fix remaining JIT ASM issues
Browse files Browse the repository at this point in the history
  • Loading branch information
ashmind committed Dec 3, 2023
1 parent a1f77dc commit afa0e2c
Show file tree
Hide file tree
Showing 20 changed files with 399 additions and 750 deletions.
8 changes: 7 additions & 1 deletion source/Server/Common/Diagnostics/DiagnosticLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,21 @@
namespace SharpLab.Server.Common.Diagnostics {
public static class DiagnosticLog {
private static readonly AsyncLocal<Func<string, string>> _getPathByStepName = new();
private static readonly AsyncLocal<Action<string>> _logMessage = new();

public static void Enable(Func<string, string> getPathByStepName) {
public static void Enable(Action<string> logMessage, Func<string, string> getPathByStepName) {
_logMessage.Value = logMessage;
_getPathByStepName.Value = getPathByStepName;
}

public static bool IsEnabled() {
return _getPathByStepName.Value != null;
}

public static void LogMessage(string message) {
_logMessage.Value?.Invoke(message);
}

public static void LogAssembly(string stepName, ModuleDefinition module) {
var path = GetLogPathWithoutExtension(stepName);
if (path == null)
Expand Down
14 changes: 12 additions & 2 deletions source/Server/Decompilation/JitAsmDecompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using SharpLab.Runtime;
using SharpLab.Runtime.Internal;
using SharpLab.Server.Common;
using SharpLab.Server.Common.Diagnostics;
using SharpLab.Server.Decompilation.Internal;

namespace SharpLab.Server.Decompilation {
Expand Down Expand Up @@ -165,6 +166,10 @@ private bool TryDisassembleAndWriteMembersOfGeneric(JitWriteContext context, Typ
}

private void DisassembleAndWriteMethod(JitWriteContext context, MethodBase method) {
#if DEBUG
DiagnosticLog.LogMessage($"[JitAsm] Processing method {method.Name}");
#endif

if ((method.MethodImplementationFlags & MethodImplAttributes.Runtime) == MethodImplAttributes.Runtime) {
WriteSignatureFromReflection(context, method);
context.Writer.WriteLine(" ; Cannot produce JIT assembly for runtime-implemented method.");
Expand Down Expand Up @@ -259,12 +264,17 @@ private void DisassembleAndWriteSimpleMethod(JitWriteContext context, MethodBase
}

private ClrMethodData? FindJitCompiledMethod(JitWriteContext context, RuntimeMethodHandle handle) {
context.Runtime.FlushCachedData();
lock (context.Runtime.DacLibrary.DacPrivateInterface)
context.Runtime.DacLibrary.DacPrivateInterface.Flush();
var sos = context.Runtime.DacLibrary.SOSDacInterface;

var methodDescAddress = unchecked((ulong)handle.Value.ToInt64());
if (!sos.GetMethodDescData(methodDescAddress, 0, out var methodDesc))
if (sos.GetMethodDescData(methodDescAddress, 0, out var methodDesc) is var hresult && !hresult) {
#if DEBUG
DiagnosticLog.LogMessage($"[JitAsm] Failed to get GetMethodDescData(0x{methodDescAddress:X}): {hresult}");
#endif
return null;
}

return GetJitCompiledMethodByMethodDescIfValid(sos, methodDesc)
?? FindJitCompiledMethodInMethodTable(sos, methodDesc);
Expand Down
1 change: 0 additions & 1 deletion source/Tests/Decompilation/TargetJitAsmTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Runtime.Intrinsics.X86;
using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;
Expand Down
100 changes: 50 additions & 50 deletions source/Tests/Decompilation/TestCode/FSharp/SimpleUnion.fs
Original file line number Diff line number Diff line change
Expand Up @@ -27,54 +27,54 @@ _+T.__DebugDisplay()
L0004: push rbp
L0005: push rbx
L0006: sub rsp, 0x20
L000a: mov rsi, rcx
L000a: mov rbx, rcx
L000d: mov rcx, 0x<IGNORE>
L0017: call 0x<IGNORE>
L001c: mov rdi, rax
L001f: mov rdx, 0x<IGNORE>
L0029: mov rdx, [rdx]
L002c: lea rcx, [rdi+8]
L001c: mov rsi, rax
L001f: mov rcx, 0x<IGNORE>
L0029: mov rdx, [rcx]
L002c: lea rcx, [rsi+8]
L0030: call 0x<IGNORE>
L0035: xor edx, edx
L0037: mov [rdi+0x10], rdx
L003b: mov [rdi+0x18], rdx
L003f: mov rdx, rdi
L0037: mov [rsi+0x10], rdx
L003b: mov [rsi+0x18], rdx
L003f: mov rdx, rsi
L0042: mov rcx, 0x<IGNORE>
L004c: call qword ptr [0x<IGNORE>]
L0052: mov rbx, rax
L0055: mov rbp, [rdi+0x10]
L0052: mov rdi, rax
L0055: mov rbp, [rsi+0x10]
L0059: test rbp, rbp
L005c: jne short L006b
L005e: mov rcx, rbx
L005e: mov rcx, rdi
L0061: cmp [rcx], ecx
L0063: call qword ptr [0x<IGNORE>]
L0069: jmp short L00aa
L006b: mov rcx, rbx
L006b: mov rcx, rdi
L006e: cmp [rcx], ecx
L0070: call qword ptr [0x<IGNORE>]
L0076: mov r14, rax
L0079: mov ecx, [rbx+0x28]
L0079: mov ecx, [rdi+0x28]
L007c: call qword ptr [0x<IGNORE>]
L0082: mov rcx, rax
L0085: mov r8, [rdi+0x18]
L0085: mov r8, [rsi+0x18]
L0089: mov rdx, rbp
L008c: mov r9, r14
L008f: cmp [rcx], ecx
L0091: call qword ptr [0x<IGNORE>]
L0097: mov rdx, rax
L009a: mov rcx, 0x<IGNORE>
L00a4: call qword ptr [0x<IGNORE>]
L00aa: movsx rdx, byte ptr [rsi]
L00ae: mov rcx, rax
L00b1: mov rax, [rax]
L00b4: mov rax, [rax+0x40]
L00b8: add rsp, 0x20
L00bc: pop rbx
L00bd: pop rbp
L00be: pop rsi
L00bf: pop rdi
L00c0: pop r14
L00c2: jmp qword ptr [rax+0x20]
L00aa: movzx edx, byte ptr [rbx]
L00ad: mov rcx, rax
L00b0: mov rax, [rax]
L00b3: mov rax, [rax+0x40]
L00b7: add rsp, 0x20
L00bb: pop rbx
L00bc: pop rbp
L00bd: pop rsi
L00be: pop rdi
L00bf: pop r14
L00c1: jmp qword ptr [rax+0x20]
_+T.ToString()
L0000: push r14
Expand All @@ -83,54 +83,54 @@ _+T.ToString()
L0004: push rbp
L0005: push rbx
L0006: sub rsp, 0x20
L000a: mov rsi, rcx
L000a: mov rbx, rcx
L000d: mov rcx, 0x<IGNORE>
L0017: call 0x<IGNORE>
L001c: mov rdi, rax
L001f: mov rdx, 0x<IGNORE>
L0029: mov rdx, [rdx]
L002c: lea rcx, [rdi+8]
L001c: mov rsi, rax
L001f: mov rcx, 0x<IGNORE>
L0029: mov rdx, [rcx]
L002c: lea rcx, [rsi+8]
L0030: call 0x<IGNORE>
L0035: xor edx, edx
L0037: mov [rdi+0x10], rdx
L003b: mov [rdi+0x18], rdx
L003f: mov rdx, rdi
L0037: mov [rsi+0x10], rdx
L003b: mov [rsi+0x18], rdx
L003f: mov rdx, rsi
L0042: mov rcx, 0x<IGNORE>
L004c: call qword ptr [0x<IGNORE>]
L0052: mov rbx, rax
L0055: mov rbp, [rdi+0x10]
L0052: mov rdi, rax
L0055: mov rbp, [rsi+0x10]
L0059: test rbp, rbp
L005c: jne short L006b
L005e: mov rcx, rbx
L005e: mov rcx, rdi
L0061: cmp [rcx], ecx
L0063: call qword ptr [0x<IGNORE>]
L0069: jmp short L00aa
L006b: mov rcx, rbx
L006b: mov rcx, rdi
L006e: cmp [rcx], ecx
L0070: call qword ptr [0x<IGNORE>]
L0076: mov r14, rax
L0079: mov ecx, [rbx+0x28]
L0079: mov ecx, [rdi+0x28]
L007c: call qword ptr [0x<IGNORE>]
L0082: mov rcx, rax
L0085: mov r8, [rdi+0x18]
L0085: mov r8, [rsi+0x18]
L0089: mov rdx, rbp
L008c: mov r9, r14
L008f: cmp [rcx], ecx
L0091: call qword ptr [0x<IGNORE>]
L0097: mov rdx, rax
L009a: mov rcx, 0x<IGNORE>
L00a4: call qword ptr [0x<IGNORE>]
L00aa: movsx rdx, byte ptr [rsi]
L00ae: mov rcx, rax
L00b1: mov rax, [rax]
L00b4: mov rax, [rax+0x40]
L00b8: add rsp, 0x20
L00bc: pop rbx
L00bd: pop rbp
L00be: pop rsi
L00bf: pop rdi
L00c0: pop r14
L00c2: jmp qword ptr [rax+0x20]
L00aa: movzx edx, byte ptr [rbx]
L00ad: mov rcx, rax
L00b0: mov rax, [rax]
L00b3: mov rax, [rax+0x40]
L00b7: add rsp, 0x20
L00bb: pop rbx
L00bc: pop rbp
L00bd: pop rsi
L00be: pop rdi
L00bf: pop r14
L00c1: jmp qword ptr [rax+0x20]
_+T.Equals(System.Object)
L0000: xor eax, eax
Expand Down
76 changes: 20 additions & 56 deletions source/Tests/Decompilation/TestCode/JitAsm/ArrayElement.cs
Original file line number Diff line number Diff line change
@@ -1,57 +1,21 @@
static class C {
static int M(int[] x) {
return x[0];
}
}

/* asm
; Core CLR <IGNORE> on x64
C.M(Int32[])
L0000: sub rsp, 0x28
L0004: cmp dword ptr [rcx+8], 0
L0008: jbe short L0012
L000a: mov eax, [rcx+0x10]
L000d: add rsp, 0x28
L0011: ret
L0012: call 0x<IGNORE>
L0017: int3
Microsoft.CodeAnalysis.EmbeddedAttribute..ctor()
L0000: ret
System.Runtime.CompilerServices.NullableAttribute..ctor(Byte)
L0000: push rdi
L0001: push rsi
L0002: sub rsp, 0x28
L0006: mov rsi, rcx
L0009: mov edi, edx
L000b: mov rcx, 0x<IGNORE>
L0015: mov edx, 1
L001a: call 0x<IGNORE>
L001f: mov [rax+0x10], dil
L0023: lea rcx, [rsi+8]
L0027: mov rdx, rax
L002a: call 0x<IGNORE>
L002f: nop
L0030: add rsp, 0x28
L0034: pop rsi
L0035: pop rdi
L0036: ret
System.Runtime.CompilerServices.NullableAttribute..ctor(Byte[])
L0000: lea rcx, [rcx+8]
L0004: call 0x<IGNORE>
L0009: nop
L000a: ret
System.Runtime.CompilerServices.NullableContextAttribute..ctor(Byte)
L0000: mov [rcx+8], dl
L0003: ret
System.Runtime.CompilerServices.RefSafetyRulesAttribute..ctor(Int32)
L0000: mov [rcx+8], edx
L0003: ret
static class C {
static int M(int[] x) {
return x[0];
}
}

/* asm
; Core CLR <IGNORE> on x64
C.M(Int32[])
L0000: sub rsp, 0x28
L0004: cmp dword ptr [rcx+8], 0
L0008: jbe short L0012
L000a: mov eax, [rcx+0x10]
L000d: add rsp, 0x28
L0011: ret
L0012: call 0x<IGNORE>
L0017: int3
*/
Loading

0 comments on commit afa0e2c

Please sign in to comment.