Skip to content

Commit 48c2122

Browse files
committed
Use _PyObject_IsFreed and _PyObject_Dump in native for native objects
1 parent de4c7d1 commit 48c2122

File tree

5 files changed

+26
-22
lines changed

5 files changed

+26
-22
lines changed

graalpython/com.oracle.graal.python.cext/src/gcmodule.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,16 @@ call_traverse(traverseproc traverse, PyObject *op, visitproc visit, void *arg)
4949
Py_TYPE((op))->tp_name);
5050
return 0;
5151
} else {
52+
if (_PyObject_IsFreed(op)) {
53+
PyTruffle_Log(PY_TRUFFLE_LOG_INFO,
54+
"we tried to call tp_traverse on a freed object at %p (ctx %p)!",
55+
op, arg);
56+
return 0;
57+
}
5258
if (_PyObject_IsFreed((PyObject *)Py_TYPE(op))) {
53-
if (_PyObject_IsFreed(op)) {
54-
PyTruffle_Log(PY_TRUFFLE_LOG_INFO,
55-
"we tried to call tp_traverse on a freed object at %p (ctx %p)!",
56-
op, arg);
57-
} else {
58-
PyTruffle_Log(PY_TRUFFLE_LOG_INFO,
59-
"we tried to call tp_traverse on an object at %p with a freed type at %p (ctx %p)!",
60-
op, Py_TYPE(op), arg);
61-
}
59+
PyTruffle_Log(PY_TRUFFLE_LOG_INFO,
60+
"we tried to call tp_traverse on an object at %p with a freed type at %p (ctx %p)!",
61+
op, Py_TYPE(op), arg);
6262
return 0;
6363
}
6464
return traverse(op, (visitproc)visit, arg);

graalpython/com.oracle.graal.python.cext/src/object.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,6 @@ PyObject_Print(PyObject *op, FILE *fp, int flags)
372372
return ret;
373373
}
374374

375-
#if 0 // GraalPy change
376375
/* For debugging convenience. Set a breakpoint here and call it from your DLL */
377376
void
378377
_Py_BreakPoint(void)
@@ -389,9 +388,14 @@ _Py_BreakPoint(void)
389388
int
390389
_PyObject_IsFreed(PyObject *op)
391390
{
391+
if (points_to_py_handle_space(op)) {
392+
return Graal_PyTruffleObject_IsFreed(op);
393+
}
394+
#if 0 // GraalPy change
392395
if (_PyMem_IsPtrFreed(op) || _PyMem_IsPtrFreed(Py_TYPE(op))) {
393396
return 1;
394397
}
398+
#endif
395399
/* ignore op->ob_ref: its value can have be modified
396400
by Py_INCREF() and Py_DECREF(). */
397401
#ifdef Py_TRACE_REFS
@@ -405,11 +409,14 @@ _PyObject_IsFreed(PyObject *op)
405409
return 0;
406410
}
407411

