1## 2# Copyright (c) 2023 Apple Inc. All rights reserved. 3# 4# @APPLE_OSREFERENCE_LICENSE_HEADER_START@ 5# 6# This file contains Original Code and/or Modifications of Original Code 7# as defined in and that are subject to the Apple Public Source License 8# Version 2.0 (the 'License'). You may not use this file except in 9# compliance with the License. The rights granted to you under the License 10# may not be used to create, or enable the creation or redistribution of, 11# unlawful or unlicensed copies of an Apple operating system, or to 12# circumvent, violate, or enable the circumvention or violation of, any 13# terms of an Apple operating system software license agreement. 14# 15# Please obtain a copy of the License at 16# http://www.opensource.apple.com/apsl/ and read it before using this file. 17# 18# The Original Code and all software distributed under the License are 19# distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 20# EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 21# INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 22# FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 23# Please see the License for the specific language governing rights and 24# limitations under the License. 25# 26# @APPLE_OSREFERENCE_LICENSE_HEADER_END@ 27## 28 29""" Value class mock 30 31 The main purpose of this mock is to allow easy mocking of value class 32 instance inside LLDB macros. A mock is constructed from a SBType instance 33 and replicates all of its members as attributes. The final result is almost 34 identical to value class and user can access members with: 35 36 mock.member.sub_member = ... 37 38 There is no handling of unions/bitfields so developer has to carefuly fill 39 the members. 40 41 Given that a mock is replicating SBType from real kernel it is possible to 42 use it to check whether an attribute is present or not in the final binary. 43 44 For example to catch problems where a member is compiled out on RELEASE 45 kernel. 46""" 47 48import unittest.mock 49from typing import Union 50from lldbmock.utils import visit_type, lookup_type 51import lldb 52 53 54class ValueMock(unittest.mock.MagicMock): 55 """ Creates mock of a C structure (not extensible) based on binary dSYM. """ 56 57 @staticmethod 58 def _createArray(mocktype): 59 60 count = mocktype.GetByteSize() // mocktype.GetArrayElementType().GetByteSize() 61 62 return [ 63 ValueMock.createFromType(mocktype.GetArrayElementType()) 64 for _ in range(count) 65 ] 66 67 @staticmethod 68 def createFromType(mocktype: Union[lldb.SBType, str]) -> 'ValueMock': 69 """ Creates ValueMock for selected type. 70 71 A type is specified either as string or SBType. 72 """ 73 sbtype = lookup_type(mocktype) 74 type_class = sbtype.GetTypeClass() 75 76 # Handle arrays 77 if type_class == lldb.eTypeClassArray: 78 return ValueMock._createArray(sbtype) 79 80 # Convert SBType members to mock specification. 81 instmock = ValueMock(spec_set = [ 82 t.GetName() for t, _ 83 in visit_type(sbtype) 84 if isinstance(t, lldb.SBTypeMember) 85 ]) 86 87 # Recursively construct sub-mocks for compound type members. 88 for member, _ in visit_type(sbtype): 89 if isinstance(member, lldb.SBTypeMember) and \ 90 member.GetType().GetNumberOfFields() > 0: 91 92 nest = ValueMock.createFromType(member.GetType()) 93 setattr(instmock, member.GetName(), nest) 94 95 return instmock 96