From 9505faa6e72a0f49abc7529f429d907b8133aa9d Mon Sep 17 00:00:00 2001 From: lulan <49630018+ReneeGA2020@users.noreply.github.com> Date: Thu, 12 Nov 2026 18:38:31 +0800 Subject: [PATCH 1/9] Add opt-in session-origin host surface on current main Port the archived session-origin host side onto origin/main (cbac2a657793dca7f22b1e1feff4aab26b898b4e): - PrototypeRequireOriginScan on MapRunOptions and HostContractSessionOptions, requiring RequireInterpreter plus an API policy - PrototypeReflectionPolicy.PrepareOriginScan binds the runtime capability before author loading and fails closed with PlatformNotSupportedException - session-origin test projects, verifier and the default-off matrix mode - integration notes in docs/validation/session-origin-integration-20261112.md and short pointers in development.md and ugc-session-policy.md Paired runtime branch: runeclr/session-origin-integration-20261112 in ReneeGA2020/runtime, commit 2077f1478f2b43533d368dd7521a04bf6c52809a. The runtime switch defaults to disabled, so a host that does not opt in keeps the existing reflection-policy behavior. Verified: host build, stock reflection tests, lifecycle 156/156, and the 16-mode matrix against a fresh Checked runtime build. Not merged into main. Assisted-by: Cindy (https://cindy.app) --- RuneCLR.slnx | 4 + docs/development.md | 10 + docs/ugc-session-policy.md | 8 + .../session-origin-integration-20261112.md | 73 +++ eng/test-session-origin-provenance.py | 191 +++++++ eng/verify-runtime-session-origin.py | 501 ++++++++++++++++++ src/RuneCLR.Runtime/HostContractSession.cs | 1 + .../HostContractSessionOptions.cs | 4 + src/RuneCLR.Runtime/MapRunOptions.cs | 5 + src/RuneCLR.Runtime/MapSession.cs | 1 + .../PrototypeReflectionPolicy.cs | 37 ++ tests/RuneCLR.SessionOrigin.Tests/Program.cs | 404 ++++++++++++++ .../RuneCLR.SessionOrigin.Tests.csproj | 17 + tests/SessionOrigin.Author/OriginScript.cs | 140 +++++ .../SessionOrigin.Author.csproj | 6 + tests/SessionOrigin.Chain/ChainStep.cs | 7 + .../SessionOrigin.Chain.csproj | 1 + tests/SessionOrigin.Compiled/OriginHooks.cs | 308 +++++++++++ .../SessionOrigin.Compiled.csproj | 1 + 19 files changed, 1719 insertions(+) create mode 100644 docs/validation/session-origin-integration-20261112.md create mode 100644 eng/test-session-origin-provenance.py create mode 100644 eng/verify-runtime-session-origin.py create mode 100644 tests/RuneCLR.SessionOrigin.Tests/Program.cs create mode 100644 tests/RuneCLR.SessionOrigin.Tests/RuneCLR.SessionOrigin.Tests.csproj create mode 100644 tests/SessionOrigin.Author/OriginScript.cs create mode 100644 tests/SessionOrigin.Author/SessionOrigin.Author.csproj create mode 100644 tests/SessionOrigin.Chain/ChainStep.cs create mode 100644 tests/SessionOrigin.Chain/SessionOrigin.Chain.csproj create mode 100644 tests/SessionOrigin.Compiled/OriginHooks.cs create mode 100644 tests/SessionOrigin.Compiled/SessionOrigin.Compiled.csproj diff --git a/RuneCLR.slnx b/RuneCLR.slnx index 70be0b3..5bf6cfd 100644 --- a/RuneCLR.slnx +++ b/RuneCLR.slnx @@ -24,6 +24,10 @@ + + + + diff --git a/docs/development.md b/docs/development.md index 0950253..180909b 100644 --- a/docs/development.md +++ b/docs/development.md @@ -20,6 +20,16 @@ callback-epoch stacks are not part of it. Its Windows x64 build/test evidence does not establish iOS or device behavior - the accepted iPad results stay with their own recorded pins. +## Session-origin integration branch (2026-11-12) + +The opt-in session-origin policy is prepared on the integration bases +`feature/session-origin-integration-20261112` (RuneCLR `origin/main`) and +`runeclr/session-origin-integration-20261112` (`runeclr/dev`). The design, opt-in +contract and limits are in +[validation/session-origin-integration-20261112.md](validation/session-origin-integration-20261112.md). +The runtime `RuneCLR.EnableReflectionPolicyOriginScan` switch defaults to disabled, +so a host that does not opt in keeps the existing reflection-policy behavior. + ## Physical iPad device R2R experiment (2026-11-10) The [iPad device R2R experiment](ios-device-r2r-experiment.md) records the first diff --git a/docs/ugc-session-policy.md b/docs/ugc-session-policy.md index cc85540..f4de79b 100644 --- a/docs/ugc-session-policy.md +++ b/docs/ugc-session-policy.md @@ -210,3 +210,11 @@ non-Windows builds and physical iOS need separate validation. No hostile-code sandbox, serializer/database compatibility, native/R2R execution or physical-device claim is added. + +## Session-origin integration (2026-11-12) + +An opt-in session-origin capability is prepared on top of this policy in the +integration branches; see +[validation/session-origin-integration-20261112.md](validation/session-origin-integration-20261112.md). +The runtime `RuneCLR.EnableReflectionPolicyOriginScan` switch defaults to +disabled, so the policy described here is unchanged for existing hosts. diff --git a/docs/validation/session-origin-integration-20261112.md b/docs/validation/session-origin-integration-20261112.md new file mode 100644 index 0000000..1605f1a --- /dev/null +++ b/docs/validation/session-origin-integration-20261112.md @@ -0,0 +1,73 @@ + +# Session-origin integration branch (2026-11-12) + +Status: review-ready integration branch, not merged into `main` or `runeclr/dev`. + +| repository | branch | base | +| --- | --- | --- | +| `ReneeGA2020/RuneCLR` | `feature/session-origin-integration-20261112` | `origin/main` `cbac2a657793dca7f22b1e1feff4aab26b898b4e` | +| `ReneeGA2020/runtime` | `runeclr/session-origin-integration-20261112` | `fork/runeclr/dev` `474eb8ad7230122cc0d34318b25d673da63fd138` | + +The archived experiment (`experiment/session-origin-20261112`, +`runeclr/session-origin-20261112`) is the source reference only; this branch is +rebuilt on the current integration bases and does not copy the old files over +the newer upstream state. + +## Opt-in contract + +- Runtime: capability bit 0 (`SessionOriginCapability`) is reported only when the + process is started with `RuneCLR.EnableReflectionPolicyOriginScan=true` in its + runtime configuration. An absent switch means **disabled**. +- When enabled, `RuneCLRReflectionPolicy_GetStackLoadContext` returns the + managed `AssemblyLoadContext` of a bounded 16-frame stack walk; saturation or a + walk error returns no context and the caller fails closed. +- Host: `PrototypeRequireOriginScan` on `MapRunOptions` and + `HostContractSessionOptions` binds the capability before author loading and + throws `PlatformNotSupportedException` when bit 0 is absent, so no author + assembly loads. The option asserts the capability; it does not enable scanning + by itself, and it requires `RequireInterpreter` plus an API policy. +- Consequence: a host that does not opt in keeps the pre-existing + reflection-policy behavior - no stack scanning and no capability bit. A host + that wants origin scanning must set both the process startup switch and the + host option. + +## Tests + +`eng/verify-runtime-session-origin.py --expect-fixed` drives the matrix against a +matching private runtime layout: fixed, frame, retirement, conflict, overflow, +disposal, default-off, capability-on, capability-off, map-gate-on/off, +host-gate-on/off, toggle-on/off and invalid option combinations. The `default-off` +mode starts without the switch and requires capability bit 0 to be absent. + +Measured results for this branch are recorded in the integration artifact +(`session-origin-integration-20261112/`), together with the fresh source, +runtime, toolchain and binary identities and the before/after pins. + +## Measured on 2026-11-12 + +- Runtime build: `build.cmd -subset clr -configuration Checked -arch x64` on + `474eb8ad7230122cc0d34318b25d673da63fd138` plus the five source files below; + SDK `12.0.100-alpha.1.26480.102`, CMake 4.2.1, MSVC 19.44.35229.0; build exit 0. +- Private binary identities: `coreclr.dll` + `42933493ffeb20c2b97baca808afc7d5f3d1cadeaa48354e2502a5f77134d7d9`, + `clrjit.dll` `dd432f9e7f4383be0e352a76ed592a47cb0b1831d89371fe1e25e2c666b13898`, + `clrinterpreter.dll` `83b2f6bbb25f95e01904788b7fb9d8d44a3b594e96bde2b626d8869350ef56b3`, + `corerun.exe` `cda4877d19e629ba8175cc55fa4553e4006f87cc53e26a3c054f93a62198b7ee`, + `System.Private.CoreLib.dll` `578089837a4d6695e0b581827bb50e21d8e005a546e926c136511d9bb5311fae`. +- Private layout: fresh Checked native output and CoreLib over the `runeclr/dev` + Release managed framework (same source pin); the matrix verifies the native + outputs and CoreLib against the fresh build hashes. +- Matrix: `SESSION_ORIGIN_FIXED LOCAL_UNCOMMITTED_SOURCE_MANIFEST PASS`, + 16 modes, 696 pins, runtime head `474eb8ad` plus the five uncommitted source + files; `default-off` reports capability 0, `capability-on` reports bit 0, and both + host gates reject before author loading with `PlatformNotSupportedException`. +- Host stock runtime: `REFLECTION_POLICY_STOCK_TESTS PASS` and the lifecycle suite + `156/156`. + +## Limits + +Native walker status 1 (saturation) and status 2 (walker error) have only +indirect coverage; the compiled-only suppressed child has no author frame; +unmanaged entry, AOT/mobile, global all-load and the published walk-cost bound +remain out of scope. No sandbox, native/R2R body, device or unload claim is made +by this branch. diff --git a/eng/test-session-origin-provenance.py b/eng/test-session-origin-provenance.py new file mode 100644 index 0000000..9dc20e2 --- /dev/null +++ b/eng/test-session-origin-provenance.py @@ -0,0 +1,191 @@ +#!/usr/bin/env python3 +"""Provenance negatives for the session-origin driver (source-manifest mode). + +Builds tiny throwaway git repositories and layout/build fixtures; no runtime or +product sources are read or written. The published verifier is not involved. +""" +import argparse +import hashlib +import importlib.util +import json +import os +from pathlib import Path +import subprocess +import sys +import tempfile + +NL = chr(10) +BACKSLASH = chr(92) + + +def load_driver(root): + path = root / "eng/verify-runtime-session-origin.py" + spec = importlib.util.spec_from_file_location("session_origin_driver", path) + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + return module + + +def run(root, *args, check=True): + result = subprocess.run(["git", "-C", str(root), *args], capture_output=True, text=True) + if check and result.returncode: + raise RuntimeError("git " + " ".join(args) + " failed: " + result.stderr.strip()) + return result.stdout.strip() + + +def commit(root, message): + run(root, "-c", "user.name=session-origin", "-c", "user.email=session-origin@example.invalid", + "-c", "commit.gpgsign=false", "commit", "-q", "-m", message) + + +def digest_bytes(path): + with path.open("rb") as stream: + return hashlib.file_digest(stream, "sha256").hexdigest() + + +def new_repo(base): + repo = base / "repo" + repo.mkdir(parents=True) + run(repo, "init", "-q", "-b", "main") + (repo / "src").mkdir() + (repo / "src/x.cs").write_text("new" + NL, encoding="utf-8") + (repo / "src/gone.cs").write_text("gone" + NL, encoding="utf-8") + (repo / "a.txt").write_text("stable" + NL, encoding="utf-8") + run(repo, "add", "-A") + commit(repo, "base") + base_commit = run(repo, "rev-parse", "HEAD") + (repo / "src/x.cs").write_text("changed" + NL, encoding="utf-8") + (repo / "src/new.cs").write_text("added" + NL, encoding="utf-8") + return repo, base_commit + + +def manifest_for(repo, base_commit, names): + return {"base": base_commit, "head": run(repo, "rev-parse", "HEAD"), + "files": {name: digest_bytes(repo / name) for name in names}} + + +def expect_reject(label, call, contains): + try: + call() + except ValueError as error: + if contains.lower() not in str(error).lower(): + return label + ": rejected with unexpected message: " + str(error) + print("PASS reject", label, "->", str(error).split(":")[0][:80]) + return None + return label + ": was accepted but must be rejected" + + +def expect_accept(label, call): + try: + call() + except Exception as error: + return label + ": failed unexpectedly: " + str(error) + print("PASS accept", label) + return None + + +def native_fixture(base): + build = base / "build" + layout = base / "layout" + shared = layout / "shared/Microsoft.NETCore.App/12.0.0" + (build / "IL").mkdir(parents=True) + shared.mkdir(parents=True) + for name in ("coreclr.dll", "clrjit.dll", "clrinterpreter.dll", "corerun.exe"): + (build / name).write_bytes(("built-" + name).encode()) + (shared / name).write_bytes(("built-" + name).encode()) + (build / "IL/System.Private.CoreLib.dll").write_bytes(b"built-corelib") + (shared / "System.Private.CoreLib.dll").write_bytes(b"built-corelib") + return build, layout, build / "IL/System.Private.CoreLib.dll" + + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument("--workdir", type=Path) + args = parser.parse_args() + + root = Path(__file__).resolve().parents[1] + driver = load_driver(root) + base = (args.workdir or Path(tempfile.mkdtemp(prefix="session-origin-provenance-"))).resolve() + base.mkdir(parents=True, exist_ok=True) + + failures = [] + repo, base_commit = new_repo(base) + manifest = manifest_for(repo, base_commit, ["src/x.cs", "src/new.cs"]) + + failures.append(expect_accept("exact delta and hashes", lambda: driver.verify_source_manifest(repo, manifest))) + + extra = dict(manifest) + extra["files"] = dict(manifest["files"]) + (repo / "src/extra.cs").write_text("extra" + NL, encoding="utf-8") + failures.append(expect_reject("extra source", lambda: driver.verify_source_manifest(repo, extra), "outside the source manifest")) + (repo / "src/extra.cs").unlink() + + wrong = dict(manifest) + wrong["files"] = dict(manifest["files"]) + wrong["files"]["src/x.cs"] = "0" * 64 + failures.append(expect_reject("changed hash", lambda: driver.verify_source_manifest(repo, wrong), "hash mismatch")) + + wrong_head = dict(manifest) + wrong_head["head"] = "1" * 40 + failures.append(expect_reject("head mismatch", lambda: driver.verify_source_manifest(repo, wrong_head), "must equal the source manifest head")) + + manifest_path = base / "manifest.json" + manifest_path.write_text(json.dumps({"base": "0" * 40, "head": manifest["head"], + "files": manifest["files"]}), encoding="utf-8") + failures.append(expect_reject("base mismatch", lambda: driver.load_source_manifest(manifest_path), "base must be")) + + for label, name in (("path escape parent", "../escape.cs"), + ("path escape absolute", "/tmp/escape.cs"), + ("path escape drive", "C:/escape.cs"), + ("path escape backslash", "src" + BACKSLASH + "escape.cs")): + manifest_path.write_text(json.dumps({"base": driver.RUNTIME_BASE, "head": manifest["head"], + "files": {name: "a" * 64}}), encoding="utf-8") + failures.append(expect_reject(label, lambda: driver.load_source_manifest(manifest_path), "escapes")) + + noop = dict(manifest) + noop["files"] = dict(manifest["files"]) + noop["files"]["a.txt"] = digest_bytes(repo / "a.txt") + failures.append(expect_reject("unchanged entry", lambda: driver.verify_source_manifest(repo, noop), "without a runtime change")) + + gone = dict(manifest) + gone["files"] = dict(manifest["files"]) + gone["files"]["src/gone.cs"] = digest_bytes(repo / "src/gone.cs") + (repo / "src/gone.cs").unlink() + failures.append(expect_reject("deleted source", lambda: driver.verify_source_manifest(repo, gone), "rejects deletions")) + + failures.append(expect_reject("clean-commit mode unchanged", + lambda: driver.verify_runtime_provenance(repo, ""), "worktree must be clean")) + + build, layout, corelib = native_fixture(base) + failures.append(expect_accept("matching native outputs", + lambda: driver.verify_native_identity(build, layout, "12.0.0", corelib))) + + (layout / "shared/Microsoft.NETCore.App/12.0.0/coreclr.dll").write_bytes(b"mixed-native") + failures.append(expect_reject("native coreclr mismatch", + lambda: driver.verify_native_identity(build, layout, "12.0.0", corelib), + "does not equal the fresh runtime build output")) + (layout / "shared/Microsoft.NETCore.App/12.0.0/coreclr.dll").write_bytes(b"built-coreclr.dll") + + (layout / "shared/Microsoft.NETCore.App/12.0.0/System.Private.CoreLib.dll").write_bytes(b"mixed-corelib") + failures.append(expect_reject("native CoreLib mismatch", + lambda: driver.verify_native_identity(build, layout, "12.0.0", corelib), + "Layout CoreLib does not equal")) + (layout / "shared/Microsoft.NETCore.App/12.0.0/System.Private.CoreLib.dll").write_bytes(b"built-corelib") + + (build / "clrinterpreter.dll").unlink() + failures.append(expect_reject("missing build output", + lambda: driver.verify_native_identity(build, layout, "12.0.0", corelib), + "build output is absent")) + (build / "clrinterpreter.dll").write_bytes(b"built-clrinterpreter.dll") + + failures = [failure for failure in failures if failure] + print("CASES", 16, "FAILURES", len(failures)) + for failure in failures: + print("FAILED", failure) + if failures: + raise SystemExit(1) + print("SESSION_ORIGIN_PROVENANCE PASS") + + +if __name__ == "__main__": + main() diff --git a/eng/verify-runtime-session-origin.py b/eng/verify-runtime-session-origin.py new file mode 100644 index 0000000..e338b0f --- /dev/null +++ b/eng/verify-runtime-session-origin.py @@ -0,0 +1,501 @@ +#!/usr/bin/env python3 +"""Run the opt-in session-origin baseline or fixed acceptance matrix. + +Baseline mode reproduces the pre-fix gap on the published runtime. Fixed mode +requires every in-scope author-frame, gate, capability and lifetime control to +pass on the new runtime build. The published reflection verifier stays separate. +""" +import argparse +import hashlib +import importlib.util +import json +import os +from pathlib import Path +import shutil +import subprocess +import sys + +NL = chr(10) +VERIFIER = Path(__file__).with_name("verify-runtime-reflection.py") +spec = importlib.util.spec_from_file_location("published_reflection_verifier", VERIFIER) +published = importlib.util.module_from_spec(spec) +spec.loader.exec_module(published) + +RUNTIME_BASE = published.RUNTIME_COMMIT +BUILD_RID = "windows.x64.Checked" +NATIVE_OUTPUTS = ("coreclr.dll", "clrjit.dll", "clrinterpreter.dll", "corerun.exe") + + +def digest(path): + with path.open("rb") as stream: + return hashlib.file_digest(stream, "sha256").hexdigest() + + +def write_json(path, value): + path.write_text(json.dumps(value, indent=2) + NL, encoding="utf-8") + + +def git(root, *args): + return subprocess.check_output(["git", "-C", str(root), *args], text=True).strip() + + +def git_raw(root, *args): + # Porcelain status encodes the index/worktree flags in the first two columns; + # stripping the output would eat the leading space of the first line. + return subprocess.check_output(["git", "-C", str(root), *args], text=True) + + +def verify_runtime_provenance(runtime, expected): + """Require a clean worktree carrying only in-tree runtime source changes.""" + status = git(runtime, "status", "--porcelain", "--untracked-files=normal") + if status: + raise ValueError("Runtime worktree must be clean; found: " + status.replace(NL, " | ")) + head = git(runtime, "rev-parse", "HEAD") + if expected and head != expected: + raise ValueError("Runtime HEAD must be " + expected + "; found " + head + ".") + if subprocess.run(["git", "-C", str(runtime), "merge-base", "--is-ancestor", RUNTIME_BASE, head]).returncode: + raise ValueError("Runtime HEAD " + head + " is not descended from published base " + RUNTIME_BASE + ".") + changed = [line for line in git(runtime, "diff", "--name-only", RUNTIME_BASE + ".." + head).splitlines() if line] + outside = [path for path in changed if not path.startswith("src/")] + if outside: + raise ValueError("Runtime changes outside src/: " + ", ".join(outside)) + if not changed and head != RUNTIME_BASE: + raise ValueError("Runtime HEAD carries no source delta over the published base.") + return head, changed + + +def runtime_build_dir(runtime, build_rid): + return runtime / "artifacts/bin/coreclr" / build_rid + + +def runtime_corelib(runtime, build_rid=BUILD_RID): + return runtime_build_dir(runtime, build_rid) / "IL/System.Private.CoreLib.dll" + + +def load_source_manifest(path): + """Read the explicit local-experiment manifest: {base, head, files{path: sha256}}.""" + data = json.loads(path.read_text(encoding="utf-8")) + if not isinstance(data, dict) or set(data) != {"base", "head", "files"}: + raise ValueError("Source manifest must contain exactly base, head and files.") + base, head, files = data["base"], data["head"], data["files"] + if base != RUNTIME_BASE: + raise ValueError("Source manifest base must be " + RUNTIME_BASE + "; found " + str(base) + ".") + if not isinstance(head, str) or len(head) != 40: + raise ValueError("Source manifest head must be a full commit id.") + if not isinstance(files, dict) or not files: + raise ValueError("Source manifest files must be a non-empty object.") + for name, value in files.items(): + if not isinstance(name, str) or not isinstance(value, str) or len(value) != 64: + raise ValueError("Source manifest entry is malformed: " + repr(name)) + if name.startswith("/") or chr(92) in name or ":" in name or ".." in name.split("/"): + raise ValueError("Source manifest path escapes the runtime tree: " + name) + return {"base": base, "head": head, "files": dict(files)} + + +def worktree_delta(runtime): + """Exactly the tracked, staged and untracked non-ignored paths the worktree changes.""" + status = git_raw(runtime, "status", "--porcelain=v1", "--untracked-files=normal") + changed = set() + for line in status.splitlines(): + code, rest = line[:2], line[3:] + if " -> " in rest or '"' in rest: + raise ValueError("Source-manifest mode rejects renames and quoted paths: " + line) + if code.strip().startswith("D"): + raise ValueError("Source-manifest mode rejects deletions: " + rest) + changed.add(rest) + return changed + + +def committed_hash(runtime, commit, name): + result = subprocess.run(["git", "-C", str(runtime), "rev-parse", commit + ":" + name], + capture_output=True, text=True) + return result.stdout.strip() if result.returncode == 0 else None + + +def verify_source_manifest(runtime, manifest): + """Require the worktree delta to equal the manifest exactly, with matching hashes.""" + head = git(runtime, "rev-parse", "HEAD") + if head != manifest["head"]: + raise ValueError("Runtime HEAD must equal the source manifest head " + + manifest["head"] + "; found " + head + ".") + changed = worktree_delta(runtime) + expected = set(manifest["files"]) + extra = sorted(changed - expected) + absent = sorted(expected - changed) + if extra: + raise ValueError("Runtime changes outside the source manifest: " + ", ".join(extra)) + if absent: + raise ValueError("Source manifest entries without a runtime change: " + ", ".join(absent)) + for name, value in sorted(manifest["files"].items()): + target = runtime / name + if not target.is_file(): + raise ValueError("Source manifest file is absent in the worktree: " + name) + actual = digest(target) + if actual != value: + raise ValueError("Source manifest hash mismatch for " + name + + ": expected " + value + ", found " + actual + ".") + if committed_hash(runtime, manifest["base"], name) == actual: + raise ValueError("Source manifest entry does not differ from the base commit: " + name) + return head, sorted(manifest["files"]) + + +def verify_native_identity(runtime_build, layout, framework, corelib): + """Require the layout native files to equal the fresh runtime build outputs.""" + shared = layout / "shared/Microsoft.NETCore.App" / framework + observed = {} + for name in NATIVE_OUTPUTS: + produced = runtime_build / name + installed = shared / name + if not produced.is_file(): + raise ValueError("Fresh runtime build output is absent: " + str(produced)) + if not installed.is_file(): + raise ValueError("Layout is missing " + name + ": " + str(installed)) + built, copied = digest(produced), digest(installed) + if built != copied: + raise ValueError("Layout " + name + " does not equal the fresh runtime build output.") + observed[name] = built + if not corelib.is_file(): + raise ValueError("Runtime build CoreLib is absent: " + str(corelib)) + installed_corelib = shared / "System.Private.CoreLib.dll" + if digest(corelib) != digest(installed_corelib): + raise ValueError("Layout CoreLib does not equal the runtime build output.") + observed["System.Private.CoreLib.dll"] = digest(corelib) + return observed + + +def build_layout(layout, core_root, host_source, framework_version, framework_manifest=None): + """Compose a private layout: build output bits plus a private host. + + The build's Core_Root-like directory can carry the framework assemblies and + native outputs without Microsoft.NETCore.App.deps.json; a read-only copy of + that metadata file completes the framework without contributing binaries. + """ + if not core_root.exists(): + raise ValueError("Core_Root is absent: " + str(core_root)) + if not (core_root / "System.Private.CoreLib.dll").exists(): + raise ValueError("Core_Root has no System.Private.CoreLib.dll: " + str(core_root)) + if layout.exists(): + raise ValueError("--build-layout requires a new layout directory.") + shared = layout / "shared/Microsoft.NETCore.App" / framework_version + shared.parent.mkdir(parents=True) + shutil.copytree(core_root, shared) + if framework_manifest is not None: + if not framework_manifest.is_file(): + raise ValueError("Framework manifest is absent: " + str(framework_manifest)) + shutil.copy2(framework_manifest, shared / "Microsoft.NETCore.App.deps.json") + name = "dotnet.exe" if os.name == "nt" else "dotnet" + if not (host_source / name).exists(): + raise ValueError("Host source has no " + name + ": " + str(host_source)) + shutil.copy2(host_source / name, layout / name) + if (host_source / "host").is_dir(): + shutil.copytree(host_source / "host", layout / "host") + + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument("--runtime-root", required=True, type=Path) + parser.add_argument("--layout", required=True, type=Path) + parser.add_argument("--output", required=True, type=Path) + parser.add_argument("--dotnet", default="dotnet") + parser.add_argument("--corelib", type=Path) + parser.add_argument("--core-root", type=Path) + parser.add_argument("--framework-manifest", type=Path) + parser.add_argument("--host-source", type=Path) + parser.add_argument("--framework-version", default="12.0.0") + parser.add_argument("--timeout", type=int, default=300) + parser.add_argument("--runtime-commit", default="") + parser.add_argument("--runtime-source-manifest", type=Path) + parser.add_argument("--build-rid", default=BUILD_RID) + parser.add_argument("--build-layout", action="store_true") + parser.add_argument("--expect-fixed", action="store_true") + args = parser.parse_args() + + root = Path(__file__).resolve().parents[1] + runtime = args.runtime_root.resolve() + layout = args.layout.resolve() + output = args.output.resolve() + if published.within(output, root) or published.within(output, runtime) or published.within(output, layout): + raise ValueError("Output must be outside source/runtime/layout roots.") + if output.exists(): + raise ValueError("Output must be a new directory.") + + manifest_file = None + if args.runtime_source_manifest: + if args.runtime_commit: + raise ValueError("--runtime-source-manifest and --runtime-commit are mutually exclusive.") + manifest_file = args.runtime_source_manifest.resolve() + if published.within(manifest_file, runtime) or published.within(manifest_file, root): + raise ValueError("The source manifest must live outside the source and runtime trees.") + manifest = load_source_manifest(manifest_file) + runtime_head, runtime_changed = verify_source_manifest(runtime, manifest) + provenance = "source-manifest" + else: + runtime_head, runtime_changed = verify_runtime_provenance(runtime, args.runtime_commit) + provenance = "clean-commit" + + if args.build_layout: + core_root = (args.core_root or + runtime / "artifacts/tests/coreclr/windows.x64.Checked/Tests/Core_Root").resolve() + host_source = (args.host_source or runtime / ".dotnet").resolve() + build_layout(layout, core_root, host_source, args.framework_version, + args.framework_manifest.resolve() if args.framework_manifest else None) + + private_host = layout / ("dotnet.exe" if os.name == "nt" else "dotnet") + framework = layout / "shared/Microsoft.NETCore.App" / args.framework_version + runtime_build = runtime_build_dir(runtime, args.build_rid) + corelib = (args.corelib or runtime_corelib(runtime, args.build_rid)).resolve() + if not private_host.exists(): + raise ValueError("Private layout host is absent: " + str(private_host)) + native = verify_native_identity(runtime_build, layout, args.framework_version, corelib) + + harness = root / "tests/RuneCLR.SessionOrigin.Tests/bin/Release/net11.0/RuneCLR.SessionOrigin.Tests.dll" + fixtures = harness.parent / "session-origin" + required = [harness, fixtures / "SessionOrigin.Author.dll", + fixtures / "SessionOrigin.Compiled.dll", + fixtures / "SessionOrigin.Chain.dll", + fixtures / "ReflectionPolicy.DeniedDependency.dll"] + for path in required: + if not path.exists(): + raise ValueError("Required fixture absent: " + str(path)) + + inputs = {Path(__file__).resolve(), VERIFIER.resolve(), private_host, harness, corelib, + framework / "System.Private.CoreLib.dll"} + inputs.update(runtime_build / name for name in NATIVE_OUTPUTS) + inputs.update(required[1:]) + # Everything the harness actually loads, plus the sources that produced it: + # the runtime source manifest alone cannot prove the managed host is current. + inputs.update(path for path in harness.parent.rglob("*") if path.is_file()) + for folder in (root / "src", root / "tests"): + inputs.update(path for path in folder.rglob("*") if path.is_file() + and not {"bin", "obj"}.intersection(path.relative_to(folder).parts)) + inputs.update({root / "RuneCLR.slnx", root / "Directory.Build.props"}) + if manifest_file is not None: + inputs.add(manifest_file) + inputs.update(runtime / name for name in published.RUNTIME_FILES) + inputs.update(runtime / name for name in runtime_changed) + inputs.update(path for path in layout.rglob("*") if path.is_file()) + pins = {str(path): digest(path) for path in sorted(inputs)} + + output.mkdir(parents=True) + environment = dict(os.environ) + for key in list(environment): + if key.startswith(("DOTNET_", "COMPlus_")): + del environment[key] + environment.update({"DOTNET_MULTILEVEL_LOOKUP": "0", "DOTNET_RuneCLRInterpretCollectible": "1", + "DOTNET_InterpMode": "0", "DOTNET_ReadyToRun": "1", "DOTNET_TieredCompilation": "0"}) + + # The origin-scan switch is latched during runtime startup, so a switch-off + # process must be configured before the runtime starts, never from Main. + harness_config = json.loads((harness.parent / "RuneCLR.SessionOrigin.Tests.runtimeconfig.json") + .read_text(encoding="utf-8")) + + def startup_config(name, scan): + data = json.loads(json.dumps(harness_config)) + properties = data.setdefault("runtimeOptions", {}).setdefault("configProperties", {}) + properties["RuneCLR.EnableReflectionPolicyOriginScan"] = bool(scan) + path = output / (name + ".runtimeconfig.json") + write_json(path, data) + return path + + def run_mode(name, mode_args, startup_scan=None): + stdout = output / (name + ".stdout.log") + stderr = output / (name + ".stderr.log") + command = [str(private_host), "exec", "--fx-version", args.framework_version] + if startup_scan is not None: + command += ["--runtimeconfig", str(startup_config(name, startup_scan))] + command += [str(harness), *mode_args] + write_json(output / (name + "-request.json"), {"argv": command, "cwd": str(root), + "startupOriginScan": startup_scan}) + with stdout.open("wb") as out, stderr.open("wb") as err: + process = subprocess.run(command, cwd=root, env=environment, stdout=out, stderr=err, + timeout=args.timeout) + text = stdout.read_text(encoding="utf-8", errors="replace") + parsed = None + start = text.find("{") + if start >= 0: + try: parsed = json.loads(text[start:]) + except json.JSONDecodeError: parsed = None + return {"exit": process.returncode, "stdout": str(stdout), "stderr": str(stderr), "parsed": parsed} + + modes = {} + failures = [] + if not args.expect_fixed: + mode = run_mode("baseline", ["--require-private-runtime"]) + modes["baseline"] = mode + observed = mode["exit"] == 0 and mode["parsed"] is not None and mode["parsed"].get("baselineGapObserved") is True + if not observed: + failures.append("baseline gap not observed") + result = {"mode": "baseline", "baselineGapObserved": observed} + else: + checks = [] + + def bit0(value): + return isinstance(value, int) and (value & 1) == 1 + + def capability(parsed): + return bit0(parsed.get("originScanCapability")) + + def run_check(name, mode_args, predicate, description, startup_scan=None): + mode = run_mode(name, mode_args, startup_scan) + modes[name] = mode + parsed = mode["parsed"] or {} + ok = mode["exit"] == 0 and mode["parsed"] is not None and bool(predicate(parsed)) + checks.append({"name": name, "passed": ok, "description": description, + "exit": mode["exit"], "parsed": parsed}) + if not ok: + failures.append(name + ": " + description) + return ok + + def reached_or_refused(ran, failure): + """The probe either ran its body or was refused before entry with a named error.""" + return ran is True or (isinstance(failure, str) and failure != "") + + def reached_missing_author_file(parsed): + return parsed.get("rejectedBeforeFile") is False and parsed.get("errorType") in ( + "System.IO.FileNotFoundException", "System.IO.DirectoryNotFoundException") + + def gate_refused_before_file(parsed): + return parsed.get("rejectedBeforeFile") is True and parsed.get("errorType") == "System.PlatformNotSupportedException" + + run_check("fixed", ["--expect-fixed", "--require-origin"], + lambda p: p.get("fixedModeObserved") is True and capability(p), + "normal/suppressed/compiled-helper author frames restricted with capability bit0", startup_scan=True) + + frame = run_mode("frame", ["--frame-probes", "--require-origin"], startup_scan=True) + modes["frame"] = frame + parsed = frame["parsed"] or {} + frame_ok = (frame["exit"] == 0 and frame["parsed"] is not None and + parsed.get("hidden", {}).get("FixedRestricted") is True and + parsed.get("inlined", {}).get("FixedRestricted") is True and + parsed.get("compiledOnlySuppressedRan") is True) + checks.append({"name": "frame", "passed": frame_ok, "exit": frame["exit"], + "description": "hidden/inlined author frames restricted; the compiled-only suppressed child " + "attempts forbidden reflection with no author frame and its outcome is " + "recorded, not required (ran=" + + str(parsed.get("compiledOnlySuppressedRan")) + ", escaped=" + + str(parsed.get("compiledOnlyEscaped")) + ", failure=" + + str(parsed.get("compiledOnlyFailure")) + ", effects=" + + str(parsed.get("compiledOnlyEffects")) + ")", + "parsed": parsed}) + if not frame_ok: + failures.append("frame: hidden/inlined author-frame control failed") + + run_check("retirement", ["--retirement", "--require-origin"], + lambda p: p.get("Effects") == 0 and + reached_or_refused(p.get("RetiredProbeRan"), p.get("RetiredFailure")), + "retired owner cannot revive a captured author probe: probe ran or was refused by name, " + "and no forbidden effect happened", startup_scan=True) + run_check("conflict", ["--conflict", "--require-origin"], + lambda p: p.get("BStored") is True and p.get("Effects") == 0 and + reached_or_refused(p.get("ConflictProbeRan"), p.get("ConflictFailure")), + "actual A/B owner conflict fails closed deny-wins: probe ran or was refused by name, " + "and no forbidden effect happened", startup_scan=True) + run_check("overflow", ["--overflow", "--require-origin"], + lambda p: p.get("Effects") == 0 and p.get("ChainDepth") == 17 and + p.get("ChainCompleted") is True and + p.get("ChainLeafFailure") == "System.NotSupportedException", + "17-deep foreign collectible-ALC stack reaches the leaf and is refused with " + "System.NotSupportedException (native saturation), with no forbidden effect", startup_scan=True) + run_check("disposal", ["--disposal", "--require-origin"], + lambda p: p.get("Effects") == 0 and + ((p.get("Parked") is True and p.get("ChildJoined") is True and + p.get("ReflectionDenied") is True) or + (isinstance(p.get("ChildFailure"), str) and p.get("ChildFailure") != "")), + "disposed owner fail-closes a live author frame: parked probe reached its denied reflection " + "or the entry was refused by name, with no forbidden effect", startup_scan=True) + run_check("default-off", ["--capability-probe", "false"], + lambda p: not capability(p) and p.get("switchEffective") is False, + "an absent RuneCLR.EnableReflectionPolicyOriginScan keeps the experiment off, so a host " + "that does not opt in keeps the pre-existing reflection-policy behavior") + run_check("capability-on", ["--capability-probe", "true"], + lambda p: capability(p) and p.get("switchEffective") is True, + "capability bit0 present with the startup switch true", startup_scan=True) + run_check("capability-off", ["--capability-probe", "false"], + lambda p: not capability(p) and p.get("switchEffective") is False, + "capability bit0 absent when the process starts with the switch false", startup_scan=False) + run_check("map-gate-on", ["--required-origin-gate", "true"], + reached_missing_author_file, + "Map required-origin gate accepts the capability and fails on the intentionally absent " + "author file (FileNotFound/DirectoryNotFound)", + startup_scan=True) + run_check("map-gate-off", ["--required-origin-gate", "false"], + gate_refused_before_file, + "Map required-origin gate rejects a startup-off process before author loading " + "(PlatformNotSupportedException)", + startup_scan=False) + run_check("host-gate-on", ["--host-required-origin-gate", "true"], + reached_missing_author_file, + "HostContract required-origin gate accepts the capability and fails on the intentionally " + "absent author file (FileNotFound/DirectoryNotFound)", + startup_scan=True) + run_check("host-gate-off", ["--host-required-origin-gate", "false"], + gate_refused_before_file, + "HostContract required-origin gate rejects a startup-off process before author loading " + "(PlatformNotSupportedException)", + startup_scan=False) + run_check("toggle-on", ["--toggle-gate", "true"], + lambda p: bit0(p.get("capabilityAfter")) and + p.get("mapRejectedBeforeFile") is False and + p.get("hostRejectedBeforeFile") is False, + "a post-latch switch-off does not revoke the latched capability", startup_scan=True) + run_check("toggle-off", ["--toggle-gate", "false"], + lambda p: not bit0(p.get("capabilityBefore")) and not bit0(p.get("capabilityAfter")) and + p.get("mapRejectedBeforeFile") is True and + p.get("hostRejectedBeforeFile") is True, + "a startup-off process stays latched off after the switch is turned on", startup_scan=False) + + run_check("invalid-option-combos", ["--invalid-option-combos"], + lambda p: p.get("invalidRejected") is True and + p.get("mapError") == "System.ArgumentException" and + p.get("hostError") == "System.ArgumentException", + "RequireOriginScan without RequireInterpreter and a policy is rejected with ArgumentException " + "for both MapRunOptions and HostContractSessionOptions") + + result = {"mode": "fixed", "checks": checks, + "compiledOnlyBoundary": (modes["frame"]["parsed"] or {}).get("compiledOnlySuppressedRan"), + "uncovered": [ + "native walker status 2 (no thread, aborted walk, unmaterialisable owner) has no " + "executable injection; it stays an explicit uncovered branch", + "status 1 (saturation) is witnessed only through the managed overflow lane, which " + "requires the leaf refusal System.NotSupportedException rather than a raw status value", + "compiled-only suppressed child has no author frame; its reflection outcome is recorded " + "as a boundary and scheduling-token/escaped-target work is deliberately deferred", + "unmanaged entry, AOT/mobile and global all-load coverage remain outside this milestone"]} + + after = {str(path): digest(path) if path.is_file() else None for path in sorted(inputs)} + drift = [path for path in pins if pins[path] != after[path]] + result["modes"] = {name: {"exit": mode["exit"], "parsed": mode["parsed"] is not None, + "stdout": mode["stdout"], "stderr": mode["stderr"]} + for name, mode in modes.items()} + result["failures"] = failures + result["inputCount"] = len(pins) + result["drift"] = drift + result["runtimeBase"] = RUNTIME_BASE + result["runtimeHead"] = runtime_head + result["runtimeChangedFiles"] = runtime_changed + result["layoutBuilt"] = bool(args.build_layout) + result["provenance"] = provenance + result["runtimeSourceManifest"] = str(manifest_file) if manifest_file is not None else None + result["localUncommitted"] = provenance == "source-manifest" + result["publishedAcceptance"] = provenance == "clean-commit" + result["nativeIdentity"] = native + result["passed"] = not failures and not drift and all( + mode["exit"] == 0 and mode["parsed"] is not None for mode in modes.values()) + write_json(output / "pins-before.json", pins) + write_json(output / "pins-after.json", after) + write_json(output / "summary.json", result) + if not result["passed"]: + raise RuntimeError("; ".join(failures) if failures else "input drift or mode failure") + label = ("SESSION_ORIGIN_FIXED" if args.expect_fixed else "SESSION_ORIGIN_BASELINE") + if provenance == "source-manifest": + label += " LOCAL_UNCOMMITTED_SOURCE_MANIFEST (not published acceptance)" + print(label + " PASS; modes=" + str(len(modes)) + "; pins=" + str(len(pins)) + + "; runtimeHead=" + runtime_head) + + +if __name__ == "__main__": + try: + main() + except Exception as error: + print("FAIL " + str(error), file=sys.stderr) + raise diff --git a/src/RuneCLR.Runtime/HostContractSession.cs b/src/RuneCLR.Runtime/HostContractSession.cs index 5ae65cc..93b9822 100644 --- a/src/RuneCLR.Runtime/HostContractSession.cs +++ b/src/RuneCLR.Runtime/HostContractSession.cs @@ -100,6 +100,7 @@ private static void PrepareOptions(HostContractSessionOptions options) _ = RequiredInterpreterBridge.Bind(); PrototypeReflectionPolicy.Prepare(); } + if (options.PrototypeRequireOriginScan) PrototypeReflectionPolicy.PrepareOriginScan(); } private static void RequireManifestMetadata(PackageManifest manifest) diff --git a/src/RuneCLR.Runtime/HostContractSessionOptions.cs b/src/RuneCLR.Runtime/HostContractSessionOptions.cs index 5c05e7a..bfe3c12 100644 --- a/src/RuneCLR.Runtime/HostContractSessionOptions.cs +++ b/src/RuneCLR.Runtime/HostContractSessionOptions.cs @@ -9,6 +9,7 @@ public sealed record HostContractSessionOptions public MapExecutionPolicy ExecutionPolicy { get; init; } = MapExecutionPolicy.Default; public IApiAccessPolicy? PrototypeApiPolicy { get; init; } public IReadOnlyList PrototypeSharedAssemblies { get; init; } = []; + public bool PrototypeRequireOriginScan { get; init; } internal void Validate() { @@ -21,6 +22,9 @@ internal void Validate() throw new ArgumentException("RequireInterpreter requires a HostContract API policy.", nameof(PrototypeApiPolicy)); if (PrototypeSharedAssemblies.Count != 0 && PrototypeApiPolicy is null) throw new ArgumentException("Explicit shared assemblies require a HostContract API policy."); + if (PrototypeRequireOriginScan && + (PrototypeApiPolicy is null || ExecutionPolicy != MapExecutionPolicy.RequireInterpreter)) + throw new ArgumentException("Prototype origin scanning requires RequireInterpreter and a HostContract API policy."); var names = new HashSet(StringComparer.OrdinalIgnoreCase); foreach (var assembly in PrototypeSharedAssemblies) { diff --git a/src/RuneCLR.Runtime/MapRunOptions.cs b/src/RuneCLR.Runtime/MapRunOptions.cs index 54c5ec5..a22228f 100644 --- a/src/RuneCLR.Runtime/MapRunOptions.cs +++ b/src/RuneCLR.Runtime/MapRunOptions.cs @@ -12,6 +12,8 @@ public sealed record MapRunOptions // Opt-in until every loading/entry path has matching runtime enforcement. public RuneCLR.Packages.IApiAccessPolicy? PrototypeApiPolicy { get; init; } public IReadOnlyList PrototypeSharedAssemblies { get; init; } = []; + // Opt-in requirement that the runtime enforce policy from actual stack origin. + public bool PrototypeRequireOriginScan { get; init; } // Experimental native IR-dispatch budget, not a complete execution-time sandbox. public long? PrototypeInstructionBudget { get; init; } public TimeSpan? PrototypeCallbackDeadline { get; init; } @@ -35,6 +37,9 @@ internal void Validate() ArgumentNullException.ThrowIfNull(PrototypeSharedAssemblies); if (PrototypeSharedAssemblies.Count != 0 && PrototypeApiPolicy is null) throw new ArgumentException("Explicit shared assemblies require a UGC API policy."); + if (PrototypeRequireOriginScan && + (PrototypeApiPolicy is null || ExecutionPolicy != MapExecutionPolicy.RequireInterpreter)) + throw new ArgumentException("Prototype origin scanning requires RequireInterpreter and a UGC API policy."); if (PrototypeInstructionBudget is { } instructionBudget) ArgumentOutOfRangeException.ThrowIfNegativeOrZero(instructionBudget, nameof(PrototypeInstructionBudget)); ArgumentOutOfRangeException.ThrowIfNegativeOrZero(PrototypeCleanupInstructionBudget); diff --git a/src/RuneCLR.Runtime/MapSession.cs b/src/RuneCLR.Runtime/MapSession.cs index b35164f..8890308 100644 --- a/src/RuneCLR.Runtime/MapSession.cs +++ b/src/RuneCLR.Runtime/MapSession.cs @@ -40,6 +40,7 @@ private MapSession(MapPackage package, MapRunOptions options, CallbackDeadlineBr _terminal = options.PrototypeInstructionBudget is not null || _deadline is not null ? TerminalPrototypeBridge.Bind(required!) : null; _policyVersion = required is null ? 0 : 1; if (options.PrototypeApiPolicy is not null) PrototypeReflectionPolicy.Prepare(); + if (options.PrototypeRequireOriginScan) PrototypeReflectionPolicy.PrepareOriginScan(); try { var resolver = new MapDependencyResolver(package, allowSessionAssemblyNames: options.PrototypeApiPolicy is not null); diff --git a/src/RuneCLR.Runtime/PrototypeReflectionPolicy.cs b/src/RuneCLR.Runtime/PrototypeReflectionPolicy.cs index 9eae0f3..88ccb47 100644 --- a/src/RuneCLR.Runtime/PrototypeReflectionPolicy.cs +++ b/src/RuneCLR.Runtime/PrototypeReflectionPolicy.cs @@ -13,6 +13,7 @@ public static class PrototypeReflectionPolicy private delegate IDisposable CreateSession(Action check, AssemblyLoadContext context, Action admit, out Func activate); private static readonly Lazy SessionEntry = new(BindSession); + private static readonly Lazy> OriginScanCapability = new(BindOriginScan); public static IDisposable Enter(IApiAccessPolicy policy) { @@ -28,6 +29,42 @@ internal static void Prepare() _ = SessionEntry.Value; } + internal static void PrepareOriginScan() + { + int capabilities; + try { capabilities = OriginScanCapability.Value(); } + catch (PlatformNotSupportedException) { throw; } + catch (Exception error) + { + throw new PlatformNotSupportedException( + "The runtime reflection-policy origin-scan capability cannot be queried.", error); + } + if ((capabilities & 1) == 0) + throw new PlatformNotSupportedException( + "The runtime reflection-policy origin-scan capability is unavailable. " + + "Start the process with RuneCLR.EnableReflectionPolicyOriginScan=true and use a " + + "runtime that reports capability bit 0."); + } + + private static Func BindOriginScan() + { + try + { + var type = typeof(MethodInfo).Assembly.GetType("System.Reflection.RuneCLRReflectionPolicy", false) + ?? throw new PlatformNotSupportedException( + "The runtime does not provide the reflection-policy origin-scan capability."); + var method = RequiredInterpreterBridge.Method(type, "RuneCLR_GetReflectionPolicyCapabilities", + isStatic: true, typeof(int)); + return method.CreateDelegate>(); + } + catch (PlatformNotSupportedException) { throw; } + catch (Exception error) + { + throw new PlatformNotSupportedException( + "The runtime reflection-policy origin-scan capability cannot be bound.", error); + } + } + internal static ReflectionPolicySession Create(IApiAccessPolicy policy, AssemblyLoadContext context, Action admit) { ArgumentNullException.ThrowIfNull(policy); diff --git a/tests/RuneCLR.SessionOrigin.Tests/Program.cs b/tests/RuneCLR.SessionOrigin.Tests/Program.cs new file mode 100644 index 0000000..47966fe --- /dev/null +++ b/tests/RuneCLR.SessionOrigin.Tests/Program.cs @@ -0,0 +1,404 @@ +using System.Reflection; +using System.Text.Json; +using RuneCLR; +using RuneCLR.Packages; +using RuneCLR.Runtime; +using SessionOrigin.Compiled; + +internal static class Program +{ + private static string Contract => typeof(RuneCLR.IMapScript).Assembly.FullName!; + private static string AuthorPath => Path.Combine(AppContext.BaseDirectory, "session-origin", "SessionOrigin.Author.dll"); + private static string DeniedPath => Path.Combine(AppContext.BaseDirectory, "session-origin", "ReflectionPolicy.DeniedDependency.dll"); + + private static int Main(string[] args) + { + AppContext.SetSwitch("RuneCLR.EnableReflectionPolicy", true); + OriginHooks.DeniedAssemblyBytes = File.ReadAllBytes(DeniedPath); + bool requireOrigin = args.Length == 2 && args[1] == "--require-origin"; + bool IsRequiredMode(string name) => args.Length is 1 or 2 && args[0] == name && + (args.Length == 1 || requireOrigin); + if (IsRequiredMode("--expect-fixed")) + { + var normalFixed = Run("SessionOrigin.Author.NormalThreadScript", requireOrigin); + var threadFixed = Run("SessionOrigin.Author.SuppressedThreadScript", requireOrigin); + var taskFixed = Run("SessionOrigin.Author.SuppressedTaskScript", requireOrigin); + var compiledHelperFixed = Run("SessionOrigin.Author.CompiledHelperScript", requireOrigin); + OriginHooks.Reset(); + int beforeHost = OriginHooks.ForbiddenEffects; + bool hostThreadAllowedFixed; + using (var hostSession = new MapRunner().Start( + new MapPackage("session-origin", AuthorPath, "SessionOrigin.Author.NormalThreadScript"), Options(requireOrigin))) + { + var hostThread = new Thread(() => OriginHooks.ForbiddenEffect()); + hostThread.Start(); + hostThread.Join(10000); + hostThreadAllowedFixed = OriginHooks.ForbiddenEffects == beforeHost + 1; + hostSession.Stop(); + } + bool fixedModeObserved = normalFixed.Restricted && threadFixed.FixedRestricted && + taskFixed.FixedRestricted && compiledHelperFixed.CompiledHelperRestricted && hostThreadAllowedFixed; + Console.WriteLine(JsonSerializer.Serialize(new + { + normal = normalFixed, + suppressedThread = threadFixed, + suppressedTask = taskFixed, + compiledHelper = compiledHelperFixed, + fixedModeObserved, + hostThreadAllowed = hostThreadAllowedFixed, + originScanCapability = OriginHooks.GetOriginScanCapability() + })); + return fixedModeObserved ? 0 : 1; + } + if (IsRequiredMode("--frame-probes")) + { + var hidden = Run("SessionOrigin.Author.HiddenHelperScript", requireOrigin); + var inlined = Run("SessionOrigin.Author.InlinedHelperScript", requireOrigin); + OriginHooks.Reset(); + OriginHooks.RunCompiledOnlySuppressed(); + Console.WriteLine(JsonSerializer.Serialize(new + { + hidden, + inlined, + compiledOnlySuppressedRan = OriginHooks.CompiledOnlySuppressedRan, + compiledOnlyEscaped = OriginHooks.CompiledOnlyEscaped, + compiledOnlyFailure = OriginHooks.CompiledOnlyFailure, + compiledOnlyEffects = OriginHooks.ForbiddenEffects, + authorFrameSlice = "hidden/inlined probes have an author frame; compiled-only attempts reflection without one and its outcome is recorded, not required" + })); + return 0; + } + if (IsRequiredMode("--retirement")) + { + OriginHooks.Reset(); + using (var session = new MapRunner().Start( + new MapPackage("session-origin", AuthorPath, "SessionOrigin.Author.RetirementScript"), Options(requireOrigin))) + { + session.Stop(); + } + OriginHooks.InvokeRetainedOnSuppressedThread(); + Console.WriteLine(JsonSerializer.Serialize(new + { + RetiredProbeRan = OriginHooks.RetiredProbeRan, + RetiredFailure = OriginHooks.RetiredFailure, + Effects = OriginHooks.ForbiddenEffects + })); + return 0; + } + if (IsRequiredMode("--conflict")) + { + OriginHooks.Reset(); + using (var ownerB = new MapRunner().Start( + new MapPackage("session-origin", AuthorPath, "SessionOrigin.Author.RetirementScript"), Options(requireOrigin))) + { + using var ownerA = new MapRunner().Start( + new MapPackage("session-origin", AuthorPath, "SessionOrigin.Author.ConflictingOwnerScript"), Options(requireOrigin)); + Console.WriteLine(JsonSerializer.Serialize(new + { + BStored = OriginHooks.OtherProbe is not null, + ConflictProbeRan = OriginHooks.ConflictProbeRan, + ConflictFailure = OriginHooks.ConflictFailure, + Effects = OriginHooks.ForbiddenEffects + })); + ownerA.Stop(); + ownerB.Stop(); + } + return 0; + } + if (IsRequiredMode("--overflow")) + { + OriginHooks.Reset(); + var chainPath = Path.Combine(AppContext.BaseDirectory, "session-origin", "SessionOrigin.Chain.dll"); + OriginHooks.BuildOverflowChain(File.ReadAllBytes(chainPath), + "SessionOrigin.Chain.ChainStep", "Invoke", 17); + using var session = new MapRunner().Start( + new MapPackage("session-origin", AuthorPath, "SessionOrigin.Author.OverflowScript"), Options(requireOrigin)); + bool running = session.IsRunning; + string? error = session.Result?.Report.Error; + if (running) session.Stop(); + Console.WriteLine(JsonSerializer.Serialize(new + { + SessionRunning = running, + Error = error, + OriginHooks.ChainDepth, + OriginHooks.ChainCompleted, + OriginHooks.ChainFailure, + OriginHooks.ChainLeafFailure, + ProbeRan = OriginHooks.ChildProbeRan, + ReflectionDenied = OriginHooks.ChildReflectionDenied, + LoadDenied = OriginHooks.ChildLoadDenied, + LoadSucceeded = OriginHooks.ChildLoadSucceeded, + Effects = OriginHooks.ForbiddenEffects, + ChildFailure = OriginHooks.ChildFailure + })); + return 0; + } + if (IsRequiredMode("--disposal")) + { + OriginHooks.Reset(); + bool parked; + bool retiredWhileParked; + bool childJoined; + string? startError; + using (var session = new MapRunner().Start( + new MapPackage("session-origin", AuthorPath, "SessionOrigin.Author.DisposalScript"), Options(requireOrigin))) + { + parked = OriginHooks.WaitChildParked(15000); + startError = session.Result?.Report.Error; + retiredWhileParked = session.IsRunning; + if (session.IsRunning) session.Stop(); + childJoined = OriginHooks.ReleaseBlockingChild(15000); + } + Console.WriteLine(JsonSerializer.Serialize(new + { + Parked = parked, + RetiredWhileParked = retiredWhileParked, + ChildJoined = childJoined, + StartError = startError, + ReflectionDenied = OriginHooks.ChildReflectionDenied, + LoadDenied = OriginHooks.ChildLoadDenied, + LoadSucceeded = OriginHooks.ChildLoadSucceeded, + Effects = OriginHooks.ForbiddenEffects, + ChildFailure = OriginHooks.ChildFailure + })); + return 0; + } + if (args.Length == 2 && args[0] == "--toggle-gate") + { + bool initial = bool.Parse(args[1]); + AppContext.SetSwitch("RuneCLR.EnableReflectionPolicyOriginScan", initial); + int? capabilityBefore = OriginHooks.GetOriginScanCapability(); + AppContext.SetSwitch("RuneCLR.EnableReflectionPolicyOriginScan", !initial); + int? capabilityAfter = OriginHooks.GetOriginScanCapability(); + Exception? mapError = null; + Exception? hostError = null; + try + { + new MapRunner().Start(new MapPackage("missing", "missing.dll", "Missing"), + Options() with { PrototypeRequireOriginScan = true }); + } + catch (Exception error) { mapError = error; } + try + { + using var rejected = new HostContractSession(typeof(IOriginContract), new OriginContractHost(), + "missing.dll", "Missing.Entry", ["Echo"], new HostContractSessionOptions + { + ExecutionPolicy = MapExecutionPolicy.RequireInterpreter, + PrototypeApiPolicy = new OriginPolicy(), + PrototypeRequireOriginScan = true + }); + } + catch (Exception error) { hostError = error; } + Console.WriteLine(JsonSerializer.Serialize(new + { + initialScan = initial, + capabilityBefore, + capabilityAfter, + mapRejectedBeforeFile = mapError is PlatformNotSupportedException, + hostRejectedBeforeFile = hostError is PlatformNotSupportedException, + mapError = mapError?.GetType().FullName, + hostError = hostError?.GetType().FullName + })); + return 0; + } + if (args.Length == 2 && args[0] == "--host-required-origin-gate") + { + bool scan = bool.Parse(args[1]); + AppContext.SetSwitch("RuneCLR.EnableReflectionPolicyOriginScan", scan); + Exception? hostGateError = null; + try + { + using var rejected = new HostContractSession(typeof(IOriginContract), new OriginContractHost(), + Path.Combine(Path.GetTempPath(), "missing-hostcontract-origin.dll"), "Missing.Entry", + ["Echo"], new HostContractSessionOptions + { + ExecutionPolicy = MapExecutionPolicy.RequireInterpreter, + PrototypeApiPolicy = new OriginPolicy(), + PrototypeRequireOriginScan = true + }); + } + catch (Exception error) { hostGateError = error; } + bool rejectedBeforeFile = hostGateError is PlatformNotSupportedException; + Console.WriteLine(JsonSerializer.Serialize(new + { + requestedScan = scan, + rejectedBeforeFile, + errorType = hostGateError?.GetType().FullName, + message = hostGateError?.Message + })); + // Report-only probe: the driver asserts accept vs reject semantics. + return 0; + } + if (args.Length == 1 && args[0] == "--invalid-option-combos") + { + Exception? mapError = null; + Exception? hostError = null; + try + { + new MapRunner().Start(new MapPackage("missing", "missing.dll", "Missing"), + new MapRunOptions { PrototypeRequireOriginScan = true }); + } + catch (Exception error) { mapError = error; } + try + { + using var rejected = new HostContractSession(typeof(IOriginContract), new OriginContractHost(), + "missing.dll", "Missing.Entry", ["Echo"], + new HostContractSessionOptions { PrototypeRequireOriginScan = true }); + } + catch (Exception error) { hostError = error; } + bool invalidRejected = mapError is ArgumentException && hostError is ArgumentException; + Console.WriteLine(JsonSerializer.Serialize(new + { + invalidRejected, + mapError = mapError?.GetType().FullName, + hostError = hostError?.GetType().FullName + })); + return invalidRejected ? 0 : 1; + } + if (args.Length == 2 && args[0] == "--required-origin-gate") + { + bool scan = bool.Parse(args[1]); + AppContext.SetSwitch("RuneCLR.EnableReflectionPolicyOriginScan", scan); + Exception? gateError = null; + try + { + using var rejected = new MapRunner().Start( + new MapPackage("missing", Path.Combine(Path.GetTempPath(), "missing-session-origin.dll"), "Missing"), + Options() with { PrototypeRequireOriginScan = true }); + } + catch (Exception error) { gateError = error; } + bool rejectedBeforeFile = gateError is PlatformNotSupportedException; + Console.WriteLine(JsonSerializer.Serialize(new + { + requestedScan = scan, + rejectedBeforeFile, + errorType = gateError?.GetType().FullName, + message = gateError?.Message + })); + // Report-only probe: the driver asserts accept vs reject semantics. + return 0; + } + if (args.Length == 2 && args[0] == "--capability-probe") + { + bool enabled = bool.Parse(args[1]); + AppContext.SetSwitch("RuneCLR.EnableReflectionPolicyOriginScan", enabled); + bool present = false; + bool effective = OriginHooks.GetOriginScanSwitch(out present); + Console.WriteLine(JsonSerializer.Serialize(new + { + requested = enabled, + switchPresent = present, + switchEffective = effective, + originScanCapability = OriginHooks.GetOriginScanCapability() + })); + return 0; + } + if (args.Length != 1 || args[0] != "--require-private-runtime") + return 2; + OriginHooks.DeniedAssemblyBytes = File.ReadAllBytes(DeniedPath); + var normal = Run("SessionOrigin.Author.NormalThreadScript"); + var thread = Run("SessionOrigin.Author.SuppressedThreadScript"); + var task = Run("SessionOrigin.Author.SuppressedTaskScript"); + var compiledHelper = Run("SessionOrigin.Author.CompiledHelperScript"); + bool hostThreadAllowed; + using (var hostSession = new MapRunner().Start( + new MapPackage("session-origin", AuthorPath, "SessionOrigin.Author.NormalThreadScript"), Options())) + { + int beforeHost = OriginHooks.ForbiddenEffects; + var hostThread = new Thread(() => OriginHooks.ForbiddenEffect()); + hostThread.Start(); + hostThread.Join(10000); + hostThreadAllowed = OriginHooks.ForbiddenEffects == beforeHost + 1; + hostSession.Stop(); + } + var result = new + { + normal, + suppressedThread = thread, + suppressedTask = task, + compiledHelper, + originScanCapability = OriginHooks.GetOriginScanCapability(), + originScanSwitch = new { present = OriginHooks.GetOriginScanSwitch(out bool scanPresent), presentFlag = scanPresent }, + baselineGapObserved = normal.Restricted && + thread.FlowAbsentAndEscaped && task.FlowAbsentAndEscaped && + compiledHelper.CompiledHelperRestricted && hostThreadAllowed, + hostThreadAllowed + }; + Console.WriteLine(JsonSerializer.Serialize(result, new JsonSerializerOptions { WriteIndented = true })); + return result.baselineGapObserved ? 0 : 1; + } + + private static ControlResult Run(string entryType, bool requireOrigin = false, bool resetHooks = true) + { + if (resetHooks) OriginHooks.Reset(); + using var session = new MapRunner().Start(new MapPackage("session-origin", AuthorPath, entryType), Options(requireOrigin)); + bool running = session.IsRunning; + string? error = session.Result?.Report.Error; + if (running) session.Stop(); + return new ControlResult( + entryType, + running, + error, + OriginHooks.ParentThreadId, + OriginHooks.ChildThreadId, + OriginHooks.ChildFlowAbsent, + OriginHooks.ChildProbeRan || OriginHooks.CompiledHelperDenied || OriginHooks.CompiledHelperEscaped, + OriginHooks.ChildReflectionDenied || OriginHooks.CompiledHelperDenied, + OriginHooks.ChildLoadDenied, + OriginHooks.ChildLoadSucceeded, + OriginHooks.ForbiddenEffects, + OriginHooks.ChildFailure); + } + + private static MapRunOptions Options(bool requireOrigin = false) => new() + { + ExecutionPolicy = MapExecutionPolicy.RequireInterpreter, + PrototypeSharedAssemblies = [typeof(OriginHooks).Assembly], + PrototypeApiPolicy = new OriginPolicy(), + PrototypeRequireOriginScan = requireOrigin + }; + + private sealed class OriginContractHost : IOriginContract + { + public int Echo(int value) => value; + } + + private sealed class OriginPolicy : IApiAccessPolicy + { + private readonly IApiAccessPolicy legacy = GameplayCapabilityPolicy.CreateApiPolicy([], Contract); + public ApiDecision Evaluate(ApiTarget target, ApiOperation operation) + { + if (target.IsSessionCode) return legacy.Evaluate(target, operation); + if (target.MemberName == "ForbiddenEffect" || target.TypeName == "System.IO.File") + return ApiDecision.Deny("Session-origin probe target is forbidden."); + return ApiDecision.Allow; + } + } + + private sealed record ControlResult( + string EntryType, + bool SessionRunning, + string? Error, + int ParentThreadId, + int ChildThreadId, + bool FlowAbsent, + bool ProbeRan, + bool ReflectionDenied, + bool LoadDenied, + bool LoadSucceeded, + int ForbiddenEffects, + string ChildFailure) + { + public bool Restricted => SessionRunning && ChildThreadId != 0 && ChildThreadId != ParentThreadId && + !FlowAbsent && ProbeRan && ReflectionDenied && LoadDenied && !LoadSucceeded && + ForbiddenEffects == 0 && string.IsNullOrEmpty(ChildFailure); + public bool FlowAbsentAndEscaped => SessionRunning && ChildThreadId != 0 && ChildThreadId != ParentThreadId && + FlowAbsent && ProbeRan && !ReflectionDenied && !LoadDenied && LoadSucceeded && + ForbiddenEffects == 1 && string.IsNullOrEmpty(ChildFailure); + public bool CompiledHelperRestricted => SessionRunning && ReflectionDenied && ForbiddenEffects == 0 && + string.IsNullOrEmpty(ChildFailure); + public bool FixedRestricted => SessionRunning && FlowAbsent && ChildThreadId != 0 && + ChildThreadId != ParentThreadId && ProbeRan && ReflectionDenied && LoadDenied && + !LoadSucceeded && ForbiddenEffects == 0 && string.IsNullOrEmpty(ChildFailure); + } +} diff --git a/tests/RuneCLR.SessionOrigin.Tests/RuneCLR.SessionOrigin.Tests.csproj b/tests/RuneCLR.SessionOrigin.Tests/RuneCLR.SessionOrigin.Tests.csproj new file mode 100644 index 0000000..68ee8f4 --- /dev/null +++ b/tests/RuneCLR.SessionOrigin.Tests/RuneCLR.SessionOrigin.Tests.csproj @@ -0,0 +1,17 @@ + + Exe + + + + + + + + + + + + + + + diff --git a/tests/SessionOrigin.Author/OriginScript.cs b/tests/SessionOrigin.Author/OriginScript.cs new file mode 100644 index 0000000..fc1f61c --- /dev/null +++ b/tests/SessionOrigin.Author/OriginScript.cs @@ -0,0 +1,140 @@ +using System.Reflection; +using RuneCLR; +using SessionOrigin.Compiled; + +namespace SessionOrigin.Author; + +public class OriginScript : IMapScript +{ + public OriginScript() { } + public virtual void OnStart(IMapContext context) { } + public virtual void OnTick(MapTick tick) { } + public virtual void OnStop() { } + + protected static void Run(bool suppressFlow, bool useTask, bool hiddenFrame = false, bool inlinedFrame = false) + { + OriginHooks.RunChild(Probe(nameof(ChildProbe)), suppressFlow, useTask, + hiddenFrame: hiddenFrame, inlinedFrame: inlinedFrame); + } + + protected static MethodInfo Probe(string name) => typeof(OriginScript).GetMethod(name, + BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static)!; + + public static void ChildProbe() => ProbeBody(); + + public static void OverflowProbe() => OriginHooks.RunOverflowChain(); + + /// Parks inside the author frame so the host can retire the session underneath it. + public static void BlockingProbe() + { + OriginHooks.ParkUntilReleased(15000); + ProbeBody(); + } + + private static void ProbeBody() + { + try + { + var effect = typeof(OriginHooks).GetMethod(nameof(OriginHooks.ForbiddenEffect))!; + effect.Invoke(null, null); + } + catch (TargetInvocationException error) when (error.InnerException is InvalidOperationException) + { + OriginHooks.RecordReflectionDenied(); + } + catch (InvalidOperationException) + { + OriginHooks.RecordReflectionDenied(); + } + + try + { + Assembly.Load(OriginHooks.DeniedAssemblyBytes!); + OriginHooks.RecordLoadSucceeded(); + } + catch (Exception error) when (error is InvalidOperationException or InvalidDataException or BadImageFormatException) + { + OriginHooks.RecordLoadDenied(); + } + } +} + +public sealed class NormalThreadScript : OriginScript +{ + public override void OnStart(IMapContext context) { base.OnStart(context); Run(false, false); } +} + +public sealed class SuppressedThreadScript : OriginScript +{ + public override void OnStart(IMapContext context) { base.OnStart(context); Run(true, false); } +} + +public sealed class SuppressedTaskScript : OriginScript +{ + public override void OnStart(IMapContext context) { base.OnStart(context); Run(true, true); } +} + +public sealed class CompiledHelperScript : OriginScript +{ + public override void OnStart(IMapContext context) + { + base.OnStart(context); + OriginHooks.ReflectForbiddenCompiledHelper(); + } +} + +public sealed class RetirementScript : OriginScript +{ + public override void OnStart(IMapContext context) + { + base.OnStart(context); + OriginHooks.SetRetainedProbe(typeof(OriginScript).GetMethod(nameof(ChildProbe), + BindingFlags.Public | BindingFlags.Static)!); + } +} + +public sealed class HiddenHelperScript : OriginScript +{ + public override void OnStart(IMapContext context) + { + base.OnStart(context); + Run(true, false, hiddenFrame: true); + } +} + +public sealed class InlinedHelperScript : OriginScript +{ + public override void OnStart(IMapContext context) + { + base.OnStart(context); + Run(true, false, inlinedFrame: true); + } +} + +public sealed class ConflictingOwnerScript : OriginScript +{ + public override void OnStart(IMapContext context) + { + base.OnStart(context); + OriginHooks.InvokeOther(); + } +} + +public sealed class OverflowScript : OriginScript +{ + public override void OnStart(IMapContext context) + { + base.OnStart(context); + OriginHooks.SetRetainedProbe(Probe(nameof(ChildProbe))); + OriginHooks.RunChild(Probe(nameof(OverflowProbe)), suppressFlow: true, useTask: false); + } +} + +public sealed class DisposalScript : OriginScript +{ + public override void OnStart(IMapContext context) + { + base.OnStart(context); + OriginHooks.StartBlockingChild(Probe(nameof(BlockingProbe))); + } +} diff --git a/tests/SessionOrigin.Author/SessionOrigin.Author.csproj b/tests/SessionOrigin.Author/SessionOrigin.Author.csproj new file mode 100644 index 0000000..bc00fda --- /dev/null +++ b/tests/SessionOrigin.Author/SessionOrigin.Author.csproj @@ -0,0 +1,6 @@ + + + + + + diff --git a/tests/SessionOrigin.Chain/ChainStep.cs b/tests/SessionOrigin.Chain/ChainStep.cs new file mode 100644 index 0000000..91be438 --- /dev/null +++ b/tests/SessionOrigin.Chain/ChainStep.cs @@ -0,0 +1,7 @@ +namespace SessionOrigin.Chain; + +/// A single foreign-frame hop used to build a deep collectible-ALC stack. +public static class ChainStep +{ + public static void Invoke(System.Action next) => next(); +} \ No newline at end of file diff --git a/tests/SessionOrigin.Chain/SessionOrigin.Chain.csproj b/tests/SessionOrigin.Chain/SessionOrigin.Chain.csproj new file mode 100644 index 0000000..6b512ec --- /dev/null +++ b/tests/SessionOrigin.Chain/SessionOrigin.Chain.csproj @@ -0,0 +1 @@ + diff --git a/tests/SessionOrigin.Compiled/OriginHooks.cs b/tests/SessionOrigin.Compiled/OriginHooks.cs new file mode 100644 index 0000000..12ed04e --- /dev/null +++ b/tests/SessionOrigin.Compiled/OriginHooks.cs @@ -0,0 +1,308 @@ +using System.IO; +using System.Reflection; +using System.Runtime.Loader; +using System.Threading; +using System.Threading.Tasks; + +namespace SessionOrigin.Compiled; + +public interface IOriginContract +{ + int Echo(int value); +} + +public static class OriginHooks +{ + public static byte[]? DeniedAssemblyBytes; + public static int ParentThreadId; + public static int ChildThreadId; + public static bool ChildFlowAbsent; + public static bool ChildProbeRan; + public static bool ChildReflectionDenied; + public static bool ChildLoadDenied; + public static bool ChildLoadSucceeded; + public static int ForbiddenEffects; + public static string ChildFailure = ""; + public static bool CompiledHelperDenied; + public static bool CompiledHelperEscaped; + public static MethodInfo? RetainedProbe; + public static MethodInfo? OtherProbe; + public static bool RetiredProbeRan; + public static bool ConflictProbeRan; + public static string RetiredFailure = ""; + public static string ConflictFailure = ""; + public static bool CompiledOnlySuppressedRan; + public static bool CompiledOnlyEscaped; + public static string CompiledOnlyFailure = ""; + public static readonly AsyncLocal FlowMarker = new(); + public static Action? OverflowChain; + public static int ChainDepth; + public static bool ChainCompleted; + public static string ChainFailure = ""; + public static string ChainLeafFailure = ""; + private static readonly List ChainContexts = []; + private static Thread? BlockingChild; + private static readonly ManualResetEventSlim BlockingParked = new(false); + private static readonly ManualResetEventSlim BlockingRelease = new(false); + + public static void ForbiddenEffect() => ForbiddenEffects++; + + public static void ReflectForbiddenCompiledHelper() + { + try + { + typeof(OriginHooks).GetMethod(nameof(ForbiddenEffect))!.Invoke(null, null); + CompiledHelperEscaped = true; + } + catch (InvalidOperationException) + { + CompiledHelperDenied = true; + } + } + + public static void RecordReflectionDenied() => ChildReflectionDenied = true; + public static void RecordLoadDenied() => ChildLoadDenied = true; + public static void RecordLoadSucceeded() => ChildLoadSucceeded = true; + + public static int? GetOriginScanCapability() + { + var type = typeof(object).Assembly.GetType("System.Reflection.RuneCLRReflectionPolicy"); + var method = type?.GetMethod("RuneCLR_GetReflectionPolicyCapabilities", + BindingFlags.Static | BindingFlags.NonPublic); + if (method is null) return null; + return method.CreateDelegate>()(); + } + + public static bool GetOriginScanSwitch(out bool present) + { + present = AppContext.TryGetSwitch("RuneCLR.EnableReflectionPolicyOriginScan", out bool value); + return !present || value; + } + + public static void Reset() + { + ParentThreadId = 0; + ChildThreadId = 0; + ChildFlowAbsent = false; + ChildProbeRan = false; + ChildReflectionDenied = false; + ChildLoadDenied = false; + ChildLoadSucceeded = false; + ForbiddenEffects = 0; + ChildFailure = ""; + CompiledHelperDenied = false; + CompiledHelperEscaped = false; + RetainedProbe = null; + OtherProbe = null; + RetiredProbeRan = false; + ConflictProbeRan = false; + RetiredFailure = ""; + ConflictFailure = ""; + CompiledOnlySuppressedRan = false; + CompiledOnlyEscaped = false; + CompiledOnlyFailure = ""; + OverflowChain = null; + ChainDepth = 0; + ChainCompleted = false; + ChainFailure = ""; + ChainLeafFailure = ""; + ClearChainContexts(); + BlockingChild = null; + BlockingParked.Reset(); + BlockingRelease.Reset(); + } + + private static void ClearChainContexts() + { + foreach (var context in ChainContexts.ToArray()) context.Unload(); + ChainContexts.Clear(); + } + + public static void SetRetainedProbe(MethodInfo probe) + { + RetainedProbe = probe; + OtherProbe = probe; + } + + public static void InvokeRetainedOnSuppressedThread() + { + if (RetainedProbe is null) { RetiredFailure = "missing-probe"; return; } + var thread = new Thread(() => + { + try { RetainedProbe.Invoke(null, null); RetiredProbeRan = true; } + catch (Exception error) { RetiredFailure = (error is TargetInvocationException && error.InnerException is not null + ? error.InnerException : error).GetType().Name; } + }); + using (ExecutionContext.SuppressFlow()) thread.Start(); + thread.Join(10000); + } + + public static void InvokeOther() + { + if (OtherProbe is null) { ConflictFailure = "missing-probe"; return; } + try { OtherProbe.Invoke(null, null); ConflictProbeRan = true; } + catch (Exception error) { ConflictFailure = (error is TargetInvocationException && error.InnerException is not null + ? error.InnerException : error).GetType().Name; } + } + + [System.Diagnostics.StackTraceHidden] + [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] + private static void InvokeAuthorHidden(MethodInfo probe) => probe.Invoke(null, null); + + [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] + private static void InvokeAuthorInlined(MethodInfo probe) => probe.Invoke(null, null); + + public static void RunChild(MethodInfo probe, bool suppressFlow, bool useTask, int timeoutMs = 15000, + bool hiddenFrame = false, bool inlinedFrame = false) + { + ParentThreadId = Environment.CurrentManagedThreadId; + FlowMarker.Value = "scheduled"; + Action child = () => + { + ChildThreadId = Environment.CurrentManagedThreadId; + ChildFlowAbsent = FlowMarker.Value is null; + try + { + if (hiddenFrame) InvokeAuthorHidden(probe); + else if (inlinedFrame) InvokeAuthorInlined(probe); + else probe.Invoke(null, null); + ChildProbeRan = true; + } + catch (Exception error) + { + ChildFailure = (error is TargetInvocationException && error.InnerException is not null + ? error.InnerException : error).GetType().Name; + } + }; + + if (useTask) + { + Task task; + if (suppressFlow) + { + using (ExecutionContext.SuppressFlow()) task = Task.Run(child); + } + else + { + task = Task.Run(child); + } + task.Wait(timeoutMs); + } + else + { + Thread thread; + if (suppressFlow) + { + using (ExecutionContext.SuppressFlow()) + { + thread = new Thread(() => child()); + thread.Start(); + } + } + else + { + thread = new Thread(() => child()); + thread.Start(); + } + thread.Join(timeoutMs); + } + FlowMarker.Value = null; + } + + /// Loads one helper assembly into distinct collectible contexts and chains them + /// so a single call nests that many foreign ALC frames. + public static void BuildOverflowChain(byte[] helperBytes, string typeName, string methodName, int depth) + { + ChainDepth = 0; + ChainCompleted = false; + ChainFailure = ""; + ChainLeafFailure = ""; + Action chain = () => + { + // The author sets the retained probe during OnStart, before the chain is entered. + var probe = RetainedProbe; + if (probe is null) { ChainLeafFailure = "missing-probe"; return; } + try { probe.Invoke(null, null); } + catch (Exception error) { ChainLeafFailure = Describe(error); } + }; + for (var index = 0; index < depth; index++) + { + var context = new AssemblyLoadContext("session-origin-chain-" + index, isCollectible: true); + var assembly = context.LoadFromStream(new MemoryStream(helperBytes)); + var method = assembly.GetType(typeName, throwOnError: true)!.GetMethod(methodName, + BindingFlags.Public | BindingFlags.Static) + ?? throw new InvalidOperationException("The chain helper method is missing."); + var step = method.CreateDelegate>(); + ChainContexts.Add(context); + var next = chain; + var level = index + 1; + chain = () => + { + ChainDepth = Math.Max(ChainDepth, level); + step(next); + }; + } + OverflowChain = chain; + } + + public static void RunOverflowChain() + { + try { OverflowChain?.Invoke(); ChainCompleted = true; } + catch (Exception error) { ChainFailure = Describe(error); } + } + + /// Starts an author frame on a suppressed-flow thread and leaves it parked. + public static void StartBlockingChild(MethodInfo probe) + { + BlockingParked.Reset(); + BlockingRelease.Reset(); + var thread = new Thread(() => + { + try { probe.Invoke(null, null); } + catch (Exception error) { ChildFailure = Describe(error); } + }); + using (ExecutionContext.SuppressFlow()) thread.Start(); + BlockingChild = thread; + } + + public static bool WaitChildParked(int timeoutMs) => BlockingParked.Wait(timeoutMs); + + public static bool ReleaseBlockingChild(int joinMs) + { + BlockingRelease.Set(); + return BlockingChild?.Join(joinMs) == true; + } + + public static void ParkUntilReleased(int timeoutMs) + { + BlockingParked.Set(); + BlockingRelease.Wait(timeoutMs); + } + + private static string Describe(Exception error) + { + var inner = error is TargetInvocationException { InnerException: not null } wrapped ? wrapped.InnerException! : error; + return inner.GetType().FullName ?? inner.GetType().Name; + } + + /// Suppressed-flow child with no author frame that attempts the forbidden + /// reflection. The outcome is recorded, never asserted as an escape. + public static void RunCompiledOnlySuppressed() + { + var thread = new Thread(() => + { + CompiledOnlySuppressedRan = true; + try + { + typeof(OriginHooks).GetMethod(nameof(ForbiddenEffect))!.Invoke(null, null); + CompiledOnlyEscaped = true; + } + catch (Exception error) + { + CompiledOnlyFailure = Describe(error); + } + }); + using (ExecutionContext.SuppressFlow()) thread.Start(); + thread.Join(10000); + } +} diff --git a/tests/SessionOrigin.Compiled/SessionOrigin.Compiled.csproj b/tests/SessionOrigin.Compiled/SessionOrigin.Compiled.csproj new file mode 100644 index 0000000..6c5e81f --- /dev/null +++ b/tests/SessionOrigin.Compiled/SessionOrigin.Compiled.csproj @@ -0,0 +1 @@ + From 19650e94ae25f4868dd81812ad067b5f131f1c2c Mon Sep 17 00:00:00 2001 From: lulan <49630018+ReneeGA2020@users.noreply.github.com> Date: Thu, 12 Nov 2026 19:00:22 +0800 Subject: [PATCH 2/9] Correct source-provenance wording and record host SDK receipt - Distinguish the three runtime relations: tested tree 5/5 raw against the manifest, published blobs 5/5 after CRLF to LF normalization, and only 3/5 byte-identical to the archived experiment (qcall entry re-applied onto dev, CoreLib changed for default-off). Host: 14 of 16 archived files identical, two changed intentionally. - Record the host toolchain (ambient dotnet 11.0.100), the explicit rebuild and the fact that rebuilt outputs embed the source revision, plus the post-commit matrix / stock / lifecycle re-run. - State that upstream targeted tests are owned by a separate worker and remain pending; no pass is claimed here. Docs only; no source, test, policy or runtime commit changes. Assisted-by: Cindy (https://cindy.app) --- .../session-origin-integration-20261112.md | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/docs/validation/session-origin-integration-20261112.md b/docs/validation/session-origin-integration-20261112.md index 1605f1a..5d06254 100644 --- a/docs/validation/session-origin-integration-20261112.md +++ b/docs/validation/session-origin-integration-20261112.md @@ -43,6 +43,24 @@ Measured results for this branch are recorded in the integration artifact (`session-origin-integration-20261112/`), together with the fresh source, runtime, toolchain and binary identities and the before/after pins. +## Source provenance + +Three distinct relations, not one: + +- Tested working tree vs test manifest: runtime 5/5 raw sha256 equal (the matrix ran on those bytes). +- Tested bytes vs the published runtime commit: 5/5 equal after CRLF to LF normalization; Git stores + text normalized (`text=auto`). +- Integration vs the archived experiment commit: 3/5 byte-identical (`assemblynative.cpp`, + `assemblynative.hpp`, `ILLink.Descriptors.Shared.xml`); `qcallentrypoints.cpp` differs because the + registration is re-applied onto the newer dev upstream; `RuneCLRReflectionPolicy.cs` differs by the + deliberate default-off change. The archived experiment is not overwritten. +- Host: 14 of 16 archived source/test files are byte-identical; `PrototypeReflectionPolicy.cs` + (fail-closed message) and `eng/verify-runtime-session-origin.py` (default-off mode and startup + switch wiring) changed intentionally. + +Upstream targeted tests from `dotnet/runtime` are owned by a separate worker and are still pending; +this branch does not claim them. + ## Measured on 2026-11-12 - Runtime build: `build.cmd -subset clr -configuration Checked -arch x64` on @@ -64,6 +82,13 @@ runtime, toolchain and binary identities and the before/after pins. - Host stock runtime: `REFLECTION_POLICY_STOCK_TESTS PASS` and the lifecycle suite `156/156`. +- Host toolchain: `dotnet` version 11.0.100 (the repository has no + global.json; the SDK resolution was captured in local evidence and the build re-run explicitly). The rebuilt host + outputs embed the source revision `9505faa6...`; the pre-commit outputs embedded the previous + revision and are kept as history. +- Post-commit re-run with the rebuilt harness: matrix 16 modes / 696 pins PASS, stock reflection + PASS, lifecycle 156/156 (evidence: matrix-postcommit.log and repro-matrix-postcommit/). + ## Limits Native walker status 1 (saturation) and status 2 (walker error) have only From 24e5a8fae10f4c561253cc685544c7fbb875eb0b Mon Sep 17 00:00:00 2001 From: lulan <49630018+ReneeGA2020@users.noreply.github.com> Date: Thu, 12 Nov 2026 19:07:10 +0800 Subject: [PATCH 3/9] Verify committed runtime source without switching HEAD Add --runtime-committed-source: validate the exact integration commit against a recorded build-source receipt instead of requiring a dirty worktree. - receipt splits buildSource (head + raw file hashes that were built), integration (commit, its exact parent, canonical blob hashes) and the private runtime binary hashes - require a clean worktree at the receipt commit, the exact parent, a five-file modification-only delta, per-file committed blob hash and parent difference - per file, accept the working bytes only when their hash equals the recorded build source (raw) or when the build read the canonical form while the checkout materialises CRLF (normalized-canonical); mismatches are rejected - verify the fresh build outputs and CoreLib against the receipt binaries - the existing source-manifest (dirty build) and clean-commit modes stay unchanged This removes the need to check out the build-time dirty state; the shared runtime tree stays clean at the integration commit. No runtime source, binary or facades change. Assisted-by: Cindy (https://cindy.app) --- eng/verify-runtime-session-origin.py | 134 ++++++++++++++++++++++++++- 1 file changed, 133 insertions(+), 1 deletion(-) diff --git a/eng/verify-runtime-session-origin.py b/eng/verify-runtime-session-origin.py index e338b0f..338c983 100644 --- a/eng/verify-runtime-session-origin.py +++ b/eng/verify-runtime-session-origin.py @@ -139,6 +139,111 @@ def verify_source_manifest(runtime, manifest): return head, sorted(manifest["files"]) +COMMITTED_SCHEMA = "runeclr.session-origin.committed-source.v1" + + +def load_committed_receipt(path): + data = json.loads(path.read_text(encoding="utf-8")) + if not isinstance(data, dict) or data.get("schema") != COMMITTED_SCHEMA: + raise ValueError("Unexpected committed-source receipt schema: " + repr(data.get("schema") if isinstance(data, dict) else None)) + build, integ, binaries = data.get("buildSource"), data.get("integration"), data.get("binaries") + for name, value in (("buildSource", build), ("integration", integ), ("binaries", binaries)): + if not isinstance(value, dict): + raise ValueError("Committed-source receipt is missing " + name + ".") + for key in ("head", "files"): + if key not in build: + raise ValueError("buildSource is missing " + key + ".") + for key in ("commit", "parent", "files"): + if key not in integ: + raise ValueError("integration is missing " + key + ".") + if not isinstance(build["files"], dict) or not build["files"]: + raise ValueError("buildSource.files must be a non-empty object.") + if set(build["files"]) != set(integ["files"]): + raise ValueError("buildSource and integration file sets differ.") + for path in build["files"]: + if not isinstance(path, str) or path.startswith("/") or chr(92) in path or ":" in path or ".." in path.split("/"): + raise ValueError("Committed-source path escapes the runtime tree: " + repr(path)) + for table in (build["files"], integ["files"]): + if not isinstance(table[path], str) or len(table[path]) != 64: + raise ValueError("Committed-source hash is malformed for " + path) + if not isinstance(binaries, dict) or not binaries: + raise ValueError("Committed-source binaries must be a non-empty object.") + for name, value in binaries.items(): + if not isinstance(name, str) or not isinstance(value, str) or len(value) != 64: + raise ValueError("Committed-source binary entry is malformed: " + repr(name)) + for field, value in (("head", build["head"]), ("commit", integ["commit"]), ("parent", integ["parent"])): + if not isinstance(value, str) or len(value) != 40: + raise ValueError("Committed-source " + field + " must be a full commit id.") + if integ["parent"] != build["head"]: + raise ValueError("integration.parent must equal buildSource.head.") + relations = data.get("relations") or {} + if not isinstance(relations, dict): + raise ValueError("relations must be an object.") + return {"build": build, "integration": integ, "binaries": dict(binaries), "relations": dict(relations)} + + +def verify_committed_source(runtime, receipt): + """Require the exact committed integration snapshot with a recorded build source. + + The build ran at buildSource.head plus the recorded raw files. The commit is the + canonical (LF) form of the same files, parented on that exact build head. The + worktree must be clean; working bytes count as the raw build form only when their + hash matches the receipt, otherwise their newline-normalised form must equal the + committed blob and the relation must be declared as normalized. + """ + status = git_raw(runtime, "status", "--porcelain=v1", "--untracked-files=all") + if status.strip(): + raise ValueError("Runtime worktree must be clean for committed-source mode; found: " + status.replace(NL, " | ")) + head = git(runtime, "rev-parse", "HEAD") + integ = receipt["integration"] + if head != integ["commit"]: + raise ValueError("Runtime HEAD must equal the receipt commit " + integ["commit"] + "; found " + head + ".") + parent = git(runtime, "rev-parse", "HEAD^") + if parent != integ["parent"]: + raise ValueError("Runtime parent must equal the receipt parent " + integ["parent"] + "; found " + parent + ".") + if subprocess.run(["git", "-C", str(runtime), "merge-base", "--is-ancestor", RUNTIME_BASE, head]).returncode: + raise ValueError("Runtime HEAD " + head + " is not descended from published base " + RUNTIME_BASE + ".") + declared = set(integ["files"]) + changed = {} + for line in git(runtime, "diff", "--name-status", parent + ".." + head).splitlines(): + if not line: + continue + code, name = line.split(chr(9), 1) + changed[name] = code + if set(changed) != declared: + extra = sorted(set(changed) - declared) + absent = sorted(declared - set(changed)) + raise ValueError("Committed delta does not match the receipt: extra=" + repr(extra) + " absent=" + repr(absent)) + relations = {} + for path in sorted(declared): + if changed[path] != "M": + raise ValueError("Committed file is not a modification: " + path + " (" + changed[path] + ")") + blob = subprocess.run(["git", "-C", str(runtime), "show", head + ":" + path], capture_output=True).stdout + if hashlib.sha256(blob).hexdigest() != integ["files"][path]: + raise ValueError("Committed blob hash mismatch for " + path + ".") + parent_blob = subprocess.run(["git", "-C", str(runtime), "show", parent + ":" + path], capture_output=True).stdout + if parent_blob == blob: + raise ValueError("Committed file does not differ from its parent: " + path) + work = (runtime / path).read_bytes() + normalized = work.replace(b"\r\n", b"\n") + if normalized != blob: + raise ValueError("Working bytes are not the committed content modulo newlines: " + path) + raw_sha = hashlib.sha256(work).hexdigest() + src_sha = receipt["build"]["files"][path] + declared_rel = receipt["relations"].get(path) + if raw_sha == src_sha: + relation = "raw" + elif src_sha == integ["files"][path]: + # The build read the canonical (LF) form while the checkout materialises CRLF. + relation = "normalized-canonical" + else: + raise ValueError("Working bytes do not match the recorded build source: " + path) + if declared_rel is not None and declared_rel != relation: + raise ValueError("Source relation mismatch for " + path + ": declared " + str(declared_rel) + ", observed " + relation + ".") + relations[path] = relation + return head, sorted(declared), relations + + def verify_native_identity(runtime_build, layout, framework, corelib): """Require the layout native files to equal the fresh runtime build outputs.""" shared = layout / "shared/Microsoft.NETCore.App" / framework @@ -205,6 +310,7 @@ def main(): parser.add_argument("--timeout", type=int, default=300) parser.add_argument("--runtime-commit", default="") parser.add_argument("--runtime-source-manifest", type=Path) + parser.add_argument("--runtime-committed-source", type=Path) parser.add_argument("--build-rid", default=BUILD_RID) parser.add_argument("--build-layout", action="store_true") parser.add_argument("--expect-fixed", action="store_true") @@ -220,7 +326,19 @@ def main(): raise ValueError("Output must be a new directory.") manifest_file = None - if args.runtime_source_manifest: + receipt_file = None + committed_receipt = None + source_relations = None + if args.runtime_committed_source: + if args.runtime_source_manifest or args.runtime_commit: + raise ValueError("--runtime-committed-source is mutually exclusive with --runtime-source-manifest/--runtime-commit.") + receipt_file = args.runtime_committed_source.resolve() + if published.within(receipt_file, runtime) or published.within(receipt_file, root): + raise ValueError("The committed-source receipt must live outside the source and runtime trees.") + committed_receipt = load_committed_receipt(receipt_file) + runtime_head, runtime_changed, source_relations = verify_committed_source(runtime, committed_receipt) + provenance = "committed-integration" + elif args.runtime_source_manifest: if args.runtime_commit: raise ValueError("--runtime-source-manifest and --runtime-commit are mutually exclusive.") manifest_file = args.runtime_source_manifest.resolve() @@ -247,6 +365,13 @@ def main(): if not private_host.exists(): raise ValueError("Private layout host is absent: " + str(private_host)) native = verify_native_identity(runtime_build, layout, args.framework_version, corelib) + if provenance == "committed-integration": + if set(committed_receipt["binaries"]) != set(native): + raise ValueError("Committed-source receipt binary set does not match the fresh build outputs: " + + repr(sorted(committed_receipt["binaries"])) + " vs " + repr(sorted(native))) + for name, expected in sorted(committed_receipt["binaries"].items()): + if native[name] != expected: + raise ValueError("Binary hash mismatch for " + name + ": expected " + expected + ", found " + native[name] + ".") harness = root / "tests/RuneCLR.SessionOrigin.Tests/bin/Release/net11.0/RuneCLR.SessionOrigin.Tests.dll" fixtures = harness.parent / "session-origin" @@ -475,6 +600,11 @@ def gate_refused_before_file(parsed): result["runtimeChangedFiles"] = runtime_changed result["layoutBuilt"] = bool(args.build_layout) result["provenance"] = provenance + result["committedIntegration"] = provenance == "committed-integration" + result["committedReceipt"] = str(receipt_file) if receipt_file is not None else None + result["buildSourceHead"] = committed_receipt["build"]["head"] if committed_receipt else None + result["integrationCommit"] = committed_receipt["integration"]["commit"] if committed_receipt else None + result["sourceRelations"] = source_relations result["runtimeSourceManifest"] = str(manifest_file) if manifest_file is not None else None result["localUncommitted"] = provenance == "source-manifest" result["publishedAcceptance"] = provenance == "clean-commit" @@ -489,6 +619,8 @@ def gate_refused_before_file(parsed): label = ("SESSION_ORIGIN_FIXED" if args.expect_fixed else "SESSION_ORIGIN_BASELINE") if provenance == "source-manifest": label += " LOCAL_UNCOMMITTED_SOURCE_MANIFEST (not published acceptance)" + elif provenance == "committed-integration": + label += " COMMITTED_INTEGRATION_SOURCE (build source recorded)" print(label + " PASS; modes=" + str(len(modes)) + "; pins=" + str(len(pins)) + "; runtimeHead=" + runtime_head) From f5fdaa4caa1fd72ee7e46b0edece9ddd7c90bc73 Mon Sep 17 00:00:00 2001 From: lulan <49630018+ReneeGA2020@users.noreply.github.com> Date: Thu, 12 Nov 2026 19:08:42 +0800 Subject: [PATCH 4/9] Document committed-source verification and the stable run Describe --runtime-committed-source (clean commit and parent, five-file modification-only delta, per-file raw vs normalized-canonical relations, binary receipt), record the 5/5 negative checks, and record the stable run: runtime clean at 2077f147, host clean at 24e5a8fa, 16 modes / 695 pins, with no HEAD switching. The earlier dev+dirty run stays as history. Docs only; no source, test, policy or runtime change. Assisted-by: Cindy (https://cindy.app) --- .../session-origin-integration-20261112.md | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/docs/validation/session-origin-integration-20261112.md b/docs/validation/session-origin-integration-20261112.md index 5d06254..32e6566 100644 --- a/docs/validation/session-origin-integration-20261112.md +++ b/docs/validation/session-origin-integration-20261112.md @@ -89,6 +89,31 @@ this branch does not claim them. - Post-commit re-run with the rebuilt harness: matrix 16 modes / 696 pins PASS, stock reflection PASS, lifecycle 156/156 (evidence: matrix-postcommit.log and repro-matrix-postcommit/). +- Committed-source run (2026-11-12, no HEAD switching): runtime clean at `2077f147`, + host clean at `24e5a8fa`; matrix + `SESSION_ORIGIN_FIXED COMMITTED_INTEGRATION_SOURCE PASS`, 16 modes, 695 pins, + drift 0 (matrix-committed.log, repro-matrix-committed/). The earlier dev+dirty run + (696 pins) is kept as history and is no longer the committed-source result. + +## Committed-source verification + +The verifier accepts the committed integration snapshot without moving HEAD: + +- `--runtime-committed-source ` requires a clean runtime worktree at the + receipt commit, that commit's exact parent, and a modification-only delta equal to the + receipt's five files. +- The receipt records the build source (parent commit plus raw file hashes that were built), + the integration commit (canonical Git blob hashes) and the private runtime binary hashes. +- Per file the working bytes must equal the recorded build source (raw) or, when the build + read the canonical form while the checkout materialises CRLF, normalise to the committed + blob (normalized-canonical). Any other difference is rejected. +- Fresh build outputs and CoreLib must match the receipt binaries. +- The existing source-manifest (dirty build) and clean-commit modes are unchanged. + +Negative checks (negative-tests.py, 5/5): wrong build-source hash, wrong parent, internally +inconsistent parent, wrong binary hash and an unexpected dirty file are rejected before any +mode runs. + ## Limits Native walker status 1 (saturation) and status 2 (walker error) have only From 49fc6df13eca1a681da8b1e32acc0538e1e243ef Mon Sep 17 00:00:00 2001 From: lulan <49630018+ReneeGA2020@users.noreply.github.com> Date: Thu, 12 Nov 2026 19:22:32 +0800 Subject: [PATCH 5/9] Pin the consumed source receipt in verification inputs The committed-source mode read the receipt but never froze it: inputs only included the dirty-mode manifest, so the run could not prove which authoritative receipt it validated. - read the receipt bytes once, parse those bytes, and keep their sha256 and length - add the committed-source receipt to the before/after input pins, alongside the manifest path in the existing dirty mode - reject a receipt that changes between load and pin - write the consumed receipt bytes into the run output as consumed-source-receipt.json and record its sha256/bytes in the summary - re-check the consumed file hash, the copy hash and the runtime commit/parent/ clean state after the run; any drift fails the run - apply the same consumed-bytes identity and post-run re-check to the dirty source-manifest mode without relaxing its existing checks No runtime source, binary, layout or host business code change. Assisted-by: Cindy (https://cindy.app) --- eng/verify-runtime-session-origin.py | 61 ++++++++++++++++++++++++++-- 1 file changed, 57 insertions(+), 4 deletions(-) diff --git a/eng/verify-runtime-session-origin.py b/eng/verify-runtime-session-origin.py index 338c983..55485b2 100644 --- a/eng/verify-runtime-session-origin.py +++ b/eng/verify-runtime-session-origin.py @@ -74,7 +74,8 @@ def runtime_corelib(runtime, build_rid=BUILD_RID): def load_source_manifest(path): """Read the explicit local-experiment manifest: {base, head, files{path: sha256}}.""" - data = json.loads(path.read_text(encoding="utf-8")) + raw = path.read_bytes() + data = json.loads(raw.decode("utf-8")) if not isinstance(data, dict) or set(data) != {"base", "head", "files"}: raise ValueError("Source manifest must contain exactly base, head and files.") base, head, files = data["base"], data["head"], data["files"] @@ -89,7 +90,8 @@ def load_source_manifest(path): raise ValueError("Source manifest entry is malformed: " + repr(name)) if name.startswith("/") or chr(92) in name or ":" in name or ".." in name.split("/"): raise ValueError("Source manifest path escapes the runtime tree: " + name) - return {"base": base, "head": head, "files": dict(files)} + return {"base": base, "head": head, "files": dict(files), + "raw": raw, "sha256": hashlib.sha256(raw).hexdigest()} def worktree_delta(runtime): @@ -143,7 +145,8 @@ def verify_source_manifest(runtime, manifest): def load_committed_receipt(path): - data = json.loads(path.read_text(encoding="utf-8")) + raw = path.read_bytes() + data = json.loads(raw.decode("utf-8")) if not isinstance(data, dict) or data.get("schema") != COMMITTED_SCHEMA: raise ValueError("Unexpected committed-source receipt schema: " + repr(data.get("schema") if isinstance(data, dict) else None)) build, integ, binaries = data.get("buildSource"), data.get("integration"), data.get("binaries") @@ -179,7 +182,9 @@ def load_committed_receipt(path): relations = data.get("relations") or {} if not isinstance(relations, dict): raise ValueError("relations must be an object.") - return {"build": build, "integration": integ, "binaries": dict(binaries), "relations": dict(relations)} + return {"build": build, "integration": integ, "binaries": dict(binaries), + "relations": dict(relations), "raw": raw, + "sha256": hashlib.sha256(raw).hexdigest()} def verify_committed_source(runtime, receipt): @@ -396,12 +401,25 @@ def main(): inputs.update({root / "RuneCLR.slnx", root / "Directory.Build.props"}) if manifest_file is not None: inputs.add(manifest_file) + if receipt_file is not None: + inputs.add(receipt_file) inputs.update(runtime / name for name in published.RUNTIME_FILES) inputs.update(runtime / name for name in runtime_changed) inputs.update(path for path in layout.rglob("*") if path.is_file()) pins = {str(path): digest(path) for path in sorted(inputs)} + if provenance == "committed-integration" and pins.get(str(receipt_file)) != committed_receipt["sha256"]: + raise ValueError("Committed-source receipt changed between load and pin.") + if provenance == "source-manifest" and pins.get(str(manifest_file)) != manifest["sha256"]: + raise ValueError("Source manifest changed between load and pin.") output.mkdir(parents=True) + consumed_copy = None + if provenance == "committed-integration": + consumed_copy = output / "consumed-source-receipt.json" + consumed_copy.write_bytes(committed_receipt["raw"]) + elif provenance == "source-manifest": + consumed_copy = output / "consumed-source-manifest.json" + consumed_copy.write_bytes(manifest["raw"]) environment = dict(os.environ) for key in list(environment): if key.startswith(("DOTNET_", "COMPlus_")): @@ -589,6 +607,40 @@ def gate_refused_before_file(parsed): after = {str(path): digest(path) if path.is_file() else None for path in sorted(inputs)} drift = [path for path in pins if pins[path] != after[path]] + + consumed_identity = None + if provenance == "committed-integration": + consumed_identity = {"kind": "committed-source-receipt", "path": str(receipt_file), + "sha256": committed_receipt["sha256"], + "bytes": len(committed_receipt["raw"]), + "copy": str(consumed_copy)} + elif provenance == "source-manifest": + consumed_identity = {"kind": "source-manifest", "path": str(manifest_file), + "sha256": manifest["sha256"], + "bytes": len(manifest["raw"]), + "copy": str(consumed_copy)} + if consumed_identity is not None: + try: + current_source = consumed_identity["path"] + current_sha = hashlib.sha256(Path(current_source).read_bytes()).hexdigest() + except OSError as error: + failures.append("consumed source receipt is unreadable after the run: " + str(error)) + else: + if current_sha != consumed_identity["sha256"]: + failures.append("consumed source receipt changed during the run") + if consumed_copy is not None: + if hashlib.sha256(consumed_copy.read_bytes()).hexdigest() != consumed_identity["sha256"]: + failures.append("consumed source receipt copy does not match the parsed bytes") + if provenance == "committed-integration": + try: + verify_committed_source(runtime, committed_receipt) + except ValueError as error: + failures.append("runtime state changed during the run: " + str(error)) + elif provenance == "source-manifest": + try: + verify_source_manifest(runtime, manifest) + except ValueError as error: + failures.append("runtime source state changed during the run: " + str(error)) result["modes"] = {name: {"exit": mode["exit"], "parsed": mode["parsed"] is not None, "stdout": mode["stdout"], "stderr": mode["stderr"]} for name, mode in modes.items()} @@ -605,6 +657,7 @@ def gate_refused_before_file(parsed): result["buildSourceHead"] = committed_receipt["build"]["head"] if committed_receipt else None result["integrationCommit"] = committed_receipt["integration"]["commit"] if committed_receipt else None result["sourceRelations"] = source_relations + result["consumedSource"] = consumed_identity result["runtimeSourceManifest"] = str(manifest_file) if manifest_file is not None else None result["localUncommitted"] = provenance == "source-manifest" result["publishedAcceptance"] = provenance == "clean-commit" From 941e456c5704894d288f8f1dfb0ca608c49839b2 Mon Sep 17 00:00:00 2001 From: lulan <49630018+ReneeGA2020@users.noreply.github.com> Date: Thu, 12 Nov 2026 19:24:34 +0800 Subject: [PATCH 6/9] Document receipt pinning and the 696-pin committed run Record that the committed-source receipt is a frozen input (single read, pinned before/after, consumed bytes copied into the run output with its sha256), that a receipt change between load and pin or during the run fails the run, and that the same identity applies to the dirty manifest mode. Add the receipt-drift negative (6/6) and the new stable run: runtime clean at 2077f147, host clean at 49fc6df1, 16 modes, 696 pins (the receipt pin), drift 0. The earlier 695-pin run is kept as history and marked superseded. Docs only; no source, test, policy or runtime change. Assisted-by: Cindy (https://cindy.app) --- .../session-origin-integration-20261112.md | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/docs/validation/session-origin-integration-20261112.md b/docs/validation/session-origin-integration-20261112.md index 32e6566..545ca10 100644 --- a/docs/validation/session-origin-integration-20261112.md +++ b/docs/validation/session-origin-integration-20261112.md @@ -110,9 +110,24 @@ The verifier accepts the committed integration snapshot without moving HEAD: - Fresh build outputs and CoreLib must match the receipt binaries. - The existing source-manifest (dirty build) and clean-commit modes are unchanged. -Negative checks (negative-tests.py, 5/5): wrong build-source hash, wrong parent, internally -inconsistent parent, wrong binary hash and an unexpected dirty file are rejected before any -mode runs. +Negative checks (negative-tests.py, 6/6): wrong build-source hash, wrong parent, internally +inconsistent parent, wrong binary hash, an unexpected dirty file and a receipt rewritten +mid-run are rejected; the first five fail before any mode runs. + +- Committed-source run with the receipt pinned as an input (2026-11-12): host clean at + `49fc6df1`, runtime clean at `2077f147`; matrix + `SESSION_ORIGIN_FIXED COMMITTED_INTEGRATION_SOURCE PASS`, 16 modes, 696 pins + (the extra pin is the consumed receipt), drift 0, consumed receipt sha256 + `c911c665...98b1` with a copy in the run output (matrix-committed2.log, + repro-matrix-committed2/). The earlier 695-pin run is kept as history; it pinned one input + fewer and is superseded by this run. + +Receipt integrity: the verifier reads the receipt bytes once, parses those bytes and pins the +same file before and after the run. The summary records the consumed sha256, byte count and +the in-output copy (`consumed-source-receipt.json`). A receipt that changes between load and +pin is rejected, and a change during the run (or a runtime commit/parent/worktree change) +fails the run; the dirty source-manifest mode gets the same consumed-bytes identity and +post-run re-check without relaxing its existing checks. ## Limits From 46c80a1bcf6bb161adb90a0357c4c66f9b5cf271 Mon Sep 17 00:00:00 2001 From: lulan <49630018+ReneeGA2020@users.noreply.github.com> Date: Thu, 12 Nov 2026 19:46:17 +0800 Subject: [PATCH 7/9] Cover committed-source provenance and normalize fixture line endings - Extract check_consumed_source and check_binary_identity from the verifier so the consumed-receipt drift, copy mismatch and binary mismatch paths are shared with the tests instead of reimplemented. - Extend eng/test-session-origin-provenance.py with a synthetic committed-source fixture: one positive with four raw and one normalized-canonical file, and rejections for wrong build source, wrong parent, inconsistent parent, unexpected dirty tree, binary mismatch, receipt drift and receipt copy mismatch. All cases are tooling fixtures; the historical native negative stays in the artifact evidence. - Convert the two session-origin fixture project files to LF so git diff --check no longer reports trailing CR. This is an integration-only line-ending change: the archived snapshot bytes are preserved in the archive branch and evidence. No runtime source, binary, layout or host policy change. Assisted-by: Cindy (https://cindy.app) --- eng/test-session-origin-provenance.py | 147 +++++++++++++++++- eng/verify-runtime-session-origin.py | 56 +++++-- .../SessionOrigin.Author.csproj | 2 +- .../SessionOrigin.Compiled.csproj | 2 +- 4 files changed, 187 insertions(+), 20 deletions(-) diff --git a/eng/test-session-origin-provenance.py b/eng/test-session-origin-provenance.py index 9dc20e2..28da02b 100644 --- a/eng/test-session-origin-provenance.py +++ b/eng/test-session-origin-provenance.py @@ -5,6 +5,7 @@ product sources are read or written. The published verifier is not involved. """ import argparse +import copy import hashlib import importlib.util import json @@ -16,6 +17,7 @@ NL = chr(10) BACKSLASH = chr(92) +CASES = 0 def load_driver(root): @@ -65,6 +67,8 @@ def manifest_for(repo, base_commit, names): def expect_reject(label, call, contains): + global CASES + CASES += 1 try: call() except ValueError as error: @@ -76,6 +80,8 @@ def expect_reject(label, call, contains): def expect_accept(label, call): + global CASES + CASES += 1 try: call() except Exception as error: @@ -98,6 +104,144 @@ def native_fixture(base): return build, layout, build / "IL/System.Private.CoreLib.dll" + +def committed_fixture(base): + """Synthetic repository: base commit, five-file integration commit, four raw and one canonical build form.""" + repo = base / "committed" + repo.mkdir() + run(repo, "init", "-q", "-b", "main") + run(repo, "config", "core.autocrlf", "true") + names = ["src/f1.cs", "src/f2.cs", "src/f3.cs", "src/f4.cs", "src/f5.cs"] + for index, name in enumerate(names): + (repo / name).parent.mkdir(parents=True, exist_ok=True) + (repo / name).write_bytes(("base-" + str(index) + NL).encode()) + run(repo, "add", "-A") + commit(repo, "base") + base_commit = run(repo, "rev-parse", "HEAD") + for index, name in enumerate(names[:4]): + (repo / name).write_bytes(("built-" + str(index) + chr(13) + NL).encode()) + (repo / names[4]).write_bytes(("built-" + str(4) + NL).encode()) + run(repo, "add", "-A") + commit(repo, "integration") + integration = run(repo, "rev-parse", "HEAD") + # the canonical build form for the fifth file, materialised as CRLF by the checkout; + # refreshing the index clears git's stat-cache entry without changing the blob + (repo / names[4]).write_bytes(("built-" + str(4) + chr(13) + NL).encode()) + run(repo, "add", "--", names[4]) + if run(repo, "status", "--porcelain", "--untracked-files=all"): + raise RuntimeError("fixture worktree is unexpectedly dirty") + canonical = {} + build = {} + relations = {} + for index, name in enumerate(names): + blob = subprocess.run(["git", "-C", str(repo), "show", integration + ":" + name], capture_output=True).stdout + canonical[name] = hashlib.sha256(blob).hexdigest() + if index < 4: + build[name] = digest_bytes(repo / name) + relations[name] = "raw" + else: + build[name] = hashlib.sha256(("built-" + str(4) + NL).encode()).hexdigest() + relations[name] = "normalized-canonical" + receipt = {"schema": "runeclr.session-origin.committed-source.v1", + "buildSource": {"head": base_commit, "files": build}, + "integration": {"commit": integration, "parent": base_commit, "files": canonical}, + "binaries": {"coreclr.dll": "a" * 64, "clrjit.dll": "b" * 64, + "clrinterpreter.dll": "c" * 64, "corerun.exe": "d" * 64, + "System.Private.CoreLib.dll": "e" * 64}, + "relations": relations} + return repo, receipt, integration + + +def committed_cases(base, driver): + failures = [] + repo, receipt, integration = committed_fixture(base) + receipt_path = base / "committed-receipt.json" + receipt_path.write_text(json.dumps(receipt), encoding="utf-8") + loaded = driver.load_committed_receipt(receipt_path) + original_base = driver.RUNTIME_BASE + driver.RUNTIME_BASE = receipt["buildSource"]["head"] + try: + def accept_shape(): + head, changed, relations = driver.verify_committed_source(repo, loaded) + if head != integration or len(changed) != 5: + raise ValueError("unexpected head or changed set") + raw = [p for p, r in relations.items() if r == "raw"] + normalized = [p for p, r in relations.items() if r == "normalized-canonical"] + if len(raw) != 4 or len(normalized) != 1: + raise ValueError("expected four raw and one normalized-canonical relation, got " + repr(relations)) + failures.append(expect_accept("committed source with four raw and one normalized-canonical file", accept_shape)) + + wrong_source = copy.deepcopy(loaded) + first = sorted(wrong_source["build"]["files"])[0] + wrong_source["build"]["files"][first] = "0" * 64 + failures.append(expect_reject("committed wrong build source", + lambda: driver.verify_committed_source(repo, wrong_source), + "do not match the recorded build source")) + + wrong_parent = copy.deepcopy(loaded) + wrong_parent["integration"]["parent"] = "1" * 40 + wrong_parent["build"]["head"] = "1" * 40 + failures.append(expect_reject("committed wrong parent", + lambda: driver.verify_committed_source(repo, wrong_parent), + "must equal the receipt parent")) + + inconsistent = json.loads(json.dumps(receipt)) + inconsistent["integration"]["parent"] = "2" * 40 + receipt_path = base / "inconsistent.json" + receipt_path.write_text(json.dumps(inconsistent), encoding="utf-8") + failures.append(expect_reject("committed inconsistent parent", + lambda: driver.load_committed_receipt(receipt_path), + "must equal buildSource.head")) + + (repo / "UNTRACKED.txt").write_text("dirty" + NL, encoding="utf-8") + failures.append(expect_reject("committed unexpected dirty tree", + lambda: driver.verify_committed_source(repo, loaded), + "must be clean")) + (repo / "UNTRACKED.txt").unlink() + + observed = dict(receipt["binaries"]) + expected = dict(observed) + expected["coreclr.dll"] = "f" * 64 + global CASES + CASES += 1 + problems = driver.check_binary_identity(observed, expected) + failures.append(None if any("Binary hash mismatch for coreclr.dll" in p for p in problems) + else "committed binary mismatch was not detected") + if not any("Binary hash mismatch for coreclr.dll" in p for p in problems): + print("FAIL binary mismatch detection") + else: + print("PASS reject committed binary mismatch") + + drift_path = base / "receipt-drift.json" + drift_path.write_text(json.dumps(receipt), encoding="utf-8") + consumed = driver.load_committed_receipt(drift_path) + copy_path = base / "receipt-copy.json" + copy_path.write_bytes(consumed["raw"]) + drift_path.write_text(json.dumps(receipt, indent=2), encoding="utf-8") + CASES += 1 + drift = driver.check_consumed_source(drift_path, consumed["sha256"], copy_path) + failures.append(None if any("changed during the run" in p for p in drift) + else "receipt drift was not detected") + if not any("changed during the run" in p for p in drift): + print("FAIL receipt drift detection") + else: + print("PASS reject receipt drift") + + other_copy = base / "receipt-copy-other.json" + other_copy.write_text("different" + NL, encoding="utf-8") + CASES += 1 + mismatch = driver.check_consumed_source(drift_path, consumed["sha256"], other_copy) + failures.append(None if any("copy does not match" in p for p in mismatch) + else "receipt copy mismatch was not detected") + if not any("copy does not match" in p for p in mismatch): + print("FAIL receipt copy mismatch detection") + else: + print("PASS reject receipt copy mismatch") + finally: + driver.RUNTIME_BASE = original_base + return failures + + def main(): parser = argparse.ArgumentParser() parser.add_argument("--workdir", type=Path) @@ -178,8 +322,9 @@ def main(): "build output is absent")) (build / "clrinterpreter.dll").write_bytes(b"built-clrinterpreter.dll") + failures.extend(committed_cases(base, driver)) failures = [failure for failure in failures if failure] - print("CASES", 16, "FAILURES", len(failures)) + print("CASES", CASES, "FAILURES", len(failures)) for failure in failures: print("FAILED", failure) if failures: diff --git a/eng/verify-runtime-session-origin.py b/eng/verify-runtime-session-origin.py index 55485b2..805b197 100644 --- a/eng/verify-runtime-session-origin.py +++ b/eng/verify-runtime-session-origin.py @@ -141,6 +141,40 @@ def verify_source_manifest(runtime, manifest): return head, sorted(manifest["files"]) +def check_consumed_source(path, consumed_sha256, copy_path=None): + """Return problems when the consumed receipt/manifest bytes no longer match.""" + problems = [] + try: + current = hashlib.sha256(Path(path).read_bytes()).hexdigest() + except OSError as error: + problems.append("consumed source receipt is unreadable after the run: " + str(error)) + current = None + if current is not None and current != consumed_sha256: + problems.append("consumed source receipt changed during the run") + if copy_path is not None: + try: + copy_sha = hashlib.sha256(Path(copy_path).read_bytes()).hexdigest() + except OSError as error: + problems.append("consumed source receipt copy is unreadable: " + str(error)) + else: + if copy_sha != consumed_sha256: + problems.append("consumed source receipt copy does not match the parsed bytes") + return problems + + +def check_binary_identity(observed, expected): + """Return problems when the fresh build outputs differ from the receipt binaries.""" + problems = [] + if set(observed) != set(expected): + problems.append("Committed-source receipt binary set does not match the fresh build outputs: " + + repr(sorted(expected)) + " vs " + repr(sorted(observed))) + for name in sorted(set(observed) & set(expected)): + if observed[name] != expected[name]: + problems.append("Binary hash mismatch for " + name + ": expected " + expected[name] + ", found " + observed[name] + ".") + return problems + + + COMMITTED_SCHEMA = "runeclr.session-origin.committed-source.v1" @@ -371,12 +405,9 @@ def main(): raise ValueError("Private layout host is absent: " + str(private_host)) native = verify_native_identity(runtime_build, layout, args.framework_version, corelib) if provenance == "committed-integration": - if set(committed_receipt["binaries"]) != set(native): - raise ValueError("Committed-source receipt binary set does not match the fresh build outputs: " - + repr(sorted(committed_receipt["binaries"])) + " vs " + repr(sorted(native))) - for name, expected in sorted(committed_receipt["binaries"].items()): - if native[name] != expected: - raise ValueError("Binary hash mismatch for " + name + ": expected " + expected + ", found " + native[name] + ".") + binary_problems = check_binary_identity(native, committed_receipt["binaries"]) + if binary_problems: + raise ValueError(binary_problems[0]) harness = root / "tests/RuneCLR.SessionOrigin.Tests/bin/Release/net11.0/RuneCLR.SessionOrigin.Tests.dll" fixtures = harness.parent / "session-origin" @@ -620,17 +651,8 @@ def gate_refused_before_file(parsed): "bytes": len(manifest["raw"]), "copy": str(consumed_copy)} if consumed_identity is not None: - try: - current_source = consumed_identity["path"] - current_sha = hashlib.sha256(Path(current_source).read_bytes()).hexdigest() - except OSError as error: - failures.append("consumed source receipt is unreadable after the run: " + str(error)) - else: - if current_sha != consumed_identity["sha256"]: - failures.append("consumed source receipt changed during the run") - if consumed_copy is not None: - if hashlib.sha256(consumed_copy.read_bytes()).hexdigest() != consumed_identity["sha256"]: - failures.append("consumed source receipt copy does not match the parsed bytes") + failures.extend(check_consumed_source(consumed_identity["path"], consumed_identity["sha256"], + consumed_copy)) if provenance == "committed-integration": try: verify_committed_source(runtime, committed_receipt) diff --git a/tests/SessionOrigin.Author/SessionOrigin.Author.csproj b/tests/SessionOrigin.Author/SessionOrigin.Author.csproj index bc00fda..b2eb933 100644 --- a/tests/SessionOrigin.Author/SessionOrigin.Author.csproj +++ b/tests/SessionOrigin.Author/SessionOrigin.Author.csproj @@ -3,4 +3,4 @@ - + diff --git a/tests/SessionOrigin.Compiled/SessionOrigin.Compiled.csproj b/tests/SessionOrigin.Compiled/SessionOrigin.Compiled.csproj index 6c5e81f..6b512ec 100644 --- a/tests/SessionOrigin.Compiled/SessionOrigin.Compiled.csproj +++ b/tests/SessionOrigin.Compiled/SessionOrigin.Compiled.csproj @@ -1 +1 @@ - + From af7a59b257700c37ac314f6d6d394328a2e6b811 Mon Sep 17 00:00:00 2001 From: lulan <49630018+ReneeGA2020@users.noreply.github.com> Date: Thu, 12 Nov 2026 19:49:25 +0800 Subject: [PATCH 8/9] Update session-origin validation with upstream results and run table - Replace the pending upstream statement with the focused dotnet/runtime results (ContextualReflection, CollectibleAssemblies/Statics, GenericVirtualMethod pass with origin-scan off; the switch-on contextual run is parsing-only, not enforcement). - Key runs by provenance, host commit and consumed source sha instead of pin count; explain the self-declared receipt versus build-log provenance boundary and the four-raw/one-normalized basis. - Describe the 24-case tooling suite and the integration-only LF normalization of the two fixture project files. - Separate PR base resolution from the verifier's historical ancestor check. Docs only; no source, runtime or policy change. Assisted-by: Cindy (https://cindy.app) --- .../session-origin-integration-20261112.md | 128 +++++++++--------- 1 file changed, 65 insertions(+), 63 deletions(-) diff --git a/docs/validation/session-origin-integration-20261112.md b/docs/validation/session-origin-integration-20261112.md index 545ca10..00fa808 100644 --- a/docs/validation/session-origin-integration-20261112.md +++ b/docs/validation/session-origin-integration-20261112.md @@ -54,80 +54,77 @@ Three distinct relations, not one: `assemblynative.hpp`, `ILLink.Descriptors.Shared.xml`); `qcallentrypoints.cpp` differs because the registration is re-applied onto the newer dev upstream; `RuneCLRReflectionPolicy.cs` differs by the deliberate default-off change. The archived experiment is not overwritten. -- Host: 14 of 16 archived source/test files are byte-identical; `PrototypeReflectionPolicy.cs` - (fail-closed message) and `eng/verify-runtime-session-origin.py` (default-off mode and startup - switch wiring) changed intentionally. - -Upstream targeted tests from `dotnet/runtime` are owned by a separate worker and are still pending; -this branch does not claim them. +- Host: 12 of 16 archived source/test files are byte-identical. Four changed intentionally: + `PrototypeReflectionPolicy.cs` (fail-closed message), `eng/verify-runtime-session-origin.py` + (default-off mode and startup switch wiring), and the two session-origin fixture project files, + whose line endings were normalized to LF for the integration (integration-only change; the + archived snapshot bytes stay on the archive branch). + +Upstream focused tests (dotnet/runtime, matching Checked runtime, default origin-scan off): +ContextualReflection, CollectibleAssemblies/Statics and CollectibleAssemblies/GenericVirtualMethod +pass (expected 100, actual 100). A switch-on ContextualReflection run also succeeds; because those +upstream tests do not attach a RuneCLR session, that run shows only switch parsing, not enforcement. +Enforcement evidence remains the committed host matrix below. The full run identity and logs are in +the focused-upstream report recorded with the session-origin integration evidence. ## Measured on 2026-11-12 -- Runtime build: `build.cmd -subset clr -configuration Checked -arch x64` on - `474eb8ad7230122cc0d34318b25d673da63fd138` plus the five source files below; - SDK `12.0.100-alpha.1.26480.102`, CMake 4.2.1, MSVC 19.44.35229.0; build exit 0. -- Private binary identities: `coreclr.dll` - `42933493ffeb20c2b97baca808afc7d5f3d1cadeaa48354e2502a5f77134d7d9`, - `clrjit.dll` `dd432f9e7f4383be0e352a76ed592a47cb0b1831d89371fe1e25e2c666b13898`, - `clrinterpreter.dll` `83b2f6bbb25f95e01904788b7fb9d8d44a3b594e96bde2b626d8869350ef56b3`, - `corerun.exe` `cda4877d19e629ba8175cc55fa4553e4006f87cc53e26a3c054f93a62198b7ee`, - `System.Private.CoreLib.dll` `578089837a4d6695e0b581827bb50e21d8e005a546e926c136511d9bb5311fae`. -- Private layout: fresh Checked native output and CoreLib over the `runeclr/dev` - Release managed framework (same source pin); the matrix verifies the native - outputs and CoreLib against the fresh build hashes. -- Matrix: `SESSION_ORIGIN_FIXED LOCAL_UNCOMMITTED_SOURCE_MANIFEST PASS`, - 16 modes, 696 pins, runtime head `474eb8ad` plus the five uncommitted source - files; `default-off` reports capability 0, `capability-on` reports bit 0, and both - host gates reject before author loading with `PlatformNotSupportedException`. -- Host stock runtime: `REFLECTION_POLICY_STOCK_TESTS PASS` and the lifecycle suite - `156/156`. - -- Host toolchain: `dotnet` version 11.0.100 (the repository has no - global.json; the SDK resolution was captured in local evidence and the build re-run explicitly). The rebuilt host - outputs embed the source revision `9505faa6...`; the pre-commit outputs embedded the previous - revision and are kept as history. -- Post-commit re-run with the rebuilt harness: matrix 16 modes / 696 pins PASS, stock reflection - PASS, lifecycle 156/156 (evidence: matrix-postcommit.log and repro-matrix-postcommit/). - -- Committed-source run (2026-11-12, no HEAD switching): runtime clean at `2077f147`, - host clean at `24e5a8fa`; matrix - `SESSION_ORIGIN_FIXED COMMITTED_INTEGRATION_SOURCE PASS`, 16 modes, 695 pins, - drift 0 (matrix-committed.log, repro-matrix-committed/). The earlier dev+dirty run - (696 pins) is kept as history and is no longer the committed-source result. +Toolchain and binary identities: +- Runtime build: `build.cmd -subset clr -configuration Checked -arch x64` on `474eb8ad` plus the five + source files; SDK 12.0.100-alpha.1.26480.102, CMake 4.2.1, MSVC 19.44.35229.0; build exit 0. +- Private binaries: coreclr `42933493...`, clrjit `dd432f9e...`, clrinterpreter `83b2f6bb...`, + corerun `cda4877d...`, CoreLib `57808983...`. +- Host toolchain: `dotnet` 11.0.100 (ambient SDK; no global.json). Rebuilt host outputs embed the + source revision; earlier outputs are history. +- Private layout reuses the dev Release managed framework (same source pin); the matrix verifies + the native outputs and CoreLib against the fresh build hashes. + +### Runs (keyed by provenance, host commit and consumed source sha) + +| provenance | host commit | consumed source | modes | pins | drift | output | +| --- | --- | --- | --- | --- | --- | --- | +| source-manifest (dirty build) | cbac2a6 + dirty | manifest `49609720...` | 16 | 696 | 0 | repro-matrix/ | +| committed-integration | 49fc6df1 | receipt `c911c665...` | 16 | 695 | 0 | repro-matrix-committed/ | +| committed-integration | 49fc6df1 | receipt `c911c665...` | 16 | 696 | 0 | repro-matrix-committed2/ | +| committed-integration | 46c80a1b | receipt `c911c665...` | 16 | 696 | 0 | repro-matrix-final/ | + +The pin count is not the acceptance key: 695 vs 696 reflects whether the consumed receipt was +included in the frozen inputs. The final row is the current result; earlier rows are kept as history. + +Receipt consistency versus build-log provenance: the receipt is a self-declared tooling input. +This branch freezes its bytes (single read, before/after pin, in-output copy) and verifies the +declared relations to the current commit and to the fresh binaries, but it cannot re-prove from the +receipt alone that a specific build produced those binaries. That part rests on the recorded build +log, the build-source head and the binary hashes. The mixed basis is deliberate: four runtime files +were built from their raw (CRLF) bytes and RuneCLRReflectionPolicy.cs from its canonical (LF) form; +the verifier records each as raw or normalized-canonical. ## Committed-source verification The verifier accepts the committed integration snapshot without moving HEAD: -- `--runtime-committed-source ` requires a clean runtime worktree at the - receipt commit, that commit's exact parent, and a modification-only delta equal to the - receipt's five files. -- The receipt records the build source (parent commit plus raw file hashes that were built), - the integration commit (canonical Git blob hashes) and the private runtime binary hashes. -- Per file the working bytes must equal the recorded build source (raw) or, when the build - read the canonical form while the checkout materialises CRLF, normalise to the committed - blob (normalized-canonical). Any other difference is rejected. +- `--runtime-committed-source ` requires a clean runtime worktree at the receipt + commit, that commit's exact parent, and a modification-only delta equal to the receipt's five files. +- The receipt records the build source (parent commit plus raw file hashes that were built), the + integration commit (canonical Git blob hashes) and the private runtime binary hashes. +- Per file the working bytes must equal the recorded build source (raw) or, when the build read the + canonical form while the checkout materialises CRLF, normalise to the committed blob + (normalized-canonical). Any other difference is rejected. - Fresh build outputs and CoreLib must match the receipt binaries. - The existing source-manifest (dirty build) and clean-commit modes are unchanged. -Negative checks (negative-tests.py, 6/6): wrong build-source hash, wrong parent, internally -inconsistent parent, wrong binary hash, an unexpected dirty file and a receipt rewritten -mid-run are rejected; the first five fail before any mode runs. - -- Committed-source run with the receipt pinned as an input (2026-11-12): host clean at - `49fc6df1`, runtime clean at `2077f147`; matrix - `SESSION_ORIGIN_FIXED COMMITTED_INTEGRATION_SOURCE PASS`, 16 modes, 696 pins - (the extra pin is the consumed receipt), drift 0, consumed receipt sha256 - `c911c665...98b1` with a copy in the run output (matrix-committed2.log, - repro-matrix-committed2/). The earlier 695-pin run is kept as history; it pinned one input - fewer and is superseded by this run. - -Receipt integrity: the verifier reads the receipt bytes once, parses those bytes and pins the -same file before and after the run. The summary records the consumed sha256, byte count and -the in-output copy (`consumed-source-receipt.json`). A receipt that changes between load and -pin is rejected, and a change during the run (or a runtime commit/parent/worktree change) -fails the run; the dirty source-manifest mode gets the same consumed-bytes identity and -post-run re-check without relaxing its existing checks. +Tooling tests: `eng/test-session-origin-provenance.py` (24 cases) builds synthetic repositories and +covers both provenance modes: a positive with four raw and one normalized-canonical file, and +rejections for wrong build source, wrong parent, inconsistent parent, unexpected dirty tree, binary +mismatch, receipt drift and receipt copy mismatch. The historical real-tree native negative remains +in the artifact evidence. + +Receipt integrity: the verifier reads the receipt bytes once, parses those bytes and pins the same +file before and after the run. The summary records the consumed sha256, byte count and the in-output +copy (`consumed-source-receipt.json`). A receipt that changes between load and pin is rejected, and a +change during the run (or a runtime commit/parent/worktree change) fails the run; the dirty +source-manifest mode gets the same consumed-bytes identity and post-run re-check without relaxing +its existing checks. ## Limits @@ -136,3 +133,8 @@ indirect coverage; the compiled-only suppressed child has no author frame; unmanaged entry, AOT/mobile, global all-load and the published walk-cost bound remain out of scope. No sandbox, native/R2R body, device or unload claim is made by this branch. + +Review-tool note: the verifier's `RUNTIME_BASE` (7cbdad39) is a historical ancestor check for +provenance, not the PR base. PR base resolution is a separate explicit merge-base and diff check +against the target branch (`origin/main` for the host, fork `runeclr/dev` for the runtime), because the +verifier has no `--base` option. The two checks answer different questions. From b0401f1f95c61b307083d54efaf905341fe7ca80 Mon Sep 17 00:00:00 2001 From: lulan <49630018+ReneeGA2020@users.noreply.github.com> Date: Thu, 12 Nov 2026 20:16:02 +0800 Subject: [PATCH 9/9] Record lifecycle runs on the matching patched runtime Record separate ordinary and RequireInterpreter lifecycle passes on the matching private runtime, with unchanged binary identities. Documentation only; no build or epoch performance claim. Assisted-by: Cindy (https://cindy.app) --- docs/validation/session-origin-integration-20261112.md | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/docs/validation/session-origin-integration-20261112.md b/docs/validation/session-origin-integration-20261112.md index 00fa808..23509a4 100644 --- a/docs/validation/session-origin-integration-20261112.md +++ b/docs/validation/session-origin-integration-20261112.md @@ -67,6 +67,14 @@ upstream tests do not attach a RuneCLR session, that run shows only switch parsi Enforcement evidence remains the committed host matrix below. The full run identity and logs are in the focused-upstream report recorded with the session-origin integration evidence. +The RuneCLR lifecycle harness also passes **156/156** on the matching private Checked runtime +in each of two separate processes: ordinary runtime selection and explicit `--require-interpreter`. +Both runs use the existing Release host harness from host source `46c80a1b`; the runtime source +stays clean at `2077f147` and all five private runtime binary hashes match the committed-source +receipt. These lifecycle runs are separate from the stock-SDK regression and the origin-scan +enforcement matrix. Their evidence is recorded under `lead-patched-lifecycle/` in the integration +artifact (default/required logs, launch receipts and binary identities). + ## Measured on 2026-11-12 Toolchain and binary identities: @@ -84,7 +92,7 @@ Toolchain and binary identities: | provenance | host commit | consumed source | modes | pins | drift | output | | --- | --- | --- | --- | --- | --- | --- | | source-manifest (dirty build) | cbac2a6 + dirty | manifest `49609720...` | 16 | 696 | 0 | repro-matrix/ | -| committed-integration | 49fc6df1 | receipt `c911c665...` | 16 | 695 | 0 | repro-matrix-committed/ | +| committed-integration | 24e5a8fa | receipt `c911c665...` | 16 | 695 | 0 | repro-matrix-committed/ | | committed-integration | 49fc6df1 | receipt `c911c665...` | 16 | 696 | 0 | repro-matrix-committed2/ | | committed-integration | 46c80a1b | receipt `c911c665...` | 16 | 696 | 0 | repro-matrix-final/ |