mirror of
https://github.com/RPCS3/rpcs3
synced 2024-09-30 05:23:36 +00:00
LLVM: Prevent crash on disk space shortage
This commit is contained in:
parent
c556322fc5
commit
26b21ed031
|
@ -1,6 +1,7 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "util/types.hpp"
|
#include "util/types.hpp"
|
||||||
|
#include "util/atomic.hpp"
|
||||||
|
|
||||||
// Include asmjit with warnings ignored
|
// Include asmjit with warnings ignored
|
||||||
#define ASMJIT_EMBED
|
#define ASMJIT_EMBED
|
||||||
|
@ -508,6 +509,9 @@ class jit_compiler final
|
||||||
// Arch
|
// Arch
|
||||||
std::string m_cpu{};
|
std::string m_cpu{};
|
||||||
|
|
||||||
|
// Disk Space left
|
||||||
|
atomic_t<usz> m_disk_space = umax;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
jit_compiler(const std::unordered_map<std::string, u64>& _link, const std::string& _cpu, u32 flags = 0);
|
jit_compiler(const std::unordered_map<std::string, u64>& _link, const std::string& _cpu, u32 flags = 0);
|
||||||
~jit_compiler();
|
~jit_compiler();
|
||||||
|
@ -530,7 +534,7 @@ public:
|
||||||
void add(std::unique_ptr<llvm::Module> _module);
|
void add(std::unique_ptr<llvm::Module> _module);
|
||||||
|
|
||||||
// Add object (path to obj file)
|
// Add object (path to obj file)
|
||||||
void add(const std::string& path);
|
bool add(const std::string& path);
|
||||||
|
|
||||||
// Update global mapping for a single value
|
// Update global mapping for a single value
|
||||||
void update_global_mapping(const std::string& name, u64 addr);
|
void update_global_mapping(const std::string& name, u64 addr);
|
||||||
|
@ -552,6 +556,8 @@ public:
|
||||||
|
|
||||||
// Get system triple (SPU)
|
// Get system triple (SPU)
|
||||||
static std::string triple2();
|
static std::string triple2();
|
||||||
|
|
||||||
|
bool add_sub_disk_space(ssz space);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // LLVM_AVAILABLE
|
#endif // LLVM_AVAILABLE
|
||||||
|
|
|
@ -320,10 +320,12 @@ struct MemoryManager2 : llvm::RTDyldMemoryManager
|
||||||
class ObjectCache final : public llvm::ObjectCache
|
class ObjectCache final : public llvm::ObjectCache
|
||||||
{
|
{
|
||||||
const std::string& m_path;
|
const std::string& m_path;
|
||||||
|
const std::add_pointer_t<jit_compiler> m_compiler = nullptr;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
ObjectCache(const std::string& path)
|
ObjectCache(const std::string& path, jit_compiler* compiler = nullptr)
|
||||||
: m_path(path)
|
: m_path(path)
|
||||||
|
, m_compiler(compiler)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -332,10 +334,19 @@ public:
|
||||||
void notifyObjectCompiled(const llvm::Module* _module, llvm::MemoryBufferRef obj) override
|
void notifyObjectCompiled(const llvm::Module* _module, llvm::MemoryBufferRef obj) override
|
||||||
{
|
{
|
||||||
std::string name = m_path;
|
std::string name = m_path;
|
||||||
|
|
||||||
name.append(_module->getName().data());
|
name.append(_module->getName().data());
|
||||||
//fs::file(name, fs::rewrite).write(obj.getBufferStart(), obj.getBufferSize());
|
//fs::file(name, fs::rewrite).write(obj.getBufferStart(), obj.getBufferSize());
|
||||||
name.append(".gz");
|
name.append(".gz");
|
||||||
|
|
||||||
|
if (!obj.getBufferSize())
|
||||||
|
{
|
||||||
|
jit_log.error("LLVM: Nothing to write: %s", name);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
ensure(m_compiler);
|
||||||
|
|
||||||
fs::file module_file(name, fs::rewrite);
|
fs::file module_file(name, fs::rewrite);
|
||||||
|
|
||||||
if (!module_file)
|
if (!module_file)
|
||||||
|
@ -344,6 +355,15 @@ public:
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Bold assumption about upper limit of space consumption
|
||||||
|
const usz max_size = obj.getBufferSize() * 4;
|
||||||
|
|
||||||
|
if (!m_compiler->add_sub_disk_space(0 - max_size))
|
||||||
|
{
|
||||||
|
jit_log.error("LLVM: Failed to create module file: %s (not enough disk space left)", name);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (!zip(obj.getBufferStart(), obj.getBufferSize(), module_file))
|
if (!zip(obj.getBufferStart(), obj.getBufferSize(), module_file))
|
||||||
{
|
{
|
||||||
jit_log.error("LLVM: Failed to compress module: %s", _module->getName().data());
|
jit_log.error("LLVM: Failed to compress module: %s", _module->getName().data());
|
||||||
|
@ -353,6 +373,9 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
jit_log.notice("LLVM: Created module: %s", _module->getName().data());
|
jit_log.notice("LLVM: Created module: %s", _module->getName().data());
|
||||||
|
|
||||||
|
// Restore space that was overestimated
|
||||||
|
ensure(m_compiler->add_sub_disk_space(max_size - module_file.size()));
|
||||||
}
|
}
|
||||||
|
|
||||||
static std::unique_ptr<llvm::MemoryBuffer> load(const std::string& path)
|
static std::unique_ptr<llvm::MemoryBuffer> load(const std::string& path)
|
||||||
|
@ -388,7 +411,6 @@ public:
|
||||||
|
|
||||||
auto buf = llvm::WritableMemoryBuffer::getNewUninitMemBuffer(cached.size());
|
auto buf = llvm::WritableMemoryBuffer::getNewUninitMemBuffer(cached.size());
|
||||||
cached.read(buf->getBufferStart(), buf->getBufferSize());
|
cached.read(buf->getBufferStart(), buf->getBufferSize());
|
||||||
|
|
||||||
return buf;
|
return buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -510,6 +532,26 @@ std::string jit_compiler::triple2()
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool jit_compiler::add_sub_disk_space(ssz space)
|
||||||
|
{
|
||||||
|
if (space >= 0)
|
||||||
|
{
|
||||||
|
ensure(m_disk_space.fetch_add(space) < ~static_cast<usz>(space));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return m_disk_space.fetch_op([sub_size = static_cast<usz>(0 - space)](usz& val)
|
||||||
|
{
|
||||||
|
if (val >= sub_size)
|
||||||
|
{
|
||||||
|
val -= sub_size;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}).second;
|
||||||
|
}
|
||||||
|
|
||||||
jit_compiler::jit_compiler(const std::unordered_map<std::string, u64>& _link, const std::string& _cpu, u32 flags)
|
jit_compiler::jit_compiler(const std::unordered_map<std::string, u64>& _link, const std::string& _cpu, u32 flags)
|
||||||
: m_context(new llvm::LLVMContext)
|
: m_context(new llvm::LLVMContext)
|
||||||
, m_cpu(cpu(_cpu))
|
, m_cpu(cpu(_cpu))
|
||||||
|
@ -576,6 +618,13 @@ jit_compiler::jit_compiler(const std::unordered_map<std::string, u64>& _link, co
|
||||||
{
|
{
|
||||||
fmt::throw_exception("LLVM: Failed to create ExecutionEngine: %s", result);
|
fmt::throw_exception("LLVM: Failed to create ExecutionEngine: %s", result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fs::device_stat stats{};
|
||||||
|
|
||||||
|
if (fs::statfs(fs::get_cache_dir(), stats))
|
||||||
|
{
|
||||||
|
m_disk_space = stats.avail_free / 4;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
jit_compiler::~jit_compiler()
|
jit_compiler::~jit_compiler()
|
||||||
|
@ -584,7 +633,7 @@ jit_compiler::~jit_compiler()
|
||||||
|
|
||||||
void jit_compiler::add(std::unique_ptr<llvm::Module> _module, const std::string& path)
|
void jit_compiler::add(std::unique_ptr<llvm::Module> _module, const std::string& path)
|
||||||
{
|
{
|
||||||
ObjectCache cache{path};
|
ObjectCache cache{path, this};
|
||||||
m_engine->setObjectCache(&cache);
|
m_engine->setObjectCache(&cache);
|
||||||
|
|
||||||
const auto ptr = _module.get();
|
const auto ptr = _module.get();
|
||||||
|
@ -612,23 +661,26 @@ void jit_compiler::add(std::unique_ptr<llvm::Module> _module)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void jit_compiler::add(const std::string& path)
|
bool jit_compiler::add(const std::string& path)
|
||||||
{
|
{
|
||||||
auto cache = ObjectCache::load(path);
|
auto cache = ObjectCache::load(path);
|
||||||
|
|
||||||
if (!cache)
|
if (!cache)
|
||||||
{
|
{
|
||||||
jit_log.error("ObjectCache: Failed to read file. (path='%s', error=%s)", path, fs::g_tls_error);
|
jit_log.error("ObjectCache: Failed to read file. (path='%s', error=%s)", path, fs::g_tls_error);
|
||||||
return;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (auto object_file = llvm::object::ObjectFile::createObjectFile(*cache))
|
if (auto object_file = llvm::object::ObjectFile::createObjectFile(*cache))
|
||||||
{
|
{
|
||||||
m_engine->addObjectFile(llvm::object::OwningBinary<llvm::object::ObjectFile>(std::move(*object_file), std::move(cache)));
|
m_engine->addObjectFile(llvm::object::OwningBinary<llvm::object::ObjectFile>(std::move(*object_file), std::move(cache)));
|
||||||
|
jit_log.trace("ObjectCache: Successfully added %s", path);
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
jit_log.error("ObjectCache: Adding failed: %s", path);
|
jit_log.error("ObjectCache: Adding failed: %s", path);
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5011,6 +5011,7 @@ bool ppu_initialize(const ppu_module& info, bool check_only, u64 file_size)
|
||||||
g_watchdog_hold_ctr--;
|
g_watchdog_hold_ctr--;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool failed_to_load = false;
|
||||||
{
|
{
|
||||||
if (!is_being_used_in_emulation || (cpu ? cpu->state.all_of(cpu_flag::exit) : Emu.IsStopped()))
|
if (!is_being_used_in_emulation || (cpu ? cpu->state.all_of(cpu_flag::exit) : Emu.IsStopped()))
|
||||||
{
|
{
|
||||||
|
@ -5030,7 +5031,21 @@ bool ppu_initialize(const ppu_module& info, bool check_only, u64 file_size)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
jit->add(cache_path + obj_name);
|
if (!failed_to_load && !jit->add(cache_path + obj_name))
|
||||||
|
{
|
||||||
|
ppu_log.error("LLVM: Failed to load module %s", obj_name);
|
||||||
|
failed_to_load = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (failed_to_load)
|
||||||
|
{
|
||||||
|
if (!is_compiled)
|
||||||
|
{
|
||||||
|
g_progr_pdone++;
|
||||||
|
}
|
||||||
|
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
if (!is_compiled)
|
if (!is_compiled)
|
||||||
{
|
{
|
||||||
|
@ -5040,7 +5055,7 @@ bool ppu_initialize(const ppu_module& info, bool check_only, u64 file_size)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!is_being_used_in_emulation || (cpu ? cpu->state.all_of(cpu_flag::exit) : Emu.IsStopped()))
|
if (failed_to_load || !is_being_used_in_emulation || (cpu ? cpu->state.all_of(cpu_flag::exit) : Emu.IsStopped()))
|
||||||
{
|
{
|
||||||
return compiled_new;
|
return compiled_new;
|
||||||
}
|
}
|
||||||
|
@ -5073,6 +5088,7 @@ bool ppu_initialize(const ppu_module& info, bool check_only, u64 file_size)
|
||||||
if (is_first)
|
if (is_first)
|
||||||
{
|
{
|
||||||
jit_mod.symbol_resolver = reinterpret_cast<void(*)(u8*, u64)>(jit->get("__resolve_symbols"));
|
jit_mod.symbol_resolver = reinterpret_cast<void(*)(u8*, u64)>(jit->get("__resolve_symbols"));
|
||||||
|
ensure(jit_mod.symbol_resolver);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
|
@ -141,6 +141,7 @@ using s8 = std::int8_t;
|
||||||
using s16 = std::int16_t;
|
using s16 = std::int16_t;
|
||||||
using s32 = std::int32_t;
|
using s32 = std::int32_t;
|
||||||
using s64 = std::int64_t;
|
using s64 = std::int64_t;
|
||||||
|
using ssz = std::make_signed_t<std::size_t>;
|
||||||
|
|
||||||
// Get integral type from type size
|
// Get integral type from type size
|
||||||
template <usz N>
|
template <usz N>
|
||||||
|
|
Loading…
Reference in a new issue