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
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fix: PyUnicodeObject needs to be well-formed in a debug build of CP…
…ython

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
  • Loading branch information
Xmader committed Sep 24, 2024
commit 73957c02ef459ca28739059c3847bbc698998283
22 changes: 21 additions & 1 deletion 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 @@ -195,7 +215,7 @@ PyObject *StrType::getPyObject(JSContext *cx, JS::HandleValue str) {

const char *StrType::getValue(JSContext *cx, JS::HandleValue str) {
PyObject *pyString = proxifyString(cx, str);
const char *value = PyUnicode_AsUTF8(pyString);
const char *value = PyUnicode_AsUTF8(PyUnicode_FromObject(pyString));
Py_DECREF(pyString);
return value;
}