|
| 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 | +#include <regex.h> |
| 6 | + |
| 7 | +#include "stream.skel.h" |
| 8 | +#include "stream_fail.skel.h" |
| 9 | + |
| 10 | +void test_stream_failure(void) |
| 11 | +{ |
| 12 | + RUN_TESTS(stream_fail); |
| 13 | +} |
| 14 | + |
| 15 | +void test_stream_success(void) |
| 16 | +{ |
| 17 | + RUN_TESTS(stream); |
| 18 | + return; |
| 19 | +} |
| 20 | + |
| 21 | +struct { |
| 22 | + int prog_off; |
| 23 | + const char *errstr; |
| 24 | +} stream_error_arr[] = { |
| 25 | + { |
| 26 | + offsetof(struct stream, progs.stream_cond_break), |
| 27 | + "ERROR: Timeout detected for may_goto instruction\n" |
| 28 | + "CPU: [0-9]+ UID: 0 PID: [0-9]+ Comm: test_progs\n" |
| 29 | + "Call trace:\n" |
| 30 | + "([a-zA-Z_][a-zA-Z0-9_]*\\+0x[0-9a-fA-F]+/0x[0-9a-fA-F]+\n" |
| 31 | + "|[ \t]+[^\n]+\n)*", |
| 32 | + }, |
| 33 | + { |
| 34 | + offsetof(struct stream, progs.stream_deadlock), |
| 35 | + "ERROR: AA or ABBA deadlock detected for bpf_res_spin_lock\n" |
| 36 | + "Attempted lock = (0x[0-9a-fA-F]+)\n" |
| 37 | + "Total held locks = 1\n" |
| 38 | + "Held lock\\[ 0\\] = \\1\n" // Lock address must match |
| 39 | + "CPU: [0-9]+ UID: 0 PID: [0-9]+ Comm: test_progs\n" |
| 40 | + "Call trace:\n" |
| 41 | + "([a-zA-Z_][a-zA-Z0-9_]*\\+0x[0-9a-fA-F]+/0x[0-9a-fA-F]+\n" |
| 42 | + "|[ \t]+[^\n]+\n)*", |
| 43 | + }, |
| 44 | +}; |
| 45 | + |
| 46 | +static int match_regex(const char *pattern, const char *string) |
| 47 | +{ |
| 48 | + int err, rc; |
| 49 | + regex_t re; |
| 50 | + |
| 51 | + err = regcomp(&re, pattern, REG_EXTENDED | REG_NEWLINE); |
| 52 | + if (err) { |
| 53 | + char errbuf[512]; |
| 54 | + |
| 55 | + regerror(err, &re, errbuf, sizeof(errbuf)); |
| 56 | + return -1; |
| 57 | + } |
| 58 | + rc = regexec(&re, string, 0, NULL, 0); |
| 59 | + regfree(&re); |
| 60 | + return rc == 0 ? 1 : 0; |
| 61 | +} |
| 62 | + |
| 63 | +void test_stream_errors(void) |
| 64 | +{ |
| 65 | + LIBBPF_OPTS(bpf_test_run_opts, opts); |
| 66 | + LIBBPF_OPTS(bpf_prog_stream_read_opts, ropts); |
| 67 | + char buf[1024] = {}; |
| 68 | + struct stream *skel; |
| 69 | + int ret, prog_fd; |
| 70 | + |
| 71 | + skel = stream__open_and_load(); |
| 72 | + if (!ASSERT_OK_PTR(skel, "stream__open_and_load")) |
| 73 | + return; |
| 74 | + |
| 75 | + for (int i = 0; i < ARRAY_SIZE(stream_error_arr); i++) { |
| 76 | + struct bpf_program **prog; |
| 77 | + |
| 78 | + prog = (struct bpf_program **)(((char *)skel) + stream_error_arr[i].prog_off); |
| 79 | + prog_fd = bpf_program__fd(*prog); |
| 80 | + ret = bpf_prog_test_run_opts(prog_fd, &opts); |
| 81 | + ASSERT_OK(ret, "ret"); |
| 82 | + ASSERT_OK(opts.retval, "retval"); |
| 83 | + |
| 84 | +#if !defined(__x86_64__) |
| 85 | + ASSERT_TRUE(1, "Timed may_goto unsupported, skip."); |
| 86 | + if (i == 0) { |
| 87 | + ret = bpf_prog_stream_read(prog_fd, 2, buf, sizeof(buf), &ropts); |
| 88 | + ASSERT_EQ(ret, 0, "stream read"); |
| 89 | + continue; |
| 90 | + } |
| 91 | +#endif |
| 92 | + |
| 93 | + ret = bpf_prog_stream_read(prog_fd, BPF_STREAM_STDERR, buf, sizeof(buf), &ropts); |
| 94 | + ASSERT_GT(ret, 0, "stream read"); |
| 95 | + int len = ret; |
| 96 | + |
| 97 | + ret = match_regex(stream_error_arr[i].errstr, buf); |
| 98 | + ASSERT_GE(ret, 0, "regex match noerror"); |
| 99 | + ASSERT_TRUE(ret == 0, "regex match"); |
| 100 | + |
| 101 | + fwrite(buf, 1, len, stdout); |
| 102 | + } |
| 103 | + |
| 104 | + stream__destroy(skel); |
| 105 | +} |
| 106 | + |
| 107 | +void test_stream_syscall(void) |
| 108 | +{ |
| 109 | + LIBBPF_OPTS(bpf_test_run_opts, opts); |
| 110 | + LIBBPF_OPTS(bpf_prog_stream_read_opts, ropts); |
| 111 | + struct stream *skel; |
| 112 | + int ret, prog_fd; |
| 113 | + char buf[64]; |
| 114 | + |
| 115 | + skel = stream__open_and_load(); |
| 116 | + if (!ASSERT_OK_PTR(skel, "stream__open_and_load")) |
| 117 | + return; |
| 118 | + |
| 119 | + prog_fd = bpf_program__fd(skel->progs.stream_syscall); |
| 120 | + ret = bpf_prog_test_run_opts(prog_fd, &opts); |
| 121 | + ASSERT_OK(ret, "ret"); |
| 122 | + ASSERT_OK(opts.retval, "retval"); |
| 123 | + |
| 124 | + ASSERT_LT(bpf_prog_stream_read(0, BPF_STREAM_STDOUT, buf, sizeof(buf), &ropts), 0, "error"); |
| 125 | + ret = -errno; |
| 126 | + ASSERT_EQ(ret, -EINVAL, "bad prog_fd"); |
| 127 | + |
| 128 | + ASSERT_LT(bpf_prog_stream_read(prog_fd, 0, buf, sizeof(buf), &ropts), 0, "error"); |
| 129 | + ret = -errno; |
| 130 | + ASSERT_EQ(ret, -ENOENT, "bad stream id"); |
| 131 | + |
| 132 | + ASSERT_LT(bpf_prog_stream_read(prog_fd, BPF_STREAM_STDOUT, NULL, sizeof(buf), NULL), 0, "error"); |
| 133 | + ret = -errno; |
| 134 | + ASSERT_EQ(ret, -EFAULT, "bad stream buf"); |
| 135 | + |
| 136 | + ret = bpf_prog_stream_read(prog_fd, BPF_STREAM_STDOUT, buf, 2, NULL); |
| 137 | + ASSERT_EQ(ret, 2, "bytes"); |
| 138 | + ret = bpf_prog_stream_read(prog_fd, BPF_STREAM_STDOUT, buf, 2, NULL); |
| 139 | + ASSERT_EQ(ret, 2, "bytes"); |
| 140 | + ret = bpf_prog_stream_read(prog_fd, BPF_STREAM_STDOUT, buf, 1, &ropts); |
| 141 | + ASSERT_EQ(ret, 0, "no bytes stdout"); |
| 142 | + ret = bpf_prog_stream_read(prog_fd, BPF_STREAM_STDERR, buf, 1, &ropts); |
| 143 | + ASSERT_EQ(ret, 0, "no bytes stderr"); |
| 144 | + |
| 145 | + stream__destroy(skel); |
| 146 | +} |
0 commit comments