|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | +/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */ |
| 3 | +#include <test_progs.h> |
| 4 | +#include <sys/mman.h> |
| 5 | + |
| 6 | +#include "stream.skel.h" |
| 7 | +#include "stream_fail.skel.h" |
| 8 | + |
| 9 | +void test_stream_failure(void) |
| 10 | +{ |
| 11 | + RUN_TESTS(stream_fail); |
| 12 | +} |
| 13 | + |
| 14 | +void test_stream_success(void) |
| 15 | +{ |
| 16 | + RUN_TESTS(stream); |
| 17 | + return; |
| 18 | +} |
| 19 | + |
| 20 | +struct { |
| 21 | + int prog_off; |
| 22 | + const char *errstr; |
| 23 | +} stream_error_arr[] = { |
| 24 | + { |
| 25 | + offsetof(struct stream, progs.stream_cond_break), |
| 26 | + "ERROR: Timeout detected for may_goto instruction", |
| 27 | + }, |
| 28 | + { |
| 29 | + offsetof(struct stream, progs.stream_deadlock), |
| 30 | + "ERROR: AA or ABBA deadlock detected", |
| 31 | + }, |
| 32 | +}; |
| 33 | + |
| 34 | +void test_stream_errors(void) |
| 35 | +{ |
| 36 | + LIBBPF_OPTS(bpf_test_run_opts, opts); |
| 37 | + LIBBPF_OPTS(bpf_prog_stream_read_opts, ropts); |
| 38 | + struct stream *skel; |
| 39 | + int ret, prog_fd; |
| 40 | + char buf[64]; |
| 41 | + |
| 42 | + skel = stream__open_and_load(); |
| 43 | + if (!ASSERT_OK_PTR(skel, "stream__open_and_load")) |
| 44 | + return; |
| 45 | + |
| 46 | + for (int i = 0; i < ARRAY_SIZE(stream_error_arr); i++) { |
| 47 | + struct bpf_program **prog; |
| 48 | + |
| 49 | + prog = (struct bpf_program **)(((char *)skel) + stream_error_arr[i].prog_off); |
| 50 | + prog_fd = bpf_program__fd(*prog); |
| 51 | + ret = bpf_prog_test_run_opts(prog_fd, &opts); |
| 52 | + ASSERT_OK(ret, "ret"); |
| 53 | + ASSERT_OK(opts.retval, "retval"); |
| 54 | + |
| 55 | +#if !defined(__x86_64__) |
| 56 | + ASSERT_TRUE(1, "Timed may_goto unsupported, skip."); |
| 57 | + if (i == 0) { |
| 58 | + ret = bpf_prog_stream_read(prog_fd, 2, buf, sizeof(buf), &ropts); |
| 59 | + ASSERT_EQ(ret, 0, "stream read"); |
| 60 | + continue; |
| 61 | + } |
| 62 | +#endif |
| 63 | + |
| 64 | + ret = bpf_prog_stream_read(prog_fd, BPF_STREAM_STDERR, buf, sizeof(buf), &ropts); |
| 65 | + ASSERT_EQ(ret, sizeof(buf), "stream read"); |
| 66 | + ASSERT_STRNEQ(stream_error_arr[i].errstr, buf, strlen(stream_error_arr[i].errstr), |
| 67 | + "compare error msg"); |
| 68 | + } |
| 69 | + |
| 70 | + stream__destroy(skel); |
| 71 | +} |
| 72 | + |
| 73 | +void test_stream_syscall(void) |
| 74 | +{ |
| 75 | + LIBBPF_OPTS(bpf_test_run_opts, opts); |
| 76 | + LIBBPF_OPTS(bpf_prog_stream_read_opts, ropts); |
| 77 | + struct stream *skel; |
| 78 | + int ret, prog_fd; |
| 79 | + char buf[64]; |
| 80 | + |
| 81 | + skel = stream__open_and_load(); |
| 82 | + if (!ASSERT_OK_PTR(skel, "stream__open_and_load")) |
| 83 | + return; |
| 84 | + |
| 85 | + prog_fd = bpf_program__fd(skel->progs.stream_syscall); |
| 86 | + ret = bpf_prog_test_run_opts(prog_fd, &opts); |
| 87 | + ASSERT_OK(ret, "ret"); |
| 88 | + ASSERT_OK(opts.retval, "retval"); |
| 89 | + |
| 90 | + ASSERT_LT(bpf_prog_stream_read(0, BPF_STREAM_STDOUT, buf, sizeof(buf), &ropts), 0, "error"); |
| 91 | + ret = -errno; |
| 92 | + ASSERT_EQ(ret, -EINVAL, "bad prog_fd"); |
| 93 | + |
| 94 | + ASSERT_LT(bpf_prog_stream_read(prog_fd, 0, buf, sizeof(buf), &ropts), 0, "error"); |
| 95 | + ret = -errno; |
| 96 | + ASSERT_EQ(ret, -ENOENT, "bad stream id"); |
| 97 | + |
| 98 | + ASSERT_LT(bpf_prog_stream_read(prog_fd, BPF_STREAM_STDOUT, NULL, sizeof(buf), NULL), 0, "error"); |
| 99 | + ret = -errno; |
| 100 | + ASSERT_EQ(ret, -EFAULT, "bad stream buf"); |
| 101 | + |
| 102 | + ret = bpf_prog_stream_read(prog_fd, BPF_STREAM_STDOUT, buf, 2, NULL); |
| 103 | + ASSERT_EQ(ret, 2, "bytes"); |
| 104 | + ret = bpf_prog_stream_read(prog_fd, BPF_STREAM_STDOUT, buf, 2, NULL); |
| 105 | + ASSERT_EQ(ret, 2, "bytes"); |
| 106 | + ret = bpf_prog_stream_read(prog_fd, BPF_STREAM_STDOUT, buf, 1, &ropts); |
| 107 | + ASSERT_EQ(ret, 0, "no bytes stdout"); |
| 108 | + ret = bpf_prog_stream_read(prog_fd, BPF_STREAM_STDERR, buf, 1, &ropts); |
| 109 | + ASSERT_EQ(ret, 0, "no bytes stderr"); |
| 110 | + |
| 111 | + stream__destroy(skel); |
| 112 | +} |
0 commit comments