verify() fix

This commit is contained in:
Nekotekina 2016-08-15 13:18:47 +03:00
parent 05fb57baff
commit 56b9b38c9c
10 changed files with 55 additions and 68 deletions

View file

@ -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<int>(length) + 1, &out.front(), buf_size, NULL, NULL); const int result = WideCharToMultiByte(CP_UTF8, 0, source, static_cast<int>(length) + 1, &out.front(), buf_size, NULL, NULL);
// Fix the size // 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) static time_t to_time(const ULARGE_INTEGER& ft)

View file

@ -132,7 +132,7 @@ namespace fmt
throw std::runtime_error{msg}; 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"}; std::string out{"Verification failed"};
@ -149,15 +149,18 @@ namespace fmt
} }
#endif #endif
if (position) if (sup)
{ {
out += " (+"; out += " (";
out += std::to_string(position); sup->fmt_string(out, arg); // Print value
out += ")"; out += ")";
} }
out += ": "; if (msg)
out += msg; {
out += ": ";
out += msg;
}
throw std::runtime_error{out}; throw std::runtime_error{out};
} }
@ -166,13 +169,16 @@ namespace fmt
{ {
std::string out{"Narrow error"}; std::string out{"Narrow error"};
out += " ("; if (sup)
sup->fmt_string(out, arg); // Print value {
out += "): "; out += " (";
sup->fmt_string(out, arg); // Print value
out += ")";
}
if (msg) if (msg)
{ {
out += " "; out += ": ";
out += msg; out += msg;
} }

View file

@ -213,9 +213,6 @@ struct fmt_type_info
} }
}; };
template <typename Arg>
using fmt_unveil_t = typename fmt_unveil<Arg>::type;
// Argument array type (each element generated via fmt_unveil<>) // Argument array type (each element generated via fmt_unveil<>)
template <typename... Args> template <typename... Args>
using fmt_args_t = const u64(&&)[sizeof...(Args) + 1]; using fmt_args_t = const u64(&&)[sizeof...(Args) + 1];

View file

@ -16,9 +16,9 @@ namespace memory_helper
void* reserve_memory(size_t size) void* reserve_memory(size_t size)
{ {
#ifdef _WIN32 #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 #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 #endif
} }

View file

