Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions core/libs/prx/libSceSysmodule/Export.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,6 @@ int APS5_VABI sceSysmoduleIsLoaded(std::uint16_t id) {
}
if (!findModuleName(id)) {
APS5_LOG_OUT("unknown id: %u", static_cast<unsigned>(id));
// throw std::runtime_error(std::string("sceSysmoduleIsLoaded: unknown id ") + std::to_string(id));
return 0;
}
std::lock_guard<std::mutex> lock(gMutex);
Expand All @@ -203,7 +202,6 @@ int APS5_VABI sceSysmoduleLoadModule(std::uint16_t id) {
}
if (!findModuleName(id)) {
APS5_LOG_OUT("unknown id: %u", static_cast<unsigned>(id));
// throw std::runtime_error(std::string("sceSysmoduleLoadModule: unknown id ") + std::to_string(id));
return 0;
}
std::lock_guard<std::mutex> lock(gMutex);
Expand Down
26 changes: 26 additions & 0 deletions core/libs/tests/GuestSysmodule.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "SceTypes.hpp"
#include "prx/libc/include/general/VabiMacros.hpp"
#include "prx/libSceSysmodule/ModuleTable.hpp"
#include <cstdint>
#include <cstdlib>
#include <cstring>
Expand Down Expand Up @@ -108,6 +109,30 @@ void CheckUnwindInfo() {
}
#endif

std::uint16_t UnknownModuleId() {
for (std::uint32_t id = 0xffff; id != 0; --id) {
if (kModuleTable.find(id) == kModuleTable.end()) return static_cast<std::uint16_t>(id);
}
std::abort();
}

void CheckUnknownIds() {
const std::uint16_t id = UnknownModuleId();
const std::uint32_t internalId = id | kInternalFlag;
Require(kModuleTable.find(internalId) == kModuleTable.end());
Require(sceSysmoduleIsLoaded(id) == 0);
Require(sceSysmoduleLoadModule(id) == 0);
Require(sceSysmoduleIsLoaded(id) == 0);
Require(sceSysmoduleUnloadModule(id) == 0);
Require(sceSysmoduleUnloadModule(id) == 0);
Require(sceSysmoduleIsLoaded(id) == 0);
Require(ThrowsRuntimeError([internalId] { sceSysmoduleLoadModuleInternal(internalId); }));
Require(ThrowsRuntimeError([internalId] { sceSysmoduleUnloadModuleInternal(internalId); }));
int result = 0x5a5a5a5a;
Require(ThrowsRuntimeError([internalId, &result] { sceSysmoduleLoadModuleInternalWithArg(internalId, 0, nullptr, 0, &result); }));
Require(result == 0x5a5a5a5a);
}

#ifdef _WIN32
void RequireUnwindMatchesRegion(const void* pointer) {
const auto address = reinterpret_cast<std::uint64_t>(pointer);
Expand Down Expand Up @@ -172,6 +197,7 @@ void CheckUnwindInfo() {
}

int main() {
CheckUnknownIds();
Require(sceSysmoduleIsLoaded(0) == kInvalidValue);
Require(sceSysmoduleLoadModule(0) == kInvalidValue);
Require(sceSysmoduleUnloadModule(0) == kInvalidValue);
Expand Down
4 changes: 4 additions & 0 deletions docs/dev/debt/libSceSysmodule.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# libSceSysmodule technical debt

### Silent stubs

- [sceSysmoduleIsLoaded, sceSysmoduleLoadModule, sceSysmoduleUnloadModule](../../../core/libs/prx/libSceSysmodule/Export.cpp) (libSceSysmodule) accept a module id missing from the module table: they log it and return 0 without loading anything, so `sceSysmoduleIsLoaded` reports an unknown module as loaded and unloading it succeeds, because titles that load or unload such an id aborted otherwise. The internal variants (`sceSysmoduleLoadModuleInternal`, `sceSysmoduleUnloadModuleInternal`, `sceSysmoduleLoadModuleInternalWithArg`, `sceSysmoduleGetModuleHandleInternal`) throw for an unknown id. What the console returns for an id it does not know is not verified.

### Functional

- [sceSysmoduleGetModuleInfoForUnwind](../../../core/libs/prx/libSceSysmodule/Export.cpp) (libSceSysmodule) - on Windows the region and file name of an address inside a loaded image are cached after the first lookup, because `VirtualQuery` on a large image section is slow. An entry is used only while the loader still reports the same image for the address (same base, time stamp and size). A page protection change inside the image after the first lookup, which would split or merge the region `VirtualQuery` reports, is not noticed. Addresses outside loaded images are queried every time
Loading