Skip to content

Commit 5f792dd

Browse files
blakej11Kernel Patches Daemon
authored andcommitted
Tests for the ".emit_strings" functionality in the BTF dumper.
When this mode is turned on, "emit_zeroes" and "compact" have no effect, and embedded NUL characters always terminate printing of an array. Signed-off-by: Blake Jones <[email protected]>
1 parent 35158c5 commit 5f792dd

File tree

1 file changed

+106
-0
lines changed

1 file changed

+106
-0
lines changed

tools/testing/selftests/bpf/prog_tests/btf_dump.c

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -879,6 +879,110 @@ static void test_btf_dump_var_data(struct btf *btf, struct btf_dump *d,
879879
"static int bpf_cgrp_storage_busy = (int)2", 2);
880880
}
881881

882+
struct btf_dump_string_ctx {
883+
struct btf *btf;
884+
struct btf_dump *d;
885+
char *str;
886+
struct btf_dump_type_data_opts *opts;
887+
int array_id;
888+
};
889+
890+
static int btf_dump_one_string(struct btf_dump_string_ctx *ctx,
891+
char *ptr, size_t ptr_sz,
892+
const char *expected_val)
893+
{
894+
size_t type_sz;
895+
int ret;
896+
897+
ctx->str[0] = '\0';
898+
type_sz = btf__resolve_size(ctx->btf, ctx->array_id);
899+
ret = btf_dump__dump_type_data(ctx->d, ctx->array_id, ptr, ptr_sz, ctx->opts);
900+
if (type_sz <= ptr_sz) {
901+
if (!ASSERT_EQ(ret, type_sz, "failed/unexpected type_sz"))
902+
return -EINVAL;
903+
} else {
904+
if (!ASSERT_EQ(ret, -E2BIG, "failed to return -E2BIG"))
905+
return -EINVAL;
906+
}
907+
if (!ASSERT_STREQ(ctx->str, expected_val, "ensure expected/actual match"))
908+
return -EFAULT;
909+
return 0;
910+
}
911+
912+
static void btf_dump_strings(struct btf_dump_string_ctx *ctx)
913+
{
914+
struct btf_dump_type_data_opts *opts = ctx->opts;
915+
916+
opts->emit_strings = true;
917+
918+
opts->compact = true;
919+
opts->emit_zeroes = false;
920+
921+
opts->skip_names = false;
922+
btf_dump_one_string(ctx, "foo", 4, "(char[4])\"foo\"");
923+
924+
opts->skip_names = true;
925+
btf_dump_one_string(ctx, "foo", 4, "\"foo\"");
926+
927+
/* This should have no effect. */
928+
opts->emit_zeroes = false;
929+
btf_dump_one_string(ctx, "foo", 4, "\"foo\"");
930+
931+
/* This should have no effect. */
932+
opts->compact = false;
933+
btf_dump_one_string(ctx, "foo", 4, "\"foo\"");
934+
935+
/* Non-printable characters come out as hex. */
936+
btf_dump_one_string(ctx, "fo\xff", 4, "\"fo\\xff\"");
937+
btf_dump_one_string(ctx, "fo\x7", 4, "\"fo\\x07\"");
938+
939+
/* Should get printed properly even though there's no NUL. */
940+
char food[4] = { 'f', 'o', 'o', 'd' };
941+
942+
btf_dump_one_string(ctx, food, 4, "\"food\"");
943+
944+
/* The embedded NUL should terminate the string. */
945+
char embed[4] = { 'f', 'o', '\0', 'd' };
946+
947+
btf_dump_one_string(ctx, embed, 4, "\"fo\"");
948+
}
949+
950+
static void test_btf_dump_string_data(void)
951+
{
952+
struct test_ctx t = {};
953+
char str[STRSIZE];
954+
struct btf_dump *d;
955+
DECLARE_LIBBPF_OPTS(btf_dump_type_data_opts, opts);
956+
struct btf_dump_string_ctx ctx;
957+
int char_id, int_id, array_id;
958+
959+
if (test_ctx__init(&t))
960+
return;
961+
962+
d = btf_dump__new(t.btf, btf_dump_snprintf, str, NULL);
963+
if (!ASSERT_OK_PTR(d, "could not create BTF dump"))
964+
return;
965+
966+
/* Generate BTF for a four-element char array. */
967+
char_id = btf__add_int(t.btf, "char", 1, BTF_INT_CHAR);
968+
ASSERT_EQ(char_id, 1, "char_id");
969+
int_id = btf__add_int(t.btf, "int", 4, BTF_INT_SIGNED);
970+
ASSERT_EQ(int_id, 2, "int_id");
971+
array_id = btf__add_array(t.btf, int_id, char_id, 4);
972+
ASSERT_EQ(array_id, 3, "array_id");
973+
974+
ctx.btf = t.btf;
975+
ctx.d = d;
976+
ctx.str = str;
977+
ctx.opts = &opts;
978+
ctx.array_id = array_id;
979+
980+
btf_dump_strings(&ctx);
981+
982+
btf_dump__free(d);
983+
test_ctx__free(&t);
984+
}
985+
882986
static void test_btf_datasec(struct btf *btf, struct btf_dump *d, char *str,
883987
const char *name, const char *expected_val,
884988
void *data, size_t data_sz)
@@ -970,6 +1074,8 @@ void test_btf_dump() {
9701074
test_btf_dump_struct_data(btf, d, str);
9711075
if (test__start_subtest("btf_dump: var_data"))
9721076
test_btf_dump_var_data(btf, d, str);
1077+
if (test__start_subtest("btf_dump: string_data"))
1078+
test_btf_dump_string_data();
9731079
btf_dump__free(d);
9741080
btf__free(btf);
9751081

0 commit comments

Comments
 (0)