镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Commit cff13e9

Browse files
committed
test(vm): force exact execution tiers
Add test-only tree-walker and required-bytecode execution contracts. Required mode fails closed when a program or called plain function lacks compiled bytecode, so a fallback can no longer masquerade as VM coverage. Differential witnesses compare successful result/state and exception/message/pre-throw side effects, with admission counters proving both paths.\n\nVerified focused Debug and TSan witnesses, admission tests, BunPress manifest/build, docs links, and the full 1,474-test suite across six shards with zero failures or leaks.\n\nFlips 0 test262 cases; production contexts retain automatic admission.
1 parent 64bacae commit cff13e9

4 files changed

Lines changed: 172 additions & 6 deletions

File tree

‎docs/.data/bunpress-output-v1.json‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@
5050
},
5151
{
5252
"path": "architecture/index.html",
53-
"bytes": 167349,
54-
"sha256": "5d7933efe37b5e8c451133eabd2cb7ef903e8151aabb4ee623f4fd4594a4e056"
53+
"bytes": 167853,
54+
"sha256": "e9543567b57fa59396b1cec7b8db136ad46a4b9719b92bfc73ff0e25c8585d53"
5555
},
5656
{
5757
"path": "baseline-jit/index.html",
@@ -264,5 +264,5 @@
264264
"sha256": "036254fd0141573e60c6a927672b5dbf853a640eaa5d1dbbe9a7bd74c9ad6783"
265265
}
266266
],
267-
"tree_sha256": "0f59606cf1329e4cbe49229f39ecd66c1df3ea400d46a852756bfa5ec3130635"
267+
"tree_sha256": "e6ce1bf1613e109e8fd923b966a8a46eee4ae5d2449d33fabf10973da5fa7e79"
268268
}

‎docs/architecture.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ explicit target for removal. Source-policy skips and null nested-template chunks
4343
retain their exact causal subreason; legacy generic counters remain in the stable
4444
schema but new decisions do not increment them.
4545

46+
Test harnesses can create a context with `TestingOptions.bytecode_execution_mode`
47+
set to `tree_walker` or `required`. The former suppresses bytecode for plain
48+
synchronous code; the latter throws an `InternalError` if a program or called
49+
plain function lacks compiled bytecode. Differential tests can therefore prove
50+
both tier witnesses explicitly instead of treating a silent fallback as VM
51+
coverage. Production contexts always use automatic admission.
52+
4653
## Source map
4754

4855
| File | Responsibility |

‎src/context.zig‎

