ref: ebbcb610d3223a15ceb39b7fddc899b5012d8892
parent: 10405d31c608fe9d448607545195b758b424e70c
author: 9ferno <[email protected]>
date: Fri Jan 28 04:59:46 EST 2022
use macros for address bounds checking
--- a/os/pc64/bindings.s
+++ b/os/pc64/bindings.s
@@ -117,8 +117,6 @@
* Hence, need 40 bytes on the stack
* if fd == 0 and read return value == 0 == End of file, terminate
*/
-TEXT sysread(SB), 1, $40 /* ( fd a n -- n2 ) */
-
PUSH(TOP) /* ( fd a n n ) */
MOVQ 8(PSP), CX /* CX = a ( fd a n n ) */
PUSH(CX) /* ( fd a n a n ) */
@@ -125,6 +123,20 @@
CALL validatebuffer(SB) /* ( fd a n a n -- fd a n ) */
+#define CHECKBUFFER \
+ MOVQ TOP, CX; \
+ CMPQ CX, $0; /* negative n? */\
+ JLT belowum; /* TODO have an appropriate error message */\
+ ADDQ (PSP), CX; /* CX = a+n */\
+ CMPQ CX, UME; /* a+n, UME */\
+ JGT aboveume; /* a+n > UME */\
+ CMPQ (PSP), UM; /* a, UM */\
+ JLT belowum; /* a < UM */
+
+TEXT sysread(SB), 1, $40 /* ( fd a n -- n2 ) */
+
+ CHECKBUFFER;
+
MOVQ 8(PSP), CX
MOVQ CX, 32(SP) /* storing the fd to double check later */
MOVQ UM, 24(SP)
@@ -157,11 +169,8 @@
* Hence, need 32 bytes on the stack
*/
TEXT syswrite(SB), 1, $32 /* ( fd a n -- n2|-1 ) */
- PUSH(TOP) /* ( fd a n n ) */
- MOVQ 8(PSP), CX /* CX = a ( fd a n n ) */
- PUSH(CX) /* ( fd a n a n ) */
- CALL validatebuffer(SB) /* ( fd a n a n -- fd a n ) */
+ CHECKBUFFER
MOVQ UM, 24(SP)
F_TO_C_3
--- a/os/pc64/forth.s
+++ b/os/pc64/forth.s
@@ -220,15 +220,19 @@
fetch, store, cfetch and cstore to speed up these words
(a || UM) && ~UME
*/
+#define CHECKADDRESS \
+ CMPQ TOP, UME; \
+ JGT aboveume; /* a > UME */\
+ CMPQ TOP, UM;\
+ JLT belowum; /* a < UM */
+
TEXT fetch(SB), 1, $-4 /* ( a -- n) */
- PUSH(TOP)
- CALL validateaddress(SB) /* a a -- a */
+ CHECKADDRESS
MOVQ (TOP), TOP
NEXT
TEXT store(SB), 1, $-4 /* ( n a -- ) */
- PUSH(TOP)
- CALL validateaddress(SB) /* a a -- a */
+ CHECKADDRESS
POP(CX)
MOVQ CX, (TOP)
POP(TOP)
@@ -235,8 +239,7 @@
NEXT
TEXT cfetch(SB), 1, $-4 /* ( a -- c ) */
- PUSH(TOP)
- CALL validateaddress(SB) /* a a -- a */
+ CHECKADDRESS
XORQ CX, CX
MOVB (TOP), CL
MOVQ CX, TOP
@@ -243,8 +246,7 @@
NEXT
TEXT cstore(SB), 1, $-4 /* ( c a -- ) */
- PUSH(TOP)
- CALL validateaddress(SB) /* c a a -- c a */
+ CHECKADDRESS
POP(CX)
MOVB CL, (TOP)
POP(TOP)