Skip to content

Commit 9967721

Browse files
committed
Simplify PyTraceMalloc_Track
1 parent 82ce0ec commit 9967721

File tree

7 files changed

+84
-291
lines changed

7 files changed

+84
-291
lines changed

graalpython/com.oracle.graal.python.cext/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ set(SRC_FILES ${CAPI_SRC}/codecs.c ${CAPI_SRC}/setobject.c ${CAPI_SRC}/compile.c
118118
${CAPI_SRC}/fileobject.c ${CAPI_SRC}/pystrcmp.c ${CAPI_SRC}/getversion.c
119119
${CAPI_SRC}/genobject.c ${CAPI_SRC}/methodobject.c ${CAPI_SRC}/boolobject.c ${CAPI_SRC}/pylifecycle.c
120120
${CAPI_SRC}/errors.c ${CAPI_SRC}/signals.c ${CAPI_SRC}/datetime.c ${CAPI_SRC}/call.c
121-
${CAPI_SRC}/getargs.c
121+
${CAPI_SRC}/getargs.c ${CAPI_SRC}/tracemalloc.c
122122
)
123123

124124
file(GLOB_RECURSE ACTUAL_SRC_FILES
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* The Universal Permissive License (UPL), Version 1.0
6+
*
7+
* Subject to the condition set forth below, permission is hereby granted to any
8+
* person obtaining a copy of this software, associated documentation and/or
9+
* data (collectively the "Software"), free of charge and under any and all
10+
* copyright rights in the Software, and any and all patent rights owned or
11+
* freely licensable by each licensor hereunder covering either (i) the
12+
* unmodified Software as contributed to or provided by such licensor, or (ii)
13+
* the Larger Works (as defined below), to deal in both
14+
*
15+
* (a) the Software, and
16+
*
17+
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
18+
* one is included with the Software each a "Larger Work" to which the Software
19+
* is contributed by such licensors),
20+
*
21+
* without restriction, including without limitation the rights to copy, create
22+
* derivative works of, display, perform, and distribute the Software and make,
23+
* use, sell, offer for sale, import, export, have made, and have sold the
24+
* Software and the Larger Work(s), and to sublicense the foregoing rights on
25+
* either these or other terms.
26+
*
27+
* This license is subject to the following condition:
28+
*
29+
* The above copyright notice and either this complete permission notice or at a
30+
* minimum a reference to the UPL must be included in all copies or substantial
31+
* portions of the Software.
32+
*
33+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
39+
* SOFTWARE.
40+
*/
41+
#include "capi.h"
42+
43+
int
44+
PyTraceMalloc_Track(unsigned int domain, uintptr_t ptr,
45+
size_t size)
46+
{
47+
if (PyTruffle_Trace_Memory()) {
48+
GraalPyTruffleTraceMalloc_Track(domain, ptr, size);
49+
}
50+
51+
return 0;
52+
}
53+
54+
55+
int
56+
PyTraceMalloc_Untrack(unsigned int domain, uintptr_t ptr)
57+
{
58+
if (PyTruffle_Trace_Memory()) {
59+
GraalPyTruffleTraceMalloc_Untrack(domain, ptr);
60+
}
61+
62+
return 0;
63+
}

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

Lines changed: 16 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -186,11 +186,9 @@
186186
import com.oracle.graal.python.nodes.statement.AbstractImportNode;
187187
import com.oracle.graal.python.nodes.util.CastToJavaIntExactNode;
188188
import com.oracle.graal.python.runtime.GilNode;
189-
import com.oracle.graal.python.runtime.IndirectCallData;
190189
import com.oracle.graal.python.runtime.PosixSupportLibrary;
191190
import com.oracle.graal.python.runtime.PosixSupportLibrary.PosixException;
192191
import com.oracle.graal.python.runtime.PythonContext;
193-
import com.oracle.graal.python.runtime.PythonContext.GetThreadStateNode;
194192
import com.oracle.graal.python.runtime.PythonOptions;
195193
import com.oracle.graal.python.runtime.exception.ExceptionUtils;
196194
import com.oracle.graal.python.runtime.exception.PException;
@@ -1315,70 +1313,30 @@ static PNone doObject(Object ptr,
13151313
}
13161314
}
13171315

