|
| 1 | +//! add-flags.py(LDFLAGS): -Wl,--stack-first -Wl,--initial-memory=327680 |
| 2 | + |
| 3 | +#include <__macro_PAGESIZE.h> |
| 4 | +#include <stddef.h> |
| 5 | +#include <stdio.h> |
| 6 | +#include <string.h> |
| 7 | + |
| 8 | +void test(char *ptr, size_t length, void *want) { |
| 9 | + void *got = memchr(ptr, 7, length); |
| 10 | + if (got != want) { |
| 11 | + printf("memchr(%p, 7, %lu) = %p, want %p\n", ptr, length, got, want); |
| 12 | + } |
| 13 | +} |
| 14 | + |
| 15 | +int main(void) { |
| 16 | + char *const LIMIT = (char *)(__builtin_wasm_memory_size(0) * PAGESIZE); |
| 17 | + |
| 18 | + for (size_t length = 0; length < 64; length++) { |
| 19 | + for (size_t alignment = 0; alignment < 24; alignment++) { |
| 20 | + for (ptrdiff_t pos = -2; pos < length + 2; pos++) { |
| 21 | + // Create a buffer with the given length, at a pointer with the given |
| 22 | + // alignment. Using the offset LIMIT - PAGESIZE - 8 means many buffers |
| 23 | + // will straddle a (Wasm, and likely OS) page boundary. Place the |
| 24 | + // character to find at every position in the buffer, including just |
| 25 | + // prior to it and after its end. |
| 26 | + char *ptr = LIMIT - PAGESIZE - 8 + alignment; |
| 27 | + memset(LIMIT - 2 * PAGESIZE, 0, 2 * PAGESIZE); |
| 28 | + memset(ptr, 5, length); |
| 29 | + ptr[pos] = 7; |
| 30 | + |
| 31 | + // The first instance of the character is found. |
| 32 | + if (pos >= 0) ptr[pos + 2] = 7; |
| 33 | + |
| 34 | + // The character is found if it's within range. |
| 35 | + test(ptr, length, 0 <= pos && pos < length ? &ptr[pos] : NULL); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + // Ensure we never read past the end of memory. |
| 40 | + char *ptr = LIMIT - length; |
| 41 | + memset(LIMIT - 2 * PAGESIZE, 0, 2 * PAGESIZE); |
| 42 | + memset(ptr, 5, length); |
| 43 | + ptr[length - 1] = 7; |
| 44 | + |
| 45 | + // Nothing found on an empty buffer. |
| 46 | + test(ptr, length, length != 0 ? &ptr[length - 1] : NULL); |
| 47 | + |
| 48 | + // Test for length overflow. |
| 49 | + if (length > 0) test(ptr, SIZE_MAX, &ptr[length - 1]); |
| 50 | + } |
| 51 | + |
| 52 | + return 0; |
| 53 | +} |
0 commit comments