about summary refs log tree commit diff stats
path: root/subx
diff options
context:
space:
mode:
authorKartik Agaram <vc@akkartik.com>2019-05-11 22:44:49 -0700
committerKartik Agaram <vc@akkartik.com>2019-05-11 22:44:49 -0700
commite59a74abddaf0b4f9a39432da21c6a98eae46df6 (patch)
treeb21109b5889dcdb9011e5a8559e7fb8e0ba00c4c /subx
parent6362c51d11ef27335875469e0ff06347357f46b1 (diff)
downloadmu-e59a74abddaf0b4f9a39432da21c6a98eae46df6.tar.gz
5156 - error-checking on writes to file
Pretty blunt for now; just abort the entire program on any failure to write.

I'm encountering it because I'm somehow treating a stream address as a
file descriptor. Maybe mmap is returning addresses below 0x08000000?
Diffstat (limited to 'subx')
-rw-r--r--subx/050_write.subx11
-rw-r--r--subx/062write-stream.subx18
-rwxr-xr-xsubx/apps/assortbin21700 -> 21809 bytes
-rwxr-xr-xsubx/apps/crenshaw2-1bin18859 -> 18968 bytes
-rwxr-xr-xsubx/apps/crenshaw2-1bbin19418 -> 19527 bytes
-rwxr-xr-xsubx/apps/factorialbin17775 -> 17884 bytes
-rwxr-xr-xsubx/apps/handlebin18602 -> 18711 bytes
-rwxr-xr-xsubx/apps/hexbin21868 -> 21977 bytes
-rwxr-xr-xsubx/apps/packbin36460 -> 36569 bytes
9 files changed, 29 insertions, 0 deletions
diff --git a/subx/050_write.subx b/subx/050_write.subx
index 083adad6..0d6b8152 100644
--- a/subx/050_write.subx
+++ b/subx/050_write.subx
@@ -32,6 +32,9 @@ _write:  # fd : int, s : (address array byte) -> <void>
     # . syscall
     b8/copy-to-EAX  4/imm32/write
     cd/syscall  0x80/imm8
+    # if (EAX < 0) abort
+    3d/compare-EAX-with  0/imm32
+    0f 8c/jump-if-lesser  $_write:abort/disp32
 $_write:end:
     # . restore registers
     5b/pop-to-EBX
@@ -43,4 +46,12 @@ $_write:end:
     5d/pop-to-EBP
     c3/return
 
+$_write:abort:
+    # can't write a message here for risk of an infinite loop, so we'll use a special exit code instead
+    # . syscall(exit, 255)
+    bb/copy-to-EBX  0xff/imm32
+    b8/copy-to-EAX  1/imm32/exit
+    cd/syscall  0x80/imm8
+    # never gets here
+
 # . . vim:nowrap:textwidth=0
diff --git a/subx/062write-stream.subx b/subx/062write-stream.subx
index eb2df0c5..83268422 100644
--- a/subx/062write-stream.subx
+++ b/subx/062write-stream.subx
@@ -102,6 +102,9 @@ _write-stream:  # fd : int, s : (address stream) -> <void>
     # . . syscall
     b8/copy-to-EAX  4/imm32/write
     cd/syscall  0x80/imm8
+    # if (EAX < 0) abort
+    3d/compare-EAX-with  0/imm32
+    0f 8c/jump-if-lesser  $_write-stream:abort/disp32
     # s->read += EAX
     01/add                          1/mod/*+disp8   6/rm32/ESI    .           .             .           0/r32/EAX   4/disp8         .                 # add EAX to *(ESI+4)
     # . restore registers
@@ -116,6 +119,21 @@ _write-stream:  # fd : int, s : (address stream) -> <void>
     5d/pop-to-EBP
     c3/return
 
+$_write-stream:abort:
+    # . _write(2/stderr, error)
+    # . . push args
+    68/push  "_write-stream: failed to write to file"/imm32
+    68/push  2/imm32/stderr
+    # . . call
+    e8/call  _write/disp32
+    # . . discard args
+    81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
+    # . syscall(exit, 1)
+    bb/copy-to-EBX  1/imm32
+    b8/copy-to-EAX  1/imm32/exit
+    cd/syscall  0x80/imm8
+    # never gets here
+
 test-write-stream-single:
     # setup
     # . clear-stream(_test-stream)
diff --git a/subx/apps/assort b/subx/apps/assort
index af7b47c2..3fbc3b7e 100755
--- a/subx/apps/assort
+++ b/subx/apps/assort
Binary files differdiff --git a/subx/apps/crenshaw2-1 b/subx/apps/crenshaw2-1
index d8c36cab..ca1e52fa 100755
--- a/subx/apps/crenshaw2-1
+++ b/subx/apps/crenshaw2-1
Binary files differdiff --git a/subx/apps/crenshaw2-1b b/subx/apps/crenshaw2-1b
index 66bd4fa8..8a468ff5 100755
--- a/subx/apps/crenshaw2-1b
+++ b/subx/apps/crenshaw2-1b
Binary files differdiff --git a/subx/apps/factorial b/subx/apps/factorial
index 0db812db..9ea9716b 100755
--- a/subx/apps/factorial
+++ b/subx/apps/factorial
Binary files differdiff --git a/subx/apps/handle b/subx/apps/handle
index 7869c416..b32183db 100755
--- a/subx/apps/handle
+++ b/subx/apps/handle
Binary files differdiff --git a/subx/apps/hex b/subx/apps/hex
index a07214ea..ed1d9eeb 100755
--- a/subx/apps/hex
+++ b/subx/apps/hex
Binary files differdiff --git a/subx/apps/pack b/subx/apps/pack
index 30bacf4d..a4a20626 100755
--- a/subx/apps/pack
+++ b/subx/apps/pack
Binary files differ