Skip to content

Commit f76d0c4

Browse files
committed
Fix several bugs causing java.lang.AssertionErrors
1 parent 17314d2 commit f76d0c4

File tree

3 files changed

+56
-94
lines changed

3 files changed

+56
-94
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/MarshalModuleBuiltins.java

+13-6
Original file line numberDiff line numberDiff line change
@@ -1292,8 +1292,13 @@ private void setSource(Source s) {
12921292
}
12931293

12941294
private Source getSource() {
1295-
assert source != null;
1296-
return source;
1295+
if (source != null) {
1296+
return source;
1297+
} else {
1298+
// This should never happen when deserializing a bytecode DSL code unit, but could
1299+
// happen if the user tries to deserialize arbitrary bytes.
1300+
throw new MarshalError(ValueError, ErrorMessages.BAD_MARSHAL_DATA);
1301+
}
12971302
}
12981303

12991304
private void writeSparseTable(int[][] table) {
@@ -1330,8 +1335,9 @@ private CodeUnit readCodeUnit() {
13301335

13311336
private BytecodeCodeUnit readBytecodeCodeUnit() {
13321337
if (PythonOptions.ENABLE_BYTECODE_DSL_INTERPRETER) {
1333-
throw new AssertionError(
1334-
"Attempted to deserialize a code object from the manual bytecode interpreter, but the DSL interpreter is enabled. Consider clearing or setting a different pycache folder.");
1338+
throw new MarshalError(ValueError,
1339+
PythonUtils.tsLiteral(
1340+
"Attempted to deserialize a code object from the manual bytecode interpreter, but the DSL interpreter is enabled. Consider clearing or setting a different pycache folder."));
13351341
}
13361342

13371343
int fileVersion = readByte();
@@ -1375,8 +1381,9 @@ private BytecodeCodeUnit readBytecodeCodeUnit() {
13751381

13761382
private BytecodeDSLCodeUnit readBytecodeDSLCodeUnit() {
13771383
if (!PythonOptions.ENABLE_BYTECODE_DSL_INTERPRETER) {
1378-
throw new AssertionError(
1379-
"Attempted to deserialize a code object from the Bytecode DSL interpreter, but the manual interpreter is enabled. Consider clearing or setting a different pycache folder.");
1384+
throw new MarshalError(ValueError,
1385+
PythonUtils.tsLiteral(
1386+
"Attempted to deserialize a code object from the Bytecode DSL interpreter, but the manual interpreter is enabled. Consider clearing or setting a different pycache folder."));
13801387
}
13811388

13821389
byte[] serialized = readBytes();

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/compiler/bytecode_dsl/RootNodeCompiler.java

+20-17
Original file line numberDiff line numberDiff line change
@@ -1411,7 +1411,7 @@ private void emitCall(ExprTy func, ExprTy[] args, KeywordTy[] keywords) {
14111411
b.emitLoadLocal(function);
14121412
b.endBlock();
14131413

1414-
emitUnstar(args, receiver);
1414+
emitUnstar(() -> b.emitLoadLocal(receiver), args);
14151415
emitKeywords(keywords, function);
14161416
} else {
14171417
assert len(keywords) == 0;
@@ -1665,9 +1665,11 @@ private void createConstant(ConstantValue value) {
16651665
break;
16661666
case TUPLE:
16671667
b.beginMakeTuple();
1668-
for (ConstantValue cv : value.getFrozensetElements()) {
1668+
b.beginMakeVariadic();
1669+
for (ConstantValue cv : value.getTupleElements()) {
16691670
createConstant(cv);
16701671
}
1672+
b.endMakeVariadic();
16711673
b.endMakeTuple();
16721674
break;
16731675
case FROZENSET:
@@ -1944,18 +1946,19 @@ private void emitConstantTuple(ConstantCollection constantCollection) {
19441946
* @param args the sequence of expressions
19451947
*/
19461948
private void emitUnstar(ExprTy[] args) {
1947-
emitUnstar(args, null);
1949+
emitUnstar(null, args);
19481950
}
19491951

19501952
/**
1951-
* Same as above, but takes an optional receiver local. This is only used for method calls
1952-
* where the receiver needs to be included in the positional arguments.
1953+
* Same as above, but takes an optional Runnable to produce elements at the beginning of the
1954+
* sequence.
19531955
*
1956+
* @param initialElementsProducer a runnable to produce the first element(s) of the
1957+
* sequence.
19541958
* @param args the sequence of expressions to unstar
1955-
* @param receiver an optional local storing the receiver
19561959
*/
1957-
private void emitUnstar(ExprTy[] args, BytecodeLocal receiver) {
1958-
if (len(args) == 0 && receiver == null) {
1960+
private void emitUnstar(Runnable initialElementsProducer, ExprTy[] args) {
1961+
if (len(args) == 0 && initialElementsProducer == null) {
19591962
b.emitLoadConstant(PythonUtils.EMPTY_OBJECT_ARRAY);
19601963
} else if (anyIsStarred(args)) {
19611964
/**
@@ -1979,10 +1982,10 @@ private void emitUnstar(ExprTy[] args, BytecodeLocal receiver) {
19791982
boolean inVariadic = false;
19801983
int numOperands = 0;
19811984

1982-
if (receiver != null) {
1985+
if (initialElementsProducer != null) {
19831986
b.beginMakeVariadic();
1987+
initialElementsProducer.run();
19841988
inVariadic = true;
1985-
b.emitLoadLocal(receiver);
19861989
}
19871990

19881991
for (int i = 0; i < args.length; i++) {
@@ -2015,8 +2018,8 @@ private void emitUnstar(ExprTy[] args, BytecodeLocal receiver) {
20152018
b.endUnstar(numOperands);
20162019
} else {
20172020
b.beginMakeVariadic();
2018-
if (receiver != null) {
2019-
b.emitLoadLocal(receiver);
2021+
if (initialElementsProducer != null) {
2022+
initialElementsProducer.run();
20202023
}
20212024
visitSequence(args);
20222025
b.endMakeVariadic();
@@ -2925,13 +2928,13 @@ public Void visit(StmtTy.ClassDef node) {
29252928
b.emitLoadLocal(buildClassFunction);
29262929

29272930
// positional args
2928-
b.beginMakeVariadic();
2929-
emitMakeFunction(node, node.name, null, null);
2930-
emitPythonConstant(toTruffleStringUncached(node.name), b);
2931-
visitSequence(node.bases);
2932-
b.endMakeVariadic();
2931+
emitUnstar(() -> {
2932+
emitMakeFunction(node, node.name, null, null);
2933+
emitPythonConstant(toTruffleStringUncached(node.name), b);
2934+
}, node.bases);
29332935

29342936
// keyword args
2937+
validateKeywords(node.keywords);
29352938
emitKeywords(node.keywords, buildClassFunction);
29362939

29372940
b.endCallVarargsMethod();

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/bytecode_dsl/PBytecodeDSLRootNode.java

+23-71
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@
271271
@ShortCircuitOperation(name = "PrimitiveBoolOr", booleanConverter = PBytecodeDSLRootNode.BooleanIdentity.class, operator = Operator.OR_RETURN_CONVERTED)
272272
@SuppressWarnings("unused")
273273
public abstract class PBytecodeDSLRootNode extends PRootNode implements BytecodeRootNode {
274-
private static final int EXPLODE_LOOP_THRESHOLD = 32;
274+
public static final int EXPLODE_LOOP_THRESHOLD = 32;
275275
private static final BytecodeConfig TRACE_AND_PROFILE_CONFIG = PBytecodeDSLRootNodeGen.newConfigBuilder() //
276276
.addInstrumentation(TraceOrProfileCall.class) //
277277
.addInstrumentation(TraceLine.class) //
@@ -379,7 +379,7 @@ public static void doExit(VirtualFrame frame, AbstractTruffleException ate,
379379
@Bind("$root") PBytecodeDSLRootNode root,
380380
@Bind("this") Node location) {
381381
if (ate instanceof PException pe) {
382-
pe.notifyAddedTracebackFrame(!root.isPythonInternal());
382+
pe.notifyAddedTracebackFrame(!root.isInternal());
383383
}
384384

385385
if (root.needsTraceAndProfileInstrumentation()) {
@@ -1904,37 +1904,34 @@ public static PList perform(Object[] elements,
19041904
}
19051905

19061906
@Operation
1907+
@ImportStatic({PBytecodeDSLRootNode.class})
19071908
public static final class MakeSet {
1908-
@Specialization
1909-
public static PSet perform(VirtualFrame frame, Object[] elements,
1909+
@Specialization(guards = {"elements.length == length", "length <= EXPLODE_LOOP_THRESHOLD"}, limit = "1")
1910+
@ExplodeLoop
1911+
public static PSet performExploded(VirtualFrame frame, Object[] elements,
19101912
@Bind("$root") PBytecodeDSLRootNode rootNode,
19111913
@Bind("this") Node node,
1912-
@Cached(value = "elements.length", neverDefault = false) int length,
1913-
@Cached SetNodes.AddNode addNode,
1914-
@Cached HashingCollectionNodes.SetItemNode setItemNode) {
1915-
assert elements.length == length;
1916-
1914+
@Cached(value = "elements.length") int length,
1915+
@Shared @Cached SetNodes.AddNode addNode,
1916+
@Shared @Cached HashingCollectionNodes.SetItemNode setItemNode) {
19171917
PSet set = rootNode.factory.createSet();
1918-
if (length <= EXPLODE_LOOP_THRESHOLD) {
1919-
doExploded(frame, set, elements, length, addNode, setItemNode);
1920-
} else {
1921-
doRegular(frame, set, elements, length, addNode, setItemNode);
1922-
}
1923-
return set;
1924-
}
1925-
1926-
@ExplodeLoop
1927-
private static void doExploded(VirtualFrame frame, PSet set, Object[] elements, int length, SetNodes.AddNode addNode, HashingCollectionNodes.SetItemNode setItemNode) {
1928-
CompilerAsserts.partialEvaluationConstant(length);
19291918
for (int i = 0; i < length; i++) {
19301919
SetNodes.AddNode.add(frame, set, elements[i], addNode, setItemNode);
19311920
}
1921+
return set;
19321922
}
19331923

1934-
private static void doRegular(VirtualFrame frame, PSet set, Object[] elements, int length, SetNodes.AddNode addNode, HashingCollectionNodes.SetItemNode setItemNode) {
1935-
for (int i = 0; i < length; i++) {
1924+
@Specialization
1925+
public static PSet performRegular(VirtualFrame frame, Object[] elements,
1926+
@Bind("$root") PBytecodeDSLRootNode rootNode,
1927+
@Bind("this") Node node,
1928+
@Shared @Cached SetNodes.AddNode addNode,
1929+
@Shared @Cached HashingCollectionNodes.SetItemNode setItemNode) {
1930+
PSet set = rootNode.factory.createSet();
1931+
for (int i = 0; i < elements.length; i++) {
19361932
SetNodes.AddNode.add(frame, set, elements[i], addNode, setItemNode);
19371933
}
1934+
return set;
19381935
}
19391936
}
19401937

@@ -1999,54 +1996,9 @@ public static PList perform(@Bind("$root") PBytecodeDSLRootNode rootNode) {
19991996
@Operation
20001997
public static final class MakeTuple {
20011998
@Specialization
2002-
public static Object perform(@Variadic Object[] elements,
2003-
@Cached(value = "elements.length", neverDefault = false) int length,
1999+
public static Object perform(Object[] elements,
20042000
@Bind("$root") PBytecodeDSLRootNode rootNode) {
2005-
assert elements.length == length;
2006-
2007-
Object[] elts;
2008-
if (length <= EXPLODE_LOOP_THRESHOLD) {
2009-
elts = doExploded(elements, length);
2010-
} else {
2011-
elts = doRegular(elements, length);
2012-
}
2013-
return rootNode.factory.createTuple(elts);
2014-
}
2015-
2016-
@ExplodeLoop
2017-
private static Object[] doExploded(Object[] elements, int length) {
2018-
CompilerAsserts.partialEvaluationConstant(length);
2019-
int totalLength = 0;
2020-
for (int i = 0; i < length; i++) {
2021-
totalLength += ((Object[]) elements[i]).length;
2022-
}
2023-
2024-
Object[] elts = new Object[totalLength];
2025-
int idx = 0;
2026-
for (int i = 0; i < length; i++) {
2027-
Object[] arr = (Object[]) elements[i];
2028-
int len = arr.length;
2029-
System.arraycopy(arr, 0, elts, idx, len);
2030-
idx += len;
2031-
}
2032-
return elts;
2033-
}
2034-
2035-
private static Object[] doRegular(Object[] elements, int length) {
2036-
int totalLength = 0;
2037-
for (int i = 0; i < length; i++) {
2038-
totalLength += ((Object[]) elements[i]).length;
2039-
}
2040-
2041-
Object[] elts = new Object[totalLength];
2042-
int idx = 0;
2043-
for (int i = 0; i < length; i++) {
2044-
Object[] arr = (Object[]) elements[i];
2045-
int len = arr.length;
2046-
System.arraycopy(arr, 0, elts, idx, len);
2047-
idx += len;
2048-
}
2049-
return elts;
2001+
return rootNode.factory.createTuple(elements);
20502002
}
20512003
}
20522004

@@ -3621,7 +3573,7 @@ public static void doExceptional(VirtualFrame frame,
36213573
Object result = callExit.execute(frame, exit, contextManager, excType, pythonException, excTraceback);
36223574
if (!isTrue.execute(frame, inliningTarget, result)) {
36233575
if (exception instanceof PException pException) {
3624-
throw pException.getExceptionForReraise(rootNode.isPythonInternal());
3576+
throw pException.getExceptionForReraise(!rootNode.isInternal());
36253577
} else if (exception instanceof AbstractTruffleException ate) {
36263578
throw ate;
36273579
} else {
@@ -3723,7 +3675,7 @@ public static void doExceptional(VirtualFrame frame,
37233675
@Cached PRaiseNode.Lazy raiseNode) {
37243676
if (!isTrue.execute(frame, inliningTarget, result)) {
37253677
if (exception instanceof PException) {
3726-
throw ((PException) exception).getExceptionForReraise(rootNode.isPythonInternal());
3678+
throw ((PException) exception).getExceptionForReraise(!rootNode.isInternal());
37273679
} else if (exception instanceof AbstractTruffleException) {
37283680
throw (AbstractTruffleException) exception;
37293681
} else {

0 commit comments

Comments
 (0)