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