Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions NOTICE
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
SMDA
Copyright (c) 2018-2020, Daniel Plohmann and Steffen Enders

This product is licensed under the BSD 2-Clause License; see LICENSE.

It includes third-party code, listed below with the notices its license
requires.

================================================================================
rust_demangler
--------------------------------------------------------------------------------
src/smda/common/labelprovider/rust_demangler/ is derived from the rust_demangler
package by Team bi0s (https://github.com/teambi0s/rust_demangler), used under the
MIT License and modified for use here.

MIT License

Copyright (c) 2021 Team bi0s

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.

================================================================================
Ghidra
--------------------------------------------------------------------------------
Parts of the same directory reimplement behaviour from Ghidra's Rust demanglers
(https://github.com/NationalSecurityAgency/ghidra), specifically
Ghidra/Features/Rust/src/main/java/ghidra/app/plugin/core/analysis/rust/demangler/
RustDemanglerV0.java and RustDemanglerLegacy.java: the recursion bound in the v0
demangler and the strict hash handling in the legacy demangler. Ghidra is
licensed under the Apache License, Version 2.0, available at

http://www.apache.org/licenses/LICENSE-2.0

Ghidra's own V0 demangler is a port of the rustc-demangle crate
(https://github.com/rust-lang/rustc-demangle), dual-licensed Apache-2.0 OR MIT.

================================================================================
Tarjan's algorithm
--------------------------------------------------------------------------------
src/smda/common/Tarjan.py is based on the implementation by Bas Westerbaan
(https://github.com/bwesterb/py-tarjan), refactored into a class for pooled
computation.

================================================================================
Lengauer-Tarjan dominator tree
--------------------------------------------------------------------------------
src/smda/common/DominatorTree.py is based on the implementation by Armin Rigo
(https://bitbucket.org/arigo/arigo/src/default/hack/pypy-hack/heapstats/dominator.py).
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ Thanks to Jonathan Crussell for helping me to beef up SMDA enough to make it a d
Thanks to Willi Ballenthin for improving the handling of ELF files, including properly handling API usage!
Thanks to Daniel Enders for his contributions to the parsing of the Golang function registry and label information!
The project uses the implementation of Tarjan's Algorithm by Bas Westerbaan and the implementation of Lengauer-Tarjan's Algorithm for the DominatorTree by Armin Rigo.
Rust symbol demangling is derived from the rust_demangler package by Team bi0s (MIT), with behaviour reimplemented from Ghidra's Rust demanglers (Apache-2.0); see [NOTICE](NOTICE) for the full list of third-party components.
Thanks to r0ny123 for his major code quality improvements via ruff and various contributions for several aspects of this project!

Pull requests welcome! :)
5 changes: 0 additions & 5 deletions src/smda/common/labelprovider/RustSymbolProvider.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from .ElfSymbolProvider import is_defined_elf_symbol
from .import_parsers import resolve_pe_base_addr
from .rust_demangler import demangle
from .rust_demangler.utils import remove_bad_spaces
from .RustSymbolEvidence import RUST_DEMANGLE_ERRORS, is_rust_language_evidence

LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -169,7 +168,6 @@ def _update_macho(self, lief_binary, binary_info):
try:
demangled = demangle(raw_name)
if demangled:
demangled = remove_bad_spaces(demangled)
self._func_symbols[adjusted] = demangled
except _DEMANGLE_ERRORS as exc:
LOGGER.debug("Failed to demangle Rust symbol %s: %s", raw_name, exc)
Expand All @@ -194,7 +192,6 @@ def _update_pe(self, lief_binary, base_addr=None):
if self._is_rust_symbol(raw_name):
demangled = demangle(raw_name)
if demangled:
demangled = remove_bad_spaces(demangled)
self._func_symbols[active_base + function.address] = demangled
except _DEMANGLE_ERRORS as exc:
LOGGER.debug("Failed to demangle Rust symbol %s: %s", raw_name, exc)
Expand All @@ -218,7 +215,6 @@ def _update_pe(self, lief_binary, base_addr=None):
if self._is_rust_symbol(raw_name):
demangled = demangle(raw_name)
if demangled:
demangled = remove_bad_spaces(demangled)
function_offset = active_base + symbol.section.virtual_address + symbol.value
if function_offset not in self._func_symbols:
self._func_symbols[function_offset] = demangled
Expand All @@ -239,7 +235,6 @@ def _parse_lief_symbols(self, symbols):
try:
demangled = demangle(raw_name)
if demangled:
demangled = remove_bad_spaces(demangled)
function_symbols[symbol.value] = demangled
except _DEMANGLE_ERRORS as exc:
LOGGER.debug("Failed to demangle Rust symbol %s: %s", raw_name, exc)
Expand Down
4 changes: 4 additions & 0 deletions src/smda/common/labelprovider/rust_demangler/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
"""Rust symbol demangling, derived from Team bi0s' rust_demangler (MIT) with
behaviour reimplemented from Ghidra's Rust demanglers. See NOTICE.
"""

from .main import demangle

__all__ = ["demangle"]
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def is_ascii_punctuation(self, c):
return c in string.punctuation

def is_rust_hash(self, s):
# Improved robustness based on Ghidra's rust-demangle.c
# Improved robustness based on Ghidra's RustDemanglerLegacy
# Legacy Rust symbols end with a path segment that encodes a 16 hex digit hash,
# prefixed with "17h", i.e. '17h[a-f0-9]{16}'.
if len(s) == 19 and s.startswith("17h"):
Expand Down
4 changes: 2 additions & 2 deletions src/smda/common/labelprovider/rust_demangler/rust_v0.py
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ def skip_const(self):


class Printer:
# Based on Ghidra's rust-demangle.c, we limit recursion to prevent stack overflows
# Following Ghidra's RustDemanglerV0, we limit recursion to prevent stack overflows
# or excessive resource usage on malformed inputs.
# Must fire well below CPython's own recursion limit (default 1000), or a
# self-referential backref chain raises RecursionError before this guard.
Expand Down Expand Up @@ -559,7 +559,7 @@ def f1():
if abi:
self.out += 'extern "'
self.out += "-".join(abi.split("_"))
self.out += '"'
self.out += '" '

self.out += "fn("
self.print_sep_list("print_type", ", ")
Expand Down
41 changes: 0 additions & 41 deletions src/smda/common/labelprovider/rust_demangler/utils.py

This file was deleted.

37 changes: 27 additions & 10 deletions tests/testRustSymbolProvider.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
UnableTov0Demangle,
V0Demangler,
)
from smda.common.labelprovider.rust_demangler.utils import remove_bad_spaces
from smda.common.labelprovider.RustSymbolEvidence import is_rust_language_evidence
from smda.common.labelprovider.RustSymbolProvider import RustSymbolProvider

Expand Down Expand Up @@ -122,7 +121,7 @@ def test_v0_empty_const_hex_nibbles_raise_demangler_error(self):
def test_v0_non_c_abi_fn_type_demangles(self):
# the skip-pass abi validation was inverted, rejecting every valid
# non-C abi (e.g. extern "system") fn-type symbol
self.assertEqual(demangle("_RIC1aFK6systemuEuE"), 'a::<extern "system"fn(())>')
self.assertEqual(demangle("_RIC1aFK6systemuEuE"), 'a::<extern "system" fn(())>')

def test_legacy_strict_hash(self):
"""Test that hash segments are properly handled in legacy symbols."""
Expand Down Expand Up @@ -554,15 +553,33 @@ def test_pe_symbol_provider_returns_raw_rust_names(self):
self.assertEqual(results[0x403000], "ExportedFunc")


class TestUtilityFunctions(unittest.TestCase):
"""Tests for utility functions."""
class TestDemangledSpacing(unittest.TestCase):
"""Demangled names reach the report spelled the way rustc spells them."""

def test_space_cleanup(self):
"""Test remove_bad_spaces utility function."""
# Inner spaces removed
self.assertEqual(remove_bad_spaces("Vec< T >"), "Vec<T>")
# Separating space becomes underscore
self.assertEqual(remove_bad_spaces("Foo< Bar Baz >"), "Foo<Bar_Baz>")
# a real symbol from a rust-lld/MSVC x64 image
TRAIT_IMPL = "_RNvXs5_NtNtCslFVcyoAu48q_3std2io5errorNtB5_5ErrorNtNtCs55qC6OcLGgs_4core3fmt7Display3fmt"

def test_a_trait_impl_keeps_the_as_separator(self):
self.assertEqual(demangle(self.TRAIT_IMPL), "<std::io::error::Error as core::fmt::Display>::fmt")

def test_a_generic_argument_list_keeps_its_separating_space(self):
self.assertEqual(demangle("_RIC1aKh4_Kh4_E"), "a::<4, 4>")

def test_a_function_pointer_abi_is_separated_from_its_fn(self):
# real symbol; the space after the ABI string used to be missing
name = "_RNvMs3_NtCs8oYkXk2gzQW_5alloc7raw_vecINtB5_6RawVecTOhFUKCBN_EuENtNtCslFVcyoAu48q_3std5alloc6SystemE8grow_oneB13_"
self.assertIn('unsafe extern "C" fn(*mut u8)', demangle(name))

def test_the_provider_stores_the_name_the_demangler_produced(self):
provider = RustSymbolProvider(None)
mock_binary = MockLiefBinary([], exported_functions=[MockExport(self.TRAIT_IMPL, 0x1000)])
mock_binary.imagebase = 0x140000000
mock_binary.sections = [MockSection(0x20000000, 0x1000)]

with mock.patch("lief.PE.Binary", MockLiefBinary):
provider._update_pe(mock_binary, base_addr=0x400000)

self.assertEqual(provider.getSymbol(0x401000), demangle(self.TRAIT_IMPL))


class TestRustV0ConstBackrefs(unittest.TestCase):
Expand Down