summaryrefslogtreecommitdiff
path: root/rust/src/disassembly.rs
diff options
context:
space:
mode:
authorMason Reed <mason@vector35.com>2024-12-22 01:21:02 -0500
committerMason Reed <mason@vector35.com>2025-01-25 00:52:29 -0500
commit6a63c17853009ce7a0071458e39c140a09440230 (patch)
treead59ab3b356b0a2c60423e55f62c996891788788 /rust/src/disassembly.rs
parentbfa1d409b2d5e734804a5fdb4c68c90a93712f1f (diff)
Rust refactor
Moves a bunch of stuff out of src/types.rs that did not belong: - Confidence - Variable - Function specific stuff - Refactored InstructionInfo, see the msp430 and riscv examples. - Renamed Function::from_raw to Function::ref_from_raw and fixed places where the ref was incremented twice - Fixed FunctionRecognizer leaking functions (see above) - Fixed some apis incorrectly returning Result where Option is clearer - Started to move destructured types to the From trait for converting to an from ffi types, see Location for an example - Started to remove bad module level imports (importing std modules like mem all over the place) - Moved some wrapper handle types to named handle field (this improves readability), see CoreArchitecture for an example - Removed some unintuitive getters, this is bad practice for Rust code, just access the field directly, see DataVariable for an example - General code cleanup, purposely did not run rustfmt, that will be a single seperate commit More rust cleanup - Fixed invalid views being able to invoke UB when dealing with databases - Cleaned up some helper code in dwarf_import - Fixed inverted is_null checks causing crashes! Oops! More rust cleanup Still a WIP, I think branch info is still invalid, need to figure out the issue there. - Fixed some invalid Ref lifetimes when constructing indirectly, see Array<DataVariable> for example - Added some more comments - Removed some "magic" functions like MLIL Function::ssa_variables There are still a bunch of invalid lifetimes that aren't crashing us due to the usage of those API's not living long enough. But they ARE an issue. More rust cleanup Trying to comment more TODO's as I go along. - Renamed llil::Function to llil::LowLevelILFunction for clarity and consistency - Take base structures by ref in StructureBuilder::set_base_structures to prevent premature drops - Added more raw to wrapper conversions - Removed UB prone apis - Getting rid of more core module references, use std! - Removed improper Guard usage in array impls for wrapper types with no context - Untangling the UB of the Label api, still only half done :/ More rust cleanup - Misc formatting - Made Logger ref counted - Fixed leaking name of logger every time something was logged - Fixed the last (hopefully) of the unresolved labels - Simplified some code Fix leak in DebugInfo::AddType componentArray was never freed Add more HLIL related functions to rust More rust cleanup improve the CustomBinaryView init process Canonicalize path in `create_delete_empty` test Link core in rust When linking you must depend on the -sys crate. This is because linker arguments (what says where to find binaryninjacore) are NOT transitive. The top level application crate MUST provide it. For more information see: - https://github.com/rust-lang/cargo/issues/9554#issuecomment-857882964 - https://github.com/oxidecomputer/omicron/pull/225 Remove vendored pdb crate Use cargo to manage the git repo ref instead Fix misc rustdoc warnings Move actual plugins out of `rust/examples` and into `plugins` This is where all shipped public plugins that are not arch/view/platform/lang will be at from now on Originally they were in the rust workspace, meaning they all shared a Cargo.lock which is ill-advised. More rust cleanup - More clarification on plugin/executable requirements - Made examples actually rust examples - Add Display impl for InstructionTextToken - Renamed feature "noexports" to "no_exports" Move under_construction.png to assets directory This really did bother me Remove unneeded `extern crate bindgen` Replace nop'd log::info with println We don't register a compatible log sink so they will just get sent into the void Move inline tests into tests directory This is being done in the hopes of curbing the multi-thousand line files that will occur once we flesh out the tests Format rust code Update rust ci Still need to add support for running tests in ci More rust cleanup - Architecture id's are now typed accordingly - Fix some clippy lints - Make instruction index public in LLIL - Removed useless helper functions - LLIL expressions and instruction indexes are now typed accordingly Generate binaryninjacore-sys documentation This should show binaryninjacore-sys alongside binaryninja crate More rust cleanup - Remove lazy_static dependency - Remove hacky impl Debug for Type and just use the display impl - Add more debug impls - Reorder some top level namespace items - Add first type test Remove unneeded script helper in rust api More rust cleanup - Added main thread handler api - Register a headless main thread handler by default in initialization - Refactor QualifiedName to be properly owned - Loosened some type constraints on some apis involving QualifiedName - Fixed some apis that were crashing due to incorrect param types - Removed extern crate cruft for log crate - Simplified headless initialization using more wrapper apis - Fixed segments leaking because of no ref wrapper, see BinaryViewExt::segment_at - Added rstest to manage headless init in unit tests - Added some more unit tests - Refactored demangler api to be more ergonomic - Fixed minidump plugin not building More rust cleanup - Fixup usage of QualifiedName in plugins - Make QualifiedName more usable Implement rust TypeParser fix Platform TypeParser related functions separate User and System implementations of TypeParserResult Implement rust TypeContainer More rust cleanup - Hopefully fixed the rust.yml CI - Added TypePrinter API (this is still WIP and will crash) - Added TypeParser API - Added TypeContainer API - More work around QualifiedName apis Oh your suppose to do this Add workflow_dispatch trigger to rust.yml More rust fixes - Swapped some usage of raw 255 to MAX_CONFIDENCE, no one likes magic numbers - New InstructionTextToken API, complete with owned data, this still needs a lot of testing. - InstructionTextTokenKind describes a destructured InstructionTextToken, this should make token usage much clearer, some docs pending - Added some misc Default and Debug impls - Updated TypePrinter and architectures to use new InstructionTextToken API Misc formatting changes More rust cleanup - Fixed MANY memory leaks (most due to QualifiedName) - Made StructureBuilder more builder and less structure - Fixed CMakeLists.txt that were globbing entire api, resulting in 100 second slowdown on cmake generation - Added more Debug impl's - Added some more tests - Fixed TypeParserResult UB - Moved the From impls to blank impl for clarity, we have multiple different variants of core to rust for some structures, it should be explicit which one you are choosing. - PossibleValueSet should now be able to allocate so we can go from rust to core with those variants that require allocation - Misc doc code formatting Misc clippy lints and clippy CI Co-authored-by: Michael Krasnitski <michael.krasnitski@gmail.com> Fix typo in rust CI Misc rust formatting Fix misc typos and add typos to rust CI Add cargo workspace This will help tooling and external contributors get a map of the rust crates within binaryninja-api More rust cleanup - Format all rust plugins - Fix some tests that were out of date - Simplify WARP tests to only binaries, building object files from source is a pain - Link to core in all rust plugins - Fix some memory leaks - Update warp insta snapshots - Fix some misc clippy lints Run rust tests in CI This commit also coincides with the creation of the "testing" environment which exposes a BN_SERIAL secret for pulling a headless Binary Ninja Install missing wayland dependency in github CI Apparently its needed for linux file picker for the WARP integration Set the BINARYNINJADIR so rust can find binaryninjacore in CI The chances of this working are low Misc remove unused dependency Rust misc formatting fixes Improve initialization in rust headless scripts Provide sensible errors and validation to rust headless scripts, solves https://github.com/Vector35/binaryninja-api/issues/5796 Add BN_LICENSE environment variable to rust CI We pass the serial to download binary ninja, but we never provided the license for core initialization Fix typo More rust cleanup - Improved binary view initialization (see init_with_opts) - Allow floating license to free itself before initialization - Add initialization unit test - Add better Debug impls for some common types - Use Path api for opening binary views, this is not breaking as it uses the AsRef impl - Bump rayon dependency and constrain dependencies to x.x Update readme and include that directly in the rustdocs More rust documentation changes Add format comment to InitializationOptions::with_license Misc formatting and clippy lint allow More rust cleanup - Remove under_construction.png from the build.rs it has been removed - Use InstructionIndex in more places - Add missing PartialOrd and Ord impls for id types More rust cleanup - Make workflow cloning explicit - Add workflow tests - Add missing property string list getting for settings - Remove IntoActivityName (see https://github.com/Vector35/binaryninja-api/pull/6257) More rust cleanup This commit is half done Misc rust formatting More rust cleanup - Renamed common name conflictions (I will put my justification in the PR) - Fixed invalid instruction retrieval for LLIL - Added common aliases for llil function, instruction and expression types (see my comment in the PR) - Refactored the instruction retrieval for LLIL, MLIL and HLIL - Added instruction index types to MLIL and HLIL - Moved llil module to lowlevelil module (mlil and hlil will be moved as well) - Added preliminary LLIL unit testing Fix typos Misc clippy fixes More rust cleanup - Normalized modules - Split some code out into own files - Fixed some UB in type archive and projects - Improved API around type archives and projects substantially - Added ProgressExecutor abstraction for functions which have a progress callback - Improved background task documentation and added unit tests - Added worker thread api and unit tests - Moved some owned types to ref types, this is still not complete, but this is the path forward. - Add external location/library accessors to the binary view - Added some misc documentation - Replaced mod.rs with the module name source file Still need to normalize some paths and also update some documentation surrounding that change. Update some tests and examples Fix background task tests colliding We were creating multiple background tasks with the same progress text on multiple threads More rust cleanup - Fixed progress executor freeing itself after one iteration - Updated the last of the doc imports - Moved mainthread to main_thread - Made project creation and opening failable We could probably AsRef<ProgressExecutor> to get around the allocation and free inside the function bodies, not high priority as those functions are long running anyways. Move binary view initialization into function body for LLIL test Normalize test file names More rust cleanup - Updated README to clarify offline documentation - Refactored settings api - Added settings example to show dumping settings value and specific properties - Use the workspace to depend on binaryninja and binaryninjacore-sys - Remove binaryninjacore-sys as a workspace member (its not really required) Update workflow test to new settings api More rust cleanup - Rename Label to LowLevelILLabel - Update the functions label map automatically This fixed a major api blunder where the label map is returned as a reference and originally resulted in UB prone lifetime semantics. It was temporarily "fixed" with explicit label updates in the architecture implementation code. But that was less than ideal and was easy to mess up. Now the label map will be updated automatically as the location of labels is now tracked. Misc clippy lints More rust cleanup - Get rid of RawFunctionViewType - Add better Debug impl for Function More rust cleanup - Fixed the documentation icon using local files (thank you @mkrasnitski) - Fixed labels being updated and overwriting the label location used to update the label map More rust cleanup - Added unit tests for MLIL and HLIL - "Fixed" MLIL, LLIL, and HLIL having issues regarding Instruction vs Expression indexes - Renamed CallingConvention to CoreCallingConvention and removed architecture generic - Renamed CallingConventionBase to CallingConvention - Simplified calling convention code and fixed some bugs with implicit registers - Added impl Debug to MLIL and HLIL instructions Still need to at some point add an Expression to MLIL and HLIL. We also might want to look into having the Instruction kind just return the expression kind. Misc clippy lint More rust cleanup - Allow calling conventions to be registered for multiple architectures - Swapped a unreachable statement to an unimplemented statement More rust cleanup - Fixed the issue with PDB types, this has caused me an insane amount of grief - Fixed LLIL visit_tree missing LLIL_LOAD expressions - Added LLIL visitor test - Made all WARP file pickers use the rfd crate Use the dev branch of Binary Ninja in rust CI Misc rust fmt More rust cleanup - Refactored BinaryReader and BinaryWriter - Added some warnings to high_level_il and medium_level_il modules that they are unstable - Add BinaryReader and BinaryWriter tests - Changed BinaryView len to return u64 (that is what the core returns) - Misc formatting changes - Remove extern uses in lib.rs Add impl Debug for BinaryReader and BinaryWriter Turn off broken tests Add more info to the rust README.md More rust cleanup - Make EdgeStyle type not wrap raw - Regression tests for WARP will run on all bins in the out dir now impl rust Collaboration and Remote API Fix typo Update collaboration API Makes collaboration more in line with the refactor. Still a lot of work to do. Namely still need: - Proper errors - _with_opts functions - More ergonomic api - Better connection procedure - Updated documentation - A LOT of unit tests - An example - Typed id's for everything (i dont want BnString as the id!!!) - NEED to refactor the progress callbacks into the new progress api, but we should pull in some of the stuff the collab progress has - Elimination of apis that are dumb helpers Separate out the rust testing and use pull_request_target pull_request_target allows PR's to access the headless license, for this to be safe we need to prevent people from running the job. To prevent the job from being ran we add an environment requirement on testing that a reviewer must review the code and then manually approve it to run. More rust cleanup - Use GroupId instead of u64 - Use ProgressCallback in place of ProgressExecutor - Misc cleanup of FileMetadata - Add `save_to_path` and `save_to_accessor` to save modified binaries - Added binary_view unit tests - Added collaboration unit tests - Fixed a few issues with the collaboration apis - Renamed Command registration functions so that there is no import ambiguity - Split out RemoteUndoEntry - Collaboration apis now have a explicit `_with_progress` set of apis - Misc clippy lint fixes Fix some typos More rust cleanup - Add extra info to README.md - Refactor components api - Add components unit test Add testing and documentation to contributing section in README.md Fix misc doc comments
Diffstat (limited to 'rust/src/disassembly.rs')
-rw-r--r--rust/src/disassembly.rs1208
1 files changed, 847 insertions, 361 deletions
diff --git a/rust/src/disassembly.rs b/rust/src/disassembly.rs
index 5d3da31e..99938e5f 100644
--- a/rust/src/disassembly.rs
+++ b/rust/src/disassembly.rs
@@ -11,467 +11,955 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
-
-// TODO : Combine this with the architecture implementation
+#![allow(unused)]
use binaryninjacore_sys::*;
-use crate::string::BnString;
-use crate::{BN_FULL_CONFIDENCE, BN_INVALID_EXPR};
+use crate::string::{raw_to_string, strings_to_string_list, BnString};
use crate::rc::*;
+use crate::confidence::MAX_CONFIDENCE;
+use crate::function::HighlightColor;
+use crate::tags::Tag;
+use crate::types::Type;
use std::convert::From;
-use std::ffi::CStr;
-use std::mem;
-use std::ptr;
+use std::fmt::{Display, Formatter};
+pub type DisassemblyOption = BNDisassemblyOption;
pub type InstructionTextTokenType = BNInstructionTextTokenType;
-pub type InstructionTextTokenContext = BNInstructionTextTokenContext;
-
-// InstructionTextTokenType's; * = Implemented
-// *TextToken = 0,
-// *InstructionToken = 1,
-// *OperandSeparatorToken = 2,
-// *RegisterToken = 3,
-// *IntegerToken = 4,
-// *PossibleAddressToken = 5,
-// *BeginMemoryOperandToken = 6,
-// *EndMemoryOperandToken = 7,
-// *FloatingPointToken = 8,
-// AnnotationToken = 9,
-// *CodeRelativeAddressToken = 10,
-// ArgumentNameToken = 11,
-// HexDumpByteValueToken = 12,
-// HexDumpSkippedByteToken = 13,
-// HexDumpInvalidByteToken = 14,
-// HexDumpTextToken = 15,
-// OpcodeToken = 16,
-// *StringToken = 17,
-// CharacterConstantToken = 18,
-// *KeywordToken = 19,
-// *TypeNameToken = 20,
-// *FieldNameToken = 21,
-// *NameSpaceToken = 22,
-// NameSpaceSeparatorToken = 23,
-// TagToken = 24,
-// StructOffsetToken = 25,
-// StructOffsetByteValueToken = 26,
-// StructureHexDumpTextToken = 27,
-// *GotoLabelToken = 28,
-// CommentToken = 29,
-// PossibleValueToken = 30,
-// PossibleValueTypeToken = 31,
-// ArrayIndexToken = 32,
-// *IndentationToken = 33,
-// UnknownMemoryToken = 34,
-// CodeSymbolToken = 64,
-// DataSymbolToken = 65,
-// LocalVariableToken = 66,
-// ImportToken = 67,
-// AddressDisplayToken = 68,
-// IndirectImportToken = 69,
-// ExternalSymbolToken = 70,
-
-#[repr(transparent)]
-pub struct InstructionTextToken(pub(crate) BNInstructionTextToken);
+pub type StringType = BNStringType;
-#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
-pub enum InstructionTextTokenContents {
- Text,
- Instruction,
- OperandSeparator,
- Register,
- Integer(u64), // TODO size?
- PossibleAddress(u64), // TODO size?
- BeginMemoryOperand,
- EndMemoryOperand,
- FloatingPoint,
- CodeRelativeAddress(u64),
- String(u64),
- Keyword,
- TypeName,
- FieldName,
- NameSpace,
- GotoLabel(u64),
- Indentation,
- Brace,
+#[derive(Clone, PartialEq, Debug, Default)]
+pub struct DisassemblyTextLine {
+ pub address: u64,
+ pub instruction_index: usize,
+ pub tokens: Vec<InstructionTextToken>,
+ pub highlight: HighlightColor,
+ pub tags: Vec<Ref<Tag>>,
+ pub type_info: DisassemblyTextLineTypeInfo,
}
-impl InstructionTextToken {
- pub(crate) unsafe fn from_raw(raw: &BNInstructionTextToken) -> &Self {
- mem::transmute(raw)
+impl DisassemblyTextLine {
+ pub(crate) fn from_raw(value: &BNDisassemblyTextLine) -> Self {
+ let raw_tokens = unsafe { std::slice::from_raw_parts(value.tokens, value.count) };
+ let tokens: Vec<_> = raw_tokens
+ .iter()
+ .map(InstructionTextToken::from_raw)
+ .collect();
+ // SAFETY: Increment the tag ref as we are going from ref to owned.
+ let raw_tags = unsafe { std::slice::from_raw_parts(value.tags, value.tagCount) };
+ let tags: Vec<_> = raw_tags
+ .iter()
+ .map(|&t| unsafe { Tag::from_raw(t) }.to_owned())
+ .collect();
+ Self {
+ address: value.addr,
+ instruction_index: value.instrIndex,
+ tokens,
+ highlight: value.highlight.into(),
+ tags,
+ type_info: DisassemblyTextLineTypeInfo::from_raw(&value.typeInfo),
+ }
}
- pub(crate) fn into_raw(self) -> BNInstructionTextToken {
- mem::ManuallyDrop::new(self).0
+ /// Convert into a raw [BNDisassemblyTextLine], use with caution.
+ ///
+ /// NOTE: The allocations here for tokens and tags MUST be freed by rust using [Self::free_raw].
+ pub(crate) fn into_raw(value: Self) -> BNDisassemblyTextLine {
+ // NOTE: The instruction text and type names fields are being leaked here. To be freed with [Self::free_raw].
+ let tokens: Box<[BNInstructionTextToken]> = value
+ .tokens
+ .into_iter()
+ .map(InstructionTextToken::into_raw)
+ .collect();
+ let tags: Box<[*mut BNTag]> = value
+ .tags
+ .into_iter()
+ .map(|t| {
+ // SAFETY: The tags ref will be temporarily incremented here, until [Self::free_raw] is called.
+ // SAFETY: This is so that tags lifetime is long enough, as we might be the last holders of the ref.
+ unsafe { Ref::into_raw(t) }.handle
+ })
+ .collect();
+ BNDisassemblyTextLine {
+ addr: value.address,
+ instrIndex: value.instruction_index,
+ count: tokens.len(),
+ // NOTE: Leaking tokens here to be freed with [Self::free_raw].
+ tokens: Box::leak(tokens).as_mut_ptr(),
+ highlight: value.highlight.into(),
+ tagCount: tags.len(),
+ // NOTE: Leaking tags here to be freed with [Self::free_raw].
+ tags: Box::leak(tags).as_mut_ptr(),
+ typeInfo: DisassemblyTextLineTypeInfo::into_raw(value.type_info),
+ }
}
- pub fn new(text: &str, contents: InstructionTextTokenContents) -> Self {
- let (value, address) = match contents {
- InstructionTextTokenContents::Integer(v) => (v, 0),
- InstructionTextTokenContents::PossibleAddress(v)
- | InstructionTextTokenContents::CodeRelativeAddress(v)
- | InstructionTextTokenContents::GotoLabel(v) => (v, v),
- InstructionTextTokenContents::String(v) => (v, 0),
- _ => (0, 0),
- };
+ /// Frees raw object created with [Self::into_raw], use with caution.
+ ///
+ /// NOTE: The allocations freed MUST have been created in rust using [Self::into_raw].
+ pub(crate) fn free_raw(value: BNDisassemblyTextLine) {
+ // Free the token list
+ let raw_tokens = unsafe { std::slice::from_raw_parts_mut(value.tokens, value.count) };
+ let boxed_tokens = unsafe { Box::from_raw(raw_tokens) };
+ for token in boxed_tokens {
+ // SAFETY: As we have leaked the token contents we need to now free them (text and typeNames).
+ InstructionTextToken::free_raw(token);
+ }
+ // Free the tag list
+ let raw_tags = unsafe { std::slice::from_raw_parts_mut(value.tags, value.tagCount) };
+ let boxed_tags = unsafe { Box::from_raw(raw_tags) };
+ for tag in boxed_tags {
+ // SAFETY: As we have incremented the tags ref in [Self::into_raw] we must now decrement.
+ let _ = unsafe { Tag::ref_from_raw(tag) };
+ }
+ // Free the type info
+ DisassemblyTextLineTypeInfo::free_raw(value.typeInfo);
+ }
- let type_ = match contents {
- InstructionTextTokenContents::Text => InstructionTextTokenType::TextToken,
- InstructionTextTokenContents::Instruction => InstructionTextTokenType::InstructionToken,
- InstructionTextTokenContents::OperandSeparator => {
- InstructionTextTokenType::OperandSeparatorToken
- }
- InstructionTextTokenContents::Register => InstructionTextTokenType::RegisterToken,
- InstructionTextTokenContents::Integer(_) => InstructionTextTokenType::IntegerToken,
- InstructionTextTokenContents::PossibleAddress(_) => {
- InstructionTextTokenType::PossibleAddressToken
- }
- InstructionTextTokenContents::BeginMemoryOperand => {
- InstructionTextTokenType::BeginMemoryOperandToken
- }
- InstructionTextTokenContents::EndMemoryOperand => {
- InstructionTextTokenType::EndMemoryOperandToken
- }
- InstructionTextTokenContents::FloatingPoint => {
- InstructionTextTokenType::FloatingPointToken
- }
- InstructionTextTokenContents::CodeRelativeAddress(_) => {
- InstructionTextTokenType::CodeRelativeAddressToken
- }
- InstructionTextTokenContents::String(_) => InstructionTextTokenType::StringToken,
- InstructionTextTokenContents::Keyword => InstructionTextTokenType::KeywordToken,
- InstructionTextTokenContents::TypeName => InstructionTextTokenType::TypeNameToken,
- InstructionTextTokenContents::FieldName => InstructionTextTokenType::FieldNameToken,
- InstructionTextTokenContents::NameSpace => InstructionTextTokenType::NameSpaceToken,
- InstructionTextTokenContents::GotoLabel(_) => InstructionTextTokenType::GotoLabelToken,
- InstructionTextTokenContents::Indentation => InstructionTextTokenType::IndentationToken,
- InstructionTextTokenContents::Brace => InstructionTextTokenType::BraceToken,
- };
+ pub fn new(tokens: Vec<InstructionTextToken>) -> Self {
+ Self {
+ tokens,
+ ..Default::default()
+ }
+ }
+}
- let width = text.len() as u64;
+impl From<&str> for DisassemblyTextLine {
+ fn from(value: &str) -> Self {
+ Self::new(vec![InstructionTextToken::new(
+ value,
+ InstructionTextTokenKind::Text,
+ )])
+ }
+}
- InstructionTextToken(BNInstructionTextToken {
- type_,
- text: BnString::new(text).into_raw(),
+impl From<String> for DisassemblyTextLine {
+ fn from(value: String) -> Self {
+ Self::new(vec![InstructionTextToken::new(
value,
- width,
- size: 0,
- operand: 0xffff_ffff,
- context: InstructionTextTokenContext::NoTokenContext,
- confidence: BN_FULL_CONFIDENCE,
- address,
- typeNames: ptr::null_mut(),
- namesCount: 0,
- exprIndex: BN_INVALID_EXPR,
- })
+ InstructionTextTokenKind::Text,
+ )])
}
+}
- pub fn set_value(&mut self, value: u64) {
- self.0.value = value;
+impl Display for DisassemblyTextLine {
+ fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
+ for token in &self.tokens {
+ write!(f, "{}", token)?;
+ }
+ Ok(())
}
+}
- pub fn set_context(&mut self, context: InstructionTextTokenContext) {
- self.0.context = context;
+impl CoreArrayProvider for DisassemblyTextLine {
+ type Raw = BNDisassemblyTextLine;
+ type Context = ();
+ type Wrapped<'a> = Self;
+}
+
+unsafe impl CoreArrayProviderInner for DisassemblyTextLine {
+ unsafe fn free(raw: *mut Self::Raw, count: usize, _context: &Self::Context) {
+ BNFreeDisassemblyTextLines(raw, count)
}
- pub fn text(&self) -> &str {
- unsafe { CStr::from_ptr(self.0.text) }.to_str().unwrap()
+ unsafe fn wrap_raw<'a>(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped<'a> {
+ Self::from_raw(raw)
}
+}
- pub fn contents(&self) -> InstructionTextTokenContents {
- use self::BNInstructionTextTokenType::*;
- use self::InstructionTextTokenContents::*;
+#[derive(Default, Clone, PartialEq, Eq, Debug, Hash)]
+pub struct DisassemblyTextLineTypeInfo {
+ pub has_type_info: bool,
+ pub parent_type: Option<Ref<Type>>,
+ pub field_index: usize,
+ pub offset: u64,
+}
- match self.0.type_ {
- TextToken => Text,
- InstructionToken => Instruction,
- OperandSeparatorToken => OperandSeparator,
- RegisterToken => Register,
- IntegerToken => Integer(self.0.value),
- PossibleAddressToken => PossibleAddress(self.0.value),
- BeginMemoryOperandToken => BeginMemoryOperand,
- EndMemoryOperandToken => EndMemoryOperand,
- FloatingPointToken => FloatingPoint,
- CodeRelativeAddressToken => CodeRelativeAddress(self.0.value),
- _ => unimplemented!("woops"),
+impl DisassemblyTextLineTypeInfo {
+ pub(crate) fn from_raw(value: &BNDisassemblyTextLineTypeInfo) -> Self {
+ Self {
+ has_type_info: value.hasTypeInfo,
+ parent_type: match value.parentType.is_null() {
+ false => Some(unsafe { Type::from_raw(value.parentType).to_owned() }),
+ true => None,
+ },
+ field_index: value.fieldIndex,
+ offset: value.offset,
}
}
- pub fn context(&self) -> InstructionTextTokenContext {
- self.0.context
+ pub(crate) fn from_owned_raw(value: BNDisassemblyTextLineTypeInfo) -> Self {
+ Self {
+ has_type_info: value.hasTypeInfo,
+ parent_type: match value.parentType.is_null() {
+ false => Some(unsafe { Type::ref_from_raw(value.parentType) }),
+ true => None,
+ },
+ field_index: value.fieldIndex,
+ offset: value.offset,
+ }
}
- pub fn size(&self) -> usize {
- self.0.size
+ pub(crate) fn into_raw(value: Self) -> BNDisassemblyTextLineTypeInfo {
+ BNDisassemblyTextLineTypeInfo {
+ hasTypeInfo: value.has_type_info,
+ parentType: value
+ .parent_type
+ .map(|t| unsafe { Ref::into_raw(t) }.handle)
+ .unwrap_or(std::ptr::null_mut()),
+ fieldIndex: value.field_index,
+ offset: value.offset,
+ }
}
- pub fn operand(&self) -> usize {
- self.0.operand
+ pub(crate) fn into_owned_raw(value: &Self) -> BNDisassemblyTextLineTypeInfo {
+ BNDisassemblyTextLineTypeInfo {
+ hasTypeInfo: value.has_type_info,
+ parentType: value
+ .parent_type
+ .as_ref()
+ .map(|t| t.handle)
+ .unwrap_or(std::ptr::null_mut()),
+ fieldIndex: value.field_index,
+ offset: value.offset,
+ }
}
- pub fn address(&self) -> u64 {
- self.0.address
+ pub(crate) fn free_raw(value: BNDisassemblyTextLineTypeInfo) {
+ if !value.parentType.is_null() {
+ let _ = unsafe { Type::ref_from_raw(value.parentType) };
+ }
}
+}
- pub fn expr_index(&self) -> usize {
- self.0.exprIndex
- }
+#[derive(Debug, Clone, PartialEq)]
+pub struct InstructionTextToken {
+ pub address: u64,
+ pub text: String,
+ pub confidence: u8,
+ pub context: InstructionTextTokenContext,
+ // TODO: Document that this is not necessary to set and that this is valid in a limited context.
+ pub expr_index: usize,
+ pub kind: InstructionTextTokenKind,
}
-impl Default for InstructionTextToken {
- fn default() -> Self {
- InstructionTextToken(BNInstructionTextToken {
- type_: InstructionTextTokenType::TextToken,
- text: ptr::null_mut(),
- value: 0,
- width: 0,
- size: 0,
- operand: 0,
- context: InstructionTextTokenContext::NoTokenContext,
- confidence: BN_FULL_CONFIDENCE,
- address: 0,
- typeNames: ptr::null_mut(),
- namesCount: 0,
- exprIndex: BN_INVALID_EXPR,
- })
+impl InstructionTextToken {
+ pub(crate) fn from_raw(value: &BNInstructionTextToken) -> Self {
+ Self {
+ address: value.address,
+ text: raw_to_string(value.text).unwrap(),
+ confidence: value.confidence,
+ context: value.context.into(),
+ expr_index: value.exprIndex,
+ kind: InstructionTextTokenKind::from_raw(value),
+ }
}
-}
-impl Clone for InstructionTextToken {
- fn clone(&self) -> Self {
- InstructionTextToken(BNInstructionTextToken {
- type_: self.0.type_,
- context: self.0.context,
- address: self.0.address,
- size: self.0.size,
- operand: self.0.operand,
- value: self.0.value,
+ pub(crate) fn into_raw(value: Self) -> BNInstructionTextToken {
+ let bn_text = BnString::new(value.text);
+ // These can be gathered from value.kind
+ let kind_value = value.kind.try_value().unwrap_or(0);
+ let operand = value.kind.try_operand().unwrap_or(0);
+ let size = value.kind.try_size().unwrap_or(0);
+ let type_names = value.kind.try_type_names().unwrap_or_default();
+ BNInstructionTextToken {
+ type_: value.kind.into(),
+ // NOTE: Expected to be freed with `InstructionTextToken::free_raw`.
+ text: BnString::into_raw(bn_text),
+ value: kind_value,
+ // TODO: Where is this even used?
width: 0,
- text: BnString::new(self.text()).into_raw(),
- confidence: 0xff,
- typeNames: ptr::null_mut(),
- namesCount: 0,
- exprIndex: self.0.exprIndex,
- })
+ size,
+ operand,
+ context: value.context.into(),
+ confidence: value.confidence,
+ address: value.address,
+ // NOTE: Expected to be freed with `InstructionTextToken::free_raw`.
+ typeNames: strings_to_string_list(&type_names),
+ namesCount: type_names.len(),
+ exprIndex: value.expr_index,
+ }
}
-}
-impl Drop for InstructionTextToken {
- fn drop(&mut self) {
- if !self.0.text.is_null() {
- let _owned = unsafe { BnString::from_raw(self.0.text) };
+ pub(crate) fn free_raw(value: BNInstructionTextToken) {
+ if !value.text.is_null() {
+ unsafe { BNFreeString(value.text) };
+ }
+ if !value.typeNames.is_null() {
+ unsafe { BNFreeStringList(value.typeNames, value.namesCount) };
}
- if !self.0.typeNames.is_null() && self.0.namesCount != 0 {
- unsafe { BNFreeStringList(self.0.typeNames, self.0.namesCount) }
+ }
+
+ pub fn new(text: impl Into<String>, kind: InstructionTextTokenKind) -> Self {
+ Self {
+ address: 0,
+ text: text.into(),
+ confidence: MAX_CONFIDENCE,
+ context: InstructionTextTokenContext::Normal,
+ expr_index: 0,
+ kind,
+ }
+ }
+
+ pub fn new_with_address(
+ address: u64,
+ text: impl Into<String>,
+ kind: InstructionTextTokenKind,
+ ) -> Self {
+ Self {
+ address,
+ text: text.into(),
+ confidence: MAX_CONFIDENCE,
+ context: InstructionTextTokenContext::Normal,
+ expr_index: 0,
+ kind,
}
}
}
+impl Display for InstructionTextToken {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ self.text.fmt(f)
+ }
+}
+
impl CoreArrayProvider for InstructionTextToken {
type Raw = BNInstructionTextToken;
type Context = ();
- type Wrapped<'a> = &'a Self;
+ type Wrapped<'a> = Self;
}
+
unsafe impl CoreArrayProviderInner for InstructionTextToken {
unsafe fn free(raw: *mut Self::Raw, count: usize, _context: &Self::Context) {
+ // SAFETY: The Array MUST have been allocated on the core side. This will `delete[] raw`.
BNFreeInstructionText(raw, count)
}
+
unsafe fn wrap_raw<'a>(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped<'a> {
- core::mem::transmute(raw)
+ Self::from_raw(raw)
}
}
impl CoreArrayProvider for Array<InstructionTextToken> {
type Raw = BNInstructionTextLine;
type Context = ();
- type Wrapped<'a> = mem::ManuallyDrop<Self>;
+ type Wrapped<'a> = std::mem::ManuallyDrop<Self>;
}
+
unsafe impl CoreArrayProviderInner for Array<InstructionTextToken> {
unsafe fn free(raw: *mut Self::Raw, count: usize, _context: &Self::Context) {
+ // SAFETY: The Array MUST have been allocated on the core side. This will `delete[] raw`.
BNFreeInstructionTextLines(raw, count)
}
+
unsafe fn wrap_raw<'a>(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped<'a> {
- mem::ManuallyDrop::new(Self::new(raw.tokens, raw.count, ()))
+ // TODO: This is insane.
+ std::mem::ManuallyDrop::new(Self::new(raw.tokens, raw.count, ()))
}
}
-#[repr(transparent)]
-pub struct DisassemblyTextLine(pub(crate) BNDisassemblyTextLine);
-
-impl DisassemblyTextLine {
- // TODO : this should probably be removed, though it doesn't actually hurt anything
- pub fn debug_print(&self) {
- println!("{}", self);
- }
-
- pub fn addr(&self) -> u64 {
- self.0.addr
- }
-
- pub fn instr_idx(&self) -> usize {
- self.0.instrIndex
- }
+#[derive(Clone, PartialEq, Debug)]
+pub enum InstructionTextTokenKind {
+ Text,
+ Instruction,
+ OperandSeparator,
+ Register,
+ Integer {
+ value: u64,
+ /// Size of the integer
+ size: Option<usize>,
+ },
+ PossibleAddress {
+ value: u64,
+ /// Size of the address
+ size: Option<usize>,
+ },
+ BeginMemoryOperand,
+ EndMemoryOperand,
+ FloatingPoint {
+ value: f64,
+ /// Size of the floating point
+ size: Option<usize>,
+ },
+ Annotation,
+ CodeRelativeAddress {
+ value: u64,
+ size: Option<usize>,
+ },
+ ArgumentName {
+ // TODO: The argument index?
+ value: u64,
+ },
+ HexDumpByteValue {
+ value: u8,
+ },
+ HexDumpSkippedByte,
+ HexDumpInvalidByte,
+ HexDumpText {
+ // TODO: Explain what this does
+ width: u64,
+ },
+ Opcode,
+ String {
+ ty: StringType,
+ },
+ CharacterConstant,
+ Keyword,
+ TypeName,
+ FieldName {
+ /// Offset to this field in the respective structure
+ offset: u64,
+ /// Stores the type names for the referenced field name.
+ ///
+ /// This is typically just the members name.
+ /// For example MyStructure.my_field will have type_names be \["my_field"\].
+ type_names: Vec<String>,
+ },
+ NameSpace,
+ NameSpaceSeparator,
+ Tag,
+ StructOffset {
+ /// Offset to this field in the respective structure
+ offset: u64,
+ // TODO: This makes no sense for struct offset, they dont have types?
+ /// Stores the type names for the referenced field name.
+ type_names: Vec<String>,
+ },
+ // TODO: Unused?
+ StructOffsetByteValue,
+ // TODO: Unused?
+ StructureHexDumpText {
+ // TODO: Explain what this does
+ width: u64,
+ },
+ GotoLabel {
+ target: u64,
+ },
+ Comment {
+ target: u64,
+ },
+ PossibleValue {
+ value: u64,
+ },
+ // TODO: This is weird, you pass the value type as the text, we should restrict this behavior and type it
+ PossibleValueType,
+ ArrayIndex {
+ index: u64,
+ },
+ Indentation,
+ UnknownMemory,
+ EnumerationMember {
+ value: u64,
+ // TODO: Document where this type id comes from
+ // TODO: Can we type this to something other than a string?
+ /// The enumerations type id
+ type_id: Option<String>,
+ },
+ /// Operations like +, -, %
+ Operation,
+ BaseStructureName,
+ BaseStructureSeparator,
+ Brace {
+ // TODO: Explain what this is
+ hash: Option<u64>,
+ },
+ CodeSymbol {
+ // TODO: Value of what?
+ value: u64,
+ // TODO: Size of what?
+ size: usize, // TODO: Operand?
+ },
+ DataSymbol {
+ // TODO: Value of what?
+ value: u64,
+ // TODO: Size of what?
+ size: usize, // TODO: Operand?
+ },
+ LocalVariable {
+ // This comes from the token.value
+ // TODO: Do we have a variable id type we can attach to this?
+ // TODO: Probably not considering this is used at multiple IL levels.
+ variable_id: u64,
+ /// NOTE: This is only valid in SSA form
+ ssa_version: usize,
+ },
+ Import {
+ // TODO: Looks to be the target address from the import.
+ target: u64,
+ },
+ AddressDisplay {
+ address: u64,
+ },
+ // TODO: BAD
+ IndirectImport {
+ /// The address of the import
+ ///
+ /// If you want the address of the import token use [`InstructionTextToken::address`] instead.
+ target: u64,
+ /// Size of the instruction this token is apart of
+ size: usize,
+ // TODO: Type this
+ source_operand: usize,
+ },
+ ExternalSymbol {
+ // TODO: Value of what?
+ value: u64,
+ },
+ StackVariable {
+ // TODO: Do we have a variable id type we can attach to this?
+ // TODO: Probably not considering this is used at multiple IL levels.
+ variable_id: u64,
+ },
+ AddressSeparator,
+ CollapsedInformation,
+ CollapseStateIndicator {
+ // TODO: Explain what this is
+ hash: Option<u64>,
+ },
+}
- pub fn count(&self) -> usize {
- self.0.count
+impl InstructionTextTokenKind {
+ pub(crate) fn from_raw(value: &BNInstructionTextToken) -> Self {
+ match value.type_ {
+ BNInstructionTextTokenType::TextToken => Self::Text,
+ BNInstructionTextTokenType::InstructionToken => Self::Instruction,
+ BNInstructionTextTokenType::OperandSeparatorToken => Self::OperandSeparator,
+ BNInstructionTextTokenType::RegisterToken => Self::Register,
+ BNInstructionTextTokenType::IntegerToken => Self::Integer {
+ value: value.value,
+ size: match value.size {
+ 0 => None,
+ size => Some(size),
+ },
+ },
+ BNInstructionTextTokenType::PossibleAddressToken => Self::PossibleAddress {
+ value: value.value,
+ size: match value.size {
+ 0 => None,
+ size => Some(size),
+ },
+ },
+ BNInstructionTextTokenType::BeginMemoryOperandToken => Self::BeginMemoryOperand,
+ BNInstructionTextTokenType::EndMemoryOperandToken => Self::EndMemoryOperand,
+ BNInstructionTextTokenType::FloatingPointToken => Self::FloatingPoint {
+ value: value.value as f64,
+ size: match value.size {
+ 0 => None,
+ size => Some(size),
+ },
+ },
+ BNInstructionTextTokenType::AnnotationToken => Self::Annotation,
+ BNInstructionTextTokenType::CodeRelativeAddressToken => Self::CodeRelativeAddress {
+ value: value.value,
+ size: match value.size {
+ 0 => None,
+ size => Some(size),
+ },
+ },
+ BNInstructionTextTokenType::ArgumentNameToken => {
+ Self::ArgumentName { value: value.value }
+ }
+ BNInstructionTextTokenType::HexDumpByteValueToken => Self::HexDumpByteValue {
+ value: value.value as u8,
+ },
+ BNInstructionTextTokenType::HexDumpSkippedByteToken => Self::HexDumpSkippedByte,
+ BNInstructionTextTokenType::HexDumpInvalidByteToken => Self::HexDumpInvalidByte,
+ BNInstructionTextTokenType::HexDumpTextToken => {
+ Self::HexDumpText { width: value.value }
+ }
+ BNInstructionTextTokenType::OpcodeToken => Self::Opcode,
+ BNInstructionTextTokenType::StringToken => Self::String {
+ ty: match value.value {
+ 0 => StringType::AsciiString,
+ 1 => StringType::Utf8String,
+ 2 => StringType::Utf16String,
+ 3 => StringType::Utf32String,
+ _ => unreachable!(),
+ },
+ },
+ BNInstructionTextTokenType::CharacterConstantToken => Self::CharacterConstant,
+ BNInstructionTextTokenType::KeywordToken => Self::Keyword,
+ BNInstructionTextTokenType::TypeNameToken => Self::TypeName,
+ BNInstructionTextTokenType::FieldNameToken => Self::FieldName {
+ offset: value.value,
+ type_names: {
+ // NOTE: Do not need to free, this is a part of the From<&> impl
+ let raw_names =
+ unsafe { std::slice::from_raw_parts(value.typeNames, value.namesCount) };
+ raw_names.iter().filter_map(|&r| raw_to_string(r)).collect()
+ },
+ },
+ BNInstructionTextTokenType::NameSpaceToken => Self::NameSpace,
+ BNInstructionTextTokenType::NameSpaceSeparatorToken => Self::NameSpaceSeparator,
+ BNInstructionTextTokenType::TagToken => Self::Tag,
+ BNInstructionTextTokenType::StructOffsetToken => Self::StructOffset {
+ offset: value.value,
+ type_names: {
+ // NOTE: Do not need to free, this is a part of the From<&> impl
+ let raw_names =
+ unsafe { std::slice::from_raw_parts(value.typeNames, value.namesCount) };
+ raw_names.iter().filter_map(|&r| raw_to_string(r)).collect()
+ },
+ },
+ BNInstructionTextTokenType::StructOffsetByteValueToken => Self::StructOffsetByteValue,
+ BNInstructionTextTokenType::StructureHexDumpTextToken => {
+ Self::StructureHexDumpText { width: value.value }
+ }
+ BNInstructionTextTokenType::GotoLabelToken => Self::GotoLabel {
+ target: value.value,
+ },
+ BNInstructionTextTokenType::CommentToken => Self::Comment {
+ target: value.value,
+ },
+ BNInstructionTextTokenType::PossibleValueToken => {
+ Self::PossibleValue { value: value.value }
+ }
+ // NOTE: See my comment about this type in [`Self::PossibleValueType`]
+ BNInstructionTextTokenType::PossibleValueTypeToken => Self::PossibleValueType,
+ BNInstructionTextTokenType::ArrayIndexToken => Self::ArrayIndex { index: value.value },
+ BNInstructionTextTokenType::IndentationToken => Self::Indentation,
+ BNInstructionTextTokenType::UnknownMemoryToken => Self::UnknownMemory,
+ BNInstructionTextTokenType::EnumerationMemberToken => Self::EnumerationMember {
+ value: value.value,
+ type_id: {
+ // NOTE: Type id comes from value.typeNames, it should be the first one (hence the .next)
+ // NOTE: Do not need to free, this is a part of the From<&> impl
+ let raw_names =
+ unsafe { std::slice::from_raw_parts(value.typeNames, value.namesCount) };
+ raw_names.iter().filter_map(|&r| raw_to_string(r)).next()
+ },
+ },
+ BNInstructionTextTokenType::OperationToken => Self::Operation,
+ BNInstructionTextTokenType::BaseStructureNameToken => Self::BaseStructureName,
+ BNInstructionTextTokenType::BaseStructureSeparatorToken => Self::BaseStructureSeparator,
+ BNInstructionTextTokenType::BraceToken => Self::Brace {
+ hash: match value.value {
+ 0 => None,
+ hash => Some(hash),
+ },
+ },
+ BNInstructionTextTokenType::CodeSymbolToken => Self::CodeSymbol {
+ value: value.value,
+ size: value.size,
+ },
+ BNInstructionTextTokenType::DataSymbolToken => Self::DataSymbol {
+ value: value.value,
+ size: value.size,
+ },
+ BNInstructionTextTokenType::LocalVariableToken => Self::LocalVariable {
+ variable_id: value.value,
+ ssa_version: value.operand,
+ },
+ BNInstructionTextTokenType::ImportToken => Self::Import {
+ target: value.value,
+ },
+ BNInstructionTextTokenType::AddressDisplayToken => Self::AddressDisplay {
+ address: value.value,
+ },
+ BNInstructionTextTokenType::IndirectImportToken => Self::IndirectImport {
+ target: value.value,
+ size: value.size,
+ source_operand: value.operand,
+ },
+ BNInstructionTextTokenType::ExternalSymbolToken => {
+ Self::ExternalSymbol { value: value.value }
+ }
+ BNInstructionTextTokenType::StackVariableToken => Self::StackVariable {
+ variable_id: value.value,
+ },
+ BNInstructionTextTokenType::AddressSeparatorToken => Self::AddressSeparator,
+ BNInstructionTextTokenType::CollapsedInformationToken => Self::CollapsedInformation,
+ BNInstructionTextTokenType::CollapseStateIndicatorToken => {
+ Self::CollapseStateIndicator {
+ hash: match value.value {
+ 0 => None,
+ hash => Some(hash),
+ },
+ }
+ }
+ }
}
- pub fn tag_count(&self) -> usize {
- self.0.tagCount
+ /// Mapping to the [`BNInstructionTextTokenType::value`] field.
+ fn try_value(&self) -> Option<u64> {
+ // TODO: Double check to make sure these are correct.
+ match self {
+ InstructionTextTokenKind::Integer { value, .. } => Some(*value),
+ InstructionTextTokenKind::PossibleAddress { value, .. } => Some(*value),
+ InstructionTextTokenKind::PossibleValue { value, .. } => Some(*value),
+ InstructionTextTokenKind::FloatingPoint { value, .. } => Some(*value as u64),
+ InstructionTextTokenKind::CodeRelativeAddress { value, .. } => Some(*value),
+ InstructionTextTokenKind::ArgumentName { value, .. } => Some(*value),
+ InstructionTextTokenKind::HexDumpByteValue { value, .. } => Some(*value as u64),
+ InstructionTextTokenKind::HexDumpText { width, .. } => Some(*width),
+ InstructionTextTokenKind::String { ty, .. } => Some(*ty as u64),
+ InstructionTextTokenKind::FieldName { offset, .. } => Some(*offset),
+ InstructionTextTokenKind::StructOffset { offset, .. } => Some(*offset),
+ InstructionTextTokenKind::StructureHexDumpText { width, .. } => Some(*width),
+ InstructionTextTokenKind::GotoLabel { target, .. } => Some(*target),
+ InstructionTextTokenKind::Comment { target, .. } => Some(*target),
+ InstructionTextTokenKind::ArrayIndex { index, .. } => Some(*index),
+ InstructionTextTokenKind::EnumerationMember { value, .. } => Some(*value),
+ InstructionTextTokenKind::LocalVariable { variable_id, .. } => Some(*variable_id),
+ InstructionTextTokenKind::Import { target, .. } => Some(*target),
+ InstructionTextTokenKind::AddressDisplay { address, .. } => Some(*address),
+ InstructionTextTokenKind::IndirectImport { target, .. } => Some(*target),
+ InstructionTextTokenKind::Brace { hash, .. } => *hash,
+ InstructionTextTokenKind::CodeSymbol { value, .. } => Some(*value),
+ InstructionTextTokenKind::DataSymbol { value, .. } => Some(*value),
+ InstructionTextTokenKind::ExternalSymbol { value, .. } => Some(*value),
+ InstructionTextTokenKind::StackVariable { variable_id, .. } => Some(*variable_id),
+ InstructionTextTokenKind::CollapseStateIndicator { hash, .. } => *hash,
+ _ => None,
+ }
}
- pub fn tokens(&self) -> Vec<InstructionTextToken> {
- unsafe {
- std::slice::from_raw_parts::<BNInstructionTextToken>(self.0.tokens, self.0.count)
- .iter()
- .map(|x| InstructionTextToken::from_raw(x).clone())
- .collect()
+ /// Mapping to the [`BNInstructionTextTokenType::size`] field.
+ fn try_size(&self) -> Option<usize> {
+ match self {
+ InstructionTextTokenKind::Integer { size, .. } => *size,
+ InstructionTextTokenKind::FloatingPoint { size, .. } => *size,
+ InstructionTextTokenKind::PossibleAddress { size, .. } => *size,
+ InstructionTextTokenKind::CodeRelativeAddress { size, .. } => *size,
+ InstructionTextTokenKind::CodeSymbol { size, .. } => Some(*size),
+ InstructionTextTokenKind::DataSymbol { size, .. } => Some(*size),
+ InstructionTextTokenKind::IndirectImport { size, .. } => Some(*size),
+ _ => None,
}
}
-}
-impl std::fmt::Display for DisassemblyTextLine {
- fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
- for token in self.tokens() {
- write!(f, "{}", token.text())?;
+ /// Mapping to the [`BNInstructionTextTokenType::operand`] field.
+ fn try_operand(&self) -> Option<usize> {
+ match self {
+ InstructionTextTokenKind::LocalVariable { ssa_version, .. } => Some(*ssa_version),
+ InstructionTextTokenKind::IndirectImport { source_operand, .. } => {
+ Some(*source_operand)
+ }
+ _ => None,
}
-
- Ok(())
}
-}
-impl From<Vec<InstructionTextToken>> for DisassemblyTextLine {
- fn from(tokens: Vec<InstructionTextToken>) -> Self {
- let mut tokens: Box<[_]> = tokens.into();
-
- // TODO: let (tokens_pointer, tokens_len, _) = unsafe { tokens.into_raw_parts() }; // Can't use for now...still a rust nightly feature
- let tokens_pointer = tokens.as_mut_ptr();
- let tokens_len = tokens.len();
- mem::forget(tokens);
-
- DisassemblyTextLine(BNDisassemblyTextLine {
- addr: 0,
- instrIndex: BN_INVALID_EXPR,
- tokens: tokens_pointer as *mut _,
- count: tokens_len,
- highlight: BNHighlightColor {
- style: BNHighlightColorStyle::StandardHighlightColor,
- color: BNHighlightStandardColor::NoHighlightColor,
- mixColor: BNHighlightStandardColor::NoHighlightColor,
- mix: 0,
- r: 0,
- g: 0,
- b: 0,
- alpha: 0,
- },
- tags: ptr::null_mut(),
- tagCount: 0,
- typeInfo: BNDisassemblyTextLineTypeInfo {
- hasTypeInfo: false,
- parentType: ptr::null_mut(),
- fieldIndex: usize::MAX,
- offset: 0,
- },
- })
+ /// Mapping to the [`BNInstructionTextTokenType::typeNames`] field.
+ fn try_type_names(&self) -> Option<Vec<String>> {
+ match self {
+ InstructionTextTokenKind::FieldName { type_names, .. } => Some(type_names.clone()),
+ InstructionTextTokenKind::StructOffset { type_names, .. } => Some(type_names.clone()),
+ InstructionTextTokenKind::EnumerationMember { type_id, .. } => {
+ Some(vec![type_id.clone()?])
+ }
+ _ => None,
+ }
}
}
-impl From<&Vec<&str>> for DisassemblyTextLine {
- fn from(string_tokens: &Vec<&str>) -> Self {
- let mut tokens: Box<[BNInstructionTextToken]> = string_tokens
- .iter()
- .map(|&token| InstructionTextToken::new(token, InstructionTextTokenContents::Text).into_raw())
- .collect();
-
- // let (tokens_pointer, tokens_len, _) = unsafe { tokens.into_raw_parts() }; // Can't use for now...still a rust nighly feature
- let tokens_pointer = tokens.as_mut_ptr();
- let tokens_len = tokens.len();
- mem::forget(tokens);
-
- DisassemblyTextLine(BNDisassemblyTextLine {
- addr: 0,
- instrIndex: BN_INVALID_EXPR,
- tokens: tokens_pointer as *mut _,
- count: tokens_len,
- highlight: BNHighlightColor {
- style: BNHighlightColorStyle::StandardHighlightColor,
- color: BNHighlightStandardColor::NoHighlightColor,
- mixColor: BNHighlightStandardColor::NoHighlightColor,
- mix: 0,
- r: 0,
- g: 0,
- b: 0,
- alpha: 0,
- },
- tags: ptr::null_mut(),
- tagCount: 0,
- typeInfo: BNDisassemblyTextLineTypeInfo {
- hasTypeInfo: false,
- parentType: ptr::null_mut(),
- fieldIndex: usize::MAX,
- offset: 0,
- },
- })
+impl From<InstructionTextTokenKind> for BNInstructionTextTokenType {
+ fn from(value: InstructionTextTokenKind) -> Self {
+ match value {
+ InstructionTextTokenKind::Text => BNInstructionTextTokenType::TextToken,
+ InstructionTextTokenKind::Instruction => BNInstructionTextTokenType::InstructionToken,
+ InstructionTextTokenKind::OperandSeparator => {
+ BNInstructionTextTokenType::OperandSeparatorToken
+ }
+ InstructionTextTokenKind::Register => BNInstructionTextTokenType::RegisterToken,
+ InstructionTextTokenKind::Integer { .. } => BNInstructionTextTokenType::IntegerToken,
+ InstructionTextTokenKind::PossibleAddress { .. } => {
+ BNInstructionTextTokenType::PossibleAddressToken
+ }
+ InstructionTextTokenKind::BeginMemoryOperand => {
+ BNInstructionTextTokenType::BeginMemoryOperandToken
+ }
+ InstructionTextTokenKind::EndMemoryOperand => {
+ BNInstructionTextTokenType::EndMemoryOperandToken
+ }
+ InstructionTextTokenKind::FloatingPoint { .. } => {
+ BNInstructionTextTokenType::FloatingPointToken
+ }
+ InstructionTextTokenKind::Annotation => BNInstructionTextTokenType::AnnotationToken,
+ InstructionTextTokenKind::CodeRelativeAddress { .. } => {
+ BNInstructionTextTokenType::CodeRelativeAddressToken
+ }
+ InstructionTextTokenKind::ArgumentName { .. } => {
+ BNInstructionTextTokenType::ArgumentNameToken
+ }
+ InstructionTextTokenKind::HexDumpByteValue { .. } => {
+ BNInstructionTextTokenType::HexDumpByteValueToken
+ }
+ InstructionTextTokenKind::HexDumpSkippedByte => {
+ BNInstructionTextTokenType::HexDumpSkippedByteToken
+ }
+ InstructionTextTokenKind::HexDumpInvalidByte => {
+ BNInstructionTextTokenType::HexDumpInvalidByteToken
+ }
+ InstructionTextTokenKind::HexDumpText { .. } => {
+ BNInstructionTextTokenType::HexDumpTextToken
+ }
+ InstructionTextTokenKind::Opcode => BNInstructionTextTokenType::OpcodeToken,
+ InstructionTextTokenKind::String { .. } => BNInstructionTextTokenType::StringToken,
+ InstructionTextTokenKind::CharacterConstant => {
+ BNInstructionTextTokenType::CharacterConstantToken
+ }
+ InstructionTextTokenKind::Keyword => BNInstructionTextTokenType::KeywordToken,
+ InstructionTextTokenKind::TypeName => BNInstructionTextTokenType::TypeNameToken,
+ InstructionTextTokenKind::FieldName { .. } => {
+ BNInstructionTextTokenType::FieldNameToken
+ }
+ InstructionTextTokenKind::NameSpace => BNInstructionTextTokenType::NameSpaceToken,
+ InstructionTextTokenKind::NameSpaceSeparator => {
+ BNInstructionTextTokenType::NameSpaceSeparatorToken
+ }
+ InstructionTextTokenKind::Tag => BNInstructionTextTokenType::TagToken,
+ InstructionTextTokenKind::StructOffset { .. } => {
+ BNInstructionTextTokenType::StructOffsetToken
+ }
+ InstructionTextTokenKind::StructOffsetByteValue => {
+ BNInstructionTextTokenType::StructOffsetByteValueToken
+ }
+ InstructionTextTokenKind::StructureHexDumpText { .. } => {
+ BNInstructionTextTokenType::StructureHexDumpTextToken
+ }
+ InstructionTextTokenKind::GotoLabel { .. } => {
+ BNInstructionTextTokenType::GotoLabelToken
+ }
+ InstructionTextTokenKind::Comment { .. } => BNInstructionTextTokenType::CommentToken,
+ InstructionTextTokenKind::PossibleValue { .. } => {
+ BNInstructionTextTokenType::PossibleValueToken
+ }
+ InstructionTextTokenKind::PossibleValueType => {
+ BNInstructionTextTokenType::PossibleValueTypeToken
+ }
+ InstructionTextTokenKind::ArrayIndex { .. } => {
+ BNInstructionTextTokenType::ArrayIndexToken
+ }
+ InstructionTextTokenKind::Indentation => BNInstructionTextTokenType::IndentationToken,
+ InstructionTextTokenKind::UnknownMemory => {
+ BNInstructionTextTokenType::UnknownMemoryToken
+ }
+ InstructionTextTokenKind::EnumerationMember { .. } => {
+ BNInstructionTextTokenType::EnumerationMemberToken
+ }
+ InstructionTextTokenKind::Operation => BNInstructionTextTokenType::OperationToken,
+ InstructionTextTokenKind::BaseStructureName => {
+ BNInstructionTextTokenType::BaseStructureNameToken
+ }
+ InstructionTextTokenKind::BaseStructureSeparator => {
+ BNInstructionTextTokenType::BaseStructureSeparatorToken
+ }
+ InstructionTextTokenKind::Brace { .. } => BNInstructionTextTokenType::BraceToken,
+ InstructionTextTokenKind::CodeSymbol { .. } => {
+ BNInstructionTextTokenType::CodeSymbolToken
+ }
+ InstructionTextTokenKind::DataSymbol { .. } => {
+ BNInstructionTextTokenType::DataSymbolToken
+ }
+ InstructionTextTokenKind::LocalVariable { .. } => {
+ BNInstructionTextTokenType::LocalVariableToken
+ }
+ InstructionTextTokenKind::Import { .. } => BNInstructionTextTokenType::ImportToken,
+ InstructionTextTokenKind::AddressDisplay { .. } => {
+ BNInstructionTextTokenType::AddressDisplayToken
+ }
+ InstructionTextTokenKind::IndirectImport { .. } => {
+ BNInstructionTextTokenType::IndirectImportToken
+ }
+ InstructionTextTokenKind::ExternalSymbol { .. } => {
+ BNInstructionTextTokenType::ExternalSymbolToken
+ }
+ InstructionTextTokenKind::StackVariable { .. } => {
+ BNInstructionTextTokenType::StackVariableToken
+ }
+ InstructionTextTokenKind::AddressSeparator => {
+ BNInstructionTextTokenType::AddressSeparatorToken
+ }
+ InstructionTextTokenKind::CollapsedInformation => {
+ BNInstructionTextTokenType::CollapsedInformationToken
+ }
+ InstructionTextTokenKind::CollapseStateIndicator { .. } => {
+ BNInstructionTextTokenType::CollapseStateIndicatorToken
+ }
+ }
}
}
-impl Default for DisassemblyTextLine {
- fn default() -> Self {
- DisassemblyTextLine(BNDisassemblyTextLine {
- addr: 0,
- instrIndex: BN_INVALID_EXPR,
- tokens: ptr::null_mut(),
- count: 0,
- highlight: BNHighlightColor {
- style: BNHighlightColorStyle::StandardHighlightColor,
- color: BNHighlightStandardColor::NoHighlightColor,
- mixColor: BNHighlightStandardColor::NoHighlightColor,
- mix: 0,
- r: 0,
- g: 0,
- b: 0,
- alpha: 0,
- },
- tags: ptr::null_mut(),
- tagCount: 0,
- typeInfo: BNDisassemblyTextLineTypeInfo {
- hasTypeInfo: false,
- parentType: ptr::null_mut(),
- fieldIndex: usize::MAX,
- offset: 0,
- },
- })
- }
+#[derive(Clone, Copy, PartialEq, Eq, Debug)]
+pub enum InstructionTextTokenContext {
+ Normal,
+ LocalVariable,
+ DataVariable,
+ FunctionReturn,
+ InstructionAddress,
+ ILInstructionIndex,
+ ConstData,
+ /// Use only with [`InstructionTextTokenKind::String`]
+ ConstStringData,
+ /// Use only with [`InstructionTextTokenKind::String`]
+ StringReference,
+ /// Use only with [`InstructionTextTokenKind::String`]
+ StringDataVariable,
+ /// For displaying strings which aren't associated with an address
+ ///
+ /// Use only with [`InstructionTextTokenKind::String`]
+ StringDisplay,
+ /// Use only with [`InstructionTextTokenKind::CollapseStateIndicator`]
+ Collapsed,
+ /// Use only with [`InstructionTextTokenKind::CollapseStateIndicator`]
+ Expanded,
+ /// Use only with [`InstructionTextTokenKind::CollapseStateIndicator`]
+ CollapsiblePadding,
}
-impl Drop for DisassemblyTextLine {
- fn drop(&mut self) {
- if !self.0.tokens.is_null() {
- let ptr = core::ptr::slice_from_raw_parts_mut(self.0.tokens, self.0.count);
- let _ = unsafe { Box::from_raw(ptr) };
+impl From<BNInstructionTextTokenContext> for InstructionTextTokenContext {
+ fn from(value: BNInstructionTextTokenContext) -> Self {
+ match value {
+ BNInstructionTextTokenContext::NoTokenContext => Self::Normal,
+ BNInstructionTextTokenContext::LocalVariableTokenContext => Self::LocalVariable,
+ BNInstructionTextTokenContext::DataVariableTokenContext => Self::DataVariable,
+ BNInstructionTextTokenContext::FunctionReturnTokenContext => Self::FunctionReturn,
+ BNInstructionTextTokenContext::InstructionAddressTokenContext => {
+ Self::InstructionAddress
+ }
+ BNInstructionTextTokenContext::ILInstructionIndexTokenContext => {
+ Self::ILInstructionIndex
+ }
+ BNInstructionTextTokenContext::ConstDataTokenContext => Self::ConstData,
+ // For use with [`InstructionTextTokenKind::String`]
+ BNInstructionTextTokenContext::ConstStringDataTokenContext => Self::ConstStringData,
+ BNInstructionTextTokenContext::StringReferenceTokenContext => Self::StringReference,
+ BNInstructionTextTokenContext::StringDataVariableTokenContext => {
+ Self::StringDataVariable
+ }
+ BNInstructionTextTokenContext::StringDisplayTokenContext => Self::StringDisplay,
+ // For use with [`InstructionTextTokenKind::CollapseStateIndicator`]
+ BNInstructionTextTokenContext::ContentCollapsedContext => Self::Collapsed,
+ BNInstructionTextTokenContext::ContentExpandedContext => Self::Expanded,
+ BNInstructionTextTokenContext::ContentCollapsiblePadding => Self::CollapsiblePadding,
}
}
}
-impl CoreArrayProvider for DisassemblyTextLine {
- type Raw = BNDisassemblyTextLine;
- type Context = ();
- type Wrapped<'a> = &'a Self;
-}
-
-unsafe impl CoreArrayProviderInner for DisassemblyTextLine {
- unsafe fn free(raw: *mut Self::Raw, count: usize, _context: &Self::Context) {
- BNFreeDisassemblyTextLines(raw, count)
- }
- unsafe fn wrap_raw<'a>(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped<'a> {
- core::mem::transmute(raw)
+impl From<InstructionTextTokenContext> for BNInstructionTextTokenContext {
+ fn from(value: InstructionTextTokenContext) -> Self {
+ match value {
+ InstructionTextTokenContext::Normal => Self::NoTokenContext,
+ InstructionTextTokenContext::LocalVariable => Self::LocalVariableTokenContext,
+ InstructionTextTokenContext::DataVariable => Self::DataVariableTokenContext,
+ InstructionTextTokenContext::FunctionReturn => Self::FunctionReturnTokenContext,
+ InstructionTextTokenContext::InstructionAddress => Self::InstructionAddressTokenContext,
+ InstructionTextTokenContext::ILInstructionIndex => Self::ILInstructionIndexTokenContext,
+ InstructionTextTokenContext::ConstData => Self::ConstDataTokenContext,
+ InstructionTextTokenContext::ConstStringData => Self::ConstStringDataTokenContext,
+ InstructionTextTokenContext::StringReference => Self::StringReferenceTokenContext,
+ InstructionTextTokenContext::StringDataVariable => Self::StringDataVariableTokenContext,
+ InstructionTextTokenContext::StringDisplay => Self::StringDisplayTokenContext,
+ InstructionTextTokenContext::Collapsed => Self::ContentCollapsedContext,
+ InstructionTextTokenContext::Expanded => Self::ContentExpandedContext,
+ InstructionTextTokenContext::CollapsiblePadding => Self::ContentCollapsiblePadding,
+ }
}
}
-pub type DisassemblyOption = BNDisassemblyOption;
-
+// TODO: Make a builder for this.
#[derive(PartialEq, Eq, Hash)]
pub struct DisassemblySettings {
pub(crate) handle: *mut BNDisassemblySettings,
@@ -481,9 +969,7 @@ impl DisassemblySettings {
pub fn new() -> Ref<Self> {
unsafe {
let handle = BNCreateDisassemblySettings();
-
debug_assert!(!handle.is_null());
-
Ref::new(Self { handle })
}
}