Skip to content

Commit a819274

Browse files
committed
[GR-42673] Fix possible AssertionError in the parser due to stack exhaustion.
PullRequest: js/2769
2 parents 28c448d + 21e62f9 commit a819274

File tree

8 files changed

+338
-263
lines changed

8 files changed

+338
-263
lines changed

graal-js/src/com.oracle.js.parser/src/com/oracle/js/parser/Lexer.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2010, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2010, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -80,7 +80,7 @@
8080
* Responsible for converting source content into a stream of tokens.
8181
*/
8282
@SuppressWarnings("fallthrough")
83-
public class Lexer extends Scanner {
83+
public class Lexer extends Scanner implements StringPool {
8484
private static final boolean XML_LITERALS = Options.getBooleanProperty("lexer.xmlliterals");
8585

8686
private static final String MSG_EDIT_STRING_MISSING_BRACE = "edit.string.missing.brace";
@@ -2131,6 +2131,7 @@ public TruffleString valueOfRawString(final long token) {
21312131
return stringIntern(sb.toString());
21322132
}
21332133

2134+
@Override
21342135
public TruffleString stringIntern(TruffleString candidate) {
21352136
TruffleString interned = internedStrings.putIfAbsent(candidate.toJavaStringUncached(), candidate);
21362137
return interned == null ? candidate : interned;

graal-js/src/com.oracle.js.parser/src/com/oracle/js/parser/Parser.java

Lines changed: 268 additions & 245 deletions
Large diffs are not rendered by default.

graal-js/src/com.oracle.js.parser/src/com/oracle/js/parser/ParserContext.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2014, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2014, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -104,12 +104,11 @@ public ParserContextNode peek() {
104104
* @param node The node expected to be popped, used for sanity check
105105
* @return The removed node
106106
*/
107+
@SuppressWarnings("unchecked")
107108
public <T extends ParserContextNode> T pop(final T node) {
108109
--sp;
109-
@SuppressWarnings("unchecked")
110110
final T popped = (T) stack[sp];
111111
stack[sp] = null;
112-
assert node == popped;
113112

114113
return popped;
115114
}

graal-js/src/com.oracle.js.parser/src/com/oracle/js/parser/ParserContextFunctionNode.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2014, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2014, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -44,7 +44,6 @@
4444
import java.util.ArrayList;
4545
import java.util.List;
4646
import java.util.Map;
47-
import java.util.function.Function;
4847

4948
import com.oracle.js.parser.ir.Block;
5049
import com.oracle.js.parser.ir.Expression;
@@ -555,9 +554,9 @@ private void putFunctionSymbolIfAbsent(String bindingName, TruffleString binding
555554
}
556555
}
557556

558-
public void finishBodyScope(Function<TruffleString, TruffleString> stringIntern) {
557+
public void finishBodyScope(StringPool strings) {
559558
if (needsArguments()) {
560-
putFunctionSymbolIfAbsent(Parser.ARGUMENTS_NAME, stringIntern.apply(Parser.ARGUMENTS_NAME_TS), Symbol.IS_ARGUMENTS);
559+
putFunctionSymbolIfAbsent(Parser.ARGUMENTS_NAME, strings.stringIntern(Parser.ARGUMENTS_NAME_TS), Symbol.IS_ARGUMENTS);
561560
}
562561
if (hoistableBlockFunctionDeclarations != null) {
563562
declareHoistedBlockFunctionDeclarations();
@@ -571,18 +570,18 @@ public void finishBodyScope(Function<TruffleString, TruffleString> stringIntern)
571570
if (!isArrow()) {
572571
boolean needsThisForEval = hasEval() || hasArrowEval();
573572
if (usesThis() || usesSuper() || needsThisForEval || getFlag(FunctionNode.HAS_DIRECT_SUPER) != 0) {
574-
putFunctionSymbolIfAbsent(TokenType.THIS.getName(), stringIntern.apply(TokenType.THIS.getNameTS()), Symbol.IS_THIS);
573+
putFunctionSymbolIfAbsent(TokenType.THIS.getName(), strings.stringIntern(TokenType.THIS.getNameTS()), Symbol.IS_THIS);
575574
}
576575
if (usesSuper() || (isMethod() && needsThisForEval)) {
577-
putFunctionSymbolIfAbsent(TokenType.SUPER.getName(), stringIntern.apply(TokenType.SUPER.getNameTS()), Symbol.IS_SUPER);
576+
putFunctionSymbolIfAbsent(TokenType.SUPER.getName(), strings.stringIntern(TokenType.SUPER.getNameTS()), Symbol.IS_SUPER);
578577
}
579578
if (usesNewTarget() || needsThisForEval) {
580-
putFunctionSymbolIfAbsent(Parser.NEW_TARGET_NAME, stringIntern.apply(Parser.NEW_TARGET_NAME_TS), Symbol.IS_NEW_TARGET);
579+
putFunctionSymbolIfAbsent(Parser.NEW_TARGET_NAME, strings.stringIntern(Parser.NEW_TARGET_NAME_TS), Symbol.IS_NEW_TARGET);
581580
}
582581
}
582+
// Close the scopes already to make sure we don't add any more symbols.
583+
bodyScope.close();
583584
if (hasParameterExpressions()) {
584-
// Lock the scopes to make sure we don't add any more symbols. Not strictly necessary.
585-
bodyScope.close();
586585
getParameterScope().close();
587586
}
588587
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* The Universal Permissive License (UPL), Version 1.0
6+
*
7+
* Subject to the condition set forth below, permission is hereby granted to any
8+
* person obtaining a copy of this software, associated documentation and/or
9+
* data (collectively the "Software"), free of charge and under any and all
10+
* copyright rights in the Software, and any and all patent rights owned or
11+
* freely licensable by each licensor hereunder covering either (i) the
12+
* unmodified Software as contributed to or provided by such licensor, or (ii)
13+
* the Larger Works (as defined below), to deal in both
14+
*
15+
* (a) the Software, and
16+
*
17+
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
18+
* one is included with the Software each a "Larger Work" to which the Software
19+
* is contributed by such licensors),
20+
*
21+
* without restriction, including without limitation the rights to copy, create
22+
* derivative works of, display, perform, and distribute the Software and make,
23+
* use, sell, offer for sale, import, export, have made, and have sold the
24+
* Software and the Larger Work(s), and to sublicense the foregoing rights on
25+
* either these or other terms.
26+
*
27+
* This license is subject to the following condition:
28+
*
29+
* The above copyright notice and either this complete permission notice or at a
30+
* minimum a reference to the UPL must be included in all copies or substantial
31+
* portions of the Software.
32+
*
33+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
39+
* SOFTWARE.
40+
*/
41+
package com.oracle.js.parser;
42+
43+
import com.oracle.truffle.api.strings.TruffleString;
44+
45+
public interface StringPool {
46+
TruffleString stringIntern(TruffleString candidate);
47+
}

graal-js/src/com.oracle.js.parser/src/com/oracle/js/parser/ir/Block.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2010, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2010, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -115,6 +115,7 @@ public class Block extends Node implements BreakableNode, Terminal, Flags<Block>
115115
public Block(final long token, final int finish, final int flags, final Scope scope, final List<Statement> statements) {
116116
super(token, finish);
117117
assert start <= finish;
118+
assert scope.isClosed() : scope;
118119

119120
this.statements = List.copyOf(statements);
120121
this.scope = scope;

graal-js/src/com.oracle.js.parser/src/com/oracle/js/parser/ir/ClassNode.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ public ClassNode(long token, int finish, IdentNode ident, Expression classHerita
8787
this.hasClassElementDecorators = hasClassElementDecorators;
8888
this.classDecorators = classDecorators;
8989
assert staticElementCount == elementCount(classElements, true);
90+
assert scope.isClosed() : scope;
9091
}
9192

9293
private ClassNode(final ClassNode classNode, final IdentNode ident, final Expression classHeritage, final ClassElement constructor, final List<ClassElement> classElements,

graal-js/src/com.oracle.js.parser/src/com/oracle/js/parser/ir/Scope.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2019, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -394,6 +394,10 @@ public void close() {
394394
closed = true;
395395
}
396396

397+
public boolean isClosed() {
398+
return closed;
399+
}
400+
397401
/**
398402
* Clears defined symbols and moves any local uses into the parent scope.
399403
*/
@@ -732,6 +736,8 @@ private String getScopeKindName() {
732736
return "Global";
733737
} else if (isModuleScope()) {
734738
return "Module";
739+
} else if (isEvalScope()) {
740+
return "Eval";
735741
} else if (isFunctionBodyScope()) {
736742
return "Var";
737743
} else if (isFunctionParameterScope()) {
@@ -744,8 +750,6 @@ private String getScopeKindName() {
744750
return "Class";
745751
} else if (isClassBodyScope()) {
746752
return "Private";
747-
} else if (isEvalScope()) {
748-
return "Eval";
749753
}
750754
return "";
751755
}

0 commit comments

Comments
 (0)