Skip to content

Commit fa7e2f2

Browse files
committed
[GR-65639] Safe upcall and read ob_fval value directly from our stubs.
1 parent 411099e commit fa7e2f2

File tree

4 files changed

+32
-37
lines changed

4 files changed

+32
-37
lines changed

graalpython/com.oracle.graal.python.cext/include/cpython/floatobject.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (c) 2023, 2024, Oracle and/or its affiliates.
1+
/* Copyright (c) 2023, 2025, Oracle and/or its affiliates.
22
* Copyright (C) 1996-2023 Python Software Foundation
33
*
44
* Licensed under the PYTHON SOFTWARE FOUNDATION LICENSE VERSION 2
@@ -12,6 +12,12 @@ typedef struct {
1212
double ob_fval;
1313
} PyFloatObject;
1414

15+
// GraalPy addition
16+
typedef struct {
17+
GraalPyObject ob_base;
18+
double ob_fval;
19+
} GraalPyFloatObject;
20+
1521
#define _PyFloat_CAST(op) \
1622
(assert(PyFloat_Check(op)), _Py_CAST(PyFloatObject*, op))
1723

@@ -21,7 +27,11 @@ static inline double PyFloat_AS_DOUBLE(PyObject *op) {
2127
#if 0 // GraalPy change
2228
return _PyFloat_CAST(op)->ob_fval;
2329
#else // GraalPy change
24-
return PyFloat_AsDouble(op);
30+
if (points_to_py_handle_space(op)) {
31+
return ((GraalPyFloatObject*) pointer_to_stub(op))->ob_fval;
32+
} else {
33+
return _PyFloat_CAST(op)->ob_fval;
34+
}
2535
#endif // GraalPy change
2636
}
2737
#define PyFloat_AS_DOUBLE(op) PyFloat_AS_DOUBLE(_PyObject_CAST(op))

graalpython/com.oracle.graal.python.cext/include/object.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,8 +1036,20 @@ static inline int PyType_CheckExact(PyObject *op) {
10361036
# define PyType_CheckExact(op) PyType_CheckExact(_PyObject_CAST(op))
10371037
#endif
10381038

1039+
// GraalPy additions
10391040
PyAPI_FUNC(void) _PyTruffle_DebugTrace(void);
10401041

1042+
typedef struct {
1043+
PyObject_HEAD
1044+
int32_t handle_table_index;
1045+
} GraalPyObject;
1046+
1047+
typedef struct {
1048+
GraalPyObject ob_base;
1049+
Py_ssize_t ob_size;
1050+
PyObject **ob_item;
1051+
} GraalPyVarObject;
1052+
10411053
#ifdef __cplusplus
10421054
}
10431055
#endif

graalpython/com.oracle.graal.python.cext/src/capi.h

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -112,22 +112,6 @@ typedef struct {
112112
int getter_doc;
113113
} propertyobject;
114114

115-
typedef struct {
116-
PyObject_HEAD
117-
int32_t handle_table_index;
118-
} GraalPyObject;
119-
120-
typedef struct {
121-
GraalPyObject ob_base;
122-
Py_ssize_t ob_size;
123-
PyObject **ob_item;
124-
} GraalPyVarObject;
125-
126-
typedef struct {
127-
GraalPyObject ob_base;
128-
double ob_fval;
129-
} GraalPyFloatObject;
130-
131115
/* GraalPy GC support */
132116

133117
typedef struct _cycle_node {

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

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (c) 2018, 2024, Oracle and/or its affiliates.
1+
/* Copyright (c) 2018, 2025, Oracle and/or its affiliates.
22
* Copyright (C) 1996-2017 Python Software Foundation
33
*
44
* Licensed under the PYTHON SOFTWARE FOUNDATION LICENSE VERSION 2
@@ -308,20 +308,6 @@ float_dealloc(PyObject *op)
308308
double
309309
PyFloat_AsDouble(PyObject *op)
310310
{
311-
// GraalPy change: read from native object stub or upcall for managed
312-
if (points_to_py_handle_space(op)) {
313-
if (PyFloat_Check(op)) {
314-
double val = ((GraalPyFloatObject*) pointer_to_stub(op))->ob_fval;
315-
#ifndef NDEBUG
316-
if (PyTruffle_Debug_CAPI() && GraalPyTruffleFloat_AsDouble(op) != val) {
317-
Py_FatalError("ob_size of native stub and managed object differ");
318-
}
319-
#endif
320-
return val;
321-
}
322-
return GraalPyTruffleFloat_AsDouble(op);
323-
}
324-
325311
PyNumberMethods *nb;
326312
PyObject *res;
327313
double val;
@@ -332,8 +318,12 @@ PyFloat_AsDouble(PyObject *op)
332318
}
333319

334320
if (PyFloat_Check(op)) {
335-
// GraalPy change: avoid macro recursion
336-
return ((PyFloatObject*) op)->ob_fval;
321+
return PyFloat_AS_DOUBLE(op);
322+
}
323+
324+
// GraalPy change: upcall for managed
325+
if (points_to_py_handle_space(op)) {
326+
return GraalPyTruffleFloat_AsDouble(op);
337327
}
338328

339329
nb = Py_TYPE(op)->tp_as_number;
@@ -374,8 +364,7 @@ PyFloat_AsDouble(PyObject *op)
374364
}
375365
}
376366

377-
// GraalPy change: avoid macro recursion
378-
val = PyFloat_AsDouble(res);
367+
val = PyFloat_AS_DOUBLE(res);
379368
Py_DECREF(res);
380369
return val;
381370
}

0 commit comments

Comments
 (0)