about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKartik Agaram <vc@akkartik.com>2020-11-05 19:08:44 -0800
committerKartik Agaram <vc@akkartik.com>2020-11-05 19:08:44 -0800
commit7a25625c43415d863f676de49aff6438aab6911d (patch)
treee935cdaa1fc361e7a0d20c197d5c0189091759a6
parentf12ea75a76f3a064ffde4ec6a80aacb3597ed733 (diff)
downloadmu-7a25625c43415d863f676de49aff6438aab6911d.tar.gz
7188 - raise error on deref of var on stack
-rwxr-xr-xapps/mubin542629 -> 543656 bytes
-rw-r--r--apps/mu.subx75
2 files changed, 74 insertions, 1 deletions
diff --git a/apps/mu b/apps/mu
index 8d1b0c60..c8daea22 100755
--- a/apps/mu
+++ b/apps/mu
Binary files differdiff --git a/apps/mu.subx b/apps/mu.subx
index d25b0005..7da28859 100644
--- a/apps/mu.subx
+++ b/apps/mu.subx
@@ -2832,6 +2832,51 @@ test-convert-function-with-local-var-dereferenced:
     5d/pop-to-ebp
     c3/return
 
+test-dereference-of-var-on-stack:
+    # . prologue
+    55/push-ebp
+    89/<- %ebp 4/r32/esp
+    # setup
+    (clear-stream _test-input-stream)
+    (clear-stream $_test-input-buffered-file->buffer)
+    (clear-stream _test-output-stream)
+    (clear-stream $_test-output-buffered-file->buffer)
+    (clear-stream _test-error-stream)
+    (clear-stream $_test-error-buffered-file->buffer)
+    # var ed/edx: exit-descriptor = tailor-exit-descriptor(16)
+    68/push 0/imm32
+    68/push 0/imm32
+    89/<- %edx 4/r32/esp
+    (tailor-exit-descriptor %edx 0x10)
+    #
+    (write _test-input-stream "fn foo {\n")
+    (write _test-input-stream "  var x: (addr int)\n")
+    (write _test-input-stream "  increment *x\n")
+    (write _test-input-stream "}\n")
+    # convert
+    (convert-mu _test-input-buffered-file _test-output-buffered-file _test-error-buffered-file %edx)
+    # registers except esp clobbered at this point
+    # restore ed
+    89/<- %edx 4/r32/esp
+    (flush _test-output-buffered-file)
+    (flush _test-error-buffered-file)
+#?     # dump _test-error-stream {{{
+#?     (write 2 "^")
+#?     (write-stream 2 _test-error-stream)
+#?     (write 2 "$\n")
+#?     (rewind-stream _test-error-stream)
+#?     # }}}
+    # check output
+    (check-stream-equal _test-output-stream  ""  "F - test-dereference-of-var-on-stack: output should be empty")
+    (check-next-stream-line-equal _test-error-stream  "fn foo: cannot dereference var 'x' on stack"  "F - test-dereference-of-var-on-stack: error message")
+    # check that stop(1) was called
+    (check-ints-equal *(edx+4) 2 "F - test-dereference-of-var-on-stack: exit status")
+    # don't restore from ebp
+    81 0/subop/add %esp 8/imm32
+    # . epilogue
+    5d/pop-to-ebp
+    c3/return
+
 # variables of type 'byte' are not allowed on the stack
 test-convert-function-with-byte-operations:
     # . prologue
@@ -16019,6 +16064,18 @@ $add-operation-and-inputs-to-stmt:inout-is-deref:
         ba/copy-to-edx 1/imm32/true
       }
       (lookup-var-or-literal %ecx *(ebp+0x10) %esi *(ebp+0x14) *(ebp+0x18) *(ebp+0x1c))
+      # if is-deref? is true and out is on the stack, abort
+      81 7/subop/compare %edx 0/imm32/false
+      {
+        74/jump-if-= break/disp8
+        (lookup *esi *(esi+4))  # => eax
+        81 7/subop/compare *(eax+0x18) 0/imm32  # Var-register
+        75/jump-if-!= break/disp8
+        (lookup *(eax+8) *(eax+0xc))  # Var-type Var-type => eax
+        (is-simple-mu-type? %eax 0)  # literal => eax
+        3d/compare-eax-and 0/imm32/false
+        0f 84/jump-if-= $add-operation-and-inputs-to-stmt:error-deref-on-stack/disp32
+      }
 $add-operation-and-inputs-to-stmt:save-var:
       8d/copy-address *(edi+0xc) 0/r32/eax
       (append-stmt-var Heap  *esi *(esi+4)  *(edi+0xc) *(edi+0x10)  %edx  %eax)  # Stmt1-inouts or Regvardef-inouts
@@ -16054,6 +16111,22 @@ $add-operation-and-inputs-to-stmt:abort:
     (stop *(ebp+0x1c) 1)
     # never gets here
 
+$add-operation-and-inputs-to-stmt:error-deref-on-stack:
+    # error("fn ___: invalid identifier in '" line "'\n")
+    (write-buffered *(ebp+0x18) "fn ")
+    8b/-> *(ebp+0x14) 0/r32/eax
+    (lookup *eax *(eax+4))  # Function-name Function-name => eax
+    (write-buffered *(ebp+0x18) %eax)
+    (rewind-stream *(ebp+0xc))
+    (write-buffered *(ebp+0x18) ": cannot dereference var '")
+    (lookup *esi *(esi+4))  # => eax
+    (lookup *eax *(eax+4))  # Var-name Var-name => eax
+    (write-buffered *(ebp+0x18) %eax)
+    (write-buffered *(ebp+0x18) "' on stack\n")
+    (flush *(ebp+0x18))
+    (stop *(ebp+0x1c) 1)
+    # never gets here
+
 stmt-has-outputs?:  # line: (addr stream byte) -> result/eax: boolean
     # . prologue
     55/push-ebp
@@ -31567,7 +31640,7 @@ type-category:  # a: (addr type-tree) -> result/eax: int
     # . save registers
     51/push-ecx
     # var lit?/ecx: boolean = is-literal-type?(a)
-    (is-simple-mu-type? *(ebp+8) 0)  # => eax
+    (is-simple-mu-type? *(ebp+8) 0)  # literal => eax
     89/<- %ecx 0/r32/eax
     # var float?/eax: int = is-float?(a)
     (is-simple-mu-type? *(ebp+8) 0xf)  # => eax