Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite verify() #9399

Merged
merged 3 commits into from
Dec 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Utilities/Config.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "stdafx.h"
#include "stdafx.h"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought BOM was removed ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can check yourself

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think Utilities/ was untouched

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes

#include "Config.h"
#include "Utilities/types.h"

Expand Down
48 changes: 24 additions & 24 deletions Utilities/File.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "File.h"
#include "File.h"
#include "mutex.h"
#include "StrFmt.h"
#include "BEType.h"
Expand All @@ -24,7 +24,7 @@ static std::unique_ptr<wchar_t[]> to_wchar(const std::string& source)
const std::size_t buf_size = source.size() + 1;

// Safe size
const int size = narrow<int>(buf_size, "to_wchar" HERE);
const int size = narrow<int>(buf_size);

// Buffer for max possible output length
std::unique_ptr<wchar_t[]> buffer(new wchar_t[buf_size + 8 + 32768]);
Expand All @@ -41,10 +41,10 @@ static std::unique_ptr<wchar_t[]> to_wchar(const std::string& source)
std::memcpy(buffer.get() + 32768 + 4, L"UNC\\", 4 * sizeof(wchar_t));
}

verify("to_wchar" HERE), MultiByteToWideChar(CP_UTF8, 0, source.c_str(), size, buffer.get() + 32768 + (unc ? 8 : 4), size);
ensure(MultiByteToWideChar(CP_UTF8, 0, source.c_str(), size, buffer.get() + 32768 + (unc ? 8 : 4), size)); // "to_wchar"

// Canonicalize wide path (replace '/', ".", "..", \\ repetitions, etc)
verify("to_wchar" HERE), GetFullPathNameW(buffer.get() + 32768, 32768, buffer.get(), nullptr) - 1 < 32768 - 1;
ensure(GetFullPathNameW(buffer.get() + 32768, 32768, buffer.get(), nullptr) - 1 < 32768 - 1); // "to_wchar"

return buffer;
}
Expand All @@ -55,15 +55,15 @@ static void to_utf8(std::string& out, const wchar_t* source)
const std::size_t length = std::wcslen(source);

// Safe buffer size for max possible output length (including null terminator)
const int buf_size = narrow<int>(length * 3 + 1, "to_utf8" HERE);
const int buf_size = narrow<int>(length * 3 + 1);

// Resize buffer
out.resize(buf_size - 1);

const int result = WideCharToMultiByte(CP_UTF8, 0, source, static_cast<int>(length) + 1, &out.front(), buf_size, NULL, NULL);

// Fix the size
out.resize(verify("to_utf8" HERE, result) - 1);
out.resize(ensure(result) - 1);
}

static time_t to_time(const ULARGE_INTEGER& ft)
Expand Down Expand Up @@ -315,7 +315,7 @@ std::shared_ptr<fs::device_base> fs::get_virtual_device(const std::string& path)

std::shared_ptr<fs::device_base> fs::set_virtual_device(const std::string& name, const std::shared_ptr<device_base>& device)
{
verify(HERE), name.starts_with("//"), name[2] != '/';
ensure(name.starts_with("//") && name[2] != '/');

return get_device_manager().set_device(name, device);
}
Expand Down Expand Up @@ -355,7 +355,7 @@ bool fs::stat(const std::string& path, stat_t& info)
if (!GetFileAttributesExW(to_wchar(std::string(epath) + '/').get(), GetFileExInfoStandard, &attrs))
{
g_tls_error = to_error(GetLastError());
return false;
return false;
}

info.is_directory = true; // Handle drives as directories
Expand Down Expand Up @@ -404,7 +404,7 @@ bool fs::stat(const std::string& path, stat_t& info)
if (const DWORD err = GetLastError(); err != ERROR_NO_MORE_FILES)
{
g_tls_error = to_error(err);
return false;
return false;
}

