ouadji
Joined: 24 Dec 2008
Posts: 1081
Location: Belgium
|
where is the "pop" ?
no problem with "esp" because "ret+endp" equal "mov esp,ebp / pop ebp"
but if VirtualAlloc fails,
the registers ebx, esi and edi are not restored properly.
;FASMW.ASM
;----------
proc MainWindow hwnd,wmsg,wparam,lparam
push ebx esi edi
...
...
cmp [wmsg],FM_OPEN
je fmopen
...
...
fmopen:
...
...
load_file:
invoke CreateFile,\
lparam],GENERIC_READ,FILE_SHARE_READ,0,OPEN_EXISTING,0,0
cmp eax,-1
je open_failed
mov ebx,eax
invoke GetFileSize,ebx,NULL
inc eax
push eax ;<-------- PUSH
invoke VirtualAlloc,0,eax,MEM_COMMIT,PAGE_READWRITE
or eax,eax
jz load_out_of_memory
pop ecx
dec ecx
...
...
load_out_of_memory:
invoke CloseHandle,ebx
open_failed:
invoke VirtualFree,[lparam],0,MEM_RELEASE
jmp failed
...
...
failed: or eax,-1
jmp finish
...
...
finish: ; <--- here, esp != ebp ... esp == ebp-4
pop edi esi ebx
ret
endp
solution
proc MainWindow hwnd,wmsg,wparam,lparam
push ebx esi edi
...
...
push eax ;<---------- PUSH
invoke VirtualAlloc,0,eax,MEM_COMMIT,PAGE_READWRITE
or eax,eax
pop ecx ;<---------- POP
jz load_out_of_memory
dec ecx
...
...
finish: ; <--- now, esp == ebp
pop edi esi ebx
ret
_________________ I am not young enough to know everything (Oscar Wilde)-
|