Skip to content

Commit

Permalink
Add macro to check if pointer points into array
Browse files Browse the repository at this point in the history
  • Loading branch information
ChillerDragon committed Aug 10, 2024
1 parent 9b11f44 commit 6a4b1fb
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/check_bounds.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
_check_bounds:
; check_bounds [rax] [rdi] [rsi] [rdx]
; rax = pointer to check
; rdi = array
; rsi = element size (in bytes)
; rdx = array size (in elements)
push_registers

; rcx size of array in bytes
mov rcx, rdx
imul rcx, rsi
; rbx is end of array
mov rbx, rdi
add rbx, rcx

._check_bounds_check_oob_left:
cmp rax, rdi
jl ._check_bounds_oob_left

._check_bounds_check_oob_right:
; todo: is this jg or jge?
cmp rax, rbx
jg ._check_bounds_oob_right


._check_bounds_check_element_align:

jmp ._check_bounds_end
._check_bounds_oob_left:
puts "[error] array pointer out of bounds. (pointer too low)"
jmp ._check_bounds_error

._check_bounds_oob_right:
puts "[error] array pointer out of bounds. (pointer too high)"
jmp ._check_bounds_error

._check_bounds_error:
printlnf " pointer: %p", rax
printlnf " array start: %p", rdi
printlnf " array end: %p", rbx
exit 1

._check_bounds_end:
pop_registers
ret

24 changes: 24 additions & 0 deletions src/macros.asm
Original file line number Diff line number Diff line change
@@ -1,3 +1,27 @@
%macro check_bounds 4
; check_bounds [ptr into array] [array] [element size in bytes] [array size in elements]
push rbp

mov rbp, rsp
sub rsp, 32


mov qword [rbp-32], %1
mov qword [rbp-24], %2
mov qword [rbp-16], %3
mov qword [rbp-8], %4

mov rax, [rbp-32]
mov rdi, [rbp-24]
mov rsi, [rbp-16]
mov rdx, [rbp-8]
call _check_bounds

mov rsp, rbp

pop rbp
%endmacro check_bounds

%macro str_to_stack 1
; str_to_stack [fixed str]
; returns into rax a pointer to the stack
Expand Down
1 change: 1 addition & 0 deletions src/teeworlds_asmr.asm
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ section .text
%include "src/printf.asm"
%include "src/logger.asm"
%include "src/hex.asm"
%include "src/check_bounds.asm"
%include "src/terminal.asm"
%include "src/udp.asm"
%include "src/packer.asm"
Expand Down
9 changes: 9 additions & 0 deletions tests/array_oob_test.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
%include "tests/assert.asm"

_start:
; rax is pointer to 2nd element in the array
mov rax, huff_nodes
add rax, HUFF_CNODE_SIZE
check_bounds rax, huff_nodes, HUFF_CNODE_SIZE, HUFFMAN_MAX_NODES
exit 0

1 change: 1 addition & 0 deletions tests/assert.asm
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ section .text
%include "src/printf.asm"
%include "src/logger.asm"
%include "src/hex.asm"
%include "src/check_bounds.asm"
%include "src/udp.asm"
%include "src/packer.asm"
%include "src/packet.asm"
Expand Down

0 comments on commit 6a4b1fb

Please sign in to comment.