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/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/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/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 540d3eb247710..1bd07e917019d 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 }; }; @@ -1114,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/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/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::span (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/builtins/holders.builtins.ts b/Source/JavaScriptCore/aot/builtins/holders.builtins.ts index 9087a7fde23af..1fc022703eed1 100644 --- a/Source/JavaScriptCore/aot/builtins/holders.builtins.ts +++ b/Source/JavaScriptCore/aot/builtins/holders.builtins.ts @@ -511,10 +511,12 @@ export default b.declarations( }), b.class("%MapIterator%", { ...lockedIteratorColumns, + jsType: "JSMapIteratorType", proto: { next: libraryStep("MapIteratorPrototype", "next", iteratorResult) }, }), b.class("%SetIterator%", { ...lockedIteratorColumns, + jsType: "JSSetIteratorType", proto: { next: libraryStep("SetIteratorPrototype", "next", iteratorResult) }, }), b.class("%StringIterator%", { 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/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/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"); 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..a5b811609941e 100644 --- a/Source/JavaScriptCore/aot/image/AOTFaultTable.cpp +++ b/Source/JavaScriptCore/aot/image/AOTFaultTable.cpp @@ -33,14 +33,29 @@ #include "MachineContext.h" #include "VM.h" #include +#include +#include #include #include #include #include +#include #include #include #include +#if OS(UNIX) +#include +#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 { @@ -526,6 +541,84 @@ 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 + +// 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 (!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; +#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); + } +} +#else +void takeFaultsOfThisThreadOnAnotherStack() { } +#endif + } // namespace JSC::AOT WTF_ALLOW_UNSAFE_BUFFER_USAGE_END diff --git a/Source/JavaScriptCore/aot/image/AOTImage.h b/Source/JavaScriptCore/aot/image/AOTImage.h index 98f14d586bfdf..364f24fd2fe22 100644 --- a/Source/JavaScriptCore/aot/image/AOTImage.h +++ b/Source/JavaScriptCore/aot/image/AOTImage.h @@ -117,7 +117,6 @@ class Image { // Images that share their realms stand in a list, in the order in which they were built. An image that shares none is a list of one. bool sharesRealms() const { return header().has(ImageFlag::SharesRealms); } - bool holdsLibraryAlone() const { return header().has(ImageFlag::HoldsLibraryAlone); } const Image& first() const { return *m_first; } const Image* above() const { return m_above.load(std::memory_order_acquire); } const Image& last() const; diff --git a/Source/JavaScriptCore/aot/image/AOTImageBuilder.cpp b/Source/JavaScriptCore/aot/image/AOTImageBuilder.cpp index 8f783861069f4..df981ba75540a 100644 --- a/Source/JavaScriptCore/aot/image/AOTImageBuilder.cpp +++ b/Source/JavaScriptCore/aot/image/AOTImageBuilder.cpp @@ -750,8 +750,6 @@ static void writeHeader(ImageHeader& header, const ImageWriterInputs& parts, con flags.add(ImageFlag::IsObfuscated); if (context.options().sharesRealms()) flags.add(ImageFlag::SharesRealms); - if (context.options().holdsLibraryAlone()) - flags.add(ImageFlag::HoldsLibraryAlone); header.numbers = { .shapesBelow = context.imagesBelow() ? context.imagesBelow()->numbersOfLast().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/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/ir/AOTEffectSummaries.cpp b/Source/JavaScriptCore/aot/ir/AOTEffectSummaries.cpp index 121192b4994c4..1be7b1ed6aadd 100644 --- a/Source/JavaScriptCore/aot/ir/AOTEffectSummaries.cpp +++ b/Source/JavaScriptCore/aot/ir/AOTEffectSummaries.cpp @@ -782,25 +782,33 @@ 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, 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; } -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; + 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/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..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()) @@ -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,15 @@ 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) + { + m_rootsOfBodiesToRead = roots; + m_bodiesToRead = &bodies; + } void giveReferencesTo(BodyReferences* references) { m_references = references; } void update(EffectSummaries&); void bringSeedUpToDate(EffectSummaries&); @@ -295,6 +307,7 @@ class EffectSummarizer { private: void findRecordsThatLookDifferent(); + void findBodiesToRead(); void updateSeed(BorrowedParameters&&); Vector findReasonsToSummarize() const; void summarizeBodies(); @@ -346,6 +359,9 @@ 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 }; 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..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(); @@ -532,6 +560,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); diff --git a/Source/JavaScriptCore/aot/ir/AOTForwardWalk.cpp b/Source/JavaScriptCore/aot/ir/AOTForwardWalk.cpp new file mode 100644 index 0000000000000..81683fdad1e62 --- /dev/null +++ b/Source/JavaScriptCore/aot/ir/AOTForwardWalk.cpp @@ -0,0 +1,111 @@ +/* + * Copyright (C) 2026 Oven, Inc. All rights reserved. + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include "config.h" +#include "AOTForwardWalk.h" + +#if ENABLE(AOT) + +#include "AOTCFG.h" +#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) 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/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/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/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(); 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/ir/schema/compile-call.def b/Source/JavaScriptCore/aot/ir/schema/compile-call.def index 05cb64cb137bc..22809459ce93b 100644 --- a/Source/JavaScriptCore/aot/ir/schema/compile-call.def +++ b/Source/JavaScriptCore/aot/ir/schema/compile-call.def @@ -197,11 +197,12 @@ enum Sabotage { ReadsEnterNoGetterThatEscaped RopesPassForFlatStrings EscapedObjectsBearOnlyTheirOwnMarks + FunctionsMarkNoCard } 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 +212,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/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/ir/schema/statistics.def b/Source/JavaScriptCore/aot/ir/schema/statistics.def index 543444de4f5af..eb2d54da57a70 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 @@ -30,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/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/passes/AOTExpandOwnKeys.cpp b/Source/JavaScriptCore/aot/passes/AOTExpandOwnKeys.cpp index d242923d8120d..8ec034cf43911 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) @@ -1433,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; @@ -1443,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) @@ -1647,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/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/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/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/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); 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/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/AOTErrorMessages.cpp b/Source/JavaScriptCore/aot/runtime/AOTErrorMessages.cpp index 20a59cef633b8..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. @@ -3300,7 +3316,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 +3427,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/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) 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; diff --git a/Source/JavaScriptCore/aot/runtime/AOTModuleEvaluation.cpp b/Source/JavaScriptCore/aot/runtime/AOTModuleEvaluation.cpp index ad2a0bc4aa59d..9bad89d179fb0 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 @@ -2731,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) { @@ -2862,6 +2873,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/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 ScopedLambda& 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 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); return true; @@ -1846,6 +1853,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)); @@ -2241,8 +2250,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(); @@ -2268,7 +2277,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; @@ -2290,7 +2299,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) { @@ -2701,6 +2709,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; } @@ -2866,8 +2902,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); } @@ -3020,6 +3057,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,13 +3145,55 @@ 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) @@ -3341,7 +3421,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 +4115,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 +4146,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); @@ -4077,7 +4168,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)); } @@ -4506,9 +4599,8 @@ bool Modules::deliverFirstTextThatWaits() promise->fulfill(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); @@ -4589,10 +4681,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; @@ -4617,6 +4705,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); @@ -4657,6 +4753,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]] { @@ -4688,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; @@ -4710,6 +4811,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)) { @@ -4924,7 +5030,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; @@ -5635,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; @@ -6290,12 +6399,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)))); } @@ -7383,7 +7503,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; @@ -8054,6 +8174,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)) { @@ -9101,7 +9223,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 ddd26ccf196f1..bd74ed5a97bfa 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*, handedOnByMocks) \ macro(JSValue, errorKeptByLoader) \ macro(JSObject*, exportsWhileRequired) \ macro(JSObject*, capturedExports) \ @@ -234,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 }; @@ -383,6 +384,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 +635,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); @@ -681,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; @@ -752,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; @@ -768,6 +774,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 +1091,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; @@ -1303,6 +1313,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; 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))); } } 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/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); 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/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/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" 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/AOTIRTestCommands.cpp b/Source/JavaScriptCore/aot/tests/AOTIRTestCommands.cpp index 5de449387ce87..bd4c27c307438 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,233 @@ 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, 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) + , 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(); +} + +// 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) +{ + 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() { static constexpr TestCommand commands[] = { @@ -325,6 +554,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/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 }, }; 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..e113394f8562a 100644 --- a/Source/JavaScriptCore/aot/tests/AOTTestRunner.cpp +++ b/Source/JavaScriptCore/aot/tests/AOTTestRunner.cpp @@ -23,6 +23,7 @@ #include #include #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(getCurrentProcessID())); + 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())); 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..55ba0a20e69ac --- /dev/null +++ b/Source/JavaScriptCore/aot/tests/codegen/classes-told-by-their-cells.ir @@ -0,0 +1,50 @@ +# 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)], 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 +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 + 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}] { + 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 +} +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/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 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 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 +} 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] 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 +} 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/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, +} 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/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 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..e06fa65191ada --- /dev/null +++ b/Source/JavaScriptCore/aot/tests/types/classes-told-by-their-cells.ir @@ -0,0 +1,20 @@ +# 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 = 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: 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=SetIterator}, {type=Map}, {type=Array}, {type=Array|MapIterator|SetIterator}] results=[{type=undefined}] 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}] 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 +} 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 +} 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 +} 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 +} 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 e64d3d30d55b3..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)); @@ -1356,7 +1378,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)); } } 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)); } diff --git a/Source/JavaScriptCore/aot/valueflow/AOTValueFlowResultsBuiltins.cpp b/Source/JavaScriptCore/aot/valueflow/AOTValueFlowResultsBuiltins.cpp index 6194fc5c9846c..f2f58775b571a 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); @@ -926,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) @@ -946,6 +986,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); 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) 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; 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) {