Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,7 @@ source on disk. When a run skips a method it had patched before, `Warnings` name
| Method or getter on a `partial` type whose body uses an `internal` member of a type in another assembly (through `InternalsVisibleTo`) | Reported as a name no source file of the `partial` type declares; hot reload does not patch this use from a `partial` type yet. Run `uloop compile` |
| Method on a struct (value type) | Value-type patching is out of scope |
| Generic method, or method on a generic type | Harmony cannot safely patch open generics |
| Method or accessor that needs an unsafe context: a pointer or function pointer in its signature or body, a `fixed` statement, `sizeof` of a type that is not built in, or an `unsafe` block or local function | Patches are compiled without unsafe code; the rest of the file still applies. Run `uloop compile` for this member |
| Explicit interface method or property accessor the compiled assembly does not hold yet, or an accessor of an explicit interface indexer | Only the bodies of explicit interface methods and property getters and setters the compiled type already implements are patched. Run `uloop compile` |
| No body (`abstract` / `extern`) | Nothing to transplant |
| Body uses `base.` for something other than a direct call to a base method (a base property, indexer, event, or method group) | C# cannot express that outside the type; run `uloop compile` |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,7 @@ source on disk. When a run skips a method it had patched before, `Warnings` name
| Method or getter on a `partial` type whose body uses an `internal` member of a type in another assembly (through `InternalsVisibleTo`) | Reported as a name no source file of the `partial` type declares; hot reload does not patch this use from a `partial` type yet. Run `uloop compile` |
| Method on a struct (value type) | Value-type patching is out of scope |
| Generic method, or method on a generic type | Harmony cannot safely patch open generics |
| Method or accessor that needs an unsafe context: a pointer or function pointer in its signature or body, a `fixed` statement, `sizeof` of a type that is not built in, or an `unsafe` block or local function | Patches are compiled without unsafe code; the rest of the file still applies. Run `uloop compile` for this member |
| Explicit interface method or property accessor the compiled assembly does not hold yet, or an accessor of an explicit interface indexer | Only the bodies of explicit interface methods and property getters and setters the compiled type already implements are patched. Run `uloop compile` |
| No body (`abstract` / `extern`) | Nothing to transplant |
| Body uses `base.` for something other than a direct call to a base method (a base property, indexer, event, or method group) | C# cannot express that outside the type; run `uloop compile` |
Expand Down
186 changes: 186 additions & 0 deletions Assets/Tests/Editor/HotReload/HotReloadUnsafeCodeE2ETests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

using NUnit.Framework;

using UnityEngine;

using io.github.hatayama.UnityCliLoop.FirstPartyTools;
using io.github.hatayama.UnityCliLoop.ToolContracts;

