Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork32k
gh-93649: Split tracemalloc tests from _testcapimodule.c#99551
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
erlend-aasland merged 5 commits intopython:mainfromerlend-aasland:split-testcapi/tracemallocDec 17, 2022
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
5 commits Select commitHold shift + click to select a range
5de1dc4
gh-93649: Split tracemalloc tests from _testcapimodule.c
erlend-aasland657eff1
Split memory tests from Lib/test/test_capi/test_misc.py
erlend-aasland752b41b
Merge branch 'main' into split-testcapi/tracemalloc
erlend-aaslandd2a4e42
Merge branch 'main' into split-testcapi/tracemalloc
erlend-aaslande88f1e1
Address review: whitespace lint
erlend-aaslandFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
169 changes: 169 additions & 0 deletionsLib/test/test_capi/test_mem.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
import re | ||
import textwrap | ||
import unittest | ||
from test import support | ||
from test.support import import_helper, requires_subprocess | ||
from test.support.script_helper import assert_python_failure, assert_python_ok | ||
# Skip this test if the _testcapi module isn't available. | ||
_testcapi = import_helper.import_module('_testcapi') | ||
@requires_subprocess() | ||
class PyMemDebugTests(unittest.TestCase): | ||
PYTHONMALLOC = 'debug' | ||
# '0x04c06e0' or '04C06E0' | ||
PTR_REGEX = r'(?:0x)?[0-9a-fA-F]+' | ||
def check(self, code): | ||
with support.SuppressCrashReport(): | ||
out = assert_python_failure( | ||
'-c', code, | ||
PYTHONMALLOC=self.PYTHONMALLOC, | ||
# FreeBSD: instruct jemalloc to not fill freed() memory | ||
# with junk byte 0x5a, see JEMALLOC(3) | ||
MALLOC_CONF="junk:false", | ||
) | ||
stderr = out.err | ||
return stderr.decode('ascii', 'replace') | ||
def test_buffer_overflow(self): | ||
out = self.check('import _testcapi; _testcapi.pymem_buffer_overflow()') | ||
regex = (r"Debug memory block at address p={ptr}: API 'm'\n" | ||
r" 16 bytes originally requested\n" | ||
r" The [0-9] pad bytes at p-[0-9] are FORBIDDENBYTE, as expected.\n" | ||
r" The [0-9] pad bytes at tail={ptr} are not all FORBIDDENBYTE \(0x[0-9a-f]{{2}}\):\n" | ||
r" at tail\+0: 0x78 \*\*\* OUCH\n" | ||
r" at tail\+1: 0xfd\n" | ||
r" at tail\+2: 0xfd\n" | ||
r" .*\n" | ||
r"( The block was made by call #[0-9]+ to debug malloc/realloc.\n)?" | ||
r" Data at p: cd cd cd .*\n" | ||
r"\n" | ||
r"Enable tracemalloc to get the memory block allocation traceback\n" | ||
r"\n" | ||
r"Fatal Python error: _PyMem_DebugRawFree: bad trailing pad byte") | ||
regex = regex.format(ptr=self.PTR_REGEX) | ||
regex = re.compile(regex, flags=re.DOTALL) | ||
self.assertRegex(out, regex) | ||
def test_api_misuse(self): | ||
out = self.check('import _testcapi; _testcapi.pymem_api_misuse()') | ||
regex = (r"Debug memory block at address p={ptr}: API 'm'\n" | ||
r" 16 bytes originally requested\n" | ||
r" The [0-9] pad bytes at p-[0-9] are FORBIDDENBYTE, as expected.\n" | ||
r" The [0-9] pad bytes at tail={ptr} are FORBIDDENBYTE, as expected.\n" | ||
r"( The block was made by call #[0-9]+ to debug malloc/realloc.\n)?" | ||
r" Data at p: cd cd cd .*\n" | ||
r"\n" | ||
r"Enable tracemalloc to get the memory block allocation traceback\n" | ||
r"\n" | ||
r"Fatal Python error: _PyMem_DebugRawFree: bad ID: Allocated using API 'm', verified using API 'r'\n") | ||
regex = regex.format(ptr=self.PTR_REGEX) | ||
self.assertRegex(out, regex) | ||
def check_malloc_without_gil(self, code): | ||
out = self.check(code) | ||
expected = ('Fatal Python error: _PyMem_DebugMalloc: ' | ||
'Python memory allocator called without holding the GIL') | ||
self.assertIn(expected, out) | ||
def test_pymem_malloc_without_gil(self): | ||
# Debug hooks must raise an error if PyMem_Malloc() is called | ||
# without holding the GIL | ||
code = 'import _testcapi; _testcapi.pymem_malloc_without_gil()' | ||
self.check_malloc_without_gil(code) | ||
def test_pyobject_malloc_without_gil(self): | ||
# Debug hooks must raise an error if PyObject_Malloc() is called | ||
# without holding the GIL | ||
code = 'import _testcapi; _testcapi.pyobject_malloc_without_gil()' | ||
self.check_malloc_without_gil(code) | ||
def check_pyobject_is_freed(self, func_name): | ||
code = textwrap.dedent(f''' | ||
import gc, os, sys, _testcapi | ||
# Disable the GC to avoid crash on GC collection | ||
gc.disable() | ||
try: | ||
_testcapi.{func_name}() | ||
# Exit immediately to avoid a crash while deallocating | ||
# the invalid object | ||
os._exit(0) | ||
except _testcapi.error: | ||
os._exit(1) | ||
''') | ||
assert_python_ok( | ||
'-c', code, | ||
PYTHONMALLOC=self.PYTHONMALLOC, | ||
MALLOC_CONF="junk:false", | ||
) | ||
def test_pyobject_null_is_freed(self): | ||
self.check_pyobject_is_freed('check_pyobject_null_is_freed') | ||
def test_pyobject_uninitialized_is_freed(self): | ||
self.check_pyobject_is_freed('check_pyobject_uninitialized_is_freed') | ||
def test_pyobject_forbidden_bytes_is_freed(self): | ||
self.check_pyobject_is_freed('check_pyobject_forbidden_bytes_is_freed') | ||
def test_pyobject_freed_is_freed(self): | ||
self.check_pyobject_is_freed('check_pyobject_freed_is_freed') | ||
def test_set_nomemory(self): | ||
code = """if 1: | ||
import _testcapi | ||
class C(): pass | ||
# The first loop tests both functions and that remove_mem_hooks() | ||
# can be called twice in a row. The second loop checks a call to | ||
# set_nomemory() after a call to remove_mem_hooks(). The third | ||
# loop checks the start and stop arguments of set_nomemory(). | ||
for outer_cnt in range(1, 4): | ||
start = 10 * outer_cnt | ||
for j in range(100): | ||
if j == 0: | ||
if outer_cnt != 3: | ||
_testcapi.set_nomemory(start) | ||
else: | ||
_testcapi.set_nomemory(start, start + 1) | ||
try: | ||
C() | ||
except MemoryError as e: | ||
if outer_cnt != 3: | ||
_testcapi.remove_mem_hooks() | ||
print('MemoryError', outer_cnt, j) | ||
_testcapi.remove_mem_hooks() | ||
break | ||
""" | ||
rc, out, err = assert_python_ok('-c', code) | ||
lines = out.splitlines() | ||
for i, line in enumerate(lines, 1): | ||
self.assertIn(b'MemoryError', out) | ||
*_, count = line.split(b' ') | ||
count = int(count) | ||
self.assertLessEqual(count, i*5) | ||
self.assertGreaterEqual(count, i*5-2) | ||
class PyMemMallocDebugTests(PyMemDebugTests): | ||
PYTHONMALLOC = 'malloc_debug' | ||
@unittest.skipUnless(support.with_pymalloc(), 'need pymalloc') | ||
class PyMemPymallocDebugTests(PyMemDebugTests): | ||
PYTHONMALLOC = 'pymalloc_debug' | ||
@unittest.skipUnless(support.Py_DEBUG, 'need Py_DEBUG') | ||
class PyMemDefaultTests(PyMemDebugTests): | ||
# test default allocator of Python compiled in debug mode | ||
PYTHONMALLOC = '' | ||
if __name__ == "__main__": | ||
unittest.main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.