mirror of
https://github.com/WinampDesktop/winamp.git
synced 2025-06-18 19:55:46 -04:00
Misc: Remove unused code
This commit is contained in:
@ -53,7 +53,7 @@ static void DeinterleaveSubcode(const u8* subcode_in, u8* subcode_out)
|
||||
}
|
||||
}
|
||||
|
||||
#if defined(_WIN32) && !defined(_UWP)
|
||||
#if defined(_WIN32)
|
||||
|
||||
// The include order here is critical.
|
||||
// clang-format off
|
||||
|
@ -22,33 +22,12 @@ static std::string GetTemporaryFileName(const std::string& original_filename)
|
||||
{
|
||||
std::string temporary_filename;
|
||||
temporary_filename.reserve(original_filename.length() + 8);
|
||||
temporary_filename.append(original_filename);
|
||||
|
||||
#ifdef _WIN32
|
||||
// On UWP, preserve the extension, as it affects permissions.
|
||||
#ifdef _UWP
|
||||
const std::string_view original_view(original_filename);
|
||||
const std::string_view extension(Path::GetExtension(original_view));
|
||||
if (extension.length() < original_filename.length())
|
||||
{
|
||||
temporary_filename.append(original_view.substr(0, original_filename.length() - extension.length() - 1));
|
||||
temporary_filename.append("_XXXXXX");
|
||||
_mktemp_s(temporary_filename.data(), temporary_filename.length() + 1);
|
||||
temporary_filename += '.';
|
||||
temporary_filename.append(extension);
|
||||
}
|
||||
else
|
||||
{
|
||||
temporary_filename.append(original_filename);
|
||||
temporary_filename.append(".XXXXXXX");
|
||||
_mktemp_s(temporary_filename.data(), temporary_filename.length() + 1);
|
||||
}
|
||||
#else
|
||||
temporary_filename.append(original_filename);
|
||||
temporary_filename.append(".XXXXXXX");
|
||||
_mktemp_s(temporary_filename.data(), temporary_filename.length() + 1);
|
||||
#endif
|
||||
#else
|
||||
temporary_filename.append(original_filename);
|
||||
temporary_filename.append(".XXXXXX");
|
||||
#if defined(__linux__) || defined(__ANDROID__) || defined(__APPLE__)
|
||||
mkstemp(temporary_filename.data());
|
||||
|
@ -44,21 +44,7 @@ bool JitCodeBuffer::Allocate(u32 size /* = 64 * 1024 * 1024 */, u32 far_code_siz
|
||||
m_total_size = size + far_code_size;
|
||||
|
||||
#if defined(_WIN32)
|
||||
#if !defined(_UWP)
|
||||
m_code_ptr = static_cast<u8*>(VirtualAlloc(nullptr, m_total_size, MEM_COMMIT, PAGE_EXECUTE_READWRITE));
|
||||
#else
|
||||
m_code_ptr = static_cast<u8*>(
|
||||
VirtualAlloc2FromApp(GetCurrentProcess(), nullptr, m_total_size, MEM_COMMIT, PAGE_READWRITE, nullptr, 0));
|
||||
if (m_code_ptr)
|
||||
{
|
||||
ULONG old_protection;
|
||||
if (!VirtualProtectFromApp(m_code_ptr, m_total_size, PAGE_EXECUTE_READWRITE, &old_protection))
|
||||
{
|
||||
VirtualFree(m_code_ptr, m_total_size, MEM_RELEASE);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
if (!m_code_ptr)
|
||||
{
|
||||
Log_ErrorPrintf("VirtualAlloc(RWX, %u) for internal buffer failed: %u", m_total_size, GetLastError());
|
||||
|
@ -127,13 +127,8 @@ bool MemoryArena::Create(size_t size, bool writable, bool executable)
|
||||
|
||||
#if defined(_WIN32)
|
||||
const DWORD protect = (writable ? (executable ? PAGE_EXECUTE_READWRITE : PAGE_READWRITE) : PAGE_READONLY);
|
||||
#ifndef _UWP
|
||||
m_file_handle = CreateFileMappingA(INVALID_HANDLE_VALUE, nullptr, protect, Truncate32(size >> 32), Truncate32(size),
|
||||
file_mapping_name.c_str());
|
||||
#else
|
||||
m_file_handle = CreateFileMappingFromApp(INVALID_HANDLE_VALUE, nullptr, protect, size,
|
||||
StringUtil::UTF8StringToWideString(file_mapping_name).c_str());
|
||||
#endif
|
||||
if (!m_file_handle)
|
||||
{
|
||||
Log_ErrorPrintf("CreateFileMapping failed: %u", GetLastError());
|
||||
@ -254,16 +249,8 @@ void* MemoryArena::CreateViewPtr(size_t offset, size_t size, bool writable, bool
|
||||
void* base_pointer;
|
||||
#if defined(_WIN32)
|
||||
const DWORD desired_access = FILE_MAP_READ | (writable ? FILE_MAP_WRITE : 0) | (executable ? FILE_MAP_EXECUTE : 0);
|
||||
#ifndef _UWP
|
||||
base_pointer =
|
||||
MapViewOfFileEx(m_file_handle, desired_access, Truncate32(offset >> 32), Truncate32(offset), size, fixed_address);
|
||||
#else
|
||||
// UWP does not support fixed mappings.
|
||||
if (!fixed_address)
|
||||
base_pointer = MapViewOfFileFromApp(m_file_handle, desired_access, offset, size);
|
||||
else
|
||||
base_pointer = nullptr;
|
||||
#endif
|
||||
if (!base_pointer)
|
||||
return nullptr;
|
||||
#elif defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__)
|
||||
|
@ -80,7 +80,7 @@ static bool IsStoreInstruction(const void* ptr)
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(_WIN32) && !defined(_UWP) && (defined(CPU_X64) || defined(CPU_AARCH64))
|
||||
#if defined(_WIN32) && (defined(CPU_X64) || defined(CPU_AARCH64))
|
||||
static PVOID s_veh_handle;
|
||||
|
||||
static LONG ExceptionHandler(PEXCEPTION_POINTERS exi)
|
||||
@ -116,145 +116,6 @@ static LONG ExceptionHandler(PEXCEPTION_POINTERS exi)
|
||||
return EXCEPTION_CONTINUE_SEARCH;
|
||||
}
|
||||
|
||||
u32 GetHandlerCodeSize()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
#elif defined(_UWP)
|
||||
|
||||
// https://docs.microsoft.com/en-us/cpp/build/exception-handling-x64?view=msvc-160
|
||||
struct UNWIND_INFO
|
||||
{
|
||||
BYTE version : 3;
|
||||
BYTE flags : 5;
|
||||
BYTE size_of_prologue;
|
||||
BYTE count_of_unwind_codes;
|
||||
BYTE frame_register : 4;
|
||||
BYTE frame_offset_scaled : 4;
|
||||
ULONG exception_handler_address;
|
||||
};
|
||||
|
||||
struct UnwindHandler
|
||||
{
|
||||
RUNTIME_FUNCTION runtime_function;
|
||||
UNWIND_INFO unwind_info;
|
||||
uint8_t exception_handler_code[32];
|
||||
};
|
||||
|
||||
static constexpr size_t UNWIND_HANDLER_ALLOC_SIZE = 4096;
|
||||
static_assert(sizeof(UnwindHandler) <= UNWIND_HANDLER_ALLOC_SIZE);
|
||||
|
||||
static EXCEPTION_DISPOSITION UnwindExceptionHandler(PEXCEPTION_RECORD ExceptionRecord, ULONG64 EstablisherFrame,
|
||||
PCONTEXT ContextRecord, PDISPATCHER_CONTEXT DispatcherContext)
|
||||
{
|
||||
if (s_in_handler)
|
||||
return ExceptionContinueSearch;
|
||||
|
||||
s_in_handler = true;
|
||||
|
||||
void* const exception_pc = reinterpret_cast<void*>(DispatcherContext->ControlPc);
|
||||
void* const exception_address = reinterpret_cast<void*>(ExceptionRecord->ExceptionInformation[1]);
|
||||
bool const is_write = ExceptionRecord->ExceptionInformation[0] == 1;
|
||||
|
||||
std::lock_guard<std::mutex> guard(m_handler_lock);
|
||||
for (const RegisteredHandler& rh : m_handlers)
|
||||
{
|
||||
if (static_cast<const u8*>(exception_pc) >= static_cast<const u8*>(rh.start_pc) &&
|
||||
static_cast<const u8*>(exception_pc) <= (static_cast<const u8*>(rh.start_pc) + rh.code_size))
|
||||
{
|
||||
if (rh.callback(exception_pc, exception_address, is_write) == HandlerResult::ContinueExecution)
|
||||
{
|
||||
s_in_handler = false;
|
||||
return ExceptionContinueExecution;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
s_in_handler = false;
|
||||
return ExceptionContinueSearch;
|
||||
}
|
||||
|
||||
static PRUNTIME_FUNCTION GetRuntimeFunctionCallback(DWORD64 ControlPc, PVOID Context)
|
||||
{
|
||||
std::lock_guard<std::mutex> guard(m_handler_lock);
|
||||
for (const RegisteredHandler& rh : m_handlers)
|
||||
{
|
||||
if (ControlPc >= reinterpret_cast<DWORD64>(rh.start_pc) &&
|
||||
ControlPc <= (reinterpret_cast<DWORD64>(rh.start_pc) + rh.code_size))
|
||||
{
|
||||
return reinterpret_cast<PRUNTIME_FUNCTION>(rh.start_pc);
|
||||
}
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
static bool InstallFunctionTableCallback(const void* owner, void* start_pc, u32 code_size)
|
||||
{
|
||||
if (code_size < UNWIND_HANDLER_ALLOC_SIZE)
|
||||
{
|
||||
Log_ErrorPrintf("Invalid code size: %u @ %p", code_size, UNWIND_HANDLER_ALLOC_SIZE);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!RtlInstallFunctionTableCallback(reinterpret_cast<DWORD64>(owner) | 0x3, reinterpret_cast<DWORD64>(start_pc),
|
||||
static_cast<DWORD>(code_size), &GetRuntimeFunctionCallback, nullptr, nullptr))
|
||||
{
|
||||
Log_ErrorPrintf("RtlInstallFunctionTableCallback() failed: %08X", GetLastError());
|
||||
return false;
|
||||
}
|
||||
|
||||
// This is only valid on x86 for now.
|
||||
#ifndef CPU_X64
|
||||
Log_ErrorPrint("Exception unwind codegen not implemented");
|
||||
return false;
|
||||
#else
|
||||
UnwindHandler* uh = static_cast<UnwindHandler*>(start_pc);
|
||||
ULONG old_protection;
|
||||
if (!VirtualProtectFromApp(uh, UNWIND_HANDLER_ALLOC_SIZE, PAGE_READWRITE, &old_protection))
|
||||
{
|
||||
Log_ErrorPrintf("VirtualProtectFromApp(RW) for exception handler failed: %08X", GetLastError());
|
||||
return false;
|
||||
}
|
||||
|
||||
uh->runtime_function.BeginAddress = UNWIND_HANDLER_ALLOC_SIZE;
|
||||
uh->runtime_function.EndAddress = code_size;
|
||||
uh->runtime_function.UnwindInfoAddress = offsetof(UnwindHandler, unwind_info);
|
||||
|
||||
uh->unwind_info.version = 1;
|
||||
uh->unwind_info.flags = UNW_FLAG_EHANDLER;
|
||||
uh->unwind_info.size_of_prologue = 0;
|
||||
uh->unwind_info.count_of_unwind_codes = 0;
|
||||
uh->unwind_info.frame_register = 0;
|
||||
uh->unwind_info.frame_offset_scaled = 0;
|
||||
uh->unwind_info.exception_handler_address = offsetof(UnwindHandler, exception_handler_code);
|
||||
|
||||
// mov rax, handler
|
||||
const void* handler = UnwindExceptionHandler;
|
||||
uh->exception_handler_code[0] = 0x48;
|
||||
uh->exception_handler_code[1] = 0xb8;
|
||||
std::memcpy(&uh->exception_handler_code[2], &handler, sizeof(handler));
|
||||
|
||||
// jmp rax
|
||||
uh->exception_handler_code[10] = 0xff;
|
||||
uh->exception_handler_code[11] = 0xe0;
|
||||
|
||||
if (!VirtualProtectFromApp(uh, UNWIND_HANDLER_ALLOC_SIZE, PAGE_EXECUTE_READ, &old_protection))
|
||||
{
|
||||
Log_ErrorPrintf("VirtualProtectFromApp(RX) for exception handler failed: %08X", GetLastError());
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
#endif
|
||||
}
|
||||
|
||||
u32 GetHandlerCodeSize()
|
||||
{
|
||||
return UNWIND_HANDLER_ALLOC_SIZE;
|
||||
}
|
||||
|
||||
#elif defined(USE_SIGSEGV)
|
||||
|
||||
static struct sigaction s_old_sigsegv_action;
|
||||
@ -345,18 +206,6 @@ static void SIGSEGVHandler(int sig, siginfo_t* info, void* ctx)
|
||||
sa.sa_handler(sig);
|
||||
}
|
||||
|
||||
u32 GetHandlerCodeSize()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
u32 GetHandlerCodeSize()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
bool InstallHandler(const void* owner, void* start_pc, u32 code_size, Callback callback)
|
||||
@ -375,19 +224,13 @@ bool InstallHandler(const void* owner, void* start_pc, u32 code_size, Callback c
|
||||
|
||||
if (was_empty)
|
||||
{
|
||||
#if defined(_WIN32) && !defined(_UWP) && (defined(CPU_X64) || defined(CPU_AARCH64))
|
||||
#if defined(_WIN32) && (defined(CPU_X64) || defined(CPU_AARCH64))
|
||||
s_veh_handle = AddVectoredExceptionHandler(1, ExceptionHandler);
|
||||
if (!s_veh_handle)
|
||||
{
|
||||
Log_ErrorPrint("Failed to add vectored exception handler");
|
||||
return false;
|
||||
}
|
||||
#elif defined(_UWP)
|
||||
if (!InstallFunctionTableCallback(owner, start_pc, code_size))
|
||||
{
|
||||
Log_ErrorPrint("Failed to install function table callback");
|
||||
return false;
|
||||
}
|
||||
#elif defined(USE_SIGSEGV)
|
||||
struct sigaction sa = {};
|
||||
sa.sa_sigaction = SIGSEGVHandler;
|
||||
@ -427,11 +270,9 @@ bool RemoveHandler(const void* owner)
|
||||
|
||||
if (m_handlers.empty())
|
||||
{
|
||||
#if defined(_WIN32) && !defined(_UWP) && (defined(CPU_X64) || defined(CPU_AARCH64))
|
||||
#if defined(_WIN32) && (defined(CPU_X64) || defined(CPU_AARCH64))
|
||||
RemoveVectoredExceptionHandler(s_veh_handle);
|
||||
s_veh_handle = nullptr;
|
||||
#elif defined(_UWP)
|
||||
// nothing to do here, any unregistered regions will be ignored
|
||||
#elif defined(USE_SIGSEGV)
|
||||
// restore old signal handler
|
||||
#if defined(__APPLE__) || defined(__aarch64__)
|
||||
|
@ -11,8 +11,6 @@ enum class HandlerResult
|
||||
using Callback = HandlerResult (*)(void* exception_pc, void* fault_address, bool is_write);
|
||||
using Handle = void*;
|
||||
|
||||
u32 GetHandlerCodeSize();
|
||||
|
||||
bool InstallHandler(const void* owner, void* start_pc, u32 code_size, Callback callback);
|
||||
bool RemoveHandler(const void* owner);
|
||||
|
||||
|
Reference in New Issue
Block a user