Format everything

This commit is contained in:
losfair
2019-08-21 15:23:56 -07:00
parent 53ebcc355a
commit 56e735349d
8 changed files with 93 additions and 96 deletions

View File

@ -6,7 +6,7 @@
extern "C" void __register_frame(uint8_t *); extern "C" void __register_frame(uint8_t *);
extern "C" void __deregister_frame(uint8_t *); extern "C" void __deregister_frame(uint8_t *);
MemoryManager::~MemoryManager() { MemoryManager::~MemoryManager() {
deregisterEHFrames(); deregisterEHFrames();
// Deallocate all of the allocated memory. // Deallocate all of the allocated memory.
callbacks.dealloc_memory(code_section.base, code_section.size); callbacks.dealloc_memory(code_section.base, code_section.size);
@ -14,72 +14,68 @@ MemoryManager::~MemoryManager() {
callbacks.dealloc_memory(readwrite_section.base, readwrite_section.size); callbacks.dealloc_memory(readwrite_section.base, readwrite_section.size);
} }
void unwinding_setjmp(jmp_buf stack_out, void (*func)(void *), void *userdata) { void unwinding_setjmp(jmp_buf stack_out, void (*func)(void *), void *userdata) {
if(setjmp(stack_out)) { if (setjmp(stack_out)) {
} else { } else {
func(userdata); func(userdata);
} }
} }
[[noreturn]] [[noreturn]] void unwinding_longjmp(jmp_buf stack_in) { longjmp(stack_in, 42); }
void unwinding_longjmp(jmp_buf stack_in) {
longjmp(stack_in, 42);
}
struct UnwindPoint { struct UnwindPoint {
UnwindPoint *prev; UnwindPoint *prev;
jmp_buf stack; jmp_buf stack;
std::function<void()> *f; std::function<void()> *f;
std::unique_ptr<std::exception> exception; std::unique_ptr<std::exception> exception;
}; };
static thread_local UnwindPoint *unwind_state = nullptr; static thread_local UnwindPoint *unwind_state = nullptr;
static void unwind_payload(void *_point) { static void unwind_payload(void *_point) {
UnwindPoint *point = (UnwindPoint *) _point; UnwindPoint *point = (UnwindPoint *)_point;
(*point->f)(); (*point->f)();
} }
void catch_unwind(std::function<void()>&& f) { void catch_unwind(std::function<void()> &&f) {
UnwindPoint current; UnwindPoint current;
current.prev = unwind_state; current.prev = unwind_state;
current.f = &f; current.f = &f;
unwind_state = &current; unwind_state = &current;
unwinding_setjmp(current.stack, unwind_payload, (void *) &current); unwinding_setjmp(current.stack, unwind_payload, (void *)&current);
if(current.exception) { if (current.exception) {
throw *current.exception; throw *current.exception;
} }
} }
void unsafe_unwind(std::exception *exception) { void unsafe_unwind(std::exception *exception) {
UnwindPoint *state = unwind_state; UnwindPoint *state = unwind_state;
if(state) { if (state) {
state->exception.reset(exception); state->exception.reset(exception);
unwinding_longjmp(state->stack); unwinding_longjmp(state->stack);
} else { } else {
abort(); abort();
} }
} }
uint8_t *MemoryManager::allocateCodeSection(uintptr_t size, unsigned alignment, uint8_t *MemoryManager::allocateCodeSection(uintptr_t size, unsigned alignment,
unsigned section_id, unsigned section_id,
llvm::StringRef section_name) { llvm::StringRef section_name) {
return allocate_bump(code_section, code_bump_ptr, size, alignment); return allocate_bump(code_section, code_bump_ptr, size, alignment);
} }
uint8_t *MemoryManager::allocateDataSection(uintptr_t size, unsigned alignment, uint8_t *MemoryManager::allocateDataSection(uintptr_t size, unsigned alignment,
unsigned section_id, unsigned section_id,
llvm::StringRef section_name, llvm::StringRef section_name,
bool read_only) { bool read_only) {
// Allocate from the read-only section or the read-write section, depending // Allocate from the read-only section or the read-write section, depending
// on if this allocation should be read-only or not. // on if this allocation should be read-only or not.
uint8_t *ret; uint8_t *ret;
if (read_only) { if (read_only) {
ret = allocate_bump(read_section, read_bump_ptr, size, alignment); ret = allocate_bump(read_section, read_bump_ptr, size, alignment);
} else { } else {
ret = ret = allocate_bump(readwrite_section, readwrite_bump_ptr, size, alignment);
allocate_bump(readwrite_section, readwrite_bump_ptr, size, alignment);
} }
if (section_name.equals(llvm::StringRef("__llvm_stackmaps")) || if (section_name.equals(llvm::StringRef("__llvm_stackmaps")) ||
section_name.equals(llvm::StringRef(".llvm_stackmaps"))) { section_name.equals(llvm::StringRef(".llvm_stackmaps"))) {
@ -89,11 +85,12 @@ uint8_t *MemoryManager::allocateDataSection(uintptr_t size, unsigned alignment,
return ret; return ret;
} }
void MemoryManager::reserveAllocationSpace(uintptr_t code_size, uint32_t code_align, void MemoryManager::reserveAllocationSpace(uintptr_t code_size,
uintptr_t read_data_size, uint32_t code_align,
uint32_t read_data_align, uintptr_t read_data_size,
uintptr_t read_write_data_size, uint32_t read_data_align,
uint32_t read_write_data_align) { uintptr_t read_write_data_size,
uint32_t read_write_data_align) {
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;
@ -104,7 +101,7 @@ void MemoryManager::reserveAllocationSpace(uintptr_t code_size, uint32_t code_al
size_t code_size_out = 0; size_t code_size_out = 0;
auto code_result = auto code_result =
callbacks.alloc_memory(aligner(code_size, 4096), PROTECT_READ_WRITE, callbacks.alloc_memory(aligner(code_size, 4096), PROTECT_READ_WRITE,
&code_ptr_out, &code_size_out); &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;
@ -113,9 +110,9 @@ void MemoryManager::reserveAllocationSpace(uintptr_t code_size, uint32_t code_al
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), auto read_result =
PROTECT_READ_WRITE, &read_ptr_out, callbacks.alloc_memory(aligner(read_data_size, 4096), PROTECT_READ_WRITE,
&read_size_out); &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;
@ -133,7 +130,7 @@ void MemoryManager::reserveAllocationSpace(uintptr_t code_size, uint32_t code_al
bool MemoryManager::needsToReserveAllocationSpace() { return true; } bool MemoryManager::needsToReserveAllocationSpace() { return true; }
void MemoryManager::registerEHFrames(uint8_t *addr, uint64_t LoadAddr, void MemoryManager::registerEHFrames(uint8_t *addr, uint64_t LoadAddr,
size_t size) { size_t size) {
// 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
@ -157,7 +154,7 @@ void MemoryManager::deregisterEHFrames() {
bool MemoryManager::finalizeMemory(std::string *ErrMsg) { bool MemoryManager::finalizeMemory(std::string *ErrMsg) {
auto code_result = auto code_result =
callbacks.protect_memory(code_section.base, code_section.size, callbacks.protect_memory(code_section.base, code_section.size,
mem_protect_t::PROTECT_READ_EXECUTE); mem_protect_t::PROTECT_READ_EXECUTE);
if (code_result != RESULT_OK) { if (code_result != RESULT_OK) {
return false; return false;
} }
@ -174,10 +171,10 @@ bool MemoryManager::finalizeMemory(std::string *ErrMsg) {
} }
void MemoryManager::notifyObjectLoaded(llvm::RuntimeDyld &RTDyld, void MemoryManager::notifyObjectLoaded(llvm::RuntimeDyld &RTDyld,
const llvm::object::ObjectFile &Obj) {} const llvm::object::ObjectFile &Obj) {}
uint8_t *MemoryManager::allocate_bump(Section &section, uintptr_t &bump_ptr, size_t size, uint8_t *MemoryManager::allocate_bump(Section &section, uintptr_t &bump_ptr,
size_t align) { 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);
}; };

View File

@ -3,10 +3,10 @@
#include <cstddef> #include <cstddef>
#include <cstdint> #include <cstdint>
#include <exception> #include <exception>
#include <iostream>
#include <sstream>
#include <setjmp.h>
#include <functional> #include <functional>
#include <iostream>
#include <setjmp.h>
#include <sstream>
#include <llvm/ExecutionEngine/RuntimeDyld.h> #include <llvm/ExecutionEngine/RuntimeDyld.h>
@ -81,7 +81,7 @@ public:
virtual void deregisterEHFrames() override; virtual void deregisterEHFrames() override;
virtual bool finalizeMemory(std::string *ErrMsg = nullptr) override; virtual bool finalizeMemory(std::string *ErrMsg = nullptr) override;
virtual void notifyObjectLoaded(llvm::RuntimeDyld &RTDyld, virtual void notifyObjectLoaded(llvm::RuntimeDyld &RTDyld,
const llvm::object::ObjectFile &Obj) override; const llvm::object::ObjectFile &Obj) override;
private: private:
struct Section { struct Section {
@ -106,12 +106,12 @@ private:
size_t stack_map_size = 0; size_t stack_map_size = 0;
}; };
struct WasmException: std::exception { struct WasmException : std::exception {
public: public:
virtual std::string description() const noexcept = 0; virtual std::string description() const noexcept = 0;
}; };
void catch_unwind(std::function<void()>&& f); void catch_unwind(std::function<void()> &&f);
[[noreturn]] void unsafe_unwind(std::exception *exception); [[noreturn]] void unsafe_unwind(std::exception *exception);
struct UncatchableException : WasmException { struct UncatchableException : WasmException {
@ -239,7 +239,9 @@ result_t module_load(const uint8_t *mem_ptr, size_t mem_size,
return RESULT_OK; return RESULT_OK;
} }
[[noreturn]] void throw_trap(WasmTrap::Type ty) { unsafe_unwind(new WasmTrap(ty)); } [[noreturn]] void throw_trap(WasmTrap::Type ty) {
unsafe_unwind(new WasmTrap(ty));
}
void module_delete(WasmModule *module) { delete module; } void module_delete(WasmModule *module) { delete module; }
@ -260,7 +262,7 @@ bool invoke_trampoline(trampoline_t trampoline, void *ctx, void *func,
box_any_t *user_error, void *invoke_env) noexcept { box_any_t *user_error, void *invoke_env) noexcept {
try { try {
catch_unwind([trampoline, ctx, func, params, results]() { catch_unwind([trampoline, ctx, func, params, results]() {
trampoline(ctx, func, params, results); trampoline(ctx, func, params, results);
}); });
return true; return true;
} catch (const WasmTrap &e) { } catch (const WasmTrap &e) {

View File

@ -331,7 +331,7 @@ impl LLVMBackend {
local_func_id_to_offset, local_func_id_to_offset,
}, },
LLVMCache { buffer }, LLVMCache { buffer },
) );
} }
} }

View File

@ -4,7 +4,9 @@ use libc::{
c_void, mmap, mprotect, munmap, siginfo_t, MAP_ANON, MAP_PRIVATE, PROT_EXEC, PROT_NONE, c_void, mmap, mprotect, munmap, siginfo_t, MAP_ANON, MAP_PRIVATE, PROT_EXEC, PROT_NONE,
PROT_READ, PROT_WRITE, PROT_READ, PROT_WRITE,
}; };
use nix::sys::signal::{sigaction, SaFlags, SigAction, SigHandler, SigSet, SIGBUS, SIGSEGV, SIGILL}; use nix::sys::signal::{
sigaction, SaFlags, SigAction, SigHandler, SigSet, SIGBUS, SIGILL, SIGSEGV,
};
use std::ptr; use std::ptr;
/// `__register_frame` and `__deregister_frame` on macos take a single fde as an /// `__register_frame` and `__deregister_frame` on macos take a single fde as an

View File

@ -232,10 +232,8 @@ impl StackmapEntry {
if loc.offset_or_small_constant >= 0 { if loc.offset_or_small_constant >= 0 {
assert!(loc.offset_or_small_constant >= 16); // (saved_rbp, return_address) assert!(loc.offset_or_small_constant >= 16); // (saved_rbp, return_address)
assert!(loc.offset_or_small_constant % 8 == 0); assert!(loc.offset_or_small_constant % 8 == 0);
prev_frame_diff.insert( prev_frame_diff
(loc.offset_or_small_constant as usize - 16) / 8, .insert((loc.offset_or_small_constant as usize - 16) / 8, Some(mv));
Some(mv),
);
} else { } else {
let stack_offset = ((-loc.offset_or_small_constant) / 4) as usize; let stack_offset = ((-loc.offset_or_small_constant) / 4) as usize;
assert!( assert!(

View File

@ -726,11 +726,8 @@ pub mod x64 {
assert_eq!(vmctx.internal.memory_bound, memory.len()); assert_eq!(vmctx.internal.memory_bound, memory.len());
} }
std::slice::from_raw_parts_mut( std::slice::from_raw_parts_mut(vmctx.internal.memory_base, vmctx.internal.memory_bound)
vmctx.internal.memory_base, .copy_from_slice(memory);
vmctx.internal.memory_bound,
)
.copy_from_slice(memory);
} }
let globals_len = (*vmctx.module).info.globals.len(); let globals_len = (*vmctx.module).info.globals.len();

View File

@ -210,8 +210,7 @@ pub unsafe fn run_tiering<F: Fn(InteractiveShellContext) -> ShellExitOperation>(
if let Err(e) = ret { if let Err(e) = ret {
if let Ok(new_image) = e.downcast::<InstanceImage>() { if let Ok(new_image) = e.downcast::<InstanceImage>() {
// Tier switch event // Tier switch event
if !was_sigint_triggered_fault() && opt_state.outcome.lock().unwrap().is_some() if !was_sigint_triggered_fault() && opt_state.outcome.lock().unwrap().is_some() {
{
resume_image = Some(*new_image); resume_image = Some(*new_image);
continue; continue;
} }

View File

@ -587,32 +587,34 @@ fn execute_wasm(options: &Run) -> Result<(), String> {
let start_raw: extern "C" fn(&mut wasmer_runtime_core::vm::Ctx) = let start_raw: extern "C" fn(&mut wasmer_runtime_core::vm::Ctx) =
unsafe { ::std::mem::transmute(start.get_vm_func()) }; unsafe { ::std::mem::transmute(start.get_vm_func()) };
unsafe { run_tiering( unsafe {
module.info(), run_tiering(
&wasm_binary, module.info(),
if let Some(ref path) = options.resume { &wasm_binary,
let mut f = File::open(path).unwrap(); if let Some(ref path) = options.resume {
let mut out: Vec<u8> = vec![]; let mut f = File::open(path).unwrap();
f.read_to_end(&mut out).unwrap(); let mut out: Vec<u8> = vec![];
Some( f.read_to_end(&mut out).unwrap();
wasmer_runtime_core::state::InstanceImage::from_bytes(&out) Some(
.expect("failed to decode image"), wasmer_runtime_core::state::InstanceImage::from_bytes(&out)
) .expect("failed to decode image"),
} else { )
None } else {
}, None
&import_object, },
start_raw, &import_object,
&mut instance, start_raw,
options &mut instance,
.optimized_backends options
.iter() .optimized_backends
.map(|&backend| -> Box<dyn Fn() -> Box<dyn Compiler> + Send> { .iter()
Box::new(move || get_compiler_by_backend(backend).unwrap()) .map(|&backend| -> Box<dyn Fn() -> Box<dyn Compiler> + Send> {
}) Box::new(move || get_compiler_by_backend(backend).unwrap())
.collect(), })
interactive_shell, .collect(),
)? }; interactive_shell,
)?
};
} }
#[cfg(not(feature = "managed"))] #[cfg(not(feature = "managed"))]