File: x86.lpr

package info (click to toggle)
unicorn-engine 2.1.4-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 23,912 kB
  • sloc: ansic: 379,830; python: 9,213; sh: 9,011; java: 8,609; ruby: 4,241; pascal: 1,805; haskell: 1,379; xml: 490; cs: 424; makefile: 348; cpp: 298; asm: 64
file content (1001 lines) | stat: -rw-r--r-- 32,236 bytes parent folder | download | duplicates (3)
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
1001
{
  FreePascal/Delphi bindings for the UnicornEngine Emulator Engine .

  Copyright(c) 2018 Coldzer0 .

  License : GPLv2 .
}

program x86;

{$IFDEF FPC}
  {$MODE Delphi}
{$ENDIF}

{$ifdef MSWINDOWS}
  {$apptype CONSOLE}
{$endif}

uses
  SysUtils,
  Unicorn_dyn,
  UnicornConst,
  X86Const;

const
  // code to be emulated .
  X86_CODE32: array[0..6] of Byte = ($41, $4a,$66,$0f,$ef,$c1, $00); // INC ecx; DEC edx ; PXOR xmm0, xmm1 ;
  X86_CODE32_JUMP: array[0..8] of Byte = ($eb, $02, $90, $90, $90, $90, $90, $90, $00); // jmp 4; nop; nop; nop; nop; nop; nop ;
  X86_CODE32_LOOP: array[0..4] of Byte = ($41, $4a, $eb, $fe, $00); // INC ecx; DEC edx; JMP self-loop
  X86_CODE32_MEM_WRITE: array[0..8] of Byte = ($89, $0d, $aa, $aa, $aa, $aa, $41, $4a, $00); // mov [0xaaaaaaaa], ecx; INC ecx; DEC edx ;
  X86_CODE32_MEM_READ: array[0..8] of Byte = ($8b, $0d, $aa, $aa, $aa, $aa, $41, $4a, $00); // mov ecx,[0xaaaaaaaa]; INC ecx; DEC edx ;

  X86_CODE32_JMP_INVALID: array[0..6] of Byte = ($e9, $e9, $ee, $ee, $41, $4a, $00); //  JMP outside; INC ecx; DEC edx ;
  X86_CODE32_INOUT: array[0..7] of Byte = ($41, $E4, $3F, $4a, $E6, $46, $43, $00); // INC ecx; IN AL, 0x3f; DEC edx; OUT 0x46, AL; INC ebx ;
  X86_CODE32_INC : array[0..1] of byte = ($40,$00); // INC eax .

  X86_CODE64: array[0..75] of Byte = (
    $41, $BC, $3B, $B0, $28, $2A, $49, $0F, $C9, $90, $4D, $0F, $AD, $CF, $49, $87, $FD, $90, $48, $81,
    $D2, $8A, $CE, $77, $35, $48, $F7, $D9, $4D, $29, $F4, $49, $81, $C9, $F6, $8A, $C6, $53, $4D, $87,
    $ED, $48, $0F, $AD, $D2, $49, $F7, $D4, $48, $F7, $E1, $4D, $19, $C5, $4D, $89, $C5, $48, $F7, $D6,
    $41, $B8, $4F, $8D, $6B, $59, $4D, $87, $D0, $68, $6A, $1E, $09, $3C, $59, $00);
  X86_CODE16: array[0..2] of Byte = ($00, $00, $00);   // add   byte ptr [bx + si], al
  X86_CODE64_SYSCALL: array[0..2] of Byte = ($0f, $05, $00); // SYSCALL

  // memory address where emulation starts
  ADDRESS = $1000000;

// callback for tracing basic blocks
procedure HookBlock(uc: uc_engine; address: UInt64; size: Cardinal; user_data: Pointer); cdecl;
begin
  WriteLn(Format('>>> Tracing basic block at 0x%x, block size = 0x%x', [address, size]));
end;

// callback for tracing instruction
procedure HookCode(uc: uc_engine; address: UInt64; size: Cardinal; user_data: Pointer); cdecl;
var
  eflags: integer;
begin
  WriteLn(Format('>>> Tracing instruction at 0x%x, instruction size = 0x%x', [address, size]));
  uc_reg_read(uc, UC_X86_REG_EFLAGS, @eflags);
  WriteLn(Format('>>> --- EFLAGS is 0x%x', [eflags]));
end;

// callback for tracing instruction
procedure HookCode64(uc: uc_engine; address: UInt64; size: Cardinal; user_data: Pointer); cdecl;
var
  rip: UInt64;
begin
  WriteLn(Format('>>> Tracing instruction at 0x%x, instruction size = 0x%x', [address, size]));
  uc_reg_read(uc, UC_X86_REG_RIP, @rip);
  WriteLn(Format('>>> --- RIP is 0x%x', [rip]));
end;

function HookMemInvalid(uc: uc_engine; _type: uc_mem_type; address: UInt64; size: Cardinal; value: Int64; user_data: Pointer): LongBool; cdecl;
begin
  case _type of
    UC_MEM_WRITE_UNMAPPED:
      begin
        WriteLn(Format('>>> Missing memory is being WRITE at 0x%x, data size = %u, data value = 0x%x', [address, size, value]));
        // map this memory in with 2MB in size
        uc_mem_map(uc, $aaaa0000, 2 * 1024*1024, UC_PROT_ALL);
        // return true to indicate we want to continue
        Result := true;
      end
    else
      begin
        // return false to indicate we want to stop emulation
        Result := false;
      end;
  end;
end;

