• Marek Vavruša's avatar
    lua: codegen tests, tracking inverse form of null guards, IPv6 header builtins (#1991) · d1791f0b
    Marek Vavruša authored
    * lua: eliding repetitive NULL checks, fixes for read types, luacheck
    
    This fixes verifier failures in some kernel versions that track whether
    a possible NULL pointer has been check (and shouldn't be checked twice).
    
    It also fixes accessing memory from maps with composite keys.
    
    It fixes a case when temporary variable is used in condition,
    and the body of the condition contains just an assignment from
    immediate value to a variable that existed before the condition, e.g.:
    
    ```
    local x = 1 -- Assign constant to 'x', it will be materialized here
    if skb.len > 0 then -- New BB, not possible to fold as condition isn't const
        x = 2 -- Assign constant, it failed to materialize here
    end -- End of BB
    -- Value of x is always 1
    ```
    
    The `bpf.socket()` support ljsyscall socket as first parameter, and
    fixes compatibility with newer ljsyscall (type name has changed).
    
    * reverse BPF_LD ntohl() semantics for <= 32bit loads as well
    
    The loads using traditional instructions always do ntohl():
    
    https://www.kernel.org/doc/Documentation/networking/filter.txt
    
    They are however needed to support reads not using `skb`, e.g. NET_OFF
    
    * proto: add builtins for traversing IPv6 header and extension headers
    
    The IPv6 header is fixed size (40B), but it may contain either
    extension header, or transport  protocol after it, so the caller
    must check the `next_header` field before traversing to determine
    which dissector to use to traverse it, e.g.:
    
    ```
    if ip6.next_header == 44 then
       ip6 = ip6.ip6_opt -- Skip fragment header
    end
    if ip6.next_header == c.ip.proto_tcp then
       local tcp = ip6.tcp -- Finally, TCP
    end
    ```
    
    * reverse ntohl() for indirect BPF_LD as well
    
    * lua: started codegen tests, direct skb support, bugfixes
    
    This starts adding compiler correctness tests for basic expressions,
    variable source tracking, and control flow.
    
    Added:
    * direct skb->data access (in addition to BPF_LDABS, BPF_LDIND)
    * loads and stores from skb->data and map value backed memory
    * unified variable source tracking (ptr_to_ctx, ptr_to_map[_or_null], ptr_to_skb, ptr_to_pkt, ptr_to_stack)
    * BPF constants introduced between 4.10-4.15
    * bpf.dump_string() to dump generated assembly (text) to string
    
    Fixes:
    * pointer nil check tracking
    * dissectors for map value backed memory
    * ljsyscall extensions when the version is too old
    * KPRI nil variables used in conditions
    * wrongly elided constant materialization on condition-less jumps
    * loads/stores from stack memory using variable offset
    
    * lua: track inverse null guards (if x ~= nil)
    
    The verifier prohibits pointer comparisons except the first NULL check,
    so both forms must be tracked, otherwise it will check for NULL twice
    and verifier will reject it.
    
    * lua: support cdata and constants larger than i32, fix shadowed variables
    
    This adds support for numeric cdata constants (either in program, or
    retrieved with GGET/UGET). Values larger than i32 are coerced into i64.
    
    This also fixes shadowing of variables, and fixes materialization of
    result of variable copy at the end of basic block.
    d1791f0b
ljbytecode.lua 2.18 KB