Skip to content

[JSC] bind() in DFG and FTL code uses the target's length only when it is a Number (one hunk of WebKit/WebKit#75885) - #1051

Open
robobun wants to merge 2 commits into
mainfrom
robobun/7be26801/bind-length-only-when-number
Open

robobun wants to merge 2 commits into
mainfrom
robobun/7be26801/bind-length-only-when-number

Conversation

@robobun

@robobun robobun commented Nov 13, 2026 •

Copy link
Copy Markdown
Collaborator

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

  • In DFG and FTL code, bind() converts a target length that is not a Number: "3" gives 3, and a Symbol throws TypeError: Cannot convert a symbol to a number. Bun 1.4.4 does this by default. Node answers 0.
  • operationFunctionBind (dfg/DFGOperations.cpp:4793) calls toIntegerOrInfinity with no isNumber() check. functionProtoFuncBind has it.
  • Since [JSC] Add live module exports for module mocks, plus fifteen smaller changes for bun test #781, call.bind(bind)(f) converts in a caller that was never compiled. No release has it.

Fix

Background

Downsides

  • A DFG/FTL bind() of a bound function, or of a target with a redefined Number length, 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).
  • Hot code that read the converted value gets 0: a bound class C { static length = "2" } had length 2.
Notes

Where it comes from

What a release has, and what only main has

  • Bun 1.4.4 (1.4.4+38afee6c8) with default flags: the reproduction below prints 2 and 6 and throws. With BUN_JSC_useJIT=0 it prints 0 and 0 and does not throw.
  • The new test has 342 cells (below). Bun 1.4.4 is wrong in 93 of them. de043228c2 is wrong in 99: the 6 more are the call.bind(bind) form.
  • That form is the fork's builtin 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 if bind is 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, the jsc of release autobuild-de043228c2).

The port

  • The commit is hunk 2 of the DFGOperations.cpp diff of [JSC] Read the bind target's prototype before length and name WebKit/WebKit#75885, applied with git 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.
  • Left out: the other change of that pull request. It reads the prototype of the target before length and name (JSBoundFunction::materializeObservablePrototype, a new JSBoundFunction::create overload). 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 whose length is "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 whose length is 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.
  • The host function has an ENABLE(AOT) arm that the operation does not have. That is a difference of speed, not of results.
  • The steps stay written out in each function: functionProtoFuncBind, operationFunctionBind, JSRemoteFunction::copyNameAndLength, and in Bun src/jsc/modules/_NativeModule.h. The new test compares the operation with Function.prototype.bind itself in each cell.

The Bun side

Order with #1023

  • git merge-file of DFGOperations.cpp and BUN-TEST-DIFFERENCES.md with its head (79f76b4761), in both orders: 0 conflicts.
  • [JSC] bind() reads the name of a target that was named at run time #1023 sends two more class shapes through these steps (a static length getter 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 with console.log for print and without the noInline line)

function targetWithLength(length) {
    function target(a, b, c) { }
    Object.defineProperty(target, "length", { value: length });
    return target;
}
function bound(target) { return target.bind(null, 1); }
noInline(bound);
for (let i = 0; i < 100000; i++)
    bound(targetWithLength(5));
print(bound(targetWithLength("3")).length);                         // 2, want 0
print(bound(targetWithLength({ valueOf() { return 7; } })).length); // 6, want 0
bound(targetWithLength(Symbol()));                                  // TypeError: Cannot convert a symbol to a number

The test

  • stress/bound-function-does-not-convert-non-number-length.js: 342 cells, from 18 noInline callers 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 whose length is a value. Two of them (f.bind(null, 1) and bind.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 first bind() gives a function its own name property, 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 a length getter for each bind.
  • It passes on de043228c2 in two modes: lockdown (no JIT) and no-cjit-validate-phases (--useLLIntICs=false: the DFG does not learn that the callee is bind, so it compiles a plain call).
  • Release build, each mode by itself (median of 3): the default mode takes 0.12 s, and 13 of the 16 modes that run jsc once are under 200 ms. The others are no-cjit-validate-phases (0.20 s), ftl-eager-no-cjit (0.21 s) and no-cjit-collect-continuously (0.41 s). Each caller runs testLoopCount times: 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: de043228c2 and this branch.)

Instructions of one call of operationFunctionBind, callees included (gdb stepi, 6 calls in a row, all equal):

Target de043228c2 This branch
original name and length 336 336
redefined int32 length 1,145 1,148
redefined double length 1,157 1,160
a bound function 1,069 1,072
string length 1,249 1,139
  • The 3 instructions are in the body of the operation and include 2 conditional branches (16 to 18).
  • User instructions for each bind (perf_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): inline NewBoundFunction 86.94 and 86.93, operation with an original target 405.54 and 405.56, operation with a Number length 1,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.
  • Bytes (nm -S, size -A): operationFunctionBind 1,682 to 1,695. .text of jsc 37,809,100 in both builds. functionProtoFuncBind 1,838 in both.

