Debian Codegraph @

lldb/packages/Python/lldbsuite/test/decorators.py

raw
1 # System modules
2 from functools import wraps
3 from packaging import version
4 import ctypes
5 import locale
6 import os
7 import platform
8 import re
9 import sys
10 import tempfile
11 import subprocess
12
13 # Third-party modules
14 import unittest
15
16 # LLDB modules
17 import lldb
18 from . import configuration
19 from . import test_categories
20 from . import lldbtest_config
21 from lldbsuite.support import funcutils
22 from lldbsuite.test import lldbplatform
23 from lldbsuite.test import lldbplatformutil
24
25
26 class DecorateMode:
27 Skip, Xfail = range(2)
28
29
30 # You can use no_match to reverse the test of the conditional that is used to match keyword
31 # arguments in the skip / xfail decorators. If oslist=["windows", "linux"] skips windows
32 # and linux, oslist=no_match(["windows", "linux"]) skips *unless* windows
33 # or linux.
34 class no_match:
35 def __init__(self, item):
36 self.item = item
37
38
39 def _check_expected_version(comparison, expected, actual):
40 def fn_leq(x, y):
41 return x <= y
42
43 def fn_less(x, y):
44 return x < y
45
46 def fn_geq(x, y):
47 return x >= y
48
49 def fn_greater(x, y):
50 return x > y
51
52 def fn_eq(x, y):
53 return x == y
54
55 def fn_neq(x, y):
56 return x != y
57
58 op_lookup = {
59 "==": fn_eq,
60 "=": fn_eq,
61 "!=": fn_neq,
62 "<>": fn_neq,
63 ">": fn_greater,
64 "<": fn_less,
65 ">=": fn_geq,
66 "<=": fn_leq,
67 }
68
69 return op_lookup[comparison](version.parse(actual), version.parse(expected))
70
71
72 def _match_decorator_property(expected, actual):
73 if expected is None:
74 return True
75
76 if actual is None:
77 return False
78
79 if isinstance(expected, no_match):
80 return not _match_decorator_property(expected.item, actual)
81
82 # Python 3.6 doesn't declare a `re.Pattern` type, get the dynamic type.
83 pattern_type = type(re.compile(""))
84 if isinstance(expected, (pattern_type, str)):
85 return re.search(expected, actual) is not None
86
87 if hasattr(expected, "__iter__"):
88 return any(
89 [x is not None and _match_decorator_property(x, actual) for x in expected]
90 )
91
92 return expected == actual
93
94
95 def _compiler_supports(
96 compiler, flag, source="int main() {}", output_file=tempfile.NamedTemporaryFile()
97 ):
98 """Test whether the compiler supports the given flag."""
99 if platform.system() == "Darwin":
100 compiler = "xcrun " + compiler
101 try:
102 cmd = "echo '%s' | %s %s -x c -o %s -" % (
103 source,
104 compiler,
105 flag,
106 output_file.name,
107 )
108 subprocess.check_call(cmd, shell=True)
109 except subprocess.CalledProcessError:
110 return False
111 return True
112
113
114 def expectedFailureIf(condition, bugnumber=None):
115 def expectedFailure_impl(func):
116 if isinstance(func, type) and issubclass(func, unittest.TestCase):
117 raise Exception("Decorator can only be used to decorate a test method")
118
119 if condition:
120 return unittest.expectedFailure(func)
121 return func
122
123 if callable(bugnumber):
124 return expectedFailure_impl(bugnumber)
125 else:
126 return expectedFailure_impl
127
128
129 def skipTestIfFn(expected_fn, bugnumber=None):
130 def skipTestIfFn_impl(func):
131 if isinstance(func, type) and issubclass(func, unittest.TestCase):
132 reason = expected_fn()
133 # The return value is the reason (or None if we don't skip), so
134 # reason is used for both args.
135 return unittest.skipIf(condition=reason, reason=reason)(func)
136
137 @wraps(func)
138 def wrapper(*args, **kwargs):
139 self = args[0]
140 if funcutils.requires_self(expected_fn):
141 reason = expected_fn(self)
142 else:
143 reason = expected_fn()
144
145 if reason is not None:
146 self.skipTest(reason)
147 else:
148 return func(*args, **kwargs)
149
150 return wrapper
151
152 # Some decorators can be called both with no arguments (e.g. @expectedFailureWindows)
153 # or with arguments (e.g. @expectedFailureWindows(compilers=['gcc'])). When called
154 # the first way, the first argument will be the actual function because decorators are
155 # weird like that. So this is basically a check that says "how was the
156 # decorator used"
157 if callable(bugnumber):
158 return skipTestIfFn_impl(bugnumber)
159 else:
160 return skipTestIfFn_impl
161
162
163 def _xfailForDebugInfo(expected_fn, bugnumber=None):
164 def expectedFailure_impl(func):
165 if isinstance(func, type) and issubclass(func, unittest.TestCase):
166 raise Exception("Decorator can only be used to decorate a test method")
167
168 func.__xfail_for_debug_info_cat_fn__ = expected_fn
169 return func
170
171 if callable(bugnumber):
172 return expectedFailure_impl(bugnumber)
173 else:
174 return expectedFailure_impl
175
176
177 def _skipForDebugInfo(expected_fn, bugnumber=None):
178 def skipImpl(func):
179 if isinstance(func, type) and issubclass(func, unittest.TestCase):
180 raise Exception("Decorator can only be used to decorate a test method")
181
182 func.__skip_for_debug_info_cat_fn__ = expected_fn
183 return func
184
185 if callable(bugnumber):
186 return skipImpl(bugnumber)
187 else:
188 return skipImpl
189
190
191 def _decorateTest(
192 mode,
193 bugnumber=None,
194 oslist=None,
195 hostoslist=None,
196 compiler=None,
197 compiler_version=None,
198 archs=None,
199 triple=None,
200 debug_info=None,
201 swig_version=None,
202 py_version=None,
203 macos_version=None,
204 remote=None,
205 dwarf_version=None,
206 setting=None,
207 asan=None,
208 ):
209 def fn(actual_debug_info=None):
210 skip_for_os = _match_decorator_property(
211 lldbplatform.translate(oslist), lldbplatformutil.getPlatform()
212 )
213 skip_for_hostos = _match_decorator_property(
214 lldbplatform.translate(hostoslist), lldbplatformutil.getHostPlatform()
215 )
216 skip_for_compiler = _match_decorator_property(
217 compiler, lldbplatformutil.getCompiler()
218 ) and lldbplatformutil.expectedCompilerVersion(compiler_version)
219 skip_for_arch = _match_decorator_property(
220 archs, lldbplatformutil.getArchitecture()
221 )
222 skip_for_debug_info = _match_decorator_property(debug_info, actual_debug_info)
223 skip_for_triple = _match_decorator_property(
224 triple, lldb.selected_platform.GetTriple()
225 )
226 skip_for_remote = _match_decorator_property(
227 remote, lldb.remote_platform is not None
228 )
229
230 skip_for_swig_version = (
231 (swig_version is None)
232 or (not hasattr(lldb, "swig_version"))
233 or (
234 _check_expected_version(
235 swig_version[0], swig_version[1], lldb.swig_version
236 )
237 )
238 )
239 skip_for_py_version = (py_version is None) or _check_expected_version(
240 py_version[0],
241 py_version[1],
242 "{}.{}".format(sys.version_info.major, sys.version_info.minor),
243 )
244 skip_for_macos_version = (macos_version is None) or (
245 (platform.mac_ver()[0] != "")
246 and (
247 _check_expected_version(
248 macos_version[0], macos_version[1], platform.mac_ver()[0]
249 )
250 )
251 )
252 skip_for_dwarf_version = (dwarf_version is None) or (
253 _check_expected_version(
254 dwarf_version[0], dwarf_version[1], lldbplatformutil.getDwarfVersion()
255 )
256 )
257 skip_for_setting = (setting is None) or (setting in configuration.settings)
258 skip_for_asan = (asan is None) or is_running_under_asan()
259
260 # For the test to be skipped, all specified (e.g. not None) parameters must be True.
261 # An unspecified parameter means "any", so those are marked skip by default. And we skip
262 # the final test if all conditions are True.
263 conditions = [
264 (oslist, skip_for_os, "target o/s"),
265 (hostoslist, skip_for_hostos, "host o/s"),
266 (compiler, skip_for_compiler, "compiler or version"),
267 (archs, skip_for_arch, "architecture"),
268 (debug_info, skip_for_debug_info, "debug info format"),
269 (triple, skip_for_triple, "target triple"),
270 (swig_version, skip_for_swig_version, "swig version"),
271 (py_version, skip_for_py_version, "python version"),
272 (macos_version, skip_for_macos_version, "macOS version"),
273 (remote, skip_for_remote, "platform locality (remote/local)"),
274 (dwarf_version, skip_for_dwarf_version, "dwarf version"),
275 (setting, skip_for_setting, "setting"),
276 (asan, skip_for_asan, "running under asan"),
277 ]
278 reasons = []
279 final_skip_result = True
280 for this_condition in conditions:
281 final_skip_result = final_skip_result and this_condition[1]
282 if this_condition[0] is not None and this_condition[1]:
283 reasons.append(this_condition[2])
284 reason_str = None
285 if final_skip_result:
286 mode_str = {DecorateMode.Skip: "skipping", DecorateMode.Xfail: "xfailing"}[
287 mode
288 ]
289 if len(reasons) > 0:
290 reason_str = ",".join(reasons)
291 reason_str = "{} due to the following parameter(s): {}".format(
292 mode_str, reason_str
293 )
294 else:
295 reason_str = "{} unconditionally".format(mode_str)
296 if bugnumber is not None and not callable(bugnumber):
297 reason_str = reason_str + " [" + str(bugnumber) + "]"
298 return reason_str
299
300 if mode == DecorateMode.Skip:
301 if debug_info:
302 return _skipForDebugInfo(fn, bugnumber)
303 return skipTestIfFn(fn, bugnumber)
304 elif mode == DecorateMode.Xfail:
305 if debug_info:
306 return _xfailForDebugInfo(fn, bugnumber)
307 return expectedFailureIf(fn(), bugnumber)
308 else:
309 return None
310
311
312 # provide a function to xfail on defined oslist, compiler version, and archs
313 # if none is specified for any argument, that argument won't be checked and thus means for all
314 # for example,
315 # @expectedFailureAll, xfail for all platform/compiler/arch,
316 # @expectedFailureAll(compiler='gcc'), xfail for gcc on all platform/architecture
317 # @expectedFailureAll(bugnumber, ["linux"], "gcc", ['>=', '4.9'], ['i386']), xfail for gcc>=4.9 on linux with i386
318
319
320 def expectedFailureAll(
321 bugnumber=None,
322 oslist=None,
323 hostoslist=None,
324 compiler=None,
325 compiler_version=None,
326 archs=None,
327 triple=None,
328 debug_info=None,
329 swig_version=None,
330 py_version=None,
331 macos_version=None,
332 remote=None,
333 dwarf_version=None,
334 setting=None,
335 asan=None,
336 ):
337 return _decorateTest(
338 DecorateMode.Xfail,
339 bugnumber=bugnumber,
340 oslist=oslist,
341 hostoslist=hostoslist,
342 compiler=compiler,
343 compiler_version=compiler_version,
344 archs=archs,
345 triple=triple,
346 debug_info=debug_info,
347 swig_version=swig_version,
348 py_version=py_version,
349 macos_version=macos_version,
350 remote=remote,
351 dwarf_version=dwarf_version,
352 setting=setting,
353 asan=asan,
354 )
355
356
357 # provide a function to skip on defined oslist, compiler version, and archs
358 # if none is specified for any argument, that argument won't be checked and thus means for all
359 # for example,
360 # @skipIf, skip for all platform/compiler/arch,
361 # @skipIf(compiler='gcc'), skip for gcc on all platform/architecture
362 # @skipIf(bugnumber, ["linux"], "gcc", ['>=', '4.9'], ['i386']), skip for gcc>=4.9 on linux with i386 (all conditions must be true)
363 def skipIf(
364 bugnumber=None,
365 oslist=None,
366 hostoslist=None,
367 compiler=None,
368 compiler_version=None,
369 archs=None,
370 triple=None,
371 debug_info=None,
372 swig_version=None,
373 py_version=None,
374 macos_version=None,
375 remote=None,
376 dwarf_version=None,
377 setting=None,
378 asan=None,
379 ):
380 return _decorateTest(
381 DecorateMode.Skip,
382 bugnumber=bugnumber,
383 oslist=oslist,
384 hostoslist=hostoslist,
385 compiler=compiler,
386 compiler_version=compiler_version,
387 archs=archs,
388 triple=triple,
389 debug_info=debug_info,
390 swig_version=swig_version,
391 py_version=py_version,
392 macos_version=macos_version,
393 remote=remote,
394 dwarf_version=dwarf_version,
395 setting=setting,
396 asan=asan,
397 )
398
399
400 def _skip_fn_for_android(reason, api_levels, archs):
401 def impl():
402 result = lldbplatformutil.match_android_device(
403 lldbplatformutil.getArchitecture(),
404 valid_archs=archs,
405 valid_api_levels=api_levels,
406 )
407 return reason if result else None
408
409 return impl
410
411
412 def add_test_categories(cat):
413 """Add test categories to a TestCase method"""
414 cat = test_categories.validate(cat, True)
415
416 def impl(func):
417 try:
418 if hasattr(func, "categories"):
419 cat.extend(func.categories)
420 setattr(func, "categories", cat)
421 except AttributeError:
422 raise Exception("Cannot assign categories to inline tests.")
423
424 return func
425
426 return impl
427
428
429 def benchmarks_test(func):
430 """Decorate the item as a benchmarks test."""
431
432 def should_skip_benchmarks_test():
433 return "benchmarks test"
434
435 # Mark this function as such to separate them from the regular tests.
436 result = skipTestIfFn(should_skip_benchmarks_test)(func)
437 result.__benchmarks_test__ = True
438 return result
439
440
441 def no_debug_info_test(func):
442 """Decorate the item as a test what don't use any debug info. If this annotation is specified
443 then the test runner won't generate a separate test for each debug info format."""
444 if isinstance(func, type) and issubclass(func, unittest.TestCase):
445 raise Exception(
446 "@no_debug_info_test can only be used to decorate a test method"
447 )
448
449 @wraps(func)
450 def wrapper(self, *args, **kwargs):
451 return func(self, *args, **kwargs)
452
453 # Mark this function as such to separate them from the regular tests.
454 wrapper.__no_debug_info_test__ = True
455 return wrapper
456
457
458 def apple_simulator_test(platform):
459 """
460 Decorate the test as a test requiring a simulator for a specific platform.
461
462 Consider that a simulator is available if you have the corresponding SDK installed.
463 The SDK identifiers for simulators are iphonesimulator, appletvsimulator, watchsimulator
464 """
465
466 def should_skip_simulator_test():
467 if lldbplatformutil.getHostPlatform() not in ["darwin", "macosx"]:
468 return "simulator tests are run only on darwin hosts."
469 try:
470 DEVNULL = open(os.devnull, "w")
471 output = subprocess.check_output(
472 ["xcodebuild", "-showsdks"], stderr=DEVNULL
473 ).decode("utf-8")
474 if re.search("%ssimulator" % platform, output):
475 return None
476 else:
477 return "%s simulator is not supported on this system." % platform
478 except subprocess.CalledProcessError:
479 return "Simulators are unsupported on this system (xcodebuild failed)"
480
481 return skipTestIfFn(should_skip_simulator_test)
482
483
484 def debugserver_test(func):
485 """Decorate the item as a debugserver test."""
486 return add_test_categories(["debugserver"])(func)
487
488
489 def llgs_test(func):
490 """Decorate the item as a lldb-server test."""
491 return add_test_categories(["llgs"])(func)
492
493
494 def expectedFailureOS(
495 oslist, bugnumber=None, compilers=None, debug_info=None, archs=None
496 ):
497 return expectedFailureAll(
498 oslist=oslist,
499 bugnumber=bugnumber,
500 compiler=compilers,
501 archs=archs,
502 debug_info=debug_info,
503 )
504
505
506 def expectedFailureDarwin(bugnumber=None, compilers=None, debug_info=None, archs=None):
507 # For legacy reasons, we support both "darwin" and "macosx" as OS X
508 # triples.
509 return expectedFailureOS(
510 lldbplatform.darwin_all,
511 bugnumber,
512 compilers,
513 debug_info=debug_info,
514 archs=archs,
515 )
516
517
518 def expectedFailureAndroid(bugnumber=None, api_levels=None, archs=None):
519 """Mark a test as xfail for Android.
520
521 Arguments:
522 bugnumber - The LLVM pr associated with the problem.
523 api_levels - A sequence of numbers specifying the Android API levels
524 for which a test is expected to fail. None means all API level.
525 arch - A sequence of architecture names specifying the architectures
526 for which a test is expected to fail. None means all architectures.
527 """
528 return expectedFailureIf(
529 _skip_fn_for_android("xfailing on android", api_levels, archs)(), bugnumber
530 )
531
532
533 def expectedFailureNetBSD(bugnumber=None):
534 return expectedFailureOS(["netbsd"], bugnumber)
535
536
537 def expectedFailureWindows(bugnumber=None):
538 return expectedFailureOS(["windows"], bugnumber)
539
540
541 # TODO: This decorator does not do anything. Remove it.
542 def expectedFlakey(expected_fn, bugnumber=None):
543 def expectedFailure_impl(func):
544 @wraps(func)
545 def wrapper(*args, **kwargs):
546 func(*args, **kwargs)
547
548 return wrapper
549
550 # Some decorators can be called both with no arguments (e.g. @expectedFailureWindows)
551 # or with arguments (e.g. @expectedFailureWindows(compilers=['gcc'])). When called
552 # the first way, the first argument will be the actual function because decorators are
553 # weird like that. So this is basically a check that says "which syntax was the original
554 # function decorated with?"
555 if callable(bugnumber):
556 return expectedFailure_impl(bugnumber)
557 else:
558 return expectedFailure_impl
559
560
561 def expectedFlakeyOS(oslist, bugnumber=None, compilers=None):
562 def fn(self):
563 return (
564 lldbplatformutil.getPlatform() in oslist
565 and lldbplatformutil.expectedCompiler(compilers)
566 )
567
568 return expectedFlakey(fn, bugnumber)
569
570
571 def expectedFlakeyDarwin(bugnumber=None, compilers=None):
572 # For legacy reasons, we support both "darwin" and "macosx" as OS X
573 # triples.
574 return expectedFlakeyOS(lldbplatformutil.getDarwinOSTriples(), bugnumber, compilers)
575
576
577 def expectedFlakeyFreeBSD(bugnumber=None, compilers=None):
578 return expectedFlakeyOS(["freebsd"], bugnumber, compilers)
579
580
581 def expectedFlakeyLinux(bugnumber=None, compilers=None):
582 return expectedFlakeyOS(["linux"], bugnumber, compilers)
583
584
585 def expectedFlakeyNetBSD(bugnumber=None, compilers=None):
586 return expectedFlakeyOS(["netbsd"], bugnumber, compilers)
587
588
589 def expectedFlakeyAndroid(bugnumber=None, api_levels=None, archs=None):
590 return expectedFlakey(
591 _skip_fn_for_android("flakey on android", api_levels, archs), bugnumber
592 )
593
594
595 def skipIfOutOfTreeDebugserver(func):
596 """Decorate the item to skip tests if using an out-of-tree debugserver."""
597
598 def is_out_of_tree_debugserver():
599 return (
600 "out-of-tree debugserver"
601 if lldbtest_config.out_of_tree_debugserver
602 else None
603 )
604
605 return skipTestIfFn(is_out_of_tree_debugserver)(func)
606
607
608 def skipIfRemote(func):
609 """Decorate the item to skip tests if testing remotely."""
610 return unittest.skipIf(lldb.remote_platform, "skip on remote platform")(func)
611
612
613 def skipIfNoSBHeaders(func):
614 """Decorate the item to mark tests that should be skipped when LLDB is built with no SB API headers."""
615
616 def are_sb_headers_missing():
617 if lldb.remote_platform:
618 return "skip because SBHeaders tests make no sense remotely"
619
620 if (
621 lldbplatformutil.getHostPlatform() == "darwin"
622 and configuration.lldb_framework_path
623 ):
624 header = os.path.join(
625 configuration.lldb_framework_path,
626 "Versions",
627 "Current",
628 "Headers",
629 "LLDB.h",
630 )
631 if os.path.exists(header):
632 return None
633
634 header = os.path.join(
635 os.environ["LLDB_SRC"], "include", "lldb", "API", "LLDB.h"
636 )
637 if not os.path.exists(header):
638 return "skip because LLDB.h header not found"
639 return None
640
641 return skipTestIfFn(are_sb_headers_missing)(func)
642
643
644 def skipIfRosetta(bugnumber):
645 """Skip a test when running the testsuite on macOS under the Rosetta translation layer."""
646
647 def is_running_rosetta():
648 if lldbplatformutil.getPlatform() in ["darwin", "macosx"]:
649 if (platform.uname()[5] == "arm") and (
650 lldbplatformutil.getArchitecture() == "x86_64"
651 ):
652 return "skipped under Rosetta"
653 return None
654
655 return skipTestIfFn(is_running_rosetta)
656
657
658 def skipIfiOSSimulator(func):
659 """Decorate the item to skip tests that should be skipped on the iOS Simulator."""
660
661 def is_ios_simulator():
662 return (
663 "skip on the iOS Simulator"
664 if configuration.lldb_platform_name == "ios-simulator"
665 else None
666 )
667
668 return skipTestIfFn(is_ios_simulator)(func)
669
670
671 def skipIfiOS(func):
672 return skipIfPlatform(lldbplatform.translate(lldbplatform.ios))(func)
673
674
675 def skipIftvOS(func):
676 return skipIfPlatform(lldbplatform.translate(lldbplatform.tvos))(func)
677
678
679 def skipIfwatchOS(func):
680 return skipIfPlatform(lldbplatform.translate(lldbplatform.watchos))(func)
681
682
683 def skipIfbridgeOS(func):
684 return skipIfPlatform(lldbplatform.translate(lldbplatform.bridgeos))(func)
685
686
687 def skipIfDarwinEmbedded(func):
688 """Decorate the item to skip tests that should be skipped on Darwin armv7/arm64 targets."""
689 return skipIfPlatform(lldbplatform.translate(lldbplatform.darwin_embedded))(func)
690
691
692 def skipIfDarwinSimulator(func):
693 """Decorate the item to skip tests that should be skipped on Darwin simulator targets."""
694 return skipIfPlatform(lldbplatform.translate(lldbplatform.darwin_simulator))(func)
695
696
697 def skipIfFreeBSD(func):
698 """Decorate the item to skip tests that should be skipped on FreeBSD."""
699 return skipIfPlatform(["freebsd"])(func)
700
701
702 def skipIfNetBSD(func):
703 """Decorate the item to skip tests that should be skipped on NetBSD."""
704 return skipIfPlatform(["netbsd"])(func)
705
706
707 def skipIfDarwin(func):
708 """Decorate the item to skip tests that should be skipped on Darwin."""
709 return skipIfPlatform(lldbplatform.translate(lldbplatform.darwin_all))(func)
710
711
712 def skipIfLinux(func):
713 """Decorate the item to skip tests that should be skipped on Linux."""
714 return skipIfPlatform(["linux"])(func)
715
716
717 def skipIfWindows(func):
718 """Decorate the item to skip tests that should be skipped on Windows."""
719 return skipIfPlatform(["windows"])(func)
720
721
722 def skipIfWindowsAndNonEnglish(func):
723 """Decorate the item to skip tests that should be skipped on non-English locales on Windows."""
724
725 def is_Windows_NonEnglish():
726 if sys.platform != "win32":
727 return None
728 kernel = ctypes.windll.kernel32
729 if locale.windows_locale[kernel.GetUserDefaultUILanguage()] == "en_US":
730 return None
731 return "skipping non-English Windows locale"
732
733 return skipTestIfFn(is_Windows_NonEnglish)(func)
734
735
736 def skipUnlessWindows(func):
737 """Decorate the item to skip tests that should be skipped on any non-Windows platform."""
738 return skipUnlessPlatform(["windows"])(func)
739
740
741 def skipUnlessDarwin(func):
742 """Decorate the item to skip tests that should be skipped on any non Darwin platform."""
743 return skipUnlessPlatform(lldbplatformutil.getDarwinOSTriples())(func)
744
745
746 def skipUnlessTargetAndroid(func):
747 return unittest.skipUnless(
748 lldbplatformutil.target_is_android(), "requires target to be Android"
749 )(func)
750
751
752 def skipIfHostIncompatibleWithTarget(func):
753 """Decorate the item to skip tests when the host and target are incompatible."""
754
755 def is_host_incompatible_with_remote():
756 host_arch = lldbplatformutil.getLLDBArchitecture()
757 host_platform = lldbplatformutil.getHostPlatform()
758 target_arch = lldbplatformutil.getArchitecture()
759 target_platform = lldbplatformutil.getPlatform()
760 if (
761 not (target_arch == "x86_64" and host_arch == "i386")
762 and host_arch != target_arch
763 ):
764 return (
765 "skipping because target %s is not compatible with host architecture %s"
766 % (target_arch, host_arch)
767 )
768 if target_platform != host_platform:
769 return "skipping because target is %s but host is %s" % (
770 target_platform,
771 host_platform,
772 )
773 if lldbplatformutil.match_android_device(target_arch):
774 return "skipping because target is android"
775 return None
776
777 return skipTestIfFn(is_host_incompatible_with_remote)(func)
778
779
780 def skipIfPlatform(oslist):
781 """Decorate the item to skip tests if running on one of the listed platforms."""
782 # This decorator cannot be ported to `skipIf` yet because it is used on entire
783 # classes, which `skipIf` explicitly forbids.
784 return unittest.skipIf(
785 lldbplatformutil.getPlatform() in oslist, "skip on %s" % (", ".join(oslist))
786 )
787
788
789 def skipUnlessPlatform(oslist):
790 """Decorate the item to skip tests unless running on one of the listed platforms."""
791 # This decorator cannot be ported to `skipIf` yet because it is used on entire
792 # classes, which `skipIf` explicitly forbids.
793 return unittest.skipUnless(
794 lldbplatformutil.getPlatform() in oslist,
795 "requires one of %s" % (", ".join(oslist)),
796 )
797
798
799 def skipUnlessArch(arch):
800 """Decorate the item to skip tests unless running on the specified architecture."""
801
802 def arch_doesnt_match():
803 target_arch = lldbplatformutil.getArchitecture()
804 if arch != target_arch:
805 return "Test only runs on " + arch + ", but target arch is " + target_arch
806 return None
807
808 return skipTestIfFn(arch_doesnt_match)
809
810
811 def skipIfTargetAndroid(bugnumber=None, api_levels=None, archs=None):
812 """Decorator to skip tests when the target is Android.
813
814 Arguments:
815 api_levels - The API levels for which the test should be skipped. If
816 it is None, then the test will be skipped for all API levels.
817 arch - A sequence of architecture names specifying the architectures
818 for which a test is skipped. None means all architectures.
819 """
820 return skipTestIfFn(
821 _skip_fn_for_android("skipping for android", api_levels, archs), bugnumber
822 )
823
824
825 def skipUnlessAppleSilicon(func):
826 """Decorate the item to skip tests unless running on Apple Silicon."""
827
828 def not_apple_silicon():
829 if platform.system() != "Darwin" or lldbplatformutil.getArchitecture() not in [
830 "arm64",
831 "arm64e",
832 ]:
833 return "Test only runs on Apple Silicon"
834 return None
835
836 return skipTestIfFn(not_apple_silicon)(func)
837
838
839 def skipUnlessSupportedTypeAttribute(attr):
840 """Decorate the item to skip test unless Clang supports type __attribute__(attr)."""
841
842 def compiler_doesnt_support_struct_attribute():
843 compiler_path = lldbplatformutil.getCompiler()
844 f = tempfile.NamedTemporaryFile()
845 cmd = [lldbplatformutil.getCompiler(), "-x", "c++", "-c", "-o", f.name, "-"]
846 p = subprocess.Popen(
847 cmd,
848 stdin=subprocess.PIPE,
849 stdout=subprocess.PIPE,
850 stderr=subprocess.PIPE,
851 universal_newlines=True,
852 )
853 stdout, stderr = p.communicate("struct __attribute__((%s)) Test {};" % attr)
854 if attr in stderr:
855 return "Compiler does not support attribute %s" % (attr)
856 return None
857
858 return skipTestIfFn(compiler_doesnt_support_struct_attribute)
859
860
861 def skipUnlessHasCallSiteInfo(func):
862 """Decorate the function to skip testing unless call site info from clang is available."""
863
864 def is_compiler_clang_with_call_site_info():
865 compiler_path = lldbplatformutil.getCompiler()
866 compiler = os.path.basename(compiler_path)
867 if not compiler.startswith("clang"):
868 return "Test requires clang as compiler"
869
870 f = tempfile.NamedTemporaryFile()
871 cmd = (
872 "echo 'int main() {}' | "
873 "%s -g -glldb -O1 -S -emit-llvm -x c -o %s -" % (compiler_path, f.name)
874 )
875 if os.popen(cmd).close() is not None:
876 return "Compiler can't compile with call site info enabled"
877
878 with open(f.name, "r") as ir_output_file:
879 buf = ir_output_file.read()
880
881 if "DIFlagAllCallsDescribed" not in buf:
882 return "Compiler did not introduce DIFlagAllCallsDescribed IR flag"
883
884 return None
885
886 return skipTestIfFn(is_compiler_clang_with_call_site_info)(func)
887
888
889 def skipUnlessThreadSanitizer(func):
890 """Decorate the item to skip test unless Clang -fsanitize=thread is supported."""
891
892 def is_compiler_clang_with_thread_sanitizer():
893 if is_running_under_asan():
894 return "Thread sanitizer tests are disabled when runing under ASAN"
895
896 compiler_path = lldbplatformutil.getCompiler()
897 compiler = os.path.basename(compiler_path)
898 if not compiler.startswith("clang"):
899 return "Test requires clang as compiler"
900 if lldbplatformutil.getPlatform() == "windows":
901 return "TSAN tests not compatible with 'windows'"
902 # rdar://28659145 - TSAN tests don't look like they're supported on i386
903 if (
904 lldbplatformutil.getArchitecture() == "i386"
905 and platform.system() == "Darwin"
906 ):
907 return "TSAN tests not compatible with i386 targets"
908 if not _compiler_supports(compiler_path, "-fsanitize=thread"):
909 return "Compiler cannot compile with -fsanitize=thread"
910 return None
911
912 return skipTestIfFn(is_compiler_clang_with_thread_sanitizer)(func)
913
914
915 def skipUnlessUndefinedBehaviorSanitizer(func):
916 """Decorate the item to skip test unless -fsanitize=undefined is supported."""
917
918 def is_compiler_clang_with_ubsan():
919 if is_running_under_asan():
920 return (
921 "Undefined behavior sanitizer tests are disabled when runing under ASAN"
922 )
923
924 # We need to write out the object into a named temp file for inspection.
925 outputf = tempfile.NamedTemporaryFile()
926
927 # Try to compile with ubsan turned on.
928 if not _compiler_supports(
929 lldbplatformutil.getCompiler(),
930 "-fsanitize=undefined",
931 "int main() { int x = 0; return x / x; }",
932 outputf,
933 ):
934 return "Compiler cannot compile with -fsanitize=undefined"
935
936 # Check that we actually see ubsan instrumentation in the binary.
937 cmd = "nm %s" % outputf.name
938 with os.popen(cmd) as nm_output:
939 if "___ubsan_handle_divrem_overflow" not in nm_output.read():
940 return "Division by zero instrumentation is missing"
941
942 # Find the ubsan dylib.
943 # FIXME: This check should go away once compiler-rt gains support for __ubsan_on_report.
944 cmd = (
945 "%s -fsanitize=undefined -x c - -o - -### 2>&1"
946 % lldbplatformutil.getCompiler()
947 )
948 with os.popen(cmd) as cc_output:
949 driver_jobs = cc_output.read()
950 m = re.search(r'"([^"]+libclang_rt.ubsan_osx_dynamic.dylib)"', driver_jobs)
951 if not m:
952 return "Could not find the ubsan dylib used by the driver"
953 ubsan_dylib = m.group(1)
954
955 # Check that the ubsan dylib has special monitor hooks.
956 cmd = "nm -gU %s" % ubsan_dylib
957 with os.popen(cmd) as nm_output:
958 syms = nm_output.read()
959 if "___ubsan_on_report" not in syms:
960 return "Missing ___ubsan_on_report"
961 if "___ubsan_get_current_report_data" not in syms:
962 return "Missing ___ubsan_get_current_report_data"
963
964 # OK, this dylib + compiler works for us.
965 return None
966
967 return skipTestIfFn(is_compiler_clang_with_ubsan)(func)
968
969
970 def is_running_under_asan():
971 if "ASAN_OPTIONS" in os.environ:
972 return "ASAN unsupported"
973 return None
974
975
976 def skipUnlessAddressSanitizer(func):
977 """Decorate the item to skip test unless Clang -fsanitize=thread is supported."""
978
979 def is_compiler_with_address_sanitizer():
980 # Also don't run tests that use address sanitizer inside an
981 # address-sanitized LLDB. The tests don't support that
982 # configuration.
983 if is_running_under_asan():
984 return "Address sanitizer tests are disabled when runing under ASAN"
985
986 if lldbplatformutil.getPlatform() == "windows":
987 return "ASAN tests not compatible with 'windows'"
988 if not _compiler_supports(lldbplatformutil.getCompiler(), "-fsanitize=address"):
989 return "Compiler cannot compile with -fsanitize=address"
990 return None
991
992 return skipTestIfFn(is_compiler_with_address_sanitizer)(func)
993
994
995 def skipIfAsan(func):
996 """Skip this test if the environment is set up to run LLDB *itself* under ASAN."""
997 return skipTestIfFn(is_running_under_asan)(func)
998
999
1000 def skipUnlessAArch64MTELinuxCompiler(func):
1001 """Decorate the item to skip test unless MTE is supported by the test compiler."""
1002
1003 def is_toolchain_with_mte():
1004 compiler_path = lldbplatformutil.getCompiler()
1005 compiler = os.path.basename(compiler_path)
1006 f = tempfile.NamedTemporaryFile()
1007 if lldbplatformutil.getPlatform() == "windows":
1008 return "MTE tests are not compatible with 'windows'"
1009
1010 cmd = "echo 'int main() {}' | %s -x c -o %s -" % (compiler_path, f.name)
1011 if os.popen(cmd).close() is not None:
1012 # Cannot compile at all, don't skip the test
1013 # so that we report the broken compiler normally.
1014 return None
1015
1016 # We need the Linux headers and ACLE MTE intrinsics
1017 test_src = """
1018 #include <asm/hwcap.h>
1019 #include <arm_acle.h>
1020 #ifndef HWCAP2_MTE
1021 #error
1022 #endif
1023 int main() {
1024 void* ptr = __arm_mte_create_random_tag((void*)(0), 0);
1025 }"""
1026 cmd = "echo '%s' | %s -march=armv8.5-a+memtag -x c -o %s -" % (
1027 test_src,
1028 compiler_path,
1029 f.name,
1030 )
1031 if os.popen(cmd).close() is not None:
1032 return "Toolchain does not support MTE"
1033 return None
1034
1035 return skipTestIfFn(is_toolchain_with_mte)(func)
1036
1037
1038 def _get_bool_config(key, fail_value=True):
1039 """
1040 Returns the current LLDB's build config value.
1041 :param key The key to lookup in LLDB's build configuration.
1042 :param fail_value The error value to return when the key can't be found.
1043 Defaults to true so that if an unknown key is lookup up we rather
1044 enable more tests (that then fail) than silently skipping them.
1045 """
1046 config = lldb.SBDebugger.GetBuildConfiguration()
1047 value_node = config.GetValueForKey(key)
1048 return value_node.GetValueForKey("value").GetBooleanValue(fail_value)
1049
1050
1051 def _get_bool_config_skip_if_decorator(key):
1052 have = _get_bool_config(key)
1053 return unittest.skipIf(not have, "requires " + key)
1054
1055
1056 def skipIfCursesSupportMissing(func):
1057 return _get_bool_config_skip_if_decorator("curses")(func)
1058
1059
1060 def skipIfXmlSupportMissing(func):
1061 return _get_bool_config_skip_if_decorator("xml")(func)
1062
1063
1064 def skipIfEditlineSupportMissing(func):
1065 return _get_bool_config_skip_if_decorator("editline")(func)
1066
1067
1068 def skipIfEditlineWideCharSupportMissing(func):
1069 return _get_bool_config_skip_if_decorator("editline_wchar")(func)
1070
1071
1072 def skipIfFBSDVMCoreSupportMissing(func):
1073 return _get_bool_config_skip_if_decorator("fbsdvmcore")(func)
1074
1075
1076 def skipIfLLVMTargetMissing(target):
1077 config = lldb.SBDebugger.GetBuildConfiguration()
1078 targets = config.GetValueForKey("targets").GetValueForKey("value")
1079 found = False
1080 for i in range(targets.GetSize()):
1081 if targets.GetItemAtIndex(i).GetStringValue(99) == target:
1082 found = True
1083 break
1084
1085 return unittest.skipIf(not found, "requires " + target)
1086
1087
1088 # Call sysctl on darwin to see if a specified hardware feature is available on this machine.
1089 def skipUnlessFeature(feature):
1090 def is_feature_enabled():
1091 if platform.system() == "Darwin":
1092 try:
1093 DEVNULL = open(os.devnull, "w")
1094 output = subprocess.check_output(
1095 ["/usr/sbin/sysctl", feature], stderr=DEVNULL
1096 ).decode("utf-8")
1097 # If 'feature: 1' was output, then this feature is available and
1098 # the test should not be skipped.
1099 if re.match(r"%s: 1\s*" % feature, output):
1100 return None
1101 else:
1102 return "%s is not supported on this system." % feature
1103 except subprocess.CalledProcessError:
1104 return "%s is not supported on this system." % feature
1105
1106 return skipTestIfFn(is_feature_enabled)