Skip to content
This repository was archived by the owner on Mar 24, 2022. It is now read-only.

Commit f495487

Browse files
author
Pat Hickey
authored
Merge pull request #289 from PLSysSec/remove_runtime_cranelift
Fixes #288 - Remove cranelift-codegen from the dependencies of the lucet-runtime components such as lucet-module
2 parents 043ad6f + 0747b73 commit f495487

File tree

6 files changed

+106
-117
lines changed

6 files changed

+106
-117
lines changed

Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lucet-module/Cargo.toml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@ edition = "2018"
1111

1212
[dependencies]
1313
cranelift-entity = { path = "../cranelift/cranelift-entity", version = "0.41.0" }
14-
# We only depend on the types in codegen used inside ir::Signature. TODO: make
15-
# a lucet-module version of those types, and make lucetc responsible for translating
16-
# from the cranelift-codegen::ir::Signature to a lucet-module::Signature
17-
cranelift-codegen = { path = "../cranelift/cranelift-codegen", version = "0.41.0" }
1814
failure = "0.1"
1915
serde = { version = "1.0", features = ["derive"] }
2016
serde_json = "1.0"

lucet-module/src/types.rs

Lines changed: 0 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
use cranelift_codegen::ir;
21
use serde::{Deserialize, Serialize};
3-
use std::convert::TryFrom;
42
use std::fmt::{Display, Formatter};
53

64
#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
@@ -22,46 +20,6 @@ impl Display for ValueType {
2220
}
2321
}
2422

25-
#[derive(Debug)]
26-
pub enum ValueError {
27-
Unrepresentable,
28-
InvalidVMContext,
29-
}
30-
31-
impl TryFrom<&ir::AbiParam> for ValueType {
32-
type Error = ValueError;
33-
34-
fn try_from(value: &ir::AbiParam) -> Result<Self, Self::Error> {
35-
match value {
36-
ir::AbiParam {
37-
value_type: cranelift_ty,
38-
purpose: ir::ArgumentPurpose::Normal,
39-
extension: ir::ArgumentExtension::None,
40-
location: ir::ArgumentLoc::Unassigned,
41-
} => {
42-
let size = cranelift_ty.bits();
43-
44-
if cranelift_ty.is_int() {
45-
match size {
46-
32 => Ok(ValueType::I32),
47-
64 => Ok(ValueType::I64),
48-
_ => Err(ValueError::Unrepresentable),
49-
}
50-
} else if cranelift_ty.is_float() {
51-
match size {
52-
32 => Ok(ValueType::F32),
53-
64 => Ok(ValueType::F64),
54-
_ => Err(ValueError::Unrepresentable),
55-
}
56-
} else {
57-
Err(ValueError::Unrepresentable)
58-
}
59-
}
60-
_ => Err(ValueError::Unrepresentable),
61-
}
62-
}
63-
}
64-
6523
/// A signature for a function in a wasm module.
6624
///
6725
/// Note that this does not explicitly name VMContext as a parameter! It is assumed that all wasm
@@ -112,71 +70,3 @@ macro_rules! lucet_signature {
11270
}
11371
};
11472
}
115-
116-
#[derive(Debug)]
117-
pub enum SignatureError {
118-
BadElement(ir::AbiParam, ValueError),
119-
BadSignature,
120-
}
121-
122-
impl TryFrom<&ir::Signature> for Signature {
123-
type Error = SignatureError;
124-
125-
fn try_from(value: &ir::Signature) -> Result<Self, Self::Error> {
126-
let mut params: Vec<ValueType> = Vec::new();
127-
128-
let mut param_iter = value.params.iter();
129-
130-
// Enforce that the first parameter is VMContext, as Signature assumes.
131-
// Even functions declared no-arg take VMContext in reality.
132-
if let Some(param) = param_iter.next() {
133-
match &param {
134-
ir::AbiParam {
135-
value_type: value,
136-
purpose: ir::ArgumentPurpose::VMContext,
137-
extension: ir::ArgumentExtension::None,
138-
location: ir::ArgumentLoc::Unassigned,
139-
} => {
140-
if value.is_int() && value.bits() == 64 {
141-
// this is VMContext, so we can move on.
142-
} else {
143-
return Err(SignatureError::BadElement(
144-
param.to_owned(),
145-
ValueError::InvalidVMContext,
146-
));
147-
}
148-
}
149-
_ => {
150-
return Err(SignatureError::BadElement(
151-
param.to_owned(),
152-
ValueError::InvalidVMContext,
153-
));
154-
}
155-
}
156-
} else {
157-
return Err(SignatureError::BadSignature);
158-
}
159-
160-
for param in param_iter {
161-
let value_ty = ValueType::try_from(param)
162-
.map_err(|e| SignatureError::BadElement(param.clone(), e))?;
163-
164-
params.push(value_ty);
165-
}
166-
167-
let ret_ty: Option<ValueType> = match value.returns.as_slice() {
168-
&[] => None,
169-
&[ref ret_ty] => {
170-
let value_ty = ValueType::try_from(ret_ty)
171-
.map_err(|e| SignatureError::BadElement(ret_ty.clone(), e))?;
172-
173-
Some(value_ty)
174-
}
175-
_ => {
176-
return Err(SignatureError::BadSignature);
177-
}
178-
};
179-
180-
Ok(Signature { params, ret_ty })
181-
}
182-
}

