1"""
2Test that --allow-jit=false does disallow JITting:
3"""
4
5
6
7import lldb
8import lldbsuite.test.lldbutil as lldbutil
9from lldbsuite.test.lldbtest import *
10from lldbsuite.test.decorators import *
11
12class TestAllowJIT(TestBase):
13
14    mydir = TestBase.compute_mydir(__file__)
15
16    # If your test case doesn't stress debug info, then
17    # set this to true.  That way it won't be run once for
18    # each debug info format.
19    NO_DEBUG_INFO_TESTCASE = True
20
21    def test_allow_jit_expr_command(self):
22        """Test the --allow-jit command line flag"""
23        self.build()
24        self.main_source_file = lldb.SBFileSpec("main.c")
25        self.expr_cmd_test()
26
27    def test_allow_jit_options(self):
28        """Test the SetAllowJIT SBExpressionOption setting"""
29        self.build()
30        self.main_source_file = lldb.SBFileSpec("main.c")
31        self.expr_options_test()
32
33    def expr_cmd_test(self):
34        (target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint(self,
35                                   "Set a breakpoint here", self.main_source_file)
36
37        frame = thread.GetFrameAtIndex(0)
38
39        # First make sure we can call the function with
40        interp = self.dbg.GetCommandInterpreter()
41        self.expect("expr --allow-jit 1 -- call_me(10)",
42                    substrs = ["(int) $", "= 18"])
43        # Now make sure it fails with the "can't IR interpret message" if allow-jit is false:
44        self.expect("expr --allow-jit 0 -- call_me(10)",
45                    error=True,
46                    substrs = ["Can't evaluate the expression without a running target"])
47
48    def expr_options_test(self):
49        (target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint(self,
50                                   "Set a breakpoint here", self.main_source_file)
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.assertEqual(options.GetAllowJIT(), True, "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.assertEqual(options.GetAllowJIT(), False, "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("Can't evaluate the expression without a running target", result.GetError().GetCString(), "Got right error")
73
74        # Finally set the allow JIT value back to true and make sure that works:
75        options.SetAllowJIT(True)
76        self.assertEqual(options.GetAllowJIT(), True, "Set back to True correctly")
77
78        # And again, make sure this works:
79        result = frame.EvaluateExpression("call_me(10)", options)
80        self.assertSuccess(result.GetError())
81        self.assertEqual(result.GetValueAsSigned(), 18, "got the right value.")
82
83    def test_allow_jit_with_top_level(self):
84        """Test combined --allow-jit and --top-level flags"""
85        # Can't force interpreting for top-level expressions which are always
86        # injected.
87        self.expect("expr --allow-jit false --top-level -- int i;", error=True,
88                    substrs=["Can't disable JIT compilation for top-level expressions."])
89
90        self.build()
91        lldbutil.run_to_source_breakpoint(self, "Set a breakpoint here", lldb.SBFileSpec("main.c"))
92        # Allowing JITing for top-level expressions is redundant but should work.
93        self.expect("expr --allow-jit true --top-level -- int top_level_f() { return 2; }")
94        # Make sure we actually declared a working top-level function.
95        self.expect_expr("top_level_f()", result_value="2")
96