shithub: 9ferno

Download patch

ref: 00e6f81a080c81828854db16160d8b58a5cd8ca9
parent: 83906bfb6b2102104cb242ad578caa994785f72f
author: 9ferno <gophone2015@gmail.com>
date: Tue Dec 14 23:42:56 EST 2021

check that the addresses are within UP and UPE

--- a/os/pc64/bindings.s	Tue Dec 14 22:28:54 2021
+++ b/os/pc64/bindings.s	Tue Dec 14 23:42:56 2021
@@ -88,41 +88,57 @@
 	STOREFORTH;
 
 TEXT	fthopen(SB), 1, $24	/* ( mode cstr -- fd ) */
-	PUSHQ UP
+	MOVQ UP, 16(SP)
 	F_TO_C_2
 	CALL kopen(SB)
-	POPQ UP
+	MOVQ 16(SP), UP
 	C_TO_F_1
 	NEXT
 
 TEXT	fthclose(SB), 1, $16	/* ( fd -- n ) */
-	PUSHQ UP
+	MOVQ UP, 8(SP)
 	F_TO_C_1
 	CALL kclose(SB)
-	POPQ UP
+	MOVQ 24(SP), UP
 	C_TO_F_1
 	NEXT
 
 TEXT	fthread(SB), 1, $32	/* ( n a fd -- n2 ) */
-	MOVQ (PSP), CX	/* address = start of heap + address */
-	ADDQ UP, CX
-	MOVQ CX, (PSP)
-	PUSHQ UP
+	MOVQ(PSP), CX
+	PUSH(TOP)
+	MOVQ CX, TOP	/* ( n a fd -- n a fd a ) */
+	CALL inup(SB)
+	MOVQ TOP, CX
+	POP(TOP)
+	CMPQ CX, $0
+	JNE	invalidaddress
+
+	MOVQ UP, 24(SP)
 	F_TO_C_3
 	CALL kread(SB)
-	POPQ UP
+	MOVQ 24(SP), UP
 	C_TO_F_1
 	NEXT
+invalidaddress:
+	ADDQ $16, PSP
+	MOVQ $-1, TOP
+	NEXT
 
 /* no link register in amd64
  * 3 arguments for kwrite = 24 bytes
  * 1 local for storing UP = 8 bytes
  * Hence, need 32 bytes on the stack
  */
-TEXT	fthwrite(SB), 1, $32	/* ( n a fd -- n2 ) */
-	MOVQ (PSP), CX	/* address = start of heap + address */
-	ADDQ UP, CX
-	MOVQ CX, (PSP)
+TEXT	fthwrite(SB), 1, $32	/* ( n a fd -- n2|-1 ) */
+	MOVQ(PSP), CX
+	PUSH(TOP)
+	MOVQ CX, TOP	/* ( n a fd -- n a fd a ) */
+	CALL inup(SB)
+	MOVQ TOP, CX
+	POP(TOP)
+	CMPQ CX, $0
+	JNE	invalidaddress
+
 	MOVQ UP, 24(SP)
 	F_TO_C_3
 	CALL kwrite(SB)
@@ -131,9 +147,9 @@
 	NEXT
 
 TEXT	fthseek(SB), 1, $32	/* ( type pos fd -- n ) */
-	PUSHQ UP
+	MOVQ UP, 24(SP)
 	F_TO_C_3
 	CALL kseek(SB)
-	POPQ UP
+	MOVQ 24(SP), UP
 	C_TO_F_1
 	NEXT
--- a/os/pc64/forth.s	Tue Dec 14 22:28:54 2021
+++ b/os/pc64/forth.s	Tue Dec 14 23:42:56 2021
@@ -141,6 +141,33 @@
 
 	NEXT
 
+/*
+callable by UP using forth macro entries to check address
+	( a -- -1|0|1 )
+	argument 1 in TOP = address
+	return value in TOP
+	-1			0			1
+	if UP < address < UPE
+		return 0	within range
+	else if address < UP
+		return -1	below UP
+	else if UPE < address
+		return 1	above UP
+ */
+TEXT	inup(SB), 1, $-4
+	CMPQ TOP, UPE
+	JGT aboveupe
+	CMPQ TOP, UP
+	JLT belowup
+	MOVQ $0, TOP
+	RET
+belowup:
+	MOVQ $-1, TOP
+	RET
+aboveupe:
+	MOVQ $1, TOP
+	RET
+
 TEXT	reset(SB), 1, $-4
 	MOVQ UP, RSP
 	ADDQ $RSTACK_END, RSP