@@ -45,6 +45,8 @@ class WasmWriter {
45
45
int writeSectionContent (raw_ostream &OS, WasmYAML::NameSection &Section);
46
46
int writeSectionContent (raw_ostream &OS, WasmYAML::LinkingSection &Section);
47
47
WasmYAML::Object &Obj;
48
+ uint32_t NumImportedFunctions = 0 ;
49
+ uint32_t NumImportedGlobals = 0 ;
48
50
};
49
51
50
52
static int writeUint64 (raw_ostream &OS, uint64_t Value) {
@@ -101,7 +103,7 @@ static int writeInitExpr(const wasm::WasmInitExpr &InitExpr, raw_ostream &OS) {
101
103
encodeULEB128 (InitExpr.Value .Global , OS);
102
104
break ;
103
105
default :
104
- errs () << " Unknown opcode in init_expr: " << InitExpr.Opcode ;
106
+ errs () << " Unknown opcode in init_expr: " << InitExpr.Opcode << " \n " ;
105
107
return 1 ;
106
108
}
107
109
writeUint8 (OS, wasm::WASM_OPCODE_END);
@@ -211,7 +213,13 @@ int WasmWriter::writeSectionContent(raw_ostream &OS,
211
213
int WasmWriter::writeSectionContent (raw_ostream &OS,
212
214
WasmYAML::TypeSection &Section) {
213
215
encodeULEB128 (Section.Signatures .size (), OS);
216
+ uint32_t ExpectedIndex = 0 ;
214
217
for (const WasmYAML::Signature &Sig : Section.Signatures ) {
218
+ if (Sig.Index != ExpectedIndex) {
219
+ errs () << " Unexpected type index: " << Sig.Index << " \n " ;
220
+ return 1 ;
221
+ }
222
+ ++ExpectedIndex;
215
223
encodeSLEB128 (Sig.Form , OS);
216
224
encodeULEB128 (Sig.ParamTypes .size (), OS);
217
225
for (auto ParamType : Sig.ParamTypes )
@@ -236,10 +244,12 @@ int WasmWriter::writeSectionContent(raw_ostream &OS,
236
244
switch (Import.Kind ) {
237
245
case wasm::WASM_EXTERNAL_FUNCTION:
238
246
encodeULEB128 (Import.SigIndex , OS);
247
+ NumImportedFunctions++;
239
248
break ;
240
249
case wasm::WASM_EXTERNAL_GLOBAL:
241
250
encodeSLEB128 (Import.GlobalImport .Type , OS);
242
251
writeUint8 (OS, Import.GlobalImport .Mutable );
252
+ NumImportedGlobals++;
243
253
break ;
244
254
case wasm::WASM_EXTERNAL_MEMORY:
245
255
writeLimits (Import.Memory , OS);
@@ -249,7 +259,7 @@ int WasmWriter::writeSectionContent(raw_ostream &OS,
249
259
writeLimits (Import.TableImport .TableLimits , OS);
250
260
break ;
251
261
default :
252
- errs () << " Unknown import type: " << Import.Kind ;
262
+ errs () << " Unknown import type: " << Import.Kind << " \n " ;
253
263
return 1 ;
254
264
}
255
265
}
@@ -304,7 +314,13 @@ int WasmWriter::writeSectionContent(raw_ostream &OS,
304
314
int WasmWriter::writeSectionContent (raw_ostream &OS,
305
315
WasmYAML::GlobalSection &Section) {
306
316
encodeULEB128 (Section.Globals .size (), OS);
317
+ uint32_t ExpectedIndex = NumImportedGlobals;
307
318
for (auto &Global : Section.Globals ) {
319
+ if (Global.Index != ExpectedIndex) {
320
+ errs () << " Unexpected global index: " << Global.Index << " \n " ;
321
+ return 1 ;
322
+ }
323
+ ++ExpectedIndex;
308
324
encodeSLEB128 (Global.Type , OS);
309
325
writeUint8 (OS, Global.Mutable );
310
326
writeInitExpr (Global.InitExpr , OS);
@@ -330,9 +346,15 @@ int WasmWriter::writeSectionContent(raw_ostream &OS,
330
346
int WasmWriter::writeSectionContent (raw_ostream &OS,
331
347
WasmYAML::CodeSection &Section) {
332
348
encodeULEB128 (Section.Functions .size (), OS);
349
+ uint32_t ExpectedIndex = NumImportedFunctions;
333
350
for (auto &Func : Section.Functions ) {
334
351
std::string OutString;
335
352
raw_string_ostream StringStream (OutString);
353
+ if (Func.Index != ExpectedIndex) {
354
+ errs () << " Unexpected function index: " << Func.Index << " \n " ;
355
+ return 1 ;
356
+ }
357
+ ++ExpectedIndex;
336
358
337
359
encodeULEB128 (Func.Locals .size (), StringStream);
338
360
for (auto &LocalDecl : Func.Locals ) {
@@ -402,9 +424,18 @@ int WasmWriter::writeWasm(raw_ostream &OS) {
402
424
writeUint32 (OS, Obj.Header .Version );
403
425
404
426
// Write each section
427
+ uint32_t LastType = 0 ;
405
428
for (const std::unique_ptr<WasmYAML::Section> &Sec : Obj.Sections ) {
406
- encodeULEB128 (Sec->Type , OS);
429
+ uint32_t Type = Sec->Type ;
430
+ if (Type != wasm::WASM_SEC_CUSTOM) {
431
+ if (Type < LastType) {
432
+ errs () << " Out of order section type: " << Type << " \n " ;
433
+ return 1 ;
434
+ }
435
+ LastType = Type;
436
+ }
407
437
438
+ encodeULEB128 (Sec->Type , OS);
408
439
std::string OutString;
409
440
raw_string_ostream StringStream (OutString);
410
441
if (auto S = dyn_cast<WasmYAML::CustomSection>(Sec.get ())) {
0 commit comments