void CodeGen::Visit(FuncDefNode* _node) { auto c_state = state; int var_id = -1; if (_node->name) var_id = state->AddVariable(_node->name->name); auto new_state = GenState::CreateNewState(state); state = new_state; for (auto i = _node->parameters.begin(); i != _node->parameters.end(); ++i) Visit(*i); Visit(_node->block); AddInst(Instruction(VM_CODE::RETURN, 0)); state = new_state->GetPrevState(); auto new_fun = Context::GetGC()->New<Function>(GenState::GenerateCodePack(new_state)); delete new_state; int const_id = state->AddConstant(new_fun->toValue()); AddInst(Instruction(VM_CODE::LOAD_C, const_id)); AddInst(Instruction(VM_CODE::CLOSURE, 0)); if (var_id >= 0) AddInst(Instruction(VM_CODE::STORE_V, var_id)); }
void CodeGen::Visit(InvokeExprNode* _node) { Visit(_node->source); auto _name_id = state->AddConstant( String::FromU16String(_node->id->name)->toValue()); AddInst(Instruction(VM_CODE::LOAD_C, _name_id)); AddInst(Instruction(VM_CODE::DOT, 0)); }
void skipPushData(code_iterator& _curr, code_iterator _end) { auto pushInst = *_curr; assert(Instruction(pushInst) >= Instruction::PUSH1 && Instruction(pushInst) <= Instruction::PUSH32); auto numBytes = pushInst - static_cast<size_t>(Instruction::PUSH1) + 1; --_end; for (decltype(numBytes) i = 0; i < numBytes && _curr < _end; ++i, ++_curr) {} }
void CodeGen::Visit(ReturnStmtNode* _node) { if (_node->expression) { Visit(_node->expression); AddInst(Instruction(VM_CODE::RETURN, 1)); } else AddInst(Instruction(VM_CODE::RETURN, 0)); }
void CodeGen::Visit(FuncCallNode* _node) { for (auto i = _node->parameters.begin(); i != _node->parameters.end(); ++i) Visit(*i); AddInst(Instruction(VM_CODE::PUSH_NULL, 0)); // Push This Visit(_node->exp); AddInst(Instruction(VM_CODE::CALL, _node->parameters.size())); }
/* void expandPair() * * The purpose of this function is to replace an instruction that * contains a list-head pair with a series of instructions, one for each * expression in the list * * Notes: * 1. the getChildren method of Instruction just retrieves the children * if the instruction points to a pair. This function has to do all * the rest of the necessary bookkeeping (i.e. putting in OPCODE_STARTLIST, * OPCODE_ENDLIST, etc) * 2. Instruction::getChildren automatically propagates the Environment * of the parent to the children. * 3. Calling instructions.splice actually removes all elements from the * temporary "tl" list, but any iterators pointing to any elements of it * remain valid. Since after the function is over, we want IP to point to * the first element in "tl", we obtain an iterator pointing to it before * we do the splice. */ void Machine::expandPair() { list<Instruction> tl = IP->getChildren(); list<Instruction>::iterator it; it = tl.begin(); tl.push_front(Instruction(OPCODE_STARTLIST)); tl.push_back(Instruction(OPCODE_ENDLIST)); it = tl.begin(); IP = instructions.erase(IP); instructions.splice(IP, tl); IP = it; }
void push_close() { int open = pcstack.top(); int diff = insns->size() - open; (*insns)[open].value = diff; insns->push_back(Instruction(CLOSE, diff + 1)); pcstack.pop(); }
/*Start Up Screen*/ bool dope::Start() { for (int i = 0; i < 80; i++) { cout << '\xb1'; } SetConsoleTextAttribute(GetStdHandle (STD_OUTPUT_HANDLE), 3); cout << '\xb1' << "\t\t\t\tVERSION #1\t\t\t\t " << '\xb1'; for (int j = 0; j < 80; j++) { cout << '\xb1'; } cout << endl << endl; init(); cout << "\n\n\n\n\n\n\n\t\t1 = Start\t2 = Instructions\t3 = Quit: "; cin >> decision; if (decision == 1) { Adventure(); } else if (decision == 2) { Instruction(); } else { exit(3); } return 0; }
void CodeGen::Visit(StringNode* _node) { auto index = state->AddConstant( Value(String::FromU16String(_node->content), TypeId::String)); AddInst(Instruction(VM_CODE::LOAD_C, index)); }
llvm::APInt readPushData(code_iterator& _curr, code_iterator _end) { auto pushInst = *_curr; assert(Instruction(pushInst) >= Instruction::PUSH1 && Instruction(pushInst) <= Instruction::PUSH32); auto numBytes = pushInst - static_cast<size_t>(Instruction::PUSH1) + 1; llvm::APInt value(256, 0); ++_curr; // Point the data for (decltype(numBytes) i = 0; i < numBytes; ++i) { byte b = (_curr != _end) ? *_curr++ : 0; value <<= 8; value |= b; } --_curr; // Point the last real byte read return value; }
scriptID LoadCompiledScript(const std::string& scriptFile) { std::unordered_map<std::string, scriptID>::iterator scriptIter = loadedScripts.find(scriptFile); if (scriptIter != loadedScripts.end()) { return loadedScripts[scriptFile]; } std::fstream file; size_t dataSize; unsigned char code; unsigned char data[256]; file.open(scriptFile, std::ios::in | std::ios::binary); scriptID newscriptID = scripts.size(); scripts.push_back(Script()); Script& newScript = scripts.back(); size_t scriptSize; file.read((char*)&scriptSize, sizeof(size_t)); newScript.reserve(scriptSize); for(size_t i = 0; i < scriptSize; ++i) { file.read((char*) &dataSize, sizeof(size_t)); file.read((char*) &code, sizeof(unsigned char)); file.read((char*) data, sizeof(unsigned char) * dataSize); newScript.push_back( Instruction(code, &data[0], dataSize) ); } loadedScripts[scriptFile] = newscriptID; return newscriptID; }
// public Methods Instruction Decoder::buildInstruction(instr ins){ InstructionData* id = resolver.getInstructionData(ins); if(id == NULL){ //TODO: fix Instruction jnk = buildInstruction(0x0); return jnk; } string asmString; int32_t argumentValues[NUMBER_OF_INSTRUCTION_PARAMETERS]; if(id->isDecodedNormally()){ //get argument values for(int i=0; i<id->getNumParameters(); i++){ bitrange br = id->getParameterBitrange(i); int32_t argVal = decodeArgumentToValue(ins, id, i); argumentValues[i] = argVal; } for(int i=id->getNumParameters(); i<NUMBER_OF_INSTRUCTION_PARAMETERS; i++){ argumentValues[i] = 0; } //decode normal instr asmString = decodeInstruction(id, ins); }else{ //decode abnormal instr asmString = decodeAbnormalInstruction(id, ins, argumentValues); } Instruction instruction = Instruction(id, asmString, ins, argumentValues); return instruction; }
/* * Instrumentation-time routine looking for the routine we'd like to instrument. */ static VOID ImageLoad(IMG img, VOID * v) { if (IMG_IsMainExecutable(img)) { *outFile << "In main application image" << endl; // Search for the assembly routine in the application RTN AsmRtn = RTN_FindByName(img, "operImmCmds"); if (!RTN_Valid(AsmRtn)) { AsmRtn = RTN_FindByName(img, "_operImmCmds"); } if (RTN_Valid(AsmRtn)) { *outFile << "Function operImmCmds found" << endl; RTN_Open(AsmRtn); // Go over each of the routine's instructions for (INS ins = RTN_InsHead(AsmRtn); INS_Valid(ins); ins = INS_Next(ins)) { Instruction(ins, 0); } RTN_Close(AsmRtn); *outFile << "Done with function operImmCmds" << endl; } else { *outFile << "Function operImmCmds not found!" << endl; } } }
std::multiset<Gadget*> BeaRopGadgetFinder::find_rop_gadgets(const unsigned char* data, unsigned long long size, unsigned long long vaddr) { std::multiset<Gadget*> merged_gadgets; DISASM dis; init_disasm_struct(&dis); for(unsigned long long offset = 0; offset < size; ++offset) { dis.EIP = (UIntPtr)(data + offset); dis.VirtualAddr = SafeAddU64(vaddr, offset); dis.SecurityBlock = (UInt32)(size - offset + 1); int len = Disasm(&dis); /* I guess we're done ! */ if(len == OUT_OF_BLOCK) break; /* OK this one is an unknow opcode, goto the next one */ if(len == UNKNOWN_OPCODE) continue; if(is_valid_ending_instruction(&dis)) { DISASM ret_instr; /* Okay I found a RET ; now I can build the gadget */ memcpy(&ret_instr, &dis, sizeof(DISASM)); /* Do not forget to add the ending instruction only -- we give to the user all gadget with < depth instruction */ std::list<Instruction> only_ending_instr; only_ending_instr.push_back(Instruction( std::string(ret_instr.CompleteInstr), std::string(ret_instr.Instruction.Mnemonic), offset, len )); Gadget *gadget_with_one_instr = new (std::nothrow) Gadget(); if(gadget_with_one_instr == NULL) RAISE_EXCEPTION("Cannot allocate gadget_with_one_instr"); /* the gadget will only have 1 ending instruction */ gadget_with_one_instr->add_instructions(only_ending_instr, vaddr); merged_gadgets.insert(gadget_with_one_instr); /* if we want to see gadget with more instructions */ if(m_depth > 0) { std::multiset<Gadget*> gadgets = find_all_gadget_from_ret(data, vaddr, &ret_instr, len); for(std::multiset<Gadget*>::iterator it = gadgets.begin(); it != gadgets.end(); ++it) merged_gadgets.insert(*it); } } } return merged_gadgets; }
void Generator::generate_double_arithmetic(LexemeType t) { switch (t) { case plus_op: push(Instruction(cmd_faddp)); break; case minus_op: push(Instruction(cmd_fsubp)); break; case mul_op: push(Instruction(cmd_fmulp)); break; case div_op: push(Instruction(cmd_fdivp)); break; } }
Function* CodeGen::generate(Parser* p) { parser = p; Visit(parser->getRoot()); AddInst(Instruction(VM_CODE::STOP, 0)); return Context::GetGC()->New<Function>(GenState::GenerateCodePack(state)); }
void LiftRightExecutor::SensorDriverCallback(int _id, bool _detected){ std::cout << "LIRT RIGHT" << std::endl; LiftProgressNotification* liftNotification = new LiftProgressNotification(LiftProgressNotification::ProgressType::SENSOR_TRIGGERED); sendNotification(liftNotification); Command* cmd = ActuatorAction::LiftRight(CALLBACK_GET_RIGHT); commandQueueLock.lock(); commandsToProcess.push(Instruction(cmd)); commandQueueLock.unlock(); queueNotEmpty.notify_one(); }
void VM::fetchInstruction() { m_OP = Instruction(m_code[m_PC]); auto const metric = c_metrics[static_cast<size_t>(m_OP)]; adjustStack(metric.num_stack_arguments, metric.num_stack_returned_items); // FEES... m_runGas = metric.gas_cost; m_newMemSize = m_mem.size(); m_copyMemSize = 0; }
void CodeGen::Visit(NumberNode* _node) { unsigned int index; if (_node->maybeInt) index = state->AddConstant(Value(static_cast<TSmallInt>(_node->number))); else index = state->AddConstant(Value(_node->number)); AddInst(Instruction(VM_CODE::LOAD_C, index)); }
void CodeGen::Visit(IdentifierNode* _node) { auto _var = FindVar(state, _node->name); switch (_var.type()) { case VarType::TYPE::GLOBAL: AddInst(Instruction(VM_CODE::LOAD_G, _var.id())); break; case VarType::TYPE::LOCAL: AddInst(Instruction(VM_CODE::LOAD_V, _var.id())); break; case VarType::TYPE::UPVAL: AddInst(Instruction(VM_CODE::LOAD_UPVAL, _var.id())); break; case VarType::TYPE::NONE: // ReportError(std::u16string(u"<Identifier>Variables not found: ") + utils::utf8_to_utf16(_node->name)); break; } }
void CodeGen::Visit(WhileStmtNode* _node) { auto new_state = GenState::CreateEqualState(state); state = new_state; auto _def_vs = _while_statement; auto _def_break_loc = _break_loc; auto _def_continue_loc = this->_continue_loc; _while_statement = true; _break_loc = -1; this->_continue_loc = -1; auto _begin_loc = state->GetInstructionVector()->size(); Visit(_node->condition); auto _condition_loc = state->GetInstructionVector()->size(); state->AddInstruction(VM_CODE::IFNO, 0); Visit(_node->child); state->AddInstruction(VM_CODE::JMP, -1 * (state->GetInstructionVector()->size() - _begin_loc)); (*state->GetInstructionVector())[_condition_loc] = Instruction(VM_CODE::IFNO, state->GetInstructionVector()->size() - _condition_loc); if (_break_loc >= 0) (*state->GetInstructionVector())[_break_loc] = Instruction(VM_CODE::JMP, state->GetInstructionVector()->size() - _break_loc); if (this->_continue_loc >= 0) (*state->GetInstructionVector())[this->_continue_loc] = Instruction(VM_CODE::JMP, _begin_loc - this->_continue_loc); _break_loc = _def_break_loc; _continue_loc = _def_continue_loc; _while_statement = _def_vs; state = state->GetPrevState(); delete new_state; }
void Processor::BranchTest() { if (Cycle - RCnt().nextsCounter >= RCnt().nextCounter) { RCnt().Update(); } // interrupt mask register ($1f801074) if (U32H_ref(0x1070) & U32H_ref(0x1074)) { if ((CP0.SR & 0x401) == 0x401) { Exception(Instruction(this, 0x400), false); } } }
void CodeGen::Visit(VarSubExprNode* _node) { auto _id_node = _node->varName; int _id = state->AddVariable(_id_node->name); // you must add the name first and then Visit the expression. // to generate the next code if (_node->expression) { Visit(_node->expression); AddInst(Instruction(VM_CODE::STORE_V, _id)); } }
void CodeGen::Visit(BlockExprNode* _node) { for (auto i = _node->children.begin(); i != _node->children.end(); ++i) { (*i)->Visit(this); // TODO: debug if ((*i)->asUnaryExpression() || (*i)->asBinaryExpression() || (*i)->asIdentifier() || (*i)->asNumber()) AddInst(Instruction(VM_CODE::OUT, 0)); } }
void initComputePseudoBlur() { vector<Instruction> a; a.push_back(Instruction(LOCAL, 0, LOCAL, 0, LOCAL, 0, VECTOR, XOR, 0, false)); // local_reg[0] := 0 a.push_back(Instruction(WINDOW, 0, COMMON, 0, LOCAL, 1, VECTOR, SHORT_MUL, 0, false)); // local_reg[1] := window[0] * common_reg[0] a.push_back(Instruction(LOCAL, 0, LOCAL, 1, LOCAL, 0, VECTOR, ADD, 0, false)); // local_reg[0] += local_reg[1] // a.push_back(Instruction(LOCAL, 0, LOCAL, 0, LOCAL, 0, VECTOR, PRINT, 0, false)); // debug print result a.push_back(Instruction(WINDOW, 1, COMMON, 1, LOCAL, 1, VECTOR, SHORT_MUL, 0, false)); // local_reg[1] := window[0] * common_reg[0] a.push_back(Instruction(LOCAL, 0, LOCAL, 1, LOCAL, 0, VECTOR, ADD, 0, false)); // local_reg[0] += local_reg[1] // a.push_back(Instruction(LOCAL, 0, LOCAL, 0, LOCAL, 0, VECTOR, PRINT, 0, false)); // debug print result a.push_back(Instruction(WINDOW, 2, COMMON, 2, LOCAL, 1, VECTOR, SHORT_MUL, 0, false)); // local_reg[1] := window[0] * common_reg[0] a.push_back(Instruction(LOCAL, 0, LOCAL, 1, LOCAL, 0, VECTOR, ADD, 0, false)); // local_reg[0] += local_reg[1] // a.push_back(Instruction(LOCAL, 0, LOCAL, 0, LOCAL, 0, VECTOR, PRINT, 0, false)); // debug print result a.push_back(Instruction(LOCAL, 2/*reductionStage*/, LOCAL, 0, LOCAL, 0, REDUCE, ADD, 0, false)); // add all components of the vector with two "reduction" additions a.push_back(Instruction(LOCAL, 3/*reductionStage*/, LOCAL, 0, LOCAL, 0, REDUCE, ADD, 0, false)); // a.push_back(Instruction(LOCAL, 0, LOCAL, 0, LOCAL, 0, VECTOR, PRINT, 0, true)); // debug print result computePseudoBlur = a; }
void CodeGen::Visit(AssignmentNode* _node) { // find if the var is exisits // if not exisits add a possition for it auto _id_node = dynamic_cast<IdentifierNode*>(_node->identifier); auto _var = FindVar(state, _id_node->name); int _id; switch(_var.type()) { case VarType::TYPE::GLOBAL: Visit(_node->expression); AddInst(Instruction(VM_CODE::STORE_G, _var.id())); AddInst(Instruction(VM_CODE::LOAD_G, _var.id())); break; case VarType::TYPE::LOCAL: Visit(_node->expression); AddInst(Instruction(VM_CODE::STORE_V, _var.id())); AddInst(Instruction(VM_CODE::LOAD_V, _var.id())); break; case VarType::TYPE::UPVAL: Visit(_node->expression); AddInst(Instruction(VM_CODE::STORE_UPVAL, _var.id())); AddInst(Instruction(VM_CODE::LOAD_UPVAL, _var.id())); break; case VarType::TYPE::NONE: if (_var_statement) { _id = state->AddVariable(_id_node->name); // you must add the name first and then Visit the expression. // to generate the next code Visit(_node->expression); AddInst(Instruction(VM_CODE::STORE_V, _id)); AddInst(Instruction(VM_CODE::LOAD_V, _id)); } else { // i don't know how to fix it, f**k you. // ReportError(std::u16string(u"<Assignment>Identifier not found: ") + _id_node->name); } break; } }
void CodeGen::Visit(IfStmtNode* _node) { auto new_state = GenState::CreateEqualState(state); state = new_state; int jmp_val; Visit(_node->condition); auto jmp_loc = state->AddInstruction(VM_CODE::IFNO, 1); Visit(_node->true_branch); auto true_finish_loc = state->AddInstruction(VM_CODE::JMP, 1); // if condition not ture, jmp to the right location jmp_val = state->GetInstructionVector()->size() - jmp_loc; (*state->GetInstructionVector())[jmp_loc] = Instruction(VM_CODE::IFNO, jmp_val); if (_node->false_branch) { Visit(_node->false_branch); jmp_val = state->GetInstructionVector()->size() - true_finish_loc; (*state->GetInstructionVector())[true_finish_loc] = Instruction(VM_CODE::JMP, jmp_val); } state = state->GetPrevState(); delete new_state; }
vector<StatementNode*> Parser::InstructionList() { if(IsInstruccion(currenttoken)) { StatementNode* inst=Instruction(); vector<StatementNode*> list=InstructionList(); list.insert(list.begin(),inst); return list; } else { vector<StatementNode*> list; return list; } }
void Generator::generate_setcc(LexemeType t, bool is_unsigned_cmp) { switch(t) { case lesser_equal: if (is_unsigned_cmp) push(Instruction(cmd_setbe, op_register, "al")); else push(Instruction(cmd_setle, op_register, "al")); break; case greater_equal: if (is_unsigned_cmp) push(Instruction(cmd_setae, op_register, "al")); else push(Instruction(cmd_setge, op_register, "al")); break; case equal: push(Instruction(cmd_sete, op_register, "al")); break; case not_equal: push(Instruction(cmd_setne, op_register, "al")); break; case greater: if (is_unsigned_cmp) push(Instruction(cmd_seta, op_register, "al")); else push(Instruction(cmd_setg, op_register, "al")); break; case lesser: if (is_unsigned_cmp) push(Instruction(cmd_setb, op_register, "al")); else push(Instruction(cmd_setl, op_register, "al")); break; } }
void HTokenizer::Tokenize(const DISASM *Instr, QList<Token_t> *Tok) { // clear current list Tok->clear(); //-- Address(Tok,Instr); //-- Label(Tok,Instr); Prefix(Tok,Instr); // get basic instruction like "mov, push, stosb" Instruction(Tok,Instr); // get arguments in temporary token-lists to place the comma between them if we need one QList<Token_t> Arg1, Arg2, Arg3; Argument(&Arg1,Instr,Instr->Argument1); Argument(&Arg2,Instr,Instr->Argument2); Argument(&Arg3,Instr,Instr->Argument3); // append the first argument *Tok << Arg1; // do we need a comma (makes sure, that tere is no comma between push and constant like // "push , 10h" if(Arg2.count()>0 && Arg1.count() >0){ Token_t tmp; tmp.Text = ", "; tmp.Type = T_CHAR; Tok->append(tmp); } // append second argument (can be empty) *Tok << Arg2; // do we need a comma ? if(Arg3.count()>0){ Token_t tmp; tmp.Text = ", "; tmp.Type = T_CHAR; Tok->append(tmp); } // append third argument (can be empty) *Tok << Arg3; }