Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix remaining memory bugs #443

Merged
merged 18 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
7ea7a4c
fix: `Py_XDECREF` requires the GIL to be held, but it's unavailable a…
Xmader Sep 24, 2024
3453696
fix: accessing a non-existent property on a Python `bytes` in JS land…
Xmader Sep 24, 2024
dd0aaed
fix: clean up SpiderMonkey when the PythonMonkey module gets destroyed
Xmader Sep 24, 2024
a0e3b05
fix: to fix memory corruption, use `PyUnicode_AsUTF8AndSize` with the…
Xmader Sep 24, 2024
667abc3
fix: `PyUnicode_AsUTF8` needs a strict Python str object (not a subtype)
Xmader Sep 24, 2024
c804ca9
fix: to fix memory corruption, use `PyUnicode_AsUTF8AndSize` with the…
Xmader Sep 24, 2024
7c74274
fix: `PyEventLoop`'s destructor should not use any Python API, after …
Xmader Sep 24, 2024
73957c0
fix: `PyUnicodeObject` needs to be well-formed in a debug build of C…
Xmader Sep 24, 2024
5a13901
fix: the code argument to `python.exec`/`eval` needs to be a well-for…
Xmader Sep 24, 2024
8e365a7
refactor: replace the use of our own roundtrip `StrType::getValue` me…
Xmader Sep 24, 2024
0eeda35
fix: properly handle the reference count when doing `list.concat()`
Xmader Sep 24, 2024
c63b609
perf: simply do a pythonic `result = list[:]` to get a copy of all it…
Xmader Sep 24, 2024
9474990
fix the reference count
Xmader Sep 24, 2024
a1f11b4
Merge branch 'Xmader/feat/python-3.13-support' into Xmader/fix/fix-me…
Xmader Oct 1, 2024
dda2916
fix: reference count for `array_fill`
Xmader Oct 1, 2024
5b9deec
set `GLOBAL_CX = null` in the final cleanup function since it's no lo…
Xmader Oct 1, 2024
a4762ae
fix the reference count for dicts `test_get_default_not_found` in Pyt…
Xmader Oct 1, 2024
38309a3
Merge branch 'main' into Xmader/fix/fix-mem-bugs-using-debug-build
wesgarland Oct 10, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions include/StrType.hh
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ public:
*/
static PyObject *getPyObject(JSContext *cx, JS::HandleValue str);

static const char *getValue(JSContext *cx, JS::HandleValue str);

static PyObject *proxifyString(JSContext *cx, JS::HandleValue str);
};

Expand Down
4 changes: 2 additions & 2 deletions python/pythonmonkey/require.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@
globalThis.python.stderr.read = lambda n: sys.stderr.read(n)
# Python 3.13 dramatically changed how the namespace in `exec`/`eval` works
# See https://docs.python.org/3.13/whatsnew/3.13.html#defined-mutation-semantics-for-locals
globalThis.python.eval = lambda x: eval(x, None, sys._getframe(1).f_locals)
globalThis.python.exec = lambda x: exec(x, None, sys._getframe(1).f_locals)
globalThis.python.eval = lambda x: eval(str(x)[:], None, sys._getframe(1).f_locals)
globalThis.python.exec = lambda x: exec(str(x)[:], None, sys._getframe(1).f_locals)
globalThis.python.getenv = os.getenv
globalThis.python.paths = sys.path

Expand Down
4 changes: 2 additions & 2 deletions src/ExceptionType.cc
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ JSObject *ExceptionType::toJsError(JSContext *cx, PyObject *exceptionValue, PyOb
if (stackObj.get()) {
JS::RootedString stackStr(cx);
JS::BuildStackString(cx, nullptr, stackObj, &stackStr, 2, js::StackFormat::SpiderMonkey);
JS::RootedValue stackStrVal(cx, JS::StringValue(stackStr));
stackStream << "\nJS Stack Trace:\n" << StrType::getValue(cx, stackStrVal);
JS::UniqueChars stackStrUtf8 = JS_EncodeStringToUTF8(cx, stackStr);
stackStream << "\nJS Stack Trace:\n" << stackStrUtf8.get();
}


Expand Down
7 changes: 4 additions & 3 deletions src/JSObjectProxy.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ JSContext *GLOBAL_CX; /**< pointer to PythonMonkey's JSContext */
bool keyToId(PyObject *key, JS::MutableHandleId idp) {
if (PyUnicode_Check(key)) { // key is str type
JS::RootedString idString(GLOBAL_CX);
const char *keyStr = PyUnicode_AsUTF8(key);
JS::ConstUTF8CharsZ utf8Chars(keyStr, strlen(keyStr));
idString.set(JS_NewStringCopyUTF8Z(GLOBAL_CX, utf8Chars));
Py_ssize_t length;
const char *keyStr = PyUnicode_AsUTF8AndSize(key, &length);
JS::UTF8Chars utf8Chars(keyStr, length);
idString.set(JS_NewStringCopyUTF8N(GLOBAL_CX, utf8Chars));
return JS_StringToId(GLOBAL_CX, idString, idp);
Xmader marked this conversation as resolved.
Show resolved Hide resolved
} else if (PyLong_Check(key)) { // key is int type
uint32_t keyAsInt = PyLong_AsUnsignedLong(key); // TODO raise OverflowError if the value of pylong is out of range for a unsigned long
Expand Down
1 change: 1 addition & 0 deletions src/JobQueue.cc
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ bool sendJobToMainLoop(PyObject *pyFunc) {
}
loop.enqueue(pyFunc);

loop._loop = nullptr; // the `Py_XDECREF` Python API call in `PyEventLoop`'s destructor will not be accessible once we hand over the GIL by `PyGILState_Release`
PyGILState_Release(gstate);
return true;
}
Expand Down
3 changes: 3 additions & 0 deletions src/PyBytesProxyHandler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,9 @@ bool PyBytesProxyHandler::getOwnPropertyDescriptor(
PyObject *attrName = idToKey(cx, id);
PyObject *self = JS::GetMaybePtrFromReservedSlot<PyObject>(proxy, PyObjectSlot);
PyObject *item = PyObject_GetAttr(self, attrName);
if (!item && PyErr_ExceptionMatches(PyExc_AttributeError)) {
PyErr_Clear(); // clear error, we will be returning undefined in this case
}

return handleGetOwnPropertyDescriptor(cx, id, desc, item);
}
Expand Down
2 changes: 1 addition & 1 deletion src/PyDictProxyHandler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ bool PyDictProxyHandler::getOwnPropertyDescriptor(
) const {
PyObject *attrName = idToKey(cx, id);
PyObject *self = JS::GetMaybePtrFromReservedSlot<PyObject>(proxy, PyObjectSlot);
PyObject *item = PyDict_GetItemWithError(self, attrName);
PyObject *item = PyDict_GetItemWithError(self, attrName); // returns NULL without an exception set if the key wasn’t present.

return handleGetOwnPropertyDescriptor(cx, id, desc, item);
}
Expand Down
13 changes: 8 additions & 5 deletions src/PyIterableProxyHandler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ static bool toPrimitive(JSContext *cx, unsigned argc, JS::Value *vp) {
_PyUnicodeWriter writer;

_PyUnicodeWriter_Init(&writer);

PyObject *s = PyObject_Repr(self);

if (s == nullptr) {
Expand All @@ -95,8 +95,8 @@ static bool toPrimitive(JSContext *cx, unsigned argc, JS::Value *vp) {
return true;
}

PyObject* repr = _PyUnicodeWriter_Finish(&writer);
PyObject *repr = _PyUnicodeWriter_Finish(&writer);

args.rval().set(jsTypeFactory(cx, repr));
return true;
}
Expand Down Expand Up @@ -262,7 +262,7 @@ bool PyIterableProxyHandler::getOwnPropertyDescriptor(
// symbol property
if (id.isSymbol()) {
JS::RootedSymbol rootedSymbol(cx, id.toSymbol());
JS::SymbolCode symbolCode = JS::GetSymbolCode(rootedSymbol);
JS::SymbolCode symbolCode = JS::GetSymbolCode(rootedSymbol);

if (symbolCode == JS::SymbolCode::iterator) {
JSFunction *newFunction = JS_NewFunction(cx, iterable_values, 0, 0, NULL);
Expand All @@ -275,7 +275,7 @@ bool PyIterableProxyHandler::getOwnPropertyDescriptor(
)
));
return true;
}
}
else if (symbolCode == JS::SymbolCode::toPrimitive) {
JSFunction *newFunction = JS_NewFunction(cx, toPrimitive, 0, 0, nullptr);
if (!newFunction) return false;
Expand All @@ -293,6 +293,9 @@ bool PyIterableProxyHandler::getOwnPropertyDescriptor(
PyObject *attrName = idToKey(cx, id);
PyObject *self = JS::GetMaybePtrFromReservedSlot<PyObject>(proxy, PyObjectSlot);
PyObject *item = PyObject_GetAttr(self, attrName);
if (!item && PyErr_ExceptionMatches(PyExc_AttributeError)) {
PyErr_Clear(); // clear error, we will be returning undefined in this case
}

return handleGetOwnPropertyDescriptor(cx, id, desc, item);
}
16 changes: 5 additions & 11 deletions src/PyListProxyHandler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -428,17 +428,16 @@ static bool array_fill(JSContext *cx, unsigned argc, JS::Value *vp) {

JS::RootedValue fillValue(cx, args[0].get());
PyObject *fillValueItem = pyTypeFactory(cx, fillValue);
bool setItemCalled = false;
for (int index = actualStart; index < actualEnd; index++) {
setItemCalled = true;
// Since each call of `PyList_SetItem` steals a reference (even if its to the same object),
// We need multiple references to it for it to steal.
Py_INCREF(fillValueItem);
if (PyList_SetItem(self, index, fillValueItem) < 0) {
return false;
}
}

if (!setItemCalled) {
Py_DECREF(fillValueItem);
}
Py_DECREF(fillValueItem);

// return ref to self
args.rval().set(jsTypeFactory(cx, self));
Expand Down Expand Up @@ -550,12 +549,7 @@ static bool array_concat(JSContext *cx, unsigned argc, JS::Value *vp) {
PyObject *self = JS::GetMaybePtrFromReservedSlot<PyObject>(proxy, PyObjectSlot);

Py_ssize_t selfSize = PyList_GET_SIZE(self);

PyObject *result = PyList_New(selfSize);

for (Py_ssize_t index = 0; index < selfSize; index++) {
PyList_SetItem(result, index, PyList_GetItem(self, index));
}
PyObject *result = PyList_GetSlice(self, 0, selfSize);
Xmader marked this conversation as resolved.
Show resolved Hide resolved

unsigned numArgs = args.length();
JS::RootedValue elementVal(cx);
Expand Down
4 changes: 2 additions & 2 deletions src/PyObjectProxyHandler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@ bool PyObjectProxyHandler::getOwnPropertyDescriptor(
PyObject *attrName = idToKey(cx, id);
PyObject *self = JS::GetMaybePtrFromReservedSlot<PyObject>(proxy, PyObjectSlot);
PyObject *item = PyObject_GetAttr(self, attrName);
if (!item) { // clear error, we will be returning undefined in this case
PyErr_Clear();
if (!item && PyErr_ExceptionMatches(PyExc_AttributeError)) {
PyErr_Clear(); // clear error, we will be returning undefined in this case
}

return handleGetOwnPropertyDescriptor(cx, id, desc, item);
Expand Down
27 changes: 20 additions & 7 deletions src/StrType.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ static bool containsSurrogatePair(const char16_t *chars, size_t length) {
return false;
}

/**
* @brief check if the Latin-1 encoded `chars` only contain ascii characters
*/
static bool containsOnlyAscii(const JS::Latin1Char *chars, size_t length) {
for (size_t i = 0; i < length; i++) {
if (chars[i] >= 128) return false;
}
return true;
}

/**
* @brief creates new UCS4-encoded pyObject string. This must be called by the user if the original JSString contains any surrogate pairs
*
Expand Down Expand Up @@ -134,6 +144,16 @@ PyObject *StrType::proxifyString(JSContext *cx, JS::HandleValue strVal) {
PY_UNICODE_OBJECT_WSTR_LENGTH(pyString) = 0;
PY_UNICODE_OBJECT_READY(pyString) = 1;
#endif

#ifdef Py_DEBUG
// In a debug build of CPython, it needs to be a well-formed PyUnicodeObject, otherwise a `_PyObject_AssertFailed` error will be raised.
// See: `_PyUnicode_CheckConsistency` https://github.com/python/cpython/blob/v3.11.3/Objects/unicodeobject.c#L594-L600, #L552-L553
if (containsOnlyAscii(chars, length)) {
PY_UNICODE_OBJECT_STATE(pyString).ascii = 1;
PY_UNICODE_OBJECT_UTF8(pyString) = (char *)chars; // XXX: most APIs (e.g. PyUnicode_AsUTF8) assume this is a \0 terminated string
PY_UNICODE_OBJECT_UTF8_LENGTH(pyString) = length;
}
#endif
}
else { // utf16 spidermonkey, ucs2 python
const char16_t *chars = JS::GetTwoByteLinearStringChars(nogc, lstr);
Expand Down Expand Up @@ -192,10 +212,3 @@ PyObject *StrType::getPyObject(JSContext *cx, JS::HandleValue str) {

return proxifyString(cx, str);
}

const char *StrType::getValue(JSContext *cx, JS::HandleValue str) {
PyObject *pyString = proxifyString(cx, str);
const char *value = PyUnicode_AsUTF8(pyString);
Py_DECREF(pyString);
return value;
}
37 changes: 29 additions & 8 deletions src/modules/pythonmonkey/pythonmonkey.cc
Original file line number Diff line number Diff line change
Expand Up @@ -303,14 +303,23 @@ PyTypeObject JSObjectItemsProxyType = {
};

static void cleanup() {
// Clean up the PythonMonkey module
Py_XDECREF(PythonMonkey_Null);
Py_XDECREF(PythonMonkey_BigInt);

// Clean up SpiderMonkey
delete autoRealm;
delete global;
if (GLOBAL_CX) JS_DestroyContext(GLOBAL_CX);
if (GLOBAL_CX) {
JS_DestroyContext(GLOBAL_CX);
GLOBAL_CX = nullptr;
}
delete JOB_QUEUE;
JS_ShutDown();
}
static void cleanup(PyObject *) {
cleanup();
}

static PyObject *collect(PyObject *self, PyObject *args) {
JS_GC(GLOBAL_CX);
Expand All @@ -325,7 +334,7 @@ static bool getEvalOption(PyObject *evalOptions, const char *optionName, const c
value = PyDict_GetItemString(evalOptions, optionName);
}
if (value && value != Py_None) {
*s_p = PyUnicode_AsUTF8(value);
*s_p = PyUnicode_AsUTF8(PyUnicode_FromObject(value));
}
return value != NULL && value != Py_None;
}
Expand Down Expand Up @@ -443,7 +452,8 @@ static PyObject *eval(PyObject *self, PyObject *args) {
#endif
if (!getEvalOption(evalOptions, "filename", &s)) {
if (filename && PyUnicode_Check(filename)) {
options.setFile(PyUnicode_AsUTF8(filename));
PyObject *filenameStr = PyUnicode_FromObject(filename); // needs a strict Python str object (not a subtype)
options.setFile(PyUnicode_AsUTF8(filenameStr));
}
} /* filename */
} /* fromPythonFrame */
Expand All @@ -454,8 +464,9 @@ static PyObject *eval(PyObject *self, PyObject *args) {
JS::Rooted<JS::Value> rval(GLOBAL_CX);
if (code) {
JS::SourceText<mozilla::Utf8Unit> source;
const char *codeChars = PyUnicode_AsUTF8(code);
if (!source.init(GLOBAL_CX, codeChars, strlen(codeChars), JS::SourceOwnership::Borrowed)) {
Py_ssize_t codeLength;
const char *codeChars = PyUnicode_AsUTF8AndSize(code, &codeLength);
if (!source.init(GLOBAL_CX, codeChars, codeLength, JS::SourceOwnership::Borrowed)) {
setSpiderMonkeyException(GLOBAL_CX);
return NULL;
}
Xmader marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -514,9 +525,10 @@ static PyObject *isCompilableUnit(PyObject *self, PyObject *args) {
return NULL;
}

const char *bufferUtf8 = PyUnicode_AsUTF8(item);
Py_ssize_t bufferLength;
const char *bufferUtf8 = PyUnicode_AsUTF8AndSize(item, &bufferLength);

if (JS_Utf8BufferIsCompilableUnit(GLOBAL_CX, *global, bufferUtf8, strlen(bufferUtf8))) {
if (JS_Utf8BufferIsCompilableUnit(GLOBAL_CX, *global, bufferUtf8, bufferLength)) {
Py_RETURN_TRUE;
} else {
Py_RETURN_FALSE;
Expand Down Expand Up @@ -552,7 +564,6 @@ PyMODINIT_FUNC PyInit_pythonmonkey(void)
PyErr_SetString(SpiderMonkeyError, "Spidermonkey could not be initialized.");
return NULL;
}
Py_AtExit(cleanup);

GLOBAL_CX = JS_NewContext(JS::DefaultHeapMaxBytes);
if (!GLOBAL_CX) {
Expand Down Expand Up @@ -645,6 +656,16 @@ PyMODINIT_FUNC PyInit_pythonmonkey(void)
if (pyModule == NULL)
return NULL;

// Clean up SpiderMonkey when the PythonMonkey module gets destroyed (module.___cleanup is GCed)
// The `cleanup` function will be called automatically when this PyCapsule gets GCed
// We cannot use `Py_AtExit(cleanup);` because the GIL is unavailable after Python finalization, no more Python APIs can be called.
PyObject *autoDestructor = PyCapsule_New(&pythonmonkey, NULL, cleanup);
if (PyModule_AddObject(pyModule, "___cleanup", autoDestructor) < 0) {
Py_DECREF(autoDestructor);
Py_DECREF(pyModule);
return NULL;
}

Py_INCREF(&NullType);
if (PyModule_AddObject(pyModule, "null", (PyObject *)&NullType) < 0) {
Py_DECREF(&NullType);
Expand Down
4 changes: 2 additions & 2 deletions src/setSpiderMonkeyException.cc
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ PyObject *getExceptionString(JSContext *cx, const JS::ExceptionStack &exceptionS
if (stackObj.get()) {
JS::RootedString stackStr(cx);
BuildStackString(cx, nullptr, stackObj, &stackStr, 2, js::StackFormat::SpiderMonkey);
JS::RootedValue stackStrVal(cx, JS::StringValue(stackStr));
outStrStream << "Stack Trace:\n" << StrType::getValue(cx, stackStrVal);
JS::UniqueChars stackStrUtf8 = JS_EncodeStringToUTF8(cx, stackStr);
outStrStream << "Stack Trace:\n" << stackStrUtf8.get();
}
}

Expand Down
Loading