Skip to content

Commit

Permalink
Merge pull request #14132 from unknownbrackets/io-validsize
Browse files Browse the repository at this point in the history
Io: Truncate reads/writes to valid memory
  • Loading branch information
hrydgard authored Feb 13, 2021
2 parents 69d621d + 788e8c3 commit 14e3df6
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 16 deletions.
8 changes: 5 additions & 3 deletions Core/Dialog/SavedataParam.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -844,11 +844,13 @@ bool SavedataParam::GetExpectedHash(const std::string &dirPath, const std::strin

void SavedataParam::LoadFile(const std::string& dirPath, const std::string& filename, PspUtilitySavedataFileData *fileData) {
std::string filePath = dirPath + "/" + filename;
s64 readSize = -1;
if(!fileData->buf.IsValid())
if (!fileData->buf.IsValid())
return;

u8 *buf = fileData->buf;
if(ReadPSPFile(filePath, &buf, fileData->bufSize, &readSize))
u32 size = Memory::ValidSize(fileData->buf.ptr, fileData->bufSize);
s64 readSize = -1;
if (ReadPSPFile(filePath, &buf, size, &readSize))
fileData->size = readSize;
}

Expand Down
2 changes: 1 addition & 1 deletion Core/FileSystems/ISOFileSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ int ISOFileSystem::Ioctl(u32 handle, u32 cmd, u32 indataPtr, u32 inlen, u32 outd
return SCE_KERNEL_ERROR_ERRNO_INVALID_ARGUMENT;
} else {
int block = (u16)desc.firstLETableSector;
u32 size = (u32)desc.pathTableLength;
u32 size = Memory::ValidSize(outdataPtr, (u32)desc.pathTableLength);
u8 *out = Memory::GetPointer(outdataPtr);

int blocks = size / blockDevice->GetBlockSize();
Expand Down
26 changes: 14 additions & 12 deletions Core/HLE/sceIo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1027,10 +1027,11 @@ static bool __IoRead(int &result, int id, u32 data_addr, int size, int &us) {
return true;
} else if (Memory::IsValidAddress(data_addr)) {
CBreakPoints::ExecMemCheck(data_addr, true, size, currentMIPS->pc);
u8 *data = (u8*) Memory::GetPointer(data_addr);
u8 *data = (u8 *)Memory::GetPointer(data_addr);
u32 validSize = Memory::ValidSize(data_addr, size);
if (f->npdrm) {
result = npdrmRead(f, data, size);
currentMIPS->InvalidateICache(data_addr, size);
result = npdrmRead(f, data, validSize);
currentMIPS->InvalidateICache(data_addr, validSize);
return true;
}

Expand All @@ -1046,17 +1047,17 @@ static bool __IoRead(int &result, int id, u32 data_addr, int size, int &us) {
AsyncIOEvent ev = IO_EVENT_READ;
ev.handle = f->handle;
ev.buf = data;
ev.bytes = size;
ev.bytes = validSize;
ev.invalidateAddr = data_addr;
ioManager.ScheduleOperation(ev);
return false;
} else {
if (GetIOTimingMethod() != IOTIMING_REALISTIC) {
result = (int) pspFileSystem.ReadFile(f->handle, data, size);
result = (int)pspFileSystem.ReadFile(f->handle, data, validSize);
} else {
result = (int) pspFileSystem.ReadFile(f->handle, data, size, us);
result = (int)pspFileSystem.ReadFile(f->handle, data, validSize, us);
}
currentMIPS->InvalidateICache(data_addr, size);
currentMIPS->InvalidateICache(data_addr, validSize);
return true;
}
} else {
Expand Down Expand Up @@ -1136,12 +1137,13 @@ static bool __IoWrite(int &result, int id, u32 data_addr, int size, int &us) {
}

const void *data_ptr = Memory::GetPointer(data_addr);
const u32 validSize = Memory::ValidSize(data_addr, size);
// Let's handle stdout/stderr specially.
if (id == PSP_STDOUT || id == PSP_STDERR) {
const char *str = (const char *) data_ptr;
const int str_size = size == 0 ? 0 : (str[size - 1] == '\n' ? size - 1 : size);
const int str_size = size <= 0 ? 0 : (str[validSize - 1] == '\n' ? validSize - 1 : validSize);
INFO_LOG(SCEIO, "%s: %.*s", id == 1 ? "stdout" : "stderr", str_size, str);
result = size;
result = validSize;
return true;
}
u32 error;
Expand Down Expand Up @@ -1174,15 +1176,15 @@ static bool __IoWrite(int &result, int id, u32 data_addr, int size, int &us) {
AsyncIOEvent ev = IO_EVENT_WRITE;
ev.handle = f->handle;
ev.buf = (u8 *) data_ptr;
ev.bytes = size;
ev.bytes = validSize;
ev.invalidateAddr = 0;
ioManager.ScheduleOperation(ev);
return false;
} else {
if (GetIOTimingMethod() != IOTIMING_REALISTIC) {
result = (int) pspFileSystem.WriteFile(f->handle, (u8 *) data_ptr, size);
result = (int)pspFileSystem.WriteFile(f->handle, (u8 *) data_ptr, validSize);
} else {
result = (int) pspFileSystem.WriteFile(f->handle, (u8 *) data_ptr, size, us);
result = (int)pspFileSystem.WriteFile(f->handle, (u8 *) data_ptr, validSize, us);
}
}
return true;
Expand Down

0 comments on commit 14e3df6

Please sign in to comment.