Skip to content

Commit

Permalink
Add test for invalid hiwire refs
Browse files Browse the repository at this point in the history
  • Loading branch information
hoodmane committed Sep 23, 2023
1 parent e628bcb commit 1ab1392
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions src/tests/test_pyodide.py
Original file line number Diff line number Diff line change
Expand Up @@ -1685,3 +1685,54 @@ def f2():
assert dedent("\n".join(msg.splitlines()[1:-1])) == "\n".join(
expected.splitlines()[2:]
)

@run_in_pyodide
def test_hiwire_invalid_ref(selenium):
import pyodide_js
from pyodide.ffi import JsException
from pyodide.code import run_js
import pytest

_hiwire_get = pyodide_js._module._hiwire_get
_hiwire_incref = pyodide_js._module._hiwire_incref
_hiwire_decref = pyodide_js._module._hiwire_decref
_api = pyodide_js._api

_hiwire_incref(0)
assert not _api.fail_test
_hiwire_decref(0)
assert not _api.fail_test
expected = r"Pyodide internal error: Argument to hiwire_get is falsy \(but error indicator is not set\)\."
with pytest.raises(JsException, match=expected):
_hiwire_get(0)
assert _api.fail_test
_api.fail_test = False

with pytest.raises(AssertionError, match="This is a message"):
run_js(
"""
const msgptr = pyodide._module.stringToNewUTF8("This is a message");
const AssertionError = pyodide._module.HEAP32[pyodide._module._PyExc_AssertionError/4];
pyodide._module._PyErr_SetString(AssertionError, msgptr);
pyodide._module._free(msgptr);
try {
pyodide._module._hiwire_get(0);
} finally {
pyodide._module._PyErr_Clear();
}
"""
)
msg = "hiwire_{} on invalid reference 77. This is most likely due to use after free. It may also be due to memory corruption."
with pytest.raises(JsException, match=msg.format("get")):
_hiwire_get(77)
assert _api.fail_test
_api.fail_test = False
with pytest.raises(JsException, match=msg.format("incref")):
_hiwire_incref(77)
assert _api.fail_test
_api.fail_test = False
with pytest.raises(JsException, match=msg.format("decref")):
_hiwire_decref(77)
assert _api.fail_test
_api.fail_test = False

0 comments on commit 1ab1392

Please sign in to comment.