-
Notifications
You must be signed in to change notification settings - Fork 77
/
vmir.c
1127 lines (910 loc) · 24 KB
/
vmir.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) 2016 Lonelycoder AB
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#define _GNU_SOURCE
#include <setjmp.h>
#include <string.h>
#include <stdarg.h>
#include <inttypes.h>
#include <sys/queue.h>
#include <assert.h>
#include <sys/param.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include "bitcode.h"
#include <time.h>
#include <sys/time.h>
#include "vmir.h"
// #define VM_TRACE
#if defined(__SANITIZE_ADDRESS__) && !defined(VM_DONT_USE_COMPUTED_GOTO)
#define VM_DONT_USE_COMPUTED_GOTO
#endif
#if defined(VM_TRACE) && !defined(VM_DONT_USE_COMPUTED_GOTO)
#define VM_DONT_USE_COMPUTED_GOTO
#endif
#if defined(VM_TRACE)
#undef VM_NO_STACK_FRAME
#endif
#ifndef VM_NO_STACK_FRAME
static void vmir_traceback(struct ir_unit *iu, const char *info);
#endif
#ifdef VM_TRACE
static void vmir_access_violation(struct ir_unit *iu, const void *p,
const char *func);
static void vmir_access_trap(struct ir_unit *iu, const void *p,
const char *func);
#endif
#include "vmir_support.c"
#include "vmir_bitstream.c"
TAILQ_HEAD(ir_abbrev_queue, ir_abbrev);
LIST_HEAD(ir_blockinfo_list, ir_blockinfo);
LIST_HEAD(ir_attr_list, ir_attr);
/**
*
*/
typedef struct ir_blockinfo {
LIST_ENTRY(ir_blockinfo) ib_link;
int ib_id;
int ib_num_abbrevs;
struct ir_abbrev_queue ib_abbrevs;
} ir_blockinfo_t;
typedef struct ir_block {
LIST_ENTRY(ir_block) ib_link;
struct ir_abbrev_queue ib_scoped_abbrevs;
ir_blockinfo_t *ib_blockinfo;
} ir_block_t;
LIST_HEAD(ir_block_list, ir_block);
/**
*
*/
typedef struct ir_valuetype {
int value;
int type;
} ir_valuetype_t;
struct ir_unit;
typedef void (rec_handler_t)(struct ir_unit *iu, int op,
unsigned int argc, const int64_t *argv);
VECTOR_HEAD(ir_op_vector, struct ir_op);
VECTOR_HEAD(ir_attrset_vector, struct ir_attrset);
VECTOR_HEAD(ir_type_vector, struct ir_type);
VECTOR_HEAD(ir_value_vector, struct ir_value *);
VECTOR_HEAD(ir_instrumentation_vector, struct ir_instrumentation);
TAILQ_HEAD(ir_bb_queue, ir_bb);
TAILQ_HEAD(ir_function_queue, ir_function);
TAILQ_HEAD(ir_instr_queue, ir_instr);
LIST_HEAD(ir_bb_list, ir_bb);
LIST_HEAD(ir_bb_edge_list, ir_bb_edge);
LIST_HEAD(ir_value_instr_list, ir_value_instr);
typedef struct vmir_exception {
uint32_t exception;
uint32_t type_info;
uint32_t uncaught;
uint32_t caught;
} vmir_exception_t;
/**
* Translation unit
*/
typedef int (vm_function_t)(void *ret,
const void *regs,
struct ir_unit *iu,
void *hostmem);
struct ir_unit {
vmir_function_resolver_t iu_external_function_resolver;
void *iu_mem_low;
void *iu_mem_high;
void *iu_data_breakpoint;
void *iu_mem;
void **iu_vm_funcs;
vm_function_t **iu_function_table;
jmp_buf *iu_err_jmpbuf;
int iu_exit_code;
void *iu_opaque;
void *iu_jit_mem;
int iu_jit_mem_alloced;
int iu_jit_ptr;
uint32_t iu_jit_cpuflags;
enum {
VMIR_BITCODE,
VMIR_WASM,
} iu_mode;
vmir_exception_t iu_exception;
const struct vm_frame *iu_current_frame;
char *iu_traced_function;
uint32_t iu_data_ptr;
uint32_t iu_heap_start;
void *iu_heap;
uint32_t iu_heap_usage;
uint32_t iu_rsize;
uint32_t iu_asize;
uint32_t iu_memsize;
uint32_t iu_stack_stash;
struct vFILE *iu_stdin;
struct vFILE *iu_stdout;
struct vFILE *iu_stderr;
const vmir_fsops_t *iu_fsops;
VECTOR_HEAD(, struct vmir_fd) iu_vfds;
int iu_vfd_free; // Point to first free FD (-1 == nothing free)
LIST_HEAD(, vFILE) iu_vfiles;
char *iu_strtok_tmp;
uint32_t iu_debug_flags;
uint32_t iu_debug_flags_func;
char *iu_debugged_function;
struct ir_instrumentation_vector iu_instrumentation;
char *iu_triple;
int iu_version;
struct ir_blockinfo_list iu_blockinfos;
struct ir_block_list iu_blocks;
struct ir_attr_list iu_attribute_groups;
struct ir_attrset_vector iu_attrsets;
struct ir_type_vector iu_types;
struct ir_value_vector iu_values;
int iu_next_value;
int iu_first_func_value;
char *iu_current_struct_name;
struct ir_function_queue iu_functions_with_bodies;
int iu_current_type;
struct ir_function *iu_current_function;
struct ir_bb *iu_current_bb;
#define IU_MAX_TMP_STR 32
char *iu_tmp_str[IU_MAX_TMP_STR];
int iu_tmp_str_ptr;
VECTOR_HEAD(, struct ir_function *) iu_functions;
VECTOR_HEAD(, struct ir_initializer) iu_initializers;
VECTOR_HEAD(, int) iu_branch_fixups;
VECTOR_HEAD(, int) iu_jit_vmbb_fixups;
VECTOR_HEAD(, int) iu_jit_branch_fixups;
VECTOR_HEAD(, int) iu_jit_bb_to_addr_fixups;
int iu_types_created;
// Parser
jmp_buf iu_parser_jmp;
void *iu_text_alloc;
void *iu_text_ptr;
size_t iu_text_alloc_memsize;
VECTOR_HEAD(, int64_t) iu_argv;
char iu_err_buf[256];
const char *iu_err_file;
int iu_err_line;
int iu_failed;
int iu_vstoffset;
// WASM
VECTOR_HEAD(, int) iu_wasm_type_map;
VECTOR_HEAD(, ir_valuetype_t) iu_wasm_globalvar_map;
VECTOR_HEAD(, int) iu_wasm_functions;
VECTOR_HEAD(, ir_valuetype_t) iu_wasm_value_stack;
VECTOR_HEAD(, struct ir_bb *) iu_wasm_cfg_stack;
// Stats
vmir_stats_t iu_stats;
vmir_logger_t *iu_logger;
vmir_log_level_t iu_log_level;
};
static uint32_t
vmir_host_to_vmaddr(ir_unit_t *iu, void *ptr)
{
if(ptr == NULL)
return 0;
return ptr - iu->iu_mem;
}
/**
* Attribute
*/
typedef struct ir_attr {
LIST_ENTRY(ir_attr) ia_link;
uint32_t ia_group_id;
uint32_t ia_index;
uint64_t ia_flags;
} ir_attr_t;
/**
* Attribute Set
*/
typedef struct ir_attrset {
int ias_size;
const ir_attr_t **ias_list;
} ir_attrset_t;
/**
*
*/
struct ir_function {
TAILQ_ENTRY(ir_function) if_body_link;
unsigned int if_type;
char *if_name;
char if_isproto;
char if_used;
char if_full_jit;
int if_regframe_size; // Size of all temporary registers
int if_callarg_size; // Size of all (non vararg) arguments
int if_gfid; // Global function id
int if_vmop; // Set if function maps directly to a VM opcode
int if_vmop_args; // Number of args the vmop expects
int if_num_bbs;
struct ir_bb_queue if_bbs;
struct ir_bb_edge_list if_edges;
void *if_vm_text;
int if_vm_text_size;
vm_function_t *if_ext_func;
struct ir_instr_backref *if_instr_backrefs;
int if_instr_backref_size;
int if_jit_offset;
#ifndef VM_NO_STACK_FRAME
int if_peak_stack_use;
#endif
};
/**
*
*/
typedef struct ir_globalvar {
TAILQ_ENTRY(ir_globalvar) ig_unit_link;
unsigned int ig_type;
char *ig_name;
uint32_t ig_addr;
uint32_t ig_size;
} ir_globalvar_t;
/**
*
*/
typedef struct ir_initializer {
int ii_globalvar;
int ii_constant;
} ir_initializer_t;
/**
*
*/
typedef struct ir_bb {
TAILQ_ENTRY(ir_bb) ib_link;
struct ir_instr_queue ib_instrs;
int ib_text_offset;
int ib_jit_offset;
int ib_id;
uint8_t ib_mark;
uint8_t ib_jit;
uint8_t ib_only_jit_sucessors;
uint8_t ib_force_jit_entrypoint;
struct ir_bb_edge_list ib_incoming_edges;
struct ir_bb_edge_list ib_outgoing_edges;
LIST_ENTRY(ir_bb) ib_traversal_link; // Temporary for graph traversal
char *ib_name;
} ir_bb_t;
/**
*
*/
typedef struct ir_bb_edge {
LIST_ENTRY(ir_bb_edge) ibe_from_link;
LIST_ENTRY(ir_bb_edge) ibe_to_link;
LIST_ENTRY(ir_bb_edge) ibe_function_link;
ir_bb_t *ibe_from;
ir_bb_t *ibe_to;
} ir_bb_edge_t;
/**
*
*/
typedef struct ir_instrumentation {
struct ir_function *ii_func;
int ii_bb;
int ii_instructions;
int64_t ii_count;
} ir_instrumentation_t;
/**
*
*/
typedef enum {
// Instructions defined in bitcode
IR_IC_UNREACHABLE,
IR_IC_RET,
IR_IC_BINOP,
IR_IC_CAST,
IR_IC_LOAD,
IR_IC_STORE,
IR_IC_GEP,
IR_IC_CMP2,
IR_IC_BR,
IR_IC_PHI,
IR_IC_CALL,
IR_IC_INVOKE, // http://llvm.org/docs/LangRef.html#invoke-instruction
IR_IC_SWITCH,
IR_IC_ALLOCA,
IR_IC_SELECT,
IR_IC_VAARG,
IR_IC_EXTRACTVAL,
IR_IC_INSERTVAL, // http://llvm.org/docs/LangRef.html#insertvalue-instruction
IR_IC_LANDINGPAD, // http://llvm.org/docs/LangRef.html#landingpad-instruction
IR_IC_RESUME, // http://llvm.org/docs/LangRef.html#resume-instruction
// VMIR special instructions
IR_IC_LEA,
IR_IC_MOVE,
IR_IC_VMOP,
IR_IC_STACKCOPY,
IR_IC_STACKSHRINK,
IR_IC_CMP_BRANCH,
IR_IC_CMP_SELECT,
IR_IC_MLA,
} instr_class_t;
/**
*
*/
typedef struct ir_instr {
TAILQ_ENTRY(ir_instr) ii_link;
struct ir_bb *ii_bb;
ir_valuetype_t *ii_rets;
struct ir_value_instr_list ii_values;
instr_class_t ii_class;
ir_valuetype_t ii_ret;
uint32_t *ii_liveness; /* Points to three consecutive bitfields used for
* during liveness analysis.
* [out] [gen] [in]
* The size of these bitfields are given by the number
* of temporaries in each function
*/
struct ir_bb **ii_succ;
SLIST_ENTRY(ir_instr) ii_tmplink;
int16_t ii_num_succ;
uint8_t ii_jit;
} ir_instr_t;
static void
vmir_log(ir_unit_t *iu, vmir_log_level_t level, const char *fmt, ...)
{
if(level > iu->iu_log_level)
return;
va_list ap;
char tmp[1024];
va_start(ap, fmt);
vsnprintf(tmp, sizeof(tmp), fmt, ap);
va_end(ap);
if(iu->iu_logger) {
iu->iu_logger(iu, level, tmp);
} else {
printf("%s\n", tmp);
}
}
void
vmir_set_logger(ir_unit_t *iu, vmir_logger_t *logger)
{
iu->iu_logger = logger;
}
void
vmir_set_log_level(ir_unit_t *iu, vmir_log_level_t level)
{
iu->iu_log_level = level;
}
static void type_print_list(ir_unit_t *iu);
static void value_print_list(ir_unit_t *iu);
static void function_print(ir_unit_t *iu, ir_function_t *f, const char *what);
#define parser_error(iu, fmt...) \
parser_error0(iu, __FILE__, __LINE__, fmt)
/**
*
*/
static void __attribute__((noreturn))
parser_error0(ir_unit_t *iu, const char *file, int line,
const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vsnprintf(iu->iu_err_buf, sizeof(iu->iu_err_buf), fmt, ap);
va_end(ap);
iu->iu_err_file = file;
iu->iu_err_line = line;
vmir_log(iu, VMIR_LOG_ERROR, "Parser error: %s:%d : %s",
file, line, iu->iu_err_buf);
if(iu->iu_failed) {
printf("Double failure\n");
} else {
iu->iu_failed = 1;
}
longjmp(iu->iu_parser_jmp, 1);
}
/**
*
*/
static char * __attribute__((warn_unused_result))
tmpstr(ir_unit_t *iu, int len)
{
int idx = (iu->iu_tmp_str_ptr++) & (IU_MAX_TMP_STR - 1);
free(iu->iu_tmp_str[idx]);
iu->iu_tmp_str[idx] = malloc(len + 1);
iu->iu_tmp_str[idx][len] = 0;
return iu->iu_tmp_str[idx];
}
/**
*
*/
static int __attribute__((warn_unused_result))
addstr(char **dst, const char *str)
{
int len = strlen(str);
if(dst != NULL) {
memcpy(*dst, str, len);
*dst += len;
}
return len;
}
/**
*
*/
static int __attribute__((warn_unused_result))
addstrf(char **dst, const char *fmt, ...)
{
int len = 0;
va_list ap;
va_start(ap, fmt);
if(dst == NULL) {
len = vsnprintf(NULL, 0, fmt, ap);
} else {
len = vsprintf(*dst, fmt, ap);
*dst += len;
}
va_end(ap);
return len;
}
#include "vmir_mem.c"
#include "vmir_type.c"
#include "vmir_value.c"
#include "vmir_vm.h"
#include "vmir_instr.c"
#include "vmir_function.c"
#if defined(__arm__) && (defined(__linux__) || defined(__ANDROID__))
#include "vmir_jit_arm.c"
#endif
#include "vmir_transform.c"
#include "vmir_vm.c"
#include "vmir_libc.c"
#include "vmir_bitcode_instr.c"
#include "vmir_bitcode_parser.c"
#include "vmir_wasm_parser.c"
/**
*
*/
static void
initialize_global(ir_unit_t *iu, void *addr,
int dstty_index, const ir_value_t *c)
{
int x, size;
ir_valuetype_t *ivt;
const ir_type_t *dstty = type_get(iu, dstty_index);
switch(dstty->it_code) {
case IR_TYPE_INT1:
case IR_TYPE_INT8:
mem_wr8(addr, value_get_const32(iu, c), iu);
break;
case IR_TYPE_INT16:
mem_wr16(addr, value_get_const32(iu, c), iu);
break;
case IR_TYPE_INT32:
case IR_TYPE_FLOAT:
mem_wr32(addr, value_get_const32(iu, c), iu);
break;
case IR_TYPE_INT64:
case IR_TYPE_DOUBLE:
mem_wr64(addr, value_get_const64(iu, c), iu);
break;
case IR_TYPE_POINTER:
switch(c->iv_class) {
case IR_VC_GLOBALVAR:
case IR_VC_CONSTANT:
mem_wr32(addr, value_get_const32(iu, c), iu);
break;
case IR_VC_FUNCTION:
mem_wr32(addr, value_function_addr(c), iu);
break;
default:
parser_error(iu, "Unable to initialize pointer from value class %d",
c->iv_class);
}
break;
case IR_TYPE_ARRAY:
size = type_sizeof(iu, dstty_index);
switch(c->iv_class) {
case IR_VC_DATA:
memcpy(addr, c->iv_data, size);
break;
case IR_VC_ZERO_INITIALIZER:
memset(addr, 0, size);
break;
case IR_VC_AGGREGATE:
// Iterate over all elements
x = dstty->it_array.num_elements;
assert(c->iv_num_values == x);
ivt = c->iv_data;
for(int i = 0; i < x; i++) {
ir_value_t *subvalue = value_get(iu, ivt[i].value);
initialize_global(iu, addr, dstty->it_array.element_type, subvalue);
addr += type_sizeof(iu, dstty->it_array.element_type);
}
break;
default:
parser_error(iu, "Unable to initialize array from value class %d",
c->iv_class);
}
break;
case IR_TYPE_STRUCT:
size = type_sizeof(iu, dstty_index);
switch(c->iv_class) {
case IR_VC_ZERO_INITIALIZER:
memset(addr, 0, size);
break;
case IR_VC_AGGREGATE:
// Iterate over all elements
x = dstty->it_struct.num_elements;
assert(c->iv_num_values == x);
ivt = c->iv_data;
for(int i = 0; i < x; i++) {
ir_value_t *subvalue = value_get(iu, ivt[i].value);
initialize_global(iu, addr + dstty->it_struct.elements[i].offset,
dstty->it_struct.elements[i].type, subvalue);
}
break;
default:
parser_error(iu, "Unable to initialize struct from value class %d",
c->iv_class);
}
break;
default:
parser_error(iu, "Unable to initialize global 0x%x (%s) from %s",
addr, type_str(iu, dstty),
type_str_index(iu, c->iv_type));
}
}
/**
*
*/
static void
initialize_globals(ir_unit_t *iu, void *mem)
{
for(int i = 0; i < VECTOR_LEN(&iu->iu_initializers); i++) {
ir_initializer_t *ii = &VECTOR_ITEM(&iu->iu_initializers, i);
const ir_value_t *gv = value_get(iu, ii->ii_globalvar);
const ir_value_t *c = value_get(iu, ii->ii_constant);
const ir_globalvar_t *ig = gv->iv_gvar;
#if 0
printf("Initializing global @ 0x%08x + 0x%08x '%s' with '%s'\n",
ig->ig_addr, ig->ig_size,
value_str_id(iu, ii->ii_globalvar),
value_str_id(iu, ii->ii_constant));
#endif
initialize_global(iu, mem + ig->ig_addr, ig->ig_type, c);
}
}
/**
*
*/
static void
iu_cleanup(ir_unit_t *iu)
{
value_resize(iu, 0);
VECTOR_CLEAR(&iu->iu_argv);
VECTOR_CLEAR(&iu->iu_branch_fixups);
VECTOR_CLEAR(&iu->iu_jit_vmbb_fixups);
VECTOR_CLEAR(&iu->iu_jit_branch_fixups);
VECTOR_CLEAR(&iu->iu_jit_bb_to_addr_fixups);
VECTOR_CLEAR(&iu->iu_initializers);
VECTOR_CLEAR(&iu->iu_values);
VECTOR_CLEAR(&iu->iu_wasm_type_map);
VECTOR_CLEAR(&iu->iu_wasm_globalvar_map);
VECTOR_CLEAR(&iu->iu_wasm_functions);
VECTOR_CLEAR(&iu->iu_wasm_value_stack);
VECTOR_CLEAR(&iu->iu_wasm_cfg_stack);
ir_attr_t *ia;
while((ia = LIST_FIRST(&iu->iu_attribute_groups)) != NULL) {
LIST_REMOVE(ia, ia_link);
free(ia);
}
for(int i = 0; i < VECTOR_LEN(&iu->iu_attrsets); i++) {
ir_attrset_t *ias = &VECTOR_ITEM(&iu->iu_attrsets, i);
free(ias->ias_list);
}
VECTOR_CLEAR(&iu->iu_attrsets);
iu->iu_current_bb = NULL;
iu->iu_current_function = NULL;
for(int i = 0; i < IU_MAX_TMP_STR; i++) {
free(iu->iu_tmp_str[i]);
iu->iu_tmp_str[i] = NULL;
}
ir_blockinfo_t *ib;
while((ib = LIST_FIRST(&iu->iu_blockinfos)) != NULL)
blockinfo_destroy(ib);
}
/**
*
*/
void
vmir_destroy(ir_unit_t *iu)
{
libc_terminate(iu);
for(int i = 0; i < VECTOR_LEN(&iu->iu_functions); i++) {
function_destroy(VECTOR_ITEM(&iu->iu_functions, i));
}
free(iu->iu_vm_funcs);
free(iu->iu_function_table);
VECTOR_CLEAR(&iu->iu_functions);
for(int i = 0; i < VECTOR_LEN(&iu->iu_types); i++) {
ir_type_t *it = &VECTOR_ITEM(&iu->iu_types, i);
type_clean(it);
}
VECTOR_CLEAR(&iu->iu_types);
free(iu->iu_triple);
free(iu->iu_debugged_function);
VECTOR_CLEAR(&iu->iu_vfds);
free(iu);
}
/**
*
*/
ir_unit_t *
vmir_create(void *membase, uint32_t memsize,
uint32_t rsize, uint32_t asize,
void *opaque)
{
ir_unit_t *iu = calloc(1, sizeof(ir_unit_t));
iu->iu_log_level = VMIR_LOG_INFO;
iu->iu_external_function_resolver = vmir_default_external_function_resolver;
iu->iu_opaque = opaque;
iu->iu_mem = membase;
iu->iu_memsize = memsize;
iu->iu_rsize = rsize;
iu->iu_asize = asize;
iu->iu_text_alloc_memsize = 1024 * 1024;
iu->iu_text_alloc = malloc(iu->iu_text_alloc_memsize);
iu->iu_mem_low = iu->iu_mem;
iu->iu_mem_high = iu->iu_mem + memsize;
return iu;
}
void *
vmir_get_opaque(ir_unit_t *iu)
{
return iu->iu_opaque;
}
vmir_function_resolver_t vmir_get_external_function_resolver(ir_unit_t *iu)
{
return iu->iu_external_function_resolver;
}
void vmir_set_external_function_resolver(ir_unit_t *iu, vmir_function_resolver_t fn)
{
iu->iu_external_function_resolver = fn;
}
/**
*
*/
static const ir_globalvar_t *
find_globalvar(ir_unit_t *iu, const char *name)
{
for(int i = 0; i < iu->iu_next_value; i++) {
ir_value_t *iv = value_get(iu, i);
if(iv->iv_class != IR_VC_GLOBALVAR)
continue;
ir_globalvar_t *ig = iv->iv_gvar;
if(ig->ig_name != NULL && !strcmp(ig->ig_name, name))
return ig;
}
return NULL;
}
/**
*
*/
static void
run_global_ctors(ir_unit_t *iu)
{
const ir_globalvar_t *ig = find_globalvar(iu, "llvm.global_ctors");
if(ig == NULL)
return;
const int ctor_size = 12;
int num_ctors = ig->ig_size / ctor_size;
for(int i = 0; i < num_ctors; i++) {
uint32_t fn = mem_rd32(iu->iu_mem + ig->ig_addr + ctor_size * i + 4, iu);
if(fn < VECTOR_LEN(&iu->iu_functions)) {
ir_function_t *f = VECTOR_ITEM(&iu->iu_functions, fn);
vmir_vm_function_call(iu, f, NULL);
}
}
}
/**
*
*/
int
vmir_load(ir_unit_t *iu, const uint8_t *u8, int len)
{
bcbitstream_t bs = {0};
bs.rdata = u8;
bs.bytes_length = len;
TAILQ_INIT(&iu->iu_functions_with_bodies);
if(setjmp(iu->iu_parser_jmp)) {
iu_cleanup(iu);
return VMIR_ERR_LOAD_ERROR;
}
#ifdef VMIR_VM_JIT
jit_init(iu);
#endif
const uint32_t magic = read_bits(&bs, 32);
iu->iu_data_ptr = 4096;
switch(magic) {
case 0x6d736100: // WebAssembly
iu->iu_mode = VMIR_WASM;
wasm_parse_module(iu, u8 + 4, u8 + len);
break;
case 0xdec04342: // LLVM Bitcode
// WebAssembly need memory at 0. Bitcode don't really.
ir_parse_blocks(iu, 2, NULL, NULL, &bs);
break;
default:
return VMIR_ERR_NOT_BITCODE;
}
free(iu->iu_text_alloc);
#ifdef VMIR_VM_JIT
jit_seal_code(iu);
#endif
iu->iu_heap_start = VMIR_ALIGN(iu->iu_data_ptr, 4096);
iu->iu_stats.data_size = iu->iu_heap_start;
vmir_heap_init(iu);
initialize_globals(iu, iu->iu_mem);
libc_initialize(iu);
iu->iu_vm_funcs = calloc(VECTOR_LEN(&iu->iu_functions), sizeof(void *));
iu->iu_function_table = calloc(VECTOR_LEN(&iu->iu_functions), sizeof(void *));
for(int i = 0; i < VECTOR_LEN(&iu->iu_functions); i++) {
ir_function_t *f = VECTOR_ITEM(&iu->iu_functions, i);
iu->iu_vm_funcs[i] = f->if_vm_text;
iu->iu_function_table[i] = f->if_ext_func;
if(f->if_used && f->if_vm_text == NULL && f->if_ext_func == NULL) {
vmir_log(iu, VMIR_LOG_ERROR, "Function %s() is not defined", f->if_name);
if(!(iu->iu_debug_flags & VMIR_DBG_IGNORE_UNRESOLVED_FUNCTIONS)) {
parser_error(iu, "Function %s() is not defined", f->if_name);
}
}
}
run_global_ctors(iu);
iu_cleanup(iu);
return 0;
}
/**
*
*/
static int
instrumentation_cmp(const ir_instrumentation_t *a,
const ir_instrumentation_t *b)
{
if(a->ii_count * a->ii_instructions < b->ii_count * b->ii_instructions)
return -1;
if(a->ii_count * a->ii_instructions > b->ii_count * b->ii_instructions)
return 1;
return 0;
}
/**
*
*/
void
vmir_instrumentation_dump(ir_unit_t *iu)
{
VECTOR_SORT(&iu->iu_instrumentation, instrumentation_cmp);
for(int i = 0; i < VECTOR_LEN(&iu->iu_instrumentation); i++) {
const ir_instrumentation_t *ii = &VECTOR_ITEM(&iu->iu_instrumentation, i);
printf("%10"PRId64" %10"PRId64" %s.%d\n",
ii->ii_count,
ii->ii_count * ii->ii_instructions,
ii->ii_func->if_name, ii->ii_bb);
}
}
/**
*
*/