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# pylint: disable=protected-access 31*bbb1b6f9SApple OSS Distributions 32*bbb1b6f9SApple OSS Distributions""" Test process.py """ 33*bbb1b6f9SApple OSS Distributions 34*bbb1b6f9SApple OSS Distributionsimport contextlib 35*bbb1b6f9SApple OSS Distributionsimport io 36*bbb1b6f9SApple OSS Distributionsimport unittest 37*bbb1b6f9SApple OSS Distributionsfrom lldbtest.testcase import LLDBTestCase 38*bbb1b6f9SApple OSS Distributionsfrom lldbmock.utils import lookup_type 39*bbb1b6f9SApple OSS Distributionsfrom lldbmock.valuemock import ValueMock 40*bbb1b6f9SApple OSS Distributions 41*bbb1b6f9SApple OSS Distributionsimport process as tst_process 42*bbb1b6f9SApple OSS Distributionsimport utils as tst_utils 43*bbb1b6f9SApple OSS Distributionsfrom process import ShowTask, ShowProc, INVALID_PROC_SUMMARY, INVALID_TASK_SUMMARY 44*bbb1b6f9SApple OSS Distributions 45*bbb1b6f9SApple OSS Distributionsclass ProcessTest(LLDBTestCase): 46*bbb1b6f9SApple OSS Distributions """ Tests for process.py module """ 47*bbb1b6f9SApple OSS Distributions 48*bbb1b6f9SApple OSS Distributions ROUNDED_UP_PROC_SIZE = 2048 49*bbb1b6f9SApple OSS Distributions 50*bbb1b6f9SApple OSS Distributions def test_GetProcPid(self): 51*bbb1b6f9SApple OSS Distributions """ Test a pid gets returned. """ 52*bbb1b6f9SApple OSS Distributions 53*bbb1b6f9SApple OSS Distributions proc = ValueMock.createFromType('proc') 54*bbb1b6f9SApple OSS Distributions proc.p_pid = 12345 55*bbb1b6f9SApple OSS Distributions 56*bbb1b6f9SApple OSS Distributions self.assertEqual(tst_process.GetProcPID(proc), 12345) 57*bbb1b6f9SApple OSS Distributions self.assertEqual(tst_process.GetProcPID(None), -1) 58*bbb1b6f9SApple OSS Distributions 59*bbb1b6f9SApple OSS Distributions def test_GetNameShort(self): 60*bbb1b6f9SApple OSS Distributions """ Test fallback to short name. """ 61*bbb1b6f9SApple OSS Distributions 62*bbb1b6f9SApple OSS Distributions proc = ValueMock.createFromType('proc') 63*bbb1b6f9SApple OSS Distributions proc.p_name = "" 64*bbb1b6f9SApple OSS Distributions proc.p_comm = "short-proc" 65*bbb1b6f9SApple OSS Distributions 66*bbb1b6f9SApple OSS Distributions self.assertEqual(tst_process.GetProcName(proc), "short-proc") 67*bbb1b6f9SApple OSS Distributions 68*bbb1b6f9SApple OSS Distributions def test_GetNameLong(self): 69*bbb1b6f9SApple OSS Distributions """ Test that long name is preferred. """ 70*bbb1b6f9SApple OSS Distributions 71*bbb1b6f9SApple OSS Distributions proc = ValueMock.createFromType('proc') 72*bbb1b6f9SApple OSS Distributions proc.p_name = "long-proc" 73*bbb1b6f9SApple OSS Distributions proc.p_comm = "short-proc" 74*bbb1b6f9SApple OSS Distributions 75*bbb1b6f9SApple OSS Distributions self.assertEqual(tst_process.GetProcName(proc), "long-proc") 76*bbb1b6f9SApple OSS Distributions 77*bbb1b6f9SApple OSS Distributions def test_GetNameInvalid(self): 78*bbb1b6f9SApple OSS Distributions """ Test that invalid proc returns default name. """ 79*bbb1b6f9SApple OSS Distributions 80*bbb1b6f9SApple OSS Distributions self.assertEqual( 81*bbb1b6f9SApple OSS Distributions tst_process.GetProcName(None), 82*bbb1b6f9SApple OSS Distributions tst_process.NO_PROC_NAME 83*bbb1b6f9SApple OSS Distributions ) 84*bbb1b6f9SApple OSS Distributions 85*bbb1b6f9SApple OSS Distributions def test_ASTValuesInSync(self): 86*bbb1b6f9SApple OSS Distributions """ Test that thread states cover all values defined in kernel. """ 87*bbb1b6f9SApple OSS Distributions 88*bbb1b6f9SApple OSS Distributions # Compare all values with AST chars dictionary. 89*bbb1b6f9SApple OSS Distributions macro = tst_process._AST_CHARS.keys() 90*bbb1b6f9SApple OSS Distributions 91*bbb1b6f9SApple OSS Distributions # Add rest of values from the enum in kernel. 92*bbb1b6f9SApple OSS Distributions enum = lookup_type('ast_t') 93*bbb1b6f9SApple OSS Distributions self.assertTrue(enum.IsValid()) 94*bbb1b6f9SApple OSS Distributions 95*bbb1b6f9SApple OSS Distributions kernel = [ 96*bbb1b6f9SApple OSS Distributions k.GetValueAsUnsigned() 97*bbb1b6f9SApple OSS Distributions for k in enum.get_enum_members_array() 98*bbb1b6f9SApple OSS Distributions ] 99*bbb1b6f9SApple OSS Distributions 100*bbb1b6f9SApple OSS Distributions # Assert that both sides handle identical set of flags. 101*bbb1b6f9SApple OSS Distributions self.assertSetEqual(set(macro), set(kernel), 102*bbb1b6f9SApple OSS Distributions "thread state chars mismatch") 103*bbb1b6f9SApple OSS Distributions 104*bbb1b6f9SApple OSS Distributions def test_GetAstSummary(self): 105*bbb1b6f9SApple OSS Distributions """ Test AST string genration. """ 106*bbb1b6f9SApple OSS Distributions 107*bbb1b6f9SApple OSS Distributions ast = tst_utils.GetEnumValue('ast_t', 'AST_DTRACE') 108*bbb1b6f9SApple OSS Distributions ast |= tst_utils.GetEnumValue('ast_t', 'AST_TELEMETRY_KERNEL') 109*bbb1b6f9SApple OSS Distributions 110*bbb1b6f9SApple OSS Distributions # Check valid AST 111*bbb1b6f9SApple OSS Distributions self.assertEqual(tst_process.GetASTSummary(ast), 'TD') 112*bbb1b6f9SApple OSS Distributions 113*bbb1b6f9SApple OSS Distributions # Check that we never touch unsupported bits in an invalid value 114*bbb1b6f9SApple OSS Distributions ast = 0xffffffff 115*bbb1b6f9SApple OSS Distributions aststr = ''.join(char for _, char in tst_process._AST_CHARS.items()) 116*bbb1b6f9SApple OSS Distributions 117*bbb1b6f9SApple OSS Distributions self.assertEqual(tst_process.GetASTSummary(ast), aststr) 118*bbb1b6f9SApple OSS Distributions 119*bbb1b6f9SApple OSS Distributions # Mock global variable value (accessed by the macro being used) 120*bbb1b6f9SApple OSS Distributions @unittest.mock.patch('xnu.kern.globals.proc_struct_size', ROUNDED_UP_PROC_SIZE) 121*bbb1b6f9SApple OSS Distributions def test_ProcWithoutTask(self): 122*bbb1b6f9SApple OSS Distributions """ Test that ShowTask handles process-less task gracefully, and vice-versa for ShowProc """ 123*bbb1b6f9SApple OSS Distributions self.reset_mocks() 124*bbb1b6f9SApple OSS Distributions 125*bbb1b6f9SApple OSS Distributions PROC_ADDR = 0xffffffff90909090 126*bbb1b6f9SApple OSS Distributions TASK_ADDR = PROC_ADDR + self.ROUNDED_UP_PROC_SIZE 127*bbb1b6f9SApple OSS Distributions PROC_RO_ADDR = 0xffffff0040404040 128*bbb1b6f9SApple OSS Distributions 129*bbb1b6f9SApple OSS Distributions # Create fake proc_t instance at 0xffffffff90909090 130*bbb1b6f9SApple OSS Distributions proc = self.create_mock('proc', PROC_ADDR).fromDict({ 131*bbb1b6f9SApple OSS Distributions 'p_pid': 12345, 132*bbb1b6f9SApple OSS Distributions 'p_lflag': 0, # P_LHASTASK not set 133*bbb1b6f9SApple OSS Distributions 'p_comm': b'test-proc\0' 134*bbb1b6f9SApple OSS Distributions }) 135*bbb1b6f9SApple OSS Distributions 136*bbb1b6f9SApple OSS Distributions # Create task which is expected to be placed + sizeof(proc) 137*bbb1b6f9SApple OSS Distributions task = self.create_mock('task', TASK_ADDR).fromDict({ 138*bbb1b6f9SApple OSS Distributions 'effective_policy': { 139*bbb1b6f9SApple OSS Distributions 'tep_sup_active': 0, 140*bbb1b6f9SApple OSS Distributions 'tep_darwinbg': 0, 141*bbb1b6f9SApple OSS Distributions 'tep_lowpri_cpu': 1 142*bbb1b6f9SApple OSS Distributions }, 143*bbb1b6f9SApple OSS Distributions 't_flags': 0 # TF_HASPROC not set 144*bbb1b6f9SApple OSS Distributions }) 145*bbb1b6f9SApple OSS Distributions 146*bbb1b6f9SApple OSS Distributions # Created shared proc_ro reference from both task/proc 147*bbb1b6f9SApple OSS Distributions self.create_mock('proc_ro', PROC_RO_ADDR) 148*bbb1b6f9SApple OSS Distributions proc.p_proc_ro = PROC_RO_ADDR 149*bbb1b6f9SApple OSS Distributions task.bsd_info_ro = PROC_RO_ADDR 150*bbb1b6f9SApple OSS Distributions 151*bbb1b6f9SApple OSS Distributions # Capture stdout and check expected output 152*bbb1b6f9SApple OSS Distributions stdout = io.StringIO() 153*bbb1b6f9SApple OSS Distributions with contextlib.redirect_stdout(stdout): 154*bbb1b6f9SApple OSS Distributions ShowTask([f'{TASK_ADDR:#x}']) 155*bbb1b6f9SApple OSS Distributions ShowProc([f'{PROC_ADDR:#x}']) 156*bbb1b6f9SApple OSS Distributions 157*bbb1b6f9SApple OSS Distributions self.assertIn(INVALID_PROC_SUMMARY, stdout.getvalue()) 158*bbb1b6f9SApple OSS Distributions self.assertIn(INVALID_TASK_SUMMARY, stdout.getvalue()) 159