I have the following generated assembly code that keeps crashing for some reason.
0000000040001AF0: 48 8D 44 24 30 lea rax,[rsp 30h]
0000000040001AF5: 48 89 08 mov qword ptr [rax],rcx
0000000040001AF8: 48 8D 4C 24 38 lea rcx,[rsp 38h]
0000000040001AFD: 48 89 11 mov qword ptr [rcx],rdx
0000000040001B00: 48 8D 54 24 40 lea rdx,[rsp 40h]
0000000040001B05: 48 8B 28 mov rbp,qword ptr [rax]
0000000040001B08: 48 8B 31 mov rsi,qword ptr [rcx]
0000000040001B0B: 48 89 E8 mov rax,rbp
0000000040001B0E: 48 31 D2 xor rdx,rdx
0000000040001B11: 48 F7 F6 div rax,rsi
0000000040001B14: 48 89 D5 mov rbp,rdx
0000000040001B17: 48 8D 44 24 40 lea rax,[rsp 40h]
0000000040001B1C: 48 89 28 mov qword ptr [rax],rbp
0000000040001B1F: 48 8D 54 24 58 lea rdx,[rsp 58h]
0000000040001B24: 48 8B 28 mov rbp,qword ptr [rax]
0000000040001B27: 48 31 F6 xor rsi,rsi
0000000040001B2A: 48 63 F6 movsxd rsi,esi
0000000040001B2D: 48 39 F5 cmp rbp,rsi
0000000040001B30: BD 00 00 00 00 mov ebp,0
0000000040001B35: 0F 94 C5 sete ch
0000000040001B38: 48 83 FD 01 cmp rbp,1
0000000040001B3C: 0F 84 26 00 00 00 je 0000000040001B68
0000000040001B42: 48 8D 6C 24 30 lea rbp,[rsp 30h]
0000000040001B47: 48 8B 6D 00 mov rbp,qword ptr [rbp]
0000000040001B4B: 48 8B 09 mov rcx,qword ptr [rcx]
0000000040001B4E: 48 8B 00 mov rax,qword ptr [rax]
0000000040001B51: 48 29 C1 sub rcx,rax
0000000040001B54: 48 01 CD add rbp,rcx
0000000040001B57: 48 89 2A mov qword ptr [rdx],rbp
0000000040001B5A: 66 0F 1F 44 00 00 nop word ptr [rax rax]
0000000040001B60: 48 8B 12 mov rdx,qword ptr [rdx]
0000000040001B63: 48 89 D0 mov rax,rdx
0000000040001B66: C3 ret
0000000040001B67: 90 nop
0000000040001B68: 48 8D 44 24 30 lea rax,[rsp 30h]
0000000040001B6D: 48 8B 00 mov rax,qword ptr [rax]
0000000040001B70: 48 89 02 mov qword ptr [rdx],rax
0000000040001B73: E9 E8 FF FF FF jmp 0000000040001B60
This is generated by a program from the following pseudo-code function.
align_up(uint64 value, uint64 alignment) : uint64 {
uint64 val = value
uint64 algn = alignment
// align to the next alignment boundary, if unaligned
uint64 tmp = val % algn
return (tmp == 0 ? val : val (algn - tmp))
}
value and alignment are in rcx
and rdx
respectively.
Any ideas about why this code might be crashing would be greatly appreciated.
I know it is the above code that is the offending code as I can replace it with the following code and the program performs as expected.
0000000040001AF0: 48 8D 44 24 30 lea rax,[rsp 30h]
0000000040001AF5: 48 89 08 mov qword ptr [rax],rcx
0000000040001AF8: 48 8D 4C 24 38 lea rcx,[rsp 38h]
0000000040001AFD: 48 89 11 mov qword ptr [rcx],rdx
0000000040001B00: 48 8D 54 24 40 lea rdx,[rsp 40h]
0000000040001B05: 48 8B 28 mov rbp,qword ptr [rax]
0000000040001B08: 48 8B 31 mov rsi,qword ptr [rcx]
0000000040001B0B: 48 89 E8 mov rax,rbp
0000000040001B0E: 48 31 D2 xor rdx,rdx
0000000040001B11: 48 F7 F6 div rax,rsi
0000000040001B14: 48 89 D5 mov rbp,rdx
0000000040001B17: 48 8D 44 24 40 lea rax,[rsp 40h]
0000000040001B1C: 48 89 28 mov qword ptr [rax],rbp
0000000040001B1F: 48 8B 10 mov rdx,qword ptr [rax]
0000000040001B22: 48 31 ED xor rbp,rbp
0000000040001B25: 48 63 ED movsxd rbp,ebp
0000000040001B28: 48 39 EA cmp rdx,rbp
0000000040001B2B: BA 00 00 00 00 mov edx,0
0000000040001B30: 0F 94 C2 sete dl
0000000040001B33: 48 83 FA 01 cmp rdx,1
0000000040001B37: 0F 84 1B 00 00 00 je 0000000040001B58
0000000040001B3D: 48 8D 54 24 30 lea rdx,[rsp 30h]
0000000040001B42: 48 8B 12 mov rdx,qword ptr [rdx]
0000000040001B45: 48 8B 09 mov rcx,qword ptr [rcx]
0000000040001B48: 48 8B 00 mov rax,qword ptr [rax]
0000000040001B4B: 48 29 C1 sub rcx,rax
0000000040001B4E: 48 01 CA add rdx,rcx
0000000040001B51: 48 89 D0 mov rax,rdx
0000000040001B54: C3 ret
0000000040001B55: 0F 1F 00 nop dword ptr [rax]
0000000040001B58: 48 8D 44 24 30 lea rax,[rsp 30h]
0000000040001B5D: 48 8B 00 mov rax,qword ptr [rax]
0000000040001B60: C3 ret
0000000040001B61: 0F 1F 80 00 00 00 nop dword ptr [rax 0000000000000000h]
00
This makes me think this has something to do with the negative jump maybe???
CodePudding user response:
//....
0000000040001B2D: 48 39 F5 cmp rbp,rsi
0000000040001B30: BD 00 00 00 00 mov ebp,0
0000000040001B35: 0F 94 C5 sete ch
0000000040001B38: 48 83 FD 01 cmp rbp,1
//....
0000000040001B4B: 48 8B 09 mov rcx,qword ptr [rcx]
//....
As Peter Cordes said "sete ch
before falling through into mov rcx,qword ptr [rcx]
" seems like problem. This is the problem, the code sets the higher byte (second byte) of rcx before trying to load a value from rcx.