2020-09-29 16:50:46 -07:00
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2020 Google */
# include <test_progs.h>
# include <bpf/libbpf.h>
# include <bpf/btf.h>
# include "test_ksyms_btf.skel.h"
2020-10-19 12:42:25 -07:00
# include "test_ksyms_btf_null_check.skel.h"
2021-08-11 17:38:19 -07:00
# include "test_ksyms_weak.skel.h"
2021-10-28 12:04:59 +05:30
# include "test_ksyms_weak.lskel.h"
2021-12-16 16:31:52 -08:00
# include "test_ksyms_btf_write_check.skel.h"
2020-09-29 16:50:46 -07:00
static int duration ;
2020-10-19 12:42:25 -07:00
static void test_basic ( void )
2020-09-29 16:50:46 -07:00
{
__u64 runqueues_addr , bpf_prog_active_addr ;
2020-09-29 16:50:49 -07:00
__u32 this_rq_cpu ;
int this_bpf_prog_active ;
2020-09-29 16:50:46 -07:00
struct test_ksyms_btf * skel = NULL ;
struct test_ksyms_btf__data * data ;
int err ;
err = kallsyms_find ( " runqueues " , & runqueues_addr ) ;
if ( CHECK ( err = = - EINVAL , " kallsyms_fopen " , " failed to open: %d \n " , errno ) )
return ;
if ( CHECK ( err = = - ENOENT , " ksym_find " , " symbol 'runqueues' not found \n " ) )
return ;
err = kallsyms_find ( " bpf_prog_active " , & bpf_prog_active_addr ) ;
if ( CHECK ( err = = - EINVAL , " kallsyms_fopen " , " failed to open: %d \n " , errno ) )
return ;
if ( CHECK ( err = = - ENOENT , " ksym_find " , " symbol 'bpf_prog_active' not found \n " ) )
return ;
skel = test_ksyms_btf__open_and_load ( ) ;
if ( CHECK ( ! skel , " skel_open " , " failed to open and load skeleton \n " ) )
goto cleanup ;
err = test_ksyms_btf__attach ( skel ) ;
if ( CHECK ( err , " skel_attach " , " skeleton attach failed: %d \n " , err ) )
goto cleanup ;
/* trigger tracepoint */
usleep ( 1 ) ;
data = skel - > data ;
CHECK ( data - > out__runqueues_addr ! = runqueues_addr , " runqueues_addr " ,
" got %llu, exp %llu \n " ,
( unsigned long long ) data - > out__runqueues_addr ,
( unsigned long long ) runqueues_addr ) ;
CHECK ( data - > out__bpf_prog_active_addr ! = bpf_prog_active_addr , " bpf_prog_active_addr " ,
" got %llu, exp %llu \n " ,
( unsigned long long ) data - > out__bpf_prog_active_addr ,
( unsigned long long ) bpf_prog_active_addr ) ;
2020-09-29 16:50:49 -07:00
CHECK ( data - > out__rq_cpu = = - 1 , " rq_cpu " ,
" got %u, exp != -1 \n " , data - > out__rq_cpu ) ;
CHECK ( data - > out__bpf_prog_active < 0 , " bpf_prog_active " ,
" got %d, exp >= 0 \n " , data - > out__bpf_prog_active ) ;
CHECK ( data - > out__cpu_0_rq_cpu ! = 0 , " cpu_rq(0)->cpu " ,
" got %u, exp 0 \n " , data - > out__cpu_0_rq_cpu ) ;
this_rq_cpu = data - > out__this_rq_cpu ;
CHECK ( this_rq_cpu ! = data - > out__rq_cpu , " this_rq_cpu " ,
" got %u, exp %u \n " , this_rq_cpu , data - > out__rq_cpu ) ;
this_bpf_prog_active = data - > out__this_bpf_prog_active ;
CHECK ( this_bpf_prog_active ! = data - > out__bpf_prog_active , " this_bpf_prog_active " ,
" got %d, exp %d \n " , this_bpf_prog_active ,
data - > out__bpf_prog_active ) ;
2020-09-29 16:50:46 -07:00
cleanup :
test_ksyms_btf__destroy ( skel ) ;
}
2020-10-19 12:42:25 -07:00
static void test_null_check ( void )
{
struct test_ksyms_btf_null_check * skel ;
skel = test_ksyms_btf_null_check__open_and_load ( ) ;
CHECK ( skel , " skel_open " , " unexpected load of a prog missing null check \n " ) ;
test_ksyms_btf_null_check__destroy ( skel ) ;
}
2021-08-11 17:38:19 -07:00
static void test_weak_syms ( void )
{
struct test_ksyms_weak * skel ;
struct test_ksyms_weak__data * data ;
int err ;
skel = test_ksyms_weak__open_and_load ( ) ;
2021-10-28 12:04:59 +05:30
if ( ! ASSERT_OK_PTR ( skel , " test_ksyms_weak__open_and_load " ) )
2021-08-11 17:38:19 -07:00
return ;
err = test_ksyms_weak__attach ( skel ) ;
2021-10-28 12:04:59 +05:30
if ( ! ASSERT_OK ( err , " test_ksyms_weak__attach " ) )
2021-08-11 17:38:19 -07:00
goto cleanup ;
/* trigger tracepoint */
usleep ( 1 ) ;
data = skel - > data ;
ASSERT_EQ ( data - > out__existing_typed , 0 , " existing typed ksym " ) ;
ASSERT_NEQ ( data - > out__existing_typeless , - 1 , " existing typeless ksym " ) ;
ASSERT_EQ ( data - > out__non_existent_typeless , 0 , " nonexistent typeless ksym " ) ;
ASSERT_EQ ( data - > out__non_existent_typed , 0 , " nonexistent typed ksym " ) ;
cleanup :
test_ksyms_weak__destroy ( skel ) ;
}
2021-10-28 12:04:59 +05:30
static void test_weak_syms_lskel ( void )
{
struct test_ksyms_weak_lskel * skel ;
struct test_ksyms_weak_lskel__data * data ;
int err ;
skel = test_ksyms_weak_lskel__open_and_load ( ) ;
if ( ! ASSERT_OK_PTR ( skel , " test_ksyms_weak_lskel__open_and_load " ) )
return ;
err = test_ksyms_weak_lskel__attach ( skel ) ;
if ( ! ASSERT_OK ( err , " test_ksyms_weak_lskel__attach " ) )
goto cleanup ;
/* trigger tracepoint */
usleep ( 1 ) ;
data = skel - > data ;
ASSERT_EQ ( data - > out__existing_typed , 0 , " existing typed ksym " ) ;
ASSERT_NEQ ( data - > out__existing_typeless , - 1 , " existing typeless ksym " ) ;
ASSERT_EQ ( data - > out__non_existent_typeless , 0 , " nonexistent typeless ksym " ) ;
ASSERT_EQ ( data - > out__non_existent_typed , 0 , " nonexistent typed ksym " ) ;
cleanup :
test_ksyms_weak_lskel__destroy ( skel ) ;
}
2022-03-19 13:38:26 +05:30
static void test_write_check ( bool test_handler1 )
2021-12-16 16:31:52 -08:00
{
struct test_ksyms_btf_write_check * skel ;
2022-03-19 13:38:26 +05:30
skel = test_ksyms_btf_write_check__open ( ) ;
if ( ! ASSERT_OK_PTR ( skel , " test_ksyms_btf_write_check__open " ) )
return ;
bpf_program__set_autoload ( test_handler1 ? skel - > progs . handler2 : skel - > progs . handler1 , false ) ;
ASSERT_ERR ( test_ksyms_btf_write_check__load ( skel ) ,
" unexpected load of a prog writing to ksym memory \n " ) ;
2021-12-16 16:31:52 -08:00
test_ksyms_btf_write_check__destroy ( skel ) ;
}
2020-10-19 12:42:25 -07:00
void test_ksyms_btf ( void )
{
int percpu_datasec ;
struct btf * btf ;
btf = libbpf_find_kernel_btf ( ) ;
2021-05-24 20:59:32 -07:00
if ( ! ASSERT_OK_PTR ( btf , " btf_exists " ) )
2020-10-19 12:42:25 -07:00
return ;
percpu_datasec = btf__find_by_name_kind ( btf , " .data..percpu " ,
BTF_KIND_DATASEC ) ;
btf__free ( btf ) ;
if ( percpu_datasec < 0 ) {
printf ( " %s:SKIP:no PERCPU DATASEC in kernel btf \n " ,
__func__ ) ;
test__skip ( ) ;
return ;
}
if ( test__start_subtest ( " basic " ) )
test_basic ( ) ;
if ( test__start_subtest ( " null_check " ) )
test_null_check ( ) ;
2021-08-11 17:38:19 -07:00
if ( test__start_subtest ( " weak_ksyms " ) )
test_weak_syms ( ) ;
2021-10-28 12:04:59 +05:30
if ( test__start_subtest ( " weak_ksyms_lskel " ) )
test_weak_syms_lskel ( ) ;
2021-12-16 16:31:52 -08:00
2022-03-19 13:38:26 +05:30
if ( test__start_subtest ( " write_check1 " ) )
test_write_check ( true ) ;
if ( test__start_subtest ( " write_check2 " ) )
test_write_check ( false ) ;
2020-10-19 12:42:25 -07:00
}