procedure HookMem64(uc: uc_engine; _type: uc_mem_type; address: UInt64; size: Cardinal; value: Int64; user_data: Pointer); cdecl;
begin
  case _type of
    UC_MEM_READ:
      begin
        WriteLn(Format('>>> Memory is being READ at 0x%x, data size = %u', [address, size]));
      end;
    UC_MEM_WRITE:
      begin
        WriteLn(Format('>>> Memory is being WRITE at 0x%x, data size = %u, data value = 0x%x', [address, size, value]));
      end;
  end;
end;

// callback for IN instruction (X86).
// this returns the data read from the port
function HookIn(uc: uc_engine; port: UInt32; size: integer; user_data: Pointer): Uint32; cdecl;
var
  eip: UInt32;
begin
  uc_reg_read(uc, UC_X86_REG_EIP, @eip);
  WriteLn(Format('--- reading from port 0x%x, size: %u, address: 0x%x', [port, size, eip]));
  case size of
    1:
      begin
        // read 1 byte to AL
        Result := $f1;
      end;
    2:
      begin
        // read 2 byte to AX
        Result := $f2;
      end;
    4:
      begin
        // read 4 byte to EAX
        Result := $f4;
      end;
    else
      begin
        // should never reach this
        Result := 0;
      end;
  end;
end;

// callback for OUT instruction (X86).
procedure HookOut(uc: uc_engine; port: UInt32; size: integer; value: UInt32; user_data: Pointer); cdecl;
var
  tmp, eip: UInt32;
begin
  uc_reg_read(uc, UC_X86_REG_EIP, @eip);
  WriteLn(Format('--- writing to port 0x%x, size: %u, value: 0x%x, address: 0x%x', [port, size, value, eip]));

  // confirm that value is indeed the value of AL/AX/EAX
  case size of
    1:
      begin
        uc_reg_read(uc, UC_X86_REG_AL, @tmp);
      end;
    2:
      begin
        uc_reg_read(uc, UC_X86_REG_AX, @tmp);
      end;
    4:
      begin
        uc_reg_read(uc, UC_X86_REG_EAX, @tmp);
      end;
    else
      begin
        // should never reach this
        Exit;
      end;
  end;
  WriteLn(Format('--- register value = 0x%x', [tmp]));
end;

// callback for SYSCALL instruction (X86).
procedure HookSyscall(uc: uc_engine; user_data: Pointer); cdecl;
var
  rax: UInt64;
begin
  uc_reg_read(uc, UC_X86_REG_RAX, @rax);
  if (rax = $100) then begin
    rax := $200;
    uc_reg_write(uc, UC_X86_REG_RAX, @rax);
  end else
    WriteLn(Format('ERROR: was not expecting rax=0x%x in syscall', [rax]));
end;

procedure TestI386;
var
  uc: uc_engine;
  err: uc_err;
  tmp: UInt32;
  trace1, trace2: uc_hook;
  r_ecx, r_edx: integer;
  r_xmm0,r_xmm1 : array [0..1] of UInt64;
begin
  r_ecx := $1234;     // ECX register
  r_edx := $7890;     // EDX register
  r_xmm0[0] := $08090a0b0c0d0e0f; r_xmm0[1] := $0001020304050607;
  r_xmm1[0] := {%H-}$8090a0b0c0d0e0f0; r_xmm1[1] := $0010203040506070;


  WriteLn('Emulate i386 code');

  // Initialize emulator in X86-32bit mode
  err := uc_open(UC_ARCH_X86, UC_MODE_32, uc);
  if (err <> UC_ERR_OK) then begin
    WriteLn(Format('Failed on uc_open() with error returned: %u', [err]));
    Exit;
  end;

  // map 2MB memory for this emulation
  uc_mem_map(uc, ADDRESS, 2 * 1024 * 1024, UC_PROT_ALL);

  // write machine code to be emulated to memory
  if (uc_mem_write_(uc, ADDRESS, @X86_CODE32, SizeOf(X86_CODE32) - 1) <> UC_ERR_OK) then begin
    WriteLn('Failed to write emulation code to memory, quit!');
    Exit;
  end;

  // initialize machine registers
  uc_reg_write(uc, UC_X86_REG_ECX, @r_ecx);
  uc_reg_write(uc, UC_X86_REG_EDX, @r_edx);
  uc_reg_write(uc, UC_X86_REG_XMM0, @r_xmm0);
  uc_reg_write(uc, UC_X86_REG_XMM1, @r_xmm1);

  // tracing all basic blocks with customized callback
  uc_hook_add(uc, trace1, UC_HOOK_BLOCK, @HookBlock, nil, 1, 0,[]);

  // tracing all instruction by having @begin > @end
  uc_hook_add(uc, trace2, UC_HOOK_CODE, @HookCode, nil, 1, 0,[]);

  // emulate machine code in infinite time
  err := uc_emu_start(uc, ADDRESS, ADDRESS + SizeOf(X86_CODE32) - 1, 0, 0);
  if (err <> UC_ERR_OK) then begin
    WriteLn(Format('Failed on uc_emu_start() with error returned %u: %s', [err, uc_strerror(err)]));
  end;

  // now print out some registers
  WriteLn('>>> Emulation done. Below is the CPU context');

  uc_reg_read(uc, UC_X86_REG_ECX, @r_ecx);
  uc_reg_read(uc, UC_X86_REG_EDX, @r_edx);
  uc_reg_read(uc, UC_X86_REG_XMM0, @r_xmm0);

  WriteLn(Format('>>> ECX = 0x%x', [r_ecx]));
  WriteLn(Format('>>> EDX = 0x%x', [r_edx]));
  WriteLn(Format('>>> XMM0 = 0x%s%s', [IntToHex(r_xmm0[1],16),IntToHex(r_xmm0[0],16)]));

  // read from memory
  err := uc_mem_read_(uc, ADDRESS, @tmp, SizeOf(tmp));
  if (err = UC_ERR_OK) then begin
    WriteLn(Format('>>> Read 4 bytes from [0x%x] = 0x%x', [ADDRESS, tmp]));
  end else begin
    WriteLn(Format('>>> Failed to read 4 bytes from [0x%x], err = %u: %s', [ADDRESS, err, uc_strerror(err)]));
  end;

  uc_close(uc);
