Repository navigation
Conversation
…hen it is a Number https://bugs.webkit.org/show_bug.cgi?id=326072 Function.prototype.bind takes the length of its result from the "length" of its target only if that value is a Number. functionProtoFuncBind has that check. operationFunctionBind, which DFG and FTL code call for a bind() with up to three bound arguments, had the same steps without it. A string was converted ("3" gave a length of 3), valueOf() of an object ran, and a Symbol or a BigInt made bind() throw a TypeError. functionProtoFuncBind, which the interpreter and Baseline code call, answers 0 for all of these. The builtin Function.prototype.call of this fork reaches the operation from its own optimized code. So call.bind(bind)(f) could convert in a caller that was never compiled. This is the hunk of WebKit#75885 that adds the check to operationFunctionBind, with upstream's text. That pull request is open and not reviewed yet. Its other change, which reads the prototype of the target before "length" and "name", is not part of this commit. * JSTests/BUN-TEST-DIFFERENCES.md: * JSTests/stress/bound-function-does-not-convert-non-number-length.js: Added. * Source/JavaScriptCore/dfg/DFGOperations.cpp: (JSC::operationFunctionBind): Co-authored-by: Sergey Rubanov <chi187@gmail.com>
|
Status
|
|
No actionable comments were generated in the recent review. WalkthroughThe DFG bind operation leaves bound-function length at zero when the target length is not a number. Numeric lengths retain conversion, bound-argument subtraction, and clamping. A stress test checks these behaviors across target types and caller forms. ChangesBound Function Length
Merge Risk:
|
|
Preview build of 270c83e: |
Each way to write the call has one caller, for a function whose length is a value. Two of them also have a caller for each other kind of target: one that shows the compiler the Structure of its target, and one that does not. In a release build the default mode takes 0.09 s, was 0.25 s. * JSTests/stress/bound-function-does-not-convert-non-number-length.js:
There was a problem hiding this comment.
I reviewed the updated PR and found no bugs; the new commit reduces the test to 18 callers, which addresses the timing nit from my earlier pass. Because Source/JavaScriptCore and JSTests are CODEOWNERS paths, a human look from the jsc reviewers is still worthwhile.
What was reviewed:
- The
isNumber()guard inoperationFunctionBind(DFGOperations.cpp:4793) now matchesfunctionProtoFuncBind(FunctionPrototype.cpp:187) line for line;lengthstays at its initial 0 for non-Number values, and the exception checks aroundgetandtoIntegerOrInfinityare unchanged. - The new stress test's
expectedLengthmatches the spec (non-Number gives 0, -Infinity gives 0, trunc then subtract bound args, floor at 0), and the test is registered in BUN-TEST-DIFFERENCES.md. - Not done in this review: running the test or measuring it against the 200 ms limit in JSTests/README.md; the 0.09 s figure is the author's.
Extended reasoning...
The change adds a one-line if (lengthValue.isNumber()) guard around the length conversion in the DFG/FTL operationFunctionBind JIT operation, matching the existing check in the host function functionProtoFuncBind, plus a new JSTests/stress test and a row in JSTests/BUN-TEST-DIFFERENCES.md. It touches no security-sensitive surface (no auth, crypto, injection, or data exposure); the behavioral effect is that bind() in optimized tiers no longer calls user valueOf/toString or throws on a Symbol length. The second commit (270c83e) cut the test from 50 to 18 noInline callers in response to the earlier inline nit. The C++ hunk is small and identical to established code, but Source/JavaScriptCore and JSTests are covered by .github/CODEOWNERS (jsc-reviewers), which is why this is a defer rather than an approve.
… its shorter test oven-sh/WebKit#1051 now runs 18 callers in its test, not 50. The engine code of the two preview builds is the same.
One hunk of WebKit#75885 (https://bugs.webkit.org/show_bug.cgi?id=326072), open upstream. No user reported it: I read these steps for #1023.
Problem
bind()converts a targetlengththat is not a Number:"3"gives 3, and a Symbol throwsTypeError: Cannot convert a symbol to a number. Bun 1.4.4 does this by default. Node answers 0.operationFunctionBind(dfg/DFGOperations.cpp:4793) callstoIntegerOrInfinitywith noisNumber()check.functionProtoFuncBindhas it.call.bind(bind)(f)converts in a caller that was never compiled. No release has it.Fix
if (lengthValue.isNumber()). Any other value leaves the length 0, as ECMA-262 says.JSTests/stresstest, all modes.de043228c2fails 15 of 17 runs, this branch 0. Also 1,704 related runs.Background
operationFunctionBindforbind()with up to three bound arguments. Other tiers callfunctionProtoFuncBind.[[GetPrototypeOf]]step for everybind()) and one helper for the copies of these steps: say if you want it.Downsides
bind()of a bound function, or of a target with a redefined Numberlength, runs 3 more instructions (1,145 to 1,148). Others: 336 in both.JSRemoteFunction::copyNameAndLength(ShadowRealm) still converts. Its check waits for [JSC] ShadowRealm: wrap a generic target's return value for the caller's realm #590 (Notes).class C { static length = "2" }had length 2.Notes
Where it comes from
functionProtoFuncBindin 24e437b and addedoperationFunctionBindwithout it 17 hours later, in 1094054 (2023-03-22). [JSC] Read the bind target's prototype before length and name WebKit/WebKit#75885 is the first fix for it: open since 2026-10-05, no review yet.What a release has, and what only
mainhas1.4.4+38afee6c8) with default flags: the reproduction below prints 2 and 6 and throws. WithBUN_JSC_useJIT=0it prints 0 and 0 and does not throw.de043228c2is wrong in 99: the 6 more are thecall.bind(bind)form.call([JSC] Add live module exports for module mocks, plus fifteen smaller changes for bun test #781): its own optimized code calls the operation. A caller that is never compiled gets the converted length from its call 638 on ifbindis the only function that goes through that call site of the builtin, from call 3,319 on with 1 to 7 other functions there, and never with 8 or more (jsc --useConcurrentJIT=0, thejscof releaseautobuild-de043228c2).The port
DFGOperations.cppdiff of [JSC] Read the bind target's prototype before length and name WebKit/WebKit#75885, applied withgit apply. The 14 lines of the block equal the post-image of that hunk: 0 differing lines. No// Bun:line is inside the block. If upstream changes the text before it lands, take upstream's lines.lengthandname(JSBoundFunction::materializeObservablePrototype, a newJSBoundFunction::createoverload). Only a Proxy target can observe that order, and the new call is in front of the common path of both bind functions. It arrives with a sync if upstream takes it.Not in this PR
JSRemoteFunction::copyNameAndLength(runtime/JSRemoteFunction.cpp:266) converts the same way for a function that crosses the border of a ShadowRealm, in every tier: the wrapper of a target whoselengthis"3"has length 3, and a Symbol makes the wrap throw. Upstream removed ShadowRealm, so no sync brings a check there. I took my check for it out of this PR. With it, a Proxy target whoselengthis not a Number (a catch-all Proxy) is wrapped where it throws today, and a call through that wrapper then returns a function of the other realm: the defect that [JSC] ShadowRealm: wrap a generic target's return value for the caller's realm #590 fixes for the other Proxy targets. The check must land after [JSC] ShadowRealm: wrap a generic target's return value for the caller's realm #590.ENABLE(AOT)arm that the operation does not have. That is a difference of speed, not of results.functionProtoFuncBind,operationFunctionBind,JSRemoteFunction::copyNameAndLength, and in Bunsrc/jsc/modules/_NativeModule.h. The new test compares the operation withFunction.prototype.binditself in each cell.The Bun side
lengthonly when it is a Number bun#48863 (draft) pins the preview build of this PR and runs the test as ajsc-stressfixture. A debug build of Bun with that pin passes it. Bun 1.4.4 and a debug build of Bun'smainfail it.lengthonly when it is a Number bun#48863): 188 of 189 jobs pass. The one job that fails is the macOS 27 beta lane, which cannot fail the build.nodemailer.test.tstimed out there, and the same binary passes it on the other macOS aarch64 lane.Order with #1023
git merge-fileofDFGOperations.cppandBUN-TEST-DIFFERENCES.mdwith its head (79f76b4761), in both orders: 0 conflicts.lengthgetter beside a member with a computed key, or under["length"]). With its build alone (b3049a873b) they answer 8 for"9"and one bound argument in DFG code, and 0 in the interpreter. So this PR goes first, or both go in the same move of Bun's pin.Reproduce (
jsc --useConcurrentJIT=0, or Bun 1.4.4 withconsole.logforprintand without thenoInlineline)The test
stress/bound-function-does-not-convert-non-number-length.js: 342 cells, from 18noInlinecallers and 19 lengths. There are 10 ways to write the call (0 to 4 bound arguments,bind.call,bind.apply,call.bind(bind), a tail call, an unused result), each with a function whoselengthis a value. Two of them (f.bind(null, 1)andbind.call(f, null, 1)) also have a caller for each other kind of target: a getter, a class static field, a bound function, a Proxy. Each target is bound once first: the firstbind()gives a function its ownnameproperty, so another Structure, and a caller compiled before that exits at its Structure check.de043228c2: 99 cells are wrong with the JIT (default thresholds, DFG eager, FTL eager). These are all the cells whose length converts to another answer or throws. 0 are wrong with--useJIT=0. This branch: 0 wrong, 0 conversions, 1 read of alengthgetter for each bind.de043228c2in two modes:lockdown(no JIT) andno-cjit-validate-phases(--useLLIntICs=false: the DFG does not learn that the callee isbind, so it compiles a plain call).jsconce are under 200 ms. The others areno-cjit-validate-phases(0.20 s),ftl-eager-no-cjit(0.21 s) andno-cjit-collect-continuously(0.41 s). Each caller runstestLoopCounttimes: with half of that, 8 of the 18 callers reach the FTL in the modes with default thresholds.Measurements (x64 Linux. Local release builds of
jsc, clang 23.1.2,-O3, no LTO:de043228c2and this branch.)Instructions of one call of
operationFunctionBind, callees included (gdbstepi, 6 calls in a row, all equal):de043228c2nameandlengthlengthlengthlengthperf_event_open, (I(2N) - I(N)) / N at N = 1,000,000, median of 10 interleaved runs, spread inside one build at most 0.4): inlineNewBoundFunction86.94 and 86.93, operation with an original target 405.54 and 405.56, operation with a Numberlength1,214.81 and 1,217.84, operation with a bound function 1,138.84 and 1,141.82, host function with four bound arguments 1,365.53 and 1,365.51.nm -S,size -A):operationFunctionBind1,682 to 1,695..textofjsc37,809,100 in both builds.functionProtoFuncBind1,838 in both.Tests run
run-javascriptcore-tests. Release build, 17 modes:de043228c2fails 15, this branch 0. Debug build with ASAN, 15 modes: thejscof releaseautobuild-de043228c2fails 13, thejscof the preview build of this PR fails 0.run-javascriptcore-tests --filter 'stress/.*(bind|bound-function|shadow-realm|remote-function|function-length|bun-aot-builtins-table|function-prototype-call)': 1,704 runs of 104 files, 0 failures.built-ins/Function/prototype/bind(100 files), sloppy and strict, three sets of options: 600 runs pass on both builds. No test there gives a target alengththat is not a Number in optimized code.