diff --git a/doc/design/luaebpf/compiler.md b/doc/design/luaebpf/compiler.md index b1f9fb3..ce0649e 100644 --- a/doc/design/luaebpf/compiler.md +++ b/doc/design/luaebpf/compiler.md @@ -292,7 +292,9 @@ what `frame.lua` holds: `LEN`. A global or an upvalue resolves to the value the body left, a field of a table the compiler holds to that field's value, read when the function is compiled, and an index into a map to the map's helpers; a field of the context or of a struct map value goes to `context.lua` - or `map.lua`, and a method call on a proxy is read back by `CALL`. + or `map.lua`, and a method call on a proxy is read back by `CALL`. `CALL` holds a method to the + value past it, the one it was read from: a loop's first pass may have read the method before a + later pass widened that value. * `arithmetic.lua`: the arithmetic and bitwise opcodes and their forms with a constant, `UNM`, `BNOT` and `NOT`, with Lua's semantics over 64-bit integers. `MMBIN` and its kin, which the VM runs only when an operand is not a number, emit nothing, since an operand that is not a number is diff --git a/doc/guide/08-luaebpf.md b/doc/guide/08-luaebpf.md index 8b0cec1..2f8cc43 100644 --- a/doc/guide/08-luaebpf.md +++ b/doc/guide/08-luaebpf.md @@ -599,6 +599,10 @@ Give the variable one type on every path: `local v = 0`. These are refused too: * A view read in a function that has assigned the parameter its context or its packet came in: "a view is read through the context or a packet, which no parameter of the function holds here". A view keeps the offset it starts at and no packet, and its fields are read through that parameter. +* A method called on a variable that a loop sets to nil, or to another value, after a pass that + called it on the packet: "'getbyte' takes the packet, not a packet that may be nil". Lua raises + on the pass where the variable is not the packet, and the read would go through whatever word the + register holds there. * A verdict that may be nil: "the verdict may be nil here; test it first". The kernel would read nil as 0, which is an action. * A verdict that may be a boolean: "the verdict may be a boolean here; return an action". The @@ -641,6 +645,7 @@ Each refusal reads `lunatik: :: `. | a runtime is called through its method; write 'limits:run(...)' | `limits(1)` | `limits:run(1)` | | a runtime has no method 'call'; write 'limits:run(...)' | `limits:call(1)` | `limits:run(1)` | | the function needs 528 bytes of stack, over the 512 eBPF allows | a call into a runtime named by 480 bytes | a shorter name | +| 'getbyte' takes the packet, not a packet that may be nil | `p:getbyte(i)` in a loop that sets `p = nil` | a variable of its own for nil | | a number that may be nil cannot be compared with a number here | `==` on a variable nil on one path | `local v = 0` | | a number that may be nil has no truth value in a compiled function | `if v then`, `v` nil on one path | `local v = 0`, then `if v ~= 0 then` | | the verdict may be nil here; test it first | `return v`, `v` nil on one path | a verdict on every path | diff --git a/emit/call.lua b/emit/call.lua index b791834..317a066 100644 --- a/emit/call.lua +++ b/emit/call.lua @@ -156,12 +156,22 @@ local function subprogram(f, pc, ins, state, callee) end end +-- the call passes A+1 and the method is lowered for its recv, which a join keeps from a loop's first pass +local function received(f, pc, ins, state, callee) + local recv = state[ins.a + 1] + if not frame.same(recv, callee.recv) then + frame.refuse(f, pc, "'%s' takes the %s, not a %s", callee.name, frame.typename(callee.recv), + frame.typename(recv)) + end +end + function call.CALL(f, pc, ins, state) local callee = state[ins.a] if ins.b == 0 or ins.c == 0 then frame.refuse(f, pc, "a call with a variable number of values cannot be compiled") end if callee ~= nil and callee.t == METHOD then + received(f, pc, ins, state, callee) packet.method(f, pc, ins, state, callee) elseif callee ~= nil and runtime.isproxy(callee.k) then runtime.call(f, pc, ins, state, callee) diff --git a/emit/frame.lua b/emit/frame.lua index 28f7a68..c10bb2d 100644 --- a/emit/frame.lua +++ b/emit/frame.lua @@ -169,6 +169,7 @@ local function same(a, b) end return a.t == b.t and a.k == b.k and a.map == b.map and a.codec == b.codec end +frame.same = same local function copy(state) local out = {}