end;

procedure test_i386_map_ptr();
var
  uc: uc_engine;
  err: uc_err;
  tmp: UInt32;
  trace1, trace2: uc_hook;
  mem : Pointer;
  r_ecx, r_edx: integer;
  r_xmm0,r_xmm1 : array [0..1] of UInt64;
begin
  r_ecx := $1234;     // ECX register
  r_edx := $7890;     // EDX register
  r_xmm0[0] := $08090a0b0c0d0e0f; r_xmm0[1] := $0001020304050607;
  r_xmm1[0] := {%H-}$8090a0b0c0d0e0f0; r_xmm1[1] := $0010203040506070;


  WriteLn('===================================');
  WriteLn('Emulate i386 code - use uc_mem_map_ptr()');

  // Initialize emulator in X86-32bit mode
  err := uc_open(UC_ARCH_X86, UC_MODE_32, uc);
  if (err <> UC_ERR_OK) then begin
    WriteLn(Format('Failed on uc_open() with error returned: %u', [err]));
    Exit;
  end;

  mem := AllocMem(2 * 1024 * 1024);
  if mem = nil then
  begin
    Writeln('Failed to Allocmem');
    uc_close(uc);
    exit;
  end;

  err := uc_mem_map_ptr(uc,ADDRESS,2 * 1024 * 1024,UC_PROT_ALL,mem);
  if err <> UC_ERR_OK then
  begin
    WriteLn(Format('Failed on uc_mem_map_ptr() with error returned: %u - %s', [err,uc_strerror(err)]));
    FreeMem(mem,2 * 1024 * 1024);
    uc_close(uc);
    Exit;
  end;

  Move(X86_CODE32,mem^,SizeOf(X86_CODE32)-1);
  if CompareMem(mem,@X86_CODE32,SizeOf(X86_CODE32)-1) <> true then
  begin
    Writeln('Failed to write emulation code to memory, quit!');
    Freemem(mem,2 * 1024 * 1024);
    uc_close(uc);
    exit;
  end;
  uc_reg_write(uc, UC_X86_REG_ECX, @r_ecx);
  uc_reg_write(uc, UC_X86_REG_EDX, @r_edx);
  uc_reg_write(uc, UC_X86_REG_XMM0, @r_xmm0);
  uc_reg_write(uc, UC_X86_REG_XMM1, @r_xmm1);

  // tracing all basic blocks with customized callback
  uc_hook_add(uc, trace1, UC_HOOK_BLOCK, @HookBlock, nil, 1, 0,[]);

  // tracing all instruction by having @begin > @end .
  uc_hook_add(uc, trace2, UC_HOOK_CODE, @HookCode, nil, 1, 0,[]);

  // emulate machine code in infinite time
  err := uc_emu_start(uc, ADDRESS, ADDRESS + sizeof(X86_CODE32) - 1, 0, 0);
  if err <> UC_ERR_OK then
     WriteLn(Format('Failed on uc_emu_start() with error returned %u: %s', [err, uc_strerror(err)]));

  Writeln('>>> Emulation done. Below is the CPU context');

  uc_reg_read(uc, UC_X86_REG_ECX, @r_ecx);
  uc_reg_read(uc, UC_X86_REG_EDX, @r_edx);
  uc_reg_read(uc, UC_X86_REG_XMM0, @r_xmm0);

  WriteLn(Format('>>> ECX = 0x%x', [r_ecx]));
  WriteLn(Format('>>> EDX = 0x%x', [r_edx]));
  WriteLn(Format('>>> XMM0 = 0x%s%s', [IntToHex(r_xmm0[1],16),IntToHex(r_xmm0[0],16)]));

  // read from memory
  err := uc_mem_read_(uc, ADDRESS, @tmp, SizeOf(tmp));
  if (err = UC_ERR_OK) then begin
    WriteLn(Format('>>> Read 4 bytes from [0x%x] = 0x%x', [ADDRESS, tmp]));
  end else begin
    WriteLn(Format('>>> Failed to read 4 bytes from [0x%x], err = %u: %s', [ADDRESS, err, uc_strerror(err)]));
  end;

  Freemem(mem,2 * 1024 * 1024);
  uc_close(uc);
end;

procedure TestI386Jump;
var
  uc: uc_engine;
  err: uc_err;
  trace1, trace2: uc_hook;
begin
  WriteLn('===================================');
  WriteLn('Emulate i386 code with jump');

  // Initialize emulator in X86-32bit mode
  err := uc_open(UC_ARCH_X86, UC_MODE_32, uc);
  if (err <> UC_ERR_OK) then begin
    WriteLn(Format('Failed on uc_open() with error returned: %u', [err]));
    Exit;
  end;

  // map 2MB memory for this emulation
  uc_mem_map(uc, ADDRESS, 2 * 1024 * 1024, UC_PROT_ALL);

  // write machine code to be emulated to memory
  if (uc_mem_write_(uc, ADDRESS, @X86_CODE32_JUMP, SizeOf(X86_CODE32_JUMP) - 1) <> UC_ERR_OK) then begin
    WriteLn('Failed to write emulation code to memory, quit!');
    Exit;
  end;

  // tracing 1 basic block with customized callback
  uc_hook_add(uc, trace1, UC_HOOK_BLOCK, @HookBlock, nil, ADDRESS, ADDRESS,[]);

  // tracing 1 instruction at ADDRESS
  uc_hook_add(uc, trace2, UC_HOOK_CODE, @HookCode, nil, ADDRESS, ADDRESS,[]);

  // emulate machine code in infinite time
  err := uc_emu_start(uc, ADDRESS, ADDRESS + SizeOf(X86_CODE32_JUMP) - 1, 0, 0);
  if (err <> UC_ERR_OK) then begin
    WriteLn(Format('Failed on uc_emu_start() with error returned %u: %s', [err, uc_strerror(err)]));
  end;

  WriteLn('>>> Emulation done.');
  uc_close(uc);
