Skip to content

Commit

Permalink
Fix Rust InstructionTextTokenKind not consulting the string token con…
Browse files Browse the repository at this point in the history
…text

This caused a crash if we visited a builtin with a "fake" string. Where the token value is not actually the string type.
  • Loading branch information
emesare committed Feb 11, 2025
1 parent 2b0afb5 commit 5fd6a9e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 10 deletions.
2 changes: 1 addition & 1 deletion plugins/dwarf/dwarfdump/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ fn get_info_string<R: Reader>(
if let Ok(attr_string) = attr_reader.to_string() {
attr_line.push(InstructionTextToken::new(
attr_string.as_ref(),
InstructionTextTokenKind::String {
InstructionTextTokenKind::StringContent {
ty: StringType::Utf8String,
},
));
Expand Down
43 changes: 34 additions & 9 deletions rust/src/disassembly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,14 @@ pub enum InstructionTextTokenKind {
},
Opcode,
String {
// TODO: What is this?
// TODO: It seems like people just throw things in here...
value: u64,
},
/// String content is only present for:
/// - [`InstructionTextTokenContext::StringReference`]
/// - [`InstructionTextTokenContext::StringDisplay`]
StringContent {
ty: StringType,
},
CharacterConstant,
Expand Down Expand Up @@ -588,14 +596,29 @@ impl InstructionTextTokenKind {
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::StringToken => match value.context {
BNInstructionTextTokenContext::StringReferenceTokenContext
| BNInstructionTextTokenContext::StringDisplayTokenContext => {
match value.value {
0 => Self::StringContent {
ty: StringType::AsciiString,
},
1 => Self::StringContent {
ty: StringType::Utf8String,
},
2 => Self::StringContent {
ty: StringType::Utf16String,
},
3 => Self::StringContent {
ty: StringType::Utf32String,
},
// If we reach here all hope is lost.
// Reaching here means someone made a ref or display context token with no
// StringType and instead some other random value...
value => Self::String { value },
}
}
_ => Self::String { value: value.value },
},
BNInstructionTextTokenType::CharacterConstantToken => Self::CharacterConstant,
BNInstructionTextTokenType::KeywordToken => Self::Keyword,
Expand Down Expand Up @@ -712,7 +735,8 @@ impl InstructionTextTokenKind {
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::String { value, .. } => Some(*value),
InstructionTextTokenKind::StringContent { ty, .. } => Some(*ty as u64),
InstructionTextTokenKind::FieldName { offset, .. } => Some(*offset),
InstructionTextTokenKind::StructOffset { offset, .. } => Some(*offset),
InstructionTextTokenKind::StructureHexDumpText { width, .. } => Some(*width),
Expand Down Expand Up @@ -815,6 +839,7 @@ impl From<InstructionTextTokenKind> for BNInstructionTextTokenType {
}
InstructionTextTokenKind::Opcode => BNInstructionTextTokenType::OpcodeToken,
InstructionTextTokenKind::String { .. } => BNInstructionTextTokenType::StringToken,
InstructionTextTokenKind::StringContent { .. } => BNInstructionTextTokenType::StringToken,
InstructionTextTokenKind::CharacterConstant => {
BNInstructionTextTokenType::CharacterConstantToken
}
Expand Down

0 comments on commit 5fd6a9e

Please sign in to comment.