PE loader: error checking

git-svn-id: svn://kolibrios.org@740 a494cfbc-eb01-0410-851d-a64ba20cac60
This commit is contained in:
Sergey Semyonov (Serge) 2008-02-15 16:15:47 +00:00
parent ff590c086c
commit 2c7785b277
5 changed files with 294 additions and 193 deletions

View File

@ -183,6 +183,7 @@ endp
align 4 align 4
proc pci_read32 stdcall, bus:dword, devfn:dword, reg:dword proc pci_read32 stdcall, bus:dword, devfn:dword, reg:dword
push ebx
xor eax, eax xor eax, eax
xor ebx, ebx xor ebx, ebx
mov ah, byte [bus] mov ah, byte [bus]
@ -190,6 +191,7 @@ proc pci_read32 stdcall, bus:dword, devfn:dword, reg:dword
mov bh, byte [devfn] mov bh, byte [devfn]
mov bl, byte [reg] mov bl, byte [reg]
call pci_read_reg call pci_read_reg
pop ebx
ret ret
endp endp
@ -349,26 +351,32 @@ proc get_service stdcall, sz_name:dword
endp endp
align 4 align 4
reg_service: proc reg_service stdcall, name:dword, handler:dword
.sz_name equ esp+4
.handler equ esp+8
mov eax, [.sz_name]
test eax, eax
jz .fail
mov ebx, [.handler] xor eax, eax
test ebx, ebx
jz .fail
cmp [name], eax
je .fail
cmp [handler], eax
je .fail
push ebx
mov eax, SRV_SIZE mov eax, SRV_SIZE
call malloc ;call alloc_service call malloc ;call alloc_service
pop ebx
test eax, eax test eax, eax
jz .fail jz .fail
push ebx
push esi
push edi
mov edi, eax mov edi, eax
mov esi, [.sz_name] mov esi, [name]
mov ecx, 16/4 mov ecx, 16/4
rep movsd rep movsd
pop edi
pop esi
mov [eax+SRV.magic], ' SRV' mov [eax+SRV.magic], ' SRV'
mov [eax+SRV.size], SRV_SIZE mov [eax+SRV.size], SRV_SIZE
@ -380,12 +388,14 @@ reg_service:
mov [ebx+SRV.fd], eax mov [ebx+SRV.fd], eax
mov [edx+SRV.bk], eax mov [edx+SRV.bk], eax
mov ecx, [.handler] mov ecx, [handler]
mov [eax+SRV.srv_proc], ecx mov [eax+SRV.srv_proc], ecx
ret 8 pop ebx
ret
.fail: .fail:
xor eax, eax xor eax, eax
ret 8 ret
endp
align 4 align 4
proc get_proc stdcall, exp:dword, sz_name:dword proc get_proc stdcall, exp:dword, sz_name:dword

View File

@ -1011,11 +1011,35 @@ new_services:
ret ret
@@: @@:
cmp eax, 20 cmp eax, 20
ja .fail ja @F
mov eax, ecx mov eax, ecx
call user_realloc call user_realloc
mov [esp+36], eax mov [esp+36], eax
ret ret
@@:
cmp eax, 21
ja @f
cmp ebx, OS_BASE
jae .fail
stdcall load_PE, ebx
test eax, eax
jz @F
mov esi, eax
stdcall eax, DRV_ENTRY
test eax, eax
jz @F
mov [eax+SRV.entry], esi
@@:
mov [esp+36], eax
ret
.fail: .fail:
xor eax, eax xor eax, eax
mov [esp+36], eax mov [esp+36], eax

View File