end;

procedure TestI386Loop;
var
  uc: uc_engine;
  err: uc_err;
  r_ecx, r_edx: integer;
begin
  r_ecx := $1234;     // ECX register
  r_edx := $7890;     // EDX register
  WriteLn('===================================');
  WriteLn('Emulate i386 code that loop forever');

  // Initialize emulator in X86-32bit mode
  err := uc_open(UC_ARCH_X86, UC_MODE_32, uc);
  if (err <> UC_ERR_OK) then begin
    WriteLn(Format('Failed on uc_open() with error returned: %u', [err]));
    Exit;
  end;

  // map 2MB memory for this emulation
  uc_mem_map(uc, ADDRESS, 2 * 1024 * 1024, UC_PROT_ALL);

  // write machine code to be emulated to memory
  if (uc_mem_write_(uc, ADDRESS, @X86_CODE32_LOOP, SizeOf(X86_CODE32_LOOP) - 1) <> UC_ERR_OK) then begin
    WriteLn('Failed to write emulation code to memory, quit!');
    Exit;
  end;

  // initialize machine registers
  uc_reg_write(uc, UC_X86_REG_ECX, @r_ecx);
  uc_reg_write(uc, UC_X86_REG_EDX, @r_edx);

  // emulate machine code in 2 seconds, so we can quit even
  // if the code loops
  err := uc_emu_start(uc, ADDRESS, ADDRESS + SizeOf(X86_CODE32_LOOP) - 1, 2 * UC_SECOND_SCALE, 0);
  if (err <> UC_ERR_OK) then begin
    WriteLn(Format('Failed on uc_emu_start() with error returned %u: %s', [err, uc_strerror(err)]));
  end;

  // now print out some registers
  WriteLn('>>> Emulation done. Below is the CPU context');

  uc_reg_read(uc, UC_X86_REG_ECX, @r_ecx);
  uc_reg_read(uc, UC_X86_REG_EDX, @r_edx);
  WriteLn(Format('>>> ECX = 0x%x', [r_ecx]));
  WriteLn(Format('>>> EDX = 0x%x', [r_edx]));

  uc_close(uc);
end;

procedure TestI386InvalidMemRead;
var
  uc: uc_engine;
  err: uc_err;
  trace1, trace2: uc_hook;
  r_ecx, r_edx: integer;
begin
  r_ecx := $1234;     // ECX register
  r_edx := $7890;     // EDX register
  WriteLn('===================================');
  WriteLn('Emulate i386 code that read from invalid memory');

  // Initialize emulator in X86-32bit mode
  err := uc_open(UC_ARCH_X86, UC_MODE_32, uc);
  if (err <> UC_ERR_OK) then begin
    WriteLn(Format('Failed on uc_open() with error returned: %u', [err]));
    Exit;
  end;

  // map 2MB memory for this emulation
  uc_mem_map(uc, ADDRESS, 2 * 1024 * 1024, UC_PROT_ALL);

  // write machine code to be emulated to memory
  if (uc_mem_write_(uc, ADDRESS, @X86_CODE32_MEM_READ, SizeOf(X86_CODE32_MEM_READ) - 1) <> UC_ERR_OK) then begin
    WriteLn('Failed to write emulation code to memory, quit!');
    uc_close(uc);
    Exit;
  end;

  // initialize machine registers
  uc_reg_write(uc, UC_X86_REG_ECX, @r_ecx);
  uc_reg_write(uc, UC_X86_REG_EDX, @r_edx);

  // tracing all basic blocks with customized callback
  uc_hook_add(uc, trace1, UC_HOOK_BLOCK, @HookBlock, nil, 1, 0,[]);

  // tracing all instruction by having @begin > @end
  uc_hook_add(uc, trace2, UC_HOOK_CODE, @HookCode, nil, 1, 0,[]);

  err := uc_emu_start(uc, ADDRESS, ADDRESS + SizeOf(X86_CODE32_MEM_READ) - 1, 0, 0);
  if (err <> UC_ERR_OK) then begin
    WriteLn(Format('Failed on uc_emu_start() with error returned %u: %s', [err, uc_strerror(err)]));
  end;

  // now print out some registers
  WriteLn('>>> Emulation done. Below is the CPU context');

  uc_reg_read(uc, UC_X86_REG_ECX, @r_ecx);
  uc_reg_read(uc, UC_X86_REG_EDX, @r_edx);
  WriteLn(Format('>>> ECX = 0x%x', [r_ecx]));
  WriteLn(Format('>>> EDX = 0x%x', [r_edx]));

  uc_close(uc);
end;

procedure TestI386InvalidMemWrite;
var
  uc: uc_engine;
  err: uc_err;
  trace1, trace2, trace3: uc_hook;
  r_ecx, r_edx: integer;
  tmp: UInt32;
