Re: [RFC bpf-next v1 5/8] selftests/bpf: no need to track next_match_pos in struct test_loader

Andrii Nakryiko
2024-07-02T00:42:10Z

On Sat, Jun 29, 2024 at 2:48 AM Eduard Zingerman <eddyz87@gmail.com> wrote:
>
> The call stack for validate_case() function looks as follows:
> - test_loader__run_subtests()
> - process_subtest()
> - run_subtest()
> - prepare_case(), which does 'tester->next_match_pos = 0';
> - validate_case(), which increments tester->next_match_pos.
>
> Hence, each subtest is run with next_match_pos freshly set to zero.
> Meaning that there is no need to persist this variable in the
> struct test_loader, use local variable instead.
>
> Signed-off-by: Eduard Zingerman <eddyz87@gmail.com>
> ---
> tools/testing/selftests/bpf/test_loader.c | 17 ++++++++---------
> tools/testing/selftests/bpf/test_progs.h | 1 -
> 2 files changed, 8 insertions(+), 10 deletions(-)
>

Nice cleanup:

Acked-by: Andrii Nakryiko <andrii@kernel.org>

> diff --git a/tools/testing/selftests/bpf/test_loader.c b/tools/testing/selftests/bpf/test_loader.c
> index f14e10b0de96..ac9d3e81abdb 100644
> --- a/tools/testing/selftests/bpf/test_loader.c
> +++ b/tools/testing/selftests/bpf/test_loader.c
> @@ -434,7 +434,6 @@ static void prepare_case(struct test_loader *tester,
> bpf_program__set_flags(prog, prog_flags | spec->prog_flags);
>
> tester->log_buf[0] = '\0';
> - tester->next_match_pos = 0;
> }
>
> static void emit_verifier_log(const char *log_buf, bool force)
> @@ -450,23 +449,23 @@ static void validate_case(struct test_loader *tester,
> struct bpf_program *prog,
> int load_err)
> {
> - int i, j, err;
> - char *match;
> regmatch_t reg_match[1];
> + const char *match;
> + const char *log = tester->log_buf;
> + int i, j, err;
>
> for (i = 0; i < subspec->expect_msg_cnt; i++) {
> struct expect_msg *msg = &subspec->expect_msgs[i];
>
> if (msg->substr) {
> - match = strstr(tester->log_buf + tester->next_match_pos, msg->substr);
> + match = strstr(log, msg->substr);
> if (match)
> - tester->next_match_pos = match - tester->log_buf + strlen(msg->substr);
> + log += strlen(msg->substr);
> } else {
> - err = regexec(&msg->regex,
> - tester->log_buf + tester->next_match_pos, 1, reg_match, 0);
> + err = regexec(&msg->regex, log, 1, reg_match, 0);
> if (err == 0) {
> - match = tester->log_buf + tester->next_match_pos + reg_match[0].rm_so;
> - tester->next_match_pos += reg_match[0].rm_eo;
> + match = log + reg_match[0].rm_so;
> + log += reg_match[0].rm_eo;

invert and simplify:

log += reg_match[0].rm_eo;
match = log;

?

> } else {
> match = NULL;
> }

how about we move this to the beginning of iteration (before `if
(msg->substr)`) and so we'll assume the match is NULL on regexec
failing?

> diff --git a/tools/testing/selftests/bpf/test_progs.h b/tools/testing/selftests/bpf/test_progs.h
> index 0ba5a20b19ba..8e997de596db 100644
> --- a/tools/testing/selftests/bpf/test_progs.h
> +++ b/tools/testing/selftests/bpf/test_progs.h
> @@ -438,7 +438,6 @@ typedef int (*pre_execution_cb)(struct bpf_object *obj);
> struct test_loader {
> char *log_buf;
> size_t log_buf_sz;
> - size_t next_match_pos;
> pre_execution_cb pre_execution_cb;
>
> struct bpf_object *obj;
> --
> 2.45.2
>