g_tls_error = fs::error::noent;
Expand Down Expand Up @@ -990,7 +990,7 @@ fs::file::file(const std::string& path, bs_t<open_mode> mode)
stat_t stat() override
{
FILE_BASIC_INFO basic_info;
verify("file::stat" HERE), GetFileInformationByHandleEx(m_handle, FileBasicInfo, &basic_info, sizeof(FILE_BASIC_INFO));
ensure(GetFileInformationByHandleEx(m_handle, FileBasicInfo, &basic_info, sizeof(FILE_BASIC_INFO))); // "file::stat"

stat_t info;
info.is_directory = (basic_info.FileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
Expand All @@ -1008,7 +1008,7 @@ fs::file::file(const std::string& path, bs_t<open_mode> mode)

void sync() override
{
verify("file::sync" HERE), FlushFileBuffers(m_handle);
ensure(FlushFileBuffers(m_handle)); // "file::sync"
}

bool trunc(u64 length) override
Expand All @@ -1028,21 +1028,21 @@ fs::file::file(const std::string& path, bs_t<open_mode> mode)
u64 read(void* buffer, u64 count) override
{
// TODO (call ReadFile multiple times if count is too big)
const int size = narrow<int>(count, "file::read" HERE);
const int size = narrow<int>(count);

DWORD nread;
verify("file::read" HERE), ReadFile(m_handle, buffer, size, &nread, NULL);
ensure(ReadFile(m_handle, buffer, size, &nread, NULL)); // "file::read"

return nread;
}

u64 write(const void* buffer, u64 count) override
{
// TODO (call WriteFile multiple times if count is too big)
const int size = narrow<int>(count, "file::write" HERE);
const int size = narrow<int>(count);

DWORD nwritten;
verify("file::write" HERE), WriteFile(m_handle, buffer, size, &nwritten, NULL);
ensure(WriteFile(m_handle, buffer, size, &nwritten, NULL)); // "file::write"

return nwritten;
}
Expand Down Expand Up @@ -1070,7 +1070,7 @@ fs::file::file(const std::string& path, bs_t<open_mode> mode)
u64 size() override
{
LARGE_INTEGER size;
verify("file::size" HERE), GetFileSizeEx(m_handle, &size);
ensure(GetFileSizeEx(m_handle, &size)); // "file::size"

return size.QuadPart;
}
Expand Down Expand Up @@ -1119,7 +1119,7 @@ fs::file::file(const std::string& path, bs_t<open_mode> mode)
if (mode & fs::trunc && mode & (fs::lock + fs::unread) && mode & fs::write)
{
// Postpone truncation in order to avoid using O_TRUNC on a locked file
verify(HERE), ::ftruncate(fd, 0) == 0;
ensure(::ftruncate(fd, 0) == 0);
}

class unix_file final : public file_base
Expand All @@ -1140,7 +1140,7 @@ fs::file::file(const std::string& path, bs_t<open_mode> mode)
stat_t stat() override
{
struct ::stat file_info;
verify("file::stat" HERE), ::fstat(m_fd, &file_info) == 0;
ensure(::fstat(m_fd, &file_info) == 0); // "file::stat"

stat_t info;
info.is_directory = S_ISDIR(file_info.st_mode);
Expand All @@ -1158,7 +1158,7 @@ fs::file::file(const std::string& path, bs_t<open_mode> mode)

void sync() override
{
verify("file::sync" HERE), ::fsync(m_fd) == 0;
ensure(::fsync(m_fd) == 0); // "file::sync"
}

bool trunc(u64 length) override
Expand All @@ -1175,15 +1175,15 @@ fs::file::file(const std::string& path, bs_t<open_mode> mode)
u64 read(void* buffer, u64 count) override
{
const auto result = ::read(m_fd, buffer, count);
verify("file::read" HERE), result != -1;
ensure(result != -1); // "file::read"

return result;
}

u64 write(const void* buffer, u64 count) override
{
const auto result = ::write(m_fd, buffer, count);
verify("file::write" HERE), result != -1;
ensure(result != -1); // "file::write"

return result;
}
Expand All @@ -1210,7 +1210,7 @@ fs::file::file(const std::string& path, bs_t<open_mode> mode)
u64 size() override
{
struct ::stat file_info;
verify("file::size" HERE), ::fstat(m_fd, &file_info) == 0;
ensure(::fstat(m_fd, &file_info) == 0); // "file::size"

return file_info.st_size;
}
Expand All @@ -1226,7 +1226,7 @@ fs::file::file(const std::string& path, bs_t<open_mode> mode)
static_assert(offsetof(iovec, iov_len) == offsetof(iovec_clone, iov_len), "Weird iovec::iov_len offset");

const auto result = ::writev(m_fd, reinterpret_cast<const iovec*>(buffers), buf_count);
verify("file::write_gather" HERE), result != -1;
ensure(result != -1); // "file::write_gather"

return result;
}
Expand Down Expand Up @@ -1386,7 +1386,7 @@ bool fs::dir::open(const std::string& path)
add_entry(found);
}

verify("dir::read" HERE), ERROR_NO_MORE_FILES == GetLastError();
ensure(ERROR_NO_MORE_FILES == GetLastError()); // "dir::read"
FindClose(handle);
}

Expand Down
4 changes: 2 additions & 2 deletions Utilities/JIT.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "types.h"
#include "types.h"
#include "JIT.h"
#include "StrFmt.h"
#include "File.h"
Expand Down Expand Up @@ -452,7 +452,7 @@ class ObjectCache final : public llvm::ObjectCache
name.append(".gz");