namespace io.github.hatayama.UnityCliLoop.Tests.Editor.HotReload
{
/// <summary>
/// End-to-end EditMode coverage for edited members that need an unsafe context: such a member is
/// Skipped with its own row while a safe method edited in the same file is still patched, and a
/// safe method inside an unsafe class is patched. The fixture lives in an assembly that allows
/// unsafe code, which this test assembly does not, so it is reached by reflection.
/// </summary>
public class HotReloadUnsafeCodeE2ETests
{
private const string FixtureAssemblyName = "UnityCLILoop.Tests.Editor.HotReloadUnsafeFixture";
private const string FixtureNamespace = "io.github.hatayama.UnityCliLoop.Tests.Editor.HotReloadUnsafeFixture.";
private const string UnsafeCodeReason = "need an unsafe context";

private HotReloadDomainTestScope _scope;

[SetUp]
public void SetUp()
{
_scope = new HotReloadDomainTestScope();
HotReloadAutoRefreshHold.SyncToActiveChanges();
}

[TearDown]
public void TearDown()
{
HotReloadCompositionRoot.Services.Patcher.RevertAll();
_scope.Dispose();
HotReloadAutoRefreshHold.SyncToActiveChanges();
VibeLogger.ClearMemoryLogs();
}

/// <summary>
/// What: a pointer body in an unsafe class is Skipped with the unsafe-code reason and keeps its
/// compiled body, while the safe method edited in the same file is patched.
/// </summary>
[Test]
public async Task Run_PointerBodyInUnsafeClass_IsSkippedAndTheSafeMethodIsPatched()
{
Assert.That(Read("UnsafeFixtureClassPointerHost"), Is.EqualTo(1303), "Precondition: compiled body.");

HotReloadOrchestratorResult result = await RunEditedAsync("UnsafeClassPointerE2E.cs", "*p + 1300", "*p + 1310");

AssertSkippedAsUnsafe(result, "UnsafeFixtureClassPointerHost.Read(");
AssertSafeMethodPatched(result);
Assert.That(Read("UnsafeFixtureClassPointerHost"), Is.EqualTo(1303), "The skipped member must keep its compiled body.");
}

/// <summary>
/// What: an edited getter of a pointer-typed property gets its own Skipped row (not a
/// shim-compile failure), while the safe method edited in the same file is patched.
/// </summary>
[Test]
public async Task Run_PointerTypedPropertyGetter_IsSkippedAndTheSafeMethodIsPatched()
{
HotReloadOrchestratorResult result = await RunEditedAsync("PointerTypedPropertyE2E.cs", "(IntPtr)1650", "(IntPtr)1660");

AssertSkippedAsUnsafe(result, "UnsafeFixturePointerTypedPropertyHost.get_Pointer(");
AssertSafeMethodPatched(result);
}

/// <summary>
/// What: a safe method inside an unsafe class is patched, returns the edited value, and returns
/// the compiled value after RevertAll.
/// </summary>
[Test]
public async Task Run_SafeMethodInUnsafeClass_IsPatched()
{
Assert.That(Read("UnsafeFixtureClassSafeHost"), Is.EqualTo(1350), "Precondition: compiled body.");

HotReloadOrchestratorResult result = await RunEditedAsync("UnsafeClassSafeE2E.cs", "return 1350;", "return 1360;");

AssertPatched(result, "UnsafeFixtureClassSafeHost.Read(");
AssertSafeMethodPatched(result);
Assert.That(Read("UnsafeFixtureClassSafeHost"), Is.EqualTo(1360), "The patched body must run.");

HotReloadCompositionRoot.Services.Patcher.RevertAll();
Assert.That(Read("UnsafeFixtureClassSafeHost"), Is.EqualTo(1350), "RevertAll must restore the compiled body.");
}

// Edits the anchor and the safe method together, and applies the edit as a content override so
// the fixture file on disk is never written.
private static async Task<HotReloadOrchestratorResult> RunEditedAsync(
string editedFileName,
string anchor,
string replacement)
{
string fixturePath = Path.GetFullPath(
Path.Combine(Application.dataPath, "Tests", "Editor", "HotReloadUnsafeFixture", "HotReloadUnsafeFixture.cs"));
string onDisk = File.ReadAllText(fixturePath);
string edited = ReplaceUnique(ReplaceUnique(onDisk, anchor, replacement), "return 1000;", "return 1001;");
HotReloadOrchestratorResult result = await HotReloadCompositionRoot.Services.Orchestrator.RunAsync(
new[] { fixturePath },
contentPathOverride: null,
CancellationToken.None,
new Dictionary<string, string>
{
[fixturePath] = HotReloadTestSourceWriter.WriteEditedSource(editedFileName, edited)
});
Assert.That(File.ReadAllText(fixturePath), Is.EqualTo(onDisk), "The fixture file must stay untouched.");
return result;
}

private static string ReplaceUnique(string source, string anchor, string replacement)
{
Assert.That(
source.IndexOf(anchor, StringComparison.Ordinal),
Is.EqualTo(source.LastIndexOf(anchor, StringComparison.Ordinal)).And.GreaterThanOrEqualTo(0),
"Precondition: the fixture must hold the anchor exactly once: " + anchor);
return source.Replace(anchor, replacement, StringComparison.Ordinal);
}

private static int Read(string typeName)
{
Type type = Assembly.Load(FixtureAssemblyName).GetType(FixtureNamespace + typeName, true);
object instance = Activator.CreateInstance(type);
MethodInfo read = type.GetMethod("Read", BindingFlags.Public | BindingFlags.Instance);
return (int)read.Invoke(instance, null);
}

private static void AssertSafeMethodPatched(HotReloadOrchestratorResult result)
{
AssertPatched(result, "UnsafeFixtureSafeHost.Read(");
Assert.That(Read("UnsafeFixtureSafeHost"), Is.EqualTo(1001), "The safe method in the same file must be patched.");
}

private static void AssertPatched(HotReloadOrchestratorResult result, string methodFragment)
{
HotReloadMethodOutcome outcome = FindSingle(result, methodFragment);
Assert.That(outcome.Kind, Is.EqualTo(HotReloadMethodOutcomeKind.Patched), FormatOutcomes(result));
}

private static void AssertSkippedAsUnsafe(HotReloadOrchestratorResult result, string methodFragment)
{
HotReloadMethodOutcome outcome = FindSingle(result, methodFragment);
Assert.That(outcome.Kind, Is.EqualTo(HotReloadMethodOutcomeKind.Skipped), FormatOutcomes(result));
Assert.That(outcome.Reason, Does.Contain(UnsafeCodeReason), FormatOutcomes(result));
foreach (HotReloadMethodOutcome other in result.Methods)
{
Assert.That(other.Kind, Is.Not.EqualTo(HotReloadMethodOutcomeKind.Failed), FormatOutcomes(result));
}
}

private static HotReloadMethodOutcome FindSingle(HotReloadOrchestratorResult result, string methodFragment)
{
List<HotReloadMethodOutcome> found = new List<HotReloadMethodOutcome>();
foreach (HotReloadMethodOutcome outcome in result.Methods)
{
if (outcome.Method.Contains(methodFragment))
{
found.Add(outcome);
}
}

Assert.That(found.Count, Is.EqualTo(1), "Expected one row for " + methodFragment + ".\n" + FormatOutcomes(result));
return found[0];
}

private static string FormatOutcomes(HotReloadOrchestratorResult result)
{
StringBuilder builder = new StringBuilder();
foreach (HotReloadMethodOutcome outcome in result.Methods)
{
builder.Append(outcome.Kind).Append(' ').Append(outcome.Method)
.Append(" :: ").Append(outcome.Reason).Append('\n');
}

return builder.ToString();
}
}
}
11 changes: 11 additions & 0 deletions Assets/Tests/Editor/HotReload/HotReloadUnsafeCodeE2ETests.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,12 @@ private static IEnumerable<TestCaseData> RenderCases()
NoArgs,
"Generic methods and methods inside generic types cannot be safely patched with Harmony. "
+ "Run 'uloop compile'.");
yield return Case(
HotReloadWorkerReasonCode.MethodTransformUnsafeCode,
NoArgs,
"Methods and accessors that need an unsafe context (pointers, function pointers, fixed "
+ "statements, or unsafe blocks) are skipped; the patch is compiled without unsafe code. "
+ "Run 'uloop compile'.");
yield return Case(
HotReloadWorkerReasonCode.MethodTransformExplicitInterfaceImplementation,
NoArgs,
Expand Down
8 changes: 4 additions & 4 deletions Assets/Tests/Editor/HotReload/TransformWorkerBaseCallTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -257,11 +257,11 @@ await AssertEditIsSkippedWithCode(
}

/// <summary>
/// What: a base call to a method taking an array of pointers is Skipped for its argument shape,
/// because the stand-in cannot declare that parameter in a shim compiled without unsafe code.
/// What: a base call to a method taking an array of pointers is Skipped as unsafe code, because
/// the call itself needs an unsafe context and the patch is compiled without unsafe code.
/// </summary>
[Test]
public async Task Run_BaseCallToPointerArrayParameter_IsSkippedAsArgumentShape()
public async Task Run_BaseCallToPointerArrayParameter_IsSkippedAsUnsafeCode()
{
TransformWorkerClientResult result = await RunWorkerOnEditedFixtureAsync(
"BaseCallToPointerArrayParameter.cs",
Expand All @@ -277,7 +277,7 @@ public async Task Run_BaseCallToPointerArrayParameter_IsSkippedAsArgumentShape()
AssertSkippedWithCode(
result,
nameof(HotReloadBaseCallDerivedFixture.CallKeywordParameter),
HotReloadWorkerReasonCode.MethodTransformBaseCallArgumentShape);
HotReloadWorkerReasonCode.MethodTransformUnsafeCode);
}

/// <summary>
Expand Down
11 changes: 7 additions & 4 deletions Assets/Tests/Editor/HotReload/TransformWorkerClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4325,7 +4325,9 @@ internal static TransformWorkerInputDto BuildE2EFixtureInput()
return BuildInputForSource(ResolveE2EFixturePath(), ResolveE2EFixtureProjectRelativePath());
}

