1"""
2Test mpx-table command.
3"""
4
5from __future__ import print_function
6
7
8import os
9import time
10import re
11import lldb
12from lldbsuite.test.decorators import *
13from lldbsuite.test.lldbtest import *
14from lldbsuite.test import lldbutil
15
16
17class TestMPXTable(TestBase):
18
19    mydir = TestBase.compute_mydir(__file__)
20
21    def setUp(self):
22        TestBase.setUp(self)
23
24    @skipIf(compiler="clang")
25    @skipIf(oslist=no_match(['linux']))
26    @skipIf(archs=no_match(['i386', 'x86_64']))
27    @skipIf(compiler="gcc", compiler_version=["<", "5"]) #GCC version >= 5 supports
28    #Intel(R) Memory Protection Extensions (Intel(R) MPX).
29    def test_show_command(self):
30        """Test 'mpx-table show' command"""
31        self.build()
32
33        lldb_exec_dir = os.environ["LLDB_IMPLIB_DIR"]
34        lldb_lib_dir = os.path.join(lldb_exec_dir, os.pardir, "lib")
35        plugin_file = os.path.join(lldb_lib_dir, "liblldbIntelFeatures.so")
36        if not os.path.isfile(plugin_file):
37            self.skipTest("features plugin missing.")
38        plugin_command = " "
39        seq = ("plugin", "load", plugin_file)
40        plugin_command = plugin_command.join(seq)
41        self.runCmd(plugin_command)
42
43        exe = os.path.join(os.getcwd(), "a.out")
44        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
45
46        self.b1 = line_number('main.cpp', '// Break 1.')
47        self.b2 = line_number('main.cpp', '// Break 2.')
48        self.b3 = line_number('main.cpp', '// Break 3.')
49        self.b4 = line_number('main.cpp', '// Break 4.')
50        lldbutil.run_break_set_by_file_and_line(self, "main.cpp", self.b1, num_expected_locations=1)
51        lldbutil.run_break_set_by_file_and_line(self, "main.cpp", self.b2, num_expected_locations=1)
52        lldbutil.run_break_set_by_file_and_line(self, "main.cpp", self.b3, num_expected_locations=1)
53        lldbutil.run_break_set_by_file_and_line(self, "main.cpp", self.b4, num_expected_locations=1)
54        self.runCmd("run", RUN_SUCCEEDED)
55
56        target = self.dbg.GetSelectedTarget()
57        process = target.GetProcess()
58
59        if (process.GetState() == lldb.eStateExited):
60            self.skipTest("Intel(R) MPX is not supported.")
61        else:
62            self.expect("thread backtrace", STOPPED_DUE_TO_BREAKPOINT,
63                        substrs = ["stop reason = breakpoint 1."])
64
65        self.expect("mpx-table show a",
66                     substrs = ['lbound = 0x',
67                                ', ubound = 0x',
68                                '(pointer value = 0x',
69                                ', metadata = 0x',
70                                ')'],
71                     error = False)
72
73        self.expect("continue", STOPPED_DUE_TO_BREAKPOINT,
74                     substrs = ["stop reason = breakpoint 2."])
75
76        # Check that out of scope pointer cannot be reached.
77        #
78        self.expect("mpx-table show a",
79                     substrs = ['Invalid pointer.'],
80                     error = True)
81
82        self.expect("mpx-table show tmp",
83                     substrs = ['lbound = 0x',
84                                ', ubound = 0x',
85                                '(pointer value = 0x',
86                                ', metadata = 0x',
87                                ')'],
88                     error = False)
89
90        self.expect("continue", STOPPED_DUE_TO_BREAKPOINT,
91                     substrs = ["stop reason = breakpoint 3."])
92
93        # Check that the pointer value is correctly updated.
94        #
95        self.expect("mpx-table show tmp",
96                     substrs = ['lbound = 0x',
97                                ', ubound = 0x',
98                                '(pointer value = 0x2',
99                                ', metadata = 0x',
100                                ')'],
101                     error = False)
102
103        self.expect("continue", STOPPED_DUE_TO_BREAKPOINT,
104                     substrs = ["stop reason = breakpoint 4."])
105
106        # After going back to main(), check that out of scope pointer cannot be
107        # reached.
108        #
109        self.expect("mpx-table show tmp",
110                     substrs = ['Invalid pointer.'],
111                     error = True)
112
113        self.expect("mpx-table show a",
114                     substrs = ['lbound = 0x',
115                                ', ubound = 0x',
116                                '(pointer value = 0x',
117                                ', metadata = 0x',
118                                ')'],
119                     error = False)
120
121    def test_set_command(self):
122        """Test 'mpx-table set' command"""
123        self.build()
124
125        lldb_exec_dir = os.environ["LLDB_IMPLIB_DIR"]
126        lldb_lib_dir = os.path.join(lldb_exec_dir, os.pardir, "lib")
127        plugin_file = os.path.join(lldb_lib_dir, "liblldbIntelFeatures.so")
128        if not os.path.isfile(plugin_file):
129            self.skipTest("features plugin missing.")
130        plugin_command = " "
131        seq = ("plugin", "load", plugin_file)
132        plugin_command = plugin_command.join(seq)
133        self.runCmd(plugin_command)
134
135        exe = os.path.join(os.getcwd(), "a.out")
136        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
137
138        self.b1 = line_number('main.cpp', '// Break 1.')
139        lldbutil.run_break_set_by_file_and_line(self, "main.cpp", self.b1, num_expected_locations=1)
140        self.runCmd("run", RUN_SUCCEEDED)
141
142        target = self.dbg.GetSelectedTarget()
143        process = target.GetProcess()
144
145        if (process.GetState() == lldb.eStateExited):
146            self.skipTest("Intel(R) MPX is not supported.")
147        else:
148            self.expect("thread backtrace", STOPPED_DUE_TO_BREAKPOINT,
149                        substrs = ["stop reason = breakpoint 1."])
150
151        # Check that the BT Entry doesn't already contain the test values.
152        #
153        self.expect("mpx-table show a", matching=False,
154                     substrs = ['lbound = 0xcafecafe',
155                                ', ubound = 0xbeefbeef'])
156
157        # Set the test values.
158        #
159        self.expect("mpx-table set a 0xcafecafe 0xbeefbeef", error = False)
160
161        # Verify that the test values have been correctly written in the BT
162        # entry.
163        #
164        self.expect("mpx-table show a",
165                     substrs = ['lbound = 0xcafecafe',
166                                ', ubound = 0xbeefbeef'],
167                     error = False)
168
169
170