Repository navigation
[JSC] AOT: an accessor that unseen code may have put above a module object gets that module object - #994
[JSC] AOT: an accessor that unseen code may have put above a module object gets that module object#994robobun wants to merge 1 commit into
Conversation
|
Preview build of 1a85295: |
c0c9ed1 to
11955b5
Compare
…bject gets that module object Text that a program evaluates runs in the global scope. From there it comes to the object that a host keeps above every CommonJS module object, and it can lay an accessor on that object under a name or at an index that nothing locks. A file that looks the key up on its own `module` enters the accessor with its module object, and `exports` of that object is what the file exports. The flow analysis takes an accessor that no row describes for native code of the host (the stand-in %UndeclaredNativeCode%), and hands it a receiver only if the receiver is no built-in object. A module object is one, so it stayed in the program, and so did the layout that compiled code had proven for its exports. Where unseen code runs, the stand-in arm of HeapRule::callAccessors now passes the receiver through a second set: what is no built-in object, and the module objects of the program. A require function and an import.meta are not in it: neither leads to a module object. Where no unseen code runs the arm is as it was. The tests read and store a name, an index and a key that nobody knows on a module object, and come to the arm through Reflect.get, Object.assign, Object.prototype.toString and the scope of a with statement. They name no entry of a host above the module objects, so they hold for a table that states that link and for one that does not.
11955b5 to
1a85295
Compare
|
Status This pull request is rebased on main (b83d8a1) and holds the engine change alone. CI is green on 1a85295, and the How I reproduced it: # main's engine, CI testaot of b83d8a12ae
testaot run Source/JavaScriptCore/aot/tests/valueflow/modules/a-name-no-row-lists-read-off-a-module-object-hands-it-to-evaluated-text.ir --show-output
# held: those of 0 of 2 modules
# this head, CI testaot of 1a85295985
# held: those of 1 of 2 modulesThe programs for Bun's suite are in oven-sh/bun#47912. |
There was a problem hiding this comment.
Nothing blocking. The comments below are optional suggestions. There is no need to push a fix for them before merging.
Beyond the inline finding, I also checked the subspan(1) in the new whatIsNewToUnseenCode construction (AOTValueFlowHeapTables.cpp:745) for an out-of-bounds slice when a program has no module objects — appendObjectsOf always appends the canonical realm object first, so the span is never empty. I also checked that doesUnseenCodeRun() in the swapped arm is the same host-node predicate the rest of this rule file already uses, so the arm re-evaluates when that node flips rather than depending on rule order.
Extended reasoning...
The change is three source lines in the AOT value-flow analysis (a new ValueSetID in the heap-tables macro list, its construction in the ValueFlowHeapTables constructor, and a conditional in HeapRule::callAccessors) plus fifteen .ir tests; it touches no auth, crypto or injection surface. The one posted finding is about the N-API loader path the author documents as deliberately out of scope, which is a scope decision for a maintainer of aot/ to weigh. The concrete candidates I examined (span bounds, predicate ordering) were ruled out from the code.
| if (m_context.contains(functions, m_tables.undescribedNativeCode())) { | ||
| passToAccessorNobodyKnows(receiver, m_tables.whatIsNewToNativeCode()); | ||
| // The stand-in is native code of the host, to which no built-in object is new. Where unseen code runs it is also an accessor that such code has laid on an object above the receiver. | ||
| passToAccessorNobodyKnows(receiver, doesUnseenCodeRun() ? m_tables.whatIsNewToUnseenCode() : m_tables.whatIsNewToNativeCode()); |
There was a problem hiding this comment.
🟣 pre-existing, not blocking: Pre-existing, left open by this partial fix: a compiled program whose N-API addon lays a getter above module objects still throws TypeError: inner.secret is not a function where plain bun runs. The new arm at AOTValueFlowRulesHeapLookups.cpp:1032 hands module objects out only when doesUnseenCodeRun() is true, but a loader of machine code raises no UnseenCodeRuns flag. The sibling rule exposeModuleObjectsOfEntryPoints at AOTValueFlowRulesHost.cpp:1346 already treats machine code as reaching module objects via mayMachineCodeRun. Fix: make the hand-over condition at 1032 cover every way foreign code can reach the object above module objects, i.e. also when mayMachineCodeRun (AOTValueFlowRulesHost.cpp:1333) holds, matching the guard at 1346.
A small fix can ride a push you are already making; otherwise a short reply is enough.
Why this was flagged
A program loads an N-API addon and the addon, calling back into JS, defines a getter under an unlisted name on the object above module objects; a compiled file reads that name off its own module. The getter stores into module.exports. At AOTValueFlowRulesHeapLookups.cpp:1032 the lookup resolves to undescribedNativeCode, but doesUnseenCodeRun() is false because no text is evaluated, so passToAccessorNobodyKnows gets whatIsNewToNativeCode, which excludes every module object. The exported closure stays proven in the program, the executable constant-folds the callee and throws TypeError: inner.secret is not a function while plain bun runs. The base branch fails the same way; the diff fixes only the evaluated-text variant. The sibling guard at AOTValueFlowRulesHost.cpp:1346 already ORs isHostFlagRaised(UnseenCodeRuns) with mayMachineCodeRun(context) for exposing main-module objects, so the codebase treats machine code as able to reach module objects; this arm does not.
Verification: Pre-existing; acknowledged in diff: the code does nothing for machine code, and the base already fails by the same route. AOTValueFlowRulesHeapLookups.cpp:1032 chooses whatIsNewToNativeCode unless doesUnseenCodeRun(), which (AOTValueFlowRulesHeapInternal.h:155) reads only ValueFlowHostNode::UnseenCodeRuns. A loader of machine code never raises that flag (AOTValueFlowRulesHost.cpp:1069-1077).
|
No actionable comments were generated in the recent review. WalkthroughThe AOT value-flow accessor rule now uses a value set that includes program module objects when unseen code runs. New CommonJS IR fixtures check module-object access and escape results across evaluated-text, delayed-rule, and non-evaluated-text cases. ChangesUnseen-code module object flow
Possibly related PRs
Merge Risk:
|
|
Closing: #997 is merged. It replaces the middle and the back end of the ahead-of-time compiler, so this no longer applies to |
Problem
--aotexecutable throwsTypeError: inner.secret is not a functionwhere plainbunruns.new Functiontext puts a getter above every CommonJS module object. A compiled file reads that name off itsmodule, and the getter stores intomodule.exports.HeapRule::callAccessors(aot/valueflow/AOTValueFlowRulesHeapLookups.cpp:1031) hands a receiver to an accessor without a table row only if the receiver is no built-in object. A module object is one.Fix
whatIsNewToUnseenCode).Background
Functiontext.Downsides
moduleloses the proofs about its exports. A spread ofmodulecosts the same. On the old pin, 289 benchmarks and 8 servers compile to identical code.TypeError), a trap aboverequirefunctions, a redefinedidabove module objects.process.envornode:http.Notes
The program
inner.cjs:main.cjs:Plain
bunprints1 replaced. The executable throwsTypeError: inner.secret is not a function. (In 'inner.secret()', 'inner.secret' is undefined). WithBUN_JSC_validateAOTRuntime=1it aborts withAOT validation: ImpossibleMutation. Indirectevalandvm.runInThisContextgive the same result.What the flow analysis said
valueflow-why escaped-builtin=ModuleObjectprintsheld: those of 0 of 2 moduleson main, and the callee ofinner.secret()is the one function. With the change it prints1 of 2, and the callee set holds the unknown value.Why the guard
Without unseen code, what a lookup finds under an unlisted name above a module object is native code of the host. The host has every module object already. The test
a-name-no-row-lists-read-off-a-module-object-hands-it-to-nobody-where-no-text-is-evaluated.irkeeps that. Without the guard,module.hotwould hand out the module object in every program.Why
requireandimport.metastay outText that gets a
requirefunction of the program calls it with a name. An executable answers that with the documented error for a dynamic path, andrequire.mainanswers undefined where no compiled code names a way. The testa-name-no-row-lists-read-off-a-require-function-hands-it-to-nobody-beside-evaluated-text.irpins this.The tests
All 15 are in
aot/tests/valueflow/modules. Run with thetestaotandjscthat CI built: for main b83d8a1 the 12 printheld: those of 0 of 2 modules. For this head the folder gives 147 passed, and all ofaot/testsgives 5876 passed, 0 failed, 42 todo.requirefunction.Reflect.get,Object.assign,Object.prototype.toString(it readsSymbol.toStringTag), and a module object as the scope of awithstatement.The first version of this pull request also gave the stand-in host a row for the object above module objects, and three tests that needed it. They are gone: Bun's CI runs these tests against Bun's table, and that table does not state the link today.
Where main already answers right
On main the forms give the right answer when the program uses the environment object:
process.env,Bun.env, or a library module that reads it, such asnode:httpandnode:os. That object has native accessors without rows, and the link above a module object is unstated. With the object in use,valueflow-why escaped-builtin=ModuleObjectanswersescaped by Lookup "hot"on main. Of 29 programs that fail on Bun main, 26 print plain Bun's line there withvoid process.env.PORT;added.process.pid,process.versions,node:fsandnode:pathdo not have that effect.What a copy of
modulecosts({ ...module }), a rest pattern andObject.entries(module)read own properties only, so they enter no accessor above. The flow analysis looks the keys up through the chain, finds the stand-in, and now hands the module object out where text is evaluated (held: 1 of 2, on main0 of 2). The answer is safe and less exact.Object.keys(module)hands nothing out.What stays open
Moduleornew Module()from the program and lays the getter. The executable still throwsinner.secret is not a function. Bun's table counts a loader of machine code as no unseen code.mayMachineCodeRuninAOTValueFlowRulesHost.cppis the predicate that this arm would need. No program of the suites loads an addon and reads an unlisted name offmodule, so that guard has no measurement.requirefunctions.id,filename,pathorloadedon the object above module objects. A module object lists those names itself, so a lookup on one never goes above it. That needs the link in the table. node:module: make Module the class of CommonJS module objects bun#47848 makesModule.prototypethe object above module objects, makes those accessors non-configurable and plans the row.module.__proto__ornew Module().__proto__. It is wrong with the engine at 4d45755. The flow analysis of main already answersf3 f5for the call of the replaced export.What the self-review changed
The first version of this pull request was based on 4d45755. It had a second rule for index keys, and the stand-in host had a row for the object above module objects.
Reflect.get,Object.assign,Object.prototype.toStringand awithscope.Measurements
No Bun consumes WebKit main yet (oven-sh/bun#47605 moves the pin). The numbers on programs are from Bun main 675ee5b97b with the engine at 4d45755, against the same with this change. Release builds without LTO, linux-x64.
test/aot/tools/same-output.ts: IDENTICAL for 289 programs oftest/aot/perf/idiomsand for 8 servers oftest/aot/servers/app. All 8 servers raise the flagUnseenCodeRuns.--aot-validateand the checks of the run time on. Before, each of the 15 stops at anAOT validationline.test/aot/parallelthat use modules, loaders andnode:vm: 1012 pass, 4 are todo and 1 fails, before and after.AnalyzeFlowinstructions, least of 5 builds: hono 2,986.6 M against 2,993.9 M, express 9,799.2 M against 9,799.4 M. Two readings of one build differ by up to 0.9%.Order
// Todo:line.