Skip to content

Commit 7dbf557

Browse files
committed
[GR-29633] Fix race conditions in PythonXYZClinicBuiltinNode classes.
PullRequest: graalpython/1704
2 parents 999eff2 + 4eb8fd8 commit 7dbf557

File tree

4 files changed

+86
-66
lines changed

4 files changed

+86
-66
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/function/builtins/PythonBinaryClinicBuiltinNode.java

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -51,41 +51,37 @@
5151

5252
@ClinicBuiltinBaseClass
5353
public abstract class PythonBinaryClinicBuiltinNode extends PythonBinaryBuiltinNode {
54-
private @Children ArgumentCastNode[] castNodes;
54+
private @Child ArgumentCastNode castNode0;
55+
private @Child ArgumentCastNode castNode1;
5556

5657
/**
5758
* Returns the provider of argument clinic logic. It should be singleton instance of a class
5859
* generated from the {@link ArgumentClinic} annotations.
5960
*/
6061
protected abstract ArgumentClinicProvider getArgumentClinic();
6162

62-
private Object cast(ArgumentClinicProvider clinic, VirtualFrame frame, int argIndex, Object value) {
63-
if (!clinic.hasCastNode(argIndex)) {
64-
return value;
65-
} else {
66-
return castWithNode(clinic, frame, argIndex, value);
63+
private Object cast0WithNode(ArgumentClinicProvider clinic, VirtualFrame frame, Object value) {
64+
if (castNode0 == null) {
65+
CompilerDirectives.transferToInterpreterAndInvalidate();
66+
castNode0 = insert(clinic.createCastNode(0, this));
6767
}
68+
return castNode0.execute(frame, value);
6869
}
6970

70-
protected Object castWithNode(ArgumentClinicProvider clinic, VirtualFrame frame, int argIndex, Object value) {
71-
ArgumentCastNode castNode;
72-
if (castNodes == null) {
73-
CompilerDirectives.transferToInterpreterAndInvalidate();
74-
castNodes = new ArgumentCastNode[2];
75-
}
76-
castNode = castNodes[argIndex];
77-
if (castNode == null) {
71+
private Object cast1WithNode(ArgumentClinicProvider clinic, VirtualFrame frame, Object value) {
72+
if (castNode1 == null) {
7873
CompilerDirectives.transferToInterpreterAndInvalidate();
79-
castNode = insert(clinic.createCastNode(argIndex, this));
80-
castNodes[argIndex] = castNode;
74+
castNode1 = insert(clinic.createCastNode(1, this));
8175
}
82-
return castNode.execute(frame, value);
76+
return castNode1.execute(frame, value);
8377
}
8478

8579
@Override
8680
public final Object call(VirtualFrame frame, Object arg, Object arg2) {
8781
ArgumentClinicProvider clinic = getArgumentClinic();
88-
return execute(frame, cast(clinic, frame, 0, arg), cast(clinic, frame, 1, arg2));
82+
Object val = clinic.hasCastNode(0) ? cast0WithNode(clinic, frame, arg) : arg;
83+
Object val2 = clinic.hasCastNode(1) ? cast1WithNode(clinic, frame, arg2) : arg2;
84+
return execute(frame, val, val2);
8985
}
9086

9187
@Override

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/function/builtins/PythonQuaternaryClinicBuiltinNode.java

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -49,40 +49,56 @@
4949

5050
@ClinicBuiltinBaseClass
5151
public abstract class PythonQuaternaryClinicBuiltinNode extends PythonQuaternaryBuiltinNode {
52-
@Children ArgumentCastNode[] castNodes;
52+
private @Child ArgumentCastNode castNode0;
53+
private @Child ArgumentCastNode castNode1;
54+
private @Child ArgumentCastNode castNode2;
55+
private @Child ArgumentCastNode castNode3;
5356

5457
/**
5558
* Returns the provider of argument clinic logic. It should be singleton instance of a class
5659
* generated from the {@link ArgumentClinic} annotations.
5760
*/
5861
protected abstract ArgumentClinicProvider getArgumentClinic();
5962

60-
private Object cast(ArgumentClinicProvider clinic, VirtualFrame frame, int argIndex, Object value) {
61-
if (!clinic.hasCastNode(argIndex)) {
62-
return value;
63-
} else {
64-
return castWithNode(clinic, frame, argIndex, value);
63+
private Object cast0WithNode(ArgumentClinicProvider clinic, VirtualFrame frame, Object value) {
64+
if (castNode0 == null) {
65+
CompilerDirectives.transferToInterpreterAndInvalidate();
66+
castNode0 = insert(clinic.createCastNode(0, this));
6567
}
68+
return castNode0.execute(frame, value);
6669
}
6770

68-
protected Object castWithNode(ArgumentClinicProvider clinic, VirtualFrame frame, int argIndex, Object value) {
69-
ArgumentCastNode castNode;
70-
if (castNodes == null) {
71+
private Object cast1WithNode(ArgumentClinicProvider clinic, VirtualFrame frame, Object value) {
72+
if (castNode1 == null) {
7173
CompilerDirectives.transferToInterpreterAndInvalidate();
72-
castNodes = new ArgumentCastNode[4];
74+
castNode1 = insert(clinic.createCastNode(1, this));
7375
}
74-
castNode = castNodes[argIndex];
75-
if (castNode == null) {
76+
return castNode1.execute(frame, value);
77+
}
78+
79+
private Object cast2WithNode(ArgumentClinicProvider clinic, VirtualFrame frame, Object value) {
80+
if (castNode2 == null) {
81+
CompilerDirectives.transferToInterpreterAndInvalidate();
82+
castNode2 = insert(clinic.createCastNode(2, this));
83+
}
84+
return castNode2.execute(frame, value);
85+
}
86+
87+
private Object cast3WithNode(ArgumentClinicProvider clinic, VirtualFrame frame, Object value) {
88+
if (castNode3 == null) {
7689
CompilerDirectives.transferToInterpreterAndInvalidate();
77-
castNode = insert(clinic.createCastNode(argIndex, this));
78-
castNodes[argIndex] = castNode;
90+
castNode3 = insert(clinic.createCastNode(3, this));
7991
}
80-
return castNode.execute(frame, value);
92+
return castNode3.execute(frame, value);
8193
}
8294

8395
@Override
84-
public Object call(VirtualFrame frame, Object arg, Object arg2, Object arg3, Object arg4) {
96+
public final Object call(VirtualFrame frame, Object arg, Object arg2, Object arg3, Object arg4) {
8597
ArgumentClinicProvider clinic = getArgumentClinic();
86-
return super.call(frame, cast(clinic, frame, 0, arg), cast(clinic, frame, 1, arg2), cast(clinic, frame, 2, arg3), cast(clinic, frame, 3, arg4));
98+
Object val = clinic.hasCastNode(0) ? cast0WithNode(clinic, frame, arg) : arg;
99+
Object val2 = clinic.hasCastNode(1) ? cast1WithNode(clinic, frame, arg2) : arg2;
100+
Object val3 = clinic.hasCastNode(2) ? cast2WithNode(clinic, frame, arg3) : arg3;
101+
Object val4 = clinic.hasCastNode(3) ? cast3WithNode(clinic, frame, arg4) : arg4;
102+
return super.call(frame, val, val2, val3, val4);
87103
}
88104
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/function/builtins/PythonTernaryClinicBuiltinNode.java

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -49,50 +49,58 @@
4949

5050
@ClinicBuiltinBaseClass
5151
public abstract class PythonTernaryClinicBuiltinNode extends PythonTernaryBuiltinNode {
52-
private @Children ArgumentCastNode[] castNodes;
52+
private @Child ArgumentCastNode castNode0;
53+
private @Child ArgumentCastNode castNode1;
54+
private @Child ArgumentCastNode castNode2;
5355

5456
/**
5557
* Returns the provider of argument clinic logic. It should be singleton instance of a class
5658
* generated from the {@link ArgumentClinic} annotations.
5759
*/
5860
protected abstract ArgumentClinicProvider getArgumentClinic();
5961

60-
private Object cast(ArgumentClinicProvider clinic, VirtualFrame frame, int argIndex, Object value) {
61-
if (!clinic.hasCastNode(argIndex)) {
62-
return value;
63-
} else {
64-
return castWithNode(clinic, frame, argIndex, value);
62+
private Object cast0WithNode(ArgumentClinicProvider clinic, VirtualFrame frame, Object value) {
63+
if (castNode0 == null) {
64+
CompilerDirectives.transferToInterpreterAndInvalidate();
65+
castNode0 = insert(clinic.createCastNode(0, this));
6566
}
67+
return castNode0.execute(frame, value);
6668
}
6769

68-
protected Object castWithNode(ArgumentClinicProvider clinic, VirtualFrame frame, int argIndex, Object value) {
69-
ArgumentCastNode castNode;
70-
if (castNodes == null) {
70+
private Object cast1WithNode(ArgumentClinicProvider clinic, VirtualFrame frame, Object value) {
71+
if (castNode1 == null) {
7172
CompilerDirectives.transferToInterpreterAndInvalidate();
72-
castNodes = new ArgumentCastNode[3];
73+
castNode1 = insert(clinic.createCastNode(1, this));
7374
}
74-
castNode = castNodes[argIndex];
75-
if (castNode == null) {
75+
return castNode1.execute(frame, value);
76+
}
77+
78+
private Object cast2WithNode(ArgumentClinicProvider clinic, VirtualFrame frame, Object value) {
79+
if (castNode2 == null) {
7680
CompilerDirectives.transferToInterpreterAndInvalidate();
77-
castNode = insert(clinic.createCastNode(argIndex, this));
78-
castNodes[argIndex] = castNode;
81+
castNode2 = insert(clinic.createCastNode(2, this));
7982
}
80-
return castNode.execute(frame, value);
83+
return castNode2.execute(frame, value);
8184
}
8285

8386
@Override
84-
public Object callWithInt(VirtualFrame frame, Object arg, int arg2, Object arg3) {
87+
public final Object callWithInt(VirtualFrame frame, Object arg, int arg2, Object arg3) {
8588
ArgumentClinicProvider clinic = getArgumentClinic();
8689
if (clinic.acceptsInt(1)) {
87-
return executeWithInt(frame, cast(clinic, frame, 0, arg), arg2, cast(clinic, frame, 2, arg3));
90+
Object val = clinic.hasCastNode(0) ? cast0WithNode(clinic, frame, arg) : arg;
91+
Object val3 = clinic.hasCastNode(2) ? cast2WithNode(clinic, frame, arg3) : arg3;
92+
return executeWithInt(frame, val, arg2, val3);
8893
} else {
8994
return call(frame, arg, arg2, arg3);
9095
}
9196
}
9297

9398
@Override
94-
public Object call(VirtualFrame frame, Object arg, Object arg2, Object arg3) {
99+
public final Object call(VirtualFrame frame, Object arg, Object arg2, Object arg3) {
95100
ArgumentClinicProvider clinic = getArgumentClinic();
96-
return execute(frame, cast(clinic, frame, 0, arg), cast(clinic, frame, 1, arg2), cast(clinic, frame, 2, arg3));
101+
Object val = clinic.hasCastNode(0) ? cast0WithNode(clinic, frame, arg) : arg;
102+
Object val2 = clinic.hasCastNode(1) ? cast1WithNode(clinic, frame, arg2) : arg2;
103+
Object val3 = clinic.hasCastNode(2) ? cast2WithNode(clinic, frame, arg3) : arg3;
104+
return execute(frame, val, val2, val3);
97105
}
98106
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/function/builtins/PythonUnaryClinicBuiltinNode.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,24 +59,24 @@ public abstract class PythonUnaryClinicBuiltinNode extends PythonUnaryBuiltinNod
5959
*/
6060
protected abstract ArgumentClinicProvider getArgumentClinic();
6161

62-
private Object cast(VirtualFrame frame, int argIndex, Object value) {
62+
private Object cast(VirtualFrame frame, Object value) {
6363
// no point in using argument clinic if the only argument does not have a cast node
6464
ArgumentClinicProvider clinic = getArgumentClinic();
65-
assert clinic.hasCastNode(argIndex);
65+
assert clinic.hasCastNode(0);
6666
if (castNode == null) {
6767
CompilerDirectives.transferToInterpreterAndInvalidate();
68-
castNode = insert(clinic.createCastNode(argIndex, this));
68+
castNode = insert(clinic.createCastNode(0, this));
6969
}
7070
return castNode.execute(frame, value);
7171
}
7272

7373
@Override
74-
public Object call(VirtualFrame frame, Object arg) {
75-
return execute(frame, cast(frame, 0, arg));
74+
public final Object call(VirtualFrame frame, Object arg) {
75+
return execute(frame, cast(frame, arg));
7676
}
7777

7878
@Override
79-
public boolean callBool(VirtualFrame frame, boolean arg) throws UnexpectedResultException {
79+
public final boolean callBool(VirtualFrame frame, boolean arg) throws UnexpectedResultException {
8080
if (getArgumentClinic().acceptsBoolean(0)) {
8181
return executeBool(frame, arg);
8282
} else {
@@ -85,7 +85,7 @@ public boolean callBool(VirtualFrame frame, boolean arg) throws UnexpectedResult
8585
}
8686

8787
@Override
88-
public int callInt(VirtualFrame frame, int arg) throws UnexpectedResultException {
88+
public final int callInt(VirtualFrame frame, int arg) throws UnexpectedResultException {
8989
if (getArgumentClinic().acceptsInt(0)) {
9090
return executeInt(frame, arg);
9191
} else {
@@ -94,7 +94,7 @@ public int callInt(VirtualFrame frame, int arg) throws UnexpectedResultException
9494
}
9595

9696
@Override
97-
public long callLong(VirtualFrame frame, long arg) throws UnexpectedResultException {
97+
public final long callLong(VirtualFrame frame, long arg) throws UnexpectedResultException {
9898
if (getArgumentClinic().acceptsLong(0)) {
9999
return executeLong(frame, arg);
100100
} else {
@@ -103,7 +103,7 @@ public long callLong(VirtualFrame frame, long arg) throws UnexpectedResultExcept
103103
}
104104

105105
@Override
106-
public double callDouble(VirtualFrame frame, double arg) throws UnexpectedResultException {
106+
public final double callDouble(VirtualFrame frame, double arg) throws UnexpectedResultException {
107107
if (getArgumentClinic().acceptsDouble(0)) {
108108
return executeDouble(frame, arg);
109109
} else {
@@ -112,7 +112,7 @@ public double callDouble(VirtualFrame frame, double arg) throws UnexpectedResult
112112
}
113113

114114
@Override
115-
public boolean callBool(VirtualFrame frame, int arg) throws UnexpectedResultException {
115+
public final boolean callBool(VirtualFrame frame, int arg) throws UnexpectedResultException {
116116
if (getArgumentClinic().acceptsInt(0)) {
117117
return executeBool(frame, arg);
118118
} else {
@@ -121,7 +121,7 @@ public boolean callBool(VirtualFrame frame, int arg) throws UnexpectedResultExce
121121
}
122122

123123
@Override
124-
public boolean callBool(VirtualFrame frame, long arg) throws UnexpectedResultException {
124+
public final boolean callBool(VirtualFrame frame, long arg) throws UnexpectedResultException {
125125
if (getArgumentClinic().acceptsLong(0)) {
126126
return executeBool(frame, arg);
127127
} else {
@@ -130,7 +130,7 @@ public boolean callBool(VirtualFrame frame, long arg) throws UnexpectedResultExc
130130
}
131131

132132
@Override
133-
public boolean callBool(VirtualFrame frame, double arg) throws UnexpectedResultException {
133+
public final boolean callBool(VirtualFrame frame, double arg) throws UnexpectedResultException {
134134
if (getArgumentClinic().acceptsDouble(0)) {
135135
return executeBool(frame, arg);
136136
} else {

0 commit comments

Comments
 (0)