// Shared with worker tests in other classes that run one edited source against this assembly.
// Shared with worker tests in other classes that run one edited source against this assembly,
// or against another test assembly named by targetAssemblyName (for example one that allows
// unsafe code, which this assembly does not).
internal static TransformWorkerInputDto BuildInputForSource(
string sourcePath,
string projectRelativePath,
Expand All @@ -4334,20 +4336,21 @@ internal static TransformWorkerInputDto BuildInputForSource(
string[] changedSiblingSourcePaths = null,
string secondSourcePath = null,
string secondProjectRelativePath = null,
string operation = null)
string operation = null,
string targetAssemblyName = TestAssemblyName)
{
string projectRoot = Path.GetFullPath(Path.Combine(Application.dataPath, ".."));
string targetDllPath = Path.Combine(
projectRoot,
"Library",
"ScriptAssemblies",
TestAssemblyName + ".dll");
targetAssemblyName + ".dll");
Assert.That(File.Exists(targetDllPath), Is.True, "Test assembly dll missing: " + targetDllPath);

UnityEditor.Compilation.Assembly compilationAssembly = null;
foreach (UnityEditor.Compilation.Assembly assembly in CompilationPipeline.GetAssemblies())
{
if (assembly.name == TestAssemblyName)
if (assembly.name == targetAssemblyName)
{
compilationAssembly = assembly;
break;
Expand Down
Loading
Loading