详细解释代码: 这是xeddisassembler.cc #include "./instruction/xeddisassembler.h" #include…
- User
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
详细解释代码: 这是xed_disassembler.cc #include "./instruction/xed_disassembler.h"
#include <stdint.h>
#include #include
#include "absl/strings/ascii.h" #include "./instruction/disassembler.h" #include "./instruction/xed_util.h" #include "./util/checks.h"
extern "C" { #include "third_party/libxed/xed-category-enum.h" #include "third_party/libxed/xed-decoded-inst-api.h" #include "third_party/libxed/xed-iclass-enum.h" }
namespace silifuzz {
XedDisassembler::XedDisassembler() : valid_(false) { InitXedIfNeeded(); }
XedDisassembler::~XedDisassembler() {}
bool XedDisassembler::Disassemble(uint64_t address, const uint8_t* buffer, size_t buffer_size) { address_ = address;
xed_decoded_inst_zero(&xedd_); xed_decoded_inst_set_mode(&xedd_, XED_MACHINE_MODE_LONG_64, XED_ADDRESS_WIDTH_64b); valid_ = xed_decode(&xedd_, buffer, buffer_size) == XED_ERROR_NONE; return valid_; }
size_t XedDisassembler::InstructionSize() const { return valid_ ? xed_decoded_inst_get_length(&xedd_) : 0; }
bool XedDisassembler::CanBranch() const { if (!valid_) return false; return InstructionIsBranch(xed_decoded_inst_inst(&xedd_)); }
bool XedDisassembler::CanLoad() const { if (!valid_) return false;
// Note: XED makes implicit memory operands for call/push/pop/etc explicit // through this API. const unsigned int num_mem = xed_decoded_inst_number_of_memory_operands(&xedd_); for (int i = 0; i < num_mem; ++i) { if (xed_decoded_inst_mem_read(&xedd_, i)) { return true; } } return false; }
bool XedDisassembler::CanStore() const { if (!valid_) return false;
// Note: XED makes implicit memory operands for call/push/pop/etc explicit // through this API. const unsigned int num_mem = xed_decoded_inst_number_of_memory_operands(&xedd_); for (int i = 0; i < num_mem; ++i) { if (xed_decoded_inst_mem_written(&xedd_, i)) { return true; } } return false; }
std::string XedDisassembler::FullText() { if (valid_) { CHECK(FormatInstruction(xedd_, address_, full_text_, sizeof(full_text_))); return full_text_; } else { return kInvalidInstructionName; } }
uint32_t XedDisassembler::InstructionID() const { return valid_ ? xed_decoded_inst_get_iclass(&xedd_) : InvalidInstructionID(); }
uint32_t XedDisassembler::InvalidInstructionID() const { return XED_ICLASS_INVALID; }
uint32_t XedDisassembler::NumInstructionIDs() const { return XED_ICLASS_LAST; }
std::string XedDisassembler::InstructionIDName(uint32_t id) const { if (id == InvalidInstructionID()) { return kInvalidInstructionName; } std::string name = xed_iclass_enum_t2str(static_cast<xed_iclass_enum_t>(id)); absl::AsciiStrToLower(&name); return name; }
} // namespace silifuzz 下面是xed_disassembler.h // Copyright 2023 The SiliFuzz Authors. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // https://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // 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.
#ifndef THIRD_PARTY_SILIFUZZ_INSTRUCTION_XED_DISASSEMBLER_H_ #define THIRD_PARTY_SILIFUZZ_INSTRUCTION_XED_DISASSEMBLER_H_
#include #include #include
#include "./instruction/disassembler.h" #include "./util/arch.h"
extern "C" { #include "third_party/libxed/xed-interface.h" }
namespace silifuzz {
// This class wraps XED for use by instruction tracers. // This class is not thread safe since a XED instance is not thread safe. class XedDisassembler : public Disassembler { public: XedDisassembler(); ~XedDisassembler();
// Non-copyable / non-moveable. XedDisassembler(const XedDisassembler&) = delete; XedDisassembler(XedDisassembler&&) = delete; XedDisassembler& operator=(const XedDisassembler&) = delete; XedDisassembler& operator=(XedDisassembler&&) = delete;
// Disassmeble a single instruction.
// address is the address of the instruction being disassembled. The address
// is required to print absolute jump targets.
// buffer points to memory that contains the complete instruction. It is OK
// if it contains additional data.
// buffer_size specifies the amount of data available in buffer. Only part
// of that data may be consumed.
// Returns the true if the instruction is valid.
bool Disassemble(uint64_t address, const uint8_t* buffer,
size_t buffer_size) override;
// How much data was consumed by the last call to Disassemble. [[nodiscard]] size_t InstructionSize() const override;
[[nodiscard]] virtual bool CanBranch() const override; [[nodiscard]] virtual bool CanLoad() const override; [[nodiscard]] virtual bool CanStore() const override;
// The textual representation of the last instruction that was disassembled. [[nodiscard]] std::string FullText() override;
// A numerical ID for the last type of instruction that was disassembled. [[nodiscard]] uint32_t InstructionID() const override;
[[nodiscard]] uint32_t InvalidInstructionID() const override;
// The number of possible instruction IDs. [[nodiscard]] uint32_t NumInstructionIDs() const override;
// A human-readable name for the instruction ID. [[nodiscard]] std::string InstructionIDName(uint32_t id) const override;
private: xed_decoded_inst_t xedd_; uint64_t address_; char full_text_[96]; bool valid_; };
} // namespace silifuzz
#endif // THIRD_PARTY_SILIFUZZ_INSTRUCTION_XED_DISASSEMBLER_H_
这是一个基于 Intel XED 库的反汇编器实现,用于 SiliFuzz 项目中的指令分析。让我详细解释代码的各个部分:
class XedDisassembler : public Disassembler {
XedDisassembler 继承自 Disassembler 基类,实现了统一的反汇编器接口XedDisassembler(const XedDisassembler&) = delete;
XedDisassembler(XedDisassembler&&) = delete;
XedDisassembler& operator=(const XedDisassembler&) = delete;
XedDisassembler& operator=(XedDisassembler&&) = delete;
= delete 显式禁用拷贝构造、移动构造、拷贝赋值和移动赋值private:
xed_decoded_inst_t xedd_; // XED 解码后的指令结构
uint64_t address_; // 指令地址
char full_text_[96]; // 反汇编文本缓冲区
bool valid_; // 指令是否有效
XedDisassembler::XedDisassembler() : valid_(false) {
InitXedIfNeeded();
}
valid_ 为 falseInitXedIfNeeded() 确保 XED 库被正确初始化(全局初始化)bool XedDisassembler::Disassemble(uint64_t address, const uint8_t* buffer,
size_t buffer_size) {
address_ = address;
xed_decoded_inst_zero(&xedd_); // 清零解码结构
xed_decoded_inst_set_mode(&xedd_,
XED_MACHINE_MODE_LONG_64, // 64位模式
XED_ADDRESS_WIDTH_64b); // 64位地址宽度
valid_ = xed_decode(&xedd_, buffer, buffer_size) == XED_ERROR_NONE;
return valid_;
}
详细步骤:
xedd_ 结构体XED_ERROR_NONE 表示解码成功size_t XedDisassembler::InstructionSize() const {
return valid_ ? xed_decoded_inst_get_length(&xedd_) : 0;
}
bool XedDisassembler::CanBranch() const {
if (!valid_) return false;
return InstructionIsBranch(xed_decoded_inst_inst(&xedd_));
}
InstructionIsBranch() 是自定义函数,检测是否为分支指令(JMP、CALL、条件跳转等)bool XedDisassembler::CanLoad() const {
if (!valid_) return false;
const unsigned int num_mem =
xed_decoded_inst_number_of_memory_operands(&xedd_);
for (int i = 0; i < num_mem; ++i) {
if (xed_decoded_inst_mem_read(&xedd_, i)) {
return true;
}
}
return false;
}
关键点:
CanLoad() 检测读操作,CanStore() 检测写操作std::string XedDisassembler::FullText() {
if (valid_) {
CHECK(FormatInstruction(xedd_, address_, full_text_, sizeof(full_text_)));
return full_text_;
} else {
return kInvalidInstructionName;
}
}
FormatInstruction() 是自定义包装函数,将 XED 结构转换为可读文本full_text_[96] 避免动态内存分配uint32_t XedDisassembler::InstructionID() const {
return valid_ ? xed_decoded_inst_get_iclass(&xedd_) : InvalidInstructionID();
}
std::string XedDisassembler::InstructionIDName(uint32_t id) const {
if (id == InvalidInstructionID()) {
return kInvalidInstructionName;
}
std::string name = xed_iclass_enum_t2str(static_cast<xed_iclass_enum_t>(id));
absl::AsciiStrToLower(&name); // 转换为小写
return name;
}
指令分类系统:
XED_ICLASS_ADD、XED_ICLASS_MOV)// 统一接口
class Disassembler {
public:
virtual bool Disassemble(...) = 0;
virtual std::string FullText() = 0;
// ...
};
// XED 适配器
class XedDisassembler : public Disassembler {
// 将 XED C API 包装成 C++ 接口
};
// 防御性编程
if (!valid_) return false;
// 断言式检查(开发时错误)
CHECK(FormatInstruction(...));
char full_text_[96] 避免堆分配这个反汇编器在 SiliFuzz 中用于:
通过统一的接口设计,SiliFuzz 可以轻松切换到其他反汇编器实现,提高了代码的可维护性和扩展性。