1
2import gdbremote_testcase
3from lldbsuite.test.decorators import *
4from lldbsuite.test.lldbtest import *
5from lldbsuite.test import lldbutil
6
7
8supported_linux_archs = ["x86_64", "i386"]
9supported_oses = ["linux", "windows"]
10
11class TestGdbRemoteMemoryAllocation(gdbremote_testcase.GdbRemoteTestCaseBase):
12
13    mydir = TestBase.compute_mydir(__file__)
14
15    def allocate(self, size, permissions):
16        self.test_sequence.add_log_lines(["read packet: $_M{:x},{}#00".format(size, permissions),
17                                          {"direction": "send",
18                                           "regex":
19                                           r"^\$([0-9a-f]+)#[0-9a-fA-F]{2}$",
20                                           "capture": {
21                                               1: "addr"}},
22                                          ],
23                                         True)
24        context = self.expect_gdbremote_sequence()
25        self.assertIsNotNone(context)
26
27        addr = int(context.get("addr"), 16)
28        self.test_sequence.add_log_lines(["read packet: $qMemoryRegionInfo:{:x}#00".format(addr),
29                                          {"direction": "send",
30                                           "regex":
31                                           r"^\$start:([0-9a-fA-F]+);size:([0-9a-fA-F]+);permissions:([rwx]*);.*#[0-9a-fA-F]{2}$",
32                                           "capture": {
33                                               1: "addr",
34                                               2: "size",
35                                               3: "permissions"}},
36                                           "read packet: $_m{:x}#00".format(addr),
37                                           "send packet: $OK#00",
38                                          ],
39                                         True)
40        context = self.expect_gdbremote_sequence()
41        self.assertIsNotNone(context)
42
43        self.assertEqual(addr, int(context.get("addr"), 16))
44        self.assertLessEqual(size, int(context.get("size"), 16))
45        self.assertEqual(permissions, context.get("permissions"))
46
47    @skipIf(oslist=no_match(supported_oses))
48    @skipIf(oslist=["linux"], archs=no_match(supported_linux_archs))
49    @expectedFailureAll(oslist=["windows"]) # Memory allocated with incorrect permissions
50    @llgs_test
51    def test_supported(self):
52        """Make sure (de)allocation works on platforms where it's supposed to
53        work"""
54        self.init_llgs_test()
55        self.build()
56        self.set_inferior_startup_launch()
57        procs = self.prep_debug_monitor_and_inferior()
58
59        self.allocate(0x1000, "r")
60        self.allocate(0x2000, "rw")
61        self.allocate(0x100, "rx")
62        self.allocate(0x1100, "rwx")
63
64    @skipIf(oslist=["linux"], archs=supported_linux_archs)
65    @skipIf(oslist=supported_oses)
66    @llgs_test
67    def test_unsupported(self):
68        """Make sure we get an "unsupported" error on platforms where the
69        feature is not implemented."""
70
71        self.init_llgs_test()
72        self.build()
73        self.set_inferior_startup_launch()
74        procs = self.prep_debug_monitor_and_inferior()
75
76        self.test_sequence.add_log_lines(["read packet: $_M1000,rw#00",
77                                           "send packet: $#00",
78                                          ],
79                                         True)
80        self.expect_gdbremote_sequence()
81
82    @llgs_test
83    def test_bad_packet(self):
84        """Make sure we get a proper error for malformed packets."""
85
86        self.init_llgs_test()
87        self.build()
88        self.set_inferior_startup_launch()
89        procs = self.prep_debug_monitor_and_inferior()
90
91        def e():
92            return {"direction": "send",
93                    "regex":
94                    r"^\$E([0-9a-fA-F]+){2}#[0-9a-fA-F]{2}$"}
95
96        self.test_sequence.add_log_lines([
97            "read packet: $_M#00", e(),
98            "read packet: $_M1x#00", e(),
99            "read packet: $_M1:#00", e(),
100            "read packet: $_M1,q#00", e(),
101            "read packet: $_m#00", e(),
102            ], True)
103        self.expect_gdbremote_sequence()
104