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