1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
use crate::traps::{TrapManifest, TrapSite};
use cranelift_entity::entity_impl;
use serde::{Deserialize, Serialize};
use std::slice::from_raw_parts;
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, Serialize, Deserialize)]
pub struct FunctionIndex(u32);
impl FunctionIndex {
pub fn from_u32(idx: u32) -> FunctionIndex {
FunctionIndex(idx)
}
pub fn as_u32(&self) -> u32 {
self.0
}
}
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, Serialize, Deserialize)]
pub struct ImportFunction<'a> {
pub fn_idx: FunctionIndex,
pub module: &'a str,
pub name: &'a str,
}
#[derive(Clone, PartialEq, Eq, Hash, Debug, Serialize, Deserialize)]
pub struct ExportFunction<'a> {
pub fn_idx: FunctionIndex,
#[serde(borrow)]
pub names: Vec<&'a str>,
}
pub struct OwnedExportFunction {
pub fn_idx: FunctionIndex,
pub names: Vec<String>,
}
impl OwnedExportFunction {
pub fn to_ref<'a>(&'a self) -> ExportFunction<'a> {
ExportFunction {
fn_idx: self.fn_idx.clone(),
names: self.names.iter().map(|x| x.as_str()).collect(),
}
}
}
pub struct OwnedImportFunction {
pub fn_idx: FunctionIndex,
pub module: String,
pub name: String,
}
impl OwnedImportFunction {
pub fn to_ref<'a>(&'a self) -> ImportFunction<'a> {
ImportFunction {
fn_idx: self.fn_idx.clone(),
module: self.module.as_str(),
name: self.name.as_str(),
}
}
}
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug, Serialize, Deserialize)]
pub struct UniqueSignatureIndex(u32);
entity_impl!(UniqueSignatureIndex);
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug, Serialize, Deserialize)]
pub struct FunctionPointer(usize);
impl FunctionPointer {
pub fn from_usize(ptr: usize) -> FunctionPointer {
FunctionPointer(ptr)
}
pub fn as_usize(&self) -> usize {
self.0
}
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct FunctionMetadata<'a> {
pub signature: UniqueSignatureIndex,
#[serde(borrow)]
pub name: Option<&'a str>,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct OwnedFunctionMetadata {
pub signature: UniqueSignatureIndex,
pub name: Option<String>,
}
impl OwnedFunctionMetadata {
pub fn to_ref(&self) -> FunctionMetadata<'_> {
FunctionMetadata {
signature: self.signature.clone(),
name: self.name.as_ref().map(|n| n.as_str()),
}
}
}
pub struct FunctionHandle {
pub ptr: FunctionPointer,
pub id: FunctionIndex,
}
#[repr(C)]
#[derive(Clone, Debug)]
pub struct FunctionSpec {
code_addr: u64,
code_len: u32,
traps_addr: u64,
traps_len: u64,
}
impl FunctionSpec {
pub fn new(code_addr: u64, code_len: u32, traps_addr: u64, traps_len: u64) -> Self {
FunctionSpec {
code_addr,
code_len,
traps_addr,
traps_len,
}
}
pub fn ptr(&self) -> FunctionPointer {
FunctionPointer::from_usize(self.code_addr as usize)
}
pub fn code_len(&self) -> u32 {
self.code_len
}
pub fn traps_len(&self) -> u64 {
self.traps_len
}
pub fn contains(&self, addr: u64) -> bool {
addr >= self.code_addr && (addr - self.code_addr) < (self.code_len as u64)
}
pub fn relative_addr(&self, addr: u64) -> Option<u32> {
if let Some(offset) = addr.checked_sub(self.code_addr) {
if offset < (self.code_len as u64) {
return Some(offset as u32);
}
}
None
}
pub fn traps(&self) -> Option<TrapManifest<'_>> {
let traps_ptr = self.traps_addr as *const TrapSite;
if !traps_ptr.is_null() {
let traps_slice = unsafe { from_raw_parts(traps_ptr, self.traps_len as usize) };
Some(TrapManifest::new(traps_slice))
} else {
None
}
}
}