Coverage Report

Created: 2025-12-14 06:30

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