| 1 |
# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 |
# SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 |
|
| 4 |
|
| 5 |
import binascii |
| 6 |
import contextlib |
| 7 |
import os |
| 8 |
import os.path |
| 9 |
from pathlib import Path |
| 10 |
import tempfile |
| 11 |
import unittest |
| 12 |
import unittest.mock |
| 13 |
|
| 14 |
from _drgn_util.elf import ET, PT, SHF, SHT |
| 15 |
from drgn import ( |
| 16 |
DebugInfoOptions, |
| 17 |
MainModule, |
| 18 |
MissingDebugInfoError, |
| 19 |
ModuleFileStatus, |
| 20 |
Program, |
| 21 |
SharedLibraryModule, |
| 22 |
SupplementaryFileKind, |
| 23 |
VdsoModule, |
| 24 |
) |
| 25 |
from tests import TestCase |
| 26 |
from tests.dwarfwriter import create_dwarf_file |
| 27 |
from tests.elfwriter import ElfSection, create_elf_file |
| 28 |
from tests.resources import get_resource |
| 29 |
|
| 30 |
ALLOCATED_SECTION = ElfSection( |
| 31 |
name=".bss", |
| 32 |
sh_type=SHT.PROGBITS, |
| 33 |
sh_flags=SHF.ALLOC, |
| 34 |
p_type=PT.LOAD, |
| 35 |
vaddr=0x10000000, |
| 36 |
memsz=0x1000, |
| 37 |
) |
| 38 |
|
| 39 |
|
| 40 |
@contextlib.contextmanager |
| 41 |
def NamedTemporaryElfFile(*, loadable=True, debug=True, sections=(), **kwargs): |
| 42 |
if loadable: |
| 43 |
sections = (ALLOCATED_SECTION,) + sections |
| 44 |
with tempfile.NamedTemporaryFile() as f: |
| 45 |
if debug: |
| 46 |
f.write(create_dwarf_file((), sections=sections, **kwargs)) |
| 47 |
else: |
| 48 |
f.write(create_elf_file(ET.EXEC, sections=sections, **kwargs)) |
| 49 |
f.flush() |
| 50 |
yield f |
| 51 |
|
| 52 |
|
| 53 |
class TestModuleTryFile(TestCase): |
| 54 |
def setUp(self): |
| 55 |
self.prog = Program() |
| 56 |
self.prog.set_enabled_debug_info_finders([]) |
| 57 |
|
| 58 |
def test_want_both(self): |
| 59 |
module = self.prog.extra_module("/foo/bar", create=True) |
| 60 |
with NamedTemporaryElfFile() as f: |
| 61 |
module.try_file(f.name) |
| 62 |
self.assertEqual(module.loaded_file_status, ModuleFileStatus.HAVE) |
| 63 |
self.assertEqual(module.loaded_file_path, f.name) |
| 64 |
self.assertEqual(module.debug_file_status, ModuleFileStatus.HAVE) |
| 65 |
self.assertEqual(module.debug_file_path, f.name) |
| 66 |
|
| 67 |
for status in set(ModuleFileStatus) - {ModuleFileStatus.HAVE}: |
| 68 |
for file in ("loaded", "debug"): |
| 69 |
with self.subTest(file=file): |
| 70 |
self.assertEqual(getattr(module, f"wants_{file}_file")(), False) |
| 71 |
# Test that we can't unset the file once it's set. |
| 72 |
status_attr = file + "_file_status" |
| 73 |
with self.subTest(from_=ModuleFileStatus.HAVE, to=status): |
| 74 |
self.assertRaises( |
| 75 |
ValueError, setattr, module, status_attr, status |
| 76 |
) |
| 77 |
self.assertEqual( |
| 78 |
getattr(module, status_attr), ModuleFileStatus.HAVE |
| 79 |
) |
| 80 |
|
| 81 |
def test_want_both_not_loadable(self): |
| 82 |
module = self.prog.extra_module("/foo/bar", create=True) |
| 83 |
with NamedTemporaryElfFile(loadable=False) as f: |
| 84 |
module.try_file(f.name) |
| 85 |
self.assertEqual(module.loaded_file_status, ModuleFileStatus.WANT) |
| 86 |
self.assertIsNone(module.loaded_file_path) |
| 87 |
self.assertEqual(module.debug_file_status, ModuleFileStatus.HAVE) |
| 88 |
self.assertEqual(module.debug_file_path, f.name) |
| 89 |
|
| 90 |
def test_want_both_no_debug(self): |
| 91 |
module = self.prog.extra_module("/foo/bar", create=True) |
| 92 |
with NamedTemporaryElfFile(debug=False) as f: |
| 93 |
module.try_file(f.name) |
| 94 |
self.assertEqual(module.loaded_file_status, ModuleFileStatus.HAVE) |
| 95 |
self.assertEqual(module.loaded_file_path, f.name) |
| 96 |
self.assertEqual(module.debug_file_status, ModuleFileStatus.WANT) |
| 97 |
self.assertIsNone(module.debug_file_path) |
| 98 |
|
| 99 |
def test_want_both_is_neither(self): |
| 100 |
module = self.prog.extra_module("/foo/bar", create=True) |
| 101 |
with NamedTemporaryElfFile(loadable=False, debug=False) as f: |
| 102 |
module.try_file(f.name) |
| 103 |
self.assertEqual(module.loaded_file_status, ModuleFileStatus.WANT) |
| 104 |
self.assertEqual(module.debug_file_status, ModuleFileStatus.WANT) |
| 105 |
self.assertIsNone(module.loaded_file_path) |
| 106 |
self.assertIsNone(module.debug_file_path) |
| 107 |
|
| 108 |
def test_only_want_loaded(self): |
| 109 |
module = self.prog.extra_module("/foo/bar", create=True) |
| 110 |
module.debug_file_status = ModuleFileStatus.DONT_WANT |
| 111 |
with NamedTemporaryElfFile() as f: |
| 112 |
module.try_file(f.name) |
| 113 |
self.assertEqual(module.loaded_file_status, ModuleFileStatus.HAVE) |
| 114 |
self.assertEqual(module.loaded_file_path, f.name) |
| 115 |
self.assertEqual(module.debug_file_status, ModuleFileStatus.DONT_WANT) |
| 116 |
self.assertIsNone(module.debug_file_path) |
| 117 |
|
| 118 |
def test_only_want_loaded_not_loadable(self): |
| 119 |
module = self.prog.extra_module("/foo/bar", create=True) |
| 120 |
module.debug_file_status = ModuleFileStatus.DONT_WANT |
| 121 |
with NamedTemporaryElfFile(loadable=False) as f: |
| 122 |
module.try_file(f.name) |
| 123 |
self.assertEqual(module.loaded_file_status, ModuleFileStatus.WANT) |
| 124 |
self.assertIsNone(module.loaded_file_path) |
| 125 |
self.assertEqual(module.debug_file_status, ModuleFileStatus.DONT_WANT) |
| 126 |
self.assertIsNone(module.debug_file_path) |
| 127 |
|
| 128 |
def test_only_want_loaded_no_debug(self): |
| 129 |
module = self.prog.extra_module("/foo/bar", create=True) |
| 130 |
module.debug_file_status = ModuleFileStatus.DONT_WANT |
| 131 |
with NamedTemporaryElfFile(debug=False) as f: |
| 132 |
module.try_file(f.name) |
| 133 |
self.assertEqual(module.loaded_file_status, ModuleFileStatus.HAVE) |
| 134 |
self.assertEqual(module.loaded_file_path, f.name) |
| 135 |
self.assertEqual(module.debug_file_status, ModuleFileStatus.DONT_WANT) |
| 136 |
self.assertIsNone(module.debug_file_path) |
| 137 |
|
| 138 |
def test_only_want_loaded_is_neither(self): |
| 139 |
module = self.prog.extra_module("/foo/bar", create=True) |
| 140 |
module.debug_file_status = ModuleFileStatus.DONT_WANT |
| 141 |
with NamedTemporaryElfFile(loadable=False, debug=False) as f: |
| 142 |
module.try_file(f.name) |
| 143 |
self.assertEqual(module.loaded_file_status, ModuleFileStatus.WANT) |
| 144 |
self.assertIsNone(module.loaded_file_path) |
| 145 |
self.assertEqual(module.debug_file_status, ModuleFileStatus.DONT_WANT) |
| 146 |
self.assertIsNone(module.debug_file_path) |
| 147 |
|
| 148 |
def test_only_want_debug(self): |
| 149 |
module = self.prog.extra_module("/foo/bar", create=True) |
| 150 |
module.loaded_file_status = ModuleFileStatus.DONT_WANT |
| 151 |
with NamedTemporaryElfFile() as f: |
| 152 |
module.try_file(f.name) |
| 153 |
self.assertEqual(module.loaded_file_status, ModuleFileStatus.DONT_WANT) |
| 154 |
self.assertIsNone(module.loaded_file_path) |
| 155 |
self.assertEqual(module.debug_file_status, ModuleFileStatus.HAVE) |
| 156 |
self.assertEqual(module.debug_file_path, f.name) |
| 157 |
|
| 158 |
def test_only_want_debug_not_loadable(self): |
| 159 |
module = self.prog.extra_module("/foo/bar", create=True) |
| 160 |
module.loaded_file_status = ModuleFileStatus.DONT_WANT |
| 161 |
with NamedTemporaryElfFile(loadable=False) as f: |
| 162 |
module.try_file(f.name) |
| 163 |
self.assertEqual(module.loaded_file_status, ModuleFileStatus.DONT_WANT) |
| 164 |
self.assertIsNone(module.loaded_file_path) |
| 165 |
self.assertEqual(module.debug_file_status, ModuleFileStatus.HAVE) |
| 166 |
self.assertEqual(module.debug_file_path, f.name) |
| 167 |
|
| 168 |
def test_only_want_debug_no_debug(self): |
| 169 |
module = self.prog.extra_module("/foo/bar", create=True) |
| 170 |
module.loaded_file_status = ModuleFileStatus.DONT_WANT |
| 171 |
with NamedTemporaryElfFile(debug=False) as f: |
| 172 |
module.try_file(f.name) |
| 173 |
self.assertEqual(module.loaded_file_status, ModuleFileStatus.DONT_WANT) |
| 174 |
self.assertIsNone(module.loaded_file_path) |
| 175 |
self.assertEqual(module.debug_file_status, ModuleFileStatus.WANT) |
| 176 |
self.assertIsNone(module.debug_file_path) |
| 177 |
|
| 178 |
def test_only_want_debug_is_neither(self): |
| 179 |
module = self.prog.extra_module("/foo/bar", create=True) |
| 180 |
module.loaded_file_status = ModuleFileStatus.DONT_WANT |
| 181 |
with NamedTemporaryElfFile(loadable=False, debug=False) as f: |
| 182 |
module.try_file(f.name) |
| 183 |
self.assertEqual(module.loaded_file_status, ModuleFileStatus.DONT_WANT) |
| 184 |
self.assertIsNone(module.loaded_file_path) |
| 185 |
self.assertEqual(module.debug_file_status, ModuleFileStatus.WANT) |
| 186 |
self.assertIsNone(module.debug_file_path) |
| 187 |
|
| 188 |
def test_want_neither(self): |
| 189 |
module = self.prog.extra_module("/foo/bar", create=True) |
| 190 |
module.loaded_file_status = ModuleFileStatus.DONT_WANT |
| 191 |
module.debug_file_status = ModuleFileStatus.DONT_WANT |
| 192 |
with NamedTemporaryElfFile() as f: |
| 193 |
module.try_file(f.name) |
| 194 |
self.assertEqual(module.loaded_file_status, ModuleFileStatus.DONT_WANT) |
| 195 |
self.assertIsNone(module.loaded_file_path) |
| 196 |
self.assertEqual(module.debug_file_status, ModuleFileStatus.DONT_WANT) |
| 197 |
self.assertIsNone(module.debug_file_path) |
| 198 |
|
| 199 |
def test_separate_files_loaded_first(self): |
| 200 |
module = self.prog.extra_module("/foo/bar", create=True) |
| 201 |
with NamedTemporaryElfFile(debug=False) as f1: |
| 202 |
module.try_file(f1.name) |
| 203 |
self.assertEqual(module.loaded_file_status, ModuleFileStatus.HAVE) |
| 204 |
self.assertEqual(module.loaded_file_path, f1.name) |
| 205 |
self.assertEqual(module.debug_file_status, ModuleFileStatus.WANT) |
| 206 |
self.assertIsNone(module.debug_file_path) |
| 207 |
|
| 208 |
with NamedTemporaryElfFile(loadable=False) as f2: |
| 209 |
module.try_file(f2.name) |
| 210 |
self.assertEqual(module.loaded_file_status, ModuleFileStatus.HAVE) |
| 211 |
self.assertEqual(module.loaded_file_path, f1.name) |
| 212 |
self.assertEqual(module.debug_file_status, ModuleFileStatus.HAVE) |
| 213 |
self.assertEqual(module.debug_file_path, f2.name) |
| 214 |
|
| 215 |
def test_separate_files_debug_first(self): |
| 216 |
module = self.prog.extra_module("/foo/bar", create=True) |
| 217 |
with NamedTemporaryElfFile(loadable=False) as f1: |
| 218 |
module.try_file(f1.name) |
| 219 |
self.assertEqual(module.loaded_file_status, ModuleFileStatus.WANT) |
| 220 |
self.assertIsNone(module.loaded_file_path) |
| 221 |
self.assertEqual(module.debug_file_status, ModuleFileStatus.HAVE) |
| 222 |
self.assertEqual(module.debug_file_path, f1.name) |
| 223 |
|
| 224 |
with NamedTemporaryElfFile(debug=False) as f2: |
| 225 |
module.try_file(f2.name) |
| 226 |
self.assertEqual(module.loaded_file_status, ModuleFileStatus.HAVE) |
| 227 |
self.assertEqual(module.loaded_file_path, f2.name) |
| 228 |
self.assertEqual(module.debug_file_status, ModuleFileStatus.HAVE) |
| 229 |
self.assertEqual(module.debug_file_path, f1.name) |
| 230 |
|
| 231 |
def test_loadable_then_both(self): |
| 232 |
module = self.prog.extra_module("/foo/bar", create=True) |
| 233 |
with NamedTemporaryElfFile(debug=False) as f1: |
| 234 |
module.try_file(f1.name) |
| 235 |
self.assertEqual(module.loaded_file_status, ModuleFileStatus.HAVE) |
| 236 |
self.assertEqual(module.loaded_file_path, f1.name) |
| 237 |
self.assertEqual(module.debug_file_status, ModuleFileStatus.WANT) |
| 238 |
self.assertIsNone(module.debug_file_path) |
| 239 |
|
| 240 |
with NamedTemporaryElfFile() as f2: |
| 241 |
module.try_file(f2.name) |
| 242 |
self.assertEqual(module.loaded_file_status, ModuleFileStatus.HAVE) |
| 243 |
self.assertEqual(module.loaded_file_path, f1.name) |
| 244 |
self.assertEqual(module.debug_file_status, ModuleFileStatus.HAVE) |
| 245 |
self.assertEqual(module.debug_file_path, f2.name) |
| 246 |
|
| 247 |
def test_debug_then_both(self): |
| 248 |
module = self.prog.extra_module("/foo/bar", create=True) |
| 249 |
with NamedTemporaryElfFile(loadable=False) as f1: |
| 250 |
module.try_file(f1.name) |
| 251 |
self.assertEqual(module.loaded_file_status, ModuleFileStatus.WANT) |
| 252 |
self.assertIsNone(module.loaded_file_path) |
| 253 |
self.assertEqual(module.debug_file_status, ModuleFileStatus.HAVE) |
| 254 |
self.assertEqual(module.debug_file_path, f1.name) |
| 255 |
|
| 256 |
with NamedTemporaryElfFile() as f2: |
| 257 |
module.try_file(f2.name) |
| 258 |
self.assertEqual(module.loaded_file_status, ModuleFileStatus.HAVE) |
| 259 |
self.assertEqual(module.loaded_file_path, f2.name) |
| 260 |
self.assertEqual(module.debug_file_status, ModuleFileStatus.HAVE) |
| 261 |
self.assertEqual(module.debug_file_path, f1.name) |
| 262 |
|
| 263 |
def test_no_build_id_force(self): |
| 264 |
module = self.prog.extra_module("/foo/bar", create=True) |
| 265 |
with NamedTemporaryElfFile() as f: |
| 266 |
module.try_file(f.name, force=True) |
| 267 |
self.assertEqual(module.debug_file_status, ModuleFileStatus.HAVE) |
| 268 |
self.assertEqual(module.debug_file_path, f.name) |
| 269 |
|
| 270 |
def test_no_build_id_file_has_build_id(self): |
| 271 |
module = self.prog.extra_module("/foo/bar", create=True) |
| 272 |
with NamedTemporaryElfFile(build_id=b"\x01\x23\x45\x67\x89\xab\xcd\xef") as f: |
| 273 |
module.try_file(f.name) |
| 274 |
self.assertEqual(module.debug_file_status, ModuleFileStatus.HAVE) |
| 275 |
self.assertEqual(module.debug_file_path, f.name) |
| 276 |
self.assertEqual(module.build_id, b"\x01\x23\x45\x67\x89\xab\xcd\xef") |
| 277 |
|
| 278 |
def test_no_build_id_file_has_build_id_force(self): |
| 279 |
module = self.prog.extra_module("/foo/bar", create=True) |
| 280 |
with NamedTemporaryElfFile(build_id=b"\x01\x23\x45\x67\x89\xab\xcd\xef") as f: |
| 281 |
module.try_file(f.name, force=True) |
| 282 |
self.assertEqual(module.debug_file_status, ModuleFileStatus.HAVE) |
| 283 |
self.assertEqual(module.debug_file_path, f.name) |
| 284 |
self.assertEqual(module.build_id, b"\x01\x23\x45\x67\x89\xab\xcd\xef") |
| 285 |
|
| 286 |
def test_build_id_match(self): |
| 287 |
module = self.prog.extra_module("/foo/bar", create=True) |
| 288 |
module.build_id = b"\x01\x23\x45\x67\x89\xab\xcd\xef" |
| 289 |
with NamedTemporaryElfFile(build_id=b"\x01\x23\x45\x67\x89\xab\xcd\xef") as f: |
| 290 |
module.try_file(f.name) |
| 291 |
self.assertEqual(module.debug_file_status, ModuleFileStatus.HAVE) |
| 292 |
self.assertEqual(module.debug_file_path, f.name) |
| 293 |
self.assertEqual(module.build_id, b"\x01\x23\x45\x67\x89\xab\xcd\xef") |
| 294 |
|
| 295 |
def test_build_id_match_force(self): |
| 296 |
module = self.prog.extra_module("/foo/bar", create=True) |
| 297 |
module.build_id = b"\x01\x23\x45\x67\x89\xab\xcd\xef" |
| 298 |
with NamedTemporaryElfFile(build_id=b"\x01\x23\x45\x67\x89\xab\xcd\xef") as f: |
| 299 |
module.try_file(f.name, force=True) |
| 300 |
self.assertEqual(module.debug_file_status, ModuleFileStatus.HAVE) |
| 301 |
self.assertEqual(module.debug_file_path, f.name) |
| 302 |
self.assertEqual(module.build_id, b"\x01\x23\x45\x67\x89\xab\xcd\xef") |
| 303 |
|
| 304 |
def test_build_id_mismatch(self): |
| 305 |
module = self.prog.extra_module("/foo/bar", create=True) |
| 306 |
module.build_id = b"\x01\x23\x45\x67\x89\xab\xcd\xef" |
| 307 |
with NamedTemporaryElfFile(build_id=b"\xff\xff\xff\xff") as f: |
| 308 |
module.try_file(f.name) |
| 309 |
self.assertEqual(module.debug_file_status, ModuleFileStatus.WANT) |
| 310 |
self.assertIsNone(module.debug_file_path) |
| 311 |
self.assertEqual(module.build_id, b"\x01\x23\x45\x67\x89\xab\xcd\xef") |
| 312 |
|
| 313 |
def test_build_id_mismatch_force(self): |
| 314 |
module = self.prog.extra_module("/foo/bar", create=True) |
| 315 |
module.build_id = b"\x01\x23\x45\x67\x89\xab\xcd\xef" |
| 316 |
with NamedTemporaryElfFile(build_id=b"\xff\xff\xff\xff") as f: |
| 317 |
module.try_file(f.name, force=True) |
| 318 |
self.assertEqual(module.debug_file_status, ModuleFileStatus.HAVE) |
| 319 |
self.assertEqual(module.debug_file_path, f.name) |
| 320 |
self.assertEqual(module.build_id, b"\x01\x23\x45\x67\x89\xab\xcd\xef") |
| 321 |
|
| 322 |
def test_build_id_missing(self): |
| 323 |
module = self.prog.extra_module("/foo/bar", create=True) |
| 324 |
module.build_id = b"\x01\x23\x45\x67\x89\xab\xcd\xef" |
| 325 |
with NamedTemporaryElfFile() as f: |
| 326 |
module.try_file(f.name) |
| 327 |
self.assertEqual(module.debug_file_status, ModuleFileStatus.WANT) |
| 328 |
self.assertIsNone(module.debug_file_path) |
| 329 |
self.assertEqual(module.build_id, b"\x01\x23\x45\x67\x89\xab\xcd\xef") |
| 330 |
|
| 331 |
def test_build_id_missing_force(self): |
| 332 |
module = self.prog.extra_module("/foo/bar", create=True) |
| 333 |
module.build_id = b"\x01\x23\x45\x67\x89\xab\xcd\xef" |
| 334 |
with NamedTemporaryElfFile() as f: |
| 335 |
module.try_file(f.name, force=True) |
| 336 |
self.assertEqual(module.debug_file_status, ModuleFileStatus.HAVE) |
| 337 |
self.assertEqual(module.debug_file_path, f.name) |
| 338 |
self.assertEqual(module.build_id, b"\x01\x23\x45\x67\x89\xab\xcd\xef") |
| 339 |
|
| 340 |
def test_gnu_debugaltlink(self): |
| 341 |
build_id = b"\x01\x23\x45\x67\x89\xab\xcd\xef" |
| 342 |
alt_build_id = b"\xfe\xdc\xba\x98\x76\x54\x32\x10" |
| 343 |
|
| 344 |
with tempfile.TemporaryDirectory( |
| 345 |
prefix="bin-" |
| 346 |
) as bin_dir, tempfile.TemporaryDirectory(prefix="debug-") as debug_dir: |
| 347 |
bin_dir = Path(bin_dir) |
| 348 |
debug_dir = Path(debug_dir) |
| 349 |
|
| 350 |
alt_path = debug_dir / "alt.debug" |
| 351 |
alt_path.write_bytes(create_dwarf_file((), build_id=alt_build_id)) |
| 352 |
|
| 353 |
binary_path = bin_dir / "binary" |
| 354 |
binary_path.write_bytes( |
| 355 |
create_dwarf_file( |
| 356 |
(), |
| 357 |
sections=(ALLOCATED_SECTION,), |
| 358 |
build_id=build_id, |
| 359 |
gnu_debugaltlink=(alt_path, alt_build_id), |
| 360 |
) |
| 361 |
) |
| 362 |
|
| 363 |
module = self.prog.extra_module(bin_dir / "binary", create=True) |
| 364 |
module.build_id = build_id |
| 365 |
|
| 366 |
self.assertEqual(module.debug_file_status, ModuleFileStatus.WANT) |
| 367 |
self.assertRaises(ValueError, module.wanted_supplementary_debug_file) |
| 368 |
|
| 369 |
module.try_file(binary_path) |
| 370 |
self.assertEqual( |
| 371 |
module.debug_file_status, ModuleFileStatus.WANT_SUPPLEMENTARY |
| 372 |
) |
| 373 |
self.assertEqual(module.wants_debug_file(), True) |
| 374 |
self.assertIsNone(module.debug_file_path) |
| 375 |
self.assertIsNone(module.supplementary_debug_file_kind) |
| 376 |
self.assertIsNone(module.supplementary_debug_file_path) |
| 377 |
self.assertEqual( |
| 378 |
module.wanted_supplementary_debug_file(), |
| 379 |
( |
| 380 |
SupplementaryFileKind.GNU_DEBUGALTLINK, |
| 381 |
str(binary_path), |
| 382 |
str(alt_path), |
| 383 |
alt_build_id, |
| 384 |
), |
| 385 |
) |
| 386 |
|
| 387 |
with self.assertRaises(ValueError): |
| 388 |
module.debug_file_status = ModuleFileStatus.HAVE |
| 389 |
self.assertEqual( |
| 390 |
module.debug_file_status, ModuleFileStatus.WANT_SUPPLEMENTARY |
| 391 |
) |
| 392 |
module.debug_file_status = ModuleFileStatus.WANT_SUPPLEMENTARY |
| 393 |
self.assertEqual( |
| 394 |
module.debug_file_status, ModuleFileStatus.WANT_SUPPLEMENTARY |
| 395 |
) |
| 396 |
|
| 397 |
module.try_file(alt_path) |
| 398 |
self.assertEqual(module.debug_file_status, ModuleFileStatus.HAVE) |
| 399 |
self.assertEqual(module.debug_file_path, str(binary_path)) |
| 400 |
self.assertEqual( |
| 401 |
module.supplementary_debug_file_kind, |
| 402 |
SupplementaryFileKind.GNU_DEBUGALTLINK, |
| 403 |
) |
| 404 |
self.assertEqual(module.supplementary_debug_file_path, str(alt_path)) |
| 405 |
self.assertRaises(ValueError, module.wanted_supplementary_debug_file) |
| 406 |
|
| 407 |
def test_gnu_debugaltlink_build_id_mismatch(self): |
| 408 |
build_id = b"\x01\x23\x45\x67\x89\xab\xcd\xef" |
| 409 |
alt_build_id = b"\xfe\xdc\xba\x98\x76\x54\x32\x10" |
| 410 |
|
| 411 |
with tempfile.TemporaryDirectory( |
| 412 |
prefix="bin-" |
| 413 |
) as bin_dir, tempfile.TemporaryDirectory(prefix="debug-") as debug_dir: |
| 414 |
bin_dir = Path(bin_dir) |
| 415 |
debug_dir = Path(debug_dir) |
| 416 |
|
| 417 |
alt_path = debug_dir / "alt.debug" |
| 418 |
alt_path.write_bytes(create_dwarf_file((), build_id=alt_build_id[::-1])) |
| 419 |
|
| 420 |
binary_path = bin_dir / "binary" |
| 421 |
binary_path.write_bytes( |
| 422 |
create_dwarf_file( |
| 423 |
(), |
| 424 |
sections=(ALLOCATED_SECTION,), |
| 425 |
build_id=build_id, |
| 426 |
gnu_debugaltlink=(alt_path, alt_build_id), |
| 427 |
) |
| 428 |
) |
| 429 |
|
| 430 |
module = self.prog.extra_module(bin_dir / "binary", create=True) |
| 431 |
module.build_id = build_id |
| 432 |
|
| 433 |
self.assertEqual(module.debug_file_status, ModuleFileStatus.WANT) |
| 434 |
self.assertRaises(ValueError, module.wanted_supplementary_debug_file) |
| 435 |
|
| 436 |
module.try_file(binary_path) |
| 437 |
self.assertEqual( |
| 438 |
module.debug_file_status, ModuleFileStatus.WANT_SUPPLEMENTARY |
| 439 |
) |
| 440 |
self.assertIsNone(module.debug_file_path) |
| 441 |
self.assertIsNone(module.supplementary_debug_file_kind) |
| 442 |
self.assertIsNone(module.supplementary_debug_file_path) |
| 443 |
self.assertEqual( |
| 444 |
module.wanted_supplementary_debug_file(), |
| 445 |
( |
| 446 |
SupplementaryFileKind.GNU_DEBUGALTLINK, |
| 447 |
str(binary_path), |
| 448 |
str(alt_path), |
| 449 |
alt_build_id, |
| 450 |
), |
| 451 |
) |
| 452 |
|
| 453 |
module.try_file(alt_path) |
| 454 |
self.assertEqual( |
| 455 |
module.debug_file_status, ModuleFileStatus.WANT_SUPPLEMENTARY |
| 456 |
) |
| 457 |
self.assertIsNone(module.debug_file_path) |
| 458 |
self.assertIsNone(module.supplementary_debug_file_kind) |
| 459 |
self.assertIsNone(module.supplementary_debug_file_path) |
| 460 |
self.assertEqual( |
| 461 |
module.wanted_supplementary_debug_file(), |
| 462 |
( |
| 463 |
SupplementaryFileKind.GNU_DEBUGALTLINK, |
| 464 |
str(binary_path), |
| 465 |
str(alt_path), |
| 466 |
alt_build_id, |
| 467 |
), |
| 468 |
) |
| 469 |
|
| 470 |
def test_gnu_debugaltlink_then_both(self): |
| 471 |
build_id = b"\x01\x23\x45\x67\x89\xab\xcd\xef" |
| 472 |
alt_build_id = b"\xfe\xdc\xba\x98\x76\x54\x32\x10" |
| 473 |
|
| 474 |
with tempfile.TemporaryDirectory( |
| 475 |
prefix="bin-" |
| 476 |
) as bin_dir, tempfile.TemporaryDirectory(prefix="debug-") as debug_dir: |
| 477 |
bin_dir = Path(bin_dir) |
| 478 |
debug_dir = Path(debug_dir) |
| 479 |
|
| 480 |
alt_path = debug_dir / "alt.debug" |
| 481 |
alt_path.write_bytes(create_dwarf_file((), build_id=alt_build_id)) |
| 482 |
|
| 483 |
module = self.prog.extra_module(bin_dir / "binary", create=True) |
| 484 |
module.build_id = build_id |
| 485 |
with NamedTemporaryElfFile( |
| 486 |
build_id=build_id, |
| 487 |
gnu_debugaltlink=(alt_path, alt_build_id), |
| 488 |
) as f1: |
| 489 |
module.try_file(f1.name) |
| 490 |
self.assertEqual( |
| 491 |
module.debug_file_status, ModuleFileStatus.WANT_SUPPLEMENTARY |
| 492 |
) |
| 493 |
|
| 494 |
with NamedTemporaryElfFile(build_id=build_id) as f2: |
| 495 |
module.try_file(f2.name) |
| 496 |
|
| 497 |
self.assertEqual(module.loaded_file_status, ModuleFileStatus.HAVE) |
| 498 |
self.assertEqual(module.loaded_file_path, f1.name) |
| 499 |
self.assertEqual(module.debug_file_status, ModuleFileStatus.HAVE) |
| 500 |
self.assertEqual(module.debug_file_path, f2.name) |
| 501 |
|
| 502 |
def test_gnu_debugaltlink_cancel(self): |
| 503 |
build_id = b"\x01\x23\x45\x67\x89\xab\xcd\xef" |
| 504 |
alt_build_id = b"\xfe\xdc\xba\x98\x76\x54\x32\x10" |
| 505 |
|
| 506 |
with tempfile.TemporaryDirectory( |
| 507 |
prefix="bin-" |
| 508 |
) as bin_dir, tempfile.TemporaryDirectory(prefix="debug-") as debug_dir: |
| 509 |
bin_dir = Path(bin_dir) |
| 510 |
debug_dir = Path(debug_dir) |
| 511 |
|
| 512 |
alt_path = debug_dir / "alt.debug" |
| 513 |
alt_path.write_bytes(create_dwarf_file((), build_id=alt_build_id)) |
| 514 |
|
| 515 |
module = self.prog.extra_module(bin_dir / "binary", create=True) |
| 516 |
module.build_id = build_id |
| 517 |
with NamedTemporaryElfFile( |
| 518 |
build_id=build_id, |
| 519 |
gnu_debugaltlink=(alt_path, alt_build_id), |
| 520 |
) as f: |
| 521 |
module.try_file(f.name) |
| 522 |
self.assertEqual( |
| 523 |
module.debug_file_status, ModuleFileStatus.WANT_SUPPLEMENTARY |
| 524 |
) |
| 525 |
|
| 526 |
module.debug_file_status = ModuleFileStatus.WANT |
| 527 |
self.assertEqual(module.debug_file_status, ModuleFileStatus.WANT) |
| 528 |
self.assertEqual(module.wants_debug_file(), True) |
| 529 |
self.assertRaises(ValueError, module.wanted_supplementary_debug_file) |
| 530 |
|
| 531 |
def test_extra_module_no_address_range(self): |
| 532 |
module = self.prog.extra_module("/foo/bar", create=True) |
| 533 |
with NamedTemporaryElfFile() as f: |
| 534 |
module.try_file(f.name) |
| 535 |
self.assertIsNone(module.address_range) |
| 536 |
self.assertEqual(module.loaded_file_bias, 0) |
| 537 |
self.assertEqual(module.debug_file_bias, 0) |
| 538 |
|
| 539 |
def test_extra_module_address_range(self): |
| 540 |
module = self.prog.extra_module("/foo/bar", create=True) |
| 541 |
module.address_range = (0x40000000, 0x40001000) |
| 542 |
with NamedTemporaryElfFile() as f: |
| 543 |
module.try_file(f.name) |
| 544 |
self.assertEqual(module.address_range, (0x40000000, 0x40001000)) |
| 545 |
self.assertEqual(module.loaded_file_bias, 0x30000000) |
| 546 |
self.assertEqual(module.debug_file_bias, 0x30000000) |
| 547 |
|
| 548 |
def test_extra_module_empty_address_range(self): |
| 549 |
module = self.prog.extra_module("/foo/bar", create=True) |
| 550 |
module.address_range = (0, 0) |
| 551 |
with NamedTemporaryElfFile() as f: |
| 552 |
module.try_file(f.name) |
| 553 |
self.assertEqual(module.address_range, (0, 0)) |
| 554 |
self.assertEqual(module.loaded_file_bias, 0) |
| 555 |
self.assertEqual(module.debug_file_bias, 0) |
| 556 |
|
| 557 |
|
| 558 |
class TestLinuxUserspaceCoreDump(TestCase): |
| 559 |
def setUp(self): |
| 560 |
self.prog = Program() |
| 561 |
self.prog.debug_info_options.directories = () |
| 562 |
self.prog.debug_info_options.debug_link_directories = () |
| 563 |
self.prog.set_enabled_debug_info_finders(["standard"]) |
| 564 |
|
| 565 |
def test_loaded_modules(self): |
| 566 |
self.prog.set_core_dump(get_resource("crashme.core")) |
| 567 |
|
| 568 |
loaded_modules = [] |
| 569 |
for module, new in self.prog.loaded_modules(): |
| 570 |
self.assertEqual(new, True) |
| 571 |
loaded_modules.append(module) |
| 572 |
found_modules = [] |
| 573 |
|
| 574 |
with self.subTest(module="main"): |
| 575 |
module = self.prog.main_module() |
| 576 |
found_modules.append(module) |
| 577 |
self.assertEqual(module.name, "/home/osandov/crashme") |
| 578 |
self.assertEqual(module.address_range, (0x400000, 0x404010)) |
| 579 |
self.assertEqual( |
| 580 |
module.build_id.hex(), "99a6524c4df01fbff9b43a6ead3d8e8e6201568b" |
| 581 |
) |
| 582 |
|
| 583 |
with self.subTest(module="crashme"): |
| 584 |
module = self.prog.shared_library_module( |
| 585 |
"/home/osandov/crashme.so", 0x7F6112CACE08 |
| 586 |
) |
| 587 |
found_modules.append(module) |
| 588 |
self.assertEqual(module.address_range, (0x7F6112CA9000, 0x7F6112CAD010)) |
| 589 |
self.assertEqual( |
| 590 |
module.build_id.hex(), "7bd58f10e741c3c8fbcf2031aa65f830f933d616" |
| 591 |
) |
| 592 |
|
| 593 |
with self.subTest(module="libc"): |
| 594 |
module = self.prog.shared_library_module("/lib64/libc.so.6", 0x7F6112C94960) |
| 595 |
found_modules.append(module) |
| 596 |
self.assertEqual(module.address_range, (0x7F6112AAE000, 0x7F6112C9EB70)) |
| 597 |
self.assertEqual( |
| 598 |
module.build_id.hex(), "77c77fee058b19c6f001cf2cb0371ce3b8341211" |
| 599 |
) |
| 600 |
|
| 601 |
with self.subTest(module="ld-linux"): |
| 602 |
module = self.prog.shared_library_module( |
| 603 |
"/lib64/ld-linux-x86-64.so.2", 0x7F6112CEAE68 |
| 604 |
) |
| 605 |
found_modules.append(module) |
| 606 |
self.assertEqual(module.address_range, (0x7F6112CB6000, 0x7F6112CEC2D8)) |
| 607 |
self.assertEqual( |
| 608 |
module.build_id.hex(), "91dcd0244204201b616bbf59427771b3751736ce" |
| 609 |
) |
| 610 |
|
| 611 |
with self.subTest(module="vdso"): |
| 612 |
module = self.prog.vdso_module("linux-vdso.so.1", 0x7F6112CB4438) |
| 613 |
found_modules.append(module) |
| 614 |
self.assertEqual(module.address_range, (0x7F6112CB4000, 0x7F6112CB590F)) |
| 615 |
self.assertEqual( |
| 616 |
module.build_id.hex(), "fdc3e4d463911345fbc6d9cc432e5ebc276e8e03" |
| 617 |
) |
| 618 |
|
| 619 |
self.assertCountEqual(loaded_modules, found_modules) |
| 620 |
|
| 621 |
loaded_modules = [] |
| 622 |
for module, new in self.prog.loaded_modules(): |
| 623 |
self.assertEqual(new, False) |
| 624 |
loaded_modules.append(module) |
| 625 |
self.assertCountEqual(loaded_modules, found_modules) |
| 626 |
|
| 627 |
def _try_vdso_in_core(self, module): |
| 628 |
module.debug_file_status = ModuleFileStatus.DONT_WANT |
| 629 |
self.prog.load_module_debug_info(module) |
| 630 |
self.assertEqual(module.loaded_file_status, ModuleFileStatus.HAVE) |
| 631 |
|
| 632 |
def test_bias(self): |
| 633 |
self.prog.set_core_dump(get_resource("crashme.core")) |
| 634 |
self.prog.create_loaded_modules() |
| 635 |
|
| 636 |
with self.subTest(module="main"): |
| 637 |
module = self.prog.main_module() |
| 638 |
module.try_file(get_resource("crashme")) |
| 639 |
self.assertEqual(module.loaded_file_bias, 0) |
| 640 |
self.assertEqual(module.debug_file_bias, 0) |
| 641 |
|
| 642 |
with self.subTest(module="crashme"): |
| 643 |
module = self.prog.shared_library_module( |
| 644 |
"/home/osandov/crashme.so", 0x7F6112CACE08 |
| 645 |
) |
| 646 |
module.try_file(get_resource("crashme.so")) |
| 647 |
self.assertEqual(module.loaded_file_bias, 0x7F6112CA9000) |
| 648 |
self.assertEqual(module.debug_file_bias, 0x7F6112CA9000) |
| 649 |
|
| 650 |
with self.subTest(module="vdso"): |
| 651 |
module = self.prog.vdso_module("linux-vdso.so.1", 0x7F6112CB4438) |
| 652 |
self._try_vdso_in_core(module) |
| 653 |
self.assertEqual(module.loaded_file_bias, 0x7F6112CB4000) |
| 654 |
self.assertIsNone(module.debug_file_bias) |
| 655 |
|
| 656 |
def test_loaded_modules_pie(self): |
| 657 |
self.prog.set_core_dump(get_resource("crashme_pie.core")) |
| 658 |
|
| 659 |
loaded_modules = [] |
| 660 |
for module, new in self.prog.loaded_modules(): |
| 661 |
self.assertEqual(new, True) |
| 662 |
loaded_modules.append(module) |
| 663 |
found_modules = [] |
| 664 |
|
| 665 |
with self.subTest(module="main"): |
| 666 |
module = self.prog.main_module() |
| 667 |
found_modules.append(module) |
| 668 |
self.assertEqual(module.name, "/home/osandov/crashme_pie") |
| 669 |
self.assertEqual(module.address_range, (0x557ED343D000, 0x557ED3441018)) |
| 670 |
self.assertEqual( |
| 671 |
module.build_id.hex(), "eb4ad7aaded3815ab133a6d7784a2c95a4e52998" |
| 672 |
) |
| 673 |
|
| 674 |
with self.subTest(module="crashme"): |
| 675 |
module = self.prog.shared_library_module( |
| 676 |
"/home/osandov/crashme.so", 0x7FAB2C38DE08 |
| 677 |
) |
| 678 |
found_modules.append(module) |
| 679 |
self.assertEqual(module.address_range, (0x7FAB2C38A000, 0x7FAB2C38E010)) |
| 680 |
self.assertEqual( |
| 681 |
module.build_id.hex(), "7bd58f10e741c3c8fbcf2031aa65f830f933d616" |
| 682 |
) |
| 683 |
|
| 684 |
with self.subTest(module="libc"): |
| 685 |
module = self.prog.shared_library_module("/lib64/libc.so.6", 0x7FAB2C375960) |
| 686 |
found_modules.append(module) |
| 687 |
self.assertEqual(module.address_range, (0x7FAB2C18F000, 0x7FAB2C37FB70)) |
| 688 |
self.assertEqual( |
| 689 |
module.build_id.hex(), "77c77fee058b19c6f001cf2cb0371ce3b8341211" |
| 690 |
) |
| 691 |
|
| 692 |
with self.subTest(module="ld-linux"): |
| 693 |
module = self.prog.shared_library_module( |
| 694 |
"/lib64/ld-linux-x86-64.so.2", 0x7FAB2C3CBE68 |
| 695 |
) |
| 696 |
found_modules.append(module) |
| 697 |
self.assertEqual(module.address_range, (0x7FAB2C397000, 0x7FAB2C3CD2D8)) |
| 698 |
self.assertEqual( |
| 699 |
module.build_id.hex(), "91dcd0244204201b616bbf59427771b3751736ce" |
| 700 |
) |
| 701 |
|
| 702 |
with self.subTest(module="vdso"): |
| 703 |
module = self.prog.vdso_module("linux-vdso.so.1", 0x7FAB2C395438) |
| 704 |
found_modules.append(module) |
| 705 |
self.assertEqual(module.address_range, (0x7FAB2C395000, 0x7FAB2C39690F)) |
| 706 |
self.assertEqual( |
| 707 |
module.build_id.hex(), "fdc3e4d463911345fbc6d9cc432e5ebc276e8e03" |
| 708 |
) |
| 709 |
|
| 710 |
self.assertCountEqual(loaded_modules, found_modules) |
| 711 |
|
| 712 |
loaded_modules = [] |
| 713 |
for module, new in self.prog.loaded_modules(): |
| 714 |
self.assertEqual(new, False) |
| 715 |
loaded_modules.append(module) |
| 716 |
self.assertCountEqual(loaded_modules, found_modules) |
| 717 |
|
| 718 |
def test_bias_pie(self): |
| 719 |
self.prog.set_core_dump(get_resource("crashme_pie.core")) |
| 720 |
self.prog.create_loaded_modules() |
| 721 |
|
| 722 |
with self.subTest(module="main"): |
| 723 |
module = self.prog.main_module() |
| 724 |
module.try_file(get_resource("crashme_pie")) |
| 725 |
self.assertEqual(module.loaded_file_bias, 0x557ED343D000) |
| 726 |
self.assertEqual(module.debug_file_bias, 0x557ED343D000) |
| 727 |
|
| 728 |
with self.subTest(module="crashme"): |
| 729 |
module = self.prog.shared_library_module( |
| 730 |
"/home/osandov/crashme.so", 0x7FAB2C38DE08 |
| 731 |
) |
| 732 |
module.try_file(get_resource("crashme.so")) |
| 733 |
self.assertEqual(module.loaded_file_bias, 0x7FAB2C38A000) |
| 734 |
self.assertEqual(module.debug_file_bias, 0x7FAB2C38A000) |
| 735 |
|
| 736 |
with self.subTest(module="vdso"): |
| 737 |
module = self.prog.vdso_module("linux-vdso.so.1", 0x7FAB2C395438) |
| 738 |
self._try_vdso_in_core(module) |
| 739 |
self.assertEqual(module.loaded_file_bias, 0x7FAB2C395000) |
| 740 |
self.assertIsNone(module.debug_file_bias) |
| 741 |
|
| 742 |
def test_loaded_modules_static(self): |
| 743 |
self.prog.set_core_dump(get_resource("crashme_static.core")) |
| 744 |
|
| 745 |
loaded_modules = [] |
| 746 |
for module, new in self.prog.loaded_modules(): |
| 747 |
self.assertEqual(new, True) |
| 748 |
loaded_modules.append(module) |
| 749 |
found_modules = [] |
| 750 |
|
| 751 |
with self.subTest(module="main"): |
| 752 |
module = self.prog.main_module() |
| 753 |
found_modules.append(module) |
| 754 |
self.assertEqual(module.name, "/home/osandov/crashme_static") |
| 755 |
self.assertEqual(module.address_range, (0x400000, 0x4042B8)) |
| 756 |
self.assertEqual( |
| 757 |
module.build_id.hex(), "a0b6befad9f0883c52c475ba3cee9c549cd082cf" |
| 758 |
) |
| 759 |
|
| 760 |
with self.subTest(module="vdso"): |
| 761 |
module = self.prog.vdso_module("linux-vdso.so.1", 0x7FBC73A66438) |
| 762 |
found_modules.append(module) |
| 763 |
self.assertEqual(module.address_range, (0x7FBC73A66000, 0x7FBC73A6790F)) |
| 764 |
self.assertEqual( |
| 765 |
module.build_id.hex(), "fdc3e4d463911345fbc6d9cc432e5ebc276e8e03" |
| 766 |
) |
| 767 |
|
| 768 |
self.assertCountEqual(loaded_modules, found_modules) |
| 769 |
|
| 770 |
loaded_modules = [] |
| 771 |
for module, new in self.prog.loaded_modules(): |
| 772 |
self.assertEqual(new, False) |
| 773 |
loaded_modules.append(module) |
| 774 |
self.assertCountEqual(loaded_modules, found_modules) |
| 775 |
|
| 776 |
def test_bias_static(self): |
| 777 |
self.prog.set_core_dump(get_resource("crashme_static.core")) |
| 778 |
self.prog.create_loaded_modules() |
| 779 |
|
| 780 |
with self.subTest(module="main"): |
| 781 |
module = self.prog.main_module() |
| 782 |
module.try_file(get_resource("crashme_static")) |
| 783 |
self.assertEqual(module.loaded_file_bias, 0x0) |
| 784 |
self.assertEqual(module.debug_file_bias, 0x0) |
| 785 |
|
| 786 |
with self.subTest(module="vdso"): |
| 787 |
module = self.prog.vdso_module("linux-vdso.so.1", 0x7FBC73A66438) |
| 788 |
self._try_vdso_in_core(module) |
| 789 |
self.assertEqual(module.loaded_file_bias, 0x7FBC73A66000) |
| 790 |
self.assertIsNone(module.debug_file_bias) |
| 791 |
|
| 792 |
def test_loaded_modules_static_pie(self): |
| 793 |
self.prog.set_core_dump(get_resource("crashme_static_pie.core")) |
| 794 |
|
| 795 |
loaded_modules = [] |
| 796 |
for module, new in self.prog.loaded_modules(): |
| 797 |
self.assertEqual(new, True) |
| 798 |
loaded_modules.append(module) |
| 799 |
found_modules = [] |
| 800 |
|
| 801 |
with self.subTest(module="main"): |
| 802 |
module = self.prog.main_module() |
| 803 |
found_modules.append(module) |
| 804 |
self.assertEqual(module.name, "/home/osandov/crashme_static_pie") |
| 805 |
self.assertEqual(module.address_range, (0x7FD981DC9000, 0x7FD981DCD278)) |
| 806 |
self.assertEqual( |
| 807 |
module.build_id.hex(), "3e0bc47f80d7e64724e11fc021a251ed0d35bc2c" |
| 808 |
) |
| 809 |
|
| 810 |
with self.subTest(module="vdso"): |
| 811 |
module = self.prog.vdso_module("linux-vdso.so.1", 0x7FD981DC7438) |
| 812 |
found_modules.append(module) |
| 813 |
self.assertEqual(module.address_range, (0x7FD981DC7000, 0x7FD981DC890F)) |
| 814 |
self.assertEqual( |
| 815 |
module.build_id.hex(), "fdc3e4d463911345fbc6d9cc432e5ebc276e8e03" |
| 816 |
) |
| 817 |
|
| 818 |
self.assertCountEqual(loaded_modules, found_modules) |
| 819 |
|
| 820 |
loaded_modules = [] |
| 821 |
for module, new in self.prog.loaded_modules(): |
| 822 |
self.assertEqual(new, False) |
| 823 |
loaded_modules.append(module) |
| 824 |
self.assertCountEqual(loaded_modules, found_modules) |
| 825 |
|
| 826 |
def test_bias_static_pie(self): |
| 827 |
self.prog.set_core_dump(get_resource("crashme_static_pie.core")) |
| 828 |
self.prog.create_loaded_modules() |
| 829 |
|
| 830 |
with self.subTest(module="main"): |
| 831 |
module = self.prog.main_module() |
| 832 |
module.try_file(get_resource("crashme_static_pie")) |
| 833 |
self.assertEqual(module.loaded_file_bias, 0x7FD981DC9000) |
| 834 |
self.assertEqual(module.debug_file_bias, 0x7FD981DC9000) |
| 835 |
|
| 836 |
with self.subTest(module="vdso"): |
| 837 |
module = self.prog.vdso_module("linux-vdso.so.1", 0x7FD981DC7438) |
| 838 |
self._try_vdso_in_core(module) |
| 839 |
self.assertEqual(module.loaded_file_bias, 0x7FD981DC7000) |
| 840 |
self.assertIsNone(module.debug_file_bias) |
| 841 |
|
| 842 |
def test_loaded_modules_pie_no_headers(self): |
| 843 |
self.prog.set_core_dump(get_resource("crashme_pie_no_headers.core")) |
| 844 |
|
| 845 |
loaded_modules = [] |
| 846 |
for module, new in self.prog.loaded_modules(): |
| 847 |
self.assertEqual(new, True) |
| 848 |
loaded_modules.append(module) |
| 849 |
found_modules = [] |
| 850 |
|
| 851 |
# Without ELF headers saved in the core dump, and without the main ELF |
| 852 |
# file, only the main module (with limited information) and vDSO can be |
| 853 |
# found. |
| 854 |
with self.subTest(module="main"): |
| 855 |
module = self.prog.main_module() |
| 856 |
found_modules.append(module) |
| 857 |
self.assertEqual(module.name, "/home/osandov/crashme_pie") |
| 858 |
self.assertIsNone(module.address_range) |
| 859 |
self.assertIsNone(module.build_id) |
| 860 |
|
| 861 |
with self.subTest(module="vdso"): |
| 862 |
module = self.prog.vdso_module("linux-vdso.so.1", 0x7F299F607438) |
| 863 |
found_modules.append(module) |
| 864 |
self.assertEqual(module.address_range, (0x7F299F607000, 0x7F299F60890F)) |
| 865 |
self.assertEqual( |
| 866 |
module.build_id.hex(), "fdc3e4d463911345fbc6d9cc432e5ebc276e8e03" |
| 867 |
) |
| 868 |
|
| 869 |
self.assertCountEqual(loaded_modules, found_modules) |
| 870 |
|
| 871 |
loaded_modules = [] |
| 872 |
for module, new in self.prog.loaded_modules(): |
| 873 |
self.assertEqual(new, False) |
| 874 |
loaded_modules.append(module) |
| 875 |
self.assertCountEqual(loaded_modules, found_modules) |
| 876 |
|
| 877 |
# If we can read the file headers (specifically, the program header |
| 878 |
# table and the interpreter path), then we should be able to get all of |
| 879 |
# the modules (with limited information). |
| 880 |
exe_file = self.enterContext(open(get_resource("crashme_pie"), "rb")) |
| 881 |
|
| 882 |
def read_headers(address, count, offset, physical): |
| 883 |
exe_file.seek(offset) |
| 884 |
return exe_file.read(count) |
| 885 |
|
| 886 |
self.prog.add_memory_segment(0x5623363D6000, 4096, read_headers, False) |
| 887 |
|
| 888 |
old_loaded_modules = [] |
| 889 |
new_loaded_modules = [] |
| 890 |
for module, new in self.prog.loaded_modules(): |
| 891 |
(new_loaded_modules if new else old_loaded_modules).append(module) |
| 892 |
new_found_modules = [] |
| 893 |
|
| 894 |
with self.subTest(module="main2"): |
| 895 |
module = self.prog.main_module() |
| 896 |
self.assertIsNone(module.address_range) |
| 897 |
self.assertIsNone(module.build_id) |
| 898 |
|
| 899 |
with self.subTest(module="crashme"): |
| 900 |
module = self.prog.shared_library_module( |
| 901 |
"/home/osandov/crashme.so", 0x7F299F5FFE08 |
| 902 |
) |
| 903 |
new_found_modules.append(module) |
| 904 |
self.assertIsNone(module.address_range) |
| 905 |
self.assertIsNone(module.build_id) |
| 906 |
|
| 907 |
with self.subTest(module="libc"): |
| 908 |
module = self.prog.shared_library_module("/lib64/libc.so.6", 0x7F299F5E7960) |
| 909 |
new_found_modules.append(module) |
| 910 |
self.assertIsNone(module.address_range) |
| 911 |
self.assertIsNone(module.build_id) |
| 912 |
|
| 913 |
with self.subTest(module="ld-linux"): |
| 914 |
module = self.prog.shared_library_module( |
| 915 |
"/lib64/ld-linux-x86-64.so.2", 0x7F299F63DE68 |
| 916 |
) |
| 917 |
new_found_modules.append(module) |
| 918 |
self.assertIsNone(module.address_range) |
| 919 |
self.assertIsNone(module.build_id) |
| 920 |
|
| 921 |
self.assertCountEqual(old_loaded_modules, loaded_modules) |
| 922 |
self.assertCountEqual(new_loaded_modules, new_found_modules) |
| 923 |
|
| 924 |
|
| 925 |
class TestLoadDebugInfo(TestCase): |
| 926 |
def setUp(self): |
| 927 |
self.prog = Program() |
| 928 |
self.prog.set_core_dump(get_resource("crashme.core")) |
| 929 |
self.prog.set_enabled_debug_info_finders([]) |
| 930 |
self.finder = unittest.mock.Mock() |
| 931 |
self.prog.register_debug_info_finder("mock", self.finder, enable_index=0) |
| 932 |
|
| 933 |
def test_nothing(self): |
| 934 |
self.prog.load_debug_info(None, default=False, main=False) |
| 935 |
self.assertFalse(list(self.prog.modules())) |
| 936 |
self.finder.assert_not_called() |
| 937 |
|
| 938 |
def test_empty_list(self): |
| 939 |
self.prog.load_debug_info([], default=False, main=False) |
| 940 |
self.assertFalse(list(self.prog.modules())) |
| 941 |
self.finder.assert_not_called() |
| 942 |
|
| 943 |
def test_no_such_file(self): |
| 944 |
with tempfile.TemporaryDirectory() as tmp_dir: |
| 945 |
self.prog.load_debug_info([Path(tmp_dir) / "file"]) |
| 946 |
self.assertFalse(list(self.prog.modules())) |
| 947 |
self.finder.assert_not_called() |
| 948 |
|
| 949 |
def test_not_elf(self): |
| 950 |
with tempfile.NamedTemporaryFile() as f: |
| 951 |
f.write(b"hello, world\n") |
| 952 |
f.flush() |
| 953 |
self.prog.load_debug_info([f.name]) |
| 954 |
self.assertFalse(list(self.prog.modules())) |
| 955 |
self.finder.assert_not_called() |
| 956 |
|
| 957 |
def test_no_build_id(self): |
| 958 |
with NamedTemporaryElfFile() as f: |
| 959 |
self.prog.load_debug_info([f.name]) |
| 960 |
self.assertFalse(list(self.prog.modules())) |
| 961 |
self.finder.assert_not_called() |
| 962 |
|
| 963 |
def test_only_main_path(self): |
| 964 |
crashme_path = get_resource("crashme") |
| 965 |
|
| 966 |
self.prog.load_debug_info([crashme_path]) |
| 967 |
|
| 968 |
# The main module should be created. |
| 969 |
self.assertIn(self.prog.main_module(), list(self.prog.modules())) |
| 970 |
# The provided path should be used for the main module. |
| 971 |
self.assertEqual( |
| 972 |
self.prog.main_module().loaded_file_path, |
| 973 |
str(crashme_path), |
| 974 |
) |
| 975 |
self.assertEqual( |
| 976 |
self.prog.main_module().debug_file_path, |
| 977 |
str(crashme_path), |
| 978 |
) |
| 979 |
# Finders shouldn't be called. |
| 980 |
self.finder.assert_not_called() |
| 981 |
|
| 982 |
def test_only_paths(self): |
| 983 |
crashme_path = get_resource("crashme") |
| 984 |
crashme_so_path = get_resource("crashme.so") |
| 985 |
|
| 986 |
self.prog.load_debug_info([crashme_path, crashme_so_path]) |
| 987 |
|
| 988 |
modules = list(self.prog.modules()) |
| 989 |
# All loaded modules should be created. |
| 990 |
self.assertEqual(len(modules), 5) |
| 991 |
# The provided files should be used for their respective modules. |
| 992 |
self.assertEqual( |
| 993 |
self.prog.main_module().loaded_file_path, |
| 994 |
str(crashme_path), |
| 995 |
) |
| 996 |
self.assertEqual( |
| 997 |
self.prog.main_module().debug_file_path, |
| 998 |
str(crashme_path), |
| 999 |
) |
| 1000 |
crashme_so_module = next( |
| 1001 |
module for module in modules if module.name == "/home/osandov/crashme.so" |
| 1002 |
) |
| 1003 |
self.assertEqual( |
| 1004 |
crashme_so_module.loaded_file_path, |
| 1005 |
str(crashme_so_path), |
| 1006 |
) |
| 1007 |
self.assertEqual( |
| 1008 |
crashme_so_module.debug_file_path, |
| 1009 |
str(crashme_so_path), |
| 1010 |
) |
| 1011 |
# The rest should not have a file. |
| 1012 |
for module in modules: |
| 1013 |
if module.name not in ("/home/osandov/crashme", "/home/osandov/crashme.so"): |
| 1014 |
self.assertEqual(module.loaded_file_status, ModuleFileStatus.WANT) |
| 1015 |
self.assertEqual(module.debug_file_status, ModuleFileStatus.WANT) |
| 1016 |
# Finders shouldn't be called. |
| 1017 |
self.finder.assert_not_called() |
| 1018 |
|
| 1019 |
def test_main_by_path(self): |
| 1020 |
crashme_path = get_resource("crashme") |
| 1021 |
|
| 1022 |
self.prog.load_debug_info([crashme_path], main=True) |
| 1023 |
|
| 1024 |
# The main module should be created. |
| 1025 |
self.assertIn(self.prog.main_module(), list(self.prog.modules())) |
| 1026 |
# The provided path should be used for the main module. |
| 1027 |
self.assertEqual( |
| 1028 |
self.prog.main_module().loaded_file_path, |
| 1029 |
str(crashme_path), |
| 1030 |
) |
| 1031 |
self.assertEqual( |
| 1032 |
self.prog.main_module().debug_file_path, |
| 1033 |
str(crashme_path), |
| 1034 |
) |
| 1035 |
# Finders shouldn't be called. |
| 1036 |
self.finder.assert_not_called() |
| 1037 |
|
| 1038 |
def test_main_by_finder(self): |
| 1039 |
crashme_path = get_resource("crashme") |
| 1040 |
|
| 1041 |
def finder(modules): |
| 1042 |
for module in modules: |
| 1043 |
if module.name == "/home/osandov/crashme": |
| 1044 |
module.try_file(crashme_path) |
| 1045 |
|
| 1046 |
self.finder.side_effect = finder |
| 1047 |
|
| 1048 |
self.prog.load_debug_info(main=True) |
| 1049 |
|
| 1050 |
# The main module should be created. |
| 1051 |
self.assertIn(self.prog.main_module(), list(self.prog.modules())) |
| 1052 |
# The finder should be called and set the file for the main module. |
| 1053 |
self.assertEqual( |
| 1054 |
self.prog.main_module().loaded_file_path, |
| 1055 |
str(crashme_path), |
| 1056 |
) |
| 1057 |
self.assertEqual( |
| 1058 |
self.prog.main_module().debug_file_path, |
| 1059 |
str(crashme_path), |
| 1060 |
) |
| 1061 |
self.finder.assert_called_once_with([self.prog.main_module()]) |
| 1062 |
|
| 1063 |
def test_default_by_paths(self): |
| 1064 |
crashme_path = get_resource("crashme") |
| 1065 |
crashme_so_path = get_resource("crashme.so") |
| 1066 |
|
| 1067 |
self.assertRaises( |
| 1068 |
MissingDebugInfoError, |
| 1069 |
self.prog.load_debug_info, |
| 1070 |
[crashme_path, crashme_so_path], |
| 1071 |
default=True, |
| 1072 |
) |
| 1073 |
|
| 1074 |
# All loaded modules should be created. |
| 1075 |
modules = list(self.prog.modules()) |
| 1076 |
self.assertEqual(len(modules), 5) |
| 1077 |
# The provided files should be used for their respective modules. |
| 1078 |
self.assertEqual( |
| 1079 |
self.prog.main_module().loaded_file_path, |
| 1080 |
str(crashme_path), |
| 1081 |
) |
| 1082 |
self.assertEqual( |
| 1083 |
self.prog.main_module().debug_file_path, |
| 1084 |
str(crashme_path), |
| 1085 |
) |
| 1086 |
crashme_so_module = next( |
| 1087 |
module for module in modules if module.name == "/home/osandov/crashme.so" |
| 1088 |
) |
| 1089 |
self.assertEqual( |
| 1090 |
crashme_so_module.loaded_file_path, |
| 1091 |
str(crashme_so_path), |
| 1092 |
) |
| 1093 |
self.assertEqual( |
| 1094 |
crashme_so_module.debug_file_path, |
| 1095 |
str(crashme_so_path), |
| 1096 |
) |
| 1097 |
# The rest should not have a file. |
| 1098 |
missing_modules = [] |
| 1099 |
for module in modules: |
| 1100 |
if module.name not in ("/home/osandov/crashme", "/home/osandov/crashme.so"): |
| 1101 |
self.assertEqual(module.loaded_file_status, ModuleFileStatus.WANT) |
| 1102 |
self.assertEqual(module.debug_file_status, ModuleFileStatus.WANT) |
| 1103 |
missing_modules.append(module) |
| 1104 |
self.assertEqual(len(missing_modules), 3) |
| 1105 |
# The finder should be called for the rest. |
| 1106 |
self.finder.assert_called_once() |
| 1107 |
self.assertCountEqual(self.finder.call_args[0][0], missing_modules) |
| 1108 |
|
| 1109 |
def test_default_by_finder(self): |
| 1110 |
crashme_path = get_resource("crashme") |
| 1111 |
crashme_so_path = get_resource("crashme.so") |
| 1112 |
|
| 1113 |
def finder(modules): |
| 1114 |
for module in modules: |
| 1115 |
if module.name == "/home/osandov/crashme": |
| 1116 |
module.try_file(crashme_path) |
| 1117 |
elif module.name == "/home/osandov/crashme.so": |
| 1118 |
module.try_file(crashme_so_path) |
| 1119 |
|
| 1120 |
self.finder.side_effect = finder |
| 1121 |
|
| 1122 |
self.assertRaises( |
| 1123 |
MissingDebugInfoError, self.prog.load_debug_info, default=True |
| 1124 |
) |
| 1125 |
|
| 1126 |
# All loaded modules should be created. |
| 1127 |
modules = list(self.prog.modules()) |
| 1128 |
self.assertEqual(len(modules), 5) |
| 1129 |
# The finder should be called and set the files for the matching |
| 1130 |
# modules. |
| 1131 |
self.assertEqual( |
| 1132 |
self.prog.main_module().loaded_file_path, |
| 1133 |
str(crashme_path), |
| 1134 |
) |
| 1135 |
self.assertEqual( |
| 1136 |
self.prog.main_module().debug_file_path, |
| 1137 |
str(crashme_path), |
| 1138 |
) |
| 1139 |
crashme_so_module = next( |
| 1140 |
module for module in modules if module.name == "/home/osandov/crashme.so" |
| 1141 |
) |
| 1142 |
self.assertEqual( |
| 1143 |
crashme_so_module.loaded_file_path, |
| 1144 |
str(crashme_so_path), |
| 1145 |
) |
| 1146 |
self.assertEqual( |
| 1147 |
crashme_so_module.debug_file_path, |
| 1148 |
str(crashme_so_path), |
| 1149 |
) |
| 1150 |
# The rest should not have a file. |
| 1151 |
for module in modules: |
| 1152 |
if module.name not in ("/home/osandov/crashme", "/home/osandov/crashme.so"): |
| 1153 |
self.assertEqual(module.loaded_file_status, ModuleFileStatus.WANT) |
| 1154 |
self.assertEqual(module.debug_file_status, ModuleFileStatus.WANT) |
| 1155 |
# The finder should be called for all loaded modules. |
| 1156 |
self.finder.assert_called_once() |
| 1157 |
self.assertCountEqual(self.finder.call_args[0][0], modules) |
| 1158 |
|
| 1159 |
def test_main_gnu_debugaltlink_by_path(self): |
| 1160 |
crashme_dwz_path = get_resource("crashme.dwz") |
| 1161 |
crashme_alt_path = get_resource("crashme.alt") |
| 1162 |
|
| 1163 |
self.prog.load_debug_info([crashme_dwz_path, crashme_alt_path], main=True) |
| 1164 |
|
| 1165 |
# The main module should be created. |
| 1166 |
self.assertIn(self.prog.main_module(), list(self.prog.modules())) |
| 1167 |
# The provided paths should be used for the main module. |
| 1168 |
self.assertEqual( |
| 1169 |
self.prog.main_module().loaded_file_path, |
| 1170 |
str(crashme_dwz_path), |
| 1171 |
) |
| 1172 |
self.assertEqual( |
| 1173 |
self.prog.main_module().debug_file_path, |
| 1174 |
str(crashme_dwz_path), |
| 1175 |
) |
| 1176 |
self.assertEqual( |
| 1177 |
self.prog.main_module().supplementary_debug_file_path, |
| 1178 |
str(crashme_alt_path), |
| 1179 |
) |
| 1180 |
# Finders shouldn't be called. |
| 1181 |
self.finder.assert_not_called() |
| 1182 |
|
| 1183 |
def test_main_gnu_debugaltlink_by_finder(self): |
| 1184 |
crashme_dwz_path = get_resource("crashme.dwz") |
| 1185 |
crashme_alt_path = get_resource("crashme.alt") |
| 1186 |
|
| 1187 |
def finder(modules): |
| 1188 |
for module in modules: |
| 1189 |
if module.name == "/home/osandov/crashme": |
| 1190 |
module.try_file(crashme_dwz_path) |
| 1191 |
module.try_file(crashme_alt_path) |
| 1192 |
|
| 1193 |
self.finder.side_effect = finder |
| 1194 |
|
| 1195 |
self.prog.load_debug_info(main=True) |
| 1196 |
|
| 1197 |
# The main module should be created. |
| 1198 |
self.assertIn(self.prog.main_module(), list(self.prog.modules())) |
| 1199 |
# The finder should be called and set the files for the main module. |
| 1200 |
self.assertEqual( |
| 1201 |
self.prog.main_module().loaded_file_path, |
| 1202 |
str(crashme_dwz_path), |
| 1203 |
) |
| 1204 |
self.assertEqual( |
| 1205 |
self.prog.main_module().debug_file_path, |
| 1206 |
str(crashme_dwz_path), |
| 1207 |
) |
| 1208 |
self.assertEqual( |
| 1209 |
self.prog.main_module().supplementary_debug_file_path, |
| 1210 |
str(crashme_alt_path), |
| 1211 |
) |
| 1212 |
self.finder.assert_called_once_with([self.prog.main_module()]) |
| 1213 |
|
| 1214 |
def test_main_by_path_gnu_debugaltlink_not_found(self): |
| 1215 |
crashme_dwz_path = get_resource("crashme.dwz") |
| 1216 |
|
| 1217 |
def finder(modules): |
| 1218 |
for module in modules: |
| 1219 |
if module.name == "/home/osandov/crashme": |
| 1220 |
self.assertEqual( |
| 1221 |
module.debug_file_status, ModuleFileStatus.WANT_SUPPLEMENTARY |
| 1222 |
) |
| 1223 |
|
| 1224 |
self.finder.side_effect = finder |
| 1225 |
|
| 1226 |
self.assertRaises( |
| 1227 |
MissingDebugInfoError, |
| 1228 |
self.prog.load_debug_info, |
| 1229 |
[crashme_dwz_path], |
| 1230 |
main=True, |
| 1231 |
) |
| 1232 |
|
| 1233 |
# The main module should be created. |
| 1234 |
self.assertIn(self.prog.main_module(), list(self.prog.modules())) |
| 1235 |
# The provided path should be used for the loaded file. |
| 1236 |
self.assertEqual( |
| 1237 |
self.prog.main_module().loaded_file_path, |
| 1238 |
str(crashme_dwz_path), |
| 1239 |
) |
| 1240 |
# The finder should be called and fail to find the supplementary file |
| 1241 |
# for the main module. |
| 1242 |
self.assertEqual( |
| 1243 |
self.prog.main_module().debug_file_status, |
| 1244 |
ModuleFileStatus.WANT_SUPPLEMENTARY, |
| 1245 |
) |
| 1246 |
self.assertEqual( |
| 1247 |
self.prog.main_module().wanted_supplementary_debug_file()[:3], |
| 1248 |
( |
| 1249 |
SupplementaryFileKind.GNU_DEBUGALTLINK, |
| 1250 |
str(crashme_dwz_path), |
| 1251 |
"crashme.alt", |
| 1252 |
), |
| 1253 |
) |
| 1254 |
self.finder.assert_called_once_with([self.prog.main_module()]) |
| 1255 |
|
| 1256 |
def test_main_by_finder_gnu_debugaltlink_not_found(self): |
| 1257 |
crashme_dwz_path = get_resource("crashme.dwz") |
| 1258 |
|
| 1259 |
def finder(modules): |
| 1260 |
for module in modules: |
| 1261 |
if module.name == "/home/osandov/crashme": |
| 1262 |
self.assertEqual(module.debug_file_status, ModuleFileStatus.WANT) |
| 1263 |
module.try_file(crashme_dwz_path) |
| 1264 |
self.assertEqual( |
| 1265 |
module.debug_file_status, ModuleFileStatus.WANT_SUPPLEMENTARY |
| 1266 |
) |
| 1267 |
|
| 1268 |
self.finder.side_effect = finder |
| 1269 |
|
| 1270 |
self.assertRaises(MissingDebugInfoError, self.prog.load_debug_info, main=True) |
| 1271 |
|
| 1272 |
# The main module should be created. |
| 1273 |
self.assertIn(self.prog.main_module(), list(self.prog.modules())) |
| 1274 |
# The finder should be called and set the loaded file for the main |
| 1275 |
# module but fail to find the supplementary file. |
| 1276 |
self.assertEqual( |
| 1277 |
self.prog.main_module().loaded_file_path, |
| 1278 |
str(crashme_dwz_path), |
| 1279 |
) |
| 1280 |
self.assertEqual( |
| 1281 |
self.prog.main_module().debug_file_status, |
| 1282 |
ModuleFileStatus.WANT_SUPPLEMENTARY, |
| 1283 |
) |
| 1284 |
self.assertEqual( |
| 1285 |
self.prog.main_module().wanted_supplementary_debug_file()[:3], |
| 1286 |
( |
| 1287 |
SupplementaryFileKind.GNU_DEBUGALTLINK, |
| 1288 |
str(crashme_dwz_path), |
| 1289 |
"crashme.alt", |
| 1290 |
), |
| 1291 |
) |
| 1292 |
self.finder.assert_called_once_with([self.prog.main_module()]) |
| 1293 |
|
| 1294 |
def test_main_by_path_gnu_debugaltlink_by_finder(self): |
| 1295 |
crashme_dwz_path = get_resource("crashme.dwz") |
| 1296 |
crashme_alt_path = get_resource("crashme.alt") |
| 1297 |
|
| 1298 |
def finder(modules): |
| 1299 |
for module in modules: |
| 1300 |
if ( |
| 1301 |
module.name == "/home/osandov/crashme" |
| 1302 |
and module.debug_file_status == ModuleFileStatus.WANT_SUPPLEMENTARY |
| 1303 |
): |
| 1304 |
module.try_file(crashme_alt_path) |
| 1305 |
|
| 1306 |
self.finder.side_effect = finder |
| 1307 |
|
| 1308 |
self.prog.load_debug_info([crashme_dwz_path], main=True) |
| 1309 |
|
| 1310 |
# The main module should be created. |
| 1311 |
self.assertIn(self.prog.main_module(), list(self.prog.modules())) |
| 1312 |
# The provided path should be used for the main module. |
| 1313 |
self.assertEqual( |
| 1314 |
self.prog.main_module().loaded_file_path, |
| 1315 |
str(crashme_dwz_path), |
| 1316 |
) |
| 1317 |
self.assertEqual( |
| 1318 |
self.prog.main_module().debug_file_path, |
| 1319 |
str(crashme_dwz_path), |
| 1320 |
) |
| 1321 |
# The finder should be called and set the supplementary file for the |
| 1322 |
# main module. |
| 1323 |
self.assertEqual( |
| 1324 |
self.prog.main_module().supplementary_debug_file_path, |
| 1325 |
str(crashme_alt_path), |
| 1326 |
) |
| 1327 |
self.finder.assert_called_once_with([self.prog.main_module()]) |
| 1328 |
|
| 1329 |
def test_main_by_finder_gnu_debugaltlink_by_path(self): |
| 1330 |
crashme_dwz_path = get_resource("crashme.dwz") |
| 1331 |
crashme_alt_path = get_resource("crashme.alt") |
| 1332 |
|
| 1333 |
def finder(modules): |
| 1334 |
for module in modules: |
| 1335 |
if module.name == "/home/osandov/crashme": |
| 1336 |
module.try_file(crashme_dwz_path) |
| 1337 |
|
| 1338 |
self.finder.side_effect = finder |
| 1339 |
|
| 1340 |
self.prog.load_debug_info([crashme_alt_path], main=True) |
| 1341 |
|
| 1342 |
# The provided path should be used for the supplementary file for the |
| 1343 |
# main module. |
| 1344 |
self.assertEqual( |
| 1345 |
self.prog.main_module().supplementary_debug_file_path, |
| 1346 |
str(crashme_alt_path), |
| 1347 |
) |
| 1348 |
# The finder should be called and set the file for the main module. |
| 1349 |
self.assertEqual( |
| 1350 |
self.prog.main_module().loaded_file_path, |
| 1351 |
str(crashme_dwz_path), |
| 1352 |
) |
| 1353 |
self.assertEqual( |
| 1354 |
self.prog.main_module().debug_file_path, |
| 1355 |
str(crashme_dwz_path), |
| 1356 |
) |
| 1357 |
self.finder.assert_called_once_with([self.prog.main_module()]) |
| 1358 |
|
| 1359 |
def test_main_wants_gnu_debugaltlink_by_path(self): |
| 1360 |
crashme_dwz_path = get_resource("crashme.dwz") |
| 1361 |
crashme_alt_path = get_resource("crashme.alt") |
| 1362 |
|
| 1363 |
for module, _ in self.prog.loaded_modules(): |
| 1364 |
if isinstance(module, MainModule): |
| 1365 |
module.try_file(crashme_dwz_path) |
| 1366 |
break |
| 1367 |
self.assertEqual( |
| 1368 |
self.prog.main_module().loaded_file_path, |
| 1369 |
str(crashme_dwz_path), |
| 1370 |
) |
| 1371 |
self.assertEqual( |
| 1372 |
self.prog.main_module().debug_file_status, |
| 1373 |
ModuleFileStatus.WANT_SUPPLEMENTARY, |
| 1374 |
) |
| 1375 |
|
| 1376 |
self.prog.load_debug_info([crashme_alt_path], main=True) |
| 1377 |
|
| 1378 |
# The main module should be created. |
| 1379 |
self.assertIn(self.prog.main_module(), list(self.prog.modules())) |
| 1380 |
# The provided path should be used for the supplementary file. |
| 1381 |
self.assertEqual( |
| 1382 |
self.prog.main_module().supplementary_debug_file_path, |
| 1383 |
str(crashme_alt_path), |
| 1384 |
) |
| 1385 |
# Finders shouldn't be called. |
| 1386 |
self.finder.assert_not_called() |
| 1387 |
|
| 1388 |
def test_main_wants_gnu_debugaltlink_by_finder(self): |
| 1389 |
crashme_dwz_path = get_resource("crashme.dwz") |
| 1390 |
crashme_alt_path = get_resource("crashme.alt") |
| 1391 |
|
| 1392 |
for module, _ in self.prog.loaded_modules(): |
| 1393 |
if isinstance(module, MainModule): |
| 1394 |
module.try_file(crashme_dwz_path) |
| 1395 |
break |
| 1396 |
self.assertEqual( |
| 1397 |
self.prog.main_module().loaded_file_path, |
| 1398 |
str(crashme_dwz_path), |
| 1399 |
) |
| 1400 |
self.assertEqual( |
| 1401 |
self.prog.main_module().debug_file_status, |
| 1402 |
ModuleFileStatus.WANT_SUPPLEMENTARY, |
| 1403 |
) |
| 1404 |
|
| 1405 |
def finder(modules): |
| 1406 |
for module in modules: |
| 1407 |
if ( |
| 1408 |
module.name == "/home/osandov/crashme" |
| 1409 |
and module.debug_file_status == ModuleFileStatus.WANT_SUPPLEMENTARY |
| 1410 |
): |
| 1411 |
module.try_file(crashme_alt_path) |
| 1412 |
|
| 1413 |
self.finder.side_effect = finder |
| 1414 |
|
| 1415 |
self.prog.load_debug_info(main=True) |
| 1416 |
|
| 1417 |
# The main module should be created. |
| 1418 |
self.assertIn(self.prog.main_module(), list(self.prog.modules())) |
| 1419 |
# The finder should be called and set the supplementary file for the |
| 1420 |
# main module. |
| 1421 |
self.assertEqual( |
| 1422 |
self.prog.main_module().supplementary_debug_file_path, |
| 1423 |
str(crashme_alt_path), |
| 1424 |
) |
| 1425 |
self.finder.assert_called_once_with([self.prog.main_module()]) |
| 1426 |
|
| 1427 |
def test_main_wants_gnu_debugaltlink_not_found(self): |
| 1428 |
crashme_dwz_path = get_resource("crashme.dwz") |
| 1429 |
|
| 1430 |
for module, _ in self.prog.loaded_modules(): |
| 1431 |
if isinstance(module, MainModule): |
| 1432 |
module.try_file(crashme_dwz_path) |
| 1433 |
break |
| 1434 |
self.assertEqual( |
| 1435 |
self.prog.main_module().loaded_file_path, |
| 1436 |
str(crashme_dwz_path), |
| 1437 |
) |
| 1438 |
self.assertEqual( |
| 1439 |
self.prog.main_module().debug_file_status, |
| 1440 |
ModuleFileStatus.WANT_SUPPLEMENTARY, |
| 1441 |
) |
| 1442 |
|
| 1443 |
self.assertRaises(MissingDebugInfoError, self.prog.load_debug_info, main=True) |
| 1444 |
|
| 1445 |
# The main module should be created. |
| 1446 |
self.assertIn(self.prog.main_module(), list(self.prog.modules())) |
| 1447 |
# The finder should be called and fail to find the supplementary file |
| 1448 |
# for the main module, but the supplementary file should still be |
| 1449 |
# wanted. |
| 1450 |
self.assertEqual( |
| 1451 |
self.prog.main_module().debug_file_status, |
| 1452 |
ModuleFileStatus.WANT_SUPPLEMENTARY, |
| 1453 |
) |
| 1454 |
self.finder.assert_called_once_with([self.prog.main_module()]) |
| 1455 |
|
| 1456 |
def test_default_gnu_debugaltlink_by_paths(self): |
| 1457 |
crashme_dwz_path = get_resource("crashme.dwz") |
| 1458 |
crashme_so_dwz_path = get_resource("crashme.so.dwz") |
| 1459 |
crashme_alt_path = get_resource("crashme.alt") |
| 1460 |
|
| 1461 |
self.assertRaises( |
| 1462 |
MissingDebugInfoError, |
| 1463 |
self.prog.load_debug_info, |
| 1464 |
[crashme_dwz_path, crashme_so_dwz_path, crashme_alt_path], |
| 1465 |
default=True, |
| 1466 |
) |
| 1467 |
|
| 1468 |
# All loaded modules should be created. |
| 1469 |
modules = list(self.prog.modules()) |
| 1470 |
self.assertEqual(len(modules), 5) |
| 1471 |
# The provided files should be used for their respective modules. |
| 1472 |
self.assertEqual( |
| 1473 |
self.prog.main_module().loaded_file_path, |
| 1474 |
str(crashme_dwz_path), |
| 1475 |
) |
| 1476 |
self.assertEqual( |
| 1477 |
self.prog.main_module().debug_file_path, |
| 1478 |
str(crashme_dwz_path), |
| 1479 |
) |
| 1480 |
self.assertEqual( |
| 1481 |
self.prog.main_module().supplementary_debug_file_path, |
| 1482 |
str(crashme_alt_path), |
| 1483 |
) |
| 1484 |
crashme_so_module = next( |
| 1485 |
module for module in modules if module.name == "/home/osandov/crashme.so" |
| 1486 |
) |
| 1487 |
self.assertEqual( |
| 1488 |
crashme_so_module.loaded_file_path, |
| 1489 |
str(crashme_so_dwz_path), |
| 1490 |
) |
| 1491 |
self.assertEqual( |
| 1492 |
crashme_so_module.debug_file_path, |
| 1493 |
str(crashme_so_dwz_path), |
| 1494 |
) |
| 1495 |
self.assertEqual( |
| 1496 |
crashme_so_module.supplementary_debug_file_path, |
| 1497 |
str(crashme_alt_path), |
| 1498 |
) |
| 1499 |
# The rest should not have a file. |
| 1500 |
missing_modules = [] |
| 1501 |
for module in modules: |
| 1502 |
if module.name not in ("/home/osandov/crashme", "/home/osandov/crashme.so"): |
| 1503 |
self.assertEqual(module.loaded_file_status, ModuleFileStatus.WANT) |
| 1504 |
self.assertEqual(module.debug_file_status, ModuleFileStatus.WANT) |
| 1505 |
missing_modules.append(module) |
| 1506 |
self.assertEqual(len(missing_modules), 3) |
| 1507 |
# The finder should be called for the rest. |
| 1508 |
self.finder.assert_called_once() |
| 1509 |
self.assertCountEqual(self.finder.call_args[0][0], missing_modules) |
| 1510 |
|
| 1511 |
def test_dont_want(self): |
| 1512 |
for module, _ in self.prog.loaded_modules(): |
| 1513 |
if isinstance(module, MainModule): |
| 1514 |
module.loaded_file_status = ModuleFileStatus.DONT_WANT |
| 1515 |
module.debug_file_status = ModuleFileStatus.DONT_WANT |
| 1516 |
break |
| 1517 |
# DONT_WANT should be reset to WANT. |
| 1518 |
self.assertRaises(MissingDebugInfoError, self.prog.load_debug_info, main=True) |
| 1519 |
self.assertEqual(module.loaded_file_status, ModuleFileStatus.WANT) |
| 1520 |
self.assertEqual(module.debug_file_status, ModuleFileStatus.WANT) |
| 1521 |
self.finder.assert_called_once_with([self.prog.main_module()]) |
| 1522 |
|
| 1523 |
def test_dont_need(self): |
| 1524 |
for module, _ in self.prog.loaded_modules(): |
| 1525 |
if isinstance(module, MainModule): |
| 1526 |
module.loaded_file_status = ModuleFileStatus.DONT_NEED |
| 1527 |
module.debug_file_status = ModuleFileStatus.DONT_NEED |
| 1528 |
break |
| 1529 |
# DONT_NEED should be preserved. |
| 1530 |
self.prog.load_debug_info(main=True) |
| 1531 |
self.assertEqual(module.loaded_file_status, ModuleFileStatus.DONT_NEED) |
| 1532 |
self.assertEqual(module.debug_file_status, ModuleFileStatus.DONT_NEED) |
| 1533 |
self.finder.assert_not_called() |
| 1534 |
|
| 1535 |
def test_unmatched(self): |
| 1536 |
self.prog.load_debug_info([get_resource("crashme_static")]) |
| 1537 |
modules = list(self.prog.modules()) |
| 1538 |
# All loaded modules should be created. |
| 1539 |
self.assertEqual(len(modules), 5) |
| 1540 |
# None of them should have files. |
| 1541 |
for module in modules: |
| 1542 |
self.assertEqual(module.loaded_file_status, ModuleFileStatus.WANT) |
| 1543 |
self.assertEqual(module.debug_file_status, ModuleFileStatus.WANT) |
| 1544 |
self.finder.assert_not_called() |
| 1545 |
|
| 1546 |
|
| 1547 |
class TestLoadDebugInfoCoreNoHeaders(TestCase): |
| 1548 |
def setUp(self): |
| 1549 |
self.prog = Program() |
| 1550 |
self.prog.set_core_dump(get_resource("crashme_pie_no_headers.core")) |
| 1551 |
self.prog.set_enabled_debug_info_finders([]) |
| 1552 |
self.finder = unittest.mock.Mock() |
| 1553 |
self.prog.register_debug_info_finder("mock", self.finder, enable_index=0) |
| 1554 |
|
| 1555 |
def test_main_by_finder(self): |
| 1556 |
crashme_pie_path = get_resource("crashme_pie") |
| 1557 |
|
| 1558 |
def finder(modules): |
| 1559 |
for module in modules: |
| 1560 |
if module.name == "/home/osandov/crashme_pie": |
| 1561 |
module.try_file(crashme_pie_path) |
| 1562 |
|
| 1563 |
self.finder.side_effect = finder |
| 1564 |
|
| 1565 |
self.prog.load_debug_info(main=True) |
| 1566 |
|
| 1567 |
# The main module should be created. |
| 1568 |
self.assertIn(self.prog.main_module(), list(self.prog.modules())) |
| 1569 |
# The finder should be called and set the files, address range, and |
| 1570 |
# build ID for the main module. |
| 1571 |
self.assertEqual( |
| 1572 |
self.prog.main_module().loaded_file_path, |
| 1573 |
str(crashme_pie_path), |
| 1574 |
) |
| 1575 |
self.assertEqual( |
| 1576 |
self.prog.main_module().debug_file_path, |
| 1577 |
str(crashme_pie_path), |
| 1578 |
) |
| 1579 |
self.assertEqual( |
| 1580 |
self.prog.main_module().address_range, (0x5623363D6000, 0x5623363DA018) |
| 1581 |
) |
| 1582 |
self.assertEqual( |
| 1583 |
self.prog.main_module().build_id.hex(), |
| 1584 |
"eb4ad7aaded3815ab133a6d7784a2c95a4e52998", |
| 1585 |
) |
| 1586 |
self.finder.assert_called_once_with([self.prog.main_module()]) |
| 1587 |
|
| 1588 |
@unittest.expectedFailure # Issue #291 |
| 1589 |
def test_default_by_finder(self): |
| 1590 |
crashme_pie_path = get_resource("crashme_pie") |
| 1591 |
crashme_so_path = get_resource("crashme.so") |
| 1592 |
|
| 1593 |
def finder(modules): |
| 1594 |
for module in modules: |
| 1595 |
if module.name == "/home/osandov/crashme_pie": |
| 1596 |
module.try_file(crashme_pie_path) |
| 1597 |
elif module.name == "/home/osandov/crashme.so": |
| 1598 |
module.try_file(crashme_so_path) |
| 1599 |
else: |
| 1600 |
module.loaded_file_status = ModuleFileStatus.DONT_NEED |
| 1601 |
module.debug_file_status = ModuleFileStatus.DONT_NEED |
| 1602 |
|
| 1603 |
self.finder.side_effect = finder |
| 1604 |
|
| 1605 |
self.prog.load_debug_info(default=True) |
| 1606 |
|
| 1607 |
# All loaded modules should be created (except ld-linux.so; see |
| 1608 |
# tests.test_module.TestLinuxUserspaceCoreDump.test_loaded_modules_pie_no_headers). |
| 1609 |
self.assertCountEqual( |
| 1610 |
list(self.prog.modules()), |
| 1611 |
[ |
| 1612 |
self.prog.main_module(), |
| 1613 |
self.prog.vdso_module("linux-vdso.so.1", 0x7F299F607438), |
| 1614 |
self.prog.shared_library_module( |
| 1615 |
"/home/osandov/crashme.so", 0x7F299F5FFE08 |
| 1616 |
), |
| 1617 |
self.prog.shared_library_module("/lib64/libc.so.6", 0x7F299F5E7960), |
| 1618 |
self.prog.shared_library_module( |
| 1619 |
"/lib64/ld-linux-x86-64.so.2", 0x7F299F63DE68 |
| 1620 |
), |
| 1621 |
], |
| 1622 |
) |
| 1623 |
# The finder should be called and set the files, address range, and |
| 1624 |
# build ID for the main and crashme.so modules. |
| 1625 |
self.assertEqual( |
| 1626 |
self.prog.main_module().loaded_file_path, |
| 1627 |
str(crashme_pie_path), |
| 1628 |
) |
| 1629 |
self.assertEqual( |
| 1630 |
self.prog.main_module().debug_file_path, |
| 1631 |
str(crashme_pie_path), |
| 1632 |
) |
| 1633 |
self.assertEqual( |
| 1634 |
self.prog.main_module().address_range, (0x5623363D6000, 0x5623363DA018) |
| 1635 |
) |
| 1636 |
self.assertEqual( |
| 1637 |
self.prog.main_module().build_id.hex(), |
| 1638 |
"eb4ad7aaded3815ab133a6d7784a2c95a4e52998", |
| 1639 |
) |
| 1640 |
self.assertEqual( |
| 1641 |
self.prog.shared_library_module( |
| 1642 |
"/home/osandov/crashme.so", 0x7F299F5FFE08 |
| 1643 |
).loaded_file_path, |
| 1644 |
str(crashme_so_path), |
| 1645 |
) |
| 1646 |
self.assertEqual( |
| 1647 |
self.prog.shared_library_module( |
| 1648 |
"/home/osandov/crashme.so", 0x7F299F5FFE08 |
| 1649 |
).debug_file_path, |
| 1650 |
str(crashme_so_path), |
| 1651 |
) |
| 1652 |
self.assertEqual( |
| 1653 |
self.prog.shared_library_module( |
| 1654 |
"/home/osandov/crashme.so", 0x7F299F5FFE08 |
| 1655 |
).address_range, |
| 1656 |
(0x7F299F5FC000, 0x7F299F600010), |
| 1657 |
) |
| 1658 |
self.assertEqual( |
| 1659 |
self.prog.shared_library_module( |
| 1660 |
"/home/osandov/crashme.so", 0x7F299F5FFE08 |
| 1661 |
).build_id.hex(), |
| 1662 |
"7bd58f10e741c3c8fbcf2031aa65f830f933d616", |
| 1663 |
) |
| 1664 |
self.finder.assert_called() |
| 1665 |
|
| 1666 |
|
| 1667 |
class TestLoadModuleDebugInfo(TestCase): |
| 1668 |
def setUp(self): |
| 1669 |
self.prog = Program() |
| 1670 |
self.prog.set_enabled_debug_info_finders([]) |
| 1671 |
self.finder = unittest.mock.Mock() |
| 1672 |
self.prog.register_debug_info_finder("mock", self.finder, enable_index=0) |
| 1673 |
|
| 1674 |
def test_empty(self): |
| 1675 |
self.prog.load_module_debug_info() |
| 1676 |
self.finder.assert_not_called() |
| 1677 |
|
| 1678 |
def test_multiple(self): |
| 1679 |
self.prog.load_module_debug_info( |
| 1680 |
self.prog.extra_module("/foo/bar", create=True), |
| 1681 |
self.prog.extra_module("/foo/baz", create=True), |
| 1682 |
) |
| 1683 |
self.finder.assert_called_once() |
| 1684 |
self.assertCountEqual( |
| 1685 |
self.finder.call_args[0][0], |
| 1686 |
[ |
| 1687 |
self.prog.extra_module("/foo/bar"), |
| 1688 |
self.prog.extra_module("/foo/baz"), |
| 1689 |
], |
| 1690 |
) |
| 1691 |
|
| 1692 |
def test_wrong_program(self): |
| 1693 |
self.assertRaisesRegex( |
| 1694 |
ValueError, |
| 1695 |
"module from wrong program", |
| 1696 |
self.prog.load_module_debug_info, |
| 1697 |
self.prog.extra_module("/foo/bar", create=True), |
| 1698 |
Program().extra_module("/foo/baz", create=True), |
| 1699 |
) |
| 1700 |
|
| 1701 |
def test_type_error(self): |
| 1702 |
self.assertRaises( |
| 1703 |
TypeError, |
| 1704 |
self.prog.load_module_debug_info, |
| 1705 |
self.prog.extra_module("/foo/bar", create=True), |
| 1706 |
None, |
| 1707 |
) |
| 1708 |
|
| 1709 |
|
| 1710 |
class TestStandardDebugInfoFinder(TestCase): |
| 1711 |
def setUp(self): |
| 1712 |
self.prog = Program() |
| 1713 |
self.prog.debug_info_options.directories = () |
| 1714 |
self.prog.debug_info_options.debug_link_directories = () |
| 1715 |
self.prog.set_enabled_debug_info_finders(["standard"]) |
| 1716 |
|
| 1717 |
def test_by_module_name(self): |
| 1718 |
with NamedTemporaryElfFile() as f: |
| 1719 |
module = self.prog.extra_module(f.name, create=True) |
| 1720 |
self.prog.load_module_debug_info(module) |
| 1721 |
self.assertEqual(module.loaded_file_status, ModuleFileStatus.HAVE) |
| 1722 |
self.assertEqual(module.debug_file_status, ModuleFileStatus.HAVE) |
| 1723 |
self.assertEqual(module.loaded_file_path, f.name) |
| 1724 |
self.assertEqual(module.debug_file_path, f.name) |
| 1725 |
|
| 1726 |
def test_by_module_name_with_build_id(self): |
| 1727 |
build_id = b"\x01\x23\x45\x67\x89\xab\xcd\xef" |
| 1728 |
|
| 1729 |
with NamedTemporaryElfFile(build_id=build_id) as f: |
| 1730 |
module = self.prog.extra_module(f.name, create=True) |
| 1731 |
module.build_id = build_id |
| 1732 |
self.prog.load_module_debug_info(module) |
| 1733 |
self.assertEqual(module.loaded_file_status, ModuleFileStatus.HAVE) |
| 1734 |
self.assertEqual(module.debug_file_status, ModuleFileStatus.HAVE) |
| 1735 |
self.assertEqual(module.loaded_file_path, f.name) |
| 1736 |
self.assertEqual(module.debug_file_path, f.name) |
| 1737 |
|
| 1738 |
def test_by_module_name_missing_build_id(self): |
| 1739 |
build_id = b"\x01\x23\x45\x67\x89\xab\xcd\xef" |
| 1740 |
|
| 1741 |
with NamedTemporaryElfFile() as f: |
| 1742 |
module = self.prog.extra_module(f.name, create=True) |
| 1743 |
module.build_id = build_id |
| 1744 |
self.prog.load_module_debug_info(module) |
| 1745 |
self.assertEqual(module.loaded_file_status, ModuleFileStatus.WANT) |
| 1746 |
self.assertEqual(module.debug_file_status, ModuleFileStatus.WANT) |
| 1747 |
|
| 1748 |
def test_by_module_name_build_id_mismatch(self): |
| 1749 |
build_id = b"\x01\x23\x45\x67\x89\xab\xcd\xef" |
| 1750 |
|
| 1751 |
with NamedTemporaryElfFile(build_id=build_id[::-1]) as f: |
| 1752 |
module = self.prog.extra_module(f.name, create=True) |
| 1753 |
module.build_id = build_id |
| 1754 |
self.prog.load_module_debug_info(module) |
| 1755 |
self.assertEqual(module.loaded_file_status, ModuleFileStatus.WANT) |
| 1756 |
self.assertEqual(module.debug_file_status, ModuleFileStatus.WANT) |
| 1757 |
|
| 1758 |
def test_reuse_loaded_file(self): |
| 1759 |
with NamedTemporaryElfFile() as f: |
| 1760 |
module = self.prog.extra_module(f.name, create=True) |
| 1761 |
module.debug_file_status = ModuleFileStatus.DONT_WANT |
| 1762 |
self.prog.load_module_debug_info(module) |
| 1763 |
self.assertEqual(module.loaded_file_status, ModuleFileStatus.HAVE) |
| 1764 |
self.assertEqual(module.loaded_file_path, f.name) |
| 1765 |
self.assertEqual(module.debug_file_status, ModuleFileStatus.DONT_WANT) |
| 1766 |
|
| 1767 |
module.debug_file_status = ModuleFileStatus.WANT |
| 1768 |
self.prog.load_module_debug_info(module) |
| 1769 |
self.assertEqual(module.loaded_file_status, ModuleFileStatus.HAVE) |
| 1770 |
self.assertEqual(module.loaded_file_path, f.name) |
| 1771 |
self.assertEqual(module.debug_file_status, ModuleFileStatus.HAVE) |
| 1772 |
self.assertEqual(module.debug_file_path, f.name) |
| 1773 |
|
| 1774 |
def test_reuse_debug_file(self): |
| 1775 |
with NamedTemporaryElfFile() as f: |
| 1776 |
module = self.prog.extra_module(f.name, create=True) |
| 1777 |
module.loaded_file_status = ModuleFileStatus.DONT_WANT |
| 1778 |
self.prog.load_module_debug_info(module) |
| 1779 |
self.assertEqual(module.loaded_file_status, ModuleFileStatus.DONT_WANT) |
| 1780 |
self.assertEqual(module.debug_file_status, ModuleFileStatus.HAVE) |
| 1781 |
self.assertEqual(module.debug_file_path, f.name) |
| 1782 |
|
| 1783 |
module.loaded_file_status = ModuleFileStatus.WANT |
| 1784 |
self.prog.load_module_debug_info(module) |
| 1785 |
self.assertEqual(module.loaded_file_status, ModuleFileStatus.HAVE) |
| 1786 |
self.assertEqual(module.loaded_file_path, f.name) |
| 1787 |
self.assertEqual(module.debug_file_status, ModuleFileStatus.HAVE) |
| 1788 |
self.assertEqual(module.debug_file_path, f.name) |
| 1789 |
|
| 1790 |
def test_reuse_wanted_supplementary_debug_file(self): |
| 1791 |
alt_build_id = b"\xfe\xdc\xba\x98\x76\x54\x32\x10" |
| 1792 |
|
| 1793 |
with NamedTemporaryElfFile( |
| 1794 |
gnu_debugaltlink=("alt.debug", alt_build_id), |
| 1795 |
) as f: |
| 1796 |
module = self.prog.extra_module(f.name, create=True) |
| 1797 |
module.loaded_file_status = ModuleFileStatus.DONT_WANT |
| 1798 |
self.prog.load_module_debug_info(module) |
| 1799 |
self.assertEqual(module.loaded_file_status, ModuleFileStatus.DONT_WANT) |
| 1800 |
self.assertEqual(module.debug_file_status, ModuleFileStatus.WANT_SUPPLEMENTARY) |
| 1801 |
|
| 1802 |
module.loaded_file_status = ModuleFileStatus.WANT |
| 1803 |
self.prog.load_module_debug_info(module) |
| 1804 |
self.assertEqual(module.loaded_file_status, ModuleFileStatus.HAVE) |
| 1805 |
self.assertEqual(module.loaded_file_path, f.name) |
| 1806 |
self.assertEqual(module.debug_file_status, ModuleFileStatus.WANT_SUPPLEMENTARY) |
| 1807 |
|
| 1808 |
def test_vdso_in_core(self): |
| 1809 |
self.prog.set_core_dump(get_resource("crashme.core")) |
| 1810 |
for module, _ in self.prog.loaded_modules(): |
| 1811 |
if isinstance(module, VdsoModule): |
| 1812 |
break |
| 1813 |
else: |
| 1814 |
self.fail("vDSO module not found") |
| 1815 |
self.prog.load_module_debug_info(module) |
| 1816 |
self.assertEqual(module.loaded_file_status, ModuleFileStatus.HAVE) |
| 1817 |
self.assertEqual(module.loaded_file_path, "[vdso]") |
| 1818 |
self.assertEqual(module.debug_file_status, ModuleFileStatus.WANT) |
| 1819 |
|
| 1820 |
def test_main_by_proc(self): |
| 1821 |
self.prog.set_pid(os.getpid()) |
| 1822 |
for module, _ in self.prog.loaded_modules(): |
| 1823 |
if isinstance(module, MainModule): |
| 1824 |
break |
| 1825 |
else: |
| 1826 |
self.fail("main module not found") |
| 1827 |
self.prog.load_module_debug_info(module) |
| 1828 |
self.assertEqual(module.loaded_file_status, ModuleFileStatus.HAVE) |
| 1829 |
|
| 1830 |
def test_vdso_by_proc(self): |
| 1831 |
self.prog.set_pid(os.getpid()) |
| 1832 |
for module, _ in self.prog.loaded_modules(): |
| 1833 |
if isinstance(module, VdsoModule): |
| 1834 |
break |
| 1835 |
else: |
| 1836 |
self.skipTest("vDSO module not found") |
| 1837 |
self.prog.load_module_debug_info(module) |
| 1838 |
self.assertEqual(module.loaded_file_status, ModuleFileStatus.HAVE) |
| 1839 |
self.assertEqual(module.loaded_file_path, "[vdso]") |
| 1840 |
self.assertEqual(module.debug_file_status, ModuleFileStatus.WANT) |
| 1841 |
|
| 1842 |
def test_shared_library_by_proc(self): |
| 1843 |
self.prog.set_pid(os.getpid()) |
| 1844 |
for module, _ in self.prog.loaded_modules(): |
| 1845 |
if isinstance(module, SharedLibraryModule): |
| 1846 |
break |
| 1847 |
else: |
| 1848 |
self.skipTest("shared library module not found") |
| 1849 |
self.prog.load_module_debug_info(module) |
| 1850 |
self.assertEqual(module.loaded_file_status, ModuleFileStatus.HAVE) |
| 1851 |
|
| 1852 |
def test_by_build_id(self): |
| 1853 |
build_id = b"\x01\x23\x45\x67\x89\xab\xcd\xef" |
| 1854 |
|
| 1855 |
for i, relative in enumerate((False, True)): |
| 1856 |
with self.subTest(relative=relative): |
| 1857 |
with tempfile.TemporaryDirectory( |
| 1858 |
prefix="bin-" |
| 1859 |
) as bin_dir, tempfile.TemporaryDirectory(prefix="debug-") as debug_dir: |
| 1860 |
bin_dir = Path(bin_dir) |
| 1861 |
debug_dir = Path(debug_dir) |
| 1862 |
|
| 1863 |
build_id_dir = debug_dir / ".build-id" / build_id.hex()[:2] |
| 1864 |
build_id_dir.mkdir(parents=True) |
| 1865 |
binary_path = build_id_dir / build_id.hex()[2:] |
| 1866 |
binary_path.write_bytes( |
| 1867 |
create_dwarf_file((), sections=(ALLOCATED_SECTION,)) |
| 1868 |
) |
| 1869 |
|
| 1870 |
module = self.prog.extra_module(bin_dir / "binary", i, create=True) |
| 1871 |
module.build_id = build_id |
| 1872 |
|
| 1873 |
self.prog.debug_info_options.directories = ( |
| 1874 |
os.path.relpath(debug_dir) if relative else str(debug_dir), |
| 1875 |
) |
| 1876 |
self.prog.load_module_debug_info(module) |
| 1877 |
self.assertEqual(module.loaded_file_status, ModuleFileStatus.HAVE) |
| 1878 |
self.assertEqual(module.debug_file_status, ModuleFileStatus.HAVE) |
| 1879 |
self.assertEqual(module.loaded_file_path, str(binary_path)) |
| 1880 |
self.assertEqual(module.debug_file_path, str(binary_path)) |
| 1881 |
|
| 1882 |
def test_by_build_id_separate(self): |
| 1883 |
build_id = b"\x01\x23\x45\x67\x89\xab\xcd\xef" |
| 1884 |
|
| 1885 |
with tempfile.TemporaryDirectory( |
| 1886 |
prefix="bin-" |
| 1887 |
) as bin_dir, tempfile.TemporaryDirectory(prefix="debug-") as debug_dir: |
| 1888 |
bin_dir = Path(bin_dir) |
| 1889 |
debug_dir = Path(debug_dir) |
| 1890 |
|
| 1891 |
build_id_dir = debug_dir / ".build-id" / build_id.hex()[:2] |
| 1892 |
build_id_dir.mkdir(parents=True) |
| 1893 |
loadable_path = build_id_dir / build_id.hex()[2:] |
| 1894 |
loadable_path.write_bytes( |
| 1895 |
create_elf_file(ET.EXEC, sections=(ALLOCATED_SECTION,)) |
| 1896 |
) |
| 1897 |
debug_path = build_id_dir / (build_id.hex()[2:] + ".debug") |
| 1898 |
debug_path.write_bytes(create_dwarf_file(())) |
| 1899 |
|
| 1900 |
module = self.prog.extra_module(bin_dir / "binary", create=True) |
| 1901 |
module.build_id = build_id |
| 1902 |
|
| 1903 |
self.prog.debug_info_options.directories = (str(debug_dir),) |
| 1904 |
self.prog.load_module_debug_info(module) |
| 1905 |
self.assertEqual(module.loaded_file_status, ModuleFileStatus.HAVE) |
| 1906 |
self.assertEqual(module.debug_file_status, ModuleFileStatus.HAVE) |
| 1907 |
self.assertEqual(module.loaded_file_path, str(loadable_path)) |
| 1908 |
self.assertEqual(module.debug_file_path, str(debug_path)) |
| 1909 |
|
| 1910 |
def test_by_build_id_from_loaded(self): |
| 1911 |
build_id = b"\x01\x23\x45\x67\x89\xab\xcd\xef" |
| 1912 |
|
| 1913 |
with tempfile.TemporaryDirectory( |
| 1914 |
prefix="bin-" |
| 1915 |
) as bin_dir, tempfile.TemporaryDirectory(prefix="debug-") as debug_dir: |
| 1916 |
bin_dir = Path(bin_dir) |
| 1917 |
debug_dir = Path(debug_dir) |
| 1918 |
|
| 1919 |
loadable_path = bin_dir / "binary" |
| 1920 |
loadable_path.write_bytes( |
| 1921 |
create_elf_file( |
| 1922 |
ET.EXEC, sections=(ALLOCATED_SECTION,), build_id=build_id |
| 1923 |
) |
| 1924 |
) |
| 1925 |
build_id_dir = debug_dir / ".build-id" / build_id.hex()[:2] |
| 1926 |
build_id_dir.mkdir(parents=True) |
| 1927 |
debug_path = build_id_dir / (build_id.hex()[2:] + ".debug") |
| 1928 |
debug_path.write_bytes(create_dwarf_file(())) |
| 1929 |
|
| 1930 |
module = self.prog.extra_module(bin_dir / "binary", create=True) |
| 1931 |
|
| 1932 |
self.prog.debug_info_options.directories = (str(debug_dir),) |
| 1933 |
self.prog.load_module_debug_info(module) |
| 1934 |
self.assertEqual(module.loaded_file_status, ModuleFileStatus.HAVE) |
| 1935 |
self.assertEqual(module.debug_file_status, ModuleFileStatus.HAVE) |
| 1936 |
self.assertEqual(module.loaded_file_path, str(loadable_path)) |
| 1937 |
self.assertEqual(module.debug_file_path, str(debug_path)) |
| 1938 |
|
| 1939 |
def test_by_build_id_method(self): |
| 1940 |
build_id = b"\x01\x23\x45\x67\x89\xab\xcd\xef" |
| 1941 |
|
| 1942 |
with tempfile.TemporaryDirectory( |
| 1943 |
prefix="bin-" |
| 1944 |
) as bin_dir, tempfile.TemporaryDirectory(prefix="debug-") as debug_dir: |
| 1945 |
bin_dir = Path(bin_dir) |
| 1946 |
debug_dir = Path(debug_dir) |
| 1947 |
|
| 1948 |
build_id_dir = debug_dir / ".build-id" / build_id.hex()[:2] |
| 1949 |
build_id_dir.mkdir(parents=True) |
| 1950 |
binary_path = build_id_dir / build_id.hex()[2:] |
| 1951 |
binary_path.write_bytes( |
| 1952 |
create_dwarf_file((), sections=(ALLOCATED_SECTION,)) |
| 1953 |
) |
| 1954 |
|
| 1955 |
module = self.prog.extra_module(bin_dir / "binary", create=True) |
| 1956 |
module.build_id = build_id |
| 1957 |
|
| 1958 |
self.prog.find_standard_debug_info( |
| 1959 |
[module], |
| 1960 |
options=DebugInfoOptions(directories=(str(debug_dir),)), |
| 1961 |
) |
| 1962 |
self.assertEqual(module.loaded_file_status, ModuleFileStatus.HAVE) |
| 1963 |
self.assertEqual(module.debug_file_status, ModuleFileStatus.HAVE) |
| 1964 |
self.assertEqual(module.loaded_file_path, str(binary_path)) |
| 1965 |
self.assertEqual(module.debug_file_path, str(binary_path)) |
| 1966 |
|
| 1967 |
def test_by_gnu_debuglink(self): |
| 1968 |
with tempfile.TemporaryDirectory( |
| 1969 |
prefix="bin-" |
| 1970 |
) as bin_dir, tempfile.TemporaryDirectory(prefix="debug-") as debug_dir: |
| 1971 |
bin_dir = Path(bin_dir) |
| 1972 |
debug_dir = Path(debug_dir) |
| 1973 |
|
| 1974 |
debug_file_contents = create_dwarf_file(()) |
| 1975 |
crc = binascii.crc32(debug_file_contents) |
| 1976 |
|
| 1977 |
loadable_path = bin_dir / "binary" |
| 1978 |
loadable_path.write_bytes( |
| 1979 |
create_elf_file( |
| 1980 |
ET.EXEC, |
| 1981 |
sections=(ALLOCATED_SECTION,), |
| 1982 |
gnu_debuglink=("binary.debug", crc), |
| 1983 |
) |
| 1984 |
) |
| 1985 |
|
| 1986 |
self.prog.debug_info_options.directories = (str(debug_dir),) |
| 1987 |
self.prog.debug_info_options.debug_link_directories = ( |
| 1988 |
"$ORIGIN", |
| 1989 |
"$ORIGIN/.debug", |
| 1990 |
"", |
| 1991 |
) |
| 1992 |
for i, debug_path in enumerate( |
| 1993 |
( |
| 1994 |
bin_dir / "binary.debug", |
| 1995 |
bin_dir / ".debug" / "binary.debug", |
| 1996 |
debug_dir / bin_dir.relative_to("/") / "binary.debug", |
| 1997 |
) |
| 1998 |
): |
| 1999 |
with self.subTest(debug_path=debug_path): |
| 2000 |
try: |
| 2001 |
debug_path.parent.mkdir(parents=True, exist_ok=True) |
| 2002 |
debug_path.write_bytes(debug_file_contents) |
| 2003 |
|
| 2004 |
module = self.prog.extra_module( |
| 2005 |
bin_dir / "binary", i, create=True |
| 2006 |
) |
| 2007 |
|
| 2008 |
self.prog.load_module_debug_info(module) |
| 2009 |
self.assertEqual( |
| 2010 |
module.loaded_file_status, ModuleFileStatus.HAVE |
| 2011 |
) |
| 2012 |
self.assertEqual( |
| 2013 |
module.debug_file_status, ModuleFileStatus.HAVE |
| 2014 |
) |
| 2015 |
self.assertEqual(module.loaded_file_path, str(loadable_path)) |
| 2016 |
self.assertEqual(module.debug_file_path, str(debug_path)) |
| 2017 |
finally: |
| 2018 |
try: |
| 2019 |
debug_path.unlink() |
| 2020 |
except FileNotFoundError: |
| 2021 |
pass |
| 2022 |
|
| 2023 |
def test_by_gnu_debuglink_absolute(self): |
| 2024 |
with tempfile.TemporaryDirectory( |
| 2025 |
prefix="bin-" |
| 2026 |
) as bin_dir, tempfile.TemporaryDirectory(prefix="debug-") as debug_dir: |
| 2027 |
bin_dir = Path(bin_dir) |
| 2028 |
debug_dir = Path(debug_dir) |
| 2029 |
|
| 2030 |
debug_file_contents = create_dwarf_file(()) |
| 2031 |
crc = binascii.crc32(debug_file_contents) |
| 2032 |
debug_path = debug_dir / "binary.debug" |
| 2033 |
|
| 2034 |
loadable_path = bin_dir / "binary" |
| 2035 |
loadable_path.write_bytes( |
| 2036 |
create_elf_file( |
| 2037 |
ET.EXEC, |
| 2038 |
sections=(ALLOCATED_SECTION,), |
| 2039 |
gnu_debuglink=(debug_path, crc), |
| 2040 |
) |
| 2041 |
) |
| 2042 |
|
| 2043 |
debug_path.parent.mkdir(parents=True, exist_ok=True) |
| 2044 |
debug_path.write_bytes(debug_file_contents) |
| 2045 |
|
| 2046 |
module = self.prog.extra_module(bin_dir / "binary", create=True) |
| 2047 |
|
| 2048 |
self.prog.load_module_debug_info(module) |
| 2049 |
self.assertEqual(module.loaded_file_status, ModuleFileStatus.HAVE) |
| 2050 |
self.assertEqual(module.debug_file_status, ModuleFileStatus.HAVE) |
| 2051 |
self.assertEqual(module.loaded_file_path, str(loadable_path)) |
| 2052 |
self.assertEqual(module.debug_file_path, str(debug_path)) |
| 2053 |
|
| 2054 |
def test_by_gnu_debuglink_origin_with_braces(self): |
| 2055 |
with tempfile.TemporaryDirectory(prefix="bin-") as bin_dir: |
| 2056 |
bin_dir = Path(bin_dir) |
| 2057 |
|
| 2058 |
debug_file_contents = create_dwarf_file(()) |
| 2059 |
crc = binascii.crc32(debug_file_contents) |
| 2060 |
debug_path = bin_dir / "binary.debug" |
| 2061 |
|
| 2062 |
loadable_path = bin_dir / "binary" |
| 2063 |
loadable_path.write_bytes( |
| 2064 |
create_elf_file( |
| 2065 |
ET.EXEC, |
| 2066 |
sections=(ALLOCATED_SECTION,), |
| 2067 |
gnu_debuglink=("binary.debug", crc), |
| 2068 |
) |
| 2069 |
) |
| 2070 |
|
| 2071 |
debug_path.parent.mkdir(parents=True, exist_ok=True) |
| 2072 |
debug_path.write_bytes(debug_file_contents) |
| 2073 |
|
| 2074 |
module = self.prog.extra_module(bin_dir / "binary", create=True) |
| 2075 |
|
| 2076 |
self.prog.debug_info_options.debug_link_directories = ("${ORIGIN}",) |
| 2077 |
self.prog.load_module_debug_info(module) |
| 2078 |
self.assertEqual(module.loaded_file_status, ModuleFileStatus.HAVE) |
| 2079 |
self.assertEqual(module.debug_file_status, ModuleFileStatus.HAVE) |
| 2080 |
self.assertEqual(module.loaded_file_path, str(loadable_path)) |
| 2081 |
self.assertEqual(module.debug_file_path, str(debug_path)) |
| 2082 |
|
| 2083 |
def test_by_gnu_debuglink_not_origin(self): |
| 2084 |
# Test that strings other than $ORIGIN followed by a word boundary are |
| 2085 |
# not replaced. |
| 2086 |
for i, subdir in enumerate(("$ORIGINAL", "$foo", "$")): |
| 2087 |
with self.subTest(subdir=subdir): |
| 2088 |
with tempfile.TemporaryDirectory(prefix="bin-") as bin_dir: |
| 2089 |
bin_dir = Path(bin_dir) |
| 2090 |
debug_dir = bin_dir / subdir |
| 2091 |
|
| 2092 |
debug_file_contents = create_dwarf_file(()) |
| 2093 |
crc = binascii.crc32(debug_file_contents) |
| 2094 |
debug_path = debug_dir / "binary.debug" |
| 2095 |
|
| 2096 |
loadable_path = bin_dir / "binary" |
| 2097 |
loadable_path.write_bytes( |
| 2098 |
create_elf_file( |
| 2099 |
ET.EXEC, |
| 2100 |
sections=(ALLOCATED_SECTION,), |
| 2101 |
gnu_debuglink=("binary.debug", crc), |
| 2102 |
) |
| 2103 |
) |
| 2104 |
|
| 2105 |
debug_path.parent.mkdir(parents=True, exist_ok=True) |
| 2106 |
debug_path.write_bytes(debug_file_contents) |
| 2107 |
|
| 2108 |
module = self.prog.extra_module(bin_dir / "binary", i, create=True) |
| 2109 |
|
| 2110 |
self.prog.debug_info_options.debug_link_directories = ( |
| 2111 |
str(debug_dir), |
| 2112 |
) |
| 2113 |
self.prog.load_module_debug_info(module) |
| 2114 |
self.assertEqual(module.loaded_file_status, ModuleFileStatus.HAVE) |
| 2115 |
self.assertEqual(module.debug_file_status, ModuleFileStatus.HAVE) |
| 2116 |
self.assertEqual(module.loaded_file_path, str(loadable_path)) |
| 2117 |
self.assertEqual(module.debug_file_path, str(debug_path)) |
| 2118 |
|
| 2119 |
def test_by_gnu_debuglink_origin_multiple(self): |
| 2120 |
# Pathological case combining the cases above. |
| 2121 |
with tempfile.TemporaryDirectory(prefix="bin-") as bin_dir: |
| 2122 |
bin_dir = Path(bin_dir) |
| 2123 |
debug_dir = ( |
| 2124 |
bin_dir |
| 2125 |
/ "$ORIGINx" |
| 2126 |
/ (bin_dir.parent / (bin_dir.name + "x")).relative_to("/") |
| 2127 |
/ "$" |
| 2128 |
/ bin_dir.relative_to("/") |
| 2129 |
/ "$O" |
| 2130 |
) |
| 2131 |
|
| 2132 |
debug_file_contents = create_dwarf_file(()) |
| 2133 |
crc = binascii.crc32(debug_file_contents) |
| 2134 |
debug_path = debug_dir / "binary.debug" |
| 2135 |
|
| 2136 |
loadable_path = bin_dir / "binary" |
| 2137 |
loadable_path.write_bytes( |
| 2138 |
create_elf_file( |
| 2139 |
ET.EXEC, |
| 2140 |
sections=(ALLOCATED_SECTION,), |
| 2141 |
gnu_debuglink=("binary.debug", crc), |
| 2142 |
) |
| 2143 |
) |
| 2144 |
|
| 2145 |
debug_path.parent.mkdir(parents=True, exist_ok=True) |
| 2146 |
debug_path.write_bytes(debug_file_contents) |
| 2147 |
|
| 2148 |
module = self.prog.extra_module(bin_dir / "binary", create=True) |
| 2149 |
|
| 2150 |
self.prog.debug_info_options.debug_link_directories = ( |
| 2151 |
str(bin_dir) + "/$ORIGINx${ORIGIN}x/$$ORIGIN/$O", |
| 2152 |
) |
| 2153 |
self.prog.load_module_debug_info(module) |
| 2154 |
self.assertEqual(module.loaded_file_status, ModuleFileStatus.HAVE) |
| 2155 |
self.assertEqual(module.debug_file_status, ModuleFileStatus.HAVE) |
| 2156 |
self.assertEqual(module.loaded_file_path, str(loadable_path)) |
| 2157 |
self.assertEqual(module.debug_file_path, str(debug_path)) |
| 2158 |
|
| 2159 |
def test_by_gnu_debuglink_crc_mismatch(self): |
| 2160 |
with tempfile.TemporaryDirectory(prefix="bin-") as bin_dir: |
| 2161 |
bin_dir = Path(bin_dir) |
| 2162 |
|
| 2163 |
debug_file_contents = create_dwarf_file(()) |
| 2164 |
crc = binascii.crc32(debug_file_contents) |
| 2165 |
|
| 2166 |
loadable_path = bin_dir / "binary" |
| 2167 |
loadable_path.write_bytes( |
| 2168 |
create_elf_file( |
| 2169 |
ET.EXEC, |
| 2170 |
sections=(ALLOCATED_SECTION,), |
| 2171 |
gnu_debuglink=("binary.debug", crc ^ 1), |
| 2172 |
) |
| 2173 |
) |
| 2174 |
|
| 2175 |
debug_path = bin_dir / "binary.debug" |
| 2176 |
debug_path.write_bytes(debug_file_contents) |
| 2177 |
|
| 2178 |
module = self.prog.extra_module(bin_dir / "binary", create=True) |
| 2179 |
self.prog.debug_info_options.debug_link_directories = ("$ORIGIN",) |
| 2180 |
self.prog.load_module_debug_info(module) |
| 2181 |
self.assertEqual(module.debug_file_status, ModuleFileStatus.WANT) |
| 2182 |
|
| 2183 |
def test_invalid_gnu_debuglink(self): |
| 2184 |
with tempfile.TemporaryDirectory(prefix="bin-") as bin_dir: |
| 2185 |
bin_dir = Path(bin_dir) |
| 2186 |
|
| 2187 |
loadable_path = bin_dir / "binary" |
| 2188 |
loadable_path.write_bytes( |
| 2189 |
create_elf_file( |
| 2190 |
ET.EXEC, |
| 2191 |
sections=( |
| 2192 |
ALLOCATED_SECTION, |
| 2193 |
ElfSection( |
| 2194 |
name=".gnu_debuglink", sh_type=SHT.PROGBITS, data=b"foo" |
| 2195 |
), |
| 2196 |
), |
| 2197 |
) |
| 2198 |
) |
| 2199 |
|
| 2200 |
module = self.prog.extra_module(bin_dir / "binary", create=True) |
| 2201 |
self.prog.load_module_debug_info(module) |
| 2202 |
self.assertEqual(module.loaded_file_status, ModuleFileStatus.HAVE) |
| 2203 |
self.assertEqual(module.debug_file_status, ModuleFileStatus.WANT) |
| 2204 |
self.assertEqual(module.loaded_file_path, str(loadable_path)) |
| 2205 |
|
| 2206 |
def test_gnu_debugaltlink_absolute(self): |
| 2207 |
alt_build_id = b"\xfe\xdc\xba\x98\x76\x54\x32\x10" |
| 2208 |
|
| 2209 |
with tempfile.TemporaryDirectory( |
| 2210 |
prefix="bin-" |
| 2211 |
) as bin_dir, tempfile.TemporaryDirectory(prefix="debug-") as debug_dir: |
| 2212 |
bin_dir = Path(bin_dir) |
| 2213 |
debug_dir = Path(debug_dir) |
| 2214 |
|
| 2215 |
alt_path = debug_dir / "alt.debug" |
| 2216 |
alt_path.write_bytes(create_dwarf_file((), build_id=alt_build_id)) |
| 2217 |
|
| 2218 |
binary_path = bin_dir / "binary" |
| 2219 |
binary_path.write_bytes( |
| 2220 |
create_dwarf_file( |
| 2221 |
(), |
| 2222 |
sections=(ALLOCATED_SECTION,), |
| 2223 |
gnu_debugaltlink=(alt_path, alt_build_id), |
| 2224 |
) |
| 2225 |
) |
| 2226 |
|
| 2227 |
module = self.prog.extra_module(bin_dir / "binary", create=True) |
| 2228 |
self.prog.load_module_debug_info(module) |
| 2229 |
self.assertEqual(module.loaded_file_status, ModuleFileStatus.HAVE) |
| 2230 |
self.assertEqual(module.debug_file_status, ModuleFileStatus.HAVE) |
| 2231 |
self.assertEqual(module.loaded_file_path, str(binary_path)) |
| 2232 |
self.assertEqual(module.debug_file_path, str(binary_path)) |
| 2233 |
self.assertEqual(module.supplementary_debug_file_path, str(alt_path)) |
| 2234 |
|
| 2235 |
def test_gnu_debugaltlink_not_found(self): |
| 2236 |
alt_build_id = b"\xfe\xdc\xba\x98\x76\x54\x32\x10" |
| 2237 |
|
| 2238 |
with tempfile.TemporaryDirectory( |
| 2239 |
prefix="bin-" |
| 2240 |
) as bin_dir, tempfile.TemporaryDirectory(prefix="debug-") as debug_dir: |
| 2241 |
bin_dir = Path(bin_dir) |
| 2242 |
debug_dir = Path(debug_dir) |
| 2243 |
|
| 2244 |
binary_path = bin_dir / "binary" |
| 2245 |
binary_path.write_bytes( |
| 2246 |
create_dwarf_file( |
| 2247 |
(), |
| 2248 |
sections=(ALLOCATED_SECTION,), |
| 2249 |
gnu_debugaltlink=(debug_dir / "alt.debug", alt_build_id), |
| 2250 |
) |
| 2251 |
) |
| 2252 |
|
| 2253 |
module = self.prog.extra_module(bin_dir / "binary", create=True) |
| 2254 |
self.prog.load_module_debug_info(module) |
| 2255 |
self.assertEqual(module.loaded_file_status, ModuleFileStatus.HAVE) |
| 2256 |
self.assertEqual( |
| 2257 |
module.debug_file_status, ModuleFileStatus.WANT_SUPPLEMENTARY |
| 2258 |
) |
| 2259 |
self.assertEqual( |
| 2260 |
module.wanted_supplementary_debug_file(), |
| 2261 |
( |
| 2262 |
SupplementaryFileKind.GNU_DEBUGALTLINK, |
| 2263 |
str(binary_path), |
| 2264 |
str(debug_dir / "alt.debug"), |
| 2265 |
alt_build_id, |
| 2266 |
), |
| 2267 |
) |
| 2268 |
self.assertEqual(module.loaded_file_path, str(binary_path)) |
| 2269 |
|
| 2270 |
def test_only_gnu_debugaltlink_absolute(self): |
| 2271 |
alt_build_id = b"\xfe\xdc\xba\x98\x76\x54\x32\x10" |
| 2272 |
|
| 2273 |
with tempfile.TemporaryDirectory( |
| 2274 |
prefix="bin-" |
| 2275 |
) as bin_dir, tempfile.TemporaryDirectory(prefix="debug-") as debug_dir: |
| 2276 |
bin_dir = Path(bin_dir) |
| 2277 |
debug_dir = Path(debug_dir) |
| 2278 |
|
| 2279 |
alt_path = debug_dir / "alt.debug" |
| 2280 |
alt_path.write_bytes(create_dwarf_file((), build_id=alt_build_id)) |
| 2281 |
|
| 2282 |
binary_path = bin_dir / "binary" |
| 2283 |
binary_path.write_bytes( |
| 2284 |
create_dwarf_file( |
| 2285 |
(), |
| 2286 |
sections=(ALLOCATED_SECTION,), |
| 2287 |
gnu_debugaltlink=(alt_path, alt_build_id), |
| 2288 |
) |
| 2289 |
) |
| 2290 |
|
| 2291 |
module = self.prog.extra_module(bin_dir / "binary", create=True) |
| 2292 |
module.try_file(binary_path) |
| 2293 |
self.assertEqual(module.loaded_file_status, ModuleFileStatus.HAVE) |
| 2294 |
self.assertEqual( |
| 2295 |
module.debug_file_status, ModuleFileStatus.WANT_SUPPLEMENTARY |
| 2296 |
) |
| 2297 |
self.assertEqual(module.loaded_file_path, str(binary_path)) |
| 2298 |
|
| 2299 |
self.prog.load_module_debug_info(module) |
| 2300 |
self.assertEqual(module.loaded_file_status, ModuleFileStatus.HAVE) |
| 2301 |
self.assertEqual(module.debug_file_status, ModuleFileStatus.HAVE) |
| 2302 |
self.assertEqual(module.debug_file_path, str(binary_path)) |
| 2303 |
self.assertEqual(module.supplementary_debug_file_path, str(alt_path)) |
| 2304 |
|
| 2305 |
def test_only_gnu_debugaltlink_not_found(self): |
| 2306 |
alt_build_id = b"\xfe\xdc\xba\x98\x76\x54\x32\x10" |
| 2307 |
|
| 2308 |
with tempfile.TemporaryDirectory( |
| 2309 |
prefix="bin-" |
| 2310 |
) as bin_dir, tempfile.TemporaryDirectory(prefix="debug-") as debug_dir: |
| 2311 |
bin_dir = Path(bin_dir) |
| 2312 |
debug_dir = Path(debug_dir) |
| 2313 |
|
| 2314 |
binary_path = bin_dir / "binary" |
| 2315 |
binary_path.write_bytes( |
| 2316 |
create_dwarf_file( |
| 2317 |
(), |
| 2318 |
sections=(ALLOCATED_SECTION,), |
| 2319 |
gnu_debugaltlink=(debug_dir / "alt.debug", alt_build_id), |
| 2320 |
) |
| 2321 |
) |
| 2322 |
|
| 2323 |
module = self.prog.extra_module(bin_dir / "binary", create=True) |
| 2324 |
module.try_file(binary_path) |
| 2325 |
self.assertEqual(module.loaded_file_status, ModuleFileStatus.HAVE) |
| 2326 |
self.assertEqual( |
| 2327 |
module.debug_file_status, ModuleFileStatus.WANT_SUPPLEMENTARY |
| 2328 |
) |
| 2329 |
self.assertEqual(module.loaded_file_path, str(binary_path)) |
| 2330 |
|
| 2331 |
self.prog.load_module_debug_info(module) |
| 2332 |
self.assertEqual(module.loaded_file_status, ModuleFileStatus.HAVE) |
| 2333 |
self.assertEqual( |
| 2334 |
module.debug_file_status, ModuleFileStatus.WANT_SUPPLEMENTARY |
| 2335 |
) |
| 2336 |
self.assertEqual( |
| 2337 |
module.wanted_supplementary_debug_file(), |
| 2338 |
( |
| 2339 |
SupplementaryFileKind.GNU_DEBUGALTLINK, |
| 2340 |
str(binary_path), |
| 2341 |
str(debug_dir / "alt.debug"), |
| 2342 |
alt_build_id, |
| 2343 |
), |
| 2344 |
) |
| 2345 |
|
| 2346 |
def test_gnu_debugaltlink_relative(self): |
| 2347 |
alt_build_id = b"\xfe\xdc\xba\x98\x76\x54\x32\x10" |
| 2348 |
|
| 2349 |
with tempfile.TemporaryDirectory( |
| 2350 |
prefix="bin-" |
| 2351 |
) as bin_dir, tempfile.TemporaryDirectory(prefix="debug-") as debug_dir: |
| 2352 |
bin_dir = Path(bin_dir) |
| 2353 |
debug_dir = Path(debug_dir) |
| 2354 |
|
| 2355 |
alt_path = debug_dir / "alt.debug" |
| 2356 |
alt_path.write_bytes(create_dwarf_file((), build_id=alt_build_id)) |
| 2357 |
|
| 2358 |
binary_path = bin_dir / "binary" |
| 2359 |
binary_path.write_bytes( |
| 2360 |
create_dwarf_file( |
| 2361 |
(), |
| 2362 |
sections=(ALLOCATED_SECTION,), |
| 2363 |
gnu_debugaltlink=( |
| 2364 |
Path(os.path.relpath(alt_path, bin_dir)), |
| 2365 |
alt_build_id, |
| 2366 |
), |
| 2367 |
) |
| 2368 |
) |
| 2369 |
|
| 2370 |
module = self.prog.extra_module(bin_dir / "binary", create=True) |
| 2371 |
self.prog.load_module_debug_info(module) |
| 2372 |
self.assertEqual(module.loaded_file_status, ModuleFileStatus.HAVE) |
| 2373 |
self.assertEqual(module.debug_file_status, ModuleFileStatus.HAVE) |
| 2374 |
self.assertEqual(module.loaded_file_path, str(binary_path)) |
| 2375 |
self.assertEqual(module.debug_file_path, str(binary_path)) |
| 2376 |
self.assertEqual(module.supplementary_debug_file_path, str(alt_path)) |
| 2377 |
|
| 2378 |
def test_gnu_debugaltlink_debug_directories(self): |
| 2379 |
alt_build_id = b"\xfe\xdc\xba\x98\x76\x54\x32\x10" |
| 2380 |
|
| 2381 |
with tempfile.TemporaryDirectory( |
| 2382 |
prefix="bin-" |
| 2383 |
) as bin_dir, tempfile.TemporaryDirectory(prefix="debug-") as debug_dir: |
| 2384 |
bin_dir = Path(bin_dir) |
| 2385 |
debug_dir = Path(debug_dir) |
| 2386 |
|
| 2387 |
alt_path = debug_dir / ".dwz/alt.debug" |
| 2388 |
alt_path.parent.mkdir() |
| 2389 |
alt_path.write_bytes(create_dwarf_file((), build_id=alt_build_id)) |
| 2390 |
|
| 2391 |
self.prog.debug_info_options.directories = (str(debug_dir),) |
| 2392 |
for i, debugaltlink in enumerate( |
| 2393 |
( |
| 2394 |
bin_dir / "debug/.dwz/alt.debug", |
| 2395 |
Path("debug/.dwz/alt.debug"), |
| 2396 |
) |
| 2397 |
): |
| 2398 |
with self.subTest(debugaltlink=debugaltlink): |
| 2399 |
binary_path = bin_dir / f"binary{i}" |
| 2400 |
binary_path.write_bytes( |
| 2401 |
create_dwarf_file( |
| 2402 |
(), |
| 2403 |
sections=(ALLOCATED_SECTION,), |
| 2404 |
gnu_debugaltlink=(debugaltlink, alt_build_id), |
| 2405 |
) |
| 2406 |
) |
| 2407 |
|
| 2408 |
module = self.prog.extra_module(binary_path, create=True) |
| 2409 |
self.prog.load_module_debug_info(module) |
| 2410 |
self.assertEqual(module.loaded_file_status, ModuleFileStatus.HAVE) |
| 2411 |
self.assertEqual(module.debug_file_status, ModuleFileStatus.HAVE) |
| 2412 |
self.assertEqual(module.loaded_file_path, str(binary_path)) |
| 2413 |
self.assertEqual(module.debug_file_path, str(binary_path)) |
| 2414 |
self.assertEqual( |
| 2415 |
module.supplementary_debug_file_path, str(alt_path) |
| 2416 |
) |
| 2417 |
|
| 2418 |
def test_gnu_debugaltlink_build_id_mismatch(self): |
| 2419 |
build_id = b"\x01\x23\x45\x67\x89\xab\xcd\xef" |
| 2420 |
alt_build_id = b"\xfe\xdc\xba\x98\x76\x54\x32\x10" |
| 2421 |
|
| 2422 |
with tempfile.TemporaryDirectory( |
| 2423 |
prefix="bin-" |
| 2424 |
) as bin_dir, tempfile.TemporaryDirectory(prefix="debug-") as debug_dir: |
| 2425 |
bin_dir = Path(bin_dir) |
| 2426 |
debug_dir = Path(debug_dir) |
| 2427 |
|
| 2428 |
alt_path = debug_dir / "alt.debug" |
| 2429 |
alt_path.write_bytes(create_dwarf_file((), build_id=alt_build_id[::-1])) |
| 2430 |
|
| 2431 |
binary_path = bin_dir / "binary" |
| 2432 |
binary_path.write_bytes( |
| 2433 |
create_dwarf_file( |
| 2434 |
(), |
| 2435 |
sections=(ALLOCATED_SECTION,), |
| 2436 |
build_id=build_id, |
| 2437 |
gnu_debugaltlink=(alt_path, alt_build_id), |
| 2438 |
) |
| 2439 |
) |
| 2440 |
|
| 2441 |
module = self.prog.extra_module(bin_dir / "binary", create=True) |
| 2442 |
self.prog.load_module_debug_info(module) |
| 2443 |
self.assertEqual(module.loaded_file_status, ModuleFileStatus.HAVE) |
| 2444 |
self.assertEqual( |
| 2445 |
module.debug_file_status, ModuleFileStatus.WANT_SUPPLEMENTARY |
| 2446 |
) |
| 2447 |
self.assertEqual( |
| 2448 |
module.wanted_supplementary_debug_file(), |
| 2449 |
( |
| 2450 |
SupplementaryFileKind.GNU_DEBUGALTLINK, |
| 2451 |
str(binary_path), |
| 2452 |
str(alt_path), |
| 2453 |
alt_build_id, |
| 2454 |
), |
| 2455 |
) |
| 2456 |
self.assertEqual(module.loaded_file_path, str(binary_path)) |
| 2457 |
|
| 2458 |
def test_invalid_gnu_debugaltlink(self): |
| 2459 |
build_id = b"\x01\x23\x45\x67\x89\xab\xcd\xef" |
| 2460 |
|
| 2461 |
with tempfile.TemporaryDirectory(prefix="bin-") as bin_dir: |
| 2462 |
bin_dir = Path(bin_dir) |
| 2463 |
|
| 2464 |
binary_path = bin_dir / "binary" |
| 2465 |
binary_path.write_bytes( |
| 2466 |
create_dwarf_file( |
| 2467 |
(), |
| 2468 |
sections=( |
| 2469 |
ALLOCATED_SECTION, |
| 2470 |
ElfSection( |
| 2471 |
name=".gnu_debugaltlink", |
| 2472 |
sh_type=SHT.PROGBITS, |
| 2473 |
data=b"foo", |
| 2474 |
), |
| 2475 |
), |
| 2476 |
build_id=build_id, |
| 2477 |
) |
| 2478 |
) |
| 2479 |
|
| 2480 |
module = self.prog.extra_module(bin_dir / "binary", create=True) |
| 2481 |
self.prog.load_module_debug_info(module) |
| 2482 |
self.assertEqual(module.loaded_file_status, ModuleFileStatus.HAVE) |
| 2483 |
self.assertEqual(module.debug_file_status, ModuleFileStatus.WANT) |
| 2484 |
self.assertEqual(module.loaded_file_path, str(binary_path)) |