-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathparams.c
210 lines (198 loc) · 6.42 KB
/
params.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
* Copyright (c) 2016 Andrei Tatar
* Copyright (c) 2017-2018 Vrije Universiteit Amsterdam
* Copyright (c) 2023 Ingab Kang
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "include/params.h"
#include "include/utils.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <errno.h>
#include <getopt.h>
void print_usage(char *bin_name)
{
fprintf(stderr,
"[ HELP ] - Usage ./%s [-h] [-r rounds] [-a aggr] [-o o_file] [-v] [-m] [-d] [-c] [--mem mem_size] [--[huge/HUGE] f_name] [--conf f_name] [--align val] [--off val] [--no-overwrite] [--fuzzing]\n",
bin_name);
fprintf(stderr, "\t-h\t\t\t= this help message\n");
fprintf(stderr, "\t-v\t\t\t= verbose\n");
fprintf(stderr, "\t-r rounds\t\t= number of rounds per tuple\t\t\t(default: %d)\n", ROUNDS_std);
//fprintf(stderr, "\t-a --aggr\t\t= number of aggressors\t\t\t\t(default: %d)\n", AGGR_std);
fprintf(stderr, "\t-o --o_file_prefix\t= prefix for output files\t\t\t(default: %s)\n", O_FILE_std);
//fprintf(stderr, "\t--mem mem_size\t\t= allocation size\t\t\t\t(default: %ld)\n",
//(uint64_t) ALLOC_SIZE);
fprintf(stderr, "\t--huge f_name\t\t= hugetlbfs entry (1GB if HUGE)\t\t\t(default: %s)\n",
HUGETLB_std);
//fprintf(stderr, "\t--conf f_name\t\t= SessionConfig file\t\t\t\t(default: %s)\n",
//CONFIG_NAME_std);
//fprintf(stderr, "\t--align val\t\t= alignment of the buffer\t\t\t(default: %ld)\n",
//(uint64_t) ALIGN_std);
//fprintf(stderr, "\t--off val\t\t= offset from first row\t\t\t\t(default: 0)\n");
//fprintf(stderr, "\t--no-overwrite\t\t= don't overwrite previous file\n");
//fprintf(stderr, "\t-V --victim-pattern\t= hex value for the victim patter\n");
//fprintf(stderr, "\t-T --target-pattern\t= hex value for the target pattern\n");
//fprintf(stderr, "\t-f --fuzzing\t\t= Start fuzzing (--aggr will be ignored)\n");
//fprintf(stderr, "\t-f\t\t\t= Start fuzzing (--aggr will be ignored)\n");
//fprintf(stderr, "\t-g\t\t\t= Run group hammer\n");
fprintf(stderr, "\t-t --threshold\t\t= Align the hammering to refresh ops,\n\t\t\t\t looking at the memory latency in CPU cycles.\t(default: 0)\n");
//fprintf(stderr, "\t-d\t\t\t= Distance between sets\n");
}
static int str2pat(const char *str, char **pat)
{
char *endp = NULL;
char tmp[3];
char *p;
tmp[2] = '\0';
size_t len = strlen(str);
if (len % 2) {
return EINVAL;
}
len /= 2;
p = (char *)malloc(len);
for (size_t i = 0; i < len; i++) {
tmp[0] = str[2 * i];
tmp[1] = str[2 * i + 1];
errno = 0;
((uint8_t *) p)[i] = (uint8_t) strtol(tmp, &endp, 16);
if (errno) {
free(p);
return errno;
}
if (*endp != '\0') {
free(p);
return EINVAL;
}
}
*pat = p;
return 0;
}
int process_argv(int argc, char *argv[], ProfileParams *p)
{
/* Default */
p->g_flags = 0;
p->tpat = (char *)NULL;
p->vpat = (char *)NULL;
p->threshold = 0;
p->hsess = N_SIDED_HAMMER;
p->dist = -1;
p->vics = -1;
p->m_size = ALLOC_SIZE;
p->m_align = ALIGN_std;
p->rounds = ROUNDS_std;
p->base_off = 0;
p->huge_file = (char *)HUGETLB_std;
p->conf_file = (char *)CONFIG_NAME_std;
p->aggr = AGGR_std;
p->find_vic = 0;
const struct option long_options[] = {
/* These options set a flag. */
//{"mem", required_argument, 0, 0},
//{"align", required_argument, 0, 0},
//{"huge", optional_argument, 0, 0},
//{"HUGE", optional_argument, 0, 0},
//{"conf", optional_argument, 0, 0},
//{"off", required_argument, 0, 0},
//{"no-overwrite", no_argument, 0, 0},
//{.name = "target-pattern",.has_arg = required_argument,.flag = NULL,.val='T'},
//{.name = "victim-pattern",.has_arg = required_argument,.flag = NULL,.val = 'V'},
//{.name = "aggr",.has_arg = required_argument,.flag = NULL,.val='a'},
//{.name = "find_vic",.has_arg = no_argument,.flag = &p->find_vic, .val = 1},
//{.name = "dist",.has_arg = required_argument,.flag = NULL,.val ='d'},
//{.name = "vics",.has_arg = required_argument,.flag = NULL,.val ='c'},
{.name = "threshold",.has_arg = required_argument,.flag = NULL,.val = 't'},
{0, 0, 0, 0}
};
p->g_out_prefix = (char *)O_FILE_std;
p->g_flags |= F_POPULATE;
while (1) {
int this_option_optind = optind ? optind : 1;
int option_index = 0;
//int arg = getopt_long(argc, argv, "o:r:hvV:T:a:fgc:d:t:F",
// long_options, &option_index);
int arg = getopt_long(argc, argv, "o:vt:",
long_options, &option_index);
if (arg == -1)
break;
switch (arg) {
//case 0:
// switch (option_index) {
// case 0:
// p->m_size = atoi(optarg);
// break;
// case 1:
// p->m_align = atoi(optarg);
// break;
// case 2:
// p->g_flags |= F_ALLOC_HUGE_2M;
// case 3:
// p->huge_file = (char *)malloc(sizeof(char) * strlen(optarg));
// strncpy(p->huge_file, optarg, strlen(optarg));
// p->g_flags |= F_ALLOC_HUGE_1G;
// break;
// case 4:
// p->g_flags |= F_CONFIG;
// if (!optarg)
// break;
// p->conf_file = (char *)malloc(sizeof(char) * strlen(optarg));
// strncpy(p->conf_file, optarg, strlen(optarg));
// break;
// case 5:
// p->base_off = atoi(optarg);
// break;
// case 6:
// p->g_flags |= F_NO_OVERWRITE;
// break;
// default:
// break;
// }
// break;
case 'o':
p->g_out_prefix = (char *)malloc(sizeof(char) * strlen(optarg));
strncpy(p->g_out_prefix, optarg, strlen(optarg));
p->g_flags |= F_EXPORT;
break;
case 'r':
p->rounds = atoi(optarg);
break;
case 'v':
p->g_flags |= F_VERBOSE;
break;
case 't':
p->threshold = atoi(optarg);
break;
case 'h':
default:
print_usage(argv[0]);
return -1;
}
}
#ifdef USE_1GB
p->g_flags |= F_ALLOC_HUGE_1G;
#endif
if (p->g_flags & (F_ALLOC_HUGE_2M | F_ALLOC_HUGE_1G)) {
if ((p->huge_fd = open(p->huge_file, O_CREAT | O_RDWR, 0755)) == -1) {
perror("[ERROR] - Unable to open hugetlbfs");
return -1;
}
}
return 0;
}