#include #include #include #include #include #include #define LEN 256 #define PRINT_STRIDE 0x20 static unsigned char data[LEN]; static unsigned char val = 0; static unsigned char prev_data[LEN]; static unsigned char map_copy[LEN]; int main(int argc, char *argv[]) { unsigned long n; int h; unsigned char *map; unsigned start, end, i, j; unsigned int seed = 0; if (argc < 2) fprintf(stderr, "argc\n"), exit(1); h = open(argv[1], O_RDWR | O_DSYNC); if (h == -1) perror("open"), exit(1); map = mmap(NULL, LEN, PROT_READ | PROT_WRITE, MAP_SHARED, h, argc >= 3 ? strtoull(argv[2], NULL, 16) : 0); if (map == MAP_FAILED) perror("mmap"), exit(1); sleep(1); rep: n = 0; srandom(seed); memset(data, 0, LEN); memset(prev_data, 0, LEN); memset(map, 0, LEN); while (1) { start = (unsigned)random() % LEN; end = (unsigned)random() % LEN; if (start > end) continue; for (i = start; i < end; i++) data[i] = val++; memcpy(map + start, data + start, end - start); /*usleep(10000);*/ if (memcmp(map, data, LEN)) break; memcpy(prev_data, data, LEN); n++; } memcpy(map_copy, map, LEN); fprintf(stderr, "mismatch after %lu loops! seed %u\n", n, seed); fprintf(stderr, "last copied range: 0x%x - 0x%x (0x%x)\n", start, end, (unsigned)(end - start)); for (j = 0; j < LEN; j += PRINT_STRIDE) { fprintf(stderr, "p[%03x]", j); for (i = j; i < j + PRINT_STRIDE && i < LEN; i++) fprintf(stderr, "%s%s%02x", !(i % 4) ? " " : "", data[i] != map_copy[i] ? "*" : " ", prev_data[i]); fprintf(stderr, "\n"); fprintf(stderr, "d[%03x]", j); for (i = j; i < j + PRINT_STRIDE && i < LEN; i++) fprintf(stderr, "%s%s%02x", !(i % 4) ? " " : "", data[i] != map_copy[i] ? "*" : " ", data[i]); fprintf(stderr, "\n"); fprintf(stderr, "m[%03x]", j); for (i = j; i < j + PRINT_STRIDE && i < LEN; i++) fprintf(stderr, "%s%s%02x", !(i % 4) ? " " : "", data[i] != map_copy[i] ? "*" : " ", map_copy[i]); fprintf(stderr, "\n\n"); } seed++; goto rep; }