|
2 | 2 | from unicorn import *
|
3 | 3 | from unicorn.arm_const import *
|
4 | 4 |
|
5 |
| -# issue #287 |
6 |
| -# Initial Register States: R0=3, R1=24, R2=16, R3=0 |
7 |
| -# |
8 |
| -# ----- code start ----- |
9 |
| -# CMP R0,R1,LSR#3 |
10 |
| -# SUBCS R0,R0,R1,LSR#3 # CPU flags got changed in these two instructions, and *REMEMBERED*, now NF == VF == 0 |
11 |
| -# CMP R0,#1 # CPU flags changed again, now NF == 1, VF == 0, but they are not properly *REMEMBERED* |
12 |
| -# MOV R1,R1,LSR#4 |
13 |
| -# SUBGES R2,R2,#4 # according to the result of CMP, we should skip this op |
14 |
| -# |
15 |
| -# MOVGE R3,#100 # since changed flags are not *REMEMBERED* in CMP, now NF == VF == 0, which result in wrong branch |
16 |
| -# # at the end of this code block, should R3 == 0 |
17 |
| -# ----- code end ------ |
18 |
| -# |
19 |
| -# TCG ops are correct, plain op translation is done correctly, |
20 |
| -# but there're In-Memory bits invisible from ops that control the host code generation. |
21 |
| -# all these codes are in one TCG translation-block, so wrong things could happen. |
22 |
| -# detail explanation is given on the right side. |
23 |
| -# remember, both set_label and brcond are point to refresh the dead_temps and mem_temps states in TCG |
24 |
| -# |
25 |
| -# ----- TCG ops ------ |
26 |
| -# ld_i32 tmp5,env,$0xfffffffffffffff4 |
27 |
| -# movi_i32 tmp6,$0x0 |
28 |
| -# brcond_i32 tmp5,tmp6,ne,$0x0 |
29 |
| -# mov_i32 tmp5,r1 ------------------------- |
30 |
| -# movi_i32 tmp6,$0x3 | |
31 |
| -# shr_i32 tmp5,r1,tmp6 | |
32 |
| -# mov_i32 tmp6,r0 | |
33 |
| -# sub_i32 NF,r0,tmp5 | |
34 |
| -# mov_i32 ZF,NF | |
35 |
| -# setcond_i32 CF,r0,tmp5,geu | # This part is "CMP R0,R1,LSR#3" |
36 |
| -# xor_i32 VF,NF,r0 |-----> # and "SUBCS R0,R0,R1,LSR#3" |
37 |
| -# xor_i32 tmp7,r0,tmp5 | # the last op in this block, set_label get a chance to refresh the TCG globals memory states, |
38 |
| -# and_i32 VF,VF,tmp7 | # so things get back to normal states |
39 |
| -# mov_i32 tmp6,NF | # these codes are not affected by the bug. Let's called this Part-D |
40 |
| -# movi_i32 tmp5,$0x0 | |
41 |
| -# brcond_i32 CF,tmp5,eq,$0x1 | |
42 |
| -# mov_i32 tmp5,r1 | |
43 |
| -# movi_i32 tmp6,$0x3 | |
44 |
| -# shr_i32 tmp5,r1,tmp6 | |
45 |
| -# mov_i32 tmp6,r0 | |
46 |
| -# sub_i32 tmp6,r0,tmp5 | |
47 |
| -# mov_i32 r0,tmp6 | |
48 |
| -# set_label $0x1 ------------------------- |
49 |
| -# movi_i32 tmp5,$0x1 ----------------- # Let's called this Part-C |
50 |
| -# mov_i32 tmp6,r0 | # NF is used as output operand again! |
51 |
| -# sub_i32 NF,r0,tmp5 ----------------|-----> # but it is stated as Not-In-Memory, |
52 |
| -# mov_i32 ZF,NF | # no need to sync it after calculation. |
53 |
| -# setcond_i32 CF,r0,tmp5,geu | # the generated host code does not write NF |
54 |
| -# xor_i32 VF,NF,r0 | # back to its memory location, hence forgot. And the CPU flags after this calculation is not changed. |
55 |
| -# xor_i32 tmp7,r0,tmp5 | # Caution: the following SUBGES's condition check is right, even though the generated host code does not *REMEMBER* NF, it will cache the calculated result and serve SUBGES correctly |
56 |
| -# and_i32 VF,VF,tmp7 | |
57 |
| -# mov_i32 tmp6,NF | |
58 |
| -# mov_i32 tmp5,r1 | # this part is "CMP R0,#1" |
59 |
| -# movi_i32 tmp6,$0x4 | # and "MOV R1,R1,LSR#4" |
60 |
| -# shr_i32 tmp5,r1,tmp6 | # and "SUBGES R2,R2,#4" |
61 |
| -# mov_i32 r1,tmp5 |-----> # This is the part where problem start to arise |
62 |
| -# xor_i32 tmp5,VF,NF | |
63 |
| -# movi_i32 tmp6,$0x0 | |
64 |
| -# brcond_i32 tmp5,tmp6,lt,$0x2 --------|-----> # QEMU will refresh the InMemory bit for TCG globals here, but Unicorn won't |
65 |
| -# movi_i32 tmp5,$0x4 | |
66 |
| -# mov_i32 tmp6,r2 | # this is the 1st bug-related op get analyzed. |
67 |
| -# sub_i32 NF,r2,tmp5 ----------------|-----> # here, NF is an output operand, it's flagged dead |
68 |
| -# mov_i32 ZF,NF | # and the InMemory bit is clear, tell the previous(above) ops |
69 |
| -# setcond_i32 CF,r2,tmp5,geu | # if it is used as output operand again, do not sync it |
70 |
| -# xor_i32 VF,NF,r2 | # so the generated host-code for previous ops will not write it back to Memory |
71 |
| -# xor_i32 tmp7,r2,tmp5 | # Caution: the CPU flags after this calculation is also right, because the set_label is a point of refresh, make them *REMEMBERED* |
72 |
| -# and_i32 VF,VF,tmp7 | # Let's call this Part-B |
73 |
| -# mov_i32 tmp6,NF | |
74 |
| -# mov_i32 r2,ZF | |
75 |
| -# set_label $0x2 ----------------- |
76 |
| -# xor_i32 tmp5,VF,NF ----------------- |
77 |
| -# movi_i32 tmp6,$0x0 | |
78 |
| -# brcond_i32 tmp5,tmp6,lt,$0x3 | # Let's call this Part-A |
79 |
| -# movi_i32 tmp5,$0x64 | # if Part-B is not skipped, this part won't go wrong, because we'll check the CPU flags as the result of Part-B, it's *REMEMBERED* |
80 |
| -# movi_i32 r3,$0x64 |-----> # but if Part-B is skipped, |
81 |
| -# set_label $0x3 | # what should we expected? we will check the condition based on the result of Part-D!!! |
82 |
| -# call wfi,$0x0,$0,env | # because result of Part-C is lost. this is why things go wrong. |
83 |
| -# set_label $0x0 | |
84 |
| -# exit_tb $0x7f6401714013 ----------------- |
85 |
| -# ########### |
86 |
| -# ----- TCG ends ------ |
87 |
| - |
| 5 | +''' |
| 6 | + issue #287 |
| 7 | + Initial Register States: R0=3, R1=24, R2=16, R3=0 |
| 8 | + ----- code start ----- |
| 9 | + CMP R0,R1,LSR#3 |
| 10 | + SUBCS R0,R0,R1,LSR#3 # CPU flags got changed in these two instructions, and *REMEMBERED*, now NF == VF == 0 |
| 11 | + CMP R0,#1 # CPU flags changed again, now NF == 1, VF == 0, but they are not properly *REMEMBERED* |
| 12 | + MOV R1,R1,LSR#4 |
| 13 | + SUBGES R2,R2,#4 # according to the result of CMP, we should skip this op |
| 14 | + |
| 15 | + MOVGE R3,#100 # since changed flags are not *REMEMBERED* in CMP, now NF == VF == 0, which result in wrong branch |
| 16 | + # at the end of this code block, should R3 == 0 |
| 17 | + ----- code end ------ |
| 18 | +
|
| 19 | + # TCG ops are correct, plain op translation is done correctly, |
| 20 | + # but there're In-Memory bits invisible from ops that control the host code generation. |
| 21 | + # all these codes are in one TCG translation-block, so wrong things could happen. |
| 22 | + # detail explanation is given on the right side. |
| 23 | + # remember, both set_label and brcond are point to refresh the dead_temps and mem_temps states in TCG |
| 24 | + ----- TCG ops ------ |
| 25 | + ld_i32 tmp5,env,$0xfffffffffffffff4 |
| 26 | + movi_i32 tmp6,$0x0 |
| 27 | + brcond_i32 tmp5,tmp6,ne,$0x0 |
| 28 | + mov_i32 tmp5,r1 ------------------------- |
| 29 | + movi_i32 tmp6,$0x3 | |
| 30 | + shr_i32 tmp5,r1,tmp6 | |
| 31 | + mov_i32 tmp6,r0 | |
| 32 | + sub_i32 NF,r0,tmp5 | |
| 33 | + mov_i32 ZF,NF | |
| 34 | + setcond_i32 CF,r0,tmp5,geu | # This part is "CMP R0,R1,LSR#3" |
| 35 | + xor_i32 VF,NF,r0 |-----> # and "SUBCS R0,R0,R1,LSR#3" |
| 36 | + xor_i32 tmp7,r0,tmp5 | # the last op in this block, set_label get a chance to refresh the TCG globals memory states, |
| 37 | + and_i32 VF,VF,tmp7 | # so things get back to normal states |
| 38 | + mov_i32 tmp6,NF | # these codes are not affected by the bug. Let's called this Part-D |
| 39 | + movi_i32 tmp5,$0x0 | |
| 40 | + brcond_i32 CF,tmp5,eq,$0x1 | |
| 41 | + mov_i32 tmp5,r1 | |
| 42 | + movi_i32 tmp6,$0x3 | |
| 43 | + shr_i32 tmp5,r1,tmp6 | |
| 44 | + mov_i32 tmp6,r0 | |
| 45 | + sub_i32 tmp6,r0,tmp5 | |
| 46 | + mov_i32 r0,tmp6 | |
| 47 | + set_label $0x1 ------------------------- |
| 48 | + movi_i32 tmp5,$0x1 ----------------- # Let's called this Part-C |
| 49 | + mov_i32 tmp6,r0 | # NF is used as output operand again! |
| 50 | + sub_i32 NF,r0,tmp5 ----------------|-----> # but it is stated as Not-In-Memory, |
| 51 | + mov_i32 ZF,NF | # no need to sync it after calculation. |
| 52 | + setcond_i32 CF,r0,tmp5,geu | # the generated host code does not write NF |
| 53 | + xor_i32 VF,NF,r0 | # back to its memory location, hence forgot. And the CPU flags after this calculation is not changed. |
| 54 | + xor_i32 tmp7,r0,tmp5 | # Caution: the following SUBGES's condition check is right, even though the generated host code does not *REMEMBER* NF, it will cache the calculated result and serve SUBGES correctly |
| 55 | + and_i32 VF,VF,tmp7 | |
| 56 | + mov_i32 tmp6,NF | |
| 57 | + mov_i32 tmp5,r1 | # this part is "CMP R0,#1" |
| 58 | + movi_i32 tmp6,$0x4 | # and "MOV R1,R1,LSR#4" |
| 59 | + shr_i32 tmp5,r1,tmp6 | # and "SUBGES R2,R2,#4" |
| 60 | + mov_i32 r1,tmp5 |-----> # This is the part where problem start to arise |
| 61 | + xor_i32 tmp5,VF,NF | |
| 62 | + movi_i32 tmp6,$0x0 | |
| 63 | + brcond_i32 tmp5,tmp6,lt,$0x2 --------|-----> # QEMU will refresh the InMemory bit for TCG globals here, but Unicorn won't |
| 64 | + movi_i32 tmp5,$0x4 | |
| 65 | + mov_i32 tmp6,r2 | # this is the 1st bug-related op get analyzed. |
| 66 | + sub_i32 NF,r2,tmp5 ----------------|-----> # here, NF is an output operand, it's flagged dead |
| 67 | + mov_i32 ZF,NF | # and the InMemory bit is clear, tell the previous(above) ops |
| 68 | + setcond_i32 CF,r2,tmp5,geu | # if it is used as output operand again, do not sync it |
| 69 | + xor_i32 VF,NF,r2 | # so the generated host-code for previous ops will not write it back to Memory |
| 70 | + xor_i32 tmp7,r2,tmp5 | # Caution: the CPU flags after this calculation is also right, because the set_label is a point of refresh, make them *REMEMBERED* |
| 71 | + and_i32 VF,VF,tmp7 | # Let's call this Part-B |
| 72 | + mov_i32 tmp6,NF | |
| 73 | + mov_i32 r2,ZF | |
| 74 | + set_label $0x2 ----------------- |
| 75 | + xor_i32 tmp5,VF,NF ----------------- |
| 76 | + movi_i32 tmp6,$0x0 | |
| 77 | + brcond_i32 tmp5,tmp6,lt,$0x3 | # Let's call this Part-A |
| 78 | + movi_i32 tmp5,$0x64 | # if Part-B is not skipped, this part won't go wrong, because we'll check the CPU flags as the result of Part-B, it's *REMEMBERED* |
| 79 | + movi_i32 r3,$0x64 |-----> # but if Part-B is skipped, |
| 80 | + set_label $0x3 | # what should we expected? we will check the condition based on the result of Part-D!!! |
| 81 | + call wfi,$0x0,$0,env | # because result of Part-C is lost. this is why things go wrong. |
| 82 | + set_label $0x0 | |
| 83 | + exit_tb $0x7f6401714013 ----------------- |
| 84 | + ########### |
| 85 | + ----- TCG ends ------ |
| 86 | +''' |
88 | 87 |
|
89 | 88 | CODE = (
|
90 | 89 | b'\xa1\x01\x50\xe1' # cmp r0, r1, lsr #3
|
|
0 commit comments