1318-
@CApiBuiltin(ret = Int, args = {UNSIGNED_INT, UINTPTR_T, SIZE_T}, call = Direct)
1319-
@ImportStatic(CApiGuards.class)
1320-
abstract static class PyTraceMalloc_Track extends CApiTernaryBuiltinNode {
1321-
private static final TruffleLogger LOGGER = CApiContext.getLogger(PyTraceMalloc_Track.class);
1322-
1323-
@Specialization(guards = {"isSingleContext()", "domain == cachedDomain"}, limit = "3")
1324-
static int doCachedDomainIdx(@SuppressWarnings("unused") int domain, long ptrVal, long size,
1325-
@Bind("this") Node inliningTarget,
1326-
@Shared @Cached("createFor(this)") IndirectCallData indirectCallData,
1327-
@Shared @Cached GetThreadStateNode getThreadStateNode,
1328-
@Cached("domain") @SuppressWarnings("unused") long cachedDomain,
1329-
@Cached("lookupDomain(inliningTarget, domain)") int cachedDomainIdx) {
1316+
@CApiBuiltin(ret = Void, args = {UNSIGNED_INT, UINTPTR_T, SIZE_T}, call = Ignored)
1317+
abstract static class PyTruffleTraceMalloc_Track extends CApiTernaryBuiltinNode {
1318+
private static final TruffleLogger LOGGER = CApiContext.getLogger(PyTruffleTraceMalloc_Track.class);
13301319

1320+
@Specialization
1321+
@TruffleBoundary
1322+
static Object doCachedDomainIdx(int domain, long ptrVal, long size) {
13311323
// this will also be called if the allocation failed
13321324
if (ptrVal != 0) {
1333-
CApiContext cApiContext = getCApiContext(inliningTarget);
1334-
cApiContext.getTraceMallocDomain(cachedDomainIdx).track(ptrVal, size);
1335-
cApiContext.increaseMemoryPressure(null, inliningTarget, getThreadStateNode, indirectCallData, size);
1336-
if (LOGGER.isLoggable(Level.FINE)) {
1337-
LOGGER.fine(PythonUtils.formatJString("Tracking memory (size: %d): %s", size, CApiContext.asHex(ptrVal)));
1338-
}
1325+
LOGGER.fine(() -> PythonUtils.formatJString("Tracking memory (domain: %d, size: %d): %s", domain, size, CApiContext.asHex(ptrVal)));
13391326
}
1340-
return 0;
1341-
}
1342-
1343-
@Specialization(replaces = "doCachedDomainIdx")
1344-
static int doGeneric(int domain, long ptrVal, long size,
1345-
@Bind("this") Node inliningTarget,
1346-
@Shared @Cached("createFor(this)") IndirectCallData indirectCallData,
1347-
@Shared @Cached GetThreadStateNode getThreadStateNode) {
1348-
return doCachedDomainIdx(domain, ptrVal, size, inliningTarget, indirectCallData, getThreadStateNode, domain, lookupDomain(inliningTarget, domain));
1349-
}
1350-
1351-
static int lookupDomain(Node inliningTarget, int domain) {
1352-
return getCApiContext(inliningTarget).findOrCreateTraceMallocDomain(domain);
1327+
return PNone.NO_VALUE;
13531328
}
13541329
}
13551330

1356-
@CApiBuiltin(ret = Int, args = {UNSIGNED_INT, UINTPTR_T}, call = Direct)
1357-
@ImportStatic(CApiGuards.class)
1358-
abstract static class PyTraceMalloc_Untrack extends CApiBinaryBuiltinNode {
1359-
private static final TruffleLogger LOGGER = CApiContext.getLogger(PyTraceMalloc_Untrack.class);
1360-
1361-
@Specialization(guards = {"isSingleContext()", "domain == cachedDomain"}, limit = "3")
1362-
int doCachedDomainIdx(@SuppressWarnings("unused") int domain, long ptrVal,
1363-
@Cached("domain") @SuppressWarnings("unused") long cachedDomain,
1364-
@Cached("lookupDomain(domain)") int cachedDomainIdx) {
1365-
1366-
CApiContext cApiContext = getCApiContext();
1367-
long trackedMemorySize = cApiContext.getTraceMallocDomain(cachedDomainIdx).untrack(ptrVal);
1368-
cApiContext.reduceMemoryPressure(trackedMemorySize);
1369-
if (LOGGER.isLoggable(Level.FINE)) {
1370-
LOGGER.fine(PythonUtils.formatJString("Untracking memory (size: %d): %s", trackedMemorySize, CApiContext.asHex(ptrVal)));
1371-
}
1372-
return 0;
1373-
}
1374-
1375-
@Specialization(replaces = "doCachedDomainIdx")
1376-
int doGeneric(int domain, long ptrVal) {
1377-
return doCachedDomainIdx(domain, ptrVal, domain, lookupDomain(domain));
1378-
}
1331+
@CApiBuiltin(ret = Void, args = {UNSIGNED_INT, UINTPTR_T}, call = Ignored)
1332+
abstract static class PyTruffleTraceMalloc_Untrack extends CApiBinaryBuiltinNode {
1333+
private static final TruffleLogger LOGGER = CApiContext.getLogger(PyTruffleTraceMalloc_Untrack.class);
13791334

1380-
int lookupDomain(int domain) {
1381-
return getCApiContext().findOrCreateTraceMallocDomain(domain);
1335+
@Specialization
1336+
@TruffleBoundary
1337+
Object doCachedDomainIdx(int domain, long ptrVal) {
1338+
LOGGER.fine(() -> PythonUtils.formatJString("Untracking memory (domain: %d): %s", domain, CApiContext.asHex(ptrVal)));
1339+
return PNone.NO_VALUE;
13821340
}
13831341
}
13841342

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

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@
8282
import com.oracle.graal.python.builtins.objects.PNotImplemented;
8383
import com.oracle.graal.python.builtins.objects.bytes.BytesUtils;
8484
import com.oracle.graal.python.builtins.objects.bytes.PBytesLike;
85-
import com.oracle.graal.python.builtins.objects.cext.capi.CApiContext;
8685
import com.oracle.graal.python.builtins.objects.cext.capi.CApiGuards;
8786
import com.oracle.graal.python.builtins.objects.cext.capi.CExtNodes.ResolvePointerNode;
8887
import com.oracle.graal.python.builtins.objects.cext.capi.PythonNativeWrapper;
@@ -133,7 +132,6 @@
133132
import com.oracle.graal.python.nodes.util.CastToTruffleStringNode;
134133
import com.oracle.graal.python.runtime.PosixSupportLibrary;
135134
import com.oracle.graal.python.runtime.PythonContext;
136-
import com.oracle.graal.python.runtime.PythonOptions;
137135
import com.oracle.graal.python.runtime.exception.PException;
138136
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
139137
import com.oracle.graal.python.runtime.sequence.PSequence;
@@ -614,8 +612,6 @@ int doGeneric(Object ptrObject,
614612
@Cached CStructAccess.ReadI64Node readI64) {
615613
PythonContext context = getContext();
616614
PrintWriter stderr = new PrintWriter(context.getStandardErr());
617-
CApiContext cApiContext = context.getCApiContext();
618-
InteropLibrary lib = InteropLibrary.getUncached(ptrObject);
619615

620616
// There are three cases we need to distinguish:
621617
// 1) The pointer object is a native pointer and is NOT a handle
@@ -624,21 +620,6 @@ int doGeneric(Object ptrObject,
624620

625621
boolean isWrapper = CApiGuards.isNativeWrapper(ptrObject);
626622

627-
boolean pointsToHandleSpace = !isWrapper; // TODO: use CApiTransitions here
628-
boolean isValidHandle = pointsToHandleSpace;
629-
630-
/*
631-
* If the pointer points to the handle space but it's not a valid handle or if we do
632-
* memory tracing and we know that the pointer is not allocated (was free'd), we assumed
633-
* it's a use-after-free.
634-
*/
635-
boolean traceNativeMemory = context.getOption(PythonOptions.TraceNativeMemory);
636-
if (pointsToHandleSpace && !isValidHandle || traceNativeMemory && !isWrapper && !cApiContext.isAllocated(ptrObject)) {
637-
stderr.println(PythonUtils.formatJString("<object at %s is freed>", CApiContext.asPointer(ptrObject, lib)));
638-
stderr.flush();
639-
return 0;
640-
}
641-
642623
/*
643624
* At this point we don't know if the pointer is invalid, so we try to resolve it to an
644625
* object.

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@
105105
import com.oracle.graal.python.nodes.classes.IsSubtypeNode;
106106
import com.oracle.graal.python.nodes.object.GetDictIfExistsNode;
107107
import com.oracle.graal.python.nodes.util.CastToTruffleStringNode;
108-
import com.oracle.graal.python.runtime.PythonContext;
109108
import com.oracle.graal.python.runtime.PythonOptions;
110109
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
111110
import com.oracle.graal.python.runtime.sequence.storage.MroSequenceStorage;
@@ -297,9 +296,7 @@ int trace(Object ptr, Object classNameObj,
297296
} else {
298297
className = null;
299298
}
300-
PythonContext context = getContext();
301299
Object primitivePtr = CApiContext.asPointer(ptr, ptrLib);
302-
context.getCApiContext().traceStaticMemory(primitivePtr, null, className);
303300
LOGGER.fine(() -> PythonUtils.formatJString("Initializing native type %s (ptr = %s)", className, CApiContext.asHex(primitivePtr)));
304301
return 0;
305302
}

0 commit comments

Comments
 (0)