z_stream zs{};
uLong zsz = compressBound(::narrow<u32>(obj.getBufferSize(), HERE)) + 256;
uLong zsz = compressBound(::narrow<u32>(obj.getBufferSize())) + 256;
auto zbuf = std::make_unique<uchar[]>(zsz);
#ifndef _MSC_VER
#pragma GCC diagnostic push
Expand Down
47 changes: 32 additions & 15 deletions Utilities/StrFmt.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "StrFmt.h"
#include "StrFmt.h"
#include "BEType.h"
#include "StrUtil.h"
#include "cfmt.h"
Expand Down Expand Up @@ -249,40 +249,45 @@ namespace fmt
thread_ctrl::emergency_exit(msg);
}

void raw_verify_error(const char* msg, const fmt_type_info* sup, u64 arg)
void raw_verify_error(const src_loc& loc)
{
std::string out{"Verification failed"};

// Print error code (may be irrelevant)
#ifdef _WIN32
if (DWORD error = GetLastError())
{
fmt::append(out, " (e=%#x)", error);
fmt::append(out, " (e=%#x):", error);
}
#else
if (int error = errno)
{
fmt::append(out, " (e=%d)", error);
fmt::append(out, " (e=%d):", error);
}
#endif

if (sup)
if (loc.col != umax)
{
out += " (";
sup->fmt_string(out, arg); // Print value
out += ")";
fmt::append(out, "\n(in file %s:%s[:%s]", loc.file, loc.line, loc.col);
}
else
{
fmt::append(out, "\n(in file %s:%s", loc.file, loc.line);
}

if (msg)
if (loc.func && *loc.func)
{
fmt::append(out, ", in function %s)", loc.func);
}
else
{
out += ": ";
out += msg;
out += ')';
}

thread_ctrl::emergency_exit(out);
}

void raw_narrow_error(const char* msg, const fmt_type_info* sup, u64 arg)
void raw_narrow_error(const src_loc& loc, const fmt_type_info* sup, u64 arg)
{
std::string out{"Narrow error"};

Expand All @@ -293,10 +298,22 @@ namespace fmt
out += ")";
}

if (msg)
if (loc.col != umax)
{
fmt::append(out, "\n(in file %s:%s[:%s]", loc.file, loc.line, loc.col);
}
else
{
fmt::append(out, "\n(in file %s:%s", loc.file, loc.line);
}

if (loc.func && *loc.func)
{
fmt::append(out, ", in function %s)", loc.func);
}
else
{
out += ": ";
out += msg;
out += ')';
}

thread_ctrl::emergency_exit(out);
Expand Down
9 changes: 5 additions & 4 deletions Utilities/Thread.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "stdafx.h"
#include "stdafx.h"
#include "Emu/System.h"
#include "Emu/Cell/SPUThread.h"
#include "Emu/Cell/PPUThread.h"
Expand Down Expand Up @@ -1872,17 +1872,18 @@ void thread_base::start()
// Receive "that" native thread handle, sent "this" thread_base
const u64 _self = reinterpret_cast<u64>(atomic_storage<thread_base*>::load(*tls));
m_thread.release(_self);
verify(HERE), _self != reinterpret_cast<u64>(this);
ensure(_self != reinterpret_cast<u64>(this));
atomic_storage<thread_base*>::store(*tls, this);
s_thread_pool[pos].notify_one();
return;
}

#ifdef _WIN32
m_thread = ::_beginthreadex(nullptr, 0, entry_point, this, CREATE_SUSPENDED, nullptr);
verify("thread_ctrl::start" HERE), m_thread, ::ResumeThread(reinterpret_cast<HANDLE>(+m_thread)) != -1;
ensure(m_thread);
ensure(::ResumeThread(reinterpret_cast<HANDLE>(+m_thread)) != -1);
#else
verify("thread_ctrl::start" HERE), pthread_create(reinterpret_cast<pthread_t*>(&m_thread.raw()), nullptr, entry_point, this) == 0;
ensure(pthread_create(reinterpret_cast<pthread_t*>(&m_thread.raw()), nullptr, entry_point, this) == 0);
#endif
}

Expand Down
4 changes: 2 additions & 2 deletions Utilities/cond.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "cond.h"
#include "cond.h"
#include "sync.h"
#include "lockless.h"

Expand All @@ -9,7 +9,7 @@
void cond_variable::imp_wait(u32 _old, u64 _timeout) noexcept
{
// Not supposed to fail
verify(HERE), _old;
ensure(_old);

// Wait with timeout
m_value.wait(_old, c_signal_mask, atomic_wait_timeout{_timeout > max_timeout ? UINT64_MAX : _timeout * 1000});
Expand Down
Loading