From 56b9b38c9c67241e91650d1ec0b5150c6633f08e Mon Sep 17 00:00:00 2001 From: Nekotekina Date: Mon, 15 Aug 2016 13:18:47 +0300 Subject: [PATCH] verify() fix --- Utilities/File.cpp | 2 +- Utilities/StrFmt.cpp | 26 ++++++---- Utilities/StrFmt.h | 3 -- Utilities/VirtualMemory.cpp | 4 +- Utilities/types.h | 72 +++++++++++---------------- rpcs3/Emu/Cell/RawSPUThread.cpp | 2 +- rpcs3/Emu/Cell/SPUThread.cpp | 2 +- rpcs3/Emu/Cell/lv2/sys_memory.cpp | 4 +- rpcs3/Emu/PSP2/ARMv7Thread.cpp | 2 +- rpcs3/Emu/RSX/D3D12/D3D12GSRender.cpp | 6 +-- 10 files changed, 55 insertions(+), 68 deletions(-) diff --git a/Utilities/File.cpp b/Utilities/File.cpp index 53172a3909..ec470f195d 100644 --- a/Utilities/File.cpp +++ b/Utilities/File.cpp @@ -43,7 +43,7 @@ static void to_utf8(std::string& out, const wchar_t* source) const int result = WideCharToMultiByte(CP_UTF8, 0, source, static_cast(length) + 1, &out.front(), buf_size, NULL, NULL); // Fix the size - out.resize(verify(result, "to_utf8" HERE) - 1); + out.resize(verify("to_utf8" HERE, result) - 1); } static time_t to_time(const ULARGE_INTEGER& ft) diff --git a/Utilities/StrFmt.cpp b/Utilities/StrFmt.cpp index 93fd9693d0..fb33f1a831 100644 --- a/Utilities/StrFmt.cpp +++ b/Utilities/StrFmt.cpp @@ -132,7 +132,7 @@ namespace fmt throw std::runtime_error{msg}; } - void raw_verify_error(const char* msg, uint position) + void raw_verify_error(const char* msg, const fmt_type_info* sup, u64 arg) { std::string out{"Verification failed"}; @@ -149,15 +149,18 @@ namespace fmt } #endif - if (position) + if (sup) { - out += " (+"; - out += std::to_string(position); + out += " ("; + sup->fmt_string(out, arg); // Print value out += ")"; } - out += ": "; - out += msg; + if (msg) + { + out += ": "; + out += msg; + } throw std::runtime_error{out}; } @@ -166,13 +169,16 @@ namespace fmt { std::string out{"Narrow error"}; - out += " ("; - sup->fmt_string(out, arg); // Print value - out += "): "; + if (sup) + { + out += " ("; + sup->fmt_string(out, arg); // Print value + out += ")"; + } if (msg) { - out += " "; + out += ": "; out += msg; } diff --git a/Utilities/StrFmt.h b/Utilities/StrFmt.h index 9c080d7e94..845f673074 100644 --- a/Utilities/StrFmt.h +++ b/Utilities/StrFmt.h @@ -213,9 +213,6 @@ struct fmt_type_info } }; -template -using fmt_unveil_t = typename fmt_unveil::type; - // Argument array type (each element generated via fmt_unveil<>) template using fmt_args_t = const u64(&&)[sizeof...(Args) + 1]; diff --git a/Utilities/VirtualMemory.cpp b/Utilities/VirtualMemory.cpp index 0dd7b6c3bd..7527d3b4dd 100644 --- a/Utilities/VirtualMemory.cpp +++ b/Utilities/VirtualMemory.cpp @@ -16,9 +16,9 @@ namespace memory_helper void* reserve_memory(size_t size) { #ifdef _WIN32 - return verify(VirtualAlloc(NULL, size, MEM_RESERVE, PAGE_NOACCESS), "reserve_memory" HERE); + return verify("reserve_memory" HERE, VirtualAlloc(NULL, size, MEM_RESERVE, PAGE_NOACCESS)); #else - return verify(::mmap(nullptr, size, PROT_NONE, MAP_ANON | MAP_PRIVATE, -1, 0), "reserve_memory" HERE); + return verify("reserve_memory" HERE, ::mmap(nullptr, size, PROT_NONE, MAP_ANON | MAP_PRIVATE, -1, 0)); #endif } diff --git a/Utilities/types.h b/Utilities/types.h index f82798e597..015f86aa4c 100644 --- a/Utilities/types.h +++ b/Utilities/types.h @@ -96,6 +96,9 @@ namespace gsl template struct fmt_unveil; +template +using fmt_unveil_t = typename fmt_unveil::type; + struct fmt_type_info; namespace fmt @@ -443,25 +446,25 @@ struct ignore }; template ::value>> -constexpr T align(const T& value, std::uint64_t align) +constexpr T align(const T& value, ullong align) { return static_cast((value + (align - 1)) & ~(align - 1)); } -inline std::uint32_t cntlz32(std::uint32_t arg, bool nonzero = false) +inline u32 cntlz32(u32 arg, bool nonzero = false) { -#if defined(_MSC_VER) - unsigned long res; +#ifdef _MSC_VER + ulong res; return _BitScanReverse(&res, arg) || nonzero ? res ^ 31 : 32; #else return arg || nonzero ? __builtin_clzll(arg) - 32 : 32; #endif } -inline std::uint64_t cntlz64(std::uint64_t arg, bool nonzero = false) +inline u64 cntlz64(u64 arg, bool nonzero = false) { -#if defined(_MSC_VER) - unsigned long res; +#ifdef _MSC_VER + ulong res; return _BitScanReverse64(&res, arg) || nonzero ? res ^ 63 : 64; #else return arg || nonzero ? __builtin_clzll(arg) : 64; @@ -469,13 +472,13 @@ inline std::uint64_t cntlz64(std::uint64_t arg, bool nonzero = false) } // Helper function, used by ""_u16, ""_u32, ""_u64 -constexpr std::uint8_t to_u8(char c) +constexpr u8 to_u8(char c) { - return static_cast(c); + return static_cast(c); } // Convert 2-byte string to u16 value like reinterpret_cast does -constexpr std::uint16_t operator""_u16(const char* s, std::size_t length) +constexpr u16 operator""_u16(const char* s, std::size_t length) { return length != 2 ? throw s : #if IS_LE_MACHINE == 1 @@ -484,7 +487,7 @@ constexpr std::uint16_t operator""_u16(const char* s, std::size_t length) } // Convert 4-byte string to u32 value like reinterpret_cast does -constexpr std::uint32_t operator""_u32(const char* s, std::size_t length) +constexpr u32 operator""_u32(const char* s, std::size_t length) { return length != 4 ? throw s : #if IS_LE_MACHINE == 1 @@ -493,18 +496,18 @@ constexpr std::uint32_t operator""_u32(const char* s, std::size_t length) } // Convert 8-byte string to u64 value like reinterpret_cast does -constexpr std::uint64_t operator""_u64(const char* s, std::size_t length) +constexpr u64 operator""_u64(const char* s, std::size_t length) { return length != 8 ? throw s : #if IS_LE_MACHINE == 1 - static_cast(to_u8(s[7]) << 24 | to_u8(s[6]) << 16 | to_u8(s[5]) << 8 | to_u8(s[4])) << 32 | to_u8(s[3]) << 24 | to_u8(s[2]) << 16 | to_u8(s[1]) << 8 | to_u8(s[0]); + static_cast(to_u8(s[7]) << 24 | to_u8(s[6]) << 16 | to_u8(s[5]) << 8 | to_u8(s[4])) << 32 | to_u8(s[3]) << 24 | to_u8(s[2]) << 16 | to_u8(s[1]) << 8 | to_u8(s[0]); #endif } namespace fmt { [[noreturn]] void raw_error(const char* msg); - [[noreturn]] void raw_verify_error(const char* msg, uint position); + [[noreturn]] void raw_verify_error(const char* msg, const fmt_type_info* sup, u64 arg); [[noreturn]] void raw_narrow_error(const char* msg, const fmt_type_info* sup, u64 arg); } @@ -533,7 +536,7 @@ struct verify_impl // Verification (can be safely disabled) if (!verify_func()(std::forward(value))) { - fmt::raw_verify_error(cause, N); + fmt::raw_verify_error(cause, fmt::get_type_info(), N); } return verify_impl{cause}; @@ -548,11 +551,12 @@ inline auto verify(const char* cause) // Verification helper (returns value or lvalue reference, may require to use verify_move instead) template -inline T verify(T&& value, const char* cause, F&& func = F()) +inline T verify(const char* cause, T&& value, F&& pred = F()) { - if (!func(std::forward(value))) + if (!pred(std::forward(value))) { - fmt::raw_verify_error(cause, 0); + using unref = std::remove_const_t>; + fmt::raw_verify_error(cause, fmt::get_type_info>(), fmt_unveil::get(value)); } return std::forward(value); @@ -560,11 +564,12 @@ inline T verify(T&& value, const char* cause, F&& func = F()) // Verification helper (must be used in return expression or in place of std::move) template -inline std::remove_reference_t&& verify_move(T&& value, const char* cause, F&& func = F()) +inline std::remove_reference_t&& verify_move(const char* cause, T&& value, F&& pred = F()) { - if (!func(std::forward(value))) + if (!pred(std::forward(value))) { - fmt::raw_verify_error(cause, 0); + using unref = std::remove_const_t>; + fmt::raw_verify_error(cause, fmt::get_type_info>(), fmt_unveil::get(value)); } return std::move(value); @@ -635,28 +640,7 @@ struct narrow_impl::value && std } }; -// Enum to integer (TODO?) -template -struct narrow_impl::value && std::is_integral::value>> - : narrow_impl, To> -{ -}; - -// Integer to enum (TODO?) -template -struct narrow_impl::value && std::is_enum::value>> - : narrow_impl> -{ -}; - -// Enum to enum (TODO?) -template -struct narrow_impl::value && std::is_enum::value>> - : narrow_impl, std::underlying_type_t> -{ -}; - -// Simple type enabled (TODO?) +// Simple type enabled (TODO: allow for To as well) template struct narrow_impl> : narrow_impl, To> @@ -670,7 +654,7 @@ inline To narrow(const From& value, const char* msg = nullptr) if (narrow_impl::test(value)) { // Pack value as formatting argument - fmt::raw_narrow_error(msg, fmt::get_type_info::type>(), fmt_unveil::get(value)); + fmt::raw_narrow_error(msg, fmt::get_type_info>(), fmt_unveil::get(value)); } return static_cast(value); diff --git a/rpcs3/Emu/Cell/RawSPUThread.cpp b/rpcs3/Emu/Cell/RawSPUThread.cpp index d83d270b4d..cfd87cede3 100644 --- a/rpcs3/Emu/Cell/RawSPUThread.cpp +++ b/rpcs3/Emu/Cell/RawSPUThread.cpp @@ -30,7 +30,7 @@ void RawSPUThread::on_init(const std::shared_ptr& _this) { // Install correct SPU index and LS address const_cast(index) = id; - const_cast(offset) = verify(vm::falloc(RAW_SPU_BASE_ADDR + RAW_SPU_OFFSET * index, 0x40000), HERE); + const_cast(offset) = verify(HERE, vm::falloc(RAW_SPU_BASE_ADDR + RAW_SPU_OFFSET * index, 0x40000)); SPUThread::on_init(_this); } diff --git a/rpcs3/Emu/Cell/SPUThread.cpp b/rpcs3/Emu/Cell/SPUThread.cpp index f5f07c21a7..7658ca3577 100644 --- a/rpcs3/Emu/Cell/SPUThread.cpp +++ b/rpcs3/Emu/Cell/SPUThread.cpp @@ -249,7 +249,7 @@ SPUThread::SPUThread(const std::string& name, u32 index) : cpu_thread() , m_name(name) , index(index) - , offset(verify(vm::alloc(0x40000, vm::main), __func__)) + , offset(verify("SPU LS" HERE, vm::alloc(0x40000, vm::main))) { } diff --git a/rpcs3/Emu/Cell/lv2/sys_memory.cpp b/rpcs3/Emu/Cell/lv2/sys_memory.cpp index 68e3bdc63f..b018c0c342 100644 --- a/rpcs3/Emu/Cell/lv2/sys_memory.cpp +++ b/rpcs3/Emu/Cell/lv2/sys_memory.cpp @@ -48,7 +48,7 @@ ppu_error_code sys_memory_allocate(u32 size, u64 flags, vm::ptr alloc_addr) } // Allocate memory, write back the start address of the allocated area - *alloc_addr = verify(vm::alloc(size, vm::user_space, flags == SYS_MEMORY_PAGE_SIZE_1M ? 0x100000 : 0x10000), HERE); + *alloc_addr = verify(HERE, vm::alloc(size, vm::user_space, flags == SYS_MEMORY_PAGE_SIZE_1M ? 0x100000 : 0x10000)); return CELL_OK; } @@ -111,7 +111,7 @@ ppu_error_code sys_memory_allocate_from_container(u32 size, u32 cid, u64 flags, } // Allocate memory, write back the start address of the allocated area, use cid as the supplementary info - *alloc_addr = verify(vm::alloc(size, vm::user_space, flags == SYS_MEMORY_PAGE_SIZE_1M ? 0x100000 : 0x10000, cid), HERE); + *alloc_addr = verify(HERE, vm::alloc(size, vm::user_space, flags == SYS_MEMORY_PAGE_SIZE_1M ? 0x100000 : 0x10000, cid)); return CELL_OK; } diff --git a/rpcs3/Emu/PSP2/ARMv7Thread.cpp b/rpcs3/Emu/PSP2/ARMv7Thread.cpp index 58180ac32a..7a272ea81e 100644 --- a/rpcs3/Emu/PSP2/ARMv7Thread.cpp +++ b/rpcs3/Emu/PSP2/ARMv7Thread.cpp @@ -100,7 +100,7 @@ ARMv7Thread::ARMv7Thread(const std::string& name, u32 prio, u32 stack) , stack_addr(vm::alloc(stack, vm::main)) , stack_size(stack) { - verify(__func__), stack_size, stack_addr; + verify(HERE), stack_size, stack_addr; std::memset(GPR, 0, sizeof(GPR)); APSR.APSR = 0; diff --git a/rpcs3/Emu/RSX/D3D12/D3D12GSRender.cpp b/rpcs3/Emu/RSX/D3D12/D3D12GSRender.cpp index b5e8bded69..359666c4b3 100644 --- a/rpcs3/Emu/RSX/D3D12/D3D12GSRender.cpp +++ b/rpcs3/Emu/RSX/D3D12/D3D12GSRender.cpp @@ -44,13 +44,13 @@ HMODULE D3DCompiler; void loadD3D12FunctionPointers() { - D3D12Module = verify(LoadLibrary(L"d3d12.dll"), "d3d12.dll"); + D3D12Module = verify("d3d12.dll", LoadLibrary(L"d3d12.dll")); wrapD3D12CreateDevice = (PFN_D3D12_CREATE_DEVICE)GetProcAddress(D3D12Module, "D3D12CreateDevice"); wrapD3D12GetDebugInterface = (PFN_D3D12_GET_DEBUG_INTERFACE)GetProcAddress(D3D12Module, "D3D12GetDebugInterface"); wrapD3D12SerializeRootSignature = (PFN_D3D12_SERIALIZE_ROOT_SIGNATURE)GetProcAddress(D3D12Module, "D3D12SerializeRootSignature"); - D3D11Module = verify(LoadLibrary(L"d3d11.dll"), "d3d11.dll"); + D3D11Module = verify("d3d11.dll", LoadLibrary(L"d3d11.dll")); wrapD3D11On12CreateDevice = (PFN_D3D11ON12_CREATE_DEVICE)GetProcAddress(D3D11Module, "D3D11On12CreateDevice"); - D3DCompiler = verify(LoadLibrary(L"d3dcompiler_47.dll"), "d3dcompiler_47.dll"); + D3DCompiler = verify("d3dcompiler_47.dll", LoadLibrary(L"d3dcompiler_47.dll")); wrapD3DCompile = (pD3DCompile)GetProcAddress(D3DCompiler, "D3DCompile"); }