blob: fc07fb01123a377e95f3951d56c3389fb0023edf [file] [log] [blame]
Mike Frysingerf1ba7ad2022-09-12 05:42:57 -04001# Copyright 2013 The ChromiumOS Authors
Mike Frysinger69cb41d2013-08-11 20:08:19 -04002# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
Don Garrett25f309a2014-03-19 14:02:12 -07005"""Test cros_generate_breakpad_symbols."""
6
Mike Frysinger69cb41d2013-08-11 20:08:19 -04007import ctypes
Mike Frysinger374ba4f2019-11-14 23:45:15 -05008import io
Ian Barkley-Yeungc5b6f582023-03-08 18:23:07 -08009import logging
10import multiprocessing
Mike Frysinger69cb41d2013-08-11 20:08:19 -040011import os
Ian Barkley-Yeungc5b6f582023-03-08 18:23:07 -080012import pathlib
Mike Frysinger166fea02021-02-12 05:30:33 -050013from unittest import mock
Mike Frysinger69cb41d2013-08-11 20:08:19 -040014
Mike Frysinger69cb41d2013-08-11 20:08:19 -040015from chromite.lib import cros_test_lib
16from chromite.lib import osutils
Mike Frysinger69cb41d2013-08-11 20:08:19 -040017from chromite.lib import parallel_unittest
18from chromite.lib import partial_mock
19from chromite.scripts import cros_generate_breakpad_symbols
20
Mike Frysinger69cb41d2013-08-11 20:08:19 -040021
22class FindDebugDirMock(partial_mock.PartialMock):
Alex Klein1699fab2022-09-08 08:46:06 -060023 """Mock out the DebugDir helper so we can point it to a tempdir."""
Mike Frysinger69cb41d2013-08-11 20:08:19 -040024
Alex Klein1699fab2022-09-08 08:46:06 -060025 TARGET = "chromite.scripts.cros_generate_breakpad_symbols"
26 ATTRS = ("FindDebugDir",)
27 DEFAULT_ATTR = "FindDebugDir"
Mike Frysinger69cb41d2013-08-11 20:08:19 -040028
Alex Klein1699fab2022-09-08 08:46:06 -060029 def __init__(self, path, *args, **kwargs):
30 self.path = path
31 super().__init__(*args, **kwargs)
Mike Frysinger69cb41d2013-08-11 20:08:19 -040032
Alex Klein1699fab2022-09-08 08:46:06 -060033 # pylint: disable=unused-argument
34 def FindDebugDir(self, _board, sysroot=None):
35 return self.path
Mike Frysinger69cb41d2013-08-11 20:08:19 -040036
37
Ian Barkley-Yeung5b485132023-07-25 16:54:31 -070038class IsSharedLibraryTest(cros_test_lib.TestCase):
39 """Test IsSharedLibrary"""
40
41 def testSharedLibaries(self):
42 """Verify that shared libraries return truthy"""
43 shared_libraries = [
44 "lib/libcontainer.so",
45 "lib64/libnss_db.so.2",
46 "usr/lib/libboost_type_erasure.so.1.81.0",
47 "usr/lib/v4l1compat.so",
48 ]
49
50 for shared_library in shared_libraries:
51 self.assertTrue(
52 cros_generate_breakpad_symbols.IsSharedLibrary(shared_library),
53 msg=f"expected {shared_library} to be a shared library",
54 )
55
56 def testExecutables(self):
57 """Verify that executables return None"""
58 executables = [
59 "sbin/crash_reporter",
60 "usr/bin/pqso", # ends in so but not .so
61 "usr/bin/perl5.36.0", # ends in numbers but not .so
62 ]
63
64 for executable in executables:
65 self.assertFalse(
66 cros_generate_breakpad_symbols.IsSharedLibrary(executable),
67 msg=f"expected {executable} to not be a shared library",
68 )
69
70
Mike Frysinger3ef6d972019-08-24 20:07:42 -040071# This long decorator triggers a false positive in the docstring test.
72# https://github.com/PyCQA/pylint/issues/3077
73# pylint: disable=bad-docstring-quotes
Alex Klein1699fab2022-09-08 08:46:06 -060074@mock.patch(
75 "chromite.scripts.cros_generate_breakpad_symbols." "GenerateBreakpadSymbol"
76)
Mike Frysinger69cb41d2013-08-11 20:08:19 -040077class GenerateSymbolsTest(cros_test_lib.MockTempDirTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -060078 """Test GenerateBreakpadSymbols."""
Mike Frysinger69cb41d2013-08-11 20:08:19 -040079
Alex Klein1699fab2022-09-08 08:46:06 -060080 def setUp(self):
81 self.board = "monkey-board"
82 self.board_dir = os.path.join(self.tempdir, "build", self.board)
83 self.debug_dir = os.path.join(self.board_dir, "usr", "lib", "debug")
84 self.breakpad_dir = os.path.join(self.debug_dir, "breakpad")
Mike Frysinger69cb41d2013-08-11 20:08:19 -040085
Alex Klein1699fab2022-09-08 08:46:06 -060086 # Generate a tree of files which we'll scan through.
87 elf_files = [
88 "bin/elf",
89 "iii/large-elf",
90 # Need some kernel modules (with & without matching .debug).
91 "lib/modules/3.10/module.ko",
92 "lib/modules/3.10/module-no-debug.ko",
93 # Need a file which has an ELF only, but not a .debug.
94 "usr/bin/elf-only",
95 "usr/sbin/elf",
96 ]
97 debug_files = [
98 "bin/bad-file",
99 "bin/elf.debug",
100 "iii/large-elf.debug",
101 "boot/vmlinux.debug",
102 "lib/modules/3.10/module.ko.debug",
103 # Need a file which has a .debug only, but not an ELF.
104 "sbin/debug-only.debug",
105 "usr/sbin/elf.debug",
106 ]
107 for f in [os.path.join(self.board_dir, x) for x in elf_files] + [
108 os.path.join(self.debug_dir, x) for x in debug_files
109 ]:
110 osutils.Touch(f, makedirs=True)
Mike Frysinger69cb41d2013-08-11 20:08:19 -0400111
Alex Klein1699fab2022-09-08 08:46:06 -0600112 # Set up random build dirs and symlinks.
113 buildid = os.path.join(self.debug_dir, ".build-id", "00")
114 osutils.SafeMakedirs(buildid)
115 os.symlink("/asdf", os.path.join(buildid, "foo"))
116 os.symlink("/bin/sh", os.path.join(buildid, "foo.debug"))
117 os.symlink("/bin/sh", os.path.join(self.debug_dir, "file.debug"))
118 osutils.WriteFile(
119 os.path.join(self.debug_dir, "iii", "large-elf.debug"),
120 "just some content",
121 )
Mike Frysinger69cb41d2013-08-11 20:08:19 -0400122
Alex Klein1699fab2022-09-08 08:46:06 -0600123 self.StartPatcher(FindDebugDirMock(self.debug_dir))
Mike Frysinger69cb41d2013-08-11 20:08:19 -0400124
Alex Klein1699fab2022-09-08 08:46:06 -0600125 def testNormal(self, gen_mock):
126 """Verify all the files we expect to get generated do"""
127 with parallel_unittest.ParallelMock():
128 ret = cros_generate_breakpad_symbols.GenerateBreakpadSymbols(
129 self.board, sysroot=self.board_dir
130 )
131 self.assertEqual(ret, 0)
132 self.assertEqual(gen_mock.call_count, 5)
Mike Frysinger69cb41d2013-08-11 20:08:19 -0400133
Alex Klein1699fab2022-09-08 08:46:06 -0600134 # The largest ELF should be processed first.
135 call1 = (
136 os.path.join(self.board_dir, "iii/large-elf"),
137 os.path.join(self.debug_dir, "iii/large-elf.debug"),
138 )
139 self.assertEqual(gen_mock.call_args_list[0][0], call1)
Mike Frysinger69cb41d2013-08-11 20:08:19 -0400140
Alex Klein1699fab2022-09-08 08:46:06 -0600141 # The other ELFs can be called in any order.
142 call2 = (
143 os.path.join(self.board_dir, "bin/elf"),
144 os.path.join(self.debug_dir, "bin/elf.debug"),
145 )
146 call3 = (
147 os.path.join(self.board_dir, "usr/sbin/elf"),
148 os.path.join(self.debug_dir, "usr/sbin/elf.debug"),
149 )
150 call4 = (
151 os.path.join(self.board_dir, "lib/modules/3.10/module.ko"),
152 os.path.join(
153 self.debug_dir, "lib/modules/3.10/module.ko.debug"
154 ),
155 )
156 call5 = (
157 os.path.join(self.board_dir, "boot/vmlinux"),
158 os.path.join(self.debug_dir, "boot/vmlinux.debug"),
159 )
160 exp_calls = set((call2, call3, call4, call5))
161 actual_calls = set(
162 (
163 gen_mock.call_args_list[1][0],
164 gen_mock.call_args_list[2][0],
165 gen_mock.call_args_list[3][0],
166 gen_mock.call_args_list[4][0],
167 )
168 )
169 self.assertEqual(exp_calls, actual_calls)
Mike Frysinger69cb41d2013-08-11 20:08:19 -0400170
Alex Klein1699fab2022-09-08 08:46:06 -0600171 def testFileList(self, gen_mock):
172 """Verify that file_list restricts the symbols generated"""
173 with parallel_unittest.ParallelMock():
174 call1 = (
175 os.path.join(self.board_dir, "usr/sbin/elf"),
176 os.path.join(self.debug_dir, "usr/sbin/elf.debug"),
177 )
Prathmesh Prabhu9995e9b2013-10-31 16:43:55 -0700178
Alex Klein1699fab2022-09-08 08:46:06 -0600179 # Filter with elf path.
180 ret = cros_generate_breakpad_symbols.GenerateBreakpadSymbols(
181 self.board,
182 sysroot=self.board_dir,
183 breakpad_dir=self.breakpad_dir,
184 file_list=[os.path.join(self.board_dir, "usr", "sbin", "elf")],
185 )
186 self.assertEqual(ret, 0)
187 self.assertEqual(gen_mock.call_count, 1)
188 self.assertEqual(gen_mock.call_args_list[0][0], call1)
Prathmesh Prabhu9995e9b2013-10-31 16:43:55 -0700189
Alex Klein1699fab2022-09-08 08:46:06 -0600190 # Filter with debug symbols file path.
191 gen_mock.reset_mock()
192 ret = cros_generate_breakpad_symbols.GenerateBreakpadSymbols(
193 self.board,
194 sysroot=self.board_dir,
195 breakpad_dir=self.breakpad_dir,
196 file_list=[
197 os.path.join(self.debug_dir, "usr", "sbin", "elf.debug")
198 ],
199 )
200 self.assertEqual(ret, 0)
201 self.assertEqual(gen_mock.call_count, 1)
202 self.assertEqual(gen_mock.call_args_list[0][0], call1)
Prathmesh Prabhu9995e9b2013-10-31 16:43:55 -0700203
Alex Klein1699fab2022-09-08 08:46:06 -0600204 def testGenLimit(self, gen_mock):
205 """Verify generate_count arg works"""
206 with parallel_unittest.ParallelMock():
207 # Generate nothing!
208 ret = cros_generate_breakpad_symbols.GenerateBreakpadSymbols(
209 self.board,
210 sysroot=self.board_dir,
211 breakpad_dir=self.breakpad_dir,
212 generate_count=0,
213 )
214 self.assertEqual(ret, 0)
215 self.assertEqual(gen_mock.call_count, 0)
Prathmesh Prabhu9995e9b2013-10-31 16:43:55 -0700216
Alex Klein1699fab2022-09-08 08:46:06 -0600217 # Generate just one.
218 ret = cros_generate_breakpad_symbols.GenerateBreakpadSymbols(
219 self.board,
220 sysroot=self.board_dir,
221 breakpad_dir=self.breakpad_dir,
222 generate_count=1,
223 )
224 self.assertEqual(ret, 0)
225 self.assertEqual(gen_mock.call_count, 1)
Mike Frysinger69cb41d2013-08-11 20:08:19 -0400226
Alex Klein1699fab2022-09-08 08:46:06 -0600227 # The largest ELF should be processed first.
228 call1 = (
229 os.path.join(self.board_dir, "iii/large-elf"),
230 os.path.join(self.debug_dir, "iii/large-elf.debug"),
231 )
232 self.assertEqual(gen_mock.call_args_list[0][0], call1)
Mike Frysinger69cb41d2013-08-11 20:08:19 -0400233
Alex Klein1699fab2022-09-08 08:46:06 -0600234 def testGenErrors(self, gen_mock):
235 """Verify we handle errors from generation correctly"""
Mike Frysinger69cb41d2013-08-11 20:08:19 -0400236
Alex Klein1699fab2022-09-08 08:46:06 -0600237 def _SetError(*_args, **kwargs):
238 kwargs["num_errors"].value += 1
239 return 1
Mike Frysinger69cb41d2013-08-11 20:08:19 -0400240
Alex Klein1699fab2022-09-08 08:46:06 -0600241 gen_mock.side_effect = _SetError
242 with parallel_unittest.ParallelMock():
243 ret = cros_generate_breakpad_symbols.GenerateBreakpadSymbols(
244 self.board, sysroot=self.board_dir
245 )
246 self.assertEqual(ret, 5)
247 self.assertEqual(gen_mock.call_count, 5)
Mike Frysinger9a628bb2013-10-24 15:51:37 -0400248
Alex Klein1699fab2022-09-08 08:46:06 -0600249 def testCleaningTrue(self, gen_mock):
250 """Verify behavior of clean_breakpad=True"""
251 with parallel_unittest.ParallelMock():
252 # Dir does not exist, and then does.
253 self.assertNotExists(self.breakpad_dir)
254 ret = cros_generate_breakpad_symbols.GenerateBreakpadSymbols(
255 self.board,
256 sysroot=self.board_dir,
257 generate_count=1,
258 clean_breakpad=True,
259 )
260 self.assertEqual(ret, 0)
261 self.assertEqual(gen_mock.call_count, 1)
262 self.assertExists(self.breakpad_dir)
Mike Frysinger9a628bb2013-10-24 15:51:37 -0400263
Alex Klein1699fab2022-09-08 08:46:06 -0600264 # Dir exists before & after.
265 # File exists, but then doesn't.
266 stub_file = os.path.join(self.breakpad_dir, "fooooooooo")
267 osutils.Touch(stub_file)
268 ret = cros_generate_breakpad_symbols.GenerateBreakpadSymbols(
269 self.board,
270 sysroot=self.board_dir,
271 generate_count=1,
272 clean_breakpad=True,
273 )
274 self.assertEqual(ret, 0)
275 self.assertEqual(gen_mock.call_count, 2)
276 self.assertNotExists(stub_file)
Mike Frysinger9a628bb2013-10-24 15:51:37 -0400277
Alex Klein1699fab2022-09-08 08:46:06 -0600278 def testCleaningFalse(self, gen_mock):
279 """Verify behavior of clean_breakpad=False"""
280 with parallel_unittest.ParallelMock():
281 # Dir does not exist, and then does.
282 self.assertNotExists(self.breakpad_dir)
283 ret = cros_generate_breakpad_symbols.GenerateBreakpadSymbols(
284 self.board,
285 sysroot=self.board_dir,
286 generate_count=1,
287 clean_breakpad=False,
288 )
289 self.assertEqual(ret, 0)
290 self.assertEqual(gen_mock.call_count, 1)
291 self.assertExists(self.breakpad_dir)
Mike Frysinger9a628bb2013-10-24 15:51:37 -0400292
Alex Klein1699fab2022-09-08 08:46:06 -0600293 # Dir exists before & after.
294 # File exists before & after.
295 stub_file = os.path.join(self.breakpad_dir, "fooooooooo")
296 osutils.Touch(stub_file)
297 ret = cros_generate_breakpad_symbols.GenerateBreakpadSymbols(
298 self.board,
299 sysroot=self.board_dir,
300 generate_count=1,
301 clean_breakpad=False,
302 )
303 self.assertEqual(ret, 0)
304 self.assertEqual(gen_mock.call_count, 2)
305 self.assertExists(stub_file)
306
307 def testExclusionList(self, gen_mock):
308 """Verify files in directories of the exclusion list are excluded"""
309 exclude_dirs = ["bin", "usr", "fake/dir/fake"]
310 with parallel_unittest.ParallelMock():
311 ret = cros_generate_breakpad_symbols.GenerateBreakpadSymbols(
312 self.board, sysroot=self.board_dir, exclude_dirs=exclude_dirs
313 )
314 self.assertEqual(ret, 0)
315 self.assertEqual(gen_mock.call_count, 3)
316
Ian Barkley-Yeungb5274442023-04-28 16:32:20 -0700317 def testExpectedFilesCompleteFailure(self, _):
318 """Verify if no files are processed, all expected files give errors"""
319 with parallel_unittest.ParallelMock() and self.assertLogs(
320 level=logging.WARNING
321 ) as cm:
322 ret = cros_generate_breakpad_symbols.GenerateBreakpadSymbols(
323 self.board, sysroot=self.board_dir
324 )
325 self.assertEqual(ret, 0)
326 self.assertIn(
327 "Not all expected files were processed successfully",
328 "\n".join(cm.output),
329 )
330 for output in cm.output:
331 if (
332 "Not all expected files were processed successfully"
333 in output
334 ):
335 # This is the line that lists all the files we didn't find.
336 for (
337 expected_file
338 ) in cros_generate_breakpad_symbols.ExpectedFiles:
339 self.assertIn(expected_file.name, output)
340
341 def testExpectedFilesPartialFailure(self, gen_mock):
342 """If some expected files are processed, the others give errors"""
343 expected_found = (
344 cros_generate_breakpad_symbols.ExpectedFiles.LIBC,
345 cros_generate_breakpad_symbols.ExpectedFiles.CRASH_REPORTER,
346 )
347
348 def _SetFound(*_args, **kwargs):
349 kwargs["found_files"].extend(expected_found)
350 gen_mock.side_effect = None
351 return 1
352
353 gen_mock.side_effect = _SetFound
354 with parallel_unittest.ParallelMock() and self.assertLogs(
355 level=logging.WARNING
356 ) as cm:
357 ret = cros_generate_breakpad_symbols.GenerateBreakpadSymbols(
358 self.board, sysroot=self.board_dir
359 )
360 self.assertEqual(ret, 0)
361 self.assertIn(
362 "Not all expected files were processed successfully",
363 "\n".join(cm.output),
364 )
365 for output in cm.output:
366 if (
367 "Not all expected files were processed successfully"
368 in output
369 ):
370 # This is the line that lists all the files we didn't find.
371 for (
372 expected_file
373 ) in cros_generate_breakpad_symbols.ExpectedFiles:
374 if expected_file in expected_found:
375 self.assertNotIn(expected_file.name, output)
376 else:
377 self.assertIn(expected_file.name, output)
378
379 def testExpectedFilesWithSomeIgnored(self, _):
380 """If some expected files are ignored, they don't give errors"""
381 ignore_expected_files = [
382 cros_generate_breakpad_symbols.ExpectedFiles.ASH_CHROME,
383 cros_generate_breakpad_symbols.ExpectedFiles.LIBC,
384 ]
385 with parallel_unittest.ParallelMock() and self.assertLogs(
386 level=logging.WARNING
387 ) as cm:
388 ret = cros_generate_breakpad_symbols.GenerateBreakpadSymbols(
389 self.board,
390 sysroot=self.board_dir,
391 ignore_expected_files=ignore_expected_files,
392 )
393 self.assertEqual(ret, 0)
394 self.assertIn(
395 "Not all expected files were processed successfully",
396 "\n".join(cm.output),
397 )
398 for output in cm.output:
399 if (
400 "Not all expected files were processed successfully"
401 in output
402 ):
403 # This is the line that lists all the files we didn't find.
404 for (
405 expected_file
406 ) in cros_generate_breakpad_symbols.ExpectedFiles:
407 if expected_file in ignore_expected_files:
408 self.assertNotIn(expected_file.name, output)
409 else:
410 self.assertIn(expected_file.name, output)
411
412 def testExpectedFilesWithAllIgnored(self, _):
413 """If all expected files are ignored, there is no error"""
414 with parallel_unittest.ParallelMock() and self.assertLogs(
415 level=logging.WARNING
416 ) as cm:
417 ret = cros_generate_breakpad_symbols.GenerateBreakpadSymbols(
418 self.board,
419 sysroot=self.board_dir,
420 ignore_expected_files=list(
421 cros_generate_breakpad_symbols.ExpectedFiles
422 ),
423 )
424 self.assertEqual(ret, 0)
425 self.assertNotIn(
426 "Not all expected files were processed successfully",
427 "\n".join(cm.output),
428 )
429
430 def testExpectedFilesWithSomeIgnoredAndSomeFound(self, gen_mock):
431 """Some expected files are ignored, others processed => no error"""
432 expected_found = (
433 cros_generate_breakpad_symbols.ExpectedFiles.LIBC,
434 cros_generate_breakpad_symbols.ExpectedFiles.CRASH_REPORTER,
435 )
436
437 def _SetFound(*_args, **kwargs):
438 kwargs["found_files"].extend(expected_found)
439 gen_mock.side_effect = None
440 return 1
441
442 gen_mock.side_effect = _SetFound
443 with parallel_unittest.ParallelMock() and self.assertLogs(
444 level=logging.WARNING
445 ) as cm:
446 ret = cros_generate_breakpad_symbols.GenerateBreakpadSymbols(
447 self.board,
448 sysroot=self.board_dir,
449 ignore_expected_files=[
450 cros_generate_breakpad_symbols.ExpectedFiles.ASH_CHROME,
451 cros_generate_breakpad_symbols.ExpectedFiles.LIBMETRICS,
452 ],
453 )
454 self.assertEqual(ret, 0)
455 self.assertNotIn(
456 "Not all expected files were processed successfully",
457 "\n".join(cm.output),
458 )
459
Mike Frysinger69cb41d2013-08-11 20:08:19 -0400460
Benjamin Gordon121a2aa2018-05-04 16:24:45 -0600461class GenerateSymbolTest(cros_test_lib.RunCommandTempDirTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -0600462 """Test GenerateBreakpadSymbol."""
Mike Frysinger69cb41d2013-08-11 20:08:19 -0400463
Peter Boström763baca2023-05-25 16:36:33 +0000464 _DUMP_SYMS_BASE_CMD = ["dump_syms", "-v", "-d", "-m"]
465
Alex Klein1699fab2022-09-08 08:46:06 -0600466 def setUp(self):
467 self.elf_file = os.path.join(self.tempdir, "elf")
468 osutils.Touch(self.elf_file)
469 self.debug_dir = os.path.join(self.tempdir, "debug")
470 self.debug_file = os.path.join(self.debug_dir, "elf.debug")
471 osutils.Touch(self.debug_file, makedirs=True)
472 # Not needed as the code itself should create it as needed.
473 self.breakpad_dir = os.path.join(self.debug_dir, "breakpad")
Mike Frysinger69cb41d2013-08-11 20:08:19 -0400474
Ian Barkley-Yeungfdaaf2e2023-03-02 17:30:39 -0800475 self.FILE_OUT = (
476 f"{self.elf_file}: ELF 64-bit LSB pie executable, x86-64, "
477 "version 1 (SYSV), dynamically linked, interpreter "
478 "/lib64/ld-linux-x86-64.so.2, for GNU/Linux 3.2.0, "
479 "BuildID[sha1]=cf9a21fa6b14bfb2dfcb76effd713c4536014d95, stripped"
480 )
Ian Barkley-Yeungc5b6f582023-03-08 18:23:07 -0800481 # A symbol file which would pass validation.
482 MINIMAL_SYMBOL_FILE = (
483 "MODULE OS CPU ID NAME\n"
484 "PUBLIC f10 0 func\n"
485 "STACK CFI INIT f10 22 .cfa: $rsp 8 + .ra: .cfa -8 + ^\n"
486 )
487 self.rc.SetDefaultCmdResult(stdout=MINIMAL_SYMBOL_FILE)
Ian Barkley-Yeungfdaaf2e2023-03-02 17:30:39 -0800488 self.rc.AddCmdResult(
489 ["/usr/bin/file", self.elf_file], stdout=self.FILE_OUT
490 )
Alex Klein1699fab2022-09-08 08:46:06 -0600491 self.assertCommandContains = self.rc.assertCommandContains
492 self.sym_file = os.path.join(self.breakpad_dir, "NAME/ID/NAME.sym")
Mike Frysinger69cb41d2013-08-11 20:08:19 -0400493
Alex Klein1699fab2022-09-08 08:46:06 -0600494 self.StartPatcher(FindDebugDirMock(self.debug_dir))
Mike Frysinger69cb41d2013-08-11 20:08:19 -0400495
Alex Klein1699fab2022-09-08 08:46:06 -0600496 def assertCommandArgs(self, i, args):
497 """Helper for looking at the args of the |i|th call"""
498 self.assertEqual(self.rc.call_args_list[i][0][0], args)
Mike Frysinger69cb41d2013-08-11 20:08:19 -0400499
Alex Klein1699fab2022-09-08 08:46:06 -0600500 def testNormal(self):
501 """Normal run -- given an ELF and a debug file"""
502 ret = cros_generate_breakpad_symbols.GenerateBreakpadSymbol(
Ian Barkley-Yeungfdaaf2e2023-03-02 17:30:39 -0800503 self.elf_file,
504 self.debug_file,
505 self.breakpad_dir,
Alex Klein1699fab2022-09-08 08:46:06 -0600506 )
507 self.assertEqual(ret, self.sym_file)
Ian Barkley-Yeungfdaaf2e2023-03-02 17:30:39 -0800508 self.assertEqual(self.rc.call_count, 2)
Alex Klein1699fab2022-09-08 08:46:06 -0600509 self.assertCommandArgs(
Peter Boström763baca2023-05-25 16:36:33 +0000510 1, self._DUMP_SYMS_BASE_CMD + [self.elf_file, self.debug_dir]
Alex Klein1699fab2022-09-08 08:46:06 -0600511 )
512 self.assertExists(self.sym_file)
Mike Frysinger69cb41d2013-08-11 20:08:19 -0400513
Alex Klein1699fab2022-09-08 08:46:06 -0600514 def testNormalNoCfi(self):
515 """Normal run w/out CFI"""
516 # Make sure the num_errors flag works too.
Trent Apted1e2e4f32023-05-05 03:50:20 +0000517 num_errors = ctypes.c_int(0)
Alex Klein1699fab2022-09-08 08:46:06 -0600518 ret = cros_generate_breakpad_symbols.GenerateBreakpadSymbol(
519 self.elf_file,
520 breakpad_dir=self.breakpad_dir,
521 strip_cfi=True,
522 num_errors=num_errors,
523 )
524 self.assertEqual(ret, self.sym_file)
525 self.assertEqual(num_errors.value, 0)
Peter Boström763baca2023-05-25 16:36:33 +0000526 self.assertCommandArgs(
527 1, self._DUMP_SYMS_BASE_CMD + ["-c", self.elf_file]
528 )
Ian Barkley-Yeungfdaaf2e2023-03-02 17:30:39 -0800529 self.assertEqual(self.rc.call_count, 2)
Alex Klein1699fab2022-09-08 08:46:06 -0600530 self.assertExists(self.sym_file)
Mike Frysinger69cb41d2013-08-11 20:08:19 -0400531
Alex Klein1699fab2022-09-08 08:46:06 -0600532 def testNormalElfOnly(self):
533 """Normal run -- given just an ELF"""
534 ret = cros_generate_breakpad_symbols.GenerateBreakpadSymbol(
535 self.elf_file, breakpad_dir=self.breakpad_dir
536 )
537 self.assertEqual(ret, self.sym_file)
Peter Boström763baca2023-05-25 16:36:33 +0000538 self.assertCommandArgs(1, self._DUMP_SYMS_BASE_CMD + [self.elf_file])
Ian Barkley-Yeungfdaaf2e2023-03-02 17:30:39 -0800539 self.assertEqual(self.rc.call_count, 2)
Alex Klein1699fab2022-09-08 08:46:06 -0600540 self.assertExists(self.sym_file)
Mike Frysinger69cb41d2013-08-11 20:08:19 -0400541
Alex Klein1699fab2022-09-08 08:46:06 -0600542 def testNormalSudo(self):
543 """Normal run where ELF is readable only by root"""
544 with mock.patch.object(os, "access") as mock_access:
545 mock_access.return_value = False
546 ret = cros_generate_breakpad_symbols.GenerateBreakpadSymbol(
547 self.elf_file, breakpad_dir=self.breakpad_dir
548 )
549 self.assertEqual(ret, self.sym_file)
550 self.assertCommandArgs(
Peter Boström763baca2023-05-25 16:36:33 +0000551 1, ["sudo", "--"] + self._DUMP_SYMS_BASE_CMD + [self.elf_file]
Alex Klein1699fab2022-09-08 08:46:06 -0600552 )
Mike Frysinger69cb41d2013-08-11 20:08:19 -0400553
Alex Klein1699fab2022-09-08 08:46:06 -0600554 def testLargeDebugFail(self):
555 """Running w/large .debug failed, but retry worked"""
556 self.rc.AddCmdResult(
Peter Boström763baca2023-05-25 16:36:33 +0000557 self._DUMP_SYMS_BASE_CMD + [self.elf_file, self.debug_dir],
558 returncode=1,
Alex Klein1699fab2022-09-08 08:46:06 -0600559 )
560 ret = cros_generate_breakpad_symbols.GenerateBreakpadSymbol(
561 self.elf_file, self.debug_file, self.breakpad_dir
562 )
563 self.assertEqual(ret, self.sym_file)
Ian Barkley-Yeungfdaaf2e2023-03-02 17:30:39 -0800564 self.assertEqual(self.rc.call_count, 4)
Alex Klein1699fab2022-09-08 08:46:06 -0600565 self.assertCommandArgs(
Peter Boström763baca2023-05-25 16:36:33 +0000566 1, self._DUMP_SYMS_BASE_CMD + [self.elf_file, self.debug_dir]
Ian Barkley-Yeungfdaaf2e2023-03-02 17:30:39 -0800567 )
568 # The current fallback from _DumpExpectingSymbols() to
Peter Boström763baca2023-05-25 16:36:33 +0000569 # _DumpAllowingBasicFallback() causes the first dump_syms command to get
Ian Barkley-Yeungfdaaf2e2023-03-02 17:30:39 -0800570 # repeated.
571 self.assertCommandArgs(
Peter Boström763baca2023-05-25 16:36:33 +0000572 2, self._DUMP_SYMS_BASE_CMD + [self.elf_file, self.debug_dir]
Alex Klein1699fab2022-09-08 08:46:06 -0600573 )
574 self.assertCommandArgs(
Peter Boström763baca2023-05-25 16:36:33 +0000575 3,
576 self._DUMP_SYMS_BASE_CMD
577 + ["-c", "-r", self.elf_file, self.debug_dir],
Alex Klein1699fab2022-09-08 08:46:06 -0600578 )
579 self.assertExists(self.sym_file)
Mike Frysinger69cb41d2013-08-11 20:08:19 -0400580
Ian Barkley-Yeungb5274442023-04-28 16:32:20 -0700581 def testForceBasicFallback(self):
582 """Running with force_basic_fallback
583
584 Test force_basic_fallback goes straight to _DumpAllowingBasicFallback().
585 """
586 self.rc.AddCmdResult(
Peter Boström763baca2023-05-25 16:36:33 +0000587 self._DUMP_SYMS_BASE_CMD + [self.elf_file, self.debug_dir],
588 returncode=1,
Ian Barkley-Yeungb5274442023-04-28 16:32:20 -0700589 )
590 ret = cros_generate_breakpad_symbols.GenerateBreakpadSymbol(
591 self.elf_file,
592 self.debug_file,
593 self.breakpad_dir,
594 force_basic_fallback=True,
595 )
596 self.assertEqual(ret, self.sym_file)
597 self.assertEqual(self.rc.call_count, 2)
598 # dump_syms -v should only happen once in _DumpAllowingBasicFallback()
599 # and not in _DumpExpectingSymbols(). We don't call /usr/bin/file in
600 # _ExpectGoodSymbols() either, so there's 2 fewer commands than
601 # in testLargeDebugFail.
602 self.assertCommandArgs(
Peter Boström763baca2023-05-25 16:36:33 +0000603 0, self._DUMP_SYMS_BASE_CMD + [self.elf_file, self.debug_dir]
Ian Barkley-Yeungb5274442023-04-28 16:32:20 -0700604 )
605 self.assertCommandArgs(
Peter Boström763baca2023-05-25 16:36:33 +0000606 1,
607 self._DUMP_SYMS_BASE_CMD
608 + ["-c", "-r", self.elf_file, self.debug_dir],
Ian Barkley-Yeungb5274442023-04-28 16:32:20 -0700609 )
610 self.assertExists(self.sym_file)
611
Alex Klein1699fab2022-09-08 08:46:06 -0600612 def testDebugFail(self):
613 """Running w/.debug always failed, but works w/out"""
614 self.rc.AddCmdResult(
Peter Boström763baca2023-05-25 16:36:33 +0000615 self._DUMP_SYMS_BASE_CMD + [self.elf_file, self.debug_dir],
616 returncode=1,
Alex Klein1699fab2022-09-08 08:46:06 -0600617 )
618 self.rc.AddCmdResult(
Peter Boström763baca2023-05-25 16:36:33 +0000619 self._DUMP_SYMS_BASE_CMD
620 + ["-c", "-r", self.elf_file, self.debug_dir],
Alex Klein1699fab2022-09-08 08:46:06 -0600621 returncode=1,
622 )
623 ret = cros_generate_breakpad_symbols.GenerateBreakpadSymbol(
624 self.elf_file, self.debug_file, self.breakpad_dir
625 )
626 self.assertEqual(ret, self.sym_file)
Ian Barkley-Yeungfdaaf2e2023-03-02 17:30:39 -0800627 self.assertEqual(self.rc.call_count, 5)
Alex Klein1699fab2022-09-08 08:46:06 -0600628 self.assertCommandArgs(
Peter Boström763baca2023-05-25 16:36:33 +0000629 1, self._DUMP_SYMS_BASE_CMD + [self.elf_file, self.debug_dir]
Ian Barkley-Yeungfdaaf2e2023-03-02 17:30:39 -0800630 )
631 # The current fallback from _DumpExpectingSymbols() to
Peter Boström763baca2023-05-25 16:36:33 +0000632 # _DumpAllowingBasicFallback() causes the first dump_syms command to get
Ian Barkley-Yeungfdaaf2e2023-03-02 17:30:39 -0800633 # repeated.
634 self.assertCommandArgs(
Peter Boström763baca2023-05-25 16:36:33 +0000635 2, self._DUMP_SYMS_BASE_CMD + [self.elf_file, self.debug_dir]
Alex Klein1699fab2022-09-08 08:46:06 -0600636 )
637 self.assertCommandArgs(
Peter Boström763baca2023-05-25 16:36:33 +0000638 3,
639 self._DUMP_SYMS_BASE_CMD
640 + ["-c", "-r", self.elf_file, self.debug_dir],
Alex Klein1699fab2022-09-08 08:46:06 -0600641 )
Peter Boström763baca2023-05-25 16:36:33 +0000642 self.assertCommandArgs(4, self._DUMP_SYMS_BASE_CMD + [self.elf_file])
Alex Klein1699fab2022-09-08 08:46:06 -0600643 self.assertExists(self.sym_file)
Mike Frysinger69cb41d2013-08-11 20:08:19 -0400644
Alex Klein1699fab2022-09-08 08:46:06 -0600645 def testCompleteFail(self):
646 """Running dump_syms always fails"""
647 self.rc.SetDefaultCmdResult(returncode=1)
648 ret = cros_generate_breakpad_symbols.GenerateBreakpadSymbol(
649 self.elf_file, breakpad_dir=self.breakpad_dir
650 )
651 self.assertEqual(ret, 1)
652 # Make sure the num_errors flag works too.
Trent Apted1e2e4f32023-05-05 03:50:20 +0000653 num_errors = ctypes.c_int(0)
Alex Klein1699fab2022-09-08 08:46:06 -0600654 ret = cros_generate_breakpad_symbols.GenerateBreakpadSymbol(
655 self.elf_file, breakpad_dir=self.breakpad_dir, num_errors=num_errors
656 )
657 self.assertEqual(ret, 1)
658 self.assertEqual(num_errors.value, 1)
Mike Frysinger69cb41d2013-08-11 20:08:19 -0400659
Ian Barkley-Yeungfdaaf2e2023-03-02 17:30:39 -0800660 def testKernelObjects(self):
661 """Kernel object files should call _DumpAllowingBasicFallback()"""
662 ko_file = os.path.join(self.tempdir, "elf.ko")
663 osutils.Touch(ko_file)
664 self.rc.AddCmdResult(
Peter Boström763baca2023-05-25 16:36:33 +0000665 self._DUMP_SYMS_BASE_CMD + [ko_file, self.debug_dir],
666 returncode=1,
Ian Barkley-Yeungfdaaf2e2023-03-02 17:30:39 -0800667 )
668 self.rc.AddCmdResult(
Peter Boström763baca2023-05-25 16:36:33 +0000669 self._DUMP_SYMS_BASE_CMD + ["-c", "-r", ko_file, self.debug_dir],
Ian Barkley-Yeungfdaaf2e2023-03-02 17:30:39 -0800670 returncode=1,
671 )
672 ret = cros_generate_breakpad_symbols.GenerateBreakpadSymbol(
673 ko_file, self.debug_file, self.breakpad_dir
674 )
675 self.assertEqual(ret, self.sym_file)
676 self.assertEqual(self.rc.call_count, 3)
677 # Only one call (at the beginning of _DumpAllowingBasicFallback())
678 # to "dump_syms -v"
Ian Barkley-Yeungfdaaf2e2023-03-02 17:30:39 -0800679 self.assertCommandArgs(
Peter Boström763baca2023-05-25 16:36:33 +0000680 0, self._DUMP_SYMS_BASE_CMD + [ko_file, self.debug_dir]
Ian Barkley-Yeungfdaaf2e2023-03-02 17:30:39 -0800681 )
Peter Boström763baca2023-05-25 16:36:33 +0000682 self.assertCommandArgs(
683 1,
684 self._DUMP_SYMS_BASE_CMD + ["-c", "-r", ko_file, self.debug_dir],
685 )
686 self.assertCommandArgs(2, self._DUMP_SYMS_BASE_CMD + [ko_file])
Ian Barkley-Yeungfdaaf2e2023-03-02 17:30:39 -0800687 self.assertExists(self.sym_file)
688
689 def testGoBinary(self):
690 """Go binaries should call _DumpAllowingBasicFallback()
691
692 Also tests that dump_syms failing with 'file contains no debugging
693 information' does not fail the script.
694 """
695 go_binary = os.path.join(self.tempdir, "goprogram")
696 osutils.Touch(go_binary)
697 go_debug_file = os.path.join(self.debug_dir, "goprogram.debug")
698 osutils.Touch(go_debug_file, makedirs=True)
699 FILE_OUT_GO = go_binary + (
700 ": ELF 64-bit LSB executable, x86-64, "
701 "version 1 (SYSV), statically linked, "
Ian Barkley-Yeungc5b6f582023-03-08 18:23:07 -0800702 "Go BuildID=KKXVlL66E8Qmngr4qll9/5kOKGZw9I7TmNhoqKLqq/SiYVJam6w5Fo"
703 "39B3BtDo/ba8_ceezZ-3R4qEv6_-K, not stripped"
Ian Barkley-Yeungfdaaf2e2023-03-02 17:30:39 -0800704 )
705 self.rc.AddCmdResult(["/usr/bin/file", go_binary], stdout=FILE_OUT_GO)
706 self.rc.AddCmdResult(
Peter Boström763baca2023-05-25 16:36:33 +0000707 self._DUMP_SYMS_BASE_CMD + [go_binary, self.debug_dir],
Ian Barkley-Yeungfdaaf2e2023-03-02 17:30:39 -0800708 returncode=1,
709 )
710 self.rc.AddCmdResult(
Peter Boström763baca2023-05-25 16:36:33 +0000711 self._DUMP_SYMS_BASE_CMD + ["-c", "-r", go_binary, self.debug_dir],
712 returncode=1,
713 )
714 self.rc.AddCmdResult(
715 self._DUMP_SYMS_BASE_CMD + [go_binary],
Ian Barkley-Yeungfdaaf2e2023-03-02 17:30:39 -0800716 returncode=1,
717 stderr=(
718 f"{go_binary}: file contains no debugging information "
719 '(no ".stab" or ".debug_info" sections)'
720 ),
721 )
Trent Apted1e2e4f32023-05-05 03:50:20 +0000722 num_errors = ctypes.c_int(0)
Ian Barkley-Yeungfdaaf2e2023-03-02 17:30:39 -0800723 ret = cros_generate_breakpad_symbols.GenerateBreakpadSymbol(
724 go_binary, go_debug_file, self.breakpad_dir
725 )
726 self.assertEqual(ret, 0)
727 self.assertEqual(self.rc.call_count, 4)
728 self.assertCommandArgs(0, ["/usr/bin/file", go_binary])
729 # Only one call (at the beginning of _DumpAllowingBasicFallback())
730 # to "dump_syms -v"
731 self.assertCommandArgs(
Peter Boström763baca2023-05-25 16:36:33 +0000732 1, self._DUMP_SYMS_BASE_CMD + [go_binary, self.debug_dir]
Ian Barkley-Yeungfdaaf2e2023-03-02 17:30:39 -0800733 )
734 self.assertCommandArgs(
Peter Boström763baca2023-05-25 16:36:33 +0000735 2,
736 self._DUMP_SYMS_BASE_CMD + ["-c", "-r", go_binary, self.debug_dir],
Ian Barkley-Yeungfdaaf2e2023-03-02 17:30:39 -0800737 )
Peter Boström763baca2023-05-25 16:36:33 +0000738 self.assertCommandArgs(3, self._DUMP_SYMS_BASE_CMD + [go_binary])
Ian Barkley-Yeungfdaaf2e2023-03-02 17:30:39 -0800739 self.assertNotExists(self.sym_file)
740 self.assertEqual(num_errors.value, 0)
741
Ian Barkley-Yeunge1785762023-03-08 18:32:18 -0800742 def _testBinaryIsInLocalFallback(self, directory, filename):
743 binary = os.path.join(self.tempdir, directory, filename)
Ian Barkley-Yeungfdaaf2e2023-03-02 17:30:39 -0800744 osutils.Touch(binary, makedirs=True)
Ian Barkley-Yeunge1785762023-03-08 18:32:18 -0800745 debug_dir = os.path.join(self.debug_dir, directory)
746 debug_file = os.path.join(debug_dir, f"{filename}.debug")
Ian Barkley-Yeungfdaaf2e2023-03-02 17:30:39 -0800747 osutils.Touch(debug_file, makedirs=True)
748 self.rc.AddCmdResult(["/usr/bin/file", binary], stdout=self.FILE_OUT)
749 self.rc.AddCmdResult(
Peter Boström763baca2023-05-25 16:36:33 +0000750 self._DUMP_SYMS_BASE_CMD + [binary, debug_dir], returncode=1
Ian Barkley-Yeungfdaaf2e2023-03-02 17:30:39 -0800751 )
752 self.rc.AddCmdResult(
Peter Boström763baca2023-05-25 16:36:33 +0000753 self._DUMP_SYMS_BASE_CMD + ["-c", "-r", binary, debug_dir],
Ian Barkley-Yeungfdaaf2e2023-03-02 17:30:39 -0800754 returncode=1,
755 )
756 self.rc.AddCmdResult(
Peter Boström763baca2023-05-25 16:36:33 +0000757 self._DUMP_SYMS_BASE_CMD + [binary],
Ian Barkley-Yeungfdaaf2e2023-03-02 17:30:39 -0800758 returncode=1,
759 stderr=(
760 f"{binary}: file contains no debugging information "
761 '(no ".stab" or ".debug_info" sections)'
762 ),
763 )
Trent Apted1e2e4f32023-05-05 03:50:20 +0000764 num_errors = ctypes.c_int(0)
Ian Barkley-Yeungfdaaf2e2023-03-02 17:30:39 -0800765 ret = cros_generate_breakpad_symbols.GenerateBreakpadSymbol(
766 binary, debug_file, self.breakpad_dir, sysroot=self.tempdir
767 )
768 self.assertEqual(ret, 0)
769 self.assertEqual(self.rc.call_count, 4)
770 self.assertCommandArgs(0, ["/usr/bin/file", binary])
771 # Only one call (at the beginning of _DumpAllowingBasicFallback())
772 # to "dump_syms -v"
Ian Barkley-Yeungfdaaf2e2023-03-02 17:30:39 -0800773 self.assertCommandArgs(
Peter Boström763baca2023-05-25 16:36:33 +0000774 1, self._DUMP_SYMS_BASE_CMD + [binary, debug_dir]
Ian Barkley-Yeungfdaaf2e2023-03-02 17:30:39 -0800775 )
Peter Boström763baca2023-05-25 16:36:33 +0000776 self.assertCommandArgs(
777 2, self._DUMP_SYMS_BASE_CMD + ["-c", "-r", binary, debug_dir]
778 )
779 self.assertCommandArgs(3, self._DUMP_SYMS_BASE_CMD + [binary])
Ian Barkley-Yeungfdaaf2e2023-03-02 17:30:39 -0800780 self.assertNotExists(self.sym_file)
781 self.assertEqual(num_errors.value, 0)
782
Ian Barkley-Yeunge1785762023-03-08 18:32:18 -0800783 def testAllowlist(self):
784 """Binaries in the allowlist should call _DumpAllowingBasicFallback()"""
785 self._testBinaryIsInLocalFallback("usr/bin", "goldctl")
786
787 def testUsrLocalSkip(self):
788 """Binaries in /usr/local should call _DumpAllowingBasicFallback()"""
789 self._testBinaryIsInLocalFallback("usr/local", "minidump_stackwalk")
790
Mike Frysinger69cb41d2013-08-11 20:08:19 -0400791
Ian Barkley-Yeungc5b6f582023-03-08 18:23:07 -0800792class ValidateSymbolFileTest(cros_test_lib.TempDirTestCase):
793 """Tests ValidateSymbolFile"""
794
795 def _GetTestdataFile(self, filename: str) -> str:
796 """Gets the path to a file in the testdata directory.
797
798 Args:
799 filename: The base filename of the file.
800
801 Returns:
802 A string with the complete path to the file.
803 """
804 return os.path.join(os.path.dirname(__file__), "testdata", filename)
805
806 def testValidSymbolFiles(self):
807 """Make sure ValidateSymbolFile passes on valid files"""
808
809 # All files are in the testdata/ subdirectory.
810 VALID_SYMBOL_FILES = [
811 # A "normal" symbol file from an executable.
812 "basic.sym",
813 # A "normal" symbol file from a shared library.
814 "basic_lib.sym",
815 # A symbol file with PUBLIC records but no FUNC records.
816 "public_only.sym",
817 # A symbol file with FUNC records but no PUBLIC records.
818 "func_only.sym",
819 # A symbol file with at least one of every line type.
820 "all_line_types.sym",
821 ]
822
823 for file in VALID_SYMBOL_FILES:
824 with self.subTest(
825 file=file
826 ), multiprocessing.Manager() as mp_manager:
827 found_files = mp_manager.list()
828 self.assertTrue(
829 cros_generate_breakpad_symbols.ValidateSymbolFile(
830 self._GetTestdataFile(file),
831 "/build/board/bin/foo",
832 "/build/board",
833 found_files,
834 )
835 )
836 self.assertFalse(found_files)
837
838 def testInvalidSymbolFiles(self):
839 """Make sure ValidateSymbolFile fails on invalid files.
840
841 This test only covers cases that return false, not cases that raise
842 exceptions.
843 """
844
845 class InvalidSymbolFile:
846 """The name of an invalid symbol file + the expected error msg."""
847
848 def __init__(self, filename, expected_errors):
849 self.filename = filename
850 self.expected_errors = expected_errors
851
852 INVALID_SYMBOL_FILES = [
853 InvalidSymbolFile(
854 "bad_no_func_or_public.sym",
855 [
856 "WARNING:root:/build/board/bin/foo: "
857 "Symbol file has no FUNC or PUBLIC records"
858 ],
859 ),
860 InvalidSymbolFile(
861 "bad_no_stack.sym",
862 [
863 "WARNING:root:/build/board/bin/foo: "
864 "Symbol file has no STACK records"
865 ],
866 ),
867 InvalidSymbolFile(
868 "bad_no_module.sym",
869 [
870 "WARNING:root:/build/board/bin/foo: "
871 "Symbol file has 0 MODULE lines"
872 ],
873 ),
874 InvalidSymbolFile(
875 "bad_two_modules.sym",
876 [
877 "WARNING:root:/build/board/bin/foo: "
878 "Symbol file has 2 MODULE lines"
879 ],
880 ),
881 InvalidSymbolFile(
882 "bad_func_no_line_numbers.sym",
883 [
884 "WARNING:root:/build/board/bin/foo: "
885 "Symbol file has FUNC records but no line numbers"
886 ],
887 ),
888 InvalidSymbolFile(
889 "bad_line_numbers_no_file.sym",
890 [
891 "WARNING:root:/build/board/bin/foo: "
892 "Symbol file has line number records but no FILE records"
893 ],
894 ),
895 InvalidSymbolFile(
896 "bad_inline_no_files.sym",
897 [
898 "WARNING:root:/build/board/bin/foo: "
899 "Symbol file has INLINE records but no FILE records"
900 ],
901 ),
902 InvalidSymbolFile(
903 "bad_inline_no_origins.sym",
904 [
905 "WARNING:root:/build/board/bin/foo: "
906 "Symbol file has INLINE records but no INLINE_ORIGIN "
907 "records"
908 ],
909 ),
910 InvalidSymbolFile(
911 "blank.sym",
912 [
913 "WARNING:root:/build/board/bin/foo: "
914 "Symbol file has no STACK records",
915 "WARNING:root:/build/board/bin/foo: "
916 "Symbol file has 0 MODULE lines",
917 "WARNING:root:/build/board/bin/foo: "
918 "Symbol file has no FUNC or PUBLIC records",
919 ],
920 ),
921 ]
922
923 for file in INVALID_SYMBOL_FILES:
924 with self.subTest(
925 file=file.filename
926 ), multiprocessing.Manager() as mp_manager:
927 found_files = mp_manager.list()
928 with self.assertLogs(level=logging.WARNING) as cm:
929 self.assertFalse(
930 cros_generate_breakpad_symbols.ValidateSymbolFile(
931 self._GetTestdataFile(file.filename),
932 "/build/board/bin/foo",
933 "/build/board",
934 found_files,
935 )
936 )
937 self.assertEqual(file.expected_errors, cm.output)
938 self.assertFalse(found_files)
939
940 def testInvalidSymbolFilesWhichRaise(self):
941 """Test ValidateSymbolFile raise exceptions on certain files"""
942
943 class InvalidSymbolFile:
944 """The invalid symbol file + the expected exception message"""
945
946 def __init__(self, filename, expected_exception_regex):
947 self.filename = filename
948 self.expected_exception_regex = expected_exception_regex
949
950 INVALID_SYMBOL_FILES = [
951 InvalidSymbolFile(
952 "bad_unknown_line_type.sym",
Ian Barkley-Yeungccfa1b52023-07-07 18:55:46 -0700953 r"symbol file has unknown line type UNKNOWN "
954 r"\(line='UNKNOWN line type\n'\)",
Ian Barkley-Yeungc5b6f582023-03-08 18:23:07 -0800955 ),
956 InvalidSymbolFile(
957 "bad_blank_line.sym",
958 "symbol file has unexpected blank line",
959 ),
960 InvalidSymbolFile(
961 "bad_short_func.sym",
962 r"symbol file has FUNC line with 2 words "
Ian Barkley-Yeungccfa1b52023-07-07 18:55:46 -0700963 r"\(expected 5 or more\) \(line='FUNC fb0\n'\)",
Ian Barkley-Yeungc5b6f582023-03-08 18:23:07 -0800964 ),
965 InvalidSymbolFile(
966 "bad_short_line_number.sym",
967 r"symbol file has line number line with 3 words "
Ian Barkley-Yeungccfa1b52023-07-07 18:55:46 -0700968 r"\(expected 4 - 4\) \(line='fb0 106 0\n'\)",
Ian Barkley-Yeungc5b6f582023-03-08 18:23:07 -0800969 ),
970 InvalidSymbolFile(
971 "bad_long_line_number.sym",
972 r"symbol file has line number line with 5 words "
Ian Barkley-Yeungccfa1b52023-07-07 18:55:46 -0700973 r"\(expected 4 - 4\) \(line='c184 7 59 4 8\n'\)",
Ian Barkley-Yeungc5b6f582023-03-08 18:23:07 -0800974 ),
975 ]
976
977 for file in INVALID_SYMBOL_FILES:
978 with self.subTest(
979 file=file.filename
980 ), multiprocessing.Manager() as mp_manager:
981 found_files = mp_manager.list()
982 self.assertRaisesRegex(
983 ValueError,
984 file.expected_exception_regex,
985 cros_generate_breakpad_symbols.ValidateSymbolFile,
986 self._GetTestdataFile(file.filename),
987 "/build/board/bin/foo",
988 "/build/board",
989 found_files,
990 )
991
992 def testAllowlist(self):
993 """Test that ELFs on the allowlist are allowed to pass."""
994 with multiprocessing.Manager() as mp_manager:
995 found_files = mp_manager.list()
996 self.assertTrue(
997 cros_generate_breakpad_symbols.ValidateSymbolFile(
Ian Barkley-Yeung5b485132023-07-25 16:54:31 -0700998 self._GetTestdataFile("bad_no_module.sym"),
Ian Barkley-Yeungc5b6f582023-03-08 18:23:07 -0800999 "/build/board/opt/google/chrome/nacl_helper_bootstrap",
1000 "/build/board",
1001 found_files,
1002 )
1003 )
1004 self.assertFalse(found_files)
1005
1006 def testAllowlistRegex(self):
1007 """Test that ELFs on the regex-based allowlist are allowed to pass."""
1008 with multiprocessing.Manager() as mp_manager:
1009 found_files = mp_manager.list()
1010 self.assertTrue(
1011 cros_generate_breakpad_symbols.ValidateSymbolFile(
Ian Barkley-Yeung5b485132023-07-25 16:54:31 -07001012 self._GetTestdataFile("bad_no_module.sym"),
1013 "/build/board/lib64/libnss_dns.so.2",
1014 "/build/board",
1015 found_files,
1016 )
1017 )
1018 self.assertFalse(found_files)
1019
1020 def testSharedLibrariesSkipStackTest(self):
1021 """Test that shared libraries can pass validation with no STACK."""
1022 with multiprocessing.Manager() as mp_manager:
1023 found_files = mp_manager.list()
1024 self.assertTrue(
1025 cros_generate_breakpad_symbols.ValidateSymbolFile(
Ian Barkley-Yeungc5b6f582023-03-08 18:23:07 -08001026 self._GetTestdataFile("bad_no_stack.sym"),
Ian Barkley-Yeung5b485132023-07-25 16:54:31 -07001027 "/build/board/lib64/libiw.so.30",
Ian Barkley-Yeungc5b6f582023-03-08 18:23:07 -08001028 "/build/board",
1029 found_files,
1030 )
1031 )
1032 self.assertFalse(found_files)
1033
1034 def _CreateSymbolFile(
1035 self,
1036 sym_file: pathlib.Path,
1037 func_lines: int = 0,
1038 public_lines: int = 0,
1039 stack_lines: int = 0,
1040 line_number_lines: int = 0,
1041 ) -> None:
1042 """Creates a symbol file.
1043
1044 Creates a symbol file with the given number of lines (and enough other
1045 lines to pass validation) in the temp directory.
1046
1047 To pass validation, chrome.sym files must be huge; create them
1048 programmatically during the test instead of checking in a real 800MB+
1049 chrome symbol file.
1050 """
1051 with sym_file.open(mode="w", encoding="utf-8") as f:
1052 f.write("MODULE OS CPU ID NAME\n")
1053 f.write("FILE 0 /path/to/source.cc\n")
1054 for func in range(0, func_lines):
1055 f.write(f"FUNC {func} 1 0 function{func}\n")
1056 for public in range(0, public_lines):
1057 f.write(f"PUBLIC {public} 0 Public{public}\n")
1058 for line in range(0, line_number_lines):
1059 f.write(f"{line} 1 {line} 0\n")
1060 for stack in range(0, stack_lines):
1061 f.write(f"STACK CFI {stack} .cfa: $esp {stack} +\n")
1062
1063 def testValidChromeSymbolFile(self):
1064 """Test that a chrome symbol file can pass the additional checks"""
1065 sym_file = self.tempdir / "chrome.sym"
1066 self._CreateSymbolFile(
1067 sym_file,
1068 func_lines=100000,
1069 public_lines=10,
1070 stack_lines=1000000,
1071 line_number_lines=1000000,
1072 )
1073 with multiprocessing.Manager() as mp_manager:
1074 found_files = mp_manager.list()
1075 self.assertTrue(
1076 cros_generate_breakpad_symbols.ValidateSymbolFile(
1077 str(sym_file),
1078 "/build/board/opt/google/chrome/chrome",
1079 "/build/board",
1080 found_files,
1081 )
1082 )
1083 self.assertEqual(
1084 list(found_files),
1085 [cros_generate_breakpad_symbols.ExpectedFiles.ASH_CHROME],
1086 )
1087
1088 def testInvalidChromeSymbolFile(self):
1089 """Test that a chrome symbol file is held to higher standards."""
1090
1091 class ChromeSymbolFileTest:
1092 """Defines the subtest for an invalid Chrome symbol file."""
1093
1094 def __init__(
1095 self,
1096 name,
1097 expected_error,
1098 func_lines=100000,
1099 stack_lines=1000000,
1100 line_number_lines=1000000,
1101 ):
1102 self.name = name
1103 self.expected_error = expected_error
1104 self.func_lines = func_lines
1105 self.stack_lines = stack_lines
1106 self.line_number_lines = line_number_lines
1107
1108 CHROME_SYMBOL_TESTS = [
1109 ChromeSymbolFileTest(
1110 name="Insufficient FUNC records",
1111 func_lines=10000,
1112 expected_error="chrome should have at least 100,000 FUNC "
1113 "records, found 10000",
1114 ),
1115 ChromeSymbolFileTest(
1116 name="Insufficient STACK records",
1117 stack_lines=100000,
1118 expected_error="chrome should have at least 1,000,000 STACK "
1119 "records, found 100000",
1120 ),
1121 ChromeSymbolFileTest(
1122 name="Insufficient line number records",
1123 line_number_lines=100000,
1124 expected_error="chrome should have at least 1,000,000 "
1125 "line number records, found 100000",
1126 ),
1127 ]
1128 for test in CHROME_SYMBOL_TESTS:
1129 with self.subTest(
1130 name=test.name
1131 ), multiprocessing.Manager() as mp_manager:
1132 sym_file = self.tempdir / "chrome.sym"
1133 self._CreateSymbolFile(
1134 sym_file,
1135 func_lines=test.func_lines,
1136 public_lines=10,
1137 stack_lines=test.stack_lines,
1138 line_number_lines=test.line_number_lines,
1139 )
1140 found_files = mp_manager.list()
1141 with self.assertLogs(level=logging.WARNING) as cm:
1142 self.assertFalse(
1143 cros_generate_breakpad_symbols.ValidateSymbolFile(
1144 str(sym_file),
1145 "/build/board/opt/google/chrome/chrome",
1146 "/build/board",
1147 found_files,
1148 )
1149 )
1150 self.assertIn(test.expected_error, cm.output[0])
1151 self.assertEqual(len(cm.output), 1)
1152
1153 def testValidLibcSymbolFile(self):
1154 """Test that a libc.so symbol file can pass the additional checks."""
1155 with multiprocessing.Manager() as mp_manager:
1156 sym_file = self.tempdir / "libc.so.sym"
1157 self._CreateSymbolFile(
1158 sym_file, public_lines=200, stack_lines=20000
1159 )
1160 found_files = mp_manager.list()
1161 self.assertTrue(
1162 cros_generate_breakpad_symbols.ValidateSymbolFile(
1163 str(sym_file),
1164 "/build/board/lib64/libc.so.6",
1165 "/build/board",
1166 found_files,
1167 )
1168 )
1169 self.assertEqual(
1170 list(found_files),
1171 [cros_generate_breakpad_symbols.ExpectedFiles.LIBC],
1172 )
1173
1174 def testInvalidLibcSymbolFile(self):
1175 """Test that a libc.so symbol file is held to higher standards."""
1176
1177 class LibcSymbolFileTest:
1178 """Defines the subtest for an invalid libc symbol file."""
1179
1180 def __init__(
1181 self,
1182 name,
1183 expected_error,
1184 public_lines=200,
1185 stack_lines=20000,
1186 ):
1187 self.name = name
1188 self.expected_error = expected_error
1189 self.public_lines = public_lines
1190 self.stack_lines = stack_lines
1191
1192 LIBC_SYMBOL_TESTS = [
1193 LibcSymbolFileTest(
1194 name="Insufficient PUBLIC records",
1195 public_lines=50,
1196 expected_error="/build/board/lib64/libc.so.6 should have at "
1197 "least 100 PUBLIC records, found 50",
1198 ),
1199 LibcSymbolFileTest(
1200 name="Insufficient STACK records",
1201 stack_lines=1000,
1202 expected_error="/build/board/lib64/libc.so.6 should have at "
1203 "least 10000 STACK records, found 1000",
1204 ),
1205 ]
1206 for test in LIBC_SYMBOL_TESTS:
1207 with self.subTest(
1208 name=test.name
1209 ), multiprocessing.Manager() as mp_manager:
1210 sym_file = self.tempdir / "libc.so.sym"
1211 self._CreateSymbolFile(
1212 sym_file,
1213 public_lines=test.public_lines,
1214 stack_lines=test.stack_lines,
1215 )
1216 found_files = mp_manager.list()
1217 with self.assertLogs(level=logging.WARNING) as cm:
1218 self.assertFalse(
1219 cros_generate_breakpad_symbols.ValidateSymbolFile(
1220 str(sym_file),
1221 "/build/board/lib64/libc.so.6",
1222 "/build/board",
1223 found_files,
1224 )
1225 )
1226 self.assertIn(test.expected_error, cm.output[0])
1227 self.assertEqual(len(cm.output), 1)
1228
1229 def testValidCrashReporterSymbolFile(self):
1230 """Test a crash_reporter symbol file can pass the additional checks."""
1231 with multiprocessing.Manager() as mp_manager:
1232 sym_file = self.tempdir / "crash_reporter.sym"
1233 self._CreateSymbolFile(
1234 sym_file,
1235 func_lines=2000,
1236 public_lines=10,
1237 stack_lines=2000,
1238 line_number_lines=20000,
1239 )
1240 found_files = mp_manager.list()
1241 self.assertTrue(
1242 cros_generate_breakpad_symbols.ValidateSymbolFile(
1243 str(sym_file),
1244 "/build/board/sbin/crash_reporter",
1245 "/build/board",
1246 found_files,
1247 )
1248 )
1249 self.assertEqual(
1250 list(found_files),
1251 [cros_generate_breakpad_symbols.ExpectedFiles.CRASH_REPORTER],
1252 )
1253
1254 def testInvalidCrashReporterSymbolFile(self):
1255 """Test that a crash_reporter symbol file is held to higher standards"""
1256
1257 class CrashReporterSymbolFileTest:
1258 """Defines the subtest for an invalid crash_reporter symbol file."""
1259
1260 def __init__(
1261 self,
1262 name,
1263 expected_error,
1264 func_lines=2000,
1265 stack_lines=2000,
1266 line_number_lines=20000,
1267 ):
1268 self.name = name
1269 self.expected_error = expected_error
1270 self.func_lines = func_lines
1271 self.stack_lines = stack_lines
1272 self.line_number_lines = line_number_lines
1273
1274 CRASH_REPORTER_SYMBOL_TESTS = [
1275 CrashReporterSymbolFileTest(
1276 name="Insufficient FUNC records",
1277 func_lines=500,
1278 expected_error="crash_reporter should have at least 1000 FUNC "
1279 "records, found 500",
1280 ),
1281 CrashReporterSymbolFileTest(
1282 name="Insufficient STACK records",
1283 stack_lines=100,
1284 expected_error="crash_reporter should have at least 1000 STACK "
1285 "records, found 100",
1286 ),
1287 CrashReporterSymbolFileTest(
1288 name="Insufficient line number records",
1289 line_number_lines=2000,
1290 expected_error="crash_reporter should have at least 10,000 "
1291 "line number records, found 2000",
1292 ),
1293 ]
1294 for test in CRASH_REPORTER_SYMBOL_TESTS:
1295 with self.subTest(
1296 name=test.name
1297 ), multiprocessing.Manager() as mp_manager:
1298 sym_file = self.tempdir / "crash_reporter.sym"
1299 self._CreateSymbolFile(
1300 sym_file,
1301 func_lines=test.func_lines,
1302 stack_lines=test.stack_lines,
1303 line_number_lines=test.line_number_lines,
1304 )
1305 found_files = mp_manager.list()
1306 with self.assertLogs(level=logging.WARNING) as cm:
1307 self.assertFalse(
1308 cros_generate_breakpad_symbols.ValidateSymbolFile(
1309 str(sym_file),
1310 "/build/board/sbin/crash_reporter",
1311 "/build/board",
1312 found_files,
1313 )
1314 )
1315 self.assertIn(test.expected_error, cm.output[0])
1316 self.assertEqual(len(cm.output), 1)
1317
1318 def testValidLibMetricsSymbolFile(self):
1319 """Test a libmetrics.so symbol file can pass the additional checks."""
1320 with multiprocessing.Manager() as mp_manager:
1321 sym_file = self.tempdir / "libmetrics.so.sym"
1322 self._CreateSymbolFile(
1323 sym_file,
1324 func_lines=200,
1325 public_lines=2,
1326 stack_lines=2000,
1327 line_number_lines=10000,
1328 )
1329 found_files = mp_manager.list()
1330 self.assertTrue(
1331 cros_generate_breakpad_symbols.ValidateSymbolFile(
1332 str(sym_file),
1333 "/build/board/usr/lib64/libmetrics.so",
1334 "/build/board",
1335 found_files,
1336 )
1337 )
1338 self.assertEqual(
1339 list(found_files),
1340 [cros_generate_breakpad_symbols.ExpectedFiles.LIBMETRICS],
1341 )
1342
1343 def testInvalidLibMetricsSymbolFile(self):
1344 """Test that a libmetrics.so symbol file is held to higher standards."""
1345
1346 class LibMetricsSymbolFileTest:
1347 """Defines the subtest for an invalid libmetrics.so symbol file."""
1348
1349 def __init__(
1350 self,
1351 name,
1352 expected_error,
1353 func_lines=200,
1354 public_lines=2,
1355 stack_lines=2000,
1356 line_number_lines=10000,
1357 ):
1358 self.name = name
1359 self.expected_error = expected_error
1360 self.func_lines = func_lines
1361 self.public_lines = public_lines
1362 self.stack_lines = stack_lines
1363 self.line_number_lines = line_number_lines
1364
1365 LIBMETRICS_SYMBOL_TESTS = [
1366 LibMetricsSymbolFileTest(
1367 name="Insufficient FUNC records",
1368 func_lines=10,
1369 expected_error="libmetrics should have at least 100 FUNC "
1370 "records, found 10",
1371 ),
1372 LibMetricsSymbolFileTest(
1373 name="Insufficient PUBLIC records",
1374 public_lines=0,
1375 expected_error="libmetrics should have at least 1 PUBLIC "
1376 "record, found 0",
1377 ),
1378 LibMetricsSymbolFileTest(
1379 name="Insufficient STACK records",
1380 stack_lines=500,
1381 expected_error="libmetrics should have at least 1000 STACK "
1382 "records, found 500",
1383 ),
1384 LibMetricsSymbolFileTest(
1385 name="Insufficient line number records",
1386 line_number_lines=2000,
1387 expected_error="libmetrics should have at least 5000 "
1388 "line number records, found 2000",
1389 ),
1390 ]
1391 for test in LIBMETRICS_SYMBOL_TESTS:
1392 with self.subTest(
1393 name=test.name
1394 ), multiprocessing.Manager() as mp_manager:
1395 sym_file = self.tempdir / "libmetrics.so.sym"
1396 self._CreateSymbolFile(
1397 sym_file,
1398 func_lines=test.func_lines,
1399 public_lines=test.public_lines,
1400 stack_lines=test.stack_lines,
1401 line_number_lines=test.line_number_lines,
1402 )
1403 found_files = mp_manager.list()
1404 with self.assertLogs(level=logging.WARNING) as cm:
1405 self.assertFalse(
1406 cros_generate_breakpad_symbols.ValidateSymbolFile(
1407 str(sym_file),
1408 "/build/board/usr/lib64/libmetrics.so",
1409 "/build/board",
1410 found_files,
1411 )
1412 )
1413 self.assertIn(test.expected_error, cm.output[0])
1414 self.assertEqual(len(cm.output), 1)
1415
1416
Mike Frysinger69cb41d2013-08-11 20:08:19 -04001417class UtilsTestDir(cros_test_lib.TempDirTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06001418 """Tests ReadSymsHeader."""
Mike Frysinger69cb41d2013-08-11 20:08:19 -04001419
Alex Klein1699fab2022-09-08 08:46:06 -06001420 def testReadSymsHeaderGoodFile(self):
1421 """Make sure ReadSymsHeader can parse sym files"""
1422 sym_file = os.path.join(self.tempdir, "sym")
1423 osutils.WriteFile(sym_file, "MODULE Linux x86 s0m31D chrooome")
Ian Barkley-Yeungfdaaf2e2023-03-02 17:30:39 -08001424 result = cros_generate_breakpad_symbols.ReadSymsHeader(
1425 sym_file, "unused_elfname"
1426 )
Alex Klein1699fab2022-09-08 08:46:06 -06001427 self.assertEqual(result.cpu, "x86")
1428 self.assertEqual(result.id, "s0m31D")
1429 self.assertEqual(result.name, "chrooome")
1430 self.assertEqual(result.os, "Linux")
Mike Frysinger69cb41d2013-08-11 20:08:19 -04001431
1432
1433class UtilsTest(cros_test_lib.TestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06001434 """Tests ReadSymsHeader."""
Mike Frysinger69cb41d2013-08-11 20:08:19 -04001435
Alex Klein1699fab2022-09-08 08:46:06 -06001436 def testReadSymsHeaderGoodBuffer(self):
1437 """Make sure ReadSymsHeader can parse sym file handles"""
1438 result = cros_generate_breakpad_symbols.ReadSymsHeader(
Ian Barkley-Yeungfdaaf2e2023-03-02 17:30:39 -08001439 io.BytesIO(b"MODULE Linux arm MY-ID-HERE blkid"), "unused_elfname"
Alex Klein1699fab2022-09-08 08:46:06 -06001440 )
1441 self.assertEqual(result.cpu, "arm")
1442 self.assertEqual(result.id, "MY-ID-HERE")
1443 self.assertEqual(result.name, "blkid")
1444 self.assertEqual(result.os, "Linux")
Mike Frysinger69cb41d2013-08-11 20:08:19 -04001445
Alex Klein1699fab2022-09-08 08:46:06 -06001446 def testReadSymsHeaderBadd(self):
1447 """Make sure ReadSymsHeader throws on bad sym files"""
1448 self.assertRaises(
1449 ValueError,
1450 cros_generate_breakpad_symbols.ReadSymsHeader,
1451 io.BytesIO(b"asdf"),
Ian Barkley-Yeungfdaaf2e2023-03-02 17:30:39 -08001452 "unused_elfname",
Alex Klein1699fab2022-09-08 08:46:06 -06001453 )
Mike Frysinger69cb41d2013-08-11 20:08:19 -04001454
Alex Klein1699fab2022-09-08 08:46:06 -06001455 def testBreakpadDir(self):
1456 """Make sure board->breakpad path expansion works"""
1457 expected = "/build/blah/usr/lib/debug/breakpad"
1458 result = cros_generate_breakpad_symbols.FindBreakpadDir("blah")
1459 self.assertEqual(expected, result)
Mike Frysinger69cb41d2013-08-11 20:08:19 -04001460
Alex Klein1699fab2022-09-08 08:46:06 -06001461 def testDebugDir(self):
1462 """Make sure board->debug path expansion works"""
1463 expected = "/build/blah/usr/lib/debug"
1464 result = cros_generate_breakpad_symbols.FindDebugDir("blah")
1465 self.assertEqual(expected, result)