cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

dummy_st_ops.c (3459B)


      1// SPDX-License-Identifier: GPL-2.0
      2/* Copyright (C) 2021. Huawei Technologies Co., Ltd */
      3#include <test_progs.h>
      4#include "dummy_st_ops.skel.h"
      5#include "trace_dummy_st_ops.skel.h"
      6
      7/* Need to keep consistent with definition in include/linux/bpf.h */
      8struct bpf_dummy_ops_state {
      9	int val;
     10};
     11
     12static void test_dummy_st_ops_attach(void)
     13{
     14	struct dummy_st_ops *skel;
     15	struct bpf_link *link;
     16
     17	skel = dummy_st_ops__open_and_load();
     18	if (!ASSERT_OK_PTR(skel, "dummy_st_ops_load"))
     19		return;
     20
     21	link = bpf_map__attach_struct_ops(skel->maps.dummy_1);
     22	ASSERT_EQ(libbpf_get_error(link), -EOPNOTSUPP, "dummy_st_ops_attach");
     23
     24	dummy_st_ops__destroy(skel);
     25}
     26
     27static void test_dummy_init_ret_value(void)
     28{
     29	__u64 args[1] = {0};
     30	LIBBPF_OPTS(bpf_test_run_opts, attr,
     31		.ctx_in = args,
     32		.ctx_size_in = sizeof(args),
     33	);
     34	struct dummy_st_ops *skel;
     35	int fd, err;
     36
     37	skel = dummy_st_ops__open_and_load();
     38	if (!ASSERT_OK_PTR(skel, "dummy_st_ops_load"))
     39		return;
     40
     41	fd = bpf_program__fd(skel->progs.test_1);
     42	err = bpf_prog_test_run_opts(fd, &attr);
     43	ASSERT_OK(err, "test_run");
     44	ASSERT_EQ(attr.retval, 0xf2f3f4f5, "test_ret");
     45
     46	dummy_st_ops__destroy(skel);
     47}
     48
     49static void test_dummy_init_ptr_arg(void)
     50{
     51	int exp_retval = 0xbeef;
     52	struct bpf_dummy_ops_state in_state = {
     53		.val = exp_retval,
     54	};
     55	__u64 args[1] = {(unsigned long)&in_state};
     56	LIBBPF_OPTS(bpf_test_run_opts, attr,
     57		.ctx_in = args,
     58		.ctx_size_in = sizeof(args),
     59	);
     60	struct trace_dummy_st_ops *trace_skel;
     61	struct dummy_st_ops *skel;
     62	int fd, err;
     63
     64	skel = dummy_st_ops__open_and_load();
     65	if (!ASSERT_OK_PTR(skel, "dummy_st_ops_load"))
     66		return;
     67
     68	fd = bpf_program__fd(skel->progs.test_1);
     69
     70	trace_skel = trace_dummy_st_ops__open();
     71	if (!ASSERT_OK_PTR(trace_skel, "trace_dummy_st_ops__open"))
     72		goto done;
     73
     74	err = bpf_program__set_attach_target(trace_skel->progs.fentry_test_1,
     75					     fd, "test_1");
     76	if (!ASSERT_OK(err, "set_attach_target(fentry_test_1)"))
     77		goto done;
     78
     79	err = trace_dummy_st_ops__load(trace_skel);
     80	if (!ASSERT_OK(err, "load(trace_skel)"))
     81		goto done;
     82
     83	err = trace_dummy_st_ops__attach(trace_skel);
     84	if (!ASSERT_OK(err, "attach(trace_skel)"))
     85		goto done;
     86
     87	err = bpf_prog_test_run_opts(fd, &attr);
     88	ASSERT_OK(err, "test_run");
     89	ASSERT_EQ(in_state.val, 0x5a, "test_ptr_ret");
     90	ASSERT_EQ(attr.retval, exp_retval, "test_ret");
     91	ASSERT_EQ(trace_skel->bss->val, exp_retval, "fentry_val");
     92
     93done:
     94	dummy_st_ops__destroy(skel);
     95	trace_dummy_st_ops__destroy(trace_skel);
     96}
     97
     98static void test_dummy_multiple_args(void)
     99{
    100	__u64 args[5] = {0, -100, 0x8a5f, 'c', 0x1234567887654321ULL};
    101	LIBBPF_OPTS(bpf_test_run_opts, attr,
    102		.ctx_in = args,
    103		.ctx_size_in = sizeof(args),
    104	);
    105	struct dummy_st_ops *skel;
    106	int fd, err;
    107	size_t i;
    108	char name[8];
    109
    110	skel = dummy_st_ops__open_and_load();
    111	if (!ASSERT_OK_PTR(skel, "dummy_st_ops_load"))
    112		return;
    113
    114	fd = bpf_program__fd(skel->progs.test_2);
    115	err = bpf_prog_test_run_opts(fd, &attr);
    116	ASSERT_OK(err, "test_run");
    117	for (i = 0; i < ARRAY_SIZE(args); i++) {
    118		snprintf(name, sizeof(name), "arg %zu", i);
    119		ASSERT_EQ(skel->bss->test_2_args[i], args[i], name);
    120	}
    121
    122	dummy_st_ops__destroy(skel);
    123}
    124
    125void test_dummy_st_ops(void)
    126{
    127	if (test__start_subtest("dummy_st_ops_attach"))
    128		test_dummy_st_ops_attach();
    129	if (test__start_subtest("dummy_init_ret_value"))
    130		test_dummy_init_ret_value();
    131	if (test__start_subtest("dummy_init_ptr_arg"))
    132		test_dummy_init_ptr_arg();
    133	if (test__start_subtest("dummy_multiple_args"))
    134		test_dummy_multiple_args();
    135}