@ -96,6 +96,9 @@ namespace gsl
template <typename T, typename = void> template <typename T, typename = void>
struct fmt_unveil; struct fmt_unveil;
template <typename Arg>
using fmt_unveil_t = typename fmt_unveil<Arg>::type;
struct fmt_type_info; struct fmt_type_info;
namespace fmt namespace fmt
@ -443,25 +446,25 @@ struct ignore
}; };
template <typename T, typename = std::enable_if_t<std::is_integral<T>::value>> template <typename T, typename = std::enable_if_t<std::is_integral<T>::value>>
constexpr T align(const T& value, std::uint64_t align) constexpr T align(const T& value, ullong align)
{ {
return static_cast<T>((value + (align - 1)) & ~(align - 1)); return static_cast<T>((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) #ifdef _MSC_VER
unsigned long res; ulong res;
return _BitScanReverse(&res, arg) || nonzero ? res ^ 31 : 32; return _BitScanReverse(&res, arg) || nonzero ? res ^ 31 : 32;
#else #else
return arg || nonzero ? __builtin_clzll(arg) - 32 : 32; return arg || nonzero ? __builtin_clzll(arg) - 32 : 32;
#endif #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) #ifdef _MSC_VER
unsigned long res; ulong res;
return _BitScanReverse64(&res, arg) || nonzero ? res ^ 63 : 64; return _BitScanReverse64(&res, arg) || nonzero ? res ^ 63 : 64;
#else #else
return arg || nonzero ? __builtin_clzll(arg) : 64; 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 // Helper function, used by ""_u16, ""_u32, ""_u64
constexpr std::uint8_t to_u8(char c) constexpr u8 to_u8(char c)
{ {
return static_cast<std::uint8_t>(c); return static_cast<u8>(c);
} }
// Convert 2-byte string to u16 value like reinterpret_cast does // 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 : return length != 2 ? throw s :
#if IS_LE_MACHINE == 1 #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 // 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 : return length != 4 ? throw s :
#if IS_LE_MACHINE == 1 #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 // 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 : return length != 8 ? throw s :
#if IS_LE_MACHINE == 1 #if IS_LE_MACHINE == 1
static_cast<std::uint64_t>(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<u64>(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 #endif
} }
namespace fmt namespace fmt
{ {
[[noreturn]] void raw_error(const char* msg); [[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); [[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) // Verification (can be safely disabled)
if (!verify_func()(std::forward<T>(value))) if (!verify_func()(std::forward<T>(value)))
{ {
fmt::raw_verify_error(cause, N); fmt::raw_verify_error(cause, fmt::get_type_info<uint>(), N);
} }
return verify_impl<N + 1>{cause}; return verify_impl<N + 1>{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) // Verification helper (returns value or lvalue reference, may require to use verify_move instead)
template <typename F = verify_func, typename T> template <typename F = verify_func, typename T>
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<T>(value))) if (!pred(std::forward<T>(value)))
{ {
fmt::raw_verify_error(cause, 0); using unref = std::remove_const_t<std::remove_reference_t<T>>;
fmt::raw_verify_error(cause, fmt::get_type_info<fmt_unveil_t<unref>>(), fmt_unveil<unref>::get(value));
} }
return std::forward<T>(value); return std::forward<T>(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) // Verification helper (must be used in return expression or in place of std::move)
template <typename F = verify_func, typename T> template <typename F = verify_func, typename T>
inline std::remove_reference_t<T>&& verify_move(T&& value, const char* cause, F&& func = F()) inline std::remove_reference_t<T>&& verify_move(const char* cause, T&& value, F&& pred = F())
{ {
if (!func(std::forward<T>(value))) if (!pred(std::forward<T>(value)))
{ {
fmt::raw_verify_error(cause, 0); using unref = std::remove_const_t<std::remove_reference_t<T>>;
fmt::raw_verify_error(cause, fmt::get_type_info<fmt_unveil_t<unref>>(), fmt_unveil<unref>::get(value));
} }
return std::move(value); return std::move(value);
@ -635,28 +640,7 @@ struct narrow_impl<From, To, std::enable_if_t<std::is_signed<From>::value && std
} }
}; };
// Enum to integer (TODO?) // Simple type enabled (TODO: allow for To as well)
template <typename From, typename To>
struct narrow_impl<From, To, std::enable_if_t<std::is_enum<From>::value && std::is_integral<To>::value>>
: narrow_impl<std::underlying_type_t<From>, To>
{
};
// Integer to enum (TODO?)
template <typename From, typename To>
struct narrow_impl<From, To, std::enable_if_t<std::is_integral<From>::value && std::is_enum<To>::value>>
: narrow_impl<From, std::underlying_type_t<To>>
{
};
// Enum to enum (TODO?)
template <typename From, typename To>
struct narrow_impl<From, To, std::enable_if_t<std::is_enum<From>::value && std::is_enum<To>::value>>
: narrow_impl<std::underlying_type_t<From>, std::underlying_type_t<To>>
{
};
// Simple type enabled (TODO?)
template <typename From, typename To> template <typename From, typename To>
struct narrow_impl<From, To, void_t<typename From::simple_type>> struct narrow_impl<From, To, void_t<typename From::simple_type>>
: narrow_impl<simple_t<From>, To> : narrow_impl<simple_t<From>, To>
@ -670,7 +654,7 @@ inline To narrow(const From& value, const char* msg = nullptr)
if (narrow_impl<From, To>::test(value)) if (narrow_impl<From, To>::test(value))
{ {
// Pack value as formatting argument // Pack value as formatting argument
fmt::raw_narrow_error(msg, fmt::get_type_info<typename fmt_unveil<From>::type>(), fmt_unveil<From>::get(value)); fmt::raw_narrow_error(msg, fmt::get_type_info<fmt_unveil_t<From>>(), fmt_unveil<From>::get(value));
} }
return static_cast<To>(value); return static_cast<To>(value);

View file

@ -30,7 +30,7 @@ void RawSPUThread::on_init(const std::shared_ptr<void>& _this)
{ {
// Install correct SPU index and LS address // Install correct SPU index and LS address
const_cast<u32&>(index) = id; const_cast<u32&>(index) = id;
const_cast<u32&>(offset) = verify(vm::falloc(RAW_SPU_BASE_ADDR + RAW_SPU_OFFSET * index, 0x40000), HERE); const_cast<u32&>(offset) = verify(HERE, vm::falloc(RAW_SPU_BASE_ADDR + RAW_SPU_OFFSET * index, 0x40000));
SPUThread::on_init(_this); SPUThread::on_init(_this);
} }

View file

@ -249,7 +249,7 @@ SPUThread::SPUThread(const std::string& name, u32 index)
: cpu_thread() : cpu_thread()
, m_name(name) , m_name(name)
, index(index) , index(index)
, offset(verify(vm::alloc(0x40000, vm::main), __func__)) , offset(verify("SPU LS" HERE, vm::alloc(0x40000, vm::main)))
{ {
} }

View file

@ -48,7 +48,7 @@ ppu_error_code sys_memory_allocate(u32 size, u64 flags, vm::ptr<u32> alloc_addr)
} }
// Allocate memory, write back the start address of the allocated area // 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; 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 // 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; return CELL_OK;
} }

View file

@ -100,7 +100,7 @@ ARMv7Thread::ARMv7Thread(const std::string& name, u32 prio, u32 stack)
, stack_addr(vm::alloc(stack, vm::main)) , stack_addr(vm::alloc(stack, vm::main))
, stack_size(stack) , stack_size(stack)
{ {
verify(__func__), stack_size, stack_addr; verify(HERE), stack_size, stack_addr;
std::memset(GPR, 0, sizeof(GPR)); std::memset(GPR, 0, sizeof(GPR));
APSR.APSR = 0; APSR.APSR = 0;

View file

@ -44,13 +44,13 @@ HMODULE D3DCompiler;
void loadD3D12FunctionPointers() 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"); wrapD3D12CreateDevice = (PFN_D3D12_CREATE_DEVICE)GetProcAddress(D3D12Module, "D3D12CreateDevice");
wrapD3D12GetDebugInterface = (PFN_D3D12_GET_DEBUG_INTERFACE)GetProcAddress(D3D12Module, "D3D12GetDebugInterface"); wrapD3D12GetDebugInterface = (PFN_D3D12_GET_DEBUG_INTERFACE)GetProcAddress(D3D12Module, "D3D12GetDebugInterface");
wrapD3D12SerializeRootSignature = (PFN_D3D12_SERIALIZE_ROOT_SIGNATURE)GetProcAddress(D3D12Module, "D3D12SerializeRootSignature"); 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"); 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"); wrapD3DCompile = (pD3DCompile)GetProcAddress(D3DCompiler, "D3DCompile");
} }