Run clang-format-8 over the C++ code. No functionality change.

This commit is contained in:
Nick Lewycky 2019-08-01 17:12:46 -07:00
parent a224d53c93
commit ac49e57c2d
2 changed files with 343 additions and 372 deletions

View File

@ -17,28 +17,31 @@ public:
callbacks.dealloc_memory(readwrite_section.base, readwrite_section.size); callbacks.dealloc_memory(readwrite_section.base, readwrite_section.size);
} }
virtual uint8_t* allocateCodeSection(uintptr_t size, unsigned alignment, unsigned section_id, llvm::StringRef section_name) override { virtual uint8_t *allocateCodeSection(uintptr_t size, unsigned alignment,
unsigned section_id,
llvm::StringRef section_name) override {
return allocate_bump(code_section, code_bump_ptr, size, alignment); return allocate_bump(code_section, code_bump_ptr, size, alignment);
} }
virtual uint8_t* allocateDataSection(uintptr_t size, unsigned alignment, unsigned section_id, llvm::StringRef section_name, bool read_only) override { virtual uint8_t *allocateDataSection(uintptr_t size, unsigned alignment,
// Allocate from the read-only section or the read-write section, depending on if this allocation unsigned section_id,
// should be read-only or not. llvm::StringRef section_name,
bool read_only) override {
// Allocate from the read-only section or the read-write section, depending
// on if this allocation should be read-only or not.
if (read_only) { if (read_only) {
return allocate_bump(read_section, read_bump_ptr, size, alignment); return allocate_bump(read_section, read_bump_ptr, size, alignment);
} else { } else {
return allocate_bump(readwrite_section, readwrite_bump_ptr, size, alignment); return allocate_bump(readwrite_section, readwrite_bump_ptr, size,
alignment);
} }
} }
virtual void reserveAllocationSpace( virtual void reserveAllocationSpace(uintptr_t code_size, uint32_t code_align,
uintptr_t code_size,
uint32_t code_align,
uintptr_t read_data_size, uintptr_t read_data_size,
uint32_t read_data_align, uint32_t read_data_align,
uintptr_t read_write_data_size, uintptr_t read_write_data_size,
uint32_t read_write_data_align uint32_t read_write_data_align) override {
) override {
auto aligner = [](uintptr_t ptr, size_t align) { auto aligner = [](uintptr_t ptr, size_t align) {
if (ptr == 0) { if (ptr == 0) {
return align; return align;
@ -46,35 +49,39 @@ public:
return (ptr + align - 1) & ~(align - 1); return (ptr + align - 1) & ~(align - 1);
}; };
uint8_t *code_ptr_out = nullptr; uint8_t *code_ptr_out = nullptr;
size_t code_size_out = 0; size_t code_size_out = 0;
auto code_result = callbacks.alloc_memory(aligner(code_size, 4096), PROTECT_READ_WRITE, &code_ptr_out, &code_size_out); auto code_result =
callbacks.alloc_memory(aligner(code_size, 4096), PROTECT_READ_WRITE,
&code_ptr_out, &code_size_out);
assert(code_result == RESULT_OK); assert(code_result == RESULT_OK);
code_section = Section{code_ptr_out, code_size_out}; code_section = Section{code_ptr_out, code_size_out};
code_bump_ptr = (uintptr_t)code_ptr_out; code_bump_ptr = (uintptr_t)code_ptr_out;
uint8_t *read_ptr_out = nullptr; uint8_t *read_ptr_out = nullptr;
size_t read_size_out = 0; size_t read_size_out = 0;
auto read_result = callbacks.alloc_memory(aligner(read_data_size, 4096), PROTECT_READ_WRITE, &read_ptr_out, &read_size_out); auto read_result = callbacks.alloc_memory(aligner(read_data_size, 4096),
PROTECT_READ_WRITE, &read_ptr_out,
&read_size_out);
assert(read_result == RESULT_OK); assert(read_result == RESULT_OK);
read_section = Section{read_ptr_out, read_size_out}; read_section = Section{read_ptr_out, read_size_out};
read_bump_ptr = (uintptr_t)read_ptr_out; read_bump_ptr = (uintptr_t)read_ptr_out;
uint8_t *readwrite_ptr_out = nullptr; uint8_t *readwrite_ptr_out = nullptr;
size_t readwrite_size_out = 0; size_t readwrite_size_out = 0;
auto readwrite_result = callbacks.alloc_memory(aligner(read_write_data_size, 4096), PROTECT_READ_WRITE, &readwrite_ptr_out, &readwrite_size_out); auto readwrite_result = callbacks.alloc_memory(
aligner(read_write_data_size, 4096), PROTECT_READ_WRITE,
&readwrite_ptr_out, &readwrite_size_out);
assert(readwrite_result == RESULT_OK); assert(readwrite_result == RESULT_OK);
readwrite_section = Section{readwrite_ptr_out, readwrite_size_out}; readwrite_section = Section{readwrite_ptr_out, readwrite_size_out};
readwrite_bump_ptr = (uintptr_t)readwrite_ptr_out; readwrite_bump_ptr = (uintptr_t)readwrite_ptr_out;
} }
/* Turn on the `reserveAllocationSpace` callback. */ /* Turn on the `reserveAllocationSpace` callback. */
virtual bool needsToReserveAllocationSpace() override { virtual bool needsToReserveAllocationSpace() override { return true; }
return true;
}
virtual void registerEHFrames(uint8_t* addr, uint64_t LoadAddr, size_t size) override { virtual void registerEHFrames(uint8_t *addr, uint64_t LoadAddr,
size_t size) override {
// We don't know yet how to do this on Windows, so we hide this on compilation // We don't know yet how to do this on Windows, so we hide this on compilation
// so we can compile and pass spectests on unix systems // so we can compile and pass spectests on unix systems
#ifndef _WIN32 #ifndef _WIN32
@ -96,12 +103,15 @@ public:
} }
virtual bool finalizeMemory(std::string *ErrMsg = nullptr) override { virtual bool finalizeMemory(std::string *ErrMsg = nullptr) override {
auto code_result = callbacks.protect_memory(code_section.base, code_section.size, mem_protect_t::PROTECT_READ_EXECUTE); auto code_result =
callbacks.protect_memory(code_section.base, code_section.size,
mem_protect_t::PROTECT_READ_EXECUTE);
if (code_result != RESULT_OK) { if (code_result != RESULT_OK) {
return false; return false;
} }
auto read_result = callbacks.protect_memory(read_section.base, read_section.size, mem_protect_t::PROTECT_READ); auto read_result = callbacks.protect_memory(
read_section.base, read_section.size, mem_protect_t::PROTECT_READ);
if (read_result != RESULT_OK) { if (read_result != RESULT_OK) {
return false; return false;
} }
@ -111,14 +121,18 @@ public:
return false; return false;
} }
virtual void notifyObjectLoaded(llvm::RuntimeDyld &RTDyld, const llvm::object::ObjectFile &Obj) override {} virtual void
notifyObjectLoaded(llvm::RuntimeDyld &RTDyld,
const llvm::object::ObjectFile &Obj) override {}
private: private:
struct Section { struct Section {
uint8_t *base; uint8_t *base;
size_t size; size_t size;
}; };
uint8_t* allocate_bump(Section& section, uintptr_t& bump_ptr, size_t size, size_t align) { uint8_t *allocate_bump(Section &section, uintptr_t &bump_ptr, size_t size,
size_t align) {
auto aligner = [](uintptr_t &ptr, size_t align) { auto aligner = [](uintptr_t &ptr, size_t align) {
ptr = (ptr + align - 1) & ~(align - 1); ptr = (ptr + align - 1) & ~(align - 1);
}; };
@ -172,20 +186,19 @@ private:
callbacks_t callbacks; callbacks_t callbacks;
}; };
WasmModule::WasmModule( WasmModule::WasmModule(const uint8_t *object_start, size_t object_size,
const uint8_t *object_start, callbacks_t callbacks)
size_t object_size, : memory_manager(
callbacks_t callbacks std::unique_ptr<MemoryManager>(new MemoryManager(callbacks))) {
) : memory_manager(std::unique_ptr<MemoryManager>(new MemoryManager(callbacks)))
{
if (auto created_object_file =
if (auto created_object_file = llvm::object::ObjectFile::createObjectFile(llvm::MemoryBufferRef( llvm::object::ObjectFile::createObjectFile(llvm::MemoryBufferRef(
llvm::StringRef((const char *)object_start, object_size), "object" llvm::StringRef((const char *)object_start, object_size),
))) { "object"))) {
object_file = cantFail(std::move(created_object_file)); object_file = cantFail(std::move(created_object_file));
SymbolLookup symbol_resolver(callbacks); SymbolLookup symbol_resolver(callbacks);
runtime_dyld = std::unique_ptr<llvm::RuntimeDyld>(new llvm::RuntimeDyld(*memory_manager, symbol_resolver)); runtime_dyld = std::unique_ptr<llvm::RuntimeDyld>(
new llvm::RuntimeDyld(*memory_manager, symbol_resolver));
runtime_dyld->setProcessAllSections(true); runtime_dyld->setProcessAllSections(true);

View File

@ -6,16 +6,14 @@
#include <llvm/ExecutionEngine/RuntimeDyld.h> #include <llvm/ExecutionEngine/RuntimeDyld.h>
typedef enum typedef enum {
{
PROTECT_NONE, PROTECT_NONE,
PROTECT_READ, PROTECT_READ,
PROTECT_READ_WRITE, PROTECT_READ_WRITE,
PROTECT_READ_EXECUTE, PROTECT_READ_EXECUTE,
} mem_protect_t; } mem_protect_t;
typedef enum typedef enum {
{
RESULT_OK, RESULT_OK,
RESULT_ALLOCATE_FAILURE, RESULT_ALLOCATE_FAILURE,
RESULT_PROTECT_FAILURE, RESULT_PROTECT_FAILURE,
@ -23,17 +21,19 @@ typedef enum
RESULT_OBJECT_LOAD_FAILURE, RESULT_OBJECT_LOAD_FAILURE,
} result_t; } result_t;
typedef result_t (*alloc_memory_t)(size_t size, mem_protect_t protect, uint8_t **ptr_out, size_t *size_out); typedef result_t (*alloc_memory_t)(size_t size, mem_protect_t protect,
typedef result_t (*protect_memory_t)(uint8_t *ptr, size_t size, mem_protect_t protect); uint8_t **ptr_out, size_t *size_out);
typedef result_t (*protect_memory_t)(uint8_t *ptr, size_t size,
mem_protect_t protect);
typedef result_t (*dealloc_memory_t)(uint8_t *ptr, size_t size); typedef result_t (*dealloc_memory_t)(uint8_t *ptr, size_t size);
typedef uintptr_t (*lookup_vm_symbol_t)(const char *name_ptr, size_t length); typedef uintptr_t (*lookup_vm_symbol_t)(const char *name_ptr, size_t length);
typedef void (*fde_visitor_t)(uint8_t *fde); typedef void (*fde_visitor_t)(uint8_t *fde);
typedef result_t (*visit_fde_t)(uint8_t *fde, size_t size, fde_visitor_t visitor); typedef result_t (*visit_fde_t)(uint8_t *fde, size_t size,
fde_visitor_t visitor);
typedef void (*trampoline_t)(void *, void *, void *, void *); typedef void (*trampoline_t)(void *, void *, void *, void *);
typedef struct typedef struct {
{
/* Memory management. */ /* Memory management. */
alloc_memory_t alloc_memory; alloc_memory_t alloc_memory;
protect_memory_t protect_memory; protect_memory_t protect_memory;
@ -44,33 +44,27 @@ typedef struct
visit_fde_t visit_fde; visit_fde_t visit_fde;
} callbacks_t; } callbacks_t;
typedef struct typedef struct {
{
size_t data, vtable; size_t data, vtable;
} box_any_t; } box_any_t;
struct WasmException struct WasmException {
{
public: public:
virtual std::string description() const noexcept = 0; virtual std::string description() const noexcept = 0;
}; };
struct UncatchableException : WasmException struct UncatchableException : WasmException {
{
public: public:
virtual std::string description() const noexcept override virtual std::string description() const noexcept override {
{
return "Uncatchable exception"; return "Uncatchable exception";
} }
}; };
struct UserException : UncatchableException struct UserException : UncatchableException {
{
public: public:
UserException(size_t data, size_t vtable) : error_data({data, vtable}) {} UserException(size_t data, size_t vtable) : error_data({data, vtable}) {}
virtual std::string description() const noexcept override virtual std::string description() const noexcept override {
{
return "user exception"; return "user exception";
} }
@ -78,24 +72,20 @@ struct UserException : UncatchableException
box_any_t error_data; box_any_t error_data;
}; };
struct BreakpointException : UncatchableException struct BreakpointException : UncatchableException {
{
public: public:
BreakpointException(uintptr_t callback) : callback(callback) {} BreakpointException(uintptr_t callback) : callback(callback) {}
virtual std::string description() const noexcept override virtual std::string description() const noexcept override {
{
return "breakpoint exception"; return "breakpoint exception";
} }
uintptr_t callback; uintptr_t callback;
}; };
struct WasmTrap : UncatchableException struct WasmTrap : UncatchableException {
{
public: public:
enum Type enum Type {
{
Unreachable = 0, Unreachable = 0,
IncorrectCallIndirectSignature = 1, IncorrectCallIndirectSignature = 1,
MemoryOutOfBounds = 2, MemoryOutOfBounds = 2,
@ -106,12 +96,9 @@ struct WasmTrap : UncatchableException
WasmTrap(Type type) : type(type) {} WasmTrap(Type type) : type(type) {}
virtual std::string description() const noexcept override virtual std::string description() const noexcept override {
{
std::ostringstream ss; std::ostringstream ss;
ss ss << "WebAssembly trap:" << '\n' << " - type: " << type << '\n';
<< "WebAssembly trap:" << '\n'
<< " - type: " << type << '\n';
return ss.str(); return ss.str();
} }
@ -119,10 +106,8 @@ struct WasmTrap : UncatchableException
Type type; Type type;
private: private:
friend std::ostream &operator<<(std::ostream &out, const Type &ty) friend std::ostream &operator<<(std::ostream &out, const Type &ty) {
{ switch (ty) {
switch (ty)
{
case Type::Unreachable: case Type::Unreachable:
out << "unreachable"; out << "unreachable";
break; break;
@ -147,13 +132,12 @@ struct WasmTrap : UncatchableException
} }
}; };
struct CatchableException : WasmException struct CatchableException : WasmException {
{
public: public:
CatchableException(uint32_t type_id, uint32_t value_num) : type_id(type_id), value_num(value_num) {} CatchableException(uint32_t type_id, uint32_t value_num)
: type_id(type_id), value_num(value_num) {}
virtual std::string description() const noexcept override virtual std::string description() const noexcept override {
{
return "catchable exception"; return "catchable exception";
} }
@ -161,29 +145,26 @@ struct CatchableException : WasmException
uint64_t values[1]; uint64_t values[1];
}; };
struct WasmModule struct WasmModule {
{
public: public:
WasmModule( WasmModule(const uint8_t *object_start, size_t object_size,
const uint8_t *object_start,
size_t object_size,
callbacks_t callbacks); callbacks_t callbacks);
void *get_func(llvm::StringRef name) const; void *get_func(llvm::StringRef name) const;
bool _init_failed = false; bool _init_failed = false;
private: private:
std::unique_ptr<llvm::RuntimeDyld::MemoryManager> memory_manager; std::unique_ptr<llvm::RuntimeDyld::MemoryManager> memory_manager;
std::unique_ptr<llvm::object::ObjectFile> object_file; std::unique_ptr<llvm::object::ObjectFile> object_file;
std::unique_ptr<llvm::RuntimeDyld> runtime_dyld; std::unique_ptr<llvm::RuntimeDyld> runtime_dyld;
}; };
extern "C" extern "C" {
{
void callback_trampoline(void *, void *); void callback_trampoline(void *, void *);
result_t module_load(const uint8_t *mem_ptr, size_t mem_size, callbacks_t callbacks, WasmModule **module_out) result_t module_load(const uint8_t *mem_ptr, size_t mem_size,
{ callbacks_t callbacks, WasmModule **module_out) {
*module_out = new WasmModule(mem_ptr, mem_size, callbacks); *module_out = new WasmModule(mem_ptr, mem_size, callbacks);
if ((*module_out)->_init_failed) { if ((*module_out)->_init_failed) {
@ -193,14 +174,9 @@ extern "C"
return RESULT_OK; return RESULT_OK;
} }
[[noreturn]] void throw_trap(WasmTrap::Type ty) { [[noreturn]] void throw_trap(WasmTrap::Type ty) { throw WasmTrap(ty); }
throw WasmTrap(ty);
}
void module_delete(WasmModule *module) void module_delete(WasmModule *module) { delete module; }
{
delete module;
}
// Throw a fat pointer that's assumed to be `*mut dyn Any` on the rust // Throw a fat pointer that's assumed to be `*mut dyn Any` on the rust
// side. // side.
@ -214,49 +190,31 @@ extern "C"
throw BreakpointException(callback); throw BreakpointException(callback);
} }
bool invoke_trampoline( bool invoke_trampoline(trampoline_t trampoline, void *ctx, void *func,
trampoline_t trampoline, void *params, void *results, WasmTrap::Type *trap_out,
void *ctx, box_any_t *user_error, void *invoke_env) throw() {
void *func, try {
void *params,
void *results,
WasmTrap::Type *trap_out,
box_any_t *user_error,
void *invoke_env) throw()
{
try
{
trampoline(ctx, func, params, results); trampoline(ctx, func, params, results);
return true; return true;
} } catch (const WasmTrap &e) {
catch (const WasmTrap &e)
{
*trap_out = e.type; *trap_out = e.type;
return false; return false;
} } catch (const UserException &e) {
catch (const UserException &e)
{
*user_error = e.error_data; *user_error = e.error_data;
return false; return false;
} } catch (const BreakpointException &e) {
catch (const BreakpointException &e) {
callback_trampoline(user_error, (void *)e.callback); callback_trampoline(user_error, (void *)e.callback);
return false; return false;
} } catch (const WasmException &e) {
catch (const WasmException &e)
{
*trap_out = WasmTrap::Type::Unknown; *trap_out = WasmTrap::Type::Unknown;
return false; return false;
} } catch (...) {
catch (...)
{
*trap_out = WasmTrap::Type::Unknown; *trap_out = WasmTrap::Type::Unknown;
return false; return false;
} }
} }
void *get_func_symbol(WasmModule *module, const char *name) void *get_func_symbol(WasmModule *module, const char *name) {
{
return module->get_func(llvm::StringRef(name)); return module->get_func(llvm::StringRef(name));
} }
} }