add --initialize-memory option to help catch uninitialized memory errors

This commit is contained in:
misson20000 2018-03-28 14:24:58 -07:00
parent 5632e77ed4
commit 86e61651db
3 changed files with 14 additions and 3 deletions

View file

@ -120,7 +120,14 @@ void Cpu::stop() {
bool Cpu::map(gptr addr, guint size) {
CHECKED(uc_mem_map(uc, addr, size, UC_PROT_ALL));
auto temp = new uint8_t[size];
memset(temp, 0, size);
if(ctu->initializeMemory) {
uint8_t val[] = "badmem!!";
for(size_t sz = 0; sz < size; sz++) {
temp[sz] = val[sz % sizeof(val)];
}
} else {
memset(temp, 0, size);
}
writemem(addr, temp, size);
delete[] temp;
return true;

3
Ctu.h
View file

@ -214,7 +214,8 @@ public:
gptr loadbase, loadsize;
bool socketsEnabled;
bool initializeMemory = false;
private:
ghandle handleId;
unordered_map<ghandle, shared_ptr<KObject>> handles;

View file

@ -38,7 +38,7 @@ struct Arg: public option::Arg
}
};
enum optionIndex { UNKNOWN, HELP, ENABLE_GDB, PORT, NSO, NRO, KIP, ENABLE_SOCKETS, RELOCATE };
enum optionIndex { UNKNOWN, HELP, ENABLE_GDB, PORT, NSO, NRO, KIP, ENABLE_SOCKETS, RELOCATE, INITIALIZE_MEMORY };
const option::Descriptor usage[] =
{
{UNKNOWN, 0, "", "",Arg::None, "USAGE: ctu [options] <load-directory>\n\n"
@ -51,6 +51,7 @@ const option::Descriptor usage[] =
{KIP, 0,"","load-kip",Arg::NonEmpty, " --load-kip \tLoad a KIP without load directory"},
{ENABLE_SOCKETS, 0, "b","enable-sockets",Arg::None, " --enable-sockets, -b \tEnable BSD socket passthrough." },
{RELOCATE, 0, "r","relocate",Arg::None, " --relocate, -r \tRelocate loaded NRO files" },
{INITIALIZE_MEMORY, 0, "m", "initialize-memory", Arg::None, " --initialize-memory, -m \tInitialize memory to help catch uninitialized memory bugs" },
{0,0,nullptr,nullptr,nullptr,nullptr}
};
@ -172,6 +173,8 @@ int main(int argc, char **argv) {
ctu.socketsEnabled = false;
}
ctu.initializeMemory = options[INITIALIZE_MEMORY].count() > 0;
bool relocate = false;
if(options[RELOCATE].count()) {
relocate = true;