Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support casting int <-> int array #2686

Merged
merged 6 commits into from
Aug 25, 2023
Merged

Commits on Aug 23, 2023

  1. Parser: refactor simple types

    Split simple types into integer types and builtin types. This is useful
    as it will allow special treatment of integer types and their usage in
    constructions where builtin types don't make sense (e.g. arrays).
    viktormalik committed Aug 23, 2023
    Configuration menu
    Copy the full SHA
    95f6b22 View commit details
    Browse the repository at this point in the history
  2. Support casting integers to integer arrays

    The syntax is the same as for other casts:
    
        $array = (int8[8])42;
    
    For now, it is only possible to cast integers to integer arrays. Both
    the cast value and the target array must have the same size.
    
    The integer being cast is copied to the BPF stack and the pointer to it
    is then reinterpreted as the target array pointer.
    
    This, in combination with recently added array comparison, allows to
    compare values which are stored under different types in kernel.
    E.g., IP addresses are represented with 32-bit int in "struct sock" [1]
    but with byte array in TCP tracepoints [2]. This feature allows to
    compare them:
    
        kfunc:tcp_connect
        {
            @ = args->sk->__sk_common.skc_daddr;
        }
        tracepoint:tcp:tcp_rcv_space_adjust
        {
            // map values are always 64-bit so first cast to uint32
            // and then to the target array
            if ((uint8[4])(uint32)@ == args->daddr)
            {
                ...
            }
        }
    
    [1] https://elixir.bootlin.com/linux/v6.4/source/include/net/sock.h#L167
    [2] https://elixir.bootlin.com/linux/v6.4/source/include/trace/events/tcp.h#L64
    viktormalik committed Aug 23, 2023
    Configuration menu
    Copy the full SHA
    f814b46 View commit details
    Browse the repository at this point in the history
  3. Casting to arrays: auto-determine number of elems

    Support omitting the array size when casting to arrays as it can be
    computed automatically in semantic analyser.
    
    E.g. for `(int8[])42`, the array will have 8 elements (since ints are by
    default 64-bit).
    viktormalik committed Aug 23, 2023
    Configuration menu
    Copy the full SHA
    2e5186e View commit details
    Browse the repository at this point in the history
  4. Support casting integer arrays to integers

    It is only possible to cast the entire array to an integer of the same
    size. The main purpose is to allow conversion of byte arrays such as
    those returned by the 'pton' builtin:
    
        kfunc:tcp_connect
        {
            if (args->sk->__sk_common.skc_daddr == (uint32)pton("127.0.0.1"))
                ...
        }
    
    Since arrays are internally stored as pointers, the codegen for this
    just reinterprets the pointer to the expected integer size and loads the
    data as an integer (by a direct load or by an additional proberead).
    viktormalik committed Aug 23, 2023
    Configuration menu
    Copy the full SHA
    6cd00fc View commit details
    Browse the repository at this point in the history
  5. Document casting ints to int arrays

    Add examples of casts to both the Reference guide and manpage.
    viktormalik committed Aug 23, 2023
    Configuration menu
    Copy the full SHA
    5fe8184 View commit details
    Browse the repository at this point in the history
  6. Configuration menu
    Copy the full SHA
    388e7e3 View commit details
    Browse the repository at this point in the history