Phase 6A: .aji layer 6 (intrinsics, resources, narrowing, unsafe operations)
- intrinsics from the registry as plain calls: fallible ones are outcome matches or `or trap` (E7006 otherwise), infallible ones plain calls - resource operands by name, borrowed mut for the one operation and ended on every edge; a mut binding/parameter passes as is; `mut f` is E7006 - closes consume with `move` (E7003); no inserted closes (E3002/E3001) - data operands explicit (ref/mut/byte string, E7005 otherwise) - file<…>, stream, listener types; cap.narrow.prefix with cap_error edges - ref/mut unchecked s[i] (also as a binding) and assume(c) under effects [unsafe]; boundary left to the verifier (E6001) - trusted is reserved and rejected (E7006) - parameter names preserved; generated names avoid them (normative) - parser: >> closes two type arguments - conformance: `equivalent:` compares the run record with a reference AIR program (hello, file_copy, tcp_echo, narrow ok/not_subset/invalid, random) - spec 15, grammar, quick reference (io_error match fixed), plan, examples
This commit is contained in:
1 parent
bdcf871e5d
commit
c18fb7acc1
39 files changed
+1309
-69
No files matched your search
@@ -0,0 +1,18 @@
|
||||
// stdin is read into a buffer and the part read is echoed to stdout.
|
||||
// expect: run
|
||||
// grant: input=io.read
|
||||
// grant: out=io.write
|
||||
// stdin: echo me
|
||||
// result: null
|
||||
// stdout: echo me
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md
|
||||
module demo.echostdin
|
||||
|
||||
fn main(input: cap<io.read>, out: cap<io.write>) -> void
|
||||
effects [io.read, io.write]
|
||||
{
|
||||
var buf: [u8; 16] = zero;
|
||||
let n = io.stdin_read(input, mut buf) or trap;
|
||||
io.stdout_write(out, ref buf[0..n]) or trap;
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
// Copy /in/data.txt to /out/copy.txt through scoped capabilities; the files are closed explicitly.
|
||||
// expect: run
|
||||
// grant: src=fs.read:/in
|
||||
// grant: dst=fs.write:/out
|
||||
// vfs: /in/data.txt=Aji copies files.\n
|
||||
// result: null
|
||||
// file: /out/copy.txt=Aji copies files.\n
|
||||
// equivalent: phase2/run/file_copy.air
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md
|
||||
module demo.filecopy
|
||||
|
||||
fn main(src: cap<fs.read>, dst: cap<fs.write>) -> void
|
||||
effects [fs.read, fs.write]
|
||||
{
|
||||
let input = fs.open_read(src, b"/in/data.txt") or trap;
|
||||
let output = fs.create(dst, b"/out/copy.txt") or trap;
|
||||
var buf: [u8; 4] = zero;
|
||||
loop {
|
||||
let n = fs.read(input, mut buf) or trap;
|
||||
if n == 0 {
|
||||
break;
|
||||
}
|
||||
fs.write_all(output, ref buf[0..n]) or trap;
|
||||
}
|
||||
fs.close(move input) or trap;
|
||||
fs.close(move output) or trap;
|
||||
}
|
||||
@@ -0,0 +1,215 @@
|
||||
module demo.filecopy
|
||||
air 0
|
||||
lang 0.1
|
||||
features [arrays, capabilities, effects, ownership, resources]
|
||||
|
||||
fn @main(%src: cap<fs.read>, %dst: cap<fs.write>) -> void
|
||||
effects [fs.read, fs.write]
|
||||
entry:
|
||||
%bytes_1 = local.alloc array<u8, 12>
|
||||
%bytes_45 = local.alloc array<u8, 13>
|
||||
%buf_92 = local.alloc array<u8, 4>
|
||||
local.zero %bytes_1
|
||||
%w_2 = borrow.mut %bytes_1
|
||||
%i_3 = const.usize 0
|
||||
%c_4 = const.u8 47
|
||||
%e_5 = borrow.index %w_2, %i_3
|
||||
store %e_5, %c_4
|
||||
borrow.end %e_5
|
||||
%i_6 = const.usize 1
|
||||
%c_7 = const.u8 105
|
||||
%e_8 = borrow.index %w_2, %i_6
|
||||
store %e_8, %c_7
|
||||
borrow.end %e_8
|
||||
%i_9 = const.usize 2
|
||||
%c_10 = const.u8 110
|
||||
%e_11 = borrow.index %w_2, %i_9
|
||||
store %e_11, %c_10
|
||||
borrow.end %e_11
|
||||
%i_12 = const.usize 3
|
||||
%c_13 = const.u8 47
|
||||
%e_14 = borrow.index %w_2, %i_12
|
||||
store %e_14, %c_13
|
||||
borrow.end %e_14
|
||||
%i_15 = const.usize 4
|
||||
%c_16 = const.u8 100
|
||||
%e_17 = borrow.index %w_2, %i_15
|
||||
store %e_17, %c_16
|
||||
borrow.end %e_17
|
||||
%i_18 = const.usize 5
|
||||
%c_19 = const.u8 97
|
||||
%e_20 = borrow.index %w_2, %i_18
|
||||
store %e_20, %c_19
|
||||
borrow.end %e_20
|
||||
%i_21 = const.usize 6
|
||||
%c_22 = const.u8 116
|
||||
%e_23 = borrow.index %w_2, %i_21
|
||||
store %e_23, %c_22
|
||||
borrow.end %e_23
|
||||
%i_24 = const.usize 7
|
||||
%c_25 = const.u8 97
|
||||
%e_26 = borrow.index %w_2, %i_24
|
||||
store %e_26, %c_25
|
||||
borrow.end %e_26
|
||||
%i_27 = const.usize 8
|
||||
%c_28 = const.u8 46
|
||||
%e_29 = borrow.index %w_2, %i_27
|
||||
store %e_29, %c_28
|
||||
borrow.end %e_29
|
||||
%i_30 = const.usize 9
|
||||
%c_31 = const.u8 116
|
||||
%e_32 = borrow.index %w_2, %i_30
|
||||
store %e_32, %c_31
|
||||
borrow.end %e_32
|
||||
%i_33 = const.usize 10
|
||||
%c_34 = const.u8 120
|
||||
%e_35 = borrow.index %w_2, %i_33
|
||||
store %e_35, %c_34
|
||||
borrow.end %e_35
|
||||
%i_36 = const.usize 11
|
||||
%c_37 = const.u8 116
|
||||
%e_38 = borrow.index %w_2, %i_36
|
||||
store %e_38, %c_37
|
||||
borrow.end %e_38
|
||||
borrow.end %w_2
|
||||
%b_39 = borrow.ref %bytes_1
|
||||
%s_40 = slice.make %b_39
|
||||
intrinsic fs.open_read(%src, %s_40), ok -> @ok_41, err -> @err_43
|
||||
ok_41(%ok_42: file<read>):
|
||||
borrow.end %s_40
|
||||
borrow.end %b_39
|
||||
local.zero %bytes_45
|
||||
%w_46 = borrow.mut %bytes_45
|
||||
%i_47 = const.usize 0
|
||||
%c_48 = const.u8 47
|
||||
%e_49 = borrow.index %w_46, %i_47
|
||||
store %e_49, %c_48
|
||||
borrow.end %e_49
|
||||
%i_50 = const.usize 1
|
||||
%c_51 = const.u8 111
|
||||
%e_52 = borrow.index %w_46, %i_50
|
||||
store %e_52, %c_51
|
||||
borrow.end %e_52
|
||||
%i_53 = const.usize 2
|
||||
%c_54 = const.u8 117
|
||||
%e_55 = borrow.index %w_46, %i_53
|
||||
store %e_55, %c_54
|
||||
borrow.end %e_55
|
||||
%i_56 = const.usize 3
|
||||
%c_57 = const.u8 116
|
||||
%e_58 = borrow.index %w_46, %i_56
|
||||
store %e_58, %c_57
|
||||
borrow.end %e_58
|
||||
%i_59 = const.usize 4
|
||||
%c_60 = const.u8 47
|
||||
%e_61 = borrow.index %w_46, %i_59
|
||||
store %e_61, %c_60
|
||||
borrow.end %e_61
|
||||
%i_62 = const.usize 5
|
||||
%c_63 = const.u8 99
|
||||
%e_64 = borrow.index %w_46, %i_62
|
||||
store %e_64, %c_63
|
||||
borrow.end %e_64
|
||||
%i_65 = const.usize 6
|
||||
%c_66 = const.u8 111
|
||||
%e_67 = borrow.index %w_46, %i_65
|
||||
store %e_67, %c_66
|
||||
borrow.end %e_67
|
||||
%i_68 = const.usize 7
|
||||
%c_69 = const.u8 112
|
||||
%e_70 = borrow.index %w_46, %i_68
|
||||
store %e_70, %c_69
|
||||
borrow.end %e_70
|
||||
%i_71 = const.usize 8
|
||||
%c_72 = const.u8 121
|
||||
%e_73 = borrow.index %w_46, %i_71
|
||||
store %e_73, %c_72
|
||||
borrow.end %e_73
|
||||
%i_74 = const.usize 9
|
||||
%c_75 = const.u8 46
|
||||
%e_76 = borrow.index %w_46, %i_74
|
||||
store %e_76, %c_75
|
||||
borrow.end %e_76
|
||||
%i_77 = const.usize 10
|
||||
%c_78 = const.u8 116
|
||||
%e_79 = borrow.index %w_46, %i_77
|
||||
store %e_79, %c_78
|
||||
borrow.end %e_79
|
||||
%i_80 = const.usize 11
|
||||
%c_81 = const.u8 120
|
||||
%e_82 = borrow.index %w_46, %i_80
|
||||
store %e_82, %c_81
|
||||
borrow.end %e_82
|
||||
%i_83 = const.usize 12
|
||||
%c_84 = const.u8 116
|
||||
%e_85 = borrow.index %w_46, %i_83
|
||||
store %e_85, %c_84
|
||||
borrow.end %e_85
|
||||
borrow.end %w_46
|
||||
%b_86 = borrow.ref %bytes_45
|
||||
%s_87 = slice.make %b_86
|
||||
intrinsic fs.create(%dst, %s_87), ok -> @ok_88, err -> @err_90
|
||||
err_43(%err_44: io_error):
|
||||
borrow.end %s_40
|
||||
borrow.end %b_39
|
||||
trap
|
||||
ok_88(%ok_89: file<write>):
|
||||
borrow.end %s_87
|
||||
borrow.end %b_86
|
||||
local.zero %buf_92
|
||||
br @loop_93()
|
||||
err_90(%err_91: io_error):
|
||||
borrow.end %s_87
|
||||
borrow.end %b_86
|
||||
trap
|
||||
loop_93:
|
||||
%input_95 = borrow.mut %ok_42
|
||||
%buf_96 = borrow.mut %buf_92
|
||||
%s_97 = slice.make %buf_96
|
||||
intrinsic fs.read(%input_95, %s_97), ok -> @ok_98, err -> @err_100
|
||||
after_94:
|
||||
intrinsic fs.close(%ok_42), ok -> @ok_116, err -> @err_117
|
||||
ok_98(%ok_99: usize):
|
||||
borrow.end %s_97
|
||||
borrow.end %buf_96
|
||||
borrow.end %input_95
|
||||
%c_102 = const.usize 0
|
||||
%cmp_103 = eq.usize %ok_99, %c_102
|
||||
cbr %cmp_103, @then_104(), @else_105()
|
||||
err_100(%err_101: io_error):
|
||||
borrow.end %s_97
|
||||
borrow.end %buf_96
|
||||
borrow.end %input_95
|
||||
trap
|
||||
then_104:
|
||||
br @after_94()
|
||||
else_105:
|
||||
br @endif_106()
|
||||
endif_106:
|
||||
%output_107 = borrow.mut %ok_89
|
||||
%buf_108 = borrow.ref %buf_92
|
||||
%s_109 = slice.make %buf_108
|
||||
%c_110 = const.usize 0
|
||||
%len_111 = sub.checked.usize %ok_99, %c_110
|
||||
%s_112 = slice.sub %s_109, %c_110, %len_111
|
||||
intrinsic fs.write_all(%output_107, %s_112), ok -> @ok_113, err -> @err_114
|
||||
ok_113:
|
||||
borrow.end %s_112
|
||||
borrow.end %s_109
|
||||
borrow.end %buf_108
|
||||
borrow.end %output_107
|
||||
br @loop_93()
|
||||
err_114(%err_115: io_error):
|
||||
borrow.end %s_112
|
||||
borrow.end %s_109
|
||||
borrow.end %buf_108
|
||||
borrow.end %output_107
|
||||
trap
|
||||
ok_116:
|
||||
intrinsic fs.close(%ok_89), ok -> @ok_119, err -> @err_120
|
||||
err_117(%err_118: io_error):
|
||||
trap
|
||||
ok_119:
|
||||
ret
|
||||
err_120(%err_121: io_error):
|
||||
trap
|
||||
@@ -0,0 +1,15 @@
|
||||
// Hello world in source; its run equals the reference AIR program's.
|
||||
// expect: run
|
||||
// grant: out=io.write
|
||||
// result: null
|
||||
// stdout: hello, aji\n
|
||||
// equivalent: phase2/run/hello.air
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md
|
||||
module demo.hello
|
||||
|
||||
fn main(out: cap<io.write>) -> void
|
||||
effects [io.write]
|
||||
{
|
||||
io.stdout_write(out, b"hello, aji\n") or trap;
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
module demo.hello
|
||||
air 0
|
||||
lang 0.1
|
||||
features [arrays, capabilities, effects, ownership, resources]
|
||||
|
||||
fn @main(%out: cap<io.write>) -> void
|
||||
effects [io.write]
|
||||
entry:
|
||||
%bytes_1 = local.alloc array<u8, 11>
|
||||
local.zero %bytes_1
|
||||
%w_2 = borrow.mut %bytes_1
|
||||
%i_3 = const.usize 0
|
||||
%c_4 = const.u8 104
|
||||
%e_5 = borrow.index %w_2, %i_3
|
||||
store %e_5, %c_4
|
||||
borrow.end %e_5
|
||||
%i_6 = const.usize 1
|
||||
%c_7 = const.u8 101
|
||||
%e_8 = borrow.index %w_2, %i_6
|
||||
store %e_8, %c_7
|
||||
borrow.end %e_8
|
||||
%i_9 = const.usize 2
|
||||
%c_10 = const.u8 108
|
||||
%e_11 = borrow.index %w_2, %i_9
|
||||
store %e_11, %c_10
|
||||
borrow.end %e_11
|
||||
%i_12 = const.usize 3
|
||||
%c_13 = const.u8 108
|
||||
%e_14 = borrow.index %w_2, %i_12
|
||||
store %e_14, %c_13
|
||||
borrow.end %e_14
|
||||
%i_15 = const.usize 4
|
||||
%c_16 = const.u8 111
|
||||
%e_17 = borrow.index %w_2, %i_15
|
||||
store %e_17, %c_16
|
||||
borrow.end %e_17
|
||||
%i_18 = const.usize 5
|
||||
%c_19 = const.u8 44
|
||||
%e_20 = borrow.index %w_2, %i_18
|
||||
store %e_20, %c_19
|
||||
borrow.end %e_20
|
||||
%i_21 = const.usize 6
|
||||
%c_22 = const.u8 32
|
||||
%e_23 = borrow.index %w_2, %i_21
|
||||
store %e_23, %c_22
|
||||
borrow.end %e_23
|
||||
%i_24 = const.usize 7
|
||||
%c_25 = const.u8 97
|
||||
%e_26 = borrow.index %w_2, %i_24
|
||||
store %e_26, %c_25
|
||||
borrow.end %e_26
|
||||
%i_27 = const.usize 8
|
||||
%c_28 = const.u8 106
|
||||
%e_29 = borrow.index %w_2, %i_27
|
||||
store %e_29, %c_28
|
||||
borrow.end %e_29
|
||||
%i_30 = const.usize 9
|
||||
%c_31 = const.u8 105
|
||||
%e_32 = borrow.index %w_2, %i_30
|
||||
store %e_32, %c_31
|
||||
borrow.end %e_32
|
||||
%i_33 = const.usize 10
|
||||
%c_34 = const.u8 10
|
||||
%e_35 = borrow.index %w_2, %i_33
|
||||
store %e_35, %c_34
|
||||
borrow.end %e_35
|
||||
borrow.end %w_2
|
||||
%b_36 = borrow.ref %bytes_1
|
||||
%s_37 = slice.make %b_36
|
||||
intrinsic io.stdout_write(%out, %s_37), ok -> @ok_38, err -> @err_39
|
||||
ok_38:
|
||||
borrow.end %s_37
|
||||
borrow.end %b_36
|
||||
ret
|
||||
err_39(%err_40: io_error):
|
||||
borrow.end %s_37
|
||||
borrow.end %b_36
|
||||
trap
|
||||
@@ -0,0 +1,32 @@
|
||||
// A missing file: err(e) binds the io_error, matched like any closed enum.
|
||||
// expect: run
|
||||
// grant: src=fs.read:/in
|
||||
// result: 1
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md
|
||||
module demo.ioerror
|
||||
|
||||
fn main(src: cap<fs.read>) -> u8
|
||||
effects [fs.read]
|
||||
{
|
||||
match fs.open_read(src, b"/in/missing.txt") {
|
||||
ok(f) => {
|
||||
fs.close(move f) or trap;
|
||||
0
|
||||
}
|
||||
err(e) => match e {
|
||||
io_error::not_found => 1,
|
||||
io_error::permission_denied => 2,
|
||||
io_error::already_exists => 3,
|
||||
io_error::invalid_input => 4,
|
||||
io_error::no_space => 5,
|
||||
io_error::broken_pipe => 6,
|
||||
io_error::connection_refused => 7,
|
||||
io_error::connection_reset => 8,
|
||||
io_error::address_in_use => 9,
|
||||
io_error::address_unavailable => 10,
|
||||
io_error::timed_out => 11,
|
||||
io_error::other => 12,
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
// Grants are not part of the source: a missing one is not_instantiated at run time, not a compile error.
|
||||
// expect: run
|
||||
// not_instantiated: missing_capability
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md
|
||||
module demo.missinggrant
|
||||
|
||||
fn main(out: cap<io.write>) -> void
|
||||
effects [io.write]
|
||||
{
|
||||
io.stdout_write(out, b"x") or trap;
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
// cap.narrow.prefix to /data/../etc under scope /data: the err edge carries the cap_error.
|
||||
// expect: run
|
||||
// grant: fs=fs.read:/data
|
||||
// result: 1
|
||||
// equivalent: phase2/run/narrow_invalid.air
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md
|
||||
module demo.narrowinvalid
|
||||
|
||||
fn classify(fs: cap<fs.read>, path: slice<u8>) -> u8
|
||||
effects []
|
||||
{
|
||||
match cap.narrow.prefix(fs, path) {
|
||||
ok(c) => 0,
|
||||
err(e) => match e {
|
||||
cap_error::invalid_restriction => 1,
|
||||
cap_error::not_subset => 2,
|
||||
cap_error::unsupported_restriction => 3,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn main(fs: cap<fs.read>) -> u8
|
||||
effects []
|
||||
{
|
||||
classify(fs, b"/data/../etc")
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
// cap.narrow.prefix to /etc under scope /data: the err edge carries the cap_error.
|
||||
// expect: run
|
||||
// grant: fs=fs.read:/data
|
||||
// result: 2
|
||||
// equivalent: phase2/run/narrow_not_subset.air
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md
|
||||
module demo.narrownot_subset
|
||||
|
||||
fn classify(fs: cap<fs.read>, path: slice<u8>) -> u8
|
||||
effects []
|
||||
{
|
||||
match cap.narrow.prefix(fs, path) {
|
||||
ok(c) => 0,
|
||||
err(e) => match e {
|
||||
cap_error::invalid_restriction => 1,
|
||||
cap_error::not_subset => 2,
|
||||
cap_error::unsupported_restriction => 3,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn main(fs: cap<fs.read>) -> u8
|
||||
effects []
|
||||
{
|
||||
classify(fs, b"/etc")
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
// cap.narrow.prefix: ok narrows, err binds a cap_error matched exhaustively.
|
||||
// expect: run
|
||||
// grant: fs=fs.read:/data
|
||||
// result: 0
|
||||
// equivalent: phase2/run/narrow_ok.air
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md
|
||||
module demo.narrowprefix
|
||||
|
||||
fn main(fs: cap<fs.read>) -> u8
|
||||
effects []
|
||||
{
|
||||
match cap.narrow.prefix(fs, b"/data/logs") {
|
||||
ok(logs) => 0,
|
||||
err(e) => match e {
|
||||
cap_error::invalid_restriction => 1,
|
||||
cap_error::not_subset => 2,
|
||||
cap_error::unsupported_restriction => 3,
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
// An infallible intrinsic is a plain call.
|
||||
// expect: run
|
||||
// grant: rng=random.read
|
||||
// seed: 7
|
||||
// result: [90,118,249,78]
|
||||
// equivalent: phase2/run/random_seeded.air
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md
|
||||
module demo.random
|
||||
|
||||
fn main(rng: cap<random.read>) -> [u8; 4]
|
||||
effects [random.read]
|
||||
{
|
||||
var b: [u8; 4] = zero;
|
||||
random.fill(rng, mut b);
|
||||
b
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
// Closing consumes the resource: a named one must be written `move f`.
|
||||
// expect: reject E7003
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md
|
||||
module demo.closenomove
|
||||
|
||||
fn main(src: cap<fs.read>) -> void
|
||||
effects [fs.read]
|
||||
{
|
||||
let f = fs.open_read(src, b"/in/a") or trap;
|
||||
fs.close(f) or trap;
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
// Data borrows stay explicit: a buffer is passed as `mut buf`.
|
||||
// expect: reject E7005
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md
|
||||
module demo.databorrow
|
||||
|
||||
fn main(input: cap<io.read>) -> usize
|
||||
effects [io.read]
|
||||
{
|
||||
var buf: [u8; 4] = zero;
|
||||
io.stdin_read(input, buf) or trap
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
// An infallible intrinsic has no outcomes to match.
|
||||
// expect: reject E7006
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md
|
||||
module demo.infalliblematch
|
||||
|
||||
fn main(clock: cap<clock.read>) -> u64
|
||||
effects [clock.read]
|
||||
{
|
||||
match clock.monotonic(clock) {
|
||||
ok(t) => t,
|
||||
err(e) => 0,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
// An intrinsic without its effect: the verifier's E3101 names it.
|
||||
// expect: reject E3101
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md
|
||||
module demo.ioeffect
|
||||
|
||||
fn main(out: cap<io.write>) -> void
|
||||
effects []
|
||||
{
|
||||
io.stdout_write(out, b"x") or trap;
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
// A resource operand is written by name; the operation borrows it (one canonical spelling).
|
||||
// expect: reject E7006
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md
|
||||
module demo.resourceborrow
|
||||
|
||||
fn main(src: cap<fs.read>) -> void
|
||||
effects [fs.read]
|
||||
{
|
||||
let f = fs.open_read(src, b"/in/a") or trap;
|
||||
var buf: [u8; 4] = zero;
|
||||
let n = fs.read(mut f, mut buf) or trap;
|
||||
fs.close(move f) or trap;
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
// The frontend never inserts a close: an unclosed file is the verifier's leak.
|
||||
// expect: reject E3002
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md
|
||||
module demo.resourceleak
|
||||
|
||||
fn main(src: cap<fs.read>) -> void
|
||||
effects [fs.read]
|
||||
{
|
||||
let f = fs.open_read(src, b"/in/a") or trap;
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
// `trusted` belongs to runtime, std and toolchain modules; a program cannot declare its own code trusted.
|
||||
// expect: reject E7006
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md
|
||||
module demo.trusted
|
||||
|
||||
fn get(s: slice<u64>, i: usize) -> u64
|
||||
effects []
|
||||
trusted "callers check the bound"
|
||||
{
|
||||
ref unchecked e = s[i];
|
||||
*e
|
||||
}
|
||||
|
||||
fn main() -> u64
|
||||
effects []
|
||||
{
|
||||
0
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
// `unchecked` applies to one element, not a range.
|
||||
// expect: reject E7006
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md
|
||||
module demo.uncheckedrange
|
||||
|
||||
fn f(s: slice<u64>) -> u64
|
||||
effects [unsafe]
|
||||
{
|
||||
ref unchecked r = s[0..2];
|
||||
0
|
||||
}
|
||||
|
||||
fn main() -> u64
|
||||
effects []
|
||||
{
|
||||
0
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
// A fallible intrinsic is handled with `match` or `or trap`; there is no implicit propagation.
|
||||
// expect: reject E7006
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md
|
||||
module demo.unhandledio
|
||||
|
||||
fn main(out: cap<io.write>) -> void
|
||||
effects [io.write]
|
||||
{
|
||||
io.stdout_write(out, b"x");
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
// An unsafe operation outside an `unsafe` function: the verifier decides (E6001).
|
||||
// expect: reject E6001
|
||||
// phase: 6
|
||||
// spec: docs/spec/13-contracts-and-unsafe.md
|
||||
module demo.unsafeboundary
|
||||
|
||||
fn get(s: slice<u64>, i: usize) -> u64
|
||||
effects []
|
||||
{
|
||||
ref unchecked e = s[i];
|
||||
*e
|
||||
}
|
||||
|
||||
fn main() -> u64
|
||||
effects []
|
||||
{
|
||||
0
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
// A file used after it was closed: the verifier reports it.
|
||||
// expect: reject E3001
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md
|
||||
module demo.useafterclose
|
||||
|
||||
fn main(src: cap<fs.read>) -> void
|
||||
effects [fs.read]
|
||||
{
|
||||
let f = fs.open_read(src, b"/in/a") or trap;
|
||||
fs.close(move f) or trap;
|
||||
var buf: [u8; 4] = zero;
|
||||
let n = fs.read(f, mut buf) or trap;
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
// A capability of the wrong kind: the verifier's E3201.
|
||||
// expect: reject E3201
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md
|
||||
module demo.wrongcap
|
||||
|
||||
fn main(out: cap<io.read>) -> void
|
||||
effects [io.write]
|
||||
{
|
||||
io.stdout_write(out, b"x") or trap;
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
// fs.write_all needs a writable file; a file<read> is rejected at the operand.
|
||||
// expect: reject E7005
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md
|
||||
module demo.wrongmode
|
||||
|
||||
fn main(src: cap<fs.read>) -> void
|
||||
effects [fs.read, fs.write]
|
||||
{
|
||||
let f = fs.open_read(src, b"/in/a") or trap;
|
||||
fs.write_all(f, b"x") or trap;
|
||||
fs.close(move f) or trap;
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
// A function borrows a resource through a mut<file<…>> parameter; the caller lends it with `mut f`.
|
||||
// expect: run
|
||||
// grant: dst=fs.write:/out
|
||||
// result: null
|
||||
// file: /out/a.txt=ab
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md
|
||||
module demo.resourceparam
|
||||
|
||||
fn put(f: mut<file<write>>, byte: u8) -> void
|
||||
effects [fs.write]
|
||||
{
|
||||
var one: [u8; 1] = [byte; 1];
|
||||
fs.write_all(f, ref one) or trap;
|
||||
}
|
||||
|
||||
fn main(dst: cap<fs.write>) -> void
|
||||
effects [fs.write]
|
||||
{
|
||||
let f = fs.create(dst, b"/out/a.txt") or trap;
|
||||
put(mut f, 97);
|
||||
put(mut f, 98);
|
||||
fs.close(move f) or trap;
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
// TCP echo over the deterministic loopback; streams and the listener are closed explicitly.
|
||||
// expect: run
|
||||
// grant: listen=net.listen
|
||||
// grant: connect=net.connect
|
||||
// result: [112,105,110,103]
|
||||
// equivalent: phase2/run/tcp_echo.air
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md
|
||||
module demo.tcpecho
|
||||
|
||||
fn main(listen: cap<net.listen>, connect: cap<net.connect>) -> [u8; 4]
|
||||
effects [net.listen, net.connect, net.read, net.write]
|
||||
{
|
||||
let l = net.listen(listen, b"127.0.0.1:7") or trap;
|
||||
let client = net.connect(connect, b"127.0.0.1:7") or trap;
|
||||
let server = net.accept(l) or trap;
|
||||
net.send_all(client, b"ping") or trap;
|
||||
var buf: [u8; 4] = zero;
|
||||
let n = net.recv(server, mut buf) or trap;
|
||||
net.send_all(server, ref buf[0..n]) or trap;
|
||||
var back: [u8; 4] = zero;
|
||||
let m = net.recv(client, mut back) or trap;
|
||||
net.close_stream(move server) or trap;
|
||||
net.close_stream(move client) or trap;
|
||||
net.close_listener(move l) or trap;
|
||||
back
|
||||
}
|
||||
@@ -0,0 +1,252 @@
|
||||
module demo.tcpecho
|
||||
air 0
|
||||
lang 0.1
|
||||
features [arrays, capabilities, effects, ownership, resources]
|
||||
|
||||
fn @main(%listen: cap<net.listen>, %connect: cap<net.connect>) -> array<u8, 4>
|
||||
effects [net.listen, net.connect, net.read, net.write]
|
||||
entry:
|
||||
%bytes_1 = local.alloc array<u8, 11>
|
||||
%bytes_42 = local.alloc array<u8, 11>
|
||||
%bytes_89 = local.alloc array<u8, 4>
|
||||
%buf_108 = local.alloc array<u8, 4>
|
||||
%back_125 = local.alloc array<u8, 4>
|
||||
local.zero %bytes_1
|
||||
%w_2 = borrow.mut %bytes_1
|
||||
%i_3 = const.usize 0
|
||||
%c_4 = const.u8 49
|
||||
%e_5 = borrow.index %w_2, %i_3
|
||||
store %e_5, %c_4
|
||||
borrow.end %e_5
|
||||
%i_6 = const.usize 1
|
||||
%c_7 = const.u8 50
|
||||
%e_8 = borrow.index %w_2, %i_6
|
||||
store %e_8, %c_7
|
||||
borrow.end %e_8
|
||||
%i_9 = const.usize 2
|
||||
%c_10 = const.u8 55
|
||||
%e_11 = borrow.index %w_2, %i_9
|
||||
store %e_11, %c_10
|
||||
borrow.end %e_11
|
||||
%i_12 = const.usize 3
|
||||
%c_13 = const.u8 46
|
||||
%e_14 = borrow.index %w_2, %i_12
|
||||
store %e_14, %c_13
|
||||
borrow.end %e_14
|
||||
%i_15 = const.usize 4
|
||||
%c_16 = const.u8 48
|
||||
%e_17 = borrow.index %w_2, %i_15
|
||||
store %e_17, %c_16
|
||||
borrow.end %e_17
|
||||
%i_18 = const.usize 5
|
||||
%c_19 = const.u8 46
|
||||
%e_20 = borrow.index %w_2, %i_18
|
||||
store %e_20, %c_19
|
||||
borrow.end %e_20
|
||||
%i_21 = const.usize 6
|
||||
%c_22 = const.u8 48
|
||||
%e_23 = borrow.index %w_2, %i_21
|
||||
store %e_23, %c_22
|
||||
borrow.end %e_23
|
||||
%i_24 = const.usize 7
|
||||
%c_25 = const.u8 46
|
||||
%e_26 = borrow.index %w_2, %i_24
|
||||
store %e_26, %c_25
|
||||
borrow.end %e_26
|
||||
%i_27 = const.usize 8
|
||||
%c_28 = const.u8 49
|
||||
%e_29 = borrow.index %w_2, %i_27
|
||||
store %e_29, %c_28
|
||||
borrow.end %e_29
|
||||
%i_30 = const.usize 9
|
||||
%c_31 = const.u8 58
|
||||
%e_32 = borrow.index %w_2, %i_30
|
||||
store %e_32, %c_31
|
||||
borrow.end %e_32
|
||||
%i_33 = const.usize 10
|
||||
%c_34 = const.u8 55
|
||||
%e_35 = borrow.index %w_2, %i_33
|
||||
store %e_35, %c_34
|
||||
borrow.end %e_35
|
||||
borrow.end %w_2
|
||||
%b_36 = borrow.ref %bytes_1
|
||||
%s_37 = slice.make %b_36
|
||||
intrinsic net.listen(%listen, %s_37), ok -> @ok_38, err -> @err_40
|
||||
ok_38(%ok_39: listener):
|
||||
borrow.end %s_37
|
||||
borrow.end %b_36
|
||||
local.zero %bytes_42
|
||||
%w_43 = borrow.mut %bytes_42
|
||||
%i_44 = const.usize 0
|
||||
%c_45 = const.u8 49
|
||||
%e_46 = borrow.index %w_43, %i_44
|
||||
store %e_46, %c_45
|
||||
borrow.end %e_46
|
||||
%i_47 = const.usize 1
|
||||
%c_48 = const.u8 50
|
||||
%e_49 = borrow.index %w_43, %i_47
|
||||
store %e_49, %c_48
|
||||
borrow.end %e_49
|
||||
%i_50 = const.usize 2
|
||||
%c_51 = const.u8 55
|
||||
%e_52 = borrow.index %w_43, %i_50
|
||||
store %e_52, %c_51
|
||||
borrow.end %e_52
|
||||
%i_53 = const.usize 3
|
||||
%c_54 = const.u8 46
|
||||
%e_55 = borrow.index %w_43, %i_53
|
||||
store %e_55, %c_54
|
||||
borrow.end %e_55
|
||||
%i_56 = const.usize 4
|
||||
%c_57 = const.u8 48
|
||||
%e_58 = borrow.index %w_43, %i_56
|
||||
store %e_58, %c_57
|
||||
borrow.end %e_58
|
||||
%i_59 = const.usize 5
|
||||
%c_60 = const.u8 46
|
||||
%e_61 = borrow.index %w_43, %i_59
|
||||
store %e_61, %c_60
|
||||
borrow.end %e_61
|
||||
%i_62 = const.usize 6
|
||||
%c_63 = const.u8 48
|
||||
%e_64 = borrow.index %w_43, %i_62
|
||||
store %e_64, %c_63
|
||||
borrow.end %e_64
|
||||
%i_65 = const.usize 7
|
||||
%c_66 = const.u8 46
|
||||
%e_67 = borrow.index %w_43, %i_65
|
||||
store %e_67, %c_66
|
||||
borrow.end %e_67
|
||||
%i_68 = const.usize 8
|
||||
%c_69 = const.u8 49
|
||||
%e_70 = borrow.index %w_43, %i_68
|
||||
store %e_70, %c_69
|
||||
borrow.end %e_70
|
||||
%i_71 = const.usize 9
|
||||
%c_72 = const.u8 58
|
||||
%e_73 = borrow.index %w_43, %i_71
|
||||
store %e_73, %c_72
|
||||
borrow.end %e_73
|
||||
%i_74 = const.usize 10
|
||||
%c_75 = const.u8 55
|
||||
%e_76 = borrow.index %w_43, %i_74
|
||||
store %e_76, %c_75
|
||||
borrow.end %e_76
|
||||
borrow.end %w_43
|
||||
%b_77 = borrow.ref %bytes_42
|
||||
%s_78 = slice.make %b_77
|
||||
intrinsic net.connect(%connect, %s_78), ok -> @ok_79, err -> @err_81
|
||||
err_40(%err_41: io_error):
|
||||
borrow.end %s_37
|
||||
borrow.end %b_36
|
||||
trap
|
||||
ok_79(%ok_80: stream):
|
||||
borrow.end %s_78
|
||||
borrow.end %b_77
|
||||
%l_83 = borrow.mut %ok_39
|
||||
intrinsic net.accept(%l_83), ok -> @ok_84, err -> @err_86
|
||||
err_81(%err_82: io_error):
|
||||
borrow.end %s_78
|
||||
borrow.end %b_77
|
||||
trap
|
||||
ok_84(%ok_85: stream):
|
||||
borrow.end %l_83
|
||||
%client_88 = borrow.mut %ok_80
|
||||
local.zero %bytes_89
|
||||
%w_90 = borrow.mut %bytes_89
|
||||
%i_91 = const.usize 0
|
||||
%c_92 = const.u8 112
|
||||
%e_93 = borrow.index %w_90, %i_91
|
||||
store %e_93, %c_92
|
||||
borrow.end %e_93
|
||||
%i_94 = const.usize 1
|
||||
%c_95 = const.u8 105
|
||||
%e_96 = borrow.index %w_90, %i_94
|
||||
store %e_96, %c_95
|
||||
borrow.end %e_96
|
||||
%i_97 = const.usize 2
|
||||
%c_98 = const.u8 110
|
||||
%e_99 = borrow.index %w_90, %i_97
|
||||
store %e_99, %c_98
|
||||
borrow.end %e_99
|
||||
%i_100 = const.usize 3
|
||||
%c_101 = const.u8 103
|
||||
%e_102 = borrow.index %w_90, %i_100
|
||||
store %e_102, %c_101
|
||||
borrow.end %e_102
|
||||
borrow.end %w_90
|
||||
%b_103 = borrow.ref %bytes_89
|
||||
%s_104 = slice.make %b_103
|
||||
intrinsic net.send_all(%client_88, %s_104), ok -> @ok_105, err -> @err_106
|
||||
err_86(%err_87: io_error):
|
||||
borrow.end %l_83
|
||||
trap
|
||||
ok_105:
|
||||
borrow.end %s_104
|
||||
borrow.end %b_103
|
||||
borrow.end %client_88
|
||||
local.zero %buf_108
|
||||
%server_109 = borrow.mut %ok_85
|
||||
%buf_110 = borrow.mut %buf_108
|
||||
%s_111 = slice.make %buf_110
|
||||
intrinsic net.recv(%server_109, %s_111), ok -> @ok_112, err -> @err_114
|
||||
err_106(%err_107: io_error):
|
||||
borrow.end %s_104
|
||||
borrow.end %b_103
|
||||
borrow.end %client_88
|
||||
trap
|
||||
ok_112(%ok_113: usize):
|
||||
borrow.end %s_111
|
||||
borrow.end %buf_110
|
||||
borrow.end %server_109
|
||||
%server_116 = borrow.mut %ok_85
|
||||
%buf_117 = borrow.ref %buf_108
|
||||
%s_118 = slice.make %buf_117
|
||||
%c_119 = const.usize 0
|
||||
%len_120 = sub.checked.usize %ok_113, %c_119
|
||||
%s_121 = slice.sub %s_118, %c_119, %len_120
|
||||
intrinsic net.send_all(%server_116, %s_121), ok -> @ok_122, err -> @err_123
|
||||
err_114(%err_115: io_error):
|
||||
borrow.end %s_111
|
||||
borrow.end %buf_110
|
||||
borrow.end %server_109
|
||||
trap
|
||||
ok_122:
|
||||
borrow.end %s_121
|
||||
borrow.end %s_118
|
||||
borrow.end %buf_117
|
||||
borrow.end %server_116
|
||||
local.zero %back_125
|
||||
%client_126 = borrow.mut %ok_80
|
||||
%back_127 = borrow.mut %back_125
|
||||
%s_128 = slice.make %back_127
|
||||
intrinsic net.recv(%client_126, %s_128), ok -> @ok_129, err -> @err_131
|
||||
err_123(%err_124: io_error):
|
||||
borrow.end %s_121
|
||||
borrow.end %s_118
|
||||
borrow.end %buf_117
|
||||
borrow.end %server_116
|
||||
trap
|
||||
ok_129(%ok_130: usize):
|
||||
borrow.end %s_128
|
||||
borrow.end %back_127
|
||||
borrow.end %client_126
|
||||
intrinsic net.close_stream(%ok_85), ok -> @ok_133, err -> @err_134
|
||||
err_131(%err_132: io_error):
|
||||
borrow.end %s_128
|
||||
borrow.end %back_127
|
||||
borrow.end %client_126
|
||||
trap
|
||||
ok_133:
|
||||
intrinsic net.close_stream(%ok_80), ok -> @ok_136, err -> @err_137
|
||||
err_134(%err_135: io_error):
|
||||
trap
|
||||
ok_136:
|
||||
intrinsic net.close_listener(%ok_39), ok -> @ok_139, err -> @err_140
|
||||
err_137(%err_138: io_error):
|
||||
trap
|
||||
ok_139:
|
||||
%back_142 = local.load %back_125
|
||||
ret %back_142
|
||||
err_140(%err_141: io_error):
|
||||
trap
|
||||
@@ -0,0 +1,22 @@
|
||||
// Unsafe operations inside a function that declares `unsafe`; its callers declare it too.
|
||||
// expect: run
|
||||
// result: 30
|
||||
// phase: 6
|
||||
// spec: docs/spec/13-contracts-and-unsafe.md
|
||||
module demo.unchecked
|
||||
|
||||
fn get(s: slice<u64>, i: usize) -> u64
|
||||
effects [unsafe]
|
||||
{
|
||||
assume(i < s.len);
|
||||
ref unchecked e = s[i];
|
||||
*e
|
||||
}
|
||||
|
||||
fn main() -> u64
|
||||
effects [unsafe]
|
||||
{
|
||||
var a: [u64; 3] = [10; 3];
|
||||
a[2] = 20;
|
||||
get(ref a, 0) + get(ref a, 2)
|
||||
}
|
||||
Reference in new issue
Block a user