From 599676b89a61aaa567fb6bab996b6591aee49eb0 Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Sun, 1 Nov 2026 14:21:35 -0800 Subject: [PATCH 01/49] AOT: the checks of slot types go by a plan for each layout Under the checking modes alone. Holding every slot of every object that is still alive against its type was 20 to 23% of all user instructions of the TypeScript-compiler benchmark under checked proofs: for each field anew, whether it is a brand or an accessor, where it lies, its type and its range; and over half of the visits were to fields that the object doesn't hold. All of that is the same for every object of a layout, so a plan is made at the first object of a layout. The plan says only whether a slot is outside its type; the old walk then names it, so a report reads as before. Nothing changes at default flags. --- .../aot/runtime/AOTRuntimeValidation.cpp | 145 +++++++++++++++++- .../aot/runtime/AOTRuntimeValidation.h | 37 +++++ .../aot/tests/validation/slots-by-plan.ir | 64 ++++++++ 3 files changed, 245 insertions(+), 1 deletion(-) create mode 100644 Source/JavaScriptCore/aot/tests/validation/slots-by-plan.ir diff --git a/Source/JavaScriptCore/aot/runtime/AOTRuntimeValidation.cpp b/Source/JavaScriptCore/aot/runtime/AOTRuntimeValidation.cpp index 5e89ed231cefa..442d3e31b4ec0 100644 --- a/Source/JavaScriptCore/aot/runtime/AOTRuntimeValidation.cpp +++ b/Source/JavaScriptCore/aot/runtime/AOTRuntimeValidation.cpp @@ -40,6 +40,7 @@ #include "Reg.h" #include "SubspaceInlines.h" #include +#include #include #include #include @@ -642,7 +643,12 @@ static constexpr auto traitsOfEveryRepresentation = [] { return traits; }(); -std::optional findSlotOutsideType(VM& vm, const Object& object) +#if COMPILER(CLANG) +#pragma clang attribute push(__attribute__((minsize)), apply_to = function) +#endif + +// Field by field, in the order of the layout. It names the slot once the plan has found one, so a report reads the same whatever the order of the plan is. +static NEVER_INLINE std::optional findSlotOutsideTypeFieldByField(VM& vm, const Object& object) { State& state = object.state(); const LayoutData& layout = object.layout(); @@ -704,6 +710,143 @@ std::optional findSlotOutsideType(VM& vm, const Object& object) return std::nullopt; } +static NEVER_INLINE const SlotPlan& makePlan(RecheckedCells& rechecked, State& state, const LayoutData& layout) +{ + auto plan = makeUnique(); + auto checkOf = [&](const FieldData& field) { + SlotRepresentation representation = field.slot.representation(); + const RuntimeType& type = state.objects->layouts.typeOf(field); + OptionSet primitives = type.primitiveSet(); + int64_t least = representation == SlotRepresentation::OptionalInt32 ? minInlineInt32 : std::numeric_limits::min(); + int64_t greatest = std::numeric_limits::max(); + if (auto range = type.integerRange()) { + least = std::max(least, range->first); + greatest = std::min(greatest, range->second); + } + if (!primitives.contains(PrimitiveSet::Integer) || least > greatest) { + least = 1; + greatest = 0; + } + return SlotCheck { + .field = &field, + .type = &type, + .traits = &traitsOfEveryRepresentation[static_cast(representation)], + .leastInteger = static_cast(least), + .greatestInteger = static_cast(greatest), + .offset = field.slot.offset, + .representation = representation, + .admitsUndefined = field.slotHoldsUndefinedAndNull || primitives.contains(PrimitiveSet::Undefined), + .admitsNull = field.slotHoldsUndefinedAndNull || primitives.contains(PrimitiveSet::Null), + }; + }; + // A brand is a class token, and what the slot of an accessor holds is not the value of the property. No type describes either. + auto isDescribed = [](const FieldData& field) { + return !field.isBrand() && !field.isAccessor(); + }; + Vector fieldsOfExtension; + for (const FieldData& field : layout.fields()) { + if (!isDescribed(field)) + continue; + if (field.isOutOfLine()) + fieldsOfExtension.append(&field); + else + plan->checks.append(checkOf(field)); + } + plan->numberInCell = plan->checks.size(); + std::ranges::stable_sort(fieldsOfExtension, { }, [](const FieldData* field) { + return granuleOf(field->slot.offset); + }); + for (const FieldData* field : fieldsOfExtension) { + unsigned granule = granuleOf(field->slot.offset); + SlotPlan::Range& range = plan->checksOfGranule[granule]; + if (!(plan->granules & granuleBitOf(field->slot.offset))) { + plan->granules |= granuleBitOf(field->slot.offset); + range.begin = static_cast(plan->checks.size()); + } + plan->checks.append(checkOf(*field)); + range.end = static_cast(plan->checks.size()); + } + RELEASE_ASSERT(plan->checks.size() <= std::numeric_limits::max()); + return *rechecked.plans.add(&layout, WTF::move(plan)).iterator->value; +} + +#if COMPILER(CLANG) +#pragma clang attribute pop +#endif + +static ALWAYS_INLINE const SlotPlan& planOf(VM& vm, State& state, const LayoutData& layout) +{ + if (!vm.aotState().recheckedCells) [[unlikely]] + vm.aotState().recheckedCells = makeUnique(); + RecheckedCells& rechecked = *vm.aotState().recheckedCells; + if (rechecked.layoutAskedLast == &layout) [[likely]] + return *rechecked.planAskedLast; + auto found = rechecked.plans.find(&layout); + const SlotPlan& plan = found == rechecked.plans.end() ? makePlan(rechecked, state, layout) : *found->value; + rechecked.layoutAskedLast = &layout; + rechecked.planAskedLast = &plan; + return plan; +} + +// What findSlotOutsideTypeFieldByField() asks of one field, in the same order. +static ALWAYS_INLINE bool isOutsideType(VM& vm, State& state, const Object& object, const SlotCheck& check, const void* slot) +{ + const SlotTraits& traits = *check.traits; + uint64_t bits = readSlotBits(check.representation, slot); + if (bits == (traits.hasAbsent ? traits.absentBits : 0)) + return false; + if ((traits.hasUndefined && bits == traits.undefinedBits) || (traits.hasNull && bits == traits.nullBits)) + return !(bits == traits.undefinedBits ? check.admitsUndefined : check.admitsNull); + if (check.representation == SlotRepresentation::Int32 || check.representation == SlotRepresentation::OptionalInt32) { + int32_t integer = static_cast(bits); + return integer < check.leastInteger || integer > check.greatestInteger; + } + if (traits.valueKind == SlotValueKind::LiteralString && isLiteralStringWord(bits)) + return !slotMayHoldLiteralString(*state.instance.program, *check.type, bits); + if (!isFormOfValue(vm, check.representation, traits, bits, cageBaseOf(&object))) + return true; + JSValue value = traits.valueKind == SlotValueKind::DeferredClosure ? JSValue(cellInSlot(check.representation, slot, cageBaseOf(&object))) : readSlot(check.representation, slot, *state.strings); + if (!value) + return false; + if (value.isCell() && value.asCell()->type() == CompiledEnvironmentType) + return false; + return !slotMayHold(state, *check.type, value); +} + +static ALWAYS_INLINE bool hasSlotOutsideType(VM& vm, const Object& object) +{ + State& state = object.state(); + const SlotPlan& plan = planOf(vm, state, object.layout()); + std::span checks = plan.checks.span(); + for (const SlotCheck& check : checks.first(plan.numberInCell)) { + if (isOutsideType(vm, state, object, check, &object.at(check.offset))) [[unlikely]] + return true; + } + if (!plan.granules) + return false; + Extension* extension = object.extension(); + if (!extension) + return false; + // Only what the Extension of this object holds. + for (uint64_t held = extension->granuleBits() & plan.granules; held; held &= held - 1) { + SlotPlan::Range range = plan.checksOfGranule[std::countr_zero(held)]; + for (const SlotCheck& check : checks.subspan(range.begin, range.end - range.begin)) { + if (isOutsideType(vm, state, object, check, extension->slot(check.offset))) [[unlikely]] + return true; + } + } + return false; +} + +std::optional findSlotOutsideType(VM& vm, const Object& object) +{ + bool hasOne = hasSlotOutsideType(vm, object); + ASSERT(hasOne == !!findSlotOutsideTypeFieldByField(vm, object)); + if (!hasOne) [[likely]] + return std::nullopt; + return findSlotOutsideTypeFieldByField(vm, object); +} + #if COMPILER(CLANG) #pragma clang attribute push(__attribute__((minsize)), apply_to = function) #endif diff --git a/Source/JavaScriptCore/aot/runtime/AOTRuntimeValidation.h b/Source/JavaScriptCore/aot/runtime/AOTRuntimeValidation.h index 41aa1211894e7..56c9336355ff6 100644 --- a/Source/JavaScriptCore/aot/runtime/AOTRuntimeValidation.h +++ b/Source/JavaScriptCore/aot/runtime/AOTRuntimeValidation.h @@ -8,22 +8,27 @@ #if ENABLE(AOT) #include "AOTRecordTypes.h" +#include "AOTSlotRepresentation.h" #include "CollectionScope.h" #include "JSCJSValue.h" #include "MarkedBlock.h" #include "VM.h" #include +#include #include #include #include #include #include +#include namespace JSC { namespace AOT { class Object; enum class Entry : uint16_t; struct FieldData; +struct LayoutData; +struct RuntimeType; using Typed::ValidationFailureKind; @@ -128,6 +133,34 @@ OperationPermissions permissionsOf(Entry); using CollectionVersions = std::tuple; CollectionVersions collectionVersionsOf(VM&); +// What is asked of a slot is the same for every object of a layout: whether a type describes it at all, where it lies, whether the type admits the markers for undefined and null, +// and between which integers a number lies. It is worked out once, at the first object of the layout that is looked at. +struct SlotCheck { + const FieldData* field; + const RuntimeType* type; + const SlotTraits* traits; + // Of a slot for integers. Empty where the type admits none. + int32_t leastInteger; + int32_t greatestInteger; + uint32_t offset; + SlotRepresentation representation; + bool admitsUndefined; + bool admitsNull; +}; + +struct SlotPlan { + WTF_DEPRECATED_MAKE_STRUCT_FAST_ALLOCATED(SlotPlan); + struct Range { + uint16_t begin { 0 }; + uint16_t end { 0 }; + }; + // Those of the cell, then those of the Extension, by granule. + Vector checks; + uint32_t numberInCell { 0 }; + uint64_t granules { 0 }; + std::array checksOfGranule { }; +}; + struct RecheckedCells { WTF_DEPRECATED_MAKE_STRUCT_FAST_ALLOCATED(RecheckedCells); struct OfBlock { @@ -137,6 +170,10 @@ struct RecheckedCells { }; UncheckedKeyHashMap> blocks; uint64_t numberOfCollections { 0 }; + // A layout is data of an image, and so are the types of its fields: a plan never goes stale. + UncheckedKeyHashMap> plans; + const LayoutData* layoutAskedLast { nullptr }; + const SlotPlan* planAskedLast { nullptr }; }; struct CollectionValidator { diff --git a/Source/JavaScriptCore/aot/tests/validation/slots-by-plan.ir b/Source/JavaScriptCore/aot/tests/validation/slots-by-plan.ir new file mode 100644 index 0000000000000..27cebe830f379 --- /dev/null +++ b/Source/JavaScriptCore/aot/tests/validation/slots-by-plan.ir @@ -0,0 +1,64 @@ +# interface Coin { value: number } interface Box { required: number; kind: 1 | 2; content: Coin; late0?: Coin .. late15?: Coin } +# What a collection asks of the slots of an object is worked out once for its layout: the slots of the cell first, then those of the Extension, and of those only what this object holds. +# A writer that nobody listed (`storeInSlotWithoutTest` of the shell) is found on each road: a slot of the Extension, an integer that the type does not list, and undefined where the type has none. +# The report names the field as it did when the fields were asked one by one. With two slots at fault it names the first in the order of the layout: late3, of the Extension, though late9, of the cell, is looked at first. +# And nothing is found where nothing is wrong: with the Extension filled by ordinary stores, and with no Extension at all. +# FORM: AsRead +# RUN(SLOTS): plan-slots layouts +# RUN(EXTENSION): run-in-shell globalThis.register = (box, bare, coin, stone) => { storeInSlotWithoutTest(box, "late12", stone); print("stored"); gc(); print("collected"); }; +# RUN(INTEGER): run-in-shell globalThis.register = (box, bare, coin, stone) => { storeInSlotWithoutTest(box, "kind", 7); print("stored"); gc(); print("collected"); }; +# RUN(MARKER): run-in-shell globalThis.register = (box, bare, coin, stone) => { storeInSlotWithoutTest(box, "content", undefined); print("stored"); gc(); print("collected"); }; +# RUN(TWO): run-in-shell globalThis.register = (box, bare, coin, stone) => { storeInSlotWithoutTest(box, "late9", stone); storeInSlotWithoutTest(box, "late3", stone); print("stored"); gc(); print("collected"); }; +# RUN(SOUND): run-in-shell globalThis.register = (box, bare, coin, stone) => { box.late3 = coin; box.late12 = coin; gc(); edenGC(); print("collected", box.late9.value, box.late12.value, bare.late9); }; +# OPTIONS: testing=CheckProofsAtRunTime testing=NoLayoutIsMadeSeldom +# SLOTS: "kind" OptionalInt32 @ +# SLOTS: "late3" JSValue out@ +# SLOTS: "late9" JSValue @ +# SLOTS: "late12" JSValue out@ +# EXTENSION: stored +# EXTENSION-NOT: collected +# EXTENSION: (late12) of layout {{*}}, holds a value outside its type +# EXTENSION: AOT validation: HeapSlotType f- v- +# EXTENSION: crashed with +# INTEGER: stored +# INTEGER-NOT: collected +# INTEGER: (kind) of layout {{*}}, a slot of the kind OptionalInt32 at {{*}}, holds a value outside its type +# INTEGER: AOT validation: HeapSlotType f- v- +# INTEGER: crashed with +# MARKER: stored +# MARKER-NOT: collected +# MARKER: (content) of layout {{*}}, holds a value outside its type +# MARKER: AOT validation: HeapSlotType f- v- +# MARKER: crashed with +# TWO: stored +# TWO-NOT: collected +# TWO: (late3) of layout {{*}}, holds a value outside its type +# TWO: AOT validation: HeapSlotType f- v- +# TWO: crashed with +# SOUND-NOT: AOT validation +# SOUND: collected 1.5 1.5 undefined +# SOUND-NEXT: exited with 0 + +module m0 path="main.ts" displayPath="main.ts" body=f0 +entry module=m0 +layout L0 name="Box" fields=[{key="required" declared={type=number} flags=[IsRequired]}, {key="kind" declared={type=1|2} flags=[IsRequired]}, {key="content" declared={type=L1} flags=[IsRequired]}, {key="late0" declared={type=L1}}, {key="late1" declared={type=L1}}, {key="late2" declared={type=L1}}, {key="late3" declared={type=L1}}, {key="late4" declared={type=L1}}, {key="late5" declared={type=L1}}, {key="late6" declared={type=L1}}, {key="late7" declared={type=L1}}, {key="late8" declared={type=L1}}, {key="late9" declared={type=L1}}, {key="late10" declared={type=L1}}, {key="late11" declared={type=L1}}, {key="late12" declared={type=L1}}, {key="late13" declared={type=L1}}, {key="late14" declared={type=L1}}, {key="late15" declared={type=L1}}] +layout L1 name="Coin" fields=[{key="value" declared={type=number} flags=[IsRequired]}] +layout L2 name="Stone" fields=[{key="weight" declared={type=number} flags=[IsRequired]}] +function f0 name="main" kind=Module results=[{type=anything}] { + v0 = Double number=1.5 + v1 = NewObject -, v0 layout=L1 fields=[0] + v2 = FinishObject v1 + v3 = Integer integer=1 + v4 = NewObject -, v0, v3, v2 layout=L0 fields=[0, 1, 2] + v5 = FinishObject v4 + PropSet v5, v2 key="late9" + v7 = NewObject -, v0, v3, v2 layout=L0 fields=[0, 1, 2] + v8 = FinishObject v7 + v9 = Double number=2.5 + v10 = NewObject -, v9 layout=L2 fields=[0] + v11 = FinishObject v10 + v12 = Builtin builtin=GlobalThis + v13 = PropGet v12 key="register" [isFreeName] + v14 = Call v13, -, v5, v8, v2, v11 + Return +} From e5951877a64996b104cc1cb7a1364f0b1a19971b Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Sun, 1 Nov 2026 14:21:35 -0800 Subject: [PATCH 02/49] AOT: the file of what the flow analysis hands on says why Behind "a build can write down what the flow analysis hands on": the line of a layout ends with one column more, why the analysis could not rule more out. A tool for testing: nothing changes where the word is unset. --- Source/JavaScriptCore/aot/valueflow/AOTFlowApply.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/JavaScriptCore/aot/valueflow/AOTFlowApply.cpp b/Source/JavaScriptCore/aot/valueflow/AOTFlowApply.cpp index e64d3d30d55b3..baf0655e3d402 100644 --- a/Source/JavaScriptCore/aot/valueflow/AOTFlowApply.cpp +++ b/Source/JavaScriptCore/aot/valueflow/AOTFlowApply.cpp @@ -1356,7 +1356,7 @@ static void writeFlowFacts(const Program& program, const FlowResults& results, c name = makeString(name, '~', pathOf(key.module), '~', key.callNumber.value_or(0)); out.print(static_cast(key.kind), ":", makeStringByReplacingAll(name, ' ', '_'), " "); } - out.println(); + out.println("\t", static_cast(flow.cause)); } } From cce9fc8b361b67b3c3282857d924cec0b815f8da Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Sun, 1 Nov 2026 14:21:35 -0800 Subject: [PATCH 03/49] AOT: a call of Math.random() is not removed or run twice `setRandomSeed()` from `bun:jsc` makes the sequence of Math.random() reproducible, so a program can count the calls. The compiler believed that nobody could: the call was declared to write only the runtime's own state. function roll(use) { const r = Math.random(); return use ? r : -1; } Where `use` is false the call was removed, and every later number differed from `bun run`. The same happened to a bare `Math.random();`, to an argument that the callee does not read and to a property that nobody reads. The call could also run twice. A typed function that fails a type test before it has done anything visible starts again in its generic version: function f(holder: Holder) { const r = Math.random(); const inner = holder.inner; return inner.x + inner.y + r; } With an `inner` that is not laid out as a Point, `r` was the second number. Math.random() now also reads and writes external state, as Date.now() does. Two tests had the old effects written into them and are restated. --- Source/JavaScriptCore/aot/builtins/math.builtins.ts | 4 ++-- Source/JavaScriptCore/aot/ir/schema/inline-builtins.def | 2 +- .../aot/tests/ir/effects/calls-of-builtins-by-value.ir | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Source/JavaScriptCore/aot/builtins/math.builtins.ts b/Source/JavaScriptCore/aot/builtins/math.builtins.ts index bca0f3671f68d..c7b5ddfd86392 100644 --- a/Source/JavaScriptCore/aot/builtins/math.builtins.ts +++ b/Source/JavaScriptCore/aot/builtins/math.builtins.ts @@ -68,8 +68,8 @@ export default b.declarations( random: { returns: b.f64, capability: "none", - reads: ["RuntimeState"], - writes: ["RuntimeState"], + reads: ["RuntimeState", "ExternalState"], + writes: ["RuntimeState", "ExternalState"], implementation: b.inline("MathRandom"), }, round: rounding("MathRound"), diff --git a/Source/JavaScriptCore/aot/ir/schema/inline-builtins.def b/Source/JavaScriptCore/aot/ir/schema/inline-builtins.def index 251a1f59e3e88..b7ccfe0e32754 100644 --- a/Source/JavaScriptCore/aot/ir/schema/inline-builtins.def +++ b/Source/JavaScriptCore/aot/ir/schema/inline-builtins.def @@ -50,7 +50,7 @@ inline MathMinOfArray MathMaxOfArray ; reads(ArrayStorage, Elements[Array]) ; fa inline CallNumberConstructor NumberToString ParseInt ; asOverload ; family MathBuiltin inline NumberToStringAppend ; asOverload ; family MathBuiltin ; formOnly inline MathAbs MathFloor MathCeil MathTrunc MathRound MathFround MathF16Round MathSqrt MathPow MathMin MathMax MathImul MathClz32 MathSign MathFunction(MathFunctionKind) MathATan2 MathHypot IsNaN IsFinite NumberIsNaN NumberIsFinite NumberIsInteger NumberIsSafeInteger ; none ; family MathBuiltin ; optionalSlowPath -inline MathRandom ; UsesRuntime ; family MathBuiltin ; optionalSlowPath +inline MathRandom ; UsesRuntime reads(ExternalState) writes(ExternalState) ; family MathBuiltin ; optionalSlowPath inline DateGetField(DateField, TimeZone) DateToISOString ; asOverload ; family DateBuiltin inline DateNow ; reads(ExternalState) writes(ExternalState) ; family DateBuiltin ; optionalSlowPath inline DateGetTime DateGetMilliseconds ; reads(InternalFields) ; family DateBuiltin ; optionalSlowPath diff --git a/Source/JavaScriptCore/aot/tests/ir/effects/calls-of-builtins-by-value.ir b/Source/JavaScriptCore/aot/tests/ir/effects/calls-of-builtins-by-value.ir index 0b33bf4f5ee97..0401f563ae08f 100644 --- a/Source/JavaScriptCore/aot/tests/ir/effects/calls-of-builtins-by-value.ir +++ b/Source/JavaScriptCore/aot/tests/ir/effects/calls-of-builtins-by-value.ir @@ -27,11 +27,11 @@ function f2 name="calls" parameters=[{type=builtin(MathRandom)}, {type=builtin(A Return } # CHECK-LABEL: function f2 -# CHECK-NEXT: v9 Call: reads(RuntimeState) writes(RuntimeState) +# CHECK-NEXT: v9 Call: reads(ExternalState, RuntimeState) writes(ExternalState, RuntimeState) # CHECK-NEXT: v10 Call: reads(StringStorage, AllocatorState, RuntimeState) writes(StringStorage, AllocatorState, RuntimeState) throws collects # CHECK-NEXT: v11 Call: {{*}}callsUnknownCode # CHECK-NEXT: v12 Call: {{*}}callsUnknownCode # CHECK-NEXT: v13 Call: writes(ModuleVariables/1) # CHECK-NEXT: v14 Call: {{*}}callsUnknownCode -# CHECK-NEXT: v15 Call: reads(RuntimeState) writes(ModuleVariables/1, RuntimeState) +# CHECK-NEXT: v15 Call: reads(ExternalState, RuntimeState) writes(ModuleVariables/1, ExternalState, RuntimeState) # CHECK-NEXT: v16 Call: {{*}}callsUnknownCode From 7719ee914738a6aa638279b603976ce876635c79 Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Sun, 1 Nov 2026 14:21:35 -0800 Subject: [PATCH 04/49] AOT: the refusal of a load by a dynamic path names the flag too "Cannot load '': this executable can't load a module from a dynamic path. Pass the file to bun compile as another entry point" now goes on ", or build with --aot-allow-code-from-disk", as the other refusal of a load does. Words alone. Goes with Bun's half. --- Source/JavaScriptCore/aot/runtime/AOTErrorMessages.cpp | 5 ++++- Source/JavaScriptCore/aot/tests/AOTClosedWorldTests.cpp | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/Source/JavaScriptCore/aot/runtime/AOTErrorMessages.cpp b/Source/JavaScriptCore/aot/runtime/AOTErrorMessages.cpp index 20a59cef633b8..a3489ad134a14 100644 --- a/Source/JavaScriptCore/aot/runtime/AOTErrorMessages.cpp +++ b/Source/JavaScriptCore/aot/runtime/AOTErrorMessages.cpp @@ -3300,7 +3300,7 @@ JSObject* createLoadByNameRefusedError(JSGlobalObject* globalObject, StringView return createErrorWithCode(globalObject, withAOTErrorNote(makeString("Cannot load '"_s, name, '\'', asker.isEmpty() ? ""_s : " from '"_s, asker, asker.isEmpty() ? ""_s : "'"_s, ": it's compiled into this test run, but no test file imports it by a constant path, so only compiled code reaches it. Import it by a constant path in a test file."_s)), code); if (!isModuleOfBun && image && hasFlag(image->header().options.admitted(), AdmittedCode::ReplacedBuiltins)) return createErrorWithCode(globalObject, withAOTErrorNote(makeString("Cannot load '"_s, name, "': the build saw no import() or require() of a computed path, so compiled files answer only to the paths it saw. It can't see code from a string, as in eval() or new Function(). An executable refuses this too. Import the file by a constant path."_s)), code); - ASCIILiteral advice = isModuleOfBun ? "The compiler couldn't tell at build time that this call reaches a module loader. Call require(), import() or process.getBuiltinModule() directly."_s : "Pass the file to bun compile as another entry point."_s; + ASCIILiteral advice = isModuleOfBun ? "The compiler couldn't tell at build time that this call reaches a module loader. Call require(), import() or process.getBuiltinModule() directly."_s : "Pass the file to bun compile as another entry point, or build with --aot-allow-code-from-disk."_s; return createErrorWithCode(globalObject, withAOTErrorNote(makeString("Cannot load '"_s, name, "': this executable can't load a module from a dynamic path. "_s, advice)), code); } @@ -3411,6 +3411,9 @@ String callOfBuiltinChangedBeforeFirstUseMessage(const String& property) String assignmentThatBuildDidNotSeeMessage(const String& property) { + // Who wrote a file that is loaded from disk can't show the build anything. + if (const Image* image = Image::current(); image && image->loadsCodeOutsideImage()) + return printForFuzzilli(withAOTErrorNote(makeString("Cannot assign to "_s, property, " because built-in objects are read-only in a compiled executable, and the build saw no assignment to this one. Code that is loaded from disk can replace it only where the executable's own code assigns to it too, spelled out as in "_s, property, " = value"_s))); return printForFuzzilli(withAOTErrorNote(makeString("Cannot assign to "_s, property, " because built-in objects are read-only in a compiled executable, and the build didn't see your code assign to this one. Spell out the object and the name where it does, as in "_s, property, " = value"_s))); } diff --git a/Source/JavaScriptCore/aot/tests/AOTClosedWorldTests.cpp b/Source/JavaScriptCore/aot/tests/AOTClosedWorldTests.cpp index 07b4e7def4797..2c5c7c89f9798 100644 --- a/Source/JavaScriptCore/aot/tests/AOTClosedWorldTests.cpp +++ b/Source/JavaScriptCore/aot/tests/AOTClosedWorldTests.cpp @@ -613,7 +613,7 @@ static void testScriptShadowsNoConstantOfCompiledCode() checkShellOutput(readsGlobalsAfterScripts, expectedOutput, declareInScripts, { "validate"_s }); } -#define AOT_LOAD_BY_NAME_REFUSED "Cannot load '/other.js': this executable can't load a module from a dynamic path. Pass the file to bun compile as another entry point. (bun compile differs from bun run here: https://bun.com/docs/compiler/compatibility)\n" +#define AOT_LOAD_BY_NAME_REFUSED "Cannot load '/other.js': this executable can't load a module from a dynamic path. Pass the file to bun compile as another entry point, or build with --aot-allow-code-from-disk. (bun compile differs from bun run here: https://bun.com/docs/compiler/compatibility)\n" #define AOT_LOADER_CHANGE_REFUSED ": imports in this executable were resolved at build time, so the module loader can't be patched. (bun compile differs from bun run here: https://bun.com/docs/compiler/compatibility)\n" From 7a23cbfb64bb5513a73cfbb8acdcd5936475661c Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Sun, 1 Nov 2026 14:21:35 -0800 Subject: [PATCH 05/49] AOT: native code that runs out of stack in an executable is reported Native code that ran out of stack in an executable built with --aot ended the process with SIGSEGV, nothing on stderr, no crash report, and a raw terminal left raw. Bun's crash handler registers an alternate stack; the engine then installed its own actions for SIGSEGV and SIGBUS over it without SA_ONSTACK, and only the thread that started had an alternate stack. A thread that has none gets one, and the two actions take it. Goes with Bun's half. --- .../aot/image/AOTFaultHandler.h | 3 + .../aot/image/AOTFaultTable.cpp | 64 +++++++++++++++++++ .../aot/runtime/AOTInstance.cpp | 2 + 3 files changed, 69 insertions(+) diff --git a/Source/JavaScriptCore/aot/image/AOTFaultHandler.h b/Source/JavaScriptCore/aot/image/AOTFaultHandler.h index 877534f7d8d82..ee3b9f94e0d7c 100644 --- a/Source/JavaScriptCore/aot/image/AOTFaultHandler.h +++ b/Source/JavaScriptCore/aot/image/AOTFaultHandler.h @@ -14,6 +14,9 @@ namespace JSC { namespace AOT { void installFaultHandler(); +// A thread that has run out of stack has none left for whoever reports that. +void takeFaultsOfThisThreadOnAnotherStack(); + // Whether compiled code raised this access fault on purpose. If so, the registers continue where that code handles it. JS_EXPORT_PRIVATE bool handleAccessFault(void* faultingAddress, PlatformRegisters&); diff --git a/Source/JavaScriptCore/aot/image/AOTFaultTable.cpp b/Source/JavaScriptCore/aot/image/AOTFaultTable.cpp index 1834601a3d13e..21521b34e1db3 100644 --- a/Source/JavaScriptCore/aot/image/AOTFaultTable.cpp +++ b/Source/JavaScriptCore/aot/image/AOTFaultTable.cpp @@ -33,6 +33,7 @@ #include "MachineContext.h" #include "VM.h" #include +#include #include #include #include @@ -41,6 +42,11 @@ #include #include +#if OS(UNIX) +#include +#include +#endif + WTF_ALLOW_UNSAFE_BUFFER_USAGE_BEGIN namespace JSC::AOT { @@ -526,6 +532,64 @@ void installFaultHandler() }); } +#if OS(UNIX) +namespace { + +struct SignalStackOfThread { + static constexpr size_t size = 512 * KB; + + ~SignalStackOfThread() + { + if (!memory) + return; + stack_t none { }; + none.ss_flags = SS_DISABLE; + sigaltstack(&none, nullptr); + munmap(memory, size); + } + + void map() + { + void* mapped = mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0); + if (mapped == MAP_FAILED) + return; + stack_t stack { }; + stack.ss_sp = mapped; + stack.ss_size = size; + if (sigaltstack(&stack, nullptr)) { + munmap(mapped, size); + return; + } + memory = mapped; + } + + void* memory { nullptr }; +}; + +} // anonymous namespace + +void takeFaultsOfThisThreadOnAnotherStack() +{ + stack_t current { }; + if (!sigaltstack(nullptr, ¤t) && (current.ss_flags & SS_DISABLE)) { + WTF_THREAD_LOCAL_ALWAYS_DESTROY static thread_local SignalStackOfThread stack; + stack.map(); + } + static std::atomic areActionsChanged { false }; + if (areActionsChanged.exchange(true, std::memory_order_relaxed)) + return; + for (int signal : { SIGSEGV, SIGBUS }) { + struct sigaction action; + if (sigaction(signal, nullptr, &action) || !(action.sa_flags & SA_SIGINFO) || (action.sa_flags & SA_ONSTACK)) + continue; + action.sa_flags |= SA_ONSTACK; + sigaction(signal, &action, nullptr); + } +} +#else +void takeFaultsOfThisThreadOnAnotherStack() { } +#endif + } // namespace JSC::AOT WTF_ALLOW_UNSAFE_BUFFER_USAGE_END diff --git a/Source/JavaScriptCore/aot/runtime/AOTInstance.cpp b/Source/JavaScriptCore/aot/runtime/AOTInstance.cpp index 0b5fd6318febd..4a27b62c5daf0 100644 --- a/Source/JavaScriptCore/aot/runtime/AOTInstance.cpp +++ b/Source/JavaScriptCore/aot/runtime/AOTInstance.cpp @@ -25,6 +25,7 @@ #include "AOTCompileOptions.h" #include "AOTCoroutineTable.h" #include "AOTEffectsDeclared.h" +#include "AOTFaultHandler.h" #include "AOTFunctionTables.h" #include "AOTGenerator.h" #include "AOTGlobalBindings.h" @@ -646,6 +647,7 @@ Instance* Instance::create(JSGlobalObject* realm) { VM& vm = realm->vm(); RELEASE_ASSERT(vm.hasCompiledCode()); + takeFaultsOfThisThreadOnAnotherStack(); DeferGCForAWhile deferGC(vm); destroyDeadInstances(vm); if (auto& functionTables = vm.aotState().functionTables) From e7903242eef732710140314cf5636d3e9dff2388 Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Sun, 1 Nov 2026 14:21:35 -0800 Subject: [PATCH 06/49] AOT: seven mends in the run time's modules and mocks - `mock.module("./m.js", () => { calls++; imports.push(import("./m.js")); throw ..; })`, then two `import()`s of m.js one behind the other. Plain asks the factory each time (`failed 2`, `failed 3`); compiled as one program, the second was answered with the first's error and nothing was called, without a sign. - A mock that works, laid over the one that failed, then `import()` by a name that the build can't read: plain `mock`; compiled, rejected with the first error. - a.test: `mock.module("node:os", () => ({ EOL: "mock EOL" }))`, then a graph that ends in node:os and passes through barrel.mjs. b.test: `mock.module("./barrel.mjs", () => ({ own: "mock own" }))`, then `import { own } from "./outer.mjs"`. Plain: `mock own`. Over two layers: `SyntaxError: Export named 'own' not found in module outer.mjs`. - main: `try { require("./file.cjs") } catch {}`; file.cjs: `import("./file.cjs").then(.., error => console.log(error.message)); throw new RangeError("thrown by the file")`. `bun run` takes the file out of require.cache at the throw, so the import() runs the file again and is rejected with what the second run throws. Compiled: rejected with the first error, one run. - A plugin of the run answers for `virtual:aliased` in onResolve; `await import("virtual:aliased")` twice behind an import statement of the same name. `bun test` asks the plugin at every call; compiled, at the statement alone. - a.test.js: `vi.doMock("./dep.cjs", () => ({ who: "one" })); const held = await import("./held.js")`; held.js: `import { who } from "./dep.cjs"`; b.test.js: `import { read } from "./held.js"`. Plain and one program: `real`. Over layers: `undefined`, though dep.cjs's body ran. - main.cjs: `const a = require("./a.mjs")`; a.mjs: `import("./b.cjs"); export const fromA = ..`; b.cjs: `require("./a.mjs").fromA`. node and `bun build --compile`: "a ends | main has a | b starts | b sees a". Compiled ahead of time, as `bun run`: "a ends | b starts | b sees undefined | main has a". It goes as node and `--compile` now. They share AOTModules.cpp. Goes with Bun's half. --- .../aot/runtime/AOTModuleEvaluation.cpp | 13 +++- .../JavaScriptCore/aot/runtime/AOTModules.cpp | 76 +++++++++++++++++-- .../JavaScriptCore/aot/runtime/AOTModules.h | 7 ++ 3 files changed, 90 insertions(+), 6 deletions(-) diff --git a/Source/JavaScriptCore/aot/runtime/AOTModuleEvaluation.cpp b/Source/JavaScriptCore/aot/runtime/AOTModuleEvaluation.cpp index ad2a0bc4aa59d..97813ca14fe01 100644 --- a/Source/JavaScriptCore/aot/runtime/AOTModuleEvaluation.cpp +++ b/Source/JavaScriptCore/aot/runtime/AOTModuleEvaluation.cpp @@ -614,6 +614,7 @@ void Modules::forgetRunThatThrew(ModuleID file) loaded->numberOfChildrenShown = 0; loaded->firstRequirer = std::nullopt; loaded->synchronousLoadCountAtStart = std::nullopt; + loaded->wasFirstImportedByCallWhileItRan = false; } m_recordedRequires.removeIf([&](const auto& recorded) { return static_cast(recorded.key) == file.index(); @@ -623,6 +624,13 @@ void Modules::forgetRunThatThrew(ModuleID file) } // An import() that was called ahead of the require() has asked the loader for the file, waits for its text, or has queued its first job. +// One that was called while the file ran comes to the file when it has thrown, and runs it again. +bool Modules::isAwaitedByImportFromBeforeItRan(ModuleID file) +{ + LoadedModule* loaded = loadedIfExists(file); + return isAwaitedByImport(file) && !(loaded && loaded->wasFirstImportedByCallWhileItRan); +} + bool Modules::isAwaitedByImport(ModuleID file) { for (ModuleID fileOrFace : { file, m_table.faceOfFile(file).value_or(file) }) { @@ -640,7 +648,7 @@ void Modules::didThrowInBody(ModuleID module, JSValue error) return; // `bun run` takes a CommonJS file whose body throws out of require.cache, and the next require() runs it again. A test run loads it again from disk where its build // has compiled the code that asks for the file for a file that may load twice. - if (data(module).kind == ModuleKind::CommonJs && (mayBeEvicted(module) || (data(module).runsAgainAfterThrow() && !isAwaitedByImport(module))) && status(module) == ModuleStatus::Evaluating && !loaderHasFileThatThrew(module)) [[unlikely]] { + if (data(module).kind == ModuleKind::CommonJs && (mayBeEvicted(module) || (data(module).runsAgainAfterThrow() && !isAwaitedByImportFromBeforeItRan(module))) && status(module) == ModuleStatus::Evaluating && !loaderHasFileThatThrew(module)) [[unlikely]] { if (mayBeEvicted(module)) evict(module); else @@ -2862,6 +2870,9 @@ void Modules::evaluateModulesOwedBelowSlow(ModuleID root, bool hasImporterAboveT m_modulesOwedToImportersThatRan.clear(module.index()); if (status(module) >= ModuleStatus::Evaluating || mockInPlace(module)) continue; + // The importers were linked while the mock stood, so no link has seen to it that the names are taken behind the body. + if (data(module).capturesExportsBehindEvaluation()) + captureExportsBehindEvaluation(module); // The mock kept the loader from what the module imports from outside the image. if (status(module) == ModuleStatus::Unlinked && data(module).reachesUncompiledCode() && !data(module).hasTopLevelAwait()) { evaluateSynchronously(module, SynchronousEvaluationReason::Require); diff --git a/Source/JavaScriptCore/aot/runtime/AOTModules.cpp b/Source/JavaScriptCore/aot/runtime/AOTModules.cpp index d931c1773ac3d..526d3362b66ba 100644 --- a/Source/JavaScriptCore/aot/runtime/AOTModules.cpp +++ b/Source/JavaScriptCore/aot/runtime/AOTModules.cpp @@ -3020,6 +3020,7 @@ bool Modules::runMockThatWaitsSlow(ModuleID module) m_modulesWhoseFactoryRuns.clear(module.index()); if (Exception* exception = scope.exception()) [[unlikely]] { m_modulesWithMockToRun.set(module.index()); + m_loaderMayHoldLoadsThatFactoryEnded = true; if (m_importsMadeByOwnFactory.contains(module) && !m_instance.vm->isTerminationException(exception)) { m_mockedModulesWithErrorKeptByLoader.set(module.index()); ensureLoaded(module).errorOfMockKeptByLoader.set(*this, exception->value()); @@ -3107,15 +3108,57 @@ void Modules::dropErrorsOfLoadsEndedByMockOf(ModuleID mocked) { dropErrorOfMockKeptByLoader(mocked); dropErrorsOfLoadsThatCameTo([&](ModuleID module) { return module == mocked; }); + // A factory that failed waits anew in the middle of the load that it ended: what else of that load asks for the module shares the error. + bool loaderMayHoldLoads = m_loaderMayHoldLoadsThatFactoryEnded && !m_modulesWhoseFactoryRuns.get(mocked.index()) && !isLoaderInMiddleOfGraph(); + if (loaderMayHoldLoads) [[unlikely]] { + m_loaderMayHoldLoadsThatFactoryEnded = false; + takeFailedLoadsOutOfLoader([&](ModuleID module) { return module == mocked; }); + } if (!m_instance.vm->aotState().hasSeveralImages) [[likely]] return; Identifier key = moduleKey(mocked); for (Instance* instance = m_instance.above; instance; instance = instance->above) { - if (Modules* modules = instance->state->modules.get()) - modules->dropErrorsOfLoadsThatCameTo([&](ModuleID module) { return modules->standsForFileWithKey(module, key); }); + Modules* modules = instance->state->modules.get(); + if (!modules) + continue; + modules->dropErrorsOfLoadsThatCameTo([&](ModuleID module) { return modules->standsForFileWithKey(module, key); }); + if (loaderMayHoldLoads) [[unlikely]] + modules->takeFailedLoadsOutOfLoader([&](ModuleID module) { return modules->standsForFileWithKey(module, key); }); } } +// Where the loader itself was asked for the graph, its entries hold the error. +void Modules::takeFailedLoadsOutOfLoader(const ScopedLambda& isMocked) +{ + Vector failed; + for (auto& entry : loader()->moduleMap().values()) { + if (entry->status() < ModuleRegistryEntry::Status::FetchFailed) + continue; + std::optional root = findByKey(entry->key()); + if (!root) { + m_loaderMayHoldLoadsThatFactoryEnded = true; + continue; + } + bool cameToMock = false; + BitVector seen(m_table.size()); + Vector work { *root }; + while (!work.isEmpty() && !cameToMock) { + ModuleID module = work.takeLast(); + cameToMock = isMocked(module); + for (const ModuleRequestData& request : m_table.imports(module)) { + if (!request.isHoisted && !seen.quickSet(request.target())) + work.append(request.targetModule()); + } + } + if (cameToMock) + failed.append(entry->key()); + else + m_loaderMayHoldLoadsThatFactoryEnded = true; + } + for (const Identifier& key : failed) + loader()->removeEntry(key); +} + void Modules::dropErrorsOfLoadsThatCameTo(const ScopedLambda& isMocked) { if (m_modulesWithLoadEndedByMock.isEmpty()) [[likely]] @@ -3341,7 +3384,7 @@ bool Modules::runMocksBelowInImages(ModuleID root, RootOfLoad rootOfLoad) JSValue kept = errorOfLoadEndedByMock(module); if (!kept && isImportedByStatement && m_mockedModulesWithErrorKeptByLoader.get(module.index())) kept = loadedIfExists(module)->errorOfMockKeptByLoader.get(); - if (!kept && !isImportedByStatement && m_mockedModulesWithErrorKeptByLoader.get(module.index()) && m_importsMadeByOwnFactory.removeFirst(module)) [[unlikely]] + if (!kept && !isImportedByStatement && m_moduleThatProgramImportsNow != module && m_mockedModulesWithErrorKeptByLoader.get(module.index()) && m_importsMadeByOwnFactory.removeFirst(module)) [[unlikely]] kept = loadedIfExists(module)->errorOfMockKeptByLoader.get(); if (kept) [[unlikely]] { keepErrorOfLoad(root, kept, [&](ModuleID each) { return each == module; }); @@ -4035,7 +4078,11 @@ JSPromise* Modules::import(ModuleID module, ModuleID referrer, JSValue options) m_importsMadeByOwnFactory.append(module); else if (!m_importsMadeByOwnFactory.contains(module)) [[likely]] dropErrorOfMockKeptByLoader(module); - walkStopped = runMocksBelow(module); + { + // The error that is kept is for the call that the factory made, which goes on in a later job. + SetForScope moduleThatProgramImportsNow(m_moduleThatProgramImportsNow, m_modulesWhoseFactoryRuns.get(module.index()) ? std::nullopt : std::optional(module)); + walkStopped = runMocksBelow(module); + } RETURN_IF_EXCEPTION(scope, rejectedPromiseWithPendingException(realm, scope)); auto fetchParameters = JSModuleLoader::fetchParametersFromImportOptions(realm, options); RETURN_IF_EXCEPTION(scope, rejectedPromiseWithPendingException(realm, scope)); @@ -4062,8 +4109,15 @@ JSPromise* Modules::import(ModuleID module, ModuleID referrer, JSValue options) return start(module); } - const Identifier& key = resolvedKey(module); + const LoadedModule* loadedBefore = loadedIfExists(module); + bool hasAskedLoader = loadedBefore && !loadedBefore->resolvedKey.isNull(); + Identifier key = resolvedKey(module); RETURN_IF_EXCEPTION(scope, importPromiseRejectedByLoader(realm, scope)); + // The loader resolves the name at every call: a plugin is asked each time, and may answer otherwise. + if (hasAskedLoader) { + key = loader()->resolve(realm, pathString(module), pathString(*data(module).referrerModule()), nullptr, true); + RETURN_IF_EXCEPTION(scope, importPromiseRejectedByLoader(realm, scope)); + } // The build has left the call to the loader, with its type. if (std::optional found = findWithAttributes(key, fetchParameters->get(), FormWithoutAttributes::IsAnotherModule)) return start(*found); @@ -4657,6 +4711,11 @@ JSPromise* Modules::startImport(ModuleID module, std::optional referrer return promiseOfCaller ? promiseOfCaller : JSPromise::create(*m_instance.vm, realm->promiseStructure()); }; + if (isCommonJSFileOrFace(module)) { + if (ModuleID file = fileOf(module); data(file).runsAgainAfterThrow() && status(file) == ModuleStatus::Evaluating && !isAwaitedByImport(file)) [[unlikely]] + ensureLoaded(file).wasFirstImportedByCallWhileItRan = true; + } + bool walkStopped = answerOfWalkAlreadyMade ? *answerOfWalkAlreadyMade : runMocksBelow(module); RETURN_IF_EXCEPTION(scope, rejectedPromiseWithPendingException(realm, scope)); if (m_innermostFilesBeingRun) [[unlikely]] { @@ -4710,6 +4769,11 @@ JSPromise* Modules::startImport(ModuleID module, std::optional referrer VM& vm = *m_instance.vm; bool isBuiltinModuleThatLoaderHas = isBuiltinModule && !isBuiltinModuleNewToLoader(module); bool isCalledByProgram = caller == ImportCaller::CompiledCode; + // A call is no part of the graph that a require() loads: its steps are jobs, not steps of that load, and what it names runs when the require() has returned. + // What has begun, and a file that may be imported while it runs, still go the way of `bun run`, inside the require(). + bool hasBegun = status(module) >= ModuleStatus::Evaluating || status(fileOf(module)) >= ModuleStatus::Evaluating; + bool isNoPartOfLoad = isCalledByProgram && !hasBegun && !(isCommonJSFileOrFace(module) && data(fileOf(module)).mayBeImportedWhileItRuns()); + SetForScope noSynchronousLoad(vm.m_synchronousModuleQueueSetAside, isNoPartOfLoad ? vm.m_synchronousModuleQueue : vm.m_synchronousModuleQueueSetAside); uint32_t jobsWaited = std::exchange(m_jobsThatCallHasWaited, 0); bool isOfGraphThatRan = std::exchange(m_isCallMadeAgainOfGraphThatRan, false); if (isCalledByProgram && (!isBuiltinModule || isBuiltinModuleThatLoaderHas) && loaderOnlyHandsOverNamespace(module)) { @@ -8054,6 +8118,8 @@ void didEndMock(JSGlobalObject* realm, ModuleID moduleOfHost) // The call of a mock that ends before it is laid counts for nothing that comes later. modules->forgetCallOfMock(module); bool wasLoadedAsMock = modules->mockInPlace(module) && !modules->hasMockThatEnded(module); + // An image above was linked to the record that the loader had of the mock. Nothing stands in place of the module here. + wasLoadedAsMock |= realm->vm().aotState().hasSeveralImages && modules->hasMockOfLibraryModule(module); if (wasLoadedAsMock || modules->hasMockLaidOver(module)) modules->letGoOfImportersOf(module, Modules::ImportersLetGo::All); if (modules->isLeftAloneByMock(module)) { diff --git a/Source/JavaScriptCore/aot/runtime/AOTModules.h b/Source/JavaScriptCore/aot/runtime/AOTModules.h index ddd26ccf196f1..b4989761dd0b3 100644 --- a/Source/JavaScriptCore/aot/runtime/AOTModules.h +++ b/Source/JavaScriptCore/aot/runtime/AOTModules.h @@ -383,6 +383,8 @@ class Modules { void dropErrorsOfLoadsEndedByMockOf(ModuleID mocked); void dropErrorOfMockKeptByLoader(ModuleID); void dropErrorsOfLoadsThatCameTo(const ScopedLambda& isMocked); + bool hasMockOfLibraryModule(ModuleID module) const { return m_libraryModulesWithMock.get(module.index()); } + void takeFailedLoadsOutOfLoader(const ScopedLambda& isMocked); bool standsForFileWithKey(ModuleID, const Identifier& key); JSValue errorOfLoadEndedByMock(ModuleID module) const { return m_modulesWithLoadEndedByMock.get(module.index()) ? loadedIfExists(module)->errorKeptByLoader.get() : JSValue(); } JSObject* capturedExports(ModuleID); @@ -632,6 +634,7 @@ class Modules { bool wasImportedWhileItRan(ModuleID file) const; bool loaderHasFileThatThrew(ModuleID file); bool isAwaitedByImport(ModuleID file); + bool isAwaitedByImportFromBeforeItRan(ModuleID file); void dropErrorOfFile(ModuleID fileOrFace); void didLoseErrorOfModule(const Identifier& key); void evaluateCommonJSForLoader(ModuleID file); @@ -768,6 +771,8 @@ class Modules { bool wasRequiredBehindMock { false }; // Of a CommonJS file: didLoseErrorOfModule(). bool wasReachedBySecondLoad { false }; + // Of a CommonJS file that a require() runs: the first import() of it was called meanwhile. + bool wasFirstImportedByCallWhileItRan { false }; bool isBeingLoadedByLoader { false }; BitVector ambiguousExports; std::unique_ptr sourceOrigin; @@ -1083,6 +1088,8 @@ class Modules { // A statement came to them when the factory failed, so the loader keeps their records too: the next statement gets the error, and the factory is not asked. An import() of such a module drops the // record and asks the factory; a require() asks the factory and leaves the record. BitVector m_mockedModulesWithErrorKeptByLoader; + bool m_loaderMayHoldLoadsThatFactoryEnded { false }; + std::optional m_moduleThatProgramImportsNow; BitVector m_modulesWithMockThatWaited; // A mock stood in place of the module while its importers ran, and has ended. They read the names of the module itself now, whose body has yet to run. BitVector m_modulesOwedToImportersThatRan; From 8f2f86c07c47ad8b403dc37275aebc3723bb0c13 Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Sun, 1 Nov 2026 14:21:35 -0800 Subject: [PATCH 07/49] AOT: testaot can ask the laws of the algebra of types of every type of a program `RUN: laws-of-types`: 26 laws of intersectWith, castTo, unionWith, without, isSubsetOf and overlaps over every pair, and for three laws every triple, of the JavaScript types that a program holds: a line for each shape of pair that breaks one, and a sum for each broken law. With TESTAOT_LISTS_BROKEN_LAWS_OF_TYPES= the same behind every command of every test. A tool of the tests alone; no test pins its sums yet, because they go by the table of built-ins that the engine is built with. --- .../aot/tests/AOTIRTestCommands.cpp | 209 ++++++++++++++++++ .../JavaScriptCore/aot/tests/AOTTestCommand.h | 3 + .../aot/tests/AOTTestRunner.cpp | 14 ++ 3 files changed, 226 insertions(+) diff --git a/Source/JavaScriptCore/aot/tests/AOTIRTestCommands.cpp b/Source/JavaScriptCore/aot/tests/AOTIRTestCommands.cpp index 5de449387ce87..a3a4abadbc690 100644 --- a/Source/JavaScriptCore/aot/tests/AOTIRTestCommands.cpp +++ b/Source/JavaScriptCore/aot/tests/AOTIRTestCommands.cpp @@ -21,6 +21,8 @@ #include "AOTVerifier.h" #include #include +#include +#include #include #include @@ -317,6 +319,212 @@ static void runLawsOfResultRules(TestCommandContext& context) } } +#define FOR_EACH_LAW_OF_TYPES(v) \ + v(SubsetHoldsItself) \ + v(MeetLiesInFirst) \ + v(MeetLiesInSecond) \ + v(MeetIsTheSameBothWays) \ + v(MeetIsWrittenTheSameBothWays) \ + v(MeetWithItselfIsItself) \ + v(MeetHoldsWhatLiesInBoth) \ + v(CastLiesInFirst) \ + v(CastLiesInSecond) \ + v(CastWithItselfIsItself) \ + v(CastHoldsWhatLiesInBoth) \ + v(JoinHoldsFirst) \ + v(JoinHoldsSecond) \ + v(JoinIsTheSameBothWays) \ + v(JoinIsWrittenTheSameBothWays) \ + v(JoinWithItselfIsItself) \ + v(JoinLiesInWhatHoldsBoth) \ + v(DifferenceLiesInFirst) \ + v(DifferenceMeetsSecondNowhere) \ + v(DifferenceWithItselfIsNever) \ + v(DifferenceHoldsWhatSecondLacks) \ + v(DifferenceAndSecondHoldFirst) \ + v(OverlapIsTheSameBothWays) \ + v(WhatLiesInsideOverlaps) \ + v(WhatDoesNotOverlapMeetsNowhere) \ + v(WhatOverlapsMeetsSomewhere) + +enum class LawOfTypes : uint8_t { +#define DECLARE_LAW(name) name, + FOR_EACH_LAW_OF_TYPES(DECLARE_LAW) +#undef DECLARE_LAW +}; + +static ASCIILiteral nameOf(LawOfTypes law) +{ + switch (law) { +#define NAME_LAW(name) case LawOfTypes::name: return #name ## _s; + FOR_EACH_LAW_OF_TYPES(NAME_LAW) +#undef NAME_LAW + } + return "?"_s; +} + +class LawsOfTypes { +public: + LawsOfTypes(PrintStream& out, Program& program, unsigned mostTypes) + : m_out(out) + , m_program(program) + , m_types(program.types) + { + HashSet, WTF::UnsignedWithZeroKeyHashTraits> shapes; + m_numberOfTypes = program.types.size(); + for (bool takesShapesSeen : { false, true }) { + for (unsigned index = 0; index < m_numberOfTypes && m_corpus.size() < mostTypes; ++index) { + const Type& type = program.types.at(TypeID(index)); + if (!type.isJavaScriptValue()) + continue; + if (takesShapesSeen ? m_corpus.contains(type) : !shapes.add(shapeOf(type)).isNewEntry) + continue; + m_corpus.append(type); + } + } + } + + unsigned run() + { + size_t size = m_corpus.size(); + Vector inside(size); + Vector overlaps(size); + for (size_t a = 0; a < size; ++a) { + for (size_t b = 0; b < size; ++b) { + if (liesIn(m_corpus[a], m_corpus[b])) + inside[a].set(b); + if (m_corpus[a].overlaps(m_corpus[b], m_program)) + overlaps[a].set(b); + } + } + for (size_t a = 0; a < size; ++a) { + const Type& first = m_corpus[a]; + expect(LawOfTypes::SubsetHoldsItself, inside[a].get(a), first, first, nullptr, first); + expect(LawOfTypes::MeetWithItselfIsItself, first.intersectWith(first, m_types) == first, first, first, nullptr, first.intersectWith(first, m_types)); + expect(LawOfTypes::CastWithItselfIsItself, first.castTo(first, m_types) == first, first, first, nullptr, first.castTo(first, m_types)); + expect(LawOfTypes::JoinWithItselfIsItself, first.unionWith(first, m_types) == first, first, first, nullptr, first.unionWith(first, m_types)); + expect(LawOfTypes::DifferenceWithItselfIsNever, first.without(first, m_types).isNever(), first, first, nullptr, first.without(first, m_types)); + for (size_t b = 0; b < size; ++b) { + const Type& second = m_corpus[b]; + Type meet = first.intersectWith(second, m_types); + Type cast = first.castTo(second, m_types); + Type join = first.unionWith(second, m_types); + Type difference = first.without(second, m_types); + + expect(LawOfTypes::MeetLiesInFirst, liesIn(meet, first), first, second, nullptr, meet); + expect(LawOfTypes::MeetLiesInSecond, liesIn(meet, second), first, second, nullptr, meet); + expect(LawOfTypes::CastLiesInFirst, liesIn(cast, first), first, second, nullptr, cast); + expect(LawOfTypes::CastLiesInSecond, liesIn(cast, second), first, second, nullptr, cast); + expect(LawOfTypes::JoinHoldsFirst, liesIn(first, join), first, second, nullptr, join); + expect(LawOfTypes::JoinHoldsSecond, liesIn(second, join), first, second, nullptr, join); + expect(LawOfTypes::DifferenceLiesInFirst, liesIn(difference, first), first, second, nullptr, difference); + expect(LawOfTypes::DifferenceMeetsSecondNowhere, difference.intersectWith(second, m_types).isNever(), first, second, nullptr, difference); + expect(LawOfTypes::DifferenceAndSecondHoldFirst, liesIn(first, difference.unionWith(second, m_types)), first, second, nullptr, difference); + expect(LawOfTypes::OverlapIsTheSameBothWays, overlaps[a].get(b) == overlaps[b].get(a), first, second, nullptr, meet); + expect(LawOfTypes::WhatLiesInsideOverlaps, !inside[a].get(b) || first.isNever() || overlaps[a].get(b), first, second, nullptr, meet); + expect(LawOfTypes::WhatDoesNotOverlapMeetsNowhere, overlaps[a].get(b) || meet.isNever(), first, second, nullptr, meet); + expect(LawOfTypes::WhatOverlapsMeetsSomewhere, !overlaps[a].get(b) || !meet.isNever(), first, second, nullptr, meet); + if (inside[a].get(b)) + expect(LawOfTypes::JoinLiesInWhatHoldsBoth, liesIn(join, second), first, second, nullptr, join); + if (a < b) { + Type meetTurned = second.intersectWith(first, m_types); + bool isTheSame = liesIn(meet, meetTurned) && liesIn(meetTurned, meet); + expect(LawOfTypes::MeetIsTheSameBothWays, isTheSame, first, second, &meetTurned, meet); + expect(LawOfTypes::MeetIsWrittenTheSameBothWays, !isTheSame || meet == meetTurned, first, second, &meetTurned, meet); + Type joinTurned = second.unionWith(first, m_types); + isTheSame = liesIn(join, joinTurned) && liesIn(joinTurned, join); + expect(LawOfTypes::JoinIsTheSameBothWays, isTheSame, first, second, &joinTurned, join); + expect(LawOfTypes::JoinIsWrittenTheSameBothWays, !isTheSame || join == joinTurned, first, second, &joinTurned, join); + } + for (size_t c = 0; c < size; ++c) { + if (!inside[c].get(a)) + continue; + const Type& third = m_corpus[c]; + if (inside[c].get(b)) { + expect(LawOfTypes::MeetHoldsWhatLiesInBoth, liesIn(third, meet), first, second, &third, meet); + expect(LawOfTypes::CastHoldsWhatLiesInBoth, liesIn(third, cast), first, second, &third, cast); + } + if (!overlaps[c].get(b) && !overlaps[b].get(c)) + expect(LawOfTypes::DifferenceHoldsWhatSecondLacks, liesIn(third, difference), first, second, &third, difference); + } + } + } + + unsigned shapes = 0; + unsigned numberThatHold = 0; + m_out.println("laws of types: "_s, size, " of "_s, m_numberOfTypes, " types"_s); + for (size_t law = 0; law < std::size(m_broken); ++law) { + if (m_broken[law]) + m_out.println("sum "_s, nameOf(static_cast(law)), ": "_s, m_broken[law], " times in "_s, m_shapes[law], " shapes"_s); + else + ++numberThatHold; + shapes += m_shapes[law]; + } + m_out.println(numberThatHold, " of "_s, std::size(m_broken), " laws hold"_s); + return shapes; + } + +private: + static constexpr size_t numberOfLaws = static_cast(LawOfTypes::WhatOverlapsMeetsSomewhere) + 1; + + bool liesIn(const Type& inner, const Type& outer) const { return inner.isSubsetOf(outer, m_program); } + + static uint64_t mix(uint64_t hash, uint64_t word) { return (hash ^ word) * 1099511628211ull; } + + static uint64_t shapeOf(const Type& type) + { + uint64_t hash = mix(14695981039346656037ull, type.primitives); + hash = mix(hash, type.anyObject | type.anyFunction << 1 | !!type.layouts << 2 | !type.stringLiterals.isEmpty() << 3 | !type.symbolLiterals.isEmpty() << 4 | !type.functions.isEmpty() << 5 | !type.builtins.isEmpty() << 6 | !type.regExpLiteralSet.isEmpty() << 7 | type.integers.isSubsetOf(Range::int32()) << 8); + for (const ClassPart& part : type.classes) { + hash = mix(hash, part.builtinClass.index()); + hash = mix(hash, part.contentsAreProven | static_cast(part.arrayStorage) << 1 | static_cast(part.provenPristine.toRaw()) << 9 | static_cast(part.writtenContents.size()) << 32); + } + return hash; + } + + void expect(LawOfTypes law, bool holds, const Type& first, const Type& second, const Type* third, const Type& result) + { + if (holds) + return; + size_t index = static_cast(law); + ++m_broken[index]; + uint64_t shape = mix(mix(mix(mix(index, shapeOf(first)), shapeOf(second)), third ? shapeOf(*third) : 0), shapeOf(result)); + if (!m_seen.add(shape).isNewEntry) + return; + ++m_shapes[index]; + m_out.print("broken "_s, nameOf(law), " | a: "_s); + printType(m_out, m_program, first); + m_out.print(" | b: "_s); + printType(m_out, m_program, second); + if (third) { + m_out.print(" | c: "_s); + printType(m_out, m_program, *third); + } + m_out.print(" | gives: "_s); + printType(m_out, m_program, result); + m_out.println(); + } + + PrintStream& m_out; + const Program& m_program; + TypeArena m_types; + Vector m_corpus; + unsigned m_numberOfTypes { 0 }; + uint64_t m_broken[numberOfLaws] { }; + unsigned m_shapes[numberOfLaws] { }; + HashSet, WTF::UnsignedWithZeroKeyHashTraits> m_seen; +}; + +unsigned listBrokenLawsOfTypes(PrintStream& out, Program& program, unsigned mostTypes) +{ + return LawsOfTypes(out, program, mostTypes).run(); +} + +static void runLawsOfTypes(TestCommandContext& context) +{ + listBrokenLawsOfTypes(context.out, *context.program, std::numeric_limits::max()); +} + std::span irTestCommands() { static constexpr TestCommand commands[] = { @@ -325,6 +533,7 @@ std::span irTestCommands() { "verify"_s, IRForm::AsRead, runVerify }, { "result-types"_s, IRForm::AsRead, runResultTypes }, { "laws-of-result-rules"_s, IRForm::AsRead, runLawsOfResultRules }, + { "laws-of-types"_s, IRForm::AsRead, runLawsOfTypes }, { "int32-bounds"_s, IRForm::AsRead, runInt32Bounds }, }; return commands; diff --git a/Source/JavaScriptCore/aot/tests/AOTTestCommand.h b/Source/JavaScriptCore/aot/tests/AOTTestCommand.h index 60c89b0ddd94e..6ddcebe9f3ed7 100644 --- a/Source/JavaScriptCore/aot/tests/AOTTestCommand.h +++ b/Source/JavaScriptCore/aot/tests/AOTTestCommand.h @@ -35,6 +35,9 @@ struct TestCommandContext { WTF::Function(BuildContext&)> readProgramAgain { }; }; +// Every pair of the program's types that breaks a law of the algebra of types, one line for each shape of pair. Answers how many shapes. +unsigned listBrokenLawsOfTypes(PrintStream&, Program&, unsigned mostTypes); + enum class ProgramAsRead : uint8_t { AsParsed, WithLibrary, TypedAndVerified }; struct TestCommand { diff --git a/Source/JavaScriptCore/aot/tests/AOTTestRunner.cpp b/Source/JavaScriptCore/aot/tests/AOTTestRunner.cpp index e87b90d2b6731..b3cee2a26de94 100644 --- a/Source/JavaScriptCore/aot/tests/AOTTestRunner.cpp +++ b/Source/JavaScriptCore/aot/tests/AOTTestRunner.cpp @@ -21,6 +21,7 @@ #include "JSCConfig.h" #include "Options.h" #include +#include #include #include #include @@ -243,6 +244,19 @@ static CommandResult runCommand(const TestCommand& command, const TestFile& file return again; }; command.run(context); + if (const char* path = getenv("TESTAOT_LISTS_BROKEN_LAWS_OF_TYPES"); path && context.program) { + StringPrintStream broken; + listBrokenLawsOfTypes(broken, *context.program, 160); + char pathOfProcess[1024]; + snprintf(pathOfProcess, sizeof(pathOfProcess), "%s.%d", path, static_cast(getpid())); + if (FILE* list = fopen(pathOfProcess, "a")) { + auto text = broken.toStringWithLatin1Fallback().utf8(); + size_t endOfLine = file.text.find('\n'); + fprintf(list, "== %s | %s\n", run.command.utf8().data(), file.text.left(std::min(endOfLine == notFound ? file.text.length() : endOfLine, 150)).utf8().data()); + fwrite(text.data(), 1, text.length(), list); + fclose(list); + } + } build.diagnostics().finish(context.program.get()); Vector losses = WTF::move(context.losses); losses.appendVector(build.takeLosses(context.program.get())); From 31e1efae9b7ca878dc59a931989b423002f357aa Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Sun, 1 Nov 2026 14:21:35 -0800 Subject: [PATCH 08/49] AOT: a step of the build counts who reads a field that assumes more than a type says aot/codegen/generator/reads-that-assume.ts counts, file by file, who names the heap of a string's value and who calls the four helpers for flat strings, and holds the counts to aot/codegen/AOTReadsThatAssume.txt: 22 places, each with the reason why it is right. A place that the list lacks stops the build before the first file compiles. `AOT_CHECK_READS_THAT_ASSUME()` in aot/AOT.cmake runs it here; Bun's build has its own step. No generated code changes. --- Source/JavaScriptCore/aot/AOT.cmake | 13 ++ .../aot/codegen/AOTReadsThatAssume.txt | 24 ++++ .../codegen/generator/reads-that-assume.ts | 125 ++++++++++++++++++ 3 files changed, 162 insertions(+) create mode 100644 Source/JavaScriptCore/aot/codegen/AOTReadsThatAssume.txt create mode 100644 Source/JavaScriptCore/aot/codegen/generator/reads-that-assume.ts diff --git a/Source/JavaScriptCore/aot/AOT.cmake b/Source/JavaScriptCore/aot/AOT.cmake index 4826863bed70d..880fd30172b76 100644 --- a/Source/JavaScriptCore/aot/AOT.cmake +++ b/Source/JavaScriptCore/aot/AOT.cmake @@ -594,6 +594,18 @@ function(AOT_GENERATE_RUNTIME_FACTS) set(JavaScriptCore_SOURCES ${JavaScriptCore_SOURCES} "${JavaScriptCore_AOT_GENERATED_DIR}/AOTRuntimeFactsOfLayoutsIncludes.h" PARENT_SCOPE) endfunction() +# It reads the text of the compiler's sources too. What it writes says only that they have passed. +function(AOT_CHECK_READS_THAT_ASSUME) + file(GLOB_RECURSE sources CONFIGURE_DEPENDS "${JAVASCRIPTCORE_DIR}/aot/*.cpp" "${JAVASCRIPTCORE_DIR}/aot/*.h") + add_custom_command( + OUTPUT "${JavaScriptCore_AOT_GENERATED_DIR}/AOTReadsThatAssumeChecked.h" + DEPENDS ${sources} "${JAVASCRIPTCORE_DIR}/aot/codegen/generator/reads-that-assume.ts" "${JAVASCRIPTCORE_DIR}/aot/codegen/AOTReadsThatAssume.txt" + COMMAND "${NODE_EXECUTABLE}" "${JAVASCRIPTCORE_DIR}/aot/codegen/generator/reads-that-assume.ts" --javascriptcore "${JAVASCRIPTCORE_DIR}" --out "${JavaScriptCore_AOT_GENERATED_DIR}" + VERBATIM + ) + set(JavaScriptCore_SOURCES ${JavaScriptCore_SOURCES} "${JavaScriptCore_AOT_GENERATED_DIR}/AOTReadsThatAssumeChecked.h" PARENT_SCOPE) +endfunction() + function(AOT_READ_LIBRARY_SOURCES result problemsResult) set(problems) set(sources) @@ -696,6 +708,7 @@ AOT_GENERATE_FROM_SCHEMA() AOT_GENERATE_BUILTIN_TABLE() AOT_GENERATE_VECTOR() AOT_GENERATE_RUNTIME_FACTS() +AOT_CHECK_READS_THAT_ASSUME() AOT_COMPUTE_SOURCES() AOT_CHECK_SOURCE_LISTS() AOT_CHECK_EXPORTED_HEADERS() diff --git a/Source/JavaScriptCore/aot/codegen/AOTReadsThatAssume.txt b/Source/JavaScriptCore/aot/codegen/AOTReadsThatAssume.txt new file mode 100644 index 0000000000000..63f44e8006329 --- /dev/null +++ b/Source/JavaScriptCore/aot/codegen/AOTReadsThatAssume.txt @@ -0,0 +1,24 @@ +# Who names the heap of a field whose reading takes more for granted than a type says, and who calls a function that trusts its caller: | | | . +# aot/codegen/generator/reads-that-assume.ts holds the sources to these counts in every build. A declaration and a definition count as well. +JSString_value | builtins/AOTHashTableBuiltinEmitter.cpp | 5 | two test the rope bit next to the load, in front of any read through the word; three only compare the word with another one, which is right for a rope too +JSString_value | codegen/AOTInnerPointerValidation.cpp | 1 | a list of heaps, no load +JSString_value | codegen/AOTPrimitiveEmitter.h | 1 | isRopeString(): it is the test +JSString_value | codegen/AOTRuntimeValidationEmitter.cpp | 1 | tests the rope bit next to the load +JSString_value | codegen/AOTStringBuilderEmitter.cpp | 1 | tests the rope bit, reads nothing through the word +JSString_value | codegen/AOTStringBuiltinEmitter.cpp | 1 | tests the rope bit next to the load +JSString_value | codegen/AOTStringEmitter.cpp | 7 | implOfFlatString() takes a FlatStringCell; viewOfFlatString() is listed below; three test the rope bit next to the load; one reads behind the stub that has resolved the rope in place, or off the base of a substring, which is flat; one reads what FlattenString has just made +JSString_value | codegen/AOTStringEmitterComparisons.cpp | 4 | areBoth8Bit() is listed below; one only compares the word with the address of an atom; two test the rope bit next to the load +JSString_value | codegen/AOTStringSwitch.cpp | 1 | tests the rope bit next to the load +flatStringWrittenHere | builtins/AOTHashTableBuiltinEmitter.cpp | 1 | the cell is the key in its boxed form with the box taken off: a key without a number in its type is handed on as it came +flatStringWrittenHere | codegen/AOTStringEmitter.cpp | 2 | the definition; flatStringAssumedBy() hands it the cell that it has just made of the same value +flatStringWrittenHere | codegen/AOTStringEmitter.h | 1 | the declaration +flatStringAssumedBy | codegen/AOTStringEmitter.cpp | 2 | the definition; under a sabotage alone, which shows that it stops the build +flatStringAssumedBy | codegen/AOTStringEmitter.h | 1 | the declaration +viewOfFlatString | builtins/AOTMathBuiltinEmitterNumbers.cpp | 1 | behind isRopeString() of the same cell +viewOfFlatString | codegen/AOTStringBuiltinEmitter.cpp | 5 | each behind isRopeString() of the same cell +viewOfFlatString | codegen/AOTStringEmitter.cpp | 1 | the definition +viewOfFlatString | codegen/AOTStringEmitter.h | 1 | the declaration +viewOfFlatString | codegen/AOTStringEmitterComparisons.cpp | 1 | behind isRopeString() of the same cell +areBoth8Bit | codegen/AOTOperatorEmitterEquality.cpp | 1 | both operands are one character read off a string, which is flat; such a read inherits nothing, because the prototypes of built-ins are read-only in compiled code +areBoth8Bit | codegen/AOTStringEmitter.h | 1 | the declaration +areBoth8Bit | codegen/AOTStringEmitterComparisons.cpp | 1 | the definition diff --git a/Source/JavaScriptCore/aot/codegen/generator/reads-that-assume.ts b/Source/JavaScriptCore/aot/codegen/generator/reads-that-assume.ts new file mode 100644 index 0000000000000..7d63beebceebf --- /dev/null +++ b/Source/JavaScriptCore/aot/codegen/generator/reads-that-assume.ts @@ -0,0 +1,125 @@ +// To read some fields is to take more for granted than the type of a value says: the word in the cell of a string is a StringImpl only while the string is flat, +// and a type says what the characters are, not which object holds them. Such a field is read by few functions, which test first or take a type that only a proof can make. +// This step of the build counts, file by file, who names the heap of such a field and who calls a function that trusts its caller, and holds the counts to +// aot/codegen/AOTReadsThatAssume.txt. A place that the list lacks stops the build: go through one of the functions named in the message, or add the place with the reason why it is right. +import { mkdirSync, readdirSync, readFileSync, writeFileSync } from "node:fs"; +import { join, resolve } from "node:path"; +import { parseArgs } from "node:util"; + +const compilerDirectories = ["builtins", "codegen", "image", "object", "passes", "place", "represent", "select", "stubs"]; +const listFile = "aot/codegen/AOTReadsThatAssume.txt"; +const stampFile = "AOTReadsThatAssumeChecked.h"; + +const watched: { name: string; pattern: RegExp; instead: string }[] = [ + { + name: "JSString_value", + pattern: /\b(?:m_)?heaps\.JSString_value\b/g, + instead: "StringEmitter::viewOf(), lengthOf(), viewOfString(), isRopeString(), or implOfFlatString() with a FlatStringCell", + }, + { + name: "flatStringWrittenHere", + pattern: /\bflatStringWrittenHere\(/g, + instead: "StringEmitter::flatStringByType(): this one trusts its caller that the cell is the value's", + }, + { + name: "flatStringAssumedBy", + pattern: /\bflatStringAssumedBy\(/g, + instead: "StringEmitter::flatStringByType(), and another road for what is not flat: this one stops the build of a user's program", + }, + { + name: "viewOfFlatString", + pattern: /\bviewOfFlatString\(/g, + instead: "StringEmitter::viewOf(): this one trusts its caller that isRopeString() of the same cell has just answered no", + }, + { + name: "areBoth8Bit", + pattern: /\bareBoth8Bit\(/g, + instead: "StringEmitter::areEqual(): this one trusts its caller that both strings are flat", + }, +]; + +type Counts = Map; + +function keyOf(name: string, file: string): string { + return `${name} | ${file}`; +} + +function countPlaces(javascriptcore: string): Counts { + const counts: Counts = new Map(); + for (const directory of compilerDirectories) { + for (const entry of readdirSync(join(javascriptcore, "aot", directory), { withFileTypes: true }).sort((a, b) => (a.name < b.name ? -1 : 1))) { + if (!entry.isFile() || !/\.(?:cpp|h)$/.test(entry.name)) continue; + const text = readFileSync(join(javascriptcore, "aot", directory, entry.name), "utf8"); + for (const { name, pattern } of watched) { + const found = text.match(pattern)?.length ?? 0; + if (found) counts.set(keyOf(name, `${directory}/${entry.name}`), found); + } + } + } + return counts; +} + +function readList(text: string): { listed: Counts; problems: string[] } { + const listed: Counts = new Map(); + const problems: string[] = []; + for (const line of text.split("\n")) { + if (!line || line.startsWith("#")) continue; + const [name, file, count, reason] = line.split(" | "); + if (!name || !file || !/^\d+$/.test(count ?? "") || !reason?.trim()) problems.push(`${listFile}: not " | | | ": ${line}`); + else listed.set(keyOf(name, file), Number(count)); + } + return { listed, problems }; +} + +function compare(found: Counts, listed: Counts): string[] { + const problems: string[] = []; + for (const [key, count] of found) { + const allowed = listed.get(key) ?? 0; + if (count === allowed) continue; + const [name, file] = key.split(" | "); + if (count > allowed) + problems.push( + `aot/${file} has ${count} of ${name}, the list has ${allowed}. It takes for granted what no type says. Use ${watched.find(entry => entry.name === name)!.instead}; or say in ${listFile} why the new place is right.`, + ); + else problems.push(`aot/${file} has ${count} of ${name}, the list has ${allowed}: lower the count in ${listFile}, or the next place passes unseen.`); + } + for (const [key, allowed] of listed) { + if (!found.has(key)) problems.push(`aot/${key.split(" | ")[1]} has none of ${key.split(" | ")[0]}, the list has ${allowed}: take the line out of ${listFile}.`); + } + return problems; +} + +// A check that was never seen to fail proves nothing: every name must be found somewhere, and one place too many, one too few and one in a file of its own must each be noticed. +function hasTeeth(found: Counts): boolean { + if (!watched.every(({ name }) => [...found.keys()].some(key => key.startsWith(`${name} | `)))) return false; + const [key, count] = [...found][0] ?? []; + if (key === undefined || count === undefined) return false; + const more = new Map(found).set(key, count + 1); + const fewer = new Map(found).set(key, count - 1); + const elsewhere = new Map(found).set(keyOf(watched[0]!.name, "codegen/AOTNoSuchFile.cpp"), 1); + return !compare(found, found).length && [more, fewer, elsewhere].every(changed => compare(changed, found).length === 1 && compare(found, changed).length === 1); +} + +const { values } = parseArgs({ + strict: true, + options: { "javascriptcore": { type: "string" }, "out": { type: "string" }, "print-counts": { type: "boolean" } }, +}); +if (values.javascriptcore === undefined || (values.out === undefined && !values["print-counts"])) { + console.error("usage: aot/codegen/generator/reads-that-assume.ts --javascriptcore (--out | --print-counts)"); + process.exit(1); +} +const javascriptcore = resolve(values.javascriptcore); +const found = countPlaces(javascriptcore); +if (values["print-counts"]) { + for (const [key, count] of found) console.log(`${key} | ${count}`); + process.exit(0); +} +const { listed, problems } = readList(readFileSync(join(javascriptcore, listFile), "utf8")); +if (!hasTeeth(found)) problems.push("aot/codegen/generator/reads-that-assume.ts does not notice a place too many or too few: it checks nothing."); +problems.push(...compare(found, listed)); +if (problems.length) { + console.error(problems.join("\n")); + process.exit(1); +} +mkdirSync(values.out!, { recursive: true }); +writeFileSync(join(values.out!, stampFile), "// Written by aot/codegen/generator/reads-that-assume.ts when the sources of the compiler have passed it. Do not edit.\n"); From 099e5b1be8e85a068080e712c5653f765e954575 Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Sun, 1 Nov 2026 14:21:35 -0800 Subject: [PATCH 09/49] AOT: the build warns of a store under a name that native code locks on a host object `process.versions.node = "1.2.3"` and its like throw at run time, and the build gave no warning. The table of built-ins now says which names native code locks, and the check for writes to locked built-ins reads it. It feeds a diagnostic alone: no answer of a program and no generated code changes. Goes with Bun's half. --- .../aot/builtins/AOTBuiltinTable.h | 3 ++ .../builtins/AOTBuiltinTableDescription.cpp | 2 +- .../builtins/generator/build-attachments.ts | 4 ++ .../aot/builtins/generator/declarations.ts | 3 ++ .../aot/builtins/generator/dump.ts | 2 + .../generator/emit-cpp-attachments.ts | 2 + .../aot/builtins/generator/emit-dump.ts | 2 + .../aot/builtins/generator/emit-rust-table.ts | 2 +- .../aot/builtins/generator/lib.ts | 6 +++ .../aot/builtins/generator/model.ts | 2 + .../valueflow/AOTValueFlowResultsBuiltins.cpp | 46 ++++++++++++++++++- 11 files changed, 70 insertions(+), 4 deletions(-) diff --git a/Source/JavaScriptCore/aot/builtins/AOTBuiltinTable.h b/Source/JavaScriptCore/aot/builtins/AOTBuiltinTable.h index 540d3eb247710..6c0637f76c3d6 100644 --- a/Source/JavaScriptCore/aot/builtins/AOTBuiltinTable.h +++ b/Source/JavaScriptCore/aot/builtins/AOTBuiltinTable.h @@ -666,6 +666,9 @@ struct BuiltinHostObject { bool keepsStringsOfStoredValues { false }; // For each of these names the object has an accessor whose getter and setter are native code. std::span nativeAccessors { }; + // The build reads what lies under these names, so native code locks them in a compiled program. The holder is the member that hands out the object. + std::span lockedNames { }; + BuiltinID holder { }; // The systems on which native code makes the object. OptionSet platforms { everyBuiltinPlatform }; }; diff --git a/Source/JavaScriptCore/aot/builtins/AOTBuiltinTableDescription.cpp b/Source/JavaScriptCore/aot/builtins/AOTBuiltinTableDescription.cpp index e103cc359e884..0069773e16da9 100644 --- a/Source/JavaScriptCore/aot/builtins/AOTBuiltinTableDescription.cpp +++ b/Source/JavaScriptCore/aot/builtins/AOTBuiltinTableDescription.cpp @@ -347,7 +347,7 @@ class TableWriter { void write(const BuiltinHostObject& hostObject) { object("cell"_s, PlainName { hostObject.nativeCell }, "function"_s, hostObject.function, "prototype"_s, hostObject.prototype, "fields"_s, hostObject.fields, - "keepsStringsOfStoredValues"_s, hostObject.keepsStringsOfStoredValues, "nativeAccessors"_s, hostObject.nativeAccessors, "platforms"_s, hostObject.platforms); + "keepsStringsOfStoredValues"_s, hostObject.keepsStringsOfStoredValues, "nativeAccessors"_s, hostObject.nativeAccessors, "lockedNames"_s, hostObject.lockedNames, "holder"_s, hostObject.holder, "platforms"_s, hostObject.platforms); } void write(const FeatureUseSummary& summary) diff --git a/Source/JavaScriptCore/aot/builtins/generator/build-attachments.ts b/Source/JavaScriptCore/aot/builtins/generator/build-attachments.ts index 7b68acf112ca7..d19beab4a53eb 100644 --- a/Source/JavaScriptCore/aot/builtins/generator/build-attachments.ts +++ b/Source/JavaScriptCore/aot/builtins/generator/build-attachments.ts @@ -976,8 +976,10 @@ function buildHostObjects(context: AttachmentContext): ModelHostObject[] { })); const described = declared.function === null ? null : builder.resolve(declared.function, source); const prototype = declared.prototype === null ? null : resolveObject(builder, declared.prototype, source); + const holder = declared.holder === null ? null : builder.resolve(declared.holder, source); if ((declared.function !== null && described === null) || (declared.prototype !== null && prototype === null)) continue; + if (declared.holder !== null && holder === null) continue; objects.push({ cell: builder.internName(declared.cell, "Text"), function: described, @@ -991,6 +993,8 @@ function buildHostObjects(context: AttachmentContext): ModelHostObject[] { internalFields: internalFields.flatMap(({ index, uses }) => (uses === null ? [] : [{ index, uses }])), keepsStringsOfStoredValues: declared.keepsStringsOfStoredValues, nativeAccessors: declared.nativeAccessors.map(name => builder.internComponent(name)), + lockedNames: declared.lockedNames.map(name => builder.internComponent(name)), + holder, platforms: declared.platforms, source, }); diff --git a/Source/JavaScriptCore/aot/builtins/generator/declarations.ts b/Source/JavaScriptCore/aot/builtins/generator/declarations.ts index 9b555d82c9007..f0ce4ad65e117 100644 --- a/Source/JavaScriptCore/aot/builtins/generator/declarations.ts +++ b/Source/JavaScriptCore/aot/builtins/generator/declarations.ts @@ -344,6 +344,9 @@ export interface HostObjectDeclaration { internalFields: readonly HostObjectInternalFieldSpec[]; keepsStringsOfStoredValues: boolean; nativeAccessors: readonly string[]; + lockedNames: readonly string[]; + /** The member that hands out the object. null: no name of it is locked. */ + holder: BuiltinPath | null; /** The systems on which native code makes it. */ platforms: readonly TargetPlatform[]; } diff --git a/Source/JavaScriptCore/aot/builtins/generator/dump.ts b/Source/JavaScriptCore/aot/builtins/generator/dump.ts index f646bcf4e13cc..b0443db33253c 100644 --- a/Source/JavaScriptCore/aot/builtins/generator/dump.ts +++ b/Source/JavaScriptCore/aot/builtins/generator/dump.ts @@ -294,6 +294,8 @@ export interface DumpedHostObject { fields: { name: string | null; value: DumpedPlace }[]; keepsStringsOfStoredValues: boolean; nativeAccessors: string[]; + lockedNames: string[]; + holder: string | null; platforms: TargetPlatform[]; } diff --git a/Source/JavaScriptCore/aot/builtins/generator/emit-cpp-attachments.ts b/Source/JavaScriptCore/aot/builtins/generator/emit-cpp-attachments.ts index 1280a3f649806..339a7281345f7 100644 --- a/Source/JavaScriptCore/aot/builtins/generator/emit-cpp-attachments.ts +++ b/Source/JavaScriptCore/aot/builtins/generator/emit-cpp-attachments.ts @@ -243,6 +243,8 @@ function formatHostObject(context: AttachmentContext, object: ModelHostObject): ], ["keepsStringsOfStoredValues", object.keepsStringsOfStoredValues ? "true" : null], ["nativeAccessors", formatNames(context, object.nativeAccessors)], + ["lockedNames", formatNames(context, object.lockedNames)], + ["holder", formatId("BuiltinID", object.holder)], [ "platforms", formatOptionSet( diff --git a/Source/JavaScriptCore/aot/builtins/generator/emit-dump.ts b/Source/JavaScriptCore/aot/builtins/generator/emit-dump.ts index fef4f288f5ab5..3a7ad6f450e06 100644 --- a/Source/JavaScriptCore/aot/builtins/generator/emit-dump.ts +++ b/Source/JavaScriptCore/aot/builtins/generator/emit-dump.ts @@ -401,6 +401,8 @@ function dumpHostObject(object: ModelHostObject): DumpedHostObject { })), keepsStringsOfStoredValues: object.keepsStringsOfStoredValues, nativeAccessors: object.nativeAccessors.map(spellingOf), + lockedNames: object.lockedNames.map(spellingOf), + holder: object.holder?.token ?? null, platforms: inListOrder(targetPlatforms, object.platforms), }; } diff --git a/Source/JavaScriptCore/aot/builtins/generator/emit-rust-table.ts b/Source/JavaScriptCore/aot/builtins/generator/emit-rust-table.ts index 5529afe16771a..82898987a2e5c 100644 --- a/Source/JavaScriptCore/aot/builtins/generator/emit-rust-table.ts +++ b/Source/JavaScriptCore/aot/builtins/generator/emit-rust-table.ts @@ -301,7 +301,7 @@ function findNamesStoredByNativeCode(model: TableModel): Set { add(model.builtins.filter(builtin => builtin.holder !== null).map(builtin => builtin.name)); for (const builtinClass of model.classes) add(builtinClass.namesDefinedOnNewInstances ?? []); for (const layout of model.layouts) add(layout.fields.map(field => field.name)); - for (const hostObject of model.hostObjects) add([...hostObject.fields.map(field => field.name), ...hostObject.nativeAccessors]); + for (const hostObject of model.hostObjects) add([...hostObject.fields.map(field => field.name), ...hostObject.nativeAccessors, ...hostObject.lockedNames]); for (const { result, flow } of model.overloads) { add(result.properties.map(property => property.name)); for (const { action, key } of flow.propertyActions) { diff --git a/Source/JavaScriptCore/aot/builtins/generator/lib.ts b/Source/JavaScriptCore/aot/builtins/generator/lib.ts index c4f8cb4cfe66c..2c4a7b3ef0807 100644 --- a/Source/JavaScriptCore/aot/builtins/generator/lib.ts +++ b/Source/JavaScriptCore/aot/builtins/generator/lib.ts @@ -1507,6 +1507,8 @@ export interface HostObjectOptions { keepsStringsOfStoredValues?: boolean; /** The names under which native code lays an accessor of its own. Whoever asks for the description of one gets two functions of native code. */ nativeAccessors?: readonly string[]; + /** The build reads what lies under these names, so native code locks them in a compiled program. `holder`: the member that hands out the object. */ + locked?: { holder: BuiltinPath; names: readonly string[] }; /** By the index of an internal field: what native code does with what code of the library stores there. */ internalFields?: Readonly>; /** The systems on which native code makes the object. Without it: all of them. */ @@ -1533,6 +1535,8 @@ export const hostObject = (cell: string, options: HostObjectOptions): Declaratio })), keepsStringsOfStoredValues: options.keepsStringsOfStoredValues ?? false, nativeAccessors: options.nativeAccessors ?? [], + lockedNames: options.locked?.names ?? [], + holder: options.locked?.holder ?? null, platforms: options.platforms ?? targetPlatforms, }, ]; @@ -1565,6 +1569,8 @@ export function hostFunction( internalFields: [], keepsStringsOfStoredValues: false, nativeAccessors: [], + lockedNames: [], + holder: null, platforms: targetPlatforms, }, ]; diff --git a/Source/JavaScriptCore/aot/builtins/generator/model.ts b/Source/JavaScriptCore/aot/builtins/generator/model.ts index e1f50d1867f1c..61fbd177e5cb2 100644 --- a/Source/JavaScriptCore/aot/builtins/generator/model.ts +++ b/Source/JavaScriptCore/aot/builtins/generator/model.ts @@ -484,6 +484,8 @@ export interface ModelHostObject { internalFields: readonly ModelHostObjectInternalField[]; keepsStringsOfStoredValues: boolean; nativeAccessors: readonly ModelName[]; + lockedNames: readonly ModelName[]; + holder: ModelBuiltin | null; platforms: readonly TargetPlatform[]; source: Source; } diff --git a/Source/JavaScriptCore/aot/valueflow/AOTValueFlowResultsBuiltins.cpp b/Source/JavaScriptCore/aot/valueflow/AOTValueFlowResultsBuiltins.cpp index 6194fc5c9846c..8857153aac6ba 100644 --- a/Source/JavaScriptCore/aot/valueflow/AOTValueFlowResultsBuiltins.cpp +++ b/Source/JavaScriptCore/aot/valueflow/AOTValueFlowResultsBuiltins.cpp @@ -653,6 +653,29 @@ static bool mayWriteLockedLocationUnderKeyNobodyKnows(const BuiltinTable& builti return mayWrite; } +static bool writesLockedNameOfHostObject(const ValueFlowResultInputs& inputs, ValueFlowHeapView& heap) +{ + const Program& program = inputs.program; + std::span hostObjects = program.builtins().hostObjects(); + bool writes = false; + for (uint32_t index = 0; index < hostObjects.size(); ++index) { + if (hostObjects[index].lockedNames.empty()) + continue; + ValueSetID object = heap.maker().of(inputs.universe().hostObject(index)); + for (BuiltinName name : hostObjects[index].lockedNames) { + PropertyKeyID key = program.keyOf(name); + if (!key) + continue; + for (ValueFlowName written : { ValueFlowName::property(program, key), ValueFlowName::getter(program, key), ValueFlowName::setter(program, key), ValueFlowName { ValueFlowNameKind::DeletedMember, key.index() }, ValueFlowName { ValueFlowNameKind::RedefinedMember, key.index() } }) { + heap.forEachNodeWrittenUnder(written, [&](ValueSetID objects, ValueSetID) { + writes |= heap.reaches(objects, object); + }); + } + } + } + return writes; +} + static Vector collectWritesOfLockedLocations(const ValueFlowResultInputs& inputs, const LedgerOfBuiltinObjects& holders, ValueFlowHeapView& heap, bool writesLockedLocation, Vector& locations, Vector& underUnreadNames) { const Program& program = inputs.program; @@ -694,6 +717,17 @@ static Vector collectWritesOfLockedLocations(const V auto spelledOutTextsOf = [&](FunctionID function, ValueID keyValue) -> std::span { return keyValue ? program.typeOf(program.at(function), keyValue).stringLiterals.span() : std::span(); }; + // One object stands for all that native code makes in a cell, so it counts among those that may be anything. What lies under a locked name of it is known all the same. + std::span hostObjects = builtins.hostObjects(); + auto noteOnHostObject = [&](const Notes& notes, ValueSetID receivers, PropertyKeyID key, const Witness& witness) { + if (ValueSets::isComplement(receivers) || sets.size(receivers) != 1) + return; + for (uint32_t index = 0; index < hostObjects.size(); ++index) { + const BuiltinHostObject& hostObject = hostObjects[index]; + if (std::ranges::any_of(hostObject.lockedNames, [&](BuiltinName name) { return program.keyOf(name) == key; }) && sets.contains(receivers, universe.hostObject(index))) + notes.writes.append({ hostObject.holder, key, { }, witness }); + } + }; auto note = [&](const Notes& notes, ValueSetID receivers, PropertyKeyID key, const Witness& witness) { if (notes.changesPrototypeUnderItsName && key == keyOfPrototypeLink) { for (BuiltinID holder : holders.holdersIn(notes.maker, notes.maker.intersect(receivers, universe.builtinFunctions()))) { @@ -779,14 +813,21 @@ static Vector collectWritesOfLockedLocations(const V Notes notes { maker, found, statement.opcode == Opcode::PropSet || statement.opcode == Opcode::ElemSet }; std::optional object = statement.kind == ValueFlowStatementKind::Operation && statement.site && !modeOfCallOperation(statement.opcode) ? OperandPosition::object(statement.opcode) : std::nullopt; std::span operands = bodies.operandsOf(statement); - if (!object || !listsItsObjects(heap.possibleValuesOf(maker, operands[*object]))) + if (!object) continue; + bool listsReceivers = listsItsObjects(heap.possibleValuesOf(maker, operands[*object])); Witness witness = witnessOfStatement(program, statement); ValueFlowStatementID id(static_cast(&statement - bodies.statements().data())); inputs.forEachMadeNodeAskedForBy(id, kindsOfMadeNodes({ ValueFlowMadeNodeKind::Written }), false, [&](const ValueFlowNodeKey& key, ValueFlowStatementID) { - if (shapeOf(key.name().kind).reach == ValueFlowNameReach::OneKey) + if (shapeOf(key.name().kind).reach != ValueFlowNameReach::OneKey) + return; + if (listsReceivers) note(notes, key.set, PropertyKeyID(key.payload), witness); + else + noteOnHostObject(notes, key.set, PropertyKeyID(key.payload), witness); }); + if (!listsReceivers) + continue; const Value& operation = program.at(statement.function).at(statement.site); if (operation.opcode == Opcode::PropSet && operation.key() == keyOfPrototypeLink) note(notes, heap.possibleValuesOf(maker, operands[*object]), keyOfPrototypeLink, witness); @@ -946,6 +987,7 @@ static Vector computeBuiltinLocations(const ValueFlo } else if (!findOwnMember(program, holder, key)) tables.locations.append(BuiltinLocationFlow { holder, key, BuiltinLocationState::Unknown, noWitness }); }); + writesLockedLocation = writesLockedLocation || writesLockedNameOfHostObject(inputs, heap); Vector underNewNames = collectWritesOfLockedLocations(inputs, holders, heap, writesLockedLocation, tables.locations, tables.writesUnderUnreadNames); tables.writesOfLocations = mergeWrites(WTF::move(tables.writesOfLocations), tables.locations.span()); sortAndMergeLocations(tables.locations); From 50fdcc3b12951cf45811d88424176a1fa7395901 Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Sun, 1 Nov 2026 14:21:35 -0800 Subject: [PATCH 10/49] AOT: the end of a mock gives back what the mock replaced Behind the sixth of "seven mends in the run time's mocks". a.js and b.js both have `export * as inner from "./inner"` and both are mocked; then `vi.doUnmock("./b.js")`. Plain gives back what b's mock replaced, `mock of a`; compiled code read `real`. A mock that replaces a namespace that is handed on now keeps what stood there, and its end gives that back. A parameter that nothing read is gone. Goes with Bun's half. --- .../aot/runtime/AOTModuleLinking.cpp | 7 +++++-- .../JavaScriptCore/aot/runtime/AOTModules.cpp | 17 ++++++++++++----- Source/JavaScriptCore/aot/runtime/AOTModules.h | 1 + 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/Source/JavaScriptCore/aot/runtime/AOTModuleLinking.cpp b/Source/JavaScriptCore/aot/runtime/AOTModuleLinking.cpp index 268dcca589288..6bddd4cddee4b 100644 --- a/Source/JavaScriptCore/aot/runtime/AOTModuleLinking.cpp +++ b/Source/JavaScriptCore/aot/runtime/AOTModuleLinking.cpp @@ -1122,9 +1122,12 @@ void Modules::endMockLaidOverFile(ModuleID file) RETURN_IF_EXCEPTION(scope, void()); } + JSObject* handedOnBefore = loaded.handedOnBeforeMock; + loaded.handedOnBeforeMock.set(*this, nullptr); for (ModuleID handedOn : std::exchange(loaded.namespacesReplacedByMock, { })) { - ensureLoaded(handedOn).handedOnInPlaceOfNamespace.set(*this, JSValue()); - m_namespacesReplacedByMock.clear(handedOn.index()); + JSValue before = handedOnBefore ? storedValue(realm, handedOnBefore, Identifier::from(vm, handedOn.index())) : JSValue(); + ensureLoaded(handedOn).handedOnInPlaceOfNamespace.set(*this, before); + m_namespacesReplacedByMock.set(handedOn.index(), !!before); didReplaceNamespaceHandedOn(handedOn); } if (JSObject* variables = loaded.variablesBeforeMock) { diff --git a/Source/JavaScriptCore/aot/runtime/AOTModules.cpp b/Source/JavaScriptCore/aot/runtime/AOTModules.cpp index 526d3362b66ba..98f291f34bd69 100644 --- a/Source/JavaScriptCore/aot/runtime/AOTModules.cpp +++ b/Source/JavaScriptCore/aot/runtime/AOTModules.cpp @@ -1273,10 +1273,18 @@ bool Modules::tryReplaceNamespaceHandedOn(ModuleID handedOn, JSValue value) { if (!m_moduleWithMockBeingLaid) return false; - Vector& replaced = ensureLoaded(*m_moduleWithMockBeingLaid).namespacesReplacedByMock; - if (!replaced.contains(handedOn)) - replaced.append(handedOn); - ensureLoaded(handedOn).handedOnInPlaceOfNamespace.set(*this, value); + LoadedModule& mocked = ensureLoaded(*m_moduleWithMockBeingLaid); + LoadedModule& target = ensureLoaded(handedOn); + if (!mocked.namespacesReplacedByMock.contains(handedOn)) { + mocked.namespacesReplacedByMock.append(handedOn); + // Another mock, over another module that hands the object on, may have given a value. The end of this one gives that back. + if (JSValue before = target.handedOnInPlaceOfNamespace) { + if (!mocked.handedOnBeforeMock) + mocked.handedOnBeforeMock.set(*this, createTableOfExports()); + mocked.handedOnBeforeMock.get()->putDirectMayBeIndex(m_instance.globalObject, Identifier::from(*m_instance.vm, handedOn.index()), before); + } + } + target.handedOnInPlaceOfNamespace.set(*this, value); m_namespacesReplacedByMock.set(handedOn.index()); didReplaceNamespaceHandedOn(handedOn); return true; @@ -2290,7 +2298,6 @@ void Modules::letGoOfImportersOf(const BitVector& mocked, BitVector&& gone, cons { bool hasSeveralImages = m_instance.vm->aotState().hasSeveralImages; if (!m_momentsOfLoads.isEmpty() && images == ImagesSearched::All) { - uint32_t momentOfTestFile = m_instance.vm->aotState().momentOfTestFile; if (m_importersByStatement.isEmpty()) { m_importersByStatement.grow(m_table.size()); for (uint32_t importer = 0; importer < m_table.size(); ++importer) { diff --git a/Source/JavaScriptCore/aot/runtime/AOTModules.h b/Source/JavaScriptCore/aot/runtime/AOTModules.h index b4989761dd0b3..1250d9e345c3a 100644 --- a/Source/JavaScriptCore/aot/runtime/AOTModules.h +++ b/Source/JavaScriptCore/aot/runtime/AOTModules.h @@ -97,6 +97,7 @@ struct LinkContext { macro(JSValue, answerOfMockWhileOriginalRuns) \ macro(JSObject*, exportsAddedByMock) \ macro(JSValue, handedOnInPlaceOfNamespace) \ + macro(JSObject*, handedOnBeforeMock) \ macro(JSValue, errorKeptByLoader) \ macro(JSObject*, exportsWhileRequired) \ macro(JSObject*, capturedExports) \ From 00b407834bf820db3e94bb2d38ff20da3df7a78a Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Sun, 1 Nov 2026 14:21:35 -0800 Subject: [PATCH 11/49] AOT: a link that fails behind a computed name throws at the load, in plain's words `for (const file of ["one.js", "broken.js", "two.js"]) await import("./" + file)`; broken.js: `import { nope } from "./dep.js"`. `bun run` and `bun build --compile`: `SyntaxError: Export named 'nope' not found in module '..dep.js'.` Compiled ahead of time: `ResolveMessage: Cannot find module '/$bunfs/root/broken.js' imported from '/$bunfs/root/broken.js'`. Where broken.js only imports the file with the fault, the build was refused where `--compile` builds. Goes with Bun's half. --- Source/JavaScriptCore/aot/runtime/AOTModuleLinking.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Source/JavaScriptCore/aot/runtime/AOTModuleLinking.cpp b/Source/JavaScriptCore/aot/runtime/AOTModuleLinking.cpp index 6bddd4cddee4b..73aa4bb35b3b7 100644 --- a/Source/JavaScriptCore/aot/runtime/AOTModuleLinking.cpp +++ b/Source/JavaScriptCore/aot/runtime/AOTModuleLinking.cpp @@ -85,7 +85,9 @@ StarExportResolution::Type Modules::resolveRequiredName(ModuleID exporter, const RELEASE_ASSERT(exporterData.capturesExports() || exporterData.hasExportTable()); bool hasExport = exporterData.capturesExports() ? hasCapturedExport(exporter, name) : !!m_table.findExport(exporter, name.impl()); RETURN_IF_EXCEPTION(scope, StarExportResolution::Type::Error); - return hasExport ? StarExportResolution::Type::Resolved : StarExportResolution::Type::NotFound; + if (hasExport) + return StarExportResolution::Type::Resolved; + return m_table.isAmbiguousExport(exporter, name.string()) ? StarExportResolution::Type::Ambiguous : StarExportResolution::Type::NotFound; } // Of a module of this image, or of the module of another image that a record of this one stands for. From ee63417dfc5441470e23bf9aee1c83f851dfe6e6 Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Sun, 1 Nov 2026 14:21:35 -0800 Subject: [PATCH 12/49] AOT: the statistics tell the lines of packages, of declarations and of Bun apart Four more statistics beside SourceBytes and SourceLines, for `bun build --compile --aot`'s new closing words, "compiled 57,464 lines in 18.64 s (3,082 lines/s)". Counters alone. Goes with Bun's half. --- Source/JavaScriptCore/aot/ir/schema/statistics.def | 1 + 1 file changed, 1 insertion(+) diff --git a/Source/JavaScriptCore/aot/ir/schema/statistics.def b/Source/JavaScriptCore/aot/ir/schema/statistics.def index 543444de4f5af..43595a00b1ae6 100644 --- a/Source/JavaScriptCore/aot/ir/schema/statistics.def +++ b/Source/JavaScriptCore/aot/ir/schema/statistics.def @@ -1,6 +1,7 @@ enum StatisticID { None SourceFilesChecked SourceFilesInferred SourceFilesTypesOnly SourceFilesData SourceFilesRuntime SourceFilesDemoted SourceFilesOfBunParsed SourceBytes SourceLines + SourceBytesOfPackages SourceLinesOfPackages SourceBytesOfDeclarations SourceLinesOfDeclarations SourceBytesOfBun SourceLinesOfBun AotFilesUsed CheckerQueries CheckerQueriesGivenUp CheckerMembersGivenUp UnionMembersCompared BuiltinTypesDegradedToViews ModulesBound FunctionsLowered KnownHelperFunctions KnownHelperCalls FunctionsDroppedByLink ProgramBytes EngineRuns WholeEngineRuns From 4265659984940fe29dcb0a5061f28f212611b2f4 Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Sun, 1 Nov 2026 14:21:35 -0800 Subject: [PATCH 13/49] AOT: the detail of a stale type names an operand only where there is one It came with "testaot says both types, and prints the body around a failed verification". Behind a TypeOutOfDate the detail read the first operand after asking only how many there are; an optional operand that is left out (NewObject's prototype) is an empty id, and testaot died by SIGSEGV where it should have named the stale type. The block runs only where testaot has set the printer of types: no product build enters it. --- .../JavaScriptCore/aot/ir/AOTVerifierTypes.cpp | 4 ++-- .../stale-type-behind-an-empty-operand.ir | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 Source/JavaScriptCore/aot/tests/verifier/stale-type-behind-an-empty-operand.ir diff --git a/Source/JavaScriptCore/aot/ir/AOTVerifierTypes.cpp b/Source/JavaScriptCore/aot/ir/AOTVerifierTypes.cpp index 345bcac576d11..5407edf864f18 100644 --- a/Source/JavaScriptCore/aot/ir/AOTVerifierTypes.cpp +++ b/Source/JavaScriptCore/aot/ir/AOTVerifierTypes.cpp @@ -195,10 +195,10 @@ void FunctionVerifier::verifyOwnType(BlockID block, ValueID id, const Value& val if ((!isAsStored && (!isOptimizing() || !computed.isSubsetOf(stored, m_program))) || !hasExpectedTypeInsideType(m_program, value)) { fail(InternalErrorKind::TypeOutOfDate, block, id); m_errors.last().detail = describeDifference(m_program, typing().types.adopt(stored), computed); - if (printerOfTypesForVerifier && value.numberOfOperands()) { + if (printerOfTypesForVerifier && value.numberOfOperands() && value.operand(0)) { const Value& definition = m_function.at(value.operand(0)); m_errors.last().detail = makeString(m_errors.last().detail, "; its first operand is v"_s, value.operand(0).index(), " ("_s, nameOf(definition.opcode), "): "_s, printerOfTypesForVerifier(m_program, typeOf(value.operand(0))), - definition.numberOfOperands() ? makeString(", made of v"_s, definition.operand(0).index(), ": "_s, printerOfTypesForVerifier(m_program, typeOf(definition.operand(0)))) : String()); + definition.numberOfOperands() && definition.operand(0) ? makeString(", made of v"_s, definition.operand(0).index(), ": "_s, printerOfTypesForVerifier(m_program, typeOf(definition.operand(0)))) : String()); } return; } diff --git a/Source/JavaScriptCore/aot/tests/verifier/stale-type-behind-an-empty-operand.ir b/Source/JavaScriptCore/aot/tests/verifier/stale-type-behind-an-empty-operand.ir new file mode 100644 index 0000000000000..ebd35ee3af14a --- /dev/null +++ b/Source/JavaScriptCore/aot/tests/verifier/stale-type-behind-an-empty-operand.ir @@ -0,0 +1,17 @@ +# A type that is not what its rule gives is reported also where the first operand of the value, or of what made that operand, is left out. +# FORM: Prepared +# RUN: verify +# DIAGNOSTIC: TypeOutOfDate 2 + +module m0 path="main.js" displayPath="main.js" +layout L0 name="One" module=m0 fields=[{key="x" declared={type=number} flags=[IsRequired]}] ownFieldCount=1 +layout L1 name="Other" module=m0 fields=[{key="y" declared={type=number} flags=[IsRequired]}] ownFieldCount=1 +function f0 name="noPrototype" parameters=[{type=number}] results=[{type=anything}] { + v1: L1 = NewObject -, v0 layout=L0 fields=[0] + Return v1 +} +function f1 name="madeWithoutPrototype" parameters=[{type=number}] results=[{type=anything}] { + v1 = NewObject -, v0 layout=L0 fields=[0] + v2: L1 = FinishObject v1 + Return v2 +} From 9b5490b829c3efec44d8c14c119519093d6d7227 Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Sun, 1 Nov 2026 14:21:35 -0800 Subject: [PATCH 14/49] AOT: a store that is listed at its allocation goes by the proof of the pass A verifying build stopped with "verification failed after SplitUnions; .. (PropSet) in b0 may throw or run code, and is gone". The removal is right: `v24 = NewObject ..; PropSet v24, v12 key="inner"; FinishObject v24` becomes `NewObject -, v12, ..`, only where the store's plan is a plain store to a slot of an object that the function has just made. The check asked too much. Goes with Bun's half. --- Source/JavaScriptCore/aot/passes/AOTResolveAccesses.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Source/JavaScriptCore/aot/passes/AOTResolveAccesses.cpp b/Source/JavaScriptCore/aot/passes/AOTResolveAccesses.cpp index 81a58890ec552..5ffc4eafedc1e 100644 --- a/Source/JavaScriptCore/aot/passes/AOTResolveAccesses.cpp +++ b/Source/JavaScriptCore/aot/passes/AOTResolveAccesses.cpp @@ -756,8 +756,10 @@ static bool listFirstStoresAtAllocation(FunctionPassContext& context, IRBuilder& state.placeTakenTo[index] = placeOfLastStore; }; leavePlace(object.place); - for (uint32_t index : listedStores) + for (uint32_t index : listedStores) { + explainOperationThatGoes(function, values[index], InPlaceOfOperation::NothingByProofOfThePass); leavePlace(index); + } for (uint32_t index : movedBehindAllocation) { valuesAtPlace.append(function.at(state.block).values[index]); leavePlace(index); From b798844c7373c17eaf1233ed227f24f86239f9c9 Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Sun, 1 Nov 2026 14:21:35 -0800 Subject: [PATCH 15/49] AOT: a test: a number stored into a new extension has the owner's barrier The barrier for a new extension is decided as if a cell were stored, whatever the value's type. A build that decided by the value's type would have passed every other test. A pair of tests alone, as shipped and with the planted fault StoresNeedNoBarrier. --- .../number-in-new-extension-sabotaged.ir | 7 ++ .../validation/number-in-new-extension.ir | 83 +++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 Source/JavaScriptCore/aot/tests/validation/number-in-new-extension-sabotaged.ir create mode 100644 Source/JavaScriptCore/aot/tests/validation/number-in-new-extension.ir diff --git a/Source/JavaScriptCore/aot/tests/validation/number-in-new-extension-sabotaged.ir b/Source/JavaScriptCore/aot/tests/validation/number-in-new-extension-sabotaged.ir new file mode 100644 index 0000000000000..1cc58d7fd0fcd --- /dev/null +++ b/Source/JavaScriptCore/aot/tests/validation/number-in-new-extension-sabotaged.ir @@ -0,0 +1,7 @@ +# The lie: sabotage=StoresNeedNoBarrier leaves the barriers out. No cell is stored here, so the one that is missed is that of the owner of the new Extension. +# FORM: AsRead +# RUN: run-in-shell --verifyGC=1 globalThis.register = (late, read, other) => { gc(); other(); late(); edenGC(); read(); }; +# OPTIONS: references=narrow testing=NoLayoutIsMadeSeldom sabotage=StoresNeedNoBarrier +# CHECK: GC Verifier: ERROR cell {{*}} was not marked +# CHECK: crashed with +# PROGRAM-OF: number-in-new-extension.ir diff --git a/Source/JavaScriptCore/aot/tests/validation/number-in-new-extension.ir b/Source/JavaScriptCore/aot/tests/validation/number-in-new-extension.ir new file mode 100644 index 0000000000000..6a15b9bdc0be8 --- /dev/null +++ b/Source/JavaScriptCore/aot/tests/validation/number-in-new-extension.ir @@ -0,0 +1,83 @@ +# interface Box { r0: number .. r12: number; late0?: number; late1?: number } const late = () => { box.late0 = 1.5 }, other = () => { another.late1 = 0.5 } +# A number needs no barrier of its own. But the thirteen required fields leave no room in the cell, so late0 and late1 lie in the Extension, and the store in late() makes the Extension: +# from then on the owner names it, and the collector finds it through the owner alone. The owner is old and marked, so a collection of the young looks at it again only if the store has said so. +# other() goes first: the first allocation behind a collection finds no free cell at hand and is left to the run time, which has a barrier of its own. The one in late() is made by the stub. +# FORM: AsRead +# RUN(SLOTS): plan-slots layouts +# RUN: run-in-shell --verifyGC=1 globalThis.register = (late, read, other) => { gc(); other(); late(); edenGC(); read(); }; +# OPTIONS: references=narrow testing=NoLayoutIsMadeSeldom +# SLOTS: "late0" JSValue out@ +# CHECK-NOT: GC Verifier +# CHECK: 2.5 1.5 undefined +# CHECK: exited with 0 + +module m0 path="main.ts" displayPath="main.ts" body=f0 +entry module=m0 +layout L0 name="Box" fields=[{key="r0" declared={type=number} flags=[IsRequired]}, {key="r1" declared={type=number} flags=[IsRequired]}, {key="r2" declared={type=number} flags=[IsRequired]}, {key="r3" declared={type=number} flags=[IsRequired]}, {key="r4" declared={type=number} flags=[IsRequired]}, {key="r5" declared={type=number} flags=[IsRequired]}, {key="r6" declared={type=number} flags=[IsRequired]}, {key="r7" declared={type=number} flags=[IsRequired]}, {key="r8" declared={type=number} flags=[IsRequired]}, {key="r9" declared={type=number} flags=[IsRequired]}, {key="r10" declared={type=number} flags=[IsRequired]}, {key="r11" declared={type=number} flags=[IsRequired]}, {key="r12" declared={type=number} flags=[IsRequired]}, {key="late0" declared={type=number}}, {key="late1" declared={type=number}}] +layout L1 kind=Environment fields=[{key="held0" declared={type=anything}}, {key="held1" declared={type=anything}}, {key="held2" declared={type=anything}}] +function f0 name="main" kind=Module results=[{type=anything}] { + v0 = Double number=2.5 + v1 = NewObject layout=L1 fields=[] + v2 = NewObject -, v0, v0, v0, v0, v0, v0, v0, v0, v0, v0, v0, v0, v0 layout=L0 fields=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] + v3 = FinishObject v2 + PutEnvironmentField v1, v3 layout=L1 field=0 + v5 = NewObject -, v0, v0, v0, v0, v0, v0, v0, v0, v0, v0, v0, v0, v0 layout=L0 fields=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] + v6 = FinishObject v5 + PutEnvironmentField v1, v6 layout=L1 field=1 + v8 = NewObject -, v0, v0, v0, v0, v0, v0, v0, v0, v0, v0, v0, v0, v0 layout=L0 fields=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] + v9 = FinishObject v8 + PutEnvironmentField v1, v9 layout=L1 field=2 + v11 = NewClosure v1 function=f1 + v12 = NewClosure v1 function=f2 + v13 = NewClosure v1 function=f4 + v14 = Builtin builtin=GlobalThis + v15 = PropGet v14 key="register" [isFreeName] + v16 = Call v15, -, v11, v12, v13 + Return +} +function f1 name="late" captures=[{type=env L1}] results=[{type=undefined}] { + b0(v0: env L1): + v1 = GetEnvironmentField v0 layout=L1 field=0 + v2 = Double number=1.5 + PropSet v1, v2 key="late0" + Return +} +function f2 name="read" captures=[{type=env L1}] results=[{type=undefined}] { + b0(v0: env L1): + v1 = NewClosure function=f3 + v2 = GetEnvironmentField v0 layout=L1 field=0 + v3 = Call v1, -, v2 + v4 = GetEnvironmentField v0 layout=L1 field=1 + v5 = Call v1, -, v4 + v6 = GetEnvironmentField v0 layout=L1 field=2 + v7 = Call v1, -, v6 + Return +} +function f3 name="show" parameters=[{type=L0}] results=[{type=anything}] { + v1 = PropGet v0 key="r0" + v2 = PropGet v0 key="r1" + v3 = PropGet v0 key="r2" + v4 = PropGet v0 key="r3" + v5 = PropGet v0 key="r4" + v6 = PropGet v0 key="r5" + v7 = PropGet v0 key="r6" + v8 = PropGet v0 key="r7" + v9 = PropGet v0 key="r8" + v10 = PropGet v0 key="r9" + v11 = PropGet v0 key="r10" + v12 = PropGet v0 key="r11" + v13 = PropGet v0 key="r12" + v14 = PropGet v0 key="late0" + v15 = PropGet v0 key="late1" + v16 = Builtin builtin=GlobalThis + v17 = PropGet v16 key="print" [isFreeName] + v18 = Call v17, -, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15 + Return v18 +} +function f4 name="other" captures=[{type=env L1}] results=[{type=undefined}] { + b0(v0: env L1): + v1 = GetEnvironmentField v0 layout=L1 field=1 + v2 = Double number=0.5 + PropSet v1, v2 key="late1" + Return +} From f14feaf84fa4b7b7996a5e6f08ff48b024baa8b5 Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Sun, 1 Nov 2026 14:21:35 -0800 Subject: [PATCH 16/49] AOT: require() asks what the imports under a type have made of a data file `import typed from "./data.json" with { type: "json" }` and `require("./data.json")` in one program gave two objects in compiled code: a store through one was not seen through the other, without an error. Under `bun run` require() hands out the namespace of what the import made, whose default is that one object; `bun build --compile` has one object too. Goes with Bun's half. --- Source/JavaScriptCore/aot/runtime/AOTModules.cpp | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/Source/JavaScriptCore/aot/runtime/AOTModules.cpp b/Source/JavaScriptCore/aot/runtime/AOTModules.cpp index 98f291f34bd69..493a4585f0c86 100644 --- a/Source/JavaScriptCore/aot/runtime/AOTModules.cpp +++ b/Source/JavaScriptCore/aot/runtime/AOTModules.cpp @@ -6361,12 +6361,23 @@ JSC_DEFINE_JIT_OPERATION(operationAOTRecordRequire, void, (Instance* instance, u OPERATION_RETURN(scope); } +// What a require() or an import without a type has made of the file answers before what an import under a type has made of it. +static bool isLoadedWithoutType(const Modules& modules, ModuleID form) +{ + String path = modules.table().pathText(form); + size_t mark = path.reverseFind('?'); + if (modules.data(form).kind != ModuleKind::Data || mark == notFound) + return false; + std::optional file = modules.table().findByPath(StringView(path).left(mark)); + return file && modules.data(*file).kind == ModuleKind::Data && modules.status(*file) == ModuleStatus::Evaluated; +} + // Undefined: no import has loaded the file, and require() reads it in its own way. JSC_DEFINE_JIT_OPERATION(operationAOTRequireIfHeldForImport, EncodedJSValue, (Instance* instance, uint32_t module, uint32_t referrer)) { AOT_OPERATION_BEGIN(instance, Entry::operationAOTRequireIfHeldForImport); Modules& modules = modulesOf(*instance); - if (!modules.isHeldForImport(ModuleID(module))) + if (!modules.isHeldForImport(ModuleID(module)) || isLoadedWithoutType(modules, ModuleID(module))) OPERATION_RETURN(scope, JSValue::encode(jsUndefined())); OPERATION_RETURN(scope, JSValue::encode(modules.require(ModuleID(module), ModuleID(referrer)))); } From 75c429ddd4dc81ca336e180729f7bc16c88fa010 Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Sun, 1 Nov 2026 14:21:35 -0800 Subject: [PATCH 17/49] AOT: a test pins the laws of the algebra of types over the types that it names Behind "testaot can ask the laws of the algebra of types of every type of a program": `RUN: laws-of-types` takes for its corpus the types that the test names, so its sums no longer go by the table of built-ins that the engine is built with. tests/types/laws.ir: 21 types, 20 of 26 laws hold; the sums of the other six may only shrink. Tests alone. --- .../aot/tests/AOTIRTestCommands.cpp | 23 ++++++++++++++++++- Source/JavaScriptCore/aot/tests/types/laws.ir | 17 ++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 Source/JavaScriptCore/aot/tests/types/laws.ir diff --git a/Source/JavaScriptCore/aot/tests/AOTIRTestCommands.cpp b/Source/JavaScriptCore/aot/tests/AOTIRTestCommands.cpp index a3a4abadbc690..bd4c27c307438 100644 --- a/Source/JavaScriptCore/aot/tests/AOTIRTestCommands.cpp +++ b/Source/JavaScriptCore/aot/tests/AOTIRTestCommands.cpp @@ -365,6 +365,15 @@ static ASCIILiteral nameOf(LawOfTypes law) class LawsOfTypes { public: + LawsOfTypes(PrintStream& out, Program& program, Vector&& corpus) + : m_out(out) + , m_program(program) + , m_types(program.types) + , m_corpus(WTF::move(corpus)) + , m_numberOfTypes(m_corpus.size()) + { + } + LawsOfTypes(PrintStream& out, Program& program, unsigned mostTypes) : m_out(out) , m_program(program) @@ -520,9 +529,21 @@ unsigned listBrokenLawsOfTypes(PrintStream& out, Program& program, unsigned most return LawsOfTypes(out, program, mostTypes).run(); } +// The types that the test names. The table of types holds more: what the table of built-ins brings, which is not the same in every build. static void runLawsOfTypes(TestCommandContext& context) { - listBrokenLawsOfTypes(context.out, *context.program, std::numeric_limits::max()); + Program& program = *context.program; + if (program.functions.isEmpty()) { + context.out.println("no function"_s); + return; + } + Vector corpus; + for (const DeclaredType& parameter : program.functions[0].parameters) { + const Type& type = program.at(parameter.written()); + if (type.isJavaScriptValue() && !corpus.contains(type)) + corpus.append(type); + } + LawsOfTypes(context.out, program, WTF::move(corpus)).run(); } std::span irTestCommands() diff --git a/Source/JavaScriptCore/aot/tests/types/laws.ir b/Source/JavaScriptCore/aot/tests/types/laws.ir new file mode 100644 index 0000000000000..bb2594ac20411 --- /dev/null +++ b/Source/JavaScriptCore/aot/tests/types/laws.ir @@ -0,0 +1,17 @@ +# The laws of the algebra of types, over kinds of which no type says how they are related: a class without an instance test, a class and one that extends it, functions and classes of functions. +# Nothing is ever lost: an intersection and a cast hold whatever lies in both, a difference whatever the other lacks, a union both, and what overlaps meets somewhere. +# Six laws do not hold here: no type says that an object is an instance of two classes, so an intersection names the class of its second operand, and a difference is an upper bound. +# How often each is broken may only shrink, and never at the price of a law that holds. +# FORM: Prepared +# RUN: laws-of-types +# CHECK: laws of types: 21 of 21 types +# CHECK-NEXT: sum MeetLiesInFirst: 202 times in 202 shapes +# CHECK-NEXT: sum MeetLiesInSecond: 7 times in 7 shapes +# CHECK-NEXT: sum MeetIsTheSameBothWays: 98 times in 98 shapes +# CHECK-NEXT: sum CastLiesInFirst: 202 times in 202 shapes +# CHECK-NEXT: sum CastLiesInSecond: 7 times in 7 shapes +# CHECK-NEXT: sum DifferenceMeetsSecondNowhere: 249 times in 249 shapes +# CHECK-NEXT: 20 of 26 laws hold + +module m0 path="main.js" displayPath="main.js" +function f0 name="types" parameters=[{type=undefined|Arguments}, {type=Array}, {type=Object}, {type=null|string|Array|Object}, {type=undefined|null|Array}, {type=Proxy}, {type=function}, {type=f0}, {type=BoundFunction}, {type=Function}, {type=RangeError}, {type=Error}, {type=Float64Array}, {type=TypedArray}, {type=Buffer}, {type=Uint8Array}, {type=ArrayIterator}, {type=Map}, {type=Number}, {type=object}, {type=anything}] results=[{type=undefined}] From 3ff320313e7fda4d3afc726e3498d9eb64021536 Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Sun, 1 Nov 2026 14:21:35 -0800 Subject: [PATCH 18/49] AOT: a delete by key looks at the key, and an assignment asks for the target's prototype Two wrong answers of executables at default flags, without an error; Bun's main has both. - `Reflect.deleteProperty(Object.freeze({ m: y }), "m")` answered true for false. Nobody read the field, so it was removed, and the delete found nothing to refuse. A delete by key now looks under its key, as a test does. - `Object.assign({ __proto__: { set a(v) { o.b = "S"; } } }, o).b` gave "x" for "S". The expansion reads every field of the source first, and the setter that the target inherits ran too late. Such a call stays a call. Goes with Bun's half. --- .../aot/passes/AOTExpandOwnKeys.cpp | 50 ++++++++++++------- .../aot/passes/AOTExpandOwnKeysInternal.h | 2 + ...to-a-literal-that-was-given-a-prototype.ir | 31 ++++++++++++ ...d-fields-that-a-built-in-deletes-by-key.ir | 31 ++++++++++++ .../aot/valueflow/AOTValueFlowResultsUses.cpp | 12 +++-- 5 files changed, 102 insertions(+), 24 deletions(-) create mode 100644 Source/JavaScriptCore/aot/tests/passes/expand-own-keys-assign-to-a-literal-that-was-given-a-prototype.ir create mode 100644 Source/JavaScriptCore/aot/tests/passes/remove-unread-fields-that-a-built-in-deletes-by-key.ir diff --git a/Source/JavaScriptCore/aot/passes/AOTExpandOwnKeys.cpp b/Source/JavaScriptCore/aot/passes/AOTExpandOwnKeys.cpp index d242923d8120d..eaa55f4f29fc5 100644 --- a/Source/JavaScriptCore/aot/passes/AOTExpandOwnKeys.cpp +++ b/Source/JavaScriptCore/aot/passes/AOTExpandOwnKeys.cpp @@ -139,9 +139,7 @@ ExpectedKeyOrders expectedKeyOrdersOf(const Program& program, LayoutID id, KeyOr return { id, WTF::move(orders), coversEveryObject, static_cast(saturateToUInt32(numberOfOrdersLeftOut)) }; } -static constexpr size_t conditionsPerAllocation = 3; - -// Bit i is set when nothing between the NewObject uses[i].allocation and uses[i].point can have changed its shape, its key order or its extra properties. +// Bit i is set when nothing between the NewObject uses[i].allocation and uses[i].point can have changed its shape, its key order or its extra properties, nor, where it is assigned to, its prototype. static BitVector findUnchangedAllocations(FunctionPassContext& context, std::span uses) { BitVector unchanged(uses.size()); @@ -151,17 +149,23 @@ static BitVector findUnchangedAllocations(FunctionPassContext& context, std::spa TestConditions conditions(context.analyses); Vector requirements; Vector proofs; - Vector askedUses; + struct AskedUse { + size_t index; + size_t firstRequirement; + }; + Vector askedUses; for (size_t index = 0; index < uses.size(); ++index) { const AllocationAtPoint& use = uses[index]; if (!allocatesOwnLayout(context.program, context.function.at(use.allocation))) continue; LayoutID layout = context.function.at(use.allocation).layout(); - std::array wantedConditions { + Vector wantedConditions { makeShapeTest(use.allocation, context.types.completeLayoutSet(singleElementSpan(layout))), Emission::make<&Value::setLayout, &Value::setFields>(Opcode::HasKeyOrder, { use.allocation }, layout, context.function.at(use.allocation).fields()), IRBuilder::make(Opcode::HasNoExtraProperties, { use.allocation }), }; + if (use.isAssignedTo) + wantedConditions.append(IRBuilder::make(Opcode::HasLayoutPrototype, { use.allocation })); size_t numberOfNewConditions = std::ranges::count_if(wantedConditions, [&](const Value& condition) { return !conditions.find(condition); }); @@ -170,6 +174,7 @@ static BitVector findUnchangedAllocations(FunctionPassContext& context, std::spa continue; } + askedUses.append({ index, requirements.size() }); for (const Value& condition : wantedConditions) { unsigned numberOfKnownConditions = conditions.size(); TestConditionID id = conditions.add(condition); @@ -177,7 +182,6 @@ static BitVector findUnchangedAllocations(FunctionPassContext& context, std::spa proofs.append(ExistingProof { ProofSource::Allocation, id, use.allocation, { } }); requirements.append(TestRequirement { 0, 0, id, use.point, RequirementKind::AsksOnly }); } - askedUses.append(index); } if (askedUses.isEmpty()) return unchanged; @@ -185,11 +189,12 @@ static BitVector findUnchangedAllocations(FunctionPassContext& context, std::spa TestEvents events(TestEventInputs { context.analyses, conditions, { }, requirements.span(), proofs.span(), { }, false }); TestCoverage coverage(TestCoverageInputs { events, GapBudget::unlimited(), nullptr }); for (size_t asked = 0; asked < askedUses.size(); ++asked) { + size_t end = asked + 1 < askedUses.size() ? askedUses[asked + 1].firstRequirement : requirements.size(); bool isUnchanged = true; - for (size_t condition = 0; condition < conditionsPerAllocation; ++condition) - isUnchanged &= coverage.of(static_cast(asked * conditionsPerAllocation + condition)) == RequirementCoverage::Covered; + for (size_t requirement = askedUses[asked].firstRequirement; requirement < end; ++requirement) + isUnchanged &= coverage.of(static_cast(requirement)) == RequirementCoverage::Covered; if (isUnchanged) - unchanged.set(askedUses[asked]); + unchanged.set(askedUses[asked].index); } return unchanged; } @@ -220,7 +225,8 @@ static PassOutcome foldKeyOrderTests(FunctionPassContext& context) IRBuilder builder(context.analyses.context(), function, context.types, &context.analyses); for (size_t index : decidableTests) { - auto [allocation, point] = tests[index]; + ValueID allocation = tests[index].allocation; + ProgramPoint point = tests[index].point; ValueID test = function.at(point.block).values[point.index]; bool result = function.at(test).opcode == Opcode::HasNoExtraProperties || (function.at(test).layout() == function.at(allocation).layout() && std::ranges::equal(function.at(test).fields(), function.at(allocation).fields())); @@ -270,17 +276,17 @@ static bool isExpandedByPlan(OwnKeyWalk walk) UnchangedAllocations::UnchangedAllocations(FunctionPassContext& context, std::span sites) : m_context(context) { - auto addUse = [&](const OwnKeyWalkSite& site, ValueID object) { + auto addUse = [&](const OwnKeyWalkSite& site, ValueID object, bool isAssignedTo = false) { ValueID origin = context.analyses.sameObjectThroughCasts(object).valueInFrontOfFacts(); if (context.function.at(origin).opcode == Opcode::NewObject) - m_uses.append(AllocationAtPoint { origin, { site.block, site.index } }); + m_uses.append(AllocationAtPoint { origin, { site.block, site.index }, isAssignedTo }); }; for (const OwnKeyWalkSite& site : sites) { if (!isExpandedByPlan(site.walk)) continue; addUse(site, site.source); if (site.walk == OwnKeyWalk::Assign || site.walk == OwnKeyWalk::Spread) - addUse(site, site.target); + addUse(site, site.target, site.walk == OwnKeyWalk::Assign); } const CFG& cfg = context.analyses.cfg(); @@ -295,12 +301,18 @@ bool UnchangedAllocations::contains(ValueID allocation, ProgramPoint point) cons auto usesAtPoint = std::ranges::equal_range(m_uses, std::pair { cfg.reversePostOrderIndex(point.block), point.index }, { }, [&](const AllocationAtPoint& use) { return std::pair { cfg.reversePostOrderIndex(use.point.block), use.point.index }; }); - auto use = std::ranges::find(usesAtPoint, allocation, &AllocationAtPoint::allocation); - if (use == usesAtPoint.end()) - return false; - if (!m_unchanged) - m_unchanged = findUnchangedAllocations(m_context, m_uses.span()); - return m_unchanged->get(use - m_uses.begin()); + // An object that is assigned to itself is listed as the source and as the target. + bool isListed = false; + for (auto use = usesAtPoint.begin(); use != usesAtPoint.end(); ++use) { + if (use->allocation != allocation) + continue; + if (!m_unchanged) + m_unchanged = findUnchangedAllocations(m_context, m_uses.span()); + if (!m_unchanged->get(use - m_uses.begin())) + return false; + isListed = true; + } + return isListed; } static BuiltinID calledBuiltinOf(const BuiltinTable& builtins, const Value& call) diff --git a/Source/JavaScriptCore/aot/passes/AOTExpandOwnKeysInternal.h b/Source/JavaScriptCore/aot/passes/AOTExpandOwnKeysInternal.h index 5b4188bddf4f6..594f836b542f5 100644 --- a/Source/JavaScriptCore/aot/passes/AOTExpandOwnKeysInternal.h +++ b/Source/JavaScriptCore/aot/passes/AOTExpandOwnKeysInternal.h @@ -60,6 +60,8 @@ Vector findKeyListsToExpand(FunctionPassContext&); struct AllocationAtPoint { ValueID allocation; ProgramPoint point; + // An assignment asks the objects above its target for a setter. + bool isAssignedTo { false }; }; // Answers for the sources and targets of all the sites of a function with one solve. diff --git a/Source/JavaScriptCore/aot/tests/passes/expand-own-keys-assign-to-a-literal-that-was-given-a-prototype.ir b/Source/JavaScriptCore/aot/tests/passes/expand-own-keys-assign-to-a-literal-that-was-given-a-prototype.ir new file mode 100644 index 0000000000000..d62c6aff63349 --- /dev/null +++ b/Source/JavaScriptCore/aot/tests/passes/expand-own-keys-assign-to-a-literal-that-was-given-a-prototype.ir @@ -0,0 +1,31 @@ +# An assignment asks the objects above its target for a setter, which runs between the reads of the source. The expansion reads every field first, +# so Object.assign to a literal that was given a prototype, as `{ __proto__: above }` and Object.create(above) are, stays a call +# FORM: Prepared +# OPTIONS: heat=EverythingIsHot check=VerifyEachPass +# RUN: pass ExpandOwnKeys +# STATISTIC: OwnKeyWalksExpanded = 1 + +module m0 path="main.js" displayPath="main.js" +layout L0 name="Empty" +layout L1 name="Defaults" fields=[{key="a" declared={type=number}}, {key="b" declared={type=string}}] flow={constructorOrders=[{fields=[0, 1]}]} + +function f0 name="givenPrototype" parameters=[{type=object}, {type=object}] results=[{type=anything}] { + b0(v0: object ~ L1, v1: object): + v2 = NewObject layout=L0 fields=[] + SetLiteralPrototype v2, v1 + v4 = CallBuiltin -, v2, v0 overload=ObjectAssign/0 + Return v4 +# CHECK: SetLiteralPrototype v2, v1 +# CHECK-NEXT: v4:{{*}}= CallBuiltin -, v2, v0 overload=ObjectAssign/0 +# CHECK-NEXT: Return v4 +# CHECK-NOT: PropSet +} + +function f1 name="ordinaryPrototype" parameters=[{type=object}] results=[{type=anything}] { + b0(v0: object ~ L1): + v1 = NewObject layout=L0 fields=[] + v2 = CallBuiltin -, v1, v0 overload=ObjectAssign/0 + Return v2 +# CHECK: PropSet v1, {{*}} key="a" +# CHECK-NEXT: PropSet v1, {{*}} key="b" +} diff --git a/Source/JavaScriptCore/aot/tests/passes/remove-unread-fields-that-a-built-in-deletes-by-key.ir b/Source/JavaScriptCore/aot/tests/passes/remove-unread-fields-that-a-built-in-deletes-by-key.ir new file mode 100644 index 0000000000000..e67e69ff8fd1d --- /dev/null +++ b/Source/JavaScriptCore/aot/tests/passes/remove-unread-fields-that-a-built-in-deletes-by-key.ir @@ -0,0 +1,31 @@ +# Reflect.deleteProperty answers false where the key is there and may not go, as in a frozen object, and true where there is no such key. +# So flow does not publish a field that it may name as never read, though no read reaches it, and the field stays. The field of a frozen object that nothing is deleted from is removed +# FORM: AsRead +# RUN: pipeline RemoveUnreadFields +# OPTIONS: only-passes=ComputeEffectSummaries,ResolveCalls,ResolveAccesses,EliminateDeadCode,RemoveUnreadFields +# STATISTIC: UnreadFieldsRemoved = 1 + +module m0 path="main.js" displayPath="main.js" body=f0 +entry module=m0 +layout L0 name="DeletedFrom" fields=[{key="tag" declared={type=anything} flags=[IsRequired]}] +layout L1 name="Frozen" fields=[{key="tag" declared={type=anything} flags=[IsRequired]}] +# CHECK-NOT: layout L0 name="DeletedFrom" {{*}}reads=NeverRead +# CHECK: layout L1 name="Frozen" {{*}}initialization=SetAtCreation reads=NeverRead + +function f0 name="main" kind=Module results=[{type=anything}] { + v0 = Builtin builtin=GlobalThis + v1 = Integer integer=1 + v2 = PropGet v0 key="Object" [isFreeName] + v3 = PropGet v2 key="freeze" + v4 = PropGet v0 key="Reflect" [isFreeName] + v5 = PropGet v4 key="deleteProperty" + v6 = String string="tag" + v7 = NewObject -, v1 layout=L0 fields=[0] + v8 = Call v3, v2, v7 + v9 = Call v5, v4, v7, v6 + v10 = NewObject -, v1 layout=L1 fields=[0] + v11 = Call v3, v2, v10 + Return v9 +# CHECK: layout=L0 fields=[0] +# CHECK: = NewObject layout=L1 allocationSite= +} diff --git a/Source/JavaScriptCore/aot/valueflow/AOTValueFlowResultsUses.cpp b/Source/JavaScriptCore/aot/valueflow/AOTValueFlowResultsUses.cpp index cfc6cd08dee8d..c23d461bba17a 100644 --- a/Source/JavaScriptCore/aot/valueflow/AOTValueFlowResultsUses.cpp +++ b/Source/JavaScriptCore/aot/valueflow/AOTValueFlowResultsUses.cpp @@ -27,9 +27,12 @@ struct ValuesWithOneUse { } // anonymous namespace -static LookOfPropertyAction lookOfAction(BuiltinPropertyActionKind kind) +static LookOfPropertyAction lookOfAction(const BuiltinPropertyAction& action) { - switch (kind) { + switch (action.kind) { + // A delete answers, or throws, by whether the key is there and may go. An index is the key of no field. + case BuiltinPropertyActionKind::DeletesByKey: + return action.key.kind == BuiltinKeyKind::Indexes ? LookOfPropertyAction::None : LookOfPropertyAction::TestsKey; case BuiltinPropertyActionKind::TestsByKey: case BuiltinPropertyActionKind::ReadsGetter: case BuiltinPropertyActionKind::ReadsSetter: @@ -40,7 +43,6 @@ static LookOfPropertyAction lookOfAction(BuiltinPropertyActionKind kind) return LookOfPropertyAction::ListsKeysOfValue; case BuiltinPropertyActionKind::ReadsByKey: case BuiltinPropertyActionKind::StoresByKey: - case BuiltinPropertyActionKind::DeletesByKey: case BuiltinPropertyActionKind::DefinesName: case BuiltinPropertyActionKind::TakesDescriptor: case BuiltinPropertyActionKind::ChangesIntegrity: @@ -63,7 +65,7 @@ static bool looksAtObjects(const BuiltinOverload& overload) if ((overload.receiver && listsKeys(*overload.receiver)) || std::ranges::any_of(overload.parameters, listsKeys)) return true; return std::ranges::any_of(overload.flow.propertyActions, [](const BuiltinPropertyAction& action) { - return lookOfAction(action.kind) != LookOfPropertyAction::None; + return lookOfAction(action) != LookOfPropertyAction::None; }); } @@ -122,7 +124,7 @@ static void forEachLookAtOperands(const ValueFlowResultInputs& inputs, BuiltinID for (const BuiltinParameter& parameter : overload.parameters) lookAtParameter(parameter); for (const BuiltinPropertyAction& action : overload.flow.propertyActions) { - switch (lookOfAction(action.kind)) { + switch (lookOfAction(action)) { case LookOfPropertyAction::TestsKey: lookAtTest(action); break; From 2899c12fc4153e948ae81378ce86acadefb72f7a Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Sun, 1 Nov 2026 14:21:35 -0800 Subject: [PATCH 19/49] AOT: the file of what the flow analysis hands on has no blind spot Behind "a build can write down what the flow analysis hands on": a check that simply stays, and a call whose callees nobody knows, had no line, so two builds that differ in hundreds of them read alike. Every check and every call of a value in a function that is entered has a line now. A tool for testing: nothing changes where the word is unset. Goes with Bun's half. --- .../aot/valueflow/AOTFlowApplication.h | 5 +++ .../aot/valueflow/AOTFlowApply.cpp | 38 +++++++++++++++---- .../aot/valueflow/AOTValueFlowResults.cpp | 24 ++++++++++++ 3 files changed, 59 insertions(+), 8 deletions(-) diff --git a/Source/JavaScriptCore/aot/valueflow/AOTFlowApplication.h b/Source/JavaScriptCore/aot/valueflow/AOTFlowApplication.h index edb042109347d..e8fafa4a9fc16 100644 --- a/Source/JavaScriptCore/aot/valueflow/AOTFlowApplication.h +++ b/Source/JavaScriptCore/aot/valueflow/AOTFlowApplication.h @@ -222,8 +222,13 @@ struct FlowApplication { Vector> typesTestedBehindDictionaries; BitVector typesNamingDictionaries; BitVector layoutSetsNamingDictionaries; + // Checks, and calls of a value, whose operand holds nothing. Ascending. Only for the file of flow's facts. + Vector> operationsThatNothingArrivesAt; }; +// BUN_JSC_dumpAOTIR=FlowFacts= +StringView pathOfFlowFacts(); + TypeID typeBehindDictionaries(std::span> ascendingTypes, TypeID); struct DictionaryTypeRewriter { diff --git a/Source/JavaScriptCore/aot/valueflow/AOTFlowApply.cpp b/Source/JavaScriptCore/aot/valueflow/AOTFlowApply.cpp index baf0655e3d402..7d330ed5bc0ed 100644 --- a/Source/JavaScriptCore/aot/valueflow/AOTFlowApply.cpp +++ b/Source/JavaScriptCore/aot/valueflow/AOTFlowApply.cpp @@ -1281,15 +1281,21 @@ static void expectAccessorsDefinedBeforeInstances(Program& program, const FlowRe } } +StringView pathOfFlowFacts() +{ + StringView points = StringView::fromLatin1(byteCast(Options::dumpAOTIR())); + return points.startsWith("FlowFacts="_s) ? points.substring(10) : StringView(); +} + // BUN_JSC_dumpAOTIR=FlowFacts=. What flow hands this pass, a line a fact, each thing named by its place in the source, so that two builds can be compared: // a program and the same with more code, or one program under two orders of the rules. test/aot/harness/flow-facts.ts in Bun reads it. // "%p" stands for the process. The second compile of a process writes .1. static std::unique_ptr openFileOfFlowFacts() { - StringView points = StringView::fromLatin1(byteCast(Options::dumpAOTIR())); - if (!points.startsWith("FlowFacts="_s)) [[likely]] + StringView path = pathOfFlowFacts(); + if (path.isEmpty()) [[likely]] return nullptr; - String name = makeStringByReplacingAll(points.substring(10).toString(), "%p"_s, String::number(getCurrentProcessID())); + String name = makeStringByReplacingAll(path.toString(), "%p"_s, String::number(getCurrentProcessID())); static std::atomic filesWritten; unsigned number = filesWritten++; return FilePrintStream::open((number ? makeString(name, '.', number) : name).utf8(), "wb"_s); @@ -1325,21 +1331,37 @@ static void writeFlowFacts(const Program& program, const FlowResults& results, c return makeString(function.locationOf(value).position, '.', static_cast(function.at(value).opcode), '.', ordinals[value.index()]); }; const FlowBodyUpdate& body = application.bodies[index]; - for (const FlowCallStamp& stamp : body.calls) { - const CalleeSet& set = program.at(stamp.callees); + // Every check and every call of a value has a line: where flow says nothing of one, it stays as it was read, and that is an answer too. + auto nothingArrivesAt = [&](ValueID operation) { + return std::ranges::binary_search(application.operationsThatNothingArrivesAt, std::pair { id, operation }); + }; + for (uint32_t number = 0; number < function.values.size(); ++number) { + ValueID value(number); + Opcode opcode = function.at(value).opcode; + if (opcode == Opcode::Check) { + const FlowClaimUpdate* claim = findByValue(body.claims.span(), value, &FlowClaimUpdate::claim); + out.println(claim && claim->isRedundant ? "K" : nothingArrivesAt(value) ? "n" : "k", "\t", self, "\t", siteOf(value), "\t"); + continue; + } + if (flowRuleOf(opcode) != FlowRule::Call) + continue; + const FlowCallStamp* stamp = findByValue(body.calls.span(), value, &FlowCallStamp::value); + if (!stamp) { + out.println(nothingArrivesAt(value) ? "n" : "c", "\t", self, "\t", siteOf(value), "\t"); + continue; + } + const CalleeSet& set = program.at(stamp->callees); Vector callees; for (FunctionID callee : set.written().functions) callees.append(placeOf(callee)); for (BuiltinID builtin : set.written().builtins) callees.append(makeString('@', program.builtins().tokenOf(builtin))); std::ranges::sort(callees, codePointCompareLessThan); - out.print(set.listsEveryCallee() ? "C" : "c", "\t", self, "\t", siteOf(stamp.value), "\t"); + out.print(set.listsEveryCallee() ? "C" : "c", "\t", self, "\t", siteOf(value), "\t"); for (const String& callee : callees) out.print(callee, " "); out.println(); } - for (const FlowClaimUpdate& claim : body.claims) - out.println(claim.isRedundant ? "K" : "k", "\t", self, "\t", siteOf(claim.claim), "\t"); } for (uint32_t index = 0; index < application.layouts.size(); ++index) { const Layout& layout = program.at(LayoutID(index)); diff --git a/Source/JavaScriptCore/aot/valueflow/AOTValueFlowResults.cpp b/Source/JavaScriptCore/aot/valueflow/AOTValueFlowResults.cpp index d5bf779d25ba8..78fbbb013bc39 100644 --- a/Source/JavaScriptCore/aot/valueflow/AOTValueFlowResults.cpp +++ b/Source/JavaScriptCore/aot/valueflow/AOTValueFlowResults.cpp @@ -338,6 +338,28 @@ class ResultSteps final : public ThreadSafeRefCounted { } // anonymous namespace +static Vector> findOperationsThatNothingArrivesAt(const ValueFlowResultInputs& inputs) +{ + Vector> found; + BorrowedValueSetMaker maker(inputs.makerPool()); + auto add = [&](FunctionID function, ValueID operation, const ValueFlowOperand& operand) { + if (inputs.sets().isEmpty(inputs.possibleValuesOf(maker.get(), operand))) + found.append({ function, operation }); + }; + for (const ValueFlowObservedValue& observed : inputs.bodies().observedValues()) { + if (observed.test && inputs.program.at(observed.function).at(observed.test).opcode == Opcode::Check) + add(observed.function, observed.test, observed.operand); + } + for (uint32_t index = 0; index < inputs.program.functions.size(); ++index) { + for (const ValueFlowStatement& statement : inputs.bodies().statementsOf(index)) { + if (statement.kind == ValueFlowStatementKind::Operation && flowRuleOf(statement.opcode) == FlowRule::Call) + add(FunctionID(index), statement.site, inputs.bodies().operandsOf(statement)[0]); + } + } + AOT::sort(found); + return found; +} + FlowResults analyzeValueFlow(Program& program, TypeTable& types, BuildContext& build, const ListenerForSetsOutsideTypes* setsOutsideTypes, const JudgeOfUsesOfLibraryCode* goesOn) { RELEASE_ASSERT(&types == &program.types); @@ -601,6 +623,8 @@ FlowResults analyzeValueFlow(Program& program, TypeTable& types, BuildContext& b inputs.ledgerOfVariables = nullptr; build.destroyLater(WTF::move(ledgerOfVariables)); build.destroyLater(WTF::move(ledgerOfBuiltinObjects)); + if (!pathOfFlowFacts().isEmpty()) [[unlikely]] + tables.application->operationsThatNothingArrivesAt = findOperationsThatNothingArrivesAt(inputs); build.destroyLater(WTF::move(solution)); return FlowResults(WTF::move(tables)); } From 2ea1865ee9b8459e218b9594fa1963c06c94abe1 Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Sun, 1 Nov 2026 14:21:35 -0800 Subject: [PATCH 20/49] AOT: Bun read under a key that nobody knows keeps what code of the library makes, not every member It narrows "Bun read under a key that nobody knows keeps every member". Where that rule fired, all 115 members of Bun counted as used, Bun.FFI and Bun.Transpiler among them, and `--trim` kept the run time whole: one large application grew from 428.9 to 475.8 MB. Only the 16 members that code of the library makes ever got the bare stand-in whose members read `undefined`; they alone are kept now. What native code makes is there whoever reads it. `typeof Bun[d].braces` stays `function`. Goes with Bun's half. --- .../aot/valueflow/AOTValueFlowRulesHeapLookups.cpp | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/Source/JavaScriptCore/aot/valueflow/AOTValueFlowRulesHeapLookups.cpp b/Source/JavaScriptCore/aot/valueflow/AOTValueFlowRulesHeapLookups.cpp index a5fa905f4f7b4..ec910fdcb0bd4 100644 --- a/Source/JavaScriptCore/aot/valueflow/AOTValueFlowRulesHeapLookups.cpp +++ b/Source/JavaScriptCore/aot/valueflow/AOTValueFlowRulesHeapLookups.cpp @@ -1315,13 +1315,14 @@ void HeapRule::readMembers(ClimbingSet objects, ClimbingSet start, const HeapKey openProxiesThatMemberAnswers(m_context, member); exposeObjectsOfModulesReturnedBy(m_context, member); } - // A key nobody knows may be any name. + // A key nobody knows may be any name. What native code makes is there whoever reads it; what code of the library makes is there only if that code is. if (isBun && (!isReceiver || isHostFlagRaised(m_context, ValueFlowHostNode::NamedBunIsReadUnderKeysNobodyKnows))) { - for (BuiltinID member : m_builtins.membersOf(m_universe.builtinOf(holder))) - readMember(member, ModuleID(), result, getters); - return; + for (BuiltinID member : m_builtins.membersOf(m_universe.builtinOf(holder))) { + if (isMadeWithCodeOfLibrary(m_builtins, member)) + readMember(member, ModuleID(), result, getters); + } } - // On a Bun that nothing in the program names it may be a name that has escaped. + // It may be a name that has escaped. ClimbingSet names(m_tables.keysOfMembersOfBun()); if (isBun && !key.namesEnumerableProperty && !m_context.isNativeWalk() && holdsAnything(m_context.membersMissingFrom(ValueFlowProgramNodes::escaped(), names))) { m_context.forEachMember(escapedAmong(names), [&](ValueFlowObjectID name) { From e5e3161563c80ffce2863e316dc9bc34121930b0 Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Sun, 1 Nov 2026 14:47:09 -0800 Subject: [PATCH 21/49] AOT: testaot asks for its process id the portable way It came with "testaot can ask the laws of the algebra of types of every type of a program": `` and getpid(), for the name of the file that TESTAOT_LISTS_BROKEN_LAWS_OF_TYPES asks for. The Windows lanes have no such header: "fatal error: 'unistd.h' file not found". getCurrentProcessID() of instead. A test tool alone. --- Source/JavaScriptCore/aot/tests/AOTTestRunner.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Source/JavaScriptCore/aot/tests/AOTTestRunner.cpp b/Source/JavaScriptCore/aot/tests/AOTTestRunner.cpp index b3cee2a26de94..e113394f8562a 100644 --- a/Source/JavaScriptCore/aot/tests/AOTTestRunner.cpp +++ b/Source/JavaScriptCore/aot/tests/AOTTestRunner.cpp @@ -21,9 +21,9 @@ #include "JSCConfig.h" #include "Options.h" #include -#include #include #include +#include #include #include #include @@ -248,7 +248,7 @@ static CommandResult runCommand(const TestCommand& command, const TestFile& file StringPrintStream broken; listBrokenLawsOfTypes(broken, *context.program, 160); char pathOfProcess[1024]; - snprintf(pathOfProcess, sizeof(pathOfProcess), "%s.%d", path, static_cast(getpid())); + snprintf(pathOfProcess, sizeof(pathOfProcess), "%s.%d", path, static_cast(getCurrentProcessID())); if (FILE* list = fopen(pathOfProcess, "a")) { auto text = broken.toStringWithLatin1Fallback().utf8(); size_t endOfLine = file.text.find('\n'); From 5aee766c542ac53d844e4e03c771feb05bd6a758 Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Sun, 1 Nov 2026 15:29:29 -0800 Subject: [PATCH 22/49] AOT: a long text with accents is encoded by the library `new TextEncoder().encode(text)` and `.encodeInto()` of a Latin-1 text with accents took 10 to 14 times as long in an executable as under `bun run`: one scalar loop counted the text and a second wrote it a byte at a time. Over 16 characters simdutf does both now, as plain Bun's encoder does; up to 16 characters the old loops stay. Goes with Bun's half. --- .../aot/runtime/AOTTypedArrayOperations.cpp | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/Source/JavaScriptCore/aot/runtime/AOTTypedArrayOperations.cpp b/Source/JavaScriptCore/aot/runtime/AOTTypedArrayOperations.cpp index 489ef9c22b9b6..3d6d2ae782f46 100644 --- a/Source/JavaScriptCore/aot/runtime/AOTTypedArrayOperations.cpp +++ b/Source/JavaScriptCore/aot/runtime/AOTTypedArrayOperations.cpp @@ -29,6 +29,7 @@ #include "JSTypedArrays.h" #include "ObjectConstructor.h" #include "StructureCache.h" +#include #include #include @@ -100,6 +101,9 @@ static ALWAYS_INLINE std::optional> latin1Chara return impl->span8(); } +// Up to here a loop over the characters is quicker than a call of the library. +static constexpr size_t fewCharacters = 16; + struct ReadAndWritten { size_t read { 0 }; size_t written { 0 }; @@ -108,6 +112,9 @@ struct ReadAndWritten { // As many whole characters as there is room for. static NEVER_INLINE ReadAndWritten writeLatin1AsUTF8(std::span characters, uint8_t* bytes, size_t room) { + const char* from = byteCast(characters.data()); + if (characters.size() > fewCharacters && simdutf::utf8_length_from_latin1(from, characters.size()) <= room) [[likely]] + return { characters.size(), simdutf::convert_latin1_to_utf8(from, characters.size(), byteCast(bytes)) }; ReadAndWritten counts; for (Latin1Character character : characters) { uint8_t unit = static_cast(character); @@ -170,9 +177,14 @@ JSC_DEFINE_JIT_OPERATION(operationAOTEncodeTextToBytes, EncodedJSValue, (Instanc } } bool isASCII = charactersAreAllASCII(*characters); + bool isLong = size > fewCharacters; if (!isASCII) [[unlikely]] { - for (Latin1Character character : *characters) - size += static_cast(character) >> 7; + if (isLong) + size = simdutf::utf8_length_from_latin1(byteCast(from), size); + else { + for (Latin1Character character : *characters) + size += static_cast(character) >> 7; + } } JSUint8Array* bytes = size && size <= JSArrayBufferView::fastSizeLimit ? tryCreateFewBytes(vm, globalObject, size) : nullptr; if (!bytes) [[unlikely]] { @@ -181,6 +193,8 @@ JSC_DEFINE_JIT_OPERATION(operationAOTEncodeTextToBytes, EncodedJSValue, (Instanc } if (isASCII) [[likely]] memcpy(bytes->typedVector(), from, size); + else if (isLong) + simdutf::convert_latin1_to_utf8(byteCast(from), characters->size(), byteCast(bytes->typedVector())); else writeLatin1AsUTF8(*characters, bytes->typedVector(), size); ensureStillAliveHere(source); From 82555ca392f950d97336ebad06da282706eedc95 Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Sun, 1 Nov 2026 15:29:29 -0800 Subject: [PATCH 23/49] AOT: a call for a module of a graph under way settles behind the call for the graph main: `import("./a.mjs").then(x); import("./b.mjs").then(y)`; a.mjs: `import "./c.mjs"`; b.mjs: `await import("./c.mjs")`. node, `bun run` and `bun build --compile`: b begins, c runs, a runs, x, b ends, y. Compiled ahead of time: b begins, c, a, b ends, x, y, without an error. The modules ran the same; the order in which the calls settle didn't. Goes with Bun's half. --- Source/JavaScriptCore/aot/runtime/AOTModules.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Source/JavaScriptCore/aot/runtime/AOTModules.cpp b/Source/JavaScriptCore/aot/runtime/AOTModules.cpp index 493a4585f0c86..ddfaca51e4b27 100644 --- a/Source/JavaScriptCore/aot/runtime/AOTModules.cpp +++ b/Source/JavaScriptCore/aot/runtime/AOTModules.cpp @@ -4995,7 +4995,10 @@ Modules::LoadInFlight* Modules::findEarlierLoadAbove(ModuleID module) continue; } // The call for that graph came in an earlier job. - if ((isInHandsOfLoader || load.isOfEarlierJob || load.hasSeenJob) && shareCycleLeftToRun(module, std::span(&load.root, 1))) + if (!isInHandsOfLoader && !load.isOfEarlierJob && !load.hasSeenJob) + continue; + // A module of a graph that runs in one go runs with it, and the call for the module alone settles behind the call for the graph. + if ((data(load.root).isSynchronous() && leadsTo(load.root, module)) || shareCycleLeftToRun(module, std::span(&load.root, 1))) return &load; } return nullptr; From 3ca89f1c85f619d19f3b4c370ac826d8e0e7bf3f Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Sun, 1 Nov 2026 15:29:29 -0800 Subject: [PATCH 24/49] AOT: a refusal that the run time cannot decide decides no exact test At the IR alone: no JavaScript was found that shows it. `v1 = TypeTest v0 testedType=builtin(ImportMeta)`; on its refusal `v2 = AssumeType v0 provenType=number|builtin(ImportMeta); v3 = TypeTest v2 testedType=builtin(ImportMeta)`: the verifier admits it, and SimplifyControlFlow decided v3 false and assumed a number. With an IR test. --- .../JavaScriptCore/aot/ir/AOTEdgeResults.cpp | 20 ++- Source/JavaScriptCore/aot/ir/AOTEdgeResults.h | 2 + .../aot/passes/AOTDecidedBranches.cpp | 41 +++-- .../aot/passes/AOTDecidedBranches.h | 9 +- .../passes/decided-test-for-import-meta.ir | 169 ++++++++++++++++++ 5 files changed, 216 insertions(+), 25 deletions(-) create mode 100644 Source/JavaScriptCore/aot/tests/passes/decided-test-for-import-meta.ir diff --git a/Source/JavaScriptCore/aot/ir/AOTEdgeResults.cpp b/Source/JavaScriptCore/aot/ir/AOTEdgeResults.cpp index d7ee051d0d053..aeb4ae9296afb 100644 --- a/Source/JavaScriptCore/aot/ir/AOTEdgeResults.cpp +++ b/Source/JavaScriptCore/aot/ir/AOTEdgeResults.cpp @@ -192,19 +192,23 @@ static Type narrowByTruthiness(const TypingContext& context, const Type& source, return falsy; } +bool mayRefuseUntestableBuiltins(const Program& program, const Type& source, const Type& tested) +{ + bool namesUntestableBuiltin = std::ranges::any_of(tested.builtins, [&](BuiltinID builtin) { + return !isTestableBuiltin(program.builtins(), builtin); + }); + return namesUntestableBuiltin && !source.withOnlyObjects().isSubsetOf(tested, program); +} + static Type narrowByFailedTest(const TypingContext& context, const Type& source, const Type& tested) { - const BuiltinTable& builtins = context.program.builtins(); - auto isUntestable = [&](BuiltinID builtin) { - return !isTestableBuiltin(builtins, builtin); - }; - if (std::ranges::none_of(tested.builtins, isUntestable)) - return source.without(tested, context.types); - if (source.withOnlyObjects().isSubsetOf(tested, context.program)) + if (!mayRefuseUntestableBuiltins(context.program, source, tested)) return source.without(tested, context.types); Type recognized = tested; - removeAllMatching(recognized.builtins, isUntestable); + removeAllMatching(recognized.builtins, [&](BuiltinID builtin) { + return !isTestableBuiltin(context.program.builtins(), builtin); + }); return source.without(recognized, context.types); } diff --git a/Source/JavaScriptCore/aot/ir/AOTEdgeResults.h b/Source/JavaScriptCore/aot/ir/AOTEdgeResults.h index 76a28bb493346..d16b1d6a912a1 100644 --- a/Source/JavaScriptCore/aot/ir/AOTEdgeResults.h +++ b/Source/JavaScriptCore/aot/ir/AOTEdgeResults.h @@ -54,6 +54,8 @@ Type narrowOperandByTest(const TypingContext&, const Value& test, uint32_t opera Type narrowSubjectByCase(const TypingContext&, const Terminator&, uint32_t caseIndex); Type narrowTypeByTest(const TypingContext&, const Type& source, const Value& test, uint32_t operandIndex, EdgeSide); +// No test finds a built-in that each module has one of. Among other objects the test refuses it. Where the value may be no other object, the test asks for an object. +bool mayRefuseUntestableBuiltins(const Program&, const Type& source, const Type& tested); Type narrowTypeByCase(const TypingContext&, const Type& source, const Terminator&, uint32_t caseIndex); Type narrowTypeByOtherwise(const Type& source, const Terminator&); diff --git a/Source/JavaScriptCore/aot/passes/AOTDecidedBranches.cpp b/Source/JavaScriptCore/aot/passes/AOTDecidedBranches.cpp index fecb6a3ee0270..7c297b6649003 100644 --- a/Source/JavaScriptCore/aot/passes/AOTDecidedBranches.cpp +++ b/Source/JavaScriptCore/aot/passes/AOTDecidedBranches.cpp @@ -158,12 +158,17 @@ bool mayRefuseChangedClosures(const Type& typeOfTestedValue) return typeOfTestedValue.anyFunction || typeOfTestedValue.anyObject; } -static bool mayRefuseChangedClosures(FunctionAnalyses& analyses, const Value& test) +static OptionSet falseRefusalsOf(FunctionAnalyses& analyses, const Value& test) { const Program& program = analyses.program(); - if (analyses.context().build.isSabotaged(Sabotage::ChangedClosuresFailEveryTest)) - return false; - return !program.at(test.testedType()).functions.isEmpty() && mayRefuseChangedClosures(program.typeOf(analyses.function(), test.valueOperand())); + const Type& tested = program.at(test.testedType()); + const Type& typeOfTestedValue = program.typeOf(analyses.function(), test.valueOperand()); + OptionSet refusals; + if (!tested.functions.isEmpty() && mayRefuseChangedClosures(typeOfTestedValue) && !analyses.context().build.isSabotaged(Sabotage::ChangedClosuresFailEveryTest)) + refusals.add(FalseRefusal::ChangedClosure); + if (mayRefuseUntestableBuiltins(program, typeOfTestedValue, tested)) + refusals.add(FalseRefusal::UntestableBuiltin); + return refusals; } void keepRepeatedTests(FunctionAnalyses& analyses, Vector& branches) @@ -190,7 +195,7 @@ TypeTestBranches::TypeTestBranches(FunctionAnalyses& analyses) m_branchesByOperand.append(BranchOnValue { operand, block }); }); if (const Value* test = typeTestBranchedOn(function, function.at(block))) { - m_branches.append({ test->valueOperand(), test->testedType(), block, mayRefuseChangedClosures(analyses, *test) }); + m_branches.append({ test->valueOperand(), test->testedType(), block, falseRefusalsOf(analyses, *test) }); m_blocksThatBranchOnTypeTest.append(block); } } @@ -207,38 +212,42 @@ TypeTestBranches::TypeTestBranches(FunctionAnalyses& analyses) static Vector outcomesOfFrequentTests(FunctionAnalyses& analyses, std::span branches) { - // Two groups for a test: an even one for the branches that refuse only what is none of the functions, and the next for the others. + // A group of a test for each set of false refusals. + static constexpr uint32_t groupsOfTest = 4; + static_assert(OptionSet { FalseRefusal::ChangedClosure, FalseRefusal::UntestableBuiltin }.toRaw() == groupsOfTest - 1); Vector groupOfBlock(FillWith { }, analyses.cfg().numNodes(), noGroup); uint32_t numberOfGroups = 0; forEachRunOfEqualKeys(branches, &BranchOnTypeTest::test, [&](std::span branchesOnTest) { if (branchesOnTest.size() <= maxComparedBranches) return; for (const BranchOnTypeTest& branch : branchesOnTest) - groupOfBlock[branch.block.index()] = numberOfGroups + branch.mayRefuseChangedClosures; - numberOfGroups += 2; + groupOfBlock[branch.block.index()] = numberOfGroups + branch.falseRefusals.toRaw(); + numberOfGroups += groupsOfTest; }); return outcomesBehindOutermostEdges(analyses, numberOfGroups, [&](BlockID block, KnownBoolean outcome, const ScopedLambda& functor) { uint32_t group = groupOfBlock[block.index()]; if (group == noGroup) return; - functor(group | 1); - if (!(group & 1) || outcome == KnownBoolean::True) - functor(group & ~1u); + uint32_t refusals = group % groupsOfTest; + for (uint32_t refusalsOfAsked = 0; refusalsOfAsked < groupsOfTest; ++refusalsOfAsked) { + if (outcome == KnownBoolean::True || !(refusals & ~refusalsOfAsked)) + functor(group - refusals + refusalsOfAsked); + } }, [&](BlockID block) { return groupOfBlock[block.index()]; }); } -// What a value is tested for never changes, so behind an edge of one test every other test of the value for the same type has the same outcome. A test that may refuse a changed closure decides by its refusal only tests that may do so too. +// What a value is tested for never changes, so behind an edge of one test every other test of the value for the same type has the same outcome. A test that may refuse what it is for decides by its refusal only tests that may refuse the same. KnownBoolean TypeTestBranches::outcomeFromDominatingTest(BlockID block) const { const Value* test = m_repeatsTest.get(block.index()) ? typeTestBranchedOn(m_analyses.function(), m_analyses.function().at(block)) : nullptr; if (!test) return KnownBoolean::Unknown; std::tuple tested { m_analyses.identityOf(test->valueOperand()).index(), test->testedType().index() }; - bool everyRefusalDecides = m_branches[m_indexOfBranch[block.index()]].mayRefuseChangedClosures; + OptionSet falseRefusals = m_branches[m_indexOfBranch[block.index()]].falseRefusals; auto refusalDecides = [&](const BranchOnTypeTest& branch) { - return everyRefusalDecides || !branch.mayRefuseChangedClosures; + return falseRefusals.containsAll(branch.falseRefusals); }; return outcomeBehindEdges(m_analyses, std::ranges::equal_range(m_branches, tested, { }, &BranchOnTypeTest::test), block, m_outcomesOfFrequentTests, refusalDecides, [&] { return outcomesOfFrequentTests(m_analyses, m_branches.span()); @@ -264,7 +273,7 @@ KnownBoolean TypeTestBranches::outcomeOnEveryWay(BlockID block) const return KnownBoolean::Unknown; const size_t firstBranchOnTest = branchesOnTest.begin() - m_branches.begin(); - bool everyRefusalDecides = m_branches[m_indexOfBranch[block.index()]].mayRefuseChangedClosures; + OptionSet falseRefusals = m_branches[m_indexOfBranch[block.index()]].falseRefusals; auto branchesOnSameTest = [&](BlockID from) { return m_repeatsTest.get(from.index()) && m_indexOfBranch[from.index()] - firstBranchOnTest < branchesOnTest.size(); }; @@ -281,7 +290,7 @@ KnownBoolean TypeTestBranches::outcomeOnEveryWay(BlockID block) const KnownBoolean onEdge = outcomeOnEdge(edge); if (onEdge == KnownBoolean::Unknown || (outcome != KnownBoolean::Unknown && outcome != onEdge)) return KnownBoolean::Unknown; - if (onEdge == KnownBoolean::False && !everyRefusalDecides && m_branches[m_indexOfBranch[edge.from.index()]].mayRefuseChangedClosures) + if (onEdge == KnownBoolean::False && !falseRefusals.containsAll(m_branches[m_indexOfBranch[edge.from.index()]].falseRefusals)) return KnownBoolean::Unknown; outcome = onEdge; continue; diff --git a/Source/JavaScriptCore/aot/passes/AOTDecidedBranches.h b/Source/JavaScriptCore/aot/passes/AOTDecidedBranches.h index 93881777e2fef..21e17b8f4021d 100644 --- a/Source/JavaScriptCore/aot/passes/AOTDecidedBranches.h +++ b/Source/JavaScriptCore/aot/passes/AOTDecidedBranches.h @@ -14,6 +14,7 @@ #include #include #include +#include namespace JSC { namespace AOT { namespace Typed { @@ -38,11 +39,17 @@ bool holdsOnlyKnownFunctions(const Type&); // Nothing is left out of the type of a value that may be any function where a test for one function refuses it. Such a test may refuse a closure that is no longer as it was created. bool mayRefuseChangedClosures(const Type& typeOfTestedValue); +// What a test may refuse although it is what the test is for. That hangs on the type of the tested value, so two tests of one value for one type may differ in it. +enum class FalseRefusal : uint8_t { + ChangedClosure = 1 << 0, + UntestableBuiltin = 1 << 1, +}; + struct BranchOnTypeTest { ValueID subject; TypeID testedType; BlockID block; - bool mayRefuseChangedClosures { false }; + OptionSet falseRefusals; auto test() const { return std::tuple { subject.index(), testedType.index() }; } }; diff --git a/Source/JavaScriptCore/aot/tests/passes/decided-test-for-import-meta.ir b/Source/JavaScriptCore/aot/tests/passes/decided-test-for-import-meta.ir new file mode 100644 index 0000000000000..aa2efcee5fcbc --- /dev/null +++ b/Source/JavaScriptCore/aot/tests/passes/decided-test-for-import-meta.ir @@ -0,0 +1,169 @@ +# No test finds import.meta, which each module has one of: among other objects a test for it refuses it, and where the value may be no other object the test asks for an object. The refusal of the first kind decides a test like itself and none of the second kind. +# FORM: Prepared +# RUN: pass SimplifyControlFlow + +module m0 path="main.js" displayPath="main.js" + +function f0 name="onlyObjectBehindTheRefusalAmongAllObjects" parameters=[{type=anything}] results=[{type=anything}] { + v1 = TypeTest v0 testedType=builtin(ImportMeta) + Branch v1 -> taken b4(), notTaken b1() + b1(): + v2 = AssumeType v0 provenType=number|builtin(ImportMeta) + v3 = TypeTest v2 testedType=builtin(ImportMeta) + Branch v3 -> taken b2(| 0), notTaken b3(| 0) + b2(v4): + Return v4 + b3(v5): + v6 = Negate v5 + Return v6 + b4(): + Return v0 +# CHECK: Branch v1 -> taken b4(), notTaken b1() +# CHECK: Branch v3 -> taken b2(| 0), notTaken b3(| 0) +} + +function f1 name="onlyObjectBehindTheRefusalAmongTwoObjects" parameters=[{type=number|builtin(GlobalThis)|builtin(ImportMeta)}] results=[{type=anything}] { + v1 = TypeTest v0 testedType=builtin(ImportMeta) + Branch v1 -> taken b4(), notTaken b1() + b1(): + v2 = AssumeType v0 provenType=number|builtin(ImportMeta) + v3 = TypeTest v2 testedType=builtin(ImportMeta) + Branch v3 -> taken b2(| 0), notTaken b3(| 0) + b2(v4): + Return v4 + b3(v5): + v6 = Negate v5 + Return v6 + b4(): + Return v0 +# CHECK: Branch v1 -> taken b4(), notTaken b1() +# CHECK: Branch v3 -> taken b2(| 0), notTaken b3(| 0) +} + +function f2 name="amongAllObjectsBehindTheRefusalAmongAllObjects" parameters=[{type=anything}, {type=function}] results=[{type=anything}] { + v2 = TypeTest v0 testedType=builtin(ImportMeta) + Branch v2 -> taken b4(), notTaken b1() + b1(): + v3 = Call v1 + v4 = TypeTest v0 testedType=builtin(ImportMeta) + Branch v4 -> taken b2(| 0), notTaken b3() + b2(v5): + Return v5 + b3(): + v6 = Call v1, -, v0 + Return v6 + b4(): + Return v0 +# CHECK: Branch v2 -> taken b4(), notTaken b1() +# CHECK-NOT: Branch +# CHECK: = Call v1, -, v0 +} + +function f3 name="amongAllObjectsBehindTheRefusalOfTheOnlyObject" parameters=[{type=anything}, {type=function}] results=[{type=anything}] { + v2 = AssumeType v0 provenType=number|builtin(ImportMeta) + v3 = TypeTest v2 testedType=builtin(ImportMeta) + Branch v3 -> taken b4(), notTaken b1() + b1(): + v4 = Call v1 + v5 = TypeTest v0 testedType=builtin(ImportMeta) + Branch v5 -> taken b2(| 0), notTaken b3() + b2(v6): + Return v6 + b3(): + v7 = Call v1, -, v0 + Return v7 + b4(): + Return v0 +# CHECK: Branch v3 -> taken b4(), notTaken b1() +# CHECK-NOT: Branch +# CHECK: = Call v1, -, v0 +} + +function f4 name="amongAllObjectsBehindTheOnlyObjectThatPassed" parameters=[{type=anything}, {type=function}] results=[{type=anything}] { + v2 = AssumeType v0 provenType=number|builtin(ImportMeta) + v3 = TypeTest v2 testedType=builtin(ImportMeta) + Branch v3 -> taken b1(), notTaken b4() + b1(): + v4 = Call v1 + v5 = TypeTest v0 testedType=builtin(ImportMeta) + Branch v5 -> taken b2(| 0), notTaken b3() + b2(v6): + Return v6 + b3(): + v7 = Call v1, -, v0 + Return v7 + b4(): + Return v0 +# CHECK: Branch v3 -> taken b1(), notTaken b4() +# CHECK-NOT: Branch +# CHECK-NOT: = Call v1, -, v0 +} + +function f5 name="listedFunctionsBehindARefusalOfBothKinds" parameters=[{type=anything}] results=[{type=anything}] { + v1 = TypeTest v0 testedType=f7|builtin(ImportMeta) + Branch v1 -> taken b4(), notTaken b1() + b1(): + v2 = AssumeType v0 provenType=f7|f8|builtin(ImportMeta) + v3 = TypeTest v2 testedType=f7|builtin(ImportMeta) + Branch v3 -> taken b2(| 0), notTaken b3(| 0) + b2(v4): + v5 = TypeOf v4 + Return v5 + b3(v6): + v7 = Call v6 + Return v7 + b4(): + Return v0 +# CHECK: Branch v1 -> taken b4(), notTaken b1() +# CHECK: Branch v3 -> taken b2(| 0), notTaken b3(| 0) +} + +function f6 name="manyTestsOfOneValue" parameters=[{type=anything}, {type=function}] results=[{type=anything}] { + v2 = AssumeType v0 provenType=number|builtin(ImportMeta) + v3 = TypeTest v0 testedType=builtin(ImportMeta) + Branch v3 -> taken b11(), notTaken b1() + b1(): + v4 = TypeTest v2 testedType=builtin(ImportMeta) + Branch v4 -> taken b11(), notTaken b2() + b2(): + v5 = TypeTest v2 testedType=builtin(ImportMeta) + Branch v5 -> taken b11(), notTaken b3() + b3(): + v6 = TypeTest v2 testedType=builtin(ImportMeta) + Branch v6 -> taken b11(), notTaken b4() + b4(): + v7 = TypeTest v2 testedType=builtin(ImportMeta) + Branch v7 -> taken b11(), notTaken b5() + b5(): + v8 = TypeTest v2 testedType=builtin(ImportMeta) + Branch v8 -> taken b11(), notTaken b6() + b6(): + v9 = TypeTest v2 testedType=builtin(ImportMeta) + Branch v9 -> taken b11(), notTaken b7() + b7(): + v10 = TypeTest v2 testedType=builtin(ImportMeta) + Branch v10 -> taken b11(), notTaken b8() + b8(): + v11 = TypeTest v2 testedType=builtin(ImportMeta) + Branch v11 -> taken b11(), notTaken b9() + b9(): + v12 = TypeTest v0 testedType=builtin(ImportMeta) + Branch v12 -> taken b11(), notTaken b10() + b10(): + v13 = Call v1 + Return v13 + b11(): + Return v1 +# CHECK: Branch v3 -> taken +# CHECK: Branch v4 -> taken +# CHECK-NOT: Branch +# CHECK: = Call v1 +} + +function f7 name="callee" results=[{type=anything}] { + Return +} + +function f8 name="otherCallee" results=[{type=anything}] { + Return +} From 12aa5e1b6934df50c1cfdacb7a80a2bc6949bc4b Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Sun, 1 Nov 2026 15:29:29 -0800 Subject: [PATCH 25/49] AOT: what only an image of the library alone reached is deleted The engine's half of the build option `--compiledLibrary`, which Bun no longer has: two options of the compile call, the flag of an image (its bit stays free; no other flag's bit moves), and what only they reached. It needs a Bun without the option. --- .../aot/analysis/AOTHeatGraph.cpp | 9 ++--- .../aot/builtins/AOTBuiltinTable.h | 4 +- .../aot/builtins/AOTBuiltinTableOfBuild.cpp | 39 ++----------------- Source/JavaScriptCore/aot/image/AOTImage.h | 1 - .../aot/image/AOTImageBuilder.cpp | 2 - .../JavaScriptCore/aot/image/AOTImageFormat.h | 2 - .../JavaScriptCore/aot/image/AOTImagesBelow.h | 1 - Source/JavaScriptCore/aot/ir/AOTIRProgram.h | 2 - Source/JavaScriptCore/aot/ir/AOTReader.cpp | 1 - .../aot/ir/schema/compile-call.def | 4 +- .../aot/place/AOTProgramTableEncoder.cpp | 12 +----- .../aot/runtime/AOTBuiltinCells.cpp | 17 ++------ .../JavaScriptCore/aot/runtime/AOTModules.cpp | 2 +- .../aot/support/AOTBuildContext.cpp | 5 --- .../aot/support/AOTBuildContext.h | 2 - .../aot/support/AOTCompileOptions.cpp | 6 --- .../aot/support/AOTCompileOptions.h | 3 -- .../valueflow/AOTValueFlowResultsBuiltins.cpp | 3 +- 18 files changed, 17 insertions(+), 98 deletions(-) diff --git a/Source/JavaScriptCore/aot/analysis/AOTHeatGraph.cpp b/Source/JavaScriptCore/aot/analysis/AOTHeatGraph.cpp index 573496b98b275..c38a3bb3e061b 100644 --- a/Source/JavaScriptCore/aot/analysis/AOTHeatGraph.cpp +++ b/Source/JavaScriptCore/aot/analysis/AOTHeatGraph.cpp @@ -535,7 +535,7 @@ struct SeenCode { static bool isModuleBodyFunction(const Function& function) { return function.module && function.isModuleBody(); } // A module is loaded by the entry, or by a module that names it. A closure is made by a body that runs. A call of a callee that nobody knows reaches what has been made and may stand there. -static SeenCode findWhatSeenCodeReaches(const Program& program, const CallGraph& callGraph, const FlowResults& flow, const HeatGraph& graph, const ParallelArray& calls, const FunctionsUnderNames& names, const Vector>& functionsMadeBy, const BitVector& isCalledByHost, const BitVector& hasOnlySeenCallers, const Vector>& namesReadBy, const Vector>& accessorsReachedFrom, const Vector>& holdersReadBy, const Vector>& modulesLoadedBy, const Vector& constructsUnknownValue, EstimateOf estimate, bool hasCallersInOtherImages) +static SeenCode findWhatSeenCodeReaches(const Program& program, const CallGraph& callGraph, const FlowResults& flow, const HeatGraph& graph, const ParallelArray& calls, const FunctionsUnderNames& names, const Vector>& functionsMadeBy, const BitVector& isCalledByHost, const BitVector& hasOnlySeenCallers, const Vector>& namesReadBy, const Vector>& accessorsReachedFrom, const Vector>& holdersReadBy, const Vector>& modulesLoadedBy, const Vector& constructsUnknownValue, EstimateOf estimate) { unsigned count = graph.numberOfFunctions(); bool isForClasses = estimate == EstimateOf::Classes; @@ -608,7 +608,7 @@ static SeenCode findWhatSeenCodeReaches(const Program& program, const CallGraph& BitVector isUnderNameThatIsCalled(count); BitVector isUnderNameReadAsValue(count); auto mayStandWhereValueIsCalled = [&](FunctionID function) { - return hasEscaped.quickGet(function.index()) && (hasCallersInOtherImages || !names.isUnderSomeName(function) || isUnderNameReadAsValue.quickGet(function.index()) || seen.isAccessorReachedUnderItsName.quickGet(function.index()) || (evaluatesText && !isLibraryCode(program, program.at(function)))); + return hasEscaped.quickGet(function.index()) && (!names.isUnderSomeName(function) || isUnderNameReadAsValue.quickGet(function.index()) || seen.isAccessorReachedUnderItsName.quickGet(function.index()) || (evaluatesText && !isLibraryCode(program, program.at(function)))); }; bool constructsValue = false; Vector> functionsWaitingFor(program.modules.size()); @@ -638,8 +638,6 @@ static SeenCode findWhatSeenCodeReaches(const Program& program, const CallGraph& reachIfCalled(waiting); } }; - if (hasCallersInOtherImages) - reach(graph.unknownCodeNode().index()); for (const Entry& entry : program.entries) load(entry.module); if (program.entries.isEmpty()) { @@ -1533,7 +1531,6 @@ HeatGraph buildHeatGraph(const Program& program, const CallGraph& callGraph, con }); BitVector noFunction(count); Vector entriesPerEvent; - const bool hasCallersInOtherImages = build.imagesBelow() && build.imagesBelow()->hasPrebuiltLibrary(); auto addCallsOfNodes = [&](EstimateOf estimate) { bool isForClasses = estimate == EstimateOf::Classes; const BitVector& hasOnlySeenCallers = isForClasses ? hasOnlySeenCallersByFlow : noFunction; @@ -1545,7 +1542,7 @@ HeatGraph buildHeatGraph(const Program& program, const CallGraph& callGraph, con if (flow.mayRunUnseenCode()) calls[graph.unknownCodeNode().index()] = shareAmong(program, flow, graph, isCalledPerEvent, seen, callGraph.functionsCalledByUnknownCode()); } else - seen = findWhatSeenCodeReaches(program, callGraph, flow, graph, calls, names, functionsMadeBy, isCalledByHost, hasOnlySeenCallers, namesReadBy, accessorsReachedFrom, holdersReadBy, modulesLoadedBy, constructsUnknownValue, estimate, hasCallersInOtherImages); + seen = findWhatSeenCodeReaches(program, callGraph, flow, graph, calls, names, functionsMadeBy, isCalledByHost, hasOnlySeenCallers, namesReadBy, accessorsReachedFrom, holdersReadBy, modulesLoadedBy, constructsUnknownValue, estimate); Vector eventsOfHandlers(FillWith { }, count, 0.0); if (!isForClasses) { forEachIndexInParallel(loops, graph.m_numberOfNames, [&](unsigned position) { diff --git a/Source/JavaScriptCore/aot/builtins/AOTBuiltinTable.h b/Source/JavaScriptCore/aot/builtins/AOTBuiltinTable.h index 6c0637f76c3d6..1bd07e917019d 100644 --- a/Source/JavaScriptCore/aot/builtins/AOTBuiltinTable.h +++ b/Source/JavaScriptCore/aot/builtins/AOTBuiltinTable.h @@ -1117,13 +1117,11 @@ class BuiltinTableOfBuild { const BuiltinTable& table() const LIFETIME_BOUND { return m_table; } // `members` ascend. Leaves out a locked member that canBeReplacedInTestRuns() refuses. `holdersOfNewNames`: the objects that get a name of their own opened, which the caller keeps. - // `opensEveryHolder`: every object takes whatever name canTakeNameInTestRuns() admits. - void unlock(std::span members, std::span holdersOfNewNames, bool opensEveryHolder = false); + void unlock(std::span members, std::span holdersOfNewNames); std::span unlockedMembers() const LIFETIME_BOUND { return m_unlockedMembers.span(); } private: void lockByNames(BuiltinID wholeObject); - void openToNames(BuiltinID holder); BuiltinTableData m_data; Vector m_builtins; diff --git a/Source/JavaScriptCore/aot/builtins/AOTBuiltinTableOfBuild.cpp b/Source/JavaScriptCore/aot/builtins/AOTBuiltinTableOfBuild.cpp index 9ab8a65c3f8a1..845cba29c3940 100644 --- a/Source/JavaScriptCore/aot/builtins/AOTBuiltinTableOfBuild.cpp +++ b/Source/JavaScriptCore/aot/builtins/AOTBuiltinTableOfBuild.cpp @@ -135,34 +135,7 @@ void BuiltinTableOfBuild::lockByNames(BuiltinID wholeObject) m_closedKeys.insert(position, BuiltinClosedKeys { wholeObject, m_closedSymbols.last().span(), { } }); } -// The names that no test run adds stay closed, each by itself. -void BuiltinTableOfBuild::openToNames(BuiltinID holder) -{ - lockByNames(holder); - Vector closed(m_table.closedKeysOf(holder)); - size_t position = std::ranges::lower_bound(m_closedKeys, holder, { }, &BuiltinClosedKeys::holder) - m_closedKeys.begin(); - bool hasRecord = position < m_closedKeys.size() && m_closedKeys[position].holder == holder; - if (hasRecord) - closed = Vector(m_closedKeys[position].names); - for (uint32_t index = 0; index < m_table.nameCount(); ++index) { - BuiltinName name(index); - if (name && !m_table.isSymbolName(name) && !m_table.findOwnMember(holder, name) && !m_table.canTakeNameInTestRuns(holder, name) && !std::ranges::binary_search(m_table.openNamesOf(holder), name)) - closed.append(name); - } - AOT::sort(closed); - closed.shrink(closed.size() - std::ranges::unique(closed).size()); - m_closedSymbols.append(WTF::move(closed)); - if (hasRecord) - m_closedKeys[position] = BuiltinClosedKeys { holder, m_closedSymbols.last().span(), { } }; - else - m_closedKeys.insert(position, BuiltinClosedKeys { holder, m_closedSymbols.last().span(), { } }); - for (uint32_t index = 0; index < m_builtins.size(); ++index) { - if (BuiltinID(index) == holder || m_builtins[index].sameObjectAs == holder) - m_builtins[index].flags.remove(BuiltinFlag::IsClosedToOtherNames); - } -} - -void BuiltinTableOfBuild::unlock(std::span members, std::span holdersOfNewNames, bool opensEveryHolder) +void BuiltinTableOfBuild::unlock(std::span members, std::span holdersOfNewNames) { RELEASE_ASSERT(m_builtins.isEmpty()); for (BuiltinID member : members) { @@ -170,7 +143,7 @@ void BuiltinTableOfBuild::unlock(std::span members, std::span(m_data.builtins); m_lockedObjects = Vector(m_data.runtime.lockedObjects); @@ -185,10 +158,6 @@ void BuiltinTableOfBuild::unlock(std::span members, std::spannumbersOfLast().lastShape : 0, .lastShape = parts.inputs.pipeline.placement->shapes().numberOfShapes(), diff --git a/Source/JavaScriptCore/aot/image/AOTImageFormat.h b/Source/JavaScriptCore/aot/image/AOTImageFormat.h index 5fcf52fd83acc..add68606b8b9a 100644 --- a/Source/JavaScriptCore/aot/image/AOTImageFormat.h +++ b/Source/JavaScriptCore/aot/image/AOTImageFormat.h @@ -138,8 +138,6 @@ enum class ImageFlag : uint32_t { SharesRealms = 1 << 7, // The host may start an ES module that is an entry point once more in the same realm: ModuleFlags::RunsAgain. RunsEntryPointsAgain = 1 << 8, - // CompileOptions::holdsLibraryAlone(). It knows nothing of the run: the image above it is the first that does, and says what the realm locks. - HoldsLibraryAlone = 1 << 9, StoresPathOfMainModule = 1 << 10, // FlowResults::reachedFunctionsCountUnreadCode(). Without it whatever runs beside the code of the image is a copy of a file of the image. RunsUnreadCode = 1 << 11, diff --git a/Source/JavaScriptCore/aot/image/AOTImagesBelow.h b/Source/JavaScriptCore/aot/image/AOTImagesBelow.h index e55b293891be8..81b7c862adbfd 100644 --- a/Source/JavaScriptCore/aot/image/AOTImagesBelow.h +++ b/Source/JavaScriptCore/aot/image/AOTImagesBelow.h @@ -33,7 +33,6 @@ class ImagesBelow { static std::unique_ptr tryCreate(std::span> images); unsigned count() const { return m_images.size(); } - bool hasPrebuiltLibrary() const { return m_images.first()->holdsLibraryAlone(); } const ImageNumberRanges& numbersOfLast() const { return m_images.last()->header().numbers; } uint32_t capacityOfStringHeaders() const { return m_images.first()->header().numbers.capacityOfStringHeaders; } // Image::layoutIndicesBelow() of the image that is built. diff --git a/Source/JavaScriptCore/aot/ir/AOTIRProgram.h b/Source/JavaScriptCore/aot/ir/AOTIRProgram.h index 05bd30719ec5f..a4fa9763cfd6d 100644 --- a/Source/JavaScriptCore/aot/ir/AOTIRProgram.h +++ b/Source/JavaScriptCore/aot/ir/AOTIRProgram.h @@ -133,8 +133,6 @@ class Program final { Vector webAssemblyModules; Vector replaceableBuiltins; Vector openedNames; - // CompileOptions::holdsLibraryAlone(): a run that nobody knows yet adds whatever names a test run can add. - bool takesAnyNameFromTests { false }; Function& at(FunctionID id) { return functions[id.index()]; } const Function& at(FunctionID id) const { return ProvisionalFunctions::contains(id) ? m_provisionalFunctions->at(id) : functions[id.index()]; } diff --git a/Source/JavaScriptCore/aot/ir/AOTReader.cpp b/Source/JavaScriptCore/aot/ir/AOTReader.cpp index b3f95e76453e8..f0ec26b382737 100644 --- a/Source/JavaScriptCore/aot/ir/AOTReader.cpp +++ b/Source/JavaScriptCore/aot/ir/AOTReader.cpp @@ -64,7 +64,6 @@ bool Reader::readReplaceableBuiltins() AOT::sort(members, { }, &ReplaceableBuiltin::member); members.shrink(members.size() - std::ranges::unique(members, { }, &ReplaceableBuiltin::member).size()); } - m_program.takesAnyNameFromTests = m_build.options().holdsLibraryAlone(); if (bool byRule = m_build.recordedOptions().isSabotaged(Sabotage::MemoryRunsOutWhenCountedByRule); byRule != lackOfMemoryThrowsByRule) [[unlikely]] lackOfMemoryThrowsByRule = byRule; m_program.unlockReplaceableBuiltins(); diff --git a/Source/JavaScriptCore/aot/ir/schema/compile-call.def b/Source/JavaScriptCore/aot/ir/schema/compile-call.def index 05cb64cb137bc..de7adc7dde613 100644 --- a/Source/JavaScriptCore/aot/ir/schema/compile-call.def +++ b/Source/JavaScriptCore/aot/ir/schema/compile-call.def @@ -201,7 +201,7 @@ enum Sabotage { flags Instrumentation { CountBranches CountArrivals } flags AdmittedCode { Inspector ReplacedBuiltins FilesRunByHost } flags ExcludedCode { EvaluatedText FunctionSource CodeOutsideImage HostEvaluatedText ForeignCalls ForeignCallsOfUnseenCode LibraryText } -flags OutputOptions { Obfuscate SharesRealms HoldsLibraryAlone RunsInSeveralRealms } +flags OutputOptions { Obfuscate SharesRealms RunsInSeveralRealms } record SwitchRange abi { firstID: UInt32 count: UInt32 } record DroppedGuardWords abi { opcode: UInt32 ordinal: UInt32 } record PredictionFailureWords abi { period: UInt32 seed: UInt32 } @@ -211,7 +211,7 @@ record BisectionSwitches abi { droppedGuard: DroppedGuardWords predictionFailure: PredictionFailureWords flowSchedule: FlowScheduleWords } record ImagesBelowWords abi { address: UInt64 size: UInt64 } -record TargetOptions abi { features: UInt64 featuresKeptOnlyWhenNamed: UInt64[8] imagesBelow: ImagesBelowWords referenceLibraryImage: ImagesBelowWords architecture: Architecture referenceWidth: ReferenceWidth imageForm: ImageForm reserved: UInt32 } +record TargetOptions abi { features: UInt64 featuresKeptOnlyWhenNamed: UInt64[8] imagesBelow: ImagesBelowWords architecture: Architecture referenceWidth: ReferenceWidth imageForm: ImageForm reserved: UInt32 } record CompileOptions abi { bisection: BisectionSwitches target: TargetOptions loweredLimitValue: UInt64 sizeClassProgression: Double removedFeatures: UInt64[8] executableIdentity: UInt8[16] size: UInt32 action: CompileAction mismatchPolicy: MismatchPolicy checks: CheckSet reports: ReportSet testing: TestingSwitch instrumentation: Instrumentation diff --git a/Source/JavaScriptCore/aot/place/AOTProgramTableEncoder.cpp b/Source/JavaScriptCore/aot/place/AOTProgramTableEncoder.cpp index ab8bebbe995f9..e6fb115928f3b 100644 --- a/Source/JavaScriptCore/aot/place/AOTProgramTableEncoder.cpp +++ b/Source/JavaScriptCore/aot/place/AOTProgramTableEncoder.cpp @@ -3666,16 +3666,8 @@ static void encodeBuiltinAndBigIntTables(TableEncoder& encoder) for (BuiltinID member : constantBuiltinMembersToLock(encoder.program, encoder.flow)) members.append(member.index()); BitVector unlocked = encoder.flow.builtinMembersWrittenInSharedRealm(); - // What an image that holds the library alone takes for unlocked is whatever any run may open. The images over it take over what is listed here, so it lists what it writes itself. - if (!encoder.build.options().holdsLibraryAlone()) { - for (BuiltinID member : encoder.program.unlockedBuiltins()) - unlocked.set(member.index()); - } else if (const ImagesBelow* library = encoder.build.referenceLibraryImage()) { - unlocked.clearAll(); - library->forEachUnlockedBuiltinMember([&](BuiltinID member) { - unlocked.set(member.index()); - }); - } + for (BuiltinID member : encoder.program.unlockedBuiltins()) + unlocked.set(member.index()); for (size_t member : unlocked) members.append(static_cast(member) | ProgramTables::unlockedBuiltinMember); for (const OpenedName& opened : encoder.program.openedNames) diff --git a/Source/JavaScriptCore/aot/runtime/AOTBuiltinCells.cpp b/Source/JavaScriptCore/aot/runtime/AOTBuiltinCells.cpp index 29e9913de3540..b96568e7b9c0e 100644 --- a/Source/JavaScriptCore/aot/runtime/AOTBuiltinCells.cpp +++ b/Source/JavaScriptCore/aot/runtime/AOTBuiltinCells.cpp @@ -1120,13 +1120,6 @@ static bool isLockedByTable(const BuiltinTable& table, BuiltinID holder) return hasMemberLockedByTable(table, holder) || table.at(holder).flags.contains(BuiltinFlag::IsLockedObject); } -// The lowest program of the realm whose build knew the run. An image that holds the library alone was built for every run: it rests on no lock that a run can open, and says nothing about what this one opens. -static Instance* firstInstanceOfRun(JSGlobalObject* realm) -{ - Instance* first = realm->aotInstance(); - return first && first->program->holdsLibraryAlone() && first->above ? first->above : first; -} - namespace { // Locks a built-in object that the realm has created, and what hangs on it and is there already. @@ -1226,7 +1219,7 @@ class BuiltinLocker { } }; appendNamesOf(m_instance); - if (const Instance* first = firstInstanceOfRun(m_realm); first && first != &m_instance) + if (const Instance* first = m_realm->aotInstance(); first && first != &m_instance) appendNamesOf(*first); return names; } @@ -1339,12 +1332,8 @@ static void lockBuiltins(Instance& instance) } BuiltinCells& cells = *instance.state->builtinCells; // What the first program of the realm leaves open stays open: its build knows of every file that the programs share and of Bun's own code, which a later build does not see. - Instance* first = firstInstanceOfRun(realm); - if (instance.program->holdsLibraryAlone()) { - // It locks what that program locks, which comes behind it. - if (first != &instance) - cells.setMembersUnlockedInRealm(Vector(first->tables().unlockedBuiltinMembers())); - } else if (first && first != &instance && first->state) { + Instance* first = realm->aotInstance(); + if (first && first != &instance && first->state) { toLock.builtins.filter(first->state->builtinCells->builtinsToLock()); Vector unlocked(instance.tables().unlockedBuiltinMembers()); unlocked.append(first->tables().unlockedBuiltinMembers()); diff --git a/Source/JavaScriptCore/aot/runtime/AOTModules.cpp b/Source/JavaScriptCore/aot/runtime/AOTModules.cpp index ddfaca51e4b27..fb1461d8894e8 100644 --- a/Source/JavaScriptCore/aot/runtime/AOTModules.cpp +++ b/Source/JavaScriptCore/aot/runtime/AOTModules.cpp @@ -7468,7 +7468,7 @@ void willStorePathOfMainModule(JSGlobalObject* realm) // In a test run the runner itself stores there, for each test file in its turn. if (hasFlag(instance->options().admitted(), AdmittedCode::FilesRunByHost) || hasFlag(instance->options().admitted(), AdmittedCode::ReplacedBuiltins)) return; - isDecidedInCode |= !instance->program->holdsLibraryAlone() && !instance->program->storesPathOfMainModule(); + isDecidedInCode |= !instance->program->storesPathOfMainModule(); } if (!isDecidedInCode) return; diff --git a/Source/JavaScriptCore/aot/support/AOTBuildContext.cpp b/Source/JavaScriptCore/aot/support/AOTBuildContext.cpp index 315be22b58d94..c81e3bd1fa6b6 100644 --- a/Source/JavaScriptCore/aot/support/AOTBuildContext.cpp +++ b/Source/JavaScriptCore/aot/support/AOTBuildContext.cpp @@ -116,11 +116,6 @@ BuildContext::BuildContext(const Wire::CompileOptions& wire, const HostThreadPoo if (!m_imagesBelow) m_diagnostics.add(InternalError { .kind = InternalErrorKind::InvalidCompileOptions, .count = static_cast(offsetof(Wire::CompileOptions, target)) }); } - if (!m_options.referenceLibraryImage.empty()) { - m_referenceLibraryImage = ImagesBelow::tryCreate(std::span { &m_options.referenceLibraryImage, 1 }); - if (!m_referenceLibraryImage) - m_diagnostics.add(InternalError { .kind = InternalErrorKind::InvalidCompileOptions, .count = static_cast(offsetof(Wire::CompileOptions, target)) }); - } } BuildContext::~BuildContext() diff --git a/Source/JavaScriptCore/aot/support/AOTBuildContext.h b/Source/JavaScriptCore/aot/support/AOTBuildContext.h index b767f950ea153..c9d4b1a7ad989 100644 --- a/Source/JavaScriptCore/aot/support/AOTBuildContext.h +++ b/Source/JavaScriptCore/aot/support/AOTBuildContext.h @@ -97,7 +97,6 @@ class BuildContext { const IdentifierNumbering& identifierNumbering() const; // Null for the first image of its realms, and for an image that shares none. const ImagesBelow* imagesBelow() const { return m_imagesBelow.get(); } - const ImagesBelow* referenceLibraryImage() const { return m_referenceLibraryImage.get(); } RegExpSummaries& regExpSummaries() { return *m_regExpSummaries; } Typed::ScratchRealms& scratchRealms() { return *m_scratchRealms; } Typed::FormatPatternPlans& formatPatternPlans() { return *m_formatPatternPlans; } @@ -165,7 +164,6 @@ class BuildContext { std::optional m_functionsToDump; std::unique_ptr m_identifierNumbering; std::unique_ptr m_imagesBelow; - std::unique_ptr m_referenceLibraryImage; std::unique_ptr m_deferredDestroyer; }; diff --git a/Source/JavaScriptCore/aot/support/AOTCompileOptions.cpp b/Source/JavaScriptCore/aot/support/AOTCompileOptions.cpp index 9289388eb7ac4..9e4dee5f38acf 100644 --- a/Source/JavaScriptCore/aot/support/AOTCompileOptions.cpp +++ b/Source/JavaScriptCore/aot/support/AOTCompileOptions.cpp @@ -336,12 +336,6 @@ class WireOptionsReader { } else reportInvalid(m_wire.target.imagesBelow.size); } - if (m_wire.target.referenceLibraryImage.size) { - if (options.holdsLibraryAlone()) - options.referenceLibraryImage = std::span { std::bit_cast(static_cast(m_wire.target.referenceLibraryImage.address)), static_cast(m_wire.target.referenceLibraryImage.size) }; - else - reportInvalid(m_wire.target.referenceLibraryImage.size); - } options.executableIdentity = m_wire.executableIdentity; options.removedFeatures = m_wire.removedFeatures; options.featuresKeptOnlyWhenNamed = m_wire.target.featuresKeptOnlyWhenNamed; diff --git a/Source/JavaScriptCore/aot/support/AOTCompileOptions.h b/Source/JavaScriptCore/aot/support/AOTCompileOptions.h index 1459343f06062..6969809fdca1a 100644 --- a/Source/JavaScriptCore/aot/support/AOTCompileOptions.h +++ b/Source/JavaScriptCore/aot/support/AOTCompileOptions.h @@ -186,7 +186,6 @@ struct CompileOptions { OutputOptions output { }; // The images that were built before this one for the same realms, the first of them first, as their builds wrote them. They outlive the build. Vector, 2> imagesBelow; - std::span referenceLibraryImage; LoggingOptions logging; bool isVerifyOnly() const { return action == CompileAction::VerifyOnly; } @@ -211,8 +210,6 @@ struct CompileOptions { bool obfuscates() const { return hasFlag(output, OutputOptions::Obfuscate); } // A realm that runs the image runs other images too. Each is built alone, and takes the cells of the others for cells of code that no build has seen. bool sharesRealms() const { return hasFlag(output, OutputOptions::SharesRealms); } - // The image holds the library and nothing else, and is built before any of the runs that it serves is known. So it rests on no lock that a test run can open. - bool holdsLibraryAlone() const { return hasFlag(output, OutputOptions::HoldsLibraryAlone); } // The host runs the image in one realm of the VM after another, each with an instance of its own, and what it carries from one realm to the next may hold a function of the earlier one. bool runsInSeveralRealms() const { return hasFlag(output, OutputOptions::RunsInSeveralRealms); } bool writesCodeSizeMap() const { return !logging.mapFilePath.isNull(); } diff --git a/Source/JavaScriptCore/aot/valueflow/AOTValueFlowResultsBuiltins.cpp b/Source/JavaScriptCore/aot/valueflow/AOTValueFlowResultsBuiltins.cpp index 8857153aac6ba..f2f58775b571a 100644 --- a/Source/JavaScriptCore/aot/valueflow/AOTValueFlowResultsBuiltins.cpp +++ b/Source/JavaScriptCore/aot/valueflow/AOTValueFlowResultsBuiltins.cpp @@ -967,8 +967,7 @@ static Vector computeBuiltinLocations(const ValueFlo bool isUnlocked = program.isUnlocked(member); if (isEnabled && !isStoredThroughUnknownReceiver && !isUnlocked && isWorthLockingAtStartUp(builtins, member, uses, withoutLocks).predicted()) tables.membersLockedAtStartUp.quickSet(index); - // An image that holds the library alone takes every member for unlocked, and still has to say which of them it writes itself. - if (inputs.build.options().sharesRealms() && (!isUnlocked || inputs.build.options().holdsLibraryAlone()) && canLockAtStartUp(builtins, member) && (isStoredThroughUnknownReceiver || !isNoWriteFollowedTo(builtins, member, uses, withoutLocks).predicted())) + if (inputs.build.options().sharesRealms() && !isUnlocked && canLockAtStartUp(builtins, member) && (isStoredThroughUnknownReceiver || !isNoWriteFollowedTo(builtins, member, uses, withoutLocks).predicted())) tables.membersWrittenInSharedRealm.quickSet(index); BuiltinLocationClaim claim = isEnabled || Locks::byTable(builtins, member) ? stateOfLocation(holders.behindLocks(member, withoutLocks)) : unknownLocation(UnprovenCause::ResultDisabled); if (isStoredThroughUnknownReceiver || isUnlocked) From 9f816f4d8a01adb80a276adae3becc25b8cd9a79 Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Sun, 1 Nov 2026 15:29:29 -0800 Subject: [PATCH 26/49] AOT: a planted fault that drops the card of a variable A word for the tests, and an IR test that is red with the word and green as shipped. No product code. Goes with Bun's half: each is red without the other. --- .../aot/codegen/AOTBarrierEmitter.cpp | 2 + .../aot/ir/schema/compile-call.def | 1 + .../validation/young-cell-in-variable.ir | 42 +++++++++++++++++++ 3 files changed, 45 insertions(+) create mode 100644 Source/JavaScriptCore/aot/tests/validation/young-cell-in-variable.ir diff --git a/Source/JavaScriptCore/aot/codegen/AOTBarrierEmitter.cpp b/Source/JavaScriptCore/aot/codegen/AOTBarrierEmitter.cpp index b1cfa5fef297a..6cb1ecc8ce9c1 100644 --- a/Source/JavaScriptCore/aot/codegen/AOTBarrierEmitter.cpp +++ b/Source/JavaScriptCore/aot/codegen/AOTBarrierEmitter.cpp @@ -212,6 +212,8 @@ OwnerBarrier BarrierEmitter::barrierOfExtension(const ValueContext& store) const void BarrierEmitter::markCardOfStatics(uint32_t offsetInStatics) { + if (m_function.kind != FunctionKind::Module && m_options.recorded.isSabotaged(Sabotage::FunctionsMarkNoCard)) + return; InstanceSizes sizes = inputs().instanceSizes; ptrdiff_t offsetOfCardsFromStatics = InstanceLayout::offsetOfCards(sizes) - InstanceLayout::offsetOfStatics(sizes); ptrdiff_t card = offsetInStatics >> InstanceLayout::cardShift; diff --git a/Source/JavaScriptCore/aot/ir/schema/compile-call.def b/Source/JavaScriptCore/aot/ir/schema/compile-call.def index de7adc7dde613..22809459ce93b 100644 --- a/Source/JavaScriptCore/aot/ir/schema/compile-call.def +++ b/Source/JavaScriptCore/aot/ir/schema/compile-call.def @@ -197,6 +197,7 @@ enum Sabotage { ReadsEnterNoGetterThatEscaped RopesPassForFlatStrings EscapedObjectsBearOnlyTheirOwnMarks + FunctionsMarkNoCard } flags Instrumentation { CountBranches CountArrivals } flags AdmittedCode { Inspector ReplacedBuiltins FilesRunByHost } diff --git a/Source/JavaScriptCore/aot/tests/validation/young-cell-in-variable.ir b/Source/JavaScriptCore/aot/tests/validation/young-cell-in-variable.ir new file mode 100644 index 0000000000000..691cea6f12e41 --- /dev/null +++ b/Source/JavaScriptCore/aot/tests/validation/young-cell-in-variable.ir @@ -0,0 +1,42 @@ +# let held = { value: 0.5 }; const store = () => { held = { value: 1.5 } }, read = () => held.value +# A variable of the module lies outside the heap, so a store into it has no owner to run a barrier on: it marks the card of the variable, and a collection of the young looks at the marked cards alone. +# The first store marks the card, the collection of everything takes the mark away, and the cell that store() makes is named by the variable alone. +# FORM: AsRead +# RUN(SLOTS): plan-slots variables +# RUN: run-in-shell --verifyGC=1 globalThis.register = (store, read) => { gc(); store(); edenGC(); print(read()); }; +# SLOTS: g0 Statics Plain Cell +# CHECK-NOT: GC Verifier +# CHECK: 1.5 +# CHECK-NEXT: exited with 0 + +module m0 path="main.ts" displayPath="main.ts" body=f0 +entry module=m0 +layout L0 name="Coin" fields=[{key="value" declared={type=number} flags=[IsRequired]}] +layout L1 kind=Environment fields=[{key="held" declared={type=anything}}] +function f0 name="main" kind=Module results=[{type=anything}] { + v0 = Double number=0.5 + v1 = NewObject layout=L1 fields=[] + v2 = NewObject -, v0 layout=L0 fields=[0] + v3 = FinishObject v2 + PutEnvironmentField v1, v3 layout=L1 field=0 + v5 = NewClosure v1 function=f1 + v6 = NewClosure v1 function=f2 + v7 = Builtin builtin=GlobalThis + v8 = PropGet v7 key="register" [isFreeName] + v9 = Call v8, -, v5, v6 + Return +} +function f1 name="store" captures=[{type=env L1}] results=[{type=undefined}] { + b0(v0: env L1): + v1 = Double number=1.5 + v2 = NewObject -, v1 layout=L0 fields=[0] + v3 = FinishObject v2 + PutEnvironmentField v0, v3 layout=L1 field=0 + Return +} +function f2 name="read" captures=[{type=env L1}] results=[{type=anything}] { + b0(v0: env L1): + v1 = GetEnvironmentField v0 layout=L1 field=0 + v2 = PropGet v1 key="value" + Return v2 +} From 6f09bd370821155dd29c7dc3eef746c61c5ce3ee Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Sun, 1 Nov 2026 15:29:30 -0800 Subject: [PATCH 27/49] AOT: a test run lists the compiled files that ran from source, and why In `bun test --aot` a file that the image holds ran from its text in the interpreter, without a word, behind `import("./a.js?" + n)`, `vi.resetModules()`, `delete require.cache[..]`, and where a `Bun.ModuleGraph` imported it: such tests don't test the code that an executable runs. The run time notes each such file with its reason, for one list. No answer of a program changes. Goes with Bun's half. --- .../aot/runtime/AOTInterpreterSlowPaths.cpp | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/Source/JavaScriptCore/aot/runtime/AOTInterpreterSlowPaths.cpp b/Source/JavaScriptCore/aot/runtime/AOTInterpreterSlowPaths.cpp index 85c74454a0933..4731c396a0e4e 100644 --- a/Source/JavaScriptCore/aot/runtime/AOTInterpreterSlowPaths.cpp +++ b/Source/JavaScriptCore/aot/runtime/AOTInterpreterSlowPaths.cpp @@ -13,6 +13,8 @@ #include "AOTHeapCacheRegistry.h" #include "AOTImageOfCellScope.h" #include "AOTInstance.h" +#include "AOTModuleTable.h" +#include "AOTModules.h" #include "AOTObject.h" #include "AOTObjectBuilder.h" #include "AOTObjectBuilderInlines.h" @@ -20,6 +22,7 @@ #include "AOTObjectModelCaches.h" #include "AOTProgramIdentifiers.h" #include "AOTPropertyCachesKeys.h" +#include "AOTRealmStartup.h" #include "AOTRuntimeCounters.h" #include "AOTState.h" #include "AOTVector.h" @@ -35,15 +38,43 @@ #include #include #include +#include WTF_ALLOW_UNSAFE_BUFFER_USAGE_BEGIN namespace JSC { namespace AOT { +// A test run says at its end which files of the program it did not test as compiled code. +static void tellThatFileOfImageRunsFromSource(VM& vm, CodeBlock* codeBlock) +{ + String url = codeBlock->source().provider()->sourceURL(); + JSGlobalObject* realm = codeBlock->globalObject(); + for (VMState::PartsOfImage& parts : vm.aotState().parts) { + ModuleTable table(parts.image->programTables()); + std::optional found = table.findByPath(url); + if (!found) + continue; + Modules* modules = nullptr; + for (Instance* instance = realm->aotInstance(); instance; instance = instance->above) { + if (instance->program == parts.image && instance->state) + modules = instance->state->modules.get(); + } + ModuleID file = table.fileBehindFace(*found).value_or(*found); + ASCIILiteral why = "a separate loader imported it, such as a Bun.ModuleGraph"_s; + if (modules && modules->isEvicted(file)) + why = "it was loaded again after resetModules() or a delete from require.cache"_s; + else if (modules && modules->isLeftToHost(file)) + why = "it imports a file that was loaded again"_s; + return tellRefusedChangeListener(realm, makeString("Ran from source '"_s, url, "': "_s, why)); + } +} + void willRunInInterpreter(CodeBlock* codeBlock) { VM& vm = codeBlock->vm(); count(vm, counterFor(codeBlock->codeType())); + if (codeBlock->codeType() == ModuleCode || codeBlock->codeType() == GlobalCode) [[unlikely]] + tellThatFileOfImageRunsFromSource(vm, codeBlock); if (!Options::crashIfInterpreterIsUsed()) [[likely]] return; From 120a664c0a905438e6fb9676075c983bec5775a5 Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Sun, 1 Nov 2026 15:29:30 -0800 Subject: [PATCH 28/49] AOT: the stubs align with the same padding as the rest of the code On arm64 the padding that aligns the stubs was still a breakpoint (the assembler's align() emits brk), so with `process.on("SIGTRAP")` listeners a wild jump into it hung. The stubs align with udf now, as the rest of the code does. x64's bytes are unchanged. Goes with Bun's half, whose arm64 case is red without this. --- .../aot/stubs/AOTStubAssembler.cpp | 7 +++++++ .../aot/stubs/AOTStubAssembler.h | 2 ++ .../JavaScriptCore/aot/tests/AOTStubTests.cpp | 19 +++++++++++++++++++ 3 files changed, 28 insertions(+) diff --git a/Source/JavaScriptCore/aot/stubs/AOTStubAssembler.cpp b/Source/JavaScriptCore/aot/stubs/AOTStubAssembler.cpp index 99cfa35401895..66de13781dbcc 100644 --- a/Source/JavaScriptCore/aot/stubs/AOTStubAssembler.cpp +++ b/Source/JavaScriptCore/aot/stubs/AOTStubAssembler.cpp @@ -412,6 +412,13 @@ void StubAssembler::recordFarJump(PtrTag tag) recordCallee(tag); } +void StubAssembler::align() +{ + constexpr int alignment = 16; + while (!m_assembler.buffer().isAligned(alignment)) + emitPadding(*this); +} + void StubAssembler::emitStackAlignmentCheck() { #if CPU(X86_64) diff --git a/Source/JavaScriptCore/aot/stubs/AOTStubAssembler.h b/Source/JavaScriptCore/aot/stubs/AOTStubAssembler.h index 76cb40af5338f..12323e55398fc 100644 --- a/Source/JavaScriptCore/aot/stubs/AOTStubAssembler.h +++ b/Source/JavaScriptCore/aot/stubs/AOTStubAssembler.h @@ -176,6 +176,8 @@ class StubAssembler final : public CCallHelpers { void jumpThroughHandlerTable(BaseIndex tableEntry, std::span handlers); void callThroughHandlerTable(BaseIndex tableEntry, std::span handlers); + // In place of the assembler's own, which pads with breakpoints on ARM64. + void align(); void bindEntry(StubKey entry) { bindEntryAt(entry, label()); } Label bindReturnLabel(StubFrameKind); diff --git a/Source/JavaScriptCore/aot/tests/AOTStubTests.cpp b/Source/JavaScriptCore/aot/tests/AOTStubTests.cpp index 3be9f6dd237bf..75d858d4b1dcd 100644 --- a/Source/JavaScriptCore/aot/tests/AOTStubTests.cpp +++ b/Source/JavaScriptCore/aot/tests/AOTStubTests.cpp @@ -906,6 +906,24 @@ static void testTrapsEndListenerForBreakpoints() AOT_TEST_CHECK(breakpoint.size() == codeOffsetUnit && MacroAssembler::isBreakpoint(breakpoint.mutableSpan().data())); } +static void testAlignmentOfStubsIsPadding() +{ + StubCatalog catalog(StubOptions { }, StubPolicy { }); + StubAssembler jit(catalog.options(), catalog); + jit.generateForTesting(findStubPreserving(catalog, gprOf(StubRegister::T12)), [](StubAssembler& jit) { + jit.returnToCaller(); + jit.align(); + jit.returnToCaller(); + }); + ImageLinkBuffer buffer(jit, LinkBuffer::Profile::Thunk); + AOT_TEST_CHECK(!buffer.didFailToAllocate()); + buffer.finalize(false, "stub with an aligned part"_s); + std::span bytes = buffer.bytes(); + AOT_TEST_CHECK(bytes.size() > 16); + for (size_t offset = 0; offset < bytes.size(); offset += codeOffsetUnit) + AOT_TEST_CHECK(!MacroAssembler::isBreakpoint(bytes.subspan(offset).data())); +} + #endif // OS(UNIX) static void testRegistersLiveAcrossReads() @@ -948,6 +966,7 @@ std::span stubTests() #if OS(UNIX) { "stubs/validated-calls"_s, testValidatedCalls }, { "stubs/traps-end-listener-for-breakpoints"_s, testTrapsEndListenerForBreakpoints }, + { "stubs/alignment-is-padding"_s, testAlignmentOfStubsIsPadding }, #endif { "stubs/registers-live-across-reads"_s, testRegistersLiveAcrossReads }, }; From eed8f13850f12f20694ebe7aff2ef72da6a0371a Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Sun, 1 Nov 2026 15:29:30 -0800 Subject: [PATCH 29/49] AOT: an image that has noted no load has no moments By reading: no program was found that fails. Two functions of the run time's modules read the list of the moments of loads without asking whether the image has noted a load, and three callers came without the guard that the others have. Both ask it themselves now: an image without moments answers with the module's own place, and "not loaded in this test file". --- Source/JavaScriptCore/aot/runtime/AOTModules.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Source/JavaScriptCore/aot/runtime/AOTModules.cpp b/Source/JavaScriptCore/aot/runtime/AOTModules.cpp index fb1461d8894e8..73371fbf617e3 100644 --- a/Source/JavaScriptCore/aot/runtime/AOTModules.cpp +++ b/Source/JavaScriptCore/aot/runtime/AOTModules.cpp @@ -2249,8 +2249,8 @@ void Modules::letGoOfMocksThatEnded() uint32_t Modules::placeOfMomentOfLoad(ModuleID module) const { - // A CommonJS file and its face are in no load group. - if (!data(module).isESModuleOfProgram()) + // A CommonJS file and its face are in no load group. An image that has noted no load has no moments. + if (!data(module).isESModuleOfProgram() || m_momentsOfLoads.isEmpty()) return module.index(); uint32_t head = m_table.headOfLoadGroup(module).index(); return m_momentsOfLoads[head] ? head : module.index(); @@ -2276,7 +2276,7 @@ bool Modules::hasMockLaidOver(ModuleID module) const bool Modules::wasLoadedInThisTestFile(ModuleID module) const { uint32_t momentOfTestFile = m_instance.vm->aotState().momentOfTestFile; - if (m_momentsOfLoads[placeOfMomentOfLoad(module)] > momentOfTestFile) + if (!m_momentsOfLoads.isEmpty() && m_momentsOfLoads[placeOfMomentOfLoad(module)] > momentOfTestFile) return true; auto anew = m_momentsLoadedAnew.find(module); return anew != m_momentsLoadedAnew.end() && anew->value.momentOfTestFile == momentOfTestFile; From c8fb4feb8a5703c2c1446e5b751ceb070dea828c Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Sun, 1 Nov 2026 15:29:30 -0800 Subject: [PATCH 30/49] AOT: the recheck of guards reads only what its functions reach Build time under the program verifier alone: no byte of any output depends on it. Where the first verification reports a guard that isn't available, the summaries of effects were computed over the whole program from nothing, twice, to verify a few functions again. Now they are computed over what the bodies of those functions name; every other function counts as unknown code. --- .../aot/ir/AOTEffectSummaries.cpp | 11 +++-- .../aot/ir/AOTEffectSummaries.h | 3 +- .../aot/ir/AOTEffectSummariesInternal.h | 12 +++++ .../aot/ir/AOTEffectSummariesOfBodies.cpp | 49 ++++++++++++++++--- .../aot/ir/AOTEffectSummariesSolver.cpp | 24 +++++++++ .../aot/ir/AOTProgramVerifierFunctions.cpp | 21 +++++--- .../aot/tests/AOTEffectsTestCommands.cpp | 7 +++ ...parameter-handed-on-to-an-outlined-body.ir | 5 ++ 8 files changed, 114 insertions(+), 18 deletions(-) diff --git a/Source/JavaScriptCore/aot/ir/AOTEffectSummaries.cpp b/Source/JavaScriptCore/aot/ir/AOTEffectSummaries.cpp index 121192b4994c4..b7ac13fa64af4 100644 --- a/Source/JavaScriptCore/aot/ir/AOTEffectSummaries.cpp +++ b/Source/JavaScriptCore/aot/ir/AOTEffectSummaries.cpp @@ -782,7 +782,7 @@ EffectSummaries computeEffectSummaries(const Program& program, const FlowResults return summaries; } -static EffectSummaries computeEffectSummariesByOperandTypes(const Program& program, const FlowResults& flow, BuildContext& build, const EffectSummaries* rowsOfBodies) +static EffectSummaries computeEffectSummariesByOperandTypes(const Program& program, const FlowResults& flow, BuildContext& build, const EffectSummaries* rowsOfBodies, std::span roots, BitVector& bodiesBelowRoots) { BodySummaries nothingKept; EffectSummaries summaries; @@ -791,16 +791,19 @@ static EffectSummaries computeEffectSummariesByOperandTypes(const Program& progr summarizer.readCalleesByOperandTypes(); if (rowsOfBodies) summarizer.takeRowsOfBodiesFrom(*rowsOfBodies); + if (!roots.empty()) + summarizer.readOnlyBodiesBelow(roots, bodiesBelowRoots); summarizer.update(summaries); return summaries; } -EffectSummaries computeEffectSummariesByOperandTypes(const Program& program, const FlowResults& flow, BuildContext& build) +EffectSummaries computeEffectSummariesByOperandTypes(const Program& program, const FlowResults& flow, BuildContext& build, std::span roots) { // The seed says nothing of a function that was made after flow analysis, and a call of one may stand where operations of the caller's own body stood. // So a body that hands a parameter on to it seems to use the parameter in every way. The second reading knows what the first found in that function's body. - EffectSummaries first = computeEffectSummariesByOperandTypes(program, flow, build, nullptr); - return computeEffectSummariesByOperandTypes(program, flow, build, &first); + BitVector bodiesBelowRoots; + EffectSummaries first = computeEffectSummariesByOperandTypes(program, flow, build, nullptr, roots, bodiesBelowRoots); + return computeEffectSummariesByOperandTypes(program, flow, build, &first, roots, bodiesBelowRoots); } void updateEffectSummaries(const Program& program, const FlowResults& flow, BuildContext& build, BodySummaries& kept, EffectSummaries& summaries, BodyReferences* references) diff --git a/Source/JavaScriptCore/aot/ir/AOTEffectSummaries.h b/Source/JavaScriptCore/aot/ir/AOTEffectSummaries.h index ec9e5c1d79a6b..dd1181bcb11ff 100644 --- a/Source/JavaScriptCore/aot/ir/AOTEffectSummaries.h +++ b/Source/JavaScriptCore/aot/ir/AOTEffectSummaries.h @@ -482,7 +482,8 @@ EffectSummaries createSummariesFromFlow(const Program&, const FlowResults&, cons EffectSummaries computeEffectSummaries(const Program&, const FlowResults&, BuildContext&); // A typed body that restarts in the generic version of its function passes its own parameters. Here that call is summarized by what the generic version does to values of those types, and so is a call // to code outlined from the generic version. Each such callee is read again for each such call, which is too expensive for a build, so only the verifier asks, when a guard is unavailable. -EffectSummaries computeEffectSummariesByOperandTypes(const Program&, const FlowResults&, BuildContext&); +// With roots: for them and for what they can reach by the functions that each body names. Every other function is taken for unknown code. +EffectSummaries computeEffectSummariesByOperandTypes(const Program&, const FlowResults&, BuildContext&, std::span roots = { }); void updateEffectSummaries(const Program&, const FlowResults&, BuildContext&, BodySummaries& kept, EffectSummaries&, BodyReferences* = nullptr); // A callee listed here is read as the summary beside it says. FunctionSummary summarizeBody(const EffectContext&, FunctionID, FunctionAnalyses&, std::span> versionsOfCallees = { }); diff --git a/Source/JavaScriptCore/aot/ir/AOTEffectSummariesInternal.h b/Source/JavaScriptCore/aot/ir/AOTEffectSummariesInternal.h index 21e28c181bc73..796c058511b22 100644 --- a/Source/JavaScriptCore/aot/ir/AOTEffectSummariesInternal.h +++ b/Source/JavaScriptCore/aot/ir/AOTEffectSummariesInternal.h @@ -273,6 +273,9 @@ uint64_t parametersWhoseContentsAreCalled(const Program&, FunctionID, const Borr ExitReachability computeExitReachability(const Function&); enum class CalleesAreRead : uint8_t { AsTheyAre, ByOperandTypes }; BodySummary summarizeBodyLocally(const EffectContext&, FunctionID, FunctionAnalyses&, BodyReferenceCollector* = nullptr, CalleesAreRead = CalleesAreRead::AsTheyAre); +BodySummary summarizeAsUnknownCode(FunctionID, const Function&); +// What a reading of the body may ask a row of. +Vector functionsNamedIn(const Program&, const Function&); FunctionSummary createFunctionSummary(Effects onReturn, Effects onThrow, Vector&&, UnknownCodeWitness); FunctionSummary createSummaryFromFlow(const Program&, const FlowResults&, const BorrowedParameters&, BuildContext&, FunctionID); @@ -288,6 +291,13 @@ class EffectSummarizer { void readCalleesByOperandTypes() { m_calleesAreRead = CalleesAreRead::ByOperandTypes; } // Only from scratch. Where these say that a body uses a parameter in fewer ways than the seed does, a caller that hands it an operand reads what the body does. void takeRowsOfBodiesFrom(const EffectSummaries& summaries) { m_rowsOfBodies = &summaries; } + // Only from scratch. Reads the bodies that the roots can reach by the functions that each body names, and takes every other function for unknown code. + // `bodies` is empty, and is filled with what was found, or comes from an earlier reading. + void readOnlyBodiesBelow(std::span roots, BitVector& bodies) + { + m_rootsOfBodiesToRead = roots; + m_bodiesToRead = &bodies; + } void giveReferencesTo(BodyReferences* references) { m_references = references; } void update(EffectSummaries&); void bringSeedUpToDate(EffectSummaries&); @@ -346,6 +356,8 @@ class EffectSummarizer { bool m_isOnlyCheck { false }; CalleesAreRead m_calleesAreRead { CalleesAreRead::AsTheyAre }; const EffectSummaries* m_rowsOfBodies { nullptr }; + std::span m_rootsOfBodiesToRead; + BitVector* m_bodiesToRead { nullptr }; BodyReferences* m_references { nullptr }; unsigned m_numberOfFunctionsBefore { 0 }; Vector m_recordLooksDifferent; diff --git a/Source/JavaScriptCore/aot/ir/AOTEffectSummariesOfBodies.cpp b/Source/JavaScriptCore/aot/ir/AOTEffectSummariesOfBodies.cpp index c6c1c1597d420..015143745b04d 100644 --- a/Source/JavaScriptCore/aot/ir/AOTEffectSummariesOfBodies.cpp +++ b/Source/JavaScriptCore/aot/ir/AOTEffectSummariesOfBodies.cpp @@ -884,6 +884,47 @@ static BodySummary summarizeDescription(const Program& program, FunctionID id, c return summary; } +Vector functionsNamedIn(const Program& program, const Function& function) +{ + Vector named; + auto take = [&](const EffectDescription& description) { + named.appendVector(description.functions); + named.appendVector(description.slowPathFunctions); + }; + for (const Block& block : function.blocks) { + for (ValueID id : block.values) { + const Value& value = function.at(id); + if (value.opcode == Opcode::NewClosure) + named.append(value.function()); + if (hasEmptyDescription(value.opcode)) + continue; + take(describeEffects(program, function, value)); + if (effectRuleOf(value.opcode) != EffectRule::Call) + continue; + CalleeList callees = calleesThatBoundEffectsOf(program, value); + if (std::optional>> functions = callees.functionsAlone()) + named.append(functions->value()); + } + if (!hasEmptyDescription(block.terminator.kind)) + take(describeEffects(program, function, block.terminator)); + } + if (FunctionID genericVersion = genericVersionThatBodyRestartsIn(program, function)) + named.append(genericVersion); + AOT::sort(named); + removeRepeatedElements(named); + return named; +} + +BodySummary summarizeAsUnknownCode(FunctionID id, const Function& function) +{ + BodySummary summary; + summary.onReturn = Effects::unknownCode(); + summary.onThrow = Effects::unknownCode(); + summary.parameters = Vector(function.numberOfEntryParameters()); + summary.unknownCodeWitness = { id, ValueID() }; + return summary; +} + BodySummary summarizeBodyLocally(const EffectContext& context, FunctionID id, FunctionAnalyses& analyses, BodyReferenceCollector* references, CalleesAreRead calleesAreRead) { const Function& function = analyses.function(); @@ -894,13 +935,7 @@ BodySummary summarizeBodyLocally(const EffectContext& context, FunctionID id, Fu } if (function.hasBody()) return BodySummarizer(context, id, analyses, references, calleesAreRead == CalleesAreRead::AsTheyAre ? BodySummarizer::ReadAs::Itself : BodySummarizer::ReadAs::ItselfAndCalleesByOperandTypes).run(); - - BodySummary summary; - summary.onReturn = Effects::unknownCode(); - summary.onThrow = Effects::unknownCode(); - summary.parameters = Vector(function.numberOfEntryParameters()); - summary.unknownCodeWitness = { id, ValueID() }; - return summary; + return summarizeAsUnknownCode(id, function); } } } } // namespace JSC::AOT::Typed diff --git a/Source/JavaScriptCore/aot/ir/AOTEffectSummariesSolver.cpp b/Source/JavaScriptCore/aot/ir/AOTEffectSummariesSolver.cpp index f5b46f7d9dddf..0df7fd80bcbee 100644 --- a/Source/JavaScriptCore/aot/ir/AOTEffectSummariesSolver.cpp +++ b/Source/JavaScriptCore/aot/ir/AOTEffectSummariesSolver.cpp @@ -532,6 +532,8 @@ void EffectSummarizer::summarizeBodies() auto summarize = [&](const EffectSummaries& seed, unsigned index, BodyReferenceCollector* references = nullptr) { FunctionID id(index); + if (m_bodiesToRead && !m_bodiesToRead->quickGet(index)) + return summarizeAsUnknownCode(id, m_program.functions[index]); EffectContext context { m_program, seed }; std::unique_ptr analyses = createAnalysesForSummary(m_build, context, m_flow, id, m_program.functions[index]); return summarizeBodyLocally(context, id, *analyses, references, m_calleesAreRead); @@ -601,6 +603,28 @@ void EffectSummarizer::summarizeBodies() }; const EffectSummaries& seed = m_kept.m_seed.read(); + if (m_bodiesToRead && m_bodiesToRead->isEmpty()) { + RELEASE_ASSERT(!m_numberOfFunctionsBefore); + m_bodiesToRead->ensureSize(numberOfFunctions); + Vector frontier; + auto add = [&](FunctionID function) { + if (!m_bodiesToRead->quickSet(function.index())) + frontier.append(function.index()); + }; + for (FunctionID root : m_rootsOfBodiesToRead) + add(root); + while (!frontier.isEmpty()) { + Vector> named(frontier.size()); + forEachIndexInParallel(loops, frontier.size(), [&](unsigned position) { + named[position] = functionsNamedIn(m_program, m_program.functions[frontier[position]]); + }); + frontier.shrink(0); + for (const Vector& functions : named) { + for (FunctionID function : functions) + add(function); + } + } + } // The seed may be lent to an analysis that runs meanwhile, so the rows that bodies replace are rows of a copy. std::optional seedWithRowsOfBodies; auto summarizeBodiesOf = [&](Turn turn) { diff --git a/Source/JavaScriptCore/aot/ir/AOTProgramVerifierFunctions.cpp b/Source/JavaScriptCore/aot/ir/AOTProgramVerifierFunctions.cpp index cff4c4f829e6e..6ac54ac6a0621 100644 --- a/Source/JavaScriptCore/aot/ir/AOTProgramVerifierFunctions.cpp +++ b/Source/JavaScriptCore/aot/ir/AOTProgramVerifierFunctions.cpp @@ -264,10 +264,6 @@ void ProgramVerifier::recheckUnavailableGuards(Vector& errors) }; if (!m_inputs.effects || m_inputs.effects->isEmpty() || !std::ranges::any_of(errors, isAboutGuard)) return; - FlowResults noResults; - EffectSummaries summaries = computeEffectSummariesByOperandTypes(m_program, m_inputs.flow ? *m_inputs.flow : noResults, m_build); - VerifierInputs inputs = m_inputs; - inputs.effects = &summaries; Vector functions; for (const InternalError& error : errors) { if (isAboutGuard(error) && !functions.contains(error.function)) @@ -276,8 +272,21 @@ void ProgramVerifier::recheckUnavailableGuards(Vector& errors) errors.removeAllMatching([&](const InternalError& error) { return functions.contains(error.function); }); - for (FunctionID id : functions) - errors.appendVector(findErrorsInFunction(m_program, id, m_program.at(id), m_form, m_build, inputs)); + FlowResults noResults; + auto findErrorsBy = [&](const EffectSummaries& summaries) { + VerifierInputs inputs = m_inputs; + inputs.effects = &summaries; + Vector found; + for (FunctionID id : functions) + found.appendVector(findErrorsInFunction(m_program, id, m_program.at(id), m_form, m_build, inputs)); + return found; + }; + const FlowResults& flow = m_inputs.flow ? *m_inputs.flow : noResults; + // Unknown code does whatever the functions do that it stands for, so what passes here passes by the summaries of the whole program. + Vector found = findErrorsBy(computeEffectSummariesByOperandTypes(m_program, flow, m_build, functions.span())); + if (!found.isEmpty()) + found = findErrorsBy(computeEffectSummariesByOperandTypes(m_program, flow, m_build)); + errors.appendVector(WTF::move(found)); } void ProgramVerifier::verifyFunctionBodies() diff --git a/Source/JavaScriptCore/aot/tests/AOTEffectsTestCommands.cpp b/Source/JavaScriptCore/aot/tests/AOTEffectsTestCommands.cpp index eacc4f8a97d0f..9deb7f23713ca 100644 --- a/Source/JavaScriptCore/aot/tests/AOTEffectsTestCommands.cpp +++ b/Source/JavaScriptCore/aot/tests/AOTEffectsTestCommands.cpp @@ -147,6 +147,13 @@ static std::span takeInputWords(TestCommandContext& context, std:: inputs.hasSummaries = true; words = words.subspan(1); } + constexpr ASCIILiteral summariesBelow = "summaries-by-types-of-operands-below=f"_s; + if (!words.empty() && words.front().startsWith(summariesBelow)) { + FunctionID root(parseInteger(StringView(words.front()).substring(summariesBelow.length())).value_or(0)); + inputs.summaries = computeEffectSummariesByOperandTypes(*context.program, inputs.flow, context.build, std::span(&root, 1)); + inputs.hasSummaries = true; + words = words.subspan(1); + } constexpr ASCIILiteral summariesBefore = "summaries-before=f"_s; if (!words.empty() && words.front().startsWith(summariesBefore)) { unsigned numberOfFunctions = parseInteger(StringView(words.front()).substring(summariesBefore.length())).value_or(0); diff --git a/Source/JavaScriptCore/aot/tests/ir/effects/summaries-by-operand-types-parameter-handed-on-to-an-outlined-body.ir b/Source/JavaScriptCore/aot/tests/ir/effects/summaries-by-operand-types-parameter-handed-on-to-an-outlined-body.ir index d80d3211912d7..7d713deeace13 100644 --- a/Source/JavaScriptCore/aot/tests/ir/effects/summaries-by-operand-types-parameter-handed-on-to-an-outlined-body.ir +++ b/Source/JavaScriptCore/aot/tests/ir/effects/summaries-by-operand-types-parameter-handed-on-to-an-outlined-body.ir @@ -1,8 +1,10 @@ # An operation of a body was moved into a function of its own, made after flow analysis, so the seed says nothing of that function's parameter. Read once, the body that hands # its parameter on seems to use it in every way, and a caller's list seems to escape. The summaries for a check are read twice: the body then uses the parameter as it did before +# Where a few functions are checked, only what they can reach is read. # FORM: Selected # RUN(ONCE): effects summaries # RUN(TWICE): effects summaries-by-types-of-operands +# RUN(BELOW): effects summaries-by-types-of-operands-below=f2 module m0 path="main.js" displayPath="main.js" function f0 name="inOwnBody" module=m0 parameters=[{type=Array}] results=[{type=undefined}] capabilities=[HasOrdinaryFrame, HasOnlyDirectCallers, IsNeverCalledByHost] { @@ -29,3 +31,6 @@ function f2 name="movedOut" module=m0 parameters=[{type=Array Date: Sun, 1 Nov 2026 15:29:30 -0800 Subject: [PATCH 31/49] AOT: the iterator of a map is told by its cell The table of built-ins gives %MapIterator% its cell type, so the algebra of types tells it from Map and Array (the meet is never, both ways). Two tests, red without it: seven iterators pass, made by the shell, by compiled code, of a class below Map, with the prototype taken away; fourteen others, the prototype object and an heir of it among them, don't. Goes with Bun's half. --- .../aot/builtins/holders.builtins.ts | 1 + .../codegen/classes-told-by-their-cells.ir | 31 +++++++++++++++++++ .../types/classes-told-by-their-cells.ir | 12 +++++++ 3 files changed, 44 insertions(+) create mode 100644 Source/JavaScriptCore/aot/tests/codegen/classes-told-by-their-cells.ir create mode 100644 Source/JavaScriptCore/aot/tests/types/classes-told-by-their-cells.ir diff --git a/Source/JavaScriptCore/aot/builtins/holders.builtins.ts b/Source/JavaScriptCore/aot/builtins/holders.builtins.ts index 9087a7fde23af..36299a8e8da98 100644 --- a/Source/JavaScriptCore/aot/builtins/holders.builtins.ts +++ b/Source/JavaScriptCore/aot/builtins/holders.builtins.ts @@ -511,6 +511,7 @@ export default b.declarations( }), b.class("%MapIterator%", { ...lockedIteratorColumns, + jsType: "JSMapIteratorType", proto: { next: libraryStep("MapIteratorPrototype", "next", iteratorResult) }, }), b.class("%SetIterator%", { diff --git a/Source/JavaScriptCore/aot/tests/codegen/classes-told-by-their-cells.ir b/Source/JavaScriptCore/aot/tests/codegen/classes-told-by-their-cells.ir new file mode 100644 index 0000000000000..aa682c9b5b1b5 --- /dev/null +++ b/Source/JavaScriptCore/aot/tests/codegen/classes-told-by-their-cells.ir @@ -0,0 +1,31 @@ +# The test for a class goes by the type of the cell: it passes for every instance, whoever made it and whatever it inherits from, and for nothing else. +# FORM: AsRead +# RUN: run-in-shell globalThis.register = (name, test, make) => { const map = new Map([[1, 2]]), set = new Set([1]); const values = { MapIterator: make => [map.entries(), map.keys(), map.values(), map[Symbol.iterator](), Object.setPrototypeOf(map.entries(), null), new (class extends Map {})().entries(), make(map)], others: () => [Object.create(Object.getPrototypeOf(map.entries())), Object.getPrototypeOf(map.entries()), "a"[Symbol.iterator](), map, set, {}, [], () => 1, 1, "a", null, undefined] }; let all = 0, passed = 0, passedWrongly = 0; for (const key in values) { const list = values[key](key === name ? make : id); for (let index = 0; index < list.length; index++) { if (key === name) all++; if (test(list[index])) key === name ? passed++ : passedWrongly++; } } print(name, passed, "of", all, "and", passedWrongly, "others"); }; +# CHECK: MapIterator 7 of 7 and 0 others +# CHECK-NEXT: exited with 0 + +module m0 path="main.js" displayPath="main.js" body=f0 +entry module=m0 +function f0 name="main" kind=Module results=[{type=anything}] { + v0 = Builtin builtin=GlobalThis + v1 = PropGet v0 key="register" [isFreeName] + v2 = String string="MapIterator" + v3 = NewClosure function=f1 + v4 = NewClosure function=f2 + v5 = Call v1, -, v2, v3, v4 + Return +} +function f1 name="isMapIterator" parameters=[{type=anything}] results=[{type=anything}] { + v1 = TypeTest v0 testedType=MapIterator + Return v1 +} +function f2 name="makeMapIterator" parameters=[{type=anything}] results=[{type=anything}] { + v1 = TypeTest v0 testedType=Map + Branch v1 -> taken b1(| 0), notTaken b2() + b1(v2): + v3 = PropGet v2 key="entries" + v4 = Call v3, v2 + Return v4 + b2(): + Return +} diff --git a/Source/JavaScriptCore/aot/tests/types/classes-told-by-their-cells.ir b/Source/JavaScriptCore/aot/tests/types/classes-told-by-their-cells.ir new file mode 100644 index 0000000000000..640d49c2f5d62 --- /dev/null +++ b/Source/JavaScriptCore/aot/tests/types/classes-told-by-their-cells.ir @@ -0,0 +1,12 @@ +# A class whose instances have a type of cell of their own meets no other class. +# RUN: intersect +# CHECK: t0 & t1 = never +# CHECK: t0 & t2 = never +# CHECK: t0 & t3 = MapIterator +# CHECK: t1 & t0 = never +# CHECK: t2 & t0 = never +# CHECK: t3 & t0 = MapIterator +# CHECK: laws hold + +module m0 path="main.js" displayPath="main.js" +function f0 name="types" parameters=[{type=MapIterator}, {type=Map}, {type=Array}, {type=Array|MapIterator}] results=[{type=undefined}] From b812287e2e5b05c7ef0bae633061d2642e219333 Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Sun, 1 Nov 2026 15:29:30 -0800 Subject: [PATCH 32/49] AOT: a guard that is not available names what may have ended it Behind GuardNotAvailable the verifier's detail says which guard it is and, of the operations in front of the use that may end what it proves, the nearest six. It needs no printer of types, so a build under `--aot-check=verify-each-pass` says it too. A diagnostic alone. --- .../aot/ir/AOTVerifierGuards.cpp | 21 +++++++++++++++++++ .../aot/ir/AOTVerifierInternal.h | 1 + 2 files changed, 22 insertions(+) diff --git a/Source/JavaScriptCore/aot/ir/AOTVerifierGuards.cpp b/Source/JavaScriptCore/aot/ir/AOTVerifierGuards.cpp index 4e9d1625bdf5f..90b6539f99695 100644 --- a/Source/JavaScriptCore/aot/ir/AOTVerifierGuards.cpp +++ b/Source/JavaScriptCore/aot/ir/AOTVerifierGuards.cpp @@ -17,6 +17,26 @@ namespace JSC { namespace AOT { namespace Typed { static_assert(std::ranges::all_of(allMembersOf, [](TerminatorKind kind) { return reliesOf(kind).isEmpty(); }), "verifyGuards has to visit terminators once one of them relies on a guard"); +String FunctionVerifier::describeWhatMayEnd(BlockID blockOfUser, ValueID guard, const Value& user) +{ + // Those that stand nearest in front of the use, in the order in which the blocks are walked. + constexpr unsigned mostListed = 6; + Vector> found; + for (BlockID block : analyses().cfg().reversePostOrder()) { + const BlockValues& values = m_function.at(block).values; + for (uint32_t index = 0; index < values.size(); ++index) { + if (analyses().guards().mayBeEndedAt(guard, user, { block, index })) + found.append({ block, values[index] }); + } + if (block == blockOfUser) + break; + } + StringBuilder listed; + for (size_t index = found.size() > mostListed ? found.size() - mostListed : 0; index < found.size(); ++index) + listed.append(listed.isEmpty() ? ""_s : ", "_s, "v"_s, found[index].second.index(), " ("_s, nameOf(m_function.at(found[index].second).opcode), ") in b"_s, found[index].first.index()); + return makeString("the guard is v"_s, guard.index(), "; of "_s, found.size(), " operations in front of the use that may end what it proves the nearest are "_s, listed.toString()); +} + void FunctionVerifier::verifyGuards() { if (m_form == IRForm::AsRead && !isOptimizing()) @@ -62,6 +82,7 @@ void FunctionVerifier::verifyGuards() if (readsKeptForValidation->get(id.index())) continue; fail(InternalErrorKind::GuardNotAvailable, block, id, guard.index()); + m_errors.last().detail = describeWhatMayEnd(block, guard, value); return; } } diff --git a/Source/JavaScriptCore/aot/ir/AOTVerifierInternal.h b/Source/JavaScriptCore/aot/ir/AOTVerifierInternal.h index c2f00cecb0959..6f2c41991cec7 100644 --- a/Source/JavaScriptCore/aot/ir/AOTVerifierInternal.h +++ b/Source/JavaScriptCore/aot/ir/AOTVerifierInternal.h @@ -148,6 +148,7 @@ class FunctionVerifier { void verifyLoopMarks(); void verifyGuards(); + String describeWhatMayEnd(BlockID blockOfUser, ValueID guard, const Value& user); void verifyProofs(); void verifyOperandKindsWithoutChecks(); void verifyStoredTypesWithoutChecks(); From 59736f2b4bae06062f29d8f6721a7527d4ba6255 Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Sun, 1 Nov 2026 15:29:30 -0800 Subject: [PATCH 33/49] AOT: a cast may stand between the header's parameter and the step of a for-in `bun build --compile --aot main.js` ended with exit 134, 'panic: abort() called', 'Stopped while compiling a function (main.js:4) in ExpandOwnKeys'. Bun's main has it. `function f1(L) { for (const q in L) { if (L[+q] === 2) return 1; } return 0; }`, declared in a callback that is kept and called later, and called in a try in a loop over `const L = [2]`. A test's body is such a callback. The pass took the position that the step takes for a parameter of the loop's header, which no rule of the IR says: a cast may stand between them. --- .../aot/passes/AOTExpandOwnKeys.cpp | 7 +- ...keys-for-in-with-a-cast-of-the-position.ir | 71 +++++++++++++++++++ 2 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 Source/JavaScriptCore/aot/tests/passes/expand-own-keys-for-in-with-a-cast-of-the-position.ir diff --git a/Source/JavaScriptCore/aot/passes/AOTExpandOwnKeys.cpp b/Source/JavaScriptCore/aot/passes/AOTExpandOwnKeys.cpp index eaa55f4f29fc5..8ec034cf43911 100644 --- a/Source/JavaScriptCore/aot/passes/AOTExpandOwnKeys.cpp +++ b/Source/JavaScriptCore/aot/passes/AOTExpandOwnKeys.cpp @@ -1445,6 +1445,10 @@ static PassOutcome expandForInLoopOverList(FunctionPassContext& context, Emissio bytesOfRegion += estimatedCodeBytes(program, function, function.at(block)); if (context.exceeds(LimitID::MaxVersionedIterationBytes, bytesOfRegion, loop.open)) return PassOutcome::Unchanged; + // The position comes round as a parameter of the header. A cast may stand between that and the step. + size_t positionIndex = function.at(loop.header).parameters.find(definitionThroughUpcasts(function, function.at(loop.step).positionOperand())); + if (positionIndex == notFound) + return PassOutcome::Unchanged; builder.setOrigin(function.at(loop.open).origin()); Vector, 1> cloners; @@ -1455,7 +1459,6 @@ static PassOutcome expandForInLoopOverList(FunctionPassContext& context, Emissio ValueID step = cloner.cloneOf(loop.step); // The region may contain a cycle with its own step, which moves to any position. The original loop handles them all. - size_t positionIndex = function.at(header).parameters.find(function.at(step).positionOperand()); auto isPositionAfterStep = [&](ValueID position) { const Value& next = function.at(definitionThroughUpcasts(function, position)); if (next.opcode != Opcode::Add) @@ -1659,7 +1662,7 @@ static PassOutcome expandForInLoop(FunctionPassContext& context, Emission& emiss } builder.eraseValues(turn.header, loopHints); - size_t positionIndex = function.at(turn.header).parameters.find(function.at(turn.step).positionOperand()); + size_t positionIndex = function.at(turn.header).parameters.find(definitionThroughUpcasts(function, function.at(turn.step).positionOperand())); for (BlockID block : cloner->clonedBlocks()) { for (Target& target : function.edit(block, editsOfTargets).terminator.targets) { if (target.block != turn.header) diff --git a/Source/JavaScriptCore/aot/tests/passes/expand-own-keys-for-in-with-a-cast-of-the-position.ir b/Source/JavaScriptCore/aot/tests/passes/expand-own-keys-for-in-with-a-cast-of-the-position.ir new file mode 100644 index 0000000000000..c6d18310b57a1 --- /dev/null +++ b/Source/JavaScriptCore/aot/tests/passes/expand-own-keys-for-in-with-a-cast-of-the-position.ir @@ -0,0 +1,71 @@ +# A cast stands between the parameter of the header that carries the position and the step that takes it, as behind a prediction of the position. The loop is expanded as without it: +# over a list the copy is entered with the position that the entry hands over, and over an object of one layout the second turn follows the first. +# FORM: Prepared optimizing +# OPTIONS: heat=EverythingIsHot check=VerifyEachPass +# RUN: pass ExpandOwnKeys +# STATISTIC: OwnKeyWalksExpanded = 2 + +module m0 path="main.js" displayPath="main.js" +layout L0 name="Record" fields=[{key="alpha" declared={type=number}}, {key="bravo" declared={type=number}}] flow={constructorOrders=[{fields=[0, 1]}]} + +function f0 name="overAList" parameters=[{type=Array}] results=[{type=number}] { + b0(v0: Array): + v1 = ForInOpen v0 + v2 = Integer integer=0 + Jump -> next b1(v2) + b1(v3: int[0, 8589934591]): + v4 = Upcast v3 castType=int[0, 8589934591] + LoopHint + v6 = ForInStep v1, v0 position=v4 + v7 = ResultAt v6 index=0 + v8 = ResultAt v6 index=1 + v9 = ResultAt v6 index=2 + v10 = Integer integer=1 + v11 = Add v9, v10 + Branch v7 -> taken b3(), notTaken b2() + b2(): + v12 = ElemGet v0, v8 + v13 = Integer integer=2 + v14 = StrictEq v12, v13 + Branch v14 -> taken b4(), notTaken b5() + b3(): + v15 = Integer integer=0 + Return v15 + b4(): + v16 = Integer integer=1 + Return v16 + b5(): + Jump -> next b1(v11) +# CHECK: [[length]]: int[0, 4294967295] = GetEnumeratorIndexedLength v1 +# CHECK-NEXT: [[hasIndexes]]: boolean = Less v2, [[length]] +# CHECK-NEXT: Branch [[hasIndexes]] -> taken [[copy]](v2), notTaken b1(v2) +# CHECK: = ForInStep v1, v0 position=v4 +# CHECK: [[copy]]([[carried]]: int[0, 8589934591]): +# CHECK-NEXT: [[position]]:{{*}}= Upcast [[carried]] +# CHECK: = Less [[position]], [[length]] +# CHECK: = ToEnumeratorPosition [[position]] +} + +function f1 name="overARecord" parameters=[{type=object}] results=[{type=undefined}] { + b0(v0: object ~ L0): + v1 = ForInOpen v0 + v2 = Integer integer=0 + Jump -> next b1(v2) + b1(v3: int[0, 8589934591]): + v4 = Upcast v3 castType=int[0, 8589934591] + LoopHint + v6 = ForInStep v1, v0 position=v4 + v7 = ResultAt v6 index=0 + v8 = ResultAt v6 index=1 + v9 = ResultAt v6 index=2 + v10 = Integer integer=1 + v11 = Add v9, v10 + Branch v7 -> taken b3(), notTaken b2() + b2(): + v12 = ElemGet v0, v8 + Jump -> next b1(v11) + b3(): + v13 = Undefined + Return v13 +# CHECK-COUNT-2: = IsEnumeratorCurrent v1, v0, +} From c6c92f52cb409e0bef2b585cbb202e9bac3a5ee4 Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Sun, 1 Nov 2026 15:29:30 -0800 Subject: [PATCH 34/49] AOT: the iterator of a set is told by its cell As "the iterator of a map is told by its cell", for %SetIterator%. Its lines in the same two tests: seven iterators of sets pass, whoever made them; the iterators of maps and sixteen others don't. Goes with Bun's half. --- .../aot/builtins/holders.builtins.ts | 1 + .../codegen/classes-told-by-their-cells.ir | 21 ++++++++++++++++++- .../types/classes-told-by-their-cells.ir | 14 ++++++++++--- 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/Source/JavaScriptCore/aot/builtins/holders.builtins.ts b/Source/JavaScriptCore/aot/builtins/holders.builtins.ts index 36299a8e8da98..1fc022703eed1 100644 --- a/Source/JavaScriptCore/aot/builtins/holders.builtins.ts +++ b/Source/JavaScriptCore/aot/builtins/holders.builtins.ts @@ -516,6 +516,7 @@ export default b.declarations( }), b.class("%SetIterator%", { ...lockedIteratorColumns, + jsType: "JSSetIteratorType", proto: { next: libraryStep("SetIteratorPrototype", "next", iteratorResult) }, }), b.class("%StringIterator%", { diff --git a/Source/JavaScriptCore/aot/tests/codegen/classes-told-by-their-cells.ir b/Source/JavaScriptCore/aot/tests/codegen/classes-told-by-their-cells.ir index aa682c9b5b1b5..55ba0a20e69ac 100644 --- a/Source/JavaScriptCore/aot/tests/codegen/classes-told-by-their-cells.ir +++ b/Source/JavaScriptCore/aot/tests/codegen/classes-told-by-their-cells.ir @@ -1,7 +1,8 @@ # The test for a class goes by the type of the cell: it passes for every instance, whoever made it and whatever it inherits from, and for nothing else. # FORM: AsRead -# RUN: run-in-shell globalThis.register = (name, test, make) => { const map = new Map([[1, 2]]), set = new Set([1]); const values = { MapIterator: make => [map.entries(), map.keys(), map.values(), map[Symbol.iterator](), Object.setPrototypeOf(map.entries(), null), new (class extends Map {})().entries(), make(map)], others: () => [Object.create(Object.getPrototypeOf(map.entries())), Object.getPrototypeOf(map.entries()), "a"[Symbol.iterator](), map, set, {}, [], () => 1, 1, "a", null, undefined] }; let all = 0, passed = 0, passedWrongly = 0; for (const key in values) { const list = values[key](key === name ? make : id); for (let index = 0; index < list.length; index++) { if (key === name) all++; if (test(list[index])) key === name ? passed++ : passedWrongly++; } } print(name, passed, "of", all, "and", passedWrongly, "others"); }; +# RUN: run-in-shell globalThis.register = (name, test, make) => { const map = new Map([[1, 2]]), set = new Set([1]); const values = { MapIterator: make => [map.entries(), map.keys(), map.values(), map[Symbol.iterator](), Object.setPrototypeOf(map.entries(), null), new (class extends Map {})().entries(), make(map)], SetIterator: make => [set.values(), set.keys(), set.entries(), set[Symbol.iterator](), Object.setPrototypeOf(set.values(), null), new (class extends Set {})().values(), make(set)], others: () => [Object.create(Object.getPrototypeOf(map.entries())), Object.getPrototypeOf(map.entries()), Object.create(Object.getPrototypeOf(set.values())), Object.getPrototypeOf(set.values()), "a"[Symbol.iterator](), map, set, {}, [], () => 1, 1, "a", null, undefined] }; let all = 0, passed = 0, passedWrongly = 0; for (const key in values) { const list = values[key](key === name ? make : id); for (let index = 0; index < list.length; index++) { if (key === name) all++; if (test(list[index])) key === name ? passed++ : passedWrongly++; } } print(name, passed, "of", all, "and", passedWrongly, "others"); }; # CHECK: MapIterator 7 of 7 and 0 others +# CHECK-NEXT: SetIterator 7 of 7 and 0 others # CHECK-NEXT: exited with 0 module m0 path="main.js" displayPath="main.js" body=f0 @@ -13,6 +14,10 @@ function f0 name="main" kind=Module results=[{type=anything}] { v3 = NewClosure function=f1 v4 = NewClosure function=f2 v5 = Call v1, -, v2, v3, v4 + v6 = String string="SetIterator" + v7 = NewClosure function=f3 + v8 = NewClosure function=f4 + v9 = Call v1, -, v6, v7, v8 Return } function f1 name="isMapIterator" parameters=[{type=anything}] results=[{type=anything}] { @@ -29,3 +34,17 @@ function f2 name="makeMapIterator" parameters=[{type=anything}] results=[{type=a b2(): Return } +function f3 name="isSetIterator" parameters=[{type=anything}] results=[{type=anything}] { + v1 = TypeTest v0 testedType=SetIterator + Return v1 +} +function f4 name="makeSetIterator" parameters=[{type=anything}] results=[{type=anything}] { + v1 = TypeTest v0 testedType=Set + Branch v1 -> taken b1(| 0), notTaken b2() + b1(v2): + v3 = PropGet v2 key="values" + v4 = Call v3, v2 + Return v4 + b2(): + Return +} diff --git a/Source/JavaScriptCore/aot/tests/types/classes-told-by-their-cells.ir b/Source/JavaScriptCore/aot/tests/types/classes-told-by-their-cells.ir index 640d49c2f5d62..e06fa65191ada 100644 --- a/Source/JavaScriptCore/aot/tests/types/classes-told-by-their-cells.ir +++ b/Source/JavaScriptCore/aot/tests/types/classes-told-by-their-cells.ir @@ -2,11 +2,19 @@ # RUN: intersect # CHECK: t0 & t1 = never # CHECK: t0 & t2 = never -# CHECK: t0 & t3 = MapIterator +# CHECK: t0 & t3 = never +# CHECK: t0 & t4 = MapIterator # CHECK: t1 & t0 = never +# CHECK: t1 & t2 = never +# CHECK: t1 & t3 = never +# CHECK: t1 & t4 = SetIterator # CHECK: t2 & t0 = never -# CHECK: t3 & t0 = MapIterator +# CHECK: t2 & t1 = never +# CHECK: t3 & t0 = never +# CHECK: t3 & t1 = never +# CHECK: t4 & t0 = MapIterator +# CHECK: t4 & t1 = SetIterator # CHECK: laws hold module m0 path="main.js" displayPath="main.js" -function f0 name="types" parameters=[{type=MapIterator}, {type=Map}, {type=Array}, {type=Array|MapIterator}] results=[{type=undefined}] +function f0 name="types" parameters=[{type=MapIterator}, {type=SetIterator}, {type=Map}, {type=Array}, {type=Array|MapIterator|SetIterator}] results=[{type=undefined}] From 030c7f1dd91ff65f6d41ec8ff085804a1c40fe4b Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Sun, 1 Nov 2026 15:29:30 -0800 Subject: [PATCH 35/49] AOT: an object held from ahead of the mock never reads the mock `import * as early from "./early.js"` at the head of a test file, then `vi.doMock("./dep.js", ..)`: early.js read the mock's values of dep.js where `bun test` reads the file's, without an error: in one realm, in a run that is built in layers, with dep.js loaded by an earlier test file. Of the two places where a mock can answer a read from outside, only the first respected that the module is read past its mock. Goes with Bun's half. --- .../aot/runtime/AOTModuleNamespace.h | 8 ++++- .../JavaScriptCore/aot/runtime/AOTModules.cpp | 35 +++++++++++++++++-- .../JavaScriptCore/aot/runtime/AOTModules.h | 3 +- 3 files changed, 42 insertions(+), 4 deletions(-) diff --git a/Source/JavaScriptCore/aot/runtime/AOTModuleNamespace.h b/Source/JavaScriptCore/aot/runtime/AOTModuleNamespace.h index 2cc2e5ebae724..b6df59094215c 100644 --- a/Source/JavaScriptCore/aot/runtime/AOTModuleNamespace.h +++ b/Source/JavaScriptCore/aot/runtime/AOTModuleNamespace.h @@ -48,8 +48,13 @@ class ModuleNamespace final : public JSNonFinalObject { bool isAnsweredByMock() const; JSObject* mockInPlace() const; // `vi.doMock()` over a module that has run leaves the module alone. Whoever held its object keeps it, and it answers for the module while the mock stands. - void setKeepsToModule(bool keepsToModule) { m_keepsToModule = keepsToModule; } + void setKeepsToModule(bool keepsToModule) + { + m_keepsToModule = keepsToModule; + m_neverReadsMock = false; + } bool keepsToModule() const { return m_keepsToModule; } + void setNeverReadsMock() { m_neverReadsMock = true; } // What is made for whoever asks under such a mock is the object of that mock, for good. void setMockOfView(VM& vm, JSObject* mock) { m_mockOfView.set(vm, this, mock); } void clearMockOfView() { m_mockOfView.clear(); } @@ -117,6 +122,7 @@ class ModuleNamespace final : public JSNonFinalObject { bool m_hasESModuleMarker { false }; bool m_hasOwnStructure { false }; bool m_keepsToModule { false }; + bool m_neverReadsMock { false }; }; } } // namespace JSC::AOT diff --git a/Source/JavaScriptCore/aot/runtime/AOTModules.cpp b/Source/JavaScriptCore/aot/runtime/AOTModules.cpp index 73371fbf617e3..9de0870c83c3a 100644 --- a/Source/JavaScriptCore/aot/runtime/AOTModules.cpp +++ b/Source/JavaScriptCore/aot/runtime/AOTModules.cpp @@ -1854,6 +1854,8 @@ auto Modules::mockThatAnswersForSlow(ModuleID passingOn, const ModuleExportData& }; for (size_t index : m_modulesWithMockInPlace) { JSObject* mock = mockInPlace(ModuleID(index)); + if (m_mockNeverAnswersForModuleReadPast && m_moduleReadPastItsMock && (ModuleID(index) == *m_moduleReadPastItsMock || moduleWithExportsOf(ModuleID(index)) == *m_moduleReadPastItsMock)) + continue; if (!mock || !seesMockOf(ModuleID(index), passingOn)) continue; ModuleID declaring = moduleWithExportsOf(ModuleID(index)); @@ -2708,6 +2710,34 @@ bool Modules::setMockOverModuleThatHasRun(ModuleID module, JSObject* mock, JSVal if (isFile) loaded.exportsOfOriginalBesideMock.set(*this, commonJSExports(module)); answerRequire(); + // An importer of an image above reads the file through the object in its own module of the file. Where it was linked ahead of the mock in this test file, the mock never answers for that object, + // whichever test file loaded the file. + for (Instance* instance = m_instance.above; instance; instance = instance->above) { + Modules* above = instance->state ? instance->state->modules.get() : nullptr; + if (!above) + continue; + for (const UncompiledModuleData& entry : above->m_table.modulesOfUncompiledCode()) { + ModuleID row(entry.module); + const Identifier* key = above->resolvedKeyIfKnown(row); + std::optional file = key ? findByKey(*key) : std::nullopt; + if (!file || (*file != module && *file != withExports) || !above->value(row).isCell()) + continue; + auto* held = dynamicDowncast(above->value(row).asCell()); + if (!held || !held->keepsToModule()) + continue; + bool isHeldFromAheadOfMock = false; + bool isSeenBySomeImporter = false; + for (uint32_t importer = 0; importer < above->m_table.size(); ++importer) { + for (const ModuleRequestData& request : above->m_table.imports(ModuleID(importer))) { + if (request.targetModule() != row || request.isHoisted || request.isDeferred || above->status(ModuleID(importer)) <= ModuleStatus::Linked) + continue; + (isMockSeenFrom(module, *above, ModuleID(importer)) ? isSeenBySomeImporter : isHeldFromAheadOfMock) = true; + } + } + if (isHeldFromAheadOfMock && !isSeenBySomeImporter) + held->setNeverReadsMock(); + } + } return true; } @@ -2873,8 +2903,9 @@ bool Modules::isHiddenInModuleItself(ModuleID module, unsigned index) const return (m_table.exports(module)[index].isFromStarExport && isExportAmbiguous(module, index)) || isLeftOut(module, index, TablesBehindStars::MayLackName); } -JSValue Modules::readExportOfModuleItself(ModuleID module, unsigned index) +JSValue Modules::readExportOfModuleItself(ModuleID module, unsigned index, bool neverReadsMock) { + SetForScope never(m_mockNeverAnswersForModuleReadPast, neverReadsMock); SetForScope readsPast(m_moduleReadPastItsMock, module); return readExportFromOutside(module, index); } @@ -9188,7 +9219,7 @@ bool ModuleNamespace::getOwnPropertySlot(JSObject* object, JSGlobalObject* globa JSValue value = jsUndefined(); if (method != InternalMethodType::HasProperty) { if (index && thisObject->m_keepsToModule) [[unlikely]] - value = thisObject->modules().readExportOfModuleItself(thisObject->m_module, *index); + value = thisObject->modules().readExportOfModuleItself(thisObject->m_module, *index, thisObject->m_neverReadsMock); else value = index ? thisObject->modules().readExportFromOutside(thisObject->m_module, *index) : thisObject->readDynamicExport(globalObject, name); RETURN_IF_EXCEPTION(scope, false); diff --git a/Source/JavaScriptCore/aot/runtime/AOTModules.h b/Source/JavaScriptCore/aot/runtime/AOTModules.h index 1250d9e345c3a..157721b9f65fa 100644 --- a/Source/JavaScriptCore/aot/runtime/AOTModules.h +++ b/Source/JavaScriptCore/aot/runtime/AOTModules.h @@ -235,7 +235,7 @@ class Modules { JSObject* namespaceOfModuleItself(ModuleID); JSValue importModuleItselfBesideMock(ModuleID, JSValue answerOfImport); bool isHiddenInModuleItself(ModuleID, unsigned index) const; - JSValue readExportOfModuleItself(ModuleID, unsigned index); + JSValue readExportOfModuleItself(ModuleID, unsigned index, bool neverReadsMock = false); // `bun test` takes whoever imports a module out of its loader when a mock of the module ends, and when a mock is laid over what an earlier test file loaded. A mock of such an importer // comes ahead of its load, though the body has run here. enum class ImportersLetGo : bool { All, AllButThoseThatThisTestFileLinkedToModule }; @@ -1311,6 +1311,7 @@ class Modules { void setAsideNamespaceUnderMock(ModuleID); bool m_makesNamespaceOfModuleItself { false }; std::optional m_moduleReadPastItsMock; + bool m_mockNeverAnswersForModuleReadPast { false }; UncheckedKeyHashMap m_mocksOverModulesThatHaveRun; struct FirstMockInTestFile { uint32_t moment; From 11c292d5d3e57b4cf1ef9d9dadb9ad379d2858dd Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Sun, 1 Nov 2026 15:29:30 -0800 Subject: [PATCH 36/49] AOT: one forward walk builds a body anew in place of the compaction's five helpers aot/ir/AOTForwardWalk.{h,cpp}: a walk that reads a body once, in an order in which a block stands behind the blocks that dominate it, and builds the body anew while it reads. compactFunction() calls it, and the five static helpers that it replaces go. The order of the blocks is today's. No output changes. --- Source/JavaScriptCore/aot/Sources.txt | 1 + .../JavaScriptCore/aot/ir/AOTForwardWalk.cpp | 110 +++++++++++++ Source/JavaScriptCore/aot/ir/AOTForwardWalk.h | 45 +++++ .../JavaScriptCore/aot/ir/AOTIRFunction.cpp | 155 +++--------------- 4 files changed, 177 insertions(+), 134 deletions(-) create mode 100644 Source/JavaScriptCore/aot/ir/AOTForwardWalk.cpp create mode 100644 Source/JavaScriptCore/aot/ir/AOTForwardWalk.h diff --git a/Source/JavaScriptCore/aot/Sources.txt b/Source/JavaScriptCore/aot/Sources.txt index c14e42fc18420..680e4518e1163 100644 --- a/Source/JavaScriptCore/aot/Sources.txt +++ b/Source/JavaScriptCore/aot/Sources.txt @@ -159,6 +159,7 @@ aot/ir/AOTEffectsOfOperators.cpp aot/ir/AOTEffectsOfProtocols.cpp aot/ir/AOTEffectsOfRules.cpp aot/ir/AOTFieldValueNarrowing.cpp +aot/ir/AOTForwardWalk.cpp aot/ir/AOTIRFunction.cpp aot/ir/AOTIRProgram.cpp aot/ir/AOTKnownClosures.cpp diff --git a/Source/JavaScriptCore/aot/ir/AOTForwardWalk.cpp b/Source/JavaScriptCore/aot/ir/AOTForwardWalk.cpp new file mode 100644 index 0000000000000..49446df6c5f94 --- /dev/null +++ b/Source/JavaScriptCore/aot/ir/AOTForwardWalk.cpp @@ -0,0 +1,110 @@ +/* + * Copyright (C) 2026 Oven, Inc. All rights reserved. + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include "config.h" +#include "AOTForwardWalk.h" + +#if ENABLE(AOT) + +#include "AOTSort.h" + +namespace JSC { namespace AOT { namespace Typed { + +ForwardWalk::ForwardWalk(Function& function) + : m_function(function) +{ +} + +void ForwardWalk::mapToOutput(ValueID& value, UseInFrontOfDefinition use) +{ + if (ValueID output = m_outputOf[value.index()]) [[likely]] { + value = output; + return; + } + m_usesInFrontOfDefinitions.append(use); +} + +void ForwardWalk::mapUsesInFrontOfDefinitions() +{ + for (UseInFrontOfDefinition use : m_usesInFrontOfDefinitions) { + uint32_t indexOfChild = 0; + auto mapIfMeant = [&](ValueID& child) { + if (indexOfChild++ != use.indexOfChild) + return; + child = m_outputOf[child.index()]; + RELEASE_ASSERT(child); + }; + if (use.isOutsideValues) + m_function.storageOfBlocks()[use.indexOfHolder].forEachChildOutsideValues(mapIfMeant); + else + m_function.storageOfValues()[use.indexOfHolder].forEachChild(mapIfMeant); + } +} + +ValueID ForwardWalk::emit(ValueID input) +{ + RELEASE_ASSERT(!m_outputOf[input.index()]); + Function::Values::Storage& output = m_function.storageOfValues(); + Value value = WTF::move(m_inputValues[input.index()]); + uint32_t indexOfChild = 0; + value.forEachChild([&](ValueID& child) { mapToOutput(child, { false, static_cast(output.size()), indexOfChild++ }); }); + ValueID id = m_function.idOfValueAt(output.size()); + m_outputOf[input.index()] = id; + output.append(WTF::move(value)); + return id; +} + +void ForwardWalk::walkBlock(BlockID input) +{ + Block block = WTF::move(m_inputBlocks[input.index()]); + for (ValueID& parameter : block.parameters) + parameter = emit(parameter); + for (ValueID& value : block.values) + value = emit(value); + uint32_t indexOfChild = 0; + block.forEachChildOutsideValues([&](ValueID& child) { mapToOutput(child, { true, static_cast(m_function.storageOfBlocks().size()), indexOfChild++ }); }); + block.forEachTarget([&](Target& target) { + target.block = m_outputBlockOf[target.block.index()]; + RELEASE_ASSERT(target.block); + }); + m_function.storageOfBlocks().append(WTF::move(block)); +} + +Renumbering ForwardWalk::run(std::span order) +{ + RELEASE_ASSERT(!order.empty() && order[0] == entryBlock); + m_outputBlockOf = Vector(m_function.blocks.size()); + size_t numberOfValues = 0; + for (size_t index = 0; index < order.size(); ++index) { + m_outputBlockOf[order[index].index()] = BlockID(static_cast(index)); + const Block& block = m_function.at(order[index]); + numberOfValues += block.parameters.size() + block.values.size(); + } + + m_inputBlocks = m_function.takeBlocks(); + m_inputValues = m_function.takeValues(); + m_outputOf = Vector(m_inputValues.size()); + m_function.storageOfBlocks().reserveInitialCapacity(order.size()); + m_function.storageOfValues().reserveInitialCapacity(numberOfValues); +#if AOT_STAMPS_VALUE_IDS + m_function.stampOfValues = m_function.stampOfValues % ValueID::numberOfStamps + 1; +#endif + for (BlockID block : order) + walkBlock(block); + mapUsesInFrontOfDefinitions(); + + if (!m_function.writtenPositions.isEmpty()) { + m_function.writtenPositions.removeAllMatching([&](WrittenPosition& written) { + written.value = written.value.index() < m_outputOf.size() ? m_outputOf[written.value.index()] : ValueID(); + return !written.value; + }); + AOT::sortInLine(m_function.writtenPositions, { }, [](const WrittenPosition& written) { return written.value.index(); }); + } + return { WTF::move(m_outputBlockOf), WTF::move(m_outputOf) }; +} + +} } } // namespace JSC::AOT::Typed + +#endif // ENABLE(AOT) diff --git a/Source/JavaScriptCore/aot/ir/AOTForwardWalk.h b/Source/JavaScriptCore/aot/ir/AOTForwardWalk.h new file mode 100644 index 0000000000000..be23446fd5b9b --- /dev/null +++ b/Source/JavaScriptCore/aot/ir/AOTForwardWalk.h @@ -0,0 +1,45 @@ +/* + * Copyright (C) 2026 Oven, Inc. All rights reserved. + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#if ENABLE(AOT) + +#include "AOTIRFunction.h" + +namespace JSC { namespace AOT { namespace Typed { + +// Reads a body once and builds it anew while it reads. In the order of the walk a block stands behind the blocks that dominate it. +class ForwardWalk { + WTF_MAKE_NONCOPYABLE(ForwardWalk); +public: + explicit ForwardWalk(Function&); + + Renumbering run(std::span order); + +private: + // Only a body that nobody has verified yet holds one. + struct UseInFrontOfDefinition { + bool isOutsideValues; + uint32_t indexOfHolder; + uint32_t indexOfChild; + }; + + void walkBlock(BlockID); + ValueID emit(ValueID input); + void mapToOutput(ValueID&, UseInFrontOfDefinition); + void mapUsesInFrontOfDefinitions(); + + Function& m_function; + Function::Blocks::Storage m_inputBlocks; + Function::Values::Storage m_inputValues; + Vector m_outputBlockOf; + Vector m_outputOf; + Vector m_usesInFrontOfDefinitions; +}; + +} } } // namespace JSC::AOT::Typed + +#endif // ENABLE(AOT) diff --git a/Source/JavaScriptCore/aot/ir/AOTIRFunction.cpp b/Source/JavaScriptCore/aot/ir/AOTIRFunction.cpp index d9120720e8949..38fdc8cb11717 100644 --- a/Source/JavaScriptCore/aot/ir/AOTIRFunction.cpp +++ b/Source/JavaScriptCore/aot/ir/AOTIRFunction.cpp @@ -10,6 +10,7 @@ #include "AOTCFG.h" #include "AOTDiagnostics.h" +#include "AOTForwardWalk.h" #include "AOTObservableOperations.h" #include "AOTParkedBodies.h" #include "AOTProvisionalFunctions.h" @@ -551,11 +552,13 @@ OptionSet capabilitiesInPlaceOf(const Function& replacemen return (initialCapabilitiesOf(replacement) - kept) | (ofReplaced & kept); } -static bool hasEarlierPredecessors(const Function& function) +static bool hasEarlierPredecessors(const Function& function, const CFG* blocksToLookAt = nullptr) { unsigned count = function.blocks.size(); BitVector hasEarlierPredecessor(count); for (unsigned index = 0; index < count; ++index) { + if (blocksToLookAt && !blocksToLookAt->isReachable(BlockID(index))) + continue; if (BlockID(index) != entryBlock && !hasEarlierPredecessor.quickGet(index)) return false; function.blocks[index].forEachTarget([&](const Target& target) { @@ -567,129 +570,6 @@ static bool hasEarlierPredecessors(const Function& function) return true; } -static BitVector findReachableBlocks(const Function& function) -{ - unsigned count = function.blocks.size(); - BitVector isReachable(count); - Vector worklist; - isReachable.quickSet(entryBlock.index()); - worklist.append(entryBlock); - while (!worklist.isEmpty()) { - BlockID block = worklist.takeLast(); - function.at(block).forEachTarget([&](const Target& target) { - RELEASE_ASSERT(target.block.index() < count); - if (!isReachable.quickSet(target.block.index())) - worklist.append(target.block); - }); - } - return isReachable; -} - -static Vector removeUnreachableBlocks(Function& function, const BitVector& isReachable) -{ - unsigned count = function.blocks.size(); - if (isReachable.bitCount() == count) - return { }; - - Vector movedTo(count); - Function::Blocks::Storage& blocks = function.editAllBlocks(); - unsigned next = 0; - for (unsigned b = 0; b < count; ++b) { - if (!isReachable.quickGet(b)) - continue; - if (next != b) - blocks[next] = WTF::move(blocks[b]); - movedTo[b] = BlockID(next++); - } - blocks.shrink(next); - for (auto& block : blocks) { - block.forEachTarget([&](Target& target) { - target.block = movedTo[target.block.index()]; - }); - } - return movedTo; -} - -static Vector sortIntoReversePostOrder(Function& function) -{ - CFG cfg(function); - std::span order = cfg.reversePostOrder(); - RELEASE_ASSERT(order.size() == function.blocks.size() && order[0] == entryBlock); - - Function::Blocks::Storage blocks; - blocks.reserveInitialCapacity(order.size()); - for (BlockID block : order) - blocks.append(WTF::move(function.editAllBlocks()[block.index()])); - function.editAllBlocks() = WTF::move(blocks); - - for (auto& block : function.editAllBlocks()) { - block.forEachTarget([&](Target& target) { - target.block = BlockID(cfg.reversePostOrderIndex(target.block)); - }); - } - return Vector(order.size(), [&](size_t index) { - return BlockID(cfg.reversePostOrderIndex(BlockID(index))); - }); -} - -static Vector movedFirstAndThen(Vector&& first, Vector&& then) -{ - if (first.isEmpty()) - return WTF::move(then); - if (then.isEmpty()) - return WTF::move(first); - for (BlockID& block : first) { - if (block) - block = then[block.index()]; - } - return WTF::move(first); -} - -static Vector renumberValues(Function& function, size_t numberOfListedValues) -{ - Vector newID(function.values.size()); - Function::Values::Storage values; - Function::Values::Storage& formerValues = function.editAllValues(); - Function::Blocks::Storage& blocks = function.editAllBlocks(); - values.reserveInitialCapacity(numberOfListedValues); -#if AOT_STAMPS_VALUE_IDS - function.stampOfValues = function.stampOfValues % ValueID::numberOfStamps + 1; -#endif - auto moveListedValue = [&](ValueID& id) { - unsigned oldIndex = id.index(); - RELEASE_ASSERT(!newID[oldIndex]); - id = function.idOfValueAt(values.size()); - newID[oldIndex] = id; - values.append(WTF::move(formerValues[oldIndex])); - }; - for (auto& block : blocks) { - for (ValueID& parameter : block.parameters) - moveListedValue(parameter); - for (ValueID& value : block.values) - moveListedValue(value); - } - - auto remapChild = [&](ValueID& child) { - child = newID[child.index()]; - RELEASE_ASSERT(child); - }; - for (auto& value : values) - value.forEachChild(remapChild); - for (auto& block : blocks) - block.forEachChildOutsideValues(remapChild); - - formerValues = WTF::move(values); - - if (!function.writtenPositions.isEmpty()) { - function.writtenPositions.removeAllMatching([&](WrittenPosition& written) { - written.value = written.value.index() < newID.size() ? newID[written.value.index()] : ValueID(); - return !written.value; - }); - AOT::sortInLine(function.writtenPositions, { }, [](const WrittenPosition& written) { return written.value.index(); }); - } - return newID; -} - static size_t countListedValues(const Function& function) { size_t count = 0; @@ -698,6 +578,21 @@ static size_t countListedValues(const Function& function) return count; } +static Vector orderToKeep(const Function& function, bool isOrdered) +{ + if (isOrdered) + return Vector(function.blocks.size(), [](size_t index) { return BlockID(static_cast(index)); }); + CFG cfg(function); + if (!hasEarlierPredecessors(function, &cfg)) + return Vector(cfg.reversePostOrder()); + Vector reachableBlocks; + for (unsigned index = 0; index < function.blocks.size(); ++index) { + if (cfg.isReachable(BlockID(index))) + reachableBlocks.append(BlockID(index)); + } + return reachableBlocks; +} + bool needsCompaction(const Function& function) { return function.hasBody() && (!hasEarlierPredecessors(function) || countListedValues(function) != function.values.size()); @@ -710,22 +605,14 @@ bool compactFunction(Function& function, Renumbering* renumbering) bool valuesAreTypedAsDefined = function.valuesAreTypedAsDefined(); bool wasOrdered = hasEarlierPredecessors(function); - Vector movedBlocks; - if (!wasOrdered) { - movedBlocks = removeUnreachableBlocks(function, findReachableBlocks(function)); - if (!hasEarlierPredecessors(function)) - movedBlocks = movedFirstAndThen(WTF::move(movedBlocks), sortIntoReversePostOrder(function)); - } - - size_t numberOfListedValues = countListedValues(function); - if (wasOrdered && numberOfListedValues == function.values.size()) { + if (wasOrdered && countListedValues(function) == function.values.size()) { #if AOT_STAMPS_VALUE_IDS function.stampNumbersInPlace(); #endif return false; } - Vector movedValues = renumberValues(function, numberOfListedValues); + auto [movedBlocks, movedValues] = ForwardWalk(function).run(orderToKeep(function, wasOrdered).span()); if (ReasonsForOperationsThatWent* listener = function.listenerForOperationsThatGo()) [[unlikely]] listener->didRenumber(movedBlocks.span(), movedValues.span()); if (renumbering) From c4f59362fce9a54afea9952b1f3816af42c8f318 Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Sun, 1 Nov 2026 15:29:30 -0800 Subject: [PATCH 37/49] AOT: an import of a module that compiled code began settles when it has run main.mjs: `require("./mid.mjs")`; mid.mjs: `import "./a.mjs"; require("./b.mjs")`; b.mjs: `import("./mid.mjs").then(settled, settled)`. `bun run` and `bun build --compile`: settled. Compiled ahead of time the promise never settled: exit 0, and nothing said. The same with `await import("./mid.mjs")` in the entry, with `await` in an async function of b, and with two calls. Goes with Bun's half. --- Source/JavaScriptCore/aot/runtime/AOTModuleEvaluation.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Source/JavaScriptCore/aot/runtime/AOTModuleEvaluation.cpp b/Source/JavaScriptCore/aot/runtime/AOTModuleEvaluation.cpp index 97813ca14fe01..9bad89d179fb0 100644 --- a/Source/JavaScriptCore/aot/runtime/AOTModuleEvaluation.cpp +++ b/Source/JavaScriptCore/aot/runtime/AOTModuleEvaluation.cpp @@ -2739,13 +2739,16 @@ void Modules::evaluateWithOwnStack(ModuleID root) Exception* exception = scope.exception(); ASSERT(exception || stack.isEmpty()); // An import() of the root that was called while the root ran has given it a capability, which CyclicModuleRecord::evaluate() would settle here. - ModuleRecord* record = recordIfExists(root); + // Where compiled code began the root, the table has its status and not the record. + ModuleRecord* record = m_records.get(root); JSPromise* capability = record ? record->topLevelCapability() : nullptr; if (capability && capability->status() != JSPromise::Status::Pending) capability = nullptr; if (!exception) { - if (capability) [[unlikely]] + if (capability) [[unlikely]] { + record->takeStatus(); capability->fulfill(vm, jsUndefined()); + } return; } for (AbstractModuleRecord* member : stack) { From 0e94bf3f3164b7b0f43365ea51a7346116a15c4e Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Sun, 1 Nov 2026 15:29:30 -0800 Subject: [PATCH 38/49] AOT: the jobs behind a delivered text run ahead of the next file's body Under `bun test --aot`, `import("./a.js").then(() => setTimeout(() => { import("./b.js"); /* thirty awaits, then a line */ }, 0))` ran b's body ahead of the thirty jobs. `bun test` runs the jobs first, because a file's text comes in a later turn. It came with "an import cycle is entered where the first import() call enters it"; Bun's main is wrong there too. Goes with Bun's half, which calls a new entry of the run time. --- .../JavaScriptCore/aot/runtime/AOTModuleHost.h | 2 ++ .../JavaScriptCore/aot/runtime/AOTModules.cpp | 17 ++++++++++------- Source/JavaScriptCore/aot/runtime/AOTModules.h | 2 +- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/Source/JavaScriptCore/aot/runtime/AOTModuleHost.h b/Source/JavaScriptCore/aot/runtime/AOTModuleHost.h index 3d35a0e61371a..4c37198c0c0e8 100644 --- a/Source/JavaScriptCore/aot/runtime/AOTModuleHost.h +++ b/Source/JavaScriptCore/aot/runtime/AOTModuleHost.h @@ -178,6 +178,8 @@ JS_EXPORT_PRIVATE void forEachFirstLoad(VM&, const ScopedLambdafulfill(vm, text.requestedRecord.get()); return true; } - // The next turn ends it: no job of the run time's own stands among those of the program meanwhile. - if (text.isOnlyDelayed && !std::exchange(m_isInJobsBehindDelivery, true)) - host().callInLaterTurn(m_instance.globalObject); + // The host says when they have run: no job of the run time's own stands among those of the program meanwhile. + m_isInJobsBehindDelivery |= text.isOnlyDelayed; if (text.jobsBehindFirstCall) { m_callsToMakeAgain.append(WTF::move(text)); makeCallAgain(promise); @@ -4681,10 +4680,6 @@ void deliverTextThatWaits(VM& vm) auto scope = DECLARE_TOP_EXCEPTION_SCOPE(vm); VMState& state = vm.aotState(); ++state.turnOfTexts; - for (Instance* instance : state.instances) { - if (Modules* modules = instance->state ? instance->state->modules.get() : nullptr) - modules->didBeginTurn(); - } bool isDelivered = false; while (!isDelivered && (state.numberOfTextsThatWait || state.numberOfCallsThatWaitApart)) { Modules* first = nullptr; @@ -4709,6 +4704,14 @@ void deliverTextThatWaits(VM& vm) } } +void didRunJobsBehindDelivery(VM& vm) +{ + for (Instance* instance : vm.aotState().instances) { + if (Modules* modules = instance->state ? instance->state->modules.get() : nullptr) + modules->didRunJobsBehindDelivery(); + } +} + static JSC_DECLARE_HOST_FUNCTION(importBehindFactories); static JSC_DECLARE_HOST_FUNCTION(importFromOtherImageBehindFactories); static JSC_DECLARE_HOST_FUNCTION(resumeImportsBehindSettledLoad); diff --git a/Source/JavaScriptCore/aot/runtime/AOTModules.h b/Source/JavaScriptCore/aot/runtime/AOTModules.h index 157721b9f65fa..8bdd35bb5b0ab 100644 --- a/Source/JavaScriptCore/aot/runtime/AOTModules.h +++ b/Source/JavaScriptCore/aot/runtime/AOTModules.h @@ -685,7 +685,7 @@ class Modules { enum class AskedBy : bool { Loader, Import }; bool textOfImportComesInLaterTurn(ModuleID, AskedBy); bool hasTextAtHand() const; - void didBeginTurn() { m_isInJobsBehindDelivery = false; } + void didRunJobsBehindDelivery() { m_isInJobsBehindDelivery = false; } bool isFirstTextThatWaitsOnlyDelayed() const { return !m_textsThatWait.isEmpty() && m_textsThatWait[m_numberOfTextsThatHaveCome].isOnlyDelayed; } // No text of the program is read, and all of the program is one image. bool loadsAsExecutableDoes() const; From 03b21882b83843cffaf3cf9e40309fd46d2e3315 Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Sun, 1 Nov 2026 15:29:30 -0800 Subject: [PATCH 39/49] AOT: a call or a new in the place of what is constructed is quoted as Bun prints it `const p = Object.create(K.prototype); new p()`. Plain: "K is not a constructor (evaluating 'new (Object.create(K.prototype))')". Compiled: "K is not a constructor": the quote was missing, or wrong: 'new make', 'new new K', 'new q' for `new p.q()`. 71 of 158 forms differed. The text of an error alone. Goes with Bun's half. --- .../aot/runtime/AOTErrorMessages.cpp | 32 ++++++++++++++----- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/Source/JavaScriptCore/aot/runtime/AOTErrorMessages.cpp b/Source/JavaScriptCore/aot/runtime/AOTErrorMessages.cpp index a3489ad134a14..418f7659cea83 100644 --- a/Source/JavaScriptCore/aot/runtime/AOTErrorMessages.cpp +++ b/Source/JavaScriptCore/aot/runtime/AOTErrorMessages.cpp @@ -3147,19 +3147,30 @@ JSObject* createSuperBaseNotAnObjectError(JSGlobalObject* globalObject, JSValue } // `new a.K(b)` and `new K`, but not `new K(b).c()`, which calls. -static bool isConstruction(const String& source) +static bool isConstruction(std::span text) { - if (!source.startsWith("new "_s)) + if (!spanHasPrefix(text, byteCast("new "_span)) || text.size() == 4) return false; - CString bytes = source.utf8(); - std::span text = byteCast(bytes.span()); std::optional open = text.back() == ')' ? findOpeningBracket(text, text.size() - 1) : std::nullopt; if (!open) return true; // `new (void 0)(b)` and `new (void 0)`: the first brackets hold what is constructed. - if (text[4] == '(' && *open <= findClosingBracket(text, 4).value_or(0) + 1) + if (text[4] == '(') { + size_t close = findClosingBracket(text, 4).value_or(0); + // `new (make()).member(b)`: and what is read from them. + if (*open <= close + 1 || WTF::find(text.subspan(close, *open - close), static_cast('(')) == notFound) + return true; + } + if (WTF::find(text.first(*open), static_cast('(')) == notFound) return true; - return WTF::find(text.first(*open), static_cast('(')) == notFound; + // `new new K(a)(b)`: what is constructed is what the inner `new` made. + return *open > 4 && isConstruction(text.subspan(4, *open - 4)); +} + +static bool isConstruction(const String& source) +{ + CString bytes = source.utf8(); + return isConstruction(byteCast(bytes.span())); } JSObject* createNotAFunctionError(JSGlobalObject* globalObject, JSValue callee, StringView name) @@ -3187,8 +3198,13 @@ static String quoteConstruction(VM& vm, StringView name) source = findOnTop(vm, "[]"_s, findWrittenCallsOfElements); if (source.isNull() || !isConstruction(source)) return name.isNull() ? String() : tryMakeString("new "_s, name); - // Bun's transpiler prints `new K()` as `new K`. - return source.endsWith("()"_s) ? source.left(source.length() - 2) : source; + // Bun's transpiler prints `new K()` as `new K`. What is constructed keeps its own brackets: `new new K()`. + if (!source.startsWith("new new "_s)) + return source.endsWith("()"_s) ? source.left(source.length() - 2) : source; + size_t firstBracket = source.find('('); + if (firstBracket == notFound) + return tryMakeString(source, "()"_s); + return source.endsWith("()"_s) && firstBracket + 2 < source.length() ? source.left(source.length() - 2) : source; } // A constructor that nobody wrote passes on what it is given. From 8994ff7600587a679d8f20d5810047250e141315 Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Sun, 1 Nov 2026 15:29:30 -0800 Subject: [PATCH 40/49] AOT: a closure that a callee hands back keeps what it captured `function lender(way, n) { let a = n * 2; const r = way(() => a++); a += 10; return r; }`, where `way` is one of fifteen functions at that site and the one that runs is `function returnsIt(f) { return f; }`. The closure called later: plain prints `12 13`; compiled code printed a wrong number or crashed. Goes with Bun's half. --- .../aot/passes/AOTFrameAllocationPlanner.cpp | 3 +- ...allocate-in-frames-lent-over-a-variable.ir | 298 ++++++++++++++++++ 2 files changed, 300 insertions(+), 1 deletion(-) create mode 100644 Source/JavaScriptCore/aot/tests/passes/allocate-in-frames-lent-over-a-variable.ir diff --git a/Source/JavaScriptCore/aot/passes/AOTFrameAllocationPlanner.cpp b/Source/JavaScriptCore/aot/passes/AOTFrameAllocationPlanner.cpp index f79f3c85aa1f7..cf1470d441995 100644 --- a/Source/JavaScriptCore/aot/passes/AOTFrameAllocationPlanner.cpp +++ b/Source/JavaScriptCore/aot/passes/AOTFrameAllocationPlanner.cpp @@ -942,9 +942,10 @@ bool FramePlanner::mayLeakContentsThrough(const ObjectUse& entry) const return *known; } +// Whoever is lent a closure and does not borrow it moves it to the heap, with what it holds. bool FramePlanner::leaksContents(ObjectID cell) const { - return std::ranges::any_of(m_escape.usesOf(cell), [&](const ObjectUse& entry) { + return isLent(cell) || std::ranges::any_of(m_escape.usesOf(cell), [&](const ObjectUse& entry) { return mayLeakContentsThrough(entry); }); } diff --git a/Source/JavaScriptCore/aot/tests/passes/allocate-in-frames-lent-over-a-variable.ir b/Source/JavaScriptCore/aot/tests/passes/allocate-in-frames-lent-over-a-variable.ir new file mode 100644 index 0000000000000..8a90a6b8b90ed --- /dev/null +++ b/Source/JavaScriptCore/aot/tests/passes/allocate-in-frames-lent-over-a-variable.ir @@ -0,0 +1,298 @@ +# -- source: main.js +# function lender(way, n) { let a = n * 2; const r = way(() => a++); a += 10; return r; } +# function returnsIt(f) { return f; } +# function b0(f) { return f() + 0; } +# function b1(f) { return f() + 1; } +# function b2(f) { return f() + 2; } +# function b3(f) { return f() + 3; } +# function b4(f) { return f() + 4; } +# function b5(f) { return f() + 5; } +# function b6(f) { return f() + 6; } +# function b7(f) { return f() + 7; } +# function b8(f) { return f() + 8; } +# function b9(f) { return f() + 9; } +# function b10(f) { return f() + 10; } +# function b11(f) { return f() + 11; } +# function b12(f) { return f() + 12; } +# function b13(f) { return f() + 13; } +# const ways = [b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, returnsIt]; +# for (let i = 0; i < 15; i++) globalThis.answer = lender(ways[i], 1); +# -- end of source +# The stub of the call moves a lent closure to the heap for a callee that does not borrow it, here one that returns it, and the copy takes along what the closure holds. So the environment is one of the heap. +# FORM: AsRead +# OPTIONS: check=VerifyEachPass +# STATISTIC: ClosuresLent = 1 +# RUN: pipeline AllocateInFrames +# CHECK: env {{*}} = NewObject +# CHECK-NOT: storage= +# CHECK: = NewClosure {{*}}storage=Frame [isLent] + +module m0 path="main.js" displayPath="main.js" body=f0 namespaceLayout=L(m0) +entry module=m0 +layout L0 kind=Environment module=m0 fields=[{key=@"binding.0" declared={type=number|absent flags=[IsExpected]} variableName="a"}] ownFieldCount=1 +function f0 module=m0 kind=Module thisType={type=anything} results=[{type=anything}] capabilities=[MayChangeSignature, MayBeCloned, MayBeInlined, HasOrdinaryFrame, IsNeverCalledByHost] { + b0(): + Jump -> next b1() @0 + b1(): + v0 = NewClosure function=f1 @0 + v1 = Convert v0 declared={type=f1 flags=[IsFromCompiler]} illTypedCause=FromLooseType @0 + v2 = NewClosure function=f2 @0 + v3 = Convert v2 declared={type=f2 flags=[IsFromCompiler]} illTypedCause=FromLooseType @0 + v4 = NewClosure function=f3 @0 + v5 = Convert v4 declared={type=f3 flags=[IsFromCompiler]} illTypedCause=FromLooseType @0 + v6 = NewClosure function=f4 @0 + v7 = Convert v6 declared={type=f4 flags=[IsFromCompiler]} illTypedCause=FromLooseType @0 + v8 = NewClosure function=f5 @0 + v9 = Convert v8 declared={type=f5 flags=[IsFromCompiler]} illTypedCause=FromLooseType @0 + v10 = NewClosure function=f6 @0 + v11 = Convert v10 declared={type=f6 flags=[IsFromCompiler]} illTypedCause=FromLooseType @0 + v12 = NewClosure function=f7 @0 + v13 = Convert v12 declared={type=f7 flags=[IsFromCompiler]} illTypedCause=FromLooseType @0 + v14 = NewClosure function=f8 @0 + v15 = Convert v14 declared={type=f8 flags=[IsFromCompiler]} illTypedCause=FromLooseType @0 + v16 = NewClosure function=f9 @0 + v17 = Convert v16 declared={type=f9 flags=[IsFromCompiler]} illTypedCause=FromLooseType @0 + v18 = NewClosure function=f10 @0 + v19 = Convert v18 declared={type=f10 flags=[IsFromCompiler]} illTypedCause=FromLooseType @0 + v20 = NewClosure function=f11 @0 + v21 = Convert v20 declared={type=f11 flags=[IsFromCompiler]} illTypedCause=FromLooseType @0 + v22 = NewClosure function=f12 @0 + v23 = Convert v22 declared={type=f12 flags=[IsFromCompiler]} illTypedCause=FromLooseType @0 + v24 = NewClosure function=f13 @0 + v25 = Convert v24 declared={type=f13 flags=[IsFromCompiler]} illTypedCause=FromLooseType @0 + v26 = NewClosure function=f14 @0 + v27 = Convert v26 declared={type=f14 flags=[IsFromCompiler]} illTypedCause=FromLooseType @0 + v28 = NewClosure function=f15 @0 + v29 = Convert v28 declared={type=f15 flags=[IsFromCompiler]} illTypedCause=FromLooseType @0 + v30 = NewClosure function=f16 @0 + v31 = Convert v30 declared={type=f16 flags=[IsFromCompiler]} illTypedCause=FromLooseType @0 + v32 = NewArray v5, v7, v9, v11, v13, v15, v17, v19, v21, v23, v25, v27, v29, v31, v3 contents=undefined|null|function|f2|f3|f4|f5|f6|f7|f8|f9|f10|f11|f12|f13|f14|f15|f16 @635 + v33 = Convert v32 declared={type=undefined|null|Array flags=[IsExpected]} @635 + v34 = Integer @721 + v35 = Convert v34 declared={type=i32 flags=[IsExpected]} @721 + Jump -> next b2(v35) @717 + b2(v36): + LoopHint @717 + v38 = Integer integer=15 @728 + v39 = Less v36, v38 @724 + Branch v39 -> taken b3(| 0), notTaken b6(| 0) @724 + b3(v40): + Jump -> next b4() @724 + b4(): + v41 = Builtin builtin=GlobalThis @737 + v42 = PropGet v41 key="globalThis" [isFreeName] @737 + v43 = Convert v42 declared={type=builtin(GlobalThis) flags=[IsExpected]} @737 + v44 = ElemGet v33, v40 @764 + v45 = Convert v44 declared={type=undefined|null|function|f2|f3|f4|f5|f6|f7|f8|f9|f10|f11|f12|f13|f14|f15|f16 flags=[IsExpected]} @764 + v46 = Integer integer=1 @773 + v47 = Call v1, -, v45, v46 calleeName="lender" @757 + PropSet v43, v47 key="answer" @737 + Jump -> next b5() @737 + b5(): + v49 = Increment v40 @737 + v50 = Convert v49 declared={type=i32 flags=[IsExpected]} @732 + Jump -> next b2(v50) @732 + b6(v51): + Jump -> next b7() @724 + b7(): + v52 = Undefined @732 + Return v52 @732 +} +function f1 name="lender" module=m0 flags=[HasExplicitName] length=2 thisType={type=anything} parameters=[{type=undefined|null|function flags=[IsExpected]}, {type=anything}] results=[{type=anything}] capabilities=[MayChangeSignature, MayBeCloned, MayBeInlined, HasOrdinaryFrame, IsNeverCalledByHost] { + b0(v0: undefined|null|function, v1: anything): + Jump -> next b1() @0 + b1(): + v2 = NewObject layout=L0 @21 + v3 = Convert v1 declared={type=number flags=[IsExpected]} @34 + v4 = Integer integer=2 @38 + v5 = Mul v3, v4 @34 + v6 = Convert v5 declared={type=number flags=[IsExpected]} @34 + PutEnvironmentField v2, v6 layout=L0 @30 + v8 = NewClosure v2 function=f17 @55 + v9 = Call v0, -, v8 calleeName="way" @51 + v10 = GetEnvironmentField v2 layout=L0 @67 + v11 = CheckInitialized v10 name="a" @67 + v12 = Integer integer=10 @72 + v13 = Add v11, v12 @67 + v14 = Convert v13 declared={type=number flags=[IsExpected]} @67 + v15 = GetEnvironmentField v2 layout=L0 @67 + v16 = CheckInitialized v15 name="a" @67 + PutEnvironmentField v2, v14 layout=L0 @67 + Return v9 @83 +} +function f2 name="returnsIt" module=m0 position=88 flags=[HasExplicitName] length=1 thisType={type=anything} parameters=[{type=anything}] results=[{type=anything}] functionTypes=[f18] capabilities=[MayChangeSignature, MayBeCloned, MayBeInlined, HasOrdinaryFrame, IsNeverCalledByHost] { + b0(v0: anything): + Jump -> next b1() @88 + b1(): + Return v0 @119 +} +function f3 name="b0" module=m0 position=124 flags=[HasExplicitName] length=1 thisType={type=anything} parameters=[{type=undefined|null|function flags=[IsExpected]}] results=[{type=anything}] functionTypes=[f19] capabilities=[MayChangeSignature, MayBeCloned, MayBeInlined, HasOrdinaryFrame, IsNeverCalledByHost] { + b0(v0: undefined|null|function): + Jump -> next b1() @124 + b1(): + v1 = Call v0 calleeName="f" @148 + v2 = Convert v1 declared={type=number flags=[IsExpected]} @148 + v3 = Integer @154 + v4 = Add v2, v3 @148 + Return v4 @148 +} +function f4 name="b1" module=m0 position=159 flags=[HasExplicitName] length=1 thisType={type=anything} parameters=[{type=undefined|null|function flags=[IsExpected]}] results=[{type=anything}] functionTypes=[f20] capabilities=[MayChangeSignature, MayBeCloned, MayBeInlined, HasOrdinaryFrame, IsNeverCalledByHost] { + b0(v0: undefined|null|function): + Jump -> next b1() @159 + b1(): + v1 = Call v0 calleeName="f" @183 + v2 = Convert v1 declared={type=number flags=[IsExpected]} @183 + v3 = Integer integer=1 @189 + v4 = Add v2, v3 @183 + Return v4 @183 +} +function f5 name="b2" module=m0 position=194 flags=[HasExplicitName] length=1 thisType={type=anything} parameters=[{type=undefined|null|function flags=[IsExpected]}] results=[{type=anything}] functionTypes=[f21] capabilities=[MayChangeSignature, MayBeCloned, MayBeInlined, HasOrdinaryFrame, IsNeverCalledByHost] { + b0(v0: undefined|null|function): + Jump -> next b1() @194 + b1(): + v1 = Call v0 calleeName="f" @218 + v2 = Convert v1 declared={type=number flags=[IsExpected]} @218 + v3 = Integer integer=2 @224 + v4 = Add v2, v3 @218 + Return v4 @218 +} +function f6 name="b3" module=m0 position=229 flags=[HasExplicitName] length=1 thisType={type=anything} parameters=[{type=undefined|null|function flags=[IsExpected]}] results=[{type=anything}] functionTypes=[f22] capabilities=[MayChangeSignature, MayBeCloned, MayBeInlined, HasOrdinaryFrame, IsNeverCalledByHost] { + b0(v0: undefined|null|function): + Jump -> next b1() @229 + b1(): + v1 = Call v0 calleeName="f" @253 + v2 = Convert v1 declared={type=number flags=[IsExpected]} @253 + v3 = Integer integer=3 @259 + v4 = Add v2, v3 @253 + Return v4 @253 +} +function f7 name="b4" module=m0 position=264 flags=[HasExplicitName] length=1 thisType={type=anything} parameters=[{type=undefined|null|function flags=[IsExpected]}] results=[{type=anything}] functionTypes=[f23] capabilities=[MayChangeSignature, MayBeCloned, MayBeInlined, HasOrdinaryFrame, IsNeverCalledByHost] { + b0(v0: undefined|null|function): + Jump -> next b1() @264 + b1(): + v1 = Call v0 calleeName="f" @288 + v2 = Convert v1 declared={type=number flags=[IsExpected]} @288 + v3 = Integer integer=4 @294 + v4 = Add v2, v3 @288 + Return v4 @288 +} +function f8 name="b5" module=m0 position=299 flags=[HasExplicitName] length=1 thisType={type=anything} parameters=[{type=undefined|null|function flags=[IsExpected]}] results=[{type=anything}] functionTypes=[f24] capabilities=[MayChangeSignature, MayBeCloned, MayBeInlined, HasOrdinaryFrame, IsNeverCalledByHost] { + b0(v0: undefined|null|function): + Jump -> next b1() @299 + b1(): + v1 = Call v0 calleeName="f" @323 + v2 = Convert v1 declared={type=number flags=[IsExpected]} @323 + v3 = Integer integer=5 @329 + v4 = Add v2, v3 @323 + Return v4 @323 +} +function f9 name="b6" module=m0 position=334 flags=[HasExplicitName] length=1 thisType={type=anything} parameters=[{type=undefined|null|function flags=[IsExpected]}] results=[{type=anything}] functionTypes=[f25] capabilities=[MayChangeSignature, MayBeCloned, MayBeInlined, HasOrdinaryFrame, IsNeverCalledByHost] { + b0(v0: undefined|null|function): + Jump -> next b1() @334 + b1(): + v1 = Call v0 calleeName="f" @358 + v2 = Convert v1 declared={type=number flags=[IsExpected]} @358 + v3 = Integer integer=6 @364 + v4 = Add v2, v3 @358 + Return v4 @358 +} +function f10 name="b7" module=m0 position=369 flags=[HasExplicitName] length=1 thisType={type=anything} parameters=[{type=undefined|null|function flags=[IsExpected]}] results=[{type=anything}] functionTypes=[f26] capabilities=[MayChangeSignature, MayBeCloned, MayBeInlined, HasOrdinaryFrame, IsNeverCalledByHost] { + b0(v0: undefined|null|function): + Jump -> next b1() @369 + b1(): + v1 = Call v0 calleeName="f" @393 + v2 = Convert v1 declared={type=number flags=[IsExpected]} @393 + v3 = Integer integer=7 @399 + v4 = Add v2, v3 @393 + Return v4 @393 +} +function f11 name="b8" module=m0 position=404 flags=[HasExplicitName] length=1 thisType={type=anything} parameters=[{type=undefined|null|function flags=[IsExpected]}] results=[{type=anything}] functionTypes=[f27] capabilities=[MayChangeSignature, MayBeCloned, MayBeInlined, HasOrdinaryFrame, IsNeverCalledByHost] { + b0(v0: undefined|null|function): + Jump -> next b1() @404 + b1(): + v1 = Call v0 calleeName="f" @428 + v2 = Convert v1 declared={type=number flags=[IsExpected]} @428 + v3 = Integer integer=8 @434 + v4 = Add v2, v3 @428 + Return v4 @428 +} +function f12 name="b9" module=m0 position=439 flags=[HasExplicitName] length=1 thisType={type=anything} parameters=[{type=undefined|null|function flags=[IsExpected]}] results=[{type=anything}] functionTypes=[f28] capabilities=[MayChangeSignature, MayBeCloned, MayBeInlined, HasOrdinaryFrame, IsNeverCalledByHost] { + b0(v0: undefined|null|function): + Jump -> next b1() @439 + b1(): + v1 = Call v0 calleeName="f" @463 + v2 = Convert v1 declared={type=number flags=[IsExpected]} @463 + v3 = Integer integer=9 @469 + v4 = Add v2, v3 @463 + Return v4 @463 +} +function f13 name="b10" module=m0 position=474 flags=[HasExplicitName] length=1 thisType={type=anything} parameters=[{type=undefined|null|function flags=[IsExpected]}] results=[{type=anything}] functionTypes=[f29] capabilities=[MayChangeSignature, MayBeCloned, MayBeInlined, HasOrdinaryFrame, IsNeverCalledByHost] { + b0(v0: undefined|null|function): + Jump -> next b1() @474 + b1(): + v1 = Call v0 calleeName="f" @499 + v2 = Convert v1 declared={type=number flags=[IsExpected]} @499 + v3 = Integer integer=10 @505 + v4 = Add v2, v3 @499 + Return v4 @499 +} +function f14 name="b11" module=m0 position=511 flags=[HasExplicitName] length=1 thisType={type=anything} parameters=[{type=undefined|null|function flags=[IsExpected]}] results=[{type=anything}] functionTypes=[f30] capabilities=[MayChangeSignature, MayBeCloned, MayBeInlined, HasOrdinaryFrame, IsNeverCalledByHost] { + b0(v0: undefined|null|function): + Jump -> next b1() @511 + b1(): + v1 = Call v0 calleeName="f" @536 + v2 = Convert v1 declared={type=number flags=[IsExpected]} @536 + v3 = Integer integer=11 @542 + v4 = Add v2, v3 @536 + Return v4 @536 +} +function f15 name="b12" module=m0 position=548 flags=[HasExplicitName] length=1 thisType={type=anything} parameters=[{type=undefined|null|function flags=[IsExpected]}] results=[{type=anything}] functionTypes=[f31] capabilities=[MayChangeSignature, MayBeCloned, MayBeInlined, HasOrdinaryFrame, IsNeverCalledByHost] { + b0(v0: undefined|null|function): + Jump -> next b1() @548 + b1(): + v1 = Call v0 calleeName="f" @573 + v2 = Convert v1 declared={type=number flags=[IsExpected]} @573 + v3 = Integer integer=12 @579 + v4 = Add v2, v3 @573 + Return v4 @573 +} +function f16 name="b13" module=m0 position=585 flags=[HasExplicitName] length=1 thisType={type=anything} parameters=[{type=undefined|null|function flags=[IsExpected]}] results=[{type=anything}] functionTypes=[f32] capabilities=[MayChangeSignature, MayBeCloned, MayBeInlined, HasOrdinaryFrame, IsNeverCalledByHost] { + b0(v0: undefined|null|function): + Jump -> next b1() @585 + b1(): + v1 = Call v0 calleeName="f" @610 + v2 = Convert v1 declared={type=number flags=[IsExpected]} @610 + v3 = Integer integer=13 @616 + v4 = Add v2, v3 @610 + Return v4 @610 +} +function f17 module=m0 position=55 kind=Arrow captures=[{type=env L0 flags=[IsFromCompiler]}] thisType={type=anything} results=[{type=number flags=[IsExpected]}] capabilities=[MayChangeSignature, MayBeCloned, MayBeInlined, HasOrdinaryFrame, IsNeverCalledByHost] { + b0(v0: env L0): + Jump -> next b1() @55 + b1(): + v1 = GetEnvironmentField v0 layout=L0 @61 + v2 = CheckInitialized v1 name="a" @61 + v3 = ToNumeric v2 @61 + v4 = Increment v3 @61 + v5 = Convert v4 declared={type=number flags=[IsExpected]} @61 + v6 = GetEnvironmentField v0 layout=L0 @61 + v7 = CheckInitialized v6 name="a" @61 + PutEnvironmentField v0, v5 layout=L0 @61 + v9 = Convert v3 declared={type=number flags=[IsExpected]} @61 + Return v9 @61 +} +function f18 module=m0 role=Signature thisType={type=anything} results=[{type=anything}] +function f19 module=m0 role=Signature thisType={type=anything} results=[{type=anything}] functionTypes=[f18] +function f20 module=m0 role=Signature thisType={type=anything} results=[{type=anything}] functionTypes=[f18] +function f21 module=m0 role=Signature thisType={type=anything} results=[{type=anything}] functionTypes=[f18] +function f22 module=m0 role=Signature thisType={type=anything} results=[{type=anything}] functionTypes=[f18] +function f23 module=m0 role=Signature thisType={type=anything} results=[{type=anything}] functionTypes=[f18] +function f24 module=m0 role=Signature thisType={type=anything} results=[{type=anything}] functionTypes=[f18] +function f25 module=m0 role=Signature thisType={type=anything} results=[{type=anything}] functionTypes=[f18] +function f26 module=m0 role=Signature thisType={type=anything} results=[{type=anything}] functionTypes=[f18] +function f27 module=m0 role=Signature thisType={type=anything} results=[{type=anything}] functionTypes=[f18] +function f28 module=m0 role=Signature thisType={type=anything} results=[{type=anything}] functionTypes=[f18] +function f29 module=m0 role=Signature thisType={type=anything} results=[{type=anything}] functionTypes=[f18] +function f30 module=m0 role=Signature thisType={type=anything} results=[{type=anything}] functionTypes=[f18] +function f31 module=m0 role=Signature thisType={type=anything} results=[{type=anything}] functionTypes=[f18] +function f32 module=m0 role=Signature thisType={type=anything} results=[{type=anything}] functionTypes=[f18] From b77d9d42f4f09be918d355393f7a4c9e726c4d18 Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Sun, 1 Nov 2026 15:29:30 -0800 Subject: [PATCH 41/49] AOT: the recheck of guards scans for borrowed parameters only what it reads Behind "the recheck of guards reads only what its functions reach": build time under the program verifier alone. Each of the recheck's two readings still made the table of borrowed parameters from nothing, by a scan of every body of the program. Now the scan takes the bodies below the functions under recheck, and the second reading takes the first one's table. `--aot-check=verify-each-pass` and a validating build gain the second too. --- .../aot/analysis/AOTBorrowedParameters.cpp | 4 +- .../aot/analysis/AOTBorrowedParameters.h | 7 ++- .../aot/analysis/AOTGuardFacts.cpp | 9 ++- .../aot/ir/AOTEffectSummaries.cpp | 13 +++-- .../aot/ir/AOTEffectSummariesInternal.h | 6 +- .../aot/ir/AOTEffectSummariesSolver.cpp | 56 ++++++++++--------- 6 files changed, 58 insertions(+), 37 deletions(-) diff --git a/Source/JavaScriptCore/aot/analysis/AOTBorrowedParameters.cpp b/Source/JavaScriptCore/aot/analysis/AOTBorrowedParameters.cpp index 05dcce5e2def8..481b3347dd3fd 100644 --- a/Source/JavaScriptCore/aot/analysis/AOTBorrowedParameters.cpp +++ b/Source/JavaScriptCore/aot/analysis/AOTBorrowedParameters.cpp @@ -848,7 +848,7 @@ void BorrowingBodies::didLower(std::span functions) } } -BorrowedParameters computeBorrowedParameters(const Program& program, BuildContext& build, BorrowingBodies& kept, std::span hasChanged, std::span recordLooksDifferent) +BorrowedParameters computeBorrowedParameters(const Program& program, BuildContext& build, BorrowingBodies& kept, std::span hasChanged, std::span recordLooksDifferent, const BitVector* bodiesToScan) { const ParallelLoops& loops = build.parallelLoops(); unsigned numberOfFunctions = program.functions.size(); @@ -860,7 +860,7 @@ BorrowedParameters computeBorrowedParameters(const Program& program, BuildContex auto scan = [&](unsigned index) { const Function& function = program.functions[index]; - if (!build.isEnabled(FlowResultKind::OperandSummaries, index) || !mayBorrow(program, function, opcodesThatBorrowNothing)) + if ((bodiesToScan && !bodiesToScan->quickGet(index)) || !build.isEnabled(FlowResultKind::OperandSummaries, index) || !mayBorrow(program, function, opcodesThatBorrowNothing)) return BorrowingBody { }; return scanBorrowingBody(program, function, &kept.m_targetLists); }; diff --git a/Source/JavaScriptCore/aot/analysis/AOTBorrowedParameters.h b/Source/JavaScriptCore/aot/analysis/AOTBorrowedParameters.h index c7e4d3564a679..8bda5e7eef0c4 100644 --- a/Source/JavaScriptCore/aot/analysis/AOTBorrowedParameters.h +++ b/Source/JavaScriptCore/aot/analysis/AOTBorrowedParameters.h @@ -30,7 +30,8 @@ class BorrowingBodies; class Program; JS_EXPORT_PRIVATE BorrowedParameters computeBorrowedParameters(const Program&, BuildContext&); -BorrowedParameters computeBorrowedParameters(const Program&, BuildContext&, BorrowingBodies& kept, std::span hasChanged, std::span recordLooksDifferent); +// A function whose body is not among those to scan borrows nothing. +BorrowedParameters computeBorrowedParameters(const Program&, BuildContext&, BorrowingBodies& kept, std::span hasChanged, std::span recordLooksDifferent, const BitVector* bodiesToScan = nullptr); inline constexpr unsigned maxBorrowedParameters = 21; @@ -60,7 +61,7 @@ class BorrowedParameters { friend bool operator==(const BorrowedParameters&, const BorrowedParameters&) = default; private: - friend BorrowedParameters computeBorrowedParameters(const Program&, BuildContext&, BorrowingBodies&, std::span, std::span); + friend BorrowedParameters computeBorrowedParameters(const Program&, BuildContext&, BorrowingBodies&, std::span, std::span, const BitVector*); bool hasBit(FunctionID function, unsigned row, uint32_t entryParameter) const { return entryParameter < maxBorrowedParameters && wordOf(function) >> (row * maxBorrowedParameters + entryParameter) & 1; } @@ -137,7 +138,7 @@ class BorrowingBodies { bool wasLoweredSinceScanned(FunctionID id) const { return id.index() < m_wasLoweredSinceScanned.size() && m_wasLoweredSinceScanned[id.index()]; } private: - friend BorrowedParameters computeBorrowedParameters(const Program&, BuildContext&, BorrowingBodies&, std::span, std::span); + friend BorrowedParameters computeBorrowedParameters(const Program&, BuildContext&, BorrowingBodies&, std::span, std::span, const BitVector*); Vector m_bodies; HandedOnTargetLists m_targetLists; diff --git a/Source/JavaScriptCore/aot/analysis/AOTGuardFacts.cpp b/Source/JavaScriptCore/aot/analysis/AOTGuardFacts.cpp index 689996136b5df..5b92302079948 100644 --- a/Source/JavaScriptCore/aot/analysis/AOTGuardFacts.cpp +++ b/Source/JavaScriptCore/aot/analysis/AOTGuardFacts.cpp @@ -1390,12 +1390,17 @@ bool guardProvesSlotFilled(FunctionAnalyses& analyses, const Value& slotOperatio return false; const Program& program = analyses.program(); - bool testOfCellAnswers = isAnsweredByTestOfCell(program, slotOperation); + std::optional testOfCellAnswers; + auto askTestOfCell = [&] { + if (!testOfCellAnswers) + testOfCellAnswers = isAnsweredByTestOfCell(program, slotOperation); + return *testOfCellAnswers; + }; ValueID object = analyses.identityOf(slotOperation.objectOperand()); bool isProven = std::ranges::any_of(analyses.guardFacts(guard).facts, [&](const GuardFact& fact) { if (fact.subject != object) return false; - return provesSlotFilled(program, analyses.function(), fact, slotOperation.key()) || (testOfCellAnswers && fact.proves(Proof::BirthFieldsFilled)) || isFilledBehindEveryAllocation(analyses, fact, slotOperation); + return provesSlotFilled(program, analyses.function(), fact, slotOperation.key()) || (fact.proves(Proof::BirthFieldsFilled) && askTestOfCell()) || isFilledBehindEveryAllocation(analyses, fact, slotOperation); }); RELEASE_ASSERT(mayBeProven || !isProven); return isProven; diff --git a/Source/JavaScriptCore/aot/ir/AOTEffectSummaries.cpp b/Source/JavaScriptCore/aot/ir/AOTEffectSummaries.cpp index b7ac13fa64af4..1be7b1ed6aadd 100644 --- a/Source/JavaScriptCore/aot/ir/AOTEffectSummaries.cpp +++ b/Source/JavaScriptCore/aot/ir/AOTEffectSummaries.cpp @@ -782,18 +782,22 @@ EffectSummaries computeEffectSummaries(const Program& program, const FlowResults return summaries; } -static EffectSummaries computeEffectSummariesByOperandTypes(const Program& program, const FlowResults& flow, BuildContext& build, const EffectSummaries* rowsOfBodies, std::span roots, BitVector& bodiesBelowRoots) +static EffectSummaries computeEffectSummariesByOperandTypes(const Program& program, const FlowResults& flow, BuildContext& build, const EffectSummaries* rowsOfBodies, BorrowedParameters& borrowed, std::span roots, BitVector& bodiesBelowRoots) { BodySummaries nothingKept; EffectSummaries summaries; EffectSummarizer summarizer(program, flow, build, nothingKept, EffectSummarizer::Witnesses::Found); summarizer.setIsOnlyCheck(); summarizer.readCalleesByOperandTypes(); - if (rowsOfBodies) + if (rowsOfBodies) { summarizer.takeRowsOfBodiesFrom(*rowsOfBodies); + summarizer.takeBorrowedParametersFrom(borrowed); + } if (!roots.empty()) summarizer.readOnlyBodiesBelow(roots, bodiesBelowRoots); summarizer.update(summaries); + if (!rowsOfBodies) + borrowed = nothingKept.borrowedParameters(); return summaries; } @@ -802,8 +806,9 @@ EffectSummaries computeEffectSummariesByOperandTypes(const Program& program, con // The seed says nothing of a function that was made after flow analysis, and a call of one may stand where operations of the caller's own body stood. // So a body that hands a parameter on to it seems to use the parameter in every way. The second reading knows what the first found in that function's body. BitVector bodiesBelowRoots; - EffectSummaries first = computeEffectSummariesByOperandTypes(program, flow, build, nullptr, roots, bodiesBelowRoots); - return computeEffectSummariesByOperandTypes(program, flow, build, &first, roots, bodiesBelowRoots); + BorrowedParameters borrowed; + EffectSummaries first = computeEffectSummariesByOperandTypes(program, flow, build, nullptr, borrowed, roots, bodiesBelowRoots); + return computeEffectSummariesByOperandTypes(program, flow, build, &first, borrowed, roots, bodiesBelowRoots); } void updateEffectSummaries(const Program& program, const FlowResults& flow, BuildContext& build, BodySummaries& kept, EffectSummaries& summaries, BodyReferences* references) diff --git a/Source/JavaScriptCore/aot/ir/AOTEffectSummariesInternal.h b/Source/JavaScriptCore/aot/ir/AOTEffectSummariesInternal.h index 796c058511b22..4496ee54f60dc 100644 --- a/Source/JavaScriptCore/aot/ir/AOTEffectSummariesInternal.h +++ b/Source/JavaScriptCore/aot/ir/AOTEffectSummariesInternal.h @@ -214,7 +214,7 @@ class BodySummaries { static constexpr unsigned functionsInChunk = 256; void countLoweredBodiesWithNewStorageAsChanged(); - BorrowedParameters computeBorrowedParametersFromLatestTable(const Program&, BuildContext&, std::span stampsOfRecords, std::span recordLooksDifferent); + BorrowedParameters computeBorrowedParametersFromLatestTable(const Program&, BuildContext&, std::span stampsOfRecords, std::span recordLooksDifferent, const BitVector* bodiesToScan = nullptr); void didTouch(FunctionID id) { if (id.index() < m_wasTouchedSinceLatestTable.size()) @@ -291,6 +291,8 @@ class EffectSummarizer { void readCalleesByOperandTypes() { m_calleesAreRead = CalleesAreRead::ByOperandTypes; } // Only from scratch. Where these say that a body uses a parameter in fewer ways than the seed does, a caller that hands it an operand reads what the body does. void takeRowsOfBodiesFrom(const EffectSummaries& summaries) { m_rowsOfBodies = &summaries; } + // The table of this very program. + void takeBorrowedParametersFrom(const BorrowedParameters& table) { m_borrowedParametersOfProgram = &table; } // Only from scratch. Reads the bodies that the roots can reach by the functions that each body names, and takes every other function for unknown code. // `bodies` is empty, and is filled with what was found, or comes from an earlier reading. void readOnlyBodiesBelow(std::span roots, BitVector& bodies) @@ -305,6 +307,7 @@ class EffectSummarizer { private: void findRecordsThatLookDifferent(); + void findBodiesToRead(); void updateSeed(BorrowedParameters&&); Vector findReasonsToSummarize() const; void summarizeBodies(); @@ -356,6 +359,7 @@ class EffectSummarizer { bool m_isOnlyCheck { false }; CalleesAreRead m_calleesAreRead { CalleesAreRead::AsTheyAre }; const EffectSummaries* m_rowsOfBodies { nullptr }; + const BorrowedParameters* m_borrowedParametersOfProgram { nullptr }; std::span m_rootsOfBodiesToRead; BitVector* m_bodiesToRead { nullptr }; BodyReferences* m_references { nullptr }; diff --git a/Source/JavaScriptCore/aot/ir/AOTEffectSummariesSolver.cpp b/Source/JavaScriptCore/aot/ir/AOTEffectSummariesSolver.cpp index 0df7fd80bcbee..e4c0c5104a742 100644 --- a/Source/JavaScriptCore/aot/ir/AOTEffectSummariesSolver.cpp +++ b/Source/JavaScriptCore/aot/ir/AOTEffectSummariesSolver.cpp @@ -233,7 +233,8 @@ void EffectSummarizer::bringSeedUpToDate(EffectSummaries& summaries) summaries.forgetAnswersForCalleeLists(); m_kept.countLoweredBodiesWithNewStorageAsChanged(); findRecordsThatLookDifferent(); - BorrowedParameters borrowed = m_kept.computeBorrowedParametersFromLatestTable(m_program, m_build, m_kept.m_stampsOfRecords.span(), m_recordLooksDifferent.span()); + findBodiesToRead(); + BorrowedParameters borrowed = m_borrowedParametersOfProgram ? *m_borrowedParametersOfProgram : m_kept.computeBorrowedParametersFromLatestTable(m_program, m_build, m_kept.m_stampsOfRecords.span(), m_recordLooksDifferent.span(), m_bodiesToRead); m_kept.m_wasTouchedSinceLatestTable.clear(); m_kept.m_stampsOfRecordsAtLatestTable.clear(); times.didFinish("computeBorrowedParameters"_s); @@ -365,11 +366,12 @@ BorrowedParameters BodySummaries::computeBorrowedParametersOfChangedBodies(const return table; } -BorrowedParameters BodySummaries::computeBorrowedParametersFromLatestTable(const Program& program, BuildContext& build, std::span stampsOfRecords, std::span recordLooksDifferent) +BorrowedParameters BodySummaries::computeBorrowedParametersFromLatestTable(const Program& program, BuildContext& build, std::span stampsOfRecords, std::span recordLooksDifferent, const BitVector* bodiesToScan) { if (m_stampsOfRecordsAtLatestTable.isEmpty()) - return computeBorrowedParameters(program, build, m_borrowingBodies, m_isChangedForTable.span(), recordLooksDifferent); + return computeBorrowedParameters(program, build, m_borrowingBodies, m_isChangedForTable.span(), recordLooksDifferent, bodiesToScan); + RELEASE_ASSERT(!bodiesToScan); unsigned numberOfKeptBodies = m_stampsOfRecordsAtLatestTable.size(); unsigned numberOfSummarizedFunctions = m_isChangedForTable.size(); RELEASE_ASSERT(numberOfSummarizedFunctions <= numberOfKeptBodies && numberOfKeptBodies <= stampsOfRecords.size() && m_wasTouchedSinceLatestTable.size() == numberOfKeptBodies); @@ -384,6 +386,32 @@ BorrowedParameters BodySummaries::computeBorrowedParametersFromLatestTable(const return computeBorrowedParameters(program, build, m_borrowingBodies, hasChangedSince.span(), recordLooksDifferentSince.span()); } +void EffectSummarizer::findBodiesToRead() +{ + if (!m_bodiesToRead || !m_bodiesToRead->isEmpty()) + return; + RELEASE_ASSERT(!m_numberOfFunctionsBefore); + m_bodiesToRead->ensureSize(m_program.functions.size()); + Vector frontier; + auto add = [&](FunctionID function) { + if (!m_bodiesToRead->quickSet(function.index())) + frontier.append(function.index()); + }; + for (FunctionID root : m_rootsOfBodiesToRead) + add(root); + while (!frontier.isEmpty()) { + Vector> named(frontier.size()); + forEachIndexInParallel(m_build.parallelLoops(), frontier.size(), [&](unsigned position) { + named[position] = functionsNamedIn(m_program, m_program.functions[frontier[position]]); + }); + frontier.shrink(0); + for (const Vector& functions : named) { + for (FunctionID function : functions) + add(function); + } + } +} + void EffectSummarizer::findRecordsThatLookDifferent() { unsigned numberOfFunctions = m_program.functions.size(); @@ -603,28 +631,6 @@ void EffectSummarizer::summarizeBodies() }; const EffectSummaries& seed = m_kept.m_seed.read(); - if (m_bodiesToRead && m_bodiesToRead->isEmpty()) { - RELEASE_ASSERT(!m_numberOfFunctionsBefore); - m_bodiesToRead->ensureSize(numberOfFunctions); - Vector frontier; - auto add = [&](FunctionID function) { - if (!m_bodiesToRead->quickSet(function.index())) - frontier.append(function.index()); - }; - for (FunctionID root : m_rootsOfBodiesToRead) - add(root); - while (!frontier.isEmpty()) { - Vector> named(frontier.size()); - forEachIndexInParallel(loops, frontier.size(), [&](unsigned position) { - named[position] = functionsNamedIn(m_program, m_program.functions[frontier[position]]); - }); - frontier.shrink(0); - for (const Vector& functions : named) { - for (FunctionID function : functions) - add(function); - } - } - } // The seed may be lent to an analysis that runs meanwhile, so the rows that bodies replace are rows of a copy. std::optional seedWithRowsOfBodies; auto summarizeBodiesOf = [&](Turn turn) { From ab6866b6441ffb4a7287c29c1243f578e0e50ca2 Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Sun, 1 Nov 2026 15:29:30 -0800 Subject: [PATCH 42/49] AOT: an import of a file of an image below walks its mocks once `vi.doMock("./k.js", async () => { .. await .. }); vi.doMock("./m.js", async () => { .. }); await import("./a.js")`, where a.js imports k and m, in a test file behind another test file whose mock's factory has run. `bun test`: k's factory begins, k's ends, m's begins, m's ends. `bun test --aot` over two or three layers: k begins, m begins, m ends, k ends, without an error. For a file of an image below, import() walked the mocks twice, and nobody read the second walk's answer. --- Source/JavaScriptCore/aot/runtime/AOTModules.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Source/JavaScriptCore/aot/runtime/AOTModules.cpp b/Source/JavaScriptCore/aot/runtime/AOTModules.cpp index 2461f0847f6aa..7787378bb9387 100644 --- a/Source/JavaScriptCore/aot/runtime/AOTModules.cpp +++ b/Source/JavaScriptCore/aot/runtime/AOTModules.cpp @@ -4169,7 +4169,9 @@ JSPromise* Modules::import(ModuleID module, ModuleID referrer, JSValue options) return; if (!modules->m_modulesWhoseFactoryRuns.get(file->index()) && !modules->m_importsMadeByOwnFactory.contains(*file)) modules->dropErrorOfMockKeptByLoader(*file); - modules->runMocksBelow(*file); + // The factories of one load take turns: behind one that has not settled, the loader's fetch walks on. + if (!walkStopped) + walkStopped = modules->runMocksBelow(*file); }); RETURN_IF_EXCEPTION(scope, rejectedPromiseWithPendingException(realm, scope)); } From 76c88ac74b934c46cbb1b3a067b7ed23ec1b7a51 Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Sun, 1 Nov 2026 15:29:30 -0800 Subject: [PATCH 43/49] AOT: a copy of a parameter that holds -2 is not taken for 0 A wrong answer of an executable at default flags, without an error. It came with oven-sh/WebKit#982; Bun's main is right. const T = []; function f(a, b) { let c = b; if (!b) { if (c !== b) T.push(1); } b == c; b?.[0]; if (a == b) { b -= 1; c--; if (typeof c === "object" && c !== null && "id" in c) T.push(2); } T.push(b, c); } f(1, 0); f(-2, -2); f(1, ""); f(null, undefined); console.log(T.map(String).join(" ")); `bun run` prints `0 0 -3 -3 NaN NaN`; compiled code printed `0 0 -3 -1 NaN NaN`. While type tests are placed, a condition's first proof stands for all of its proofs, and a cast took its claim from such a stand-in: the type inside `if (!b)`, `undefined|0`. A claim is taken only from a value that stood before the run of the pass. Two IR tests, one with a planted fault. Goes with Bun's half. --- .../aot/passes/AOTPlaceTypeTests.cpp | 16 ++- .../claims-of-fresh-values-sabotaged.ir | 125 ++++++++++++++++++ .../tests/pipeline/claims-of-fresh-values.ir | 125 ++++++++++++++++++ 3 files changed, 262 insertions(+), 4 deletions(-) create mode 100644 Source/JavaScriptCore/aot/tests/pipeline/claims-of-fresh-values-sabotaged.ir create mode 100644 Source/JavaScriptCore/aot/tests/pipeline/claims-of-fresh-values.ir diff --git a/Source/JavaScriptCore/aot/passes/AOTPlaceTypeTests.cpp b/Source/JavaScriptCore/aot/passes/AOTPlaceTypeTests.cpp index 83f1364f60f46..0facad0ba1d99 100644 --- a/Source/JavaScriptCore/aot/passes/AOTPlaceTypeTests.cpp +++ b/Source/JavaScriptCore/aot/passes/AOTPlaceTypeTests.cpp @@ -3147,6 +3147,7 @@ struct TestPlacer { Vector changedValues { }; Vector, 4> ownTypesOfRepresentatives { }; Vector castsToTypesOfOperands { }; + size_t numberOfValuesBeforeTests { 0 }; PassOutcome run() { @@ -3155,7 +3156,7 @@ struct TestPlacer { findStretchesThatGuessLayouts(); auto blocksBeforeSplitting = function.blocks; - size_t numberOfValuesBeforeSplitting = function.values.size(); + numberOfValuesBeforeTests = function.values.size(); // A proof adopted on an edge that also carries tests has to become a parameter of the block the tests stand in. splitTestedEdges(); @@ -3170,7 +3171,7 @@ struct TestPlacer { if (!numberOfGuardedStretches()) { // Left split, the function would be simplified, planned, split and rejected again in every round of the fixpoint. function.replaceBlocks(WTF::move(blocksBeforeSplitting)); - function.editAllValues().shrink(numberOfValuesBeforeSplitting); + function.editAllValues().shrink(numberOfValuesBeforeTests); analyses.didChangeControlFlow(); return PassOutcome::Unchanged; } @@ -4005,7 +4006,7 @@ struct TestPlacer { guards.appendIfNotContains(proof); return; } - if (std::optional castType = findTypeOfOperand(test, *prerequisite.narrowedOperand, proof)) { + if (std::optional castType = findTypeOfOperand(test, *prerequisite.narrowedOperand, proof); castType && isTypedAsBeforeTests(test.operand(*prerequisite.narrowedOperand))) { proof = emit(makeAssumeType(proof, emit(IRBuilder::make(Opcode::Specified)), Proven::behindFact(claimOfCast(*castType, test.operand(*prerequisite.narrowedOperand))))); castsToTypesOfOperands.append(proof); } @@ -4056,6 +4057,11 @@ struct TestPlacer { return types.intern(both); } + bool isTypedAsBeforeTests(ValueID value) const + { + return value.index() < numberOfValuesBeforeTests || context.build.isSabotaged(Sabotage::TestsKeepTheTypesTheyWereMadeWith); + } + TypeID claimOfCast(TypeID typeWithStandIn, ValueID operand) const { return context.build.isSabotaged(Sabotage::TestsKeepTheTypesTheyWereMadeWith) ? typeWithStandIn : function.at(operand).type; @@ -4198,6 +4204,8 @@ struct TestPlacer { // One value stands for everything that has the identity of the tested value. The site was planned with the type of its own operand, which a branch or a cast may have narrowed in another way than the test. ValueID narrowToOperand(ValueID passed, ValueID operand, ProgramPoint point) { + if (!isTypedAsBeforeTests(operand)) + return passed; Type passedType = types.at(function.at(passed).type); Type operandType = types.at(function.at(operand).type); if (passedType.isSubsetOf(operandType, context.program)) @@ -4642,7 +4650,7 @@ struct TestPlacer { continue; } // What a value defined behind the edge says holds from its definition on. - if (!analyses.dominates(current, pointOfTest) || !isReachedByCastIn(target, use)) + if (!analyses.dominates(current, pointOfTest) || !isReachedByCastIn(target, use) || !isTypedAsBeforeTests(current)) continue; // The type of the value may say what no test finds out, such as what flow proved about a class. if (!narrowing.castType) { diff --git a/Source/JavaScriptCore/aot/tests/pipeline/claims-of-fresh-values-sabotaged.ir b/Source/JavaScriptCore/aot/tests/pipeline/claims-of-fresh-values-sabotaged.ir new file mode 100644 index 0000000000000..647ce9d545db5 --- /dev/null +++ b/Source/JavaScriptCore/aot/tests/pipeline/claims-of-fresh-values-sabotaged.ir @@ -0,0 +1,125 @@ +# FORM: AsRead +# OPTIONS: sabotage=TestsKeepTheTypesTheyWereMadeWith +# RUN: pipeline PlaceTypeTests + +module m0 path="main.js" displayPath="main.js" body=f0 +entry module=m0 + +function f0 name="main" kind=Module results=[{type=anything}] { + v0 = NewClosure function=f1 + v1 = Integer integer=1 + v2 = Integer + v3 = Call v0, -, v1, v2 + v4 = Integer integer=-2 + v5 = Call v0, -, v4, v4 + v6 = String string="" + v7 = Call v0, -, v1, v6 + v8 = Null + v9 = Undefined + v10 = Call v0, -, v8, v9 + Return v10 +} + +function f1 name="f" length=2 parameters=[{type=null|number flags=[IsExpected]}, {type=undefined|null|number|string|Array flags=[IsExpected]}] results=[{type=anything}] { + b0(v0: null|number, v1: undefined|null|number|string|Array): + Jump -> next b1() + b1(): + v2 = Convert v1 declared={type=undefined|null|number|object flags=[IsExpected]} + v3 = ToBoolean v1 + Branch v3 -> taken b2(| 0), notTaken b3(| 0) + b2(v4): + Jump -> next b9(v4, v2) + b3(v5): + Jump -> next b4() + b4(): + v6 = StrictEq v2, v5 + Branch v6 -> taken b5(| 0, 1), notTaken b6(| 0, 1) + b5(v7, v8): + Jump -> next b8(v8, v7) + b6(v9, v10): + Jump -> next b7() + b7(): + v11 = Builtin builtin=GlobalThis + v12 = PropGet v11 key="sink" [isFreeName] + v13 = Undefined + v14 = Undefined + v15 = Integer integer=1 + v16 = Call v12, -, v15 + Jump -> next b8(v10, v9) + b8(v17, v18): + Jump -> next b9(v17, v18) + b9(v19, v20): + v21 = Eq v19, v20 + v22 = TypeTest v19 testedType=undefined|null + Branch v22 -> taken b10(| 0), notTaken b12(| 0) + b10(v23): + Jump -> next b11() + b11(): + v24 = Undefined + Jump -> next b13(v24, v23) + b12(v25): + v26 = Integer + v27 = ElemGet v25, v26 + Jump -> next b13(v27, v25) + b13(v28, v29): + v30 = Eq v0, v29 + Branch v30 -> taken b14(| 0, 1), notTaken b28(| 0, 1) + b14(v31, v32): + Jump -> next b15() + b15(): + v33 = Integer integer=1 + v34 = Sub v32, v33 + v35 = Convert v34 declared={type=undefined|null|number|string|Array flags=[IsExpected]} + v36 = Decrement v20 + v37 = Convert v36 declared={type=undefined|null|number|object flags=[IsExpected]} + v38 = TypeTest v37 testedType=function + Branch v38 -> taken b16(| 0), notTaken b17(| 0) + b16(v39): + Jump -> next b27(v39) + b17(v40): + v41 = TypeTest v40 testedType=null|object + Branch v41 -> taken b18(| 0), notTaken b26(| 0) + b18(v42): + Jump -> next b19() + b19(): + v43 = TypeTest v42 testedType=null + Branch v43 -> taken b20(| 0), notTaken b21(| 0) + b20(v44): + Jump -> next b27(v44) + b21(v45): + Jump -> next b22() + b22(): + v46 = PropHas v45 key="id" + v47 = ToBoolean v46 + Branch v47 -> taken b23(| 0), notTaken b25(| 0) + b23(v48): + Jump -> next b24() + b24(): + v49 = Builtin builtin=GlobalThis + v50 = PropGet v49 key="sink" [isFreeName] + v51 = Undefined + v52 = Undefined + v53 = Integer integer=2 + v54 = Call v50, -, v53 + Jump -> next b27(v45) + b25(v55): + Jump -> next b27(v45) + b26(v56): + Jump -> next b27(v56) + b27(v57): + Jump -> next b29(v35, v57) + b28(v58, v59): + Jump -> next b29(v59, v20) + b29(v60, v61): + v62 = Builtin builtin=GlobalThis + v63 = PropGet v62 key="sink" [isFreeName] + v64 = Undefined + v65 = Undefined + v66 = Call v63, -, v60, v61 + v67 = Undefined + Return v67 +} + +# CHECK-LABEL: name="f" +# CHECK: : 0 = AssumeType +# CHECK: : -1 = Decrement diff --git a/Source/JavaScriptCore/aot/tests/pipeline/claims-of-fresh-values.ir b/Source/JavaScriptCore/aot/tests/pipeline/claims-of-fresh-values.ir new file mode 100644 index 0000000000000..35e60bb20b6d5 --- /dev/null +++ b/Source/JavaScriptCore/aot/tests/pipeline/claims-of-fresh-values.ir @@ -0,0 +1,125 @@ +# FORM: AsRead +# RUN: pipeline PlaceTypeTests + +module m0 path="main.js" displayPath="main.js" body=f0 +entry module=m0 + +function f0 name="main" kind=Module results=[{type=anything}] { + v0 = NewClosure function=f1 + v1 = Integer integer=1 + v2 = Integer + v3 = Call v0, -, v1, v2 + v4 = Integer integer=-2 + v5 = Call v0, -, v4, v4 + v6 = String string="" + v7 = Call v0, -, v1, v6 + v8 = Null + v9 = Undefined + v10 = Call v0, -, v8, v9 + Return v10 +} + +function f1 name="f" length=2 parameters=[{type=null|number flags=[IsExpected]}, {type=undefined|null|number|string|Array flags=[IsExpected]}] results=[{type=anything}] { + b0(v0: null|number, v1: undefined|null|number|string|Array): + Jump -> next b1() + b1(): + v2 = Convert v1 declared={type=undefined|null|number|object flags=[IsExpected]} + v3 = ToBoolean v1 + Branch v3 -> taken b2(| 0), notTaken b3(| 0) + b2(v4): + Jump -> next b9(v4, v2) + b3(v5): + Jump -> next b4() + b4(): + v6 = StrictEq v2, v5 + Branch v6 -> taken b5(| 0, 1), notTaken b6(| 0, 1) + b5(v7, v8): + Jump -> next b8(v8, v7) + b6(v9, v10): + Jump -> next b7() + b7(): + v11 = Builtin builtin=GlobalThis + v12 = PropGet v11 key="sink" [isFreeName] + v13 = Undefined + v14 = Undefined + v15 = Integer integer=1 + v16 = Call v12, -, v15 + Jump -> next b8(v10, v9) + b8(v17, v18): + Jump -> next b9(v17, v18) + b9(v19, v20): + v21 = Eq v19, v20 + v22 = TypeTest v19 testedType=undefined|null + Branch v22 -> taken b10(| 0), notTaken b12(| 0) + b10(v23): + Jump -> next b11() + b11(): + v24 = Undefined + Jump -> next b13(v24, v23) + b12(v25): + v26 = Integer + v27 = ElemGet v25, v26 + Jump -> next b13(v27, v25) + b13(v28, v29): + v30 = Eq v0, v29 + Branch v30 -> taken b14(| 0, 1), notTaken b28(| 0, 1) + b14(v31, v32): + Jump -> next b15() + b15(): + v33 = Integer integer=1 + v34 = Sub v32, v33 + v35 = Convert v34 declared={type=undefined|null|number|string|Array flags=[IsExpected]} + v36 = Decrement v20 + v37 = Convert v36 declared={type=undefined|null|number|object flags=[IsExpected]} + v38 = TypeTest v37 testedType=function + Branch v38 -> taken b16(| 0), notTaken b17(| 0) + b16(v39): + Jump -> next b27(v39) + b17(v40): + v41 = TypeTest v40 testedType=null|object + Branch v41 -> taken b18(| 0), notTaken b26(| 0) + b18(v42): + Jump -> next b19() + b19(): + v43 = TypeTest v42 testedType=null + Branch v43 -> taken b20(| 0), notTaken b21(| 0) + b20(v44): + Jump -> next b27(v44) + b21(v45): + Jump -> next b22() + b22(): + v46 = PropHas v45 key="id" + v47 = ToBoolean v46 + Branch v47 -> taken b23(| 0), notTaken b25(| 0) + b23(v48): + Jump -> next b24() + b24(): + v49 = Builtin builtin=GlobalThis + v50 = PropGet v49 key="sink" [isFreeName] + v51 = Undefined + v52 = Undefined + v53 = Integer integer=2 + v54 = Call v50, -, v53 + Jump -> next b27(v45) + b25(v55): + Jump -> next b27(v45) + b26(v56): + Jump -> next b27(v56) + b27(v57): + Jump -> next b29(v35, v57) + b28(v58, v59): + Jump -> next b29(v59, v20) + b29(v60, v61): + v62 = Builtin builtin=GlobalThis + v63 = PropGet v62 key="sink" [isFreeName] + v64 = Undefined + v65 = Undefined + v66 = Call v63, -, v60, v61 + v67 = Undefined + Return v67 +} + +# CHECK-LABEL: name="f" +# CHECK-NOT: : 0 = AssumeType +# CHECK-NOT: : -1 = Decrement +# CHECK: = Decrement From 9a5d50774eb58fc7e20cd8ef44e771c34da98f78 Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Sun, 1 Nov 2026 15:29:30 -0800 Subject: [PATCH 44/49] AOT: what is asked of the objects of every escaped class is asked once Build time, quadratic in a made-up shape and next to nothing on a real program. `class A0 {} class B0 extends A0 {} globalThis["a" + 0] = B0`, n times: every class that is out of sight has the objects of every such class among its own, and one step of the flow analysis read every member of every layout's set: n layouts by n objects. Now a layout keeps its own objects and two bits, and what is asked of every member is asked of each shared set once. The answers are joins, so the order can't move them. Goes with Bun's half. --- .../aot/ir/schema/statistics.def | 2 +- .../valueflow/AOTValueFlowResultsFields.cpp | 55 ++++++++++++++++--- .../AOTValueFlowResultsFieldsInternal.h | 13 +++++ .../valueflow/AOTValueFlowResultsStores.cpp | 15 +++-- 4 files changed, 71 insertions(+), 14 deletions(-) diff --git a/Source/JavaScriptCore/aot/ir/schema/statistics.def b/Source/JavaScriptCore/aot/ir/schema/statistics.def index 43595a00b1ae6..eb2d54da57a70 100644 --- a/Source/JavaScriptCore/aot/ir/schema/statistics.def +++ b/Source/JavaScriptCore/aot/ir/schema/statistics.def @@ -31,7 +31,7 @@ enum StatisticID { FunctionsTypedFromNever FunctionsTypedFromChanges FunctionsTypedAsBefore ValuesTypedFromChanges FunctionPassRunsSkipped EffectsComputed EffectsKept - HandedOnReferencesRead OperandsSummarizedForFrames FunctionsBelowEntrancesInCyclesVisited ValuesReadForClassesBelowCopies MembersReadForTypesOfCopies + HandedOnReferencesRead OperandsSummarizedForFrames FunctionsBelowEntrancesInCyclesVisited ValuesReadForClassesBelowCopies MembersReadForTypesOfCopies ObjectsReadForActorsOpenOnLayouts BlocksReadForDefinitionsThroughTests BlocksVisitedForCyclesAvoidingBlocks BlocksReadForLoopsInsideLoops StackChecksCoveredByCallers StackChecksDeferredToCallee TailRecursionsEliminated ReturnCallsDemoted ResultsBoxedForTailCalls SecondaryEntries ParametersLetGo StretchesGuarded TypeTestsPlaced TypeTestsTypedByOtherOperands StretchesNotClonable GuardsEliminated QuestionsAboutWhoEndsGuards UnionsSplit LoopParametersTestedInFrontOfLoops FailedTestsReadForSubjects diff --git a/Source/JavaScriptCore/aot/valueflow/AOTValueFlowResultsFields.cpp b/Source/JavaScriptCore/aot/valueflow/AOTValueFlowResultsFields.cpp index 28dfdf886ce15..22dda7fcc4d11 100644 --- a/Source/JavaScriptCore/aot/valueflow/AOTValueFlowResultsFields.cpp +++ b/Source/JavaScriptCore/aot/valueflow/AOTValueFlowResultsFields.cpp @@ -1091,21 +1091,48 @@ struct FieldResults { // To a field that holds a plain value, a spread is a store of an unknown value. OptionSet actorsOpenOnFieldsOfObjectsWith(LayoutID id) const { return stores.getsCopiedProperties(id) ? actorsOpenOnObjectsWith(id) - Actor::KeysNobodyKnows : actorsOpenOnObjectsWith(id); } - OptionSet findActorsOpenOnObjectsWith(LayoutID id, std::span answers) const + // What all members of a set say together. + struct AnswersAboutObjects : AnswersAboutObject { + bool isHeldByNativeCode { false }; + uint32_t numberOfObjectsRead { 0 }; + + void add(const AnswersAboutObjects& other) + { + open.add(other.open); + isMadeByRowThatNamesLayoutOrByOperation &= other.isMadeByRowThatNamesLayoutOrByOperation; + isHeldByNativeCode |= other.isHeldByNativeCode; + } + }; + + AnswersAboutObjects answersAboutMembersOf(ValueSetID objects, std::span answers) const { - constexpr OptionSet makers { Actor::Creation, Actor::Table, Actor::NativeCode }; - ValueSetID objects = layoutsOfObjects.objectsWith(id); RELEASE_ASSERT(!ValueSets::isComplement(objects)); - AnswersAboutObject all { { }, true }; + AnswersAboutObjects all { { { }, true } }; sets.forEachMember(objects, [&](ValueFlowObjectID object) { all.open.add(answers[object.index()].open); all.isMadeByRowThatNamesLayoutOrByOperation &= answers[object.index()].isMadeByRowThatNamesLayoutOrByOperation; + ++all.numberOfObjectsRead; }); + all.isHeldByNativeCode = sets.meet(objects, inputs.solver().possibleValuesOf(inputs.bodies().nodes().ofHost(ValueFlowHostNode::HeldByNativeCode))); + return all; + } + + OptionSet findActorsOpenOnObjectsWith(LayoutID id, std::span answers, const AnswersAboutObjects& aboutObjectsOfEveryEscapedClass, const AnswersAboutObjects& aboutObjectsOfEveryLostClass, uint32_t& numberOfObjectsRead) const + { + constexpr OptionSet makers { Actor::Creation, Actor::Table, Actor::NativeCode }; + ValueSetID objects = layoutsOfObjects.objectsWith(id); + LayoutsOfObjects::PartsOfObjects parts = layoutsOfObjects.partsOfObjectsWith(id); + AnswersAboutObjects all = answersAboutMembersOf(parts.own, answers); + numberOfObjectsRead = all.numberOfObjectsRead; + if (parts.holdsObjectsOfEveryEscapedClass) + all.add(aboutObjectsOfEveryEscapedClass); + if (parts.holdsObjectsOfEveryLostClass) + all.add(aboutObjectsOfEveryLostClass); OptionSet open = all.open & actorsThatCanWrite(PlaceKind::Field); if (open.containsAny(makers) && (all.isMadeByRowThatNamesLayoutOrByOperation || makerAboveClassesHasAccountedFor(id) || everyClassAboveListsOwnNames(objects))) open.remove(makers); // No row describes the native code that holds an object. - if (sets.meet(objects, inputs.solver().possibleValuesOf(inputs.bodies().nodes().ofHost(ValueFlowHostNode::HeldByNativeCode)))) + if (all.isHeldByNativeCode) open.add(Actor::NativeCode); // Nobody lists where the objects are made. if (application.layouts[id.index()].flow.allocationClasses.isEmpty()) @@ -1122,9 +1149,13 @@ struct FieldResults { void findActorsOpenOnObjects() { Vector objectsOfLayouts; + for (ValueSetID shared : { layoutsOfObjects.objectsOfEveryEscapedClass(), layoutsOfObjects.objectsOfEveryLostClass() }) { + if (shared != ValueSets::nothing) + objectsOfLayouts.append(shared); + } for (uint32_t index = 0; index < numberOfLayouts; ++index) { - if (hasObjects(LayoutID(index)) && layoutsOfObjects.objectsWith(LayoutID(index)) != ValueSets::nothing) - objectsOfLayouts.append(layoutsOfObjects.objectsWith(LayoutID(index))); + if (hasObjects(LayoutID(index)) && layoutsOfObjects.partsOfObjectsWith(LayoutID(index)).own != ValueSets::nothing) + objectsOfLayouts.append(layoutsOfObjects.partsOfObjectsWith(LayoutID(index)).own); } Vector objects; { @@ -1138,11 +1169,19 @@ struct FieldResults { ValueFlowObjectID object = objects[position]; answers[object.index()] = { actorsOpenOn(inputs, object), isMadeByRowThatNamesLayoutOrByOperation(object) }; }); + AnswersAboutObjects aboutObjectsOfEveryEscapedClass = answersAboutMembersOf(layoutsOfObjects.objectsOfEveryEscapedClass(), answers.span()); + AnswersAboutObjects aboutObjectsOfEveryLostClass = answersAboutMembersOf(layoutsOfObjects.objectsOfEveryLostClass(), answers.span()); actorsOpenOnObjectsOfLayouts = Vector>(numberOfLayouts); + Vector numbersOfObjectsRead; + numbersOfObjectsRead.fill(0u, numberOfLayouts); forEachIndexInParallel(loops, numberOfLayouts, [&](unsigned index) { if (hasObjects(LayoutID(index))) - actorsOpenOnObjectsOfLayouts[index] = findActorsOpenOnObjectsWith(LayoutID(index), answers.span()); + actorsOpenOnObjectsOfLayouts[index] = findActorsOpenOnObjectsWith(LayoutID(index), answers.span(), aboutObjectsOfEveryEscapedClass, aboutObjectsOfEveryLostClass, numbersOfObjectsRead[index]); }); + uint64_t numberOfObjectsRead = aboutObjectsOfEveryEscapedClass.numberOfObjectsRead + aboutObjectsOfEveryLostClass.numberOfObjectsRead; + for (uint32_t number : numbersOfObjectsRead) + numberOfObjectsRead += number; + build.statistics().count(StatisticID::ObjectsReadForActorsOpenOnLayouts, numberOfObjectsRead); } using WritesSeen = Vector; diff --git a/Source/JavaScriptCore/aot/valueflow/AOTValueFlowResultsFieldsInternal.h b/Source/JavaScriptCore/aot/valueflow/AOTValueFlowResultsFieldsInternal.h index 786a3295fa462..3358746122245 100644 --- a/Source/JavaScriptCore/aot/valueflow/AOTValueFlowResultsFieldsInternal.h +++ b/Source/JavaScriptCore/aot/valueflow/AOTValueFlowResultsFieldsInternal.h @@ -274,6 +274,16 @@ class LayoutsOfObjects { ValueSetID objectsWith(LayoutID layout) const { return m_objectsOfLayout[layout.index()]; } + // The same objects in parts. Thousands of layouts may hold the shared sets: what is asked of every member is asked of those once. + struct PartsOfObjects { + ValueSetID own { ValueSets::nothing }; + bool holdsObjectsOfEveryEscapedClass { false }; + bool holdsObjectsOfEveryLostClass { false }; + }; + PartsOfObjects partsOfObjectsWith(LayoutID layout) const { return m_partsOfObjectsOfLayout[layout.index()]; } + ValueSetID objectsOfEveryEscapedClass() const { return m_objectsOfEveryEscapedClass; } + ValueSetID objectsOfEveryLostClass() const { return m_objectsOfEveryLostClass; } + private: struct SelectedLayouts { uint32_t list { 0 }; @@ -303,6 +313,9 @@ class LayoutsOfObjects { Vector m_escapedClasses; Vector m_lostClasses; Vector m_objectsOfLayout; + Vector m_partsOfObjectsOfLayout; + ValueSetID m_objectsOfEveryEscapedClass { ValueSets::nothing }; + ValueSetID m_objectsOfEveryLostClass { ValueSets::nothing }; }; struct StoredUnderKey { diff --git a/Source/JavaScriptCore/aot/valueflow/AOTValueFlowResultsStores.cpp b/Source/JavaScriptCore/aot/valueflow/AOTValueFlowResultsStores.cpp index 011d3acfd4991..eee7e5b89858b 100644 --- a/Source/JavaScriptCore/aot/valueflow/AOTValueFlowResultsStores.cpp +++ b/Source/JavaScriptCore/aot/valueflow/AOTValueFlowResultsStores.cpp @@ -145,15 +145,20 @@ LayoutsOfObjects::LayoutsOfObjects(const ValueFlowResultInputs& inputs, ValueFlo m_objectsOfLayout[index] = makers.of(worker).of(objectsOfLayouts.subspan(firstObjectOfLayout[index], firstObjectOfLayout[index + 1] - firstObjectOfLayout[index])); }); } - auto addToEach = [&](const Vector& layouts, const Vector& objects) { + m_partsOfObjectsOfLayout = m_objectsOfLayout.map([](ValueSetID own) { + return PartsOfObjects { own }; + }); + auto addToEach = [&](const Vector& layouts, const Vector& objects, ValueSetID& set, bool PartsOfObjects::* holds) { if (objects.isEmpty()) return; - ValueSetID set = maker.of(objects.span()); - for (LayoutID layout : layouts) + set = maker.of(objects.span()); + for (LayoutID layout : layouts) { m_objectsOfLayout[layout.index()] = maker.unite(m_objectsOfLayout[layout.index()], set); + m_partsOfObjectsOfLayout[layout.index()].*holds = true; + } }; - addToEach(m_escapedClasses, objectsOfEveryEscapedClass); - addToEach(m_lostClasses, objectsOfEveryLostClass); + addToEach(m_escapedClasses, objectsOfEveryEscapedClass, m_objectsOfEveryEscapedClass, &PartsOfObjects::holdsObjectsOfEveryEscapedClass); + addToEach(m_lostClasses, objectsOfEveryLostClass, m_objectsOfEveryLostClass, &PartsOfObjects::holdsObjectsOfEveryLostClass); } static Vector sortedKeysOf(const StoresIntoObjects& stores) From 274e93a358c98944850ae992104a4135c9cbc3c0 Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Sun, 1 Nov 2026 15:29:30 -0800 Subject: [PATCH 45/49] AOT: a test that runs the frames pass alone gives the verifier the tables that the pass published The test tool's road for one pass hands the verifier no results of the flow analysis, so under `check=VerifyEachPass` it reported CalleeRetainsFramePointer for a callee that AllocateInFrames had just proven to keep nothing. Where the verifier's inputs hold no flow they get the pass's own table. In a build the inputs always hold it: no output changes. With an IR test. --- .../aot/pipeline/AOTPipeline.cpp | 5 ++++ ...rames-environment-lent-to-a-direct-call.ir | 28 +++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 Source/JavaScriptCore/aot/tests/passes/allocate-in-frames-environment-lent-to-a-direct-call.ir diff --git a/Source/JavaScriptCore/aot/pipeline/AOTPipeline.cpp b/Source/JavaScriptCore/aot/pipeline/AOTPipeline.cpp index 6f614ed8fc115..8df6f81527128 100644 --- a/Source/JavaScriptCore/aot/pipeline/AOTPipeline.cpp +++ b/Source/JavaScriptCore/aot/pipeline/AOTPipeline.cpp @@ -1466,6 +1466,11 @@ PassOutcome Pipeline::runProgramPass(PassID id) output.flow->setBorrowedParameters(m_analyses.borrowedParameters()); ProgramCachesWithScratchTypes caches(m_program, m_build); output.flow->setFrameArrayParameters(computeFrameArrayParameters(m_program, m_build)); + // The verifier asks the same tables whether a callee keeps what lies in a frame. A test that runs this pass alone has not handed it the results of flow. + if (VerifierInputs inputs = m_runner.verifierInputs(); !inputs.flow) { + inputs.flow = &flow(); + m_runner.setVerifierInputs(inputs); + } } *output.frameAllocation = edit(id, allocateInFrames, inputsFor(id), *output.flow, m_build); return m_runner.lastProgramPassOutcome(); diff --git a/Source/JavaScriptCore/aot/tests/passes/allocate-in-frames-environment-lent-to-a-direct-call.ir b/Source/JavaScriptCore/aot/tests/passes/allocate-in-frames-environment-lent-to-a-direct-call.ir new file mode 100644 index 0000000000000..197c49c16414c --- /dev/null +++ b/Source/JavaScriptCore/aot/tests/passes/allocate-in-frames-environment-lent-to-a-direct-call.ir @@ -0,0 +1,28 @@ +# An environment that goes nowhere but into the capture of a function that is called directly, and only borrows it, lies in the frame. The verifier behind the pass asks the same table +# as the pass whether the callee keeps its capture, also where the pass runs alone and the analysis of flow has not run. +# FORM: Prepared +# RUN: pass AllocateInFrames +# CHECK: L0 only in frames +# CHECK-NEXT: outcome: Changed + +module m0 path="main.js" displayPath="main.js" +layout L0 kind=Environment name="copiedPerIteration" fields=[{key="x" declared={type=anything}}] + +function f0 name="lendsItsEnvironment" parameters=[{type=anything}, {type=boolean}] results=[{type=anything}] { + v2 = Absent + Jump -> next b1(v2) + b1(v3): + LoopHint + v5 = CallDirect v3 function=f1 + v6 = NewObject -, v5 layout=L0 fields=[0] + Branch v1 -> taken b2(), notTaken b3() + b2(): + Jump -> next b1(v6) + b3(): + Return v5 +} + +function f1 name="onlyBorrows" kind=Arrow captures=[{type=absent|env L0}] results=[{type=anything}] { + b0(v0: absent|env L0): + Return +} From bc7b292ea71395de021c7667fa56d7c525ea78fe Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Sun, 1 Nov 2026 15:29:30 -0800 Subject: [PATCH 46/49] AOT: two system calls at the start of an executable, and only its own handler is changed Behind "native code that runs out of stack in an executable is reported", which cost every start four `rt_sigaction` calls and one `sigaltstack`: a compiled hello world made more system calls than a plain executable. The main thread is no longer asked whether it has an alternate stack, because the host gave it one before the engine started; and only SIGSEGV is looked at, but on Darwin, where running into a guard page raises SIGBUS. `SA_SIGINFO` says nothing about whose an action is: a library that sets its own SIGSEGV handler had `SA_ONSTACK` forced on it. Now only a handler that lies in the executable's own text is changed. Goes with Bun's half. --- .../aot/image/AOTFaultTable.cpp | 33 +++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/Source/JavaScriptCore/aot/image/AOTFaultTable.cpp b/Source/JavaScriptCore/aot/image/AOTFaultTable.cpp index 21521b34e1db3..a5b811609941e 100644 --- a/Source/JavaScriptCore/aot/image/AOTFaultTable.cpp +++ b/Source/JavaScriptCore/aot/image/AOTFaultTable.cpp @@ -33,11 +33,13 @@ #include "MachineContext.h" #include "VM.h" #include +#include #include #include #include #include #include +#include #include #include #include @@ -47,6 +49,13 @@ #include #endif +#if OS(DARWIN) +#include +#elif OS(UNIX) +extern "C" char __executable_start; +extern "C" char etext; +#endif + WTF_ALLOW_UNSAFE_BUFFER_USAGE_BEGIN namespace JSC::AOT { @@ -568,20 +577,40 @@ struct SignalStackOfThread { } // anonymous namespace +// The handler of another component keeps the stack that it asked for. +static bool isCodeOfThisProgram(const void* address) +{ +#if OS(DARWIN) + Dl_info theirs { }; + Dl_info ours { }; + return dladdr(address, &theirs) && dladdr(reinterpret_cast(&takeFaultsOfThisThreadOnAnotherStack), &ours) && theirs.dli_fbase == ours.dli_fbase; +#else + return address >= &__executable_start && address < &etext; +#endif +} + void takeFaultsOfThisThreadOnAnotherStack() { + // The host gives the main thread its stack. stack_t current { }; - if (!sigaltstack(nullptr, ¤t) && (current.ss_flags & SS_DISABLE)) { + if (!isMainThread() && !sigaltstack(nullptr, ¤t) && (current.ss_flags & SS_DISABLE)) { WTF_THREAD_LOCAL_ALWAYS_DESTROY static thread_local SignalStackOfThread stack; stack.map(); } static std::atomic areActionsChanged { false }; if (areActionsChanged.exchange(true, std::memory_order_relaxed)) return; - for (int signal : { SIGSEGV, SIGBUS }) { +#if OS(DARWIN) + constexpr std::array signalsOfGuardPage { SIGSEGV, SIGBUS }; +#else + constexpr std::array signalsOfGuardPage { SIGSEGV }; +#endif + for (int signal : signalsOfGuardPage) { struct sigaction action; if (sigaction(signal, nullptr, &action) || !(action.sa_flags & SA_SIGINFO) || (action.sa_flags & SA_ONSTACK)) continue; + if (!isCodeOfThisProgram(reinterpret_cast(action.sa_sigaction))) + continue; action.sa_flags |= SA_ONSTACK; sigaction(signal, &action, nullptr); } From 35c6c14d195eeba76a34c64f5f57830985355b5e Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Sun, 1 Nov 2026 15:29:30 -0800 Subject: [PATCH 47/49] AOT: the mocks of one handed-on namespace stand in a stack It replaces the way of "the end of a mock gives back what the mock replaced". m1.js and m2.js each have `export * as ns from "./h.js"`; a test lays mock A over m1, then mock B over m2. - `vi.doUnmock("./m1.js")` while B stands: compiled code read `real | real`; `bun test` reads `B | B`. - Then `vi.doUnmock("./m2.js")`: the end gave back what B had replaced, A's value, though A had ended. Every later test file of the realm read A, and nothing cleared it. The mocks that replace one namespace stand in a stack that is kept with the module whose object is handed on. The latest answers; an end takes its own entry out wherever it sits; when nothing is left the module's own object is handed on. There plain `bun test` leaves the first mock's object in place: a difference that is meant. Goes with Bun's half. --- .../aot/runtime/AOTModuleLinking.cpp | 12 +++++++----- Source/JavaScriptCore/aot/runtime/AOTModules.cpp | 15 +++++++-------- Source/JavaScriptCore/aot/runtime/AOTModules.h | 4 +++- 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/Source/JavaScriptCore/aot/runtime/AOTModuleLinking.cpp b/Source/JavaScriptCore/aot/runtime/AOTModuleLinking.cpp index 73aa4bb35b3b7..e6f763172f0b1 100644 --- a/Source/JavaScriptCore/aot/runtime/AOTModuleLinking.cpp +++ b/Source/JavaScriptCore/aot/runtime/AOTModuleLinking.cpp @@ -1124,12 +1124,14 @@ void Modules::endMockLaidOverFile(ModuleID file) RETURN_IF_EXCEPTION(scope, void()); } - JSObject* handedOnBefore = loaded.handedOnBeforeMock; - loaded.handedOnBeforeMock.set(*this, nullptr); for (ModuleID handedOn : std::exchange(loaded.namespacesReplacedByMock, { })) { - JSValue before = handedOnBefore ? storedValue(realm, handedOnBefore, Identifier::from(vm, handedOn.index())) : JSValue(); - ensureLoaded(handedOn).handedOnInPlaceOfNamespace.set(*this, before); - m_namespacesReplacedByMock.set(handedOn.index(), !!before); + LoadedModule& target = ensureLoaded(handedOn); + target.mocksThatReplaceNamespace.removeFirst(file); + bool isLeft = !target.mocksThatReplaceNamespace.isEmpty(); + target.handedOnInPlaceOfNamespace.set(*this, isLeft ? storedValue(realm, target.handedOnByMocks, Identifier::from(vm, target.mocksThatReplaceNamespace.last().index())) : JSValue()); + if (!isLeft) + target.handedOnByMocks.set(*this, nullptr); + m_namespacesReplacedByMock.set(handedOn.index(), isLeft); didReplaceNamespaceHandedOn(handedOn); } if (JSObject* variables = loaded.variablesBeforeMock) { diff --git a/Source/JavaScriptCore/aot/runtime/AOTModules.cpp b/Source/JavaScriptCore/aot/runtime/AOTModules.cpp index 7787378bb9387..ce955642fa896 100644 --- a/Source/JavaScriptCore/aot/runtime/AOTModules.cpp +++ b/Source/JavaScriptCore/aot/runtime/AOTModules.cpp @@ -1275,15 +1275,14 @@ bool Modules::tryReplaceNamespaceHandedOn(ModuleID handedOn, JSValue value) return false; LoadedModule& mocked = ensureLoaded(*m_moduleWithMockBeingLaid); LoadedModule& target = ensureLoaded(handedOn); - if (!mocked.namespacesReplacedByMock.contains(handedOn)) { + if (!mocked.namespacesReplacedByMock.contains(handedOn)) mocked.namespacesReplacedByMock.append(handedOn); - // Another mock, over another module that hands the object on, may have given a value. The end of this one gives that back. - if (JSValue before = target.handedOnInPlaceOfNamespace) { - if (!mocked.handedOnBeforeMock) - mocked.handedOnBeforeMock.set(*this, createTableOfExports()); - mocked.handedOnBeforeMock.get()->putDirectMayBeIndex(m_instance.globalObject, Identifier::from(*m_instance.vm, handedOn.index()), before); - } - } + // Another mock, over another module that hands the object on, may have given a value. The latest answers, and each goes with its own mock. + target.mocksThatReplaceNamespace.removeFirst(*m_moduleWithMockBeingLaid); + target.mocksThatReplaceNamespace.append(*m_moduleWithMockBeingLaid); + if (!target.handedOnByMocks) + target.handedOnByMocks.set(*this, createTableOfExports()); + target.handedOnByMocks.get()->putDirectMayBeIndex(m_instance.globalObject, Identifier::from(*m_instance.vm, m_moduleWithMockBeingLaid->index()), value); target.handedOnInPlaceOfNamespace.set(*this, value); m_namespacesReplacedByMock.set(handedOn.index()); didReplaceNamespaceHandedOn(handedOn); diff --git a/Source/JavaScriptCore/aot/runtime/AOTModules.h b/Source/JavaScriptCore/aot/runtime/AOTModules.h index 8bdd35bb5b0ab..bd74ed5a97bfa 100644 --- a/Source/JavaScriptCore/aot/runtime/AOTModules.h +++ b/Source/JavaScriptCore/aot/runtime/AOTModules.h @@ -97,7 +97,7 @@ struct LinkContext { macro(JSValue, answerOfMockWhileOriginalRuns) \ macro(JSObject*, exportsAddedByMock) \ macro(JSValue, handedOnInPlaceOfNamespace) \ - macro(JSObject*, handedOnBeforeMock) \ + macro(JSObject*, handedOnByMocks) \ macro(JSValue, errorKeptByLoader) \ macro(JSObject*, exportsWhileRequired) \ macro(JSObject*, capturedExports) \ @@ -756,6 +756,8 @@ class Modules { // `export * from "./leaf.cjs"`: the values of the mock went over names of these, which keep what was replaced. Vector modulesBehindStarsUnderMock; Vector namespacesReplacedByMock; + // Of the module whose object is handed on: the mocked modules whose mocks give a value in its place, the latest last. + Vector mocksThatReplaceNamespace; bool capturedFieldsAreOfESModule { false }; bool capturedFieldsAreOfRuntimeModule { false }; Identifier resolvedKey; From fc15cda31a609aed75221d29e37d976650b4bdba Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Sun, 1 Nov 2026 15:29:30 -0800 Subject: [PATCH 48/49] AOT: a declared iteration may step the iterator itself It mends "the iterator of a map is told by its cell" and "the iterator of a set is told by its cell", under `--aot-validate` alone: no answer was wrong at default flags. Every validating executable ended by SIGABRT at `Array.from(map.values())` and `map.values().toArray()`: "AOT native call: .. internal state changed, the entry writes , not InternalFields". An entry that says that it iterates its operand writes nothing by itself; the write is `next`'s, and native code steps the iterator. --- .../aot/runtime/AOTNativeCallValidation.cpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/Source/JavaScriptCore/aot/runtime/AOTNativeCallValidation.cpp b/Source/JavaScriptCore/aot/runtime/AOTNativeCallValidation.cpp index d643093808d82..caaf660de845c 100644 --- a/Source/JavaScriptCore/aot/runtime/AOTNativeCallValidation.cpp +++ b/Source/JavaScriptCore/aot/runtime/AOTNativeCallValidation.cpp @@ -1011,6 +1011,15 @@ class NativeCallValidationScope::Checker { }); } + // What the operand's own `next` would do. The call site's writes, which hold those of that code, are checked above. + bool isSteppedAsDeclared(const Operand& operand, const OperandDifference& difference) const + { + return difference.change == OperandChange::InternalStateChanged && isBuiltinIterator(operand.object) && std::ranges::any_of(m_declaration.overloads, [&](const BuiltinOverload* overload) { + const BuiltinParameter* parameter = m_table.parameterFor(*overload, operand.position); + return parameter && parameter->protocols.contains(Protocol::Iterate); + }); + } + void checkChanges(bool didRunCode) { AbstractHeaps declared = declaredWrites(); @@ -1056,7 +1065,7 @@ class NativeCallValidationScope::Checker { fail(ValidationFailureKind::NativeCallChangedOperand, operand.object, makeString(what, ", uses "_s, describeMembers(uses), ", needs "_s, describeMembers(declaring))); else if (change == difference.change && difference.key && uses != OperandUses::all() && !hasActionOn(operand.object, difference.change, difference.key.get())) fail(ValidationFailureKind::NativeCallChangedOperand, operand.object, makeString(what, ", no property action covers the key"_s)); - else if (!m_declaration.overloads.isEmpty() && !heaps.isEmpty() && !declared.containsAny(heaps)) + else if (!m_declaration.overloads.isEmpty() && !heaps.isEmpty() && !declared.containsAny(heaps) && !isSteppedAsDeclared(operand, difference)) fail(ValidationFailureKind::NativeCallWroteHeap, operand.object, makeString(what, ", the entry writes "_s, describeMembers(declared), ", not "_s, describeMembers(heaps))); } } From c448026cf05fecb75e0c0828dc55bb866cf33abb Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Sun, 1 Nov 2026 15:40:12 -0800 Subject: [PATCH 49/49] AOT: fix the macOS build AOTForwardWalk.cpp uses entryBlock without including AOTCFG.h, which declares it. AOTModules.cpp writes ArrayAllocationProfile inside namespace JSC::AOT, where the name can also mean JSC::AOT::ArrayAllocationProfile, so the call to constructArray() found no overload. Both compiled only because of what stood earlier in their unified-source bundles. Adding a file moved the bundles on macOS. --- Source/JavaScriptCore/aot/ir/AOTForwardWalk.cpp | 1 + Source/JavaScriptCore/aot/runtime/AOTModules.cpp | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/Source/JavaScriptCore/aot/ir/AOTForwardWalk.cpp b/Source/JavaScriptCore/aot/ir/AOTForwardWalk.cpp index 49446df6c5f94..81683fdad1e62 100644 --- a/Source/JavaScriptCore/aot/ir/AOTForwardWalk.cpp +++ b/Source/JavaScriptCore/aot/ir/AOTForwardWalk.cpp @@ -8,6 +8,7 @@ #if ENABLE(AOT) +#include "AOTCFG.h" #include "AOTSort.h" namespace JSC { namespace AOT { namespace Typed { diff --git a/Source/JavaScriptCore/aot/runtime/AOTModules.cpp b/Source/JavaScriptCore/aot/runtime/AOTModules.cpp index ce955642fa896..fb96b96f35201 100644 --- a/Source/JavaScriptCore/aot/runtime/AOTModules.cpp +++ b/Source/JavaScriptCore/aot/runtime/AOTModules.cpp @@ -4789,7 +4789,7 @@ JSPromise* Modules::startImport(ModuleID module, std::optional referrer call.append(jsBoolean(caller == ImportCaller::Host)); call.append(jsNumber(static_cast(text))); call.append(stateOfWait); - JSArray* heldCall = constructArray(realm, static_cast(nullptr), call); + JSArray* heldCall = constructArray(realm, static_cast(nullptr), call); RETURN_IF_EXCEPTION(scope, rejectedPromiseWithPendingException(realm, scope)); factories->performPromiseThenWithContext(vm, realm, goOn, jsUndefined(), promise, heldCall); return promise; @@ -5744,7 +5744,7 @@ JSPromise* Modules::importUncompiled(const Identifier& key, ModuleID referrer, R call.append(jsString(vm, key.string())); call.append(jsNumber(moduleIDForHost(*m_instance.program, referrer).index())); call.append(stateOfWait); - JSArray* heldCall = constructArray(realm, static_cast(nullptr), call); + JSArray* heldCall = constructArray(realm, static_cast(nullptr), call); RETURN_IF_EXCEPTION(scope, rejectedPromiseWithPendingException(realm, scope)); factories->performPromiseThenWithContext(vm, realm, goOn, jsUndefined(), promise, heldCall); return promise;