-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbitflip.c
52 lines (43 loc) · 1.28 KB
/
bitflip.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <sys/mman.h>
// https://stackoverflow.com/questions/9596945/how-to-get-appropriate-timestamp-in-c-for-logs
char *timestamp()
{
time_t ltime; /* calendar time */
ltime = time(NULL); /* get current cal time */
return asctime(localtime(<ime));
}
int main() {
size_t bytes = 1073741824;
unsigned int tests = 0;
unsigned char total = 0;
printf("=== Bitflipped ===\n");
printf("==================\n");
printf("Allocating a gigabyte ...\n");
unsigned char *buffer = (unsigned char *)calloc(bytes, 1);
// Ensure the buffer is actually resident in RAM
mlock(buffer, bytes);
memset(buffer, 0, bytes);
printf("Run started: %s\n", timestamp());
fflush(stdout);
while (total == 0) {
// We aren't going to miss a bitflip by being slow
sleep(30);
// Naively walk through and tally all zero bytes
for (size_t i = 0; i < bytes; ++i) {
total += buffer[i];
}
// Keep the user sane that it isn't frozen :)
fprintf(stderr, "\rTest run #%d", tests);
++tests;
}
printf("--- !!! ---");
printf("Error detected: %s\n", timestamp());
printf("Result should be 0 but is %d\n", total);
printf("Total tests run: %d\n", tests);
fflush(stdout);
}