xref: /llvm-project/lldb/test/API/commands/expression/dont_allow_jit/TestAllowJIT.py (revision 1eeeab82c6eb185f5139e633a59c2dbcb15616e4)
1"""
2Test that --allow-jit=false does disallow JITting:
3"""
4
5
6import lldb
7import lldbsuite.test.lldbutil as lldbutil
8from lldbsuite.test.lldbtest import *
9from lldbsuite.test.decorators import *
10
11
12class TestAllowJIT(TestBase):
13    # If your test case doesn't stress debug info, then
14    # set this to true.  That way it won't be run once for
15    # each debug info format.
16    NO_DEBUG_INFO_TESTCASE = True
17
18    def test_allow_jit_expr_command(self):
19        """Test the --allow-jit command line flag"""
20        self.build()
21        self.main_source_file = lldb.SBFileSpec("main.c")
22        self.expr_cmd_test()
23
24    def test_allow_jit_options(self):
25        """Test the SetAllowJIT SBExpressionOption setting"""
26        self.build()
27        self.main_source_file = lldb.SBFileSpec("main.c")
28        self.expr_options_test()
29
30    def expr_cmd_test(self):
31        (target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint(
32            self, "Set a breakpoint here", self.main_source_file
33        )
34
35        frame = thread.GetFrameAtIndex(0)
36
37        # First make sure we can call the function with
38        interp = self.dbg.GetCommandInterpreter()
39        self.expect("expr --allow-jit 1 -- call_me(10)", substrs=["(int) $", "= 18"])
40        # Now make sure it fails with the "can't IR interpret message" if allow-jit is false:
41        self.expect(
42            "expr --allow-jit 0 -- call_me(10)",
43            error=True,
44            substrs=["Can't evaluate the expression without a running target"],
45        )
46
47    def expr_options_test(self):
48        (target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint(
49            self, "Set a breakpoint here", self.main_source_file
50        )
51
52        frame = thread.GetFrameAtIndex(0)
53
54        # First make sure we can call the function with the default option set.
55        options = lldb.SBExpressionOptions()
56        # Check that the default is to allow JIT:
57        self.assertTrue(options.GetAllowJIT(), "Default is true")
58
59        # Now use the options:
60        result = frame.EvaluateExpression("call_me(10)", options)
61        self.assertSuccess(result.GetError())
62        self.assertEqual(result.GetValueAsSigned(), 18, "got the right value.")
63
64        # Now disallow JIT and make sure it fails:
65        options.SetAllowJIT(False)
66        # Check that we got the right value:
67        self.assertFalse(options.GetAllowJIT(), "Got False after setting to False")
68
69        # Again use it and ensure we fail:
70        result = frame.EvaluateExpression("call_me(10)", options)
71        self.assertTrue(result.GetError().Fail(), "expression failed with no JIT")
72        self.assertIn(
73            "Can't evaluate the expression without a running target",
74            result.GetError().GetCString(),
75            "Got right error",
76        )
77
78        # Finally set the allow JIT value back to true and make sure that works:
79        options.SetAllowJIT(True)
80        self.assertTrue(options.GetAllowJIT(), "Set back to True correctly")
81
82        # And again, make sure this works:
83        result = frame.EvaluateExpression("call_me(10)", options)
84        self.assertSuccess(result.GetError())
85        self.assertEqual(result.GetValueAsSigned(), 18, "got the right value.")
86
87    def test_allow_jit_with_top_level(self):
88        """Test combined --allow-jit and --top-level flags"""
89        # Can't force interpreting for top-level expressions which are always
90        # injected.
91        self.expect(
92            "expr --allow-jit false --top-level -- int i;",
93            error=True,
94            substrs=["Can't disable JIT compilation for top-level expressions."],
95        )
96
97        self.build()
98        lldbutil.run_to_source_breakpoint(
99            self, "Set a breakpoint here", lldb.SBFileSpec("main.c")
100        )
101        # Allowing JITing for top-level expressions is redundant but should work.
102        self.expect(
103            "expr --allow-jit true --top-level -- int top_level_f() { return 2; }"
104        )
105        # Make sure we actually declared a working top-level function.
106        self.expect_expr("top_level_f()", result_value="2")
107