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
193
194
195
196
197
198
199
200
201
202
203
use std::error;
use std::fmt;
use std::io;
mod module;
mod section;
mod primitives;
mod types;
mod import_entry;
mod export_entry;
mod global_entry;
mod ops;
mod func;
mod segment;
mod index_map;
mod name_section;
pub use self::module::{Module, peek_size, ImportCountType};
pub use self::section::{
Section, FunctionSection, CodeSection, MemorySection, DataSection,
ImportSection, ExportSection, GlobalSection, TypeSection, ElementSection,
TableSection, CustomSection,
};
pub use self::import_entry::{ImportEntry, ResizableLimits, MemoryType, TableType, GlobalType, External};
pub use self::export_entry::{ExportEntry, Internal};
pub use self::global_entry::GlobalEntry;
pub use self::primitives::{
VarUint32, VarUint7, Uint8, VarUint1, VarInt7, Uint32, VarInt32, VarInt64,
Uint64, VarUint64, CountedList, CountedWriter, CountedListWriter,
};
pub use self::types::{Type, ValueType, BlockType, FunctionType, TableElementType};
pub use self::ops::{Opcode, Opcodes, InitExpr};
pub use self::func::{Func, FuncBody, Local};
pub use self::segment::{ElementSegment, DataSegment};
pub use self::index_map::IndexMap;
pub use self::name_section::{
NameMap, NameSection, ModuleNameSection, FunctionNameSection,
LocalNameSection,
};
pub trait Deserialize : Sized {
type Error;
fn deserialize<R: io::Read>(reader: &mut R) -> Result<Self, Self::Error>;
}
pub trait Serialize {
type Error;
fn serialize<W: io::Write>(self, writer: &mut W) -> Result<(), Self::Error>;
}
#[derive(Debug, Clone)]
pub enum Error {
UnexpectedEof,
InvalidMagic,
UnsupportedVersion(u32),
InconsistentLength {
expected: usize,
actual: usize
},
Other(&'static str),
HeapOther(String),
UnknownValueType(i8),
UnknownTableElementType(i8),
NonUtf8String,
UnknownExternalKind(u8),
UnknownInternalKind(u8),
UnknownOpcode(u8),
InvalidVarUint1(u8),
InvalidVarInt32,
InvalidVarInt64,
InconsistentMetadata,
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Error::UnexpectedEof => write!(f, "Unexpected end of input"),
Error::InvalidMagic => write!(f, "Invalid magic number at start of file"),
Error::UnsupportedVersion(v) => write!(f, "Unsupported wasm version {}", v),
Error::InconsistentLength { expected, actual } => {
write!(f, "Expected length {}, found {}", expected, actual)
}
Error::Other(msg) => write!(f, "{}", msg),
Error::HeapOther(ref msg) => write!(f, "{}", msg),
Error::UnknownValueType(ty) => write!(f, "Invalid or unknown value type {}", ty),
Error::UnknownTableElementType(ty) => write!(f, "Unknown table element type {}", ty),
Error::NonUtf8String => write!(f, "Non-UTF-8 string"),
Error::UnknownExternalKind(kind) => write!(f, "Unknown external kind {}", kind),
Error::UnknownInternalKind(kind) => write!(f, "Unknown internal kind {}", kind),
Error::UnknownOpcode(opcode) => write!(f, "Unknown opcode {}", opcode),
Error::InvalidVarUint1(val) => write!(f, "Not an unsigned 1-bit integer: {}", val),
Error::InvalidVarInt32 => write!(f, "Not a signed 32-bit integer"),
Error::InvalidVarInt64 => write!(f, "Not a signed 64-bit integer"),
Error::InconsistentMetadata => write!(f, "Inconsistent metadata"),
}
}
}
impl error::Error for Error {
fn description(&self) -> &str {
match *self {
Error::UnexpectedEof => "Unexpected end of input",
Error::InvalidMagic => "Invalid magic number at start of file",
Error::UnsupportedVersion(_) => "Unsupported wasm version",
Error::InconsistentLength { .. } => "Inconsistent length",
Error::Other(msg) => msg,
Error::HeapOther(ref msg) => &msg[..],
Error::UnknownValueType(_) => "Invalid or unknown value type",
Error::UnknownTableElementType(_) => "Unknown table element type",
Error::NonUtf8String => "Non-UTF-8 string",
Error::UnknownExternalKind(_) => "Unknown external kind",
Error::UnknownInternalKind(_) => "Unknown internal kind",
Error::UnknownOpcode(_) => "Unknown opcode",
Error::InvalidVarUint1(_) => "Not an unsigned 1-bit integer",
Error::InvalidVarInt32 => "Not a signed 32-bit integer",
Error::InvalidVarInt64 => "Not a signed 64-bit integer",
Error::InconsistentMetadata => "Inconsistent metadata",
}
}
}
impl From<io::Error> for Error {
fn from(err: io::Error) -> Self {
Error::HeapOther(format!("I/O Error: {}", err))
}
}
pub struct Unparsed(pub Vec<u8>);
impl Deserialize for Unparsed {
type Error = Error;
fn deserialize<R: io::Read>(reader: &mut R) -> Result<Self, Self::Error> {
let len = VarUint32::deserialize(reader)?.into();
let mut vec = vec![0u8; len];
reader.read_exact(&mut vec[..])?;
Ok(Unparsed(vec))
}
}
impl From<Unparsed> for Vec<u8> {
fn from(u: Unparsed) -> Vec<u8> {
u.0
}
}
pub fn deserialize_file<P: AsRef<::std::path::Path>>(p: P) -> Result<Module, Error> {
use std::io::Read;
let mut contents = Vec::new();
::std::fs::File::open(p)?.read_to_end(&mut contents)?;
deserialize_buffer(&contents)
}
pub fn deserialize_buffer<T: Deserialize>(contents: &[u8]) -> Result<T, T::Error> {
let mut reader = io::Cursor::new(contents);
T::deserialize(&mut reader)
}
pub fn serialize<T: Serialize>(val: T) -> Result<Vec<u8>, T::Error> {
let mut buf = Vec::new();
val.serialize(&mut buf)?;
Ok(buf)
}
pub fn serialize_to_file<P: AsRef<::std::path::Path>>(p: P, module: Module) -> Result<(), Error>
{
let mut io = ::std::fs::File::create(p)?;
module.serialize(&mut io)
}