这是programbatchmutator.cc: // Copyright 2023 The Silifuzz Authors. // // Licensed under…
- User
这是program_batch_mutator.cc: // 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.
#include "./fuzzer/program_batch_mutator.h"
#include #include #include #include #include
#include "./fuzzer/program.h" #include "./fuzzer/program_mutation_ops.h" #include "./fuzzer/program_mutator.h" #include "./util/arch.h"
namespace silifuzz {
namespace {
// Centipede expects that mutators will never produce outputs that are zero // bytes in length. Get out of this situation by adding random instructions. template void AddInstructionIfZeroLength(MutatorRng& rng, Program& program) { InsertGeneratedInstruction m; while (program.ByteLen() == 0) { m.Mutate(rng, program, program); } }
template void FinalizeProgram(MutatorRng& rng, Program& program, size_t max_len) { // Note that LimitProgramLength may reduce the program size back to zero. // We consider obeying max_len more important than outputting non-trivial // inputs. AddInstructionIfZeroLength(rng, program);
// FixupEncodedDisplacements can change the program length. // LimitProgramLength can invalidate the displacements. // In most cases it should be safe to limit and then fixup. // We're doing the iterative approach because it's robust against fixup doing // something strange. // We limit the program length first, however, because it could help avoid the // need for more drastic fixups on out-of-range branches. LimitProgramLength(rng, program, max_len); while (true) { if (!program.FixupEncodedDisplacements(rng)) break; if (!LimitProgramLength(rng, program, max_len)) break; } }
} // namespace
template ProgramBatchMutator::ProgramBatchMutator(uint64_t seed, double crossover_weight, size_t max_len) : rng_(seed), max_len_(max_len) { // Clamp the crossover weight to [0.0, 1.0] crossover_weight = std::max(std::min(crossover_weight, 1.0), 0.0);
// TODO(ncbray): copy instruction from dictionary. // TODO(ncbray): how should these be weighted? // TODO(ncbray): consider what the best policy is for randomly removing // instructions. // Removing instructions is tricky to get right. // In general we want to grow larger, non-trivial inputs. // On the other hand, we also want to aggressively garbage collect // "uninteresting" instructions to prevent them from being copied along // from input to input along with the interesting ones. // How we balance growth vs. garbage collection TBD. // For now, do not remove instructions if the program is small. // This avoid cases where we remove all the instructions in a program and // destroy 100% of the information the original input contained. mutator_ = MoveIntoPtr(RetryMutation{ 128, SelectMutation( Weighted( 1.0 - crossover_weight, RepeatMutation{ {0, 1, 1, 1, 1}, RetryMutation{ 128, SelectMutation( Weighted(5.0, InsertGeneratedInstruction{}), Weighted(5.0, MutateInstruction{}), Weighted(1.0, SwapInstructions{}), Weighted(2.0, DeleteInstruction{3}),
text
text
Weighted(15.0, InsertLDMXCSRInstruction<Arch>{}), // LDMXCSR Weighted(15.0, InsertIDIVInstruction<Arch>{}) // IDIV )}}),Weighted(0.5 * crossover_weight, CrossoverInsert{}), Weighted(0.5 * crossover_weight, CrossoverOverwrite{}))});
}
template void ProgramBatchMutator::GenerateSingleOutput( const Program& input, const Program& other, std::vector<uint8_t>& output) { // Copy Program program = input;
// Mutate mutator_->Mutate(rng_, program, other);
// Output FinalizeProgram(rng_, program, max_len_); program.ToBytes(output); }
template void ProgramBatchMutator::Mutate( const std::vector<const std::vector<uint8_t>>& inputs, size_t num_mutants, std::vector<std::vector<uint8_t>>& mutants) { // Extract the programs from the inputs. // Copying a program should be cheaper that re-parsing each instruction for // each mutant. std::vector<Program> programs; programs.reserve(inputs.size()); for (const std::vector<uint8_t> input : inputs) { programs.push_back(Program(*input)); }
// Generate the requested mutants. for (size_t i = 0; i < num_mutants; ++i) { size_t base = RandomIndex(rng_, inputs.size()); size_t other = RandomIndex(rng_, inputs.size()); GenerateSingleOutput(programs[base], programs[other], mutants[i]); } }
template class ProgramBatchMutator<X86_64>; template class ProgramBatchMutator;
} // namespace silifuzz
这是program_mutation_ops.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_FUZZER_PROGRAM_MUTATION_OPS_H_ #define THIRD_PARTY_SILIFUZZ_FUZZER_PROGRAM_MUTATION_OPS_H_
#include #include #include
#include "./fuzzer/program.h" #include "./fuzzer/program_mutator.h"
namespace silifuzz {
// Try to generate a random instruction from scratch. // Returns
trueis successful. template bool GenerateSingleInstruction(MutatorRng& rng, Instruction& instruction);// Mutate
originaland place the output inmutatedusing the default // single-instruction mutation policy. // Returnstrueis successful. template bool MutateSingleInstruction(MutatorRng& rng, const Instruction& original, Instruction& mutated);// Assuming
originalis the original instruction andmutatedis a modified // copy, copy the instruction displacement boundaries if the encoded // displacement is present in bothoriginalandmutatedand did not change. // Otherwise, randomize the boundaries that are present inmutatedbut not //original, or were modified between the two versions. template void CopyOrRandomizeInstructionDisplacementBoundaries( MutatorRng& rng, const Instruction& original, Instruction& mutated, size_t num_boundaries);// Shift the instruction displacement boundaries of
instructionso that the // relative displacements are same after the instruction's index has shifted to // index +index_offset. This is done by also shifting the displacements by //index_offset. // If keeping the new displacement no longer points to a valid instruction // boundary, randomize the displacement to point to a valid boundary. // This function is used when we want to copy one or more instructions from // somewhere and we want to ensure the displacements of the copied instructions // have the same relative shape when placed at their new location rather than // preserving the absolute values. template void ShiftOrRandomizeInstructionDisplacementBoundaries( MutatorRng& rng, Instruction& instruction, int64_t index_offset, size_t num_boundaries);template void ShiftOrRandomizeInstructionDisplacementBoundaries( MutatorRng& rng, std::vector<Instruction>& block, int64_t index_offset, size_t num_boundaries) { for (Instruction& instruction : block) { ShiftOrRandomizeInstructionDisplacementBoundaries( rng, instruction, index_offset, num_boundaries); } }
// Insert a randomly generated instruction at a random boundary in the program. template class InsertGeneratedInstruction : public ProgramMutator { public: InsertGeneratedInstruction() {}
// Returns
trueif successful, returnsfalseif the the random number // generator was deeply unlucky. bool Mutate(MutatorRng& rng, Program& program, const Program& other) override { Instruction insn; bool success = GenerateSingleInstruction(rng, insn); if (!success) return false;// Inserting the instruction will increase the number of potential // instruction boundaries by one. RandomizeInstructionDisplacementBoundaries( rng, insn, program.NumInstructionBoundaries() + 1); size_t insert_boundary = program.RandomInstructionBoundary(rng); bool steal_displacements = RandomIndex(rng, 2); program.InsertInstruction(insert_boundary, steal_displacements, insn); return true;} };
// Randomly modify a random instruction in the program. template class MutateInstruction : public ProgramMutator { public: MutateInstruction() {}
// Returns
trueif successful, returnsfalseif the the random number // generator was unlucky, although some instructions may be more difficult to // successfully mutate than others. bool Mutate(MutatorRng& rng, Program& program, const Program& other) override { // Is there anything to mutate? if (program.NumInstructions() == 0) return false;// Select a random instruction. size_t target = program.RandomInstructionIndex(rng); const Instruction<Arch>& original = program.GetInstruction(target); // Try to mutate. Instruction<Arch> mutated{}; if (MutateSingleInstruction(rng, original, mutated)) { CopyOrRandomizeInstructionDisplacementBoundaries( rng, original, mutated, program.NumInstructionBoundaries()); program.SetInstruction(target, mutated); return true; } return false;} };
// Remove a random instruction from the program. template class DeleteInstruction : public ProgramMutator { public: explicit DeleteInstruction(size_t minimum_instructions = 0) : minimum_instructions_(minimum_instructions) {}
// Returns
trueif successful, returnsfalseif the program is too small // and we should not delete any instructions. bool Mutate(MutatorRng& rng, Program& program, const Program& other) override { // Can an instruction be removed without going below the minimum? if (program.NumInstructions() <= minimum_instructions_) return false;// Remove a random instruction. size_t victim = program.RandomInstructionIndex(rng); program.RemoveInstruction(victim); return true;}
private: size_t minimum_instructions_; };
template class SwapInstructions : public ProgramMutator { public: SwapInstructions() {}
bool Mutate(MutatorRng& rng, Program& program, const Program& other) override { // Need at least two instructions to swap. if (program.NumInstructions() < 2) return false;
// Select the targets. size_t a = program.RandomInstructionIndex(rng); size_t b = program.RandomInstructionIndex(rng); while (a == b) { b = program.RandomInstructionIndex(rng); } // Copy the instructions. Instruction<Arch> a_instruction = program.GetInstruction(a); Instruction<Arch> b_instruction = program.GetInstruction(b); // Swap the instructions. // Note that the branch displacements are not affected by this operation. // A branch that is swapped will target the same absolute location. // An alternative mutation would be to move the displacement how ever much // the instruction moved and re-randomize it if it goes out of range. program.SetInstruction(a, b_instruction); program.SetInstruction(b, a_instruction); return true;} };
// Copy a random chunk from the other program and insert it at a random // instruction boundary. template class CrossoverInsert : public ProgramMutator { public: CrossoverInsert() {}
bool Mutate(MutatorRng& rng, Program& program, const Program& other) override { // Is there anything to crossover with? if (other.NumInstructions() == 0) return false;
// Determine how much of the other program we want to copy. // src_size = [1, NumInstructions()] size_t max_size = other.NumInstructions(); size_t src_size = RandomIndex(rng, max_size) + 1; size_t src_index = RandomIndex(rng, other.NumInstructions() - src_size + 1); // We must copy because `program` and `other` can be aliased. std::vector<Instruction<Arch>> block = other.CopyInstructionBlock(src_index, src_size); // Determine where we want to insert the block. size_t dst_boundary = program.RandomInstructionBoundary(rng); // Fixup the branch displacements of the copied instructions. int64_t index_offset = (int64_t)dst_boundary - (int64_t)src_index; ShiftOrRandomizeInstructionDisplacementBoundaries( rng, block, index_offset, program.NumInstructionBoundaries() + block.size()); // Insert. bool steal_displacements = RandomIndex(rng, 2); program.InsertInstructionBlock(dst_boundary, steal_displacements, block); return true;} };
// Copy a random chunk from the other program and overwrite the current program // at a random instruction idnex. template class CrossoverOverwrite : public ProgramMutator { public: CrossoverOverwrite() {}
bool Mutate(MutatorRng& rng, Program& program, const Program& other) override { // Is there anything to overwrite? if (program.NumInstructions() == 0) return false;
// Is there anything to crossover with? if (other.NumInstructions() == 0) return false; // Determine how much of the other program we want to copy. // We do not want to overwrite more than half the current program and cannot // copy more from the other program than exists. size_t max_size = std::min(std::max(1UL, program.NumInstructions() / 2), other.NumInstructions()); size_t src_size = RandomIndex(rng, max_size) + 1; size_t src_index = RandomIndex(rng, other.NumInstructions() - src_size + 1); // We must copy because `program` and `other` can be aliased. std::vector<Instruction<Arch>> block = other.CopyInstructionBlock(src_index, src_size); // Determine where we want to insert the block. size_t dst_index = RandomIndex(rng, program.NumInstructions() - block.size() + 1); // Fixup the branch displacements of the copied instructions. int64_t index_offset = (int64_t)dst_index - (int64_t)src_index; ShiftOrRandomizeInstructionDisplacementBoundaries( rng, block, index_offset, program.NumInstructionBoundaries()); // Overwrite. program.SetInstructionBlock(dst_index, block); return true;} };
//添加 LDMXCSR 指令生成器 template class InsertLDMXCSRInstruction : public ProgramMutator { public: InsertLDMXCSRInstruction() {}
bool Mutate(MutatorRng& rng, Program& program, const Program& other) override { if constexpr (std::is_same_v<Arch, X86_64>) {
Instruction<X86_64> insn; // LDMXCSR m32 - 多种寻址 // 修正后的 LDMXCSR 合法机器码模式(x86_64)const std::vector<std::vector<uint8_t>> ldmxcsr_patterns = { // 1. 寄存器间接寻址(Mod=00, Reg=010, RM=xxx) {0x0F, 0xAE, 0x02}, // [eax] → ModRM=0x02(00 010 000) {0x0F, 0xAE, 0x03}, // [ecx] → ModRM=0x03(00 010 001) {0x0F, 0xAE, 0x06}, // [esi] → ModRM=0x06(00 010 110) {0x0F, 0xAE, 0x07}, // [edi] → ModRM=0x07(00 010 111)
// 2. 带8位位移(Mod=01, Reg=010, RM=xxx) {0x0F, 0xAE, 0x42, 0x10}, // [eax+0x10] → ModRM=0x42(01 010 000) {0x0F, 0xAE, 0x43, 0x10}, // [ecx+0x10] → ModRM=0x43(01 010 001) {0x0F, 0xAE, 0x46, 0x08}, // [esi+0x08] → ModRM=0x46(01 010 110) {0x0F, 0xAE, 0x47, 0x0C}, // [edi+0x0C] → ModRM=0x47(01 010 111)
// 3. 带32位位移(Mod=10, Reg=010, RM=xxx) {0x0F, 0xAE, 0x82, 0x00, 0x10, 0x00, 0x00}, // [eax+0x1000] → ModRM=0x82(10 010 000) {0x0F, 0xAE, 0x83, 0x00, 0x20, 0x00, 0x00}, // [ecx+0x2000] → ModRM=0x83(10 010 001) {0x0F, 0xAE, 0x86, 0x00, 0x30, 0x00, 0x00}, // [esi+0x3000] → ModRM=0x86(10 010 110)
// 4. SIB复杂寻址(栈相关,适合Silifuzz的栈内存模型) {0x0F, 0xAE, 0x06, 0x24}, // [esp] → ModRM=0x06(00 010 100)+ SIB=0x24(esp基址) {0x0F, 0xAE, 0x46, 0x00}, // [ebp+0] → ModRM=0x46(01 010 110)(ebp是栈基址,位移0) {0x0F, 0xAE, 0x86, 0x00, 0x00, 0x00, 0x00}, // [esi](32位位移0,兼容64位地址) };
size_t pattern_idx = RandomIndex(rng, ldmxcsr_patterns.size()); const auto& pattern = ldmxcsr_patterns[pattern_idx]; insn.encoded.Copy(pattern.data(), pattern.size()); // 设置位移边界 RandomizeInstructionDisplacementBoundaries( rng, insn, program.NumInstructionBoundaries() + 1); size_t insert_boundary = program.RandomInstructionBoundary(rng); bool steal_displacements = RandomIndex(rng, 2); program.InsertInstruction(insert_boundary, steal_displacements, insn); return true; } return false;} };
// 添加 IDIV 指令生成器 template class InsertIDIVInstruction : public ProgramMutator { public: InsertIDIVInstruction() {}
bool Mutate(MutatorRng& rng, Program& program, const Program& other) override { if constexpr (std::is_same_v<Arch, X86_64>) { Instruction<X86_64> insn;
// IDIV 指令的不同变体 const std::vector<std::vector<uint8_t>> idiv_patterns = { // 8位 IDIV {0xF6, 0xF8}, // idiv al {0xF6, 0xF9}, // idiv cl {0xF6, 0xFA}, // idiv dl {0xF6, 0xFB}, // idiv bl {0xF6, 0xFC}, // idiv ah {0xF6, 0xFD}, // idiv ch {0xF6, 0xFE}, // idiv dh {0xF6, 0xFF}, // idiv bh // 32位 IDIV (需要 REX.W 前缀) {0x48, 0xF7, 0xF8}, // idiv rax {0x48, 0xF7, 0xF9}, // idiv rcx {0x48, 0xF7, 0xFA}, // idiv rdx {0x48, 0xF7, 0xFB}, // idiv rbx {0x48, 0xF7, 0xFC}, // idiv rsp {0x48, 0xF7, 0xFD}, // idiv rbp {0x48, 0xF7, 0xFE}, // idiv rsi {0x48, 0xF7, 0xFF} // idiv rdi }; size_t pattern_idx = RandomIndex(rng, idiv_patterns.size()); const auto& pattern = idiv_patterns[pattern_idx]; insn.encoded.Copy(pattern.data(), pattern.size()); // 设置位移边界 RandomizeInstructionDisplacementBoundaries( rng, insn, program.NumInstructionBoundaries() + 1); size_t insert_boundary = program.RandomInstructionBoundary(rng); bool steal_displacements = RandomIndex(rng, 2); program.InsertInstruction(insert_boundary, steal_displacements, insn); return true; } return false;} }; // Remove instructions until
program.NumBytes()<=max_len. // Returnstrueif the program was modified. template bool LimitProgramLength(MutatorRng& rng, Program& program, size_t max_len);// Exported for testing void FlipBit(uint8_t* buffer, size_t bit); void FlipRandomBit(MutatorRng& rng, uint8_t* buffer, size_t buffer_size);
} // namespace silifuzz
#endif // THIRD_PARTY_SILIFUZZ_FUZZER_PROGRAM_MUTATION_OPS_H_
上面代码中关于LDMXCSR和idiv的部分是我为了增加这两个指令的数量而单独添加的代码,为什么我修改后LDMXCSR和idiv的数量并没有增加,检查我的修改方法是否争取并帮助我修改
- Assistant
以下是针对您问题的详细分析和修改建议。我会先解释为什么您的修改后 LDMXCSR 和 IDIV 指令的数量没有增加(基于您提供的代码),然后提供正确的修改代码。分析基于 Silifuzz 的代码结构(program_batch_mutator.cc 和 program_mutation_ops.h)、x86_64 指令编码规则(来自 Intel 手册),以及典型的 fuzzer 行为。
1. 问题分析:为什么 LDMXCSR 和 IDIV 的数量没有增加?
您的修改在概念上是正确的:您添加了两个专用的
ProgramMutator类(InsertLDMXCSRInstruction和InsertIDIVInstruction),并在SelectMutation中赋予它们较高的权重(15.0)。这应该会增加这些指令被插入的概率(总权重约 43,其中每个新操作占 ~35%)。这些操作位于RetryMutation和RepeatMutation的内部循环中,因此有机会多次执行。但是,基于代码审查,以下是可能导致数量没有增加的原因(从最可能到次要):
主要问题:LDMXCSR 的 patterns 编码错误(不是有效的 LDMXCSR 指令)
- LDMXCSR 指令的 opcode 是
0F AE /2(其中/2表示 ModR/M 字节的 reg 字段必须是 010,即 2)。这意味着 ModR/M 字节的计算公式是:- ModR/M = (mod << 6) | (2 << 3) | rm = (mod << 6) + 16 + rm(因为 2 << 3 = 16)。
- 您的 patterns 中的 ModR/M 字节都错了:
- 例如,{0x0F, 0xAE, 0x02}:0x02 = 二进制 00000010(mod=00, reg=000, rm=010)。reg=000 不是 2,这不是 LDMXCSR,而是其他指令(如 STMXCSR 或无效)。
- 类似地,0x03=00000011 (reg=000)、0x06=00000110 (reg=000)、0x07=00000111 (reg=000) 等,都将 reg 设置为 000 而不是 010。
- 带位移的 patterns(如 0x42=01000010, reg=000)也错了。
- SIB 示例 {0x0F, 0xAE, 0x06, 0x24}:0x06 的 reg=000,不是 010;正确的 ModR/M 应该是 0x14(00 010 100)+ SIB 0x24 表示 [esp]。
- 结果:插入的不是 LDMXCSR 指令,而是其他指令(如 STMXCSR 或无效编码)。fuzzer 可能将它们视为普通指令,但不会计入 LDMXCSR 的数量。或者,如果这些编码无效,程序可能在
FinalizeProgram中被修复/移除,导致数量不增加。 - IDIV 的 patterns 看起来是正确的(例如,{0xF6, 0xF8} 是有效的 idiv al;{0x48, 0xF7, 0xF8} 是 idiv rax)。所以 IDIV 的问题不是编码错误,而是下面的次要问题。
次要问题:IDIV(和潜在的 LDMXCSR)数量不增加的原因
- 权重和随机性:权重 15.0 在总 43 中占 ~35%,但:
- 这些操作在
RepeatMutation的内部(重复 0~4 次,分布 {0,1,1,1,1}),并且有RetryMutation(重试 128 次)。 - 其他操作(如
DeleteInstruction权重 2.0)可能会删除新插入的指令。 CrossoverInsert和CrossoverOverwrite(权重基于 crossover_weight)可能会覆盖或插入其他内容,稀释效果。
- 这些操作在
- FinalizeProgram 的影响:
LimitProgramLength会随机移除指令,直到程序长度 <= max_len。如果程序经常超过 max_len,新插入的指令(包括 IDIV/LDMXCSR)可能被移除。FixupEncodedDisplacements修复分支位移,但 LDMXCSR/IDIV 不是分支指令,所以不受影响。但如果程序被多次修改,这些指令可能被间接移除。
- 架构检查:您的代码有
if constexpr (std::is_same_v<Arch, X86_64>),如果 fuzzer 不是在 x86_64 上运行(例如 AArch64),则返回 false,不会插入。 - 指令有效性:
- LDMXCSR/IDIV 涉及内存访问或除法,如果在 Silifuzz 的执行环境中导致崩溃/无效(例如,除零或无效内存),fuzzer 可能过滤掉这些输入,导致数量不增加。
RandomizeInstructionDisplacementBoundaries被调用,但 LDMXCSR/IDIV 没有分支位移(displacement 是内存位移,不是分支)。这可能导致边界随机化失败或无操作,但不会阻止插入。
- 观察问题:您如何统计数量?如果是通过日志或输出检查,确保统计的是最终程序(post-FinalizeProgram),而不是中间状态。
- 其他:代码中有 "text text",可能是复制粘贴错误,但不影响逻辑。
总体评估:您的修改方法基本正确,但 LDMXCSR 的编码错误是主要问题。修复后,数量应该增加。如果仍不增加,可以进一步调整权重或在
LimitProgramLength中优先保留这些指令。2. 修改建议
- 修复 LDMXCSR patterns:使用正确的 ModR/M 计算(reg=010)。我提供了多样化的有效 patterns,包括寄存器间接、带位移、SIB(适合栈内存),并确保兼容 x86_64(添加 REX 前缀的 64-bit 变体)。
- 增强 IDIV patterns:虽然正确,但我添加了更多变体(包括内存操作数),以增加多样性。
- 增加权重:将权重从 15.0 提高到 30.0(总权重变为 73,每个占 ~41%),以抵消删除/限制的影响。
- 其他优化:
- 在
Mutate中添加日志(如果调试),以确认插入成功。 - 如果仍不增加,考虑在
InsertGeneratedInstruction中集成这些(而不是单独类),或修改LimitProgramLength以避免移除特定指令(但这需要修改 program_mutation_ops.cc 的实现)。
- 在
- 测试:编译后,运行 fuzzer 并检查生成的程序(例如,使用 objdump 或自定义日志)是否包含这些指令(opcode 0F AE /2 for LDMXCSR, F6/F7 /7 for IDIV)。
3. 修改后的代码
program_mutation_ops.h(修复 patterns 和类)
// ... (原有代码不变) // 添加 LDMXCSR 指令生成器(修正 patterns) template <typename Arch> class InsertLDMXCSRInstruction : public ProgramMutator<Arch> { public: InsertLDMXCSRInstruction() {} bool Mutate(MutatorRng& rng, Program<Arch>& program, const Program<Arch>& other) override { if constexpr (std::is_same_v<Arch, X86_64>) { Instruction<X86_64> insn; // 修正后的有效 LDMXCSR patterns (0F AE /2, reg=010) // 包括寄存器间接、带位移、SIB,兼容 x86_64(部分添加 REX) const std::vector<std::vector<uint8_t>> ldmxcsr_patterns = { // 1. 寄存器间接寻址 (mod=00, reg=010, rm=xxx) {0x0F, 0xAE, 0x10}, // ldmxcsr [rax/eax] (ModRM=0x10: 00 010 000) {0x0F, 0xAE, 0x11}, // ldmxcsr [rcx/ecx] (0x11: 00 010 001) {0x0F, 0xAE, 0x12}, // ldmxcsr [rdx/edx] (0x12: 00 010 010) {0x0F, 0xAE, 0x13}, // ldmxcsr [rbx/ebx] (0x13: 00 010 011) {0x0F, 0xAE, 0x16}, // ldmxcsr [rsi/esi] (0x16: 00 010 110) {0x0F, 0xAE, 0x17}, // ldmxcsr [rdi/edi] (0x17: 00 010 111) // 2. 带8位位移 (mod=01, reg=010, rm=xxx) {0x0F, 0xAE, 0x50, 0x10}, // ldmxcsr [rax+0x10] (0x50: 01 010 000) {0x0F, 0xAE, 0x51, 0x20}, // ldmxcsr [rcx+0x20] (0x51: 01 010 001) {0x0F, 0xAE, 0x56, 0x08}, // ldmxcsr [rsi+0x08] (0x56: 01 010 110) {0x0F, 0xAE, 0x57, 0x0C}, // ldmxcsr [rdi+0x0C] (0x57: 01 010 111) // 3. 带32位位移 (mod=10, reg=010, rm=xxx) {0x0F, 0xAE, 0x90, 0x00, 0x10, 0x00, 0x00}, // ldmxcsr [rax+0x1000] (0x90: 10 010 000) {0x0F, 0xAE, 0x91, 0x00, 0x20, 0x00, 0x00}, // ldmxcsr [rcx+0x2000] (0x91: 10 010 001) {0x0F, 0xAE, 0x96, 0x00, 0x30, 0x00, 0x00}, // ldmxcsr [rsi+0x3000] (0x96: 10 010 110) // 4. SIB 复杂寻址(栈相关) {0x0F, 0xAE, 0x14, 0x24}, // ldmxcsr [esp] (ModRM=0x14: 00 010 100 + SIB=0x24: no index, base=esp) {0x0F, 0xAE, 0x55, 0x00}, // ldmxcsr [rbp+0] (0x55: 01 010 101, ebp as stack base) {0x0F, 0xAE, 0x96, 0x00, 0x00, 0x00, 0x00}, // ldmxcsr [rsi+0] (32-bit disp=0) // 5. 添加 REX 前缀的 64-bit 变体(扩展寄存器) {0x41, 0x0F, 0xAE, 0x10}, // ldmxcsr [r8] (REX.B=1, ModRM=0x10) {0x41, 0x0F, 0xAE, 0x11} // ldmxcsr [r9] (REX.B=1, ModRM=0x11) }; size_t pattern_idx = RandomIndex(rng, ldmxcsr_patterns.size()); const auto& pattern = ldmxcsr_patterns[pattern_idx]; insn.encoded.Copy(pattern.data(), pattern.size()); // 设置位移边界(LDMXCSR 非分支,但保留以兼容) RandomizeInstructionDisplacementBoundaries( rng, insn, program.NumInstructionBoundaries() + 1); size_t insert_boundary = program.RandomInstructionBoundary(rng); bool steal_displacements = RandomIndex(rng, 2); program.InsertInstruction(insert_boundary, steal_displacements, insn); return true; } return false; } }; // 添加 IDIV 指令生成器(原有正确,添加更多内存变体以增加多样性) template <typename Arch> class InsertIDIVInstruction : public ProgramMutator<Arch> { public: InsertIDIVInstruction() {} bool Mutate(MutatorRng& rng, Program<Arch>& program, const Program<Arch>& other) override { if constexpr (std::is_same_v<Arch, X86_64>) { Instruction<X86_64> insn; // IDIV patterns(原有 + 新增内存操作数变体) const std::vector<std::vector<uint8_t>> idiv_patterns = { // 8位 IDIV (寄存器) {0xF6, 0xF8}, // idiv al (ModRM=0xF8: 11 111 000) {0xF6, 0xF9}, // idiv cl {0xF6, 0xFA}, // idiv dl {0xF6, 0xFB}, // idiv bl {0xF6, 0xFC}, // idiv ah {0xF6, 0xFD}, // idiv ch {0xF6, 0xFE}, // idiv dh {0xF6, 0xFF}, // idiv bh // 64位 IDIV (REX.W + 寄存器) {0x48, 0xF7, 0xF8}, // idiv rax {0x48, 0xF7, 0xF9}, // idiv rcx {0x48, 0xF7, 0xFA}, // idiv rdx {0x48, 0xF7, 0xFB}, // idiv rbx {0x48, 0xF7, 0xFC}, // idiv rsp {0x48, 0xF7, 0xFD}, // idiv rbp {0x48, 0xF7, 0xFE}, // idiv rsi {0x48, 0xF7, 0xFF}, // idiv rdi // 新增:64位 IDIV 内存操作数 (例如 idiv qword ptr [reg]) {0x48, 0xF7, 0x38}, // idiv qword ptr [rax] (ModRM=0x38: 00 111 000) {0x48, 0xF7, 0x39}, // idiv qword ptr [rcx] (0x39: 00 111 001) {0x48, 0xF7, 0x7E} // idiv qword ptr [rsi] (0x7E: 00 111 110) }; size_t pattern_idx = RandomIndex(rng, idiv_patterns.size()); const auto& pattern = idiv_patterns[pattern_idx]; insn.encoded.Copy(pattern.data(), pattern.size()); // 设置位移边界(IDIV 非分支,但保留) RandomizeInstructionDisplacementBoundaries( rng, insn, program.NumInstructionBoundaries() + 1); size_t insert_boundary = program.RandomInstructionBoundary(rng); bool steal_displacements = RandomIndex(rng, 2); program.InsertInstruction(insert_boundary, steal_displacements, insn); return true; } return false; } }; // ... (LimitProgramLength 等原有代码不变)program_batch_mutator.cc(增加权重,移除 "text text")
// ... (原有代码不变) // 在 SelectMutation<Arch> 的 Weighted 列表中,增加权重到 30.0 // (总权重: 5+5+1+2+30+30=73) SelectMutation<Arch>( Weighted(5.0, InsertGeneratedInstruction<Arch>{}), Weighted(5.0, MutateInstruction<Arch>{}), Weighted(1.0, SwapInstructions<Arch>{}), Weighted(2.0, DeleteInstruction<Arch>{3}), Weighted(30.0, InsertLDMXCSRInstruction<Arch>{}), // LDMXCSR Weighted(30.0, InsertIDIVInstruction<Arch>{}) // IDIV ) // ... (其余代码不变)4. 下一步
- 编译和测试:重新编译 Silifuzz,运行 fuzzer 生成一些 mutants,并使用工具(如
objdump -d)检查是否出现 LDMXCSR (opcode 0F AE with reg=2) 和更多 IDIV。 - 如果仍不增加:添加调试日志(例如,在
GenerateSingleOutput中打印程序),或进一步提高权重/减少DeleteInstruction的权重。 - 参考:Intel SDM Volume 2 for opcode details。如果需要更多 patterns 或 AArch64 支持,请提供细节。
如果您提供更多细节(如 fuzzer 输出或错误日志),我可以进一步优化。
- LDMXCSR 指令的 opcode 是
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.