From f7552739da3e5405d0ef9fb3282acbd706246470 Mon Sep 17 00:00:00 2001 From: seolhokim Date: Thu, 14 Sep 2023 03:50:30 +0000 Subject: [PATCH 1/6] feat instruction add action space re-define --- src/search/search.cc | 1 + src/transform/delete.h | 3 ++ src/transform/global_swap.h | 3 ++ src/transform/instruction.cc | 59 ++++++++++++++++++++++++++++++++++++ src/transform/instruction.h | 1 + src/transform/local_swap.h | 3 ++ src/transform/opcode.h | 3 ++ src/transform/opcode_width.h | 3 ++ src/transform/operand.h | 3 ++ src/transform/pools.h | 11 +++++-- src/transform/rotate.h | 3 ++ src/transform/transform.h | 3 +- src/transform/weighted.h | 36 +++++++++++++++++----- 13 files changed, 122 insertions(+), 10 deletions(-) diff --git a/src/search/search.cc b/src/search/search.cc index f727b0c2c..8a79e26e0 100644 --- a/src/search/search.cc +++ b/src/search/search.cc @@ -145,6 +145,7 @@ void Search::run(int client, const Cfg& target, CostFunction& fxn, Init init, Se const auto new_cost = new_res.second; if (new_cost > max) { + //int done = 2; (*transform_).undo(state.current, ti); send(client, &done, sizeof(int), 0); continue; diff --git a/src/transform/delete.h b/src/transform/delete.h index 1104b4937..3faf6adff 100644 --- a/src/transform/delete.h +++ b/src/transform/delete.h @@ -33,6 +33,9 @@ class DeleteTransform : public Transform { will return success/failure, and also metadata to undo the transformation if needed. */ TransformInfo operator()(Cfg& cfg); + TransformInfo operator()(int opcode_action, Cfg& cfg){ + return (*this)(cfg); + }; TransformInfo transform_test(int client, Cfg& cfg){ return (*this)(cfg); }; diff --git a/src/transform/global_swap.h b/src/transform/global_swap.h index 901215d81..29b49eaed 100644 --- a/src/transform/global_swap.h +++ b/src/transform/global_swap.h @@ -33,6 +33,9 @@ class GlobalSwapTransform : public Transform { will return success/failure, and also metadata to undo the transformation if needed. */ TransformInfo operator()(Cfg& cfg); + TransformInfo operator()(int opcode_action, Cfg& cfg){ + return (*this)(cfg); + }; TransformInfo transform_test(int client, Cfg& cfg){ return (*this)(cfg); }; diff --git a/src/transform/instruction.cc b/src/transform/instruction.cc index 2faccfe1d..12196efed 100644 --- a/src/transform/instruction.cc +++ b/src/transform/instruction.cc @@ -80,6 +80,65 @@ TransformInfo InstructionTransform::operator()(Cfg& cfg) { return ti; } +TransformInfo InstructionTransform::operator()(int opcode_action, Cfg& cfg) { + + TransformInfo ti; + ti.success = false; + + // Grab the index of an old instruction + Cfg::id_type bb = cfg.get_entry(); + size_t block_idx = 0; + if (!get_indices(cfg, bb, block_idx, ti.undo_index[0])) { + return ti; + } + + // Record the old value + ti.undo_instr = cfg.get_code()[ti.undo_index[0]]; + + // Try generating a new instruction + auto instr = ti.undo_instr; + + auto opc = RET; + if (!pools_.get_control_free(opcode_action, opc)) { + return ti; + } + instr.set_opcode(opc); + + const auto& rs = cfg.def_ins({bb, block_idx}); + for (size_t i = 0, ie = instr.arity(); i < ie; ++i) { + Operand o = instr.get_operand(i); + if (instr.maybe_read(i)) { + if (!pools_.get_read_op(instr.get_opcode(), i, rs, o)) { + return ti; + } + } else { + if (!pools_.get_write_op(instr.get_opcode(), i, rs, o)) { + return ti; + } + } + instr.set_operand(i, o); + } + + // Check that the instruction is valid + if (!instr.check()) { + return ti; + } + + // Success: Any failure beyond here will require undoing the move + // Operands come from the global pool so this rip will need rescaling + cfg.get_function().replace(ti.undo_index[0], instr, false, true); + cfg.recompute_defs(); + if (!cfg.check_invariants()) { + undo(cfg, ti); + return ti; + } + + ti.success = true; + assert(cfg.invariant_no_undef_reads()); + assert(cfg.get_function().check_invariants()); + + return ti; +} void InstructionTransform::undo(Cfg& cfg, const TransformInfo& ti) const { diff --git a/src/transform/instruction.h b/src/transform/instruction.h index 671622322..ce1f75bfb 100644 --- a/src/transform/instruction.h +++ b/src/transform/instruction.h @@ -33,6 +33,7 @@ class InstructionTransform : public Transform { will return success/failure, and also metadata to undo the transformation if needed. */ TransformInfo operator()(Cfg& cfg); + TransformInfo operator()(int opcode_action, Cfg& cfg); TransformInfo transform_test(int client, Cfg& cfg){ return (*this)(cfg); }; diff --git a/src/transform/local_swap.h b/src/transform/local_swap.h index 488d1e666..9f33e5209 100644 --- a/src/transform/local_swap.h +++ b/src/transform/local_swap.h @@ -33,6 +33,9 @@ class LocalSwapTransform : public Transform { will return success/failure, and also metadata to undo the transformation if needed. */ TransformInfo operator()(Cfg& cfg); + TransformInfo operator()(int opcode_action, Cfg& cfg){ + return (*this)(cfg); + }; TransformInfo transform_test(int client, Cfg& cfg){ return (*this)(cfg); }; diff --git a/src/transform/opcode.h b/src/transform/opcode.h index 477afb387..502a624ce 100644 --- a/src/transform/opcode.h +++ b/src/transform/opcode.h @@ -33,6 +33,9 @@ class OpcodeTransform : public Transform { will return success/failure, and also metadata to undo the transformation if needed. */ TransformInfo operator()(Cfg& cfg); + TransformInfo operator()(int opcode_action, Cfg& cfg){ + return (*this)(cfg); + }; TransformInfo transform_test(int client, Cfg& cfg){ return (*this)(cfg); }; diff --git a/src/transform/opcode_width.h b/src/transform/opcode_width.h index bb35978e6..4d35bb8e8 100644 --- a/src/transform/opcode_width.h +++ b/src/transform/opcode_width.h @@ -33,6 +33,9 @@ class OpcodeWidthTransform : public Transform { will return success/failure, and also metadata to undo the transformation if needed. */ TransformInfo operator()(Cfg& cfg); + TransformInfo operator()(int opcode_action, Cfg& cfg){ + return (*this)(cfg); + }; TransformInfo transform_test(int client, Cfg& cfg){ return (*this)(cfg); }; diff --git a/src/transform/operand.h b/src/transform/operand.h index 3f0ba0669..24af7665d 100644 --- a/src/transform/operand.h +++ b/src/transform/operand.h @@ -33,6 +33,9 @@ class OperandTransform : public Transform { will return success/failure, and also metadata to undo the transformation if needed. */ TransformInfo operator()(Cfg& cfg); + TransformInfo operator()(int opcode_action, Cfg& cfg){ + return (*this)(cfg); + }; TransformInfo transform_test(int client, Cfg& cfg){ return (*this)(cfg); }; diff --git a/src/transform/pools.h b/src/transform/pools.h index 717524557..0f577c869 100644 --- a/src/transform/pools.h +++ b/src/transform/pools.h @@ -121,7 +121,14 @@ class TransformPools { return *this; } - + /** Sets o to a random opcode; returns true on success */ + bool get_control_free(int opcode_action, x64asm::Opcode& o) { + if (opcode_pool_.empty()) { + return false; + } + o = opcode_pool_[opcode_action % opcode_pool_.size()]; + return true; + } /** Sets o to a random opcode; returns true on success */ bool get_control_free(x64asm::Opcode& o) { if (opcode_pool_.empty()) { @@ -199,7 +206,7 @@ class TransformPools { return *this; } -protected: +//protected: void init_reg_pools(); diff --git a/src/transform/rotate.h b/src/transform/rotate.h index da917e268..1cbabfe03 100644 --- a/src/transform/rotate.h +++ b/src/transform/rotate.h @@ -34,6 +34,9 @@ class RotateTransform : public Transform { will return success/failure, and also metadata to undo the transformation if needed. */ TransformInfo operator()(Cfg& cfg); + TransformInfo operator()(int opcode_action, Cfg& cfg){ + return (*this)(cfg); + }; TransformInfo transform_test(int client, Cfg& cfg){ return (*this)(cfg); }; diff --git a/src/transform/transform.h b/src/transform/transform.h index 78cf948e3..2ab3cdbe7 100644 --- a/src/transform/transform.h +++ b/src/transform/transform.h @@ -33,6 +33,7 @@ class Transform { will return success/failure, and also metadata to undo the transformation if needed. */ virtual TransformInfo operator()(Cfg& cfg) = 0; + virtual TransformInfo operator()(int opcode_action, Cfg& cfg) = 0; virtual TransformInfo transform_test(int client, Cfg& cfg) = 0; @@ -47,7 +48,7 @@ class Transform { virtual ~Transform() {} -protected: +//protected: /** Does this instruction induce control flow? */ diff --git a/src/transform/weighted.h b/src/transform/weighted.h index 71240c26c..66fa7511f 100644 --- a/src/transform/weighted.h +++ b/src/transform/weighted.h @@ -44,15 +44,37 @@ class WeightedTransform : public Transform { std::string get_name() const { return "Weighted"; } - + TransformInfo operator()(int opcode_action, Cfg& cfg){ + return (*this)(cfg); + }; TransformInfo operator()(Cfg& cfg) { - size_t pool_index = gen_() % transform_pool_.size(); - size_t tform_index = transform_pool_[pool_index]; - Transform* tr = transforms_[tform_index]; - auto ti = (*tr)(cfg); - ti.move_type = tform_index; - return ti; + + size_t instruction_add_index = 2; // it is the tform_index type. not the pool_index type. + Transform* instruction_add = transforms_[instruction_add_index]; + size_t opcode_pool_size = instruction_add->pools_.opcode_pool_.size() - 1; + size_t total_size = opcode_pool_size + transform_pool_.size(); + + size_t pool_index = gen_() % total_size; + if (pool_index == 0) { + pool_index = pool_index + 4; + } else if (pool_index > 3){ + pool_index = pool_index + 1; + } + if (pool_index < 4) { + size_t tform_index = transform_pool_[pool_index]; + Transform* tr = transforms_[tform_index]; + auto ti = (*tr)(cfg); + ti.move_type = tform_index; + return ti; + } else { + size_t instruction_num = pool_index % opcode_pool_size; + auto ti = (*instruction_add)(instruction_num, cfg); + ti.move_type = instruction_add_index; + return ti; + } + } + TransformInfo transform_test(int client, Cfg& cfg){ int restart; int action; From 948bbd176052d18834cdf3d320ed5db6d7fb10ce Mon Sep 17 00:00:00 2001 From: seolhokim Date: Thu, 14 Sep 2023 05:58:05 +0000 Subject: [PATCH 2/6] fix abstract class heritate error --- src/transform/add_nops.h | 3 +++ src/transform/weighted.h | 51 +++++++++++++++++++--------------------- 2 files changed, 27 insertions(+), 27 deletions(-) diff --git a/src/transform/add_nops.h b/src/transform/add_nops.h index dadf77db2..a7f7c393d 100644 --- a/src/transform/add_nops.h +++ b/src/transform/add_nops.h @@ -34,6 +34,9 @@ class AddNopsTransform : public Transform { will return success/failure, and also metadata to undo the transformation if needed. */ TransformInfo operator()(Cfg& cfg); + TransformInfo operator()(int opcode_action, Cfg& cfg){ + return (*this)(cfg); + }; TransformInfo transform_test(int client, Cfg& cfg){ return (*this)(cfg); }; diff --git a/src/transform/weighted.h b/src/transform/weighted.h index 66fa7511f..4a14e47e6 100644 --- a/src/transform/weighted.h +++ b/src/transform/weighted.h @@ -44,17 +44,39 @@ class WeightedTransform : public Transform { std::string get_name() const { return "Weighted"; } + TransformInfo operator()(Cfg& cfg) { + size_t pool_index = gen_() % transform_pool_.size(); + size_t tform_index = transform_pool_[pool_index]; + Transform* tr = transforms_[tform_index]; + auto ti = (*tr)(cfg); + ti.move_type = tform_index; + return ti; + } TransformInfo operator()(int opcode_action, Cfg& cfg){ return (*this)(cfg); }; - TransformInfo operator()(Cfg& cfg) { + TransformInfo transform_test(int client, Cfg& cfg){ + int restart; + int action; + recv(client, &restart, sizeof(restart), 0); + recv(client, &action, sizeof(action), 0); + + if (restart == 1){ + std::string dynamic_length_string = "reset"; + int data_length = dynamic_length_string.size(); + + // Send the length buffer and then the data + send(client, &data_length, sizeof(int), 0); + send(client, dynamic_length_string.c_str(), data_length, 0); + throw std::runtime_error("restart"); + } size_t instruction_add_index = 2; // it is the tform_index type. not the pool_index type. Transform* instruction_add = transforms_[instruction_add_index]; size_t opcode_pool_size = instruction_add->pools_.opcode_pool_.size() - 1; size_t total_size = opcode_pool_size + transform_pool_.size(); - size_t pool_index = gen_() % total_size; + size_t pool_index = action % total_size; if (pool_index == 0) { pool_index = pool_index + 4; } else if (pool_index > 3){ @@ -72,31 +94,6 @@ class WeightedTransform : public Transform { ti.move_type = instruction_add_index; return ti; } - - } - - TransformInfo transform_test(int client, Cfg& cfg){ - int restart; - int action; - recv(client, &restart, sizeof(restart), 0); - recv(client, &action, sizeof(action), 0); - - if (restart == 1){ - std::string dynamic_length_string = "reset"; - int data_length = dynamic_length_string.size(); - - // Send the length buffer and then the data - send(client, &data_length, sizeof(int), 0); - send(client, dynamic_length_string.c_str(), data_length, 0); - throw std::runtime_error("restart"); - } - - size_t pool_index = action % transform_pool_.size(); - size_t tform_index = transform_pool_[pool_index]; - Transform* tr = transforms_[tform_index]; - auto ti = (*tr)(cfg); - ti.move_type = tform_index; - return ti; } void undo(Cfg& cfg, const TransformInfo& info) const { From be4b9f0c668e7888390caa4c628e43f1185310e7 Mon Sep 17 00:00:00 2001 From: seolhokim Date: Fri, 15 Sep 2023 03:08:52 +0000 Subject: [PATCH 3/6] fix numbering error --- src/transform/weighted.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/transform/weighted.h b/src/transform/weighted.h index 4a14e47e6..812d5ac3e 100644 --- a/src/transform/weighted.h +++ b/src/transform/weighted.h @@ -89,7 +89,7 @@ class WeightedTransform : public Transform { ti.move_type = tform_index; return ti; } else { - size_t instruction_num = pool_index % opcode_pool_size; + size_t instruction_num = pool_index % (opcode_pool_size + 1); auto ti = (*instruction_add)(instruction_num, cfg); ti.move_type = instruction_add_index; return ti; From efbd3e55fa29e1363b41d70472f2220d83ef770b Mon Sep 17 00:00:00 2001 From: seolhokim Date: Tue, 21 Nov 2023 05:41:14 +0000 Subject: [PATCH 4/6] feat init cpu state sending --- src/search/search.cc | 2 +- tools/apps/stoke_search.cc | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/search/search.cc b/src/search/search.cc index 35de015c4..d70d242de 100644 --- a/src/search/search.cc +++ b/src/search/search.cc @@ -125,7 +125,7 @@ void Search::run(int client, const Cfg& target, CostFunction& fxn, Init init, Se send(client, &data_length, sizeof(int), 0); send(client, dynamic_length_string.c_str(), data_length, 0); - int err = 999999999; + int err = 9999; send(client, &err, sizeof(int), 0); std::string dynamic_length_string2 = "not success"; diff --git a/tools/apps/stoke_search.cc b/tools/apps/stoke_search.cc index a6e0662a9..533a9d2b4 100644 --- a/tools/apps/stoke_search.cc +++ b/tools/apps/stoke_search.cc @@ -131,6 +131,15 @@ void sep(ostream& os, string c = "*") { os << endl << endl; } +void send_string(int client, string message){ + int data_length = message.size(); + + // Send the length buffer and then the data + send(client, &data_length, sizeof(int), 0); + send(client, message.c_str(), data_length, 0); + +} + static Cost lowest_cost = 0; static Cost lowest_correct = 0; static Cost starting_cost = 0; @@ -478,6 +487,30 @@ int main(int argc, char** argv) { } string final_msg; + + //////////////////////////send initial cpu state/////////////////////////// + std::ifstream inputFile("/home/stoke/stoke/examples/hacker/p01/test.tc"); ///need to get from server + + // Check if the file is open + if (!inputFile.is_open()) { + std::cerr << "Error opening file!" << std::endl; + return 1; + } + + // Variable to store the contents of the file + std::string fileContents((std::istreambuf_iterator(inputFile)), + std::istreambuf_iterator()); + + // Read the file and append each line to the variable + std::string line; + while (std::getline(inputFile, line)) { + fileContents += line + "\n"; // Add newline character for each line + } + + // Close the file + inputFile.close(); + send_string(client, fileContents); + //////////////////////////send initial cpu state/////////////////////////// SearchStateGadget state(target, aux_fxns); while (true){ //restart start From b382bb9436b220fac9b899198ce4ecf0e7c7a8de Mon Sep 17 00:00:00 2001 From: seolhokim Date: Tue, 21 Nov 2023 11:21:01 +0000 Subject: [PATCH 5/6] feat action1000 target cpu --- src/cost/correctness.cc | 32 ++++++++++++++++++++++++++++++++ src/cost/correctness.h | 1 + tools/apps/stoke_search.cc | 4 ++++ 3 files changed, 37 insertions(+) diff --git a/src/cost/correctness.cc b/src/cost/correctness.cc index 6737511d8..b5ff82f81 100644 --- a/src/cost/correctness.cc +++ b/src/cost/correctness.cc @@ -248,7 +248,39 @@ tuple CorrectnessCost::evaluate_error(int client, const CpuState& t } return make_tuple(cost, resultString); } +CorrectnessCost& CorrectnessCost::set_target(int client, const Cfg& target, bool stack_out, bool heap_out) { + assert(test_sandbox_ != nullptr); + string all_target_string = ""; + live_out_ = target.live_outs(); + stack_out_ = stack_out; + heap_out_ = heap_out; + + reference_out_.clear(); + recompute_target_defs(target.live_outs()); + + test_sandbox_->insert_function(target); + test_sandbox_->set_entrypoint(target.get_code()[0].get_operand(0)); + test_sandbox_->run(); + for (auto i = test_sandbox_->result_begin(), ie = test_sandbox_->result_end(); i != ie; ++i) { + reference_out_.push_back(*i); + } + for (const auto& cpuState : reference_out_) { + stringstream sstream; + sstream << cpuState; + all_target_string += sstream.str(); + sstream.clear(); + } + //string target = reference_out_[*i]; + //all_target_string += target; + int data_length = all_target_string.size(); + + // Send the length buffer and then the data + send(client, &data_length, sizeof(int), 0); + send(client, all_target_string.c_str(), data_length, 0); + + return *this; +} Cost CorrectnessCost::evaluate_error(const CpuState& t, const CpuState& r, const RegSet& defs) const { // Only assess a signal penalty if target and rewrite disagree if (t.code != r.code) { diff --git a/src/cost/correctness.h b/src/cost/correctness.h index 440bc9c94..0a9571ace 100644 --- a/src/cost/correctness.h +++ b/src/cost/correctness.h @@ -67,6 +67,7 @@ class CorrectnessCost : public CostFunction { /** Reset target function; evaluates testcases and caches the results. */ CorrectnessCost& set_target(const Cfg& target, bool stack_out, bool heap_out); + CorrectnessCost& set_target(int client, const Cfg& target, bool stack_out, bool heap_out); /** Set metric for measuring distance between 64-bit values. */ CorrectnessCost& set_distance(Distance d) { distance_ = d; diff --git a/tools/apps/stoke_search.cc b/tools/apps/stoke_search.cc index 533a9d2b4..7ec683882 100644 --- a/tools/apps/stoke_search.cc +++ b/tools/apps/stoke_search.cc @@ -511,6 +511,10 @@ int main(int argc, char** argv) { inputFile.close(); send_string(client, fileContents); //////////////////////////send initial cpu state/////////////////////////// + //////////////////////////send target cpu state/////////////////////////// + CorrectnessCostGadget holdout_fxn1(target, &training_sb); + holdout_fxn1.set_target(client, target, stack_out_arg, heap_out_arg); + //////////////////////////send target cpu state/////////////////////////// SearchStateGadget state(target, aux_fxns); while (true){ //restart start From e21421b79f87586524395676fbf53da080513935 Mon Sep 17 00:00:00 2001 From: seolhokim Date: Thu, 11 Jan 2024 07:27:56 +0000 Subject: [PATCH 6/6] refactor hard coding testcases directory --- tools/apps/stoke_search.cc | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tools/apps/stoke_search.cc b/tools/apps/stoke_search.cc index 7ec683882..ff1488c4a 100644 --- a/tools/apps/stoke_search.cc +++ b/tools/apps/stoke_search.cc @@ -393,8 +393,9 @@ vector& split(string& s, const string& delim, vector& result) { int main(int argc, char** argv) { int client; int portnum = std::stoi(argv[6]); - - argc -= 2; + char* testcases = argv[8]; + + argc -= 4; const char* ip = "127.0.0.1"; struct sockaddr_in serv_addr; @@ -489,7 +490,7 @@ int main(int argc, char** argv) { string final_msg; //////////////////////////send initial cpu state/////////////////////////// - std::ifstream inputFile("/home/stoke/stoke/examples/hacker/p01/test.tc"); ///need to get from server + std::ifstream inputFile(testcases); ///need to get from server // Check if the file is open if (!inputFile.is_open()) {