From e9802a9f3ba89ea52fde7747dcd5090e5061ee27 Mon Sep 17 00:00:00 2001 From: gibbed Date: Mon, 26 Aug 2019 12:19:00 -0500 Subject: [PATCH] [x64] Further simplification / fix buffer overrun in code cache. - [x64] Further simplify padding of code / unwind reservation in code cache. - [x64] Fix accidental buffer overrun caused by previous simplification. --- src/xenia/cpu/backend/x64/x64_code_cache.cc | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/xenia/cpu/backend/x64/x64_code_cache.cc b/src/xenia/cpu/backend/x64/x64_code_cache.cc index 3dbe05f89..021d51c62 100644 --- a/src/xenia/cpu/backend/x64/x64_code_cache.cc +++ b/src/xenia/cpu/backend/x64/x64_code_cache.cc @@ -138,7 +138,6 @@ void* X64CodeCache::PlaceGuestCode(uint32_t guest_address, void* machine_code, // unwind table requires entries AND code to be sorted in order. size_t low_mark; size_t high_mark; - size_t code_offset; uint8_t* code_address; UnwindReservation unwind_reservation; { @@ -148,10 +147,11 @@ void* X64CodeCache::PlaceGuestCode(uint32_t guest_address, void* machine_code, // Reserve code. // Always move the code to land on 16b alignment. - code_offset = generated_code_offset_; - code_address = generated_code_base_ + code_offset; + code_address = generated_code_base_ + generated_code_offset_; generated_code_offset_ += xe::round_up(func_info.code_size.total, 16); + auto tail_address = generated_code_base_ + generated_code_offset_; + // Reserve unwind info. // We go on the high size of the unwind info as we don't know how big we // need it, and a few extra bytes of padding isn't the worst thing. @@ -159,6 +159,8 @@ void* X64CodeCache::PlaceGuestCode(uint32_t guest_address, void* machine_code, RequestUnwindReservation(generated_code_base_ + generated_code_offset_); generated_code_offset_ += xe::round_up(unwind_reservation.data_size, 16); + auto end_address = generated_code_base_ + generated_code_offset_; + high_mark = generated_code_offset_; // Store in map. It is maintained in sorted order of host PC dependent on @@ -191,8 +193,8 @@ void* X64CodeCache::PlaceGuestCode(uint32_t guest_address, void* machine_code, std::memcpy(code_address, machine_code, func_info.code_size.total); // Fill unused slots with 0xCC - std::memset(code_address + func_info.code_size.total, 0xCC, - generated_code_offset_ - code_offset); + std::memset(tail_address, 0xCC, + static_cast(end_address - tail_address)); // Notify subclasses of placed code. PlaceCode(guest_address, machine_code, func_info, code_address,