-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmcts.c
1418 lines (1278 loc) · 57.6 KB
/
mcts.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
#define _GNU_SOURCE
#include "mcts.h"
/* Hyper-parameters */
gdouble RHO = 1.414; //or sqrt(2)
u32 MAX_TREE_LOG_DEPTH = 5;
/* Not in use */
u32 MIN_SAMPLES = 1;
u32 MAX_SAMPLES = 100;
u32 CONEX_TIMEOUT = 0;
gboolean PERSISTENT = FALSE;
gboolean COVERAGE_ONLY = TRUE;
enum score_function SCORE_FUNCTION = UCT;
/* Statistics */
uint ROUND = 0;
/* ============================================== TreeNode Functions ============================================== */
TreeNodeData* new_tree_node_data (u32 response_code, enum node_colour colour, u32* path, u32 path_len)
{
TreeNodeData* tree_node_data;
tree_node_data = g_new(TreeNodeData, 1); // allocates 1 element of TreeNode
// set property
tree_node_data->id = response_code;
/* NOTE: The path of each node is the prefix of the path in which the node is found */
tree_node_data->path = malloc(sizeof(u32)*path_len);
memcpy(tree_node_data->path, path, sizeof(u32)*path_len);
tree_node_data->path_len = path_len;
//NOTE: This is probably not needed, left it here in case it comes in handy later.
tree_node_data->colour = colour;
int absent_in_khmn_nodes = 0;
khint_t k = kh_put_hmn(khmn_nodes, response_code, &absent_in_khmn_nodes);
if (absent_in_khmn_nodes) {
log_info("RES_CODE %u does not exist yet", response_code);
kh_value(khmn_nodes, k) = 0;
} else {
log_info("RES_CODE %u exists", response_code);
}
if (colour == Golden) {
log_assert(tree_node_data->id == 999,
"[NEW_TREE_NODE_DATA] The ID of SimNode is not 999: %03u\n", tree_node_data->id);
} else {
log_assert(tree_node_data->id == tree_node_data->path[tree_node_data->path_len - 1],
"[NEW_TREE_NODE_DATA] The ID of RspNode does not match the last code in its path: %03u != %03u",
tree_node_data->id, tree_node_data->path[tree_node_data->path_len - 1]);
}
// set statistics
tree_node_data->selected = 0;
tree_node_data->discovered = 0;
// tree_node_data->stats.selected_seed_index = 0;
tree_node_data->seeds = NULL;
tree_node_data->region_indices = NULL;
tree_node_data->seeds_count = 0;
//TODO: Detect terminations (i.e. leaves) with some response code, and mark them fully explored.
// e.g. predefine a set of termination code
//TOASK: Given the last node of a past communication, would it have any child if we give it more inputs?
tree_node_data->fully_explored = FALSE;
//NOTE: No way to know if we have found all possible inputs to fuzz a node.
tree_node_data->exhausted = FALSE;
return tree_node_data;
}
TreeNode* new_tree_node(TreeNodeData* tree_data)
{
GNode* tree_node;
tree_node = g_node_new (tree_data);
return tree_node;
}
TreeNodeData* get_tree_node_data(TreeNode* tree_node)
{
return (TreeNodeData*) tree_node->data;
}
TreeNode* get_simulation_child(TreeNode* tree_node)
{
TreeNodeData* node_data = get_tree_node_data(tree_node);
TreeNode* sim_child = node_data->simulation_child;
char* message_tree_node_repr_tree_node = tree_node_repr(tree_node);
char* message_tree_node_repr_sim_node = tree_node_repr(tree_node);
log_assert(sim_child != NULL, "[GET_SIMULATION_CHILD] No sim child of %s", message_tree_node_repr_tree_node);
log_assert(node_data->colour != Golden && node_data->colour != Black,
"[GET_SIMULATION_CHILD] %s should not have a child SimNode %s",
message_tree_node_repr_tree_node, message_tree_node_repr_sim_node);
log_assert(get_tree_node_data(sim_child)->colour == Golden,
"[GET_SIMULATION_CHILD] Golden is not the colour of the sim child (%s) of %s",
message_tree_node_repr_sim_node, message_tree_node_repr_tree_node);
free(message_tree_node_repr_tree_node);
free(message_tree_node_repr_sim_node);
message_tree_node_repr_tree_node = NULL;
message_tree_node_repr_sim_node = NULL;
return sim_child;
}
double tree_node_exploitation_score(TreeNode* tree_node)
{
TreeNodeData* node_data = get_tree_node_data(tree_node);
if (!node_data->selected) return INFINITY;
log_assert(node_data->selected, "[TREE_NODE_EXPLOITATION_SCORE] Node not selected, causing div by 0 error");
return (double) node_data->discovered / node_data->selected;
}
double seed_exploitation_score(TreeNode* tree_node, int seed_index)
{
seed_info_t* target_seed = get_tree_node_data(tree_node)->seeds[seed_index];
if (!target_seed->selected) return INFINITY;
log_assert(target_seed->selected, "[SEED_EXPLOITATION_SCORE] Seed not selected, causing div by 0 error");
return (double) target_seed->discovered / target_seed->selected;
}
double tree_node_exploration_score(TreeNode* tree_node)
{
if (G_NODE_IS_ROOT(tree_node)) return INFINITY;
log_assert(tree_node->parent != NULL,
"[TREE_NODE_EXPLORATION_SCORE] The parent of node (%03u) does not exist",
get_tree_node_data(tree_node)->id);
TreeNodeData* node_data = get_tree_node_data(tree_node);
if (!node_data->selected) { return INFINITY; }
TreeNodeData* parent_data = get_tree_node_data(tree_node->parent);
// log_trace("[TREE_NODE_EXPLORATION_SCORE] Node %03u explore score: %lf = %lf * sqrt(2 * log(%lf) / %lf)",
// node_data->id,
// RHO * sqrt(2 * log((double) parent_data->selected) / (double) node_data->selected),
// RHO, (double) parent_data->selected, (double) node_data->selected);
return RHO * sqrt(2 * log((double) parent_data->selected) / (double) node_data->selected);
}
double seed_exploration_score(TreeNode* tree_node, int seed_index)
{
seed_info_t* target_seed = get_tree_node_data(tree_node)->seeds[seed_index];
TreeNodeData* node_data = get_tree_node_data(tree_node);
if (!target_seed->selected) { return INFINITY; }
// log_trace("[SEED_EXPLORATION_SCORE] %lf = %lf * sqrt(2 * log(%lf) / %lf)",
// RHO * sqrt(2 * log((double)node_data->selected)/target_seed->selected),
// RHO, (double)node_data->selected, (double) target_seed->selected);
return RHO * sqrt(2 * log((double)node_data->selected)/ (double) target_seed->selected);
}
double tree_node_score(TreeNode* tree_node)
{
/*
* If using the random score function, return a random number
* Else compute the score of a node:
* 1. If the node is fully explored, return -INFINITY
* 2. If the node is root, return INFINITY
* 3. If the node fits the fish bone optimisation, return -INFINITY
* 4. If the node has not been selected before, return INFINITY
* 5. If the node has been selected before, apply the UCT function
*/
if (SCORE_FUNCTION == Random) return g_rand_int(RANDOM_NUMBER_GENERATOR);
TreeNodeData* tree_node_data = get_tree_node_data(tree_node);
if (tree_node_data->fully_explored) return -INFINITY;
// If a SimNode does not have a seed, do not select it.
if (tree_node_data->colour == Golden && !tree_node_data->seeds_count) return -INFINITY;
// if (G_NODE_IS_ROOT(tree_node)) return INFINITY;
// if (fits_fish_bone_optimisation(tree_node)) return -INFINITY;
// if (!get_tree_node_data(tree_node)->selected) return INFINITY;
double exploit_score = tree_node_exploitation_score(tree_node);
double explore_score = tree_node_exploration_score(tree_node);
return exploit_score + explore_score;
}
double seed_score(TreeNode* tree_node, u32 seed_index)
{
// return g_rand_int(RANDOM_NUMBER_GENERATOR);
if (SCORE_FUNCTION == Random) return g_rand_int(RANDOM_NUMBER_GENERATOR);
// seed_info_t* target_seed = get_tree_node_data(tree_node)->seeds[seed_index];
// if (!target_seed->selected) return INFINITY;
double exploit_score = seed_exploitation_score(tree_node, seed_index);
double explore_score = seed_exploration_score(tree_node, seed_index);
return exploit_score + explore_score;
}
gboolean is_leaf(TreeNode* tree_node) {
/*
* If the node is a phantom, then it is not a leaf
* If the node has no child, it is a leaf
* If the node has only a simulation child, it is a leaf
* Else it is not
*/
return (get_tree_node_data(tree_node)->colour != Purple)
&& (G_NODE_IS_LEAF(tree_node)
|| ((g_node_n_children(tree_node) == 1) &&
get_tree_node_data(g_node_first_child(tree_node))->colour == Golden));
}
//gboolean fits_fish_bone_optimisation(TreeNode * tree_node)
//{
// /* NOTE: We might not be able to know for sure if a node/subtree is fully explored.
// * One optimisation is to predefine a set of termination response codes.
// * Fish bone optimisation: if a simulation child
// * has only one sibling X who is not fully explored,
// * and X is not white (so that all siblings are found)
// * then do not simulate from that simulation child but only from X
// * as all new paths can only come from X
// */
//// gboolean is_simul = (get_tree_node_data(tree_node)->colour == Golden);
//
// return FALSE;
//}
TreeNode* best_child(TreeNode* tree_node)
{
gdouble max_score = -INFINITY;
u32 number_of_children = g_node_n_children(tree_node);
u32 number_of_ties = 0;
u32 ties[number_of_children];
char* message = tree_node_repr(tree_node);
log_info("[BEST_CHILD] Selecting the best child of: %s", message);
free(message);
message = NULL;
if (number_of_children == 1) {
char* message = tree_node_repr(g_node_nth_child(tree_node, 0));
log_info("[BEST_CHILD] Only one child: %s", message);
free(message);
message = NULL;
return g_node_nth_child(tree_node, 0);
}
log_info("[BEST_CHILD] Multiple children");
log_info("[BEST_CHILD] Max score: %lf; Number of ties: %u", max_score, number_of_ties);
for (u32 child_index = 0; child_index < number_of_children; child_index++) {
gdouble score = tree_node_score(g_node_nth_child(tree_node, child_index));
message = tree_node_repr(g_node_nth_child(tree_node, child_index));
log_info("[BEST_CHILD] Current index %u: %lf (%s)",
child_index, score, message);
free(message);
message = NULL;
if (score < max_score) continue;
if (score > max_score) number_of_ties = 0;
max_score = score;
ties[number_of_ties] = child_index;
number_of_ties ++;
log_info("[BEST_CHILD] Max score: %lf; Current score: %lf; Number of ties: %u",
max_score, score, number_of_ties);
for (u32 tie_index=0; tie_index < number_of_ties; tie_index++)
{
char* message = tree_node_repr(g_node_nth_child(tree_node, ties[tie_index]));
log_info("[BEST_CHILD] Tie index %u: %s",
tie_index, message);
free(message);
message = NULL;
}
}
if (number_of_ties == 1)
{
char* message = tree_node_repr(g_node_nth_child(tree_node, ties[0]));
log_info("[BEST_CHILD] Only one candidate: %s", message);
free(message);
message = NULL;
return g_node_nth_child(tree_node, ties[0]);
}
log_info("[BEST_CHILD] Best child has %u candidates", number_of_ties);
for (u32 i = 0; i < number_of_ties; ++i) {
char* message = tree_node_repr(g_node_nth_child(tree_node, ties[i]));
log_info("[BEST_CHILD] The %u th candidates in ties is the %u th child: %s",
i, ties[i], message);
free(message);
message = NULL;
}
u32 winner_index = g_rand_int_range(RANDOM_NUMBER_GENERATOR, 0, number_of_ties);
u32 winner = ties[winner_index];
log_info("[BEST_CHILD] Winner index in ties is: %u", winner_index);
message = tree_node_repr(g_node_nth_child(tree_node, winner));
log_info("[BEST_CHILD] Winner is the %u th child: %s", winner, message);
free(message);
message = NULL;
return g_node_nth_child(tree_node, winner);
}
seed_info_t* best_seed(TreeNode* tree_node)
{
TreeNodeData* tree_node_data = get_tree_node_data(tree_node);
gdouble max_score = -INFINITY;
u32 number_of_seeds = tree_node_data->seeds_count;
u32 number_of_ties = 0;
u32 ties[number_of_seeds];
char* message = tree_node_repr(tree_node);
log_debug("[BEST_SEED] Selecting the best seed of: %s", message);
// There should be at least one seed to select
log_assert(number_of_seeds > 0, "Selecting seed from a SimNode without any seed: %s", message);
free(message);
message = NULL;
if (number_of_seeds == 1) {
message = seed_repr(tree_node, 0, NULL);
log_debug("[BEST_SEED] Only one seed: %s", message);
free(message);
message = NULL;
return get_tree_node_data(tree_node)->seeds[0];
}
log_debug("[BEST_SEED] Multiple seeds");
log_debug("[BEST_SEED] Max score: %lf; Number of ties: %u", max_score, number_of_ties);
for (u32 seed_index = 0; seed_index < number_of_seeds; seed_index++) {
double score = seed_score(tree_node, seed_index);
message = seed_repr(tree_node, seed_index, NULL);
log_debug("[BEST_SEED] Current index %u: %lf (%s)",
seed_index, score, message);
free(message);
message = NULL;
if (score < max_score) continue;
if (score > max_score) number_of_ties = 0;
max_score = score;
ties[number_of_ties] = seed_index;
number_of_ties ++;
log_debug("[BEST_SEED] Max score: %lf; Current score: %lf; Number of ties: %u",
max_score, score, number_of_ties);
for (u32 tie_index=0; tie_index < number_of_ties; tie_index++)
{
message = seed_repr(tree_node, ties[tie_index], NULL);
log_debug("[BEST_SEED] Tie index %u: %s", tie_index, message);
free(message);
message = NULL;
}
}
if (number_of_ties == 1)
{
message = seed_repr(tree_node, ties[0], NULL);
log_debug("[BEST_SEED] Only one candidate: %s", message);
free(message);
message = NULL;
return get_tree_node_data(tree_node)->seeds[ties[0]];
}
log_debug("[BEST_SEED] Best seed has %u candidates", number_of_ties);
for (u32 i = 0; i < number_of_ties; ++i) {
message = seed_repr(tree_node, ties[i], NULL);
log_debug("[BEST_SEED] The %u th candidates in ties is the %u th seed: %s",
i, ties[i], message);
free(message);
message = NULL;
}
u32 winner_index = g_rand_int_range(RANDOM_NUMBER_GENERATOR, 0, number_of_ties);
u32 winner = ties[winner_index];
log_debug("[BEST_SEED] Winner index in ties is: %u", winner_index);
message = seed_repr(tree_node, winner, NULL);
log_debug("[BEST_SEED] Winner is the %u th seed: %s", winner, message);
free(message);
message = NULL;
return get_tree_node_data(tree_node)->seeds[winner];
}
TreeNode* exists_child(TreeNode* tree_node, u32 target_response_code)
{
TreeNode* child_node = g_node_first_child(tree_node);
while (child_node)
{
// tree_node_print(child_node);
if (get_tree_node_data(child_node)->id == target_response_code) return child_node;
child_node = g_node_next_sibling(child_node);
}
return NULL;
}
TreeNode* append_child(TreeNode* tree_node, u32 child_response_code, enum node_colour colour, u32* path, u32 path_len)
{
TreeNode* child = g_node_append_data(tree_node, new_tree_node_data(child_response_code, colour, path, path_len));
if (colour == White) get_tree_node_data(child)->simulation_child = append_child(child, 999, Golden, path, path_len);
return child;
}
//void print_reversed_path(TreeNode* tree_node)
//{
// do {
// g_printf("%u ", get_tree_node_data(tree_node)->id);
// tree_node = tree_node->parent;
// } while (tree_node);
// g_printf("\n");
//}
//
//void print_path(TreeNode* tree_node)
//{
// TreeNodeData* tree_node_data = get_tree_node_data(tree_node);
// for (u32 i = 0; i < tree_node_data->path_len; i++) {
// g_printf("%u ", tree_node_data->path[i]);
// }
// g_print("\n");
//}
char* node_path_str(TreeNode* tree_node)
{
TreeNodeData* tree_node_data = get_tree_node_data(tree_node);
char* a_str = NULL;
int a_str_len = asprintf(&a_str, "%u", tree_node_data->path[0]);
for (int i = 1; i < tree_node_data->path_len; ++i) {
log_assert(a_str_len != -1, "[NODE_PATH_STR] Function asprintf returned error code -1");
a_str_len = asprintf(&a_str, "%s, %u", a_str, tree_node_data->path[i]);
}
return a_str;
}
int colour_encoder(enum node_colour colour) {
int colour_code = 0;
switch (colour) {
case White:
colour_code = 37;
break;
case Red:
colour_code = 31;
break;
case Golden:
colour_code = 33;
break;
case Purple:
colour_code = 35;
break;
case Black:
colour_code = 90;
break;
}
log_assert(colour_code, "[COLOUR_ENCODER] Colour code was not set, incorrect colour enum? %d", colour);
return colour_code;
}
char* seed_repr(TreeNode* tree_node, uint seed_index, seed_info_t* seed)
{
if (!seed) {seed = get_tree_node_data(tree_node)->seeds[seed_index];}
else {log_assert(seed == get_tree_node_data(tree_node)->seeds[seed_index],
"[SEED_REPR] Param seed does not match with the seed at seed_index in tree_node: %s != %s",
seed->q->fname, get_tree_node_data(tree_node)->seeds[seed_index]->q->fname);}
char* message = NULL;
struct queue_entry* queue_entry = seed->q;
message_append(&message,
"Seed %03u: %06.2lf [%06.2lf + %06.2lf] {%03u, %03u} (%s)",
seed->parent_index, seed_score(tree_node, seed_index),
seed_exploitation_score(tree_node, seed_index),
seed_exploration_score(tree_node, seed_index),
seed->selected, seed->discovered, queue_entry->fname);
return message;
}
char* tree_node_repr(TreeNode* tree_node)
{
TreeNodeData* tree_node_data = get_tree_node_data(tree_node);
char* message = NULL;
message_append(&message, "\033[1;%dm%03u: %06.2lf "
"[%06.2lf + %06.2lf] "
"{%03u, %03u} "
"<%03u>\033[0m",
colour_encoder(tree_node_data->colour),
tree_node_data->id,
tree_node_score(tree_node),
tree_node_exploitation_score(tree_node),
tree_node_exploration_score(tree_node),
tree_node_data->selected,
tree_node_data->discovered,
tree_node_data->seeds_count);
return message;
}
char* region_state_repr(region_t region)
{
char* message = NULL;
char* message_arr = u32_array_to_str(region.state_sequence, region.state_count);
message_append(&message, "%02u states: %s",
region.state_count, message_arr);
free(message_arr);
message_arr = NULL;
return message;
}
void tree_log(TreeNode* tree_node, TreeNode* mark_node, int indent, int found)
{
// log_Message* message = (log_Message*) message_init();
char* message = NULL;
for (int i = 0; i < indent-1; ++i) message_append(&message, "| ");
if (indent) message_append(&message, "|-- ");
if (indent>MAX_TREE_LOG_DEPTH) {
message_append(&message, " ... ");
log_info(message);
free(message);
message = NULL;
return;
}
char* message_tree_node_repr_tree_node = tree_node_repr(tree_node);
message_append(&message, message_tree_node_repr_tree_node);
free(message_tree_node_repr_tree_node);
message_tree_node_repr_tree_node = NULL;
if (tree_node == mark_node && found >= 0) message_append(&message, "\033[1;32m <=< found %u\033[0m", found);
log_info(message);
free(message);
message = NULL;
if (g_node_n_children(tree_node)) indent++;
for (int i = 0; i < g_node_n_children(tree_node); ++i) {
tree_log(g_node_nth_child(tree_node, i), mark_node, indent, found);
}
}
void seed_log(TreeNode* tree_node, seed_info_t* seed_selected, char* calling_function)
{
char* message = NULL;
TreeNodeData* tree_node_data = get_tree_node_data(tree_node);
for (uint i = 0; i < tree_node_data->seeds_count; ++i) {
message = NULL;
seed_info_t* seed = tree_node_data->seeds[i];
message_append(&message, calling_function);
message_append(&message, "Candidate ");
char* message_seed_repr = seed_repr(tree_node, i, seed);
message_append(&message, message_seed_repr);
free(message_seed_repr);
message_seed_repr = NULL;
log_info(message);
free(message);
message = NULL;
}
}
void seed_selected_log(TreeNode* tree_node, seed_info_t* seed_selected, char* calling_function)
{
char* message = tree_node_repr(tree_node);
log_assert(seed_selected != NULL,
"[SEED_SELECTED_LOG] Seed selected cannot be NULL: %s of %s", seed_selected, message);
free(message);
message = NULL;
TreeNodeData* tree_node_data = get_tree_node_data(tree_node);
for (uint i = 0; i < tree_node_data->seeds_count; ++i) {
seed_info_t* seed = tree_node_data->seeds[i];
if (seed != seed_selected) {continue;}
message = NULL;
message_append(&message, calling_function);
message_append(&message, "Selected ");
char* message_seed_repr = seed_repr(tree_node, i, seed);
message_append(&message, message_seed_repr);
free(message_seed_repr);
message_seed_repr = NULL;
message_append(&message, "\033[1;32m <=< Selected\033[0m");
log_info(message);
free(message);
message = NULL;
}
}
void queue_state_log(struct queue_entry* queue)
{
char* message = NULL;
char* message_region_state_repr = NULL;
for (u32 region_index = 0; region_index < queue->region_count; ++region_index) {
message = NULL;
message_region_state_repr = NULL;
message_region_state_repr = region_state_repr(queue->regions[region_index]);
message_append(&message, "Region %2u has %s", region_index, message_region_state_repr);
free(message_region_state_repr);
message_region_state_repr = NULL;
if (queue->regions[region_index].state_sequence) { log_info(message); }
else { log_warn(message); }
free(message);
message = NULL;
}
}
seed_info_t* construct_seed_with_queue_entry(struct queue_entry* q)
{
seed_info_t* seed = (seed_info_t*) malloc (sizeof(seed_info_t));
seed->q = q;
seed->selected = 0;
seed->discovered = 0;
return seed;
}
void add_seed_to_node(seed_info_t* seed, u32 matching_region_index, TreeNode * node)
{
log_assert(seed != NULL, "[ADD_SEED_TO_NODE] Seed does not exist");
char* message = tree_node_repr(node);
log_assert(get_tree_node_data(node)->colour == Golden, "[ADD_SEED_TO_NODE] Not a SimNode: %s", message);
free(message);
message = NULL;
/*NOTE: Figure out M2 at here so that we don't have to do it repeatedly when the same queue_entry is selected*/
// struct queue_entry* q = seed->q;
// region_t* regions = q->regions;
TreeNodeData* tree_node_data = get_tree_node_data(node);
// TOASK: Should we allocate more spaces to avoid repeated reallocation?
TreeNodeData* node_data = get_tree_node_data(node);
node_data->seeds = (seed_info_t **) realloc (node_data->seeds, (node_data->seeds_count + 1) * sizeof(void *));
node_data->seeds[node_data->seeds_count] = (seed_info_t *) seed;
node_data->region_indices = (u32*) realloc (node_data->region_indices, (node_data->seeds_count + 1) * sizeof(u32));
node_data->region_indices[node_data->seeds_count] = matching_region_index;
seed->parent_index = node_data->seeds_count;
node_data->seeds_count++;
seed_info_t* node_seed = node_data->seeds[node_data->seeds_count-1];
struct queue_entry* node_q = node_seed->q;
region_t node_region = node_q->regions[node_data->region_indices[seed->parent_index]];
log_debug("[ADD_SEED_TO_NODE] Region ID of node : %u", node_data->region_indices[node_seed->parent_index]);
log_debug("[ADD_SEED_TO_NODE] Region count of queue: %u", node_q->region_count);
log_assert(node_data->region_indices[node_seed->parent_index] <= node_q->region_count,
"[ADD_SEED_TO_NODE] The region index stored in the parent node is greater than the region count: %u > %u",
node_data->region_indices[node_seed->parent_index], node_q->region_count);
log_debug("[ADD_SEED_TO_NODE] Region state count: %u", node_region.state_count);
log_debug("[ADD_SEED_TO_NODE] Node path length : %u", tree_node_data->path_len);
message = u32_array_to_str(node_region.state_sequence, node_region.state_count);
log_debug("[ADD_SEED_TO_NODE] Region states: %s", message);
free(message);
message = NULL;
message = u32_array_to_str(tree_node_data->path, tree_node_data->path_len);
log_debug("[ADD_SEED_TO_NODE] Node path : %s", message);
free(message);
message = NULL;
log_assert(node_region.state_count >= tree_node_data->path_len,
"[ADD_SEED_TO_NODE] The path len of a tree node is smaller than "
"the state count of the matching region: %u < %u",
tree_node_data->path_len, node_region.state_count);
message = u32_array_to_str(node_region.state_sequence, tree_node_data->path_len);
char* message_node = u32_array_to_str(tree_node_data->path, tree_node_data->path_len);
log_assert(!memcmp(node_region.state_sequence, tree_node_data->path, tree_node_data->path_len),
"[ADD_SEED_TO_NODE] The path of a tree node is different to "
"the states of the matching region:\nREGN: %s\nNODE: %s", message, message_node);
free(message);
free(message_node);
message = NULL;
message_node = NULL;
}
u32* collect_region_path(region_t region, u32* path_len)
{
*path_len = region.state_count;
return region.state_sequence;
}
/* ============================================== TreeNode Functions ============================================== */
/* ================================================ MCTS Functions ================================================ */
TreeNode* select_tree_node(TreeNode* parent_tree_node)
{
get_tree_node_data(parent_tree_node)->selected++;
char* message1 = NULL;
char* message2 = NULL;
while (get_tree_node_data(parent_tree_node)->colour != Golden) {
parent_tree_node = best_child(parent_tree_node);
message1 = tree_node_repr(parent_tree_node);
log_info("[SELECT_TREE_NODE] Best child is: %s", message1);
free(message1);
message1 = NULL;
/* NOTE: Selected stats propagation of nodes along the selection path is done here */
get_tree_node_data(parent_tree_node)->selected++;
/*NOTE: If the score of the best child is -inf,
* then the score of the parent of the best child should be -inf
* e.g. parent is a black node, whose only child is a leaf
*/
message1 = tree_node_repr(parent_tree_node);
if (!G_NODE_IS_ROOT(parent_tree_node)) {message2 = tree_node_repr(parent_tree_node);}
log_assert(
tree_node_score(parent_tree_node) > -INFINITY ||
(!G_NODE_IS_ROOT(parent_tree_node) && get_tree_node_data(parent_tree_node->parent)->colour == Black),
"[SELECT_TREE_NODE] If the score of the best child is -inf, "
"then the score of the parent of the best child should be -inf:"
"e.g. parent is a black node, whose only child is a leaf.\nNode :%s\nParent:%s",
message1,
G_NODE_IS_ROOT(parent_tree_node)?"Is ROOT":message2);
free(message1);
free(message2);
message1 = NULL;
message2 = NULL;
while (tree_node_score(parent_tree_node) == -INFINITY)
{
message1 = tree_node_repr(parent_tree_node);
log_info("[SELECT_TREE_NODE] The score of the best child is -inf: %s", message1);
free(message1);
message1 = NULL;
/* NOTE: If the score of the root is -inf, then ROOT must be fully explored,
* hence there is no point continuing;
*/
message1 = tree_node_repr(parent_tree_node);
log_assert(!G_NODE_IS_ROOT(parent_tree_node),
"[SELECT_TREE_NODE] If the score of the root is -inf, then ROOT must be fully explored, "
"hence there is no point continuing;\nNode: %s", message1);
free(message1);
message1 = NULL;
parent_tree_node = parent_tree_node->parent;
message1 = tree_node_repr(parent_tree_node);
log_info("[SELECT_TREE_NODE] Checking its parent: %s", message1);
free(message1);
message1 = NULL;
if (!is_fully_explored(parent_tree_node)) {
message1 = tree_node_repr(parent_tree_node);
log_info("[SELECT_TREE_NODE] Resume selection from its parent: %s", message1);
free(message1);
message1 = NULL;
break;
}
get_tree_node_data(parent_tree_node)->fully_explored = TRUE;
message1 = tree_node_repr(parent_tree_node);
log_info("[SELECT_TREE_NODE] Setting its parent fully explored: %s", message1);
free(message1);
message1 = NULL;
}
log_assert(parent_tree_node != NULL, "[SELECT_TREE_NODE] NULL is the best child");
message1 = tree_node_repr(parent_tree_node);
log_assert(tree_node_score(parent_tree_node) > -INFINITY,
"[SELECT_TREE_NODE] -inf is the score of the best child %s", message1);
free(message1);
message1 = NULL;
}
return parent_tree_node;
}
gboolean is_fully_explored(TreeNode* parent_tree_node)
{
/*NOTE: There are four possibilities:
* 1. White parent + white children:
* By definition, the parent is not fully explored even if all children are.
* Because there might be missing children that can be found by simulating from parent's SimNode
* 2. White parent + Black children:
* Same above, as being a black child only means all existing inputs bound the black child with a grandchild
* (i.e. by making the server returns more than response code as the same time.)
* It has nothing to do with the parent node
* 3. Black parent + white child:
* This is case we care about.
* As far as we know, it happens when the white child is a leaf. There could be other corner cases that cause this.
* Here I use -inf to represent "being a leaf" or "fully explored", in case of other corner cases.
* But the implication is the same:
* a) The white child must be the only child;
* b) The parent does not have any SimNode;
* c) The black parent should not be selected, otherwise we will fuzz a leaf, which is problematic and unnecessary.
* 4. Black parent + black child:
* This should be the same as 3, except the black child is not a leaf.
* The black child might have a single white leaf.
* After marking the black child fully explored in the past, this will make the black parent fully explored, too.
* Again:
* a) the black child must be the only child;
* b) The parent does not have any SimNode;
* c) The black parent should not be selected, otherwise we will fuzz a leaf, which is problematic and unnecessary.
* In short:
* a) If the parent is not black, it is not fully explored, return False;
* b) If the parent is black, it is fully explored only if the score of its only child is -inf.
*/
if (get_tree_node_data(parent_tree_node)->fully_explored) {return TRUE;}
if (get_tree_node_data(parent_tree_node)->colour != Black) {return FALSE;}
char* message = tree_node_repr(parent_tree_node);
log_assert(g_node_n_children(parent_tree_node) == 1,
"[IS_FULLY_EXPLORED] More than one child exists under %s", message);
log_assert(get_tree_node_data(g_node_nth_child(parent_tree_node, 0))->colour != Golden,
"[IS_FULLY_EXPLORED] A SimNode is the only child of %s", message);
free(message);
message = NULL;
return tree_node_score(g_node_nth_child(parent_tree_node, 0)) == -INFINITY;
}
seed_info_t* select_seed(TreeNode* tree_node_selected)
{
seed_info_t* seed = best_seed(tree_node_selected);
/* NOTE: Selected stats propagation of the seed is done here */
seed->selected++;
return seed;
}
TreeNode* Initialisation(uint log_lvl, uint tree_dp, uint ign_ast, double rho)
{
char log_file[100];
snprintf(log_file, sizeof(log_file), "%s", getenv("FUZZER_LOG"));
log_add_fp(fopen(log_file, "w+"), log_lvl);
log_set_quiet(TRUE);
set_ignore_assertion(ign_ast);
log_info("[INITIALISATION] LOG PATH: %s", log_file);
MAX_TREE_LOG_DEPTH = tree_dp;
RHO = rho;
log_info("[INITIALISATION] Log level: %u", log_lvl);
log_info("[INITIALISATION] Max tree log depth: %u", MAX_TREE_LOG_DEPTH);
log_info("[INITIALISATION] Ignore assertions: %s", ign_ast?"True":"False");
log_info("[INITIALISATION] Rho: %lf", RHO);
khmn_nodes = kh_init_hmn();
u32 path[] = {0};
TreeNode* root = new_tree_node(new_tree_node_data(0, White, path, 1));
get_tree_node_data(root)->simulation_child = append_child(root, 999, Golden, path, 1);
return root;
}
seed_info_t* Selection(TreeNode** tree_node)
{
ROUND += 1;
TreeNode* root = *tree_node;
log_info("[SELECTION] ==========< ROUND %03d >==========", ROUND);
log_assert(G_NODE_IS_ROOT(*tree_node),
"[SELECTION] Selection does not start from ROOT, but from %s", tree_node_repr(*tree_node));
*tree_node = select_tree_node(*tree_node);
char* message = tree_node_repr(*tree_node);
log_info("[SELECTION] Selected node : %s", message);
free(message);
message = NULL;
message = tree_node_repr((*tree_node)->parent);
log_info("[SELECTION] Selected parent : %s", message);
free(message);
message = NULL;
while (tree_node_score((*tree_node)->parent) == -INFINITY) {
log_info("[SELECTION] The score of the parent of the simulation child is -inf, redo selection");
message = tree_node_repr(*tree_node);
log_assert(G_NODE_IS_ROOT(*tree_node),
"[SELECTION] Selection does not re-start from ROOT, but from %s", message);
free(message);
message = NULL;
*tree_node = select_tree_node(root);
message = tree_node_repr(*tree_node);
log_info("[SELECTION] Selected node : %s", message);
free(message);
message = NULL;
message = tree_node_repr((*tree_node)->parent);
log_info("[SELECTION] Selected parent : %s", message);
free(message);
message = NULL;
}
message = tree_node_repr((*tree_node)->parent);
log_assert(tree_node_score((*tree_node)->parent) > -INFINITY,
"[SELECTION] Selected SimNode's parent has -inf score: %s", message);
free(message);
message = NULL;
seed_log(*tree_node, NULL, "[SELECTION] ");
TreeNodeData* parent_node_data = get_tree_node_data((*tree_node)->parent);
khiter_t k = kh_get_hmn(khmn_nodes, parent_node_data->id);
log_assert(kh_exist(khmn_nodes, k),
"[SELECTION] Parent node (%03u) does not exist in HashMap",
parent_node_data->id);
int count = kh_value(khmn_nodes, k);
kh_value(khmn_nodes, k) = 1 + count;
seed_info_t* seed_selected = select_seed(*tree_node);
seed_selected_log(*tree_node, seed_selected, "[SELECTION] ");
struct queue_entry* q = seed_selected->q;
log_info("[SELECTION] Selection seed : %s", q->fname);
for (int i = 0; i < q->region_count; ++i) {
message = u32_array_to_str(q->regions[i].state_sequence, q->regions[i].state_count);
log_debug("[SELECTION] Seed region %2d : %s",
i, message);
free(message);
message = NULL;
}
TreeNodeData* tree_node_data = get_tree_node_data(*tree_node);
log_info("[SELECTION] Selection path : %s", node_path_str(*tree_node));
message = u32_array_to_str(q->regions[tree_node_data->region_indices[seed_selected->parent_index]].state_sequence,
q->regions[tree_node_data->region_indices[seed_selected->parent_index]].state_count);
log_debug("[SELECTION] Selection region : %s", message);
free(message);
message = NULL;
return seed_selected;
}
char* Simulation(TreeNode* target)
{
// assert(get_tree_node_data(target)->colour == Golden);
return NULL;
}
void remove_null_regions(struct queue_entry* q)
{
u32 null_region_count = 0;
char* message = NULL;
for (u32 region_index = 0; region_index < q->region_count; ++region_index) {
message = u32_array_to_str(q->regions[region_index].state_sequence, q->regions[region_index].state_count);
log_assert((q->regions[region_index].state_count > 0) == (q->regions[region_index].state_sequence != NULL),
"The state count %u does not match with state sequence:\n%s",
q->regions[region_index].state_count, message);
free(message);
message = NULL;
if (!null_region_count && q->regions[region_index].state_sequence != NULL) {continue;}
while (region_index+null_region_count < q->region_count
&& q->regions[region_index+null_region_count].state_sequence == NULL)
{
null_region_count++;
}
if (region_index+null_region_count >= q->region_count)
{
log_info("[PREPROCESS_QUEUE_ENTRY] Reached region count %u while searching for region at index %u",
q->region_count, region_index+null_region_count);
q->region_count = region_index;
log_info("[PREPROCESS_QUEUE_ENTRY] Set region count to be %u accordingly", q->region_count);
break;
}
log_info("[PREPROCESS_QUEUE_ENTRY] Replacing region ID %u with %u", region_index, region_index+null_region_count);
message = u32_array_to_str(q->regions[region_index+null_region_count].state_sequence,
q->regions[region_index+null_region_count].state_count);
log_assert(q->regions[region_index+null_region_count].state_sequence != NULL,
"State sequence at region index %u should not be NULL: %s",
region_index+null_region_count, message);
free(message);
message = NULL;
q->regions[region_index] = q->regions[region_index+null_region_count];
}
for (u32 region_index = 0; region_index < q->region_count; ++region_index)
{
message = u32_array_to_str(q->regions[region_index].state_sequence, q->regions[region_index].state_count);
log_assert(q->regions[region_index].state_sequence != NULL && q->regions[region_index].state_count,
"After preprocessing q, region %u has %u states: %s",
region_index,
q->regions[region_index].state_count,
message);
free(message);
message = NULL;
}
}
void truncate_long_regions(struct queue_entry* q)
{
uint truncated_state_count=200;
char* message = NULL;
for (u32 region_index = 0; region_index < q->region_count; ++region_index) {
message = u32_array_to_str(q->regions[region_index].state_sequence, q->regions[region_index].state_count);
log_assert((q->regions[region_index].state_count > 0) == (q->regions[region_index].state_sequence != NULL),
"The state count %u does not match with state sequence:\n%s",
q->regions[region_index].state_count, message);
free(message);
message = NULL;
if (q->regions[region_index].state_count < truncated_state_count) {continue;}
log_info("[TRUNCATE_LONG_REGIONS] %d states in region %d (%s)",
q->regions[region_index].state_count, region_index, q->fname);
// unsigned int truncated_state_sequence[truncated_state_count];
// for (uint j = 0; j < truncated_state_count; ++j) {
// truncated_state_sequence[j] = q->regions[region_index].state_sequence[j];
// }
//
q->regions[region_index].state_count = truncated_state_count;
// q->regions[region_index].state_sequence = truncated_state_sequence;
}
}
void preprocess_queue_entry(struct queue_entry* q)
{
remove_null_regions(q);
truncate_long_regions(q);
}
TreeNode* Expansion(TreeNode* tree_node, struct queue_entry* q, u32* response_codes, u32 len_codes, gboolean* new_path)
{
TreeNode* parent_node = tree_node;
gboolean new_node = FALSE;
*new_path = FALSE;
u32 matching_region_index = 0;
gboolean matched_exactly = FALSE;
char* message = NULL;
char* message_node = NULL;
char* message_parent = NULL;
// Construct seed with queue_entry q
seed_info_t* seed = NULL;
log_info("[MCTS-EXPANSION] Starts");
log_info("[MCTS-EXPANSION] Record Queue Entry: %s", q->fname);
message = u32_array_to_str(response_codes, len_codes);
log_info("[MCTS-EXPANSION] State seq has %02u states: %s", len_codes, message);
free(message);
message = NULL;
message = u32_array_to_str(q->regions[q->region_count-1].state_sequence,
q->regions[q->region_count-1].state_count);
log_info("[MCTS-EXPANSION] Last reg has %02u states: %s",