-
Notifications
You must be signed in to change notification settings - Fork 571
/
Copy pathinstr.h
3209 lines (2872 loc) · 107 KB
/
instr.h
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) 2011-2020 Google, Inc. All rights reserved.
* Copyright (c) 2000-2010 VMware, Inc. All rights reserved.
* **********************************************************/
/*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of VMware, Inc. nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL VMWARE, INC. OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*/
/* Copyright (c) 2003-2007 Determina Corp. */
/* Copyright (c) 2001-2003 Massachusetts Institute of Technology */
/* Copyright (c) 2000-2001 Hewlett-Packard Company */
/* The machine-specific IR consists of instruction lists, instructions,
* operands, and opcodes. You can find related declarations and interface
* functions in corresponding headers.
*/
/* file "instr.h" -- instr_t specific definitions and utilities */
#ifndef _INSTR_H_
#define _INSTR_H_ 1
#include "opcode.h"
#include "opnd.h"
/* To avoid duplicating code we use our own exported macros, unless an includer
* needs to avoid it.
*/
#ifdef DR_NO_FAST_IR
# undef DR_FAST_IR
# undef INSTR_INLINE
#else
# define DR_FAST_IR 1
#endif
/* Avoid clang-format bug in i#3158 where having ifdef AVOID_API_EXPORT before
* a declaration causes the declaration to be indented and skipped by genapi.pl.
*/
#ifdef AVOID_API_EXPORT
# define INSTR_INLINE_INTERNALLY INSTR_INLINE
#else
# define INSTR_INLINE_INTERNALLY /* nothing */
#endif
/* can't include decode.h, it includes us, just declare struct */
struct instr_info_t;
/* DR_API EXPORT TOFILE dr_ir_opcodes.h */
/* DR_API EXPORT BEGIN */
#ifdef API_EXPORT_ONLY
# ifdef X86
# include "dr_ir_opcodes_x86.h"
# elif defined(AARCH64)
# include "dr_ir_opcodes_aarch64.h"
# elif defined(ARM)
# include "dr_ir_opcodes_arm.h"
# endif
#endif
/* DR_API EXPORT END */
/* If INSTR_INLINE is already defined, that means we've been included by
* instr_shared.c, which wants to use C99 extern inline. Otherwise, DR_FAST_IR
* determines whether our instr routines are inlined.
*/
/* Inlining macro controls. */
#ifndef INSTR_INLINE
# ifdef DR_FAST_IR
# define INSTR_INLINE inline
# else
# define INSTR_INLINE
# endif
#endif
/*************************
*** instr_t ***
*************************/
/* instr_t structure
* An instruction represented by instr_t can be in a number of states,
* depending on whether it points to raw bits that are valid,
* whether its operand and opcode fields are up to date, and whether
* its eflags field is up to date.
* Invariant: if opcode == OP_UNDECODED, raw bits should be valid.
* if opcode == OP_INVALID, raw bits may point to real bits,
* but they are not a valid instruction stream.
*
* CORRESPONDENCE WITH CGO LEVELS
* Level 0 = raw bits valid, !opcode_valid, decode_sizeof(instr) != instr->len
* opcode_valid is equivalent to opcode != OP_INVALID && opcode != OP_UNDECODED
* Level 1 = raw bits valid, !opcode_valid, decode_sizeof(instr) == instr->len
* Level 2 = raw bits valid, opcode_valid, !operands_valid
* (eflags info is auto-derived on demand so not an issue)
* Level 3 = raw bits valid, operands valid
* (we assume that if operands_valid then opcode_valid)
* Level 4 = !raw bits valid, operands valid
*
* Independent of these is whether its raw bits were allocated for
* the instr or not.
*/
/* DR_API EXPORT TOFILE dr_ir_instr.h */
/* For inlining, we need to expose some of these flags. We bracket the ones we
* want in export begin/end. AVOID_API_EXPORT does not work because there are
* nested ifdefs.
*/
/* DR_API EXPORT BEGIN */
#ifdef DR_FAST_IR
/* flags */
enum {
/* DR_API EXPORT END */
/* these first flags are shared with the LINK_ flags and are
* used to pass on info to link stubs
*/
/* used to determine type of indirect branch for exits */
INSTR_DIRECT_EXIT = LINK_DIRECT,
INSTR_INDIRECT_EXIT = LINK_INDIRECT,
INSTR_RETURN_EXIT = LINK_RETURN,
/* JMP|CALL marks an indirect jmp preceded by a call (== a PLT-style ind call)
* so use EXIT_IS_{JMP,CALL} rather than these raw bits
*/
INSTR_CALL_EXIT = LINK_CALL,
INSTR_JMP_EXIT = LINK_JMP,
INSTR_IND_JMP_PLT_EXIT = (INSTR_JMP_EXIT | INSTR_CALL_EXIT),
INSTR_FAR_EXIT = LINK_FAR,
INSTR_BRANCH_SPECIAL_EXIT = LINK_SPECIAL_EXIT,
INSTR_BRANCH_PADDED = LINK_PADDED,
# ifdef X64
/* PR 257963: since we don't store targets of ind branches, we need a flag
* so we know whether this is a trace cmp exit, which has its own ibl entry
*/
INSTR_TRACE_CMP_EXIT = LINK_TRACE_CMP,
# endif
# ifdef WINDOWS
INSTR_CALLBACK_RETURN = LINK_CALLBACK_RETURN,
# else
INSTR_NI_SYSCALL_INT = LINK_NI_SYSCALL_INT,
# endif
INSTR_NI_SYSCALL = LINK_NI_SYSCALL,
INSTR_NI_SYSCALL_ALL = LINK_NI_SYSCALL_ALL,
/* meta-flag */
EXIT_CTI_TYPES = (INSTR_DIRECT_EXIT | INSTR_INDIRECT_EXIT | INSTR_RETURN_EXIT |
INSTR_CALL_EXIT | INSTR_JMP_EXIT | INSTR_FAR_EXIT |
INSTR_BRANCH_SPECIAL_EXIT | INSTR_BRANCH_PADDED |
# ifdef X64
INSTR_TRACE_CMP_EXIT |
# endif
# ifdef WINDOWS
INSTR_CALLBACK_RETURN |
# else
INSTR_NI_SYSCALL_INT |
# endif
INSTR_NI_SYSCALL),
/* instr_t-internal flags (not shared with LINK_) */
INSTR_OPERANDS_VALID = 0x00010000,
/* meta-flag */
INSTR_FIRST_NON_LINK_SHARED_FLAG = INSTR_OPERANDS_VALID,
INSTR_EFLAGS_VALID = 0x00020000,
INSTR_EFLAGS_6_VALID = 0x00040000,
INSTR_RAW_BITS_VALID = 0x00080000,
INSTR_RAW_BITS_ALLOCATED = 0x00100000,
/* DR_API EXPORT BEGIN */
INSTR_DO_NOT_MANGLE = 0x00200000,
/* DR_API EXPORT END */
/* This flag is set by instr_noalloc_init() and used to identify the
* instr_noalloc_t "subclass" of instr_t. It should not be otherwise used.
*/
INSTR_IS_NOALLOC_STRUCT = 0x00400000,
/* used to indicate that an indirect call can be treated as a direct call */
INSTR_IND_CALL_DIRECT = 0x00800000,
# ifdef WINDOWS
/* used to indicate that a syscall should be executed via shared syscall */
INSTR_SHARED_SYSCALL = 0x01000000,
# else
/* Indicates an instruction that's part of the rseq endpoint. We use this in
* instrlist_t.flags (sort of the same namespace: INSTR_OUR_MANGLING is used there,
* but also EDI_VAL_*) and as a version of DR_NOTE_RSEQ that survives encoding
* (seems like we could store notes for labels in another field so they do
* in fact survive: a union with instr_t.translation?).
*/
INSTR_RSEQ_ENDPOINT = 0x01000000,
# endif
# ifdef CLIENT_INTERFACE
/* This enum is also used for INSTR_OUR_MANGLING_EPILOGUE. Its semantics are
* orthogonal to this and must not overlap.
*/
INSTR_CLOBBER_RETADDR = 0x02000000,
# endif
/* Indicates that the instruction is part of an own mangling region's
* epilogue (xref i#3307). Currently, instructions with the
* INSTR_CLOBBER_RETADDR property are never in a mangling epilogue, which
* is why we are reusing its enum value here.
* */
INSTR_OUR_MANGLING_EPILOGUE = 0x02000000,
/* Signifies that this instruction may need to be hot patched and should
* therefore not cross a cache line. It is not necessary to set this for
* exit cti's or linkstubs since it is mainly intended for clients etc.
* Handling of this flag is not yet implemented */
INSTR_HOT_PATCHABLE = 0x04000000,
# ifdef DEBUG
/* case 9151: only report invalid instrs for normal code decoding */
INSTR_IGNORE_INVALID = 0x08000000,
# endif
/* Currently used for frozen coarse fragments with final jmps and
* jmps to ib stubs that are elided: we need the jmp instr there
* to build the linkstub_t but we do not want to emit it. */
INSTR_DO_NOT_EMIT = 0x10000000,
/* PR 251479: re-relativization support: is instr->rip_rel_pos valid? */
INSTR_RIP_REL_VALID = 0x20000000,
# ifdef X86
/* PR 278329: each instr stores its own mode */
INSTR_X86_MODE = 0x40000000,
# elif defined(ARM)
/* We assume we don't need to distinguish A64 from A32 as you cannot swap
* between them in user mode. Thus we only need one flag.
* XXX: we might want more power for drdecode, though the global isa_mode
* should be sufficient there.
*/
INSTR_THUMB_MODE = 0x40000000,
# endif
/* PR 267260: distinguish our own mangling from client-added instrs */
INSTR_OUR_MANGLING = 0x80000000,
/* DR_API EXPORT BEGIN */
};
#endif /* DR_FAST_IR */
/** Triggers used for conditionally executed instructions. */
typedef enum _dr_pred_type_t {
#ifdef AVOID_API_EXPORT
/* We resist using #elif here because otherwise doxygen will be unable to
* document both defines, for X86 and for AARCHXX.
*/
#endif
DR_PRED_NONE, /**< No predicate is present. */
#ifdef X86
DR_PRED_O, /**< x86 condition: overflow (OF=1). */
DR_PRED_NO, /**< x86 condition: no overflow (OF=0). */
DR_PRED_B, /**< x86 condition: below (CF=1). */
DR_PRED_NB, /**< x86 condition: not below (CF=0). */
DR_PRED_Z, /**< x86 condition: zero (ZF=1). */
DR_PRED_NZ, /**< x86 condition: not zero (ZF=0). */
DR_PRED_BE, /**< x86 condition: below or equal (CF=1 or ZF=1). */
DR_PRED_NBE, /**< x86 condition: not below or equal (CF=0 and ZF=0). */
DR_PRED_S, /**< x86 condition: sign (SF=1). */
DR_PRED_NS, /**< x86 condition: not sign (SF=0). */
DR_PRED_P, /**< x86 condition: parity (PF=1). */
DR_PRED_NP, /**< x86 condition: not parity (PF=0). */
DR_PRED_L, /**< x86 condition: less (SF != OF). */
DR_PRED_NL, /**< x86 condition: not less (SF=OF). */
DR_PRED_LE, /**< x86 condition: less or equal (ZF=1 or SF != OF). */
DR_PRED_NLE, /**< x86 condition: not less or equal (ZF=0 and SF=OF). */
/**
* x86 condition: special opcode-specific condition that depends on the
* values of the source operands. Thus, unlike all of the other conditions,
* the source operands will be accessed even if the condition then fails
* and the destinations are not touched. Any written eflags are
* unconditionally written, unlike regular destination operands.
*/
DR_PRED_COMPLEX,
/* Aliases for XINST_CREATE_jump_cond() and other cross-platform routines. */
DR_PRED_EQ = DR_PRED_Z, /**< Condition code: equal. */
DR_PRED_NE = DR_PRED_NZ, /**< Condition code: not equal. */
DR_PRED_LT = DR_PRED_L, /**< Condition code: signed less than. */
/* DR_PRED_LE already matches aarchxx */
DR_PRED_GT = DR_PRED_NLE, /**< Condition code: signed greater than. */
DR_PRED_GE = DR_PRED_NL, /**< Condition code: signed greater than or equal. */
#endif
#ifdef AARCHXX
DR_PRED_EQ, /**< ARM condition: 0000 Equal (Z == 1) */
DR_PRED_NE, /**< ARM condition: 0001 Not equal (Z == 0) */
DR_PRED_CS, /**< ARM condition: 0010 Carry set (C == 1) */
DR_PRED_CC, /**< ARM condition: 0011 Carry clear (C == 0) */
DR_PRED_MI, /**< ARM condition: 0100 Minus, negative (N == 1) */
DR_PRED_PL, /**< ARM condition: 0101 Plus, positive or zero (N == 0) */
DR_PRED_VS, /**< ARM condition: 0110 Overflow (V == 1) */
DR_PRED_VC, /**< ARM condition: 0111 No overflow (V == 0) */
DR_PRED_HI, /**< ARM condition: 1000 Unsigned higher (C == 1 and Z == 0)*/
DR_PRED_LS, /**< ARM condition: 1001 Unsigned lower or same (C == 0 or Z == 1) */
DR_PRED_GE, /**< ARM condition: 1010 Signed >= (N == V) */
DR_PRED_LT, /**< ARM condition: 1011 Signed less than (N != V) */
DR_PRED_GT, /**< ARM condition: 1100 Signed greater than (Z == 0 and N == V)*/
DR_PRED_LE, /**< ARM condition: 1101 Signed <= (Z == 1 or N != V) */
DR_PRED_AL, /**< ARM condition: 1110 Always (unconditional) */
# ifdef AARCH64
DR_PRED_NV, /**< ARM condition: 1111 Never, meaning always */
# endif
# ifdef ARM
DR_PRED_OP, /**< ARM condition: 1111 Part of opcode */
# endif
/* Aliases */
DR_PRED_HS = DR_PRED_CS, /**< ARM condition: alias for DR_PRED_CS. */
DR_PRED_LO = DR_PRED_CC, /**< ARM condition: alias for DR_PRED_CC. */
#endif
} dr_pred_type_t;
/**
* Specifies hints for how an instruction should be encoded if redundant encodings are
* available. Currently, we provide a hint for x86 evex encoded instructions. It can be
* used to encode an instruction in its evex form instead of its vex format (xref #3339).
*/
typedef enum _dr_encoding_hint_type_t {
DR_ENCODING_HINT_NONE = 0x0, /**< No encoding hint is present. */
#ifdef X86
DR_ENCODING_HINT_X86_EVEX = 0x1, /**< x86: Encode in EVEX form if available. */
#endif
} dr_encoding_hint_type_t;
/* DR_API EXPORT END */
#define DR_TUPLE_TYPE_BITS 4
#define DR_TUPLE_TYPE_BITPOS (32 - DR_TUPLE_TYPE_BITS)
/* AVX-512 tuple type attributes as specified in Intel's tables. */
typedef enum _dr_tuple_type_t {
DR_TUPLE_TYPE_NONE = 0,
#ifdef X86
DR_TUPLE_TYPE_FV = 1,
DR_TUPLE_TYPE_HV = 2,
DR_TUPLE_TYPE_FVM = 3,
DR_TUPLE_TYPE_T1S = 4,
DR_TUPLE_TYPE_T1F = 5,
DR_TUPLE_TYPE_T2 = 6,
DR_TUPLE_TYPE_T4 = 7,
DR_TUPLE_TYPE_T8 = 8,
DR_TUPLE_TYPE_HVM = 9,
DR_TUPLE_TYPE_QVM = 10,
DR_TUPLE_TYPE_OVM = 11,
DR_TUPLE_TYPE_M128 = 12,
DR_TUPLE_TYPE_DUP = 13,
#endif
} dr_tuple_type_t;
/* These aren't composable, so we store them in as few bits as possible.
* The top 5 prefix bits hold the value (x86 needs 17 values).
* XXX: if we need more space we could compress the x86 values: they're
* all pos/neg pairs so we could store the pos/neg bit just once.
* XXX: if we want a slightly faster predication check we could take
* a dedicated PREFIX_PREDICATED bit.
*/
#define PREFIX_PRED_BITS 5
#define PREFIX_PRED_BITPOS (32 - PREFIX_PRED_BITS)
#define PREFIX_PRED_MASK \
(((1 << PREFIX_PRED_BITS) - 1) << PREFIX_PRED_BITPOS) /*0xf8000000 */
/* DR_API EXPORT BEGIN */
/**
* Data slots available in a label (instr_create_label()) instruction
* for storing client-controlled data. Accessible via
* instr_get_label_data_area().
*/
typedef struct _dr_instr_label_data_t {
ptr_uint_t data[4]; /**< Generic fields for storing user-controlled data */
} dr_instr_label_data_t;
/**
* Label instruction callback function. Set by instr_set_label_callback() and
* called when the label is freed. \p instr is the label instruction allowing
* the caller to free the label's auxiliary data.
*/
typedef void (*instr_label_callback_t)(void *drcontext, instr_t *instr);
/**
* Bitmask values passed as flags to routines that ask about whether operands
* and condition codes are read or written. These flags determine how to treat
* conditionally executed instructions.
* As a special case, the addressing registers inside a destination memory
* operand are covered by DR_QUERY_INCLUDE_COND_SRCS rather than
* DR_QUERY_INCLUDE_COND_DSTS.
*/
typedef enum _dr_opnd_query_flags_t {
/**
* By default, routines that take in these flags will only consider
* destinations that are always written. Thus, all destinations are skipped
* for an instruction that is predicated and executes conditionally (see
* instr_is_predicated()). If this flag is set, a conditionally executed
* instruction's destinations are included just like any other
* instruction's. As a special case, the addressing registers inside a
* destination memory operand are covered by DR_QUERY_INCLUDE_COND_SRCS
* rather than this flag.
*/
DR_QUERY_INCLUDE_COND_DSTS = 0x01,
/**
* By default, routines that take in these flags will only consider sources
* that are always read. Thus, all sources are skipped for an instruction
* that is predicated and executes conditionally (see
* instr_is_predicated()), except for predication conditions that involve
* the source operand values. If this flag is set, a conditionally executed
* instruction's sources are included just like any other instruction's.
* As a special case, the addressing registers inside a destination memory
* operand are covered by this flag rather than DR_QUERY_INCLUDE_COND_DSTS.
*/
DR_QUERY_INCLUDE_COND_SRCS = 0x02,
/** The default value that typical liveness analysis would want to use. */
DR_QUERY_DEFAULT = DR_QUERY_INCLUDE_COND_SRCS,
/** Includes all operands whether conditional or not. */
DR_QUERY_INCLUDE_ALL = (DR_QUERY_INCLUDE_COND_DSTS | DR_QUERY_INCLUDE_COND_SRCS),
} dr_opnd_query_flags_t;
#ifdef DR_FAST_IR
/* DR_API EXPORT END */
/* FIXME: could shrink prefixes, eflags, opcode, and flags fields
* this struct isn't a memory bottleneck though b/c it isn't persistent
*/
/* DR_API EXPORT BEGIN */
/**
* instr_t type exposed for optional "fast IR" access. Note that DynamoRIO
* reserves the right to change this structure across releases and does
* not guarantee binary or source compatibility when this structure's fields
* are directly accessed. If the instr_ accessor routines are used, DynamoRIO does
* guarantee source compatibility, but not binary compatibility. If binary
* compatibility is desired, do not use the fast IR feature.
*/
struct _instr_t {
/* flags contains the constants defined above */
uint flags;
/* hints for encoding this instr in a specific way, holds dr_encoding_hint_type_t */
uint encoding_hints;
/* Raw bits of length length are pointed to by the bytes field.
* label_cb stores a callback function pointer used by label instructions
* and called when the label is freed.
*/
uint length;
union {
byte *bytes;
instr_label_callback_t label_cb;
};
/* translation target for this instr */
app_pc translation;
uint opcode;
# ifdef X86
/* PR 251479: offset into instr's raw bytes of rip-relative 4-byte displacement */
byte rip_rel_pos;
# endif
/* we dynamically allocate dst and src arrays b/c x86 instrs can have
* up to 8 of each of them, but most have <=2 dsts and <=3 srcs, and we
* use this struct for un-decoded instrs too
*/
byte num_dsts;
byte num_srcs;
union {
struct {
/* for efficiency everyone has a 1st src opnd, since we often just
* decode jumps, which all have a single source (==target)
* yes this is an extra 10 bytes, but the whole struct is still < 64 bytes!
*/
opnd_t src0;
opnd_t *srcs; /* this array has 2nd src and beyond */
opnd_t *dsts;
};
dr_instr_label_data_t label_data;
};
uint prefixes; /* data size, addr size, or lock prefix info */
uint eflags; /* contains EFLAGS_ bits, but amount of info varies
* depending on how instr was decoded/built */
/* this field is for the use of passes as an annotation.
* it is also used to hold the offset of an instruction when encoding
* pc-relative instructions. A small range of values is reserved for internal use
* by DR and cannot be used by clients; see DR_NOTE_FIRST_RESERVED in globals.h.
*/
void *note;
/* fields for building instructions into instruction lists */
instr_t *prev;
instr_t *next;
}; /* instr_t */
#endif /* DR_FAST_IR */
/**
* A version of #instr_t which guarantees to not use heap allocation for regular
* decoding and encoding. It inlines all the possible operands and encoding space
* inside the structure. Some operations could still use heap if custom label data is
* used to point at heap-allocated structures through extension libraries or custom
* code.
*
* The instr_from_noalloc() function should be used to obtain an #instr_t pointer for
* passing to API functions:
*
* \code
* instr_noalloc_t noalloc;
* instr_noalloc_init(dcontext, &noalloc);
* instr_t *instr = instr_from_noalloc(&noalloc);
* pc = decode(dcontext, ptr, instr);
* \endcode
*
* No freeing is required. To re-use the same structure, instr_reset() can be called.
*
* Some operations are not supported on this instruction format:
* + instr_clone()
* + instr_remove_srcs()
* + instr_remove_dsts()
* + Automated re-relativization when encoding.
*
* This format does not support caching encodings, so it is less efficient for encoding.
* It is intended for use when decoding in a signal handler or other locations where
* heap allocation is unsafe.
*/
typedef struct instr_noalloc_t {
instr_t instr; /**< The base instruction, valid for passing to API functions. */
opnd_t srcs[MAX_SRC_OPNDS - 1]; /**< Built-in storage for source operands. */
opnd_t dsts[MAX_DST_OPNDS]; /**< Built-in storage for destination operands. */
byte encode_buf[MAX_INSTR_LENGTH]; /**< Encoding space for instr_length(), etc. */
} instr_noalloc_t;
/****************************************************************************
* INSTR ROUTINES
*/
/**
* @file dr_ir_instr.h
* @brief Functions to create and manipulate instructions.
*/
/* DR_API EXPORT END */
DR_API
/**
* Returns an initialized instr_t allocated on the thread-local heap.
* Sets the x86/x64 mode of the returned instr_t to the mode of dcontext.
* The instruction should be de-allocated with instr_destroy(), which
* will be called automatically if this instruction is added to the instruction
* list passed to the basic block or trace events.
*/
/* For -x86_to_x64, sets the mode of the instr to the code cache mode instead of
the app mode. */
instr_t *
instr_create(dcontext_t *dcontext);
DR_API
/**
* Initializes \p instr.
* Sets the x86/x64 mode of \p instr to the mode of dcontext.
* When finished with it, the instruction's internal memory should be freed
* with instr_free(), or instr_reset() for reuse.
*/
void
instr_init(dcontext_t *dcontext, instr_t *instr);
DR_API
/**
* Initializes the no-heap-allocation structure \p instr.
* Sets the x86/x64 mode of \p instr to the mode of dcontext.
*/
void
instr_noalloc_init(dcontext_t *dcontext, instr_noalloc_t *instr);
DR_API
INSTR_INLINE
/**
* Given an #instr_noalloc_t where all operands are included, returns
* an #instr_t pointer corresponding to that no-alloc structure suitable for
* passing to instruction API functions.
*/
instr_t *
instr_from_noalloc(instr_noalloc_t *noalloc);
DR_API
/**
* Deallocates all memory that was allocated by \p instr. This
* includes raw bytes allocated by instr_allocate_raw_bits() and
* operands allocated by instr_set_num_opnds(). Does not deallocate
* the storage for \p instr itself (use instr_destroy() instead if
* \p instr was created with instr_create()).
*/
void
instr_free(dcontext_t *dcontext, instr_t *instr);
DR_API
/**
* Performs both instr_free() and instr_init().
* \p instr must have been initialized.
*/
void
instr_reset(dcontext_t *dcontext, instr_t *instr);
DR_API
/**
* Frees all dynamically allocated storage that was allocated by \p instr,
* except for allocated bits.
* Also zeroes out \p instr's fields, except for raw bit fields,
* whether \p instr is instr_is_meta(), and the x86 mode of \p instr.
* \p instr must have been initialized.
*/
void
instr_reuse(dcontext_t *dcontext, instr_t *instr);
DR_API
/**
* Performs instr_free() and then deallocates the thread-local heap
* storage for \p instr that was performed by instr_create().
*/
void
instr_destroy(dcontext_t *dcontext, instr_t *instr);
DR_API
INSTR_INLINE
/**
* Returns the next instr_t in the instrlist_t that contains \p instr.
* \note The next pointer for an instr_t is inside the instr_t data
* structure itself, making it impossible to have on instr_t in
* two different InstrLists (but removing the need for an extra data
* structure for each element of the instrlist_t).
*/
instr_t *
instr_get_next(instr_t *instr);
DR_API
INSTR_INLINE
/**
* Returns the next application (non-meta) instruction in the instruction list
* that contains \p instr.
*
* \note As opposed to instr_get_next(), this routine skips all meta
* instructions inserted by either DynamoRIO or its clients.
*
* \note We recommend using this routine during the phase of application
* code analysis, as any meta instructions present are guaranteed to be ok
* to skip.
* However, caution should be exercised if using this routine after any
* instrumentation insertion has already happened, as instrumentation might
* affect register usage or other factors being analyzed.
*/
instr_t *
instr_get_next_app(instr_t *instr);
DR_API
INSTR_INLINE
/** Returns the previous instr_t in the instrlist_t that contains \p instr. */
instr_t *
instr_get_prev(instr_t *instr);
DR_API
INSTR_INLINE
/**
* Returns the previous application (non-meta) instruction in the instruction
* list that contains \p instr.
*
* \note As opposed to instr_get_prev(), this routine skips all meta
* instructions inserted by either DynamoRIO or its clients.
*
* \note We recommend using this routine during the phase of application
* code analysis, as any meta instructions present are guaranteed to be ok
* to skip.
* However, caution should be exercised if using this routine after any
* instrumentation insertion has already happened, as instrumentation might
* affect register usage or other factors being analyzed.
*/
instr_t *
instr_get_prev_app(instr_t *instr);
DR_API
INSTR_INLINE
/** Sets the next field of \p instr to point to \p next. */
void
instr_set_next(instr_t *instr, instr_t *next);
DR_API
INSTR_INLINE
/** Sets the prev field of \p instr to point to \p prev. */
void
instr_set_prev(instr_t *instr, instr_t *prev);
DR_API
INSTR_INLINE
/**
* Gets the value of the user-controlled note field in \p instr.
* \note Important: is also used when emitting for targets that are other
* instructions. Thus it will be overwritten when calling instrlist_encode()
* or instrlist_encode_to_copy() with \p has_instr_jmp_targets set to true.
* \note The note field is copied (shallowly) by instr_clone().
*/
void *
instr_get_note(instr_t *instr);
DR_API
INSTR_INLINE
/** Sets the user-controlled note field in \p instr to \p value. */
void
instr_set_note(instr_t *instr, void *value);
DR_API
/** Return the taken target pc of the (direct branch) instruction. */
app_pc
instr_get_branch_target_pc(instr_t *cti_instr);
DR_API
/** Set the taken target pc of the (direct branch) instruction. */
void
instr_set_branch_target_pc(instr_t *cti_instr, app_pc pc);
DR_API
/**
* Returns true iff \p instr is a conditional branch, unconditional branch,
* or indirect branch with a program address target (NOT an instr_t address target)
* and \p instr is ok to mangle.
*/
#ifdef UNSUPPORTED_API
/**
* This routine does NOT try to decode an opcode in a Level 1 or Level
* 0 routine, and can thus be called on Level 0 routines.
*/
#endif
bool
instr_is_exit_cti(instr_t *instr);
DR_API
/** Return true iff \p instr's opcode is OP_int, OP_into, or OP_int3. */
bool
instr_is_interrupt(instr_t *instr);
bool
instr_branch_is_padded(instr_t *instr);
void
instr_branch_set_padded(instr_t *instr, bool val);
/* Returns true iff \p instr has been marked as a special fragment
* exit cti.
*/
bool
instr_branch_special_exit(instr_t *instr);
/* If \p val is true, indicates that \p instr is a special exit cti.
* If \p val is false, indicates otherwise.
*/
void
instr_branch_set_special_exit(instr_t *instr, bool val);
DR_API
INSTR_INLINE
/**
* Return true iff \p instr is an application (non-meta) instruction
* (see instr_set_app() for more information).
*/
bool
instr_is_app(instr_t *instr);
DR_API
/**
* Sets \p instr as an application (non-meta) instruction.
* An application instruction might be mangled by DR if necessary,
* e.g., to create an exit stub for a branch instruction.
* All application instructions that are added to basic blocks or
* traces should have their translation fields set (via
* #instr_set_translation()).
*/
void
instr_set_app(instr_t *instr);
DR_API
INSTR_INLINE
/**
* Return true iff \p instr is a meta instruction
* (see instr_set_meta() for more information).
*/
bool
instr_is_meta(instr_t *instr);
DR_API
/**
* Sets \p instr as a meta instruction.
* A meta instruction will not be mangled by DR in any way, which is necessary
* to have DR not create an exit stub for a branch instruction.
* Meta instructions should not fault (unless such faults are handled
* by the client) and are not considered
* application instructions but rather added instrumentation code (see
* #dr_register_bb_event() for further information).
*/
void
instr_set_meta(instr_t *instr);
DR_API
INSTR_INLINE
/**
* Return true iff \p instr is not a meta-instruction
* (see instr_set_app() for more information).
*
* \deprecated instr_is_app()/instr_is_meta() should be used instead.
*/
bool
instr_ok_to_mangle(instr_t *instr);
DR_API
/**
* Sets \p instr to "ok to mangle" if \p val is true and "not ok to
* mangle" if \p val is false.
*
* \deprecated instr_set_app()/instr_set_meta() should be used instead.
*/
void
instr_set_ok_to_mangle(instr_t *instr, bool val);
DR_API
/**
* A convenience routine that calls both
* #instr_set_meta (instr) and
* #instr_set_translation (instr, NULL).
*/
void
instr_set_meta_no_translation(instr_t *instr);
DR_API
#ifdef AVOID_API_EXPORT
/* This is hot internally, but unlikely to be used by clients. */
INSTR_INLINE
#endif
/** Return true iff \p instr is to be emitted into the cache. */
bool
instr_ok_to_emit(instr_t *instr);
DR_API
/**
* Set \p instr to "ok to emit" if \p val is true and "not ok to emit"
* if \p val is false. An instruction that should not be emitted is
* treated normally by DR for purposes of exits but is not placed into
* the cache. It is used for final jumps that are to be elided.
*/
void
instr_set_ok_to_emit(instr_t *instr, bool val);
DR_API
/**
* Returns the length of \p instr.
* As a side effect, if instr_is_app(instr) and \p instr's raw bits
* are invalid, encodes \p instr into bytes allocated with
* instr_allocate_raw_bits(), after which instr is marked as having
* valid raw bits.
*/
int
instr_length(dcontext_t *dcontext, instr_t *instr);
/* not exported */
void
instr_shift_raw_bits(instr_t *instr, ssize_t offs);
int
instr_exit_branch_type(instr_t *instr);
void
instr_exit_branch_set_type(instr_t *instr, uint type);
DR_API
/** Returns the total number of bytes of memory used by \p instr. */
int
instr_mem_usage(instr_t *instr);
DR_API
/**
* Returns a copy of \p orig with separately allocated memory for
* operands and raw bytes if they were present in \p orig.
* Only a shallow copy of the \p note field is made. The \p label_cb
* field will not be copied at all if \p orig is a label instruction.
*/
instr_t *
instr_clone(dcontext_t *dcontext, instr_t *orig);
DR_API
/**
* Convenience routine: calls
* - instr_create(dcontext)
* - instr_set_opcode(opcode)
* - instr_set_num_opnds(dcontext, instr, num_dsts, num_srcs)
*
* and returns the resulting instr_t.
*/
instr_t *
instr_build(dcontext_t *dcontext, int opcode, int num_dsts, int num_srcs);
DR_API
/**
* Convenience routine: calls
* - instr_create(dcontext)
* - instr_set_opcode(instr, opcode)
* - instr_allocate_raw_bits(dcontext, instr, num_bytes)
*
* and returns the resulting instr_t.
*/
instr_t *
instr_build_bits(dcontext_t *dcontext, int opcode, uint num_bytes);
DR_API
/**
* Returns true iff \p instr's opcode is NOT OP_INVALID.
* Not to be confused with an invalid opcode, which can be OP_INVALID or
* OP_UNDECODED. OP_INVALID means an instruction with no valid fields:
* raw bits (may exist but do not correspond to a valid instr), opcode,
* eflags, or operands. It could be an uninitialized
* instruction or the result of decoding an invalid sequence of bytes.
*/
bool
instr_valid(instr_t *instr);
DR_API
/** Get the original application PC of \p instr if it exists. */
app_pc
instr_get_app_pc(instr_t *instr);
DR_API
/** Returns \p instr's opcode (an OP_ constant). */
int
instr_get_opcode(instr_t *instr);
DR_API
/** Assumes \p opcode is an OP_ constant and sets it to be instr's opcode. */
void
instr_set_opcode(instr_t *instr, int opcode);
const struct instr_info_t *
instr_get_instr_info(instr_t *instr);
const struct instr_info_t *
get_instr_info(int opcode);
DR_API
INSTR_INLINE
/**
* Returns the number of source operands of \p instr.
*
* \note Addressing registers used in destination memory references
* (i.e., base, index, or segment registers) are not separately listed
* as source operands.
*/
int
instr_num_srcs(instr_t *instr);
DR_API
INSTR_INLINE
/**
* Returns the number of destination operands of \p instr.
*/
int
instr_num_dsts(instr_t *instr);
DR_API
/**
* Assumes that \p instr has been initialized but does not have any
* operands yet. Allocates storage for \p num_srcs source operands
* and \p num_dsts destination operands.
*/
void
instr_set_num_opnds(dcontext_t *dcontext, instr_t *instr, int num_dsts, int num_srcs);
DR_API
/**
* Returns \p instr's source operand at position \p pos (0-based).
*/
opnd_t
instr_get_src(instr_t *instr, uint pos);
DR_API
/**
* Returns \p instr's destination operand at position \p pos (0-based).
*/
opnd_t
instr_get_dst(instr_t *instr, uint pos);
DR_API
/**
* Sets \p instr's source operand at position \p pos to be \p opnd.
* Also calls instr_set_raw_bits_valid(\p instr, false) and
* instr_set_operands_valid(\p instr, true).
*/
void
instr_set_src(instr_t *instr, uint pos, opnd_t opnd);
DR_API
/**
* Sets \p instr's destination operand at position \p pos to be \p opnd.