kallsyms-split.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/compiler.h>
  3. #include <fcntl.h>
  4. #include <signal.h>
  5. #include <unistd.h>
  6. #include <sys/stat.h>
  7. #include "util/dso.h"
  8. #include "util/map.h"
  9. #include "util/symbol.h"
  10. #include "util/debug.h"
  11. #include "util/machine.h"
  12. #include "tests.h"
  13. /*
  14. * This test is to check whether a bad symbol in a module won't split kallsyms maps.
  15. * The main_symbol[1-3] should belong to the main [kernel.kallsyms] map even if the
  16. * bad_symbol from the module is found in the middle.
  17. */
  18. static char root_template[] = "/tmp/perf-test.XXXXXX";
  19. static char *root_dir;
  20. static const char proc_version[] = "Linux version X.Y.Z (just for perf test)\n";
  21. static const char proc_modules[] = "module 4096 1 - Live 0xffffffffcd000000\n";
  22. static const char proc_kallsyms[] =
  23. "ffffffffab200000 T _stext\n"
  24. "ffffffffab200010 T good_symbol\n"
  25. "ffffffffab200020 t bad_symbol\n"
  26. "ffffffffab200030 t main_symbol1\n"
  27. "ffffffffab200040 t main_symbol2\n"
  28. "ffffffffab200050 t main_symbol3\n"
  29. "ffffffffab200060 T _etext\n"
  30. "ffffffffcd000000 T start_module\t[module]\n"
  31. "ffffffffab200020 u bad_symbol\t[module]\n"
  32. "ffffffffcd000040 T end_module\t[module]\n";
  33. static struct {
  34. const char *name;
  35. const char *contents;
  36. long len;
  37. } proc_files[] = {
  38. { "version", proc_version, sizeof(proc_version) - 1 },
  39. { "modules", proc_modules, sizeof(proc_modules) - 1 },
  40. { "kallsyms", proc_kallsyms, sizeof(proc_kallsyms) - 1 },
  41. };
  42. static void remove_proc_dir(int sig __maybe_unused)
  43. {
  44. char buf[128];
  45. if (root_dir == NULL)
  46. return;
  47. for (unsigned i = 0; i < ARRAY_SIZE(proc_files); i++) {
  48. scnprintf(buf, sizeof(buf), "%s/proc/%s", root_dir, proc_files[i].name);
  49. remove(buf);
  50. }
  51. scnprintf(buf, sizeof(buf), "%s/proc", root_dir);
  52. rmdir(buf);
  53. rmdir(root_dir);
  54. root_dir = NULL;
  55. }
  56. static int create_proc_dir(void)
  57. {
  58. char buf[128];
  59. root_dir = mkdtemp(root_template);
  60. if (root_dir == NULL)
  61. return -1;
  62. scnprintf(buf, sizeof(buf), "%s/proc", root_dir);
  63. if (mkdir(buf, 0700) < 0)
  64. goto err;
  65. for (unsigned i = 0; i < ARRAY_SIZE(proc_files); i++) {
  66. int fd, len;
  67. scnprintf(buf, sizeof(buf), "%s/proc/%s", root_dir, proc_files[i].name);
  68. fd = open(buf, O_RDWR | O_CREAT, 0600);
  69. if (fd < 0)
  70. goto err;
  71. len = write(fd, proc_files[i].contents, proc_files[i].len);
  72. close(fd);
  73. if (len != proc_files[i].len)
  74. goto err;
  75. }
  76. return 0;
  77. err:
  78. remove_proc_dir(0);
  79. return -1;
  80. }
  81. static int test__kallsyms_split(struct test_suite *test __maybe_unused,
  82. int subtest __maybe_unused)
  83. {
  84. struct machine m;
  85. struct map *map = NULL;
  86. int ret = TEST_FAIL;
  87. pr_debug("try to create fake root directory\n");
  88. if (create_proc_dir() < 0) {
  89. pr_debug("SKIP: cannot create a fake root directory\n");
  90. return TEST_SKIP;
  91. }
  92. signal(SIGINT, remove_proc_dir);
  93. signal(SIGPIPE, remove_proc_dir);
  94. signal(SIGSEGV, remove_proc_dir);
  95. signal(SIGTERM, remove_proc_dir);
  96. pr_debug("create kernel maps from the fake root directory\n");
  97. machine__init(&m, root_dir, HOST_KERNEL_ID);
  98. if (machine__create_kernel_maps(&m) < 0) {
  99. pr_debug("FAIL: failed to create kernel maps\n");
  100. goto out;
  101. }
  102. /* force to use /proc/kallsyms */
  103. symbol_conf.ignore_vmlinux = true;
  104. symbol_conf.ignore_vmlinux_buildid = true;
  105. symbol_conf.allow_aliases = true;
  106. if (map__load(machine__kernel_map(&m)) < 0) {
  107. pr_debug("FAIL: failed to load kallsyms\n");
  108. goto out;
  109. }
  110. pr_debug("kernel map loaded - check symbol and map\n");
  111. if (maps__nr_maps(machine__kernel_maps(&m)) != 2) {
  112. pr_debug("FAIL: it should have the kernel and a module, but has %u maps\n",
  113. maps__nr_maps(machine__kernel_maps(&m)));
  114. goto out;
  115. }
  116. if (machine__find_kernel_symbol_by_name(&m, "main_symbol3", &map) == NULL) {
  117. pr_debug("FAIL: failed to find a symbol\n");
  118. goto out;
  119. }
  120. if (!RC_CHK_EQUAL(map, machine__kernel_map(&m))) {
  121. pr_debug("FAIL: the symbol is not in the kernel map\n");
  122. goto out;
  123. }
  124. ret = TEST_OK;
  125. out:
  126. map__put(map);
  127. remove_proc_dir(0);
  128. machine__exit(&m);
  129. return ret;
  130. }
  131. DEFINE_SUITE("split kallsyms", kallsyms_split);