shithub: 9ferno

Download patch

ref: ebbcb610d3223a15ceb39b7fddc899b5012d8892
parent: 10405d31c608fe9d448607545195b758b424e70c
author: 9ferno <gophone2015@gmail.com>
date: Fri Jan 28 11:59:46 EST 2022

use macros for address bounds checking

--- a/os/pc64/bindings.s	Thu Jan 27 18:09:37 2022
+++ b/os/pc64/bindings.s	Fri Jan 28 11:59:46 2022
@@ -117,14 +117,26 @@
  * 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 ) */
 
 	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	Thu Jan 27 18:09:37 2022
+++ b/os/pc64/forth.s	Fri Jan 28 11:59:46 2022
@@ -220,31 +220,33 @@
 	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)
 	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
 	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)