-
Notifications
You must be signed in to change notification settings - Fork 571
/
Copy pathtranslate.c
1935 lines (1833 loc) · 84.2 KB
/
translate.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) 2010-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 */
/*
* translate.c - fault translation
*/
#include "../globals.h"
#include "../link.h"
#include "../fragment.h"
#include "arch.h"
#include "instr.h"
#include "instr_create.h"
#include "decode.h"
#include "decode_fast.h"
#include "../fcache.h"
#include "proc.h"
#include "instrument.h"
#if defined(DEBUG) || defined(INTERNAL)
# include "disassemble.h"
#endif
/***************************************************************************
* FAULT TRANSLATION
*
* Current status:
* After PR 214962, PR 267260, PR 263407, PR 268372, and PR 267764/i398, we
* properly translate indirect branch mangling and client modifications.
* FIXME: However, we still do not properly translate for:
* - PR 303413: properly translate native_exec and windows sysenter mangling faults
* - PR 208037/i#399: flushed fragments (need -safe_translate_flushed)
* - PR 213251: hot patch fragments (b/c nudge can change whether patched =>
* should store translations for all hot patch fragments)
* - i#400/PR 372021: restore eflags if within window of ibl or trace-cmp eflags-are-dead
* - i#751: fault translation has not been tested for x86_to_x64
*/
typedef struct _translate_walk_t {
/* The context we're translating */
priv_mcontext_t *mc;
/* The code cache span of the containing fragment */
byte *start_cache;
byte *end_cache;
/* PR 263407: Track registers spilled since the last cti, for
* restoring indirect branch and rip-rel spills. UINT_MAX means
* nothing recorded, otherwise holds offset of spill in local
* spill space.
*/
uint reg_spill_offs[REG_SPILL_NUM];
bool reg_tls[REG_SPILL_NUM];
/* PR 267260: Track our own mangle-inserted pushes and pops, for
* restoring state in the middle of our indirect branch mangling.
* This is the adjustment in the forward direction.
*/
int xsp_adjust;
/* Track whether we've seen an instr for which we can't relocate */
bool unsupported_mangle;
/* Are we currently in a mangle region */
bool in_mangle_region;
/* Are we currently in a mangle region's epilogue */
bool in_mangle_region_epilogue;
/* What is the translation target of the current mangle region */
app_pc translation;
} translate_walk_t;
static void
translate_walk_init(translate_walk_t *walk, byte *start_cache, byte *end_cache,
priv_mcontext_t *mc)
{
memset(walk, 0, sizeof(*walk));
walk->mc = mc;
walk->start_cache = start_cache;
walk->end_cache = end_cache;
for (int r = 0; r < REG_SPILL_NUM; r++)
walk->reg_spill_offs[r] = UINT_MAX;
}
#ifdef UNIX
static inline bool
instr_is_inline_syscall_jmp(dcontext_t *dcontext, instr_t *inst)
{
if (!instr_is_our_mangling(inst))
return false;
/* Not bothering to check whether there's a nearby syscall instr:
* any label-targeting short jump should be fine to ignore.
*/
# ifdef X86
return (instr_get_opcode(inst) == OP_jmp_short &&
opnd_is_instr(instr_get_target(inst)));
# elif defined(AARCH64)
return (instr_get_opcode(inst) == OP_b && opnd_is_instr(instr_get_target(inst)));
# elif defined(ARM)
return ((instr_get_opcode(inst) == OP_b_short ||
/* A32 uses a regular jump */
instr_get_opcode(inst) == OP_b) &&
opnd_is_instr(instr_get_target(inst)));
# else
ASSERT_NOT_IMPLEMENTED(false);
return false;
# endif /* X86/ARM */
}
static inline bool
instr_is_seg_ref_load(dcontext_t *dcontext, instr_t *inst)
{
# ifdef X86
/* This won't fault but we don't want "unsupported mangle instr" message. */
if (!instr_is_our_mangling(inst))
return false;
/* Look for the load of either segment base */
if (instr_is_tls_restore(inst, REG_NULL /*don't care*/,
os_tls_offset(os_get_app_tls_base_offset(SEG_FS))) ||
instr_is_tls_restore(inst, REG_NULL /*don't care*/,
os_tls_offset(os_get_app_tls_base_offset(SEG_GS))))
return true;
/* Look for the lea */
if (instr_get_opcode(inst) == OP_lea) {
opnd_t mem = instr_get_src(inst, 0);
if (opnd_get_scale(mem) == 1 &&
opnd_get_index(mem) == opnd_get_reg(instr_get_dst(inst, 0)))
return true;
}
# endif /* X86 */
return false;
}
static inline bool
instr_is_rseq_load(dcontext_t *dcontext, instr_t *inst)
{
/* TODO i#2350: Add non-x86 support. */
# if defined(LINUX) && defined(X86)
/* This won't fault but we don't want it marked as unsupported. */
if (!instr_is_our_mangling(inst))
return false;
/* XXX: Keep this consistent with mangle_rseq_* in mangle_shared.c. */
if (instr_get_opcode(inst) == OP_mov_ld && opnd_is_reg(instr_get_dst(inst, 0)) &&
opnd_is_base_disp(instr_get_src(inst, 0))) {
reg_id_t dst = opnd_get_reg(instr_get_dst(inst, 0));
opnd_t memref = instr_get_src(inst, 0);
int disp = opnd_get_disp(memref);
if (reg_is_gpr(dst) && reg_is_pointer_sized(dst) &&
opnd_get_index(memref) == DR_REG_NULL &&
disp ==
offsetof(dcontext_t, rseq_entry_state) +
sizeof(reg_t) * (dst - DR_REG_START_GPR))
return true;
}
# endif
return false;
}
#endif /* UNIX */
#ifdef ARM
static bool
instr_is_mov_PC_immed(dcontext_t *dcontext, instr_t *inst)
{
if (!instr_is_our_mangling(inst))
return false;
return (instr_get_opcode(inst) == OP_movw || instr_get_opcode(inst) == OP_movt);
}
#endif
#ifdef X86
/* FIXME i#3329: add support for ARM/AArch64. */
static bool
translate_walk_enters_mangling_epilogue(dcontext_t *tdcontext, instr_t *inst,
translate_walk_t *walk)
{
return !walk->in_mangle_region_epilogue && instr_is_our_mangling_epilogue(inst);
}
static bool
translate_walk_exits_mangling_epilogue(dcontext_t *tdcontext, instr_t *inst,
translate_walk_t *walk)
{
return walk->in_mangle_region_epilogue && !instr_is_our_mangling_epilogue(inst);
}
#endif
static void
translate_walk_track(dcontext_t *tdcontext, instr_t *inst, translate_walk_t *walk)
{
reg_id_t reg, r;
bool spill, spill_tls;
/* Two mangle regions can be adjacent: distinguish by translation field */
if (walk->in_mangle_region &&
/* On ARM, we spill registers across an app instr, so go solely on xl8 */
(IF_X86(!instr_is_our_mangling(inst) ||)
/* handle adjacent mangle regions */
IF_X86(translate_walk_exits_mangling_epilogue(tdcontext, inst, walk) ||)
/* Entering the mangling region's epilogue can have different xl8 */
(IF_X86(!translate_walk_enters_mangling_epilogue(tdcontext, inst, walk) &&)
instr_get_translation(inst) != walk->translation))) {
LOG(THREAD_GET, LOG_INTERP, 5, "%s: from one mangle region to another\n",
__FUNCTION__);
/* We assume our manglings are local and contiguous: once out of a
* mangling region, we're good to go again.
*/
walk->in_mangle_region = false;
walk->in_mangle_region_epilogue = false;
walk->unsupported_mangle = false;
walk->xsp_adjust = 0;
for (r = 0; r < REG_SPILL_NUM; r++) {
#ifndef ARM
/* we should have seen a restore for every spill, unless at
* fragment-ending jump to ibl, which shouldn't come here
*/
ASSERT(walk->reg_spill_offs[r] == UINT_MAX);
walk->reg_spill_offs[r] = UINT_MAX; /* be paranoid */
#else
/* On ARM we do spill registers across app instrs and mangle
* regions, though right now only the following routines do this:
* - mangle_stolen_reg()
* - mangle_gpr_list_read()
* - mangle_reads_thread_register()
* Each of these cases is a tls restore, and we assert as much.
*/
DOCHECK(1, {
if (walk->reg_spill_offs[r] != UINT_MAX) {
instr_t *curr;
bool spill_or_restore = false;
for (curr = inst; curr != NULL; curr = instr_get_next(curr)) {
spill_or_restore = instr_is_DR_reg_spill_or_restore(
tdcontext, curr, &spill_tls, &spill, ®, NULL);
if (spill_or_restore)
break;
}
ASSERT(spill_or_restore && r == reg - REG_START_SPILL && !spill &&
spill_tls);
}
});
#endif
}
}
if (instr_is_our_mangling(inst)) {
if (!walk->in_mangle_region) {
walk->in_mangle_region = true;
walk->translation = instr_get_translation(inst);
LOG(THREAD_GET, LOG_INTERP, 5, "%s: entering mangle region xl8=" PFX "\n",
__FUNCTION__, walk->translation);
} else if (IF_X86_ELSE(
translate_walk_enters_mangling_epilogue(tdcontext, inst, walk),
false)) {
walk->in_mangle_region_epilogue = true;
walk->translation = instr_get_translation(inst);
LOG(THREAD_GET, LOG_INTERP, 5,
"%s: entering mangle region epilogue xl8=" PFX "\n", __FUNCTION__,
walk->translation);
} else
ASSERT(walk->translation == instr_get_translation(inst));
/* PR 302951: we recognize a clean call by its NULL translation.
* We do not track any stack or spills: we assume we will only
* fault on an argument that references app memory, in which case
* we restore to the priv_mcontext_t on the stack.
*/
if (walk->translation == NULL) {
DOLOG(4, LOG_INTERP, {
d_r_loginst(get_thread_private_dcontext(), 4, inst,
"\tin clean call arg region");
});
return;
}
/* PR 263407: track register values that we've spilled. We assume
* that spilling to non-canonical slots only happens in ibl or
* context switch code: never in app code mangling. Since a client
* might add ctis (non-linear code) and its own spills, we track
* register spills only within our own mangling code (for
* post-mangling traces (PR 306163) we require that the client
* handle all translation if it modifies our mangling regions:
* we'll provide a query routine instr_is_DR_mangling()): our
* spills are all local anyway, except
* for selfmod, which we hardcode rep-string support for (non-linear code
* isn't handled by general reg scan). Our trace cmp is the only
* instance (besides selfmod) where we have a cti in our mangling,
* but it doesn't affect our linearity assumption. We assume we
* have no entry points in between a spill and a restore. Our
* mangling goes in last (for regular bbs and traces; see
* comment above for post-mangling traces), and so for local
* spills like rip-rel and ind branches this is fine.
*/
if (instr_is_cti(inst)
#ifdef X86
&&
/* Do not reset for a trace-cmp jecxz or jmp (32-bit) or
* jne (64-bit), since ecx needs to be restored (won't
* fault, but for thread relocation)
*/
((instr_get_opcode(inst) != OP_jecxz && instr_get_opcode(inst) != OP_jmp &&
/* x64 trace cmp uses jne for exit */
instr_get_opcode(inst) != OP_jne) ||
/* Rather than check for trace, just ignore exit jumps, which
* won't mess up linearity here. For stored translation info we
* don't have meta-flags so we can't use instr_is_exit_cti(). */
((instr_get_opcode(inst) == OP_jmp ||
/* x64 trace cmp uses jne for exit */
instr_get_opcode(inst) == OP_jne) &&
(!opnd_is_pc(instr_get_target(inst)) ||
(opnd_get_pc(instr_get_target(inst)) >= walk->start_cache &&
opnd_get_pc(instr_get_target(inst)) < walk->end_cache))))
#endif
) {
/* FIXME i#1551: add ARM version of the series of trace cti checks above */
IF_ARM(ASSERT_NOT_IMPLEMENTED(DYNAMO_OPTION(disable_traces)));
/* reset for non-exit non-trace-jecxz cti (i.e., selfmod cti) */
for (r = 0; r < REG_SPILL_NUM; r++)
walk->reg_spill_offs[r] = UINT_MAX;
}
uint offs = UINT_MAX;
if (instr_is_DR_reg_spill_or_restore(tdcontext, inst, &spill_tls, &spill, ®,
&offs)) {
r = reg - REG_START_SPILL;
ASSERT(r < REG_SPILL_NUM);
IF_ARM({
/* Ignore the spill of r0 into TLS for syscall restart
* XXX: we're assuming it's immediately prior to the syscall.
*/
if (instr_get_next(inst) != NULL &&
instr_is_syscall(instr_get_next(inst)))
spill = false;
});
/* if a restore whose spill was before a cti, ignore */
if (spill || walk->reg_spill_offs[r] != UINT_MAX) {
/* Ensure restores and spills are properly paired up, but we do
* allow for redundant spills.
*/
ASSERT(spill || (!spill && walk->reg_spill_offs[r] != UINT_MAX));
ASSERT(spill || walk->reg_tls[r] == spill_tls);
if (spill) {
ASSERT(offs != UINT_MAX);
walk->reg_spill_offs[r] = offs;
} else {
walk->reg_spill_offs[r] = UINT_MAX;
}
walk->reg_tls[r] = spill_tls;
LOG(THREAD_GET, LOG_INTERP, 5, "\tspill update: %s %s %s\n",
spill ? "spill" : "restore", spill_tls ? "tls" : "mcontext",
reg_names[reg]);
}
}
#ifdef ARM
else if (instr_is_stolen_reg_move(inst, &spill, ®)) {
/* do nothing */
LOG(THREAD_GET, LOG_INTERP, 5, "%s: stolen reg move\n", __FUNCTION__);
}
#endif
/* PR 267260: Track our own mangle-inserted pushes and pops, for
* restoring state on an app fault in the middle of our indirect
* branch mangling. We only need to support instrs added up until
* the last one that could have an app fault, as we can fail when
* called to translate for thread relocation: thus we ignore
* syscall mangling.
*
* The main scenarios are:
*
* 1) call*: "spill ecx; mov->ecx; push retaddr":
* ecx restore handled above
* 2) far direct call: "push cs; push retaddr"
* if fail on 2nd push need to undo 1st push
* 3) far call*: "spill ecx; tgt->ecx; push cs; push retaddr"
* if fail on 1st push, restore ecx (above); 2nd push, also undo 1st push
* 4) iret: "pop eip; pop cs; pop eflags; (pop rsp; pop ss)"
* if fail on non-initial pop, undo earlier pops
* 5) lret: "pop eip; pop cs"
* if fail on non-initial pop, undo earlier pops
*
* FIXME: some of these push/pops are simulated (we simply adjust
* esp or do nothing), so we're not truly fault-transparent.
*/
else if (instr_check_xsp_mangling(tdcontext, inst, &walk->xsp_adjust)) {
/* walk->xsp_adjust is now adjusted */
} else if (instr_is_trace_cmp(tdcontext, inst)) {
/* nothing to do */
/* We don't support restoring a fault in the middle, but we
* identify here to avoid "unsupported mangle instr" message
*/
}
#ifdef UNIX
else if (instr_is_inline_syscall_jmp(tdcontext, inst)) {
/* nothing to do */
} else if (instr_is_seg_ref_load(tdcontext, inst)) {
/* nothing to do */
} else if (instr_is_rseq_load(tdcontext, inst)) {
/* nothing to do */
}
#endif
#ifdef ARM
else if (instr_is_mov_PC_immed(tdcontext, inst)) {
/* nothing to do */
}
#endif
/* Single step mangling adds a nop. */
else if (instr_is_nop(inst)) {
/* nothing to do */
} else if (instr_is_app(inst)) {
/* To have reg spill+restore in the same mangle region, we mark
* the (modified) app instr for rip-rel and for segment mangling as
* "our mangling". There's nothing specific to do for it.
*/
}
/* We do not support restoring state at arbitrary points for thread
* relocation (a performance issue, not a correctness one): if not a
* spill, restore, push, or pop, we will not properly translate.
* For an exit jmp for a simple ret we could relocate: but better not to
* for a call, since we've modified the stack w/ a push, so we fail on
* all exit jmps.
*/
else {
DOLOG(4, LOG_INTERP,
d_r_loginst(get_thread_private_dcontext(), 4, inst,
"unsupported mangle instr"););
walk->unsupported_mangle = true;
}
}
}
static bool
translate_walk_good_state(dcontext_t *tdcontext, translate_walk_t *walk,
app_pc translate_pc)
{
return (!walk->unsupported_mangle ||
/* If we're at the instr AFTER the mangle region, or at an instruction
* in the mangled region's EPILOGUE, we're ok.
*/
(walk->in_mangle_region && translate_pc != walk->translation));
}
static void
translate_walk_restore(dcontext_t *tdcontext, translate_walk_t *walk, instr_t *inst,
app_pc translate_pc)
{
reg_id_t r;
if (IF_X86_ELSE(translate_walk_enters_mangling_epilogue(tdcontext, inst, walk),
false)) {
/* We handle only simple symmetric one-spill/one-restore mangling cases
* when xl8 inst addresses in mangling epilogue. Everything else is
* currently not supported. In this case, the restore routine here acts
* as if it was emulating the epilogue instructions, because we xl8 the
* PC post-app instruction. This is semantically different from restoring
* the state pre-app instruction, as this routine originally intended.
* This works, because only the simple spill-restore mangle case is
* supported (xref i#3307). For more complex cases, this should get factored
* out into a separate routine that walks the epilogue and advances the state
* accordingly.
*/
LOG(THREAD_GET, LOG_INTERP, 2,
"\ttranslation " PFX " is in mangling epilogue " PFX
" checking for simple symmetric mangling case\n",
translate_pc, walk->translation);
DOCHECK(1, {
bool spill_seen = false;
for (r = 0; r < REG_SPILL_NUM; r++) {
if (walk->reg_spill_offs[r] != UINT_MAX) {
ASSERT_NOT_IMPLEMENTED(!spill_seen);
spill_seen = true;
}
}
bool tls;
bool spill;
uint offs;
if (instr_is_reg_spill_or_restore(tdcontext, inst, &tls, &spill, NULL,
&offs)) {
ASSERT_NOT_IMPLEMENTED(!spill);
} else if (!tls || offs == -1 ||
offs != os_tls_offset((ushort)MANGLE_RIPREL_SPILL_SLOT)) {
/* Riprel mangling can put arbitrary registers into
* MANGLE_RIPREL_SPILL_SLOT and as such is not recognized as regular
* spill/restore by instr_is_reg_spill_or_restore. Either way, we don't
* support cases that are more complex than one spill and restore in this
* context if instruction was part of mangling epilogue.
*/
ASSERT_NOT_IMPLEMENTED(false);
}
/* Enforcing here what mangling needs to obey. */
ASSERT_NOT_IMPLEMENTED(walk->xsp_adjust == 0);
});
} else if (translate_pc != walk->translation) {
/* When we walk we update only each instr we pass. If we're
* now sitting at the instr AFTER the mangle region, we do
* NOT want to adjust xsp, since we're not translating to
* before that instr. We should not have any outstanding spills.
*/
LOG(THREAD_GET, LOG_INTERP, 2,
"\ttranslation " PFX " is post-walk " PFX " so not fixing xsp\n",
translate_pc, walk->translation);
DOCHECK(1, {
/* Assumes all spills are matched by the same number of restores. This
* assumption may not hold for more complex mangling.
*/
for (r = 0; r < REG_SPILL_NUM; r++)
ASSERT(walk->reg_spill_offs[r] ==
UINT_MAX
/* Register X0 is used for branches on AArch64.
* See mangle_cbr_stolen_reg.
*/
IF_AARCH64(|| r + REG_START_SPILL == DR_REG_X0)
/* The special stolen register mangling from
* mangle_syscall_arch() for a non-restartable syscall ends
* up here due to the nop having a xl8 post-syscall.
* We do need to restore that spill.
*/
IF_AARCHXX(|| r + REG_START_SPILL == dr_reg_stolen));
});
return;
}
/* PR 263407: restore register values that are currently in spill slots
* for ind branches or rip-rel mangling.
* FIXME: for rip-rel loads, we may have clobbered the destination
* already, and won't be able to restore it: but that's a minor issue.
*/
for (r = 0; r < REG_SPILL_NUM; r++) {
if (walk->reg_spill_offs[r] != UINT_MAX) {
reg_id_t reg = r + REG_START_SPILL;
reg_t value;
if (walk->reg_tls[r]) {
value =
*(reg_t *)(((byte *)&tdcontext->local_state->spill_space) +
os_local_state_offset((ushort)walk->reg_spill_offs[r]));
} else {
value = reg_get_value_priv(reg, get_mcontext(tdcontext));
}
LOG(THREAD_GET, LOG_INTERP, 2, "\trestoring spilled %s to " PFX "\n",
reg_names[reg], value);
STATS_INC(recreate_spill_restores);
reg_set_value_priv(reg, walk->mc, value);
}
}
/* PR 267260: Restore stack-adjust mangling of ctis.
* FIXME: we do NOT undo writes to the stack, so we're not completely
* transparent. If we ever do restore memory, we'll want to pass in
* the restore_memory param.
*/
if (walk->xsp_adjust != 0) {
walk->mc->xsp -= walk->xsp_adjust; /* negate to undo */
LOG(THREAD_GET, LOG_INTERP, 2, "\tundoing push/pop by %d: xsp now " PFX "\n",
walk->xsp_adjust, walk->mc->xsp);
}
}
static void
translate_restore_clean_call(dcontext_t *tdcontext, translate_walk_t *walk)
{
/* PR 302951: we recognize a clean call by its combination of
* our-mangling and NULL translation.
* We restore to the priv_mcontext_t that was pushed on the stack.
* FIXME i#4219: This is not safe: see comment below.
*/
LOG(THREAD_GET, LOG_INTERP, 2, "\ttranslating clean call arg crash\n");
dr_get_mcontext_priv(tdcontext, NULL, walk->mc);
/* walk->mc->pc will be fixed up by caller */
/* PR 306410: up to caller to shift signal or SEH frame from dstack
* to app stack. We naturally do that already for linux b/c we always
* have an alternate signal handling stack, but for Windows it takes
* extra work.
*/
}
static app_pc
translate_restore_special_cases(dcontext_t *dcontext, app_pc pc)
{
#ifdef LINUX
app_pc handler;
if (rseq_get_region_info(pc, NULL, NULL, &handler, NULL, NULL)) {
LOG(THREAD_GET, LOG_INTERP, 2,
"recreate_app: moving " PFX " inside rseq region to handler " PFX "\n", pc,
handler);
/* Remember the original for translate_last_direct_translation. */
IF_CLIENT_INTERFACE(dcontext->client_data->last_special_xl8 = pc);
return handler;
}
IF_CLIENT_INTERFACE(dcontext->client_data->last_special_xl8 = NULL);
#endif
return pc;
}
#ifdef CLIENT_INTERFACE /* i#2971: Cleanup: remove this define! */
app_pc
translate_last_direct_translation(dcontext_t *dcontext, app_pc pc)
{
# ifdef LINUX
app_pc handler;
if (dcontext->client_data->last_special_xl8 != NULL &&
rseq_get_region_info(dcontext->client_data->last_special_xl8, NULL, NULL,
&handler, NULL, NULL) &&
pc == handler)
return dcontext->client_data->last_special_xl8;
# endif
return pc;
}
#endif
/* Returns a success code, but makes a best effort regardless.
* If just_pc is true, only recreates pc.
* Modifies mc with the recreated state.
* The caller must ensure tdcontext remains valid.
*/
/* Use THREAD_GET instead of THREAD so log messages go to calling thread */
static recreate_success_t
recreate_app_state_from_info(dcontext_t *tdcontext, const translation_info_t *info,
byte *start_cache, byte *end_cache, priv_mcontext_t *mc,
bool just_pc _IF_DEBUG(uint flags))
{
byte *answer = NULL;
byte *cpc, *prev_cpc;
cache_pc target_cache = mc->pc;
uint i;
bool contig = true, ours = false;
recreate_success_t res = (just_pc ? RECREATE_SUCCESS_PC : RECREATE_SUCCESS_STATE);
instr_t instr;
translate_walk_t walk;
translate_walk_init(&walk, start_cache, end_cache, mc);
instr_init(tdcontext, &instr);
ASSERT(info != NULL);
ASSERT(end_cache >= start_cache);
LOG(THREAD_GET, LOG_INTERP, 3,
"recreate_app : looking for " PFX " in frag @ " PFX " (tag " PFX ")\n",
target_cache, start_cache, info->translation[0].app);
DOLOG(3, LOG_INTERP, { translation_info_print(info, start_cache, THREAD_GET); });
/* Strategy: walk through cache instrs, updating current app translation
* as we go along from the info table. The table records only
* translations at change points and must interpolate between them, using
* either a stride of 0 if the previous translation entry is marked
* "identical" or a stride equal to the instruction length as we decode
* from the cache if the previous entry is !identical=="contiguous".
*/
cpc = start_cache;
ASSERT(cpc - start_cache == info->translation[0].cache_offs);
i = 0;
while (cpc < end_cache) {
/* we can go beyond the end of the table: then use the last point */
if (i < info->num_entries &&
cpc - start_cache >= info->translation[i].cache_offs) {
/* We hit a change point: new app translation target */
answer = info->translation[i].app;
contig = !TEST(TRANSLATE_IDENTICAL, info->translation[i].flags);
ours = TEST(TRANSLATE_OUR_MANGLING, info->translation[i].flags);
i++;
}
if (cpc >= target_cache) {
/* we found the target to translate */
ASSERT(cpc == target_cache);
if (cpc > target_cache) { /* in debug will hit assert 1st */
LOG(THREAD_GET, LOG_INTERP, 2,
"recreate_app -- WARNING: cache pc " PFX " != " PFX "\n", cpc,
target_cache);
res = RECREATE_FAILURE; /* try to restore, but return false */
}
break;
}
/* PR 263407/PR 268372: we need to decode to instr level to track register
* values that we've spilled, and watch for ctis. So far we don't need
* enough to justify a full decode_fragment().
*/
instr_reset(tdcontext, &instr);
prev_cpc = cpc;
cpc = decode(tdcontext, cpc, &instr);
if (cpc == NULL) {
LOG(THREAD_GET, LOG_INTERP, 2,
"recreate_app -- failed to decode cache pc " PFX "\n", cpc);
ASSERT_NOT_REACHED();
instr_free(tdcontext, &instr);
return RECREATE_FAILURE;
}
instr_set_our_mangling(&instr, ours);
/* Sets the translation so that spilled registers can be restored. */
instr_set_translation(&instr, answer);
translate_walk_track(tdcontext, &instr, &walk);
/* advance translation by the stride: either instr length or 0 */
if (contig)
answer += (cpc - prev_cpc);
/* else, answer stays put */
}
/* should always find xlation */
ASSERT(cpc < end_cache);
instr_free(tdcontext, &instr);
if (answer == NULL || !translate_walk_good_state(tdcontext, &walk, answer)) {
/* PR 214962: we're either in client meta-code (NULL translation) or
* post-app-fault in our own manglings: we shouldn't get an app
* fault in either case, so it's ok to fail, and neither is a safe
* spot for thread relocation. For client meta-code we could split
* synch view (since we can get the app state consistent, just not
* the client state) from synch relocate, but that would require
* synchall re-architecting and may not be a noticeable perf win
* (should spend enough time at syscalls that will hit safe spot in
* reasonable time).
*/
/* PR 302951: our clean calls do show up here and have full state.
* FIXME i#4219: Actually we do *not* always have full state: for asynch
* xl8 we could be before setup or after teardown of the mcontext on the
* dstack, and with leaner clean calls we might not have the full mcontext.
*/
if (answer == NULL && ours)
translate_restore_clean_call(tdcontext, &walk);
else
res = RECREATE_SUCCESS_PC; /* failed on full state, but pc good */
/* should only happen for thread synch, not a fault */
DOCHECK(1, {
if (!(res == RECREATE_SUCCESS_STATE /* clean call */ ||
tdcontext != get_thread_private_dcontext() ||
INTERNAL_OPTION(stress_recreate_pc) ||
/* we can currently fail for flushed code (PR 208037/i#399)
* (and hotpatch, native_exec, and sysenter: but too rare to check) */
TEST(FRAG_SELFMOD_SANDBOXED, flags) || TEST(FRAG_WAS_DELETED, flags))) {
CLIENT_ASSERT(false,
"meta-instr faulted? must set translation"
" field and handle fault!");
}
});
if (answer == NULL) {
/* use next instr's translation. skip any further meta-instrs regions. */
for (; i < info->num_entries; i++) {
if (info->translation[i].app != NULL)
break;
}
ASSERT(i < info->num_entries);
if (i < info->num_entries)
answer = info->translation[i].app;
;
ASSERT(answer != NULL);
}
}
if (!just_pc)
translate_walk_restore(tdcontext, &walk, &instr, answer);
answer = translate_restore_special_cases(tdcontext, answer);
LOG(THREAD_GET, LOG_INTERP, 2, "recreate_app -- found ok pc " PFX "\n", answer);
mc->pc = answer;
return res;
}
/* Returns a success code, but makes a best effort regardless.
* If just_pc is true, only recreates pc.
* Modifies mc with the recreated state.
* The caller must ensure tdcontext remains valid.
*/
/* Use THREAD_GET instead of THREAD so log messages go to calling thread */
static recreate_success_t
recreate_app_state_from_ilist(dcontext_t *tdcontext, instrlist_t *ilist, byte *start_app,
byte *start_cache, byte *end_cache, priv_mcontext_t *mc,
bool just_pc, uint flags)
{
byte *answer = NULL;
byte *cpc, *prev_bytes;
instr_t *inst, *prev_ok;
cache_pc target_cache = mc->pc;
recreate_success_t res = (just_pc ? RECREATE_SUCCESS_PC : RECREATE_SUCCESS_STATE);
translate_walk_t walk;
LOG(THREAD_GET, LOG_INTERP, 3,
"recreate_app : looking for " PFX " in frag @ " PFX " (tag " PFX ")\n",
target_cache, start_cache, start_app);
DOLOG(5, LOG_INTERP, { instrlist_disassemble(tdcontext, 0, ilist, THREAD_GET); });
/* walk ilist, incrementing cache pc by each instr's length until
* cache pc equals target, then look at original address of
* current instr, which is set by routines in mangle except for
* cti_short_rewrite.
*/
cpc = start_cache;
/* since asking for the length will encode to a buffer, we cannot
* walk backwards at all. thus we keep track of the previous instr
* with valid original bytes.
*/
prev_ok = NULL;
prev_bytes = NULL;
translate_walk_init(&walk, start_cache, end_cache, mc);
for (inst = instrlist_first(ilist); inst; inst = instr_get_next(inst)) {
int len = instr_length(tdcontext, inst);
/* All we care about is that we are not going to skip over a
* bundle of app instructions.
*/
ASSERT(!instr_is_level_0(inst));
/* Case 4531, 4344: raw instructions being up-decoded can have
* their translation fields clobbered so we don't want any of those.
* (We used to have raw jecxz and nop instrs.)
* FIXME: if bb associated with this instr was hot patched, then
* the inserted raw instructions can trigger this assert. Part of
* fix for case 5981. In that case, this would be harmless.
*/
ASSERT_CURIOSITY(instr_operands_valid(inst));
/* PR 332437: skip label instrs. Nobody should expect setting
* a label's translation field to have any effect, and we
* don't need to explicitly split our mangling regions at
* labels so no reason to call translate_walk_track().
*
* We also skip all other length 0 instrs. That would
* include un-encodable instrs, which we wouldn't have output,
* and so we should skip here in case the very next instr that we
* did encode had the real fault.
*/
if (len == 0)
continue;
/* note this will be exercised for all instructions up to the answer */
#ifndef CLIENT_INTERFACE
# ifdef INTERNAL
ASSERT(instr_get_translation(inst) != NULL || DYNAMO_OPTION(optimize));
# else
ASSERT(instr_get_translation(inst) != NULL);
# endif
#endif
LOG(THREAD_GET, LOG_INTERP, 5, "cache pc " PFX " vs " PFX "\n", cpc,
target_cache);
if (cpc >= target_cache) {
if (cpc > target_cache) {
if (cpc == start_cache) {
/* Prefix instructions are not added to recreate_fragment_ilist()
* FIXME: we should do so, and then we can at least restore
* our spills, just in case.
*/
LOG(THREAD_GET, LOG_INTERP, 2,
"recreate_app -- cache pc " PFX " != " PFX ", "
"assuming a prefix instruction\n",
cpc, target_cache);
res = RECREATE_SUCCESS_PC; /* failed on full state, but pc good */
/* Should only happen for thread synch, not a fault. Checking whether
* tdcontext is the same as this thread's private dcontext is a weak
* indicator of xl8 due to a fault. */
ASSERT_CURIOSITY(tdcontext != get_thread_private_dcontext() ||
INTERNAL_OPTION(stress_recreate_pc));
} else {
LOG(THREAD_GET, LOG_INTERP, 2,
"recreate_app -- WARNING: cache pc " PFX " != " PFX ", "
"probably prefix instruction\n",
cpc, target_cache);
res = RECREATE_FAILURE; /* try to restore, but return false */
}
}
if (instr_get_translation(inst) == NULL) {
/* Clients are supposed to leave their meta instrs with
* NULL translations. (DR may hit this assert for
* -optimize but we need to fix that by setting translation
* for all our optimizations.) We assume we will never
* get an app fault here, so we fail if asked for full state
* since although we can get full app state we can't relocate
* in the middle of client meta code.
*/
ASSERT(instr_is_meta(inst));
/* PR 302951: our clean calls do show up here and have full state.
* FIXME i#4219: This is not safe: see comment above.
*/
if (instr_is_our_mangling(inst))
translate_restore_clean_call(tdcontext, &walk);
else
res = RECREATE_SUCCESS_PC; /* failed on full state, but pc good */
/* should only happen for thread synch, not a fault */
DOCHECK(1, {
if (!(instr_is_our_mangling(inst) /* PR 302951 */ ||
tdcontext != get_thread_private_dcontext() ||
INTERNAL_OPTION(stress_recreate_pc) IF_CLIENT_INTERFACE(
|| tdcontext->client_data->is_translating))) {
CLIENT_ASSERT(false,
"meta-instr faulted? must set translation "
"field and handle fault!");
}
});
if (prev_ok == NULL) {
answer = start_app;
LOG(THREAD_GET, LOG_INTERP, 2,
"recreate_app -- WARNING: guessing start pc " PFX "\n", answer);
} else {
answer = prev_bytes;
LOG(THREAD_GET, LOG_INTERP, 2,
"recreate_app -- WARNING: guessing after prev "
"translation (pc " PFX ")\n",
answer);
DOLOG(2, LOG_INTERP,
d_r_loginst(get_thread_private_dcontext(), 2, prev_ok,
"\tprev instr"););
}
} else {
answer = instr_get_translation(inst);
if (translate_walk_good_state(tdcontext, &walk, answer)) {
LOG(THREAD_GET, LOG_INTERP, 2,
"recreate_app -- found valid state pc " PFX "\n", answer);
} else {
#ifdef X86
int op = instr_get_opcode(inst);
if (TEST(FRAG_SELFMOD_SANDBOXED, flags) &&
(op == OP_rep_ins || op == OP_rep_movs || op == OP_rep_stos)) {
/* i#398: xl8 selfmod: rep string instrs have xbx spilled in
* thread-private slot. We assume no other selfmod mangling
* has a reg spilled at time of app instr execution.
*/
if (!just_pc) {
walk.mc->xbx = get_mcontext(tdcontext)->xbx;
LOG(THREAD_GET, LOG_INTERP, 2,
"\trestoring spilled xbx to " PFX "\n", walk.mc->xbx);
STATS_INC(recreate_spill_restores);
}
LOG(THREAD_GET, LOG_INTERP, 2,
"recreate_app -- found valid state pc " PFX "\n", answer);
} else
#endif /* X86 */
{
res = RECREATE_SUCCESS_PC; /* failed on full state, but pc good */
/* should only happen for thread synch, not a fault */
ASSERT(tdcontext != get_thread_private_dcontext() ||
INTERNAL_OPTION(stress_recreate_pc) ||
/* we can currently fail for flushed code (PR 208037)
* (and hotpatch, native_exec, and sysenter: but too
* rare to check) */
TEST(FRAG_SELFMOD_SANDBOXED, flags) ||
TEST(FRAG_WAS_DELETED, flags));
LOG(THREAD_GET, LOG_INTERP, 2,
"recreate_app -- not able to fully recreate "
"context, pc is in added instruction from mangling\n");
}
}
}
if (!just_pc)
translate_walk_restore(tdcontext, &walk, inst, answer);
answer = translate_restore_special_cases(tdcontext, answer);
LOG(THREAD_GET, LOG_INTERP, 2, "recreate_app -- found ok pc " PFX "\n",
answer);
mc->pc = answer;
return res;
}
/* we only use translation pointers, never just raw bit pointers */
if (instr_get_translation(inst) != NULL) {
prev_ok = inst;
DOLOG(5, LOG_INTERP,
d_r_loginst(get_thread_private_dcontext(), 5, prev_ok, "\tok instr"););
prev_bytes = instr_get_translation(inst);
if (instr_is_app(inst)) {
/* we really want the pc after the translation target since we'll
* use this if we pass up the target without hitting it:
* unless this is a meta instr in which case we assume the
* real instr is ahead (FIXME: there could be cases where
* we want the opposite: how know?)
*/
/* FIXME: do we need to check for readability first?
* in normal usage all translation targets should have been decoded
* already while building the bb ilist
*/
prev_bytes = decode_next_pc(tdcontext, prev_bytes);
}
}
translate_walk_track(tdcontext, inst, &walk);
cpc += len;
}