|
| 1 | +// SPDX-License-Identifier: BSD-3-Clause |
| 2 | +/* Copyright 2019-2024, Intel Corporation */ |
| 3 | + |
| 4 | +#include <errno.h> |
| 5 | + |
| 6 | +#include "alloc.h" |
| 7 | +#include <string.h> |
| 8 | + |
| 9 | +// #include "fault_injection.h" |
| 10 | + |
| 11 | +Malloc_func fn_malloc = malloc; |
| 12 | +Realloc_func fn_realloc = realloc; |
| 13 | + |
| 14 | +#if FAULT_INJECTION |
| 15 | +#include "log_internal.h" |
| 16 | + |
| 17 | +static __thread int malloc_num; |
| 18 | +static __thread int fail_malloc_num; |
| 19 | +static __thread const char *fail_malloc_from; |
| 20 | + |
| 21 | +void * |
| 22 | +_flt_Malloc(size_t size, const char *func) |
| 23 | +{ |
| 24 | + if (fail_malloc_from && strcmp(func, fail_malloc_from) == 0) { |
| 25 | + if (++malloc_num == fail_malloc_num) { |
| 26 | + errno = ENOMEM; |
| 27 | + return NULL; |
| 28 | + } |
| 29 | + } |
| 30 | + return fn_malloc(size); |
| 31 | +} |
| 32 | + |
| 33 | +static __thread int realloc_num; |
| 34 | +static __thread int fail_realloc_num; |
| 35 | +static __thread const char *fail_realloc_from; |
| 36 | + |
| 37 | +void * |
| 38 | +_flt_Realloc(void *ptr, size_t size, const char *func) |
| 39 | +{ |
| 40 | + if (fail_realloc_from && strcmp(func, fail_realloc_from) == 0) { |
| 41 | + if (++realloc_num == fail_realloc_num) { |
| 42 | + errno = ENOMEM; |
| 43 | + return NULL; |
| 44 | + } |
| 45 | + } |
| 46 | + return fn_realloc(ptr, size); |
| 47 | +} |
| 48 | + |
| 49 | +void |
| 50 | +core_inject_fault_at(enum pmem_allocation_type type, int nth, const char *at) |
| 51 | +{ |
| 52 | + switch (type) { |
| 53 | + case PMEM_MALLOC: |
| 54 | + malloc_num = 0; |
| 55 | + fail_malloc_num = nth; |
| 56 | + fail_malloc_from = at; |
| 57 | + break; |
| 58 | + case PMEM_REALLOC: |
| 59 | + realloc_num = 0; |
| 60 | + fail_realloc_num = nth; |
| 61 | + fail_realloc_from = at; |
| 62 | + break; |
| 63 | + default: |
| 64 | + CORE_LOG_FATAL("unknown allocation type"); |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +int |
| 69 | +core_fault_injection_enabled(void) |
| 70 | +{ |
| 71 | + return 1; |
| 72 | +} |
| 73 | +#else |
| 74 | +void *_Malloc(size_t size) { |
| 75 | + return fn_malloc(size); |
| 76 | +} |
| 77 | + |
| 78 | +void *_Realloc(void *ptr, size_t size) { |
| 79 | + return fn_realloc(ptr, size); |
| 80 | +} |
| 81 | +#endif |
| 82 | + |
| 83 | +void set_func_malloc(void *(*malloc_func)(size_t size)) { |
| 84 | + fn_malloc = (malloc_func == NULL) ? malloc : malloc_func; |
| 85 | +} |
| 86 | + |
| 87 | +void set_func_realloc(void *(*realloc_func)(void *ptr, size_t size)) { |
| 88 | + fn_realloc = (realloc_func == NULL) ? realloc : realloc_func; |
| 89 | +} |
| 90 | + |
| 91 | +/* |
| 92 | + * our versions of malloc & friends start off pointing to the libc versions |
| 93 | + */ |
| 94 | +Free_func Free = free; |
| 95 | +Strdup_func Strdup = strdup; |
| 96 | + |
| 97 | +/* |
| 98 | + * Zalloc -- allocate zeroed memory |
| 99 | + */ |
| 100 | +void * |
| 101 | +Zalloc(size_t sz) |
| 102 | +{ |
| 103 | + void *ret = Malloc(sz); |
| 104 | + if (!ret) |
| 105 | + return NULL; |
| 106 | + return memset(ret, 0, sz); |
| 107 | +} |
| 108 | + |
| 109 | +/* |
| 110 | + * util_set_alloc_funcs -- allow one to override malloc, etc. |
| 111 | + */ |
| 112 | +void |
| 113 | +util_set_alloc_funcs(void *(*malloc_func)(size_t size), |
| 114 | + void (*free_func)(void *ptr), |
| 115 | + void *(*realloc_func)(void *ptr, size_t size), |
| 116 | + char *(*strdup_func)(const char *s)) |
| 117 | +{ |
| 118 | + set_func_malloc(malloc_func); |
| 119 | + Free = (free_func == NULL) ? free : free_func; |
| 120 | + set_func_realloc(realloc_func); |
| 121 | + Strdup = (strdup_func == NULL) ? strdup : strdup_func; |
| 122 | +} |
0 commit comments