Coverage Report

Created: 2026-04-09 06:12

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