88# See https://aboutcode.org for more information about nexB OSS projects.
99#
1010
11- import os
12- import lief
1311import json
12+ import os
1413import zlib
1514
15+ import lief
1616from typecode import contenttype
1717from typecode .contenttype import get_type
1818
@@ -67,7 +67,7 @@ def get_rust_packages_data(location):
6767 to the rust binary with data on packages and dependencies.
6868 See https://github.com/rust-secure-code/cargo-auditable for more info.
6969
70- Code for parsing rust bianries to get package data is from
70+ Code for parsing rust binaries to get package data is from
7171 https://github.com/rust-secure-code/cargo-auditable/blob/master/PARSING.md
7272 """
7373 if not is_executable_binary (location ):
@@ -87,9 +87,12 @@ def get_rust_packages_data(location):
8787 return packages_data
8888
8989
90-
9190def might_have_rust_symbols (string_with_symbols ):
92-
91+ """
92+ Given a demangled symbol string obtained from a rust binary, return True if
93+ there are rust symbols present in the string which could be mapped to rust
94+ source symbols potentially, return False otherwise.
95+ """
9396 if not string_with_symbols :
9497 return False
9598
@@ -120,16 +123,20 @@ def might_have_rust_symbols(string_with_symbols):
120123
121124 return True
122125
123- def remove_standard_symbols (rust_symbols ):
124- return [
125- symbol
126- for symbol in rust_symbols
127- if symbol not in STANDARD_SYMBOLS_RUST
128- ]
126+
127+ def remove_standard_symbols (rust_symbols , standard_symbols = STANDARD_SYMBOLS_RUST ):
128+ """
129+ Remove standard symbols usually found in rust binaries. Given a list of rust
130+ symbol strings, return a list of symbol strings which are most likely non-standard.
131+ """
132+ return [symbol for symbol in rust_symbols if symbol not in standard_symbols ]
129133
130134
131135def split_strings_by_char (split_strings , split_char ):
132-
136+ """
137+ Given a list of strings, return another list of strings with all
138+ the substrings from each string, split by the `split_char`.
139+ """
133140 final_split_strings = []
134141 for split_str in split_strings :
135142 if split_char in split_str :
@@ -138,15 +145,16 @@ def split_strings_by_char(split_strings, split_char):
138145 else :
139146 final_split_strings .append (split_str )
140147
141- return [
142- split_string
143- for split_string in final_split_strings
144- if split_string
145- ]
148+ return [split_string for split_string in final_split_strings if split_string ]
146149
147150
148151def split_strings_into_rust_symbols (strings_to_split , split_by_chars = SPLIT_CHARACTERS_RUST ):
149-
152+ """
153+ Given a list of strings containing a group of rust symbols, get a list
154+ of strings with the extracted individual symbol strings, using a list of
155+ `split_by_chars` which are common characters found between rust symbols in
156+ demangled rust string containing multiple symbols.
157+ """
150158 split_strings = []
151159 split_strings_log = []
152160 for split_char in split_by_chars :
@@ -159,10 +167,17 @@ def split_strings_into_rust_symbols(strings_to_split, split_by_chars=SPLIT_CHARA
159167 return split_strings
160168
161169
162- def cleanup_symbols (split_symbols , include_stdlib = False , unique = True , sort_symbols = False ):
170+ def cleanup_symbols (symbols , include_stdlib = False , unique = True , sort_symbols = False ):
171+ """
172+ Given a list of `symbols` strings, return a list of cleaned up
173+ symbol strings, removing strings which does not have symbols.
163174
175+ If `include_stdlib` is False, remove standard rust symbols.
176+ If `unique` is True, only return unique symbol strings.
177+ If `sort_symbols` is True, return a sorted list of symbols.
178+ """
164179 rust_symbols = []
165- for split_string in split_symbols :
180+ for split_string in symbols :
166181 if might_have_rust_symbols (split_string ):
167182 rust_symbols .append (split_string )
168183
@@ -178,17 +193,22 @@ def cleanup_symbols(split_symbols, include_stdlib=False, unique=True, sort_symbo
178193 return rust_symbols
179194
180195
181- def extract_strings_with_symbols (symbols_data , include_stdlib = False , unique = True , sort_symbols = False ):
182-
196+ def extract_strings_with_symbols (
197+ symbols_data , include_stdlib = False , unique = True , sort_symbols = False
198+ ):
199+ """
200+ From a list of rust symbols data parsed and demangled from a binary,
201+ return a list of individual symbols (after cleanup) found in the strings.
202+ """
183203 strings_with_symbols = []
184-
204+
185205 ignore_types = ["NOTYPE" , "TLS" ]
186206
187207 for symbol_data in symbols_data :
188208
189209 if not symbol_data .get ("name" ):
190210 continue
191-
211+
192212 if symbol_data .get ("type" ) in ignore_types :
193213 continue
194214
@@ -202,14 +222,14 @@ def extract_strings_with_symbols(symbols_data, include_stdlib=False, unique=True
202222 # These are usually like the following:
203223 # `getrandom@GLIBC_2.25`, `__umodti3`, `_ITM_registerTMCloneTable`
204224 # So these doesn't have source symbols
205- if symbol_data .get ("binding" ) == ' WEAK' :
225+ if symbol_data .get ("binding" ) == " WEAK" :
206226 continue
207227
208228 # file/module names are also source symbols as they
209229 # are imported in source code files
210230 if symbol_data .get ("type" ) == "FILE" :
211231 file_string = symbol_data .get ("name" )
212- file_segments = file_string .split ('.' )
232+ file_segments = file_string .split ("." )
213233 if not file_segments :
214234 continue
215235
@@ -227,7 +247,7 @@ def extract_strings_with_symbols(symbols_data, include_stdlib=False, unique=True
227247
228248 split_symbols = split_strings_into_rust_symbols (strings_to_split = strings_with_symbols )
229249 rust_symbols = cleanup_symbols (
230- split_symbols = split_symbols ,
250+ symbols = split_symbols ,
231251 include_stdlib = include_stdlib ,
232252 unique = unique ,
233253 sort_symbols = sort_symbols ,
@@ -240,7 +260,6 @@ def collect_and_parse_rust_symbols(location, include_stdlib=False, sort_symbols=
240260 """
241261 Return a mapping of Rust symbols of interest for the Rust binary file at ``location``.
242262 Return an empty mapping if there is no symbols or if this is not a binary.
243- Raise exceptions on errors.
244263 """
245264 if not is_executable_binary (location ):
246265 return
@@ -254,11 +273,12 @@ def collect_and_parse_rust_symbols(location, include_stdlib=False, sort_symbols=
254273 )
255274
256275
257- def collect_and_parse_rust_symbols_from_data (rust_data , include_stdlib = False , unique = True , sort_symbols = False , ** kwargs ):
276+ def collect_and_parse_rust_symbols_from_data (
277+ rust_data , include_stdlib = False , unique = True , sort_symbols = False , ** kwargs
278+ ):
258279 """
259280 Return a mapping of Rust symbols of interest for the mapping of Rust binary of ``rust_data``.
260281 Return an empty mapping if there is no symbols or if this is not a binary.
261- Raise exceptions on errors.
262282 """
263283 if not rust_data :
264284 return {}
0 commit comments