Exemplo n.º 1
0
def _call_packet_copy(i, **kwargs):
    # If you're thinking 'wow, this function looks complicated,' just go
    # take a look at kernel/bpf/verifier.c:find_good_pkt_pointers to see
    # the hoops that we're jumping through here.
    ret = []
    fn, skb, dst_ptr, offset, num_bytes = i.src_vars

    # TODO: also support ptr to context?
    if not isinstance(skb, _mem.ArgVar):
        raise TranslationError(
            i.starts_line,
            'First argument to packet_copy must be SkBuffContext argument')
    elif not isinstance(num_bytes, _mem.ConstVar):
        raise TranslationError(
            i.starts_line,
            'Num bytes must not be dynamically defined for packet_copy')

    skb_data_mem = bi.Mem(_get_var_reg(skb), skb.var_type.data.offset,
                          bi.Size.Word)
    skb_data_end_mem = bi.Mem(_get_var_reg(skb), skb.var_type.data_end.offset,
                              bi.Size.Word)

    out_of_bounds = _make_tmp_label()

    #    ret = _mov(dst_ptr, bi.Reg.R1)
    dst_ptr = _convert_var(dst_ptr)

    # %r2 = skb->data
    ret.append(bi.Mov(skb_data_mem, bi.Reg.R2))

    # %r2 += offset
    if isinstance(offset, _mem.ConstVar):
        off_val = offset.val
        if hasattr(off_val, 'value'):
            off_val = off_val.value
        if off_val != 0:
            ret.append(bi.Add(bi.Imm(off_val), bi.Reg.R2))
    else:
        ret.extend(_mov(offset, bi.Reg.R3))
        ret.append(bi.Add(bi.Reg.R3, bi.Reg.R2))

    # %r3 = %r2
    ret.append(bi.Mov(bi.Reg.R2, bi.Reg.R3))

    # %r2 += num_bytes
    ret.append(bi.Add(bi.Imm(num_bytes.val), bi.Reg.R2))

    # %r4 = skb->data_end
    ret.append(bi.Mov(skb_data_end_mem, bi.Reg.R4))

    # if skb->data + offset + num_bytes > skb->data_end: goto out_of_bounds
    ret.append(bi.JumpIfGreaterThan(bi.Reg.R4, bi.Reg.R2, out_of_bounds))

    ret.extend(_memcpy_packet(i, dst_ptr, bi.Reg.R3, num_bytes.val))

    ret.append(bi.Label(out_of_bounds))

    return ret
Exemplo n.º 2
0
def _mov(src, dst):
    if isinstance(src, _mem.ConstVar) and isinstance(dst, _stack.StackVar):
        return _mov_const(src.var_type, src.val, _get_var_reg(dst), dst.offset)

    src, dst = _convert_var(src), _convert_var(dst)
    if isinstance(src, bi.Mem) and isinstance(dst, bi.Mem):
        return [bi.Mov(src, bi.Reg.R0), bi.Mov(bi.Reg.R0, dst)]
    else:
        return [bi.Mov(src, dst)]
Exemplo n.º 3
0
def _store_subscr_map(i, **kwargs):
    v, m, k = i.src_vars
    if not isinstance(m, _mem.ConstVar):
        raise TranslationError(i.starts_line,
                               'Cannot subscript dynamically selected map')
    return ([bi.Mov(bi.MapFdImm(m.val.fd), bi.Reg.R1)] +
            _lea(i, k, bi.Reg.R2, **kwargs) + _lea(i, v, bi.Reg.R3, **kwargs) +
            [
                bi.Mov(bi.Imm(0), bi.Reg.R4),
                bi.Call(bi.Imm(funcs.map_update_elem.num))
            ])
Exemplo n.º 4
0
def _call_mem_eq(i, **kwargs):
    if (not isinstance(i.src_vars[1], _mem.ConstVar)
            or not issubclass(i.src_vars[1].var_type, ctypes.Array)):
        raise TranslationError(
            i.starts_line, 'first arg to mem_eq must be const ctypes.Array')
    ret = []

    # stack=None because we shouldn't need to allocate anything
    ret.extend(_lea(i, i.src_vars[2], bi.Reg.R2, stack=None))

    false, done = _make_tmp_label(), _make_tmp_label()
    for off, v in enumerate(i.src_vars[1].val.value):
        ret.extend([
            bi.Mov(bi.Mem(bi.Reg.R2, off, bi.Size.Byte), bi.Reg.R1),
            bi.JumpIfNotEqual(bi.Imm(v), bi.Reg.R1, false)
        ])

    # If we made it here, it's a match
    ret.extend(_mov(bi.Imm(1), i.dst_vars[0]))
    ret.append(bi.Jump(done))

    # if we jumped here, it's not a match
    ret.append(bi.Label(false))
    ret.extend(_mov(bi.Imm(0), i.dst_vars[0]))
    ret.append(bi.Label(done))

    return ret
