diff --git a/NOTICE b/NOTICE new file mode 100644 index 00000000..c781b75a --- /dev/null +++ b/NOTICE @@ -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). diff --git a/README.md b/README.md index 3ba60181..27385001 100644 --- a/README.md +++ b/README.md @@ -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! :) diff --git a/src/smda/common/labelprovider/RustSymbolProvider.py b/src/smda/common/labelprovider/RustSymbolProvider.py index 7d02a06b..4e6efeff 100644 --- a/src/smda/common/labelprovider/RustSymbolProvider.py +++ b/src/smda/common/labelprovider/RustSymbolProvider.py @@ -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__) @@ -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) @@ -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) @@ -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 @@ -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) diff --git a/src/smda/common/labelprovider/rust_demangler/__init__.py b/src/smda/common/labelprovider/rust_demangler/__init__.py index 72d2219f..8a9d575b 100644 --- a/src/smda/common/labelprovider/rust_demangler/__init__.py +++ b/src/smda/common/labelprovider/rust_demangler/__init__.py @@ -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"] diff --git a/src/smda/common/labelprovider/rust_demangler/rust_legacy.py b/src/smda/common/labelprovider/rust_demangler/rust_legacy.py index 1534b389..ab41e08a 100644 --- a/src/smda/common/labelprovider/rust_demangler/rust_legacy.py +++ b/src/smda/common/labelprovider/rust_demangler/rust_legacy.py @@ -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"): diff --git a/src/smda/common/labelprovider/rust_demangler/rust_v0.py b/src/smda/common/labelprovider/rust_demangler/rust_v0.py index 25a22637..ea6d4ba7 100644 --- a/src/smda/common/labelprovider/rust_demangler/rust_v0.py +++ b/src/smda/common/labelprovider/rust_demangler/rust_v0.py @@ -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. @@ -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", ", ") diff --git a/src/smda/common/labelprovider/rust_demangler/utils.py b/src/smda/common/labelprovider/rust_demangler/utils.py deleted file mode 100644 index 42066fa3..00000000 --- a/src/smda/common/labelprovider/rust_demangler/utils.py +++ /dev/null @@ -1,41 +0,0 @@ -def remove_bad_spaces(text): - """ - Removes spaces that are not separating distinct objects, particularly - inside templates and parameter lists. - Based on Ghidra's CondensedString logic. - """ - if not text: - return text - - depth = 0 - condensed_parts = [] - - # Simple state machine to track depth of <...> and (...) - # and remove spaces if depth > 0, unless they separate alphanumerics - - for i, char in enumerate(text): - if char == "<" or char == "(": - depth += 1 - condensed_parts.append(char) - elif (char == ">" or char == ")") and depth > 0: - depth -= 1 - condensed_parts.append(char) - elif depth > 0 and char == " ": - # Look ahead - next_char = text[i + 1] if i + 1 < len(text) else "\0" - last_char = text[i - 1] if i - 1 >= 0 else "\0" - - if last_char.isalnum() and next_char.isalnum(): - # Keep space as underscore if it separates words inside template? - # Ghidra says: "separate words with a value so they don't run together; drop the other spaces" - # But typically Rust types don't have spaces inside unless it's `where T: ...`? - # Actually Ghidra converts it to underscore if surrounded by chars. - # Example: `Foo < Bar >` -> `Foo`. `Foo < Bar Baz >` -> `Foo`. - condensed_parts.append("_") - else: - # Remove space - pass - else: - condensed_parts.append(char) - - return "".join(condensed_parts) diff --git a/tests/testRustSymbolProvider.py b/tests/testRustSymbolProvider.py index d9dffdef..fc755920 100644 --- a/tests/testRustSymbolProvider.py +++ b/tests/testRustSymbolProvider.py @@ -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 @@ -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::') + self.assertEqual(demangle("_RIC1aFK6systemuEuE"), 'a::') def test_legacy_strict_hash(self): """Test that hash segments are properly handled in legacy symbols.""" @@ -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") - # Separating space becomes underscore - self.assertEqual(remove_bad_spaces("Foo< Bar Baz >"), "Foo") + # 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), "::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):