lucetc/src/decls.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::module::{ModuleInfo, UniqueFuncIndex};
55
use crate::name::Name;
66
use crate::runtime::{Runtime, RuntimeFunc};
77
use crate::table::TABLE_SYM;
8+
use crate::types::to_lucet_signature;
89
use cranelift_codegen::entity::{EntityRef, PrimaryMap};
910
use cranelift_codegen::ir;
1011
use cranelift_codegen::isa::TargetFrontendConfig;
@@ -21,7 +22,6 @@ use lucet_module::{
2122
ModuleData, Signature as LucetSignature, UniqueSignatureIndex,
2223
};
2324
use std::collections::HashMap;
24-
use std::convert::TryFrom;
2525

2626
#[derive(Debug)]
2727
pub struct FunctionDecl<'a> {
@@ -530,7 +530,7 @@ impl<'a> ModuleDecls<'a> {
530530
.signatures
531531
.values()
532532
.map(|sig| {
533-
LucetSignature::try_from(sig)
533+
to_lucet_signature(sig)
534534
.map_err(|e| format_err!("error converting cranelift sig to wasm sig: {:?}", e))
535535
.context(LucetcErrorKind::TranslatingModule)
536536
})

lucetc/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ mod sparsedata;
1818
mod stack_probe;
1919
mod table;
2020
mod traps;
21+
mod types;
2122

2223
use crate::load::read_bytes;
2324
pub use crate::{

lucetc/src/types.rs

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
use cranelift_codegen::ir;
2+
use lucet_module::Signature;
3+
use lucet_module::ValueType;
4+
5+
#[derive(Debug)]
6+
pub enum ValueError {
7+
Unrepresentable,
8+
InvalidVMContext,
9+
}
10+
11+
fn to_lucet_value(value: &ir::AbiParam) -> Result<ValueType, ValueError> {
12+
match value {
13+
ir::AbiParam {
14+
value_type: cranelift_ty,
15+
purpose: ir::ArgumentPurpose::Normal,
16+
extension: ir::ArgumentExtension::None,
17+
location: ir::ArgumentLoc::Unassigned,
18+
} => {
19+
let size = cranelift_ty.bits();
20+
21+
if cranelift_ty.is_int() {
22+
match size {
23+
32 => Ok(ValueType::I32),
24+
64 => Ok(ValueType::I64),
25+
_ => Err(ValueError::Unrepresentable),
26+
}
27+
} else if cranelift_ty.is_float() {
28+
match size {
29+
32 => Ok(ValueType::F32),
30+
64 => Ok(ValueType::F64),
31+
_ => Err(ValueError::Unrepresentable),
32+
}
33+
} else {
34+
Err(ValueError::Unrepresentable)
35+
}
36+
}
37+
_ => Err(ValueError::Unrepresentable),
38+
}
39+
}
40+
41+
#[derive(Debug)]
42+
pub enum SignatureError {
43+
BadElement(ir::AbiParam, ValueError),
44+
BadSignature,
45+
}
46+
47+
pub fn to_lucet_signature(value: &ir::Signature) -> Result<Signature, SignatureError> {
48+
let mut params: Vec<ValueType> = Vec::new();
49+
50+
let mut param_iter = value.params.iter();
51+
52+
// Enforce that the first parameter is VMContext, as Signature assumes.
53+
// Even functions declared no-arg take VMContext in reality.
54+
if let Some(param) = param_iter.next() {
55+
match &param {
56+
ir::AbiParam {
57+
value_type: value,
58+
purpose: ir::ArgumentPurpose::VMContext,
59+
extension: ir::ArgumentExtension::None,
60+
location: ir::ArgumentLoc::Unassigned,
61+
} => {
62+
if value.is_int() && value.bits() == 64 {
63+
// this is VMContext, so we can move on.
64+
} else {
65+
return Err(SignatureError::BadElement(
66+
param.to_owned(),
67+
ValueError::InvalidVMContext,
68+
));
69+
}
70+
}
71+
_ => {
72+
return Err(SignatureError::BadElement(
73+
param.to_owned(),
74+
ValueError::InvalidVMContext,
75+
));
76+
}
77+
}
78+
} else {
79+
return Err(SignatureError::BadSignature);
80+
}
81+
82+
for param in param_iter {
83+
let value_ty =
84+
to_lucet_value(param).map_err(|e| SignatureError::BadElement(param.clone(), e))?;
85+
86+
params.push(value_ty);
87+
}
88+
89+
let ret_ty: Option<ValueType> = match value.returns.as_slice() {
90+
&[] => None,
91+
&[ref ret_ty] => {
92+
let value_ty = to_lucet_value(ret_ty)
93+
.map_err(|e| SignatureError::BadElement(ret_ty.clone(), e))?;
94+
95+
Some(value_ty)
96+
}
97+
_ => {
98+
return Err(SignatureError::BadSignature);
99+
}
100+
};
101+
102+
Ok(Signature { params, ret_ty })
103+
}

0 commit comments

Comments
 (0)