|
| 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 | +int sign(int val) { |
| 9 | + return (0 < val) - (val < 0); |
| 10 | +} |
| 11 | +void test(char *ptr1, char *ptr2, size_t length, int want) { |
| 12 | + int got = memcmp(ptr1, ptr2, length); |
| 13 | + if (sign(got) != sign(want)) { |
| 14 | + printf("memcmp(%p, %p, %lu) = %d, want %d\n", ptr1, ptr2, length, got, |
| 15 | + want); |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +int main(void) { |
| 20 | + char *const LIMIT = (char *)(__builtin_wasm_memory_size(0) * PAGESIZE); |
| 21 | + |
| 22 | + for (ptrdiff_t length = 0; length < 64; length++) { |
| 23 | + for (ptrdiff_t alignment = 0; alignment < 24; alignment++) { |
| 24 | + for (ptrdiff_t pos = -2; pos < length + 2; pos++) { |
| 25 | + // Create a buffer with the given length, at a pointer with the given |
| 26 | + // alignment. Using the offset LIMIT - PAGESIZE - 8 means many buffers |
| 27 | + // will straddle a (Wasm, and likely OS) page boundary. |
| 28 | + // The second buffer has a fixed address, which means it won't |
| 29 | + // always share alignment with first buffer. |
| 30 | + // Place the difference to find at every position in the buffers, |
| 31 | + // including just prior to it and after its end. |
| 32 | + char *ptr1 = LIMIT - PAGESIZE - 8 + alignment; |
| 33 | + char *ptr2 = LIMIT - PAGESIZE / 2; |
| 34 | + memset(LIMIT - 2 * PAGESIZE, 0, 2 * PAGESIZE); |
| 35 | + memset(ptr1, 5, length); |
| 36 | + memset(ptr2, 5, length); |
| 37 | + |
| 38 | + ptr1[pos] = 7; |
| 39 | + ptr2[pos] = 3; |
| 40 | + |
| 41 | + test(ptr1, ptr2, length, |
| 42 | + 0 <= pos && pos < length ? ptr1[pos] - ptr2[pos] : 0); |
| 43 | + test(ptr2, ptr1, length, |
| 44 | + 0 <= pos && pos < length ? ptr2[pos] - ptr1[pos] : 0); |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + return 0; |
| 50 | +} |
0 commit comments