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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
|
#define _POSIX_C_SOURCE 199309L
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <getopt.h>
#include <fcntl.h>
#include <unistd.h>
#define ARGV0 "bitsquat"
static struct {
union {
bool help:1;
} flags;
size_t size;
double report_int;
uint32_t fault_p;
} opts;
static struct {
int fd;
} g;
static void init_opts (void) {
opts.report_int = 1.0;
}
static void init_g (void) {
g.fd = -1;
}
static bool parse_opts (const int argc, const char **argv) {
int fr;
double tmp_f;
while (true) {
fr = getopt(argc, (char*const*)argv, "hs:F:i:");
if (fr == -1) {
break;
}
switch (fr) {
case 'h': opts.flags.help = true; break;
case 's':
opts.size = 0;
fr = sscanf(optarg, "%zu", &opts.size);
if (fr != 1 || opts.size == 0) {
fprintf(stderr, ARGV0 ": invalid -s option: %s\n", optarg);
return false;
}
break;
case 'F':
tmp_f = 0.0;
fr = sscanf(optarg, "%lf", &tmp_f);
if (fr != 1) {
fprintf(stderr, ARGV0 ": invalid -F option: %s\n", optarg);
return false;
}
opts.fault_p = (uint32_t)(tmp_f * UINT32_MAX);
break;
case 'i':
opts.report_int = NAN;
fr = sscanf(optarg, "%lf", &opts.report_int);
// accept "inf" for disabling report
if (fr != 1 || isnan(opts.report_int)) {
fprintf(stderr, ARGV0 ": invalid -i option: %s\n", optarg);
return false;
}
break;
case '?':
return false;
}
}
if (opts.size == 0) {
fprintf(stderr, ARGV0 ": -s option required\n");
return false;
}
return true;
}
static void print_help (void) {
// TODO
}
static bool alloc_g (void) {
g.fd = open("/dev/urandom", O_RDONLY);
if (g.fd < 0) {
perror(ARGV0 ": /dev/urandom");
return false;
}
return true;
}
static void dealloc_g (void) {
if (g.fd >= 0) {
close(g.fd);
g.fd = -1;
}
}
static void do_report (const struct timespec *ts, const size_t itc) {
fprintf(
stderr, ARGV0 ": %10zu.%06ld %20zu\n",
(size_t)ts->tv_sec,
ts->tv_nsec / 1000,
itc);
}
static void ts_sub (
struct timespec *c,
const struct timespec *a,
const struct timespec *b)
{
c->tv_nsec = a->tv_nsec - b->tv_nsec;
c->tv_sec = a->tv_sec - b->tv_sec;
if (c->tv_nsec < 0) {
c->tv_sec -= 1;
c->tv_nsec += 1000000000;
}
}
static double ts2d (const struct timespec *ts) {
return ts->tv_nsec / 1000000000 + ts->tv_sec;
}
static void dump_error (
const size_t itc,
const struct timespec *ts,
const uint8_t *a,
const uint8_t *b,
const size_t l)
{
printf(
"iteration: %10zu.%06ld %zu\n",
(size_t)ts->tv_sec,
ts->tv_nsec / 1000,
itc);
for (size_t i = 0; i < l; i += 1) {
if (a[i] == b[i]) {
continue;
}
printf(" - offset: %zu\n", i);
printf(" a: 0x%X\n", a[i]);
printf(" b: 0x%X\n", b[i]);
}
}
static void getrnd (void *out, const size_t l) {
const ssize_t fr = read(g.fd, out, l);
if (fr < 0) {
perror(ARGV0 ": getrnd()");
abort();
}
if ((size_t)fr != l) {
abort();
}
}
static bool cointoss (void) {
uint32_t rnd;
if (opts.fault_p == 0) {
return false;
}
getrnd(&rnd, sizeof(rnd));
return opts.fault_p >= rnd;
}
static void inject_fault (uint8_t *m, const size_t l) {
size_t rnd;
getrnd(&rnd, sizeof(rnd));
rnd %= l;
m[rnd] = ~m[rnd];
}
static char *getctime (void) {
const time_t t = time(NULL);
return ctime(&t);
}
static int do_main (void) {
int ec = 0;
int fr;
void *m1 = NULL;
void *m2 = NULL;
const size_t s = opts.size;
struct {
struct timespec tp_start;
struct timespec tp_now;
struct timespec tp_report;
struct timespec d_start;
struct timespec d_report;
} ts;
size_t itc = 0; // iteration counter
int bp; // bit pattern
fprintf(stderr, ARGV0 ": allocating (%zu * 2) bytes of memory ...\n", s);
m1 = malloc(s);
m2 = malloc(s);
if (m1 == NULL || m2 == NULL) {
perror(ARGV0);
goto END;
}
bp = 0xCD;
memset(m1, bp, s);
memset(m2, bp, s);
fprintf(stderr, ARGV0 ": loop start: %s\n", getctime());
clock_gettime(CLOCK_MONOTONIC, &ts.tp_start);
ts.tp_report = ts.tp_start;
while (true) {
switch (itc % 2) {
case 0: bp = 0x55; break;
case 1: bp = 0xAA; break;
}
memset(m1, bp, s);
memset(m2, bp, s);
if (cointoss()) {
inject_fault((uint8_t*)m1, s);
}
if (cointoss()) {
inject_fault((uint8_t*)m2, s);
}
fr = memcmp(m1, m2, s);
itc += 1;
clock_gettime(CLOCK_MONOTONIC, &ts.tp_now);
ts_sub(&ts.d_start, &ts.tp_now, &ts.tp_start);
ts_sub(&ts.d_report, &ts.tp_now, &ts.tp_report);
if (fr != 0) {
dump_error(
itc,
&ts.d_start,
(const uint8_t*)m1,
(const uint8_t*)m2,
s);
ec = 1;
goto END;
}
if (ts2d(&ts.d_report) >= opts.report_int) {
do_report(&ts.d_start, itc);
ts.tp_report = ts.tp_now;
}
}
END:
free(m1);
free(m2);
return ec;
}
int main (const int argc, const char **argv) {
int ec = 0;
init_opts();
init_g();
if (!parse_opts(argc, argv)) {
ec = 2;
goto END;
}
if (opts.flags.help) {
print_help();
goto END;
}
if (!alloc_g()) {
ec = 1;
goto END;
}
ec = do_main();
END:
dealloc_g();
return ec;
}
|