From: Yonghong Song <yhs@fb.com>
To: Daniel Borkmann <daniel@iogearbox.net>,
Alexei Starovoitov <ast@fb.com>, Martin Lau <kafai@fb.com>,
"netdev@vger.kernel.org" <netdev@vger.kernel.org>
Cc: Kernel Team <Kernel-team@fb.com>
Subject: Re: [PATCH bpf-next 02/13] bpf: btf: Add BTF_KIND_FUNC and BTF_KIND_FUNC_PROTO
Date: Wed, 17 Oct 2018 03:11:58 +0000 [thread overview]
Message-ID: <f276f153-52ad-1bc3-5a12-ddcdd193e8d8@fb.com> (raw)
In-Reply-To: <3a4bc2f7-9797-ccbe-d38f-c2d3ce662889@iogearbox.net>
On 10/15/18 3:30 PM, Daniel Borkmann wrote:
> On 10/12/2018 08:54 PM, Yonghong Song wrote:
>> This patch adds BTF_KIND_FUNC and BTF_KIND_FUNC_PROTO
>> support to the type section. BTF_KIND_FUNC_PROTO is used
>> to specify the type of a function pointer. With this,
>> BTF has a complete set of C types (except float).
>>
>> BTF_KIND_FUNC is used to specify the signature of a
>> defined subprogram. BTF_KIND_FUNC_PROTO can be referenced
>> by another type, e.g., a pointer type, and BTF_KIND_FUNC
>> type cannot be referenced by another type.
>>
>> For both BTF_KIND_FUNC and BTF_KIND_FUNC_PROTO types,
>> the func return type is in t->type (where t is a
>> "struct btf_type" object). The func args are an array of
>> u32s immediately following object "t".
>>
>> As a concrete example, for the C program below,
>> $ cat test.c
>> int foo(int (*bar)(int)) { return bar(5); }
>> with latest llvm trunk built with Debug mode, we have
>> $ clang -target bpf -g -O2 -mllvm -debug-only=btf -c test.c
>> Type Table:
>> [1] FUNC name_off=1 info=0x0c000001 size/type=2
>> param_type=3
>> [2] INT name_off=11 info=0x01000000 size/type=4
>> desc=0x01000020
>> [3] PTR name_off=0 info=0x02000000 size/type=4
>> [4] FUNC_PROTO name_off=0 info=0x0d000001 size/type=2
>> param_type=2
>>
>> String Table:
>> 0 :
>> 1 : foo
>> 5 : .text
>> 11 : int
>> 15 : test.c
>> 22 : int foo(int (*bar)(int)) { return bar(5); }
>>
>> FuncInfo Table:
>> sec_name_off=5
>> insn_offset=<Omitted> type_id=1
>>
>> ...
>>
>> (Eventually we shall have bpftool to dump btf information
>> like the above.)
>>
>> Function "foo" has a FUNC type (type_id = 1).
>> The parameter of "foo" has type_id 3 which is PTR->FUNC_PROTO,
>> where FUNC_PROTO refers to function pointer "bar".
>
> Should also "bar" be part of the string table (at least at some point in future)?
Yes, we can do it. The dwarf for the abovee example looks like
0x00000043: DW_TAG_formal_parameter
DW_AT_location (0x00000000
[0x0000000000000000, 0x0000000000000008):
DW_OP_reg1 W1
[0x0000000000000008, 0x0000000000000018):
DW_OP_reg2 W2)
DW_AT_name ("bar")
DW_AT_decl_file ("/home/yhs/tmp/t.c")
DW_AT_decl_line (1)
DW_AT_type (0x0000005a "subroutine int*")
0x0000005a: DW_TAG_pointer_type
DW_AT_type (0x0000005f "subroutine int")
0x0000005f: DW_TAG_subroutine_type
DW_AT_type (0x00000053 "int")
DW_AT_prototyped (true)
0x00000064: DW_TAG_formal_parameter
DW_AT_type (0x00000053 "int")
0x00000069: NULL
0x0000006a: NULL
The current llvm implementation does not record func
parameter name, so "bar" got lost. We could associate
"bar" with pointer type in the future implementation.
> Iow, if verifier hints to an issue in the program when it would for example walk
> pointers and rewrite ctx access, then it could dump the var name along with it.
> It might be useful as well in combination with 22 from str table, when annotating
> the source. We might need support for variadic functions, though. How is LLVM
> handling the latter with the recent BTF support?
The LLVM implementation does support variadic functions.
The last type id 0 indicates a variadic function.
>
>> In FuncInfo Table, for section .text, the function,
>> with to-be-determined offset (marked as <Omitted>),
>> has type_id=1 which refers to a FUNC type.
>> This way, the function signature is
>> available to both kernel and user space.
>> Here, the insn offset is not available during the dump time
>> as relocation is resolved pretty late in the compilation process.
>>
>> Signed-off-by: Martin KaFai Lau <kafai@fb.com>
>> Signed-off-by: Yonghong Song <yhs@fb.com>
next prev parent reply other threads:[~2018-10-17 11:05 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-10-12 18:54 [PATCH bpf-next 00/13] bpf: add btf func info support Yonghong Song
2018-10-12 18:54 ` [PATCH bpf-next 01/13] bpf: btf: Break up btf_type_is_void() Yonghong Song
2018-10-15 21:50 ` Daniel Borkmann
2018-10-12 18:54 ` [PATCH bpf-next 02/13] bpf: btf: Add BTF_KIND_FUNC and BTF_KIND_FUNC_PROTO Yonghong Song
2018-10-15 22:30 ` Daniel Borkmann
2018-10-17 3:11 ` Yonghong Song [this message]
2018-10-15 22:36 ` Daniel Borkmann
2018-10-17 3:22 ` Yonghong Song
2018-10-12 18:54 ` [PATCH bpf-next 03/13] tools/bpf: sync kernel btf.h header Yonghong Song
2018-10-12 18:54 ` [PATCH bpf-next 04/13] tools/bpf: add btf func/func_proto unit tests in selftest test_btf Yonghong Song
2018-10-16 18:27 ` [PATCH bpf-next 00/13] bpf: add btf func info support Alexei Starovoitov
2018-10-17 3:25 ` Yonghong Song
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=f276f153-52ad-1bc3-5a12-ddcdd193e8d8@fb.com \
--to=yhs@fb.com \
--cc=Kernel-team@fb.com \
--cc=ast@fb.com \
--cc=daniel@iogearbox.net \
--cc=kafai@fb.com \
--cc=netdev@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox