|
| 1 | +#include <malloc.h> |
| 2 | +#include <string.h> |
| 3 | +#include <stdlib.h> |
| 4 | +#include <stdbool.h> |
| 5 | + |
| 6 | +#include "../utils.h" |
| 7 | + |
| 8 | +#define MAX_LEN 8192 |
| 9 | +#define MAX_OFFSET 16 |
| 10 | +#define MIN_REDZONE 128 |
| 11 | +#define BUFLEN (MAX_LEN+MAX_OFFSET+2*MIN_REDZONE) |
| 12 | +#define POISON 0xa5 |
| 13 | + |
| 14 | +unsigned long COPY_LOOP(void *to, const void *from, unsigned long size); |
| 15 | + |
| 16 | +static void do_one(char *src, char *dst, unsigned long src_off, |
| 17 | + unsigned long dst_off, unsigned long len, void *redzone, |
| 18 | + void *fill) |
| 19 | +{ |
| 20 | + char *srcp, *dstp; |
| 21 | + unsigned long ret; |
| 22 | + unsigned long i; |
| 23 | + |
| 24 | + srcp = src + MIN_REDZONE + src_off; |
| 25 | + dstp = dst + MIN_REDZONE + dst_off; |
| 26 | + |
| 27 | + memset(src, POISON, BUFLEN); |
| 28 | + memset(dst, POISON, BUFLEN); |
| 29 | + memcpy(srcp, fill, len); |
| 30 | + |
| 31 | + ret = COPY_LOOP(dstp, srcp, len); |
| 32 | + if (ret && ret != (unsigned long)dstp) { |
| 33 | + printf("(%p,%p,%ld) returned %ld\n", dstp, srcp, len, ret); |
| 34 | + abort(); |
| 35 | + } |
| 36 | + |
| 37 | + if (memcmp(dstp, srcp, len)) { |
| 38 | + printf("(%p,%p,%ld) miscompare\n", dstp, srcp, len); |
| 39 | + printf("src: "); |
| 40 | + for (i = 0; i < len; i++) |
| 41 | + printf("%02x ", srcp[i]); |
| 42 | + printf("\ndst: "); |
| 43 | + for (i = 0; i < len; i++) |
| 44 | + printf("%02x ", dstp[i]); |
| 45 | + printf("\n"); |
| 46 | + abort(); |
| 47 | + } |
| 48 | + |
| 49 | + if (memcmp(dst, redzone, dstp - dst)) { |
| 50 | + printf("(%p,%p,%ld) redzone before corrupted\n", |
| 51 | + dstp, srcp, len); |
| 52 | + abort(); |
| 53 | + } |
| 54 | + |
| 55 | + if (memcmp(dstp+len, redzone, dst+BUFLEN-(dstp+len))) { |
| 56 | + printf("(%p,%p,%ld) redzone after corrupted\n", |
| 57 | + dstp, srcp, len); |
| 58 | + abort(); |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +int test_copy_loop(void) |
| 63 | +{ |
| 64 | + char *src, *dst, *redzone, *fill; |
| 65 | + unsigned long len, src_off, dst_off; |
| 66 | + unsigned long i; |
| 67 | + |
| 68 | + src = memalign(BUFLEN, BUFLEN); |
| 69 | + dst = memalign(BUFLEN, BUFLEN); |
| 70 | + redzone = malloc(BUFLEN); |
| 71 | + fill = malloc(BUFLEN); |
| 72 | + |
| 73 | + if (!src || !dst || !redzone || !fill) { |
| 74 | + fprintf(stderr, "malloc failed\n"); |
| 75 | + exit(1); |
| 76 | + } |
| 77 | + |
| 78 | + memset(redzone, POISON, BUFLEN); |
| 79 | + |
| 80 | + /* Fill with sequential bytes */ |
| 81 | + for (i = 0; i < BUFLEN; i++) |
| 82 | + fill[i] = i & 0xff; |
| 83 | + |
| 84 | + for (len = 1; len < MAX_LEN; len++) { |
| 85 | + for (src_off = 0; src_off < MAX_OFFSET; src_off++) { |
| 86 | + for (dst_off = 0; dst_off < MAX_OFFSET; dst_off++) { |
| 87 | + do_one(src, dst, src_off, dst_off, len, |
| 88 | + redzone, fill); |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + return 0; |
| 94 | +} |
| 95 | + |
| 96 | +int main(void) |
| 97 | +{ |
| 98 | + return test_harness(test_copy_loop, str(COPY_LOOP)); |
| 99 | +} |
0 commit comments