llvm.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #include <stdio.h>
  2. #include <bpf/libbpf.h>
  3. #include <util/llvm-utils.h>
  4. #include <util/cache.h>
  5. #include "llvm.h"
  6. #include "tests.h"
  7. #include "debug.h"
  8. static int perf_config_cb(const char *var, const char *val,
  9. void *arg __maybe_unused)
  10. {
  11. return perf_default_config(var, val, arg);
  12. }
  13. #ifdef HAVE_LIBBPF_SUPPORT
  14. static int test__bpf_parsing(void *obj_buf, size_t obj_buf_sz)
  15. {
  16. struct bpf_object *obj;
  17. obj = bpf_object__open_buffer(obj_buf, obj_buf_sz, NULL);
  18. if (IS_ERR(obj))
  19. return TEST_FAIL;
  20. bpf_object__close(obj);
  21. return TEST_OK;
  22. }
  23. #else
  24. static int test__bpf_parsing(void *obj_buf __maybe_unused,
  25. size_t obj_buf_sz __maybe_unused)
  26. {
  27. pr_debug("Skip bpf parsing\n");
  28. return TEST_OK;
  29. }
  30. #endif
  31. static struct {
  32. const char *source;
  33. const char *desc;
  34. } bpf_source_table[__LLVM_TESTCASE_MAX] = {
  35. [LLVM_TESTCASE_BASE] = {
  36. .source = test_llvm__bpf_base_prog,
  37. .desc = "Basic BPF llvm compiling test",
  38. },
  39. [LLVM_TESTCASE_KBUILD] = {
  40. .source = test_llvm__bpf_test_kbuild_prog,
  41. .desc = "Test kbuild searching",
  42. },
  43. };
  44. int
  45. test_llvm__fetch_bpf_obj(void **p_obj_buf,
  46. size_t *p_obj_buf_sz,
  47. enum test_llvm__testcase idx,
  48. bool force)
  49. {
  50. const char *source;
  51. const char *desc;
  52. const char *tmpl_old, *clang_opt_old;
  53. char *tmpl_new = NULL, *clang_opt_new = NULL;
  54. int err, old_verbose, ret = TEST_FAIL;
  55. if (idx >= __LLVM_TESTCASE_MAX)
  56. return TEST_FAIL;
  57. source = bpf_source_table[idx].source;
  58. desc = bpf_source_table[idx].desc;
  59. perf_config(perf_config_cb, NULL);
  60. /*
  61. * Skip this test if user's .perfconfig doesn't set [llvm] section
  62. * and clang is not found in $PATH, and this is not perf test -v
  63. */
  64. if (!force && (verbose == 0 &&
  65. !llvm_param.user_set_param &&
  66. llvm__search_clang())) {
  67. pr_debug("No clang and no verbosive, skip this test\n");
  68. return TEST_SKIP;
  69. }
  70. /*
  71. * llvm is verbosity when error. Suppress all error output if
  72. * not 'perf test -v'.
  73. */
  74. old_verbose = verbose;
  75. if (verbose == 0)
  76. verbose = -1;
  77. *p_obj_buf = NULL;
  78. *p_obj_buf_sz = 0;
  79. if (!llvm_param.clang_bpf_cmd_template)
  80. goto out;
  81. if (!llvm_param.clang_opt)
  82. llvm_param.clang_opt = strdup("");
  83. err = asprintf(&tmpl_new, "echo '%s' | %s%s", source,
  84. llvm_param.clang_bpf_cmd_template,
  85. old_verbose ? "" : " 2>/dev/null");
  86. if (err < 0)
  87. goto out;
  88. err = asprintf(&clang_opt_new, "-xc %s", llvm_param.clang_opt);
  89. if (err < 0)
  90. goto out;
  91. tmpl_old = llvm_param.clang_bpf_cmd_template;
  92. llvm_param.clang_bpf_cmd_template = tmpl_new;
  93. clang_opt_old = llvm_param.clang_opt;
  94. llvm_param.clang_opt = clang_opt_new;
  95. err = llvm__compile_bpf("-", p_obj_buf, p_obj_buf_sz);
  96. llvm_param.clang_bpf_cmd_template = tmpl_old;
  97. llvm_param.clang_opt = clang_opt_old;
  98. verbose = old_verbose;
  99. if (err)
  100. goto out;
  101. ret = TEST_OK;
  102. out:
  103. free(tmpl_new);
  104. free(clang_opt_new);
  105. if (ret != TEST_OK)
  106. pr_debug("Failed to compile test case: '%s'\n", desc);
  107. return ret;
  108. }
  109. int test__llvm(void)
  110. {
  111. enum test_llvm__testcase i;
  112. for (i = 0; i < __LLVM_TESTCASE_MAX; i++) {
  113. int ret;
  114. void *obj_buf = NULL;
  115. size_t obj_buf_sz = 0;
  116. ret = test_llvm__fetch_bpf_obj(&obj_buf, &obj_buf_sz,
  117. i, false);
  118. if (ret == TEST_OK) {
  119. ret = test__bpf_parsing(obj_buf, obj_buf_sz);
  120. if (ret != TEST_OK)
  121. pr_debug("Failed to parse test case '%s'\n",
  122. bpf_source_table[i].desc);
  123. }
  124. free(obj_buf);
  125. switch (ret) {
  126. case TEST_SKIP:
  127. return TEST_SKIP;
  128. case TEST_OK:
  129. break;
  130. default:
  131. /*
  132. * Test 0 is the basic LLVM test. If test 0
  133. * fail, the basic LLVM support not functional
  134. * so the whole test should fail. If other test
  135. * case fail, it can be fixed by adjusting
  136. * config so don't report error.
  137. */
  138. if (i == 0)
  139. return TEST_FAIL;
  140. else
  141. return TEST_SKIP;
  142. }
  143. }
  144. return TEST_OK;
  145. }