Commit cbf7cbf
authored
Introduce strongly-typed system primitives (#1561)
* Introduce strongly-typed system primitives
This commit does a lot of reshuffling and even some more. It introduces
strongly-typed system primitives which are: `OsFile`, `OsDir`, `Stdio`,
and `OsOther`. Those primitives are separate structs now, each implementing
a subset of `Handle` methods, rather than all being an enumeration of some
supertype such as `OsHandle`. To summarise the structs:
* `OsFile` represents a regular file, and implements fd-ops
of `Handle` trait
* `OsDir` represents a directory, and primarily implements path-ops, plus
`readdir` and some common fd-ops such as `fdstat`, etc.
* `Stdio` represents a stdio handle, and implements a subset of fd-ops
such as `fdstat` _and_ `read_` and `write_vectored` calls
* `OsOther` currently represents anything else and implements a set similar
to that implemented by `Stdio`
This commit is effectively an experiment and an excercise into better
understanding what's going on for each OS resource/type under-the-hood.
It's meant to give us some intuition in order to move on with the idea
of having strongly-typed handles in WASI both in the syscall impl as well
as at the libc level.
Some more minor changes include making `OsHandle` represent an OS-specific
wrapper for a raw OS handle (Unix fd or Windows handle). Also, since `OsDir`
is tricky across OSes, we also have a supertype of `OsHandle` called
`OsDirHandle` which may store a `DIR*` stream pointer (mainly BSD). Last but not
least, the `Filetype` and `Rights` are now computed when the resource is created,
rather than every time we call `Handle::get_file_type` and `Handle::get_rights`.
Finally, in order to facilitate the latter, I've converted `EntryRights` into
`HandleRights` and pushed them into each `Handle` implementor.
* Do not adjust rights on Stdio
* Clean up testing for TTY and escaping writes
* Implement AsFile for dyn Handle
This cleans up a lot of repeating boilerplate code todo with
dynamic dispatch.
* Delegate definition of OsDir to OS-specific modules
Delegates defining `OsDir` struct to OS-specific modules (BSD, Linux,
Emscripten, Windows). This way, `OsDir` can safely re-use `OsHandle`
for raw OS handle storage, and can store some aux data such as an
initialized stream ptr in case of BSD. As a result, we can safely
get rid of `OsDirHandle` which IMHO was causing unnecessary noise and
overcomplicating the design. On the other hand, delegating definition
of `OsDir` to OS-specific modules isn't super clean in and of itself
either. Perhaps there's a better way of handling this?
* Check if filetype of OS handle matches WASI filetype when creating
It seems prudent to check if the passed in `File` instance is of
type matching that of the requested WASI filetype. In other words,
we'd like to avoid situations where `OsFile` is created from a
pipe.
* Make AsFile fallible
Return `EBADF` in `AsFile` in case a `Handle` cannot be made into
a `std::fs::File`.
* Remove unnecessary as_file conversion
* Remove unnecessary check for TTY for Stdio handle type
* Fix incorrect stdio ctors on Unix
* Split Stdio into three separate types: Stdin, Stdout, Stderr
* Rename PendingEntry::File to PendingEntry::OsHandle to avoid confusion
* Rename OsHandle to RawOsHandle
Also, since `RawOsHandle` on *nix doesn't need interior mutability
wrt the inner raw file descriptor, we can safely swap the `RawFd`
for `File` instance.
* Add docs explaining what OsOther is
* Allow for stdio to be non-character-device (e.g., piped)
* Return error on bad preopen rather than panic1 parent 528d3c1 commit cbf7cbf
39 files changed
Lines changed: 1635 additions & 1065 deletions
File tree
- crates/wasi-common/src
- snapshots
- sys
- unix
- bsd
- emscripten
- linux
- windows
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
4 | | - | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
5 | 7 | | |
6 | 8 | | |
7 | 9 | | |
8 | 10 | | |
9 | 11 | | |
10 | 12 | | |
| 13 | + | |
11 | 14 | | |
12 | 15 | | |
13 | 16 | | |
| |||
32 | 35 | | |
33 | 36 | | |
34 | 37 | | |
35 | | - | |
36 | | - | |
37 | | - | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
38 | 41 | | |
39 | 42 | | |
40 | 43 | | |
| |||
43 | 46 | | |
44 | 47 | | |
45 | 48 | | |
46 | | - | |
47 | | - | |
| 49 | + | |
| 50 | + | |
48 | 51 | | |
49 | 52 | | |
50 | 53 | | |
| |||
53 | 56 | | |
54 | 57 | | |
55 | 58 | | |
56 | | - | |
| 59 | + | |
57 | 60 | | |
58 | | - | |
| 61 | + | |
59 | 62 | | |
60 | 63 | | |
61 | 64 | | |
| |||
105 | 108 | | |
106 | 109 | | |
107 | 110 | | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
108 | 126 | | |
109 | 127 | | |
110 | 128 | | |
111 | 129 | | |
112 | 130 | | |
113 | | - | |
| 131 | + | |
114 | 132 | | |
115 | 133 | | |
116 | 134 | | |
117 | 135 | | |
118 | 136 | | |
119 | 137 | | |
120 | 138 | | |
121 | | - | |
122 | | - | |
123 | | - | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
124 | 142 | | |
125 | 143 | | |
126 | 144 | | |
| |||
167 | 185 | | |
168 | 186 | | |
169 | 187 | | |
170 | | - | |
| 188 | + | |
171 | 189 | | |
172 | 190 | | |
173 | 191 | | |
174 | 192 | | |
175 | 193 | | |
176 | | - | |
| 194 | + | |
177 | 195 | | |
178 | 196 | | |
179 | 197 | | |
180 | | - | |
| 198 | + | |
181 | 199 | | |
182 | | - | |
| 200 | + | |
183 | 201 | | |
184 | 202 | | |
185 | 203 | | |
186 | 204 | | |
187 | 205 | | |
188 | | - | |
189 | | - | |
190 | | - | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
191 | 209 | | |
192 | 210 | | |
193 | 211 | | |
| |||
231 | 249 | | |
232 | 250 | | |
233 | 251 | | |
234 | | - | |
| 252 | + | |
235 | 253 | | |
236 | 254 | | |
237 | 255 | | |
238 | 256 | | |
239 | 257 | | |
240 | | - | |
| 258 | + | |
241 | 259 | | |
242 | 260 | | |
243 | 261 | | |
244 | 262 | | |
245 | 263 | | |
246 | | - | |
| 264 | + | |
247 | 265 | | |
248 | 266 | | |
249 | 267 | | |
250 | 268 | | |
251 | 269 | | |
252 | | - | |
253 | | - | |
254 | | - | |
255 | | - | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
256 | 278 | | |
257 | 279 | | |
258 | 280 | | |
| |||
277 | 299 | | |
278 | 300 | | |
279 | 301 | | |
280 | | - | |
281 | | - | |
282 | | - | |
283 | | - | |
284 | | - | |
285 | | - | |
286 | | - | |
287 | | - | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
288 | 314 | | |
289 | 315 | | |
290 | 316 | | |
291 | | - | |
| 317 | + | |
292 | 318 | | |
293 | 319 | | |
294 | 320 | | |
| |||
336 | 362 | | |
337 | 363 | | |
338 | 364 | | |
339 | | - | |
340 | | - | |
| 365 | + | |
| 366 | + | |
341 | 367 | | |
342 | 368 | | |
343 | 369 | | |
344 | 370 | | |
345 | | - | |
346 | | - | |
347 | | - | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
348 | 375 | | |
349 | 376 | | |
350 | 377 | | |
| |||
353 | 380 | | |
354 | 381 | | |
355 | 382 | | |
356 | | - | |
357 | | - | |
358 | | - | |
359 | | - | |
360 | | - | |
361 | | - | |
362 | | - | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
363 | 386 | | |
364 | 387 | | |
365 | 388 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
| 1 | + | |
| 2 | + | |
3 | 3 | | |
4 | | - | |
5 | 4 | | |
6 | 5 | | |
7 | 6 | | |
8 | | - | |
9 | 7 | | |
10 | 8 | | |
11 | 9 | | |
| |||
33 | 31 | | |
34 | 32 | | |
35 | 33 | | |
36 | | - | |
37 | | - | |
| 34 | + | |
38 | 35 | | |
39 | | - | |
40 | | - | |
| 36 | + | |
| 37 | + | |
41 | 38 | | |
42 | | - | |
| 39 | + | |
43 | 40 | | |
44 | | - | |
45 | 41 | | |
46 | | - | |
47 | 42 | | |
48 | 43 | | |
49 | 44 | | |
50 | 45 | | |
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 | | - | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
76 | 49 | | |
77 | | - | |
78 | | - | |
| 50 | + | |
| 51 | + | |
79 | 52 | | |
80 | 53 | | |
81 | 54 | | |
82 | | - | |
83 | | - | |
84 | | - | |
| 55 | + | |
| 56 | + | |
85 | 57 | | |
86 | | - | |
87 | 58 | | |
88 | | - | |
89 | | - | |
90 | | - | |
91 | | - | |
92 | | - | |
93 | | - | |
94 | | - | |
| 59 | + | |
| 60 | + | |
95 | 61 | | |
96 | | - | |
97 | 62 | | |
98 | | - | |
99 | | - | |
100 | | - | |
101 | | - | |
102 | | - | |
103 | | - | |
104 | | - | |
105 | | - | |
106 | | - | |
107 | | - | |
| 63 | + | |
| 64 | + | |
108 | 65 | | |
109 | 66 | | |
110 | | - | |
| 67 | + | |
111 | 68 | | |
112 | 69 | | |
113 | | - | |
| 70 | + | |
114 | 71 | | |
115 | | - | |
| 72 | + | |
116 | 73 | | |
117 | 74 | | |
118 | | - | |
| 75 | + | |
119 | 76 | | |
120 | 77 | | |
121 | 78 | | |
122 | 79 | | |
123 | | - | |
| 80 | + | |
124 | 81 | | |
125 | 82 | | |
126 | 83 | | |
127 | | - | |
128 | | - | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
129 | 87 | | |
130 | 88 | | |
131 | 89 | | |
132 | 90 | | |
133 | 91 | | |
134 | | - | |
| 92 | + | |
135 | 93 | | |
136 | 94 | | |
137 | 95 | | |
138 | 96 | | |
139 | | - | |
140 | | - | |
141 | | - | |
142 | | - | |
143 | | - | |
144 | | - | |
145 | | - | |
146 | | - | |
147 | | - | |
148 | | - | |
149 | | - | |
150 | 97 | | |
0 commit comments