Skip to content

Commit

Permalink
Merged: [string] Fix regexp fast path in MaybeCallFunctionAtSymbol
Browse files Browse the repository at this point in the history
The regexp fast path in MaybeCallFunctionAtSymbol had an issue in which
we'd call ToString after checking that the given {object} was a fast
regexp and deciding to take the fast path. This is invalid since
ToString() can call into user-controlled JS and may mutate {object}.

There's no way to place the ToString call correctly in this instance:
1 before BranchIfFastRegExp, it's a spec violation if we end up on the
  slow regexp path;
2 the problem with the current location is already described above;
3 and we can't place it into the fast-path regexp builtin (e.g.
  RegExpReplace) either due to the same reasons as 1.

The solution in this CL is to restrict the fast path to string
arguments only, i.e. cases where ToString would be a nop and can safely
be skipped.

NOTRY=true
NOPRESUBMIT=true
NOTREECHECKS=true
TBR=yangguo@chromium.org

Bug: chromium:782145
Change-Id: Ifd35b3a9a6cf2e77c96cb860a8ec98eaec35aa85
Reviewed-on: https://chromium-review.googlesource.com/763207
Reviewed-by: Jakob Gruber <jgruber@chromium.org>
Commit-Queue: Jakob Gruber <jgruber@chromium.org>
Cr-Commit-Position: refs/branch-heads/6.2@{#88}
Cr-Branched-From: efa2ac4-refs/heads/6.2.414@{#1}
Cr-Branched-From: a861ebb-refs/heads/master@{#47693}
  • Loading branch information
schuay authored and Commit Bot committed Nov 10, 2017
1 parent c01178e commit cfc3404
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 13 deletions.
29 changes: 16 additions & 13 deletions src/builtins/builtins-string-gen.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1051,9 +1051,9 @@ void StringBuiltinsAssembler::RequireObjectCoercible(Node* const context,
}

void StringBuiltinsAssembler::MaybeCallFunctionAtSymbol(
Node* const context, Node* const object, Handle<Symbol> symbol,
const NodeFunction0& regexp_call, const NodeFunction1& generic_call,
CodeStubArguments* args) {
Node* const context, Node* const object, Node* const maybe_string,
Handle<Symbol> symbol, const NodeFunction0& regexp_call,
const NodeFunction1& generic_call, CodeStubArguments* args) {
Label out(this);

// Smis definitely don't have an attached symbol.
Expand Down Expand Up @@ -1083,14 +1083,21 @@ void StringBuiltinsAssembler::MaybeCallFunctionAtSymbol(
}

// Take the fast path for RegExps.
// There's two conditions: {object} needs to be a fast regexp, and
// {maybe_string} must be a string (we can't call ToString on the fast path
// since it may mutate {object}).
{
Label stub_call(this), slow_lookup(this);

GotoIf(TaggedIsSmi(maybe_string), &slow_lookup);
GotoIfNot(IsString(maybe_string), &slow_lookup);

RegExpBuiltinsAssembler regexp_asm(state());
regexp_asm.BranchIfFastRegExp(context, object, object_map, &stub_call,
&slow_lookup);

BIND(&stub_call);
// TODO(jgruber): Add a no-JS scope once it exists.
Node* const result = regexp_call();
if (args == nullptr) {
Return(result);
Expand Down Expand Up @@ -1196,12 +1203,10 @@ TF_BUILTIN(StringPrototypeReplace, StringBuiltinsAssembler) {
// Redirect to replacer method if {search[@@replace]} is not undefined.

MaybeCallFunctionAtSymbol(
context, search, isolate()->factory()->replace_symbol(),
context, search, receiver, isolate()->factory()->replace_symbol(),
[=]() {
Node* const subject_string = ToString_Inline(context, receiver);

return CallBuiltin(Builtins::kRegExpReplace, context, search,
subject_string, replace);
return CallBuiltin(Builtins::kRegExpReplace, context, search, receiver,
replace);
},
[=](Node* fn) {
Callable call_callable = CodeFactory::Call(isolate());
Expand Down Expand Up @@ -1439,12 +1444,10 @@ TF_BUILTIN(StringPrototypeSplit, StringBuiltinsAssembler) {
// Redirect to splitter method if {separator[@@split]} is not undefined.

MaybeCallFunctionAtSymbol(
context, separator, isolate()->factory()->split_symbol(),
context, separator, receiver, isolate()->factory()->split_symbol(),
[=]() {
Node* const subject_string = ToString_Inline(context, receiver);

return CallBuiltin(Builtins::kRegExpSplit, context, separator,
subject_string, limit);
return CallBuiltin(Builtins::kRegExpSplit, context, separator, receiver,
limit);
},
[=](Node* fn) {
Callable call_callable = CodeFactory::Call(isolate());
Expand Down
2 changes: 2 additions & 0 deletions src/builtins/builtins-string-gen.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,11 @@ class StringBuiltinsAssembler : public CodeStubAssembler {
// }
//
// Contains fast paths for Smi and RegExp objects.
// Important: {regexp_call} may not contain any code that can call into JS.
typedef std::function<Node*()> NodeFunction0;
typedef std::function<Node*(Node* fn)> NodeFunction1;
void MaybeCallFunctionAtSymbol(Node* const context, Node* const object,
Node* const maybe_string,
Handle<Symbol> symbol,
const NodeFunction0& regexp_call,
const NodeFunction1& generic_call,
Expand Down
21 changes: 21 additions & 0 deletions test/mjsunit/regress/regress-782145.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2017 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

function newFastRegExp() { return new RegExp('.'); }
function toSlowRegExp(re) { re.exec = 42; }

let re = newFastRegExp();
const evil_nonstring = { [Symbol.toPrimitive]: () => toSlowRegExp(re) };
const empty_string = "";

String.prototype.replace.call(evil_nonstring, re, empty_string);

re = newFastRegExp();
String.prototype.match.call(evil_nonstring, re, empty_string);

re = newFastRegExp();
String.prototype.search.call(evil_nonstring, re, empty_string);

re = newFastRegExp();
String.prototype.split.call(evil_nonstring, re, empty_string);

0 comments on commit cfc3404

Please sign in to comment.