12c4226f8SPavel Labathimport gdbremote_testcase
22c4226f8SPavel Labathfrom lldbsuite.test.decorators import *
32c4226f8SPavel Labathfrom lldbsuite.test.lldbtest import *
42c4226f8SPavel Labathfrom lldbsuite.test import lldbutil
52c4226f8SPavel Labath
62c4226f8SPavel Labathsupported_linux_archs = ["x86_64", "i386"]
74e0e79ddSPavel Labathsupported_oses = ["linux", "windows"]+lldbplatformutil.getDarwinOSTriples()
82c4226f8SPavel Labath
92c4226f8SPavel Labathclass TestGdbRemoteMemoryAllocation(gdbremote_testcase.GdbRemoteTestCaseBase):
102c4226f8SPavel Labath
112c4226f8SPavel Labath    def allocate(self, size, permissions):
122c4226f8SPavel Labath        self.test_sequence.add_log_lines(["read packet: $_M{:x},{}#00".format(size, permissions),
132c4226f8SPavel Labath                                          {"direction": "send",
142c4226f8SPavel Labath                                           "regex":
152c4226f8SPavel Labath                                           r"^\$([0-9a-f]+)#[0-9a-fA-F]{2}$",
162c4226f8SPavel Labath                                           "capture": {
172c4226f8SPavel Labath                                               1: "addr"}},
182c4226f8SPavel Labath                                          ],
192c4226f8SPavel Labath                                         True)
202c4226f8SPavel Labath        context = self.expect_gdbremote_sequence()
212c4226f8SPavel Labath        self.assertIsNotNone(context)
222c4226f8SPavel Labath
232c4226f8SPavel Labath        addr = int(context.get("addr"), 16)
242c4226f8SPavel Labath        self.test_sequence.add_log_lines(["read packet: $qMemoryRegionInfo:{:x}#00".format(addr),
252c4226f8SPavel Labath                                          {"direction": "send",
262c4226f8SPavel Labath                                           "regex":
272c4226f8SPavel Labath                                           r"^\$start:([0-9a-fA-F]+);size:([0-9a-fA-F]+);permissions:([rwx]*);.*#[0-9a-fA-F]{2}$",
282c4226f8SPavel Labath                                           "capture": {
292c4226f8SPavel Labath                                               1: "addr",
302c4226f8SPavel Labath                                               2: "size",
312c4226f8SPavel Labath                                               3: "permissions"}},
322c4226f8SPavel Labath                                           "read packet: $_m{:x}#00".format(addr),
332c4226f8SPavel Labath                                           "send packet: $OK#00",
342c4226f8SPavel Labath                                          ],
352c4226f8SPavel Labath                                         True)
362c4226f8SPavel Labath        context = self.expect_gdbremote_sequence()
372c4226f8SPavel Labath        self.assertIsNotNone(context)
382c4226f8SPavel Labath
392c4226f8SPavel Labath        self.assertEqual(addr, int(context.get("addr"), 16))
402c4226f8SPavel Labath        self.assertLessEqual(size, int(context.get("size"), 16))
412c4226f8SPavel Labath        self.assertEqual(permissions, context.get("permissions"))
422c4226f8SPavel Labath
432c4226f8SPavel Labath    @skipIf(oslist=no_match(supported_oses))
442c4226f8SPavel Labath    @skipIf(oslist=["linux"], archs=no_match(supported_linux_archs))
45*0d166520SJonas Devlieghere    @expectedFailureDarwin(archs=["arm64", "arm64e"]) # Memory cannot be writable and executable
46a1ab2b77SPavel Labath    @expectedFailureAll(oslist=["windows"]) # Memory allocated with incorrect permissions
472c4226f8SPavel Labath    def test_supported(self):
482c4226f8SPavel Labath        """Make sure (de)allocation works on platforms where it's supposed to
492c4226f8SPavel Labath        work"""
502c4226f8SPavel Labath        self.build()
512c4226f8SPavel Labath        self.set_inferior_startup_launch()
522c4226f8SPavel Labath        procs = self.prep_debug_monitor_and_inferior()
532c4226f8SPavel Labath
542c4226f8SPavel Labath        self.allocate(0x1000, "r")
552c4226f8SPavel Labath        self.allocate(0x2000, "rw")
562c4226f8SPavel Labath        self.allocate(0x100, "rx")
572c4226f8SPavel Labath        self.allocate(0x1100, "rwx")
582c4226f8SPavel Labath
592c4226f8SPavel Labath    @skipIf(oslist=["linux"], archs=supported_linux_archs)
60a1ab2b77SPavel Labath    @skipIf(oslist=supported_oses)
612c4226f8SPavel Labath    def test_unsupported(self):
622c4226f8SPavel Labath        """Make sure we get an "unsupported" error on platforms where the
632c4226f8SPavel Labath        feature is not implemented."""
642c4226f8SPavel Labath
652c4226f8SPavel Labath        self.build()
662c4226f8SPavel Labath        self.set_inferior_startup_launch()
672c4226f8SPavel Labath        procs = self.prep_debug_monitor_and_inferior()
682c4226f8SPavel Labath
692c4226f8SPavel Labath        self.test_sequence.add_log_lines(["read packet: $_M1000,rw#00",
702c4226f8SPavel Labath                                           "send packet: $#00",
712c4226f8SPavel Labath                                          ],
722c4226f8SPavel Labath                                         True)
732c4226f8SPavel Labath        self.expect_gdbremote_sequence()
742c4226f8SPavel Labath
752c4226f8SPavel Labath    def test_bad_packet(self):
762c4226f8SPavel Labath        """Make sure we get a proper error for malformed packets."""
772c4226f8SPavel Labath
782c4226f8SPavel Labath        self.build()
792c4226f8SPavel Labath        self.set_inferior_startup_launch()
802c4226f8SPavel Labath        procs = self.prep_debug_monitor_and_inferior()
812c4226f8SPavel Labath
822c4226f8SPavel Labath        def e():
832c4226f8SPavel Labath            return {"direction": "send",
842c4226f8SPavel Labath                    "regex":
852c4226f8SPavel Labath                    r"^\$E([0-9a-fA-F]+){2}#[0-9a-fA-F]{2}$"}
862c4226f8SPavel Labath
872c4226f8SPavel Labath        self.test_sequence.add_log_lines([
882c4226f8SPavel Labath            "read packet: $_M#00", e(),
892c4226f8SPavel Labath            "read packet: $_M1x#00", e(),
902c4226f8SPavel Labath            "read packet: $_M1:#00", e(),
912c4226f8SPavel Labath            "read packet: $_M1,q#00", e(),
922c4226f8SPavel Labath            "read packet: $_m#00", e(),
932c4226f8SPavel Labath            ], True)
942c4226f8SPavel Labath        self.expect_gdbremote_sequence()
95