begin
  r_ecx := $1234;     // ECX register
  r_edx := $7890;     // EDX register
  WriteLn('===================================');
  WriteLn('Emulate i386 code that write to invalid memory');

  // Initialize emulator in X86-32bit mode
  err := uc_open(UC_ARCH_X86, UC_MODE_32, uc);
  if (err <> UC_ERR_OK) then begin
    WriteLn(Format('Failed on uc_open() with error returned: %u', [err]));
    Exit;
  end;

  // map 2MB memory for this emulation
  uc_mem_map(uc, ADDRESS, 2 * 1024 * 1024, UC_PROT_ALL);

  // write machine code to be emulated to memory
  if (uc_mem_write_(uc, ADDRESS, @X86_CODE32_MEM_WRITE, SizeOf(X86_CODE32_MEM_WRITE) - 1) <> UC_ERR_OK) then begin
    WriteLn('Failed to write emulation code to memory, quit!');
    Exit;
  end;

  // initialize machine registers
  uc_reg_write(uc, UC_X86_REG_ECX, @r_ecx);
  uc_reg_write(uc, UC_X86_REG_EDX, @r_edx);

  // tracing all basic blocks with customized callback
  uc_hook_add(uc, trace1, UC_HOOK_BLOCK, @HookBlock, nil, 1, 0,[]);

  // tracing all instruction by having @begin > @end
  uc_hook_add(uc, trace2, UC_HOOK_CODE, @HookCode, nil, 1, 0,[]);

  // intercept invalid memory events
  uc_hook_add(uc, trace3, UC_HOOK_MEM_READ_UNMAPPED or UC_HOOK_MEM_WRITE_UNMAPPED, @HookMemInvalid, nil,1,0,[]);

  err := uc_emu_start(uc, ADDRESS, ADDRESS + SizeOf(X86_CODE32_MEM_WRITE) - 1, 0, 0);
  if (err <> UC_ERR_OK) then begin
    WriteLn(Format('Failed on uc_emu_start() with error returned %u: %s', [err, uc_strerror(err)]));
  end;

  // now print out some registers
  WriteLn('>>> Emulation done. Below is the CPU context');

  uc_reg_read(uc, UC_X86_REG_ECX, @r_ecx);
  uc_reg_read(uc, UC_X86_REG_EDX, @r_edx);
  WriteLn(Format('>>> ECX = 0x%x', [r_ecx]));
  WriteLn(Format('>>> EDX = 0x%x', [r_edx]));

  // read from memory
  err := uc_mem_read_(uc, $aaaaaaaa, @tmp, SizeOf(tmp));
  if (err = UC_ERR_OK) then
    WriteLn(Format('>>> Read 4 bytes from [0x%x] = 0x%x', [$aaaaaaaa, tmp]))
  else
    WriteLn(Format('>>> Failed to read 4 bytes from [0x%x]', [$aaaaaaaa]));

  err := uc_mem_read_(uc, $ffffffaa, @tmp, SizeOf(tmp));
  if (err = UC_ERR_OK) then
    WriteLn(Format('>>> Read 4 bytes from [0x%x] = 0x%x', [$ffffffaa, tmp]))
  else
    WriteLn(Format('>>> Failed to read 4 bytes from [0x%x]', [$ffffffaa]));

  uc_close(uc);
end;

procedure TestI386JumpInvalid;
var
  uc: uc_engine;
  err: uc_err;
  trace1, trace2: uc_hook;
  r_ecx, r_edx: integer;
begin
  r_ecx := $1234;     // ECX register
  r_edx := $7890;     // EDX register
  WriteLn('===================================');
  WriteLn('Emulate i386 code that jumps to invalid memory');

  // Initialize emulator in X86-32bit mode
  err := uc_open(UC_ARCH_X86, UC_MODE_32, uc);
  if (err <> UC_ERR_OK) then begin
    WriteLn(Format('Failed on uc_open() with error returned: %u', [err]));
    Exit;
  end;

  // map 2MB memory for this emulation
  uc_mem_map(uc, ADDRESS, 2 * 1024 * 1024, UC_PROT_ALL);

  // write machine code to be emulated to memory
  if (uc_mem_write_(uc, ADDRESS, @X86_CODE32_JMP_INVALID, SizeOf(X86_CODE32_JMP_INVALID) - 1) <> UC_ERR_OK) then begin
    WriteLn('Failed to write emulation code to memory, quit!');
    uc_close(uc);
    Exit;
  end;

  // initialize machine registers
  uc_reg_write(uc, UC_X86_REG_ECX, @r_ecx);
  uc_reg_write(uc, UC_X86_REG_EDX, @r_edx);

  // tracing all basic blocks with customized callback
  uc_hook_add(uc, trace1, UC_HOOK_BLOCK, @HookBlock, nil, 1, 0,[]);

  // tracing all instruction by having @begin > @end
  uc_hook_add(uc, trace2, UC_HOOK_CODE, @HookCode, nil, 1, 0,[]);

  err := uc_emu_start(uc, ADDRESS, ADDRESS + SizeOf(X86_CODE32_JMP_INVALID) - 1, 0, 0);
  if (err <> UC_ERR_OK) then begin
    WriteLn(Format('Failed on uc_emu_start() with error returned %u: %s', [err, uc_strerror(err)]));
  end;

  // now print out some registers
  WriteLn('>>> Emulation done. Below is the CPU context');

  uc_reg_read(uc, UC_X86_REG_ECX, @r_ecx);
  uc_reg_read(uc, UC_X86_REG_EDX, @r_edx);
  WriteLn(Format('>>> ECX = 0x%x', [r_ecx]));
  WriteLn(Format('>>> EDX = 0x%x', [r_edx]));

  uc_close(uc);
end;

procedure TestI386Inout;
var
  uc: uc_engine;
  err: uc_err;
  trace1, trace2, trace3, trace4: uc_hook;
  r_ecx, r_edx: integer;
