xref: /xnu-12377.41.6/tools/lldbmacros/tests/lldb_tests/test_scripted_process.py (revision bbb1b6f9e71b8cdde6e5cd6f4841f207dee3d828)
1*bbb1b6f9SApple OSS Distributions##
2*bbb1b6f9SApple OSS Distributions# Copyright (c) 2023 Apple Inc. All rights reserved.
3*bbb1b6f9SApple OSS Distributions#
4*bbb1b6f9SApple OSS Distributions# @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*bbb1b6f9SApple OSS Distributions#
6*bbb1b6f9SApple OSS Distributions# This file contains Original Code and/or Modifications of Original Code
7*bbb1b6f9SApple OSS Distributions# as defined in and that are subject to the Apple Public Source License
8*bbb1b6f9SApple OSS Distributions# Version 2.0 (the 'License'). You may not use this file except in
9*bbb1b6f9SApple OSS Distributions# compliance with the License. The rights granted to you under the License
10*bbb1b6f9SApple OSS Distributions# may not be used to create, or enable the creation or redistribution of,
11*bbb1b6f9SApple OSS Distributions# unlawful or unlicensed copies of an Apple operating system, or to
12*bbb1b6f9SApple OSS Distributions# circumvent, violate, or enable the circumvention or violation of, any
13*bbb1b6f9SApple OSS Distributions# terms of an Apple operating system software license agreement.
14*bbb1b6f9SApple OSS Distributions#
15*bbb1b6f9SApple OSS Distributions# Please obtain a copy of the License at
16*bbb1b6f9SApple OSS Distributions# http://www.opensource.apple.com/apsl/ and read it before using this file.
17*bbb1b6f9SApple OSS Distributions#
18*bbb1b6f9SApple OSS Distributions# The Original Code and all software distributed under the License are
19*bbb1b6f9SApple OSS Distributions# distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*bbb1b6f9SApple OSS Distributions# EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*bbb1b6f9SApple OSS Distributions# INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*bbb1b6f9SApple OSS Distributions# FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*bbb1b6f9SApple OSS Distributions# Please see the License for the specific language governing rights and
24*bbb1b6f9SApple OSS Distributions# limitations under the License.
25*bbb1b6f9SApple OSS Distributions#
26*bbb1b6f9SApple OSS Distributions# @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*bbb1b6f9SApple OSS Distributions##
28*bbb1b6f9SApple OSS Distributions
29*bbb1b6f9SApple OSS Distributions# pylint: disable=invalid-name
30*bbb1b6f9SApple OSS Distributions
31*bbb1b6f9SApple OSS Distributions""" Tests that ScriptedProcess mock behaves as expected. """
32*bbb1b6f9SApple OSS Distributions
33*bbb1b6f9SApple OSS Distributionsimport unittest.mock
34*bbb1b6f9SApple OSS Distributionsimport io
35*bbb1b6f9SApple OSS Distributions
36*bbb1b6f9SApple OSS Distributionsimport lldb
37*bbb1b6f9SApple OSS Distributionsfrom lldbtest.testcase import LLDBTestCase
38*bbb1b6f9SApple OSS Distributionsfrom lldbmock.memorymock import RawMock
39*bbb1b6f9SApple OSS Distributionsfrom lldbmock.utils import lookup_type
40*bbb1b6f9SApple OSS Distributions
41*bbb1b6f9SApple OSS Distributions
42*bbb1b6f9SApple OSS Distributionsclass ScriptedProcessTest(LLDBTestCase):
43*bbb1b6f9SApple OSS Distributions    """ Scripted process unit test. """
44*bbb1b6f9SApple OSS Distributions
45*bbb1b6f9SApple OSS Distributions    def test_RawMock(self):
46*bbb1b6f9SApple OSS Distributions        """ Install simple raw memory mock into a target. """
47*bbb1b6f9SApple OSS Distributions
48*bbb1b6f9SApple OSS Distributions        RAWMOCK_ADDR = 0xffffffff00000000
49*bbb1b6f9SApple OSS Distributions
50*bbb1b6f9SApple OSS Distributions        mock = RawMock(100)
51*bbb1b6f9SApple OSS Distributions        mock.setData(b"lldb-process-mock\x00")
52*bbb1b6f9SApple OSS Distributions        self.add_mock(RAWMOCK_ADDR, mock)
53*bbb1b6f9SApple OSS Distributions
54*bbb1b6f9SApple OSS Distributions        # Test is using LLDB command intentionaly.
55*bbb1b6f9SApple OSS Distributions        res = self.run_command(f'x/s {RAWMOCK_ADDR:#x}')
56*bbb1b6f9SApple OSS Distributions
57*bbb1b6f9SApple OSS Distributions        self.assertTrue(res.Succeeded())
58*bbb1b6f9SApple OSS Distributions        self.assertEqual(
59*bbb1b6f9SApple OSS Distributions            res.GetOutput(),
60*bbb1b6f9SApple OSS Distributions            f'{RAWMOCK_ADDR:#x}: "lldb-process-mock"\n'
61*bbb1b6f9SApple OSS Distributions        )
62*bbb1b6f9SApple OSS Distributions
63*bbb1b6f9SApple OSS Distributions    def test_RawMockIO(self):
64*bbb1b6f9SApple OSS Distributions        """ Populate simple raw memory mock from provided IO. """
65*bbb1b6f9SApple OSS Distributions
66*bbb1b6f9SApple OSS Distributions        RAWMOCK_ADDR = 0xffffffff50000000
67*bbb1b6f9SApple OSS Distributions
68*bbb1b6f9SApple OSS Distributions        mock = RawMock.fromBufferedIO(io.BytesIO(b"lldb-io-mock\x00"))
69*bbb1b6f9SApple OSS Distributions        self.add_mock(RAWMOCK_ADDR, mock)
70*bbb1b6f9SApple OSS Distributions
71*bbb1b6f9SApple OSS Distributions        # Test is using LLDB command intentionaly.
72*bbb1b6f9SApple OSS Distributions        res = self.run_command(f'x/s {RAWMOCK_ADDR:#x}')
73*bbb1b6f9SApple OSS Distributions
74*bbb1b6f9SApple OSS Distributions        self.assertTrue(res.Succeeded())
75*bbb1b6f9SApple OSS Distributions        self.assertEqual(
76*bbb1b6f9SApple OSS Distributions            res.GetOutput(),
77*bbb1b6f9SApple OSS Distributions            f'{RAWMOCK_ADDR:#x}: "lldb-io-mock"\n'
78*bbb1b6f9SApple OSS Distributions        )
79*bbb1b6f9SApple OSS Distributions
80*bbb1b6f9SApple OSS Distributions    def test_DuplicateMock(self):
81*bbb1b6f9SApple OSS Distributions        """ Install same simple mock to two VA locations. """
82*bbb1b6f9SApple OSS Distributions
83*bbb1b6f9SApple OSS Distributions        mock = RawMock(100)
84*bbb1b6f9SApple OSS Distributions        mock.setData(b"shared-mock\x00")
85*bbb1b6f9SApple OSS Distributions        self.add_mock(0xffffffff10000000, mock)
86*bbb1b6f9SApple OSS Distributions        self.add_mock(0xffffffff20000000, mock)
87*bbb1b6f9SApple OSS Distributions
88*bbb1b6f9SApple OSS Distributions        # Test both locations
89*bbb1b6f9SApple OSS Distributions        for addr in ('0xffffffff10000000', '0xffffffff20000000'):
90*bbb1b6f9SApple OSS Distributions            res = self.run_command(f'x/s {addr}')
91*bbb1b6f9SApple OSS Distributions
92*bbb1b6f9SApple OSS Distributions            self.assertTrue(res.Succeeded())
93*bbb1b6f9SApple OSS Distributions            self.assertEqual(
94*bbb1b6f9SApple OSS Distributions                res.GetOutput(),
95*bbb1b6f9SApple OSS Distributions                f'{addr}: "shared-mock"\n'
96*bbb1b6f9SApple OSS Distributions            )
97*bbb1b6f9SApple OSS Distributions
98*bbb1b6f9SApple OSS Distributions    def test_MockConflict(self):
99*bbb1b6f9SApple OSS Distributions        """ Check that we can't add overlapping mocks. """
100*bbb1b6f9SApple OSS Distributions
101*bbb1b6f9SApple OSS Distributions        mock = RawMock(16)
102*bbb1b6f9SApple OSS Distributions        self.add_mock(0x12345, mock)
103*bbb1b6f9SApple OSS Distributions        with self.assertRaises(ValueError):
104*bbb1b6f9SApple OSS Distributions            mock = RawMock(16)
105*bbb1b6f9SApple OSS Distributions            self.add_mock(0x12346, mock)
106*bbb1b6f9SApple OSS Distributions
107*bbb1b6f9SApple OSS Distributions    def test_SimpleMock(self):
108*bbb1b6f9SApple OSS Distributions        """ Mock instance of a simple type. """
109*bbb1b6f9SApple OSS Distributions
110*bbb1b6f9SApple OSS Distributions        UINT_ADDR = 0xffffffff11223344
111*bbb1b6f9SApple OSS Distributions
112*bbb1b6f9SApple OSS Distributions        self.create_mock('uint32_t', UINT_ADDR).setData(0x1234)
113*bbb1b6f9SApple OSS Distributions
114*bbb1b6f9SApple OSS Distributions        res = self.run_command(f'p/x *((uint32_t *){UINT_ADDR:#x})')
115*bbb1b6f9SApple OSS Distributions
116*bbb1b6f9SApple OSS Distributions        self.assertTrue(res.Succeeded())
117*bbb1b6f9SApple OSS Distributions        self.assertEqual(res.GetOutput(), "(uint32_t) 0x00001234\n")
118*bbb1b6f9SApple OSS Distributions
119*bbb1b6f9SApple OSS Distributions    @unittest.skipIf(LLDBTestCase.kernel().startswith('mach.release'),
120*bbb1b6f9SApple OSS Distributions                     "Not available in RELEASE embedded")
121*bbb1b6f9SApple OSS Distributions    def test_CompoundMock(self):
122*bbb1b6f9SApple OSS Distributions        """ Mock instance of simple structure. """
123*bbb1b6f9SApple OSS Distributions
124*bbb1b6f9SApple OSS Distributions        DOFHELPER_ADDR = 0xffffffff11220000
125*bbb1b6f9SApple OSS Distributions
126*bbb1b6f9SApple OSS Distributions        # Construct simple data structure mock.
127*bbb1b6f9SApple OSS Distributions        self.create_mock('struct dof_helper', DOFHELPER_ADDR).fromDict({
128*bbb1b6f9SApple OSS Distributions            'dofhp_mod': b'mock-mod',
129*bbb1b6f9SApple OSS Distributions            'dofhp_addr': 0x1234,
130*bbb1b6f9SApple OSS Distributions            'dofhp_dof': 0x5678
131*bbb1b6f9SApple OSS Distributions        })
132*bbb1b6f9SApple OSS Distributions
133*bbb1b6f9SApple OSS Distributions        # Construct SBValue on top of the mock.
134*bbb1b6f9SApple OSS Distributions        addr = self.target.ResolveLoadAddress(DOFHELPER_ADDR)
135*bbb1b6f9SApple OSS Distributions        sbv = self.target.CreateValueFromAddress(
136*bbb1b6f9SApple OSS Distributions            'test', addr, lookup_type('dof_helper_t'))
137*bbb1b6f9SApple OSS Distributions
138*bbb1b6f9SApple OSS Distributions        self.assertTrue(sbv.IsValid() and sbv.error.success)
139*bbb1b6f9SApple OSS Distributions
140*bbb1b6f9SApple OSS Distributions        # Check that LLDB SBAPI returns correct values from mock.
141*bbb1b6f9SApple OSS Distributions        err = lldb.SBError()
142*bbb1b6f9SApple OSS Distributions        self.assertEqual(
143*bbb1b6f9SApple OSS Distributions            sbv.GetChildMemberWithName('dofhp_mod').GetData()
144*bbb1b6f9SApple OSS Distributions               .GetString(err, 0),
145*bbb1b6f9SApple OSS Distributions            "mock-mod"
146*bbb1b6f9SApple OSS Distributions        )
147*bbb1b6f9SApple OSS Distributions        self.assertEqual(
148*bbb1b6f9SApple OSS Distributions            sbv.GetChildMemberWithName('dofhp_addr').GetValueAsUnsigned(),
149*bbb1b6f9SApple OSS Distributions            0x1234
150*bbb1b6f9SApple OSS Distributions        )
151*bbb1b6f9SApple OSS Distributions        self.assertEqual(
152*bbb1b6f9SApple OSS Distributions            sbv.GetChildMemberWithName('dofhp_dof').GetValueAsUnsigned(),
153*bbb1b6f9SApple OSS Distributions            0x5678
154*bbb1b6f9SApple OSS Distributions        )
155*bbb1b6f9SApple OSS Distributions
156*bbb1b6f9SApple OSS Distributions    @unittest.skipIf(LLDBTestCase.kernel().startswith('mach.release'),
157*bbb1b6f9SApple OSS Distributions                     "Not available in RELEASE embedded")
158*bbb1b6f9SApple OSS Distributions    def test_CompoundMock_UpdateProperty(self):
159*bbb1b6f9SApple OSS Distributions        """ Test that mock can deserilize properties from update. """
160*bbb1b6f9SApple OSS Distributions
161*bbb1b6f9SApple OSS Distributions        mock = self.create_mock('struct dof_helper', 0xffffffff55555555)
162*bbb1b6f9SApple OSS Distributions        mock.setData(
163*bbb1b6f9SApple OSS Distributions            b'hello-mock' + b'\x00'*54 +
164*bbb1b6f9SApple OSS Distributions            0xfeedface.to_bytes(length=8, byteorder='little') +
165*bbb1b6f9SApple OSS Distributions            0xdeadbeef.to_bytes(length=8, byteorder='little'))
166*bbb1b6f9SApple OSS Distributions
167*bbb1b6f9SApple OSS Distributions        # Test that mock has de-serialized correctly whole blob above.
168*bbb1b6f9SApple OSS Distributions        self.assertEqual(mock.dofhp_mod[:10], b'hello-mock')
169*bbb1b6f9SApple OSS Distributions        self.assertEqual(mock.dofhp_addr, 0xfeedface)
170*bbb1b6f9SApple OSS Distributions        self.assertEqual(mock.dofhp_dof, 0xdeadbeef)
171*bbb1b6f9SApple OSS Distributions
172*bbb1b6f9SApple OSS Distributions    def test_UnionMock(self):
173*bbb1b6f9SApple OSS Distributions        """ Test that simple union/bitfield propagates property updates. """
174*bbb1b6f9SApple OSS Distributions
175*bbb1b6f9SApple OSS Distributions        mock = self.create_mock('kds_ptr', 0xffffffff30000000)
176*bbb1b6f9SApple OSS Distributions
177*bbb1b6f9SApple OSS Distributions        mock.buffer_index = 0b111111111111111111111  # 21-bits
178*bbb1b6f9SApple OSS Distributions        self.assertEqual(mock.raw, 0x001fffff)
179*bbb1b6f9SApple OSS Distributions
180*bbb1b6f9SApple OSS Distributions        mock.buffer_index = 0
181*bbb1b6f9SApple OSS Distributions        mock.offset = 0b11111111111  # 11-bits
182*bbb1b6f9SApple OSS Distributions        self.assertEqual(mock.raw, 0xffe00000)
183*bbb1b6f9SApple OSS Distributions
184*bbb1b6f9SApple OSS Distributions        mock.raw = 0xffdffffe
185*bbb1b6f9SApple OSS Distributions        self.assertEqual(mock.buffer_index, 0x001ffffe)
186*bbb1b6f9SApple OSS Distributions        self.assertEqual(mock.offset, 0x7fe)
187*bbb1b6f9SApple OSS Distributions
188*bbb1b6f9SApple OSS Distributions    def test_MockArray(self):
189*bbb1b6f9SApple OSS Distributions        """ Test simple mock of char array. """
190*bbb1b6f9SApple OSS Distributions
191*bbb1b6f9SApple OSS Distributions        STR_ADDR = 0xffffffff33004400
192*bbb1b6f9SApple OSS Distributions        PTR_ADDR = 0xffffffff44000000
193*bbb1b6f9SApple OSS Distributions
194*bbb1b6f9SApple OSS Distributions        # Construct an array in memory.
195*bbb1b6f9SApple OSS Distributions        arrtype = lookup_type('char').GetArrayType(256)
196*bbb1b6f9SApple OSS Distributions        marray = self.create_mock(arrtype, STR_ADDR)
197*bbb1b6f9SApple OSS Distributions        marray.setData(b'Hello World\x00')
198*bbb1b6f9SApple OSS Distributions
199*bbb1b6f9SApple OSS Distributions        # Create a pointer to the array
200*bbb1b6f9SApple OSS Distributions        ptrtype = lookup_type('char').GetPointerType()
201*bbb1b6f9SApple OSS Distributions        mstr = self.create_mock(ptrtype, PTR_ADDR)
202*bbb1b6f9SApple OSS Distributions        mstr.setData(STR_ADDR)
203*bbb1b6f9SApple OSS Distributions
204*bbb1b6f9SApple OSS Distributions        # Let LLDB print it.
205*bbb1b6f9SApple OSS Distributions        addr = self.target.ResolveLoadAddress(PTR_ADDR)
206*bbb1b6f9SApple OSS Distributions        sbv = self.target.CreateValueFromAddress('str', addr, ptrtype)
207*bbb1b6f9SApple OSS Distributions        self.assertTrue(sbv.IsValid() and sbv.error.success)
208*bbb1b6f9SApple OSS Distributions
209*bbb1b6f9SApple OSS Distributions        err = lldb.SBError()
210*bbb1b6f9SApple OSS Distributions        self.assertEqual(sbv.GetPointeeData(0, 256).GetString(err, 0),
211*bbb1b6f9SApple OSS Distributions                         'Hello World')
212*bbb1b6f9SApple OSS Distributions
213*bbb1b6f9SApple OSS Distributions    def test_MockTypedArray(self):
214*bbb1b6f9SApple OSS Distributions        """ Test array of compound types. """
215*bbb1b6f9SApple OSS Distributions
216*bbb1b6f9SApple OSS Distributions        ARRAY_ADDR = 0xffffffff44003300
217*bbb1b6f9SApple OSS Distributions
218*bbb1b6f9SApple OSS Distributions        arrtype = lookup_type('proc').GetArrayType(10)
219*bbb1b6f9SApple OSS Distributions        self.create_mock(arrtype, ARRAY_ADDR).fromDict({
220*bbb1b6f9SApple OSS Distributions            '0': {
221*bbb1b6f9SApple OSS Distributions                'p_comm': b'bar-foo\x00'
222*bbb1b6f9SApple OSS Distributions            },
223*bbb1b6f9SApple OSS Distributions            '1': {
224*bbb1b6f9SApple OSS Distributions                'p_comm': b'foo-bar\x00'
225*bbb1b6f9SApple OSS Distributions            }
226*bbb1b6f9SApple OSS Distributions        })
227*bbb1b6f9SApple OSS Distributions
228*bbb1b6f9SApple OSS Distributions        res = self.run_command(f'p/x ((proc_t){ARRAY_ADDR:#x})[1].p_comm')
229*bbb1b6f9SApple OSS Distributions        self.assertTrue(res.Succeeded())
230*bbb1b6f9SApple OSS Distributions
231*bbb1b6f9SApple OSS Distributions        # Check that elements don't overlap somehow
232*bbb1b6f9SApple OSS Distributions        # (use SBValue to exercise LLDB's internals)
233*bbb1b6f9SApple OSS Distributions        addr = self.target.ResolveLoadAddress(ARRAY_ADDR)
234*bbb1b6f9SApple OSS Distributions        sbv = self.target.CreateValueFromAddress('proc_arr', addr, arrtype)
235*bbb1b6f9SApple OSS Distributions        self.assertTrue(sbv.IsValid() and sbv.error.success)
236*bbb1b6f9SApple OSS Distributions
237*bbb1b6f9SApple OSS Distributions        err = lldb.SBError()
238*bbb1b6f9SApple OSS Distributions        self.assertEqual(
239*bbb1b6f9SApple OSS Distributions            sbv.GetChildAtIndex(0).GetChildMemberWithName('p_comm')
240*bbb1b6f9SApple OSS Distributions               .GetData().GetString(err, 0),
241*bbb1b6f9SApple OSS Distributions            'bar-foo'
242*bbb1b6f9SApple OSS Distributions        )
243*bbb1b6f9SApple OSS Distributions        self.assertEqual(
244*bbb1b6f9SApple OSS Distributions            sbv.GetChildAtIndex(1).GetChildMemberWithName('p_comm')
245*bbb1b6f9SApple OSS Distributions               .GetData().GetString(err, 0),
246*bbb1b6f9SApple OSS Distributions            'foo-bar'
247*bbb1b6f9SApple OSS Distributions        )
248*bbb1b6f9SApple OSS Distributions
249*bbb1b6f9SApple OSS Distributions    def test_NoNewAttributes(self):
250*bbb1b6f9SApple OSS Distributions        """ Test that mock instances are properly frozen after creation. """
251*bbb1b6f9SApple OSS Distributions
252*bbb1b6f9SApple OSS Distributions        mock = self.create_mock(lookup_type('uint32_t'))
253*bbb1b6f9SApple OSS Distributions
254*bbb1b6f9SApple OSS Distributions        with self.assertRaises(TypeError):
255*bbb1b6f9SApple OSS Distributions            mock.foo = 5
256*bbb1b6f9SApple OSS Distributions
257*bbb1b6f9SApple OSS Distributions    @unittest.skipIf(LLDBTestCase.kernel().startswith('mach.release'),
258*bbb1b6f9SApple OSS Distributions                     "Not available in RELEASE embedded")
259*bbb1b6f9SApple OSS Distributions    def test_NestedStruct(self):
260*bbb1b6f9SApple OSS Distributions        """ Test that nested mocks properly serialize. """
261*bbb1b6f9SApple OSS Distributions
262*bbb1b6f9SApple OSS Distributions        PROVNAME_ADDR = 0xffffffff70707070
263*bbb1b6f9SApple OSS Distributions        DTHELPER_ADDR = 0xffffffff80808080
264*bbb1b6f9SApple OSS Distributions
265*bbb1b6f9SApple OSS Distributions        # Setup mock with fake values.
266*bbb1b6f9SApple OSS Distributions        arrtype = lookup_type('char').GetArrayType(256)
267*bbb1b6f9SApple OSS Distributions        marray = self.create_mock(arrtype, PROVNAME_ADDR)
268*bbb1b6f9SApple OSS Distributions        marray.setData(b'test-prov\x00')
269*bbb1b6f9SApple OSS Distributions
270*bbb1b6f9SApple OSS Distributions        sbtype = lookup_type('dtrace_helper_provdesc_t')
271*bbb1b6f9SApple OSS Distributions        mock = self.create_mock(sbtype, DTHELPER_ADDR)
272*bbb1b6f9SApple OSS Distributions
273*bbb1b6f9SApple OSS Distributions        mock.dthpv_provname = PROVNAME_ADDR
274*bbb1b6f9SApple OSS Distributions        mock.dthpv_pattr.dtpa_mod.dtat_name = 0x5
275*bbb1b6f9SApple OSS Distributions
276*bbb1b6f9SApple OSS Distributions        # Serializer should prevent overflowing a member's size.
277*bbb1b6f9SApple OSS Distributions        with self.assertRaises(OverflowError):
278*bbb1b6f9SApple OSS Distributions            mock.dthpv_pattr.dtpa_args.dtat_class = 0x7777
279*bbb1b6f9SApple OSS Distributions
280*bbb1b6f9SApple OSS Distributions        mock.dthpv_pattr.dtpa_args.dtat_class = 0x77
281*bbb1b6f9SApple OSS Distributions
282*bbb1b6f9SApple OSS Distributions        # Obtain SBValue and check modified members
283*bbb1b6f9SApple OSS Distributions        addr = self.target.ResolveLoadAddress(DTHELPER_ADDR)
284*bbb1b6f9SApple OSS Distributions        sbv = self.target.CreateValueFromAddress('test', addr, sbtype)
285*bbb1b6f9SApple OSS Distributions        self.assertTrue(sbv.IsValid() and sbv.error.success)
286*bbb1b6f9SApple OSS Distributions
287*bbb1b6f9SApple OSS Distributions        err = lldb.SBError()
288*bbb1b6f9SApple OSS Distributions        self.assertEqual(
289*bbb1b6f9SApple OSS Distributions            sbv.GetChildMemberWithName('dthpv_provname')
290*bbb1b6f9SApple OSS Distributions               .GetPointeeData(0, 256).GetString(err, 0),
291*bbb1b6f9SApple OSS Distributions            'test-prov'
292*bbb1b6f9SApple OSS Distributions        )
293*bbb1b6f9SApple OSS Distributions        self.assertEqual(
294*bbb1b6f9SApple OSS Distributions            sbv.GetValueForExpressionPath('.dthpv_pattr.dtpa_mod.dtat_name')
295*bbb1b6f9SApple OSS Distributions               .GetValueAsUnsigned(),
296*bbb1b6f9SApple OSS Distributions            0x5
297*bbb1b6f9SApple OSS Distributions        )
298*bbb1b6f9SApple OSS Distributions        self.assertEqual(
299*bbb1b6f9SApple OSS Distributions            sbv.GetValueForExpressionPath('.dthpv_pattr.dtpa_args.dtat_class')
300*bbb1b6f9SApple OSS Distributions               .GetValueAsUnsigned(),
301*bbb1b6f9SApple OSS Distributions            0x77
302*bbb1b6f9SApple OSS Distributions        )
303*bbb1b6f9SApple OSS Distributions
304*bbb1b6f9SApple OSS Distributions    @unittest.mock.patch('xnu.kern.globals.proc_struct_size', 2048)
305*bbb1b6f9SApple OSS Distributions    def test_ProxyMock(self):
306*bbb1b6f9SApple OSS Distributions        """ Test anonymous members forwarding. """
307*bbb1b6f9SApple OSS Distributions
308*bbb1b6f9SApple OSS Distributions        PROC_ADDR = 0xffffffff90909090
309*bbb1b6f9SApple OSS Distributions        PROC_RO_ADDR = 0xffffff0040404040
310*bbb1b6f9SApple OSS Distributions
311*bbb1b6f9SApple OSS Distributions        mock = self.create_mock('proc', PROC_ADDR)
312*bbb1b6f9SApple OSS Distributions
313*bbb1b6f9SApple OSS Distributions        mock.p_list.le_next = 0x12345678
314*bbb1b6f9SApple OSS Distributions        mock.p_smr_node.smrn_next = 0x12345678
315*bbb1b6f9SApple OSS Distributions        mock.p_pid = 12345
316*bbb1b6f9SApple OSS Distributions        mock.p_argc = 0x5
317*bbb1b6f9SApple OSS Distributions        mock.p_textvp = 0xfeedface
318*bbb1b6f9SApple OSS Distributions        mock.p_lflag = 0x00000002
319*bbb1b6f9SApple OSS Distributions
320*bbb1b6f9SApple OSS Distributions        mock.p_comm = b'foobar'  # Use forwarding property
321*bbb1b6f9SApple OSS Distributions
322*bbb1b6f9SApple OSS Distributions        task = self.create_mock('task', PROC_ADDR + 2048)
323*bbb1b6f9SApple OSS Distributions
324*bbb1b6f9SApple OSS Distributions        task.effective_policy.tep_sup_active = 0
325*bbb1b6f9SApple OSS Distributions        task.effective_policy.tep_darwinbg = 0
326*bbb1b6f9SApple OSS Distributions        task.effective_policy.tep_lowpri_cpu = 1
327*bbb1b6f9SApple OSS Distributions        task.t_flags = 0x00800000
328*bbb1b6f9SApple OSS Distributions
329*bbb1b6f9SApple OSS Distributions        self.create_mock('proc_ro', PROC_RO_ADDR)
330*bbb1b6f9SApple OSS Distributions
331*bbb1b6f9SApple OSS Distributions        mock.p_proc_ro = PROC_RO_ADDR
332*bbb1b6f9SApple OSS Distributions        task.bsd_info_ro = PROC_RO_ADDR
333*bbb1b6f9SApple OSS Distributions
334*bbb1b6f9SApple OSS Distributions        # Populate and test mock.
335*bbb1b6f9SApple OSS Distributions        res = self.run_command(f'p/x ((proc_t){PROC_ADDR:#x})->p_comm')
336*bbb1b6f9SApple OSS Distributions        self.assertEqual(res.GetOutput(), '(command_t) "foobar"\n')
337*bbb1b6f9SApple OSS Distributions        self.assertTrue(res.Succeeded())
338*bbb1b6f9SApple OSS Distributions
339*bbb1b6f9SApple OSS Distributions        # Modify mock and test again.
340*bbb1b6f9SApple OSS Distributions        mock.p_forkcopy.p_comm = b'barfoo'  # Sub-mock prop wins
341*bbb1b6f9SApple OSS Distributions        self.invalidate_cache()
342*bbb1b6f9SApple OSS Distributions
343*bbb1b6f9SApple OSS Distributions        res = self.run_command(f'p/x ((proc_t){PROC_ADDR:#x})->p_comm')
344*bbb1b6f9SApple OSS Distributions        self.assertEqual(res.GetOutput(), '(command_t) "barfoo"\n')
345*bbb1b6f9SApple OSS Distributions        self.assertTrue(res.Succeeded())
346