Skip to content

Commit

Permalink
Fixed stack alignment error
Browse files Browse the repository at this point in the history
  • Loading branch information
enkomio committed Sep 19, 2022
1 parent 844f8d2 commit 610ca84
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 9 deletions.
2 changes: 1 addition & 1 deletion TheMatrix/TheMatrix.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
<PostBuildEvent>
<Command>C:\Workspace\TheMatrix\Baseline\add_resource_x64.bat</Command>
<Command>..\add_resource_x64.bat</Command>
</PostBuildEvent>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
Expand Down
3 changes: 1 addition & 2 deletions TheMatrix/const.inc
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ GENERIC_WRITE equ 40000000h
GENERIC_READ_WRITE equ GENERIC_READ or GENERIC_WRITE
CREATE_NEW equ 1
OPEN_EXISTING equ 3
ERROR_FILE_NOT_FOUND equ 2
INVALID_FILE_SIZE equ 0FFFFFFFFh
INVALID_HANDLE_VALUE equ 0FFFFFFFFh
INVALID_HANDLE_VALUE equ -1
MEM_COMMIT equ 1000h
MEM_DECOMMIT equ 4000h
PAGE_READWRITE equ 4h
Expand Down
27 changes: 22 additions & 5 deletions TheMatrix/x64_builder.inc
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@ add_resource proc frame

_CreateFrame ar_,LOCALS_SIZE,0,rdi
_EndProlog
_AllocHomeArea

mov qword ptr ar_OffsetHomeRCX[rbp], rcx
mov qword ptr ar_OffsetHomeRDX[rbp], rdx

; used for stack alignment
sub rsp, sizeof qword

; get the size of the file to embed
push 0h
push 0h
Expand All @@ -23,10 +27,9 @@ add_resource proc frame
xor r8, r8
mov rdx, GENERIC_READ
mov rcx, qword ptr ar_OffsetHomeRDX[rbp]

_AllocHomeArea
call CreateFileW
test rax, rax
cmp rax, INVALID_HANDLE_VALUE
jz @fail
mov qword ptr ar_hFile[rbp], rax

Expand All @@ -47,6 +50,9 @@ add_resource proc frame
jz @fail
mov qword ptr ar_buffer[rbp], rax

; used for stack alignment
sub rsp, 3 * sizeof qword

; read the file content
push 0
lea r9, qword ptr LOCALS.Local4[rbp]
Expand All @@ -73,6 +79,9 @@ add_resource proc frame
jz @fail
mov qword ptr ar_hRes[rbp], rax

; used for stack alignment
sub rsp, 2 * sizeof qword

push qword ptr ar_file_size[rbp]
push qword ptr ar_buffer[rbp]
xor r9, r9
Expand Down Expand Up @@ -201,6 +210,9 @@ clone_file proc frame
cmp dword ptr LOCALS.Local3[rbp], 'lld.'
jne @f

; used for stack alignment
sub rsp, sizeof qword

; read the file content
push 0h
push 0h
Expand Down Expand Up @@ -229,6 +241,9 @@ clone_file proc frame
jz @fail
mov rdi, rax ; save buffer ptr

; used for stack alignment
sub rsp, 3 * sizeof qword

; read the file content
push 0
lea r9, qword ptr LOCALS.Local4[rbp]
Expand Down Expand Up @@ -262,12 +277,15 @@ clone_file proc frame
cmp rax, INVALID_SET_FILE_POINTER
je @fail

; used for stack alignment
sub rsp, 3 * sizeof qword

; write back the result
push 0h
mov rcx, rsi
mov rdx, rdi
mov r8, rbx
lea r9, qword ptr LOCALS.Local4[rbp]
push 0h
lea r9, qword ptr LOCALS.Local4[rbp]
_AllocHomeArea
call WriteFile
test rax, rax
Expand All @@ -281,7 +299,6 @@ clone_file proc frame
call heap_free
@@:


; return the name of the cloned file
mov rax, qword ptr LOCALS.Local2[rbp]

Expand Down
3 changes: 3 additions & 0 deletions TheMatrix/x64_console.inc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ print_buffer proc frame
test rax, rax
jz @fail

; used for stack alignment
sub rsp, 3 * sizeof qword

push 0
lea r9, LOCALS.Local1[rbp]
mov r8, qword ptr pb_OffsetHomeRDX[rbp]
Expand Down
2 changes: 1 addition & 1 deletion TheMatrix/x86_builder.inc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ add_resource proc file_to_add:ptr char , target_file:ptr char

; get file to add size
invoke CreateFileW, file_to_add, GENERIC_READ, 0h, 0h, OPEN_EXISTING, 0h, 0h
cmp eax, ERROR_FILE_NOT_FOUND
cmp eax, INVALID_HANDLE_VALUE
je @fail
mov hFile, eax
invoke GetFileSize, hFile, 0h
Expand Down

0 comments on commit 610ca84

Please sign in to comment.