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