Repository navigation
Conversation
…g#1795) builtin-array-fromasync.js looked up .call on the mapping function and iterator method (e.g. mapFn.call(thisArg, value, i) and method.call(arrayLike)). This violated the TC39 specification (sec-array.fromasync steps 8.d.ix.1 and 9.g.iv.1, and GetIteratorFromMethod step 1), which invoke the mapper and iterator method directly via Call(F, V, argumentsList) without performing a property lookup on .call. If .call was overridden, deleted, or set to a non-callable value on the mapper function or iterator method, or if Function.prototype.call was modified, Array.fromAsync would execute the hijacked .call or throw a TypeError. Fix this by passing the internal monkey patch-proof js_call_function ("call") into builtin-array-fromasync.js and calling call(thisArg, func, ...args). Regenerate builtin-array-fromasync.h via `make codegen`. Fixes quickjs-ng#1795
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Title
Array.fromAsync: use internal call instead of .call lookup
Description
Fixes #1795
Problem
Array.fromAsyncinbuiltin-array-fromasync.jsused method-call syntax on user-provided functions:mapFn.call(thisArg, value, i)in both array-like and iterable pathsmethod.call(arrayLike)when invoking[Symbol.iterator]or[Symbol.asyncIterator]This performed an observable property lookup for
.callonmapFnandmethod. Under the ECMAScript specification (Array.fromAsync):mapFnmust be invoked viaCall(mapper, thisArg, « value, 𝔽(k) »)(steps 8.d.ix.1 and 9.g.iv.1).GetIteratorFromMethodstep 1:Call(method, obj).Neither step reads a
.callproperty. If user code or a library provides a function with a custom or non-callable.callproperty, or ifFunction.prototype.callis modified/deleted,Array.fromAsynceither executes the hijacked.callmethod or throws aTypeError: not a function.Fix
quickjs.c, passedJS_NewCFunction(ctx, js_call_function, "call", 2)tojs_bytecode_autoinitwhen instantiatingqjsc_builtin_array_fromasync. This provides the same monkey patch-proofjs_call_functionhelper already used byIterator.zipbuilt-ins.builtin-array-fromasync.js:callin the outer IIFE wrapper parameters.mapFn.call(thisArg, value, i)withcall(thisArg, mapFn, value, i).method.call(arrayLike)withcall(arrayLike, method).builtin-array-fromasync.hwithmake codegen(qjsc -C -ss).Verification
Added
tests/bug1795.jscovering:mapFnwith overridden.callon an iterable source ([1, 2]) returns[10, 20].mapFnwith overridden.callon an array-like source ({ length: 2, 0: 1, 1: 2 }) returns[10, 20].[Symbol.iterator]) with overridden.callreturns[1, 2].[Symbol.asyncIterator]) with overridden.callreturns[1,2]..call(null, number) succeeds instead of throwingTypeError: not a function.thisArgand(value, index)parameters are forwarded correctly tomapFnin both iterable and array-like paths.thisreceiver for iterator methods is the sourcearrayLikeobject.Test results:
tests/bug1795.jsfails withassertion failed: got |10|, expected |hijacked|.tests/bug1795.jspasses cleanly.make test(run-test262 -c tests.conf) passes with 0 errors:Result: 0/121 errors, 9 excluded.make ctestpasses with zero compiler warnings/errors (-Wall -Wextra -Werror -pedantic).