Skip to content

Commit 0b7eac3

Browse files
committed
Report error to the caller
1 parent cf96361 commit 0b7eac3

File tree

7 files changed

+31
-15
lines changed

7 files changed

+31
-15
lines changed

Include/internal/pycore_mmap.h

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,27 @@ extern "C" {
1717
#endif
1818

1919
#if defined(HAVE_PR_SET_VMA_ANON_NAME) && defined(__linux__)
20-
static inline void
20+
static inline int
2121
_PyAnnotateMemoryMap(void *addr, size_t size, const char *name)
2222
{
2323
#ifndef Py_DEBUG
2424
if (!_Py_GetConfig()->dev_mode) {
25-
return;
25+
return 0;
2626
}
2727
#endif
2828
// The name length cannot exceed 80 (including the '\0').
2929
assert(strlen(name) < 80);
30-
int old_errno = errno;
31-
prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, (unsigned long)addr, size, name);
32-
/* Ignore errno from prctl */
33-
/* See: https://bugzilla.redhat.com/show_bug.cgi?id=2302746 */
34-
errno = old_errno;
30+
int res = prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, (unsigned long)addr, size, name);
31+
if (res < 0) {
32+
return -1;
33+
}
34+
return 0;
3535
}
3636
#else
37-
static inline void
37+
static inline int
3838
_PyAnnotateMemoryMap(void *Py_UNUSED(addr), size_t Py_UNUSED(size), const char *Py_UNUSED(name))
3939
{
40+
return 0;
4041
}
4142
#endif
4243

Lib/test/test_mmap.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from test.support.import_helper import import_module
77
from test.support.os_helper import TESTFN, unlink
88
from test.support.script_helper import assert_python_ok
9+
import errno
910
import unittest
1011
import os
1112
import re
@@ -1171,7 +1172,17 @@ def test_set_name(self):
11711172
# Test setting name on anonymous mmap
11721173
m = mmap.mmap(-1, PAGESIZE)
11731174
self.addCleanup(m.close)
1174-
result = m.set_name('test_mapping')
1175+
try:
1176+
result = m.set_name('test_mapping')
1177+
except OSError as exc:
1178+
if exc.errno == errno.EINVAL:
1179+
# gh-142419: On Fedora, prctl(PR_SET_VMA_ANON_NAME) fails with
1180+
# EINVAL because the kernel option CONFIG_ANON_VMA_NAME is
1181+
# disabled.
1182+
# See: https://bugzilla.redhat.com/show_bug.cgi?id=2302746
1183+
self.skipTest("prctl() failed with EINVAL")
1184+
else:
1185+
raise
11751186
self.assertIsNone(result)
11761187

11771188
# Test name length limit (80 chars including prefix "cpython:mmap:" and '\0')

Modules/mmapmodule.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,7 +1138,10 @@ mmap_mmap_set_name_impl(mmap_object *self, const char *name)
11381138
if (self->flags & MAP_ANONYMOUS) {
11391139
char buf[80];
11401140
sprintf(buf, "%s%s", prefix, name);
1141-
_PyAnnotateMemoryMap(self->data, self->size, buf);
1141+
if (_PyAnnotateMemoryMap(self->data, self->size, buf) < 0) {
1142+
PyErr_SetFromErrno(PyExc_OSError);
1143+
return NULL;
1144+
}
11421145
Py_RETURN_NONE;
11431146
}
11441147
else {
@@ -1993,7 +1996,7 @@ new_mmap_object(PyTypeObject *type, PyObject *args, PyObject *kwdict)
19931996
}
19941997
#ifdef MAP_ANONYMOUS
19951998
if (m_obj->flags & MAP_ANONYMOUS) {
1996-
_PyAnnotateMemoryMap(m_obj->data, map_size, "cpython:mmap");
1999+
(void)_PyAnnotateMemoryMap(m_obj->data, map_size, "cpython:mmap");
19972000
}
19982001
#endif
19992002
m_obj->access = (access_mode)access;

Objects/obmalloc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ _PyMem_ArenaAlloc(void *Py_UNUSED(ctx), size_t size)
468468
if (ptr == MAP_FAILED)
469469
return NULL;
470470
assert(ptr != NULL);
471-
_PyAnnotateMemoryMap(ptr, size, "cpython:pymalloc");
471+
(void)_PyAnnotateMemoryMap(ptr, size, "cpython:pymalloc");
472472
return ptr;
473473
#else
474474
return malloc(size);

Python/jit.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ jit_alloc(size_t size)
7777
unsigned char *memory = mmap(NULL, size, prot, flags, -1, 0);
7878
int failed = memory == MAP_FAILED;
7979
if (!failed) {
80-
_PyAnnotateMemoryMap(memory, size, "cpython:jit");
80+
(void)_PyAnnotateMemoryMap(memory, size, "cpython:jit");
8181
}
8282
#endif
8383
if (failed) {

Python/perf_jit_trampoline.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1086,7 +1086,8 @@ static void* perf_map_jit_init(void) {
10861086
close(fd);
10871087
return NULL; // Memory mapping failed
10881088
}
1089-
_PyAnnotateMemoryMap(perf_jit_map_state.mapped_buffer, page_size, "cpython:perf_jit_trampoline");
1089+
(void)_PyAnnotateMemoryMap(perf_jit_map_state.mapped_buffer, page_size,
1090+
"cpython:perf_jit_trampoline");
10901091
#endif
10911092

10921093
perf_jit_map_state.mapped_size = page_size;

Python/perf_trampoline.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ new_code_arena(void)
291291
perf_status = PERF_STATUS_FAILED;
292292
return -1;
293293
}
294-
_PyAnnotateMemoryMap(memory, mem_size, "cpython:perf_trampoline");
294+
(void)_PyAnnotateMemoryMap(memory, mem_size, "cpython:perf_trampoline");
295295
void *start = &_Py_trampoline_func_start;
296296
void *end = &_Py_trampoline_func_end;
297297
size_t code_size = end - start;

0 commit comments

Comments
 (0)