- Write a value to the heap outside 0-255.
- Write an entry to a word in Heap Manager outside 0-255.
- Read a manager entry with a value of 0.
In all cases, I want it to fail before writing. In the case of writing to the heap, if it writes outside the valid area of the heap, it could corrupt the Heap Manager (which sits on top of the heap.)
As for number 1:
MOV R6, R5, LSR#24 ; Load the size we just calculated as into R6.
ADD R6, R5 ; Add the offset to the size. If this is equal to or greater than Manager_Offset, we are out of bounds.
LDR R7, =Manager_Offset ;
CMP R7, R6
BCC write_error ; If R6 greater than or equal to R7, jump to write error.
Since an entry in the Heap Manager describes the location and size of the bytes to live on the heap, we can check whether this write will be OK by adding the size to the offset and comparing it with the managed heap size.
A fix for number 2 was already included in my code. I guess I saw that one coming. Here's the code:
find_zero_entry ADD R4, R4, #4 ; Increment R4 by one word.
CMP R4, R6 ; Compare R4 with ceiling.
BEQ write_error ; Reached end of heap manager without finding 0.
LDR R7, [R4] ; Load R7 with entry.
CMP R7, #0 ; Check whether entry is 0.
BNE find_zero_entry ; Continue loop.
There are two ways out of the loop. Either you fall through with a 0 value entry or you hit the top and jump to a write error.
Finally, for number 3, it's also already done. I did not remember writing it this way, but here is the code:
CMP R2, #0 ; Check for 0 value, meaning read is not valid
BEQ error_out
Attempting a read on a 0 value entry jumps to an error.
MORE POSSIBLE ERRORS
I've been assuming that the calling code would know not to ask for a value outside the size of the Heap Manager. Not really safe. Here's some simple code to check the input for a possible read:
LDR R2, =Manager_Offset ; Set R2 to Manager_Offset
CMP R2, R3 ; Compare entry to be read with manger offset
BCC read_error ; If value is too large, jump to error.
Finally, deleting a Heap Manager value is simple but could break things badly. For instance, it will write over a value in memory with 0 -- any value above the specified Heap_Manager value. Also, it writes over a value of 0 with a value of 0 without error.
Here's the revised delete routine, which checks to see that the input is not greater than the size of the manager.
routine_delete_value
; Takes offset of value to be deleted in R3.
; Returns 0xFFFFFFFF in R3 if there is a problem.
; Returns 0 in R3 for success.
; R3 input
; R2, R4
STMFD sp!, {R4, R14}
LDR R2, =Manager_Offset ; Set R2 to Manager_Offset - ONLY WORKS BECAUSE HEAP MANAGER = 4 TIMES MANAGED HEAP
CMP R2, R3 ; Compare entry to be read with manger offset
BCC delete_error ; If value is too large, jump to error.
LDR R4, =Heap_Manager ; Set R4 to Heap_Manager base.
MOV R3, R3, LSL#2 ; Multiply R3 by 4: Heap manager uses words, not bytes
ADD R4, R4, R3 ; Get address of heap_manager entry.
LDR R3, [R4] ; check the value at R4
CMP R3, #0 ; If value is 0, jump to delete error
BEQ delete_error
MOV R3, #0 ; Set heap manager entry to 0
STR R3, [R4]
LDMFD sp!, {R4, R14}
BX R14 ; Return
delete_error LDR R3, =0xFFFFFFFF
LDMFD sp!, {R4, R14}
BX R14 ; Return
No comments:
Post a Comment