xref: /xnu-11215.81.4/tools/lldbmacros/core/pointer.py (revision d4514f0bc1d3f944c22d92e68b646ac3fb40d452)
1*d4514f0bSApple OSS Distributions"""
2*d4514f0bSApple OSS DistributionsCustom pointer support
3*d4514f0bSApple OSS Distributions
4*d4514f0bSApple OSS DistributionsThis module provides support for special pointer types that are not native to the
5*d4514f0bSApple OSS Distributionslanguage used by the target being debugged. Such pointers may be represented as a struct
6*d4514f0bSApple OSS Distributionsor class (for example IOKit's shared pointers).
7*d4514f0bSApple OSS Distributions
8*d4514f0bSApple OSS DistributionsA custom pointer class must subclass the PointerPolicy class and implement all of its
9*d4514f0bSApple OSS Distributionsabstract methods. The MetaPointerPolicy metaclass ensures that all known subclasses are
10*d4514f0bSApple OSS Distributionsregistered in a global list (wherever they are located in the lldb macro sources).
11*d4514f0bSApple OSS Distributions
12*d4514f0bSApple OSS DistributionsA client can obtain a PointerPolicy instance by calling the match method with an SBValue
13*d4514f0bSApple OSS Distributionsinstance as an argument. The returned value is one of:
14*d4514f0bSApple OSS Distributions
15*d4514f0bSApple OSS Distributions    * None - the match was unsuccessful and this SBValue instance is not a pointer.
16*d4514f0bSApple OSS Distributions    * Concrete instance - An instance of the concrete PointerPolicy class that will handle
17*d4514f0bSApple OSS Distributions      pointer operations for the given SBValue.
18*d4514f0bSApple OSS Distributions
19*d4514f0bSApple OSS DistributionsConcrete policy instances implement an API that allows a client to operate on a value
20*d4514f0bSApple OSS Distributionslike a native pointer (for example unwrapping a native pointer from a smart pointer).
21*d4514f0bSApple OSS Distributions
22*d4514f0bSApple OSS DistributionsExample:
23*d4514f0bSApple OSS Distributions
24*d4514f0bSApple OSS Distributions    # Obtain an SBValue instance.
25*d4514f0bSApple OSS Distributions    val = kern.global.GlobalVariable.GetSBValue()
26*d4514f0bSApple OSS Distributions
27*d4514f0bSApple OSS Distributions    # Try to match the pointer policy for the given value.
28*d4514f0bSApple OSS Distributions    policy = PointerPolicy.match(val)
29*d4514f0bSApple OSS Distributions
30*d4514f0bSApple OSS Distributions    # Unwrap the pointer SBValue.
31*d4514f0bSApple OSS Distributions    if policy:
32*d4514f0bSApple OSS Distributions        val = policy.GetPointerSBValue(val)
33*d4514f0bSApple OSS Distributions
34*d4514f0bSApple OSS Distributions    ... Operate on val as usual.
35*d4514f0bSApple OSS Distributions"""
36*d4514f0bSApple OSS Distributionsfrom operator import methodcaller
37*d4514f0bSApple OSS Distributionsfrom abc import ABCMeta, abstractmethod
38*d4514f0bSApple OSS Distributions
39*d4514f0bSApple OSS Distributionsimport lldb
40*d4514f0bSApple OSS Distributions
41*d4514f0bSApple OSS Distributionsfrom .caching import cache_statically
42*d4514f0bSApple OSS Distributions
43*d4514f0bSApple OSS Distributions
44*d4514f0bSApple OSS Distributionsclass MetaPointerPolicy(ABCMeta):
45*d4514f0bSApple OSS Distributions    """ Register a custom pointer policy in global list. """
46*d4514f0bSApple OSS Distributions
47*d4514f0bSApple OSS Distributions    classes = []
48*d4514f0bSApple OSS Distributions
49*d4514f0bSApple OSS Distributions    def __new__(cls, clsname, bases, args):
50*d4514f0bSApple OSS Distributions        newcls = super(MetaPointerPolicy, cls).__new__(cls, clsname, bases, args)
51*d4514f0bSApple OSS Distributions        cls.classes.append(newcls)
52*d4514f0bSApple OSS Distributions        return newcls
53*d4514f0bSApple OSS Distributions
54*d4514f0bSApple OSS Distributions
55*d4514f0bSApple OSS Distributionsclass Singleton(MetaPointerPolicy):
56*d4514f0bSApple OSS Distributions    """ Meta class for creation of singleton instances. """
57*d4514f0bSApple OSS Distributions
58*d4514f0bSApple OSS Distributions    _instances = {}
59*d4514f0bSApple OSS Distributions
60*d4514f0bSApple OSS Distributions    def __call__(cls, *args, **kwargs):
61*d4514f0bSApple OSS Distributions        if cls not in cls._instances:
62*d4514f0bSApple OSS Distributions            cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
63*d4514f0bSApple OSS Distributions        return cls._instances[cls]
64*d4514f0bSApple OSS Distributions
65*d4514f0bSApple OSS Distributions
66*d4514f0bSApple OSS Distributionsclass PointerPolicy(object, metaclass=ABCMeta):
67*d4514f0bSApple OSS Distributions    """ Abstract base class common to every custom pointer policy. """
68*d4514f0bSApple OSS Distributions
69*d4514f0bSApple OSS Distributions    @classmethod
70*d4514f0bSApple OSS Distributions    def match(cls, sbvalue):
71*d4514f0bSApple OSS Distributions        """ Match pointer representation based on given SBValue. """
72*d4514f0bSApple OSS Distributions        matching = filter(bool, map(methodcaller('match', sbvalue), MetaPointerPolicy.classes))
73*d4514f0bSApple OSS Distributions        return next(matching, None)
74*d4514f0bSApple OSS Distributions
75*d4514f0bSApple OSS Distributions    @abstractmethod
76*d4514f0bSApple OSS Distributions    def GetPointerSBValue(self, sbvalue):
77*d4514f0bSApple OSS Distributions        """ Returns pointer value that debugger should operate on. """
78*d4514f0bSApple OSS Distributions
79*d4514f0bSApple OSS Distributions
80*d4514f0bSApple OSS Distributions# Pointers need to have their TBI byte stripped if in use. TBI KASan,
81*d4514f0bSApple OSS Distributions# for instance, tags pointers to detect improper memory accesses. Reading
82*d4514f0bSApple OSS Distributions# values from such tagged pointers fails.
83*d4514f0bSApple OSS Distributions#
84*d4514f0bSApple OSS Distributions# Stripping the pointers requires to learn whether TBI is in use or not.
85*d4514f0bSApple OSS Distributions# We do that by checking presence of 'kasan_tbi_enabled' symbol which only
86*d4514f0bSApple OSS Distributions# exists on the TBI KASan variant. Since KASan is one of more TBI
87*d4514f0bSApple OSS Distributions# consumers (along with PAC or Sandbox) this is not an ideal approach.
88*d4514f0bSApple OSS Distributions# Inspecting respective CPU state would be more appropriate.
89*d4514f0bSApple OSS Distributions
90*d4514f0bSApple OSS Distributions
91*d4514f0bSApple OSS Distributionsclass NativePointer(PointerPolicy, metaclass=Singleton):
92*d4514f0bSApple OSS Distributions    """ Policy for native pointers.
93*d4514f0bSApple OSS Distributions
94*d4514f0bSApple OSS Distributions        Strips top bits of a pointer if TBI is in use. Otherwise
95*d4514f0bSApple OSS Distributions        pointer is used as-is.
96*d4514f0bSApple OSS Distributions
97*d4514f0bSApple OSS Distributions        Native pointers do not have any per-pointer attributes so this policy
98*d4514f0bSApple OSS Distributions        can be singleton instance.
99*d4514f0bSApple OSS Distributions    """
100*d4514f0bSApple OSS Distributions
101*d4514f0bSApple OSS Distributions    @staticmethod
102*d4514f0bSApple OSS Distributions    @cache_statically
103*d4514f0bSApple OSS Distributions    def isTagged(target=None):
104*d4514f0bSApple OSS Distributions        """ Returns true on TBI KASan targets, false otherwise. """
105*d4514f0bSApple OSS Distributions        is_tagged = target.FindFirstGlobalVariable('kasan_tbi_enabled').GetValueAsUnsigned()
106*d4514f0bSApple OSS Distributions        return is_tagged
107*d4514f0bSApple OSS Distributions
108*d4514f0bSApple OSS Distributions    def __init__(self):
109*d4514f0bSApple OSS Distributions        if self.isTagged():
110*d4514f0bSApple OSS Distributions            self._stripPtr = self.stripPtr
111*d4514f0bSApple OSS Distributions        else:
112*d4514f0bSApple OSS Distributions            self._stripPtr = lambda val: val
113*d4514f0bSApple OSS Distributions
114*d4514f0bSApple OSS Distributions    @classmethod
115*d4514f0bSApple OSS Distributions    def match(cls, sbvalue):
116*d4514f0bSApple OSS Distributions        return cls() if sbvalue.GetType().IsPointerType() else None
117*d4514f0bSApple OSS Distributions
118*d4514f0bSApple OSS Distributions    @staticmethod
119*d4514f0bSApple OSS Distributions    def stripPtr(sbvalue):
120*d4514f0bSApple OSS Distributions        """ Strips the TBI byte value. Since the value is not a plain value but
121*d4514f0bSApple OSS Distributions            represents a value of a variable, a register or an expression the
122*d4514f0bSApple OSS Distributions            conversion is performed by (re-)creating the value through expression.
123*d4514f0bSApple OSS Distributions        """
124*d4514f0bSApple OSS Distributions        if sbvalue.GetValueAsAddress() != sbvalue.GetValueAsUnsigned():
125*d4514f0bSApple OSS Distributions            addr = sbvalue.GetValueAsAddress()
126*d4514f0bSApple OSS Distributions            sbv_new = sbvalue.CreateValueFromExpression(None, '(void *)' + str(addr))
127*d4514f0bSApple OSS Distributions            return sbv_new.Cast(sbvalue.GetType())
128*d4514f0bSApple OSS Distributions
129*d4514f0bSApple OSS Distributions
130*d4514f0bSApple OSS Distributions        return sbvalue
131*d4514f0bSApple OSS Distributions
132*d4514f0bSApple OSS Distributions    def GetPointerSBValue(self, sbvalue):
133*d4514f0bSApple OSS Distributions        return self._stripPtr(sbvalue)
134