-
Notifications
You must be signed in to change notification settings - Fork 294
Expand file tree
/
Copy pathdemangle.py
More file actions
643 lines (540 loc) · 22 KB
/
Copy pathdemangle.py
File metadata and controls
643 lines (540 loc) · 22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
# Copyright (c) 2015-2026 Vector 35 Inc
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
import ctypes
import warnings
# Binary Ninja components
import binaryninja
from . import _binaryninjacore as core
from . import binaryview
from . import deprecation
from . import types
from .log import log_error_for_exception
from .architecture import Architecture
from .platform import Platform
from .settings import Settings
from typing import Any, Callable, Iterable, List, NamedTuple, Optional, Tuple, Union
class DemangleResult(NamedTuple):
"""
Tuple-compatible demangle result. A successful result always has a QualifiedName;
the type may be None when the demangler can recover only a name.
"""
type: Optional['types.Type']
name: 'types.QualifiedName'
@classmethod
def _from_core_struct(cls, result: core.BNDemanglerResult) -> 'DemangleResult':
if hasattr(result, "contents"):
result = result.contents
result_var_name = types.QualifiedName._from_core_struct(result.name)
out_type = None
if result.type:
out_type = core.BNNewTypeReference(result.type)
try:
result_type = None
if out_type:
result_type = types.Type.create(handle=out_type)
out_type = None
return cls(result_type, result_var_name)
finally:
if out_type:
core.BNFreeType(out_type)
class DemanglerConfig:
"""
Platform, view, and simplification options used by demangler APIs.
Use ``default``, ``for_platform``, or ``for_binary_view`` when the configuration
should inherit the corresponding core defaults.
"""
def __init__(
self,
arch_or_platform: Optional[Union[Architecture, Platform]] = None,
view: Optional['binaryview.BinaryView'] = None,
simplify: bool = False
):
if not isinstance(simplify, bool):
raise TypeError("simplify must be a bool")
if isinstance(arch_or_platform, Architecture):
platform_obj = arch_or_platform.standalone_platform
elif isinstance(arch_or_platform, Platform):
platform_obj = arch_or_platform
elif arch_or_platform is None:
platform_obj = view.platform if view is not None else None
else:
raise TypeError("Unexpected arch or platform type")
self.platform = platform_obj
self.view = view
self.simplify_templates = simplify
@classmethod
def default(cls) -> 'DemanglerConfig':
"""Create the core default demangler configuration."""
return cls._from_core_struct(core.BNGetDefaultDemanglerConfig())
@classmethod
def for_platform(cls, platform: Platform, simplify: bool = False) -> 'DemanglerConfig':
"""Create a configuration for a platform."""
if not isinstance(platform, Platform):
raise TypeError("platform must be a Platform")
if not isinstance(simplify, bool):
raise TypeError("simplify must be a bool")
return cls._from_core_struct(core.BNGetDemanglerConfigForPlatform(platform.handle, simplify))
@classmethod
def for_binary_view(cls, view: 'binaryview.BinaryView') -> 'DemanglerConfig':
"""Create a configuration using a view's platform and template-simplifier setting."""
if not isinstance(view, binaryview.BinaryView):
raise TypeError("view must be a BinaryView")
return cls._from_core_struct(core.BNGetDemanglerConfigForBinaryView(view.handle))
def _to_core_struct(self) -> core.BNDemanglerConfig:
config = core.BNDemanglerConfig()
config.platform = self.platform.handle if self.platform is not None else None
config.view = self.view.handle if self.view is not None else None
config.simplifyTemplates = self.simplify_templates
return config
@classmethod
def _from_core_struct(cls, config: core.BNDemanglerConfig) -> 'DemanglerConfig':
if hasattr(config, "contents"):
config = config.contents
platform = None
if config.platform:
platform = Platform(handle=core.BNNewPlatformReference(config.platform))
view = None
if config.view:
view = binaryview.BinaryView(handle=core.BNNewViewReference(config.view))
return cls(platform, view, config.simplifyTemplates)
@deprecation.deprecated(deprecated_in="5.4", details="Use `demangle_any` with a `DemanglerConfig` instead.")
def demangle_generic(
archOrPlatform: Union[Architecture, Platform],
mangled_name: str,
view: Optional['binaryview.BinaryView'] = None,
simplify: bool = False
) -> Optional[Tuple[Optional['types.Type'], List[str]]]:
"""Compatibility wrapper for the legacy generic demangler API."""
if isinstance(archOrPlatform, Architecture):
arch = archOrPlatform
elif isinstance(archOrPlatform, Platform):
arch = archOrPlatform.arch
else:
raise TypeError("Unexpected arch or platform type")
if view is not None and not isinstance(view, binaryview.BinaryView):
raise TypeError("view must be a BinaryView")
if not isinstance(simplify, bool):
raise TypeError("simplify must be a bool")
platform = arch.standalone_platform
if platform is not None:
config = DemanglerConfig.for_platform(platform, simplify)
else:
config = DemanglerConfig.default()
config.view = view
config.simplify_templates = simplify
result = demangle_any(mangled_name, config)
if result is None:
return None, [mangled_name]
return result.type, result.name.name
def demangle_any(
mangled_name: str,
config: Optional[DemanglerConfig] = None
) -> Optional[DemangleResult]:
"""
Attempt to demangle a mangled name, trying all relevant demanglers and using whichever one accepts it.
:param str mangled_name: a mangled symbol name
:param Optional[DemanglerConfig] config: Platform/view/options used while demangling. If omitted,
the core default standalone platform is used.
:return: returns a DemangleResult with type and name fields, or None on error. DemangleResult can be unpacked as (type, name).
:rtype: Optional[DemangleResult]
:Example:
>>> result = demangle_any("?testf@Foobar@@SA?AW4foo@1@W421@@Z")
>>> result.type
<type: immutable:FunctionTypeClass 'enum Foobar::foo __cdecl(enum Foobar::foo)'>
>>> result.name
'Foobar::testf'
"""
if config is not None and not isinstance(config, DemanglerConfig):
raise TypeError("config must be a DemanglerConfig")
result = core.BNDemanglerResult()
api_config = (config or DemanglerConfig.default())._to_core_struct()
if not core.BNDemangle(mangled_name, api_config, result):
return None
try:
return DemangleResult._from_core_struct(result)
except UnicodeDecodeError:
return None
finally:
core.BNFreeDemanglerResult(result)
def _config_from_options(
arch_or_platform: Optional[Union[Architecture, Platform, DemanglerConfig]],
options: Any
) -> DemanglerConfig:
"""
Normalize the documented DemanglerConfig form and legacy named-helper arguments.
The bool and BinaryView branches preserve the historical call shapes. They are
intentionally omitted from the public API documentation so new callers construct
and reuse a DemanglerConfig instead of querying BinaryView settings per call.
"""
if isinstance(arch_or_platform, DemanglerConfig):
return arch_or_platform
if isinstance(options, DemanglerConfig):
return options
if options is not None and not isinstance(options, bool):
if not isinstance(options, binaryview.BinaryView):
raise TypeError("options must be a bool, BinaryView, DemanglerConfig, or None")
view = options if isinstance(options, binaryview.BinaryView) else None
simplify = (
options if isinstance(options, bool)
else Settings().get_bool("analysis.types.templateSimplifier", view)
)
if isinstance(arch_or_platform, Architecture):
platform = arch_or_platform.standalone_platform
elif isinstance(arch_or_platform, Platform):
platform = arch_or_platform
elif arch_or_platform is None:
platform = None
else:
raise TypeError("arch_or_platform must be an Architecture, Platform, or DemanglerConfig")
if platform is not None:
config = DemanglerConfig.for_platform(platform, simplify)
else:
config = DemanglerConfig.default()
config.view = view
config.simplify_templates = simplify
return config
def _demangle_with_demangler(
mangled_name: str,
config: DemanglerConfig,
demangler_getter: Callable[[], Any]
) -> Optional[DemangleResult]:
binaryninja._init_plugins()
demangler = demangler_getter()
if demangler is None:
return None
result = core.BNDemanglerResult()
if not core.BNDemangleWithDemangler(
demangler, mangled_name, config._to_core_struct(), result
):
return None
try:
return DemangleResult._from_core_struct(result)
finally:
core.BNFreeDemanglerResult(result)
def demangle_llvm(
mangled_name: str,
options: Optional[DemanglerConfig] = None
) -> Optional[DemangleResult]:
"""
``demangle_llvm`` demangles a mangled name using the LLVM demangler.
.. warning::
Passing a BinaryView through the legacy ``options`` compatibility path queries
its template-simplifier setting on every call. This is very slow and should not
be used in an inner loop. Create one ``DemanglerConfig`` with
``DemanglerConfig.for_binary_view(view)`` and pass it to ``demangle_any`` instead.
:param str mangled_name: a mangled (msvc/gnu3/rust/dlang) name
:param Optional[DemanglerConfig] options: a prebuilt demangler configuration
:return: returns a DemangleResult with type and name fields, or None on error
:rtype: Optional[DemangleResult]
:Example:
>>> config = DemanglerConfig.default()
>>> demangle_llvm("?testf@Foobar@@SA?AW4foo@1@W421@@Z", config)
DemangleResult(type=None, name='public: static enum Foobar::foo __cdecl Foobar::testf(enum Foobar::foo)')
>>>
"""
modern_result = isinstance(options, DemanglerConfig)
config = _config_from_options(None, options)
result = _demangle_with_demangler(mangled_name, config, core.BNGetLLVMDemangler)
if modern_result:
return result
return result.name.name if result is not None else None
def demangle_ms(
archOrPlatform: Union[Architecture, Platform, DemanglerConfig],
mangled_name: str,
options=False
) -> Optional[DemangleResult]:
"""
``demangle_ms`` demangles a mangled Microsoft Visual Studio C++ name to a Type object.
.. warning::
Passing a BinaryView through the legacy ``options`` compatibility path queries
its template-simplifier setting on every call. This is very slow and should not
be used in an inner loop. Create one ``DemanglerConfig`` with
``DemanglerConfig.for_binary_view(view)`` and pass it to ``demangle_any`` instead.
:param Union[Architecture, Platform, DemanglerConfig] archOrPlatform: A prebuilt configuration,
or an Architecture or Platform for the symbol
:param str mangled_name: a mangled Microsoft Visual Studio C++ name
:return: returns a DemangleResult with type and name fields, or None on error
:rtype: Optional[DemangleResult]
:Example:
>>> config = DemanglerConfig.for_platform(Architecture["x86_64"].standalone_platform)
>>> demangle_ms(config, "?testf@Foobar@@SA?AW4foo@1@W421@@Z")
DemangleResult(type=<type: immutable:FunctionTypeClass 'enum Foobar::foo __cdecl(enum Foobar::foo)'>, name='Foobar::testf')
>>>
"""
modern_result = isinstance(archOrPlatform, DemanglerConfig) or isinstance(options, DemanglerConfig)
config = _config_from_options(archOrPlatform, options)
result = _demangle_with_demangler(mangled_name, config, core.BNGetMSVCDemangler)
if modern_result:
return result
if result is None:
return None, mangled_name
return result.type, result.name.name
def demangle_gnu3(
arch: Union[Architecture, Platform, DemanglerConfig],
mangled_name: str,
options=None
) -> Optional[DemangleResult]:
"""
``demangle_gnu3`` demangles a mangled name to a Type object.
.. warning::
Passing a BinaryView through the legacy ``options`` compatibility path queries
its template-simplifier setting on every call. This is very slow and should not
be used in an inner loop. Create one ``DemanglerConfig`` with
``DemanglerConfig.for_binary_view(view)`` and pass it to ``demangle_any`` instead.
:param Union[Architecture, Platform, DemanglerConfig] arch: A prebuilt configuration,
or an Architecture or Platform for the symbol
:param str mangled_name: a mangled GNU3 name
:return: returns a DemangleResult with type and name fields, or None on error
:rtype: Optional[DemangleResult]
"""
modern_result = isinstance(arch, DemanglerConfig) or isinstance(options, DemanglerConfig)
config = _config_from_options(arch, options)
result = _demangle_with_demangler(mangled_name, config, core.BNGetGNU3Demangler)
if modern_result:
return result
if result is None:
return None, mangled_name
return result.type, result.name.name
def simplify_demangled_template_name(
name: Union[str, Iterable[str], types.QualifiedName]) -> types.QualifiedName:
"""
``simplify_demangled_template_name`` simplifies standard-library template spelling in
an already demangled qualified name.
"""
if isinstance(name, types.QualifiedName):
qualified_name = name
elif isinstance(name, str):
qualified_name = types.QualifiedName(name)
else:
qualified_name = types.QualifiedName(list(name))
api_name = qualified_name._to_core_struct()
result = core.BNQualifiedName()
if not core.BNSimplifyDemangledTemplateName(ctypes.byref(api_name), ctypes.byref(result)):
return qualified_name
try:
return types.QualifiedName._from_core_struct(result)
finally:
core.BNFreeQualifiedName(result)
class _DemanglerMetaclass(type):
def __iter__(self):
binaryninja._init_plugins()
count = ctypes.c_ulonglong()
types = core.BNGetDemanglerList(count)
try:
for i in range(0, count.value):
yield CoreDemangler(types[i])
finally:
core.BNFreeDemanglerList(types)
def __getitem__(self, value):
binaryninja._init_plugins()
handle = core.BNGetDemanglerByName(str(value))
if handle is None:
raise KeyError(f"'{value}' is not a valid Demangler")
return CoreDemangler(handle)
def __contains__(cls: '_DemanglerMetaclass', name: object) -> bool:
if not isinstance(name, str):
return False
try:
cls[name]
return True
except KeyError:
return False
def get(cls: '_DemanglerMetaclass', name: str, default: Any = None) -> Optional['Demangler']:
try:
return cls[name]
except KeyError:
if default is not None:
return default
return None
class Demangler(metaclass=_DemanglerMetaclass):
"""
Pluggable name demangling interface. See :py:func:`register` and :py:func:`demangle`
for details on the process of this interface.
Custom Demangler subclasses can be registered and promoted at runtime.
The list of Demanglers can be queried:
>>> list(Demangler)
[<Demangler: MS>, <Demangler: GNU3>, <Demangler: LLVM>]
"""
name = None
_registered_demanglers = []
_cached_name = None
def __init__(self, handle=None):
self._uses_legacy_demangle_signature = False
if handle is not None:
self.handle = core.handle_of_type(handle, core.BNDemangler)
self.__dict__["name"] = core.BNGetDemanglerName(handle)
else:
self.handle = None
@classmethod
def register(cls):
"""
Register a custom Demangler. Newly registered demanglers will get priority over
previously registered demanglers and built-in demanglers.
:return: True if registration succeeded; False if the demangler was invalid.
"""
demangler = cls()
assert demangler.__class__.name is not None
assert demangler.handle is None
try:
parameter_count = binaryninja._get_parameter_count(demangler.demangle)
except (TypeError, ValueError):
parameter_count = 2
demangler._uses_legacy_demangle_signature = parameter_count == 3
if demangler._uses_legacy_demangle_signature:
warnings.warn(
deprecation.DeprecatedWarning(
"Custom Demangler.demangle(arch, name, view)",
"5.4",
None,
"Use Demangler.demangle(name, config) instead."
),
stacklevel=2
)
demangler._cb = core.BNDemanglerCallbacks()
demangler._cb.context = 0
demangler._cb.isMangledString = demangler._cb.isMangledString.__class__(demangler._is_mangled_string)
demangler._cb.demangle = demangler._cb.demangle.__class__(demangler._demangle)
demangler._cb.freeResult = demangler._cb.freeResult.__class__(demangler._free_result)
demangler.handle = core.BNRegisterDemangler(cls.name, demangler._cb)
if not demangler.handle:
return False
cls._registered_demanglers.append(demangler)
return True
@classmethod
def promote(cls, demangler):
"""
Promote a demangler to the highest-priority position.
>>> list(Demangler)
[<Demangler: MS>, <Demangler: GNU3>, <Demangler: LLVM>]
>>> Demangler.promote(list(Demangler)[0])
True
>>> list(Demangler)
[<Demangler: GNU3>, <Demangler: LLVM>, <Demangler: MS>]
:param demangler: Demangler to promote
:return: True if promotion succeeded; False if the demangler was invalid or not registered.
"""
if demangler is None or demangler.handle is None:
return False
return core.BNPromoteDemangler(demangler.handle)
def __eq__(self, other):
if not isinstance(other, Demangler):
return False
return self.name == other.name
def __str__(self):
return f'<Demangler: {self.name}>'
def __repr__(self):
return f'<Demangler: {self.name}>'
def _is_mangled_string(self, ctxt, name):
try:
return self.is_mangled_string(core.pyNativeStr(name))
except Exception:
log_error_for_exception("Unhandled Python exception in Demangler._is_mangled_string")
return False
def _demangle(self, ctxt, name, config, result):
try:
api_config = DemanglerConfig._from_core_struct(config)
demangle = self.demangle
if self._uses_legacy_demangle_signature:
arch = api_config.platform.arch if api_config.platform is not None else None
demangle_result = demangle(arch, core.pyNativeStr(name), api_config.view)
else:
demangle_result = demangle(core.pyNativeStr(name), api_config)
if demangle_result is None:
return False
type, var_name = demangle_result
if not isinstance(var_name, types.QualifiedName):
var_name = types.QualifiedName(var_name)
Demangler._cached_name = core.BNDemanglerResult()
Demangler._cached_name.name = var_name._to_core_struct()
if type is not None:
Demangler._cached_name.type = core.BNNewTypeReference(type.handle)
else:
Demangler._cached_name.type = None
result[0] = Demangler._cached_name
return True
except Exception:
log_error_for_exception("Unhandled Python exception in Demangler._demangle")
return False
def _free_result(self, ctxt, result):
try:
if result is not None and result.contents.type:
core.BNFreeType(result.contents.type)
Demangler._cached_name = None
except Exception:
log_error_for_exception("Unhandled Python exception in Demangler._free_result")
def is_mangled_string(self, name: str) -> bool:
"""
Determine if a given name is mangled and this demangler can process it
The most recently registered demangler that claims a name is a mangled string
(returns true from this function), and then returns a value from
:py:func:`demangle` will determine the result of a call to :py:func:`demangle_any`.
Returning True from this does not require the demangler to succeed the call to
:py:func:`demangle`, but simply implies that it may succeed.
:param name: Raw mangled name string
:return: True if the demangler thinks it can handle the name
"""
raise NotImplementedError()
def demangle(
self,
name: str,
config: DemanglerConfig
) -> Optional[DemangleResult]:
"""
Demangle a raw name into a Type and QualifiedName.
The result of this function is a DemangleResult with Type and QualifiedName
fields for the demangled name's details. DemangleResult can be unpacked as
(type, name).
Any unresolved named types referenced by the resulting Type will be created as
empty structures or void typedefs in the view, if the result is used on
a data structure in the view. Given this, the call to :py:func:`demangle`
should NOT cause any side-effects creating types in the view trying to resolve this
and instead just return a type with unresolved named type references.
The most recently registered demangler that claims a name is a mangled string
(returns true from :py:func:`is_mangled_string`), and then returns a value from
this function will determine the result of a call to :py:func:`demangle_any`.
If this call returns None, the next most recently used demangler(s) will be tried instead.
If the mangled name has no type information, but a name is still possible to extract,
this function may return a successful DemangleResult(None, <name>), which will be accepted.
Custom demanglers using the legacy ``demangle(arch, name, view)`` signature remain
supported for one deprecation cycle.
:param name: Raw mangled name
:param config: Platform/view/options used while demangling
:return: DemangleResult with type and name fields if successful, None if not.
Type may be None if only a demangled name can be recovered from the raw name.
"""
raise NotImplementedError()
@staticmethod
def demangle_any(name: str, config: Optional[DemanglerConfig] = None) -> Optional[DemangleResult]:
"""
Demangle a raw name using an optional prebuilt DemanglerConfig.
"""
return demangle_any(name, config)
class CoreDemangler(Demangler):
def is_mangled_string(self, name: str) -> bool:
return core.BNIsDemanglerMangledName(self.handle, name)
def demangle(self, name: str, config: DemanglerConfig) -> Optional[DemangleResult]:
result = core.BNDemanglerResult()
api_config = config._to_core_struct()
if not core.BNDemangleWithDemangler(self.handle, name, api_config, result):
return None
try:
return DemangleResult._from_core_struct(result)
finally:
core.BNFreeDemanglerResult(result)