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    @expectedFailureDarwin(archs=["arm64", "arm64e"]) # Memory cannot be writable and executable
48    @expectedFailureAll(oslist=["windows"]) # Memory allocated with incorrect permissions
49    def test_supported(self):
50        """Make sure (de)allocation works on platforms where it's supposed to
51        work"""
52        self.build()
53        self.set_inferior_startup_launch()
54        procs = self.prep_debug_monitor_and_inferior()
55
56        self.allocate(0x1000, "r")
57        self.allocate(0x2000, "rw")
58        self.allocate(0x100, "rx")
59        self.allocate(0x1100, "rwx")
60
61    @skipIf(oslist=["linux"], archs=supported_linux_archs)
62    @skipIf(oslist=supported_oses)
63    def test_unsupported(self):
64        """Make sure we get an "unsupported" error on platforms where the
65        feature is not implemented."""
66
67        self.build()
68        self.set_inferior_startup_launch()
69        procs = self.prep_debug_monitor_and_inferior()
70
71        self.test_sequence.add_log_lines(["read packet: $_M1000,rw#00",
72                                           "send packet: $#00",
73                                          ],
74                                         True)
75        self.expect_gdbremote_sequence()
76
77    def test_bad_packet(self):
78        """Make sure we get a proper error for malformed packets."""
79
80        self.build()
81        self.set_inferior_startup_launch()
82        procs = self.prep_debug_monitor_and_inferior()
83
84        def e():
85            return {"direction": "send",
86                    "regex":
87                    r"^\$E([0-9a-fA-F]+){2}#[0-9a-fA-F]{2}$"}
88
89        self.test_sequence.add_log_lines([
90            "read packet: $_M#00", e(),
91            "read packet: $_M1x#00", e(),
92            "read packet: $_M1:#00", e(),
93            "read packet: $_M1,q#00", e(),
94            "read packet: $_m#00", e(),
95            ], True)
96        self.expect_gdbremote_sequence()
97