-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodule.cc
251 lines (231 loc) · 9.81 KB
/
module.cc
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#include <stdexcept>
#include "module.h"
#include "log.h"
namespace wvm
{
Module::Module(std::shared_ptr<Decoder> decoder) : decoder_(decoder)
{
}
Module::~Module()
{
}
std::shared_ptr<Decoder> Module::decoder()
{
return decoder_;
}
bool Module::byteArrayEq(const std::vector<uint8_t> &input1, const std::vector<uint8_t> &input2)
{
if (input1.size() != input2.size())
{
return false;
}
for (std::size_t i = 0; i < input1.size(); ++i)
{
if (input1[i] != input2[i])
{
return false;
}
}
return true;
}
void Module::parseMagicNumber()
{
std::vector<uint8_t> buffer = decoder_->readBytes(4);
if (!byteArrayEq(MAGIC_NUMBER, buffer))
{
throw std::runtime_error("magic header not detected");
}
LOG(" -> parseMagicNumber: ", buffer);
}
void Module::parseVersion()
{
std::vector<uint8_t> buffer = decoder_->readBytes(4);
if (!byteArrayEq(WASM_VERSION, buffer))
{
throw std::runtime_error("unknown binary version");
}
LOG(" -> parseVersion: ", buffer);
}
void Module::parseSection()
{
LOG(" -> parseSection");
while (!decoder_->readable().eof())
{
Section sectionId = static_cast<Section>(decoder_->readByte());
LOG(" -> parseSectionId: ", static_cast<int>(sectionId));
switch (sectionId)
{
case Section::type:
/* Type section -> https://webassembly.github.io/spec/core/binary/modules.html#binary-typesec */
parseTypeSection();
break;
case Section::import:
/* Import section -> https://webassembly.github.io/spec/core/binary/modules.html#binary-importsec */
parseImportSection();
break;
case Section::func:
/* Function section -> https://webassembly.github.io/spec/core/binary/modules.html#function-section */
parseFunctionSection();
break;
case Section::table:
/* Table section -> https://webassembly.github.io/spec/core/binary/modules.html#table-section */
parseTableSection();
break;
case Section::memory:
/* Memory section -> https://webassembly.github.io/spec/core/binary/modules.html#memory-section */
parseMemorySection();
break;
case Section::global:
/* Global section -> https://webassembly.github.io/spec/core/binary/modules.html#global-section */
parseGlobalSection();
case Section::export_section:
/* Export section -> https://webassembly.github.io/spec/core/binary/modules.html#export-section */
parseExportSection();
break;
case Section::code:
/* Code section -> https://webassembly.github.io/spec/core/binary/modules.html#binary-codesec */
parseCodeSection();
break;
default:
break;
}
}
decoder_->readable().close();
}
// ; func type 0
// 000000b: 60 ; func
// 000000c: 02 ; num params
// 000000d: 7f ; i32
// 000000e: 7f ; i32
// 000000f: 01 ; num results
// 0000010: 7f ; i32
// 0000009: 07 ; FIXUP section size
void Module::parseTypeSection()
{
WVM_KEEPALIVE const auto sectionSize = decoder_->readU32();
const auto numberOfTypes = decoder_->readU32();
for (uint32_t i = 0; i < numberOfTypes; ++i)
{
SectionType sectionType = static_cast<SectionType>(decoder_->readByte());
if (sectionType == SectionType::func)
{
auto pair = std::make_pair(
std::vector<uint8_t>{},
std::vector<uint8_t>{});
const auto paramsCount = decoder_->readU32();
for (uint32_t i = 0; i < paramsCount; ++i)
{
pair.first.push_back(decoder_->readByte());
}
const auto resultCount = decoder_->readU32();
for (uint32_t i = 0; i < resultCount; ++i)
{
pair.second.push_back(decoder_->readByte());
}
funcTypes.emplace_back(std::move(pair));
}
else
{
}
}
}
void Module::parseImportSection()
{
}
// ; section "Function" (3)
// 0000011: 03 ; section code
// 0000012: 00 ; section size (guess)
// 0000013: 02 ; num functions
// 0000014: 00 ; function 0 signature index
// 0000015: 00 ; function 1 signature index
// 0000012: 03 ; FIXUP section size
void Module::parseFunctionSection()
{
WVM_KEEPALIVE const auto sectionSize = decoder_->readU32();
const auto funcIndexCount = decoder_->readU32();
for (uint32_t i = 0; i < funcIndexCount; ++i)
{
funcTypesIndices.push_back(decoder_->readU32());
}
}
void Module::parseTableSection()
{
}
void Module::parseMemorySection()
{
}
void Module::parseGlobalSection()
{
}
// ; section "Export" (7)
// 0000016: 07 ; section code
// 0000017: 00 ; section size (guess)
// 0000018: 01 ; num exports
// 0000019: 03 ; string length
// 000001a: 6164 64 add ; export name
// 000001d: 00 ; export kind
// 000001e: 00 ; export func index
// 0000017: 07 ; FIXUP section size
void Module::parseExportSection()
{
WVM_KEEPALIVE const auto sectionSize = decoder_->readU32();
const auto exportCount = decoder_->readU32();
for (uint32_t i = 0; i < exportCount; ++i)
{
const auto fieldLen = decoder_->readU32();
const auto fieldStr = decoder_->readStringByBytes(fieldLen);
const auto extKind = decoder_->readByte();
const auto extIdx = decoder_->readU32();
exports.emplace_back(fieldStr, extKind, extIdx);
}
}
// ; section "Code" (10)
// 000001f: 0a ; section code
// 0000020: 00 ; section size (guess)
// 0000021: 02 ; num functions
// ; function body 0
// 0000022: 00 ; func body size (guess)
// 0000023: 00 ; local decl count
// 0000024: 20 ; local.get
// 0000025: 00 ; local index
// 0000026: 20 ; local.get
// 0000027: 01 ; local index
// 0000028: 10 ; call
// 0000029: 01 ; function index
// 000002a: 0b ; end
// 0000022: 08 ; FIXUP func body size
// ; function body 1
// 000002b: 00 ; func body size (guess)
// 000002c: 00 ; local decl count
// 000002d: 20 ; local.get
// 000002e: 00 ; local index
// 000002f: 20 ; local.get
// 0000030: 01 ; local index
// 0000031: 41 ; i32.const
// 0000032: 0a ; i32 literal
// 0000033: 1a ; drop
// 0000034: 6a ; i32.add
// 0000035: 0b ; end
// 000002b: 0a ; FIXUP func body size
// 0000020: 15 ; FIXUP section size
void Module::parseCodeSection()
{
WVM_KEEPALIVE const auto sectionSize = decoder_->readU32();
const auto funcDefCount = decoder_->readU32();
for (uint32_t i = 0; i < funcDefCount; ++i)
{
const auto bodySize = decoder_->readU32();
const auto startPos = decoder_->readable().tellg();
const auto locCount = decoder_->readU32();
std::vector<uint8_t> locVarTypeVec = {};
for (uint32_t j = 0; j < locCount; ++j)
{
const auto locVarCount = decoder_->readU32();
const auto locVarType = decoder_->readByte();
locVarTypeVec.insert(locVarTypeVec.end(), locVarCount, locVarType);
}
std::vector<uint8_t> body = decoder_->readBytes(bodySize - (decoder_->readable().tellg() - startPos));
funcDefs.emplace_back(locVarTypeVec, body);
}
}
}