File: TestMemoryRead.py

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (171 lines) | stat: -rw-r--r-- 6,574 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
"""
Test the 'memory read' command.
"""

import lldb
import lldbsuite.test.lldbutil as lldbutil

from lldbsuite.test.decorators import *
from lldbsuite.test.lldbtest import *


class MemoryReadTestCase(TestBase):
    NO_DEBUG_INFO_TESTCASE = True

    def build_run_stop(self):
        self.build()
        lldbutil.run_to_source_breakpoint(
            self, "// break here", lldb.SBFileSpec("main.c")
        )

    def test_memory_read_c_string(self):
        """Test that reading memory as a c string respects the size limit given
        and warns if the null terminator is missing."""
        self.build_run_stop()

        # The size here is the size in memory so it includes the null terminator.
        cmd = 'memory read --format "c-string" --size {} &my_string'

        # Size matches the size of the array.
        self.expect(cmd.format(8), substrs=['"abcdefg"'])

        # If size would take us past the terminator we stop at the terminator.
        self.expect(cmd.format(10), substrs=['"abcdefg"'])

        # Size 3 means 2 chars and a terminator. So we print 2 chars but warn because
        # the third isn't 0 as expected.
        self.expect(cmd.format(3), substrs=['"ab"'])
        self.assertRegex(
            self.res.GetError(),
            "unable to find a NULL terminated string at 0x[0-9A-Fa-f]+."
            " Consider increasing the maximum read length.",
        )

    def test_memory_read(self):
        """Test the 'memory read' command with plain and vector formats."""
        self.build_run_stop()

        # (lldb) memory read -f d -c 1 `&argc`
        # 0x7fff5fbff9a0: 1
        self.runCmd("memory read -f d -c 1 `&argc`")

        # Find the starting address for variable 'argc' to verify later that the
        # '--format uint32_t[] --size 4 --count 4' option increments the address
        # correctly.
        line = self.res.GetOutput().splitlines()[0]
        items = line.split(":")
        address = int(items[0], 0)
        argc = int(items[1], 0)
        self.assertGreater(address, 0)
        self.assertEqual(argc, 1)

        # (lldb) memory read --format uint32_t[] --size 4 --count 4 `&argc`
        # 0x7fff5fbff9a0: {0x00000001}
        # 0x7fff5fbff9a4: {0x00000000}
        # 0x7fff5fbff9a8: {0x0ec0bf27}
        # 0x7fff5fbff9ac: {0x215db505}
        self.runCmd("memory read --format uint32_t[] --size 4 --count 4 `&argc`")
        lines = self.res.GetOutput().splitlines()
        for i in range(4):
            if i == 0:
                # Verify that the printout for argc is correct.
                self.assertEqual(argc, int(lines[i].split(":")[1].strip(" {}"), 0))
            addr = int(lines[i].split(":")[0], 0)
            # Verify that the printout for addr is incremented correctly.
            self.assertEqual(addr, (address + i * 4))

        # (lldb) memory read --format char[] --size 7 --count 1 `&my_string`
        # 0x7fff5fbff990: {abcdefg}
        self.expect(
            "memory read --format char[] --size 7 --count 1 `&my_string`",
            substrs=["abcdefg"],
        )

        # (lldb) memory read --format 'hex float' --size 16 `&argc`
        # 0x7fff5fbff5b0: error: unsupported byte size (16) for hex float
        # format
        self.expect(
            "memory read --format 'hex float' --size 16 `&argc`",
            substrs=["unsupported byte size (16) for hex float format"],
        )

        self.expect(
            "memory read --format 'float' --count 1 --size 8 `&my_double`",
            substrs=["1234."],
        )

        # (lldb) memory read --format 'float' --count 1 --size 20 `&my_double`
        # 0x7fff5fbff598: error: unsupported byte size (20) for float format
        self.expect(
            "memory read --format 'float' --count 1 --size 20 `&my_double`",
            substrs=["unsupported byte size (20) for float format"],
        )

        self.expect(
            "memory read --type int --count 5 `&my_ints[0]`",
            substrs=["(int) 0x", "2", "4", "6", "8", "10"],
        )

        self.expect(
            "memory read --type int --count 5 --format hex `&my_ints[0]`",
            substrs=["(int) 0x", "0x", "0a"],
        )

        self.expect(
            "memory read --type int --count 5 --offset 5 `&my_ints[0]`",
            substrs=["(int) 0x", "12", "14", "16", "18", "20"],
        )

        # the gdb format specifier and the size in characters for
        # the returned values including the 0x prefix.
        variations = [["b", 4], ["h", 6], ["w", 10], ["g", 18]]
        for v in variations:
            formatter = v[0]
            expected_object_length = v[1]
            self.runCmd("memory read --gdb-format 4%s &my_uint64s" % formatter)
            lines = self.res.GetOutput().splitlines()
            objects_read = []
            for l in lines:
                objects_read.extend(l.split(":")[1].split())
            # Check that we got back 4 0x0000 etc bytes
            for o in objects_read:
                self.assertEqual(len(o), expected_object_length)
            self.assertEqual(len(objects_read), 4)

    def test_memory_read_file(self):
        self.build_run_stop()
        res = lldb.SBCommandReturnObject()
        self.ci.HandleCommand("memory read -f d -c 1 `&argc`", res)
        self.assertTrue(res.Succeeded(), "memory read failed:" + res.GetError())

        # Record golden output.
        golden_output = res.GetOutput()

        memory_read_file = self.getBuildArtifact("memory-read-output")

        def check_file_content(expected):
            with open(memory_read_file) as f:
                lines = f.readlines()
                lines = [s.strip() for s in lines]
                expected = [s.strip() for s in expected]
                self.assertEqual(lines, expected)

        # Sanity check.
        self.runCmd("memory read -f d -c 1 -o '{}' `&argc`".format(memory_read_file))
        check_file_content([golden_output])

        # Write some garbage to the file.
        with open(memory_read_file, "w") as f:
            f.write("some garbage")

        # Make sure the file is truncated when we run the command again.
        self.runCmd("memory read -f d -c 1 -o '{}' `&argc`".format(memory_read_file))
        check_file_content([golden_output])

        # Make sure the file is appended when we run the command with --append-outfile.
        self.runCmd(
            "memory read -f d -c 1 -o '{}' --append-outfile `&argc`".format(
                memory_read_file
            )
        )
        check_file_content([golden_output, golden_output])