详细解释代码: 这是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_
下面分两部分来讲解代码:
xed_disassembler.h(接口与数据成员)xed_disassembler.cc(具体实现)过程中会顺带解释出现的 C++/XED 专业概念、关键宏以及整体设计思路。
xed_disassembler.h#ifndef THIRD_PARTY_SILIFUZZ_INSTRUCTION_XED_DISASSEMBLER_H_
#define THIRD_PARTY_SILIFUZZ_INSTRUCTION_XED_DISASSEMBLER_H_
• “Include-guard” 防卫宏,防止头文件被重复包含。
#include <cstddef>
#include <cstdint>
#include <string>
#include "./instruction/disassembler.h"
#include "./util/arch.h"
• 引入标准库与工程内其他头文件。
– disassembler.h 是一个抽象基类,规定了解码器必须提供的公共接口。
– arch.h 描述体系结构(仅做前向声明)。
extern "C" {
#include "third_party/libxed/xed-interface.h"
}
• extern "C" 保证 Intel XED C API 以 C 语言方式链接,避免 C++ 名字改编 (name-mangling)。
class XedDisassembler : public Disassembler
• 继承抽象基类 Disassembler,实现其所有纯虚方法。
• 线程不安全:XED 全局状态不是线程安全的,所以作者在注释里特别强调。
XedDisassembler();
~XedDisassembler();
XedDisassembler(const XedDisassembler&) = delete;
XedDisassembler(XedDisassembler&&) = delete;
XedDisassembler& operator=(const XedDisassembler&) = delete;
XedDisassembler& operator=(XedDisassembler&&) = delete;
• 禁止拷贝和移动,防止意外复制 XED 解析状态。
bool Disassemble(uint64_t address, const uint8_t* buffer, size_t size);size_t InstructionSize() const;bool CanBranch() const;bool CanLoad() const;bool CanStore() const;std::string FullText();uint32_t InstructionID() const;uint32_t InvalidInstructionID() const;uint32_t NumInstructionIDs() const;std::string InstructionIDName(uint32_t id) const;xed_decoded_inst_t xedd_; // XED 内部的“已解码指令”结构
uint64_t address_;
char full_text_[96]; // 存放格式化后的反汇编字符串
bool valid_; // 最近一次 Disassemble 是否成功
xed_disassembler.cc#include#include "./instruction/xed_disassembler.h"
#include <stdint.h>
#include <cstddef>
#include <string>
#include "absl/strings/ascii.h"
#include "./instruction/disassembler.h"
#include "./instruction/xed_util.h"
#include "./util/checks.h"
• absl/ 是 Google Abseil 基础库;AsciiStrToLower 用来把指令名称转小写。
• checks.h 提供 CHECK() 断言宏(与 gLog/gTest 风格一致)。
• xed_util.h 提供项目内包装函数 FormatInstruction()、InstructionIsBranch() 等。
接着通过 extern "C" 引入 XED 的枚举与 API 头文件。
namespace silifuzz { ... }将所有符号限定在自己的命名空间内。
XedDisassembler::XedDisassembler() : valid_(false) { InitXedIfNeeded(); }
• 成员初始化列表将 valid_ 设为 false。
• 调用工具函数 InitXedIfNeeded()——它负责初始化 XED 全局表,仅会在第一次调用时真正执行;后续实例复用已初始化状态。
析构函数空实现:
XedDisassembler::~XedDisassembler() {}
XED 不需要逐实例释放资源。
bool Disassemble(...)address_ = address; // 记录指令虚拟地址
xed_decoded_inst_zero(&xedd_); // 清空 decode 结果结构体
xed_decoded_inst_set_mode(&xedd_, // 指定解码模式
XED_MACHINE_MODE_LONG_64, // 64-bit long mode
XED_ADDRESS_WIDTH_64b);
valid_ = xed_decode(&xedd_, buffer, buffer_size) == XED_ERROR_NONE;
return valid_;
xed_decoded_inst_zero:重置结构体。xed_decoded_inst_set_mode:告诉 XED 当前在 64-bit 长模式下解析。xed_decode:真正解码字节串;若返回值为 XED_ERROR_NONE 表示成功。valid_ 中并返回。size_t InstructionSize() constreturn valid_ ? xed_decoded_inst_get_length(&xedd_) : 0;
• 若解码无效则返回 0,否则调用 XED API 取这条指令的字节长度。
bool CanBranch() constreturn valid_ && InstructionIsBranch(xed_decoded_inst_inst(&xedd_));
InstructionIsBranch() 是作者包装的助手函数,内部通过 xed_category_enum_t 判断指令类别是否为分支 (XED_CATEGORY_COND_BR, CALL, RET, JMP 等)。
bool CanLoad() constconst unsigned 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;
• XED 会把例如 push/pop 这类“隐式”访存操作显式化,因此能统一检测。
• mem_read 判断第 i 个内存操作数是否为读。
CanStore() 同理使用 xed_decoded_inst_mem_written()。
std::string FullText()if (valid_) {
CHECK(FormatInstruction(xedd_, address_, full_text_, sizeof(full_text_)));
return full_text_; // 隐式从 char[] 转成 std::string
}
return kInvalidInstructionName; // "<invalid>"
• FormatInstruction():项目内封装,调用 xed_format_context() 之类 XED 函数,把二进制指令格式化成 AT&T 或 Intel 汇编文本。
• CHECK(...) 断言该函数必须成功;失败时程序会 abort() 并打印错误。
• 常量 kInvalidInstructionName 定义在基类,通常值为 "<invalid>"。
uint32_t InstructionID() constreturn valid_ ? xed_decoded_inst_get_iclass(&xedd_) : InvalidInstructionID();
• iclass 是 XED 的“指令种类”枚举 (XED_ICLASS_ADD, XED_ICLASS_MOV …)。
• 如果无效则返回特定值 XED_ICLASS_INVALID。
其余辅助函数:
uint32_t NumInstructionIDs() const { return XED_ICLASS_LAST; }
std::string InstructionIDName(uint32_t id) const;
• XED_ICLASS_LAST 表示枚举元素个数(末尾 + 1)。
• InstructionIDName 先用 XED API 转成字符串,再用 AsciiStrToLower 变小写,便于统一比较/展示。
| 概念 | 解释 |
|---|---|
| XED | Intel® X86 Encoder-Decoder library,官方开源反汇编/汇编库。 |
| xed_decoded_inst_t | XED 解析后的“一条指令”的内部描述结构体。 |
| InstructionIsBranch | 项目内工具,检测指令是否为跳转/调用/返回等控制流修改。 |
| CHECK(expr) | Google-style 断言宏;expr 为假时打印栈回溯并终止。 |
| [[nodiscard]] | C++17 属性:告诉调用者不要忽视返回值。 |
| extern "C" | 关闭 C++ 名字改编,使能与 C 库(XED)链接。 |
| Include-guard | 头文件重复包含保护宏。 |
| iclass | XED 的“Instruction Class”,比分类(category)更细,如 ADD/SUB。 |
XedDisassembler 负责逐条解码指令,提供具体信息(文本、大小、访存/分支属性等)。Disassembler,它可以在整个 SiliFuzz 框架里对接其他组件(快照重放、覆盖统计、模糊测试等)。至此,xed_disassembler.{h,cc} 的核心设计与语法细节全部解析完毕。