This repository was archived by the owner on Apr 16, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMisc.cpp
More file actions
1237 lines (980 loc) · 44.6 KB
/
Copy pathMisc.cpp
File metadata and controls
1237 lines (980 loc) · 44.6 KB
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) 2014, Trail of Bits
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 Trail of Bits 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 THE COPYRIGHT HOLDER 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.
*/
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <llvm/IR/Argument.h>
#include <llvm/IR/BasicBlock.h>
#include <llvm/IR/Function.h>
#include <llvm/IR/Instructions.h>
#include <llvm/IR/IntrinsicInst.h>
#include <llvm/IR/Intrinsics.h>
#include <llvm/MC/MCInst.h>
#include <llvm/Support/CodeGen.h>
#include <llvm/Support/Debug.h>
#include "mcsema/Arch/Arch.h"
#include "mcsema/Arch/Dispatch.h"
#include "mcsema/Arch/Register.h"
#include "mcsema/Arch/X86/Util.h"
#include "mcsema/Arch/X86/Semantics/MOV.h"
#include "mcsema/Arch/X86/Semantics/flagops.h"
#include "mcsema/Arch/X86/Semantics/Misc.h"
#include "mcsema/BC/Util.h"
static InstTransResult doNoop(llvm::BasicBlock *b) {
//isn't this exciting
return ContinueBlock;
}
static InstTransResult doHlt(llvm::BasicBlock *b) {
//isn't this exciting
std::cerr << "WARNING: Treating HLT as no-op, but HLT is normally privileged"
<< std::endl;
return ContinueBlock;
}
static InstTransResult doInt3(llvm::BasicBlock *b) {
auto M = b->getParent()->getParent();
//emit an LLVM trap intrinsic
//this should be changed to a debugtrap intrinsic eventually
auto trapIntrin = llvm::Intrinsic::getDeclaration(M, llvm::Intrinsic::trap);
llvm::CallInst::Create(trapIntrin, "", b);
(void) new llvm::UnreachableInst(b->getContext(), b);
return ContinueBlock;
}
static InstTransResult doTrap(llvm::BasicBlock *b) {
auto M = b->getParent()->getParent();
auto trapIntrin = llvm::Intrinsic::getDeclaration(M, llvm::Intrinsic::trap);
llvm::CallInst::Create(trapIntrin, "", b);
(void) new llvm::UnreachableInst(b->getContext(), b);
return ContinueBlock;
}
static InstTransResult doInt(llvm::BasicBlock *&b, const llvm::MCOperand &o) {
TASSERT(o.isImm(), "Operand not immediate");
auto F = b->getParent();
auto M = F->getParent();
auto &C = M->getContext();
int interrupt_val = o.getImm();
auto os = SystemOS(M);
if (0x2e == interrupt_val && llvm::Triple::Win32 == os) {
TASSERT(false, "System call via interrupt is not supported!");
}
if (0x80 == interrupt_val && llvm::Triple::Linux == os) {
TASSERT(32 == ArchAddressSize(),
"int 0x80 syscall not supported on 64-bit.");
llvm::Type *arg_tys[] = {llvm::Type::getInt32Ty(C)};
auto syscall_func_ty = llvm::FunctionType::get(
llvm::Type::getInt32Ty(C), arg_tys, true /* IsVarArg */);
auto syscall_func = M->getOrInsertFunction("syscall", syscall_func_ty);
std::vector<llvm::Value *> args = {
R_READ<32>(b, llvm::X86::EAX), // syscall num
R_READ<32>(b, llvm::X86::EBX),
R_READ<32>(b, llvm::X86::ECX),
R_READ<32>(b, llvm::X86::EDX),
R_READ<32>(b, llvm::X86::ESI),
R_READ<32>(b, llvm::X86::EDI),
R_READ<32>(b, llvm::X86::EBP),
};
auto ret = llvm::CallInst::Create(syscall_func, args, "", b);
R_WRITE<32>(b, llvm::X86::EAX, ret);
return ContinueBlock;
}
std::cerr << "WARNING: Treating INT " << interrupt_val << " as trap!"
<< std::endl;
return doTrap(b);
}
static InstTransResult doCdq(llvm::BasicBlock *b) {
// EDX <- SEXT(EAX)
//read EAX
auto EAX_v = R_READ<32>(b, llvm::X86::EAX);
auto sign_bit = CONST_V<32>(b, 1ULL << 31U);
auto test_bit = llvm::BinaryOperator::CreateAnd(EAX_v, sign_bit, "", b);
auto is_zero = new llvm::ICmpInst( *b, llvm::CmpInst::ICMP_EQ, test_bit,
CONST_V<32>(b, 0));
auto edx_val = llvm::SelectInst::Create(is_zero, CONST_V<32>(b, 0),
CONST_V<32>(b, 0xFFFFFFFF), "", b);
//write this value to EDX
R_WRITE<32>(b, llvm::X86::EDX, edx_val);
return ContinueBlock;
}
template<int width>
static InstTransResult doBswapR(NativeInstPtr ip, llvm::BasicBlock *&b,
const llvm::MCOperand ®) {
TASSERT(reg.isReg(), "");
auto &C = b->getContext();
auto M = b->getModule();
auto intNTy = llvm::Type::getIntNTy(C, width);
auto bswapFTy = llvm::FunctionType::get(intNTy, {intNTy}, false);
std::stringstream ss;
ss << "llvm.bswap.i" << width;
auto bswapF = llvm::dyn_cast<llvm::Function>(
M->getOrInsertFunction(ss.str(), bswapFTy));
if (llvm::Intrinsic::bswap != bswapF->getIntrinsicID()) {
bswapF->recalculateIntrinsicID();
}
auto tmp = R_READ<width>(b, reg.getReg());
auto res = llvm::CallInst::Create(bswapF, {tmp}, "", b);
R_WRITE<width>(b, reg.getReg(), res);
return ContinueBlock;
}
static InstTransResult doLAHF(llvm::BasicBlock *b) {
//we need to create an 8-bit value out of the status
//flags, shift and OR them, and then write them into AH
auto cf = F_READ(b, llvm::X86::CF);
auto af = F_READ(b, llvm::X86::AF);
auto pf = F_READ(b, llvm::X86::PF);
auto zf = F_READ(b, llvm::X86::ZF);
auto sf = F_READ(b, llvm::X86::SF);
//shift everything
auto p_0 = cf;
auto p_1 = llvm::BinaryOperator::CreateShl(CONST_V<8>(b, 1), CONST_V<8>(b, 1),
"", b);
auto p_2 = llvm::BinaryOperator::CreateShl(pf, CONST_V<8>(b, 2), "", b);
auto p_3 = llvm::BinaryOperator::CreateShl(CONST_V<8>(b, 0), CONST_V<8>(b, 3),
"", b);
auto p_4 = llvm::BinaryOperator::CreateShl(af, CONST_V<8>(b, 4), "", b);
auto p_5 = llvm::BinaryOperator::CreateShl(CONST_V<8>(b, 0), CONST_V<8>(b, 5),
"", b);
auto p_6 = llvm::BinaryOperator::CreateShl(zf, CONST_V<8>(b, 6), "", b);
auto p_7 = llvm::BinaryOperator::CreateShl(sf, CONST_V<8>(b, 7), "", b);
//OR everything
auto res = llvm::BinaryOperator::CreateOr(
llvm::BinaryOperator::CreateOr(
llvm::BinaryOperator::CreateOr(
llvm::BinaryOperator::CreateOr(p_0, p_1, "", b), p_2, "", b),
p_3, "", b),
llvm::BinaryOperator::CreateOr(
llvm::BinaryOperator::CreateOr(
llvm::BinaryOperator::CreateOr(p_4, p_5, "", b), p_6, "", b),
p_7, "", b),
"", b);
R_WRITE<8>(b, llvm::X86::AH, res);
return ContinueBlock;
}
static InstTransResult doStd(llvm::BasicBlock *b) {
F_SET(b, llvm::X86::DF);
return ContinueBlock;
}
static InstTransResult doCld(llvm::BasicBlock *b) {
F_CLEAR(b, llvm::X86::DF);
return ContinueBlock;
}
static InstTransResult doStc(llvm::BasicBlock *b) {
F_SET(b, llvm::X86::CF);
return ContinueBlock;
}
static InstTransResult doClc(llvm::BasicBlock *b) {
F_CLEAR(b, llvm::X86::CF);
return ContinueBlock;
}
template<int width>
static InstTransResult doLeaV(llvm::BasicBlock *&b, const llvm::MCOperand &dst,
llvm::Value *addrInt) {
//write the address into the register
R_WRITE<width>(b, dst.getReg(), addrInt);
return ContinueBlock;
}
template<int width>
static InstTransResult doLea(NativeInstPtr ip, llvm::BasicBlock *&b,
llvm::Value *addr, const llvm::MCOperand &dst) {
// LEA <r>, <expr>
TASSERT(addr != NULL, "");
TASSERT(dst.isReg(), "");
//addr is an address, so, convert it to an integer value to write
auto ty = llvm::Type::getIntNTy(b->getContext(), width);
auto addrInt = addr;
if (addr->getType()->isPointerTy()) {
addrInt = new llvm::PtrToIntInst(addr, ty, "", b);
}
return doLeaV<width>(b, dst, addrInt);
}
static InstTransResult doRdtsc(llvm::BasicBlock *b) {
/* write out a call to the RDTSC intrinsic */
auto M = b->getParent()->getParent();
auto &C = M->getContext();
//emit an LLVM trap intrinsic
//this should be changed to a debugtrap intrinsic eventually
auto rcc = llvm::Intrinsic::getDeclaration(M,
llvm::Intrinsic::readcyclecounter);
auto ret = llvm::CallInst::Create(rcc, "", b);
auto Int32Ty = llvm::Type::getInt32Ty(C);
auto low = new llvm::TruncInst(ret, Int32Ty, "", b);
auto high = new llvm::TruncInst(
llvm::BinaryOperator::Create(llvm::Instruction::LShr, ret,
llvm::ConstantInt::get(ret->getType(), 32), "", b),
Int32Ty, "", b);
R_WRITE<32>(b, llvm::X86::EDX, high);
R_WRITE<32>(b, llvm::X86::EAX, low);
return ContinueBlock;
}
static InstTransResult doAAA(llvm::BasicBlock *b) {
auto F = b->getParent();
auto &C = F->getContext();
//trueBlock for when ((AL & 0x0F > 9) || (AF == 1)); falseblock otherwise
auto trueBlock = llvm::BasicBlock::Create(C, "", F);
auto falseBlock = llvm::BasicBlock::Create(C, "", F);
auto endBlock = llvm::BasicBlock::Create(C, "", F);
llvm::Value *al = nullptr;
llvm::Value *af = nullptr;
al = R_READ<8>(b, llvm::X86::AL);
af = F_READ(b, llvm::X86::AF);
// AL & 0x0F
auto andRes = llvm::BinaryOperator::CreateAnd(al, CONST_V<8>(b, 0x0F), "", b);
// ((AL & 0x0F) > 9)?
auto testRes = new llvm::ICmpInst( *b, llvm::CmpInst::ICMP_UGT, andRes,
CONST_V<8>(b, 9));
auto orRes = llvm::BinaryOperator::CreateOr(testRes, af, "", b);
llvm::BranchInst::Create(trueBlock, falseBlock, orRes, b);
//True Block Statements
llvm::Value *alRes = llvm::BinaryOperator::CreateAdd(al,
CONST_V<8>(trueBlock, 6),
"", trueBlock);
R_WRITE<8>(trueBlock, llvm::X86::AL, alRes);
auto ahRes = llvm::BinaryOperator::CreateAdd(
R_READ<8>(trueBlock, llvm::X86::AH), CONST_V<8>(trueBlock, 1), "",
trueBlock);
R_WRITE<8>(trueBlock, llvm::X86::AH, ahRes);
F_SET(trueBlock, llvm::X86::AF);
F_SET(trueBlock, llvm::X86::CF);
alRes = llvm::BinaryOperator::CreateAnd(alRes, CONST_V<8>(trueBlock, 0x0F),
"", trueBlock);
R_WRITE<8>(trueBlock, llvm::X86::AL, alRes);
llvm::BranchInst::Create(endBlock, trueBlock);
//False Block Statements
F_CLEAR(falseBlock, llvm::X86::AF);
F_CLEAR(falseBlock, llvm::X86::CF);
alRes = llvm::BinaryOperator::CreateAnd(al, CONST_V<8>(trueBlock, 0x0F), "",
falseBlock);
R_WRITE<8>(falseBlock, llvm::X86::AL, alRes);
llvm::BranchInst::Create(endBlock, falseBlock);
F_ZAP(endBlock, llvm::X86::OF);
F_ZAP(endBlock, llvm::X86::SF);
F_ZAP(endBlock, llvm::X86::ZF);
F_ZAP(endBlock, llvm::X86::PF);
//update our parents concept of what the current block is
b = endBlock;
return ContinueBlock;
}
static InstTransResult doAAS(llvm::BasicBlock *b) {
auto F = b->getParent();
auto &C = F->getContext();
//trueBlock for when ((AL & 0x0F > 9) || (AF == 1)); falseblock otherwise
auto trueBlock = llvm::BasicBlock::Create(C, "", F);
auto falseBlock = llvm::BasicBlock::Create(C, "", F);
auto endBlock = llvm::BasicBlock::Create(C, "", F);
llvm::Value *al = nullptr;
llvm::Value *af = nullptr;
al = R_READ<8>(b, llvm::X86::AL);
af = F_READ(b, llvm::X86::AF);
// AL & 0x0F
llvm::Value *andRes = llvm::BinaryOperator::CreateAnd(al, CONST_V<8>(b, 0x0F),
"", b);
// ((AL & 0x0F) > 9)?
llvm::Value *testRes = new llvm::ICmpInst( *b, llvm::CmpInst::ICMP_UGT,
andRes, CONST_V<8>(b, 9));
llvm::Value *orRes = llvm::BinaryOperator::CreateOr(testRes, af, "", b);
llvm::BranchInst::Create(trueBlock, falseBlock, orRes, b);
//True Block Statements
llvm::Value *alRes = llvm::BinaryOperator::CreateSub(al,
CONST_V<8>(trueBlock, 6),
"", trueBlock);
R_WRITE<8>(trueBlock, llvm::X86::AL, alRes);
llvm::Value *ahRes = llvm::BinaryOperator::CreateSub(
R_READ<8>(trueBlock, llvm::X86::AH), CONST_V<8>(trueBlock, 1), "",
trueBlock);
R_WRITE<8>(trueBlock, llvm::X86::AH, ahRes);
F_SET(trueBlock, llvm::X86::AF);
F_SET(trueBlock, llvm::X86::CF);
alRes = llvm::BinaryOperator::CreateAnd(alRes, CONST_V<8>(trueBlock, 0x0F),
"", trueBlock);
R_WRITE<8>(trueBlock, llvm::X86::AL, alRes);
llvm::BranchInst::Create(endBlock, trueBlock);
//False Block Statements
F_CLEAR(falseBlock, llvm::X86::AF);
F_CLEAR(falseBlock, llvm::X86::CF);
alRes = llvm::BinaryOperator::CreateAnd(al, CONST_V<8>(trueBlock, 0x0F), "",
falseBlock);
R_WRITE<8>(falseBlock, llvm::X86::AL, alRes);
llvm::BranchInst::Create(endBlock, falseBlock);
F_ZAP(endBlock, llvm::X86::OF);
F_ZAP(endBlock, llvm::X86::SF);
F_ZAP(endBlock, llvm::X86::ZF);
F_ZAP(endBlock, llvm::X86::PF);
//update our parents concept of what the current block is
b = endBlock;
return ContinueBlock;
}
static InstTransResult doAAM(llvm::BasicBlock *b) {
llvm::Value *al = nullptr;
al = R_READ<8>(b, llvm::X86::AL);
llvm::Value *res = llvm::BinaryOperator::Create(llvm::Instruction::SDiv, al,
CONST_V<8>(b, 0x0A), "", b);
llvm::Value *mod = llvm::BinaryOperator::Create(llvm::Instruction::SRem, al,
CONST_V<8>(b, 0x0A), "", b);
R_WRITE<8>(b, llvm::X86::AL, mod);
R_WRITE<8>(b, llvm::X86::AH, res);
WriteSF<8>(b, mod);
WriteZF<8>(b, mod);
WritePF<8>(b, mod);
F_ZAP(b, llvm::X86::OF);
F_ZAP(b, llvm::X86::AF);
F_ZAP(b, llvm::X86::CF);
return ContinueBlock;
}
static InstTransResult doAAD(llvm::BasicBlock *b) {
llvm::Value *al = nullptr;
llvm::Value *ah = nullptr;
al = R_READ<8>(b, llvm::X86::AL);
ah = R_READ<8>(b, llvm::X86::AH);
llvm::Value *tmp = llvm::BinaryOperator::Create(llvm::Instruction::Mul, ah,
CONST_V<8>(b, 0x0A), "", b);
tmp = llvm::BinaryOperator::CreateAdd(tmp, al, "", b);
tmp = llvm::BinaryOperator::CreateAnd(tmp, CONST_V<8>(b, 0xFF), "", b);
R_WRITE<8>(b, llvm::X86::AL, tmp);
R_WRITE<8>(b, llvm::X86::AH, CONST_V<8>(b, 0x00));
WriteSF<8>(b, tmp);
WriteZF<8>(b, tmp);
WritePF<8>(b, tmp);
F_ZAP(b, llvm::X86::OF);
F_ZAP(b, llvm::X86::AF);
F_ZAP(b, llvm::X86::CF);
return ContinueBlock;
}
template<int width>
static InstTransResult doCwd(llvm::BasicBlock *b) {
// read ax or eax
llvm::Value *ax_val = R_READ<width>(b, llvm::X86::EAX);
// sign extend to twice width
auto dt = llvm::Type::getIntNTy(b->getContext(), width * 2);
auto tmp = new llvm::SExtInst(ax_val, dt, "", b);
// rotate leftmost bits into rightmost
auto t = llvm::Type::getIntNTy(b->getContext(), width);
auto res_sh = llvm::BinaryOperator::Create(llvm::Instruction::LShr, tmp,
CONST_V<width * 2>(b, width), "",
b);
// original rightmost
auto wrAX = new llvm::TruncInst(tmp, t, "", b);
// original leftmost
auto wrDX = new llvm::TruncInst(res_sh, t, "", b);
switch (width) {
case 16:
R_WRITE<width>(b, llvm::X86::DX, wrDX);
R_WRITE<width>(b, llvm::X86::AX, wrAX);
break;
case 32:
R_WRITE<width>(b, llvm::X86::EDX, wrDX);
R_WRITE<width>(b, llvm::X86::EAX, wrAX);
break;
case 64:
R_WRITE<width>(b, llvm::X86::RDX, wrDX);
R_WRITE<width>(b, llvm::X86::RAX, wrAX);
break;
default:
throw TErr(__LINE__, __FILE__, "Not supported width");
}
return ContinueBlock;
}
static InstTransResult translate_SAHF(TranslationContext &ctx,
llvm::BasicBlock *&block) {
auto ah_val = R_READ<8>(block, llvm::X86::AH);
SHR_SET_FLAG<8, 1>(block, ah_val, llvm::X86::CF, 0);
// bit 1 is reserved
SHR_SET_FLAG<8, 1>(block, ah_val, llvm::X86::PF, 2);
// bit 3 is reserved
SHR_SET_FLAG<8, 1>(block, ah_val, llvm::X86::AF, 4);
// bit 5 is reserved
SHR_SET_FLAG<8, 1>(block, ah_val, llvm::X86::ZF, 6);
SHR_SET_FLAG<8, 1>(block, ah_val, llvm::X86::SF, 7);
return ContinueBlock;
}
template<int width>
static InstTransResult doBtmi(NativeInstPtr ip, llvm::BasicBlock *&b,
llvm::Value *base, const llvm::MCOperand &index) {
TASSERT(index.isImm(), "Operand must be an immediate");
int imm = index.getImm();
int bytes_offt = imm / 8;
int whichbit = imm % 8;
if (whichbit < 0) {
// make this always positive
whichbit *= -1;
}
auto addrInt = base;
if (base->getType()->isPointerTy()) {
addrInt = new llvm::PtrToIntInst(
base, llvm::Type::getIntNTy(b->getContext(), width), "", b);
}
// pick which byte we need to bit test
auto new_base = llvm::BinaryOperator::Create(llvm::Instruction::Add, addrInt,
CONST_V<width>(b, bytes_offt),
"", b);
auto base_val = M_READ<8>(ip, b, new_base);
SHR_SET_FLAG_V<8, 1>(b, base_val, llvm::X86::CF, CONST_V<8>(b, whichbit));
return ContinueBlock;
}
template<int width>
static InstTransResult doBtri(llvm::BasicBlock *&b, const llvm::MCOperand &base,
const llvm::MCOperand &index) {
TASSERT(base.isReg(), "Operand must be an immediate");
TASSERT(index.isImm(), "Operand must be an immediate");
unsigned whichbit = index.getImm();
whichbit %= width;
auto base_val = R_READ<width>(b, base.getReg());
SHR_SET_FLAG_V<width, 1>(b, base_val, llvm::X86::CF,
CONST_V<width>(b, whichbit));
return ContinueBlock;
}
template<int width>
static InstTransResult doBtrr(llvm::BasicBlock *&b, const llvm::MCOperand &base,
const llvm::MCOperand &index) {
TASSERT(base.isReg(), "operand must be register");
TASSERT(index.isReg(), "operand must be register");
auto base_val = R_READ<width>(b, base.getReg());
auto index_val = R_READ<width>(b, index.getReg());
// modulo the index by register size
auto index_mod = llvm::BinaryOperator::CreateURem(index_val,
CONST_V<width>(b, width),
"", b);
SHR_SET_FLAG_V<width, 1>(b, base_val, llvm::X86::CF, index_mod);
return ContinueBlock;
}
template<int width>
static InstTransResult doBtmr(NativeInstPtr ip, llvm::BasicBlock *&b,
llvm::Value *base,
const llvm::MCOperand &index) {
TASSERT(index.isReg(), "Operand must be an immediate");
auto index_val = R_READ<width>(b, index.getReg());
auto word_size = CONST_V<width>(b, width);
auto bit = llvm::BinaryOperator::CreateURem(index_val, word_size, "", b);
auto word = llvm::BinaryOperator::CreateUDiv(index_val, word_size, "", b);
auto ptr = ADDR_TO_POINTER<width>(b, base);
auto gep = llvm::GetElementPtrInst::CreateInBounds(ptr, {word}, "", b);
auto val = M_READ<width>(ip, b, gep);
SHR_SET_FLAG_V<width, 1>(b, val, llvm::X86::CF, bit);
return ContinueBlock;
}
template<int width>
static InstTransResult doBTSrr(llvm::BasicBlock *&b,
const llvm::MCOperand &base,
const llvm::MCOperand &index) {
TASSERT(base.isReg(), "Operand must be a register");
TASSERT(index.isReg(), "Operand must be a register");
auto base_val = R_READ<width>(b, base.getReg());
auto index_val = R_READ<width>(b, index.getReg());
// modulo the index by register size
auto index_mod = llvm::BinaryOperator::CreateURem(index_val,
CONST_V<width>(b, width),
"", b);
SHR_SET_FLAG_V<width, 1>(b, base_val, llvm::X86::CF,
index_mod);
auto bit_mask = llvm::BinaryOperator::CreateShl(CONST_V<width>(b, 1),
index_mod, "", b);
auto new_base_val = llvm::BinaryOperator::Create(
llvm::Instruction::Or, base_val, bit_mask, "",
b);
R_WRITE<width>(b, base.getReg(), new_base_val);
return ContinueBlock;
}
template<int width>
static InstTransResult doBTSri(llvm::BasicBlock *&b,
const llvm::MCOperand &base,
const llvm::MCOperand &index) {
TASSERT(base.isReg(), "Operand must be a register");
TASSERT(index.isImm(), "Operand must be an immediate");
unsigned whichbit = index.getImm();
whichbit %= width;
auto base_val = R_READ<width>(b, base.getReg());
SHR_SET_FLAG_V<width, 1>(b, base_val, llvm::X86::CF,
CONST_V<width>(b, whichbit));
auto new_base_val = llvm::BinaryOperator::Create(
llvm::Instruction::Or, base_val, CONST_V<width>(b, 1ULL << whichbit), "",
b);
R_WRITE<width>(b, base.getReg(), new_base_val);
return ContinueBlock;
}
template<int width>
static InstTransResult doBTSmi(NativeInstPtr ip, llvm::BasicBlock *&b,
llvm::Value *base,
const llvm::MCOperand &index) {
TASSERT(index.isImm(), "Operand must be an immediate");
int imm = index.getImm();
int bytes_offt = imm / 8;
int whichbit = imm % 8;
if (whichbit < 0) {
// make this always positive
whichbit *= -1;
}
auto addrInt = base;
if (base->getType()->isPointerTy()) {
addrInt = new llvm::PtrToIntInst(
base, llvm::Type::getIntNTy(b->getContext(), width), "", b);
}
// pick which byte we need to bit test
auto new_base = llvm::BinaryOperator::Create(llvm::Instruction::Add, addrInt,
CONST_V<width>(b, bytes_offt),
"", b);
auto base_val = M_READ<8>(ip, b, new_base);
SHR_SET_FLAG_V<8, 1>(b, base_val, llvm::X86::CF, CONST_V<8>(b, whichbit));
auto new_base_val = llvm::BinaryOperator::Create(llvm::Instruction::Or,
base_val,
CONST_V<8>(b, 1 << whichbit),
"", b);
M_WRITE<8>(ip, b, new_base, new_base_val);
return ContinueBlock;
}
template<int width>
static InstTransResult doBTSmr(NativeInstPtr ip, llvm::BasicBlock *&b,
llvm::Value *base,
const llvm::MCOperand &index) {
TASSERT(index.isReg(), "Operand must be an immediate");
auto index_val = R_READ<width>(b, index.getReg());
auto word_size = CONST_V<width>(b, width);
auto bit = llvm::BinaryOperator::CreateURem(index_val, word_size, "", b);
auto word = llvm::BinaryOperator::CreateUDiv(index_val, word_size, "", b);
auto ptr = ADDR_TO_POINTER<width>(b, base);
auto gep = llvm::GetElementPtrInst::CreateInBounds(ptr, {word}, "", b);
auto val = M_READ<width>(ip, b, gep);
SHR_SET_FLAG_V<width, 1>(b, val, llvm::X86::CF, bit);
auto bit_to_set = llvm::BinaryOperator::CreateShl(
CONST_V<width>(b, 1), bit, "", b);
auto new_val = llvm::BinaryOperator::CreateOr(val, bit_to_set, "", b);
M_WRITE<width>(ip, b, gep, new_val);
return ContinueBlock;
}
template<int width>
static InstTransResult doBTRmi(NativeInstPtr ip, llvm::BasicBlock *&b,
llvm::Value *base,
const llvm::MCOperand &index) {
TASSERT(index.isImm(), "Operand must be an immediate");
int imm = index.getImm();
int bytes_offt = imm / 8;
int whichbit = imm % 8;
if (whichbit < 0) {
// make this always positive
whichbit *= -1;
}
auto addrInt = base;
if (base->getType()->isPointerTy()) {
addrInt = new llvm::PtrToIntInst(
base, llvm::Type::getIntNTy(b->getContext(), width), "", b);
}
// pick which byte we need to bit test
auto new_base = llvm::BinaryOperator::Create(llvm::Instruction::Add, addrInt,
CONST_V<width>(b, bytes_offt),
"", b);
auto base_val = M_READ<8>(ip, b, new_base);
SHR_SET_FLAG_V<8, 1>(b, base_val, llvm::X86::CF, CONST_V<8>(b, whichbit));
auto new_base_val = llvm::BinaryOperator::Create(
llvm::Instruction::And, base_val, CONST_V<8>(b, ~(1ULL << whichbit)), "",
b);
M_WRITE<8>(ip, b, new_base, new_base_val);
return ContinueBlock;
}
template<int width>
static InstTransResult doBTRmr(NativeInstPtr ip, llvm::BasicBlock *&b,
llvm::Value *base,
const llvm::MCOperand &index) {
TASSERT(index.isReg(), "Operand must be an immediate");
auto index_val = R_READ<width>(b, index.getReg());
auto word_size = CONST_V<width>(b, width);
auto bit = llvm::BinaryOperator::CreateURem(index_val, word_size, "", b);
auto word = llvm::BinaryOperator::CreateUDiv(index_val, word_size, "", b);
auto ptr = ADDR_TO_POINTER<width>(b, base);
auto gep = llvm::GetElementPtrInst::CreateInBounds(ptr, {word}, "", b);
auto val = M_READ<width>(ip, b, gep);
SHR_SET_FLAG_V<width, 1>(b, val, llvm::X86::CF, bit);
auto bit_to_clear = llvm::BinaryOperator::CreateXor(
llvm::BinaryOperator::CreateLShr(CONST_V<width>(b, 1), bit, "", b),
CONST_V<width>(b, 0), "", b);
auto new_val = llvm::BinaryOperator::CreateAnd(val, bit_to_clear, "", b);
M_WRITE<width>(ip, b, gep, new_val);
return ContinueBlock;
}
template<int width>
static InstTransResult doBsrr(llvm::BasicBlock *&b, const llvm::MCOperand &dst,
const llvm::MCOperand &src) {
TASSERT(dst.isReg(), "operand must be register");
TASSERT(src.isReg(), "operand must be register");
auto src_val = R_READ<width>(b, src.getReg());
llvm::Type *s[] = {llvm::Type::getIntNTy(b->getContext(), width)};
auto ctlzFn = llvm::Intrinsic::getDeclaration(b->getParent()->getParent(),
llvm::Intrinsic::ctlz, s);
TASSERT(ctlzFn != NULL, "Could not find ctlz intrinsic");
std::vector<llvm::Value *> ctlzArgs;
ctlzArgs.push_back(src_val);
ctlzArgs.push_back(CONST_V<1>(b, 0));
auto ctlz = llvm::CallInst::Create(ctlzFn, ctlzArgs, "", b);
auto index_of_first_1 = llvm::BinaryOperator::CreateSub(
CONST_V<width>(b, width), ctlz, "", b);
auto is_zero = new llvm::ICmpInst( *b, llvm::CmpInst::ICMP_EQ,
CONST_V<width>(b, 0), index_of_first_1);
F_WRITE(b, llvm::X86::ZF, is_zero);
auto fix_index = llvm::BinaryOperator::CreateSub(index_of_first_1,
CONST_V<width>(b, 1), "", b);
// See if we write to register
auto save_index = llvm::SelectInst::Create(is_zero, // check if the source was zero
src_val, // if it was, do not change contents
fix_index, // if it was not, set index
"", b);
R_WRITE<width>(b, dst.getReg(), save_index);
return ContinueBlock;
}
template<int width>
static InstTransResult doBsfrm(NativeInstPtr ip, llvm::BasicBlock *&b,
const llvm::MCOperand &dst,
llvm::Value *memAddr) {
TASSERT(dst.isReg(), "operand must be register");
auto src_val = M_READ<width>(ip, b, memAddr);
llvm::Type *s[] = {llvm::Type::getIntNTy(b->getContext(), width)};
auto cttzFn = llvm::Intrinsic::getDeclaration(b->getParent()->getParent(),
llvm::Intrinsic::cttz, s);
TASSERT(cttzFn != NULL, "Could not find cttz intrinsic");
std::vector<llvm::Value *> cttzArgs;
cttzArgs.push_back(src_val);
cttzArgs.push_back(CONST_V<1>(b, 0));
auto cttz = llvm::CallInst::Create(cttzFn, cttzArgs, "", b);
auto is_zero = new llvm::ICmpInst( *b, llvm::CmpInst::ICMP_EQ,
CONST_V<width>(b, width), cttz);
F_WRITE(b, llvm::X86::ZF, is_zero);
// See if we write to register
auto save_index = llvm::SelectInst::Create(is_zero, // check if the source was zero
src_val, // if it was, do not change contents
cttz, // if it was not, set index
"", b);
R_WRITE<width>(b, dst.getReg(), save_index);
return ContinueBlock;
}
template<int width>
static InstTransResult doBsfr(llvm::BasicBlock *&b, const llvm::MCOperand &dst,
const llvm::MCOperand &src) {
TASSERT(dst.isReg(), "operand must be register");
TASSERT(src.isReg(), "operand must be register");
auto src_val = R_READ<width>(b, src.getReg());
llvm::Type *s[] = {llvm::Type::getIntNTy(b->getContext(), width)};
auto cttzFn = llvm::Intrinsic::getDeclaration(b->getParent()->getParent(),
llvm::Intrinsic::cttz, s);
TASSERT(cttzFn != NULL, "Could not find cttz intrinsic");
std::vector<llvm::Value *> cttzArgs;
cttzArgs.push_back(src_val);
cttzArgs.push_back(CONST_V<1>(b, 0));
auto cttz = llvm::CallInst::Create(cttzFn, cttzArgs, "", b);
auto is_zero = new llvm::ICmpInst( *b, llvm::CmpInst::ICMP_EQ,
CONST_V<width>(b, width), cttz);
F_WRITE(b, llvm::X86::ZF, is_zero);
// See if we write to register
auto save_index = llvm::SelectInst::Create(is_zero, // check if the source was zero
src_val, // if it was, do not change contents
cttz, // if it was not, set index
"", b);
R_WRITE<width>(b, dst.getReg(), save_index);
return ContinueBlock;
}
GENERIC_TRANSLATION(CDQ, doCdq(block))
GENERIC_TRANSLATION(INT3, doInt3(block))
GENERIC_TRANSLATION(INT, doInt(block, OP(0)))
GENERIC_TRANSLATION(TRAP, doTrap(block))
GENERIC_TRANSLATION(NOOP, doNoop(block))
GENERIC_TRANSLATION(HLT, doHlt(block))
GENERIC_TRANSLATION(BSWAP32r, doBswapR<32>(ip, block, OP(0)))
GENERIC_TRANSLATION(BSWAP64r, doBswapR<64>(ip, block, OP(0)))
GENERIC_TRANSLATION(LAHF, doLAHF(block))
GENERIC_TRANSLATION(STD, doStd(block))
GENERIC_TRANSLATION(CLD, doCld(block))
GENERIC_TRANSLATION(STC, doStc(block))
GENERIC_TRANSLATION(CLC, doClc(block))
GENERIC_TRANSLATION_REF(LEA16r, doLea<16>(ip, block, ADDR_NOREF(1), OP(0)),
doLea<16>(ip, block, MEM_REFERENCE(1), OP(0)))
template<int width>
static InstTransResult doLeaRef(TranslationContext &ctx,
llvm::BasicBlock *&block) {
InstTransResult ret;
auto F = block->getParent();
auto ip = ctx.natI;
auto &inst = ip->get_inst();
auto natM = ctx.natM;
if (ip->has_code_ref()) {
NativeInst::CFGOpType optype;
if (ip->has_mem_reference) {
optype = NativeInst::MEMRef;
} else if (ip->has_imm_reference) {
optype = NativeInst::IMMRef;
} else {
throw TErr(__LINE__, __FILE__, "Have code ref but no reference");
}
auto callback_fn = ArchAddCallbackDriver(block->getParent()->getParent(),
ip->get_reference(optype));
auto addrInt = new llvm::PtrToIntInst(
callback_fn, llvm::Type::getIntNTy(block->getContext(), width), "",
block);
ret = doLeaV<width>(block, OP(0), addrInt);
} else if (ip->has_mem_reference) {
ret = doLea<width>(ip, block, MEM_REFERENCE(1), OP(0));
} else if (ip->has_imm_reference) {
ret = doLea<width>(ip, block, IMM_AS_DATA_REF<width>(block, natM, ip),
OP(0));
} else {
ret = doLea<width>(ip, block, ADDR_NOREF(1), OP(0));
}
return ret;
}
static InstTransResult translate_LEA32r(TranslationContext &ctx,
llvm::BasicBlock *&block) {
return doLeaRef<32>(ctx, block);
}
static InstTransResult translate_LEA64r(TranslationContext &ctx,
llvm::BasicBlock *&block) {
return doLeaRef<64>(ctx, block);
}
static InstTransResult translate_LEA64_32r(TranslationContext &ctx,
llvm::BasicBlock *&block) {
return doLeaRef<32>(ctx, block);
}
static InstTransResult translate_CPUID32(TranslationContext &ctx,
llvm::BasicBlock *&block) {
auto eax = R_READ<32>(block, llvm::X86::EAX);
auto ecx = R_READ<32>(block, llvm::X86::ECX);
CREATE_BLOCK(b0, block);
CREATE_BLOCK(b1, block);
CREATE_BLOCK(b2, block);
CREATE_BLOCK(b4, block);
CREATE_BLOCK(b7, block);
CREATE_BLOCK(b11, block);
CREATE_BLOCK(b8m, block);
CREATE_BLOCK(bdefault, block);
CREATE_BLOCK(bexit, block);
CREATE_BLOCK(eax4_b0, block_b4);
CREATE_BLOCK(eax4_b1, block_b4);
CREATE_BLOCK(eax4_b2, block_b4);
CREATE_BLOCK(eax4_b3, block_b4);
CREATE_BLOCK(eax4_bdefault, block_b4);
CREATE_BLOCK(eax11_b0, block_b11);
CREATE_BLOCK(eax11_b1, block_b11);
CREATE_BLOCK(eax11_bdefault, block_b11);
auto si = llvm::SwitchInst::Create(eax, block_bdefault, 7, block);
// 32-bit CPUID values taken by sampling from a live CPU
// eax = 0
R_WRITE<32>(block_b0, llvm::X86::EAX, CONST_V<32>(block, 0x0000000d));
R_WRITE<32>(block_b0, llvm::X86::EBX, CONST_V<32>(block, 0x756e6547));
R_WRITE<32>(block_b0, llvm::X86::ECX, CONST_V<32>(block, 0x6c65746e));
R_WRITE<32>(block_b0, llvm::X86::EDX, CONST_V<32>(block, 0x49656e69));
llvm::BranchInst::Create(block_bexit, block_b0);