1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
|
#include "parts.h"
#include <stddef.h>
typedef struct {
PyMemAllocatorEx alloc;
size_t malloc_size;
size_t calloc_nelem;
size_t calloc_elsize;
void *realloc_ptr;
size_t realloc_new_size;
void *free_ptr;
void *ctx;
} alloc_hook_t;
static void *
hook_malloc(void *ctx, size_t size)
{
alloc_hook_t *hook = (alloc_hook_t *)ctx;
hook->ctx = ctx;
hook->malloc_size = size;
return hook->alloc.malloc(hook->alloc.ctx, size);
}
static void *
hook_calloc(void *ctx, size_t nelem, size_t elsize)
{
alloc_hook_t *hook = (alloc_hook_t *)ctx;
hook->ctx = ctx;
hook->calloc_nelem = nelem;
hook->calloc_elsize = elsize;
return hook->alloc.calloc(hook->alloc.ctx, nelem, elsize);
}
static void *
hook_realloc(void *ctx, void *ptr, size_t new_size)
{
alloc_hook_t *hook = (alloc_hook_t *)ctx;
hook->ctx = ctx;
hook->realloc_ptr = ptr;
hook->realloc_new_size = new_size;
return hook->alloc.realloc(hook->alloc.ctx, ptr, new_size);
}
static void
hook_free(void *ctx, void *ptr)
{
alloc_hook_t *hook = (alloc_hook_t *)ctx;
hook->ctx = ctx;
hook->free_ptr = ptr;
hook->alloc.free(hook->alloc.ctx, ptr);
}
/* Most part of the following code is inherited from the pyfailmalloc project
* written by Victor Stinner. */
static struct {
int installed;
PyMemAllocatorEx raw;
PyMemAllocatorEx mem;
PyMemAllocatorEx obj;
} FmHook;
static struct {
int start;
int stop;
Py_ssize_t count;
} FmData;
static int
fm_nomemory(void)
{
FmData.count++;
if (FmData.count > FmData.start &&
(FmData.stop <= 0 || FmData.count <= FmData.stop))
{
return 1;
}
return 0;
}
static void *
hook_fmalloc(void *ctx, size_t size)
{
PyMemAllocatorEx *alloc = (PyMemAllocatorEx *)ctx;
if (fm_nomemory()) {
return NULL;
}
return alloc->malloc(alloc->ctx, size);
}
static void *
hook_fcalloc(void *ctx, size_t nelem, size_t elsize)
{
PyMemAllocatorEx *alloc = (PyMemAllocatorEx *)ctx;
if (fm_nomemory()) {
return NULL;
}
return alloc->calloc(alloc->ctx, nelem, elsize);
}
static void *
hook_frealloc(void *ctx, void *ptr, size_t new_size)
{
PyMemAllocatorEx *alloc = (PyMemAllocatorEx *)ctx;
if (fm_nomemory()) {
return NULL;
}
return alloc->realloc(alloc->ctx, ptr, new_size);
}
static void
hook_ffree(void *ctx, void *ptr)
{
PyMemAllocatorEx *alloc = (PyMemAllocatorEx *)ctx;
alloc->free(alloc->ctx, ptr);
}
static void
fm_setup_hooks(void)
{
if (FmHook.installed) {
return;
}
FmHook.installed = 1;
PyMemAllocatorEx alloc;
alloc.malloc = hook_fmalloc;
alloc.calloc = hook_fcalloc;
alloc.realloc = hook_frealloc;
alloc.free = hook_ffree;
PyMem_GetAllocator(PYMEM_DOMAIN_RAW, &FmHook.raw);
PyMem_GetAllocator(PYMEM_DOMAIN_MEM, &FmHook.mem);
PyMem_GetAllocator(PYMEM_DOMAIN_OBJ, &FmHook.obj);
alloc.ctx = &FmHook.raw;
PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &alloc);
alloc.ctx = &FmHook.mem;
PyMem_SetAllocator(PYMEM_DOMAIN_MEM, &alloc);
alloc.ctx = &FmHook.obj;
PyMem_SetAllocator(PYMEM_DOMAIN_OBJ, &alloc);
}
static void
fm_remove_hooks(void)
{
if (FmHook.installed) {
FmHook.installed = 0;
PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &FmHook.raw);
PyMem_SetAllocator(PYMEM_DOMAIN_MEM, &FmHook.mem);
PyMem_SetAllocator(PYMEM_DOMAIN_OBJ, &FmHook.obj);
}
}
static PyObject *
set_nomemory(PyObject *self, PyObject *args)
{
/* Memory allocation fails after 'start' allocation requests, and until
* 'stop' allocation requests except when 'stop' is negative or equal
* to 0 (default) in which case allocation failures never stop. */
FmData.count = 0;
FmData.stop = 0;
if (!PyArg_ParseTuple(args, "i|i", &FmData.start, &FmData.stop)) {
return NULL;
}
fm_setup_hooks();
Py_RETURN_NONE;
}
static PyObject *
remove_mem_hooks(PyObject *self, PyObject *Py_UNUSED(ignored))
{
fm_remove_hooks();
Py_RETURN_NONE;
}
static PyObject *
test_setallocators(PyMemAllocatorDomain domain)
{
PyObject *res = NULL;
const char *error_msg;
alloc_hook_t hook;
memset(&hook, 0, sizeof(hook));
PyMemAllocatorEx alloc;
alloc.ctx = &hook;
alloc.malloc = &hook_malloc;
alloc.calloc = &hook_calloc;
alloc.realloc = &hook_realloc;
alloc.free = &hook_free;
PyMem_GetAllocator(domain, &hook.alloc);
PyMem_SetAllocator(domain, &alloc);
/* malloc, realloc, free */
size_t size = 42;
hook.ctx = NULL;
void *ptr;
switch(domain) {
case PYMEM_DOMAIN_RAW:
ptr = PyMem_RawMalloc(size);
break;
case PYMEM_DOMAIN_MEM:
ptr = PyMem_Malloc(size);
break;
case PYMEM_DOMAIN_OBJ:
ptr = PyObject_Malloc(size);
break;
default:
ptr = NULL;
break;
}
#define CHECK_CTX(FUNC) \
if (hook.ctx != &hook) { \
error_msg = FUNC " wrong context"; \
goto fail; \
} \
hook.ctx = NULL; /* reset for next check */
if (ptr == NULL) {
error_msg = "malloc failed";
goto fail;
}
CHECK_CTX("malloc");
if (hook.malloc_size != size) {
error_msg = "malloc invalid size";
goto fail;
}
size_t size2 = 200;
void *ptr2;
switch(domain) {
case PYMEM_DOMAIN_RAW:
ptr2 = PyMem_RawRealloc(ptr, size2);
break;
case PYMEM_DOMAIN_MEM:
ptr2 = PyMem_Realloc(ptr, size2);
break;
case PYMEM_DOMAIN_OBJ:
ptr2 = PyObject_Realloc(ptr, size2);
break;
default:
ptr2 = NULL;
break;
}
if (ptr2 == NULL) {
error_msg = "realloc failed";
goto fail;
}
CHECK_CTX("realloc");
if (hook.realloc_ptr != ptr || hook.realloc_new_size != size2) {
error_msg = "realloc invalid parameters";
goto fail;
}
switch(domain) {
case PYMEM_DOMAIN_RAW:
PyMem_RawFree(ptr2);
break;
case PYMEM_DOMAIN_MEM:
PyMem_Free(ptr2);
break;
case PYMEM_DOMAIN_OBJ:
PyObject_Free(ptr2);
break;
}
CHECK_CTX("free");
if (hook.free_ptr != ptr2) {
error_msg = "free invalid pointer";
goto fail;
}
/* calloc, free */
size_t nelem = 2;
size_t elsize = 5;
switch(domain) {
case PYMEM_DOMAIN_RAW:
ptr = PyMem_RawCalloc(nelem, elsize);
break;
case PYMEM_DOMAIN_MEM:
ptr = PyMem_Calloc(nelem, elsize);
break;
case PYMEM_DOMAIN_OBJ:
ptr = PyObject_Calloc(nelem, elsize);
break;
default:
ptr = NULL;
break;
}
if (ptr == NULL) {
error_msg = "calloc failed";
goto fail;
}
CHECK_CTX("calloc");
if (hook.calloc_nelem != nelem || hook.calloc_elsize != elsize) {
error_msg = "calloc invalid nelem or elsize";
goto fail;
}
hook.free_ptr = NULL;
switch(domain) {
case PYMEM_DOMAIN_RAW:
PyMem_RawFree(ptr);
break;
case PYMEM_DOMAIN_MEM:
PyMem_Free(ptr);
break;
case PYMEM_DOMAIN_OBJ:
PyObject_Free(ptr);
break;
}
CHECK_CTX("calloc free");
if (hook.free_ptr != ptr) {
error_msg = "calloc free invalid pointer";
goto fail;
}
res = Py_NewRef(Py_None);
goto finally;
fail:
PyErr_SetString(PyExc_RuntimeError, error_msg);
finally:
PyMem_SetAllocator(domain, &hook.alloc);
return res;
#undef CHECK_CTX
}
static PyObject *
test_pyobject_setallocators(PyObject *self, PyObject *Py_UNUSED(ignored))
{
return test_setallocators(PYMEM_DOMAIN_OBJ);
}
static PyObject *
test_pyobject_new(PyObject *self, PyObject *Py_UNUSED(ignored))
{
PyObject *obj;
PyTypeObject *type = &PyBaseObject_Type;
PyTypeObject *var_type = &PyLong_Type;
// PyObject_New()
obj = PyObject_New(PyObject, type);
if (obj == NULL) {
goto alloc_failed;
}
Py_DECREF(obj);
// PyObject_NEW()
obj = PyObject_NEW(PyObject, type);
if (obj == NULL) {
goto alloc_failed;
}
Py_DECREF(obj);
// PyObject_NewVar()
obj = PyObject_NewVar(PyObject, var_type, 3);
if (obj == NULL) {
goto alloc_failed;
}
Py_DECREF(obj);
// PyObject_NEW_VAR()
obj = PyObject_NEW_VAR(PyObject, var_type, 3);
if (obj == NULL) {
goto alloc_failed;
}
Py_DECREF(obj);
Py_RETURN_NONE;
alloc_failed:
PyErr_NoMemory();
return NULL;
}
static PyObject *
test_pymem_alloc0(PyObject *self, PyObject *Py_UNUSED(ignored))
{
void *ptr;
ptr = PyMem_RawMalloc(0);
if (ptr == NULL) {
PyErr_SetString(PyExc_RuntimeError,
"PyMem_RawMalloc(0) returns NULL");
return NULL;
}
PyMem_RawFree(ptr);
ptr = PyMem_RawCalloc(0, 0);
if (ptr == NULL) {
PyErr_SetString(PyExc_RuntimeError,
"PyMem_RawCalloc(0, 0) returns NULL");
return NULL;
}
PyMem_RawFree(ptr);
ptr = PyMem_Malloc(0);
if (ptr == NULL) {
PyErr_SetString(PyExc_RuntimeError,
"PyMem_Malloc(0) returns NULL");
return NULL;
}
PyMem_Free(ptr);
ptr = PyMem_Calloc(0, 0);
if (ptr == NULL) {
PyErr_SetString(PyExc_RuntimeError,
"PyMem_Calloc(0, 0) returns NULL");
return NULL;
}
PyMem_Free(ptr);
ptr = PyObject_Malloc(0);
if (ptr == NULL) {
PyErr_SetString(PyExc_RuntimeError,
"PyObject_Malloc(0) returns NULL");
return NULL;
}
PyObject_Free(ptr);
ptr = PyObject_Calloc(0, 0);
if (ptr == NULL) {
PyErr_SetString(PyExc_RuntimeError,
"PyObject_Calloc(0, 0) returns NULL");
return NULL;
}
PyObject_Free(ptr);
Py_RETURN_NONE;
}
static PyObject *
test_pymem_getallocatorsname(PyObject *self, PyObject *args)
{
const char *name = _PyMem_GetCurrentAllocatorName();
if (name == NULL) {
PyErr_SetString(PyExc_RuntimeError, "cannot get allocators name");
return NULL;
}
return PyUnicode_FromString(name);
}
static PyObject *
test_pymem_setrawallocators(PyObject *self, PyObject *Py_UNUSED(ignored))
{
return test_setallocators(PYMEM_DOMAIN_RAW);
}
static PyObject *
test_pymem_setallocators(PyObject *self, PyObject *Py_UNUSED(ignored))
{
return test_setallocators(PYMEM_DOMAIN_MEM);
}
static PyObject *
pyobject_malloc_without_gil(PyObject *self, PyObject *args)
{
char *buffer;
/* Deliberate bug to test debug hooks on Python memory allocators:
call PyObject_Malloc() without holding the GIL */
Py_BEGIN_ALLOW_THREADS
buffer = PyObject_Malloc(10);
Py_END_ALLOW_THREADS
PyObject_Free(buffer);
Py_RETURN_NONE;
}
static PyObject *
pymem_buffer_overflow(PyObject *self, PyObject *args)
{
char *buffer;
/* Deliberate buffer overflow to check that PyMem_Free() detects
the overflow when debug hooks are installed. */
buffer = PyMem_Malloc(16);
if (buffer == NULL) {
PyErr_NoMemory();
return NULL;
}
buffer[16] = 'x';
PyMem_Free(buffer);
Py_RETURN_NONE;
}
static PyObject *
pymem_api_misuse(PyObject *self, PyObject *args)
{
char *buffer;
/* Deliberate misusage of Python allocators:
allococate with PyMem but release with PyMem_Raw. */
buffer = PyMem_Malloc(16);
PyMem_RawFree(buffer);
Py_RETURN_NONE;
}
static PyObject *
pymem_malloc_without_gil(PyObject *self, PyObject *args)
{
char *buffer;
/* Deliberate bug to test debug hooks on Python memory allocators:
call PyMem_Malloc() without holding the GIL */
Py_BEGIN_ALLOW_THREADS
buffer = PyMem_Malloc(10);
Py_END_ALLOW_THREADS
PyMem_Free(buffer);
Py_RETURN_NONE;
}
static PyObject *
test_pyobject_is_freed(const char *test_name, PyObject *op)
{
if (!_PyObject_IsFreed(op)) {
PyErr_SetString(PyExc_AssertionError,
"object is not seen as freed");
return NULL;
}
Py_RETURN_NONE;
}
static PyObject *
check_pyobject_null_is_freed(PyObject *self, PyObject *Py_UNUSED(args))
{
PyObject *op = NULL;
return test_pyobject_is_freed("check_pyobject_null_is_freed", op);
}
static PyObject *
check_pyobject_uninitialized_is_freed(PyObject *self,
PyObject *Py_UNUSED(args))
{
PyObject *op = (PyObject *)PyObject_Malloc(sizeof(PyObject));
if (op == NULL) {
return NULL;
}
/* Initialize reference count to avoid early crash in ceval or GC */
Py_SET_REFCNT(op, 1);
/* object fields like ob_type are uninitialized! */
return test_pyobject_is_freed("check_pyobject_uninitialized_is_freed", op);
}
static PyObject *
check_pyobject_forbidden_bytes_is_freed(PyObject *self,
PyObject *Py_UNUSED(args))
{
/* Allocate an incomplete PyObject structure: truncate 'ob_type' field */
PyObject *op = (PyObject *)PyObject_Malloc(offsetof(PyObject, ob_type));
if (op == NULL) {
return NULL;
}
/* Initialize reference count to avoid early crash in ceval or GC */
Py_SET_REFCNT(op, 1);
/* ob_type field is after the memory block: part of "forbidden bytes"
when using debug hooks on memory allocators! */
return test_pyobject_is_freed("check_pyobject_forbidden_bytes_is_freed", op);
}
static PyObject *
check_pyobject_freed_is_freed(PyObject *self, PyObject *Py_UNUSED(args))
{
/* This test would fail if run with the address sanitizer */
#ifdef _Py_ADDRESS_SANITIZER
Py_RETURN_NONE;
#else
PyObject *op = PyObject_CallNoArgs((PyObject *)&PyBaseObject_Type);
if (op == NULL) {
return NULL;
}
Py_TYPE(op)->tp_dealloc(op);
/* Reset reference count to avoid early crash in ceval or GC */
Py_SET_REFCNT(op, 1);
/* object memory is freed! */
return test_pyobject_is_freed("check_pyobject_freed_is_freed", op);
#endif
}
static PyMethodDef test_methods[] = {
{"check_pyobject_forbidden_bytes_is_freed",
check_pyobject_forbidden_bytes_is_freed, METH_NOARGS},
{"check_pyobject_freed_is_freed", check_pyobject_freed_is_freed, METH_NOARGS},
{"check_pyobject_null_is_freed", check_pyobject_null_is_freed, METH_NOARGS},
{"check_pyobject_uninitialized_is_freed",
check_pyobject_uninitialized_is_freed, METH_NOARGS},
{"pymem_api_misuse", pymem_api_misuse, METH_NOARGS},
{"pymem_buffer_overflow", pymem_buffer_overflow, METH_NOARGS},
{"pymem_getallocatorsname", test_pymem_getallocatorsname, METH_NOARGS},
{"pymem_malloc_without_gil", pymem_malloc_without_gil, METH_NOARGS},
{"pyobject_malloc_without_gil", pyobject_malloc_without_gil, METH_NOARGS},
{"remove_mem_hooks", remove_mem_hooks, METH_NOARGS,
PyDoc_STR("Remove memory hooks.")},
{"set_nomemory", (PyCFunction)set_nomemory, METH_VARARGS,
PyDoc_STR("set_nomemory(start:int, stop:int = 0)")},
{"test_pymem_alloc0", test_pymem_alloc0, METH_NOARGS},
{"test_pymem_setallocators", test_pymem_setallocators, METH_NOARGS},
{"test_pymem_setrawallocators", test_pymem_setrawallocators, METH_NOARGS},
{"test_pyobject_new", test_pyobject_new, METH_NOARGS},
{"test_pyobject_setallocators", test_pyobject_setallocators, METH_NOARGS},
{NULL},
};
int
_PyTestCapi_Init_Mem(PyObject *mod)
{
if (PyModule_AddFunctions(mod, test_methods) < 0) {
return -1;
}
PyObject *v;
#ifdef WITH_PYMALLOC
v = Py_NewRef(Py_True);
#else
v = Py_NewRef(Py_False);
#endif
int rc = PyModule_AddObjectRef(mod, "WITH_PYMALLOC", v);
Py_DECREF(v);
if (rc < 0) {
return -1;
}
return 0;
}
|