-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiffuzz-client.c
More file actions
843 lines (734 loc) · 24.9 KB
/
diffuzz-client.c
File metadata and controls
843 lines (734 loc) · 24.9 KB
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
#define _GNU_SOURCE
// sched_setaffinity
#include <sched.h>
// popen
#include <stdio.h>
#undef _GNU_SOURCE
#include <arpa/inet.h>
#include <errno.h>
#include <stdlib.h>
#include <stdint.h>
#include <sys/socket.h>
#include <sys/user.h>
#include <asm/ptrace.h>
#include <sys/ptrace.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <time.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/wait.h>
#include <signal.h>
#include <setjmp.h>
#include <sys/time.h>
#include <stdbool.h>
#include <sys/personality.h>
#include <unistd.h>
#include <sys/auxv.h>
#include <linux/auxvec.h>
#include <asm/hwcap.h>
#include <string.h>
#include <zlib.h>
// Normally defined in sys/prctl.h but can't import
// both at the same time for some reason
int prctl (int, ...);
#include <linux/prctl.h>
#include "lib/connection.h"
#include "lib/runner.h"
#include "lib/util.h"
#include "lib/maps.h"
#include "lib/cpuinfo.h"
#include "lib/rng.h"
#include "lib/dbg.h"
/* #define INTRODUCE_RANDOM_DIFFS */
#ifdef JUST_SEQ_NUM
#include "lib/randomdifffuzzgenerator.h"
#endif
// TODO: .rept instead of .fill, Static assert
// TODO: repro: fix include path?
// TODO: add floating point flag like vector
// TODO: only change stuff in linker script
// TODO: try the before after macro thing, where we have a recursive loop that goes over all registers and does before regname after
struct regs regs;
// li t0, 0xf000
#define LI_t0_1 0x0000f2b7
// lb x11, 0(x12)
#define LB_x11_x12 0x00060583
// NOTE: for this to fail set regs.f0=0 after generating inputs
#define FDIVD_F1_F1_F0 0x1a00f0d3
__attribute__((section(".hash"))) const char elf_hash[] = "00000000000000000000000000000000";
// Flexible metadata tags
typedef struct tag {
char *key;
char *value;
} tag;
tag global_tag_list[10];
int global_tag_idx = 0;
#define ADD_TAG(k, v) \
global_tag_list[global_tag_idx].key = k; \
global_tag_list[global_tag_idx].value = v; \
global_tag_idx++;
/******************************* CLI Parsing *********************************/
struct arguments {
char connect_ip[100];
uint16_t connect_port;
char hostname[100];
} __attribute__((packed));
static struct arguments arguments = {
.connect_ip = {0},
.hostname = {0},
};
/*****************************************************************************/
#include "lib/regs.h"
#include "lib/fuzzing_value_map.h"
/******************************** Protection *********************************/
int check_ptrs_safe(uint64_t* ptrs, unsigned n) {
int err = 0;
for (unsigned i = 0; i < n; ++i) {
// TODO: also check shifted targets?
uint64_t target = ptrs[i];
uint64_t page_start = target & ~(page_size - 1);
// TODO: which offset? for now we just use a crazy big number
// jalr x4, 2047(x28), 2048 is max offset
for (int j=-1000; j<=1000; j++) {
// we want to wrap around
uint64_t t = page_start+j*page_size;
if (check_page_mapped((void*)t)) {
fprintf(stderr, "%08lx (%08lx) is unsafe at offset %d (%08lx)\n", target, page_start, j, t);
err = 1;
}
}
}
return err;
}
void check_fuzzing_values_safe() {
int err = 0;
err |= check_ptrs_safe((uint64_t*)fuzzing_value_map_gp, fuzzing_value_map_gp_size/sizeof(*fuzzing_value_map_gp));
#ifdef FLOATS
err |= check_ptrs_safe((uint64_t*)fuzzing_value_map_fp, fuzzing_value_map_fp_size/sizeof(*fuzzing_value_map_fp));
#endif
if (err) {
print_proc_self_maps();
exit(EXIT_FAILURE);
}
}
#define RESULT_TAG_SINGLE 0
#define RESULT_TAG_MULTI 1
int pack_inner_result(uint8_t* out, struct result* r) {
uint64_t out_i = 0;
out[out_i++] = r->signum;
#ifdef META
*((uint16_t*)&out[out_i]) = r->meta.cycle;
out_i+=sizeof(uint16_t);
#if defined(__riscv)
*((uint16_t*)&out[out_i]) = r->meta.instret;
out_i+=sizeof(uint16_t);
#endif
#endif
// skip for regs_diff
uint8_t* regs_diff_out = &out[out_i++];
uint8_t regs_diff = 0;
LOOP_OVER_GP_DIFF((®s), (&r->regs_result),
regs_diff++;
out[out_i++]=abi_i;
*((reg*)&out[out_i])=*after;
out_i+=sizeof(*after);
)
#if defined(__aarch64__)
if (regs.pstate != r->regs_result.pstate) {
regs_diff++;
out[out_i++]=getabiindex(pstate);
*((reg*)&out[out_i])=r->regs_result.pstate;
out_i+=sizeof(r->regs_result.pstate);
}
#endif
#if defined(__riscv) && defined(FLOATS)
if (regs.fcsr != r->regs_result.fcsr) {
regs_diff++;
out[out_i++]=getabiindex(fcsr);
*((reg*)&out[out_i])=r->regs_result.fcsr;
out_i+=sizeof(r->regs_result.fcsr);
}
#endif
#if defined(__aarch64__) && (defined(FLOATS) || defined(VECTOR))
if (regs.fpsr != r->regs_result.fpsr) {
regs_diff++;
out[out_i++]=getabiindex(fpsr);
*((reg*)&out[out_i])=r->regs_result.fpsr;
out_i+=sizeof(r->regs_result.fpsr);
}
#endif
#ifdef FLOATS
LOOP_OVER_FP_DIFF((®s), (&r->regs_result),
regs_diff++;
out[out_i++]=abi_i;
*((freg*)&out[out_i])=after->u;
out_i+=sizeof(after->u);
)
#endif
#ifdef VECTOR
LOOP_OVER_VECTOR_REGS_DIFF((®s), (&r->regs_result),
regs_diff++;
out[out_i++]=abi_i;
memcpy(&out[out_i], after, sizeof(*after));
out_i+=sizeof(*after);
)
#endif
*regs_diff_out=regs_diff;
if (r->signum != 0) {
*((uint64_t*)&out[out_i]) = r->si_addrr;
out_i+=sizeof(r->si_addrr);
*((uint64_t*)&out[out_i]) = r->si_pc;
out_i+=sizeof(r->si_pc);
*((int*)&out[out_i]) = r->si_code;
out_i+=sizeof(r->si_code);
}
#ifdef CHECK_MEM
*((uint8_t*)&out[out_i]) = r->n_mem_changes;
out_i+=sizeof(r->n_mem_changes);
for (int i = 0; i < r->n_mem_changes; i++) {
*((uint64_t*)&out[out_i]) = r->mem_changes[i].start;
out_i+=sizeof(r->mem_changes[i].start);
*((uint32_t*)&out[out_i]) = r->mem_changes[i].n;
out_i+=sizeof(r->mem_changes[i].n);
memcpy(&out[out_i], r->mem_changes[i].val, r->mem_changes[i].n);
out_i+=r->mem_changes[i].n;
}
#endif
return out_i;
}
int pack_result(uint8_t* out, struct result* r) {
uint64_t out_i = 0;
uint16_t* result_bytes = (uint16_t*)&out[out_i];
out_i += sizeof(*result_bytes);
// Single result
out[out_i++] = RESULT_TAG_SINGLE;
unsigned body = pack_inner_result(&out[out_i], r);
out_i += body;
assert(out_i-sizeof(*result_bytes) <= UINT16_MAX);
*result_bytes = out_i-sizeof(*result_bytes);
return out_i;
}
int pack_results(uint8_t* out, struct result* results, unsigned max_seq_len) {
uint64_t out_i = 0;
uint16_t* result_bytes = (uint16_t*)&out[out_i];
out_i += sizeof(*result_bytes);
// Multi result
out[out_i++] = RESULT_TAG_MULTI;
out[out_i++] = max_seq_len;
for (unsigned l = 0; l < max_seq_len; l++) {
struct result* r = &results[l];
unsigned body = pack_inner_result(&out[out_i], r);
out_i += body;
}
assert(out_i-sizeof(*result_bytes) <= UINT16_MAX);
*result_bytes = out_i-sizeof(*result_bytes);
return out_i;
}
char* get_lscpu() {
FILE *fp;
char* buffer = malloc(4096);
size_t bytes_read;
char *result = NULL;
size_t total_length = 0;
fp = popen("lscpu", "r");
if (fp == NULL) {
perror("popen failed");
exit(1);
}
while ((bytes_read = fread(buffer, 1, sizeof(buffer), fp)) > 0) {
result = realloc(result, total_length + bytes_read + 1);
if (result == NULL) {
perror("realloc failed");
exit(1);
}
memcpy(result + total_length, buffer, bytes_read);
total_length += bytes_read;
}
if (result == NULL) {
perror("Getting lscpu output failed");
exit(1);
}
result[total_length] = '\0';
pclose(fp);
// Make sure that no difference comes from that buffer
memset(buffer, 0, 4096);
free(buffer);
return result;
}
void set_buffer_sizes(int socket) {
int buf_size = 1 << 20;
int result;
// Set send buffer size
result = setsockopt(socket, SOL_SOCKET, SO_SNDBUF, &buf_size, sizeof(buf_size));
if (result < 0) {
perror("setsockopt SO_SNDBUF");
exit(EXIT_FAILURE);
}
// Set receive buffer size
result = setsockopt(socket, SOL_SOCKET, SO_RCVBUF, &buf_size, sizeof(buf_size));
if (result < 0) {
perror("setsockopt SO_RCVBUF");
exit(EXIT_FAILURE);
}
// Verify the buffer sizes
int actual_size;
socklen_t optlen = sizeof(actual_size);
// Verify send buffer size
result = getsockopt(socket, SOL_SOCKET, SO_SNDBUF, &actual_size, &optlen);
if (result == 0) {
if (actual_size < buf_size) {
printf("Coulnd't set send buffer size to %d, got %d\n", buf_size, actual_size);
}
} else {
perror("getsockopt SO_SNDBUF");
exit(EXIT_FAILURE);
}
// Verify receive buffer size
result = getsockopt(socket, SOL_SOCKET, SO_RCVBUF, &actual_size, &optlen);
if (result == 0) {
if (actual_size < buf_size) {
printf("Coulnd't set receive buffer size to %d, got %d\n", buf_size, actual_size);
}
} else {
perror("getsockopt SO_RCVBUF");
exit(EXIT_FAILURE);
}
}
// NOTE: hardcoding here is not optimal but we assert below so hopefully fine
#define OUT_BIGBUFFER_SIZE (10ul<<20)
/* #ifdef VECTOR */
/* #define OUT_BIGBUFFER_SIZE (128*8+32*VEC_REG_SIZE)*max_batch_n+100*8 */
/* #else */
/* #define OUT_BIGBUFFER_SIZE 128*8*max_batch_n+100*8 */
/* #endif */
int core;
int num_cpus;
char* hostname;
char* lscpu;
char* proc_cpuinfo;
char* sys_possible;
#if defined(__riscv)
#if defined(VECTOR)
int vec_size = VEC_REG_SIZE;
#else
int vec_size = 0;
#endif
#elif defined(__aarch64__)
int vec_size = 0;
int sve_max_size = 0;
int sme_max_size = 0;
int vector_enabled = 0;
#endif
void start_client() {
printf("now connecting...\n");
connection_context connection;
connection.socket = socket(AF_INET, SOCK_STREAM, 0);
if (connection.socket == -1) {
perror("Socket creation failed");
exit(EXIT_FAILURE);
}
set_buffer_sizes(connection.socket);
struct sockaddr_in server_address;
server_address.sin_family = AF_INET;
server_address.sin_port = htons(arguments.connect_port);
server_address.sin_addr.s_addr = inet_addr(arguments.connect_ip);
connect_with_retry(&connection, &server_address);
printf("Connected!\n");
send_string(&connection, hostname);
send_msg(&connection, (uint8_t*)&num_cpus, sizeof(num_cpus));
send_msg(&connection, (uint8_t*)&core, sizeof(core));
send_string(&connection, lscpu);
send_string(&connection, proc_cpuinfo);
send_string(&connection, sys_possible);
send_msg(&connection, (uint8_t*)&vec_size, sizeof(vec_size));
#if defined(__aarch64__)
send_msg(&connection, (uint8_t*)&sve_max_size, sizeof(sve_max_size));
send_msg(&connection, (uint8_t*)&sme_max_size, sizeof(sme_max_size));
// TODO: maybe set them to zero, but sve_max_size we need
// also maybe we should use a different resetting routine depending on sve_max_size
// maybe set to zero depending on vec_size?
// TODO: do we want vec_size sent to the server? its 0 or 16 and will probably always be 16
/* vec_size = 0; */
/* sme_max_size = 0; */
/* sve_max_size = 0; */
#endif
send_msg(&connection, (uint8_t*)&global_tag_idx, sizeof(global_tag_idx));
for (int i=global_tag_idx-1; i >= 0; i--) {
send_string(&connection, global_tag_list[i].key);
send_string(&connection, global_tag_list[i].value);
free(global_tag_list[i].value);
}
memset(global_tag_list, 0, sizeof(global_tag_list));
memset(&global_tag_idx, 0, sizeof(global_tag_idx));
send_string(&connection, (char*)elf_hash);
// Make sure that no differences comes from the buffers
memset(lscpu, 0, strlen(lscpu));
free(lscpu);
memset(proc_cpuinfo, 0, strlen(proc_cpuinfo));
free(proc_cpuinfo);
memset(sys_possible, 0, strlen(sys_possible));
free(sys_possible);
memset(hostname, 0, strlen(hostname));
free(hostname);
num_cpus = 0;
core = 0;
memset(&arguments, 0, sizeof(arguments));
unsigned max_batch_n;
recv_msg_n(&connection, (uint8_t*)&max_batch_n, sizeof(max_batch_n));
uint64_t seed;
recv_msg_n(&connection, (uint8_t*)&seed, sizeof(seed));
#ifdef JUST_SEQ_NUM
random_difffuzz_generator generator;
random_difffuzz_generator_init(&generator, seed);
#else
(void)seed;
#endif
uint64_t bigbuffer_out_i = 0;
uint8_t* out_bigbuffer = malloc(OUT_BIGBUFFER_SIZE);
if (!out_bigbuffer) {
perror("out_bigbuffer malloc failed");
exit(EXIT_FAILURE);
}
#ifdef JUST_SEQ_NUM
struct input {
uint64_t seq_num;
uint16_t n;
uint8_t seq_len;
uint8_t full_seq;
} __attribute__((packed));
#elifdef WITH_REGS
struct input {
uint8_t reg_select_gp[sizeof(regs.gp)/sizeof(regs.gp.x1)];
#ifdef FLOATS
#if defined(__riscv)
uint8_t reg_select_fp[sizeof(regs.fp)/sizeof(regs.fp.f0)];
#elif defined(__aarch64__)
uint8_t reg_select_fp[sizeof(regs.fp)/sizeof(regs.fp.d0)];
#endif
#endif /* FLOATS */
#ifdef VECTOR
uint8_t reg_select_vec[VEC_REG_SIZE/8*sizeof(regs.vec)/sizeof(regs.vec.v0)];
#endif /* VECTOR */
uint8_t n_instrs;
uint8_t full_seq;
uint32_t instr_seq[1] __attribute__((aligned(4)));
} __attribute__((packed));
#elifdef WITH_FULL_REGS
struct input {
struct regs regs;
uint8_t n_instrs;
uint8_t full_seq;
uint32_t instr_seq[1] __attribute__((aligned(4)));
} __attribute__((packed));
#else
struct input {
uint8_t n_instrs;
uint8_t full_seq;
uint32_t instr_seq[1] __attribute__((aligned(4)));
} __attribute__((packed));
#endif
struct input* inputs_buf;
#ifdef JUST_SEQ_NUM
unsigned max_input_size = sizeof(*inputs_buf);
#else
unsigned max_input_size = sizeof(*inputs_buf)+(MAX_SEQ_LEN-1)*sizeof(*inputs_buf->instr_seq);
#endif
inputs_buf = malloc(max_batch_n*max_input_size);
// safety check, do this at the last end where memory could get allocated
check_fuzzing_values_safe();
init_comp(&connection, OUT_BIGBUFFER_SIZE, max_batch_n*max_input_size);
while (true) {
uint32_t* instr_seq;
uint8_t seq_len;
unsigned n;
recv_msg_n(&connection, (uint8_t*)&n, sizeof(n));
if (n > max_batch_n) {
printf("n > max_batch_n");
exit(EXIT_FAILURE);
}
#ifdef COMPRESS_RECV
recv_msg_compressed(&connection, (uint8_t*)inputs_buf, max_input_size*n);
#else
recv_msg(&connection, (uint8_t*)inputs_buf, max_input_size*n);
#endif
struct input* input = inputs_buf;
for (unsigned b = 0; b < n; b++) {
#ifdef JUST_SEQ_NUM
for (unsigned a = 0; a < input->n; a++) {
uint32_t _instr_seq[MAX_SEQ_LEN];
generate_input(&generator, input->seq_num+a, (uint32_t*)&_instr_seq, input->seq_len, ®s);
instr_seq = (uint32_t*)&_instr_seq;
seq_len = input->seq_len;
// TODO: to which value are regs set when not defined?
#elifdef WITH_REGS
for (unsigned i = 0; i < sizeof(input->reg_select_gp)/sizeof(*input->reg_select_gp); i++) {
((reg*) ®s.gp)[i] = fuzzing_value_map_gp[input->reg_select_gp[i]];
}
#ifdef FLOATS
for (unsigned i = 0; i < sizeof(input->reg_select_fp)/sizeof(*input->reg_select_fp); i++) {
((reg*) ®s.fp)[i] = fuzzing_value_map_fp[input->reg_select_fp[i]].u;
}
#endif /* FLOATS */
#ifdef VECTOR
for (unsigned i = 0; i < 32; i++) {
for (unsigned j = 0; j < VEC_REG_SIZE/8; j++) {
((uint64_t*)&((vv*)®s.vec)[i])[j] = fuzzing_value_map_gp[input->reg_select_vec[i*VEC_REG_SIZE/8+j]];
}
}
#endif /* VECTOR */
#elifdef WITH_FULL_REGS
memcpy(®s, &input->regs, sizeof(regs));
#else
static_assert(sizeof(regs) % sizeof(filler_64) == 0, "regs should have the size of the filler value");
for (unsigned i = 0; i < sizeof(regs)/sizeof(filler_64); i++) {
((uint64_t*)®s)[i] = filler_64;
}
#endif
#ifndef WITH_FULL_REGS
#if defined(__riscv)
#if defined(FLOATS)
// We zero it out in the runner
regs.fcsr = 0;
#endif
#elif defined(__aarch64__)
// We zero it out in the runner
regs.pstate = 0;
#if defined(FLOATS) || defined(VECTOR)
// We zero it out in the runner
regs.fpsr = 0;
#endif /* FLOATS */
#endif
#endif
#ifndef JUST_SEQ_NUM
instr_seq = input->instr_seq;
seq_len = input->n_instrs;
#endif
#ifdef VERBOSE
printf("running instr seq:");
for (unsigned i = 0; i<seq_len; i++) {
printf(" 0x%08x", instr_seq[i]);
}
printf("\n");
#endif
if (input->full_seq) {
struct result results[MAX_SEQ_LEN];
unsigned stopped_at = run_with_instr_seq_full_seq(instr_seq, seq_len, 0, 0, (struct result*)&results);
#ifdef INTRODUCE_RANDOM_DIFFS
if (rand() % 1000 == 0) {
results[stopped_at-1].regs_result.gp.x11++;
}
#endif
#ifdef VERBOSE
printf("finished instr seq; full seq\n");
/* print_diff(®s, &r); */
#endif
bigbuffer_out_i += pack_results(&out_bigbuffer[bigbuffer_out_i], (struct result*)results, stopped_at);
for (unsigned j = 0; j < stopped_at; j++) {
struct result* r = &results[j];
free_result(r);
}
} else {
struct result r = run_with_instr_seq(instr_seq, seq_len, 0, 0);
#ifdef INTRODUCE_RANDOM_DIFFS
if (rand() % 1000 == 0) {
r.regs_result.gp.x11++;
}
#endif
#ifdef VERBOSE
printf("finished instr seq\n");
print_diff(®s, &r);
#endif
bigbuffer_out_i += pack_result(&out_bigbuffer[bigbuffer_out_i], &r);
free_result(&r);
}
assert(bigbuffer_out_i < OUT_BIGBUFFER_SIZE);
#ifndef JUST_SEQ_NUM
input = (struct input*) (((uint64_t) input)+sizeof(*input)+(input->n_instrs-1)*sizeof(*input->instr_seq));
#else
}
input = (struct input*) (((uint64_t) input)+sizeof(*input));
#endif
}
send_msg_compressed(&connection, out_bigbuffer, bigbuffer_out_i);
bigbuffer_out_i = 0;
}
}
/*****************************************************************************/
// Get the android property, copy the value into dst.
// Returns the number of bytes written. Probably does nothing if
// getprop isn't available but also might blow up.
int get_android_property(char *propname, char *dst, size_t dst_len) {
char cmd[256] = "getprop ";
strcat(cmd, propname);
FILE *fp = popen(cmd, "r");
if (fp == NULL) {
perror("android getprop popen failed");
exit(EXIT_FAILURE);
}
fgets(dst, dst_len, fp);
// Strip newline
size_t out_len = strcspn(dst, "\n");
dst[out_len] = 0;
return out_len;
}
int set_and_get_sve(int set) {
int ret = prctl(PR_SVE_SET_VL, set);
int verify = prctl(PR_SVE_GET_VL, 0);
assert(ret == verify);
return verify;
}
int set_and_get_sme(int set) {
int ret = prctl(PR_SME_SET_VL, set);
int verify = prctl(PR_SME_GET_VL, 0);
assert(ret == verify);
return verify;
}
__attribute__((noreturn)) void fuzzer();
int main(int argc, char* argv[]) {
// always flush stdout/err
setvbuf(stdout, NULL, _IONBF, 0);
setvbuf(stderr, NULL, _IONBF, 0);
/* https://askubuntu.com/a/1355819 */
const int old_personality = personality(ADDR_NO_RANDOMIZE);
if (!(old_personality & ADDR_NO_RANDOMIZE)) {
const int new_personality = personality(ADDR_NO_RANDOMIZE);
if (new_personality & ADDR_NO_RANDOMIZE) {
printf("Disabled ASLR with personality syscall.\n");
execv(argv[0], argv);
} else {
printf("Error: Failed to disable ASLR with personality syscall.\nTry to disable system wide:\nsudo sysctl kernel.randomize_va_space=0\nor\necho 0 | sudo tee /proc/sys/kernel/randomize_va_space\n");
exit(1);
}
}
#ifdef INTRODUCE_RANDOM_DIFFS
struct timeval tv;
gettimeofday(&tv, NULL);
unsigned int _seed = (unsigned int)(tv.tv_sec ^ tv.tv_usec ^ getpid());
srand(_seed);
#endif
// Arg parsing
if (argc < 3) {
printf("Usage: %s ip port [hostname]\n", argv[0]);
exit(EXIT_FAILURE);
}
strcpy(arguments.connect_ip, argv[1]);
arguments.connect_port = strtoul(argv[2], NULL, 10);
if (argc > 3) {
strcpy(arguments.hostname, argv[3]);
}
#define HOSTNAME_SIZE 256
hostname = malloc(HOSTNAME_SIZE);
if (arguments.hostname[0]) {
strcpy(hostname, (char*)&arguments.hostname);
} else {
if (gethostname(hostname, HOSTNAME_SIZE) != 0) {
perror("gethostname");
exit(1);
}
if (strcmp(hostname, "localhost") == 0) {
// Probably running under Android
char *serialno = malloc(256);
get_android_property("ro.serialno", serialno, 256);
ADD_TAG("android_serialno", serialno);
char *model_name = malloc(256);
get_android_property("ro.product.model", model_name, 256);
ADD_TAG("android_model", model_name);
char *product_name = malloc(256);
get_android_property("ro.product.name", product_name, 256);
ADD_TAG("android_product", product_name);
char *host_env = getenv("HOST");
memset(hostname, 0, strlen(hostname));
if (host_env && strcmp(host_env, "localhost") != 0) {
snprintf(hostname, HOSTNAME_SIZE, "%s", host_env);
} else {
snprintf(hostname, HOSTNAME_SIZE, "%s_%s", product_name, serialno);
}
}
}
printf("Hostname: %s\n", hostname);
lscpu = get_lscpu();
proc_cpuinfo = read_file("/proc/cpuinfo");
sys_possible = read_file("/sys/devices/system/cpu/possible");
#if defined(__aarch64__)
unsigned long hwcaps = getauxval(AT_HWCAP);
// The vector size of SIMD
#define AARCH64_VEC_SIZE (128/8)
if (hwcaps & HWCAP_SVE) {
printf("SVE available.\n");
// architectural maximum of 2048-bit
sve_max_size = set_and_get_sve(2048/8);
assert(sve_max_size != 0);
// Set the actual sve vector size to match SIMD
vec_size = AARCH64_VEC_SIZE;
assert(sve_max_size >= vec_size);
int verify = set_and_get_sve(vec_size);
assert(verify == vec_size);
}
if (set_and_get_sme(2048/8) != -1) {
printf("SME available.\n");
// architectural maximum of 2048-bit
sme_max_size = set_and_get_sme(2048/8);
assert(sme_max_size != 0);
// also SVE -> SME
assert(sve_max_size == sme_max_size);
// Try to set the actual sme vector size to match SIMD
assert(sme_max_size >= vec_size);
int verify = set_and_get_sme(vec_size);
// But sme seems to always be maximum (TODO: maybe a QEMU bug)
assert(verify == sme_max_size);
}
if (hwcaps & HWCAP_ASIMD) {
printf("SIMD available.\n");
vec_size = AARCH64_VEC_SIZE;
}
#endif
#ifndef SINGLE_THREAD
// Detach the session. This is needed whenever the client is started
// detached. Not sure if this code is perfect but it worked.
pid_t pid;
pid = fork();
if (pid < 0) {
perror("fork");
exit(EXIT_FAILURE);
}
if (pid > 0) {
// Infinitely wait, so Android app does not die
while (1) {
sleep(10000000);
}
}
// Make child process the session leader
if (setsid() < 0) {
perror("setsid");
exit(EXIT_FAILURE);
}
num_cpus = get_num_cpus();
if (strncmp(hostname, "qemu", 4) == 0) {
if (num_cpus >= 8) {
// Restrict to 8 cores on qemu
num_cpus = 8;
}
}
printf("Spawning %d threads...\n", num_cpus);
for (core = 0; core < num_cpus; core++) {
if (fork() == 0) {
goto thread_setup;
}
}
// quit parent
exit(0);
thread_setup:
#else
core = 0;
num_cpus = 1;
#endif /* SINGLE_THREAD */
pin_to_cpu(core);
clean_memory_mappings();
switch_stack(STACK_ADDR, &fuzzer);
}
__attribute__((noreturn)) void fuzzer() {
runner_init();
start_client();
exit(0);
}