408-
409412
/* For debugging convenience. See Misc/gdbinit for some useful gdb hooks */
410413
void
411414
_PyObject_Dump(PyObject* op)
412415
{
416+
if (points_to_py_handle_space(op)) {
417+
Graal_PyTruffleObject_Dump(op);
418+
return;
419+
}
413420
if (_PyObject_IsFreed(op)) {
414421
/* It seems like the object memory has been freed:
415422
don't access it to prevent a segmentation fault. */
@@ -448,6 +455,7 @@ _PyObject_Dump(PyObject* op)
448455
fflush(stderr);
449456
}
450457

458+
#if 0 // GraalPy change
451459
PyObject *
452460
PyObject_Repr(PyObject *v)
453461
{

graalpython/com.oracle.graal.python.test/src/tests/unittest_tags/test_capi.txt

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,6 @@ test.test_capi.test_misc.CAPITest.test_subprocess_fork_exec @ darwin-arm64,darwi
9797
test.test_capi.test_misc.CAPITest.test_sys_getobject @ darwin-arm64,darwin-x86_64,linux-aarch64,linux-x86_64
9898
# Segfaults most of the time
9999
!test.test_capi.test_misc.CAPITest.test_trashcan_subclass
100-
test.test_capi.test_misc.PyMemDebugTests.test_pyobject_forbidden_bytes_is_freed @ darwin-arm64,darwin-x86_64,linux-aarch64,linux-x86_64
101-
test.test_capi.test_misc.PyMemDebugTests.test_pyobject_null_is_freed @ darwin-arm64,darwin-x86_64,linux-aarch64,linux-x86_64
102-
test.test_capi.test_misc.PyMemDebugTests.test_pyobject_uninitialized_is_freed @ darwin-arm64,darwin-x86_64,linux-aarch64,linux-x86_64
103-
test.test_capi.test_misc.PyMemMallocDebugTests.test_pyobject_forbidden_bytes_is_freed @ darwin-arm64,darwin-x86_64,linux-aarch64,linux-x86_64
104-
test.test_capi.test_misc.PyMemMallocDebugTests.test_pyobject_null_is_freed @ darwin-arm64,darwin-x86_64,linux-aarch64,linux-x86_64
105-
test.test_capi.test_misc.PyMemMallocDebugTests.test_pyobject_uninitialized_is_freed @ darwin-arm64,darwin-x86_64,linux-aarch64,linux-x86_64
106100
test.test_capi.test_misc.Test_FrameAPI.test_frame_fback_api @ darwin-arm64,darwin-x86_64,linux-aarch64,linux-x86_64
107101
test.test_capi.test_misc.Test_FrameAPI.test_frame_getters @ darwin-arm64,darwin-x86_64,linux-aarch64,linux-x86_64
108102
test.test_capi.test_misc.Test_ModuleStateAccess.test_get_module_bad_def @ darwin-arm64,darwin-x86_64,linux-aarch64,linux-x86_64

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/cext/PythonCextObjectBuiltins.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -600,17 +600,17 @@ static PNone set(PSequence obj, long size,
600600
}
601601
}
602602

603-
@CApiBuiltin(ret = Int, args = {PyObjectRawPointer}, call = Direct)
604-
abstract static class _PyObject_IsFreed extends CApiUnaryBuiltinNode {
603+
@CApiBuiltin(ret = Int, args = {PyObjectRawPointer}, call = Ignored)
604+
abstract static class _PyTruffleObject_IsFreed extends CApiUnaryBuiltinNode {
605605
@Specialization
606606
int doGeneric(Object pointer,
607607
@Cached ToPythonWrapperNode toPythonWrapperNode) {
608608
return toPythonWrapperNode.executeWrapper(pointer, false) == null ? 1 : 0;
609609
}
610610
}
611611

612-
@CApiBuiltin(ret = Void, args = {PyObjectWrapper}, call = Direct)
613-
abstract static class _PyObject_Dump extends CApiUnaryBuiltinNode {
612+
@CApiBuiltin(ret = Void, args = {PyObjectWrapper}, call = Ignored)
613+
abstract static class _PyTruffleObject_Dump extends CApiUnaryBuiltinNode {
614614

615615
@Specialization
616616
@TruffleBoundary

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -606,13 +606,15 @@ public final class CApiFunction {
606606
@CApiBuiltin(name = "_PyObject_CallMethodIdObjArgs", ret = PyObject, args = {PyObject, _PY_IDENTIFIER_PTR, VARARGS}, call = CImpl)
607607
@CApiBuiltin(name = "_PyObject_CallMethodIdObjArgs", ret = PyObject, args = {PyObject, _PY_IDENTIFIER_PTR, VARARGS}, call = CImpl)
608608
@CApiBuiltin(name = "_PyObject_CallMethod_SizeT", ret = PyObject, args = {PyObject, ConstCharPtrAsTruffleString, ConstCharPtrAsTruffleString, VARARGS}, call = CImpl)
609+
@CApiBuiltin(name = "_PyObject_Dump", ret = Void, args = {PyObject}, call = CImpl)
609610
@CApiBuiltin(name = "_PyObject_FastCall", ret = PyObject, args = {PyObject, PyObjectConstPtr, Py_ssize_t}, call = CImpl)
610611
@CApiBuiltin(name = "_PyObject_GC_New", ret = PyObject, args = {PyTypeObject}, call = CImpl)
611612
@CApiBuiltin(name = "_PyObject_GC_NewVar", ret = PyVarObject, args = {PyTypeObject, Py_ssize_t}, call = CImpl)
612613
@CApiBuiltin(name = "_PyObject_GC_Resize", ret = PyVarObject, args = {PyVarObject, Py_ssize_t}, call = CImpl)
613614
@CApiBuiltin(name = "_PyObject_GetAttrId", ret = PyObject, args = {PyObject, _PY_IDENTIFIER_PTR}, call = CImpl)
614615
@CApiBuiltin(name = "_PyObject_GetDictPtr", ret = PyObjectPtr, args = {PyObject}, call = CImpl)
615616
@CApiBuiltin(name = "_PyObject_GetMethod", ret = Int, args = {PyObject, PyObject, PyObjectPtr}, call = CImpl)
617+
@CApiBuiltin(name = "_PyObject_IsFreed", ret = Int, args = {PyObject}, call = CImpl)
616618
@CApiBuiltin(name = "_PyObject_LookupAttr", ret = Int, args = {PyObject, PyObject, PyObjectPtr}, call = CImpl)
617619
@CApiBuiltin(name = "_PyObject_LookupAttrId", ret = Int, args = {PyObject, _PY_IDENTIFIER_PTR, PyObjectPtr}, call = CImpl)
618620
@CApiBuiltin(name = "_PyObject_New", ret = PyObject, args = {PyTypeObject}, call = CImpl)
@@ -654,6 +656,7 @@ public final class CApiFunction {
654656
@CApiBuiltin(name = "_PyUnicode_ToUpperFull", ret = Int, args = {PY_UCS4, PY_UCS4_PTR}, call = CImpl)
655657
@CApiBuiltin(name = "_PyUnicode_ToUppercase", ret = PY_UCS4, args = {PY_UCS4}, call = CImpl)
656658
@CApiBuiltin(name = "_Py_BuildValue_SizeT", ret = PyObject, args = {ConstCharPtrAsTruffleString, VARARGS}, call = CImpl)
659+
@CApiBuiltin(name = "_Py_BreakPoint", ret = Void, args = {}, call = CImpl)
657660
@CApiBuiltin(name = "_Py_CheckFunctionResult", ret = PyObject, args = {PyThreadState, PyObject, PyObject, ConstCharPtrAsTruffleString}, call = CImpl)
658661
@CApiBuiltin(name = "_Py_Dealloc", ret = Void, args = {PyObject}, call = CImpl)
659662
@CApiBuiltin(name = "_Py_DecRef", ret = Void, args = {PyObject}, call = CImpl)
@@ -1214,7 +1217,6 @@ public final class CApiFunction {
12141217
@CApiBuiltin(name = "_PyUnicode_XStrip", ret = PyObject, args = {PyObject, Int, PyObject}, call = NotImplemented)
12151218
@CApiBuiltin(name = "_PyWeakref_ClearRef", ret = Void, args = {PYWEAKREFERENCE_PTR}, call = NotImplemented)
12161219
@CApiBuiltin(name = "_PyWeakref_GetWeakrefCount", ret = Py_ssize_t, args = {PYWEAKREFERENCE_PTR}, call = NotImplemented)
1217-
@CApiBuiltin(name = "_Py_BreakPoint", ret = Void, args = {}, call = NotImplemented)
12181220
@CApiBuiltin(name = "_Py_CoerceLegacyLocale", ret = Int, args = {Int}, call = NotImplemented)
12191221
@CApiBuiltin(name = "_Py_DisplaySourceLine", ret = Int, args = {PyObject, PyObject, Int, Int, INT_LIST, PyObjectPtr}, call = NotImplemented)
12201222
@CApiBuiltin(name = "_Py_FatalErrorFormat", ret = Void, args = {ConstCharPtr, ConstCharPtr, VARARGS}, call = NotImplemented)

0 commit comments

Comments
 (0)