这是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
我来帮你分析并修改代码。问题主要出在以下几个方面:
- 权重设置问题: 在 program_batch_mutator.cc 中,虽然添加了新的指令生成器,但权重设置可能不够合理。让我们修改权重分配:
// 在 program_batch_mutator.cc 中修改权重 Weighted(5.0, InsertGeneratedInstruction<Arch>{}), Weighted(5.0, MutateInstruction<Arch>{}), Weighted(1.0, SwapInstructions<Arch>{}), Weighted(2.0, DeleteInstruction<Arch>{3}), // 增加 LDMXCSR 和 IDIV 的权重 Weighted(20.0, InsertLDMXCSRInstruction<Arch>{}), // 增加权重 Weighted(20.0, InsertIDIVInstruction<Arch>{}) // 增加权重- LDMXCSR 指令生成器的改进:
template <typename Arch> class InsertLDMXCSRInstruction : public ProgramMutator<Arch> { public: bool Mutate(MutatorRng& rng, Program<Arch>& program, const Program<Arch>& other) override { if constexpr (!std::is_same_v<Arch, X86_64>) { return false; } Instruction<X86_64> insn; // 基本的 LDMXCSR 指令模式 static const std::vector<std::vector<uint8_t>> ldmxcsr_patterns = { // REX.W + LDMXCSR variants for 64-bit mode {0x48, 0x0F, 0xAE, 0x10}, // LDMXCSR [rax] {0x48, 0x0F, 0xAE, 0x11}, // LDMXCSR [rcx] {0x48, 0x0F, 0xAE, 0x12}, // LDMXCSR [rdx] {0x48, 0x0F, 0xAE, 0x13}, // LDMXCSR [rbx] {0x48, 0x0F, 0xAE, 0x14, 0x24}, // LDMXCSR [rsp] {0x48, 0x0F, 0xAE, 0x15}, // LDMXCSR [rbp] {0x48, 0x0F, 0xAE, 0x16}, // LDMXCSR [rsi] {0x48, 0x0F, 0xAE, 0x17}, // LDMXCSR [rdi] }; size_t pattern_idx = RandomIndex(rng, ldmxcsr_patterns.size()); const auto& pattern = ldmxcsr_patterns[pattern_idx]; insn.encoded.Copy(pattern.data(), pattern.size()); // 随机选择插入位置 size_t insert_boundary = program.RandomInstructionBoundary(rng); bool steal_displacements = RandomIndex(rng, 2); // 确保指令被正确编码 if (!program.InsertInstruction(insert_boundary, steal_displacements, insn)) { return false; } return true; } };- IDIV 指令生成器的改进:
template <typename Arch> class InsertIDIVInstruction : public ProgramMutator<Arch> { public: bool Mutate(MutatorRng& rng, Program<Arch>& program, const Program<Arch>& other) override { if constexpr (!std::is_same_v<Arch, X86_64>) { return false; } Instruction<X86_64> insn; // 64位模式下的 IDIV 指令模式 static const std::vector<std::vector<uint8_t>> idiv_patterns = { // REX.W + IDIV variants for 64-bit registers {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 // Memory operand variants {0x48, 0xF7, 0x38}, // IDIV QWORD PTR [RAX] {0x48, 0xF7, 0x39}, // IDIV QWORD PTR [RCX] {0x48, 0xF7, 0x3A}, // IDIV QWORD PTR [RDX] {0x48, 0xF7, 0x3B}, // IDIV QWORD PTR [RBX] }; size_t pattern_idx = RandomIndex(rng, idiv_patterns.size()); const auto& pattern = idiv_patterns[pattern_idx]; insn.encoded.Copy(pattern.data(), pattern.size()); size_t insert_boundary = program.RandomInstructionBoundary(rng); bool steal_displacements = RandomIndex(rng, 2); if (!program.InsertInstruction(insert_boundary, steal_displacements, insn)) { return false; } return true; } };主要改进点:
- 增加了这两类指令的生成权重,使它们更容易被选中
- 改进了指令编码模式,确保生成的是有效的64位模式指令
- 添加了更多的寻址模式变体
- 添加了错误检查
- 确保指令正确编码并插入
这些修改应该能够增加 LDMXCSR 和 IDIV 指令在生成程序中的出现频率。如果还是没有效果,可能需要:
- 进一步增加权重
- 检查程序其他部分是否有限制这些指令生成的逻辑
- 确认指令模式是否完全符合目标架构的要求
- 添加日志来追踪指令生成的过程
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.