Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace lazy_static crate with std::sync::LazyLock #5898

Closed
wants to merge 1 commit into from
Closed
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
7 changes: 0 additions & 7 deletions rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ rust-version = "1.83.0"
noexports = []

[dependencies]
lazy_static = "1.4.0"
log = { version = "0.4", features = ["std"] }
rayon = { version = "1.8", optional = true }
binaryninjacore-sys = { path = "binaryninjacore-sys" }
Expand Down
114 changes: 53 additions & 61 deletions rust/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ use crate::{
symbol::Symbol,
};

use lazy_static::lazy_static;
use std::ptr::null_mut;
use std::{
borrow::{Borrow, Cow},
collections::{HashMap, HashSet},
Expand All @@ -43,7 +41,7 @@ use std::{
ops::Range,
os::raw::c_char,
ptr, result, slice,
sync::Mutex,
sync::LazyLock,
};

pub type Result<R> = result::Result<R, ()>;
Expand Down Expand Up @@ -1231,77 +1229,71 @@ impl fmt::Display for Type {
}
}

lazy_static! {
static ref TYPE_DEBUG_BV: Mutex<Option<Ref<BinaryView>>> =
Mutex::new(BinaryView::from_data(&FileMetadata::new(), &[]).ok());
}
static TYPE_DEBUG_BV: LazyLock<Option<Ref<BinaryView>>> =
LazyLock::new(|| BinaryView::from_data(&FileMetadata::new(), &[]).ok());

impl fmt::Debug for Type {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if let Ok(lock) = TYPE_DEBUG_BV.lock() {
if let Some(bv) = &*lock {
let container = unsafe { BNGetAnalysisTypeContainer(bv.handle) };
let Some(bv) = &*TYPE_DEBUG_BV else {
return Err(fmt::Error);
};

let printer = if f.alternate() {
unsafe { BNGetTypePrinterByName(c"_DebugTypePrinter".as_ptr()) }
} else {
unsafe { BNGetTypePrinterByName(c"CoreTypePrinter".as_ptr()) }
};
if printer.is_null() {
return Err(fmt::Error);
}
let container = unsafe { BNGetAnalysisTypeContainer(bv.handle) };

let mut name = QualifiedName::from("");

let mut lines: *mut BNTypeDefinitionLine = null_mut();
let mut count: usize = 0;

unsafe {
BNGetTypePrinterTypeLines(
printer,
self.handle,
container,
&mut name.0,
64,
false,
BNTokenEscapingType::NoTokenEscapingType,
&mut lines,
&mut count,
)
};
unsafe {
BNFreeTypeContainer(container);
}
let printer = if f.alternate() {
unsafe { BNGetTypePrinterByName(c"_DebugTypePrinter".as_ptr()) }
} else {
unsafe { BNGetTypePrinterByName(c"CoreTypePrinter".as_ptr()) }
};
if printer.is_null() {
return Err(fmt::Error);
}

if lines.is_null() {
return Err(fmt::Error);
}
let mut name = QualifiedName::from("");

let line_slice: &[BNTypeDefinitionLine] =
unsafe { slice::from_raw_parts(lines, count) };
let mut lines = ptr::null_mut();
let mut count = 0;

for (i, line) in line_slice.iter().enumerate() {
if i > 0 {
writeln!(f)?;
}
unsafe {
BNGetTypePrinterTypeLines(
printer,
self.handle,
container,
&mut name.0,
64,
false,
BNTokenEscapingType::NoTokenEscapingType,
&mut lines,
&mut count,
)
};
unsafe {
BNFreeTypeContainer(container);
}

let tokens: &[BNInstructionTextToken] =
unsafe { slice::from_raw_parts(line.tokens, line.count) };
if lines.is_null() {
return Err(fmt::Error);
}

for token in tokens {
let text: *const c_char = token.text;
let str = unsafe { CStr::from_ptr(text) };
write!(f, "{}", str.to_string_lossy())?;
}
}
let line_slice = unsafe { slice::from_raw_parts(lines, count) };

unsafe {
BNFreeTypeDefinitionLineList(lines, count);
}
return Ok(());
for (i, line) in line_slice.iter().enumerate() {
if i > 0 {
writeln!(f)?;
}

let tokens = unsafe { slice::from_raw_parts(line.tokens, line.count) };

for token in tokens {
let text = unsafe { CStr::from_ptr(token.text) };
write!(f, "{}", text.to_string_lossy())?;
}
}
Err(fmt::Error)

unsafe {
BNFreeTypeDefinitionLineList(lines, count);
}
Ok(())
}
}

Expand Down