Skip to content

Commit f4e0578

Browse files
committed
[GR-57230] Add 'assert owns GIL' to all Java refcount operations.
PullRequest: graalpython/3468
2 parents a911775 + 24aa60c commit f4e0578

File tree

1 file changed

+8
-2
lines changed
  • graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/capi/transitions

1 file changed

+8
-2
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/capi/transitions/CApiTransitions.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -953,11 +953,11 @@ static long doGeneric(Node inliningTarget, PythonAbstractObjectNativeWrapper wra
953953
long taggedPointer = HandlePointerConverter.stubToPointer(stubPointer);
954954

955955
writeLongNode.write(stubPointer, CFields.PyObject__ob_refcnt, initialRefCount);
956-
writeObjectNode.write(stubPointer, CFields.PyObject__ob_type, type);
957956

958957
// TODO(fa): this should not require the GIL (GR-51314)
959958
boolean acquired = gil.acquire();
960959
try {
960+
writeObjectNode.write(stubPointer, CFields.PyObject__ob_type, type);
961961
int idx = nativeStubLookupReserve(handleContext);
962962
// We don't allow 'handleTableIndex == 0' to avoid that zeroed memory
963963
// accidentally maps to some valid object.
@@ -1214,6 +1214,7 @@ static Object doOther(Object obj,
12141214
@Cached InlinedConditionProfile isStrongProfile,
12151215
@CachedLibrary(limit = "3") InteropLibrary lib) {
12161216
CompilerAsserts.partialEvaluationConstant(needsTransfer);
1217+
assert PythonContext.get(inliningTarget).ownsGil();
12171218
pollReferenceQueue();
12181219
PythonNativeWrapper wrapper = getWrapper.execute(obj);
12191220

@@ -1222,7 +1223,6 @@ static Object doOther(Object obj,
12221223
return replacement;
12231224
}
12241225

1225-
assert PythonContext.get(inliningTarget).isNativeAccessAllowed();
12261226
assert obj != PNone.NO_VALUE;
12271227
if (!lib.isPointer(wrapper)) {
12281228
lib.toNative(wrapper);
@@ -1532,6 +1532,7 @@ Object doNonWrapper(long pointer, boolean stealing,
15321532
@Cached InlinedConditionProfile isHandleSpaceProfile,
15331533
@Cached InlinedExactClassProfile wrapperProfile) {
15341534

1535+
assert PythonContext.get(null).ownsGil();
15351536
CompilerAsserts.partialEvaluationConstant(stealing);
15361537
PythonNativeWrapper wrapper;
15371538

@@ -1588,6 +1589,7 @@ private static long addNativeRefCount(long pointer, long refCntDelta) {
15881589

15891590
private static long addNativeRefCount(long pointer, long refCntDelta, boolean ignoreIfDead) {
15901591
assert PythonContext.get(null).isNativeAccessAllowed();
1592+
assert PythonContext.get(null).ownsGil();
15911593
long refCount = UNSAFE.getLong(pointer + TP_REFCNT_OFFSET);
15921594
if (ignoreIfDead && refCount == 0) {
15931595
return 0;
@@ -1603,6 +1605,7 @@ private static long addNativeRefCount(long pointer, long refCntDelta, boolean ig
16031605

16041606
private static long subNativeRefCount(long pointer, long refCntDelta) {
16051607
assert PythonContext.get(null).isNativeAccessAllowed();
1608+
assert PythonContext.get(null).ownsGil();
16061609
long refCount = UNSAFE.getLong(pointer + TP_REFCNT_OFFSET);
16071610
assert (refCount & 0xFFFFFFFF00000000L) == 0 : String.format("suspicious refcnt value during managed adjustment for %016x (%d %016x - %d)\n", pointer, refCount, refCount, refCntDelta);
16081611
assert (refCount - refCntDelta) >= 0 : String.format("refcnt below zero during managed adjustment for %016x (%d %016x - %d)\n", pointer, refCount, refCount, refCntDelta);
@@ -1615,6 +1618,7 @@ private static long subNativeRefCount(long pointer, long refCntDelta) {
16151618

16161619
public static long readNativeRefCount(long pointer) {
16171620
assert PythonContext.get(null).isNativeAccessAllowed();
1621+
assert PythonContext.get(null).ownsGil();
16181622
long refCount = UNSAFE.getLong(pointer + TP_REFCNT_OFFSET);
16191623
assert refCount == IMMORTAL_REFCNT || (refCount & 0xFFFFFFFF00000000L) == 0 : String.format("suspicious refcnt value for %016x (%d %016x)\n", pointer, refCount, refCount);
16201624
if (LOGGER.isLoggable(Level.FINEST)) {
@@ -1624,6 +1628,8 @@ public static long readNativeRefCount(long pointer) {
16241628
}
16251629

16261630
public static void writeNativeRefCount(long pointer, long newValue) {
1631+
assert PythonContext.get(null).isNativeAccessAllowed();
1632+
assert PythonContext.get(null).ownsGil();
16271633
assert newValue > 0 : PythonUtils.formatJString("refcnt value to write below zero for %016x (%d %016x)\n", pointer, newValue, newValue);
16281634
if (LOGGER.isLoggable(Level.FINEST)) {
16291635
LOGGER.finest(PythonUtils.formatJString("writeNativeRefCount(%x, %d (%x))", pointer, newValue, newValue));

0 commit comments

Comments
 (0)