Skip to content

Commit

Permalink
fix: bad singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
Soulghost committed Jun 28, 2021
1 parent 44cf524 commit db7f614
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 36 deletions.
19 changes: 9 additions & 10 deletions iblessing/iblessing-core/core/memory/VirtualMemoryV2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "VirtualMemory.hpp"
#include <iblessing-core/v2/util/termcolor.h>
#include <iblessing-core/v2/util/StringUtils.h>
#include <iblessing-core/v2/mach-o/mach-o.hpp>
#include "mach-machine.h"
#include "ScannerContext.hpp"
#include "SymbolTable.hpp"
Expand Down Expand Up @@ -47,7 +48,7 @@ struct ib_dyld_info_command* VirtualMemoryV2::getDyldInfo() {
return fileMemory->dyldinfo;
}

int VirtualMemoryV2::loadWithMachOData(uint8_t *mappedFile) {
int VirtualMemoryV2::loadWithMachOData(shared_ptr<SymbolTable> symtab, shared_ptr<ObjcRuntime> objcRuntime, uint8_t *mappedFile) {
// init unicorn
if (this->uc) {
return 1;
Expand All @@ -59,10 +60,10 @@ int VirtualMemoryV2::loadWithMachOData(uint8_t *mappedFile) {
return 1;
}

return mappingMachOToEngine(uc, mappedFile);
return mappingMachOToEngine(symtab, objcRuntime, uc, mappedFile);
}

int VirtualMemoryV2::mappingMachOToEngine(uc_engine *uc, uint8_t *mappedFile) {
int VirtualMemoryV2::mappingMachOToEngine(shared_ptr<SymbolTable> symtab, shared_ptr<ObjcRuntime> objcRuntime, uc_engine *uc, uint8_t *mappedFile) {
if (!uc) {
return 1;
}
Expand Down Expand Up @@ -189,18 +190,16 @@ int VirtualMemoryV2::mappingMachOToEngine(uc_engine *uc, uint8_t *mappedFile) {
for (pair<uint64_t, uint32_t> patch : textPatch) {
uc_mem_write(uc, patch.first, &patch.second, sizeof(uint32_t));
}
relocAllRegions(uc);
relocAllRegions(symtab, objcRuntime, uc);
}
return 0;
}

void VirtualMemoryV2::relocAllRegions(uc_engine *target) {
void VirtualMemoryV2::relocAllRegions(shared_ptr<SymbolTable> symtab, shared_ptr<ObjcRuntime> objcRuntime, uc_engine *target) {
if (target == nullptr) {
target = this->uc;
}
// perform relocs
SymbolTable *symtab = SymbolTable::getInstance();
ObjcRuntime *rt = ObjcRuntime::getInstance();
for (SymbolRelocation &reloc : symtab->getAllRelocs()) {
string relocSection = string(reloc.relocSection->sectname, std::min((int)strlen(reloc.relocSection->sectname), 16));
if (relocSection == "__text") {
Expand All @@ -221,9 +220,9 @@ void VirtualMemoryV2::relocAllRegions(uc_engine *target) {
} else {
externalClassInfo->className = symbolName;
}
rt->externalClassRuntimeInfo[reloc.relocAddr] = externalClassInfo;
rt->name2ExternalClassRuntimeInfo[externalClassInfo->className] = externalClassInfo;
rt->runtimeInfo2address[externalClassInfo] = reloc.relocAddr;
objcRuntime->externalClassRuntimeInfo[reloc.relocAddr] = externalClassInfo;
objcRuntime->name2ExternalClassRuntimeInfo[externalClassInfo->className] = externalClassInfo;
objcRuntime->runtimeInfo2address[externalClassInfo] = reloc.relocAddr;
uc_mem_write(target, reloc.relocAddr, &reloc.relocAddr, 8);
}
} else {
Expand Down
12 changes: 9 additions & 3 deletions iblessing/iblessing-core/core/memory/VirtualMemoryV2.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@

NS_IB_BEGIN

class SymbolTable;
class ObjcRuntime;

class VirtualMemoryV2 {
public:
VirtualMemoryV2(std::shared_ptr<VirtualMemory> fileMemory) : fileMemory(fileMemory) {
Expand All @@ -27,9 +30,9 @@ class VirtualMemoryV2 {
std::vector<std::pair<uint64_t, uint64_t>> dataPatch;

static VirtualMemoryV2* progressDefault();
int loadWithMachOData(uint8_t *mappedFile);
int mappingMachOToEngine(uc_engine *uc, uint8_t *mappedFile);
void relocAllRegions(uc_engine *target = nullptr);
int loadWithMachOData(std::shared_ptr<SymbolTable> symtab, std::shared_ptr<ObjcRuntime> objcRuntime, uint8_t *mappedFile);
int mappingMachOToEngine(std::shared_ptr<SymbolTable> symtab, std::shared_ptr<ObjcRuntime> objcRuntime, uc_engine *uc, uint8_t *mappedFile);
void relocAllRegions(std::shared_ptr<SymbolTable> symtab, std::shared_ptr<ObjcRuntime> objcRuntime, uc_engine *target = nullptr);
uint64_t read64(uint64_t address, bool *success);
uint32_t read32(uint64_t address, bool *success);
bool write32(uint64_t address, uint32_t value);
Expand All @@ -48,6 +51,9 @@ class VirtualMemoryV2 {
uint64_t getBaseAddr();
uc_engine* getEngine();

private:
VirtualMemoryV2() {};

protected:
static VirtualMemoryV2 *_instance;
std::map<uint64_t, std::pair<std::string, std::string>> addr2segInfo;
Expand Down
14 changes: 4 additions & 10 deletions iblessing/iblessing-core/core/runtime/ObjcCategory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,13 @@ using namespace iblessing;
00000030 __objc2_category ends
*/

static vector<shared_ptr<ObjcMethod>> loadMethodsFromAddress(shared_ptr<VirtualMemoryV2> vm2, uint64_t address, ObjcClassRuntimeInfo *classInfo, bool classMethod) {
static vector<shared_ptr<ObjcMethod>> loadMethodsFromAddress(shared_ptr<SymbolTable> symtab, shared_ptr<VirtualMemoryV2> vm2, uint64_t address, ObjcClassRuntimeInfo *classInfo, bool classMethod) {
uint32_t count = vm2->read32(address + 4, nullptr);
if (count == 0) {
return {};
}

vector<shared_ptr<ObjcMethod>> methods;
SymbolTable *symtab = SymbolTable::getInstance();
uint64_t objc_classmethods_addr = address + 8;
for (uint32_t i = 0; i < count; i++) {
uint64_t sel_offset = objc_classmethods_addr;
Expand Down Expand Up @@ -88,12 +87,7 @@ static vector<shared_ptr<ObjcMethod>> loadMethodsFromAddress(shared_ptr<VirtualM
return methods;
}

shared_ptr<ObjcCategory> ObjcCategory::loadFromAddress(uint64_t address) {
assert(false);
return nullptr;
}

shared_ptr<ObjcCategory> ObjcCategory::loadFromAddress(ObjcRuntime *runtime, shared_ptr<VirtualMemoryV2> vm2, uint64_t address) {
shared_ptr<ObjcCategory> ObjcCategory::loadFromAddress(shared_ptr<SymbolTable> symtab, ObjcRuntime *runtime, shared_ptr<VirtualMemoryV2> vm2, uint64_t address) {
shared_ptr<ObjcCategory> category = make_shared<ObjcCategory>();
uint64_t namePtr = vm2->read64(address, nullptr);
if (!namePtr) {
Expand All @@ -119,13 +113,13 @@ shared_ptr<ObjcCategory> ObjcCategory::loadFromAddress(ObjcRuntime *runtime, sha

uint64_t instanceMethodsAddr = vm2->read64(address, nullptr);
if (instanceMethodsAddr) {
category->instanceMethods = loadMethodsFromAddress(vm2, instanceMethodsAddr, category->decoratedClass->classInfo, false);
category->instanceMethods = loadMethodsFromAddress(symtab, vm2, instanceMethodsAddr, category->decoratedClass->classInfo, false);
}
address += 8;

uint64_t classMethodsAddr = vm2->read64(address, nullptr);
if (classMethodsAddr) {
category->classMethods = loadMethodsFromAddress(vm2, classMethodsAddr, category->decoratedClass->classInfo, true);
category->classMethods = loadMethodsFromAddress(symtab, vm2, classMethodsAddr, category->decoratedClass->classInfo, true);
}

if (category->decoratedClass->classInfo) {
Expand Down
4 changes: 2 additions & 2 deletions iblessing/iblessing-core/core/runtime/ObjcCategory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
NS_IB_BEGIN

class ObjcRuntime;
class SymbolTable;

class ObjcCategoryDecoratedClass {
public:
Expand All @@ -29,8 +30,7 @@ class ObjcCategory {
std::vector<std::shared_ptr<ObjcMethod>> instanceMethods;
std::vector<std::shared_ptr<ObjcMethod>> classMethods;

static std::shared_ptr<ObjcCategory> loadFromAddress(uint64_t address);
static std::shared_ptr<ObjcCategory> loadFromAddress(ObjcRuntime *runtime, std::shared_ptr<VirtualMemoryV2> vm2, uint64_t address);
static std::shared_ptr<ObjcCategory> loadFromAddress(std::shared_ptr<SymbolTable> symtab, ObjcRuntime *runtime, std::shared_ptr<VirtualMemoryV2> vm2, uint64_t address);
};

NS_IB_END
Expand Down
4 changes: 2 additions & 2 deletions iblessing/iblessing-core/core/runtime/ObjcRuntime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ void ObjcRuntime::loadClassList(uint64_t vmaddr, uint64_t size) {
}
}

void ObjcRuntime::loadCatList(uint64_t vmaddr, uint64_t size) {
void ObjcRuntime::loadCatList(shared_ptr<SymbolTable>, uint64_t vmaddr, uint64_t size) {
categoryList.clear();

uint64_t *cateAddrs = (uint64_t *)vm2->readBySize(vmaddr, size);
Expand All @@ -88,7 +88,7 @@ void ObjcRuntime::loadCatList(uint64_t vmaddr, uint64_t size) {
uint64_t count = size / sizeof(void *);
for (int i = 0; i < count; i++) {
uint64_t cateAddr = *cateAddrs;
shared_ptr<ObjcCategory> category = ObjcCategory::loadFromAddress(this, vm2, cateAddr);
shared_ptr<ObjcCategory> category = ObjcCategory::loadFromAddress(symtab, this, vm2, cateAddr);
if (category != nullptr) {
categoryList.push_back(category);
}
Expand Down
4 changes: 3 additions & 1 deletion iblessing/iblessing-core/core/runtime/ObjcRuntime.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

NS_IB_BEGIN

class SymbolTable;

class ObjcRuntime {
public:
ObjcRuntime(std::shared_ptr<SymbolTable> symtab, std::shared_ptr<VirtualMemoryV2> vm2) : symtab(symtab), vm2(vm2) {}
Expand Down Expand Up @@ -49,7 +51,7 @@ class ObjcRuntime {
ObjcClassRuntimeInfo* getClassInfoByAddress(uint64_t address, bool needRealize = true);
ObjcClassRuntimeInfo* evalReturnForIvarGetter(ObjcClassRuntimeInfo *targetClass, std::string getterSEL);
void loadClassList(uint64_t vmaddr, uint64_t size);
void loadCatList(uint64_t vmaddr, uint64_t size);
void loadCatList(std::shared_ptr<SymbolTable> symtab, uint64_t vmaddr, uint64_t size);
uint64_t getClassAddrByName(std::string className);
ObjcClassRuntimeInfo* getClassInfoByName(std::string className);
bool isClassObjectAtAddress(uint64_t address);
Expand Down
6 changes: 2 additions & 4 deletions iblessing/iblessing-core/core/symtab/SymbolTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,8 @@ SymbolTable::~SymbolTable() {
}

SymbolTable* SymbolTable::getInstance() {
if (SymbolTable::_instance == nullptr) {
SymbolTable::_instance = new SymbolTable();
}
return SymbolTable::_instance;
assert(false);
return nullptr;
}

void SymbolTable::sync() {
Expand Down
6 changes: 3 additions & 3 deletions iblessing/iblessing-core/v2/memory/memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ ib_return_t Memory::loadSync() {
this->fileMemory = vm;
this->virtualMemory = vm2;

int code = vm2->loadWithMachOData(vm->mappedFile);
int code = vm2->loadWithMachOData(macho->context->symtab, macho->context->objcRuntime, vm->mappedFile);
if (code != 0) {
return IB_MEMORY_MAPPING_ERROR;
}
Expand Down Expand Up @@ -114,7 +114,7 @@ ib_return_t Memory::loadSync() {
}
}
macho->context->symtab->sync();
vm2->relocAllRegions();
vm2->relocAllRegions(macho->context->symtab, macho->context->objcRuntime);
return IB_SUCCESS;
}

Expand All @@ -128,7 +128,7 @@ ib_return_t Memory::copyToUCEngine(uc_engine *uc) {
}

shared_ptr<VirtualMemoryV2> vm2 = this->virtualMemory;
int ret = vm2->mappingMachOToEngine(uc, this->fileMemory->mappedFile);
int ret = vm2->mappingMachOToEngine(macho->context->symtab, macho->context->objcRuntime, uc, this->fileMemory->mappedFile);
if (ret == 0) {
return IB_SUCCESS;
} else {
Expand Down
2 changes: 1 addition & 1 deletion iblessing/iblessing-core/v2/objc/objc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ ib_return_t Objc::loadClassList() {
ib_return_t Objc::loadCategoryList() {
shared_ptr<ObjcRuntime> rt = this->runtime;
if (rt->catlist_addr != 0 && rt->catlist_size != 0) {
rt->loadCatList(rt->catlist_addr, rt->catlist_size);
rt->loadCatList(macho->context->symtab, rt->catlist_addr, rt->catlist_size);
}
return IB_SUCCESS;
}
Expand Down

0 comments on commit db7f614

Please sign in to comment.