begin
  r_ecx := $1234;     // ECX register
  r_edx := $7890;     // EDX register
  WriteLn('===================================');
  WriteLn('Emulate i386 code with IN/OUT instructions');

  // Initialize emulator in X86-32bit mode
  err := uc_open(UC_ARCH_X86, UC_MODE_32, uc);
  if (err <> UC_ERR_OK) then begin
    WriteLn(Format('Failed on uc_open() with error returned: %u', [err]));
    Exit;
  end;

  // map 2MB memory for this emulation
  uc_mem_map(uc, ADDRESS, 2 * 1024 * 1024, UC_PROT_ALL);

  // write machine code to be emulated to memory
  if (uc_mem_write_(uc, ADDRESS, @X86_CODE32_INOUT, SizeOf(X86_CODE32_INOUT) - 1) <> UC_ERR_OK) then begin
    WriteLn('Failed to write emulation code to memory, quit!');
    Exit;
  end;

  // initialize machine registers
  uc_reg_write(uc, UC_X86_REG_ECX, @r_ecx);
  uc_reg_write(uc, UC_X86_REG_EDX, @r_edx);

  // tracing all basic blocks with customized callback
  uc_hook_add(uc, trace1, UC_HOOK_BLOCK, @HookBlock, nil, 1, 0,[]);

  // tracing all instruction by having @begin > @end
  uc_hook_add(uc, trace2, UC_HOOK_CODE, @HookCode, nil, 1, 0,[]);

  // uc IN instruction
  uc_hook_add(uc, trace3, UC_HOOK_INSN, @HookIn, nil, 1,0,[UC_X86_INS_IN]);
  // uc OUT instruction
  uc_hook_add(uc, trace4, UC_HOOK_INSN, @HookOut, nil, 1,0,[UC_X86_INS_OUT]);

  err := uc_emu_start(uc, ADDRESS, ADDRESS + SizeOf(X86_CODE32_INOUT) - 1, 0, 0);
  if (err <> UC_ERR_OK) then begin
    WriteLn(Format('Failed on uc_emu_start() with error returned %u: %s', [err, uc_strerror(err)]));
  end;

  // now print out some registers
  WriteLn('>>> Emulation done. Below is the CPU context');

  uc_reg_read(uc, UC_X86_REG_ECX, @r_ecx);
  uc_reg_read(uc, UC_X86_REG_EDX, @r_edx);
  WriteLn(Format('>>> ECX = 0x%x', [r_ecx]));
  WriteLn(Format('>>> EDX = 0x%x', [r_edx]));

  uc_close(uc);
end;

procedure test_i386_context_save();
var
  uc: uc_engine;
  context : uc_context;
  err: uc_err;
  r_eax : integer;
begin
  r_eax := 1;     // EAX register
  WriteLn('===================================');
  WriteLn('Emulate i386 code - Save/restore CPU context in opaque blob');

  // Initialize emulator in X86-32bit mode
  err := uc_open(UC_ARCH_X86, UC_MODE_32, uc);
  if (err <> UC_ERR_OK) then begin
    WriteLn(Format('Failed on uc_open() with error returned: %u', [err]));
    Exit;
  end;

  uc_mem_map(uc,ADDRESS,8 * 1024 , UC_PROT_ALL);

  // write machine code to be emulated to memory
  if (uc_mem_write_(uc, ADDRESS, @X86_CODE32_INC, SizeOf(X86_CODE32_INC) - 1) <> UC_ERR_OK) then begin
    WriteLn('Failed to write emulation code to memory, quit!');
    uc_close(uc);
    Exit;
  end;
  // initialize machine registers
  uc_reg_write(uc, UC_X86_REG_EAX, @r_eax);

  // emulate machine code in infinite time
  writeln('>>> Running emulation for the first time');
  err := uc_emu_start(uc, ADDRESS, ADDRESS + sizeof(X86_CODE32_INC) - 1, 0, 0);
  if (err <> UC_ERR_OK) then begin
    WriteLn(Format('Failed on uc_emu_start() with error returned %u: %s', [err, uc_strerror(err)]));
  end;

  Writeln('>>> Emulation done. Below is the CPU context');

  uc_reg_read(uc, UC_X86_REG_EAX, @r_eax);
  WriteLn(Format('>>> EAX = 0x%x', [r_eax]));

  Writeln('>>> Saving CPU context');

  err := uc_context_alloc(uc,context);
  if (err <> UC_ERR_OK) then begin
    WriteLn(Format('Failed on uc_context_alloc() with error returned %u : %s', [err, uc_strerror(err)]));
    exit;
  end;

  err := uc_context_save(uc, context);
  if (err <> UC_ERR_OK) then begin
    WriteLn(Format('Failed on uc_context_save() with error returned %u : %s', [err, uc_strerror(err)]));
    exit;
  end;

  Writeln('>>> Running emulation for the second time');

  err := uc_emu_start(uc, ADDRESS, ADDRESS + sizeof(X86_CODE32_INC) - 1, 0, 0);
  if (err <> UC_ERR_OK) then begin
    WriteLn(Format('Failed on uc_emu_start() with error returned %u: %s', [err, uc_strerror(err)]));
  end;

  Writeln('>>> Emulation done. Below is the CPU context');

  uc_reg_read(uc, UC_X86_REG_EAX, @r_eax);
  WriteLn(Format('>>> EAX = 0x%x', [r_eax]));

  err := uc_context_restore(uc, context);
  if (err <> UC_ERR_OK) then begin
    WriteLn(Format('Failed on uc_context_restore() with error returned %u: %s', [err, uc_strerror(err)]));
    exit;
  end;

  Writeln('>>> CPU context restored. Below is the CPU context');

  uc_reg_read(uc, UC_X86_REG_EAX, @r_eax);
  WriteLn(Format('>>> EAX = 0x%x', [r_eax]));

  err := uc_free(context);
  if (err <> UC_ERR_OK) then begin
    WriteLn(Format('Failed on uc_free() with error returned %u: %s', [err, uc_strerror(err)]));
    exit;
  end;

  uc_close(uc);
