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