-
-
Notifications
You must be signed in to change notification settings - Fork 311
/
monster.c
1965 lines (1506 loc) · 67.1 KB
/
monster.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
/*
Copyright (c) the Selfie Project authors. All rights reserved.
Please see the AUTHORS file for details. Use of this source code is
governed by a BSD license that can be found in the LICENSE file.
Selfie is a project of the Computational Systems Group at the
Department of Computer Sciences of the University of Salzburg
in Austria. For further information and code please refer to:
selfie.cs.uni-salzburg.at
Monster is a hybrid symbolic execution and bounded model checking
engine that implements a sound and (up to a given bound) complete
translation of RISC-U code to SMT-LIB formulae. Monster serves as
research platform and facilitates teaching the absolute basics of
bit-precise reasoning on real code.
Given a RISC-U binary (or C* source code compiled to RISC-U, including
all of selfie and monster itself) and bounds on the number of machine
instructions (maximum execution depth) and the number of conditional
branch instructions (branching limit) to be executed on any code path,
monster generates an SMT-LIB file that models the bit-precise behavior
of the binary up to the maximum execution depth and branching limit on
a 64-bit machine with 4GB of memory. The SMT formulae of the model are
satisfiable if and only if there is input to the code such that the
code exits with non-zero exit codes or performs division by zero when
executing no more instructions than the maximum execution depth and no
more conditional branch instructions than the branching limit.
The first console argument is interpreted as maximum execution depth
where value zero means that the depth is unbounded. The following
optional console argument is interpreted as non-default branching
limit where value zero means that any conditional branch instruction
makes the engine backtrack. The following optional console argument
--merge-enabled instructs monster to generate a single SMT-LIB
formula for bounded model checking by merging all code paths (rather
than one SMT-LIB formula for each code path as in symbolic execution).
Any remaining console arguments are uninterpreted and passed on as
console arguments to the modeled RISC-U binary.
Monster is inspired by Professor Armin Biere from JKU Linz.
*/
// *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~
// -----------------------------------------------------------------
// ------------------- I N T E R F A C E -------------------
// -----------------------------------------------------------------
// *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~
// -----------------------------------------------------------------
// ----------------------- MIPSTER SYSCALLS ------------------------
// -----------------------------------------------------------------
void implement_symbolic_exit(uint64_t* context);
void implement_symbolic_read(uint64_t* context);
void implement_symbolic_write(uint64_t* context);
uint64_t down_load_concrete_string(uint64_t* context, uint64_t vstring, char* s);
void implement_symbolic_openat(uint64_t* context);
// -----------------------------------------------------------------
// ------------------------- MONSTER SWITCH ------------------------
// -----------------------------------------------------------------
uint64_t* mipster_symbolic_switch(uint64_t* to_context, uint64_t timeout);
// *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~
// -----------------------------------------------------------------
// ----------------- A R C H I T E C T U R E -----------------
// -----------------------------------------------------------------
// *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~
// -----------------------------------------------------------------
// ------------------------ SYMBOLIC MEMORY ------------------------
// -----------------------------------------------------------------
uint64_t* load_symbolic_memory(uint64_t vaddr);
void store_symbolic_memory(uint64_t vaddr, uint64_t val, char* sym, char* var, uint64_t bits);
uint64_t* find_word_in_unshared_symbolic_memory(uint64_t vaddr);
void update_begin_of_shared_symbolic_memory(uint64_t* context, uint64_t* partner);
uint64_t is_symbolic_value(uint64_t* sword);
void print_symbolic_memory(uint64_t* sword);
// symbolic memory word struct:
// +---+-----------+
// | 0 | next word | pointer to next memory word
// | 1 | address | address of memory word
// | 2 | value | concrete value of memory word
// | 3 | symbolic | symbolic value of memory word
// | 4 | bits | number of bits in bit vector
// +---+-----------+
uint64_t* allocate_symbolic_memory_word() {
return smalloc(2 * sizeof(uint64_t*) + 3 * sizeof(uint64_t));
}
uint64_t* get_next_word(uint64_t* word) { return (uint64_t*) *word; }
uint64_t get_word_address(uint64_t* word) { return *(word + 1); }
uint64_t get_word_value(uint64_t* word) { return *(word + 2); }
char* get_word_symbolic(uint64_t* word) { return (char*) *(word + 3); }
uint64_t get_number_of_bits(uint64_t* word) { return *(word + 4); }
void set_next_word(uint64_t* word, uint64_t* next) { *word = (uint64_t) next; }
void set_word_address(uint64_t* word, uint64_t address) { *(word + 1) = address; }
void set_word_value(uint64_t* word, uint64_t value) { *(word + 2) = value; }
void set_word_symbolic(uint64_t* word, char* sym) { *(word + 3) = (uint64_t) sym; }
void set_number_of_bits(uint64_t* word, uint64_t bits) { *(word + 4) = bits; }
// -----------------------------------------------------------------
// ------------------------- INSTRUCTIONS --------------------------
// -----------------------------------------------------------------
void constrain_lui();
void constrain_addi();
void constrain_add_sub_mul_divu_remu_sltu(char* operator);
void zero_extend_sltu();
void constrain_load();
void constrain_store();
void constrain_beq();
void constrain_jalr();
// -----------------------------------------------------------------
// -------------------------- INTERPRETER --------------------------
// -----------------------------------------------------------------
void execute_symbolically();
void run_symbolically_until_exception();
// *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~
// -----------------------------------------------------------------
// ---------------------- R U N T I M E ----------------------
// -----------------------------------------------------------------
// *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~
// -----------------------------------------------------------------
// ---------------------------- CONTEXTS ---------------------------
// -----------------------------------------------------------------
uint64_t* new_symbolic_context();
uint64_t* copy_symbolic_context(uint64_t* original, uint64_t location, char* condition);
// symbolic context extension:
// +----+-----------------+
// | +0 | execution depth | number of executed instructions
// | +1 | path condition | pointer to path condition
// | +2 | symbolic memory | pointer to symbolic memory
// | +3 | symbolic regs | pointer to symbolic registers
// | +4 | beq counter | number of executed symbolic beq instructions
// | +5 | merge partner | pointer to the context from which this context was created
// | +6 | call stack | pointer to the corresponding node in the call stack tree
// +----+-----------------+
uint64_t* allocate_symbolic_context() {
return smalloc(CONTEXTENTRIES * sizeof(uint64_t) + 5 * sizeof(uint64_t*) + 2 * sizeof(uint64_t));
}
uint64_t get_execution_depth(uint64_t* context) { return *(context + CONTEXTENTRIES); }
char* get_path_condition(uint64_t* context) { return (char*) *(context + CONTEXTENTRIES + 1); }
uint64_t* get_symbolic_memory(uint64_t* context) { return (uint64_t*) *(context + CONTEXTENTRIES + 2); }
uint64_t* get_symbolic_regs(uint64_t* context) { return (uint64_t*) *(context + CONTEXTENTRIES + 3); }
uint64_t get_beq_counter(uint64_t* context) { return *(context + CONTEXTENTRIES + 4); }
uint64_t* get_merge_partner(uint64_t* context) { return (uint64_t*) *(context + CONTEXTENTRIES + 5); }
uint64_t* get_call_stack(uint64_t* context) { return (uint64_t*) *(context + CONTEXTENTRIES + 6); }
void set_execution_depth(uint64_t* context, uint64_t depth) { *(context + CONTEXTENTRIES) = depth; }
void set_path_condition(uint64_t* context, char* condition) { *(context + CONTEXTENTRIES + 1) = (uint64_t) condition; }
void set_symbolic_memory(uint64_t* context, uint64_t* memory) { *(context + CONTEXTENTRIES + 2) = (uint64_t) memory; }
void set_symbolic_regs(uint64_t* context, uint64_t* regs) { *(context + CONTEXTENTRIES + 3) = (uint64_t) regs; }
void set_beq_counter(uint64_t* context, uint64_t counter) { *(context + CONTEXTENTRIES + 4) = counter; }
void set_merge_partner(uint64_t* context, uint64_t* partner) { *(context + CONTEXTENTRIES + 5) = (uint64_t) partner; }
void set_call_stack(uint64_t* context, uint64_t* stack) { *(context + CONTEXTENTRIES + 6) = (uint64_t) stack; }
// -----------------------------------------------------------------
// -------------------------- MICROKERNEL --------------------------
// -----------------------------------------------------------------
uint64_t* create_symbolic_context(uint64_t* parent, uint64_t* vctxt);
// -----------------------------------------------------------------
// ---------------------------- KERNEL -----------------------------
// -----------------------------------------------------------------
uint64_t handle_symbolic_system_call(uint64_t* context);
uint64_t handle_symbolic_division_by_zero(uint64_t* context);
uint64_t handle_symbolic_timer(uint64_t* context);
uint64_t handle_symbolic_exception(uint64_t* context);
// ------------------------ GLOBAL CONSTANTS -----------------------
uint64_t EXITCODE_SYMBOLICEXECUTIONERROR = 1;
uint64_t SCHEDULE = 100; // extends DONOTEXIT and EXIT
// -----------------------------------------------------------------
// ------------------- SYMBOLIC EXECUTION ENGINE -------------------
// -----------------------------------------------------------------
char* bv_constant(uint64_t value);
char* bv_variable(uint64_t bits);
char* bv_zero_extension(uint64_t bits);
char* smt_value(uint64_t val, char* sym);
char* smt_variable(char* prefix, uint64_t bits);
char* smt_unary(char* opt, char* op);
char* smt_binary(char* opt, char* op1, char* op2);
char* smt_ternary(char* opt, char* op1, char* op2, char* op3);
void merge(uint64_t* active_context, uint64_t* mergeable_context, uint64_t location);
void merge_symbolic_memory_and_registers(uint64_t* active_context, uint64_t* mergeable_context);
void merge_symbolic_memory(uint64_t* active_context, uint64_t* mergeable_context);
void merge_registers(uint64_t* active_context, uint64_t* mergeable_context);
uint64_t* schedule_next_symbolic_context();
void check_if_mergeable_and_merge_if_possible(uint64_t* context);
void add_child(uint64_t* parent, uint64_t* child);
void step_into_call(uint64_t* context, uint64_t address);
void step_out_of_call(uint64_t* context);
void use_stdout();
void use_file();
void monster(uint64_t* to_context);
uint64_t selfie_run_symbolically();
// ------------------------ GLOBAL VARIABLES -----------------------
uint64_t w = 0; // number of written characters
uint64_t max_execution_depth = 1; // in number of instructions, unbounded with 0
uint64_t variable_version = 0; // generates unique SMT-LIB variable names
uint64_t* symbolic_contexts = (uint64_t*) 0;
char* path_condition = (char*) 0;
uint64_t* symbolic_memory = (uint64_t*) 0;
uint64_t* reg_sym = (uint64_t*) 0; // symbolic values in registers as strings in SMT-LIB format
char* smt_name = (char*) 0; // name of SMT-LIB file
uint64_t smt_fd = 0; // file descriptor of open SMT-LIB file
uint64_t merge_enabled = 0; // enable or disable the merging of paths
uint64_t debug_merge = 0; // enable or disable the debugging of merging in monster
uint64_t* call_stack_tree = (uint64_t*) 0; // tree representing the program structure (each node represents a procedure call)
// ------------------------ GLOBAL CONSTANTS -----------------------
uint64_t DELETED = -1; // indicates that a symbolic memory word has been deleted
uint64_t BEGIN_OF_SHARED_SYMBOLIC_MEMORY = -2; // indicates the beginning of the shared symbolic memory space
uint64_t beq_limit = 35; // limit of symbolic beq instructions on each path
// *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~
// -----------------------------------------------------------------
// ------------------- I N T E R F A C E -------------------
// -----------------------------------------------------------------
// *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~
// -----------------------------------------------------------------
// ----------------------- MIPSTER SYSCALLS ------------------------
// -----------------------------------------------------------------
void implement_symbolic_exit(uint64_t* context) {
// parameter;
uint64_t signed_int_exit_code;
signed_int_exit_code = *(get_regs(context) + REG_A0);
set_exit_code(context, sign_shrink(signed_int_exit_code, SYSCALL_BITWIDTH));
w = w
+ dprintf(output_fd, "\n(push 1)\n")
+ dprintf(output_fd, "(assert (and %s (not (= %s (_ bv0 64))))); exit in ",
path_condition,
smt_value(*(registers + REG_A0), (char*) *(reg_sym + REG_A0)))
+ print_code_context_for_instruction(pc);
if (debug_merge)
w = w + dprintf(output_fd, " -> exiting context: 0x%08lX", (uint64_t) context);
w = w + dprintf(output_fd, "\n(check-sat)\n(get-model)\n(pop 1)\n");
}
void implement_symbolic_read(uint64_t* context) {
// parameters
// fd not needed
uint64_t vbuffer;
uint64_t size;
// local variables
uint64_t read_total;
uint64_t bytes_to_read;
uint64_t failed;
vbuffer = *(get_regs(context) + REG_A1);
size = *(get_regs(context) + REG_A2);
read_total = 0;
bytes_to_read = WORDSIZE;
failed = 0;
while (size > 0) {
if (size < bytes_to_read)
bytes_to_read = size;
if (is_virtual_address_valid(vbuffer, WORDSIZE))
if (is_data_stack_heap_address(context, vbuffer))
if (is_virtual_address_mapped(get_pt(context), vbuffer)) {
store_symbolic_memory(vbuffer, 0, 0, smt_variable("i", bytes_to_read * 8), bytes_to_read * 8);
// save symbolic memory here since context switching has already happened
set_symbolic_memory(context, symbolic_memory);
read_total = read_total + bytes_to_read;
size = size - bytes_to_read;
if (size > 0)
vbuffer = vbuffer + WORDSIZE;
} else {
failed = 1;
size = 0;
use_stdout();
printf("%s: reading into virtual address 0x%08lX failed because the address is unmapped\n", selfie_name, vbuffer);
use_file();
}
else {
failed = 1;
size = 0;
use_stdout();
printf("%s: reading into virtual address 0x%08lX failed because the address is in an invalid segment\n", selfie_name, vbuffer);
use_file();
}
else {
failed = 1;
size = 0;
use_stdout();
printf("%s: reading into virtual address 0x%08lX failed because the address is invalid\n", selfie_name, vbuffer);
use_file();
}
}
if (failed)
*(get_regs(context) + REG_A0) = sign_shrink(-1, SYSCALL_BITWIDTH);
else
*(get_regs(context) + REG_A0) = read_total;
set_pc(context, get_pc(context) + INSTRUCTIONSIZE);
}
void implement_symbolic_write(uint64_t* context) {
// parameters
// fd not needed
uint64_t vbuffer;
uint64_t size;
// local variables
uint64_t written_total;
uint64_t bytes_to_write;
uint64_t failed;
vbuffer = *(get_regs(context) + REG_A1);
size = *(get_regs(context) + REG_A2);
written_total = 0;
bytes_to_write = WORDSIZE;
failed = 0;
while (size > 0) {
if (size < bytes_to_write)
bytes_to_write = size;
if (is_virtual_address_valid(vbuffer, WORDSIZE))
if (is_data_stack_heap_address(context, vbuffer))
if (is_virtual_address_mapped(get_pt(context), vbuffer)) {
// TODO: What should symbolically executed code actually output?
written_total = written_total + bytes_to_write;
size = size - bytes_to_write;
if (size > 0)
vbuffer = vbuffer + WORDSIZE;
} else {
failed = 1;
size = 0;
use_stdout();
printf("%s: writing from virtual address 0x%08lX failed because the address is unmapped\n", selfie_name, vbuffer);
use_file();
}
else {
failed = 1;
size = 0;
use_stdout();
printf("%s: writing from virtual address 0x%08lX failed because the address is in an invalid segment\n", selfie_name, vbuffer);
use_file();
}
else {
failed = 1;
size = 0;
use_stdout();
printf("%s: writing from virtual address 0x%08lX failed because the address is invalid\n", selfie_name, vbuffer);
use_file();
}
}
if (failed)
*(get_regs(context) + REG_A0) = sign_shrink(-1, SYSCALL_BITWIDTH);
else
*(get_regs(context) + REG_A0) = written_total;
set_pc(context, get_pc(context) + INSTRUCTIONSIZE);
}
uint64_t down_load_concrete_string(uint64_t* context, uint64_t vaddr, char* s) {
uint64_t i;
uint64_t* sword;
uint64_t j;
i = 0;
while (i < MAX_FILENAME_LENGTH) {
if (is_virtual_address_valid(vaddr, WORDSIZE))
if (is_data_stack_heap_address(context, vaddr)) {
if (is_virtual_address_mapped(get_pt(context), vaddr)) {
sword = load_symbolic_memory(vaddr);
if (sword) {
if (is_symbolic_value(sword)) {
use_stdout();
printf("%s: detected symbolic value ", selfie_name);
print_symbolic_memory(sword);
printf(" in filename of open call\n");
exit(EXITCODE_SYMBOLICEXECUTIONERROR);
} else
// CAUTION: at boot levels higher than zero, s is only accessible
// in C* at machine word granularity, not individual characters
store_word((uint64_t*) s, i, 1, get_word_value(sword));
} else
// assert: vaddr is mapped
store_word((uint64_t*) s, i, 1, load_virtual_memory(get_pt(context), vaddr));
} else {
use_stdout();
printf("%s: opening file failed because the file name address 0x%08lX is unmapped\n", selfie_name, vaddr);
use_file();
return 0;
}
// WORDSIZE may be less than sizeof(uint64_t)
j = i % sizeof(uint64_t);
// check if string ends in the current word
while (j - i % sizeof(uint64_t) < WORDSIZE) {
if (load_character((char*) ((uint64_t*) s + i / sizeof(uint64_t)), j) == 0)
return 1;
j = j + 1;
}
// advance to the next word in virtual memory
vaddr = vaddr + WORDSIZE;
// advance to the corresponding word in our memory
i = i + WORDSIZE;
} else {
use_stdout();
printf("%s: opening file failed because the file name address 0x%08lX is in an invalid segment\n", selfie_name, vaddr);
use_file();
return 0;
}
else {
use_stdout();
printf("%s: opening file failed because the file name address 0x%08lX is invalid\n", selfie_name, vaddr);
use_file();
return 0;
}
}
use_stdout();
printf("%s: opening file failed because the file name is too long at address 0x%08lX\n", selfie_name, vaddr);
use_file();
return 0;
}
void implement_symbolic_openat(uint64_t* context) {
// parameters
uint64_t vfilename;
// flags not needed
// mode not needed
vfilename = *(get_regs(context) + REG_A1);
if (down_load_concrete_string(context, vfilename, filename_buffer))
// TODO: check if opening vfilename has been attempted before
*(get_regs(context) + REG_A0) = 0; // file descriptor 0
else
*(get_regs(context) + REG_A0) = sign_shrink(-1, SYSCALL_BITWIDTH);
set_pc(context, get_pc(context) + INSTRUCTIONSIZE);
}
// -----------------------------------------------------------------
// ------------------------- MONSTER SWITCH ------------------------
// -----------------------------------------------------------------
uint64_t* mipster_symbolic_switch(uint64_t* to_context, uint64_t timeout) {
uint64_t execution_depth;
path_condition = get_path_condition(to_context);
reg_sym = get_symbolic_regs(to_context);
symbolic_memory = get_symbolic_memory(to_context);
restore_context(to_context);
do_switch(to_context, timeout);
execution_depth = get_total_number_of_instructions();
run_symbolically_until_exception();
execution_depth = get_total_number_of_instructions() - execution_depth;
save_context(current_context);
set_execution_depth(current_context, get_execution_depth(current_context) + execution_depth);
set_path_condition(current_context, path_condition);
set_symbolic_memory(current_context, symbolic_memory);
return current_context;
}
// *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~
// -----------------------------------------------------------------
// ----------------- A R C H I T E C T U R E -----------------
// -----------------------------------------------------------------
// *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~
// -----------------------------------------------------------------
// ------------------------ SYMBOLIC MEMORY ------------------------
// -----------------------------------------------------------------
uint64_t* load_symbolic_memory(uint64_t vaddr) {
uint64_t* sword;
sword = symbolic_memory;
while (sword != (uint64_t*) 0) {
if (get_word_address(sword) == vaddr)
return sword;
sword = get_next_word(sword);
}
return (uint64_t*) 0;
}
void store_symbolic_memory(uint64_t vaddr, uint64_t val, char* sym, char* var, uint64_t bits) {
uint64_t* sword;
// we overwrite values, if they already exist in the unshared symbolic memory space, so that there are no duplicates in any unshared symbolic memory space
sword = find_word_in_unshared_symbolic_memory(vaddr);
// new value in this unshared symbolic memory space
if (sword == (uint64_t*) 0) {
sword = allocate_symbolic_memory_word();
set_next_word(sword, symbolic_memory);
symbolic_memory = sword;
}
set_word_address(sword, vaddr);
set_word_value(sword, val);
if (var)
set_word_symbolic(sword, var);
else if (sym) {
set_word_symbolic(sword, smt_variable("m", WORDSIZEINBITS));
w = w
+ dprintf(output_fd, "(assert (= %s %s)); store in ", get_word_symbolic(sword), sym)
+ print_code_context_for_instruction(pc)
+ dprintf(output_fd, "\n");
} else
set_word_symbolic(sword, 0);
set_number_of_bits(sword, bits);
}
uint64_t* find_word_in_unshared_symbolic_memory(uint64_t vaddr) {
uint64_t* sword;
sword = get_symbolic_memory(current_context);
while (sword) {
if (get_word_address(sword) == BEGIN_OF_SHARED_SYMBOLIC_MEMORY)
return (uint64_t*) 0;
if (get_word_address(sword) == vaddr)
return sword;
sword = get_next_word(sword);
}
return (uint64_t*) 0;
}
void update_begin_of_shared_symbolic_memory(uint64_t* context, uint64_t* partner) {
uint64_t* sword_of_shared_store;
uint64_t* sword;
if (context == (uint64_t*) 0)
return;
sword_of_shared_store = (uint64_t*) 0;
sword = get_symbolic_memory(partner);
while (sword) {
if (get_word_address(sword) == BEGIN_OF_SHARED_SYMBOLIC_MEMORY) {
// remember beginning of shared symbolic memory portion in partner context
sword_of_shared_store = get_next_word(sword);
sword = (uint64_t*) 0;
} else
sword = get_next_word(sword);
}
sword = get_symbolic_memory(context);
while (sword) {
if (get_word_address(sword) == BEGIN_OF_SHARED_SYMBOLIC_MEMORY) {
// only unshare symbolic memory if both contexts point to the same shared portion
if (get_next_word(sword) == sword_of_shared_store)
set_word_address(sword, DELETED);
else if (debug_merge)
w = w + dprintf(output_fd, "; unbalanced shared symbolic memory detected, skip unsharing\n");
return;
}
sword = get_next_word(sword);
}
}
uint64_t is_symbolic_value(uint64_t* sword) {
return get_word_symbolic(sword) != (char*) 0;
}
void print_symbolic_memory(uint64_t* sword) {
if (is_symbolic_value(sword))
w = w + dprintf(output_fd, "%s", get_word_symbolic(sword));
w = w + dprintf(output_fd, "[0x%lX]@0x%lX\n", get_word_value(sword), get_word_address(sword));
}
// -----------------------------------------------------------------
// ------------------------- INSTRUCTIONS --------------------------
// -----------------------------------------------------------------
void constrain_lui() {
if (rd != REG_ZR)
*(reg_sym + rd) = 0;
}
void constrain_addi() {
if (rd != REG_ZR) {
if (*(reg_sym + rs1))
*(reg_sym + rd) = (uint64_t) smt_binary("bvadd", (char*) *(reg_sym + rs1), bv_constant(imm));
else
*(reg_sym + rd) = 0;
}
}
void constrain_add_sub_mul_divu_remu_sltu(char* operator) {
char* op1;
char* op2;
if (rd != REG_ZR) {
op1 = (char*) *(reg_sym + rs1);
op2 = (char*) *(reg_sym + rs2);
if (op1 == (char*) 0) {
if (op2 == (char*) 0) {
*(reg_sym + rd) = 0;
return;
} else
op1 = bv_constant(*(registers + rs1));
} else if (op2 == (char*) 0)
op2 = bv_constant(*(registers + rs2));
*(reg_sym + rd) = (uint64_t) smt_binary(operator, op1, op2);
// checking for division by zero
if (string_compare(operator, "bvudiv"))
w = w
+ dprintf(output_fd, "(push 1)\n")
+ dprintf(output_fd, "(assert (and %s %s)); check if a division by zero is possible", path_condition, smt_binary("=", op2, bv_constant(0)))
+ dprintf(output_fd, "\n(check-sat)\n(get-model)\n(pop 1)\n");
}
}
void zero_extend_sltu() {
if (rd != REG_ZR)
if (*(reg_sym + rd))
*(reg_sym + rd) = (uint64_t) smt_unary(bv_zero_extension(1), (char*) *(reg_sym + rd));
}
void constrain_load() {
uint64_t vaddr;
uint64_t* sword;
uint64_t a;
// load double word
if (*(reg_sym + rs1)) {
use_stdout();
// symbolic memory addresses not yet supported
printf("%s: symbolic memory address in load instruction at 0x%lX", selfie_name, pc);
print_code_line_number_for_instruction(pc, code_start);
printf("\n");
exit(EXITCODE_SYMBOLICEXECUTIONERROR);
}
read_register(rs1);
vaddr = *(registers + rs1) + imm;
if (is_virtual_address_valid(vaddr, WORDSIZE)) {
if (is_valid_segment_read(vaddr)) {
if (is_virtual_address_mapped(pt, vaddr)) {
// semantics of load double word
if (rd != REG_ZR) {
sword = load_symbolic_memory(vaddr);
if (sword) {
*(registers + rd) = get_word_value(sword);
if (get_number_of_bits(sword) < WORDSIZEINBITS)
*(reg_sym + rd) = (uint64_t) smt_unary(bv_zero_extension(get_number_of_bits(sword)), get_word_symbolic(sword));
else
*(reg_sym + rd) = (uint64_t) get_word_symbolic(sword);
} else {
*(registers + rd) = load_virtual_memory(pt, vaddr);
*(reg_sym + rd) = 0;
}
}
write_register(rd);
// keep track of instruction address for profiling loads
a = (pc - code_start) / INSTRUCTIONSIZE;
pc = pc + INSTRUCTIONSIZE;
// keep track of number of loads in total
ic_load = ic_load + 1;
// and individually
*(loads_per_instruction + a) = *(loads_per_instruction + a) + 1;
} else
throw_exception(EXCEPTION_PAGEFAULT, page_of_virtual_address(vaddr));
} else
throw_exception(EXCEPTION_SEGMENTATIONFAULT, vaddr);
} else
// invalid concrete memory address
throw_exception(EXCEPTION_INVALIDADDRESS, vaddr);
}
void constrain_store() {
uint64_t vaddr;
uint64_t a;
// store double word
if (*(reg_sym + rs1)) {
use_stdout();
// symbolic memory addresses not yet supported
printf("%s: symbolic memory address in sd instruction at 0x%lX", selfie_name, pc);
print_code_line_number_for_instruction(pc, code_start);
printf("\n");
exit(EXITCODE_SYMBOLICEXECUTIONERROR);
}
read_register(rs1);
vaddr = *(registers + rs1) + imm;
if (is_virtual_address_valid(vaddr, WORDSIZE)) {
if (is_valid_segment_write(vaddr)) {
if (is_virtual_address_mapped(pt, vaddr)) {
// semantics of store double word
store_symbolic_memory(vaddr,
*(registers + rs2),
(char*) *(reg_sym + rs2),
0,
WORDSIZEINBITS);
// keep track of instruction address for profiling stores
a = (pc - code_start) / INSTRUCTIONSIZE;
pc = pc + INSTRUCTIONSIZE;
// keep track of number of stores in total
ic_store = ic_store + 1;
// and individually
*(stores_per_instruction + a) = *(stores_per_instruction + a) + 1;
} else
throw_exception(EXCEPTION_PAGEFAULT, page_of_virtual_address(vaddr));
} else
throw_exception(EXCEPTION_SEGMENTATIONFAULT, vaddr);
} else
// invalid concrete memory address
throw_exception(EXCEPTION_INVALIDADDRESS, vaddr);
}
void constrain_beq() {
char* op1;
char* op2;
char* bvar;
char* pvar;
op1 = (char*) *(reg_sym + rs1);
op2 = (char*) *(reg_sym + rs2);
if (op1 == (char*) 0) {
if (op2 == (char*) 0) {
do_beq();
return;
} else
op1 = bv_constant(*(registers + rs1));
} else if (op2 == (char*) 0)
op2 = bv_constant(*(registers + rs2));
bvar = smt_variable("b", 1);
w = w
+ dprintf(output_fd, "(assert (= %s %s)); beq in ", bvar, smt_binary("bvcomp", op1, op2))
+ print_code_context_for_instruction(pc)
+ dprintf(output_fd, "\n");
pvar = smt_variable("p", 1);
w = w
+ dprintf(output_fd, "(assert (= %s %s)); path condition in ", pvar, path_condition)
+ print_code_context_for_instruction(pc)
+ dprintf(output_fd, "\n");
// increase the number of executed symbolic beq instructions
set_beq_counter(current_context, get_beq_counter(current_context) + 1);
if (get_beq_counter(current_context) < beq_limit) {
// save symbolic memory so that it is copied correctly afterwards
set_symbolic_memory(current_context, symbolic_memory);
// the copied context is executed later and takes the other path
copy_symbolic_context(current_context, pc + imm, smt_binary("and", pvar, bvar));
path_condition = smt_binary("and", pvar, smt_unary("not", bvar));
pc = pc + INSTRUCTIONSIZE;
}
}
void constrain_jalr() {
if (*(reg_sym + rs1)) {
use_stdout();
// symbolic memory addresses not yet supported
printf("%s: symbolic memory address in jalr instruction at 0x%lX", selfie_name, pc);
print_code_line_number_for_instruction(pc, code_start);
printf("\n");
exit(EXITCODE_SYMBOLICEXECUTIONERROR);
}
}
// -----------------------------------------------------------------
// -------------------------- INTERPRETER --------------------------
// -----------------------------------------------------------------
void execute_symbolically() {
// assert: 1 <= is <= number of RISC-U instructions
if (is == ADDI) {
constrain_addi();
do_addi();
} else if (is == LOAD)
constrain_load();
else if (is == STORE)
constrain_store();
else if (is == ADD) {
constrain_add_sub_mul_divu_remu_sltu("bvadd");
do_add();
} else if (is == SUB) {
constrain_add_sub_mul_divu_remu_sltu("bvsub");
do_sub();
} else if (is == MUL) {
constrain_add_sub_mul_divu_remu_sltu("bvmul");
do_mul();
} else if (is == DIVU) {
constrain_add_sub_mul_divu_remu_sltu("bvudiv");
do_divu();
} else if (is == REMU) {
constrain_add_sub_mul_divu_remu_sltu("bvurem");
do_remu();
} else if (is == SLTU) {
constrain_add_sub_mul_divu_remu_sltu("bvult");
zero_extend_sltu();
do_sltu();
} else if (is == BEQ)
constrain_beq();
else if (is == JAL) {
// the JAL instruction is a procedure call, if rd is REG_RA
if (rd == REG_RA)
// push the procedure at pc + imm onto the callstack of the current context
step_into_call(current_context, pc + imm);
do_jal();
} else if (is == JALR) {
// pop off call stack, when we return from a procedure
if (rd == REG_ZR)
if (rs1 == REG_RA)
if (imm == 0)
step_out_of_call(current_context);
constrain_jalr();
do_jalr();
} else if (is == LUI) {
constrain_lui();
do_lui();
} else if (is == ECALL)
do_ecall();
}
void run_symbolically_until_exception() {
trap = 0;
while (trap == 0) {
fetch();
decode();
execute_symbolically();
interrupt();
}
trap = 0;
}
// *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~
// -----------------------------------------------------------------
// ---------------------- R U N T I M E ----------------------
// -----------------------------------------------------------------
// *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~
// -----------------------------------------------------------------
// ---------------------------- CONTEXTS ---------------------------
// -----------------------------------------------------------------
uint64_t* new_symbolic_context() {
// insert new symbolic context at top of free-list of contexts
free_context(allocate_symbolic_context());