xref: /llvm-project/lldb/test/API/tools/lldb-server/memory-allocation/TestGdbRemoteMemoryAllocation.py (revision 2238dcc39358353cac21df75c3c3286ab20b8f53)
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
9
10class TestGdbRemoteMemoryAllocation(gdbremote_testcase.GdbRemoteTestCaseBase):
11    def allocate(self, size, permissions):
12        self.test_sequence.add_log_lines(
13            [
14                "read packet: $_M{:x},{}#00".format(size, permissions),
15                {
16                    "direction": "send",
17                    "regex": r"^\$([0-9a-f]+)#[0-9a-fA-F]{2}$",
18                    "capture": {1: "addr"},
19                },
20            ],
21            True,
22        )
23        context = self.expect_gdbremote_sequence()
24        self.assertIsNotNone(context)
25
26        addr = int(context.get("addr"), 16)
27        self.test_sequence.add_log_lines(
28            [
29                "read packet: $qMemoryRegionInfo:{:x}#00".format(addr),
30                {
31                    "direction": "send",
32                    "regex": r"^\$start:([0-9a-fA-F]+);size:([0-9a-fA-F]+);permissions:([rwx]*);.*#[0-9a-fA-F]{2}$",
33                    "capture": {1: "addr", 2: "size", 3: "permissions"},
34                },
35                "read packet: $_m{:x}#00".format(addr),
36                "send packet: $OK#00",
37            ],
38            True,
39        )
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    @expectedFailureDarwin(
50        archs=["arm64", "arm64e"]
51    )  # Memory cannot be writable and executable
52    @expectedFailureAll(
53        oslist=["windows"]
54    )  # Memory allocated with incorrect permissions
55    def test_supported(self):
56        """Make sure (de)allocation works on platforms where it's supposed to
57        work"""
58        self.build()
59        self.set_inferior_startup_launch()
60        procs = self.prep_debug_monitor_and_inferior()
61
62        self.allocate(0x1000, "r")
63        self.allocate(0x2000, "rw")
64        self.allocate(0x100, "rx")
65        self.allocate(0x1100, "rwx")
66
67    @skipIf(oslist=["linux"], archs=supported_linux_archs)
68    @skipIf(oslist=supported_oses)
69    def test_unsupported(self):
70        """Make sure we get an "unsupported" error on platforms where the
71        feature is not implemented."""
72
73        self.build()
74        self.set_inferior_startup_launch()
75        procs = self.prep_debug_monitor_and_inferior()
76
77        self.test_sequence.add_log_lines(
78            [
79                "read packet: $_M1000,rw#00",
80                "send packet: $#00",
81            ],
82            True,
83        )
84        self.expect_gdbremote_sequence()
85
86    def test_bad_packet(self):
87        """Make sure we get a proper error for malformed packets."""
88
89        self.build()
90        self.set_inferior_startup_launch()
91        procs = self.prep_debug_monitor_and_inferior()
92
93        def e():
94            return {
95                "direction": "send",
96                "regex": r"^\$E([0-9a-fA-F]+){2}#[0-9a-fA-F]{2}$",
97            }
98
99        self.test_sequence.add_log_lines(
100            [
101                "read packet: $_M#00",
102                e(),
103                "read packet: $_M1x#00",
104                e(),
105                "read packet: $_M1:#00",
106                e(),
107                "read packet: $_M1,q#00",
108                e(),
109                "read packet: $_m#00",
110                e(),
111            ],
112            True,
113        )
114        self.expect_gdbremote_sequence()
115