Coverage Report

Created: 2026-01-17 06:48

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/wasm3/source/m3_parse.c
Line
Count
Source
1
//
2
//  m3_parse.c
3
//
4
//  Created by Steven Massey on 4/19/19.
5
//  Copyright © 2019 Steven Massey. All rights reserved.
6
//
7
8
#include "m3_env.h"
9
#include "m3_compile.h"
10
#include "m3_exception.h"
11
#include "m3_info.h"
12
13
14
M3Result  ParseType_Table  (IM3Module io_module, bytes_t i_bytes, cbytes_t i_end)
15
0
{
16
0
    M3Result result = m3Err_none;
17
18
0
    return result;
19
0
}
20
21
22
M3Result  ParseType_Memory  (M3MemoryInfo * o_memory, bytes_t * io_bytes, cbytes_t i_end)
23
538
{
24
538
    M3Result result = m3Err_none;
25
26
538
    u8 flag;
27
28
538
_   (ReadLEB_u7 (& flag, io_bytes, i_end));                   // really a u1
29
538
_   (ReadLEB_u32 (& o_memory->initPages, io_bytes, i_end));
30
31
538
    o_memory->maxPages = 0;
32
538
    if (flag & (1u << 0))
33
538
_       (ReadLEB_u32 (& o_memory->maxPages, io_bytes, i_end));
34
35
538
    o_memory->pageSize = 0;
36
538
    if (flag & (1u << 3)) {
37
43
        u32 logPageSize;
38
43
_       (ReadLEB_u32 (& logPageSize, io_bytes, i_end));
39
43
        o_memory->pageSize = 1u << logPageSize;
40
43
    }
41
42
538
    _catch: return result;
43
538
}
44
45
46
M3Result  ParseSection_Type  (IM3Module io_module, bytes_t i_bytes, cbytes_t i_end)
47
107
{
48
107
    IM3FuncType ftype = NULL;
49
50
107
_try {
51
107
    u32 numTypes;
52
107
_   (ReadLEB_u32 (& numTypes, & i_bytes, i_end));                                   m3log (parse, "** Type [%d]", numTypes);
53
54
107
    _throwif("too many types", numTypes > d_m3MaxSaneTypesCount);
55
56
107
    if (numTypes)
57
106
    {
58
        // table of IM3FuncType (that point to the actual M3FuncType struct in the Environment)
59
106
        io_module->funcTypes = m3_AllocArray (IM3FuncType, numTypes);
60
106
        _throwifnull (io_module->funcTypes);
61
106
        io_module->numFuncTypes = numTypes;
62
63
236
        for (u32 i = 0; i < numTypes; ++i)
64
157
        {
65
157
            i8 form;
66
157
_           (ReadLEB_i7 (& form, & i_bytes, i_end));
67
157
            _throwif (m3Err_wasmMalformed, form != -32); // for Wasm MVP
68
69
132
            u32 numArgs;
70
132
_           (ReadLEB_u32 (& numArgs, & i_bytes, i_end));
71
72
132
            _throwif (m3Err_tooManyArgsRets, numArgs > d_m3MaxSaneFunctionArgRetCount);
73
#if defined(M3_COMPILER_MSVC)
74
            u8 argTypes [d_m3MaxSaneFunctionArgRetCount];
75
#else
76
132
            u8 argTypes[numArgs+1]; // make ubsan happy
77
132
#endif
78
6.69k
            for (u32 a = 0; a < numArgs; ++a)
79
6.56k
            {
80
6.56k
                i8 wasmType;
81
6.56k
                u8 argType;
82
6.56k
_               (ReadLEB_i7 (& wasmType, & i_bytes, i_end));
83
6.56k
_               (NormalizeType (& argType, wasmType));
84
85
6.56k
                argTypes[a] = argType;
86
6.56k
            }
87
88
131
            u32 numRets;
89
131
_           (ReadLEB_u32 (& numRets, & i_bytes, i_end));
90
131
            _throwif (m3Err_tooManyArgsRets, (u64)(numRets) + numArgs > d_m3MaxSaneFunctionArgRetCount);
91
92
131
_           (AllocFuncType (& ftype, numRets + numArgs));
93
131
            ftype->numArgs = numArgs;
94
131
            ftype->numRets = numRets;
95
96
5.47k
            for (u32 r = 0; r < numRets; ++r)
97
5.34k
            {
98
5.34k
                i8 wasmType;
99
5.34k
                u8 retType;
100
5.34k
_               (ReadLEB_i7 (& wasmType, & i_bytes, i_end));
101
5.34k
_               (NormalizeType (& retType, wasmType));
102
103
5.34k
                ftype->types[r] = retType;
104
5.34k
            }
105
130
            memcpy (ftype->types + numRets, argTypes, numArgs);                                 m3log (parse, "    type %2d: %s", i, SPrintFuncTypeSignature (ftype));
106
107
130
            Environment_AddFuncType (io_module->environment, & ftype);
108
130
            io_module->funcTypes [i] = ftype;
109
130
            ftype = NULL; // ownership transferred to environment
110
130
        }
111
106
    }
112
113
107
} _catch:
114
115
107
    if (result)
116
27
    {
117
27
        m3_Free (ftype);
118
        // FIX: M3FuncTypes in the table are leaked
119
27
        m3_Free (io_module->funcTypes);
120
27
        io_module->numFuncTypes = 0;
121
27
    }
122
123
107
    return result;
124
107
}
125
126
127
M3Result  ParseSection_Function  (IM3Module io_module, bytes_t i_bytes, cbytes_t i_end)
128
72
{
129
72
    M3Result result = m3Err_none;
130
131
72
    u32 numFunctions;
132
72
_   (ReadLEB_u32 (& numFunctions, & i_bytes, i_end));                               m3log (parse, "** Function [%d]", numFunctions);
133
134
72
    _throwif("too many functions", numFunctions > d_m3MaxSaneFunctionsCount);
135
136
72
_   (Module_PreallocFunctions(io_module, io_module->numFunctions + numFunctions));
137
138
1.51k
    for (u32 i = 0; i < numFunctions; ++i)
139
1.44k
    {
140
1.44k
        u32 funcTypeIndex;
141
1.44k
_       (ReadLEB_u32 (& funcTypeIndex, & i_bytes, i_end));
142
143
1.44k
_       (Module_AddFunction (io_module, funcTypeIndex, NULL /* import info */));
144
1.44k
    }
145
146
72
    _catch: return result;
147
72
}
148
149
150
M3Result  ParseSection_Import  (IM3Module io_module, bytes_t i_bytes, cbytes_t i_end)
151
16
{
152
16
    M3Result result = m3Err_none;
153
154
16
    M3ImportInfo import = { NULL, NULL }, clearImport = { NULL, NULL };
155
156
16
    u32 numImports;
157
16
_   (ReadLEB_u32 (& numImports, & i_bytes, i_end));                                 m3log (parse, "** Import [%d]", numImports);
158
159
16
    _throwif("too many imports", numImports > d_m3MaxSaneImportsCount);
160
161
    // Most imports are functions, so we won't waste much space anyway (if any)
162
16
_   (Module_PreallocFunctions(io_module, numImports));
163
164
3.50k
    for (u32 i = 0; i < numImports; ++i)
165
3.50k
    {
166
3.50k
        u8 importKind;
167
168
3.50k
_       (Read_utf8 (& import.moduleUtf8, & i_bytes, i_end));
169
3.49k
_       (Read_utf8 (& import.fieldUtf8, & i_bytes, i_end));
170
3.49k
_       (Read_u8 (& importKind, & i_bytes, i_end));                                 m3log (parse, "    kind: %d '%s.%s' ",
171
3.49k
                                                                                                (u32) importKind, import.moduleUtf8, import.fieldUtf8);
172
3.49k
        switch (importKind)
173
3.49k
        {
174
2.42k
            case d_externalKind_function:
175
2.42k
            {
176
2.42k
                u32 typeIndex;
177
2.42k
_               (ReadLEB_u32 (& typeIndex, & i_bytes, i_end))
178
179
2.42k
_               (Module_AddFunction (io_module, typeIndex, & import))
180
2.41k
                import = clearImport;
181
182
2.41k
                io_module->numFuncImports++;
183
2.41k
            }
184
0
            break;
185
186
418
            case d_externalKind_table:
187
//                  result = ParseType_Table (& i_bytes, i_end);
188
418
                break;
189
190
538
            case d_externalKind_memory:
191
538
            {
192
538
_               (ParseType_Memory (& io_module->memoryInfo, & i_bytes, i_end));
193
538
                io_module->memoryImported = true;
194
538
                io_module->memoryImport = import;
195
538
                import = clearImport;
196
538
            }
197
0
            break;
198
199
119
            case d_externalKind_global:
200
119
            {
201
119
                i8 waType;
202
119
                u8 type, isMutable;
203
204
119
_               (ReadLEB_i7 (& waType, & i_bytes, i_end));
205
119
_               (NormalizeType (& type, waType));
206
119
_               (ReadLEB_u7 (& isMutable, & i_bytes, i_end));                     m3log (parse, "     global: %s mutable=%d", c_waTypes [type], (u32) isMutable);
207
208
119
                IM3Global global;
209
119
_               (Module_AddGlobal (io_module, & global, type, isMutable, true /* isImport */));
210
119
                global->import = import;
211
119
                import = clearImport;
212
119
            }
213
0
            break;
214
215
2
            default:
216
2
                _throw (m3Err_wasmMalformed);
217
3.49k
        }
218
219
3.49k
        FreeImportInfo (& import);
220
3.49k
    }
221
222
16
    _catch:
223
224
16
    FreeImportInfo (& import);
225
226
16
    return result;
227
16
}
228
229
230
M3Result  ParseSection_Export  (IM3Module io_module, bytes_t i_bytes, cbytes_t  i_end)
231
19
{
232
19
    M3Result result = m3Err_none;
233
19
    const char * utf8 = NULL;
234
235
19
    u32 numExports;
236
19
_   (ReadLEB_u32 (& numExports, & i_bytes, i_end));                                 m3log (parse, "** Export [%d]", numExports);
237
238
19
    _throwif("too many exports", numExports > d_m3MaxSaneExportsCount);
239
240
2.21k
    for (u32 i = 0; i < numExports; ++i)
241
2.20k
    {
242
2.20k
        u8 exportKind;
243
2.20k
        u32 index;
244
245
2.20k
_       (Read_utf8 (& utf8, & i_bytes, i_end));
246
2.20k
_       (Read_u8 (& exportKind, & i_bytes, i_end));
247
2.20k
_       (ReadLEB_u32 (& index, & i_bytes, i_end));                                  m3log (parse, "    index: %3d; kind: %d; export: '%s'; ", index, (u32) exportKind, utf8);
248
249
2.19k
        if (exportKind == d_externalKind_function)
250
503
        {
251
503
            _throwif(m3Err_wasmMalformed, index >= io_module->numFunctions);
252
502
            IM3Function func = &(io_module->functions [index]);
253
502
            if (func->numNames < d_m3MaxDuplicateFunctionImpl)
254
135
            {
255
135
                func->names[func->numNames++] = utf8;
256
135
                func->export_name = utf8;
257
135
                utf8 = NULL; // ownership transferred to M3Function
258
135
            }
259
502
        }
260
1.69k
        else if (exportKind == d_externalKind_global)
261
0
        {
262
0
            _throwif(m3Err_wasmMalformed, index >= io_module->numGlobals);
263
0
            IM3Global global = &(io_module->globals [index]);
264
0
            m3_Free (global->name);
265
0
            global->name = utf8;
266
0
            utf8 = NULL; // ownership transferred to M3Global
267
0
        }
268
1.69k
        else if (exportKind == d_externalKind_memory)
269
508
        {
270
508
            m3_Free (io_module->memoryExportName);
271
508
            io_module->memoryExportName = utf8;
272
508
            utf8 = NULL; // ownership transferred to M3Module
273
508
        }
274
1.18k
        else if (exportKind == d_externalKind_table)
275
446
        {
276
446
            m3_Free (io_module->table0ExportName);
277
446
            io_module->table0ExportName = utf8;
278
446
            utf8 = NULL; // ownership transferred to M3Module
279
446
        }
280
281
2.19k
        m3_Free (utf8);
282
2.19k
    }
283
284
19
_catch:
285
19
    m3_Free (utf8);
286
19
    return result;
287
19
}
288
289
290
M3Result  ParseSection_Start  (IM3Module io_module, bytes_t i_bytes, cbytes_t i_end)
291
1
{
292
1
    M3Result result = m3Err_none;
293
294
1
    u32 startFuncIndex;
295
1
_   (ReadLEB_u32 (& startFuncIndex, & i_bytes, i_end));                               m3log (parse, "** Start Function: %d", startFuncIndex);
296
297
1
    if (startFuncIndex < io_module->numFunctions)
298
1
    {
299
1
        io_module->startFunction = startFuncIndex;
300
1
    }
301
0
    else result = "start function index out of bounds";
302
303
1
    _catch: return result;
304
1
}
305
306
307
M3Result  Parse_InitExpr  (M3Module * io_module, bytes_t * io_bytes, cbytes_t i_end)
308
291
{
309
291
    M3Result result = m3Err_none;
310
311
    // this doesn't generate code pages. just walks the wasm bytecode to find the end
312
313
#if defined(d_m3PreferStaticAlloc)
314
    static M3Compilation compilation;
315
#else
316
291
    M3Compilation compilation;
317
291
#endif
318
291
    compilation = (M3Compilation){ .runtime = NULL, .module = io_module, .wasm = * io_bytes, .wasmEnd = i_end };
319
320
291
    result = CompileBlockStatements (& compilation);
321
322
291
    * io_bytes = compilation.wasm;
323
324
291
    return result;
325
291
}
326
327
328
M3Result  ParseSection_Element  (IM3Module io_module, bytes_t i_bytes, cbytes_t i_end)
329
3
{
330
3
    M3Result result = m3Err_none;
331
332
3
    u32 numSegments;
333
3
_   (ReadLEB_u32 (& numSegments, & i_bytes, i_end));                         m3log (parse, "** Element [%d]", numSegments);
334
335
3
    _throwif ("too many element segments", numSegments > d_m3MaxSaneElementSegments);
336
337
3
    io_module->elementSection = i_bytes;
338
3
    io_module->elementSectionEnd = i_end;
339
3
    io_module->numElementSegments = numSegments;
340
341
3
    _catch: return result;
342
3
}
343
344
345
M3Result  ParseSection_Code  (M3Module * io_module, bytes_t i_bytes, cbytes_t i_end)
346
48
{
347
48
    M3Result result;
348
349
48
    u32 numFunctions;
350
48
_   (ReadLEB_u32 (& numFunctions, & i_bytes, i_end));                               m3log (parse, "** Code [%d]", numFunctions);
351
352
48
    if (numFunctions != io_module->numFunctions - io_module->numFuncImports)
353
7
    {
354
7
        _throw ("mismatched function count in code section");
355
0
    }
356
357
197
    for (u32 f = 0; f < numFunctions; ++f)
358
157
    {
359
157
        const u8 * start = i_bytes;
360
361
157
        u32 size;
362
157
_       (ReadLEB_u32 (& size, & i_bytes, i_end));
363
364
157
        if (size)
365
41
        {
366
41
            const u8 * ptr = i_bytes;
367
41
            i_bytes += size;
368
369
41
            if (i_bytes <= i_end)
370
40
            {
371
                /*
372
                u32 numLocalBlocks;
373
_               (ReadLEB_u32 (& numLocalBlocks, & ptr, i_end));                                      m3log (parse, "    code size: %-4d", size);
374
375
                u32 numLocals = 0;
376
377
                for (u32 l = 0; l < numLocalBlocks; ++l)
378
                {
379
                    u32 varCount;
380
                    i8 wasmType;
381
                    u8 normalType;
382
383
_                   (ReadLEB_u32 (& varCount, & ptr, i_end));
384
_                   (ReadLEB_i7 (& wasmType, & ptr, i_end));
385
_                   (NormalizeType (& normalType, wasmType));
386
387
                    numLocals += varCount;                                                      m3log (parse, "      %2d locals; type: '%s'", varCount, c_waTypes [normalType]);
388
                }
389
                 */
390
391
40
                IM3Function func = Module_GetFunction (io_module, f + io_module->numFuncImports);
392
393
40
                func->module = io_module;
394
40
                func->wasm = start;
395
40
                func->wasmEnd = i_bytes;
396
                //func->ownsWasmCode = io_module->hasWasmCodeCopy;
397
//                func->numLocals = numLocals;
398
40
            }
399
40
            else _throw (m3Err_wasmSectionOverrun);
400
40
        }
401
157
    }
402
403
48
    _catch:
404
405
48
    if (not result and i_bytes != i_end)
406
0
        result = m3Err_wasmSectionUnderrun;
407
408
48
    return result;
409
41
}
410
411
412
M3Result  ParseSection_Data  (M3Module * io_module, bytes_t i_bytes, cbytes_t i_end)
413
40
{
414
40
    M3Result result = m3Err_none;
415
416
40
    u32 numDataSegments;
417
40
_   (ReadLEB_u32 (& numDataSegments, & i_bytes, i_end));                            m3log (parse, "** Data [%d]", numDataSegments);
418
419
40
    _throwif("too many data segments", numDataSegments > d_m3MaxSaneDataSegments);
420
421
40
    io_module->dataSegments = m3_AllocArray (M3DataSegment, numDataSegments);
422
40
    _throwifnull(io_module->dataSegments);
423
40
    io_module->numDataSegments = numDataSegments;
424
425
126
    for (u32 i = 0; i < numDataSegments; ++i)
426
98
    {
427
98
        M3DataSegment * segment = & io_module->dataSegments [i];
428
429
98
_       (ReadLEB_u32 (& segment->memoryRegion, & i_bytes, i_end));
430
431
98
        segment->initExpr = i_bytes;
432
98
_       (Parse_InitExpr (io_module, & i_bytes, i_end));
433
86
        segment->initExprSize = (u32) (i_bytes - segment->initExpr);
434
435
86
        _throwif (m3Err_wasmMissingInitExpr, segment->initExprSize <= 1);
436
437
86
_       (ReadLEB_u32 (& segment->size, & i_bytes, i_end));
438
86
        segment->data = i_bytes;                                                    m3log (parse, "    segment [%u]  memory: %u;  expr-size: %d;  size: %d",
439
86
                                                                                       i, segment->memoryRegion, segment->initExprSize, segment->size);
440
86
        i_bytes += segment->size;
441
442
86
        _throwif("data segment underflow", i_bytes > i_end);
443
86
    }
444
445
40
    _catch:
446
447
40
    return result;
448
40
}
449
450
451
M3Result  ParseSection_Memory  (M3Module * io_module, bytes_t i_bytes, cbytes_t i_end)
452
0
{
453
0
    M3Result result = m3Err_none;
454
455
    // TODO: MVP; assert no memory imported
456
457
0
    u32 numMemories;
458
0
_   (ReadLEB_u32 (& numMemories, & i_bytes, i_end));                             m3log (parse, "** Memory [%d]", numMemories);
459
460
0
    _throwif (m3Err_tooManyMemorySections, numMemories != 1);
461
462
0
    ParseType_Memory (& io_module->memoryInfo, & i_bytes, i_end);
463
464
0
    _catch: return result;
465
0
}
466
467
468
M3Result  ParseSection_Global  (M3Module * io_module, bytes_t i_bytes, cbytes_t i_end)
469
14
{
470
14
    M3Result result = m3Err_none;
471
472
14
    u32 numGlobals;
473
14
_   (ReadLEB_u32 (& numGlobals, & i_bytes, i_end));                                 m3log (parse, "** Global [%d]", numGlobals);
474
475
14
    _throwif("too many globals", numGlobals > d_m3MaxSaneGlobalsCount);
476
477
204
    for (u32 i = 0; i < numGlobals; ++i)
478
194
    {
479
194
        i8 waType;
480
194
        u8 type, isMutable;
481
482
194
_       (ReadLEB_i7 (& waType, & i_bytes, i_end));
483
194
_       (NormalizeType (& type, waType));
484
193
_       (ReadLEB_u7 (& isMutable, & i_bytes, i_end));                                 m3log (parse, "    global: [%d] %s mutable: %d", i, c_waTypes [type],   (u32) isMutable);
485
486
193
        IM3Global global;
487
193
_       (Module_AddGlobal (io_module, & global, type, isMutable, false /* isImport */));
488
489
193
        global->initExpr = i_bytes;
490
193
_       (Parse_InitExpr (io_module, & i_bytes, i_end));
491
190
        global->initExprSize = (u32) (i_bytes - global->initExpr);
492
493
190
        _throwif (m3Err_wasmMissingInitExpr, global->initExprSize <= 1);
494
190
    }
495
496
14
    _catch: return result;
497
14
}
498
499
500
M3Result  ParseSection_Name  (M3Module * io_module, bytes_t i_bytes, cbytes_t i_end)
501
377
{
502
377
    M3Result result;
503
504
377
    cstr_t name;
505
506
972
    while (i_bytes < i_end)
507
598
    {
508
598
        u8 nameType;
509
598
        u32 payloadLength;
510
511
598
_       (ReadLEB_u7 (& nameType, & i_bytes, i_end));
512
598
_       (ReadLEB_u32 (& payloadLength, & i_bytes, i_end));
513
514
597
        bytes_t start = i_bytes;
515
597
        if (nameType == 1)
516
216
        {
517
216
            u32 numNames;
518
216
_           (ReadLEB_u32 (& numNames, & i_bytes, i_end));
519
520
216
            _throwif("too many names", numNames > d_m3MaxSaneFunctionsCount);
521
522
972
            for (u32 i = 0; i < numNames; ++i)
523
758
            {
524
758
                u32 index;
525
758
_               (ReadLEB_u32 (& index, & i_bytes, i_end));
526
758
_               (Read_utf8 (& name, & i_bytes, i_end));
527
528
757
                if (index < io_module->numFunctions)
529
723
                {
530
723
                    IM3Function func = &(io_module->functions [index]);
531
723
                    if (func->numNames == 0)
532
115
                    {
533
115
                        func->names[0] = name;        m3log (parse, "    naming function%5d:  %s", index, name);
534
115
                        func->numNames = 1;
535
115
                        name = NULL; // transfer ownership
536
115
                    }
537
//                          else m3log (parse, "prenamed: %s", io_module->functions [index].name);
538
723
                }
539
540
757
                m3_Free (name);
541
757
            }
542
215
        }
543
544
595
        i_bytes = start + payloadLength;
545
595
    }
546
547
377
    _catch: return result;
548
377
}
549
550
551
M3Result  ParseSection_Custom  (M3Module * io_module, bytes_t i_bytes, cbytes_t i_end)
552
971
{
553
971
    M3Result result;
554
555
971
    cstr_t name;
556
971
_   (Read_utf8 (& name, & i_bytes, i_end));
557
969
                                                                                    m3log (parse, "** Custom: '%s'", name);
558
969
    if (strcmp (name, "name") == 0) {
559
377
_       (ParseSection_Name(io_module, i_bytes, i_end));
560
592
    } else if (io_module->environment->customSectionHandler) {
561
0
_       (io_module->environment->customSectionHandler(io_module, name, i_bytes, i_end));
562
0
    }
563
564
966
    m3_Free (name);
565
566
971
    _catch: return result;
567
966
}
568
569
570
M3Result  ParseModuleSection  (M3Module * o_module, u8 i_sectionType, bytes_t i_bytes, u32 i_numBytes)
571
1.29k
{
572
1.29k
    M3Result result = m3Err_none;
573
574
1.29k
    typedef M3Result (* M3Parser) (M3Module *, bytes_t, cbytes_t);
575
576
1.29k
    static M3Parser s_parsers [] =
577
1.29k
    {
578
1.29k
        ParseSection_Custom,    // 0
579
1.29k
        ParseSection_Type,      // 1
580
1.29k
        ParseSection_Import,    // 2
581
1.29k
        ParseSection_Function,  // 3
582
1.29k
        NULL,                   // 4: TODO Table
583
1.29k
        ParseSection_Memory,    // 5
584
1.29k
        ParseSection_Global,    // 6
585
1.29k
        ParseSection_Export,    // 7
586
1.29k
        ParseSection_Start,     // 8
587
1.29k
        ParseSection_Element,   // 9
588
1.29k
        ParseSection_Code,      // 10
589
1.29k
        ParseSection_Data,      // 11
590
1.29k
        NULL,                   // 12: TODO DataCount
591
1.29k
    };
592
593
1.29k
    M3Parser parser = NULL;
594
595
1.29k
    if (i_sectionType <= 12)
596
1.29k
        parser = s_parsers [i_sectionType];
597
598
1.29k
    if (parser)
599
1.29k
    {
600
1.29k
        cbytes_t end = i_bytes + i_numBytes;
601
1.29k
        result = parser (o_module, i_bytes, end);
602
1.29k
    }
603
1
    else
604
1
    {
605
1
        m3log (parse, " skipped section type: %d", (u32) i_sectionType);
606
1
    }
607
608
1.29k
    return result;
609
1.29k
}
610
611
612
M3Result  m3_ParseModule  (IM3Environment i_environment, IM3Module * o_module, cbytes_t i_bytes, u32 i_numBytes)
613
184
{
614
184
    IM3Module module;                                                               m3log (parse, "load module: %d bytes", i_numBytes);
615
184
_try {
616
184
    module = m3_AllocStruct (M3Module);
617
184
    _throwifnull (module);
618
184
    module->name = ".unnamed";                                                      m3log (parse, "load module: %d bytes", i_numBytes);
619
184
    module->startFunction = -1;
620
    //module->hasWasmCodeCopy = false;
621
184
    module->environment = i_environment;
622
623
184
    const u8 * pos = i_bytes;
624
184
    const u8 * end = pos + i_numBytes;
625
626
184
    module->wasmStart = pos;
627
184
    module->wasmEnd = end;
628
629
184
    u32 magic, version;
630
184
_   (Read_u32 (& magic, & pos, end));
631
184
_   (Read_u32 (& version, & pos, end));
632
633
184
    _throwif (m3Err_wasmMalformed, magic != 0x6d736100);
634
182
    _throwif (m3Err_incompatibleWasmVersion, version != 1);
635
636
180
    static const u8 sectionsOrder[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 12, 10, 11, 0 }; // 0 is a placeholder
637
180
    u8 expectedSection = 0;
638
639
1.39k
    while (pos < end)
640
1.30k
    {
641
1.30k
        u8 section;
642
1.30k
_       (ReadLEB_u7 (& section, & pos, end));
643
644
1.30k
        if (section != 0) {
645
            // Ensure sections appear only once and in order
646
1.33k
            while (sectionsOrder[expectedSection++] != section) {
647
1.01k
                _throwif(m3Err_misorderedWasmSection, expectedSection >= 12);
648
1.00k
            }
649
327
        }
650
651
1.29k
        u32 sectionLength;
652
1.29k
_       (ReadLEB_u32 (& sectionLength, & pos, end));
653
1.29k
        _throwif(m3Err_wasmMalformed, pos + sectionLength > end);
654
655
1.29k
_       (ParseModuleSection (module, section, pos, sectionLength));
656
657
1.21k
        pos += sectionLength;
658
1.21k
    }
659
660
184
} _catch:
661
662
184
    if (result)
663
88
    {
664
88
        m3_FreeModule (module);
665
88
        module = NULL;
666
88
    }
667
668
184
    * o_module = module;
669
670
184
    return result;
671
180
}