1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/compiler.h>
3 #include <linux/rbtree.h>
4 #include <inttypes.h>
5 #include <string.h>
6 #include <ctype.h>
7 #include <stdlib.h>
8 #include "dso.h"
9 #include "map.h"
10 #include "symbol.h"
11 #include <internal/lib.h> // page_size
12 #include "tests.h"
13 #include "debug.h"
14 #include "machine.h"
15 
16 #define UM(x) map__unmap_ip(kallsyms_map, (x))
17 
is_ignored_symbol(const char * name,char type)18 static bool is_ignored_symbol(const char *name, char type)
19 {
20 	/* Symbol names that exactly match to the following are ignored.*/
21 	static const char * const ignored_symbols[] = {
22 		/*
23 		 * Symbols which vary between passes. Passes 1 and 2 must have
24 		 * identical symbol lists. The kallsyms_* symbols below are
25 		 * only added after pass 1, they would be included in pass 2
26 		 * when --all-symbols is specified so exclude them to get a
27 		 * stable symbol list.
28 		 */
29 		"kallsyms_addresses",
30 		"kallsyms_offsets",
31 		"kallsyms_relative_base",
32 		"kallsyms_num_syms",
33 		"kallsyms_names",
34 		"kallsyms_markers",
35 		"kallsyms_token_table",
36 		"kallsyms_token_index",
37 		/* Exclude linker generated symbols which vary between passes */
38 		"_SDA_BASE_",		/* ppc */
39 		"_SDA2_BASE_",		/* ppc */
40 		NULL
41 	};
42 
43 	/* Symbol names that begin with the following are ignored.*/
44 	static const char * const ignored_prefixes[] = {
45 		"$",			/* local symbols for ARM, MIPS, etc. */
46 		".L",			/* local labels, .LBB,.Ltmpxxx,.L__unnamed_xx,.LASANPC, etc. */
47 		"__crc_",		/* modversions */
48 		"__efistub_",		/* arm64 EFI stub namespace */
49 		"__kvm_nvhe_$",		/* arm64 local symbols in non-VHE KVM namespace */
50 		"__kvm_nvhe_.L",	/* arm64 local symbols in non-VHE KVM namespace */
51 		"__AArch64ADRPThunk_",	/* arm64 lld */
52 		"__ARMV5PILongThunk_",	/* arm lld */
53 		"__ARMV7PILongThunk_",
54 		"__ThumbV7PILongThunk_",
55 		"__LA25Thunk_",		/* mips lld */
56 		"__microLA25Thunk_",
57 		NULL
58 	};
59 
60 	/* Symbol names that end with the following are ignored.*/
61 	static const char * const ignored_suffixes[] = {
62 		"_from_arm",		/* arm */
63 		"_from_thumb",		/* arm */
64 		"_veneer",		/* arm */
65 		NULL
66 	};
67 
68 	/* Symbol names that contain the following are ignored.*/
69 	static const char * const ignored_matches[] = {
70 		".long_branch.",	/* ppc stub */
71 		".plt_branch.",		/* ppc stub */
72 		NULL
73 	};
74 
75 	const char * const *p;
76 
77 	for (p = ignored_symbols; *p; p++)
78 		if (!strcmp(name, *p))
79 			return true;
80 
81 	for (p = ignored_prefixes; *p; p++)
82 		if (!strncmp(name, *p, strlen(*p)))
83 			return true;
84 
85 	for (p = ignored_suffixes; *p; p++) {
86 		int l = strlen(name) - strlen(*p);
87 
88 		if (l >= 0 && !strcmp(name + l, *p))
89 			return true;
90 	}
91 
92 	for (p = ignored_matches; *p; p++) {
93 		if (strstr(name, *p))
94 			return true;
95 	}
96 
97 	if (type == 'U' || type == 'u')
98 		return true;
99 	/* exclude debugging symbols */
100 	if (type == 'N' || type == 'n')
101 		return true;
102 
103 	if (toupper(type) == 'A') {
104 		/* Keep these useful absolute symbols */
105 		if (strcmp(name, "__kernel_syscall_via_break") &&
106 		    strcmp(name, "__kernel_syscall_via_epc") &&
107 		    strcmp(name, "__kernel_sigtramp") &&
108 		    strcmp(name, "__gp"))
109 			return true;
110 	}
111 
112 	return false;
113 }
114 
test__vmlinux_matches_kallsyms(struct test_suite * test __maybe_unused,int subtest __maybe_unused)115 static int test__vmlinux_matches_kallsyms(struct test_suite *test __maybe_unused,
116 					int subtest __maybe_unused)
117 {
118 	int err = TEST_FAIL;
119 	struct rb_node *nd;
120 	struct symbol *sym;
121 	struct map *kallsyms_map, *vmlinux_map;
122 	struct map_rb_node *rb_node;
123 	struct machine kallsyms, vmlinux;
124 	struct maps *maps;
125 	u64 mem_start, mem_end;
126 	bool header_printed;
127 
128 	/*
129 	 * Step 1:
130 	 *
131 	 * Init the machines that will hold kernel, modules obtained from
132 	 * both vmlinux + .ko files and from /proc/kallsyms split by modules.
133 	 */
134 	machine__init(&kallsyms, "", HOST_KERNEL_ID);
135 	machine__init(&vmlinux, "", HOST_KERNEL_ID);
136 
137 	maps = machine__kernel_maps(&vmlinux);
138 
139 	/*
140 	 * Step 2:
141 	 *
142 	 * Create the kernel maps for kallsyms and the DSO where we will then
143 	 * load /proc/kallsyms. Also create the modules maps from /proc/modules
144 	 * and find the .ko files that match them in /lib/modules/`uname -r`/.
145 	 */
146 	if (machine__create_kernel_maps(&kallsyms) < 0) {
147 		pr_debug("machine__create_kernel_maps failed");
148 		err = TEST_SKIP;
149 		goto out;
150 	}
151 
152 	/*
153 	 * Step 3:
154 	 *
155 	 * Load and split /proc/kallsyms into multiple maps, one per module.
156 	 * Do not use kcore, as this test was designed before kcore support
157 	 * and has parts that only make sense if using the non-kcore code.
158 	 * XXX: extend it to stress the kcorre code as well, hint: the list
159 	 * of modules extracted from /proc/kcore, in its current form, can't
160 	 * be compacted against the list of modules found in the "vmlinux"
161 	 * code and with the one got from /proc/modules from the "kallsyms" code.
162 	 */
163 	if (machine__load_kallsyms(&kallsyms, "/proc/kallsyms") <= 0) {
164 		pr_debug("machine__load_kallsyms failed");
165 		err = TEST_SKIP;
166 		goto out;
167 	}
168 
169 	/*
170 	 * Step 4:
171 	 *
172 	 * kallsyms will be internally on demand sorted by name so that we can
173 	 * find the reference relocation * symbol, i.e. the symbol we will use
174 	 * to see if the running kernel was relocated by checking if it has the
175 	 * same value in the vmlinux file we load.
176 	 */
177 	kallsyms_map = machine__kernel_map(&kallsyms);
178 
179 	/*
180 	 * Step 5:
181 	 *
182 	 * Now repeat step 2, this time for the vmlinux file we'll auto-locate.
183 	 */
184 	if (machine__create_kernel_maps(&vmlinux) < 0) {
185 		pr_info("machine__create_kernel_maps failed");
186 		goto out;
187 	}
188 
189 	vmlinux_map = machine__kernel_map(&vmlinux);
190 
191 	/*
192 	 * Step 6:
193 	 *
194 	 * Locate a vmlinux file in the vmlinux path that has a buildid that
195 	 * matches the one of the running kernel.
196 	 *
197 	 * While doing that look if we find the ref reloc symbol, if we find it
198 	 * we'll have its ref_reloc_symbol.unrelocated_addr and then
199 	 * maps__reloc_vmlinux will notice and set proper ->[un]map_ip routines
200 	 * to fixup the symbols.
201 	 */
202 	if (machine__load_vmlinux_path(&vmlinux) <= 0) {
203 		pr_info("Couldn't find a vmlinux that matches the kernel running on this machine, skipping test\n");
204 		err = TEST_SKIP;
205 		goto out;
206 	}
207 
208 	err = 0;
209 	/*
210 	 * Step 7:
211 	 *
212 	 * Now look at the symbols in the vmlinux DSO and check if we find all of them
213 	 * in the kallsyms dso. For the ones that are in both, check its names and
214 	 * end addresses too.
215 	 */
216 	map__for_each_symbol(vmlinux_map, sym, nd) {
217 		struct symbol *pair, *first_pair;
218 
219 		sym  = rb_entry(nd, struct symbol, rb_node);
220 
221 		if (sym->start == sym->end)
222 			continue;
223 
224 		mem_start = map__unmap_ip(vmlinux_map, sym->start);
225 		mem_end = map__unmap_ip(vmlinux_map, sym->end);
226 
227 		first_pair = machine__find_kernel_symbol(&kallsyms, mem_start, NULL);
228 		pair = first_pair;
229 
230 		if (pair && UM(pair->start) == mem_start) {
231 next_pair:
232 			if (arch__compare_symbol_names(sym->name, pair->name) == 0) {
233 				/*
234 				 * kallsyms don't have the symbol end, so we
235 				 * set that by using the next symbol start - 1,
236 				 * in some cases we get this up to a page
237 				 * wrong, trace_kmalloc when I was developing
238 				 * this code was one such example, 2106 bytes
239 				 * off the real size. More than that and we
240 				 * _really_ have a problem.
241 				 */
242 				s64 skew = mem_end - UM(pair->end);
243 				if (llabs(skew) >= page_size)
244 					pr_debug("WARN: %#" PRIx64 ": diff end addr for %s v: %#" PRIx64 " k: %#" PRIx64 "\n",
245 						 mem_start, sym->name, mem_end,
246 						 UM(pair->end));
247 
248 				/*
249 				 * Do not count this as a failure, because we
250 				 * could really find a case where it's not
251 				 * possible to get proper function end from
252 				 * kallsyms.
253 				 */
254 				continue;
255 			} else {
256 				pair = machine__find_kernel_symbol_by_name(&kallsyms, sym->name, NULL);
257 				if (pair) {
258 					if (UM(pair->start) == mem_start)
259 						goto next_pair;
260 
261 					pr_debug("WARN: %#" PRIx64 ": diff name v: %s k: %s\n",
262 						 mem_start, sym->name, pair->name);
263 				} else {
264 					pr_debug("WARN: %#" PRIx64 ": diff name v: %s k: %s\n",
265 						 mem_start, sym->name, first_pair->name);
266 				}
267 
268 				continue;
269 			}
270 		} else if (mem_start == map__end(kallsyms.vmlinux_map)) {
271 			/*
272 			 * Ignore aliases to _etext, i.e. to the end of the kernel text area,
273 			 * such as __indirect_thunk_end.
274 			 */
275 			continue;
276 		} else if (is_ignored_symbol(sym->name, sym->type)) {
277 			/*
278 			 * Ignore hidden symbols, see scripts/kallsyms.c for the details
279 			 */
280 			continue;
281 		} else {
282 			pr_debug("ERR : %#" PRIx64 ": %s not on kallsyms\n",
283 				 mem_start, sym->name);
284 		}
285 
286 		err = -1;
287 	}
288 
289 	if (verbose <= 0)
290 		goto out;
291 
292 	header_printed = false;
293 
294 	maps__for_each_entry(maps, rb_node) {
295 		struct map *map = rb_node->map;
296 		struct dso *dso = map__dso(map);
297 		/*
298 		 * If it is the kernel, kallsyms is always "[kernel.kallsyms]", while
299 		 * the kernel will have the path for the vmlinux file being used,
300 		 * so use the short name, less descriptive but the same ("[kernel]" in
301 		 * both cases.
302 		 */
303 		struct map *pair = maps__find_by_name(kallsyms.kmaps, (dso->kernel ?
304 								dso->short_name :
305 								dso->name));
306 		if (pair) {
307 			map__set_priv(pair, 1);
308 		} else {
309 			if (!header_printed) {
310 				pr_info("WARN: Maps only in vmlinux:\n");
311 				header_printed = true;
312 			}
313 			map__fprintf(map, stderr);
314 		}
315 	}
316 
317 	header_printed = false;
318 
319 	maps__for_each_entry(maps, rb_node) {
320 		struct map *pair, *map = rb_node->map;
321 
322 		mem_start = map__unmap_ip(vmlinux_map, map__start(map));
323 		mem_end = map__unmap_ip(vmlinux_map, map__end(map));
324 
325 		pair = maps__find(kallsyms.kmaps, mem_start);
326 		if (pair == NULL || map__priv(pair))
327 			continue;
328 
329 		if (map__start(pair) == mem_start) {
330 			struct dso *dso = map__dso(map);
331 
332 			if (!header_printed) {
333 				pr_info("WARN: Maps in vmlinux with a different name in kallsyms:\n");
334 				header_printed = true;
335 			}
336 
337 			pr_info("WARN: %" PRIx64 "-%" PRIx64 " %" PRIx64 " %s in kallsyms as",
338 				map__start(map), map__end(map), map__pgoff(map), dso->name);
339 			if (mem_end != map__end(pair))
340 				pr_info(":\nWARN: *%" PRIx64 "-%" PRIx64 " %" PRIx64,
341 					map__start(pair), map__end(pair), map__pgoff(pair));
342 			pr_info(" %s\n", dso->name);
343 			map__set_priv(pair, 1);
344 		}
345 	}
346 
347 	header_printed = false;
348 
349 	maps = machine__kernel_maps(&kallsyms);
350 
351 	maps__for_each_entry(maps, rb_node) {
352 		struct map *map = rb_node->map;
353 
354 		if (!map__priv(map)) {
355 			if (!header_printed) {
356 				pr_info("WARN: Maps only in kallsyms:\n");
357 				header_printed = true;
358 			}
359 			map__fprintf(map, stderr);
360 		}
361 	}
362 out:
363 	machine__exit(&kallsyms);
364 	machine__exit(&vmlinux);
365 	return err;
366 }
367 
368 DEFINE_SUITE("vmlinux symtab matches kallsyms", vmlinux_matches_kallsyms);
369