.githooks/
compiler/
kernel/
lib/
scripts/
seed/
sublime/
test/
boot/
bootstrap/
compiler/
cycles/
dispatch/
format/
host/
integration/
kernel/
loader/
kernel/
grown.rad
1.2 KiB
hostile.rad
15.0 KiB
loaded.rad
586 B
machine.ras
731 B
manifests/
mmio/
native/
packages/
runtime/
scheduling/
shared/
slots/
smp/
std/
sync/
termination/
tests/
trap/
verify/
Makefile
9.2 KiB
kernel.rad
776 B
runner.rad
11.6 KiB
vim/
.gitignore
333 B
.gitsigners
224 B
AGENTS.md
8.3 KiB
CONTRIBUTING
2.1 KiB
LICENSE
1.1 KiB
Makefile
2.5 KiB
README
3.4 KiB
STYLE
3.2 KiB
std.lib
2.1 KiB
std.lib.test
1.1 KiB
test/loader/hostile.rad
raw
| 1 | //! Generate binary RIL packages that the kernel loader must reject. |
| 2 | //! |
| 3 | //! The language cannot express these programs, so each one is built as typed IL and |
| 4 | //! encoded. Every package is small and well formed for the decoder, and breaks exactly one |
| 5 | //! rule of the verifier. The tool writes `<name>.ril.bin` for each package into the directory |
| 6 | //! given as the first argument. |
| 7 | |
| 8 | use std::fs; |
| 9 | use std::lang::il; |
| 10 | use std::lang::il::binary; |
| 11 | use std::lang::il::binary::linkage; |
| 12 | use std::lang::il::binary::program; |
| 13 | use std::mem::heap; |
| 14 | use std::sys; |
| 15 | |
| 16 | /// Layout of eight plain bytes. |
| 17 | constant PLAIN: u32 = 0; |
| 18 | /// Layout of a nullable owned pointer slot. |
| 19 | constant CELL: u32 = 1; |
| 20 | /// Layout of an owned writable slice slot of plain words. |
| 21 | constant SLICE: u32 = 2; |
| 22 | |
| 23 | /// Layout of an owned writable slice slot of pointer cells. |
| 24 | constant SLICECELL: u32 = 3; |
| 25 | |
| 26 | /// Layout of a typed heap handle slot. |
| 27 | constant HEAPCELL: u32 = 4; |
| 28 | |
| 29 | /// Layout of a union of sixteen bytes that mixes a pointer cell and plain words. |
| 30 | constant MIXED: u32 = 5; |
| 31 | |
| 32 | /// Storage for the IL of one package. |
| 33 | static BUILD: [u8; 65536] = [0; 65536]; |
| 34 | /// Storage for the encoding of one package. |
| 35 | static OUT: [u8; 65536] = [0; 65536]; |
| 36 | |
| 37 | /// Name of the entry function of every package. |
| 38 | constant ENTRY: *[u8] = "hostile::main"; |
| 39 | |
| 40 | /// Plain data of eight bytes. |
| 41 | fn word() -> il::ValType { |
| 42 | return il::ValType::Data(il::Type::W64); |
| 43 | } |
| 44 | |
| 45 | /// Register `n` as a value. |
| 46 | fn reg(n: u32) -> il::Val { |
| 47 | return il::Val::Reg(il::Reg { n }); |
| 48 | } |
| 49 | |
| 50 | /// A slot list of one slot. |
| 51 | fn only(arena: &heap::Heap, slot: il::Slot) -> *[il::Slot] { |
| 52 | let mut list: *mut [il::Slot] = &mut []; |
| 53 | try! list.append(slot, arena); |
| 54 | return &list[..]; |
| 55 | } |
| 56 | |
| 57 | /// Layout table shared by the packages. |
| 58 | fn layouts(arena: &heap::Heap) -> *mut [il::Layout] { |
| 59 | let cell = il::PtrType { layout: PLAIN, own: true, mutable: true, nullable: true }; |
| 60 | let mut table: *mut [il::Layout] = &mut []; |
| 61 | try! table.append(il::Layout { size: 8, alignment: 8, slots: &[], regions: 0 }, arena); |
| 62 | let pointer = only(arena, il::Slot { offset: 0, kind: il::SlotKind::Ptr(cell), regions: &[] }); |
| 63 | try! table.append(il::Layout { size: 8, alignment: 8, slots: pointer, regions: 0 }, arena); |
| 64 | let words = il::PtrType { layout: PLAIN, own: true, mutable: true, nullable: true }; |
| 65 | let slice = only(arena, il::Slot { offset: 0, kind: il::SlotKind::Slice(words), regions: &[] }); |
| 66 | try! table.append(il::Layout { size: 16, alignment: 8, slots: slice, regions: 0 }, arena); |
| 67 | let cells = il::PtrType { layout: CELL, own: true, mutable: true, nullable: true }; |
| 68 | let sliceCells = only(arena, il::Slot { |
| 69 | offset: 0, kind: il::SlotKind::Slice(cells), regions: &[] |
| 70 | }); |
| 71 | try! table.append(il::Layout { size: 16, alignment: 8, slots: sliceCells, regions: 0 }, arena); |
| 72 | let handle = only(arena, il::Slot { |
| 73 | offset: 0, kind: il::SlotKind::Heap(il::HeapType::Typed), regions: &[] |
| 74 | }); |
| 75 | try! table.append(il::Layout { size: 8, alignment: 8, slots: handle, regions: 0 }, arena); |
| 76 | let mut variants: *mut [il::UnionVariant] = &mut []; |
| 77 | try! variants.append(il::UnionVariant { tag: 0, layout: CELL, regions: &[] }, arena); |
| 78 | try! variants.append(il::UnionVariant { tag: 1, layout: PLAIN, regions: &[] }, arena); |
| 79 | let mixed = only(arena, il::Slot { |
| 80 | offset: 0, |
| 81 | kind: il::SlotKind::Union { offset: 8, size: 16, variants: &variants[..] }, |
| 82 | regions: &[], |
| 83 | }); |
| 84 | try! table.append(il::Layout { size: 16, alignment: 8, slots: mixed, regions: 0 }, arena); |
| 85 | return table; |
| 86 | } |
| 87 | |
| 88 | /// A function of one block. |
| 89 | fn function(arena: &heap::Heap, name: *[u8], instrs: &[il::Instr], isExtern: bool) -> il::Fn { |
| 90 | let mut body: *mut [il::Instr] = &mut []; |
| 91 | try! body.extend(instrs, arena); |
| 92 | let mut blocks: *mut [il::Block] = &mut []; |
| 93 | if not isExtern { |
| 94 | try! blocks.append( |
| 95 | il::Block { |
| 96 | label: il::BlockLabel { base: "b", suffix: nil }, |
| 97 | params: &[], |
| 98 | instrs: &body[..], |
| 99 | locs: &[], |
| 100 | preds: &[], |
| 101 | loopDepth: 0, |
| 102 | }, |
| 103 | arena |
| 104 | ); |
| 105 | } |
| 106 | return il::Fn { |
| 107 | name, |
| 108 | params: &[], |
| 109 | returnType: word(), |
| 110 | isExtern, |
| 111 | isLeaf: false, |
| 112 | blocks: &blocks[..], |
| 113 | vals: &[], |
| 114 | cases: &[], |
| 115 | borrows: &[], |
| 116 | }; |
| 117 | } |
| 118 | |
| 119 | /// Instruction `ret 0`. |
| 120 | fn done() -> il::Instr { |
| 121 | return il::Instr::Ret { val: il::Val::Imm(0) }; |
| 122 | } |
| 123 | |
| 124 | /// Instruction that reserves a frame object of `layout` in register `dst`. |
| 125 | fn reserve(dst: u32, layout: u32) -> il::Instr { |
| 126 | return il::Instr::Reserve { dst: il::Reg { n: dst }, layout }; |
| 127 | } |
| 128 | |
| 129 | /// Instruction that stores the 64-bit `src` at `offset` of the object in register `dst`. |
| 130 | fn store64(src: il::Val, dst: u32, offset: i32) -> il::Instr { |
| 131 | return il::Instr::Store { typ: il::Type::W64, src, dst: il::Reg { n: dst }, offset }; |
| 132 | } |
| 133 | |
| 134 | /// Instruction that loads 64 bits at `offset` of the object in register `src`. |
| 135 | fn load64(dst: u32, src: u32, offset: i32) -> il::Instr { |
| 136 | return il::Instr::Load { |
| 137 | typ: il::Type::W64, dst: il::Reg { n: dst }, src: il::Reg { n: src }, offset |
| 138 | }; |
| 139 | } |
| 140 | |
| 141 | /// Write one package to `<directory>/<name>.ril.bin`. |
| 142 | unsafe fn emit( |
| 143 | directory: *[u8], |
| 144 | name: *[u8], |
| 145 | dependencies: *[*[u8]], |
| 146 | data: &[il::Data], |
| 147 | instrs: &[il::Instr], |
| 148 | externs: bool, |
| 149 | symbols: *[*[u8]] |
| 150 | ) { |
| 151 | let arena = heap::over(&mut BUILD[..]); |
| 152 | let mut list: *mut [*il::Fn] = &mut []; |
| 153 | let mut main: *mut [il::Fn] = &mut []; |
| 154 | try! main.append(function(arena, ENTRY, instrs, false), arena); |
| 155 | try! list.append(&main[0], arena); |
| 156 | if externs { |
| 157 | let mut escape: *mut [il::Fn] = &mut []; |
| 158 | try! escape.append(function(arena, "hostile::escape", &[], true), arena); |
| 159 | try! list.append(&escape[0], arena); |
| 160 | } |
| 161 | let mut tables: *mut [il::Data] = &mut []; |
| 162 | try! tables.extend(data, arena); |
| 163 | let table = layouts(arena); |
| 164 | let mut pins: *mut [binary::Id] = &mut []; |
| 165 | for _ in dependencies { |
| 166 | try! pins.append(binary::Id { bytes: [0; linkage::ID_BYTES] }, arena); |
| 167 | } |
| 168 | let mut package = binary::Package { |
| 169 | version: nil, |
| 170 | symbols, |
| 171 | name: "hostile", |
| 172 | dependencies, |
| 173 | exports: &[ |
| 174 | binary::Export { |
| 175 | name: ENTRY, |
| 176 | kind: binary::ExportKind::Function(0), |
| 177 | startable: false, |
| 178 | }, |
| 179 | ], |
| 180 | entry: ENTRY, |
| 181 | program: il::Program { |
| 182 | data: &tables[..], fns: &list[..], layouts: &table[..], signatures: &[], traits: &[] |
| 183 | }, |
| 184 | links: binary::EMPTY_LINKS, |
| 185 | }; |
| 186 | set package.links.pins = &pins[..]; |
| 187 | let mut output: *mut [u8] = &mut OUT[..]; |
| 188 | let length = try! program::encode(&mut output, &package); |
| 189 | let mut path: *mut [u8] = &mut []; |
| 190 | try! path.extend(directory, arena); |
| 191 | try! path.extend("/", arena); |
| 192 | try! path.extend(name, arena); |
| 193 | try! path.extend(".ril.bin", arena); |
| 194 | try! fs::writeFile(&path[..], &OUT[.. length]); |
| 195 | } |
| 196 | |
| 197 | /// Generate every hostile package into the directory in the first argument. |
| 198 | @default unsafe fn main(env: *sys::Env) -> i32 { |
| 199 | assert env.args.len == 1; |
| 200 | let directory = env.args[0]; |
| 201 | let plain: *[*[u8]] = &["hostile", ENTRY]; |
| 202 | let none: *[*[u8]] = &[]; |
| 203 | // A word of data is dereferenced as if it were a pointer. |
| 204 | emit( |
| 205 | directory, |
| 206 | "forged", |
| 207 | none, |
| 208 | &[], |
| 209 | &[ |
| 210 | il::Instr::Copy { dst: il::Reg { n: 0 }, val: il::Val::Imm(0x80000000) }, |
| 211 | load64(1, 0, 0), done(), |
| 212 | ], |
| 213 | false, |
| 214 | plain |
| 215 | ); |
| 216 | // An integer is stored over a pointer slot of static data. |
| 217 | let window = il::Data { |
| 218 | name: "hostile::window", |
| 219 | size: 8, |
| 220 | alignment: 8, |
| 221 | layout: CELL, |
| 222 | readOnly: false, |
| 223 | heap: nil, |
| 224 | isZeroInit: true, |
| 225 | values: &[], |
| 226 | }; |
| 227 | emit( |
| 228 | directory, |
| 229 | "overwrite", |
| 230 | none, |
| 231 | &[window], |
| 232 | &[ |
| 233 | il::Instr::Copy { dst: il::Reg { n: 0 }, val: il::Val::DataSym("hostile::window") }, |
| 234 | store64(il::Val::Imm(0x80000000), 0, 0), done(), |
| 235 | ], |
| 236 | false, |
| 237 | &["hostile", ENTRY, "hostile::window"] |
| 238 | ); |
| 239 | // The address of a frame object is stored into the memory behind a Page. |
| 240 | emit( |
| 241 | directory, |
| 242 | "leak", |
| 243 | none, |
| 244 | &[], |
| 245 | &[ |
| 246 | reserve(0, PLAIN), reserve(1, SLICE), |
| 247 | il::Instr::PageSlice { |
| 248 | err: il::Reg { n: 2 }, |
| 249 | slot: il::Reg { n: 1 }, |
| 250 | handle: il::Val::Imm(1), |
| 251 | write: true, |
| 252 | layout: PLAIN, |
| 253 | }, |
| 254 | il::Instr::SliceElem { |
| 255 | dst: il::Reg { n: 3 }, |
| 256 | slice: il::Reg { n: 1 }, |
| 257 | index: il::Val::Imm(0), |
| 258 | layout: PLAIN, |
| 259 | }, |
| 260 | il::Instr::StorePtr { src: reg(0), dst: il::Reg { n: 3 }, offset: 0 }, done(), |
| 261 | ], |
| 262 | false, |
| 263 | plain |
| 264 | ); |
| 265 | // The package refers to code that it does not define. |
| 266 | emit(directory, "extern", none, &[], &[done()], true, &["hostile", ENTRY, "hostile::escape"]); |
| 267 | // The package needs another package. |
| 268 | emit( |
| 269 | directory, |
| 270 | "dependent", |
| 271 | &["support"], |
| 272 | &[], |
| 273 | &[done()], |
| 274 | false, |
| 275 | &[ |
| 276 | "hostile", ENTRY, |
| 277 | "support", |
| 278 | ] |
| 279 | ); |
| 280 | // The kernel would write a call result over a pointer slot. |
| 281 | emit( |
| 282 | directory, |
| 283 | "buffer", |
| 284 | none, |
| 285 | &[], |
| 286 | &[ |
| 287 | reserve(0, CELL), |
| 288 | il::Instr::Ecall { |
| 289 | dst: il::Reg { n: 1 }, |
| 290 | num: il::Val::Imm(44), |
| 291 | a0: il::Val::Imm(1), |
| 292 | a1: il::Val::Imm(0), |
| 293 | a2: il::Val::Imm(0), |
| 294 | a3: il::Val::Imm(0), |
| 295 | buf: il::EcallBuf { |
| 296 | ptr: il::Reg { n: 0 }, size: 8 |
| 297 | }, |
| 298 | }, |
| 299 | done(), |
| 300 | ], |
| 301 | false, |
| 302 | plain |
| 303 | ); |
| 304 | // The region of a heap, which no instruction can read, write or take apart. |
| 305 | let typed = il::Data { |
| 306 | name: "hostile::heap", |
| 307 | size: 8, |
| 308 | alignment: 8, |
| 309 | layout: PLAIN, |
| 310 | readOnly: false, |
| 311 | heap: il::HeapType::Typed, |
| 312 | isZeroInit: true, |
| 313 | values: &[], |
| 314 | }; |
| 315 | let plainHeap = il::Data { |
| 316 | name: "hostile::plain", |
| 317 | size: 8, |
| 318 | alignment: 8, |
| 319 | layout: PLAIN, |
| 320 | readOnly: false, |
| 321 | heap: il::HeapType::Plain, |
| 322 | isZeroInit: true, |
| 323 | values: &[], |
| 324 | }; |
| 325 | // A nonempty slice has an explicit or omitted zero capacity. |
| 326 | let backing = il::Data { |
| 327 | name: "hostile::backing", |
| 328 | size: 8, |
| 329 | alignment: 8, |
| 330 | layout: PLAIN, |
| 331 | readOnly: false, |
| 332 | heap: nil, |
| 333 | isZeroInit: true, |
| 334 | values: &[], |
| 335 | }; |
| 336 | let initializer: *[il::DataValue] = &[ |
| 337 | il::DataValue { item: il::DataItem::Sym("hostile::backing"), count: 1 }, |
| 338 | il::DataValue { item: il::DataItem::Val { typ: il::Type::W32, val: 1 }, count: 1 }, |
| 339 | il::DataValue { item: il::DataItem::Val { typ: il::Type::W32, val: 0 }, count: 1 }, |
| 340 | ]; |
| 341 | for count in &[2, 3] { |
| 342 | let view = il::Data { |
| 343 | name: "hostile::view", |
| 344 | size: 16, |
| 345 | alignment: 8, |
| 346 | layout: SLICE, |
| 347 | readOnly: true, |
| 348 | heap: nil, |
| 349 | isZeroInit: false, |
| 350 | values: &initializer[.. count], |
| 351 | }; |
| 352 | let name = "zerocap" if count == 3 else "omitcap"; |
| 353 | emit(directory, name, none, &[backing, view], &[done()], false, &[ |
| 354 | "hostile", ENTRY, "hostile::backing", "hostile::view" |
| 355 | ]); |
| 356 | } |
| 357 | // A writable slice names the protected header of a typed heap. |
| 358 | let heapView = il::Data { |
| 359 | name: "hostile::view", |
| 360 | size: 16, |
| 361 | alignment: 8, |
| 362 | layout: SLICE, |
| 363 | readOnly: true, |
| 364 | heap: nil, |
| 365 | isZeroInit: false, |
| 366 | values: &[ |
| 367 | il::DataValue { item: il::DataItem::Sym("hostile::heap"), count: 1 }, |
| 368 | il::DataValue { item: il::DataItem::Val { typ: il::Type::W32, val: 1 }, count: 1 }, |
| 369 | il::DataValue { item: il::DataItem::Val { typ: il::Type::W32, val: 1 }, count: 1 }, |
| 370 | ], |
| 371 | }; |
| 372 | emit( |
| 373 | directory, |
| 374 | "heapslice", |
| 375 | none, |
| 376 | &[typed, heapView], |
| 377 | &[ |
| 378 | il::Instr::Copy { dst: il::Reg { n: 0 }, val: il::Val::DataSym("hostile::view") }, |
| 379 | il::Instr::SliceElem { |
| 380 | dst: il::Reg { n: 1 }, |
| 381 | slice: il::Reg { n: 0 }, |
| 382 | index: il::Val::Imm(0), |
| 383 | layout: PLAIN, |
| 384 | }, |
| 385 | store64(il::Val::Imm(0), 1, 0), done(), |
| 386 | ], |
| 387 | false, |
| 388 | &["hostile", ENTRY, "hostile::heap", "hostile::view"] |
| 389 | ); |
| 390 | // A heap handle is dereferenced as if it were a pointer. |
| 391 | emit( |
| 392 | directory, |
| 393 | "heapread", |
| 394 | none, |
| 395 | &[typed], |
| 396 | &[ |
| 397 | il::Instr::Copy { dst: il::Reg { n: 0 }, val: il::Val::DataSym("hostile::heap") }, |
| 398 | load64(1, 0, 0), done(), |
| 399 | ], |
| 400 | false, |
| 401 | &["hostile", ENTRY, "hostile::heap"] |
| 402 | ); |
| 403 | // An integer is stored over a heap handle slot of static data, which forges a handle. |
| 404 | let handleWindow = il::Data { |
| 405 | name: "hostile::handle", |
| 406 | size: 8, |
| 407 | alignment: 8, |
| 408 | layout: HEAPCELL, |
| 409 | readOnly: false, |
| 410 | heap: nil, |
| 411 | isZeroInit: true, |
| 412 | values: &[], |
| 413 | }; |
| 414 | emit( |
| 415 | directory, |
| 416 | "heapforge", |
| 417 | none, |
| 418 | &[handleWindow], |
| 419 | &[ |
| 420 | il::Instr::Copy { dst: il::Reg { n: 0 }, val: il::Val::DataSym("hostile::handle") }, |
| 421 | store64(il::Val::Imm(0x80000000), 0, 0), done(), |
| 422 | ], |
| 423 | false, |
| 424 | &["hostile", ENTRY, "hostile::handle"] |
| 425 | ); |
| 426 | // A heap that provides plain elements only grows a slice of pointers. |
| 427 | emit( |
| 428 | directory, |
| 429 | "heaptyped", |
| 430 | none, |
| 431 | &[plainHeap], |
| 432 | &[ |
| 433 | reserve(0, SLICECELL), |
| 434 | il::Instr::Copy { dst: il::Reg { n: 1 }, val: il::Val::DataSym("hostile::plain") }, |
| 435 | il::Instr::SliceGrow { |
| 436 | ok: il::Reg { n: 2 }, |
| 437 | slice: il::Reg { n: 0 }, |
| 438 | heap: reg(1), |
| 439 | layout: CELL, |
| 440 | capacity: il::Val::Imm(1), |
| 441 | }, |
| 442 | done(), |
| 443 | ], |
| 444 | false, |
| 445 | &["hostile", ENTRY, "hostile::plain"] |
| 446 | ); |
| 447 | // A typed heap is reset, so that its reused memory could turn a stale pointer into a |
| 448 | // pointer of another type. |
| 449 | emit( |
| 450 | directory, |
| 451 | "heapreset", |
| 452 | none, |
| 453 | &[typed], |
| 454 | &[ |
| 455 | il::Instr::Copy { dst: il::Reg { n: 0 }, val: il::Val::DataSym("hostile::heap") }, |
| 456 | il::Instr::HeapReset { heap: reg(0) }, |
| 457 | done(), |
| 458 | ], |
| 459 | false, |
| 460 | &["hostile", ENTRY, "hostile::heap"] |
| 461 | ); |
| 462 | // An integer is stored over the pointer payload of a union that mixes pointers and data. |
| 463 | emit( |
| 464 | directory, |
| 465 | "mixed", |
| 466 | none, |
| 467 | &[], |
| 468 | &[reserve(0, MIXED), store64(il::Val::Imm(0x80000000), 0, 8), done()], |
| 469 | false, |
| 470 | plain |
| 471 | ); |
| 472 | return 0; |
| 473 | } |