end;

procedure TestX86_64;
var
  uc: uc_engine;
  err: uc_err;
  trace1, trace2, trace3, trace4: uc_hook;
  rax, rbx, rcx, rdx, rsi, rdi, r8, r9, r10, r11, r12, r13, r14, r15, rsp: UInt64;
begin
  rax := $71f3029efd49d41d;
  rbx := $d87b45277f133ddb;
  rcx := $ab40d1ffd8afc461;
  rdx := $919317b4a733f01;
  rsi := $4c24e753a17ea358;
  rdi := $e509a57d2571ce96;
  r8  := $ea5b108cc2b9ab1f;
  r9  := $19ec097c8eb618c1;
  r10 := $ec45774f00c5f682;
  r11 := $e17e9dbec8c074aa;
  r12 := $80f86a8dc0f6d457;
  r13 := $48288ca5671c5492;
  r14 := $595f72f6e4017f6e;
  r15 := $1efd97aea331cccc;

  rsp := ADDRESS + $200000;

  WriteLn('Emulate x86_64 code');

  // Initialize emulator in X86-64bit mode
  err := uc_open(UC_ARCH_X86, UC_MODE_64, uc);
  if (err <> UC_ERR_OK) then begin
    WriteLn(Format('Failed on uc_open() with error returned: %u', [err]));
    Exit;
  end;

  // map 2MB memory for this emulation
  uc_mem_map(uc, ADDRESS, 2 * 1024 * 1024, UC_PROT_ALL);

  // write machine code to be emulated to memory
  if (uc_mem_write_(uc, ADDRESS, @X86_CODE64, SizeOf(X86_CODE64) - 1) <> UC_ERR_OK) then begin
    WriteLn('Failed to write emulation code to memory, quit!');
    Exit;
  end;

  // initialize machine registers
  uc_reg_write(uc, UC_X86_REG_RSP, @rsp);

  uc_reg_write(uc, UC_X86_REG_RAX, @rax);
  uc_reg_write(uc, UC_X86_REG_RBX, @rbx);
  uc_reg_write(uc, UC_X86_REG_RCX, @rcx);
  uc_reg_write(uc, UC_X86_REG_RDX, @rdx);
  uc_reg_write(uc, UC_X86_REG_RSI, @rsi);
  uc_reg_write(uc, UC_X86_REG_RDI, @rdi);
  uc_reg_write(uc, UC_X86_REG_R8,  @r8);
  uc_reg_write(uc, UC_X86_REG_R9,  @r9);
  uc_reg_write(uc, UC_X86_REG_R10, @r10);
  uc_reg_write(uc, UC_X86_REG_R11, @r11);
  uc_reg_write(uc, UC_X86_REG_R12, @r12);
  uc_reg_write(uc, UC_X86_REG_R13, @r13);
  uc_reg_write(uc, UC_X86_REG_R14, @r14);
  uc_reg_write(uc, UC_X86_REG_R15, @r15);

  // tracing all basic blocks with customized callback
  uc_hook_add(uc, trace1, UC_HOOK_BLOCK, @HookBlock, nil, 1, 0,[]);

  // tracing all instruction by having @begin > @end
  uc_hook_add(uc, trace2, UC_HOOK_CODE, @HookCode64, nil, ADDRESS, ADDRESS + 20,[]);

  // tracing all memory WRITE access (with @begin > @end)
  uc_hook_add(uc, trace3, UC_HOOK_MEM_WRITE, @HookMem64, nil, 1, 0,[]);
  // tracing all memory READ access (with @begin > @end)
  uc_hook_add(uc, trace4, UC_HOOK_MEM_READ, @HookMem64, nil, 1, 0,[]);

  err := uc_emu_start(uc, ADDRESS, ADDRESS + SizeOf(X86_CODE64) - 1, 0, 0);
  if (err <> UC_ERR_OK) then begin
    WriteLn(Format('Failed on uc_emu_start() with error returned %u: %s', [err, uc_strerror(err)]));
  end;

  // now print out some registers
  WriteLn('>>> Emulation done. Below is the CPU context');

  uc_reg_read(uc, UC_X86_REG_RAX, @rax);
  uc_reg_read(uc, UC_X86_REG_RBX, @rbx);
  uc_reg_read(uc, UC_X86_REG_RCX, @rcx);
  uc_reg_read(uc, UC_X86_REG_RDX, @rdx);
  uc_reg_read(uc, UC_X86_REG_RSI, @rsi);
  uc_reg_read(uc, UC_X86_REG_RDI, @rdi);
  uc_reg_read(uc, UC_X86_REG_R8,  @r8);
  uc_reg_read(uc, UC_X86_REG_R9,  @r9);
  uc_reg_read(uc, UC_X86_REG_R10, @r10);
  uc_reg_read(uc, UC_X86_REG_R11, @r11);
  uc_reg_read(uc, UC_X86_REG_R12, @r12);
  uc_reg_read(uc, UC_X86_REG_R13, @r13);
  uc_reg_read(uc, UC_X86_REG_R14, @r14);
  uc_reg_read(uc, UC_X86_REG_R15, @r15);

  WriteLn(Format('>>> RAX = 0x%.16x', [rax]));
  WriteLn(Format('>>> RBX = 0x%.16x', [rbx]));
  WriteLn(Format('>>> RCX = 0x%.16x', [rcx]));
  WriteLn(Format('>>> RDX = 0x%.16x', [rdx]));
  WriteLn(Format('>>> RSI = 0x%.16x', [rsi]));
  WriteLn(Format('>>> RDI = 0x%.16x', [rdi]));
  WriteLn(Format('>>> R8  = 0x%.16x', [r8]));
  WriteLn(Format('>>> R9  = 0x%.16x', [r9]));
  WriteLn(Format('>>> R10 = 0x%.16x', [r10]));
  WriteLn(Format('>>> R11 = 0x%.16x', [r11]));
  WriteLn(Format('>>> R12 = 0x%.16x', [r12]));
  WriteLn(Format('>>> R13 = 0x%.16x', [r13]));
  WriteLn(Format('>>> R14 = 0x%.16x', [r14]));
  WriteLn(Format('>>> R15 = 0x%.16x', [r15]));

  uc_close(uc);
