// internally generate code blob // code blobs are normally page aligned, except in builtin sysmodules int ExefsCode::CreateCodeBlob(const u8* elf, bool is_page_aligned) { u8* text, *rodata, *data; safe_call(ParseElf(elf)); if (is_page_aligned) { safe_call(code_blob_.alloc(PageToSize(text_.page_num + rodata_.page_num + data_.page_num))); text = (code_blob_.data() + 0); rodata = (code_blob_.data() + PageToSize(text_.page_num)); data = (code_blob_.data() + PageToSize(text_.page_num + rodata_.page_num)); } else { safe_call(code_blob_.alloc(text_.file_size + rodata_.file_size + data_.file_size)); text = (code_blob_.data() + 0); rodata = (code_blob_.data() + text_.file_size); data = (code_blob_.data() + text_.file_size + rodata_.file_size); } memcpy(text, text_.data, text_.file_size); memcpy(rodata, rodata_.data, rodata_.file_size); memcpy(data, data_.data, data_.file_size); return 0; }
SCEDA_Vertex *SCEDA_dfs_iterator_next(SCEDA_DFSIterator *iter) { SCEDA_Vertex *v; safe_call(SCEDA_stack_pop(iter->to_visit, (void **)&v)); SCEDA_VertexSuccIterator succ; SCEDA_vertex_succ_iterator_init(v, &succ); while(SCEDA_vertex_succ_iterator_has_next(&succ)) { SCEDA_Vertex *w = SCEDA_vertex_succ_iterator_next(&succ); if(!SCEDA_hashset_contains(iter->visited, w)) { safe_call(SCEDA_stack_push(iter->to_visit, w)); safe_call(SCEDA_hashset_add(iter->visited, w)); } } SCEDA_vertex_succ_iterator_cleanup(&succ); return v; }
void __cdecl FakeCheat(jass::jstring_t cheat_str) { const char* cheat = jass::from_string(cheat_str); if (!cheat) { c_call<uint32_t>(RealCheat, cheat_str); return ; } std::string cheat_s = cheat; if (cheat_s.compare(0, 9, "exec-lua:") == 0) { cheat_s = cheat_s.substr(9); boost::algorithm::trim(cheat_s); if (cheat_s.size() >= 2 && cheat_s[0] == '"' && cheat_s[cheat_s.size() - 1] == '"') { cheat_s = cheat_s.substr(1, cheat_s.size() - 2); } lua_State* L = jass_state_s::instance().get(); lua_getglobal(L, "require"); lua_pushlstring(L, cheat_s.c_str(), cheat_s.size()); safe_call(L, 1, 1, true); } c_call<uint32_t>(RealCheat, cheat_str); }
jass::jstring_t __cdecl EXExecuteScript(jass::jstring_t script) { lua_State* L = jass_state_s::instance().get(); std::string str_script = format("return (%s)", jass::from_trigstring(jass::from_string(script))); if (luaL_loadbuffer(L, str_script.c_str(), str_script.size(), str_script.c_str()) != LUA_OK) { printf("%s\n", lua_tostring(L, -1)); lua_pop(L, 1); return 0; } if (LUA_OK != safe_call(L, 0, 1, true)) { return 0; } jass::jstring_t result = 0; if (lua_isstring(L, -1)) { result = jass::create_string(lua_tostring(L, -1)); } lua_pop(L, 1); return result; }
int main() try { // wrap the possibly throwing function in the safe_call() auto result = safe_call(throwing, "Life is good"); std::cerr << "It didn't throw: " << result.get() << "\n"; auto bad_result = safe_call(throwing, "Puke!\n"); if(!bad_result.has_value()) { std::cerr << "result has no value, and if you try to access it ... \n"; // It's going to throw now! std::cerr << bad_result.get(); } } catch(std::exception& s) { std::cerr << "\nCaught exception: " << s.what() << "\n"; }
void LuaCommand::run(Bot *b, CommandInfo *i) { /*if(b != _plugin->_bot) { throw CommandErrorException(_name, "NO"); }*/ lua_rawgeti(state, LUA_REGISTRYINDEX, cmd_ref); push_bot(state, b); push_command_info(state, i); safe_call(state, _name, 2, 0, 0); }
void LuaPlugin::init(PluginHost *h) { Bot *b = (Bot*)h; lua_rawgeti(state, LUA_REGISTRYINDEX, plugin_ref); get_table(state, "init"); push_bot(state, b); if(lua_isnil(state, -1)) { lua_pop(state, -1); throw CommandErrorException(_name, "did not provide plugin.init"); } safe_call(state, _name, 1, 0, 0); }
static void initialize_lua() { const char* buf = 0; size_t len = 0; if (storm_s::instance().load_file("script\\war3map.lua", (const void**)&buf, &len)) { lua_State* L = getMainL(); if (luaL_loadbuffer(L, buf, len, "@script\\war3map.lua") != LUA_OK) { printf("%s\n", lua_tostring(L, -1)); lua_pop(L, 1); storm_s::instance().unload_file(buf); return; } safe_call(L, 0, 0, true); storm_s::instance().unload_file(buf); } else { jass::table_hook("Cheat", (uintptr_t*)&RealCheat, (uintptr_t)FakeCheat); jass::japi_table_add((uintptr_t)EXExecuteScript, "EXExecuteScript", "(S)S"); } }
int PrerelocateSection(elf2fx2_cnvstruct_t* cs, word_t vsect, byte_t* sect, Elf32_Sym* symtab, Elf32_Rel* rels, int nrels) { int i; for(i = 0; i < nrels; i ++) { Elf32_Rel* rel = rels + i; word_t rinfo = eswap_word(rel->r_info); int rtype = ELF32_R_TYPE(rinfo); Elf32_Sym* rsym = symtab + ELF32_R_SYM(rinfo); hword_t rsymsect = eswap_hword(rsym->st_shndx); Elf32_Shdr* symsect = NULL; if ( !(rsymsect >= SHN_LORESERVE && rsymsect <= SHN_HIRESERVE) ) symsect = cs->sects + rsymsect; word_t rsymv = eswap_word(rsym->st_value); // The following is only valid for ET_REL ELF files //if(rsymsect < SHN_LORESERVE) // rsymv += eswap_word(symsect->sh_addr); word_t vtarget = /*vsect +*/ eswap_word(rel->r_offset); word_t* rtarget = (word_t*)(cs->loaddata + vtarget); word_t* rsymp = (word_t*)(cs->loaddata + rsymv); const char* symname = cs->symnames + rsym->st_name; if (symsect && strncmp(cs->sectnames + symsect->sh_name, ".imp.", 5) == 0 // It's in an import section && *symname && strncmp(symname, "__imp_", 6) != 0 // ...and it's *not* a direct import && *rsymp == 0) // ...and it's a dummy import pointer { // Add an import copy entry word_t v_rtarget = *rtarget; word_t offFromTrg = 0; if (rtype == R_ARM_ABS32 || rtype == R_ARM_TARGET1) { *rtarget = 0; offFromTrg = (word_t)((int)v_rtarget - (int)rsymv); }else if (rtype == R_ARM_TARGET2 || rtype == R_ARM_REL32) { *rtarget = 1; offFromTrg = (word_t)((int)v_rtarget - ((int)rsymv - (int)vtarget)); }else { printf("%d %s\n", rtype, symname); die("Non-supported type non-function imports are not supported!"); } word_t offTopBits = offFromTrg >> 28; if (offTopBits != 0x0 && offTopBits != 0xF) die("Offset from non-function import target too big!"); *rtarget |= offFromTrg << 4; AddImpCopy(rsymv, vtarget); continue; } /* printf("....REL type=%d, target=%08X, syma=%08X [%d]", rtype, vtarget, rsymv, rsym->st_other); if(*symname) printf(", symn=%s\n", symname); else printf("\n"); */ switch(rtype) { // Notes: // R_ARM_TARGET2 is equivalent to R_ARM_REL32 // R_ARM_PREL32 is an address-relative signed 31-bit offset case R_ARM_ABS32: case R_ARM_TARGET1: { // This relocation is the only one FXE2 supports if(vtarget & 3) die("Unaligned relocation!"); // Ignore undefined weak target symbols (keep them 0) if (ELF32_ST_BIND(rsym->st_info) == STB_WEAK && rsymv == 0) break; if(sect != cs->metadata) safe_call(AddRelocation(cs, vtarget)); break; } } }
/** Depth First Search */ void SCEDA_dfs_iterator_init(SCEDA_Vertex *v, SCEDA_DFSIterator *iter) { iter->to_visit = SCEDA_stack_create(NULL); iter->visited = SCEDA_vertex_set_create(); safe_call(SCEDA_stack_push(iter->to_visit, v)); safe_call(SCEDA_hashset_add(iter->visited, v)); }
/** Breadth First Search */ void SCEDA_bfs_iterator_init(SCEDA_Vertex *v, SCEDA_BFSIterator *iter) { iter->to_visit = SCEDA_queue_create(NULL); iter->visited = SCEDA_vertex_set_create(); safe_call(SCEDA_queue_enqueue(iter->to_visit, v)); safe_call(SCEDA_hashset_add(iter->visited, v)); }
int AssembleString(char* str, const char* initialFilename) { curFile = initialFilename; curLine = 1; ClearStatus(); int nextLineIncr = 0; char* nextStr = NULL; for (; str; str = nextStr, curLine += nextLineIncr) { size_t len = strcspn(str, "\n"); int linedelim = str[len]; str[len] = 0; nextStr = linedelim ? (str + len + 1) : NULL; nextLineIncr = linedelim == '\n' ? 1 : 0; char* line = trim_whitespace(remove_comment(str)); char* colonPos = NULL; for (;;) { colonPos = strchr(line, ':'); if (!colonPos) break; *colonPos = 0; char* labelName = line; line = trim_whitespace(colonPos + 1); if (!validateIdentifier(labelName)) return throwError("invalid label name: %s\n", labelName); std::pair<labelTableIter,bool> ret = g_labels.insert( std::pair<std::string,size_t>(labelName, BUF.size()) ); if (!ret.second) return throwError("duplicate label: %s\n", labelName); //printf("Label: %s\n", labelName); }; if (!*line) continue; if (*line == '#') { line = trim_whitespace(line + 1); nextLineIncr = 0; size_t pos = strcspn(line, " \t"); line[pos] = 0; curLine = atoi(line); line = trim_whitespace(line + pos + 1); if (*line == '"') { line ++; line[strlen(line)-1] = 0; } curFile = line; continue; } char* tok = mystrtok_spc(line); safe_call(ProcessCommand(tok)); } if (g_stackPos) return throwError("unclosed block(s)\n"); safe_call(FixupLabelRelocations()); return 0; }