-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add macro to check if pointer points into array
- Loading branch information
1 parent
9b11f44
commit 6a4b1fb
Showing
5 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters