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