xref: /llvm-project/lldb/test/API/commands/expression/ir-interpreter/TestIRInterpreter.py (revision 7822e5dbf1cb4be2d73eb13da27b04de684c318b)
1"""
2Test the IR interpreter
3"""
4
5
6import lldb
7from lldbsuite.test.decorators import *
8from lldbsuite.test.lldbtest import *
9from lldbsuite.test import lldbutil
10
11
12class IRInterpreterTestCase(TestBase):
13    NO_DEBUG_INFO_TESTCASE = True
14
15    def time_expression(self, expr, options):
16        start = time.time()
17        res = self.target.EvaluateExpression(expr, options)
18        return res, time.time() - start
19
20    def test_interpreter_timeout(self):
21        """Test the timeout logic in the IRInterpreter."""
22        self.build()
23        self.target = self.dbg.CreateTarget(self.getBuildArtifact("a.out"))
24        self.assertTrue(self.target, VALID_TARGET)
25
26        # A non-trivial infinite loop.
27        inf_loop = "for (unsigned i = 0; i < 100; ++i) --i; 1"
28        timeout_error = "Reached timeout while interpreting expression"
29        options = lldb.SBExpressionOptions()
30
31        # This is an IRInterpreter specific test, so disable the JIT.
32        options.SetAllowJIT(False)
33
34        # We use a 500ms timeout.
35        options.SetTimeoutInMicroSeconds(500000)
36        res, duration_sec = self.time_expression(inf_loop, options)
37        self.assertIn(timeout_error, str(res.GetError()))
38
39        # Depending on the machine load the expression might take quite some
40        # time, so give the time a generous upper bound.
41        self.assertLess(duration_sec, 15)
42
43        # Try a simple one second timeout.
44        options.SetTimeoutInMicroSeconds(1000000)
45        res, duration_sec = self.time_expression(inf_loop, options)
46        self.assertIn(timeout_error, str(res.GetError()))
47        # Anything within 5% of 1s is fine, to account for machine differences.
48        self.assertGreaterEqual(duration_sec, 0.95)
49        self.assertLess(duration_sec, 30)
50
51    def test_interpreter_interrupt(self):
52        """Test interrupting the IRInterpreter."""
53        self.build()
54        self.target = self.dbg.CreateTarget(self.getBuildArtifact("a.out"))
55        self.assertTrue(self.target, VALID_TARGET)
56
57        # A non-trivial infinite loop.
58        inf_loop = "for (unsigned i = 0; i < 100; ++i) --i; 1"
59
60        options = lldb.SBExpressionOptions()
61
62        # This is an IRInterpreter specific test, so disable the JIT.
63        options.SetAllowJIT(False)
64
65        # Make sure we have a pretty long (10s) timeout so we have a chance to
66        # interrupt the interpreted expression.
67        options.SetTimeoutInMicroSeconds(10000000)
68
69        self.dbg.RequestInterrupt()
70
71        self.dbg.SetAsync(True)
72        res = self.target.EvaluateExpression(inf_loop, options)
73        self.dbg.SetAsync(False)
74
75        # Be sure to turn this off again:
76        def cleanup():
77            if self.dbg.InterruptRequested():
78                self.dbg.CancelInterruptRequest()
79
80        self.addTearDownHook(cleanup)
81
82        interrupt_error = "Interrupted while interpreting expression"
83        self.assertIn(interrupt_error, str(res.GetError()))
84
85    def setUp(self):
86        # Call super's setUp().
87        TestBase.setUp(self)
88        # Find the line number to break for main.c.
89        self.line = line_number("main.c", "// Set breakpoint here")
90
91        # Disable confirmation prompt to avoid infinite wait
92        self.runCmd("settings set auto-confirm true")
93        self.addTearDownHook(lambda: self.runCmd("settings clear auto-confirm"))
94
95    def build_and_run(self):
96        """Test the IR interpreter"""
97        self.build()
98
99        self.runCmd("file " + self.getBuildArtifact("a.out"), CURRENT_EXECUTABLE_SET)
100
101        lldbutil.run_break_set_by_file_and_line(
102            self, "main.c", self.line, num_expected_locations=1, loc_exact=False
103        )
104
105        self.runCmd("run", RUN_SUCCEEDED)
106
107    @add_test_categories(["pyapi"])
108    # getpid() is POSIX, among other problems, see bug
109    @expectedFailureAll(oslist=["windows"], bugnumber="http://llvm.org/pr21765")
110    def test_ir_interpreter(self):
111        self.build_and_run()
112
113        options = lldb.SBExpressionOptions()
114        options.SetLanguage(lldb.eLanguageTypeC_plus_plus)
115
116        set_up_expressions = [
117            "int $i = 9",
118            "int $j = 3",
119            "int $k = 5",
120            "unsigned long long $ull = -1",
121            "unsigned $u = -1",
122        ]
123
124        expressions = [
125            "$i + $j",
126            "$i - $j",
127            "$i * $j",
128            "$i / $j",
129            "$i % $k",
130            "$i << $j",
131            "$i & $j",
132            "$i | $j",
133            "$i ^ $j",
134            "($ull & -1) == $u",
135        ]
136
137        for expression in set_up_expressions:
138            self.frame().EvaluateExpression(expression, options)
139
140        for expression in expressions:
141            interp_expression = expression
142            jit_expression = "(int)getpid(); " + expression
143
144            interp_result = (
145                self.frame()
146                .EvaluateExpression(interp_expression, options)
147                .GetValueAsSigned()
148            )
149            jit_result = (
150                self.frame()
151                .EvaluateExpression(jit_expression, options)
152                .GetValueAsSigned()
153            )
154
155            self.assertEqual(
156                interp_result, jit_result, "While evaluating " + expression
157            )
158
159    def test_type_conversions(self):
160        target = self.dbg.GetDummyTarget()
161        short_val = target.EvaluateExpression("(short)-1")
162        self.assertEqual(short_val.GetValueAsSigned(), -1)
163        long_val = target.EvaluateExpression("(long) " + short_val.GetName())
164        self.assertEqual(long_val.GetValueAsSigned(), -1)
165