Skip to content
This repository was archived by the owner on Mar 24, 2022. It is now read-only.
Closed
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
204 changes: 119 additions & 85 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion cranelift
2 changes: 1 addition & 1 deletion lucet-module/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ authors = ["Lucet team <lucet@fastly.com>"]
edition = "2018"

[dependencies]
cranelift-entity = { path = "../cranelift/cranelift-entity", version = "0.41.0" }
cranelift-entity = { path = "../cranelift/cranelift-entity", version = "0.42.0" }
failure = "0.1"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
Expand Down
16 changes: 9 additions & 7 deletions lucetc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ path = "src/main.rs"

[dependencies]
bincode = "1.1.4"
cranelift-codegen = { path = "../cranelift/cranelift-codegen", version = "0.41.0" }
cranelift-entity = { path = "../cranelift/cranelift-entity", version = "0.41.0" }
cranelift-native = { path = "../cranelift/cranelift-native", version = "0.41.0" }
cranelift-frontend = { path = "../cranelift/cranelift-frontend", version = "0.41.0" }
cranelift-module = { path = "../cranelift/cranelift-module", version = "0.41.0" }
cranelift-faerie = { path = "../cranelift/cranelift-faerie", version = "0.41.0" }
cranelift-wasm = { path = "../cranelift/cranelift-wasm", version = "0.41.0" }
cranelift-codegen = { path = "../cranelift/cranelift-codegen", version = "0.42.0" }
cranelift-entity = { path = "../cranelift/cranelift-entity", version = "0.42.0" }
cranelift-native = { path = "../cranelift/cranelift-native", version = "0.42.0" }
cranelift-frontend = { path = "../cranelift/cranelift-frontend", version = "0.42.0" }
cranelift-module = { path = "../cranelift/cranelift-module", version = "0.42.0" }
cranelift-faerie = { path = "../cranelift/cranelift-faerie", version = "0.42.0" }
cranelift-object = { path = "../cranelift/cranelift-object", version = "0.42.0" }
cranelift-wasm = { path = "../cranelift/cranelift-wasm", version = "0.42.0" }
target-lexicon = "0.8.0"
lucet-module = { path = "../lucet-module", version = "0.1.1" }
wasmparser = "0.37.0"
Expand All @@ -33,6 +34,7 @@ log = "0.4"
env_logger = "0.6"
faerie = "0.11.0"
goblin = "0.0.22"
object = { version = "0.14.0", default-features = false, features = ["write"] }
failure = "0.1"
byteorder = "1.2"
wasmonkey = { path = "../lucet-builtins/wasmonkey", version = "0.1.7" }
Expand Down
62 changes: 23 additions & 39 deletions lucetc/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ use cranelift_codegen::{
settings::{self, Configurable},
Context as ClifContext,
};
use cranelift_faerie::{FaerieBackend, FaerieBuilder, FaerieTrapCollection};
use cranelift_module::{Backend as ClifBackend, Module as ClifModule};
use cranelift_native;
use cranelift_object::{ObjectBackend, ObjectBuilder, ObjectTrapCollection};
use cranelift_wasm::{translate_module, FuncTranslator, WasmError};
use failure::{format_err, Fail, ResultExt};
use lucet_module::bindings::Bindings;
use lucet_module::{FunctionSpec, ModuleData, MODULE_DATA_SYM};
use lucet_module::{ModuleData, MODULE_DATA_SYM};

#[derive(Debug, Clone, Copy)]
pub enum OptLevel {
Expand All @@ -46,7 +46,7 @@ impl OptLevel {

pub struct Compiler<'a> {
decls: ModuleDecls<'a>,
clif_module: ClifModule<FaerieBackend>,
clif_module: ClifModule<ObjectBackend>,
opt_level: OptLevel,
count_instructions: bool,
}
Expand Down Expand Up @@ -85,11 +85,11 @@ impl<'a> Compiler<'a> {
_ => (cranelift_module::default_libcall_names())(libcall),
});

