Skip to content

Commit 695e8e0

Browse files
committed
Reduce usage of IntoCStr in Rust API signatures
This is being done to reduce complexity in function signatures, specifically many of the strings we are passing ultimately should be new types themselves instead of "just strings", things such as type ids. Another place which was confusing was dealing with filesystem related APIs, this commit turns most of those params into a stricter `Path` type. This is bringing the rust api more inline with both python and C++, where the wrapper eagerly converts the string into the languages standard string type. Special consideration must be made for symbols or other possible non utf-8 objects. This commit will be followed up with one that adds the `IntoCStr` bound on API's we want to keep as invalid utf-8 so we can for example, get section by name on a section with invalid utf-8.
1 parent 1771c8e commit 695e8e0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

91 files changed

+765
-1006
lines changed

plugins/dwarf/dwarf_import/src/die_handlers.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,19 @@ pub(crate) fn handle_base_type<R: ReaderType>(
4747
constants::DW_ATE_address => None,
4848
constants::DW_ATE_boolean => Some(Type::bool()),
4949
constants::DW_ATE_complex_float => None,
50-
constants::DW_ATE_float => Some(Type::named_float(size, name)),
51-
constants::DW_ATE_signed => Some(Type::named_int(size, true, name)),
52-
constants::DW_ATE_signed_char => Some(Type::named_int(size, true, name)),
53-
constants::DW_ATE_unsigned => Some(Type::named_int(size, false, name)),
54-
constants::DW_ATE_unsigned_char => Some(Type::named_int(size, false, name)),
50+
constants::DW_ATE_float => Some(Type::named_float(size, &name)),
51+
constants::DW_ATE_signed => Some(Type::named_int(size, true, &name)),
52+
constants::DW_ATE_signed_char => Some(Type::named_int(size, true, &name)),
53+
constants::DW_ATE_unsigned => Some(Type::named_int(size, false, &name)),
54+
constants::DW_ATE_unsigned_char => Some(Type::named_int(size, false, &name)),
5555
constants::DW_ATE_imaginary_float => None,
5656
constants::DW_ATE_packed_decimal => None,
5757
constants::DW_ATE_numeric_string => None,
5858
constants::DW_ATE_edited => None,
5959
constants::DW_ATE_signed_fixed => None,
6060
constants::DW_ATE_unsigned_fixed => None,
61-
constants::DW_ATE_decimal_float => Some(Type::named_float(size, name)),
62-
constants::DW_ATE_UTF => Some(Type::named_int(size, false, name)), // TODO : Verify
61+
constants::DW_ATE_decimal_float => Some(Type::named_float(size, &name)),
62+
constants::DW_ATE_UTF => Some(Type::named_int(size, false, &name)), // TODO : Verify
6363
constants::DW_ATE_UCS => None,
6464
constants::DW_ATE_ASCII => None, // Some sort of array?
6565
constants::DW_ATE_lo_user => None,
@@ -114,7 +114,7 @@ pub(crate) fn handle_enum<R: ReaderType>(
114114
match &child.entry().attr(constants::DW_AT_const_value) {
115115
Ok(Some(attr)) => {
116116
if let Some(value) = get_attr_as_u64(attr) {
117-
enumeration_builder.insert(name, value);
117+
enumeration_builder.insert(&name, value);
118118
} else {
119119
// Somehow the child entry is not a const value.
120120
log::error!("Unhandled enum member value type for `{}`", name);

plugins/dwarf/dwarf_import/src/dwarfdebuginfo.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,7 @@ impl DebugInfoBuilder {
546546
assert!(debug_info.add_data_variable(
547547
address,
548548
&self.get_type(*type_uid).unwrap().ty,
549-
name.clone(),
549+
name.as_deref(),
550550
&[] // TODO : Components
551551
));
552552
}

plugins/dwarf/dwarf_import/src/helpers.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
use std::ffi::OsStr;
1616
use std::path::{Path, PathBuf};
17-
use std::{collections::HashMap, ops::Deref, str::FromStr, sync::mpsc};
17+
use std::{ops::Deref, str::FromStr, sync::mpsc};
1818

1919
use crate::{DebugInfoBuilderContext, ReaderType};
2020
use binaryninja::binary_view::BinaryViewBase;
@@ -450,8 +450,8 @@ pub(crate) fn download_debug_info(
450450
let result = inst
451451
.perform_custom_request(
452452
"GET",
453-
artifact_url,
454-
HashMap::<String, String>::new(),
453+
&artifact_url,
454+
vec![],
455455
DownloadInstanceInputOutputCallbacks {
456456
read: None,
457457
write: Some(Box::new(write)),

plugins/dwarf/dwarf_import/src/types.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -215,17 +215,17 @@ fn do_structure_parse<R: ReaderType>(
215215
});
216216

217217
structure_builder.insert(
218-
child_type.as_ref(),
219-
child_name,
218+
&child_type,
219+
&child_name,
220220
struct_offset,
221221
false,
222222
MemberAccess::NoAccess, // TODO : Resolve actual scopes, if possible
223223
MemberScope::NoScope,
224224
);
225225
} else {
226226
structure_builder.append(
227-
child_type.as_ref(),
228-
child_name,
227+
&child_type,
228+
&child_name,
229229
MemberAccess::NoAccess,
230230
MemberScope::NoScope,
231231
);

plugins/dwarf/shared/src/lib.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,10 @@ pub fn create_section_reader<'a, Endian: 'a + Endianity>(
179179
}
180180
}
181181
// Truncate Mach-O section names to 16 bytes
182-
else if let Some(section) =
183-
view.section_by_name("__".to_string() + &section_name[1..section_name.len().min(15)])
184-
{
182+
else if let Some(section) = view.section_by_name(&format!(
183+
"__{}",
184+
&section_name[1..section_name.len().min(15)]
185+
)) {
185186
Ok(EndianRcSlice::new(
186187
Rc::from(view.read_vec(section.start(), section.len()).as_slice()),
187188
endian,

plugins/idb_import/src/lib.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ pub fn import_til_section(
198198
if let TranslateTypeResult::Translated(bn_ty)
199199
| TranslateTypeResult::PartiallyTranslated(bn_ty, _) = &ty.ty
200200
{
201-
if !debug_info.add_type(ty.name.as_utf8_lossy(), bn_ty, &[/* TODO */]) {
201+
if !debug_info.add_type(&ty.name.as_utf8_lossy(), bn_ty, &[/* TODO */]) {
202202
error!("Unable to add type `{}`", ty.name.as_utf8_lossy())
203203
}
204204
}
@@ -209,7 +209,7 @@ pub fn import_til_section(
209209
if let TranslateTypeResult::Translated(bn_ty)
210210
| TranslateTypeResult::PartiallyTranslated(bn_ty, _) = &ty.ty
211211
{
212-
if !debug_info.add_type(ty.name.as_utf8_lossy(), bn_ty, &[/* TODO */]) {
212+
if !debug_info.add_type(&ty.name.as_utf8_lossy(), bn_ty, &[/* TODO */]) {
213213
error!("Unable to fix type `{}`", ty.name.as_utf8_lossy())
214214
}
215215
}
@@ -239,10 +239,7 @@ fn parse_id0_section_info(
239239
} = info;
240240
// TODO set comments to address here
241241
for function in &bv.functions_containing(addr) {
242-
function.set_comment_at(
243-
addr,
244-
String::from_utf8_lossy(&comments.join(&b"\n"[..])).to_string(),
245-
);
242+
function.set_comment_at(addr, &String::from_utf8_lossy(&comments.join(&b"\n"[..])));
246243
}
247244

248245
let bnty = ty

plugins/idb_import/src/types.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ impl<F: Fn(usize, usize) -> Result<(), ()>> TranslateIDBTypes<'_, F> {
335335
format!("bitfield_{}_{}", offset + start_idx, offset + (i - 1))
336336
};
337337
let field = field_from_bytes(bytes);
338-
struct_builder.append(&field, name, MemberAccess::NoAccess, MemberScope::NoScope);
338+
struct_builder.append(&field, &name, MemberAccess::NoAccess, MemberScope::NoScope);
339339
};
340340

341341
for (i, member) in members {
@@ -423,7 +423,7 @@ impl<F: Fn(usize, usize) -> Result<(), ()>> TranslateIDBTypes<'_, F> {
423423
.as_ref()
424424
.map(|name| name.as_utf8_lossy().to_string())
425425
.unwrap_or_else(|| format!("member_{i}"));
426-
structure.append(&mem, name, MemberAccess::NoAccess, MemberScope::NoScope);
426+
structure.append(&mem, &name, MemberAccess::NoAccess, MemberScope::NoScope);
427427
}
428428
if let Some(start_idx) = first_bitfield_seq {
429429
let members_bitrange = &ty_struct.members[start_idx..];
@@ -470,7 +470,7 @@ impl<F: Fn(usize, usize) -> Result<(), ()>> TranslateIDBTypes<'_, F> {
470470
.as_ref()
471471
.map(|name| name.as_utf8_lossy().to_string())
472472
.unwrap_or_else(|| format!("member_{i}"));
473-
structure.append(&mem, name, MemberAccess::NoAccess, MemberScope::NoScope);
473+
structure.append(&mem, &name, MemberAccess::NoAccess, MemberScope::NoScope);
474474
}
475475
let str_ref = structure.finalize();
476476

@@ -492,7 +492,7 @@ impl<F: Fn(usize, usize) -> Result<(), ()>> TranslateIDBTypes<'_, F> {
492492
.as_ref()
493493
.map(|name| name.as_utf8_lossy().to_string())
494494
.unwrap_or_else(|| format!("member_{i}"));
495-
eb.insert(name, member.value);
495+
eb.insert(&name, member.value);
496496
}
497497
Type::enumeration(
498498
&eb.finalize(),

plugins/pdb-ng/src/lib.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
// limitations under the License.
1414
#![allow(dead_code)]
1515

16-
use std::collections::HashMap;
1716
use std::env::{current_dir, current_exe, temp_dir};
1817
use std::io::Cursor;
1918
use std::path::PathBuf;
@@ -31,7 +30,6 @@ use binaryninja::download_provider::{DownloadInstanceInputOutputCallbacks, Downl
3130
use binaryninja::interaction::{MessageBoxButtonResult, MessageBoxButtonSet};
3231
use binaryninja::logger::Logger;
3332
use binaryninja::settings::{QueryOptions, Settings};
34-
use binaryninja::string::BnString;
3533
use binaryninja::{interaction, user_directory};
3634
use parser::PDBParserInstance;
3735

@@ -196,7 +194,7 @@ fn read_from_sym_store(bv: &BinaryView, path: &str) -> Result<(bool, Vec<u8>)> {
196194
.perform_custom_request(
197195
"GET",
198196
path,
199-
HashMap::<BnString, BnString>::new(),
197+
vec![],
200198
DownloadInstanceInputOutputCallbacks {
201199
read: None,
202200
write: Some(Box::new(write)),
@@ -278,21 +276,21 @@ fn search_sym_store(
278276
}
279277

280278
fn parse_pdb_info(view: &BinaryView) -> Option<PDBInfo> {
281-
match view.get_metadata::<u64, _>("DEBUG_INFO_TYPE") {
279+
match view.get_metadata::<u64>("DEBUG_INFO_TYPE") {
282280
Some(Ok(0x53445352 /* 'SDSR' */)) => {}
283281
_ => return None,
284282
}
285283

286284
// This is stored in the BV by the PE loader
287-
let file_path = match view.get_metadata::<String, _>("PDB_FILENAME") {
285+
let file_path = match view.get_metadata::<String>("PDB_FILENAME") {
288286
Some(Ok(md)) => md,
289287
_ => return None,
290288
};
291-
let mut guid = match view.get_metadata::<Vec<u8>, _>("PDB_GUID") {
289+
let mut guid = match view.get_metadata::<Vec<u8>>("PDB_GUID") {
292290
Some(Ok(md)) => md,
293291
_ => return None,
294292
};
295-
let age = match view.get_metadata::<u64, _>("PDB_AGE") {
293+
let age = match view.get_metadata::<u64>("PDB_AGE") {
296294
Some(Ok(md)) => md as u32,
297295
_ => return None,
298296
};

plugins/pdb-ng/src/parser.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,8 @@ impl<'a, S: Source<'a> + 'a> PDBParserInstance<'a, S> {
168168
) -> Result<()> {
169169
self.parse_types(Self::split_progress(&progress, 0, &[1.0, 3.0, 0.5, 0.5]))?;
170170
for (name, ty) in self.named_types.iter() {
171-
self.debug_info.add_type(name, ty.as_ref(), &[]); // TODO : Components
171+
self.debug_info
172+
.add_type(&name.to_string(), ty.as_ref(), &[]); // TODO : Components
172173
}
173174

174175
info!(
@@ -406,7 +407,8 @@ impl<'a, S: Source<'a> + 'a> PDBParserInstance<'a, S> {
406407
match class {
407408
NamedTypeReferenceClass::UnknownNamedTypeClass
408409
| NamedTypeReferenceClass::TypedefNamedTypeClass => {
409-
self.debug_info.add_type(&name, Type::void().as_ref(), &[]);
410+
self.debug_info
411+
.add_type(&name.to_string(), Type::void().as_ref(), &[]);
410412
// TODO : Components
411413
}
412414
NamedTypeReferenceClass::ClassNamedTypeClass
@@ -429,15 +431,15 @@ impl<'a, S: Source<'a> + 'a> PDBParserInstance<'a, S> {
429431
structure.alignment(1);
430432

431433
self.debug_info.add_type(
432-
&name,
434+
&name.to_string(),
433435
Type::structure(structure.finalize().as_ref()).as_ref(),
434436
&[], // TODO : Components
435437
);
436438
}
437439
NamedTypeReferenceClass::EnumNamedTypeClass => {
438440
let enumeration = EnumerationBuilder::new();
439441
self.debug_info.add_type(
440-
&name,
442+
&name.to_string(),
441443
Type::enumeration(
442444
enumeration.finalize().as_ref(),
443445
self.arch.default_integer_size().try_into()?,

plugins/pdb-ng/src/struct_grouper.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ pub fn group_structure(
361361
for member in members {
362362
structure.insert(
363363
&member.ty,
364-
member.name.clone(),
364+
&member.name,
365365
member.offset,
366366
false,
367367
member.access,
@@ -390,7 +390,7 @@ fn apply_groups(
390390
if offset > member.offset {
391391
structure.insert(
392392
&member.ty,
393-
member.name.clone(),
393+
&member.name,
394394
0,
395395
false,
396396
member.access,
@@ -399,7 +399,7 @@ fn apply_groups(
399399
} else {
400400
structure.insert(
401401
&member.ty,
402-
member.name.clone(),
402+
&member.name,
403403
member.offset - offset,
404404
false,
405405
member.access,
@@ -412,7 +412,7 @@ fn apply_groups(
412412
apply_groups(members, &mut inner, children, inner_offset);
413413
structure.insert(
414414
&Conf::new(Type::structure(inner.finalize().as_ref()), MAX_CONFIDENCE),
415-
format!("__inner{}", i),
415+
&format!("__inner{}", i),
416416
inner_offset - offset,
417417
false,
418418
MemberAccess::PublicAccess,
@@ -425,7 +425,7 @@ fn apply_groups(
425425
apply_groups(members, &mut inner, children, inner_offset);
426426
structure.insert(
427427
&Conf::new(Type::structure(inner.finalize().as_ref()), MAX_CONFIDENCE),
428-
format!("__inner{}", i),
428+
&format!("__inner{}", i),
429429
inner_offset - offset,
430430
false,
431431
MemberAccess::PublicAccess,

0 commit comments

Comments
 (0)