Tests run

  • The new test with run-javascriptcore-tests. Release build, 17 modes: de043228c2 fails 15, this branch 0. Debug build with ASAN, 15 modes: the jsc of release autobuild-de043228c2 fails 13, the jsc of 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.
  • test262 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 a length that is not a Number in optimized code.

…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>
@robobun

robobun commented Nov 13, 2026 •

Copy link
Copy Markdown
Collaborator Author

Status

  • Reproduced on Bun 1.4.4 (1.4.4+38afee6c8) with default flags, and on the jsc of main (de04322, the commit that Bun's main pins) with --useConcurrentJIT=0. The script is under Reproduce in the description: it prints 2 and 6 and throws TypeError: Cannot convert a symbol to a number. With the JIT off, and in Node v26.3.0, it prints 0 and 0 and does not throw.
  • stress/bound-function-does-not-convert-non-number-length.js with run-javascriptcore-tests, release build, 17 modes: de04322 fails 15, this branch 0.
  • The Bun side is Bump WebKit (oven-sh/WebKit#1051 preview): bind() in DFG and FTL code uses the target's length only when it is a Number bun#48863 (draft): it pins the preview build of this pull request and runs the test as a jsc-stress fixture. Bun's CI passes with that pin (Buildkite build 141203, 188 of 189 jobs). The one job that fails is a beta lane where a mail test timed out, and the same binary passes that test on another lane.

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing blocking. The comments below are optional suggestions. There is no need to push a fix for them before merging.

@coderabbitai

coderabbitai Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The 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.

Changes

Bound Function Length

Layer / File(s) Summary
Length type guard and adjustment
Source/JavaScriptCore/dfg/DFGOperations.cpp
operationFunctionBind adjusts the bound function length only when the target length is a number.
Regression coverage
JSTests/stress/bound-function-does-not-convert-non-number-length.js, JSTests/BUN-TEST-DIFFERENCES.md
The stress test checks length results, reads, names, and conversion behavior across target types and caller forms. The test differences table records the test and its expected behavior.
Merge Risk: Minimal · up to 270c8

This change makes bind() in optimized code stop converting non-Number target lengths, consistent with the interpreter and other engines. No concrete merge-blocking risk was identified, and a regression test covers the behavior.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: DFG and FTL bind() uses the target's length only when it is a Number. It also cites the upstream hunk. The changeset matches this: operationFunctionBind gains an isNum…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Description check Passed The description follows the WebKit template's core intent. It names the bug (WebKit bug 326072, linked upstream PR #75885), explains the problem and the fix, and gives the reason for the change. It al…
Full details: Title check

Explanation

The title names the main change: DFG and FTL bind() uses the target's length only when it is a Number. It also cites the upstream hunk. The changeset matches this: operationFunctionBind gains an isNumber() check, and a stress test is added along with a doc note.

Full details: Description check

Explanation

The description follows the WebKit template's core intent. It names the bug (WebKit bug 326072, linked upstream PR #75885), explains the problem and the fix, and gives the reason for the change. It also reports verification: a new stress test that fails on the base build and passes on this branch, plus broader test runs. The template asks for a bug link, a Reviewed by line, an explanation, and a file-level changelog in the commit-message format. The description supplies the bug link and an explanation. It does not follow the template's commit-message layout, and it has no 'Reviewed by' line or per-file list of changed paths with function names. Those are formatting gaps. The content is thorough and on-topic. The description also includes a lot of detail that a reviewer must read carefully, but it is not vague.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Warning

The configuration directory could not be loaded. This review used the available YAML and UI settings; directory instructions and checks were not evaluated.

Git: CodeRabbit could not clone the repository, so clone-backed analysis was skipped and this review may be incomplete. Verify repository clone access, such as SSH credentials, before requesting another full review. If clone access is intentionally unavailable, use path_filters to narrow the review scope.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: 00bcb3bd-fcd3-4b15-9f5a-d42b7f0ebb87
Commits

Reviewing files that changed from the base of the PR and between fcd88aa and 270c83e.

Files selected for processing: 1

  • JSTests/stress/bound-function-does-not-convert-non-number-length.js

Included review availability: This review used your included allowance. Your plan provides up to 2 included reviews per hour; 1 remain after this review.

@github-actions

github-actions Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

Preview build of 270c83e: autobuild-preview-pr-1051-270c83e5

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:

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 in operationFunctionBind (DFGOperations.cpp:4793) now matches functionProtoFuncBind (FunctionPrototype.cpp:187) line for line; length stays at its initial 0 for non-Number values, and the exception checks around get and toIntegerOrInfinity are unchanged.
  • The new stress test's expectedLength matches 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.

robobun added a commit to oven-sh/bun that referenced this pull request Nov 13, 2026
… 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant