about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKartik Agaram <vc@akkartik.com>2020-06-15 17:00:26 -0700
committerKartik Agaram <vc@akkartik.com>2020-06-15 17:13:04 -0700
commitd292196ff5ab9f103364e05c42a1edc03dc1b5ab (patch)
tree2e80472fd9d248f3631903aaf2282f7e0831a075
parent5a6d2d0db7ccc1d6b09ce898e3b57f62a5b1b787 (diff)
downloadmu-d292196ff5ab9f103364e05c42a1edc03dc1b5ab.tar.gz
6529 - don't let `addr`s escape functions
I've gone back and forth on this. I initially disallowed this, then allowed
it because I forgot why I disallowed it. The reason to disallow it: if
you return an `addr` to a variable allocated on the stack, the space might
be reused for a different type, which violates type-safety. And once you
can reinterpret bits of one type as another you lose memory-safety as well.

This has some interesting implications for Mu programs; certain kinds of
helper functions become impossible to write. Now I find myself relying a
lot more on scopes (and editor folding support) for abstracting details.
And they won't help manage duplication. We'll see how this goes.

While I'm being draconian about `addr`s on the stack, I'm still abusing
`addr`s on the heap, with the expectation that future checks on reclamation
will protect me. The boon and bane of stack space is that it's constantly
reclaimed.
-rw-r--r--apps/browse.mu40
-rw-r--r--apps/mu.subx3
2 files changed, 24 insertions, 19 deletions
diff --git a/apps/browse.mu b/apps/browse.mu
index 10dd65b1..4a7ab2c1 100644
--- a/apps/browse.mu
+++ b/apps/browse.mu
@@ -6,9 +6,27 @@
 #
 # Press 'q' to quit. All other keys scroll down.
 
-fn main args: (addr array (addr array byte)) -> exit-status/ebx: int {
-  var filename/eax: (addr array byte) <- first-arg args
-  var file/esi: (addr buffered-file) <- load-file filename
+fn main args-on-stack: (addr array (addr array byte)) -> exit-status/ebx: int {
+  # var file/esi: (addr buffered-file) = open args-on-stack[1] for reading {{{
+  var file/esi: (addr buffered-file) <- copy 0
+  {
+    var file-handle: (handle buffered-file)
+    {
+      var address-of-file-handle/esi: (addr handle buffered-file) <- address file-handle
+      # var filename/ecx: (addr array byte) = args-on-stack[1] {{{
+      var filename/ecx: (addr array byte) <- copy 0
+      {
+        var args/eax: (addr array (addr array byte)) <- copy args-on-stack
+        var tmp/eax: (addr addr array byte) <- index args, 1
+        filename <- copy *tmp
+      }
+      # }}}
+      open filename, 0, address-of-file-handle
+    }
+    var tmp/eax: (addr buffered-file) <- lookup file-handle
+    file <- copy tmp
+  }
+  # }}}
   enable-screen-grid-mode
   var nrows/eax: int <- copy 0
   var ncols/ecx: int <- copy 0
@@ -154,22 +172,6 @@ fn clear toprow: int, leftcol: int, botrow: int, rightcol: int {
   }
 }
 
-fn first-arg args-on-stack: (addr array (addr array byte)) -> out/eax: (addr array byte) {
-  var args/eax: (addr array (addr array byte)) <- copy args-on-stack
-  var result/eax: (addr addr array byte) <- index args, 1
-  out <- copy *result
-}
-
-fn load-file filename: (addr array byte) -> out/esi: (addr buffered-file) {
-  var result: (handle buffered-file)
-  {
-    var tmp1/eax: (addr handle buffered-file) <- address result
-    open filename, 0, tmp1
-  }
-  var tmp2/eax: (addr buffered-file) <- lookup result
-  out <- copy tmp2
-}
-
 fn dump in: (addr buffered-file) {
   var c/eax: byte <- read-byte-buffered in
   compare c, 0xffffffff  # EOF marker
diff --git a/apps/mu.subx b/apps/mu.subx
index 70ad482d..cd08466e 100644
--- a/apps/mu.subx
+++ b/apps/mu.subx
@@ -4638,6 +4638,9 @@ $parse-mu:error2:
 # ✓ fn foo x: int {
 # ✓ fn foo x: int {
 # ✓ fn foo x: int -> y/eax: int {
+# TODO:
+#   disallow outputs of type `(... addr ...)`
+#   disallow inputs of type `(... addr ... addr ...)`
 populate-mu-function-header:  # first-line: (addr stream byte), out: (addr function), vars: (addr stack live-var), err: (addr buffered-file), ed: (addr exit-descriptor)
     # pseudocode:
     #   var name: slice
ss='oid'>007b965 ^
0d52962 ^

007b965 ^
0d52962 ^



1bdb931 ^
e2696fc ^

188bbc7 ^
1bdb931 ^
a655c21 ^


1bdb931 ^



a655c21 ^
95d88a8 ^
1bdb931 ^
a655c21 ^
3850fba ^
95d88a8 ^
3850fba ^
e2696fc ^
1bdb931 ^


2ae9cac ^
9656e13 ^
69c88da ^
3114176 ^
0d52962 ^
188bbc7 ^
9d792a2 ^

0d52962 ^

f2299cb ^

0d52962 ^

2ae9cac ^
188bbc7 ^

2ae9cac ^
4ad51d6 ^


2ae9cac ^
188bbc7 ^

5b91af1 ^
2ae9cac ^
5b91af1 ^
0d52962 ^

2ae9cac ^
188bbc7 ^



0d52962 ^







188bbc7 ^




1d3c9f4 ^
44fb3ec ^
188bbc7 ^
0d52962 ^
b95206f ^
a655c21 ^

0d52962 ^

a655c21 ^
0d52962 ^

007b965 ^
a655c21 ^
0d52962 ^
188bbc7 ^
d61b5df ^
0d52962 ^
a655c21 ^
0d52962 ^

188bbc7 ^




0d52962 ^







a655c21 ^

0d52962 ^
a655c21 ^
0d52962 ^
007b965 ^
a655c21 ^
0d52962 ^
a655c21 ^
0d52962 ^
a655c21 ^
e77157d ^
0d52962 ^
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163