Debian Codegraph @

tests/__init__.py

raw
1 # Copyright (c) Meta Platforms, Inc. and affiliates.
2 # SPDX-License-Identifier: LGPL-2.1-or-later
3
4 import contextlib
5 import functools
6 import logging
7 import os
8 import sys
9 from typing import Any, Mapping, NamedTuple, Optional
10 import unittest
11 from unittest.mock import Mock
12 import warnings
13
14 from drgn import (
15 AbsenceReason,
16 Architecture,
17 FindObjectFlags,
18 Language,
19 NoDefaultProgramError,
20 Object,
21 Platform,
22 PlatformFlags,
23 PrimitiveType,
24 Program,
25 Type,
26 TypeEnumerator,
27 TypeKind,
28 TypeMember,
29 TypeParameter,
30 TypeTemplateParameter,
31 get_default_prog,
32 set_default_prog,
33 )
34
35 try:
36 import pytest
37 except ImportError:
38
39 def slow_test(func):
40 return func
41
42 else:
43 slow_test = pytest.mark.slow
44
45
46 DEFAULT_LANGUAGE = Language.C
47
48
49 class IntWrapper:
50 def __init__(self, value):
51 self._value = value
52
53 def __index__(self):
54 return self._value
55
56
57 MOCK_32BIT_PLATFORM = Platform(Architecture.UNKNOWN, PlatformFlags.IS_LITTLE_ENDIAN)
58 MOCK_32BIT_BIG_ENDIAN_PLATFORM = Platform(Architecture.UNKNOWN, PlatformFlags(0))
59 MOCK_PLATFORM = Platform(
60 Architecture.UNKNOWN, PlatformFlags.IS_64_BIT | PlatformFlags.IS_LITTLE_ENDIAN
61 )
62 MOCK_BIG_ENDIAN_PLATFORM = Platform(Architecture.UNKNOWN, PlatformFlags.IS_64_BIT)
63
64
65 class MockMemorySegment(NamedTuple):
66 buf: bytes
67 virt_addr: Optional[int] = None
68 phys_addr: Optional[int] = None
69
70
71 def mock_memory_read(data, address, count, offset, physical):
72 return data[offset : offset + count]
73
74
75 def add_mock_memory_segments(prog, segments):
76 for segment in segments:
77 if segment.virt_addr is not None:
78 prog.add_memory_segment(
79 segment.virt_addr,
80 len(segment.buf),
81 functools.partial(mock_memory_read, segment.buf),
82 )
83 if segment.phys_addr is not None:
84 prog.add_memory_segment(
85 segment.phys_addr,
86 len(segment.buf),
87 functools.partial(mock_memory_read, segment.buf),
88 True,
89 )
90
91
92 class MockObject(NamedTuple):
93 name: str
94 type: Type
95 address: Optional[int] = None
96 value: Any = None
97
98
99 def mock_program(platform=MOCK_PLATFORM, *, segments=None, types=None, objects=None):
100 def mock_find_type(prog, kinds, name, filename):
101 if filename:
102 return None
103 for type in types:
104 if type.kind in kinds:
105 try:
106 type_name = type.name
107 except AttributeError:
108 try:
109 type_name = type.tag
110 except AttributeError:
111 continue
112 if type_name == name:
113 return type
114 return None
115
116 def mock_object_find(prog, name, flags, filename):
117 if filename:
118 return None
119 for obj in objects:
120 if obj.name == name:
121 if obj.value is not None:
122 if flags & FindObjectFlags.CONSTANT:
123 break
124 elif obj.type.kind == TypeKind.FUNCTION:
125 if flags & FindObjectFlags.FUNCTION:
126 break
127 elif flags & FindObjectFlags.VARIABLE:
128 break
129 else:
130 return None
131 return Object(prog, obj.type, address=obj.address, value=obj.value)
132
133 prog = Program(platform)
134 if segments is not None:
135 add_mock_memory_segments(prog, segments)
136 if types is not None:
137 prog.register_type_finder("mock", mock_find_type, enable_index=0)
138 if objects is not None:
139 prog.register_object_finder("mock", mock_object_find, enable_index=0)
140 return prog
141
142
143 def assertReprPrettyEqualsStr(obj):
144 pretty_printer_mock = Mock()
145
146 obj._repr_pretty_(pretty_printer_mock, False)
147 pretty_printer_mock.text.assert_called_with(str(obj))
148
149 obj._repr_pretty_(p=pretty_printer_mock, cycle=True)
150 pretty_printer_mock.text.assert_called_with("...")
151
152
153 _IDENTICAL_EQ_TYPES = (
154 type(None),
155 AbsenceReason,
156 Language,
157 PrimitiveType,
158 Program,
159 TypeEnumerator,
160 TypeKind,
161 bool,
162 float,
163 int,
164 str,
165 )
166 _IDENTICAL_MAPPING_TYPES = (dict,)
167 _IDENTICAL_SEQUENCE_TYPES = (list, tuple)
168 _IDENTICAL_SUPPORTED_TYPES = (
169 _IDENTICAL_EQ_TYPES
170 + _IDENTICAL_MAPPING_TYPES
171 + _IDENTICAL_SEQUENCE_TYPES
172 + (Object, Type, TypeMember, TypeParameter, TypeTemplateParameter)
173 )
174
175
176 def identical(a, b):
177 """
178 Return whether two objects are "identical".
179
180 drgn.Object, drgn.Type, drgn.TypeMember, drgn.TypeParameter, and
181 drgn.TypeTemplateParameter objects are identical iff they have they have
182 the same type and identical attributes. Note that for drgn.Object, this is
183 different from the objects comparing equal: their type, address, value,
184 etc. must be identical.
185
186 Two mappings or sequences are identical iff they have the same type,
187 length, and all of their items are identical.
188
189 Types in _IDENTICAL_EQ_TYPES are identical iff they have the same type and
190 they compare equal.
191 """
192 compared_types = set()
193
194 def _identical_attrs(a, b, attr_names):
195 for attr_name in attr_names:
196 if not _identical(getattr(a, attr_name), getattr(b, attr_name)):
197 return False
198 return True
199
200 def _identical_mapping(a, b):
201 return len(a) == len(b) and all(
202 _identical(key_a, key_b) and _identical(value_a, value_b)
203 for (key_a, value_a), (key_b, value_b) in zip(a.items(), b.items())
204 )
205
206 def _identical_sequence(a, b):
207 return len(a) == len(b) and all(
208 _identical(elem_a, elem_b) for elem_a, elem_b in zip(a, b)
209 )
210
211 def _identical(a, b):
212 if (
213 type(a) not in _IDENTICAL_SUPPORTED_TYPES
214 or type(b) not in _IDENTICAL_SUPPORTED_TYPES
215 ):
216 raise NotImplementedError(f"can't compare {type(a)} to {type(b)}")
217 if type(a) != type(b): # noqa: E721
218 return False
219
220 t = type(a)
221 if t == Object:
222 if not _identical_attrs(
223 a,
224 b,
225 (
226 "prog_",
227 "type_",
228 "address_",
229 "absence_reason_",
230 "bit_offset_",
231 "bit_field_size_",
232 ),
233 ):
234 return False
235 if a.address_ is None:
236 exc_a = exc_b = False
237 try:
238 value_a = a.value_()
239 except Exception:
240 exc_a = True
241 try:
242 value_b = b.value_()
243 except Exception:
244 exc_b = True
245 if exc_a != exc_b:
246 return False
247 return exc_a or _identical(value_a, value_b)
248 else:
249 return True
250 elif t == Type:
251 if a.qualifiers != b.qualifiers:
252 return False
253 if a._ptr == b._ptr:
254 return True
255 if a._ptr < b._ptr:
256 key = (a._ptr, b._ptr)
257 else:
258 key = (b._ptr, a._ptr)
259 if key in compared_types:
260 return True
261 compared_types.add(key)
262 return _identical_attrs(
263 a,
264 b,
265 [
266 name
267 for name in (
268 "prog",
269 "kind",
270 "primitive",
271 "language",
272 "name",
273 "tag",
274 "size",
275 "length",
276 "is_signed",
277 "byteorder",
278 "type",
279 "members",
280 "enumerators",
281 "parameters",
282 "is_variadic",
283 "template_parameters",
284 )
285 if hasattr(a, name) or hasattr(b, name)
286 ],
287 )
288 elif t == TypeMember:
289 return _identical_attrs(a, b, ("object", "name", "bit_offset"))
290 elif t == TypeParameter:
291 return _identical_attrs(a, b, ("default_argument", "name"))
292 elif t == TypeTemplateParameter:
293 return _identical_attrs(a, b, ("argument", "name", "is_default"))
294 elif t in _IDENTICAL_EQ_TYPES:
295 return a == b
296 elif t in _IDENTICAL_MAPPING_TYPES:
297 return _identical_mapping(a, b)
298 elif t in _IDENTICAL_SEQUENCE_TYPES:
299 return _identical_sequence(a, b)
300 else:
301 assert False
302
303 return _identical(a, b)
304
305
306 # Wrapper class that defines == using identical(). This is useful for
307 # unittest.mock.Mock.assert_called_with().
308 class IdenticalMatcher:
309 def __init__(self, obj):
310 self._obj = obj
311
312 def __str__(self):
313 return str(self._obj)
314
315 def __repr__(self):
316 return repr(self._obj)
317
318 def __eq__(self, other):
319 if isinstance(other, IdenticalMatcher):
320 other = other._obj
321 return identical(self._obj, other)
322
323
324 class TestCase(unittest.TestCase):
325 # "Backport" enterContext() and enterClassContext().
326 if sys.version_info < (3, 11):
327
328 def enterContext(self, cm):
329 result = cm.__enter__()
330 self.addCleanup(cm.__exit__, None, None, None)
331 return result
332
333 @classmethod
334 def enterClassContext(cls, cm):
335 result = cm.__enter__()
336 cls.addClassCleanup(cm.__exit__, None, None, None)
337 return result
338
339 @classmethod
340 def setUpClass(cls):
341 super().setUpClass()
342 # We have a lot of tests that need to fork, and we are also
343 # multithreaded thanks to OpenMP. Ignore deprecation warnings about
344 # forking with multiple threads until we can figure out a different
345 # approach.
346 warnings.filterwarnings(
347 "ignore",
348 message=".*fork.*may lead to deadlocks in the child.*",
349 category=DeprecationWarning,
350 )
351
352 def assertIdentical(self, a, b, msg=None):
353 return self.assertEqual(IdenticalMatcher(a), IdenticalMatcher(b), msg)
354
355 def assertNotIdentical(self, a, b, msg=None):
356 return self.assertNotEqual(IdenticalMatcher(a), IdenticalMatcher(b), msg)
357
358 def bool(self, value):
359 return Object(self.prog, "_Bool", value=value)
360
361 def int(self, value):
362 return Object(self.prog, "int", value=value)
363
364 def unsigned_int(self, value):
365 return Object(self.prog, "unsigned int", value=value)
366
367 def long(self, value):
368 return Object(self.prog, "long", value=value)
369
370 def double(self, value):
371 return Object(self.prog, "double", value=value)
372
373
374 class MockProgramTestCase(TestCase):
375 def setUp(self):
376 super().setUp()
377 self.types = []
378 self.objects = []
379 self.prog = mock_program(types=self.types, objects=self.objects)
380 self.coord_type = self.prog.class_type(
381 "coord",
382 12,
383 (
384 TypeMember(self.prog.int_type("int", 4, True), "x", 0),
385 TypeMember(self.prog.int_type("int", 4, True), "y", 32),
386 TypeMember(self.prog.int_type("int", 4, True), "z", 64),
387 ),
388 )
389 self.point_type = self.prog.struct_type(
390 "point",
391 8,
392 (
393 TypeMember(self.prog.int_type("int", 4, True), "x", 0),
394 TypeMember(self.prog.int_type("int", 4, True), "y", 32),
395 ),
396 )
397 self.line_segment_type = self.prog.struct_type(
398 "line_segment",
399 16,
400 (TypeMember(self.point_type, "a"), TypeMember(self.point_type, "b", 64)),
401 )
402 self.option_type = self.prog.union_type(
403 "option",
404 4,
405 (
406 TypeMember(self.prog.int_type("int", 4, True), "i"),
407 TypeMember(self.prog.float_type("float", 4), "f"),
408 ),
409 )
410 self.color_type = self.prog.enum_type(
411 "color",
412 self.prog.int_type("unsigned int", 4, False),
413 (
414 TypeEnumerator("RED", 0),
415 TypeEnumerator("GREEN", 1),
416 TypeEnumerator("BLUE", 2),
417 ),
418 )
419 self.pid_type = self.prog.typedef_type(
420 "pid_t", self.prog.int_type("int", 4, True)
421 )
422
423 def add_memory_segment(self, buf, virt_addr=None, phys_addr=None):
424 if virt_addr is not None:
425 self.prog.add_memory_segment(
426 virt_addr, len(buf), functools.partial(mock_memory_read, buf)
427 )
428 if phys_addr is not None:
429 self.prog.add_memory_segment(
430 phys_addr, len(buf), functools.partial(mock_memory_read, buf), True
431 )
432
433
434 @contextlib.contextmanager
435 def modifyenv(vars: Mapping[str, Optional[str]]):
436 to_restore = []
437 for key, value in vars.items():
438 old_value = os.environ.get(key)
439 if value != old_value:
440 if value is None:
441 del os.environ[key]
442 else:
443 os.environ[key] = value
444 to_restore.append((key, old_value))
445 try:
446 yield
447 finally:
448 for key, old_value in to_restore:
449 if old_value is None:
450 del os.environ[key]
451 else:
452 os.environ[key] = old_value
453
454
455 @contextlib.contextmanager
456 def drgn_log_level(level: int):
457 logger = logging.getLogger("drgn")
458 old_level = logger.getEffectiveLevel()
459 logger.setLevel(level)
460 try:
461 yield
462 finally:
463 logger.setLevel(old_level)
464
465
466 @contextlib.contextmanager
467 def with_default_prog(prog):
468 try:
469 old_default_prog = get_default_prog()
470 except NoDefaultProgramError:
471 old_default_prog = None
472 try:
473 set_default_prog(prog)
474 yield
475 finally:
476 set_default_prog(old_default_prog)