Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/action #5

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/cost/correctness.cc
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,39 @@ tuple<Cost,string> 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<x64asm::Label>(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) {
Expand Down
1 change: 1 addition & 0 deletions src/cost/correctness.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/search/search.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
3 changes: 3 additions & 0 deletions src/transform/add_nops.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
Expand Down
3 changes: 3 additions & 0 deletions src/transform/delete.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
Expand Down
3 changes: 3 additions & 0 deletions src/transform/global_swap.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
Expand Down
59 changes: 59 additions & 0 deletions src/transform/instruction.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<R64>(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 {

Expand Down
1 change: 1 addition & 0 deletions src/transform/instruction.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
Expand Down
3 changes: 3 additions & 0 deletions src/transform/local_swap.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
Expand Down
3 changes: 3 additions & 0 deletions src/transform/opcode.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
Expand Down
3 changes: 3 additions & 0 deletions src/transform/opcode_width.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
Expand Down
3 changes: 3 additions & 0 deletions src/transform/operand.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
Expand Down
11 changes: 9 additions & 2 deletions src/transform/pools.h
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down Expand Up @@ -199,7 +206,7 @@ class TransformPools {
return *this;
}

protected:
//protected:

void init_reg_pools();

Expand Down
3 changes: 3 additions & 0 deletions src/transform/rotate.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
Expand Down
3 changes: 2 additions & 1 deletion src/transform/transform.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -47,7 +48,7 @@ class Transform {

virtual ~Transform() {}

protected:
//protected:


/** Does this instruction induce control flow? */
Expand Down
33 changes: 26 additions & 7 deletions src/transform/weighted.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ 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];
Expand All @@ -53,6 +52,9 @@ class WeightedTransform : public Transform {
ti.move_type = tform_index;
return ti;
}
TransformInfo operator()(int opcode_action, Cfg& cfg){
return (*this)(cfg);
};
TransformInfo transform_test(int client, Cfg& cfg){
int restart;
int action;
Expand All @@ -69,12 +71,29 @@ class WeightedTransform : public Transform {
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;
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 = action % 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 + 1);
auto ti = (*instruction_add)(instruction_num, cfg);
ti.move_type = instruction_add_index;
return ti;
}
}

void undo(Cfg& cfg, const TransformInfo& info) const {
Expand Down
42 changes: 40 additions & 2 deletions tools/apps/stoke_search.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -384,8 +393,9 @@ vector<string>& split(string& s, const string& delim, vector<string>& 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;
Expand Down Expand Up @@ -478,6 +488,34 @@ int main(int argc, char** argv) {
}

string final_msg;

//////////////////////////send initial cpu state///////////////////////////
std::ifstream inputFile(testcases); ///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<char>(inputFile)),
std::istreambuf_iterator<char>());

// 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///////////////////////////
//////////////////////////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
Expand Down