^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <bpf/libbpf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <util/llvm-utils.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include "llvm.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include "tests.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include "debug.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #ifdef HAVE_LIBBPF_SUPPORT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) static int test__bpf_parsing(void *obj_buf, size_t obj_buf_sz)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) struct bpf_object *obj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) obj = bpf_object__open_buffer(obj_buf, obj_buf_sz, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) if (libbpf_get_error(obj))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) return TEST_FAIL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) bpf_object__close(obj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) return TEST_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) static int test__bpf_parsing(void *obj_buf __maybe_unused,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) size_t obj_buf_sz __maybe_unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) pr_debug("Skip bpf parsing\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) return TEST_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) static struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) const char *source;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) const char *desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) bool should_load_fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) } bpf_source_table[__LLVM_TESTCASE_MAX] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) [LLVM_TESTCASE_BASE] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) .source = test_llvm__bpf_base_prog,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) .desc = "Basic BPF llvm compile",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) [LLVM_TESTCASE_KBUILD] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) .source = test_llvm__bpf_test_kbuild_prog,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) .desc = "kbuild searching",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) [LLVM_TESTCASE_BPF_PROLOGUE] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) .source = test_llvm__bpf_test_prologue_prog,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) .desc = "Compile source for BPF prologue generation",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) [LLVM_TESTCASE_BPF_RELOCATION] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) .source = test_llvm__bpf_test_relocation,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) .desc = "Compile source for BPF relocation",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) .should_load_fail = true,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) test_llvm__fetch_bpf_obj(void **p_obj_buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) size_t *p_obj_buf_sz,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) enum test_llvm__testcase idx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) bool force,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) bool *should_load_fail)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) const char *source;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) const char *desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) const char *tmpl_old, *clang_opt_old;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) char *tmpl_new = NULL, *clang_opt_new = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) int err, old_verbose, ret = TEST_FAIL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) if (idx >= __LLVM_TESTCASE_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) return TEST_FAIL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) source = bpf_source_table[idx].source;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) desc = bpf_source_table[idx].desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) if (should_load_fail)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) *should_load_fail = bpf_source_table[idx].should_load_fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) * Skip this test if user's .perfconfig doesn't set [llvm] section
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) * and clang is not found in $PATH, and this is not perf test -v
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) if (!force && (verbose <= 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) !llvm_param.user_set_param &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) llvm__search_clang())) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) pr_debug("No clang and no verbosive, skip this test\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) return TEST_SKIP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) * llvm is verbosity when error. Suppress all error output if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) * not 'perf test -v'.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) old_verbose = verbose;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) if (verbose == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) verbose = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) *p_obj_buf = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) *p_obj_buf_sz = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) if (!llvm_param.clang_bpf_cmd_template)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) if (!llvm_param.clang_opt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) llvm_param.clang_opt = strdup("");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) err = asprintf(&tmpl_new, "echo '%s' | %s%s", source,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) llvm_param.clang_bpf_cmd_template,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) old_verbose ? "" : " 2>/dev/null");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) err = asprintf(&clang_opt_new, "-xc %s", llvm_param.clang_opt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) tmpl_old = llvm_param.clang_bpf_cmd_template;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) llvm_param.clang_bpf_cmd_template = tmpl_new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) clang_opt_old = llvm_param.clang_opt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) llvm_param.clang_opt = clang_opt_new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) err = llvm__compile_bpf("-", p_obj_buf, p_obj_buf_sz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) llvm_param.clang_bpf_cmd_template = tmpl_old;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) llvm_param.clang_opt = clang_opt_old;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) verbose = old_verbose;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) ret = TEST_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) free(tmpl_new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) free(clang_opt_new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) if (ret != TEST_OK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) pr_debug("Failed to compile test case: '%s'\n", desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) int test__llvm(struct test *test __maybe_unused, int subtest)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) void *obj_buf = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) size_t obj_buf_sz = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) bool should_load_fail = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) if ((subtest < 0) || (subtest >= __LLVM_TESTCASE_MAX))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) return TEST_FAIL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) ret = test_llvm__fetch_bpf_obj(&obj_buf, &obj_buf_sz,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) subtest, false, &should_load_fail);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) if (ret == TEST_OK && !should_load_fail) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) ret = test__bpf_parsing(obj_buf, obj_buf_sz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) if (ret != TEST_OK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) pr_debug("Failed to parse test case '%s'\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) bpf_source_table[subtest].desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) free(obj_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) int test__llvm_subtest_get_nr(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) return __LLVM_TESTCASE_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) const char *test__llvm_subtest_get_desc(int subtest)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) if ((subtest < 0) || (subtest >= __LLVM_TESTCASE_MAX))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) return bpf_source_table[subtest].desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) }