Exemplo n.º 5
0
def _lea(i, src, dst, stack, **kwargs):
    if issubclass(src.var_type, FileDescriptorDatastructure):
        if not isinstance(src, _mem.ConstVar):
            raise TranslationError(
                i.starts_line,
                'Cannot handle non-const file descriptor datastructures')
        return _mov(bi.MapFdImm(src.val.fd), dst)

    setup = []
    if isinstance(src, _mem.ConstVar):
        # We have to lay it down in memory ourselves, sadly
        tmp_src = stack.alloc(src.var_type)
        tmp_reg = _get_var_reg(tmp_src)
        setup = _mov_const(src.var_type, src.val, tmp_reg, tmp_src.offset)
        src = tmp_src

    # TODO: fix types. Right now, dt may by a ulong for addrof, because we
    # can't plug in real return types yet.
    # if not isinstance(dst, bi.Reg):
    #     st, dt = src.var_type, dst.var_type
    #     assert issubclass(dt,_types.Ptr) and dt.var_type == st

    reg = _get_var_reg(src)
    if src.offset == 0:
        return setup + _mov(reg, dst)
    else:
        return (
            setup +
            [bi.Mov(reg, bi.Reg.R0),
             bi.Add(bi.Imm(src.offset), bi.Reg.R0)] + _mov(bi.Reg.R0, dst))
Exemplo n.º 6
0
def _call_load_xdp_data(i, **kwargs):
    ret = []
    fn, skb, offset = i.src_vars
    data = i.dst_vars[0]
    if not isinstance(skb, _mem.ArgVar):
        raise TranslationError(
            i.starts_line,
            'First argument to load_xdp_data must be XdpMetaDataContext argument'
        )
    if isinstance(offset, _mem.ConstVar):
        assert issubclass(offset.var_type, _ctypes._SimpleCData)
        offset = offset.val.value
    skb_data_mem = bi.Mem(_get_var_reg(skb), offset, bi.Size.Word)

    ret.append(bi.Mov(skb_data_mem, bi.Reg.R2))
    data = _convert_var(data)
    ret.append(bi.Mov(bi.Reg.R2, data))
    return ret
Exemplo n.º 7
0
def _delete_subscr(i, **kwargs):
    if issubclass(i.src_vars[1].var_type, ctypes.Array):
        raise TranslationError(i.starts_line, 'Cannot delete from array')

    m, k = i.src_vars
    if not isinstance(m, _mem.ConstVar):
        raise TranslationError(i.starts_line,
                               'Cannot delete from dynamically selected map')

    return ([bi.Mov(bi.MapFdImm(m.val.fd), bi.Reg.R1)] +
            _lea(i, k, bi.Reg.R2, **kwargs) +
            [bi.Call(bi.Imm(funcs.map_delete_elem.num))])
Exemplo n.º 8
0
def _load_arr_element_addr(i, arr, index, dst_reg):
    if not isinstance(index, _mem.ConstVar):
        raise TranslationError(i.starts_line,
                               'Illegal to use dynamic index to array')

    if _is_ptr(arr.var_type):
        el_off = index.val.value * ctypes.sizeof(arr.var_type.var_type._type_)
        ret = _mov(arr, dst_reg)
    else:
        el_off = index.val.value * ctypes.sizeof(
            arr.var_type._type_) + arr.offset
        ret = [bi.Mov(_get_var_reg(arr), dst_reg)]

    if el_off != 0:
        ret.append(bi.Add(bi.Imm(el_off), dst_reg))

    return ret
Exemplo n.º 9
0
def _mov_const(val_type, val, reg, offset):
    if issubclass(val_type, FileDescriptorDatastructure):
        return (_mov(bi.MapFdImm(val.fd), bi.Reg.R0) +
                _mov(bi.Reg.R0, bi.Mem(reg, offset, bi.Size.Quad)))

    if issubclass(val_type, _ctypes._SimpleCData):
        # They may have passed us a vanilla int here
        if hasattr(val, 'value'):
            val = val.value
        dst_mem = bi.Mem(reg, offset, _get_cdata_size(val_type))
        return [bi.Mov(bi.Imm(val), dst_mem)]
    ret = []
    if issubclass(val_type, ctypes.Array):
        for i in range(val_type._length_):
            el_off = offset + ctypes.sizeof(val_type._type_) * i
            el = val[i] if i < len(val) else val_type._type_()
            ret.extend(_mov_const(val_type._type_, el, reg, el_off))
    else:
        for f, t in val_type._fields_:
            f_val = getattr(val, f)
            f_off = getattr(val_type, f).offset
            ret.extend(_mov_const(t, f_val, reg, offset + f_off))
    return ret