From 286a7a03f930b78303ff5a45e580791a315d2608 Mon Sep 17 00:00:00 2001 From: Silent Date: Sun, 8 Sep 2019 21:32:34 +0200 Subject: [PATCH 1/2] [Kernel] Fixup GetProcAddressByOrdinal Now properly takes a global lock when populating guest_trampoline_map_ as opposed to taking and immediately releasing it Also removes a redundant find() from guest_trampoline_map_ so map is not searched twice if the function has already been generated. --- src/xenia/kernel/kernel_module.cc | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/xenia/kernel/kernel_module.cc b/src/xenia/kernel/kernel_module.cc index 05304feae..b5897599c 100644 --- a/src/xenia/kernel/kernel_module.cc +++ b/src/xenia/kernel/kernel_module.cc @@ -106,12 +106,12 @@ uint32_t KernelModule::GetProcAddressByOrdinal(uint16_t ordinal) { } else { if (export_entry->function_data.trampoline || export_entry->function_data.shim) { - global_critical_region_.Acquire(); + auto global_lock = global_critical_region_.Acquire(); // See if the function has been generated already. - if (guest_trampoline_map_.find(ordinal) != guest_trampoline_map_.end()) { - auto entry = guest_trampoline_map_.find(ordinal); - return entry->second; + auto item = guest_trampoline_map_.find(ordinal); + if (item != guest_trampoline_map_.end()) { + return item->second; } cpu::GuestFunction::ExternHandler handler = nullptr; @@ -130,7 +130,7 @@ uint32_t KernelModule::GetProcAddressByOrdinal(uint16_t ordinal) { export_entry->name, guest_addr); // Register the function in our map. - guest_trampoline_map_[ordinal] = guest_addr; + guest_trampoline_map_.emplace(ordinal, guest_addr); return guest_addr; } else { // Not implemented. From 9021a7c9227b0d94a6e5215342e80953678d7221 Mon Sep 17 00:00:00 2001 From: Silent Date: Sun, 8 Sep 2019 22:21:32 +0200 Subject: [PATCH 2/2] [CPU] Fix off-by-one in max ordinals check --- src/xenia/cpu/export_resolver.cc | 2 +- src/xenia/cpu/xex_module.cc | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/xenia/cpu/export_resolver.cc b/src/xenia/cpu/export_resolver.cc index 68028e7a2..9b500caee 100644 --- a/src/xenia/cpu/export_resolver.cc +++ b/src/xenia/cpu/export_resolver.cc @@ -63,7 +63,7 @@ Export* ExportResolver::GetExportByOrdinal(const char* module_name, for (const auto& table : tables_) { if (std::strncmp(module_name, table.module_name(), std::strlen(table.module_name())) == 0) { - if (ordinal > table.exports_by_ordinal().size()) { + if (ordinal >= table.exports_by_ordinal().size()) { return nullptr; } return table.exports_by_ordinal().at(ordinal); diff --git a/src/xenia/cpu/xex_module.cc b/src/xenia/cpu/xex_module.cc index e842b58b4..53a92e25c 100644 --- a/src/xenia/cpu/xex_module.cc +++ b/src/xenia/cpu/xex_module.cc @@ -126,7 +126,7 @@ uint32_t XexModule::GetProcAddress(uint16_t ordinal) const { xex_security_info()->export_table); ordinal -= export_table->base; - if (ordinal > export_table->count) { + if (ordinal >= export_table->count) { XELOGE("GetProcAddress(%.3X): ordinal out of bounds", ordinal); return 0; }