Skip to content

[WIP] Interpreter unsafe accessor tests #116110

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/coreclr/interpreter/compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1209,6 +1209,7 @@ InterpCompiler::InterpCompiler(COMP_HANDLE compHnd,

if (InterpConfig.InterpDump().contains(compHnd, m_methodHnd, m_classHnd, &m_methodInfo->args))
m_verbose = true;
// m_verbose = true;
#endif
}

Expand All @@ -1223,7 +1224,14 @@ InterpMethod* InterpCompiler::CompileMethod()

CreateILVars();

GenerateCode(m_methodInfo);
int res = GenerateCode(m_methodInfo);
if (res != CORJIT_OK)
{
printf("Interpreter method compilation failed for ");
PrintMethodName(m_methodHnd);
printf("\n");
return nullptr;
}

#ifdef DEBUG
if (m_verbose)
Expand Down
5 changes: 5 additions & 0 deletions src/coreclr/interpreter/eeinterp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ CorJitResult CILInterp::compileMethod(ICorJitInfo* compHnd,
InterpCompiler compiler(compHnd, methodInfo);
InterpMethod *pMethod = compiler.CompileMethod();

if (!pMethod)
{
return CORJIT_BADCODE;
}

int32_t IRCodeSize;
int32_t *pIRCode = compiler.GetCode(&IRCodeSize);

Expand Down
12 changes: 12 additions & 0 deletions src/coreclr/vm/precode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@ InterleavedLoaderHeapConfig s_stubPrecodeHeapConfig;
InterleavedLoaderHeapConfig s_fixupStubPrecodeHeapConfig;
#endif

void AssertThisIsNotInterpreterCode(PCODE code)
{
#ifdef FEATURE_INTERPRETER
#ifndef DACCESS_COMPILE
EECodeInfo codeInfo(code);
if (!codeInfo.IsValid())
return;
_ASSERTE(codeInfo.GetCodeManager() != ExecutionManager::GetInterpreterCodeManager());
#endif
#endif
}

//==========================================================================================
// class Precode
//==========================================================================================
Expand Down
6 changes: 6 additions & 0 deletions src/coreclr/vm/precode.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ struct StubPrecodeData
// match the Type field. This is a defense-in-depth measure (and only matters for access from the debugger)
};

void AssertThisIsNotInterpreterCode(PCODE code);

typedef DPTR(StubPrecodeData) PTR_StubPrecodeData;

#if !(defined(TARGET_ARM64) && defined(TARGET_UNIX))
Expand Down Expand Up @@ -208,6 +210,7 @@ struct StubPrecode
CONTRACTL_END;

StubPrecodeData *pData = GetData();
AssertThisIsNotInterpreterCode(target);
return InterlockedCompareExchangeT<PCODE>(&pData->Target, (PCODE)target, (PCODE)expected) == expected;
}

Expand All @@ -222,6 +225,7 @@ struct StubPrecode
CONTRACTL_END;

StubPrecodeData *pData = GetData();
AssertThisIsNotInterpreterCode(target);
pData->Target = (PCODE)target;
}

Expand Down Expand Up @@ -314,6 +318,7 @@ struct ThisPtrRetBufPrecode : StubPrecode
CONTRACTL_END;

ThisPtrRetBufPrecodeData *pData = GetData();
AssertThisIsNotInterpreterCode(target);
return InterlockedCompareExchangeT<PCODE>(&pData->Target, (PCODE)target, (PCODE)expected) == expected;
}

Expand Down Expand Up @@ -493,6 +498,7 @@ struct FixupPrecode
}

_ASSERTE(IS_ALIGNED(&GetData()->Target, sizeof(SIZE_T)));
AssertThisIsNotInterpreterCode(target);
return InterlockedCompareExchangeT<PCODE>(&GetData()->Target, (PCODE)target, (PCODE)oldTarget) == (PCODE)oldTarget;
}
#endif
Expand Down
83 changes: 79 additions & 4 deletions src/tests/JIT/interpreter/Interpreter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -845,10 +845,10 @@ public static void RunInterpreterTests()
Console.WriteLine("TestLdtoken");
if (!TestLdtoken())
Environment.FailFast(null);
/*
if (!TestMdArray())
Environment.FailFast(null);
*/

// if (!TestMdArray())
// Environment.FailFast(null);

Console.WriteLine("TestExceptionHandling");
TestExceptionHandling();

Expand All @@ -868,6 +868,10 @@ public static void RunInterpreterTests()
if (!TestCalli())
Environment.FailFast(null);

Console.WriteLine("TestUnsafeAccessors");
if (!TestUnsafeAccessors())
Environment.FailFast(null);

System.GC.Collect();

Console.WriteLine("All tests passed successfully!");
Expand Down Expand Up @@ -2359,4 +2363,75 @@ public unsafe static bool TestCalli()
{
return &Fill<T>;
}

class UnsafeAccessorTestClass
{
private int Field = 7;
private int Property { get; set; } = 32;
private int Method (int arg) => arg * 2;
private void VoidMethod () => Field *= 2;

public void DumpState()
{
Console.WriteLine("== DumpState ==");
Console.WriteLine(Field);
Console.WriteLine(Method(4));
Console.WriteLine(Property);
VoidMethod();
Console.WriteLine(Field);
// Restore value of Field
Field = 7;
}
}

public static bool TestUnsafeAccessors()
{
UnsafeAccessorTestClass tc = new ();
// Calling DumpState triggers eager interpreted compilation of UnsafeAccessorTestClass's methods.
// They work, but then the unsafe accessors somehow end up invoking the interpreter IR instead of the interpreter stub.
tc.DumpState();

Console.WriteLine("== Unsafe Accessors ==");
ref int f = ref GetField(tc);
Console.WriteLine(f);
if (f != 7)
return false;

// If DumpState is commented out, this invocation will trigger interpreted compilation of TestClass::Method,
// and then the unsafe accessor will invoke the InterpreterStub, but the return value gets trashed. For me, it is always 176.
// This appears to be because InterpreterStub never does anything with the return address after the interpreter execution writes it to sp.
int m = InvokeMethod(tc, 4);
Console.WriteLine(m);
if (m != 8)
return false;

int p = GetProperty(tc);
Console.WriteLine(p);
if (p != 32)
return false;

// VoidMethod doubles the value of Field
InvokeVoidMethod(tc);

ref int f2 = ref GetField(tc);
Console.WriteLine(f2);
if (f2 != 14)
return false;

return true;

[UnsafeAccessor(UnsafeAccessorKind.Field, Name="Field")]
// NOTE: This won't work for an 'int' return, only 'ref int'
static extern ref int GetField (UnsafeAccessorTestClass tc);

[UnsafeAccessor(UnsafeAccessorKind.Method, Name="get_Property")]
static extern int GetProperty (UnsafeAccessorTestClass tc);

[UnsafeAccessor(UnsafeAccessorKind.Method, Name="Method")]
static extern int InvokeMethod (UnsafeAccessorTestClass tc, int arg);

// We currently lack support for passing arguments into interpreter methods as well.
[UnsafeAccessor(UnsafeAccessorKind.Method, Name="VoidMethod")]
static extern void InvokeVoidMethod (UnsafeAccessorTestClass tc);
}
}
Loading