Lines changed: 142 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2751,6 +2751,8 @@ pub const Context = struct {
27512751
/// owner along with its arena heap. Standalone contexts leave this null.
27522752
shared_jit_owner: ?*jit.Owner = null,
27532753
enable_jit: bool = true,
2754+
/// Test-only forced execution contract. Public contexts remain automatic.
2755+
bytecode_execution_mode: interp.BytecodeExecutionMode = .automatic,
27542756
/// Reusable allocator used as the GC heap's cell backing. It recycles
27552757
/// per-cell slabs for allocation/lifecycle performance and becomes
27562758
/// internally locked under `parallel_gc`, so multiple mutators can allocate
@@ -3303,6 +3305,10 @@ pub const Context = struct {
33033305
/// of the stable embedder options surface.
33043306
pub const TestingOptions = struct {
33053307
enable_jit: bool = true,
3308+
/// Force plain synchronous execution through one tier for exact
3309+
/// differentials. `required` throws instead of silently accepting a
3310+
/// compiler or template fallback as VM coverage.
3311+
bytecode_execution_mode: interp.BytecodeExecutionMode = .automatic,
33063312
enable_threads: bool = false,
33073313
enable_gc: bool = false,
33083314
concurrent_gc: bool = false,
@@ -3872,6 +3878,7 @@ pub const Context = struct {
38723878
.jit_owner = jit.Owner.init(allocator),
38733879
.shared_jit_owner = primary.shared_jit_owner orelse &primary.jit_owner,
38743880
.enable_jit = primary.enable_jit,
3881+
.bytecode_execution_mode = primary.bytecode_execution_mode,
38753882
.owner_thread = primary.owner_thread,
38763883
.sab_retains = .{ .gpa = allocator },
38773884
.env = .{ .arena = a, .fn_scope = true },
@@ -3960,6 +3967,7 @@ pub const Context = struct {
39603967
primary.assertOwnerThread();
39613968
return createWithTestingOptionsForSharedOwner(primary.host_gpa, .{
39623969
.enable_jit = primary.enable_jit,
3970+
.bytecode_execution_mode = primary.bytecode_execution_mode,
39633971
.enable_gc = true,
39643972
.wasm_features = primary.wasm_features,
39653973
}, primary);
@@ -4035,6 +4043,7 @@ pub const Context = struct {
40354043
.jit_owner = jit.Owner.init(context_gpa),
40364044
.shared_jit_owner = if (shared_owner) |owner| owner.shared_jit_owner orelse &owner.jit_owner else null,
40374045
.enable_jit = options.enable_jit,
4046+
.bytecode_execution_mode = options.bytecode_execution_mode,
40384047
.owner_thread = std.Thread.getCurrentId(),
40394048
.sab_retains = .{ .gpa = context_gpa },
40404049
.env = .{ .arena = a, .fn_scope = true }, // global is a variable scope
@@ -4282,6 +4291,7 @@ pub const Context = struct {
42824291
.profile_statement_ctx = self.profile_statement_ctx,
42834292
.profile_statement_hook = self.profile_statement_hook,
42844293
.bytecode_admission_inventory = &self.bytecode_admission_inventory,
4294+
.bytecode_execution_mode = self.bytecode_execution_mode,
42854295
.debug_statement_locations = &self.debug_statement_locations,
42864296
.debug_registry_lock = if (self.parallel_js) &self.debug_registry_lock else null,
42874297
.debug_script_ctx = self,
@@ -7223,13 +7233,21 @@ pub const Context = struct {
72237233
}
72247234

72257235
const outcome: interp.EvalError!value.Value = admission: {
7236+
if (self.bytecode_execution_mode == .tree_walker) {
7237+
self.bytecode_admission_inventory.record(.program_forced_tree_walker);
7238+
break :admission machine.eval(prog);
7239+
}
72267240
if (self.debug_statement_hook != null) {
72277241
self.bytecode_admission_inventory.record(.program_debugger);
7242+
if (self.bytecode_execution_mode == .required)
7243+
break :admission machine.throwError("InternalError", "required bytecode program is blocked by debugger execution");
72287244
break :admission machine.eval(prog);
72297245
}
7230-
if (scriptTreeWalkPolicy(owned_source)) |reason| {
7231-
self.bytecode_admission_inventory.record(reason);
7232-
break :admission machine.eval(prog);
7246+
if (self.bytecode_execution_mode != .required) {
7247+
if (scriptTreeWalkPolicy(owned_source)) |reason| {
7248+
self.bytecode_admission_inventory.record(reason);
7249+
break :admission machine.eval(prog);
7250+
}
72337251
}
72347252
switch (compiler.Compiler.admitProgram(a, prog) catch return error.OutOfMemory) {
72357253
.compiled => |chunk| {
@@ -7238,6 +7256,8 @@ pub const Context = struct {
72387256
},
72397257
.rejected => |reason| {
72407258
self.bytecode_admission_inventory.record(interp.programRejectionAdmission(reason));
7259+
if (self.bytecode_execution_mode == .required)
7260+
break :admission machine.throwError("InternalError", "required bytecode program was rejected by the compiler");
72417261
break :admission machine.eval(prog);
72427262
},
72437263
}
@@ -18174,6 +18194,125 @@ test "bytecode admission source policies retain causal reasons" {
1817418194
for (cases) |case| try std.testing.expectEqual(case.reason, Context.scriptTreeWalkPolicy(case.source));
1817518195
}
1817618196

18197+
test "forced tree-walker and required bytecode preserve exact observable state" {
18198+
const source =
18199+
\\globalThis.tierTrace = [];
18200+
\\function tierDifferential(seed) {
18201+
\\ "use strict";
18202+
\\ var box = { value: seed };
18203+
\\ var trace = [];
18204+
\\ function bump(value) {
18205+
\\ box.value = box.value + value;
18206+
\\ trace.push(box.value);
18207+
\\ return box.value;
18208+
\\ }
18209+
\\ var sum = 0;
18210+
\\ for (var i = 0; i < 16; i = i + 1)
18211+
\\ sum = sum + bump((i & 3) + 1);
18212+
\\ globalThis.tierTrace = trace.join(",");
18213+
\\ globalThis.tierBox = box.value;
18214+
\\ return sum + box.value;
18215+
\\}
18216+
\\tierDifferential(7);
18217+
;
18218+
18219+
const tree_ctx = try Context.createWithTestingOptions(std.testing.allocator, .{
18220+
.enable_jit = false,
18221+
.bytecode_execution_mode = .tree_walker,
18222+
});
18223+
defer tree_ctx.destroy();
18224+
const bytecode_ctx = try Context.createWithTestingOptions(std.testing.allocator, .{
18225+
.enable_jit = false,
18226+
.bytecode_execution_mode = .required,
18227+
});
18228+
defer bytecode_ctx.destroy();
18229+
18230+
const tree_result = try tree_ctx.evaluate(source);
18231+
const bytecode_result = try bytecode_ctx.evaluate(source);
18232+
try std.testing.expectEqual(tree_result.rawBits(), bytecode_result.rawBits());
18233+
try std.testing.expectEqualStrings(
18234+
(try tree_ctx.evaluate("tierTrace + '|' + tierBox")).asStr(),
18235+
(try bytecode_ctx.evaluate("tierTrace + '|' + tierBox")).asStr(),
18236+
);
18237+
18238+
const tree = tree_ctx.bytecodeAdmissionSnapshot();
18239+
try std.testing.expectEqual(@as(u64, 2), tree.count(.program_forced_tree_walker));
18240+
try std.testing.expectEqual(@as(u64, 2), tree.count(.plain_forced_tree_walker));
18241+
try std.testing.expectEqual(@as(u64, 0), tree.count(.program_compiled));
18242+
18243+
const bytecode = bytecode_ctx.bytecodeAdmissionSnapshot();
18244+
try std.testing.expectEqual(@as(u64, 2), bytecode.count(.program_compiled));
18245+
try std.testing.expectEqual(@as(u64, 2), bytecode.count(.template_plain_compiled));
18246+
try std.testing.expectEqual(@as(u64, 0), bytecode.count(.program_forced_tree_walker));
18247+
try std.testing.expectEqual(@as(u64, 0), bytecode.count(.template_plain_fallback));
18248+
}
18249+
18250+
test "required bytecode rejects an uncompiled function instead of counting fallback coverage" {
18251+
const ctx = try Context.createWithTestingOptions(std.testing.allocator, .{
18252+
.enable_jit = false,
18253+
.bytecode_execution_mode = .required,
18254+
});
18255+
defer ctx.destroy();
18256+
18257+
try std.testing.expectError(error.Throw, ctx.evaluate(
18258+
\\function needsParameterPrologue(value = 1) { return value; }
18259+
\\needsParameterPrologue();
18260+
));
18261+
const exception = ctx.exception orelse return error.TestUnexpectedResult;
18262+
try std.testing.expectEqualStrings("InternalError", exception.asObj().errorName());
18263+
try std.testing.expectEqualStrings(
18264+
"required bytecode function has no compiled chunk",
18265+
exception.asObj().getOwn("message").?.asStr(),
18266+
);
18267+
const inventory = ctx.bytecodeAdmissionSnapshot();
18268+
try std.testing.expectEqual(@as(u64, 1), inventory.count(.program_compiled));
18269+
try std.testing.expectEqual(@as(u64, 1), inventory.count(.template_plain_rejected_parameter_prologue));
18270+
try std.testing.expectEqual(@as(u64, 0), inventory.count(.template_plain_fallback));
18271+
}
18272+
18273+
test "forced tree-walker and required bytecode preserve exception and side effects" {
18274+
const source =
18275+
\\globalThis.tierBeforeThrow = 0;
18276+
\\function tierThrow(seed) {
18277+
\\ "use strict";
18278+
\\ globalThis.tierBeforeThrow = seed * 2;
18279+
\\ throw new TypeError("tier-boom");
18280+
\\}
18281+
\\tierThrow(9);
18282+
;
18283+
const modes = [_]interp.BytecodeExecutionMode{ .tree_walker, .required };
18284+
var side_effects: [modes.len]u64 = undefined;
18285+
18286+
for (modes, 0..) |mode, index| {
18287+
const ctx = try Context.createWithTestingOptions(std.testing.allocator, .{
18288+
.enable_jit = false,
18289+
.bytecode_execution_mode = mode,
18290+
});
18291+
defer ctx.destroy();
18292+
18293+
try std.testing.expectError(error.Throw, ctx.evaluate(source));
18294+
const exception = ctx.exception orelse return error.TestUnexpectedResult;
18295+
try std.testing.expectEqualStrings("TypeError", exception.asObj().errorName());
18296+
try std.testing.expectEqualStrings("tier-boom", exception.asObj().getOwn("message").?.asStr());
18297+
side_effects[index] = (try ctx.evaluate("tierBeforeThrow")).rawBits();
18298+
18299+
const inventory = ctx.bytecodeAdmissionSnapshot();
18300+
switch (mode) {
18301+
.tree_walker => {
18302+
try std.testing.expectEqual(@as(u64, 2), inventory.count(.program_forced_tree_walker));
18303+
try std.testing.expectEqual(@as(u64, 1), inventory.count(.plain_forced_tree_walker));
18304+
},
18305+
.required => {
18306+
try std.testing.expectEqual(@as(u64, 2), inventory.count(.program_compiled));
18307+
try std.testing.expectEqual(@as(u64, 1), inventory.count(.template_plain_compiled));
18308+
},
18309+
.automatic => unreachable,
18310+
}
18311+
}
18312+
18313+
try std.testing.expectEqual(side_effects[0], side_effects[1]);
18314+
}
18315+
1817718316
test "bytecode admission inventory merges concurrent realm records" {
1817818317
if (builtin.single_threaded) return error.SkipZigTest;
1817918318
var inventory = interp.BytecodeAdmissionInventory{};

‎src/interpreter.zig‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -915,6 +915,15 @@ pub fn vmChunkAllowsInlineCalls(chunk: *const bc.Chunk) bool {
915915
return true;
916916
}
917917

918+
/// Test-harness execution contract. `required` never counts a tree-walker
919+
/// fallback as VM coverage; `tree_walker` suppresses bytecode for plain
920+
/// synchronous functions while retaining generator/async suspend machinery.
921+
pub const BytecodeExecutionMode = enum {
922+
automatic,
923+
tree_walker,
924+
required,
925+
};
926+
918927
/// Stable runtime admission outcomes. These names are profiling schema: append
919928
/// new reasons instead of renaming an existing one without a versioned migration.
920929
pub const BytecodeAdmissionReason = enum(u8) {
@@ -960,6 +969,8 @@ pub const BytecodeAdmissionReason = enum(u8) {
960969
program_policy_nested_function_declaration,
961970
template_plain_rejected_parameter_prologue,
962971
template_plain_rejected_unsupported_lowering,
972+
program_forced_tree_walker,
973+
plain_forced_tree_walker,
963974
};
964975

965976
pub const bytecode_admission_reason_count = std.meta.fieldNames(BytecodeAdmissionReason).len;
@@ -1239,6 +1250,9 @@ pub const Interpreter = struct {
12391250
/// Realm-owned monotonic admission counts. Atomic because no-GIL workers
12401251
/// can create functions concurrently in one Context.
12411252
bytecode_admission_inventory: ?*BytecodeAdmissionInventory = null,
1253+
/// Test-only forced tier contract inherited from the Context. Production
1254+
/// contexts always use `automatic`.
1255+
bytecode_execution_mode: BytecodeExecutionMode = .automatic,
12421256
debug_statement_locations: ?*std.AutoHashMapUnmanaged(*const Node, DebugStatementLocation) = null,
12431257
/// Non-null only for a no-GIL shared realm. Script publication mutates one
12441258
/// Context-owned registry while every interpreter reads it at statement
@@ -5000,6 +5014,8 @@ pub const Interpreter = struct {
50005014
},
50015015
.rejected => |reason| admission_reason = asyncRejectionAdmission(reason),
50025016
}
5017+
} else if (self.bytecode_execution_mode == .tree_walker) {
5018+
admission_reason = .plain_forced_tree_walker;
50035019
} else if (self.debug_statement_hook != null) {
50045020
admission_reason = .plain_policy_debugger;
50055021
} else if (plainFunctionPolicyRejection(fnode)) |reason| {
@@ -6521,6 +6537,8 @@ pub const Interpreter = struct {
65216537
return throwClassConstructorCallError(self, func);
65226538
if (!func.is_generator and !func.is_async) {
65236539
if (func.chunk) |fchunk| return vm.runFunction(self, func, fchunk, args, this_val, new_target);
6540+
if (self.bytecode_execution_mode == .required)
6541+
return self.throwError("InternalError", "required bytecode function has no compiled chunk");
65246542
}
65256543
var cur_func = func;
65266544
var cur_args = args;
@@ -6537,6 +6555,8 @@ pub const Interpreter = struct {
65376555
if (next_func.is_generator or next_func.is_async or next_func.is_class_constructor)
65386556
return self.callValue(tail.callee, tail.args);
65396557
if (next_func.chunk) |fchunk| return vm.runFunction(self, next_func, fchunk, tail.args, Value.undef(), Value.undef());
6558+
if (self.bytecode_execution_mode == .required)
6559+
return self.throwError("InternalError", "required bytecode function has no compiled chunk");
65406560
cur_func = next_func;
65416561
cur_args = tail.args;
65426562
cur_this = Value.undef();

0 commit comments

Comments
 (0)