@ -1,9 +1,13 @@
include 'export.inc'
align 4 align 4
proc load_PE stdcall, file_name:dword proc load_PE stdcall, file_name:dword
locals locals
image dd ? image dd ?
entry dd ?
base dd ?
endl endl
stdcall load_file, [file_name] stdcall load_file, [file_name]
@ -18,15 +22,26 @@ proc load_PE stdcall, file_name:dword
test eax, eax test eax, eax
jz .cleanup jz .cleanup
mov [base], eax
stdcall map_PE, eax, [image] stdcall map_PE, eax, [image]
ret
mov [entry], eax
test eax, eax
jnz .cleanup
stdcall kernel_free, [base]
.cleanup: .cleanup:
stdcall kernel_free,[image] stdcall kernel_free, [image]
mov eax, [entry]
ret
.fail: .fail:
xor eax, eax xor eax, eax
ret ret
endp endp
DWORD equ dword
PTR equ
align 4 align 4
map_PE: ;stdcall base:dword, image:dword map_PE: ;stdcall base:dword, image:dword
@ -35,173 +50,237 @@ map_PE: ;stdcall base:dword, image:dword
push edi push edi
push esi push esi
push ebx push ebx
sub esp, 44 sub esp, 60
mov ebx, DWORD PTR [esp+84]
mov ebp, DWORD PTR [esp+80]
mov edx, ebx
mov esi, ebx
add edx, DWORD PTR [ebx+60]
mov edi, ebp
mov DWORD PTR [esp+32], edx
mov ecx, DWORD PTR [edx+84]
mov ebp, [esp+68]
mov ebx, [esp+64]
mov edx, ebp
mov esi, ebp
add edx, [ebp+60]
mov edi, ebx
mov [esp+32], edx
mov ecx, [edx+84]
shr ecx, 2 shr ecx, 2
rep movsd rep movsd
movzx eax, word [edx+6] movzx eax, WORD PTR [edx+6]
mov dword [esp+36], 0 mov DWORD PTR [esp+36], 0
mov [esp+28], eax mov DWORD PTR [esp+16], eax
jmp .L6 jmp L2
.L7: L3:
mov eax, [edx+264] mov eax, DWORD PTR [edx+264]
test eax, eax test eax, eax
je .L8 je L4
mov esi, ebx
mov esi, ebp mov edi, ebp
mov edi, ebx add esi, DWORD PTR [edx+268]
add esi, [edx+268]
mov ecx, eax mov ecx, eax
add edi, [edx+260] add edi, DWORD PTR [edx+260]
shr ecx, 2 shr ecx, 2
rep movsd rep movsd
.L8:
mov ecx, [edx+256] L4:
mov ecx, DWORD PTR [edx+256]
add ecx, 4095 add ecx, 4095
and ecx, -4096 and ecx, -4096
cmp ecx, eax cmp ecx, eax
jbe .L10 jbe L6
sub ecx, eax sub ecx, eax
add eax, [edx+260] add eax, DWORD PTR [edx+260]
lea edi, [eax+ebx] lea edi, [eax+ebp]
xor eax, eax xor eax, eax
rep stosb rep stosb
.L10:
inc dword [esp+36] L6:
inc DWORD PTR [esp+36]
add edx, 40 add edx, 40
.L6: L2:
mov esi, [esp+28] mov esi, DWORD PTR [esp+16]
cmp [esp+36], esi cmp DWORD PTR [esp+36], esi
jne .L7 jne L3
mov edi, DWORD PTR [esp+32]
mov edi, [esp+32] cmp DWORD PTR [edi+164], 0
cmp dword [edi+164], 0 je L9
je .L13 mov esi, ebp
mov ecx, ebp
mov eax, [esp+32] sub esi, DWORD PTR [edi+52]
mov edi, ebx add ecx, DWORD PTR [edi+160]
mov ecx, ebx mov eax, esi
sub edi, [eax+52] shr eax, 16
add ecx, [eax+160] mov DWORD PTR [esp+12], eax
jmp L11
mov edx, edi L12:
shr edx, 16 lea ebx, [eax-8]
mov [esp+20], edx xor edi, edi
jmp .L15 shr ebx,1
.L16: jmp L13
lea esi, [eax-8] L14:
xor ebp, ebp movzx eax, WORD PTR [ecx+8+edi*2]
shr esi, 1
jmp .L17
.L18:
movzx eax, word [ecx+8+ebp*2]
mov edx, eax mov edx, eax
shr eax, 12 shr eax, 12
and edx, 4095 and edx, 4095
add edx, [ecx] add edx, DWORD PTR [ecx]
cmp ax, 2 cmp ax, 2
je .L21 je L17
cmp ax, 3 cmp ax, 3
je .L22 je L18
dec ax dec ax
jne .L19 jne L15
mov eax, DWORD PTR [esp+12]
mov eax, [esp+20] add WORD PTR [edx+ebp], ax
add [edx+ebx], ax L17:
.L21: add WORD PTR [edx+ebp], si
add [edx+ebx], di L18:
.L22: add DWORD PTR [edx+ebp], esi
add [edx+ebx], edi L15:
.L19: inc edi
inc ebp L13:
.L17: cmp edi, ebx
cmp ebp, esi jne L14
jne .L18 add ecx, DWORD PTR [ecx+4]
L11:
add ecx, [ecx+4] mov eax, DWORD PTR [ecx+4]
.L15:
mov eax, [ecx+4]
test eax, eax test eax, eax
jne .L16 jne L12
.L13: L9:
mov edx, [esp+32] mov edx, DWORD PTR [esp+32]
cmp dword [edx+132], 0 cmp DWORD PTR [edx+132], 0
je .L24 je L20
mov eax, ebp
mov eax, ebx add eax, DWORD PTR [edx+128]
add eax, [edx+128] mov DWORD PTR [esp+40], 0
lea esi, [eax+20] add eax, 20
.L26: mov DWORD PTR [esp+56], eax
cmp dword [esi-16], 0 L22:
jne .L27 mov ecx, DWORD PTR [esp+56]
cmp DWORD PTR [ecx-16], 0
cmp dword [esi-8], 0 jne L23
je .L24 cmp DWORD PTR [ecx-8], 0
.L27: je L25
mov ecx, [esi-20] L23:
mov ebp, ebx mov edi, DWORD PTR [__exports+32]
add ebp, [esi-4] mov esi, DWORD PTR [__exports+28]
add ecx, ebx mov eax, DWORD PTR [esp+56]
mov [esp+40], ecx mov DWORD PTR [esp+20], edi
.L29: sub edi, -2147483648
mov edi, [esp+40] sub esi, -2147483648
mov eax, [edi] mov DWORD PTR [esp+44], esi
mov ecx, DWORD PTR [eax-4]
mov DWORD PTR [esp+48], edi
mov edx, DWORD PTR [eax-20]
mov DWORD PTR [esp+52], 0
add ecx, ebp
add edx, ebp
mov DWORD PTR [esp+24], edx
mov DWORD PTR [esp+28], ecx
L26:
mov esi, DWORD PTR [esp+52]
mov edi, DWORD PTR [esp+24]
mov eax, DWORD PTR [edi+esi*4]
test eax, eax test eax, eax
je .L30 je L27
test eax, eax test eax, eax
js .L30 js L27
lea edi, [ebp+eax]
lea eax, [eax+2+ebx] mov eax, DWORD PTR [esp+28]
mov edi, kernel_export mov DWORD PTR [eax+esi*4], 0
mov [ebp], dword -1 lea esi, [edi+2]
mov [esp+24], eax push eax
.L33: push 32
movzx eax, WORD PTR [edi]
mov edx, DWORD PTR [esp+56]
mov eax, DWORD PTR [edx+eax*4]
sub eax, -2147483648
push eax
push esi
call strncmp
pop ebx
xor ebx, ebx
test eax, eax
jne L32
jmp L30
L33:
push ecx push ecx
push 16 push 32
push dword [edi] mov ecx, DWORD PTR [esp+28]
push dword [esp+36] mov eax, DWORD PTR [ecx-2147483648+ebx*4]
sub eax, -2147483648
push eax
push esi
call strncmp call strncmp
pop edx pop edx
test eax, eax test eax, eax
jne .L34 jne L34
mov eax, [edi+4] mov esi, DWORD PTR [esp+44]
mov [ebp], eax mov edx, DWORD PTR [esp+52]
jmp .L36 mov ecx, DWORD PTR [esp+28]
.L34: mov eax, DWORD PTR [esi+ebx*4]
add edi, 8 sub eax, -2147483648
cmp dword [edi], 0 mov DWORD PTR [ecx+edx*4], eax
jne .L33 jmp L36
.L36: L34:
add dword [esp+40], 4 inc ebx
add ebp, 4 L32:
jmp .L29 cmp ebx, DWORD PTR [__exports+24]
.L30: jb L33
add esi, 20 L36:
jmp .L26 cmp ebx, DWORD PTR [__exports+24]
.L24: jne L37
mov eax, [esp+32]
add ebx, [eax+40] mov esi, msg_unresolved
add esp, 44 call sys_msg_board_str
mov eax, ebx lea esi, [edi+2]
call sys_msg_board_str
mov esi, msg_CR
call sys_msg_board_str
mov DWORD PTR [esp+40], 1
jmp L37
L30:
movzx eax, WORD PTR [edi]
mov esi, DWORD PTR [esp+44]
mov edi, DWORD PTR [esp+52]
mov edx, DWORD PTR [esp+28]
mov eax, DWORD PTR [esi+eax*4]
sub eax, -2147483648
mov DWORD PTR [edx+edi*4], eax
L37:
inc DWORD PTR [esp+52]
jmp L26
L27:
add DWORD PTR [esp+56], 20
jmp L22
L25:
xor eax, eax
cmp DWORD PTR [esp+40], 0
jne L40
L20:
mov ecx, DWORD PTR [esp+32]
mov eax, ebp
add eax, DWORD PTR [ecx+40]
L40:
add esp, 60
pop ebx pop ebx
pop esi pop esi
pop edi pop edi
pop ebp pop ebp
ret 8 ret 8
align 16
__exports:
export 'KERNEL', \
alloc_kernel_space, 'AllocKernelSpace', \ ; stdcall
free_kernel_space, 'FreeKernelSpace', \ ; stdcall
kernel_alloc, 'KernelAlloc', \ ; stdcall
kernel_free, 'KernelFree', \ ; stdcall
pci_api, 'PciApi', \
pci_read32, 'PciRead32', \ ; stdcall
reg_service, 'RegService', \
user_alloc, 'UserAlloc', \ ; stdcall
user_free, 'UserFree', \ ; stdcall
sys_msg_board_str, 'SysMsgBoardStr'

View File

@ -500,8 +500,6 @@ term9:
push esi push esi
call [eax+APPOBJ.destroy] call [eax+APPOBJ.destroy]
;mov esi, msg_obj_destroy
;call sys_msg_board_str
DEBUGF 1,"%s",msg_obj_destroy DEBUGF 1,"%s",msg_obj_destroy
pop esi pop esi
jmp @B jmp @B

View File

@ -606,16 +606,6 @@ no_lib_load:
; LOAD FONTS I and II ; LOAD FONTS I and II
; pushad
; push eax
; mov eax,char
; call file_system_lfn
; mov eax,char2
; call file_system_lfn
; pop eax
; popad
stdcall read_file, char, FONT_I, 0, 2304 stdcall read_file, char, FONT_I, 0, 2304
stdcall read_file, char2, FONT_II, 0, 2560 stdcall read_file, char2, FONT_II, 0, 2560