disassembler error handling
This commit is contained in:
@@ -88,13 +88,14 @@ pub const BundledInstruction = struct {
|
|||||||
};
|
};
|
||||||
|
|
||||||
/// Disassemble `bytes` and format them into the given buffer. Useful for error reporting or
|
/// Disassemble `bytes` and format them into the given buffer. Useful for error reporting or
|
||||||
/// debugging purposes.
|
/// debugging purposes. On error return an empty string.
|
||||||
/// This function is not threadsafe.
|
/// This function is not threadsafe.
|
||||||
pub fn formatBytes(bytes: []const u8) []u8 {
|
pub fn formatBytes(bytes: []const u8) []u8 {
|
||||||
return formatInstruction(disassembleInstruction(bytes));
|
const instr = disassembleInstruction(bytes) orelse return "";
|
||||||
|
return formatInstruction(instr);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Format the given instruction into the buffer.
|
/// Format the given instruction into the buffer. On error return an empty string.
|
||||||
/// This function is not threadsafe.
|
/// This function is not threadsafe.
|
||||||
pub fn formatInstruction(instruction: BundledInstruction) []u8 {
|
pub fn formatInstruction(instruction: BundledInstruction) []u8 {
|
||||||
// Static variable to initialize the formatter only once and have a valid address for the
|
// Static variable to initialize the formatter only once and have a valid address for the
|
||||||
@@ -118,13 +119,16 @@ pub fn formatInstruction(instruction: BundledInstruction) []u8 {
|
|||||||
instruction.address,
|
instruction.address,
|
||||||
null,
|
null,
|
||||||
);
|
);
|
||||||
assert(zydis.ZYAN_SUCCESS(status)); // TODO: handle
|
if (zydis.ZYAN_SUCCESS(status)) {
|
||||||
return mem.sliceTo(&static.buffer, 0);
|
return mem.sliceTo(&static.buffer, 0);
|
||||||
|
} else {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Disassemble the first instruction at bytes.
|
/// Disassemble the first instruction at bytes. On error return `null`.
|
||||||
/// This function is not threadsafe.
|
/// This function is not threadsafe.
|
||||||
pub fn disassembleInstruction(bytes: []const u8) BundledInstruction {
|
pub fn disassembleInstruction(bytes: []const u8) ?BundledInstruction {
|
||||||
// Static variable to initialize the decoder only once and have a valid address for the
|
// Static variable to initialize the decoder only once and have a valid address for the
|
||||||
// instruction and operands.
|
// instruction and operands.
|
||||||
const static = struct {
|
const static = struct {
|
||||||
@@ -149,10 +153,13 @@ pub fn disassembleInstruction(bytes: []const u8) BundledInstruction {
|
|||||||
&static.instruction,
|
&static.instruction,
|
||||||
&static.operands,
|
&static.operands,
|
||||||
);
|
);
|
||||||
assert(zydis.ZYAN_SUCCESS(status)); // TODO: handle
|
if (zydis.ZYAN_SUCCESS(status)) {
|
||||||
return .{
|
return .{
|
||||||
.address = @intFromPtr(bytes.ptr),
|
.address = @intFromPtr(bytes.ptr),
|
||||||
.instruction = &static.instruction,
|
.instruction = &static.instruction,
|
||||||
.operands = &static.operands,
|
.operands = &static.operands,
|
||||||
};
|
};
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user