-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrtl-core.c
192 lines (153 loc) · 5.18 KB
/
rtl-core.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
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
//===----------------------------------------------------------------------===//
//
// LLVM Fault Injection Tool
//
// Runtime Library Core
//
//===----------------------------------------------------------------------===//
//
// Copyright (C) 2019. rollrat. All Rights Reserved.
//
//===----------------------------------------------------------------------===//
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define MAX_INDEX 65535
#define PRINT_MESSAGE 1
#define SETTING_FILE_NAME "setting"
#define RESULT_FILE_NAME "result"
typedef uint32_t major_type;
// profile option
// 0: profile, 1: fault-inject
static int run_fault_injection = 0;
// fault option
static int injection_type = 0;
static int count_of_determine = 0;
// print option
static int trace_function = -1;
static int trace_index = -1;
static int trace_dependency = -1;
static int trace_register = -2;
// profile
static int determine_count = 0;
static FILE *log_file;
void fault_inject_init() {
// Open write file.
log_file = fopen(RESULT_FILE_NAME, "w");
#if PRINT_MESSAGE
fprintf(log_file, "[Fault Injector] RTL-Core Init!\n");
#endif
srand(time(NULL));
// Read setting file.
{
FILE *fp;
char op_name[255];
int op_option = 0;
if ((fp = fopen(SETTING_FILE_NAME, "r")) == NULL) {
fprintf(log_file, "[Fault Injector] Setting file not found.\n");
exit(1);
}
while (fscanf(fp, "%s %d", op_name, &op_option) != EOF) {
#define DECALE_OPTION(name) \
if (!strcmp(op_name, #name)) { \
name = op_option; \
continue; \
}
DECALE_OPTION(run_fault_injection)
DECALE_OPTION(injection_type)
DECALE_OPTION(count_of_determine)
DECALE_OPTION(trace_function)
DECALE_OPTION(trace_index)
DECALE_OPTION(trace_dependency)
DECALE_OPTION(trace_register)
}
fclose(fp);
}
//
// TODO: Initialize vars.
//
if (run_fault_injection == 0) return;
}
static void print_log(uint32_t f_index, uint32_t index, uint32_t reg_num,
uint32_t dependency, uint32_t size, char *value,
char *opcode, int simple, const char *str) {
fprintf(log_file,
"[Fault Injector] %s: f_index=%02d, index=%02d, reg_num=%02d, "
"dependency=%d",
str, f_index, index, reg_num, dependency);
if (simple == 0) {
fprintf(log_file, ", opcode=%7s, size=%02d, value=", opcode, size);
if (size == 1) {
fprintf(log_file, "%d", *value);
} else {
int sz = size / 8;
for (int i = 0; i < sz; i++)
fprintf(log_file, "%02X", value[sz - i - 1] & 0xFF);
}
}
fprintf(log_file, "\n");
}
void fault_inject_trace(uint32_t f_index, uint32_t index, uint32_t reg_num,
uint32_t dependency, uint32_t size, char *value,
char *opcode) {
if (trace_function >= 0 && f_index != trace_function) return;
if (trace_index >= 0 && index != trace_index) return;
if (trace_dependency >= 0 && dependency != trace_dependency) return;
if (trace_register >= -1 && reg_num != trace_register) return;
print_log(f_index, index, reg_num, dependency, size, value, opcode, 0,
"trace");
}
#pragma region Inject Determine
static int determine(double rate) { return (double)rand() / RAND_MAX < rate; }
major_type fault_inject_determine(major_type f_index, major_type index,
major_type reg_num, major_type dependency) {
determine_count += 1;
if (run_fault_injection == 0) return 0;
return determine((double)1 / count_of_determine);
}
#pragma endregion
#pragma region Fault Inject
static int get_fault_inject_pos(int sz) { return rand() % sz; }
static int get_fault_inject_bit() { return rand() % (sizeof(char) * 8); }
void fault_inject_flipbit(char *value) {
*value ^= (1 << get_fault_inject_bit());
}
void fault_inject_set0(char *value) {
*value &= ~(1 << get_fault_inject_bit());
}
void fault_inject_set1(char *value) { *value |= (1 << get_fault_inject_bit()); }
void fault_inject(uint32_t f_index, uint32_t index, uint32_t reg_num,
uint32_t dependency, uint32_t size, char *value,
char *opcode) {
print_log(f_index, index, reg_num, dependency, size, value, opcode, 0,
"inject_before");
if (size == 1)
*value ^= 1;
else {
int pos = get_fault_inject_pos(size / 8);
switch (injection_type) {
case 0:
fault_inject_flipbit(&value[pos]);
break;
case 1:
fault_inject_set0(&value[pos]);
break;
case 2:
fault_inject_set1(&value[pos]);
break;
}
}
print_log(f_index, index, reg_num, dependency, size, value, opcode, 0,
" inject_after");
}
#pragma endregion
void fault_inject_finish() {
#if PRINT_MESSAGE
fprintf(log_file, "[Fault Injector] RTL-Core Finish!\n");
#endif
fprintf(log_file, "[Fault Injector] determine=%d\n", determine_count);
// Save result file.
fclose(log_file);
}