|
| 1 | +// Tests for _Py_ptr_wise_atomic_memmove() in pycore_object.h |
| 2 | + |
| 3 | +#include "parts.h" |
| 4 | +#include "pycore_object.h" // _Py_ptr_wise_atomic_memmove() |
| 5 | +#include "pycore_gc.h" // _PyObject_GC_SET_SHARED() |
| 6 | + |
| 7 | +// Five distinguishable immortal singletons used as placeholder pointers. |
| 8 | +// These require no reference-count management when stored in a raw array. |
| 9 | +#define NPTRS 5 |
| 10 | +static PyObject *test_objs[NPTRS]; |
| 11 | + |
| 12 | +static void |
| 13 | +setup_test_objs(void) |
| 14 | +{ |
| 15 | + test_objs[0] = Py_None; |
| 16 | + test_objs[1] = Py_True; |
| 17 | + test_objs[2] = Py_False; |
| 18 | + test_objs[3] = Py_Ellipsis; |
| 19 | + test_objs[4] = Py_NotImplemented; |
| 20 | +} |
| 21 | + |
| 22 | +// Fill buf[0..NPTRS-1] with test_objs in order. |
| 23 | +static void |
| 24 | +fill_buf(PyObject **buf) |
| 25 | +{ |
| 26 | + for (int i = 0; i < NPTRS; i++) { |
| 27 | + buf[i] = test_objs[i]; |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +// Return a fresh empty PyListObject whose GC SHARED bit is set in |
| 32 | +// Py_GIL_DISABLED builds. This forces _Py_ptr_wise_atomic_memmove() |
| 33 | +// to take the atomic (non-fast) path so we can exercise all branches. |
| 34 | +// In non-GIL builds the function always uses memmove, so no flag is needed. |
| 35 | +static PyObject * |
| 36 | +make_shared_container(void) |
| 37 | +{ |
| 38 | + PyObject *a = PyList_New(0); |
| 39 | + if (a == NULL) { |
| 40 | + return NULL; |
| 41 | + } |
| 42 | +#ifdef Py_GIL_DISABLED |
| 43 | + _PyObject_GC_SET_SHARED(a); |
| 44 | +#endif |
| 45 | + return a; |
| 46 | +} |
| 47 | + |
| 48 | +// Helper: create container, call the function, return it for cleanup. |
| 49 | +// Returns NULL (with exception set) on allocation failure. |
| 50 | +static PyObject * |
| 51 | +call_memmove(PyObject **dest, PyObject **src, Py_ssize_t n) |
| 52 | +{ |
| 53 | + PyObject *a = make_shared_container(); |
| 54 | + if (a != NULL) { |
| 55 | + _Py_ptr_wise_atomic_memmove(a, dest, src, n); |
| 56 | + } |
| 57 | + return a; |
| 58 | +} |
| 59 | + |
| 60 | + |
| 61 | +// dest < src: forward pointer-by-pointer copy. |
| 62 | +// buf = [0,1,2,3,4], copy src=&buf[2] n=3 to dest=&buf[0] |
| 63 | +// Expected result: buf = [2,3,4,3,4] |
| 64 | +static PyObject * |
| 65 | +test_memmove_dest_lt_src(PyObject *self, PyObject *Py_UNUSED(arg)) |
| 66 | +{ |
| 67 | + setup_test_objs(); |
| 68 | + |
| 69 | + PyObject *buf[NPTRS]; |
| 70 | + fill_buf(buf); |
| 71 | + |
| 72 | + PyObject *a = call_memmove(&buf[0], &buf[2], 3); |
| 73 | + if (a == NULL) { |
| 74 | + return NULL; |
| 75 | + } |
| 76 | + |
| 77 | + assert(buf[0] == test_objs[2]); |
| 78 | + assert(buf[1] == test_objs[3]); |
| 79 | + assert(buf[2] == test_objs[4]); |
| 80 | + assert(buf[3] == test_objs[3]); // unchanged |
| 81 | + assert(buf[4] == test_objs[4]); // unchanged |
| 82 | + |
| 83 | + Py_DECREF(a); |
| 84 | + Py_RETURN_NONE; |
| 85 | +} |
| 86 | + |
| 87 | +// dest > src: backward pointer-by-pointer copy. |
| 88 | +// buf = [0,1,2,3,4], copy src=&buf[0] n=3 to dest=&buf[2] |
| 89 | +// Expected result: buf = [0,1,0,1,2] |
| 90 | +static PyObject * |
| 91 | +test_memmove_dest_gt_src(PyObject *self, PyObject *Py_UNUSED(arg)) |
| 92 | +{ |
| 93 | + setup_test_objs(); |
| 94 | + |
| 95 | + PyObject *buf[NPTRS]; |
| 96 | + fill_buf(buf); |
| 97 | + |
| 98 | + PyObject *a = call_memmove(&buf[2], &buf[0], 3); |
| 99 | + if (a == NULL) { |
| 100 | + return NULL; |
| 101 | + } |
| 102 | + |
| 103 | + assert(buf[0] == test_objs[0]); // unchanged |
| 104 | + assert(buf[1] == test_objs[1]); // unchanged |
| 105 | + assert(buf[2] == test_objs[0]); |
| 106 | + assert(buf[3] == test_objs[1]); |
| 107 | + assert(buf[4] == test_objs[2]); |
| 108 | + |
| 109 | + Py_DECREF(a); |
| 110 | + Py_RETURN_NONE; |
| 111 | +} |
| 112 | + |
| 113 | +// dest == src: backward copy where every write is a no-op. |
| 114 | +// buf = [0,1,2,3,4], copy src=&buf[1] n=3 to dest=&buf[1] |
| 115 | +// Expected result: buf unchanged. |
| 116 | +static PyObject * |
| 117 | +test_memmove_dest_eq_src(PyObject *self, PyObject *Py_UNUSED(arg)) |
| 118 | +{ |
| 119 | + setup_test_objs(); |
| 120 | + |
| 121 | + PyObject *buf[NPTRS]; |
| 122 | + fill_buf(buf); |
| 123 | + |
| 124 | + PyObject *a = call_memmove(&buf[1], &buf[1], 3); |
| 125 | + if (a == NULL) { |
| 126 | + return NULL; |
| 127 | + } |
| 128 | + |
| 129 | + for (int i = 0; i < NPTRS; i++) { |
| 130 | + assert(buf[i] == test_objs[i]); |
| 131 | + } |
| 132 | + |
| 133 | + Py_DECREF(a); |
| 134 | + Py_RETURN_NONE; |
| 135 | +} |
| 136 | + |
| 137 | +// Overlapping ranges, dest < src: forward copy is safe. |
| 138 | +// buf = [0,1,2,3,4], copy src=&buf[2] n=3 to dest=&buf[1] |
| 139 | +// Forward: buf[1]=buf[2]=2, buf[2]=buf[3]=3, buf[3]=buf[4]=4 |
| 140 | +// Expected result: buf = [0,2,3,4,4] |
| 141 | +static PyObject * |
| 142 | +test_memmove_overlapping(PyObject *self, PyObject *Py_UNUSED(arg)) |
| 143 | +{ |
| 144 | + setup_test_objs(); |
| 145 | + |
| 146 | + PyObject *buf[NPTRS]; |
| 147 | + fill_buf(buf); |
| 148 | + |
| 149 | + PyObject *a = call_memmove(&buf[1], &buf[2], 3); |
| 150 | + if (a == NULL) { |
| 151 | + return NULL; |
| 152 | + } |
| 153 | + |
| 154 | + assert(buf[0] == test_objs[0]); // unchanged |
| 155 | + assert(buf[1] == test_objs[2]); |
| 156 | + assert(buf[2] == test_objs[3]); |
| 157 | + assert(buf[3] == test_objs[4]); |
| 158 | + assert(buf[4] == test_objs[4]); // unchanged |
| 159 | + |
| 160 | + Py_DECREF(a); |
| 161 | + Py_RETURN_NONE; |
| 162 | +} |
| 163 | + |
| 164 | +// Single owner fast path (GIL_DISABLED only): object owned by this thread |
| 165 | +// and not GC-shared => memmove is used instead of atomic stores. |
| 166 | +// In non-GIL builds this always takes the memmove path, so the test |
| 167 | +// degenerates to a basic correctness check. |
| 168 | +static PyObject * |
| 169 | +test_memmove_single_owner(PyObject *self, PyObject *Py_UNUSED(arg)) |
| 170 | +{ |
| 171 | + setup_test_objs(); |
| 172 | + |
| 173 | + PyObject *a = PyList_New(0); |
| 174 | + if (a == NULL) { |
| 175 | + return NULL; |
| 176 | + } |
| 177 | + |
| 178 | +#ifdef Py_GIL_DISABLED |
| 179 | + // A freelist-reused list may carry the SHARED bit set by a sibling test. |
| 180 | + // Clear it explicitly so this test exercises the single-owner fast path. |
| 181 | + _PyObject_CLEAR_GC_BITS(a, _PyGC_BITS_SHARED); |
| 182 | + assert(_Py_IsOwnedByCurrentThread(a) && !_PyObject_GC_IS_SHARED(a)); |
| 183 | +#endif |
| 184 | + |
| 185 | + PyObject *buf[NPTRS]; |
| 186 | + fill_buf(buf); |
| 187 | + |
| 188 | + _Py_ptr_wise_atomic_memmove(a, &buf[0], &buf[2], 3); |
| 189 | + |
| 190 | + assert(buf[0] == test_objs[2]); |
| 191 | + assert(buf[1] == test_objs[3]); |
| 192 | + assert(buf[2] == test_objs[4]); |
| 193 | + |
| 194 | + Py_DECREF(a); |
| 195 | + Py_RETURN_NONE; |
| 196 | +} |
| 197 | + |
| 198 | + |
| 199 | +static PyMethodDef test_methods[] = { |
| 200 | + {"test_memmove_dest_lt_src", test_memmove_dest_lt_src, METH_NOARGS}, |
| 201 | + {"test_memmove_dest_gt_src", test_memmove_dest_gt_src, METH_NOARGS}, |
| 202 | + {"test_memmove_dest_eq_src", test_memmove_dest_eq_src, METH_NOARGS}, |
| 203 | + {"test_memmove_overlapping", test_memmove_overlapping, METH_NOARGS}, |
| 204 | + {"test_memmove_single_owner", test_memmove_single_owner, METH_NOARGS}, |
| 205 | + {NULL}, |
| 206 | +}; |
| 207 | + |
| 208 | +int |
| 209 | +_PyTestInternalCapi_Init_PtrWiseMemmove(PyObject *mod) |
| 210 | +{ |
| 211 | + return PyModule_AddFunctions(mod, test_methods); |
| 212 | +} |
0 commit comments