Commit 65afc33a authored by Thanassis Tsiodras's avatar Thanassis Tsiodras

Check for potential address space overlaps (prime windows)

parent fccd9669
Pipeline #333 skipped
...@@ -6,6 +6,8 @@ ...@@ -6,6 +6,8 @@
void memcheck() void memcheck()
{ {
//#define MEMCHECK
#ifdef MEMCHECK
uint32_t patterns[] = { uint32_t patterns[] = {
0x0, 0x0,
0xFFFFFFFF, 0xFFFFFFFF,
...@@ -14,6 +16,7 @@ void memcheck() ...@@ -14,6 +16,7 @@ void memcheck()
0xAA55AA55, 0xAA55AA55,
0x55AA55AA, 0x55AA55AA,
}; };
unsigned primes[] = { 3, 7, 31, 127 };
size_t space = 512*1024; size_t space = 512*1024;
void *p = malloc(space); void *p = malloc(space);
int i; int i;
...@@ -36,6 +39,34 @@ void memcheck() ...@@ -36,6 +39,34 @@ void memcheck()
exit(1); exit(1);
} }
// Check potential address space overlaps
printf("[MEMCHECK] Checking for potential address space overlap. Window: ");
for (i=0; i<sizeof(primes)/sizeof(primes[0]); i++) {
uint8_t *pc = p;
unsigned prime = primes[i];
unsigned bytes = space;
unsigned idx = 0;
printf("%s%d", i?", ":"", prime);
fflush(stdout);
while (bytes--) {
*pc++ = (idx % prime);
idx++;
}
pc = p;
bytes = space;
idx = 0;
while (bytes--) {
if (*pc != (idx % prime)) {
printf("[MEMCHECK] Address space wrapped around at 0x%x!\n", (unsigned)pc);
fflush(stdout);
exit(1);
}
pc++;
idx++;
}
}
puts("");
for(i=0; i<sizeof(patterns)/sizeof(patterns[0]); i++) { for(i=0; i<sizeof(patterns)/sizeof(patterns[0]); i++) {
printf("[MEMCHECK] Checking memory with pattern: 0x%x...\n", (unsigned) patterns[i]); printf("[MEMCHECK] Checking memory with pattern: 0x%x...\n", (unsigned) patterns[i]);
uint32_t *pw = p; uint32_t *pw = p;
...@@ -59,4 +90,8 @@ void memcheck() ...@@ -59,4 +90,8 @@ void memcheck()
} }
} }
free(p); free(p);
#else
puts("[MEMCHECK] Memory checks disabled in release mode.");
fflush(stdout);
#endif
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment