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