#include #include #include #include #include #include #include void hex_dump (char *desc, void *addr, int len) { int i; unsigned char buff[17]; unsigned char *pc = (unsigned char*)addr; // Output description if given. if (desc != NULL) printf ("%s:\n", desc); if (len == 0) { printf(" ZERO LENGTH\n"); return; } if (len < 0) { printf(" NEGATIVE LENGTH: %i\n",len); return; } // Process every byte in the data. for (i = 0; i < len; i++) { // Multiple of 16 means new line (with line offset). if ((i % 16) == 0) { // Just don't print ASCII for the zeroth line. if (i != 0) printf (" %s\n", buff); // Output the offset. printf (" %04x ", i); } // Now the hex code for the specific character. printf (" %02x", pc[i]); // And store a printable ASCII character for later. if ((pc[i] < 0x20) || (pc[i] > 0x7e)) buff[i % 16] = '.'; else buff[i % 16] = pc[i]; buff[(i % 16) + 1] = '\0'; } // Pad out last line if not exactly 16 characters. while ((i % 16) != 0) { printf (" "); i++; } // And print the final ASCII bit. printf (" %s\n", buff); } int main(int argc, char *argv[]) { char hostname[9] = "test"; char *mem; int pages = 1024; int size = sysconf(_SC_PAGESIZE) * pages; int count; int step = 0; int ret; char *org; int dump = 0; mem = (char *)malloc(size); org = (char *)malloc(sysconf(_SC_PAGESIZE)); printf("Setting memory to 0x42\n"); memset(mem, 0x42, size); memset(org, 0x42, sysconf(_SC_PAGESIZE)); printf("mem %p\n", mem); printf("org %p\n", org); while (1) { for (count = 0; count < pages; count+=2) { (mem+(count*sysconf(_SC_PAGESIZE)))[128] = count; } hostname[8] = 0; printf("Checking if org page is still 0x42\n"); for (count = 1; count < sysconf(_SC_PAGESIZE); count++) { if (org[count] != 0x42) { printf("org differs at %d\n", count); } } printf("Checking if all of mem is the same as org\n"); for (count = 0; count < pages; count++) { if (count%2 == 0) org[128] = count; else org[128] = 0x42; ret = memcmp(org, mem+(count*sysconf(_SC_PAGESIZE)), sysconf(_SC_PAGESIZE)); if (ret) { printf("memcmp found difference at %d(%p)\n", count, mem+(count*sysconf(_SC_PAGESIZE))); if (dump < 4) { hex_dump ("page", mem+(count*sysconf(_SC_PAGESIZE)), sysconf(_SC_PAGESIZE)); hex_dump ("org", org, sysconf(_SC_PAGESIZE)); } dump++; } } printf("Step %d on %s\n", step++, hostname); gethostname(hostname, 8); printf("Sleeping 1\n"); sleep(1); dump = 0; } return 0; }