Skip to content

Commit 003723f

Browse files
committed
Fix unbounded recursive nodes creation in GetItemSizeNode & make it return unboxed long
1 parent 6eb6e3f commit 003723f

File tree

1 file changed

+30
-12
lines changed
  • graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/type

1 file changed

+30
-12
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/type/TypeNodes.java

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@
105105
import com.oracle.graal.python.builtins.objects.type.TypeNodesFactory.GetBaseClassNodeGen;
106106
import com.oracle.graal.python.builtins.objects.type.TypeNodesFactory.GetBaseClassesNodeGen;
107107
import com.oracle.graal.python.builtins.objects.type.TypeNodesFactory.GetInstanceShapeNodeGen;
108+
import com.oracle.graal.python.builtins.objects.type.TypeNodesFactory.GetItemsizeNodeGen;
108109
import com.oracle.graal.python.builtins.objects.type.TypeNodesFactory.GetMroStorageNodeGen;
109110
import com.oracle.graal.python.builtins.objects.type.TypeNodesFactory.GetNameNodeGen;
110111
import com.oracle.graal.python.builtins.objects.type.TypeNodesFactory.GetSolidBaseNodeGen;
@@ -128,6 +129,7 @@
128129
import com.oracle.graal.python.nodes.truffle.PythonTypes;
129130
import com.oracle.graal.python.nodes.util.CastToJavaStringNode;
130131
import com.oracle.graal.python.runtime.PythonContext;
132+
import com.oracle.graal.python.runtime.PythonOptions;
131133
import com.oracle.graal.python.runtime.exception.PException;
132134
import com.oracle.graal.python.runtime.sequence.PSequence;
133135
import com.oracle.graal.python.runtime.sequence.storage.MroSequenceStorage;
@@ -1498,26 +1500,33 @@ public static GetInstanceShape create() {
14981500
}
14991501

15001502
@GenerateUncached
1503+
@ImportStatic(PythonOptions.class)
15011504
public abstract static class GetItemsizeNode extends Node {
1505+
// We cannot easily use options here
1506+
static final int MAX_RECURSION_DEPTH = 4;
15021507

1503-
public abstract Long execute(Object cls);
1508+
public final long execute(Object cls) {
1509+
return execute(cls, 0);
1510+
}
1511+
1512+
public abstract long execute(Object cls, int depth);
15041513

15051514
@Specialization
1506-
static Long getItemsizeType(PythonBuiltinClassType cls) {
1515+
static long getItemsizeType(PythonBuiltinClassType cls, @SuppressWarnings("unused") int depth) {
15071516
return getBuiltinTypeItemsize(cls);
15081517
}
15091518

15101519
@Specialization
1511-
static Long getItemsizeManaged(PythonBuiltinClass cls) {
1512-
return getItemsizeType(cls.getType());
1520+
static long getItemsizeManaged(PythonBuiltinClass cls, @SuppressWarnings("unused") int depth) {
1521+
return getItemsizeType(cls.getType(), depth);
15131522
}
15141523

1515-
@Specialization
1516-
static Long getItemsizeManaged(PythonClass cls,
1517-
@Cached ConditionProfile hasValueProfile,
1518-
@Cached ReadAttributeFromObjectNode readNode,
1519-
@Cached WriteAttributeToObjectNode writeNode,
1520-
@Cached GetBaseClassNode getBaseNode,
1524+
@Specialization(guards = "depth < MAX_RECURSION_DEPTH")
1525+
static long getItemsizeManagedRecursiveNode(PythonClass cls, @SuppressWarnings("unused") int depth,
1526+
@Shared("hasVal") @Cached ConditionProfile hasValueProfile,
1527+
@Shared("read") @Cached ReadAttributeFromObjectNode readNode,
1528+
@Shared("write") @Cached WriteAttributeToObjectNode writeNode,
1529+
@Shared("getBase") @Cached GetBaseClassNode getBaseNode,
15211530
@Cached GetItemsizeNode baseItemsizeNode) {
15221531
Object itemsize = readNode.execute(cls, TYPE_ITEMSIZE);
15231532
if (hasValueProfile.profile(itemsize != PNone.NO_VALUE)) {
@@ -1526,13 +1535,22 @@ static Long getItemsizeManaged(PythonClass cls,
15261535

15271536
Object base = getBaseNode.execute(cls);
15281537
assert base != null;
1529-
itemsize = baseItemsizeNode.execute(base);
1538+
itemsize = baseItemsizeNode.execute(base, depth + 1);
15301539
writeNode.execute(cls, TYPE_ITEMSIZE, itemsize);
15311540
return (long) itemsize;
15321541
}
15331542

1543+
@Specialization(guards = "depth >= MAX_RECURSION_DEPTH")
1544+
static long getItemsizeManagedRecursiveCall(PythonClass cls, @SuppressWarnings("unused") int depth,
1545+
@Shared("hasVal") @Cached ConditionProfile hasValueProfile,
1546+
@Shared("read") @Cached ReadAttributeFromObjectNode readNode,
1547+
@Shared("write") @Cached WriteAttributeToObjectNode writeNode,
1548+
@Shared("getBase") @Cached GetBaseClassNode getBaseNode) {
1549+
return getItemsizeManagedRecursiveNode(cls, depth, hasValueProfile, readNode, writeNode, getBaseNode, GetItemsizeNodeGen.getUncached());
1550+
}
1551+
15341552
@Specialization
1535-
static Long getNative(PythonNativeClass cls,
1553+
static long getNative(PythonNativeClass cls, @SuppressWarnings("unused") int depth,
15361554
@Cached GetTypeMemberNode getTpDictoffsetNode) {
15371555
return (long) getTpDictoffsetNode.execute(cls, NativeMember.TP_ITEMSIZE);
15381556
}

0 commit comments

Comments
 (0)