-
Notifications
You must be signed in to change notification settings - Fork 4
/
videzzo.c
2090 lines (1877 loc) · 67.3 KB
/
videzzo.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
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
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Dependency-Aware Virtual-Device Fuzzing
*
* Copyright Qiang Liu <[email protected]>
*
* This work is licensed under the terms of the GNU GPL, version 2 or later.
*/
#include "videzzo.h"
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <rfb/rfbclient.h>
#include <semaphore.h>
//
// Default size for an input
//
int get_default_input_maxsize() {
if (getenv("DEFAULT_INPUT_MAXSIZE"))
return atoi(getenv("DEFAULT_INPUT_MAXSIZE"));
else
return 0x1000;
}
static int DEFAULT_INPUT_MAXSIZE = 0x1000;
//
// Merge
//
int merge = 0;
void videzzo_set_merge() {
merge = 1;
}
void videzzo_clear_merge() {
merge = 0;
}
//
// GroupMutator Feedback
//
// We want to disable the trigger-action protocol and other advanced features.
//
bool DisableInputProcessing = false;
void disable_group_mutator(void) {
DisableInputProcessing = true;
}
void enable_group_mutator(void) {
DisableInputProcessing = false;
}
static sem_t mutex;
static int in_one_iteration = 0;
static int loop_counter = 0;
static int status = 0; // 0 -> 1/2 -> 2/1 -> bingo
typedef struct Record {
int id;
int status; // 0 -> 1/2 -> 2/1 -> bingo
int last_status;
int current_event_s;
int current_event_e;
} Record;
static Record records[32] = {{ 0 }};
// TODO: change the API convetion and API name
void GroupMutatorOrder(int id, int status) {
// TODO: this is not thread-safe (vbox)
if (DisableInputProcessing)
return;
if (getenv("VIDEZZO_DISABLE_GROUP_MUTATOR_RS") ||
getenv("VIDEZZO_DISABLE_INTER_MESSAGE_MUTATORS"))
return;
#ifdef VIDEZZO_DEBUG
fprintf(stderr, "- GroupMutatorOrder: %d\n", status);
#endif
Record *r = &records[id];
if (r->status == 0 && status == 1) {
r->status = 1;
r->last_status = status;
// start to record
r->current_event_s = gfctx_get_current_event(0);
} else if (r->status == 1 && in_one_iteration) {
if (status == 2 && r->last_status == 1)
r->status = 2;
// end the record
r->current_event_e = gfctx_get_current_event(0);
}
if (r->status == 2 && r->current_event_s < r->current_event_e) {
// we want to group any events between (_s, _e]
Input *input = gfctx_get_current_input(0);
// we dislike a group event to trigger this Order
Event *trigger_event = get_event(input, gfctx_get_current_event(0));
if (trigger_event->type == EVENT_TYPE_GROUP_EVENT_RS)
return;
// create new context
Input *new_input = init_input(NULL, DEFAULT_INPUT_MAXSIZE);
Event *event, *event_copy;
for (int i = r->current_event_s + 1; i <= r->current_event_e; ++i) {
event = get_event(input, i);
// if any EVENT_TYPE_GROUP_EVENT_RS, we drop this
if (event->type == EVENT_TYPE_GROUP_EVENT_RS)
return;
// we don't want a EVENT_TYPE_GROUP_EVENT_LM
if (event->type == EVENT_TYPE_GROUP_EVENT_LM) {
// for this, we want its last event
Input *grouped_input = (Input *)event->data;
event = get_event(grouped_input, grouped_input->n_events - 1);
}
event_copy = (Event *)calloc(sizeof(Event), 1);
event_ops[event->type].deep_copy(event, event_copy);
append_event(new_input, event_copy);
// should we delete the event?
event_ops[event->type].print_event(event);
}
// we are going to construct a group event
Event *group_event = event_ops[EVENT_TYPE_GROUP_EVENT_RS].construct(
EVENT_TYPE_GROUP_EVENT_RS, INTERFACE_GROUP_EVENT_RS, 0,
new_input->size, 0, (uint8_t *)new_input);
// bingo, we've got all events into new_input
// let's find a place to inject the group event
// currently, we don't want to make it a next event
// let's insert it after current_event_s
insert_event(input, group_event, r->current_event_s + 1);
memset(r, 0, sizeof(Record));
}
#ifdef VIDEZZO_DEBUG
fprintf(stderr, "- GroupMutatorOrder Done\n");
#endif
}
// TODO: change the API convention and API name
void GroupMutatorMiss(uint8_t id, uint64_t physaddr) {
if (DisableInputProcessing)
return;
// let's first handle intra-message annotation
if (getenv("VIDEZZO_DISABLE_INTRA_MESSAGE_ANNOTATION"))
return;
sem_wait(&mutex);
Input *old_input;
int old_current_event;
Event *trigger_event;
if (!getenv("VIDEZZO_DISABLE_GROUP_MUTATOR_LM") &&
!getenv("VIDEZZO_DISABLE_INTER_MESSAGE_MUTATORS")) {
// we dislike a group event to trigger this Miss
old_input = gfctx_get_current_input(0);
old_current_event = gfctx_get_current_event(0);
trigger_event = NULL;
if (old_input != NULL) {
trigger_event = get_event(old_input, old_current_event);
if (trigger_event->type == EVENT_TYPE_GROUP_EVENT_LM && loop_counter == 0) {
sem_post(&mutex);
return;
}
}
}
// create new context
// so all injected events will go into here
Input *input = init_input(NULL, DEFAULT_INPUT_MAXSIZE);
int current_event = 0;
gfctx_set_current_input(input, 1);
gfctx_set_current_event(current_event, 1);
// in this handler, the current input will be updated
// Don't delete any events from the current event to the end
group_mutator_miss_handlers[id](physaddr);
// let's group messages
if (getenv("VIDEZZO_DISABLE_GROUP_MUTATOR_LM") ||
getenv("VIDEZZO_DISABLE_INTER_MESSAGE_MUTATORS") || old_input == NULL) {
free_input(input);
goto recover;
}
#ifdef VIDEZZO_DEBUG
fprintf(stderr, "- GroupMutatorMiss: %d\n", loop_counter);
#endif
// nice, all events go into our new input
// we construct the group event
if (trigger_event->type == EVENT_TYPE_GROUP_EVENT_LM) {
// apprantly, we are in a loop
Input *group_event_input = (Input* )trigger_event->data;
Event *injected_event = get_first_event(input), *tmp_event;
// copy event from the injected input to the group event input
while (injected_event != NULL) {
Event *tmp_event = (Event *)calloc(sizeof(Event), 1);
event_ops[injected_event->type].deep_copy(injected_event, tmp_event);
insert_event(group_event_input, tmp_event, group_event_input->n_events - 1);
injected_event = get_next_event(injected_event);
}
free_input(input);
} else {
// apprantly, this is the first time to have a GroupMutatorMiss
Event *trigger_event_copy = (Event *)calloc(sizeof(Event), 1);
event_ops[trigger_event->type].deep_copy(trigger_event, trigger_event_copy);
append_event(input, trigger_event_copy);
// we are going to construct a group event
Event *group_event = event_ops[EVENT_TYPE_GROUP_EVENT_LM].construct(
EVENT_TYPE_GROUP_EVENT_LM, INTERFACE_GROUP_EVENT_LM, 0, input->size, 0, (uint8_t *)input);
// we inject this group event into the old input, and then
// we will delete the old_current_event in __videzzo_execute_one_input
insert_event(old_input, group_event, old_current_event);
old_input->n_groups++;
}
loop_counter += 1;
#ifdef VIDEZZO_DEBUG
fprintf(stderr, "- GroupMutatorMiss Done\n");
#endif
recover:
gfctx_set_current_input(old_input, 0);
gfctx_set_current_event(old_current_event, 0);
sem_post(&mutex);
}
//
// used in vm|fuzzer-specific mutator callback
//
size_t ViDeZZoCustomMutator(uint8_t *Data, size_t Size,
size_t MaxSize, unsigned int Seed) {
#ifdef VIDEZZO_DEBUG
fprintf(stderr, "- ViDeZZoCustomMutator, %zu\n", Size);
#endif
// Copy data to our input manager
Input *input = init_input(Data, Size);
if (!input) {
return reset_data(Data, MaxSize);
}
// Deserialize Data to Events
// If the input is too short to contain longer event, stop early.
// Note that we don't discard any intact event.
Size = deserialize(input);
if (Size == 0) {
free_input(input);
return reset_data(Data, MaxSize);
}
// set up the RNG
srand(Seed);
// Mutate all events until an non-zero size
// weighted: select_weighted_mutators
size_t aaaaaaa = select_mutators(rand());
#ifdef VIDEZZO_DEBUG
fprintf(stderr, "- %s\n", CustomMutatorNames[aaaaaaa]);
#endif
size_t NewSize = CustomMutators[aaaaaaa](input);
if (NewSize) {
size_t SerializationSize = serialize(input, Data, MaxSize);
free_input(input);
#ifdef VIDEZZO_DEBUG
if (NewSize > MaxSize)
fprintf(stderr, "- NewSize (overflow), %zu\n", NewSize);
else
fprintf(stderr, "- NewSize, %zu\n", NewSize);
#endif
return SerializationSize;
}
free_input(input);
return reset_data(Data, MaxSize); // Fallback, should not happen frequently.
}
static void __videzzo_execute_one_input(Input *input) {
Event *event = get_first_event(input);
#ifdef VIDEZZO_DEBUG
fprintf(stderr, "- dispatching events\n");
#endif
int i;
for (i = 0; event != NULL; i++) {
// set up feedback context
gfctx_set_current_event(i, 0);
videzzo_dispatch_event(event);
#ifdef VIDEZZO_DEBUG
event_ops[event->type].print_event(event);
#endif
event = get_next_event(event);
if (loop_counter) {
remove_event(input, i + 1);
}
loop_counter = 0;
}
gfctx_set_current_event(0, 0);
#ifdef VIDEZZO_DEBUG
fprintf(stderr, "- dispatching events done\n");
#endif
}
extern void __free_memory_blocks();
int videzzo_execute_one_input(uint8_t *Data, size_t Size, void *object, __flush flush) {
in_one_iteration = 1;
status = 0;
DEFAULT_INPUT_MAXSIZE = get_default_input_maxsize();
// read Data to Input
Input *input = init_input(Data, Size);
if (!input)
return 0;
// deserialize Data to Events
input->size = deserialize(input);
// set up feedback context
gfctx_set_current_input(input, 0);
gfctx_set_object(object, 0);
gfctx_set_flush(flush);
if (getenv("VIDEZZO_FORK")) {
if (fork() == 0) {
__videzzo_execute_one_input(input);
_Exit(0);
} else {
wait(0);
}
} else {
__videzzo_execute_one_input(input);
}
size_t SerializationSize = serialize(input, Data, DEFAULT_INPUT_MAXSIZE);
gfctx_set_current_input(NULL, 0);
gfctx_set_object(NULL, 0);
gfctx_set_flush(NULL);
free_input(input);
//
// We want to free all allocated blocks here to have a reliable reproducer.
//
sem_wait(&mutex);
__free_memory_blocks();
sem_post(&mutex);
in_one_iteration = 0;
return SerializationSize;
}
//
// shared interfaces
//
InterfaceDescription Id_Description[INTERFACE_END] = {
[INTERFACE_MEM_READ] = {
.type = EVENT_TYPE_MEM_READ,
.emb = {.addr = 0xFFFFFFFF, .size = 0xFFFFFFFF},
.name = "memread", .dynamic = false,
.min_access_size = 0xFF, .max_access_size = 0xFF,
}, [INTERFACE_MEM_WRITE] = {
.type = EVENT_TYPE_MEM_WRITE,
.emb = {.addr = 0xFFFFFFFF, .size = 0xFFFFFFFF},
.name = "memwrite", .dynamic = false,
.min_access_size = 0xFF, .max_access_size = 0xFF,
}, [INTERFACE_CLOCK_STEP] = {
.type = EVENT_TYPE_CLOCK_STEP,
.emb = {.addr = 0xFFFFFFFF, .size = 0xFFFFFFFF},
.name = "clock_step", .dynamic = true,
.min_access_size = 0xFF, .max_access_size = 0xFF,
}, [INTERFACE_SOCKET_WRITE] = {
.type = EVENT_TYPE_SOCKET_WRITE,
.emb = {.addr = 0xFFFFFFFF, .size = 0xFFFFFFFF},
.name = "socket_write", .dynamic = false, // disable socket write
.min_access_size = 0xFF, .max_access_size = 0xFF,
}, [INTERFACE_GROUP_EVENT_LM] = {
.type = EVENT_TYPE_GROUP_EVENT_LM,
.emb = {.addr = 0xFFFFFFFF, .size = 0xFFFFFFFF},
.name = "group_event_lm", .dynamic = false,
.min_access_size = 0xFF, .max_access_size = 0xFF,
}, [INTERFACE_GROUP_EVENT_RS] = {
.type = EVENT_TYPE_GROUP_EVENT_RS,
.emb = {.addr = 0xFFFFFFFF, .size = 0xFFFFFFFF},
.name = "group_event_re", .dynamic = false,
}, [INTERFACE_MEM_ALLOC] = {
.type = EVENT_TYPE_MEM_ALLOC,
.emb = {.addr = 0xFFFFFFFF, .size = 0xFFFFFFFF},
.name = "memalloc", .dynamic = false,
.min_access_size = 0xFF, .max_access_size = 0xFF,
}, [INTERFACE_MEM_FREE] = {
.type = EVENT_TYPE_MEM_FREE,
.emb = {.addr = 0xFFFFFFFF, .size = 0xFFFFFFFF},
.name = "memfree", .dynamic = false,
.min_access_size = 0xFF, .max_access_size = 0xFF,
}
};
static int n_interfaces = INTERFACE_DYNAMIC;
int get_number_of_interfaces(void) {
return n_interfaces;
}
void add_interface(EventType type, uint64_t addr, uint32_t size,
const char *name, uint8_t min_access_size, uint8_t max_access_size, bool dynamic) {
if (min_access_size == 0 && max_access_size == 0) {
fprintf(stderr, "\n- %s has zero size!!! Won\'t add this\n", name);
return;
}
Id_Description[n_interfaces].type = type;
Id_Description[n_interfaces].emb.addr = addr;
Id_Description[n_interfaces].emb.size = size;
Id_Description[n_interfaces].min_access_size = min_access_size;
Id_Description[n_interfaces].max_access_size = max_access_size;
memcpy(Id_Description[n_interfaces].name, name, strlen(name) <= 32 ? strlen(name) : 32);
Id_Description[n_interfaces].dynamic = dynamic;
n_interfaces++;
}
bool interface_exists(EventType type, uint64_t addr, uint32_t size) {
for (int i = 0; i < n_interfaces; i++) {
if (Id_Description[i].type == type && \
Id_Description[i].emb.addr == addr && \
Id_Description[i].emb.size == size)
return true;
}
return false;
}
static uint64_t around_event_addr(uint8_t id, uint64_t raw_addr) {
if (merge)
return raw_addr; // do nothing
if (id < INTERFACE_DYNAMIC)
return raw_addr; // do nothing
InterfaceDescription ed = Id_Description[id];
uint64_t to_avoid_overflow = ed.emb.addr + ((raw_addr - ed.emb.addr) % ed.emb.size);
if (getenv("VIDEZZO_BYTE_ALIGNED_ADDRESS")) {
return to_avoid_overflow & 0xFFFFFFFFFFFFFFFF;
} else {
return to_avoid_overflow & 0xFFFFFFFFFFFFFFFC;
}
}
static inline int clz64(uint64_t val) {
return val ? __builtin_clzll(val) : 64;
}
static inline uint64_t pow2floor(uint64_t value) {
if (!value) {
/* Avoid undefined shift by 64 */
return 0;
}
return 0x8000000000000000ull >> clz64(value);
}
static uint32_t around_event_size(uint8_t id, uint32_t raw_size) {
if (merge)
return raw_size; // do nothing
if (id != INTERFACE_SOCKET_WRITE && id < INTERFACE_DYNAMIC)
return raw_size % 0x80000; // 8M to avoid oom
if (id == INTERFACE_SOCKET_WRITE)
return (raw_size - SOCKET_WRITE_MIN_SIZE) %
(SOCKET_WRITE_MAX_SIZE - SOCKET_WRITE_MIN_SIZE) + SOCKET_WRITE_MIN_SIZE;
InterfaceDescription ed = Id_Description[id];
ed = Id_Description[id];
uint8_t diff = ed.max_access_size - ed.min_access_size + 1;
return pow2floor(((raw_size - ed.min_access_size) % diff) + ed.min_access_size);
}
void print_interfaces(void) {
for (int i = 0; i < n_interfaces; i++) {
InterfaceDescription ed = Id_Description[i];
if (!ed.dynamic)
continue;
fprintf(stderr, " * %s, %s, 0x%lx +0x%x, %d,%d\n",
ed.name, EventTypeNames[ed.type],
ed.emb.addr, ed.emb.size,
ed.min_access_size, ed.max_access_size);
}
}
// when generating a new event, we have to check possible interfaces
static uint8_t get_possible_interface(int rand) {
InterfaceDescription *id;
uint8_t *valid_interfaces = (uint8_t *)calloc(n_interfaces, 1);
// sample non-sequential data
uint32_t real_idx = 0;
uint32_t real_sum = n_interfaces;
for (int i = 0 ; i < n_interfaces; ++i, ++real_idx) {
id = &Id_Description[i];
if (!id->dynamic) {
real_idx--;
real_sum--;
continue;
}
valid_interfaces[real_idx] = i;
}
uint32_t target_idx = valid_interfaces[rand % real_sum];
free(valid_interfaces);
return target_idx;
}
//
// Event Callbacks
//
static void change_addr_generic(Event *event, uint64_t new_addr) {
event->addr = around_event_addr(event->interface, new_addr);
}
static void change_size_generic(Event *event, uint32_t new_size) {
event->size = around_event_size(event->interface, new_size);
}
static void change_valu_generic(Event *event, uint64_t new_valu) {
event->valu = new_valu;
}
static void change_data_socket_write(Event *event, uint8_t *new_data) {
// copy old data
memcpy(event->data, new_data, event->size); // remember to free new_data
}
static void print_event_prologue(Event *event) {
fprintf(stderr, " * %03d, %s", event->interface, EventTypeNames[event->type]);
}
static void print_event_io_addr_size(Event *event) {
fprintf(stderr, ", 0x%lx, 0x%x", event->addr, event->size);
}
static void print_event_io_valu(Event *event) {
fprintf(stderr, ", 0x%lx", event->valu);
}
static void print_event_data(Event *event) {
char *enc;
uint32_t size = event->size;
enc = calloc(2 * size + 1, 1);
for (int i = 0; i < size; i++) {
sprintf(&enc[i * 2], "%02x", event->data[i]);
}
if (2 * size + 1 > 80) {
enc[80] = '\0';
fprintf(stderr, ", %s...", enc);
} else
fprintf(stderr, ", %s", enc);
free(enc);
}
static void print_end() {
fprintf(stderr, "\n");
}
static void print_event_mmio_read(Event *event) {
print_event_prologue(event);
print_event_io_addr_size(event);
print_end();
}
static void print_event_pio_read(Event *event) {
print_event_prologue(event);
print_event_io_addr_size(event);
print_end();
}
static void print_event_mem_read(Event *event) {
print_event_prologue(event);
print_event_io_addr_size(event);
print_end();
}
static void print_event_mem_write(Event *event) {
print_event_prologue(event);
print_event_io_addr_size(event);
print_event_data(event);
print_end();
}
static void print_event_mem_alloc(Event *event) {
print_event_prologue(event);
fprintf(stderr, ", 0x%lx", event->valu);
print_end();
}
static void print_event_mem_free(Event *event) {
print_event_prologue(event);
fprintf(stderr, ", 0x%lx", event->valu);
print_end();
}
static void print_event_socket_write(Event *event) {
print_event_prologue(event);
fprintf(stderr, ", 0x%x", event->size);
print_event_data(event);
print_end();
}
static void print_event_group_event(Event *event) {
print_event_prologue(event);
fprintf(stderr, ", 0x%x", event->size);
fprintf(stderr, ", 0x%x events", ((Input *)event->data)->n_events);
print_end();
}
static void print_event_pio_write(Event *event) {
print_event_prologue(event);
print_event_io_addr_size(event);
print_event_io_valu(event);
print_end();
}
static void print_event_mmio_write(Event *event) {
print_event_prologue(event);
print_event_io_addr_size(event);
print_event_io_valu(event);
print_end();
}
static void print_event_clock_step(Event *event) {
print_event_prologue(event);
print_event_io_valu(event);
print_end();
}
static uint8_t *__get_buffer(size_t size) {
uint8_t *buffer = (uint8_t *)calloc(size, 1);
for (int i = 0; i < size; i++) {
buffer[i] = rand() % 0xff;
}
return buffer;
}
static Event *__alloc_an_event(uint8_t type, uint8_t interface ) {
Event *event = (Event *)calloc(sizeof(Event), 1);
event->type = type;
event->interface = interface;
return event;
}
static Event *construct_io_read(uint8_t type, uint8_t interface,
uint64_t addr, uint32_t size, uint64_t valu, uint8_t *data) {
Event *event = __alloc_an_event(type, interface);
event->addr = around_event_addr(interface, addr);
event->size = around_event_size(interface, size);
event->event_size = 14;
return event;
}
static Event *construct_io_write(uint8_t type, uint8_t interface,
uint64_t addr, uint32_t size, uint64_t valu, uint8_t *data) {
Event *event = __alloc_an_event(type, interface);
event->addr = around_event_addr(interface, addr);
event->size = around_event_size(interface, size);
event->valu = valu;
event->event_size = 22;
return event;
}
static Event *construct_mem_read_write(uint8_t type, uint8_t interface,
uint64_t addr, uint32_t size, uint64_t valu, uint8_t *data) {
Event *event = __alloc_an_event(type, interface);
event->addr = around_event_addr(interface, addr);
event->size = around_event_size(interface, size);
if (data == NULL)
data = __get_buffer(event->size);
// assume we are in charge of free this data for performance
event->data = data;
event->event_size = event->size + 14;
return event;
}
static Event *construct_mem_alloc_or_free(uint8_t type, uint8_t interface,
uint64_t addr, uint32_t size, uint64_t valu, uint8_t *data) {
Event *event = __alloc_an_event(type, interface);
event->valu = valu;
event->event_size = 10;
return event;
}
static Event *construct_socket_write(uint8_t type, uint8_t interface,
uint64_t addr, uint32_t size, uint64_t valu, uint8_t *data) {
Event *event = __alloc_an_event(type, interface);
event->size = around_event_size(interface, size);
if (data == NULL)
data = __get_buffer(event->size);
// assume we are in charge of free this data for performance
event->data = data;
event->event_size = event->size + 6;
return event;
}
static Event *construct_clock_step(uint8_t type, uint8_t interface,
uint64_t addr, uint32_t size, uint64_t valu, uint8_t *data) {
Event *event = __alloc_an_event(type, interface);
event->valu = valu % CLOCK_MAX_STEP;
event->event_size = 10;
return event;
}
static Event *construct_group_event(uint8_t type, uint8_t interface,
uint64_t addr, uint32_t size, uint64_t valu, uint8_t *data) {
Event *event = __alloc_an_event(type, interface);
event->size = size; // it's usually 0 at the beginning
assert(data != NULL);
// assume we are in charge of free this data for performance
/*(Input *)*/event->data = /*(Input *)*/data;
event->event_size = event->size + 6;
return event;
}
static uint32_t serialize_io_read(Event *event, uint8_t *Data, size_t Offset, size_t MaxSize) {
if (Offset + 14 >= MaxSize)
return 0;
Data[Offset] = event->type;
Data[Offset + 1] = event->interface;
memcpy(Data + Offset + 2, (uint8_t *)&(event->addr), 8);
memcpy(Data + Offset + 10, (uint8_t *)&(event->size), 4);
#ifdef VIDEZZO_DEBUG
event_ops[event->type].print_event(event);
#endif
return 14;
}
static uint32_t serialize_io_write(Event *event, uint8_t *Data, size_t Offset, size_t MaxSize) {
if (Offset + 22 >= MaxSize)
return 0;
Data[Offset] = event->type;
Data[Offset + 1] = event->interface;
memcpy(Data + Offset + 2, (uint8_t *)&(event->addr), 8);
memcpy(Data + Offset + 10, (uint8_t *)&(event->size), 4);
memcpy(Data + Offset + 14, (uint8_t *)&(event->valu), 8);
#ifdef VIDEZZO_DEBUG
event_ops[event->type].print_event(event);
#endif
return 22;
}
static uint32_t serialize_mem_read_or_write(Event *event, uint8_t *Data, size_t Offset, size_t MaxSize) {
uint32_t size = event->size;
if (Offset + 14 + size >= MaxSize)
return 0;
Data[Offset] = event->type;
Data[Offset + 1] = event->interface;
memcpy(Data + Offset + 2, (uint8_t *)&(event->addr), 8);
memcpy(Data + Offset + 10, (uint8_t *)&(event->size), 4);
if (event->type == EVENT_TYPE_MEM_READ)
memset(Data + Offset + 14, 0, size);
else
memcpy(Data + Offset + 14, (uint8_t *)(event->data), size);
#ifdef VIDEZZO_DEBUG
event_ops[event->type].print_event(event);
#endif
return 14 + event->size;
}
static uint32_t serialize_mem_alloc_or_free(Event *event, uint8_t *Data, size_t Offset, size_t MaxSize) {
if (Offset + 8 >= MaxSize)
return 0;
Data[Offset] = event->type;
Data[Offset + 1] = event->interface;
memcpy(Data + Offset + 2, (uint8_t *)&(event->valu), 8);
#ifdef VIDEZZO_DEBUG
event_ops[event->type].print_event(event);
#endif
return 10;
}
static uint32_t serialize_socket_write(Event *event, uint8_t *Data, size_t Offset, size_t MaxSize) {
uint32_t size = event->size;
if (Offset + 6 + size >= MaxSize)
return 0;
Data[Offset] = event->type;
Data[Offset + 1] = event->interface;
memcpy(Data + Offset + 2, (uint8_t *)&size, 4);
memcpy(Data + Offset + 6, (uint8_t *)event->data, size);
#ifdef VIDEZZO_DEBUG
event_ops[event->type].print_event(event);
#endif
return 6 + size;
}
static uint32_t serialize_clock_step(Event *event, uint8_t *Data, size_t Offset, size_t MaxSize) {
if (Offset + 10 >= MaxSize)
return 0;
Data[Offset] = event->type;
Data[Offset + 1] = event->interface;
memcpy(Data + Offset + 2, (uint8_t *)&event->valu, 8);
#ifdef VIDEZZO_DEBUG
event_ops[event->type].print_event(event);
#endif
return 10;
}
// For consecutive writes, we don't guarentee that two consecutive writes are
// address-consecutive. This make the following the solution breaks its
// assumption. Here, we sacrifice some performance to guarentee this assumption.
static int handle_non_address_consecutive_writes(Input *input) {
Event *e;
int n_delete = 0;
uint8_t *delete = (uint8_t *)calloc(input->n_events, 1);
Event *head = NULL, *next = NULL;
e = get_first_event(input);
for (int i = 0; e != NULL; i++) {
if (delete[i] || e->type != EVENT_TYPE_MEM_WRITE) {
e = get_next_event(e);
continue;
}
// for each write, we search
head = e;
next = get_next_event(head);
for (int j = i + 1; next != NULL; j++) {
if (head && delete[j] == 0 && next->addr == head->addr + head->size) {
uint32_t new_size = head->size + next->size;
uint8_t *new_data = (uint8_t *)calloc(new_size, 1);
memcpy(new_data, head->data, head->size);
memcpy(new_data + head->size, next->data, next->size);
free(head->data);
head->data = new_data;
head->size += next->size;
head->event_size += next->size;
input->size += next->size;
// let's mark this
delete[j] = 0xde;
n_delete += 1;
break;
}
next = get_next_event(next);
}
e = get_next_event(e);
}
for (int i = input->n_events - 1; i >= 0; i--) {
if (delete[i] == 0xde)
remove_event(input, i);
}
free(delete);
return n_delete;
}
// For consecutive writes, we will concat them into one message, and this will
// reduce the number of messages by 80%. However, this handler should be fast.
//
// The following is the nearest neighbor solution.
static int handle_consecutive_writes(Input *input) {
Event *e;
bool reset = false;
int consecutive_writes = 0; // 0 -> write -> 1 -> non-write|non-consecutive -> 0
Event *head = NULL, *next = NULL;
uint32_t additional_size = 0;
uint8_t *additional_data = (uint8_t *)calloc(1024, 1);
int n_delete = 0;
uint8_t *delete = (uint8_t *)calloc(input->n_events, 1);
e = get_first_event(input);
for (int i = 0; e != NULL; i++) {
if (e->type != EVENT_TYPE_MEM_WRITE) {
reset = true;
} else {
consecutive_writes += 1;
if (consecutive_writes == 1) {
head = e;
} else {
next = e;
if (head && next->addr != head->addr + head->size + additional_size) {
reset = true;
} else {
// copy
memcpy(additional_data + additional_size, next->data, next->size);
additional_size += next->size;
// let's mark this
delete[i] = 0xde;
n_delete += 1;
}
}
}
if (reset) {
// update
if (head && additional_size > 0) {
uint32_t new_size = head->size + additional_size;
uint8_t *new_data = (uint8_t *)calloc(new_size, 1);
memcpy(new_data, head->data, head->size);
memcpy(new_data + head->size, additional_data, additional_size);
free(head->data);
head->data = new_data;
head->size += additional_size;
head->event_size += additional_size;
input->size += additional_size;
}
// reset
reset = false;
consecutive_writes = 0;
head = NULL;
next = NULL;
additional_size = 0;
memset(additional_data, 0, 1024);
}
e = get_next_event(e);
}
for (int i = input->n_events - 1; i >= 0; i--) {
if (delete[i] == 0xde)
remove_event(input, i);
}
free(delete);
free(additional_data);
return n_delete;
}
// Remove useless messages as follows
static int handle_useless_messages(Input *input) {
Event *e;
int n_delete = 0;
uint8_t *delete = (uint8_t *)calloc(input->n_events, 1);
e = get_first_event(input);
for (int i = 0; e != NULL; i++) {
if (e->type == EVENT_TYPE_MEM_ALLOC ||
e->type == EVENT_TYPE_MEM_READ ||
e->type == EVENT_TYPE_MEM_FREE) {
delete[i] = 0xde; // let's mark this
n_delete += 1;
}
e = get_next_event(e);
}
for (int i = input->n_events - 1; i >= 0; i--) {
if (delete[i] == 0xde)
remove_event(input, i);
}
free(delete);
return n_delete;
}
static uint32_t serialize_group_event(Event *event, uint8_t *Data, size_t Offset, size_t MaxSize) {
static int log_counter = 0;
Input *input = (Input *)event->data;
int n_events = input->n_events;
// since this degrads the performance by 20%, I disable it
// handle_useless_messages(input);
// since this degrads the performance by 50%, I disable it
// while (handle_non_address_consecutive_writes(input)) {};
// this is covered by handle_non_address_consecutive_writes
// while (handle_consecutive_writes(input)) {};
size_t size = input->size;
// let's update event
event->size = input->size;
event->event_size = 6 + event->size;
// note that parent input is not updated, but this is fine
// we are going to end this iteration ...
if (Offset + 6 + size >= MaxSize) {
// this is only a reminder, we don't want it to degrade the performance
log_counter++;
if (log_counter < 100) {
fprintf(stderr, " * serialize_group_event: reduce %d/%d messages, remain %lu bytes",
n_events - input->n_events, n_events, input->size);
fprintf(stderr, ", but space is not enough\n");
}
Event *last_event = get_event(input, input->n_events - 1);
// serialize_xxx will print event, so we don't print it here
return event_ops[last_event->type].serialize(last_event, Data, Offset, MaxSize);
}
Data[Offset] = event->type;
Data[Offset + 1] = event->interface;
memcpy(Data + Offset + 2, (uint8_t *)&size, 4);
serialize(input, Data + Offset + 6, MaxSize - Offset - 6);
#ifdef VIDEZZO_DEBUG
event_ops[event->type].print_event(event);
#endif
return 6 + size;
}
static void release_nothing(Event *event) {
return;
}
static void release_data(Event *event) {
free(event->data);
}
static void release_group_event(Event *event) {
Input *input = (Input *)event->data;
free_input(input);
}
static void deep_copy_no_data(Event *orig, Event *copy) {
memcpy(copy, orig, sizeof(Event));
}
static void deep_copy_with_data(Event *orig, Event *copy) {
memcpy(copy, orig, sizeof(Event));