let mut clif_module: ClifModule<FaerieBackend> = ClifModule::new(
FaerieBuilder::new(
let mut clif_module: ClifModule<ObjectBackend> = ClifModule::new(
ObjectBuilder::new(
isa,
"lucet_guest".to_owned(),
FaerieTrapCollection::Enabled,
ObjectTrapCollection::Enabled,
libcalls,
)
.context(LucetcErrorKind::Validation)?,
Expand Down Expand Up @@ -121,12 +121,14 @@ impl<'a> Compiler<'a> {

for (ref func, (code, code_offset)) in self.decls.function_bodies() {
let mut func_info = FuncInfo::new(&self.decls, self.count_instructions);
let mut clif_context = ClifContext::new();
clif_context.func.name = func.name.as_externalname();
clif_context.func.signature = func.signature.clone();

func_translator
.translate(code, *code_offset, &mut clif_context.func, &mut func_info)
let mut empty_func = ir::Function::new();
empty_func.name = func.name.as_externalname();
empty_func.signature = func.signature.clone();

let mut clif_context = ClifContext::new();
clif_context.func = func_translator
.translate(code, *code_offset, empty_func, &mut func_info)
.map_err(|e| format_err!("in {}: {:?}", func.name.symbol(), e))
.context(LucetcErrorKind::FunctionTranslation)?;

Expand All @@ -142,29 +144,9 @@ impl<'a> Compiler<'a> {
write_startfunc_data(&mut self.clif_module, &self.decls)?;
let table_names = write_table_data(&mut self.clif_module, &self.decls)?;

let function_manifest: Vec<(String, FunctionSpec)> = self
.clif_module
.declared_functions()
.map(|f| {
(
f.decl.name.to_owned(), // this copy is only necessary because `clif_module` is moved in `finish, below`
FunctionSpec::new(
0,
f.compiled.as_ref().map(|c| c.code_length()).unwrap_or(0),
0,
0,
),
)
})
.collect();

let obj = ObjectFile::new(
self.clif_module.finish(),
module_data_len,
function_manifest,
table_names,
)
.context(LucetcErrorKind::Output)?;
self.clif_module.finalize_definitions();
let obj = ObjectFile::new(self.clif_module.finish(), module_data_len, table_names)
.context(LucetcErrorKind::Output)?;
Ok(obj)
}

Expand All @@ -176,12 +158,14 @@ impl<'a> Compiler<'a> {

for (ref func, (code, code_offset)) in self.decls.function_bodies() {
let mut func_info = FuncInfo::new(&self.decls, self.count_instructions);
let mut clif_context = ClifContext::new();
clif_context.func.name = func.name.as_externalname();
clif_context.func.signature = func.signature.clone();

func_translator
.translate(code, *code_offset, &mut clif_context.func, &mut func_info)
let mut empty_func = ir::Function::new();
empty_func.name = func.name.as_externalname();
empty_func.signature = func.signature.clone();

let mut clif_context = ClifContext::new();
clif_context.func = func_translator
.translate(code, *code_offset, empty_func, &mut func_info)
.map_err(|e| format_err!("in {}: {:?}", func.name.symbol(), e))
.context(LucetcErrorKind::FunctionTranslation)?;

Expand Down
2 changes: 1 addition & 1 deletion lucetc/src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ impl<'a> FuncInfo<'a> {
(-(std::mem::size_of::<InstanceRuntimeData>() as i32)
+ offset_of!(InstanceRuntimeData, instruction_count) as i32)
.into();
let vmctx_gv = environ.get_vmctx(builder.func);
let vmctx_gv = environ.get_vmctx(&mut builder.func);
let addr = builder.ins().global_value(environ.pointer_type(), vmctx_gv);
let trusted_mem = ir::MemFlags::trusted();

Expand Down
59 changes: 32 additions & 27 deletions lucetc/src/function_manifest.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::output::write_relocated_slice;
use crate::traps::trap_sym_for_func;
use faerie::{Artifact, Decl};
use failure::{Error, ResultExt};
use lucet_module::FunctionSpec;
use object::write::{Object, StandardSection, Symbol, SymbolId};
use object::{SymbolKind, SymbolScope};
use std::io::Cursor;
use std::mem::size_of;

Expand All @@ -12,17 +12,17 @@ pub const FUNCTION_MANIFEST_SYM: &str = "lucet_function_manifest";
/// Writes a manifest of functions, with relocations, to the artifact.
///
pub fn write_function_manifest(
functions: &[(String, FunctionSpec)],
obj: &mut Artifact,
functions: &[(SymbolId, Option<SymbolId>, u32)],
obj: &mut Object,
) -> Result<(), Error> {
obj.declare(FUNCTION_MANIFEST_SYM, Decl::data())
.context(format!("declaring {}", FUNCTION_MANIFEST_SYM))?;

let mut manifest_buf: Cursor<Vec<u8>> = Cursor::new(Vec::with_capacity(
functions.len() * size_of::<FunctionSpec>(),
));

for (fn_name, fn_spec) in functions.iter() {
let mut relocs = Vec::new();
for (fn_sym, trap_sym, trap_len) in functions.iter() {
let fn_len = obj.symbol(*fn_sym).size;

/*
* This has implicit knowledge of the layout of `FunctionSpec`!
*
Expand All @@ -34,30 +34,35 @@ pub fn write_function_manifest(
* and transmute, unfortunately.
*/
// Writes a (ptr, len) pair with relocation for code
write_relocated_slice(
obj,
&mut manifest_buf,
FUNCTION_MANIFEST_SYM,
Some(fn_name),
fn_spec.code_len() as u64,
)?;
write_relocated_slice(&mut manifest_buf, &mut relocs, Some(*fn_sym), fn_len);
// Writes a (ptr, len) pair with relocation for this function's trap table
let trap_sym = trap_sym_for_func(fn_name);
write_relocated_slice(
obj,
&mut manifest_buf,
FUNCTION_MANIFEST_SYM,
if fn_spec.traps_len() > 0 {
Some(&trap_sym)
} else {
None
},
fn_spec.traps_len() as u64,
)?;
&mut relocs,
*trap_sym,
u64::from(*trap_len),
);
}

obj.define(FUNCTION_MANIFEST_SYM, manifest_buf.into_inner())
.context(format!("defining {}", FUNCTION_MANIFEST_SYM))?;
let section_id = obj.section_id(StandardSection::ReadOnlyDataWithRel);
let manifest_buf = manifest_buf.into_inner();
let manifest_offset = obj.append_section_data(section_id, &manifest_buf, 8);
obj.add_symbol(Symbol {
name: FUNCTION_MANIFEST_SYM.as_bytes().to_vec(),
value: manifest_offset,
size: manifest_buf.len() as u64,
kind: SymbolKind::Data,
scope: SymbolScope::Dynamic,
weak: false,
section: Some(section_id),
});

for mut reloc in relocs.drain(..) {
reloc.offset += manifest_offset;
obj.add_relocation(section_id, reloc)
.map_err(failure::err_msg)
.context("relocating function manifest")?;
}

Ok(())
}
Loading