end;

procedure TestX86_64Syscall;
var
  uc: uc_engine;
  err: uc_err;
  trace1: uc_hook;
  rax: UInt64;
begin
  rax := $100;
  WriteLn('===================================');
  WriteLn('Emulate x86_64 code with "syscall" instruction');

  // Initialize emulator in X86-64bit mode
  err := uc_open(UC_ARCH_X86, UC_MODE_64, uc);
  if (err <> UC_ERR_OK) then begin
    WriteLn(Format('Failed on uc_open() with error returned: %u', [err]));
    Exit;
  end;

  // map 2MB memory for this emulation
  uc_mem_map(uc, ADDRESS, 2 * 1024 * 1024, UC_PROT_ALL);

  // write machine code to be emulated to memory
  if (uc_mem_write_(uc, ADDRESS, @X86_CODE64_SYSCALL, SizeOf(X86_CODE64_SYSCALL) - 1) <> UC_ERR_OK) then begin
    WriteLn('Failed to write emulation code to memory, quit!');
    Exit;
  end;

  // hook interrupts for syscall
  uc_hook_add(uc, trace1, UC_HOOK_INSN, @HookSyscall, nil, 1 , 0 , [UC_X86_INS_SYSCALL]);

  // initialize machine registers
  uc_reg_write(uc, UC_X86_REG_RAX, @rax);

  // emulate machine code in infinite time (last param = 0), or when
  // finishing all the code.
  err := uc_emu_start(uc, ADDRESS, ADDRESS + SizeOf(X86_CODE64_SYSCALL) - 1, 0, 0);
  if (err <> UC_ERR_OK) then begin
    WriteLn(Format('Failed on uc_emu_start() with error returned %u: %s', [err, uc_strerror(err)]));
  end;

  // now print out some registers
  WriteLn('>>> Emulation done. Below is the CPU context');

  uc_reg_read(uc, UC_X86_REG_RAX, @rax);
  WriteLn(Format('>>> RAX = 0x%x', [rax]));

  uc_close(uc);
end;

procedure TestX86_16;
var
  uc: uc_engine;
  err: uc_err;
  tmp: Word;
  eax, ebx, esi: UInt32;
begin
  eax := 7;
  ebx := 5;
  esi := 6;

  WriteLn('Emulate x86 16-bit code');

  // Initialize emulator in X86-16bit mode
  err := uc_open(UC_ARCH_X86, UC_MODE_16, uc);
  if (err <> UC_ERR_OK) then begin
    WriteLn(Format('Failed on uc_open() with error returned: %u', [err]));
    Exit;
  end;

  // map 8KB memory for this emulation
  uc_mem_map(uc, 0, 8 * 1024, UC_PROT_ALL);

  // write machine code to be emulated to memory
  if (uc_mem_write_(uc, 0, @X86_CODE16, SizeOf(X86_CODE16) - 1) <> UC_ERR_OK) then begin
    WriteLn('Failed to write emulation code to memory, quit!');
    Exit;
  end;

  // initialize machine registers
  uc_reg_write(uc, UC_X86_REG_EAX, @eax);
  uc_reg_write(uc, UC_X86_REG_EBX, @ebx);
  uc_reg_write(uc, UC_X86_REG_ESI, @esi);

  // emulate machine code in infinite time (last param = 0), or when
  // finishing all the code.
  err := uc_emu_start(uc, 0, SizeOf(X86_CODE16) - 1, 0, 0);
  if (err <> UC_ERR_OK) then begin
    WriteLn(Format('Failed on uc_emu_start() with error returned %u: %s', [err, uc_strerror(err)]));
  end;

  // now print out some registers
  WriteLn('>>> Emulation done. Below is the CPU context');

  err := uc_mem_read_(uc, 11, @tmp, 1);
  if (err = UC_ERR_OK) then
    WriteLn(Format('>>> Read 1 bytes from [0x%x] = 0x%x', [11, tmp]))
  else
    WriteLn(Format('>>> Failed to read 1 bytes from [0x%x]', [11]));

  uc_close(uc);
end;

begin
  if ParamCount > 0 then begin
    if (ParamStr(1) = '-32') then begin
      TestI386;
      test_i386_map_ptr;
      test_i386_context_save;
      TestI386Inout;
      TestI386Jump;
      TestI386Loop;
      TestI386InvalidMemRead;
      TestI386InvalidMemWrite;
      TestI386JumpInvalid;
    end;

    if (ParamStr(1) = '-64') then begin
      TestX86_64;
      TestX86_64Syscall;
    end;

    if (ParamStr(1) = '-16') then begin
      TestX86_16;
    end;

  end else
    WriteLn(#10'Syntax: SampleX86 <-16|-32|-64>'#10);
end.