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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
//! WASM Binary Decoder - Converts wasmparser operators to WasmOp sequences
//!
//! This module bridges the gap between parsed WASM binaries and any backend.
//! It extracts function bodies and converts wasmparser operators to our internal WasmOp format.
use crate::wasm_op::WasmOp;
use anyhow::{Context, Result};
use std::collections::HashMap;
use wasmparser::{ExternalKind, Parser, Payload};
/// Kind of a WASM import
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ImportKind {
/// Imported function with type index
Function(u32),
/// Imported memory
Memory,
/// Imported table
Table,
/// Imported global
Global,
}
/// A WASM import entry with full metadata
#[derive(Debug, Clone)]
pub struct ImportEntry {
/// Module name (e.g., "wasi:cli/stdout" or "env")
pub module: String,
/// Field name (e.g., "write" or "memory")
pub name: String,
/// Import kind and associated data
pub kind: ImportKind,
/// Index of this import within its kind (e.g., function import index)
pub index: u32,
}
/// WASM linear memory specification
#[derive(Debug, Clone)]
pub struct WasmMemory {
/// Memory index
pub index: u32,
/// Initial size in pages (64KB each)
pub initial_pages: u32,
/// Maximum size in pages (if specified)
pub max_pages: Option<u32>,
/// Whether memory is shared (requires threads proposal)
pub shared: bool,
}
/// A captured constant global initializer (#649). Only INTEGER `t.const` init
/// exprs are captured: `f32.const`/`f64.const` inits deliberately decode to
/// `None` — float-typed global ACCESS is the GI-FPU-001 (#369) loud-skip lane,
/// and fabricating a bit-pattern here must not quietly unskip it. Non-const
/// init exprs (e.g. `global.get` of an import) are not statically known and
/// also decode to `None`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum GlobalInit {
/// A leading `i32.const` initializer.
I32(i32),
/// A leading `i64.const` initializer — BOTH words must reach the emitted
/// global slot (#649: `init_i32`-shaped capture silently zeroed these).
I64(i64),
}
/// A WASM global's declaration — its initial value and mutability (#237).
/// Needed so the native-pointer ABI can recognize a global whose initializer is
/// a linear-memory address (e.g. `$__stack_pointer = 65536`) and make it
/// `__synth_wasm_data`-relative, rather than reading it from an R9 globals table
/// the self-contained drop-in object can't rely on.
#[derive(Debug, Clone)]
pub struct WasmGlobal {
/// Global index (defined globals; imported globals are not counted here).
pub index: u32,
/// The captured constant initializer (#237/#649): `i32.const` or
/// `i64.const`. Float/non-const init exprs decode to `None` — see
/// [`GlobalInit`].
pub init: Option<GlobalInit>,
/// Whether the global is mutable.
pub mutable: bool,
/// #643: byte width of the global's storage slot, from its declared value
/// type — 4 for i32/f32, 8 for i64/f64, 16 for v128. The globals table is
/// laid out by SUMMING these widths (not `index * 4`): an i64 global needs
/// room for both words, and every later global's offset shifts with it.
pub slot_bytes: u32,
}
impl WasmMemory {
/// Get initial size in bytes
pub fn initial_bytes(&self) -> u32 {
self.initial_pages * 65536
}
/// Get maximum size in bytes (or initial if not specified)
pub fn max_bytes(&self) -> u32 {
self.max_pages.unwrap_or(self.initial_pages) * 65536
}
}
/// #642: one element segment's statically-decoded shape — see
/// [`DecodedModule::elem_segments`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ElemSegmentInfo {
/// #650: the table this ACTIVE segment initializes (0 for the pre-#650
/// single-table form). Meaningless when `offset` is `None`.
pub table_index: u32,
/// Const i32 offset of an ACTIVE segment into its table; `None` =
/// placement not statically verifiable (passive/declared segment or a
/// non-const offset expression).
pub offset: Option<u32>,
/// The segment's function indices in slot order; `None` = contents not
/// statically verifiable (an entry was not a plain `ref.func`).
pub funcs: Option<Vec<u32>>,
}
/// #642/#650: one table's `call_indirect` guard inputs — see
/// [`CallIndirectGuards`] for the layout contract and soundness argument.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct TableGuards {
/// Compile-time size of this table (entries); `None` = no sound bound
/// known (an imported table with growable limits).
pub table_size: Option<u32>,
/// #650: byte offset of this table's base within the contiguous R11
/// region — `sum(size(0..N)) * 4`, a compile-time constant. `None` when
/// any PRECEDING table's size is unknown (the base is then not a
/// compile-time constant and the lowering declines).
pub base_byte_offset: Option<u32>,
/// Per expected-type index: `None` = closed-world type property VERIFIED
/// against THIS table; `Some(reason)` = not verifiable (the lowering
/// declines).
pub type_reject: Vec<Option<String>>,
/// #664: whether this table's image contains at least one uninitialized
/// (null funcref) slot. WASM Core §4.4.8 requires a `call_indirect`
/// reaching a null slot to TRAP — the closed-world type check verifies
/// the INITIALIZED slots only, and the lowering must emit a runtime
/// null check (pointer == 0 → trap) before the indirect branch when
/// this is set. `false` for a fully-initialized table keeps today's
/// exact dispatch bytes (no null check) BY CONSTRUCTION. Only
/// meaningful when the type verdict is `None` (verified); reject paths
/// decline before it is consulted.
pub has_null_slots: bool,
}
/// #642/#650: everything the `call_indirect` lowering needs to emit its
/// guards — computed once per module by
/// [`DecodedModule::call_indirect_guards`] and threaded to the instruction
/// selector via `CompileConfig`.
///
/// ## The R11 multi-table layout contract (#650)
///
/// The runtime/harness links every funcref table as ONE contiguous region of
/// raw 4-byte code pointers based at R11, in declaration order (imported
/// tables first): table 0 at `R11 + 0`, table N at
/// `R11 + sum(size(0..N)) * 4`. The offsets are compile-time constants
/// because tables are provably fixed-size (`table.grow`/`table.set` are
/// unsupported ops whose functions loud-skip at decode — #642). A
/// single-table module degenerates to the pre-#650 contract (table 0 at
/// R11, offset 0) BY CONSTRUCTION, keeping its emitted bytes identical.
///
/// WASM Core §4.4.8 requires `call_indirect` to trap when `index >=
/// table.size` and when the callee's type does not match the instruction's
/// expected type. The region stores no size fields and no type ids, so, per
/// table:
/// - the BOUNDS check is emitted at runtime against THAT table's
/// compile-time `table_size` immediate (sound: fixed-size, see above), and
/// - the TYPE check is discharged at COMPILE time: for expected type `t`,
/// `tables[n].type_reject[t]` is `None` only when every INITIALIZED slot
/// of table `n` verifiably holds a function whose signature structurally
/// equals type `t` (the closed-world property — no runtime mismatch is
/// then possible). Otherwise it holds the reason, and the lowering
/// declines LOUDLY rather than emit an unchecked indirect branch, and
/// - a NULL (uninitialized) slot traps at RUNTIME (#664): the layout
/// contract requires the runtime/harness to link every uninitialized
/// slot as a ZERO word (null funcref has no code address; 0 is never a
/// valid function pointer in the region), and when `has_null_slots` is
/// set the dispatch emits a null check on the loaded pointer
/// (`CMP #0` → trap) between the bounds guard and the indirect branch.
/// A fully-initialized table (`has_null_slots == false`) keeps the
/// pre-#664 dispatch bytes identical BY CONSTRUCTION.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct CallIndirectGuards {
/// Per-table guard inputs, indexed by table index (imports first). The
/// default (empty — no module context) DECLINES every `call_indirect`.
pub tables: Vec<TableGuards>,
}
impl CallIndirectGuards {
/// Single-table (table 0 at R11 offset 0) guards — the pre-#650 shape,
/// used by tests and single-table call sites.
pub fn single_table(table_size: Option<u32>, type_reject: Vec<Option<String>>) -> Self {
Self {
tables: vec![TableGuards {
table_size,
base_byte_offset: Some(0),
type_reject,
has_null_slots: false,
}],
}
}
}
/// Decoded WASM module with functions and memory
#[derive(Debug, Clone)]
pub struct DecodedModule {
/// Decoded functions
pub functions: Vec<FunctionOps>,
/// Linear memories
pub memories: Vec<WasmMemory>,
/// Data segments (offset, data) for memory initialization
pub data_segments: Vec<(u32, Vec<u8>)>,
/// Import entries (module name, field name, kind)
pub imports: Vec<ImportEntry>,
/// Number of imported functions (for distinguishing import calls from local calls)
pub num_imported_funcs: u32,
/// AAPCS integer-argument count per function, indexed by the *full* WASM
/// function index (imported functions first, then locally-defined ones).
/// Used by the backend to marshal call arguments into R0–R3 (issue #195).
/// Counts every parameter as one slot (i64/f64 over-counted — see the
/// backend's `set_func_arg_counts` scope note).
pub func_arg_counts: Vec<u32>,
/// AAPCS integer-argument count per *function type*, indexed by type index.
/// Used by `call_indirect`, whose callee arg count comes from the static
/// type index (issue #195).
pub type_arg_counts: Vec<u32>,
/// #311: whether each *function* (full index, imports first) returns i64 —
/// the call lowering must tag the result as a register PAIR (r0:r1) or the
/// hi half is invisible to liveness and the next constant clobbers it.
pub func_ret_i64: Vec<bool>,
/// #311: whether each *function type* returns i64 (for `call_indirect`).
pub type_ret_i64: Vec<bool>,
/// #359: declared parameter widths per *function* (full index, imports
/// first): `func_params_i64[f][k]` is true when param `k` is i64/f64. The
/// AAPCS stack-argument path needs the declared widths — op-stream inference
/// can't see an unused i64 param that still shifts the incoming-stack layout.
pub func_params_i64: Vec<Vec<bool>>,
/// Defined globals with their initializers (#237). Empty if the module has
/// no global section. Used by the native-pointer ABI to make a global whose
/// initializer is a linear-memory address (e.g. `$__stack_pointer`)
/// self-contained rather than table-relative.
pub globals: Vec<WasmGlobal>,
/// Function indices that populate any table via an element segment (#275).
/// These are the possible `call_indirect` targets — a function reached only
/// through the table is invisible to direct-`call` reachability, so the
/// whole-graph closure must treat every table entry as reachable once any
/// reachable function performs a `call_indirect`. Empty for modules with no
/// element section (every leaf/direct-call module), keeping output identical.
pub elem_func_indices: Vec<u32>,
/// #642: compile-time size (in entries) of table 0 — `table_sizes[0]`,
/// kept as a convenience accessor. See [`Self::table_sizes`].
pub table_size: Option<u32>,
/// #650: compile-time size (in entries) per table, indexed by table index
/// (imported tables first, then the table section, in declaration order).
/// A DEFINED table's size is exact: `table.grow`/`table.set` are
/// unsupported ops (their functions loud-skip at decode), so nothing
/// synth compiles can resize or retype a table. An imported table only
/// yields a sound bound when its limits pin the size (`max == initial`);
/// otherwise its entry is `None` and the `call_indirect` lowering
/// declines (for that table AND for any later table, whose base offset
/// within the contiguous R11 region is then unknown).
pub table_sizes: Vec<Option<u32>>,
/// #642: per element segment, everything the closed-world `call_indirect`
/// type check needs. `offset` is the const i32 placement of an ACTIVE
/// segment into table `table_index` (`None` = passive/declared/non-const
/// offset — statically unverifiable placement); `funcs` are the segment's
/// function indices in slot order (`None` = an entry was not a plain
/// `ref.func`, e.g. `ref.null` — statically unverifiable contents).
pub elem_segments: Vec<ElemSegmentInfo>,
/// #642: type index per function, indexed by the FULL function index
/// (imports first, then locally-defined ones).
pub func_type_indices: Vec<u32>,
/// #642: canonical structural signature per type index (params/results
/// rendered as a string) — used for the closed-world `call_indirect` type
/// check, which must compare SIGNATURES, not raw type indices (a module
/// may carry structurally-identical duplicate types).
pub type_signatures: Vec<String>,
/// VCR-PERF-002 Phase 1 (#494): proven invariants from loom's `wsc.facts`
/// custom section, keyed by `(function index, value id)` — see
/// `docs/design/wsc-facts-encoding.md` (schema v1) and
/// [`crate::wsc_facts::parse_wsc_facts`]. FAIL-SAFE by contract (loom#231
/// Q4): a missing/unparseable section or unknown version yields the empty
/// vec, unknown fact kinds are skipped — never a decode error. Phase 1 is
/// ingestion only: NO codegen path consumes these yet, so emitted bytes
/// are unchanged whether or not a module carries the section.
pub wsc_facts: Vec<crate::wsc_facts::WscFact>,
}
impl DecodedModule {
/// #642/#650: compute the `call_indirect` guard inputs — per table, the
/// compile-time size for the runtime bounds check, the base byte offset
/// within the contiguous R11 region, and the per-expected-type
/// closed-world verdict that discharges the type check at compile time.
/// See [`CallIndirectGuards`] for the layout contract and soundness
/// argument.
pub fn call_indirect_guards(&self) -> CallIndirectGuards {
let n_types = self.type_signatures.len();
// A segment whose PLACEMENT is not statically attributable
// (passive/declared segment, non-const offset, or a table index the
// module does not declare) poisons EVERY table: `table.init` (itself
// an unsupported op) or a computed offset could land its entries
// anywhere, so no table's image is verifiable.
let global_poison: Option<&'static str> = self
.elem_segments
.iter()
.any(|seg| seg.offset.is_none() || seg.table_index as usize >= self.table_sizes.len())
.then_some(
"element segment is not statically verifiable (passive/declared \
segment, non-const offset, out-of-range table, or non-`ref.func` \
entry)",
);
let mut tables = Vec::with_capacity(self.table_sizes.len());
// Running word offset of the next table's base within the R11 region;
// `None` once a table of unknown size is passed (every later base is
// then not a compile-time constant).
let mut base_words: Option<u32> = Some(0);
for (n, &size) in self.table_sizes.iter().enumerate() {
let base_byte_offset = base_words.and_then(|w| w.checked_mul(4));
let (type_reject, has_null_slots) =
self.table_type_reject(n as u32, size, global_poison, n_types);
tables.push(TableGuards {
table_size: size,
base_byte_offset,
type_reject,
has_null_slots,
});
base_words = match (base_words, size) {
(Some(w), Some(s)) => w.checked_add(s),
_ => None,
};
}
CallIndirectGuards { tables }
}
/// #642/#650: the closed-world type verdicts for ONE table — `None` per
/// expected type when every INITIALIZED slot of table `n` verifiably
/// holds a function of that exact structural signature; `Some(reason)`
/// otherwise. The second component is `has_null_slots` (#664): whether
/// the table image left any slot uninitialized — a `call_indirect`
/// reaching one must TRAP at runtime (null check on the loaded pointer),
/// which the lowering emits only when this is set. Reject paths return
/// `false` (the verdict declines before the flag is consulted).
fn table_type_reject(
&self,
n: u32,
size: Option<u32>,
global_poison: Option<&str>,
n_types: usize,
) -> (Vec<Option<String>>, bool) {
let reject_all = |reason: String| (vec![Some(reason); n_types], false);
if let Some(reason) = global_poison {
return reject_all(reason.to_string());
}
let Some(size) = size else {
return reject_all(format!(
"table {n} has no compile-time-fixed size (imported table with \
growable limits)"
));
};
// Reconstruct the table image: slot -> initializing function index.
let mut slots: Vec<Option<u32>> = vec![None; size as usize];
for seg in self.elem_segments.iter().filter(|s| s.table_index == n) {
let (Some(off), Some(funcs)) = (seg.offset, seg.funcs.as_ref()) else {
// Placement is known (global_poison ruled `offset: None` out),
// so this is an unverifiable CONTENTS case — it poisons only
// the table it targets.
return reject_all(format!(
"element segment targeting table {n} is not statically \
verifiable (non-`ref.func` entry)"
));
};
for (k, &f) in funcs.iter().enumerate() {
let Some(slot) = slots.get_mut(off as usize + k) else {
return reject_all(format!(
"element segment (offset {off}, {} entries) writes past \
table {n}'s declared size {size}",
funcs.len()
));
};
*slot = Some(f);
}
}
// #664: an uninitialized slot is a null funcref — calling it must
// trap (WASM Core §4.4.8). It no longer poisons the closed world
// (pre-#664 it rejected EVERY type): the layout contract requires
// null slots to be linked as ZERO words, so the lowering discharges
// the trap at RUNTIME with a null check on the loaded pointer. The
// type check below therefore covers the INITIALIZED slots only —
// a null slot can never produce a live callee of the wrong type,
// because the null check traps before the branch.
let has_null_slots = slots.iter().any(|s| s.is_none());
let rejects = (0..n_types)
.map(|t| {
for f in slots.iter().flatten() {
let Some(&fty) = self.func_type_indices.get(*f as usize) else {
return Some(format!(
"table {n} entry references function {f} with no known type"
));
};
if self.type_signatures.get(fty as usize) != self.type_signatures.get(t) {
return Some(format!(
"table {n} entry (function {f}, type {fty}) has a different \
signature than expected type {t} — a runtime type check \
is not implementable on the raw code-pointer table"
));
}
}
None
})
.collect();
(rejects, has_null_slots)
}
}
/// Decode a WASM binary and extract functions, memory, and data segments
pub fn decode_wasm_module(wasm_bytes: &[u8]) -> Result<DecodedModule> {
let mut functions = Vec::new();
let mut memories = Vec::new();
let mut data_segments = Vec::new();
let mut globals: Vec<WasmGlobal> = Vec::new();
let mut imports = Vec::new();
let mut func_index = 0u32;
let mut num_imported_funcs = 0u32;
let mut export_names: HashMap<u32, String> = HashMap::new();
// #195: per-type AAPCS arg count (indexed by type index) and per-function
// arg count (indexed by full function index: imports first, then locals).
let mut type_arg_counts: Vec<u32> = Vec::new();
let mut func_arg_counts: Vec<u32> = Vec::new();
let mut type_ret_i64: Vec<bool> = Vec::new();
let mut func_ret_i64: Vec<bool> = Vec::new();
// #359: declared param widths per type / per function (full index).
let mut type_params_i64: Vec<Vec<bool>> = Vec::new();
let mut func_params_i64: Vec<Vec<bool>> = Vec::new();
// #509: (param_count, result_count) per type index, for FuncType blocktypes.
let mut type_block_arity: Vec<(u8, u8)> = Vec::new();
let mut elem_func_indices: Vec<u32> = Vec::new();
// #642/#650: call_indirect guard inputs — per-table fixed sizes (imports
// first, then the table section, in declaration order), per-segment
// static shapes, per-function type index, per-type canonical signature.
let mut table_sizes: Vec<Option<u32>> = Vec::new();
let mut elem_segments: Vec<ElemSegmentInfo> = Vec::new();
let mut func_type_indices: Vec<u32> = Vec::new();
let mut type_signatures: Vec<String> = Vec::new();
// #394 Tier-1.x: function index → developer-facing name from the wasm
// `name` custom section (function-names subsection). Applied to
// `FunctionOps.debug_name` after the parse loop — the custom section
// conventionally trails the code section, so the entries are not yet
// available when each `CodeSectionEntry` is decoded.
let mut name_section_names: HashMap<u32, String> = HashMap::new();
// VCR-PERF-002 Phase 1 (#494): facts from loom's `wsc.facts` custom
// section. `None` until (and unless) the first such section is seen —
// duplicates are ignored (one prover, one section; encoding doc rule).
let mut wsc_facts: Option<Vec<crate::wsc_facts::WscFact>> = None;
// GI-FPU-001 (#369): f32/f64-typed globals in the FULL global index space
// (imports first, then defined). `global.get`/`global.set` decode fine
// (they are type-agnostic ops), but there is no float lowering: the
// f32.const/f64.const initializer is silently dropped (`init_i32: None`
// → slot zeroed), so a read returns 0.0 instead of the init — a silent
// wrong value. Functions touching a float global loud-skip instead.
let mut num_imported_globals = 0u32;
let mut float_globals: std::collections::HashSet<u32> = std::collections::HashSet::new();
for payload in Parser::new(0).parse_all(wasm_bytes) {
let payload = payload.context("Failed to parse WASM payload")?;
match payload {
Payload::TypeSection(reader) => {
// Record the parameter count of each function type so calls can
// marshal the right number of arguments (issue #195).
for rec_group in reader {
let rec_group = rec_group.context("Failed to parse type")?;
for sub_ty in rec_group.types() {
// #509: blocktype arity per type index (saturated u8 —
// >255 params/results is far beyond anything the
// selector supports anyway, and the selector declines
// rather than trusting a saturated count).
type_block_arity.push(match &sub_ty.composite_type.inner {
wasmparser::CompositeInnerType::Func(f) => (
u8::try_from(f.params().len()).unwrap_or(u8::MAX),
u8::try_from(f.results().len()).unwrap_or(u8::MAX),
),
_ => (u8::MAX, u8::MAX),
});
let (count, ret_i64, params_i64) = match &sub_ty.composite_type.inner {
wasmparser::CompositeInnerType::Func(func_ty) => (
func_ty.params().len() as u32,
func_ty
.results()
.first()
.is_some_and(|t| *t == wasmparser::ValType::I64),
// #359: i64/f64 params occupy 8 bytes / a register
// pair under AAPCS. f32/f64 are not in scope for the
// stack-arg path (refused), but mark both 64-bit
// float and i64 so the guard catches them.
func_ty
.params()
.iter()
.map(|t| {
matches!(
t,
wasmparser::ValType::I64 | wasmparser::ValType::F64
)
})
.collect::<Vec<bool>>(),
),
_ => (0, false, Vec::new()),
};
type_arg_counts.push(count);
type_ret_i64.push(ret_i64);
type_params_i64.push(params_i64);
// #642: canonical structural signature for the
// closed-world call_indirect type check (compares
// SIGNATURES so duplicate types stay interchangeable).
type_signatures.push(match &sub_ty.composite_type.inner {
wasmparser::CompositeInnerType::Func(f) => {
format!("{:?}->{:?}", f.params(), f.results())
}
other => format!("non-func:{other:?}"),
});
}
}
}
Payload::ImportSection(reader) => {
// wasmparser 0.221+ groups imports (the "compact imports"
// proposal): the section reader yields `Imports` groups, each of
// which may expand to several `Import`s. `into_imports()`
// flattens groups back to individual `Import`s (preserving the
// module/name/ty fields), keeping the per-import loop intact.
for import in reader.into_imports() {
let import = import.context("Failed to parse import")?;
let (kind, idx) = match import.ty {
wasmparser::TypeRef::Func(type_idx) => {
let idx = num_imported_funcs;
num_imported_funcs += 1;
// Record the imported function's arg count at its
// full function index (imports come first).
func_type_indices.push(type_idx); // #642
func_arg_counts
.push(type_arg_counts.get(type_idx as usize).copied().unwrap_or(0));
func_ret_i64.push(
type_ret_i64
.get(type_idx as usize)
.copied()
.unwrap_or(false),
);
func_params_i64.push(
type_params_i64
.get(type_idx as usize)
.cloned()
.unwrap_or_default(),
);
(ImportKind::Function(type_idx), idx)
}
wasmparser::TypeRef::Memory(_) => (ImportKind::Memory, 0),
wasmparser::TypeRef::Table(t) => {
// #642: an imported table only yields a SOUND
// compile-time bound when its limits pin the size
// exactly (max == initial) — a growable import
// could be larger at runtime, and a bounds guard
// against `initial` would trap spec-valid calls.
// #650: imported tables take the leading table
// indices, in declaration order.
table_sizes.push(match (u32::try_from(t.initial), t.maximum) {
(Ok(init), Some(max)) if u64::from(init) == max => Some(init),
_ => None,
});
(ImportKind::Table, 0)
}
wasmparser::TypeRef::Global(g) => {
// GI-FPU-001 (#369): imported globals come first in
// the global index space — record float-typed ones
// so accesses loud-skip their function.
if matches!(
g.content_type,
wasmparser::ValType::F32 | wasmparser::ValType::F64
) {
float_globals.insert(num_imported_globals);
}
num_imported_globals += 1;
(ImportKind::Global, 0)
}
_ => continue,
};
imports.push(ImportEntry {
module: import.module.to_string(),
name: import.name.to_string(),
kind,
index: idx,
});
}
}
Payload::FunctionSection(reader) => {
// Each entry gives the type index of a locally-defined function,
// in order. Their full function indices follow the imports, so
// appending to `func_arg_counts` keeps it indexed by full index
// (issue #195).
for ty in reader {
let type_idx = ty.context("Failed to parse function type index")?;
func_type_indices.push(type_idx); // #642
func_arg_counts
.push(type_arg_counts.get(type_idx as usize).copied().unwrap_or(0));
func_ret_i64.push(
type_ret_i64
.get(type_idx as usize)
.copied()
.unwrap_or(false),
);
func_params_i64.push(
type_params_i64
.get(type_idx as usize)
.cloned()
.unwrap_or_default(),
);
}
}
Payload::TableSection(reader) => {
// #642: a DEFINED table's compile-time size is exact — its
// initial size is its permanent size, because nothing synth
// compiles can resize it (`table.grow` is an unsupported op
// whose function loud-skips at decode). #650: EVERY table is
// recorded — the contiguous R11 region places table N at
// byte offset `sum(size(0..N)) * 4`.
for table in reader {
let table = table.context("Failed to parse table")?;
table_sizes.push(u32::try_from(table.ty.initial).ok());
}
}
Payload::MemorySection(reader) => {
for (idx, memory) in reader.into_iter().enumerate() {
let mem = memory.context("Failed to parse memory")?;
memories.push(WasmMemory {
index: idx as u32,
initial_pages: mem.initial as u32,
max_pages: mem.maximum.map(|m| m as u32),
shared: mem.shared,
});
}
}
Payload::GlobalSection(reader) => {
// #237/#649: capture each defined global's constant initializer
// + mutability. The init is a const expr; we decode a leading
// `i32.const` (the `$__stack_pointer`/data-layout shape) or
// `i64.const` (#649: capturing only i32 silently ZEROED every
// nonzero i64 init). f32/f64 inits stay `None` on purpose —
// float global access is the GI-FPU-001 (#369) loud-skip lane —
// as do non-const init exprs (`global.get` of an import).
for (idx, global) in reader.into_iter().enumerate() {
let global = global.context("Failed to parse global")?;
let mut ops = global.init_expr.get_operators_reader();
let init = match ops.read() {
Ok(wasmparser::Operator::I32Const { value }) => {
Some(GlobalInit::I32(value))
}
Ok(wasmparser::Operator::I64Const { value }) => {
Some(GlobalInit::I64(value))
}
_ => None,
};
// #643: record the slot width from the DECLARED value type.
// i64/f64 globals occupy 8 bytes (a register pair on the
// 32-bit targets), v128 sixteen; laying every global out at
// `index * 4` silently dropped the high word of every i64.
let slot_bytes = match global.ty.content_type {
wasmparser::ValType::I64 | wasmparser::ValType::F64 => 8,
wasmparser::ValType::V128 => 16,
_ => 4,
};
// GI-FPU-001 (#369): a float-typed global's initializer is
// NOT captured (`init_i32` only decodes `i32.const`), so
// its slot would be silently zeroed — record it so any
// function accessing it loud-skips instead of reading a
// silently-wrong 0.0.
if matches!(
global.ty.content_type,
wasmparser::ValType::F32 | wasmparser::ValType::F64
) {
float_globals.insert(num_imported_globals + idx as u32);
}
globals.push(WasmGlobal {
index: idx as u32,
init,
mutable: global.ty.mutable,
slot_bytes,
});
}
}
Payload::DataSection(reader) => {
for data in reader {
let data = data.context("Failed to parse data segment")?;
if let wasmparser::DataKind::Active {
memory_index: 0,
offset_expr,
} = data.kind
{
let mut ops = offset_expr.get_operators_reader();
if let Ok(wasmparser::Operator::I32Const { value }) = ops.read() {
data_segments.push((value as u32, data.data.to_vec()));
}
}
}
}
Payload::ElementSection(reader) => {
// #275: collect every function index that initializes a table.
// These are the `call_indirect` targets the direct-call closure
// cannot see; `reachable_from_exports` unions them in when a
// reachable function does a `call_indirect`. Both element forms
// are handled: a flat function-index list, and the const-expr
// form whose `ref.func` entries name the functions.
for elem in reader {
let elem = elem.context("Failed to parse element segment")?;
// #642/#650: the segment's static placement — a const i32
// offset of an ACTIVE segment into its target table (any
// table index: the R11 region is contiguous, #650);
// anything else is unverifiable and poisons the
// closed-world type check.
let (seg_table, seg_offset): (u32, Option<u32>) = match &elem.kind {
wasmparser::ElementKind::Active {
table_index,
offset_expr,
} => {
let mut ops = offset_expr.get_operators_reader();
let off = match ops.read() {
Ok(wasmparser::Operator::I32Const { value }) => {
u32::try_from(value).ok()
}
_ => None,
};
(table_index.unwrap_or(0), off)
}
_ => (0, None),
};
let mut seg_funcs: Option<Vec<u32>> = Some(Vec::new());
match elem.items {
wasmparser::ElementItems::Functions(funcs) => {
for f in funcs {
let f = f.context("Failed to parse element func index")?;
elem_func_indices.push(f);
if let Some(v) = seg_funcs.as_mut() {
v.push(f);
}
}
}
wasmparser::ElementItems::Expressions(_, exprs) => {
for expr in exprs {
let expr = expr.context("Failed to parse element expr")?;
// #642: an entry is verifiable only when it is
// a single plain `ref.func` (reader yields the
// op + the implicit `end`). `ref.null` or any
// computed entry poisons the segment.
let mut entry_func: Option<u32> = None;
let mut plain = true;
for (k, op) in expr.get_operators_reader().into_iter().enumerate() {
match (k, op.context("Failed to parse element op")?) {
(0, wasmparser::Operator::RefFunc { function_index }) => {
elem_func_indices.push(function_index);
entry_func = Some(function_index);
}
(_, wasmparser::Operator::End) => {}
(_, wasmparser::Operator::RefFunc { function_index }) => {
// Keep the pre-#642 reachability
// behaviour: every ref.func seen
// anywhere is a possible target.
elem_func_indices.push(function_index);
plain = false;
}
_ => plain = false,
}
}
match (plain, entry_func, seg_funcs.as_mut()) {
(true, Some(f), Some(v)) => v.push(f),
_ => seg_funcs = None,
}
}
}
}
elem_segments.push(ElemSegmentInfo {
table_index: seg_table,
offset: seg_offset,
funcs: seg_funcs,
});
}
}
Payload::ExportSection(exports) => {
for export in exports {
let export = export.context("Failed to parse export")?;
if export.kind == ExternalKind::Func {
export_names.insert(export.index, export.name.to_string());
}
}
}
Payload::CodeSectionEntry(body) => {
let (ops, op_offsets, block_arity, unsupported) =
decode_function_body(&body, &type_block_arity, &float_globals)?;
let actual_index = num_imported_funcs + func_index;
let export_name = export_names.get(&actual_index).cloned();
functions.push(FunctionOps {
index: actual_index,
export_name,
debug_name: None, // filled from the `name` section after the loop
ops,
op_offsets,
unsupported,
block_arity,
});
func_index += 1;
}
Payload::CustomSection(c) => {
// #394 Tier-1.x: the wasm `name` custom section.
if let wasmparser::KnownCustom::Name(reader) = c.as_known() {
parse_name_section_func_names(reader, &mut name_section_names);
}
// VCR-PERF-002 Phase 1 (#494): loom's `wsc.facts` section.
// `parse_wsc_facts` is TOTAL (fail-safe skew, loom#231 Q4):
// any malformed payload decodes to the empty fact list WITH a
// stderr diagnostic, never an error — facts are optional
// accelerators and must not be able to change a compilation
// outcome. First section wins.
if c.name() == crate::wsc_facts::WSC_FACTS_SECTION_NAME && wsc_facts.is_none() {
let parsed = crate::wsc_facts::parse_wsc_facts(c.data());
if let Some(reason) = &parsed.section_ignored {
eprintln!(
"warning: ignoring unparseable `wsc.facts` custom section \
({reason}) — facts are optional accelerators, compilation \
is unaffected (#494 fail-safe skew rule)"
);
} else if parsed.records_skipped > 0 {
eprintln!(
"warning: skipped {} unknown/undecodable `wsc.facts` \
record(s) (likely a newer loom emitter); {} known fact(s) \
kept, compilation is unaffected (#494 fail-safe skew rule)",
parsed.records_skipped,
parsed.facts.len()
);
}
wsc_facts = Some(parsed.facts);
}
}
_ => {}
}
}
apply_name_section(&mut functions, &name_section_names);
Ok(DecodedModule {
functions,
memories,
data_segments,
imports,
num_imported_funcs,
func_arg_counts,
type_arg_counts,
func_ret_i64,
type_ret_i64,
func_params_i64,
globals,
elem_func_indices,
table_size: table_sizes.first().copied().flatten(),
table_sizes,
elem_segments,
func_type_indices,
type_signatures,
wsc_facts: wsc_facts.unwrap_or_default(),
})
}
/// Parse the function-names subsection of a wasm `name` custom section into
/// `out` (function index → developer-facing name, e.g.
/// `core::panicking::panic_fmt::h...`). Best-effort by design: the section is
/// DEBUG METADATA only, so a malformed entry is skipped rather than failing the
/// compile — no codegen path depends on it (#394 Tier-1.x).
fn parse_name_section_func_names(
reader: wasmparser::NameSectionReader<'_>,
out: &mut HashMap<u32, String>,
) {
for subsection in reader.into_iter().flatten() {
if let wasmparser::Name::Function(map) = subsection {
for naming in map.into_iter().flatten() {
out.insert(naming.index, naming.name.to_string());
}
}
}
}
/// Fill each function's `debug_name` from the `name`-section map (keyed by the
/// FULL function index, imports first — the same index space `FunctionOps.index`
/// uses). A function without an entry keeps `None` (⇒ `func_N` downstream).
fn apply_name_section(functions: &mut [FunctionOps], names: &HashMap<u32, String>) {
if names.is_empty() {
return;
}
for f in functions {
f.debug_name = names.get(&f.index).cloned();
}
}
/// Decode a WASM binary and extract all function bodies as WasmOp sequences
pub fn decode_wasm_functions(wasm_bytes: &[u8]) -> Result<Vec<FunctionOps>> {
let mut functions = Vec::new();
let mut func_index = 0u32;
let mut num_imported_funcs = 0u32;
let mut export_names: HashMap<u32, String> = HashMap::new();
let mut name_section_names: HashMap<u32, String> = HashMap::new();
// #509: (param_count, result_count) per type index, for FuncType blocktypes.
let mut type_block_arity: Vec<(u8, u8)> = Vec::new();
// GI-FPU-001 (#369): float-typed globals (full index space, imports first)
// whose accesses must loud-skip — see `decode_wasm_module`.
let mut num_imported_globals = 0u32;
let mut float_globals: std::collections::HashSet<u32> = std::collections::HashSet::new();
for payload in Parser::new(0).parse_all(wasm_bytes) {
let payload = payload.context("Failed to parse WASM payload")?;
match payload {
Payload::TypeSection(reader) => {
// #509: the blocktype-arity side-table needs the type section
// for `BlockType::FuncType(i)` lookups (the wasm binary format
// places types before code, so the table is complete before any
// `CodeSectionEntry` is decoded).
for rec_group in reader {
let rec_group = rec_group.context("Failed to parse type")?;
for sub_ty in rec_group.types() {
type_block_arity.push(match &sub_ty.composite_type.inner {
wasmparser::CompositeInnerType::Func(f) => (
u8::try_from(f.params().len()).unwrap_or(u8::MAX),
u8::try_from(f.results().len()).unwrap_or(u8::MAX),
),
_ => (u8::MAX, u8::MAX),
});
}
}
}
Payload::ImportSection(imports) => {
// wasmparser 0.221+ compact-imports grouping — flatten groups
// to individual imports (see the ImportSection handler above).
for import in imports.into_imports() {
let import = import.context("Failed to parse import")?;
match import.ty {
wasmparser::TypeRef::Func(_) => num_imported_funcs += 1,
wasmparser::TypeRef::Global(g) => {
// GI-FPU-001 (#369): see `decode_wasm_module` —
// float-typed global accesses must loud-skip.
if matches!(
g.content_type,
wasmparser::ValType::F32 | wasmparser::ValType::F64
) {
float_globals.insert(num_imported_globals);
}
num_imported_globals += 1;
}
_ => {}
}
}
}
Payload::GlobalSection(reader) => {
// GI-FPU-001 (#369): record f32/f64-typed defined globals so
// `decode_function_body` flags accesses (their initializer is
// dropped on this path too — same silent-zero hazard).
for (idx, global) in reader.into_iter().enumerate() {
let global = global.context("Failed to parse global")?;
if matches!(
global.ty.content_type,
wasmparser::ValType::F32 | wasmparser::ValType::F64
) {
float_globals.insert(num_imported_globals + idx as u32);
}
}
}
Payload::ExportSection(exports) => {
for export in exports {
let export = export.context("Failed to parse export")?;
if export.kind == ExternalKind::Func {
export_names.insert(export.index, export.name.to_string());
}
}
}
Payload::CodeSectionEntry(body) => {
let (ops, op_offsets, block_arity, unsupported) =
decode_function_body(&body, &type_block_arity, &float_globals)?;
let actual_index = num_imported_funcs + func_index;
let export_name = export_names.get(&actual_index).cloned();
functions.push(FunctionOps {
index: actual_index,
export_name,
debug_name: None, // filled from the `name` section after the loop
ops,
op_offsets,
unsupported,
block_arity,
});
func_index += 1;
}
Payload::CustomSection(c) => {
// #394 Tier-1.x: the wasm `name` custom section.
if let wasmparser::KnownCustom::Name(reader) = c.as_known() {
parse_name_section_func_names(reader, &mut name_section_names);
}
}
_ => {}
}
}
apply_name_section(&mut functions, &name_section_names);
Ok(functions)
}
/// Decoded function with its WasmOp sequence
#[derive(Debug, Clone)]
pub struct FunctionOps {
/// Function index in the module (includes imported functions)
pub index: u32,
/// Export name if this function is exported
pub export_name: Option<String>,
/// #394 Tier-1.x: the function's developer-facing name from the wasm `name`
/// custom section (function-names subsection), e.g.
/// `core::panicking::panic_fmt::h6651313c3e2c6c2f` — present for INTERNAL
/// (non-exported) functions too, unlike `export_name`. DEBUG METADATA only:
/// consumed by the `--debug-line` `DW_TAG_subprogram` emit (name priority:
/// name-section > export name > `func_N`); no codegen or symbol-table path
/// reads it, so emitted `.text`/`.symtab` are unchanged (frozen-safe).
/// `None` when the module has no `name` section or no entry for this index.
pub debug_name: Option<String>,
/// The WASM operations in this function body
pub ops: Vec<WasmOp>,
/// VCR-DBG-001 step 1 (#394): module-relative wasm byte offset of each op in
/// `ops` (same index → same op). This is the address space DWARF-for-wasm
/// `.debug_line` keys on, so it is the bridge from synth's op-index
/// `source_line` to the input wasm's DWARF (wasm-offset → source). PURELY
/// ADDITIVE metadata: no codegen path reads it, so emitted `.text` is
/// unchanged and the frozen fixtures stay bit-identical. Empty until consumed
/// by the DWARF emitter (Tier 1).
pub op_offsets: Vec<u32>,
/// `Some(reason)` when the body contained a value-affecting operator the
/// decoder cannot lower (e.g. scalar f32/f64 — #369, bulk-memory
/// memory.copy/fill). Such an op would otherwise be silently *dropped*
/// (`convert_operator` → `None`), leaving the operand stack wrong and the
/// function a silent miscompile. The compile path LOUD-SKIPS a flagged
/// function (diagnostic + symbol absent → link error names it) instead —
/// the #180/#185 "unsupported op must Err, never silently continue"
/// contract. `None` once every op decoded or was intentionally ignorable
/// (Nop).
pub unsupported: Option<String>,
/// #509: blocktype arity side-table — `(param_count, result_count)` of the
/// k-th `Block`/`Loop`/`If` op in `ops`, in order of appearance.
/// ORDINAL-keyed, not op-index-keyed, on purpose: the backend may rewrite
/// the op stream before selection (e.g. the #539 `i32.const 0; memory.grow`
/// → `memory.size` fold), which shifts op indices but never adds/removes
/// control ops, so the ordinal stays aligned. `BlockType::Empty → (0,0)`,
/// `ValType → (0,1)`, `FuncType(i) →` counts from the type section
/// (saturated to u8; an unresolvable type index records `(u8::MAX,
/// u8::MAX)` so the selector declines loudly instead of miscompiling).
/// This is what lets the direct selector land a value carried by
/// `br`/`br_if`/`br_table` in the target block's designated result
/// register instead of dropping it — `WasmOp::Block/Loop/If` stay bare
/// unit variants (zero ripple through the backends' match sites), and an
/// empty table (hand-built op streams in unit tests) keeps the legacy
/// void-block lowering.
pub block_arity: Vec<(u8, u8)>,
}
/// #509: `(param_count, result_count)` of a wasm blocktype, for the
/// [`FunctionOps::block_arity`] side-table. `type_block_arity` is the type
/// section's per-type-index counts (needed for the `FuncType` form); a missing
/// entry saturates to `(u8::MAX, u8::MAX)` so downstream declines loudly.
fn blocktype_arity(bt: &wasmparser::BlockType, type_block_arity: &[(u8, u8)]) -> (u8, u8) {
match bt {
wasmparser::BlockType::Empty => (0, 0),
wasmparser::BlockType::Type(_) => (0, 1),
wasmparser::BlockType::FuncType(i) => type_block_arity
.get(*i as usize)
.copied()
.unwrap_or((u8::MAX, u8::MAX)),
}
}
/// The per-function payload [`decode_function_body`] extracts: `(ops,
/// op_offsets, block_arity, unsupported)` — see the matching
/// [`FunctionOps`] fields for each component's contract.
type DecodedBody = (Vec<WasmOp>, Vec<u32>, Vec<(u8, u8)>, Option<String>);
/// Decode a single function body to WasmOp sequence.
///
/// Returns the ops plus `Some(reason)` if any operator was a value-affecting
/// op the decoder cannot lower (so the function must be loud-skipped, #369 —
/// not silently miscompiled by dropping the op).
fn decode_function_body(
body: &wasmparser::FunctionBody,
type_block_arity: &[(u8, u8)],
float_globals: &std::collections::HashSet<u32>,
) -> Result<DecodedBody> {
let mut ops = Vec::new();
// VCR-DBG-001 step 1: parallel to `ops` — the module-relative wasm byte
// offset of each emitted op (the DWARF-for-wasm address space). Captured via
// the offset-aware reader; pushed only when an op is pushed, so indices stay
// aligned with `ops`. Additive metadata, no codegen consumer ⇒ frozen-safe.
let mut op_offsets = Vec::new();
// #509: ordinal blocktype-arity side-table — one entry per Block/Loop/If in
// `ops` order (see `FunctionOps::block_arity`).
let mut block_arity: Vec<(u8, u8)> = Vec::new();
let mut unsupported: Option<String> = None;
let ops_reader = body.get_operators_reader()?;
for item in ops_reader.into_iter_with_offsets() {
let (op, offset) = item.context("Failed to read operator")?;
if let Some(wasm_op) = convert_operator(&op) {
// #509: capture the blocktype arity BEFORE the enum flattens it away
// (`WasmOp::Block/Loop/If` are unit variants by design).
if let wasmparser::Operator::Block { blockty }
| wasmparser::Operator::Loop { blockty }
| wasmparser::Operator::If { blockty } = &op
{
block_arity.push(blocktype_arity(blockty, type_block_arity));
}
// GI-FPU-001 (#369): `global.get`/`global.set` decode fine (the
// ops are type-agnostic), but an f32/f64-typed global has no
// float lowering — its const initializer is dropped (slot zeroed),
// so a read returns a silently-wrong 0.0. Flag the function for
// the same loud-skip the scalar float ops get.
if unsupported.is_none()
&& let WasmOp::GlobalGet(i) | WasmOp::GlobalSet(i) = &wasm_op
&& float_globals.contains(i)
{
unsupported = Some(format!(
"{wasm_op:?} on an f32/f64-typed global — float globals \
have no lowering, the initializer would be silently \
zeroed (GI-FPU-001)"
));
}
ops.push(wasm_op);
op_offsets.push(offset as u32);
} else if unsupported.is_none() && !is_intentionally_ignored(&op) {
// The op was DROPPED by `convert_operator` (`_ => None`) and is not
// an intentional no-op (Nop) — record it so the
// function is loud-skipped rather than silently miscompiled (#369).
unsupported = Some(format!("{op:?}"));
}
}
Ok((ops, op_offsets, block_arity, unsupported))
}
/// Operators that `convert_operator` returns `None` for *on purpose* — they
/// carry no value-affecting semantics for our backend, so dropping them is
/// correct (NOT a silent miscompile). Everything else that decodes to `None`
/// is an unsupported op that must loud-skip its function (#369).
///
/// #665: `Unreachable` is NOT on this list — it traps (WASM §4.4.5), so it
/// decodes to `WasmOp::Unreachable` and every backend lowers it to a trap
/// instruction (or loud-declines). Only `Nop` is genuinely ignorable.
fn is_intentionally_ignored(op: &wasmparser::Operator) -> bool {
use wasmparser::Operator::*;
matches!(op, Nop)
}
/// Convert a wasmparser Operator to our WasmOp enum
fn convert_operator(op: &wasmparser::Operator) -> Option<WasmOp> {
use wasmparser::Operator::*;
match op {
// Constants
I32Const { value } => Some(WasmOp::I32Const(*value)),
// i32 Arithmetic
I32Add => Some(WasmOp::I32Add),
I32Sub => Some(WasmOp::I32Sub),
I32Mul => Some(WasmOp::I32Mul),
I32DivS => Some(WasmOp::I32DivS),
I32DivU => Some(WasmOp::I32DivU),
I32RemS => Some(WasmOp::I32RemS),
I32RemU => Some(WasmOp::I32RemU),
// i64 Constants
I64Const { value } => Some(WasmOp::I64Const(*value)),
// i64 Arithmetic
I64Add => Some(WasmOp::I64Add),
I64Sub => Some(WasmOp::I64Sub),
I64Mul => Some(WasmOp::I64Mul),
I64DivS => Some(WasmOp::I64DivS),
I64DivU => Some(WasmOp::I64DivU),
I64RemS => Some(WasmOp::I64RemS),
I64RemU => Some(WasmOp::I64RemU),
// i64 Bitwise
I64And => Some(WasmOp::I64And),
I64Or => Some(WasmOp::I64Or),
I64Xor => Some(WasmOp::I64Xor),
I64Shl => Some(WasmOp::I64Shl),
I64ShrS => Some(WasmOp::I64ShrS),
I64ShrU => Some(WasmOp::I64ShrU),
I64Rotl => Some(WasmOp::I64Rotl),
I64Rotr => Some(WasmOp::I64Rotr),
I64Clz => Some(WasmOp::I64Clz),
I64Ctz => Some(WasmOp::I64Ctz),
I64Popcnt => Some(WasmOp::I64Popcnt),
I64Extend8S => Some(WasmOp::I64Extend8S),
I64Extend16S => Some(WasmOp::I64Extend16S),
I64Extend32S => Some(WasmOp::I64Extend32S),
// i32<->i64 width conversions. Previously UNMAPPED → silently dropped,
// which left an i32 value as a 64-bit operand with a garbage high half
// (harmless when a following `i64.shl 32` discards it, but a latent
// miscompile for extend-then-arithmetic, and it breaks width-correct
// register allocation). (#204)
I64ExtendI32U => Some(WasmOp::I64ExtendI32U),
I64ExtendI32S => Some(WasmOp::I64ExtendI32S),
I32WrapI64 => Some(WasmOp::I32WrapI64),
// i64 Comparison
I64Eqz => Some(WasmOp::I64Eqz),
I64Eq => Some(WasmOp::I64Eq),
I64Ne => Some(WasmOp::I64Ne),
I64LtS => Some(WasmOp::I64LtS),
I64LtU => Some(WasmOp::I64LtU),
I64LeS => Some(WasmOp::I64LeS),
I64LeU => Some(WasmOp::I64LeU),
I64GtS => Some(WasmOp::I64GtS),
I64GtU => Some(WasmOp::I64GtU),
I64GeS => Some(WasmOp::I64GeS),
I64GeU => Some(WasmOp::I64GeU),
// Bitwise
I32And => Some(WasmOp::I32And),
I32Or => Some(WasmOp::I32Or),
I32Xor => Some(WasmOp::I32Xor),
I32Shl => Some(WasmOp::I32Shl),
I32ShrS => Some(WasmOp::I32ShrS),
I32ShrU => Some(WasmOp::I32ShrU),
I32Rotl => Some(WasmOp::I32Rotl),
I32Rotr => Some(WasmOp::I32Rotr),
I32Clz => Some(WasmOp::I32Clz),
I32Ctz => Some(WasmOp::I32Ctz),
I32Popcnt => Some(WasmOp::I32Popcnt),
I32Extend8S => Some(WasmOp::I32Extend8S),
I32Extend16S => Some(WasmOp::I32Extend16S),
// Comparison
I32Eqz => Some(WasmOp::I32Eqz),
I32Eq => Some(WasmOp::I32Eq),
I32Ne => Some(WasmOp::I32Ne),
I32LtS => Some(WasmOp::I32LtS),
I32LtU => Some(WasmOp::I32LtU),
I32LeS => Some(WasmOp::I32LeS),
I32LeU => Some(WasmOp::I32LeU),
I32GtS => Some(WasmOp::I32GtS),
I32GtU => Some(WasmOp::I32GtU),
I32GeS => Some(WasmOp::I32GeS),
I32GeU => Some(WasmOp::I32GeU),
// Memory
I32Load { memarg } => Some(WasmOp::I32Load {
offset: memarg.offset as u32,
align: memarg.align as u32,
}),
I32Store { memarg } => Some(WasmOp::I32Store {
offset: memarg.offset as u32,
align: memarg.align as u32,
}),
// #372: full-width i64 load/store. The selector already lowers these to
// a lo/hi i32 register-pair access (`generate_i64_load/store_with_bounds_check`,
// reusing the #171 pair regalloc) — only the decoder arm was missing, so
// `i64.load`/`i64.store` fell through `_ => None` and (since v0.11.46)
// loud-skipped their function. The narrow forms (I64Load8.. / I64Store32)
// were already decoded below.
I64Load { memarg } => Some(WasmOp::I64Load {
offset: memarg.offset as u32,
align: memarg.align as u32,
}),
I64Store { memarg } => Some(WasmOp::I64Store {
offset: memarg.offset as u32,
align: memarg.align as u32,
}),
// Sub-word loads (i32)
I32Load8S { memarg } => Some(WasmOp::I32Load8S {
offset: memarg.offset as u32,
align: memarg.align as u32,
}),
I32Load8U { memarg } => Some(WasmOp::I32Load8U {
offset: memarg.offset as u32,
align: memarg.align as u32,
}),
I32Load16S { memarg } => Some(WasmOp::I32Load16S {
offset: memarg.offset as u32,
align: memarg.align as u32,
}),
I32Load16U { memarg } => Some(WasmOp::I32Load16U {
offset: memarg.offset as u32,
align: memarg.align as u32,
}),
// Sub-word stores (i32)
I32Store8 { memarg } => Some(WasmOp::I32Store8 {
offset: memarg.offset as u32,
align: memarg.align as u32,
}),
I32Store16 { memarg } => Some(WasmOp::I32Store16 {
offset: memarg.offset as u32,
align: memarg.align as u32,
}),
// Local/Global
LocalGet { local_index } => Some(WasmOp::LocalGet(*local_index)),
LocalSet { local_index } => Some(WasmOp::LocalSet(*local_index)),
LocalTee { local_index } => Some(WasmOp::LocalTee(*local_index)),
GlobalGet { global_index } => Some(WasmOp::GlobalGet(*global_index)),
GlobalSet { global_index } => Some(WasmOp::GlobalSet(*global_index)),
// Control flow
Block { .. } => Some(WasmOp::Block),
Loop { .. } => Some(WasmOp::Loop),
Br { relative_depth } => Some(WasmOp::Br(*relative_depth)),
BrIf { relative_depth } => Some(WasmOp::BrIf(*relative_depth)),
// br_table: indexed multi-way branch. Previously UNMAPPED → silently
// dropped, so the selector never emitted the index dispatch and control
// fell straight into the first table arm — every br_table behaved as if
// it always took target 0 (gale's binary-sem WAKE path never fired). The
// jump-table relative depths + default depth are preserved in order.
BrTable { targets } => {
let default = targets.default();
let tgts: Vec<u32> = targets.targets().filter_map(Result::ok).collect();
Some(WasmOp::BrTable {
targets: tgts,
default,
})
}
Return => Some(WasmOp::Return),
Call { function_index } => Some(WasmOp::Call(*function_index)),
CallIndirect {
type_index,
table_index,
..
} => Some(WasmOp::CallIndirect {
type_index: *type_index,
table_index: *table_index,
}),
// End is needed for control flow pattern matching
End => Some(WasmOp::End),
// #665: `unreachable` MUST reach the backends — WASM Core §4.4.5
// requires it to trap unconditionally. It was previously dropped here
// (treated like Nop), so every backend compiled it to a no-op and
// control FELL THROUGH panic!/abort/unreachable-default guards with
// undefined register state. The selector arms (ARM: UDF #0, RV32:
// ebreak) already existed; they just never received the op.
Unreachable => Some(WasmOp::Unreachable),
// Nop - skip (genuinely no semantics)
Nop => None,
// Drop is needed for br_if pattern matching
Drop => Some(WasmOp::Drop),
// Select
Select => Some(WasmOp::Select),
// If/Else - simplified handling
If { .. } => Some(WasmOp::If),
Else => Some(WasmOp::Else),
// i64 sub-word loads
I64Load8S { memarg } => Some(WasmOp::I64Load8S {
offset: memarg.offset as u32,
align: memarg.align as u32,
}),
I64Load8U { memarg } => Some(WasmOp::I64Load8U {
offset: memarg.offset as u32,
align: memarg.align as u32,
}),
I64Load16S { memarg } => Some(WasmOp::I64Load16S {
offset: memarg.offset as u32,
align: memarg.align as u32,
}),
I64Load16U { memarg } => Some(WasmOp::I64Load16U {
offset: memarg.offset as u32,
align: memarg.align as u32,
}),
I64Load32S { memarg } => Some(WasmOp::I64Load32S {
offset: memarg.offset as u32,
align: memarg.align as u32,
}),
I64Load32U { memarg } => Some(WasmOp::I64Load32U {
offset: memarg.offset as u32,
align: memarg.align as u32,
}),
// i64 sub-word stores
I64Store8 { memarg } => Some(WasmOp::I64Store8 {
offset: memarg.offset as u32,
align: memarg.align as u32,
}),
I64Store16 { memarg } => Some(WasmOp::I64Store16 {
offset: memarg.offset as u32,
align: memarg.align as u32,
}),
I64Store32 { memarg } => Some(WasmOp::I64Store32 {
offset: memarg.offset as u32,
align: memarg.align as u32,
}),
// Memory management
MemorySize { mem, .. } => Some(WasmOp::MemorySize(*mem)),
MemoryGrow { mem, .. } => Some(WasmOp::MemoryGrow(*mem)),
// Bulk memory (#374). The backend supports a single linear memory
// (memory 0); any non-zero memory index falls through to `_ => None` and
// loud-skips the function (GI-FPU-001 honesty contract) rather than
// miscompiling a multi-memory copy. memory.copy reads dst/src memories;
// memory.fill one. The selector lowers these to a bounds-checked byte
// loop (see select_with_stack).
MemoryCopy {
dst_mem: 0,
src_mem: 0,
} => Some(WasmOp::MemoryCopy),
MemoryFill { mem: 0 } => Some(WasmOp::MemoryFill),
// ========================================================================
// v128 SIMD operations (WASM SIMD proposal, 0xFD prefix)
// ========================================================================
V128Const { value } => {
let mut bytes = [0u8; 16];
bytes.copy_from_slice(value.bytes());
Some(WasmOp::V128Const(bytes))
}
V128Load { memarg } => Some(WasmOp::V128Load {
offset: memarg.offset as u32,
align: memarg.align as u32,
}),
V128Store { memarg } => Some(WasmOp::V128Store {
offset: memarg.offset as u32,
align: memarg.align as u32,
}),
// v128 bitwise
V128And => Some(WasmOp::V128And),
V128Or => Some(WasmOp::V128Or),
V128Xor => Some(WasmOp::V128Xor),
V128Not => Some(WasmOp::V128Not),
V128AndNot => Some(WasmOp::V128AndNot),
// i8x16
I8x16Add => Some(WasmOp::I8x16Add),
I8x16Sub => Some(WasmOp::I8x16Sub),
I8x16Neg => Some(WasmOp::I8x16Neg),
I8x16Eq => Some(WasmOp::I8x16Eq),
I8x16Ne => Some(WasmOp::I8x16Ne),
I8x16LtS => Some(WasmOp::I8x16LtS),
I8x16LtU => Some(WasmOp::I8x16LtU),
I8x16GtS => Some(WasmOp::I8x16GtS),
I8x16GtU => Some(WasmOp::I8x16GtU),
I8x16LeS => Some(WasmOp::I8x16LeS),
I8x16LeU => Some(WasmOp::I8x16LeU),
I8x16GeS => Some(WasmOp::I8x16GeS),
I8x16GeU => Some(WasmOp::I8x16GeU),
I8x16Splat => Some(WasmOp::I8x16Splat),
I8x16ExtractLaneS { lane } => Some(WasmOp::I8x16ExtractLaneS(*lane)),
I8x16ExtractLaneU { lane } => Some(WasmOp::I8x16ExtractLaneU(*lane)),
I8x16ReplaceLane { lane } => Some(WasmOp::I8x16ReplaceLane(*lane)),
I8x16Shuffle { lanes } => Some(WasmOp::I8x16Shuffle(*lanes)),
I8x16Swizzle => Some(WasmOp::I8x16Swizzle),
// i16x8
I16x8Add => Some(WasmOp::I16x8Add),
I16x8Sub => Some(WasmOp::I16x8Sub),
I16x8Mul => Some(WasmOp::I16x8Mul),
I16x8Neg => Some(WasmOp::I16x8Neg),
I16x8Eq => Some(WasmOp::I16x8Eq),
I16x8Ne => Some(WasmOp::I16x8Ne),
I16x8LtS => Some(WasmOp::I16x8LtS),
I16x8LtU => Some(WasmOp::I16x8LtU),
I16x8GtS => Some(WasmOp::I16x8GtS),
I16x8GtU => Some(WasmOp::I16x8GtU),
I16x8LeS => Some(WasmOp::I16x8LeS),
I16x8LeU => Some(WasmOp::I16x8LeU),
I16x8GeS => Some(WasmOp::I16x8GeS),
I16x8GeU => Some(WasmOp::I16x8GeU),
I16x8Splat => Some(WasmOp::I16x8Splat),
I16x8ExtractLaneS { lane } => Some(WasmOp::I16x8ExtractLaneS(*lane)),
I16x8ExtractLaneU { lane } => Some(WasmOp::I16x8ExtractLaneU(*lane)),
I16x8ReplaceLane { lane } => Some(WasmOp::I16x8ReplaceLane(*lane)),
// i32x4
I32x4Add => Some(WasmOp::I32x4Add),
I32x4Sub => Some(WasmOp::I32x4Sub),
I32x4Mul => Some(WasmOp::I32x4Mul),
I32x4Neg => Some(WasmOp::I32x4Neg),
I32x4Eq => Some(WasmOp::I32x4Eq),
I32x4Ne => Some(WasmOp::I32x4Ne),
I32x4LtS => Some(WasmOp::I32x4LtS),
I32x4LtU => Some(WasmOp::I32x4LtU),
I32x4GtS => Some(WasmOp::I32x4GtS),
I32x4GtU => Some(WasmOp::I32x4GtU),
I32x4LeS => Some(WasmOp::I32x4LeS),
I32x4LeU => Some(WasmOp::I32x4LeU),
I32x4GeS => Some(WasmOp::I32x4GeS),
I32x4GeU => Some(WasmOp::I32x4GeU),
I32x4Splat => Some(WasmOp::I32x4Splat),
I32x4ExtractLane { lane } => Some(WasmOp::I32x4ExtractLane(*lane)),
I32x4ReplaceLane { lane } => Some(WasmOp::I32x4ReplaceLane(*lane)),
// i64x2
I64x2Add => Some(WasmOp::I64x2Add),
I64x2Sub => Some(WasmOp::I64x2Sub),
I64x2Mul => Some(WasmOp::I64x2Mul),
I64x2Neg => Some(WasmOp::I64x2Neg),
I64x2Eq => Some(WasmOp::I64x2Eq),
I64x2Ne => Some(WasmOp::I64x2Ne),
I64x2LtS => Some(WasmOp::I64x2LtS),
I64x2GtS => Some(WasmOp::I64x2GtS),
I64x2LeS => Some(WasmOp::I64x2LeS),
I64x2GeS => Some(WasmOp::I64x2GeS),
I64x2Splat => Some(WasmOp::I64x2Splat),
I64x2ExtractLane { lane } => Some(WasmOp::I64x2ExtractLane(*lane)),
I64x2ReplaceLane { lane } => Some(WasmOp::I64x2ReplaceLane(*lane)),
// f32x4
F32x4Add => Some(WasmOp::F32x4Add),
F32x4Sub => Some(WasmOp::F32x4Sub),
F32x4Mul => Some(WasmOp::F32x4Mul),
F32x4Div => Some(WasmOp::F32x4Div),
F32x4Abs => Some(WasmOp::F32x4Abs),
F32x4Neg => Some(WasmOp::F32x4Neg),
F32x4Sqrt => Some(WasmOp::F32x4Sqrt),
F32x4Eq => Some(WasmOp::F32x4Eq),
F32x4Ne => Some(WasmOp::F32x4Ne),
F32x4Lt => Some(WasmOp::F32x4Lt),
F32x4Le => Some(WasmOp::F32x4Le),
F32x4Gt => Some(WasmOp::F32x4Gt),
F32x4Ge => Some(WasmOp::F32x4Ge),
F32x4Splat => Some(WasmOp::F32x4Splat),
F32x4ExtractLane { lane } => Some(WasmOp::F32x4ExtractLane(*lane)),
F32x4ReplaceLane { lane } => Some(WasmOp::F32x4ReplaceLane(*lane)),
// Other operators not yet supported
_ => None,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_decode_simple_add() {
let wat = r#"
(module
(func (export "add") (param i32 i32) (result i32)
local.get 0
local.get 1
i32.add
)
)
"#;
let wasm = wat::parse_str(wat).expect("Failed to parse WAT");
let functions = decode_wasm_functions(&wasm).expect("Failed to decode");
assert_eq!(functions.len(), 1);
assert_eq!(functions[0].index, 0);
assert_eq!(functions[0].export_name, Some("add".to_string()));
assert_eq!(
functions[0].ops,
vec![
WasmOp::LocalGet(0),
WasmOp::LocalGet(1),
WasmOp::I32Add,
WasmOp::End
]
);
}
/// #204 regression: `i64.extend_i32_u`, `i64.extend_i32_s` and
/// `i32.wrap_i64` must DECODE (they were previously unmapped → silently
/// dropped by `convert_operator`, leaving an i32 value as a 64-bit operand
/// with a garbage high half — the root cause of gale's miscompiled
/// `(new_count << 32)` pack). The decoder must surface all three.
#[test]
fn test_decode_i64_i32_width_conversions() {
let wat = r#"
(module
(func (export "conv") (param i32 i64) (result i32)
local.get 0
i64.extend_i32_u
local.get 0
i64.extend_i32_s
i64.add
local.get 1
i64.add
i32.wrap_i64
)
)
"#;
let wasm = wat::parse_str(wat).expect("parse");
let functions = decode_wasm_functions(&wasm).expect("decode");
let ops = &functions[0].ops;
assert!(
ops.contains(&WasmOp::I64ExtendI32U),
"i64.extend_i32_u must decode (not be dropped): {ops:?}"
);
assert!(
ops.contains(&WasmOp::I64ExtendI32S),
"i64.extend_i32_s must decode (not be dropped): {ops:?}"
);
assert!(
ops.contains(&WasmOp::I32WrapI64),
"i32.wrap_i64 must decode (not be dropped): {ops:?}"
);
}
/// #204 WAKE-path regression: `br_table` must DECODE (it was unmapped in
/// `convert_operator` → silently dropped, so the selector emitted no index
/// dispatch and every `br_table` fell through to target 0 — gale's binary
/// semaphore never took its WAKE branch). Targets + default are preserved.
#[test]
fn test_decode_br_table() {
let wat = r#"
(module
(func (export "bt") (param i32) (result i32)
(block (block (block
local.get 0
br_table 2 0 1 2)
i32.const 30 return)
i32.const 20 return)
i32.const 10))
"#;
let wasm = wat::parse_str(wat).expect("parse");
let functions = decode_wasm_functions(&wasm).expect("decode");
let bt = functions[0]
.ops
.iter()
.find_map(|o| match o {
WasmOp::BrTable { targets, default } => Some((targets.clone(), *default)),
_ => None,
})
.expect("br_table must decode (not be dropped)");
assert_eq!(bt.0, vec![2, 0, 1], "br_table targets preserved in order");
assert_eq!(bt.1, 2, "br_table default preserved");
}
#[test]
fn test_decode_arithmetic() {
let wat = r#"
(module
(func (export "calc") (result i32)
i32.const 5
i32.const 3
i32.mul
i32.const 2
i32.add
)
)
"#;
let wasm = wat::parse_str(wat).expect("Failed to parse WAT");
let functions = decode_wasm_functions(&wasm).expect("Failed to decode");
assert_eq!(functions.len(), 1);
assert_eq!(functions[0].export_name, Some("calc".to_string()));
assert_eq!(
functions[0].ops,
vec![
WasmOp::I32Const(5),
WasmOp::I32Const(3),
WasmOp::I32Mul,
WasmOp::I32Const(2),
WasmOp::I32Add,
WasmOp::End,
]
);
}
#[test]
fn test_decode_multi_function_module() {
let wat = r#"
(module
(func $helper)
(func (export "add") (param i32 i32) (result i32)
local.get 0
local.get 1
i32.add
)
(func (export "sub") (param i32 i32) (result i32)
local.get 0
local.get 1
i32.sub
)
)
"#;
let wasm = wat::parse_str(wat).expect("Failed to parse WAT");
let functions = decode_wasm_functions(&wasm).expect("Failed to decode");
assert_eq!(functions.len(), 3);
assert_eq!(functions[0].index, 0);
assert_eq!(functions[0].export_name, None);
assert_eq!(functions[1].index, 1);
assert_eq!(functions[1].export_name, Some("add".to_string()));
assert_eq!(functions[2].index, 2);
assert_eq!(functions[2].export_name, Some("sub".to_string()));
}
#[test]
fn test_decode_module_with_imports() {
let wat = r#"
(module
(import "env" "log" (func $log (param i32)))
(import "env" "memory" (memory 1))
(func (export "run") (param i32)
local.get 0
call 0
)
)
"#;
let wasm = wat::parse_str(wat).expect("Failed to parse WAT");
let module = decode_wasm_module(&wasm).expect("Failed to decode");
// Should have 2 imports (1 func, 1 memory)
assert_eq!(module.imports.len(), 2);
assert_eq!(module.num_imported_funcs, 1);
// First import is the function
assert_eq!(module.imports[0].module, "env");
assert_eq!(module.imports[0].name, "log");
assert!(matches!(module.imports[0].kind, ImportKind::Function(_)));
// Second import is memory
assert_eq!(module.imports[1].module, "env");
assert_eq!(module.imports[1].name, "memory");
assert_eq!(module.imports[1].kind, ImportKind::Memory);
// Should have 1 local function (index 1, because import is index 0)
assert_eq!(module.functions.len(), 1);
assert_eq!(module.functions[0].index, 1);
assert_eq!(module.functions[0].export_name, Some("run".to_string()));
}
#[test]
fn test_find_function_by_export_name() {
let wat = r#"
(module
(func $helper)
(func (export "add") (param i32 i32) (result i32)
local.get 0
local.get 1
i32.add
)
)
"#;
let wasm = wat::parse_str(wat).expect("Failed to parse WAT");
let functions = decode_wasm_functions(&wasm).expect("Failed to decode");
let add_func = functions
.iter()
.find(|f| f.export_name.as_deref() == Some("add"))
.expect("Should find 'add' function");
assert_eq!(add_func.index, 1);
assert!(add_func.ops.contains(&WasmOp::I32Add));
}
#[test]
fn test_decode_subword_loads() {
let wat = r#"
(module
(memory 1)
(func (export "test") (param i32) (result i32)
local.get 0
i32.load8_u
)
)
"#;
let wasm = wat::parse_str(wat).expect("Failed to parse WAT");
let functions = decode_wasm_functions(&wasm).expect("Failed to decode");
assert_eq!(functions.len(), 1);
assert!(functions[0].ops.contains(&WasmOp::I32Load8U {
offset: 0,
align: 0,
}));
}
#[test]
fn test_decode_subword_stores() {
let wat = r#"
(module
(memory 1)
(func (export "test") (param i32 i32)
local.get 0
local.get 1
i32.store8
)
)
"#;
let wasm = wat::parse_str(wat).expect("Failed to parse WAT");
let functions = decode_wasm_functions(&wasm).expect("Failed to decode");
assert_eq!(functions.len(), 1);
assert!(functions[0].ops.contains(&WasmOp::I32Store8 {
offset: 0,
align: 0,
}));
}
#[test]
fn test_decode_memory_size_grow() {
let wat = r#"
(module
(memory 1)
(func (export "test") (result i32)
memory.size
)
)
"#;
let wasm = wat::parse_str(wat).expect("Failed to parse WAT");
let functions = decode_wasm_functions(&wasm).expect("Failed to decode");
assert_eq!(functions.len(), 1);
assert!(functions[0].ops.contains(&WasmOp::MemorySize(0)));
}
#[test]
fn test_decode_memory_grow() {
let wat = r#"
(module
(memory 1)
(func (export "test") (param i32) (result i32)
local.get 0
memory.grow
)
)
"#;
let wasm = wat::parse_str(wat).expect("Failed to parse WAT");
let functions = decode_wasm_functions(&wasm).expect("Failed to decode");
assert_eq!(functions.len(), 1);
assert!(functions[0].ops.contains(&WasmOp::MemoryGrow(0)));
}
#[test]
fn test_decode_bulk_memory_374() {
// #374: memory.copy / memory.fill on the single linear memory decode to
// the new WasmOp variants (was `_ => None` -> loud-skip).
let wat = r#"
(module
(memory 1)
(func (export "cpy") (param i32 i32 i32)
local.get 0 local.get 1 local.get 2 memory.copy)
(func (export "fil") (param i32 i32 i32)
local.get 0 local.get 1 local.get 2 memory.fill)
)
"#;
let wasm = wat::parse_str(wat).expect("Failed to parse WAT");
let functions = decode_wasm_functions(&wasm).expect("Failed to decode");
assert_eq!(functions.len(), 2);
assert!(functions[0].ops.contains(&WasmOp::MemoryCopy));
assert!(functions[1].ops.contains(&WasmOp::MemoryFill));
// Neither function is flagged unsupported (they now lower).
assert!(functions[0].unsupported.is_none());
assert!(functions[1].unsupported.is_none());
}
#[test]
fn test_decode_i64_subword_loads() {
let wat = r#"
(module
(memory 1)
(func (export "test") (param i32) (result i64)
local.get 0
i64.load8_s
)
)
"#;
let wasm = wat::parse_str(wat).expect("Failed to parse WAT");
let functions = decode_wasm_functions(&wasm).expect("Failed to decode");
assert_eq!(functions.len(), 1);
assert!(functions[0].ops.contains(&WasmOp::I64Load8S {
offset: 0,
align: 0,
}));
}
#[test]
fn test_decode_all_subword_memory_ops() {
// Test that all sub-word operations are decoded from WAT
let wat = r#"
(module
(memory 1)
(func (export "test") (param i32)
;; i32 sub-word loads
local.get 0
i32.load8_s
drop
local.get 0
i32.load8_u
drop
local.get 0
i32.load16_s
drop
local.get 0
i32.load16_u
drop
;; i32 sub-word stores
local.get 0
i32.const 42
i32.store8
local.get 0
i32.const 42
i32.store16
;; i64 sub-word loads
local.get 0
i64.load8_s
drop
local.get 0
i64.load8_u
drop
local.get 0
i64.load16_s
drop
local.get 0
i64.load16_u
drop
local.get 0
i64.load32_s
drop
local.get 0
i64.load32_u
drop
;; i64 sub-word stores
local.get 0
i64.const 42
i64.store8
local.get 0
i64.const 42
i64.store16
local.get 0
i64.const 42
i64.store32
)
)
"#;
let wasm = wat::parse_str(wat).expect("Failed to parse WAT");
let functions = decode_wasm_functions(&wasm).expect("Failed to decode");
assert_eq!(functions.len(), 1);
let ops = &functions[0].ops;
// Verify i32 sub-word ops are present
assert!(ops.iter().any(|o| matches!(o, WasmOp::I32Load8S { .. })));
assert!(ops.iter().any(|o| matches!(o, WasmOp::I32Load8U { .. })));
assert!(ops.iter().any(|o| matches!(o, WasmOp::I32Load16S { .. })));
assert!(ops.iter().any(|o| matches!(o, WasmOp::I32Load16U { .. })));
assert!(ops.iter().any(|o| matches!(o, WasmOp::I32Store8 { .. })));
assert!(ops.iter().any(|o| matches!(o, WasmOp::I32Store16 { .. })));
// Verify i64 sub-word ops are present
assert!(ops.iter().any(|o| matches!(o, WasmOp::I64Load8S { .. })));
assert!(ops.iter().any(|o| matches!(o, WasmOp::I64Load8U { .. })));
assert!(ops.iter().any(|o| matches!(o, WasmOp::I64Load16S { .. })));
assert!(ops.iter().any(|o| matches!(o, WasmOp::I64Load16U { .. })));
assert!(ops.iter().any(|o| matches!(o, WasmOp::I64Load32S { .. })));
assert!(ops.iter().any(|o| matches!(o, WasmOp::I64Load32U { .. })));
assert!(ops.iter().any(|o| matches!(o, WasmOp::I64Store8 { .. })));
assert!(ops.iter().any(|o| matches!(o, WasmOp::I64Store16 { .. })));
assert!(ops.iter().any(|o| matches!(o, WasmOp::I64Store32 { .. })));
}
#[test]
fn test_decode_simd_i32x4_add() {
let wat = r#"
(module
(func (export "add_v128") (param v128 v128) (result v128)
local.get 0
local.get 1
i32x4.add
)
)
"#;
let wasm = wat::parse_str(wat).expect("Failed to parse WAT with SIMD");
let functions = decode_wasm_functions(&wasm).expect("Failed to decode");
assert_eq!(functions.len(), 1);
assert!(
functions[0].ops.contains(&WasmOp::I32x4Add),
"Should decode i32x4.add: {:?}",
functions[0].ops
);
}
#[test]
fn test_decode_simd_v128_const() {
let wat = r#"
(module
(func (export "const_v128") (result v128)
v128.const i32x4 1 2 3 4
)
)
"#;
let wasm = wat::parse_str(wat).expect("Failed to parse WAT with SIMD");
let functions = decode_wasm_functions(&wasm).expect("Failed to decode");
assert_eq!(functions.len(), 1);
assert!(
functions[0]
.ops
.iter()
.any(|o| matches!(o, WasmOp::V128Const(_))),
"Should decode v128.const: {:?}",
functions[0].ops
);
}
#[test]
fn test_decode_simd_v128_load_store() {
let wat = r#"
(module
(memory 1)
(func (export "load_store") (param i32)
local.get 0
v128.load
local.get 0
v128.store
)
)
"#;
let wasm = wat::parse_str(wat).expect("Failed to parse WAT with SIMD");
let functions = decode_wasm_functions(&wasm).expect("Failed to decode");
assert_eq!(functions.len(), 1);
let ops = &functions[0].ops;
assert!(
ops.iter().any(|o| matches!(o, WasmOp::V128Load { .. })),
"Should decode v128.load"
);
assert!(
ops.iter().any(|o| matches!(o, WasmOp::V128Store { .. })),
"Should decode v128.store"
);
}
#[test]
fn test_decode_simd_bitwise_ops() {
let wat = r#"
(module
(func (export "bitwise") (param v128 v128) (result v128)
local.get 0
local.get 1
v128.and
)
)
"#;
let wasm = wat::parse_str(wat).expect("Failed to parse WAT with SIMD");
let functions = decode_wasm_functions(&wasm).expect("Failed to decode");
assert_eq!(functions.len(), 1);
assert!(functions[0].ops.contains(&WasmOp::V128And));
}
#[test]
fn test_decode_simd_splat() {
let wat = r#"
(module
(func (export "splat") (param i32) (result v128)
local.get 0
i32x4.splat
)
)
"#;
let wasm = wat::parse_str(wat).expect("Failed to parse WAT with SIMD");
let functions = decode_wasm_functions(&wasm).expect("Failed to decode");
assert_eq!(functions.len(), 1);
assert!(functions[0].ops.contains(&WasmOp::I32x4Splat));
}
#[test]
fn test_decode_simd_extract_lane() {
let wat = r#"
(module
(func (export "extract") (param v128) (result i32)
local.get 0
i32x4.extract_lane 2
)
)
"#;
let wasm = wat::parse_str(wat).expect("Failed to parse WAT with SIMD");
let functions = decode_wasm_functions(&wasm).expect("Failed to decode");
assert_eq!(functions.len(), 1);
assert!(
functions[0].ops.contains(&WasmOp::I32x4ExtractLane(2)),
"Should decode i32x4.extract_lane 2"
);
}
#[test]
fn test_decode_simd_f32x4_arithmetic() {
let wat = r#"
(module
(func (export "f32x4_add") (param v128 v128) (result v128)
local.get 0
local.get 1
f32x4.add
)
)
"#;
let wasm = wat::parse_str(wat).expect("Failed to parse WAT with SIMD");
let functions = decode_wasm_functions(&wasm).expect("Failed to decode");
assert_eq!(functions.len(), 1);
assert!(functions[0].ops.contains(&WasmOp::F32x4Add));
}
#[test]
fn test_369_scalar_float_op_flags_function_unsupported_not_dropped() {
// #369: a scalar f32/f64 op the decoder can't lower must FLAG the
// function (-> loud skip), never be silently dropped (which left a
// `mov r0,r1` wrong-value stub). A pure-integer function stays clean.
let wat = r#"
(module
(func (export "fadd") (param f32 f32) (result f32)
local.get 0 local.get 1 f32.add)
(func (export "iadd") (param i32 i32) (result i32)
local.get 0 local.get 1 i32.add))
"#;
let wasm = wat::parse_str(wat).expect("parse");
let functions = decode_wasm_functions(&wasm).expect("decode");
let fadd = functions
.iter()
.find(|f| f.export_name.as_deref() == Some("fadd"))
.unwrap();
let iadd = functions
.iter()
.find(|f| f.export_name.as_deref() == Some("iadd"))
.unwrap();
assert!(
fadd.unsupported.is_some(),
"f32.add must flag the function unsupported (loud-skip), got {:?}",
fadd.unsupported
);
assert!(
fadd.unsupported.as_deref().unwrap().contains("F32Add"),
"diagnostic should name the op: {:?}",
fadd.unsupported
);
assert!(
iadd.unsupported.is_none(),
"a pure-integer function must NOT be flagged: {:?}",
iadd.unsupported
);
}
#[test]
fn test_369_float_global_access_flags_function_unsupported() {
// GI-FPU-001 (#369): `global.get`/`global.set` on an f32/f64-typed
// global decode fine (the ops are type-agnostic), but the float
// initializer is dropped (`init_i32: None` -> slot zeroed), so a read
// returned a silently-wrong 0.0 instead of the init (verified: the
// 2.5f bit pattern 0x40200000 was absent from the output ELF). The
// access must flag the function for the loud-skip path. Accesses to
// integer globals stay clean.
let wat = r#"
(module
(global $fg f32 (f32.const 2.5))
(global $dg (mut f64) (f64.const 1.5))
(global $ig (mut i32) (i32.const 7))
(func (export "fget") (result f32) global.get $fg)
(func (export "dset") (param f64) local.get 0 global.set $dg)
(func (export "iget") (result i32) global.get $ig))
"#;
let wasm = wat::parse_str(wat).expect("parse");
// Both decode entry points must flag (the CLI compiles through both:
// decode_wasm_module on the all-exports/module paths,
// decode_wasm_functions on the single-function path).
let module = decode_wasm_module(&wasm).expect("decode module");
for functions in [
&module.functions,
&decode_wasm_functions(&wasm).expect("decode fns"),
] {
let by_name = |n: &str| {
functions
.iter()
.find(|f| f.export_name.as_deref() == Some(n))
.unwrap()
};
let fget = by_name("fget");
assert!(
fget.unsupported.is_some(),
"global.get of an f32 global must flag the function (loud-skip), got {:?}",
fget.unsupported
);
let reason = fget.unsupported.as_deref().unwrap();
assert!(
reason.contains("GlobalGet") && reason.contains("GI-FPU-001"),
"diagnostic should name the op and GI-FPU-001: {reason:?}"
);
let dset = by_name("dset");
assert!(
dset.unsupported
.as_deref()
.is_some_and(|r| r.contains("GlobalSet")),
"global.set of an f64 global must flag the function, got {:?}",
dset.unsupported
);
assert!(
by_name("iget").unsupported.is_none(),
"an i32 global access must NOT be flagged: {:?}",
by_name("iget").unsupported
);
}
}
#[test]
fn test_369_imported_float_global_shifts_index_space() {
// GI-FPU-001 (#369): imported globals come FIRST in the global index
// space. An imported f64 global at index 0 must be flagged, and the
// defined i32 global at index 1 must NOT be mistaken for it.
let wat = r#"
(module
(import "env" "fg" (global f64))
(global $ig i32 (i32.const 3))
(func (export "fget") (result f64) global.get 0)
(func (export "iget") (result i32) global.get 1))
"#;
let wasm = wat::parse_str(wat).expect("parse");
let functions = decode_wasm_functions(&wasm).expect("decode");
let by_name = |n: &str| {
functions
.iter()
.find(|f| f.export_name.as_deref() == Some(n))
.unwrap()
};
assert!(
by_name("fget")
.unsupported
.as_deref()
.is_some_and(|r| r.contains("GI-FPU-001")),
"imported f64 global access must flag: {:?}",
by_name("fget").unsupported
);
assert!(
by_name("iget").unsupported.is_none(),
"defined i32 global at shifted index 1 must NOT flag: {:?}",
by_name("iget").unsupported
);
}
#[test]
fn test_decode_simd_multiple_ops() {
let wat = r#"
(module
(func (export "simd_ops") (param v128 v128 v128) (result v128)
;; (a + b) * c
local.get 0
local.get 1
i32x4.add
local.get 2
i32x4.mul
)
)
"#;
let wasm = wat::parse_str(wat).expect("Failed to parse WAT with SIMD");
let functions = decode_wasm_functions(&wasm).expect("Failed to decode");
assert_eq!(functions.len(), 1);
let ops = &functions[0].ops;
assert!(ops.contains(&WasmOp::I32x4Add));
assert!(ops.contains(&WasmOp::I32x4Mul));
}
/// VCR-DBG-001 step 1 (#394): the decoder records a module-relative wasm byte
/// offset per emitted op — the DWARF-for-wasm address space that bridges
/// synth's op-index `source_line` to the input wasm's `.debug_line`. Purely
/// additive metadata (no codegen consumer ⇒ frozen fixtures byte-identical,
/// verified separately); this test pins the structural invariants.
#[test]
fn test_decode_records_aligned_increasing_op_offsets_dbg001() {
let wat = r#"
(module
(func (export "f") (param i32 i32) (result i32)
local.get 0
local.get 1
i32.add
i32.const 7
i32.mul))
"#;
let wasm = wat::parse_str(wat).expect("parse WAT");
let functions = decode_wasm_functions(&wasm).expect("decode");
let f = &functions[0];
// One offset per emitted op, index-aligned with `ops`.
assert_eq!(
f.op_offsets.len(),
f.ops.len(),
"op_offsets must be parallel to ops"
);
assert!(!f.op_offsets.is_empty());
// Byte offsets are strictly increasing through the body (each op consumes
// at least one byte) and module-relative (well past the header).
assert!(
f.op_offsets.windows(2).all(|w| w[1] > w[0]),
"wasm byte offsets must strictly increase: {:?}",
f.op_offsets
);
assert!(
f.op_offsets[0] >= 8,
"module-relative offset is past the 8-byte wasm header"
);
}
/// #237: the decoder captures a global's `i32.const` initializer + mutability,
/// so the native-pointer ABI can recognize the stack-pointer global.
#[test]
fn test_decode_captures_global_initializer() {
let wat = r#"
(module
(memory 2)
(global $__stack_pointer (mut i32) (i32.const 65536))
(global $immutable_const i32 (i32.const 7))
(func (export "f") (result i32) global.get 0)
)
"#;
let wasm = wat::parse_str(wat).expect("Failed to parse WAT");
let module = decode_wasm_module(&wasm).expect("Failed to decode");
assert_eq!(module.globals.len(), 2, "both globals captured");
let sp = &module.globals[0];
assert_eq!(sp.index, 0);
assert_eq!(
sp.init,
Some(GlobalInit::I32(65536)),
"stack-pointer init captured"
);
assert!(sp.mutable, "stack pointer is mutable");
let c = &module.globals[1];
assert_eq!(c.init, Some(GlobalInit::I32(7)));
assert!(!c.mutable, "second global is immutable");
assert_eq!(sp.slot_bytes, 4, "i32 global occupies one 4-byte slot");
assert_eq!(c.slot_bytes, 4);
}
/// #643: the decoder records the DECLARED slot width per global — an i64
/// (or f64) global occupies 8 bytes, so the globals-table layout can give
/// it room for both words and shift every later global's offset.
#[test]
fn test_decode_records_global_slot_widths_643() {
let wat = r#"
(module
(global $c (mut i64) (i64.const 0))
(global $k (mut i32) (i32.const 0))
(global $f (mut f64) (f64.const 0))
(func (export "f") (result i32) global.get 1)
)
"#;
let wasm = wat::parse_str(wat).expect("Failed to parse WAT");
let module = decode_wasm_module(&wasm).expect("Failed to decode");
assert_eq!(module.globals.len(), 3);
assert_eq!(module.globals[0].slot_bytes, 8, "i64 global is 8 bytes");
assert_eq!(module.globals[1].slot_bytes, 4, "i32 global is 4 bytes");
assert_eq!(module.globals[2].slot_bytes, 8, "f64 global is 8 bytes");
}
/// #649: a nonzero `i64.const` initializer is captured as BOTH words — the
/// `init_i32`-shaped capture dropped it to `None` and every consumer's
/// `unwrap_or(0)` silently ZEROED the global. f32/f64 inits stay `None`
/// (GI-FPU-001/#369 loud-skip lane — never fabricate a float bit-pattern).
#[test]
fn test_decode_captures_i64_global_initializer_649() {
let wat = r#"
(module
(global $g (mut i64) (i64.const 0x123456789ABCDEF0))
(global $n (mut i64) (i64.const -1))
(global $f (mut f64) (f64.const 1.5))
(global $h (mut f32) (f32.const 2.5))
(func (export "f") (result i32) i32.const 0)
)
"#;
let wasm = wat::parse_str(wat).expect("Failed to parse WAT");
let module = decode_wasm_module(&wasm).expect("Failed to decode");
assert_eq!(module.globals.len(), 4);
assert_eq!(
module.globals[0].init,
Some(GlobalInit::I64(0x123456789ABCDEF0u64 as i64)),
"nonzero i64 init captured with both words"
);
assert_eq!(module.globals[1].init, Some(GlobalInit::I64(-1)));
assert_eq!(
module.globals[2].init, None,
"f64 init is NOT captured (GI-FPU-001 loud-skip lane)"
);
assert_eq!(
module.globals[3].init, None,
"f32 init is NOT captured (GI-FPU-001 loud-skip lane)"
);
}
/// #509: the decoder records `(param_count, result_count)` for every
/// `Block`/`Loop`/`If`, ordinal-keyed in op order, covering all three
/// blocktype encodings: `Empty → (0,0)`, `ValType → (0,1)`, and
/// `FuncType(i) →` counts from the type section (here a multi-result
/// block, which wat encodes as a functype blocktype).
#[test]
fn test_decode_records_block_arity_side_table_509() {
let wat = r#"
(module
(func (export "f") (param i32) (result i32)
(block (result i32)
(block (nop))
(local.get 0)
(if (result i32)
(then (i32.const 1))
(else (i32.const 2)))))
(func (export "g") (result i32)
(block (result i32 i32)
(i32.const 1) (i32.const 2))
i32.add)
(func (export "h") (param i32) (result i32)
(local.get 0)
(loop (param i32) (result i32))))
"#;
let wasm = wat::parse_str(wat).expect("parse WAT");
// Both decode entry points must produce the same side-table.
for functions in [
decode_wasm_functions(&wasm).expect("decode"),
decode_wasm_module(&wasm).expect("decode").functions,
] {
// f: Block(result i32), Block(void), If(result i32) — in op order.
assert_eq!(
functions[0].block_arity,
vec![(0, 1), (0, 0), (0, 1)],
"f: ValType/Empty/ValType blocktypes"
);
// g: one multi-result block via a FuncType blocktype.
assert_eq!(
functions[1].block_arity,
vec![(0, 2)],
"g: functype blocktype result count from the type section"
);
// h: a parameterized loop — the input arity is what a br to the
// header would carry (the #509 loud-decline discriminator).
assert_eq!(
functions[2].block_arity,
vec![(1, 1)],
"h: loop params captured"
);
}
}
/// #642: the decoder captures table 0's compile-time size, per-segment
/// element shapes and per-function type indices, and the closed-world
/// verdict VERIFIES a fully-covered homogeneous table.
#[test]
fn test_call_indirect_guards_closed_world_verified_642() {
// The #642 repro shape: 3-entry table, fully covered, one signature.
let wat = r#"
(module
(type $bin (func (param i32 i32) (result i32)))
(table 3 funcref)
(elem (i32.const 0) $add $sub $mul)
(func $add (param i32 i32) (result i32)
(i32.add (local.get 0) (local.get 1)))
(func $sub (param i32 i32) (result i32)
(i32.sub (local.get 0) (local.get 1)))
(func $mul (param i32 i32) (result i32)
(i32.mul (local.get 0) (local.get 1)))
(func (export "f") (param i32 i32) (result i32)
(call_indirect (type $bin)
(local.get 0) (i32.const 10) (local.get 1)))
)
"#;
let wasm = wat::parse_str(wat).expect("parse");
let module = decode_wasm_module(&wasm).expect("decode");
assert_eq!(module.table_size, Some(3), "table section min size");
assert_eq!(module.table_sizes, vec![Some(3)], "#650 per-table sizes");
assert_eq!(
module.elem_segments,
vec![ElemSegmentInfo {
table_index: 0,
offset: Some(0),
funcs: Some(vec![0, 1, 2]),
}]
);
// 2 type-section entries ($bin + the export's (i32 i32)->i32 dedups
// to one in practice, but don't assume — just check func 0..2 share
// a signature with type 0).
assert_eq!(module.func_type_indices.len(), 4);
let guards = module.call_indirect_guards();
assert_eq!(guards.tables.len(), 1);
assert_eq!(guards.tables[0].table_size, Some(3));
assert_eq!(
guards.tables[0].base_byte_offset,
Some(0),
"#650: a single-table module keeps table 0 at R11 offset 0 by construction"
);
// Type index 0 ($bin) must be VERIFIED: every table entry has its
// exact signature.
assert_eq!(
guards.tables[0].type_reject.first(),
Some(&None),
"closed-world type check must verify the homogeneous table: {:?}",
guards.tables[0].type_reject
);
assert!(
!guards.tables[0].has_null_slots,
"#664: a fully-initialized table must NOT request the runtime \
null check (dispatch bytes stay identical by construction)"
);
}
/// #642: a heterogeneous table (an entry whose signature differs from the
/// expected type) must REJECT that expected type — the raw code-pointer
/// table cannot be runtime-type-checked, so the lowering has to decline.
#[test]
fn test_call_indirect_guards_heterogeneous_table_rejects_642() {
let wat = r#"
(module
(type $bin (func (param i32 i32) (result i32)))
(type $un (func (param i32) (result i32)))
(table 2 funcref)
(elem (i32.const 0) $add $neg)
(func $add (type $bin)
(i32.add (local.get 0) (local.get 1)))
(func $neg (type $un)
(i32.sub (i32.const 0) (local.get 0)))
(func (export "f") (param i32 i32) (result i32)
(call_indirect (type $bin)
(local.get 0) (i32.const 10) (local.get 1)))
)
"#;
let wasm = wat::parse_str(wat).expect("parse");
let module = decode_wasm_module(&wasm).expect("decode");
let guards = module.call_indirect_guards();
assert_eq!(guards.tables[0].table_size, Some(2));
// BOTH expected types must be rejected: the table holds one function
// of each signature, so neither type's closed world holds.
assert!(
guards.tables[0].type_reject[0].is_some() && guards.tables[0].type_reject[1].is_some(),
"heterogeneous table must reject every expected type: {:?}",
guards.tables[0].type_reject
);
}
/// #664 (relaxes the #642 all-reject): an uninitialized table slot (elem
/// covers less than the declared size) is a null funcref — calling it
/// must trap, which is now discharged at RUNTIME (null check on the
/// zero-linked pointer), so the closed-world verdict verifies the
/// INITIALIZED slots and sets `has_null_slots` for the lowering.
#[test]
fn test_call_indirect_guards_null_slot_verifies_with_flag_664() {
let wat = r#"
(module
(type $s (func (result i32)))
(table 3 funcref)
(elem (i32.const 0) $f0 $f1)
(func $f0 (result i32) (i32.const 10))
(func $f1 (result i32) (i32.const 11))
(func (export "run") (param i32) (result i32)
(call_indirect (type $s) (local.get 0)))
)
"#;
let wasm = wat::parse_str(wat).expect("parse");
let module = decode_wasm_module(&wasm).expect("decode");
let guards = module.call_indirect_guards();
assert_eq!(guards.tables[0].table_size, Some(3));
assert_eq!(
guards.tables[0].type_reject.first(),
Some(&None),
"initialized slots are homogeneous in $s — the verdict must \
verify despite the null slot (#664): {:?}",
guards.tables[0].type_reject
);
assert!(
guards.tables[0].has_null_slots,
"slot 2 is uninitialized — the lowering must emit the runtime \
null check (#664)"
);
}
/// #664: the falcon shape — a SPARSE table (only slots 1 and 3 of 4
/// initialized, by two separate segments) verifies with the null flag;
/// a sparse table whose INITIALIZED slots are heterogeneous still
/// rejects (the runtime null check cannot discharge a TYPE mismatch).
#[test]
fn test_call_indirect_guards_sparse_table_664() {
let wat = r#"
(module
(type $t (func (param i32) (result i32)))
(table 4 4 funcref)
(func $f1 (type $t) (i32.add (local.get 0) (i32.const 100)))
(func $f3 (type $t) (i32.sub (i32.const 1000) (local.get 0)))
(elem (i32.const 1) $f1)
(elem (i32.const 3) $f3)
(func (export "via") (param i32 i32) (result i32)
(call_indirect (type $t) (local.get 0) (local.get 1)))
)
"#;
let wasm = wat::parse_str(wat).expect("parse");
let module = decode_wasm_module(&wasm).expect("decode");
let guards = module.call_indirect_guards();
assert_eq!(guards.tables[0].table_size, Some(4));
assert_eq!(
guards.tables[0].type_reject.first(),
Some(&None),
"slots 1,3 are homogeneous in $t — verified: {:?}",
guards.tables[0].type_reject
);
assert!(guards.tables[0].has_null_slots, "slots 0,2 are null");
// Heterogeneous INITIALIZED slots in a sparse table: still rejected.
let wat = r#"
(module
(type $t (func (param i32) (result i32)))
(type $u (func (param i32 i32) (result i32)))
(table 4 4 funcref)
(func $f1 (type $t) (local.get 0))
(func $f3 (type $u) (i32.add (local.get 0) (local.get 1)))
(elem (i32.const 1) $f1)
(elem (i32.const 3) $f3)
(func (export "via") (param i32 i32) (result i32)
(call_indirect (type $t) (local.get 0) (local.get 1)))
)
"#;
let wasm = wat::parse_str(wat).expect("parse");
let module = decode_wasm_module(&wasm).expect("decode");
let guards = module.call_indirect_guards();
assert!(
guards.tables[0].type_reject[0].is_some() && guards.tables[0].type_reject[1].is_some(),
"a heterogeneous sparse table must still reject every type: {:?}",
guards.tables[0].type_reject
);
}
/// #642: no table at all → no compile-time bound → table_size None and
/// every type rejected (the lowering declines).
#[test]
fn test_call_indirect_guards_no_table_642() {
let wat = r#"
(module
(func (export "f") (param i32) (result i32) (local.get 0))
)
"#;
let wasm = wat::parse_str(wat).expect("parse");
let module = decode_wasm_module(&wasm).expect("decode");
assert_eq!(module.table_size, None);
assert!(module.table_sizes.is_empty(), "#650: no tables declared");
let guards = module.call_indirect_guards();
assert!(
guards.tables.is_empty(),
"no table → no guard entry → every call_indirect declines"
);
}
/// #642: duplicate-but-structurally-identical types stay interchangeable —
/// the closed-world check compares SIGNATURES, not type indices.
#[test]
fn test_call_indirect_guards_duplicate_types_verified_642() {
let wat = r#"
(module
(type $a (func (result i32)))
(type $b (func (result i32)))
(table 1 funcref)
(elem (i32.const 0) $f)
(func $f (type $a) (i32.const 7))
(func (export "run") (param i32) (result i32)
(call_indirect (type $b) (local.get 0)))
)
"#;
let wasm = wat::parse_str(wat).expect("parse");
let module = decode_wasm_module(&wasm).expect("decode");
let guards = module.call_indirect_guards();
// $f has type $a; the call expects $b — structurally identical, so
// BOTH type indices must verify. (A third type — the export's
// (i32)->i32 — is correctly rejected: different signature.)
assert_eq!(
&guards.tables[0].type_reject[0..2],
&[None, None],
"structural signature comparison must accept duplicate types: {:?}",
guards.tables[0].type_reject
);
assert!(
guards.tables[0].type_reject[2].is_some(),
"the structurally-different third type must still be rejected"
);
}
/// #650: TWO tables become a contiguous R11 region — table 0 at offset 0
/// (byte-identical single-table degeneration), table 1 at
/// `size(table 0) * 4`. Each table gets its OWN size, base offset, and
/// per-type closed-world verdicts (segments only poison the table they
/// target).
#[test]
fn test_call_indirect_guards_multi_table_650() {
// The #650 repro shape: overlapping indices, distinct functions —
// table0[1] != table1[1] (the aliasing canary).
let wat = r#"
(module
(type $t (func (param i32) (result i32)))
(type $u (func (param i32 i32) (result i32)))
(table $t0 3 3 funcref)
(table $t1 2 2 funcref)
(func $a0 (type $t) (i32.add (local.get 0) (i32.const 100)))
(func $a1 (type $t) (i32.add (local.get 0) (i32.const 200)))
(func $a2 (type $t) (i32.add (local.get 0) (i32.const 300)))
(func $b0 (type $u) (i32.add (local.get 0) (local.get 1)))
(func $b1 (type $u) (i32.sub (local.get 0) (local.get 1)))
(elem (table $t0) (i32.const 0) func $a0 $a1 $a2)
(elem (table $t1) (i32.const 0) func $b0 $b1)
(func (export "f") (param i32 i32) (result i32)
(call_indirect $t1 (type $u)
(local.get 0) (i32.const 10) (local.get 1)))
)
"#;
let wasm = wat::parse_str(wat).expect("parse");
let module = decode_wasm_module(&wasm).expect("decode");
assert_eq!(module.table_sizes, vec![Some(3), Some(2)]);
assert_eq!(module.table_size, Some(3), "compat accessor = table 0");
assert_eq!(
module.elem_segments[0].table_index, 0,
"segment 0 targets table 0"
);
assert_eq!(
module.elem_segments[1],
ElemSegmentInfo {
table_index: 1,
offset: Some(0),
funcs: Some(vec![3, 4]),
},
"segment 1 is statically attributed to table 1 (#650)"
);
let guards = module.call_indirect_guards();
assert_eq!(guards.tables.len(), 2);
assert_eq!(guards.tables[0].table_size, Some(3));
assert_eq!(guards.tables[0].base_byte_offset, Some(0));
assert_eq!(guards.tables[1].table_size, Some(2));
assert_eq!(
guards.tables[1].base_byte_offset,
Some(12),
"table 1 base = size(table 0) * 4 within the contiguous R11 region"
);
// Table 0 is homogeneous in $t (type 0); table 1 in $u (type 1) —
// each verifies ITS type and rejects the other's.
assert_eq!(guards.tables[0].type_reject[0], None, "table 0 vs $t");
assert!(guards.tables[0].type_reject[1].is_some(), "table 0 vs $u");
assert!(guards.tables[1].type_reject[0].is_some(), "table 1 vs $t");
assert_eq!(guards.tables[1].type_reject[1], None, "table 1 vs $u");
}
/// #650: an unknown-size table (growable import) declines ITSELF and
/// makes every LATER table's base offset non-constant — but a table
/// BEFORE it is unaffected.
#[test]
fn test_call_indirect_guards_unknown_size_poisons_later_bases_650() {
let wat = r#"
(module
(type $t (func (result i32)))
(import "env" "tbl" (table 4 funcref))
(table $d 1 1 funcref)
(func $f (type $t) (i32.const 7))
(elem (table $d) (i32.const 0) func $f)
(func (export "run") (param i32) (result i32)
(call_indirect $d (type $t) (local.get 0)))
)
"#;
let wasm = wat::parse_str(wat).expect("parse");
let module = decode_wasm_module(&wasm).expect("decode");
assert_eq!(
module.table_sizes,
vec![None, Some(1)],
"growable import (no max) has no sound compile-time size"
);
let guards = module.call_indirect_guards();
assert_eq!(guards.tables[0].base_byte_offset, Some(0));
assert!(
guards.tables[0].type_reject.iter().all(|r| r.is_some()),
"unknown-size table rejects every type"
);
assert_eq!(
guards.tables[1].base_byte_offset, None,
"a later table's base is not a compile-time constant when a \
preceding table's size is unknown (#650)"
);
assert_eq!(guards.tables[1].table_size, Some(1));
}
}