unpeek 1.13.0 → 1.15.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/ast/index.cjs +1089 -335
- package/dist/ast/index.cjs.map +1 -1
- package/dist/ast/index.d.cts +6 -5
- package/dist/ast/index.d.ts +6 -5
- package/dist/ast/index.js +1089 -335
- package/dist/ast/index.js.map +1 -1
- package/dist/browser.cjs +1089 -335
- package/dist/browser.cjs.map +1 -1
- package/dist/browser.d.cts +1 -1
- package/dist/browser.d.ts +1 -1
- package/dist/browser.js +1089 -335
- package/dist/browser.js.map +1 -1
- package/dist/bundler/unplugin.cjs +1109 -347
- package/dist/bundler/unplugin.cjs.map +1 -1
- package/dist/bundler/unplugin.js +1109 -347
- package/dist/bundler/unplugin.js.map +1 -1
- package/dist/cli/index.cjs +1237 -367
- package/dist/cli/index.cjs.map +1 -1
- package/dist/cli/index.js +1237 -367
- package/dist/cli/index.js.map +1 -1
- package/dist/cli/init.cjs +101 -8
- package/dist/cli/init.cjs.map +1 -1
- package/dist/cli/init.d.cts +4 -1
- package/dist/cli/init.d.ts +4 -1
- package/dist/cli/init.js +99 -7
- package/dist/cli/init.js.map +1 -1
- package/dist/cli/protect.cjs +1119 -349
- package/dist/cli/protect.cjs.map +1 -1
- package/dist/cli/protect.js +1119 -349
- package/dist/cli/protect.js.map +1 -1
- package/dist/config.cjs +13 -3
- package/dist/config.cjs.map +1 -1
- package/dist/config.js +13 -3
- package/dist/config.js.map +1 -1
- package/dist/esbuild.cjs +1109 -347
- package/dist/esbuild.cjs.map +1 -1
- package/dist/esbuild.d.cts +1 -1
- package/dist/esbuild.d.ts +1 -1
- package/dist/esbuild.js +1109 -347
- package/dist/esbuild.js.map +1 -1
- package/dist/index.cjs +1413 -382
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.js +1504 -468
- package/dist/index.js.map +1 -1
- package/dist/next/adapter.cjs +5838 -0
- package/dist/next/adapter.cjs.map +1 -0
- package/dist/next/adapter.d.cts +10 -0
- package/dist/next/adapter.d.ts +10 -0
- package/dist/next/adapter.js +5795 -0
- package/dist/next/adapter.js.map +1 -0
- package/dist/next/index.cjs +5865 -0
- package/dist/next/index.cjs.map +1 -0
- package/dist/next/index.d.cts +37 -0
- package/dist/next/index.d.ts +37 -0
- package/dist/next/index.js +5829 -0
- package/dist/next/index.js.map +1 -0
- package/dist/next/with-unpeek.cjs +5865 -0
- package/dist/next/with-unpeek.cjs.map +1 -0
- package/dist/next/with-unpeek.d.cts +2 -0
- package/dist/next/with-unpeek.d.ts +2 -0
- package/dist/next/with-unpeek.js +5829 -0
- package/dist/next/with-unpeek.js.map +1 -0
- package/dist/rollup.cjs +1109 -347
- package/dist/rollup.cjs.map +1 -1
- package/dist/rollup.d.cts +1 -1
- package/dist/rollup.d.ts +1 -1
- package/dist/rollup.js +1109 -347
- package/dist/rollup.js.map +1 -1
- package/dist/runtime/index.cjs +6 -3
- package/dist/runtime/index.cjs.map +1 -1
- package/dist/runtime/index.js +6 -3
- package/dist/runtime/index.js.map +1 -1
- package/dist/{types-CgWUnSfX.d.cts → types-CQYBNQND.d.cts} +14 -0
- package/dist/{types-CgWUnSfX.d.ts → types-CQYBNQND.d.ts} +14 -0
- package/dist/vite.cjs +1109 -347
- package/dist/vite.cjs.map +1 -1
- package/dist/vite.d.cts +1 -1
- package/dist/vite.d.ts +1 -1
- package/dist/vite.js +1109 -347
- package/dist/vite.js.map +1 -1
- package/dist/webpack.cjs +1109 -347
- package/dist/webpack.cjs.map +1 -1
- package/dist/webpack.d.cts +1 -1
- package/dist/webpack.d.ts +1 -1
- package/dist/webpack.js +1109 -347
- package/dist/webpack.js.map +1 -1
- package/package.json +17 -1
package/dist/ast/index.cjs
CHANGED
|
@@ -1272,11 +1272,14 @@ function deriveWindowKeySymbol(seed) {
|
|
|
1272
1272
|
function createDeterrentSnippet(config) {
|
|
1273
1273
|
const seed = config.seed >>> 0;
|
|
1274
1274
|
const deterrentMode = config.deterrentMode ?? "passive";
|
|
1275
|
+
const detectHeadless = config.detectHeadless ?? false;
|
|
1275
1276
|
const fnName = deriveIntegrityFnName(seed);
|
|
1276
1277
|
const sym = config.windowKeySymbol ?? deriveWindowKeySymbol(seed);
|
|
1277
1278
|
const consoleSilence = deterrentMode === "gradual" ? "for(var k in console)console[k]=function(){return 0;};" : "";
|
|
1278
|
-
const
|
|
1279
|
-
|
|
1279
|
+
const symLit = JSON.stringify(sym);
|
|
1280
|
+
const headlessDecl = detectHeadless ? ",n=w.navigator||0" : "";
|
|
1281
|
+
const headlessCond = detectHeadless ? "||n.webdriver||/HeadlessChrome/.test(n.userAgent)" : "";
|
|
1282
|
+
return `(function ${fnName}(w){var G=typeof globalThis!=='undefined'?globalThis:w;if(w){var O=Object,P=O.getOwnPropertyDescriptor,GP=O.getPrototypeOf,T=function(o,k){try{while(o){var r=P&&P(o,k);if(r&&(r.set||!r.writable))return 1;o=GP?GP(o):0;}}catch(e){return 1;}},S=function(o,k,v){try{o[k]=v;}catch(e){}},s=Function.prototype.toString${headlessDecl},src='',t=0;try{src=s.call(${fnName});if('prototype' in s||s.name!=='toString'||s.length!==0||s.call(s).indexOf('[native code]')<0||/\\n/.test(src)${headlessCond})t=1;}catch(e){t=1;}try{s.call(0);t=1;}catch(e){}if(T(w,${symLit})||T(G,${symLit})||T(G,'__unpeek_tampered')||T(w,'__unpeek_tampered')||T(String.prototype,'_u'))t=1;for(var h=0x811c9dc5,i=0;i<src.length;i++)h=Math.imul(h^src.charCodeAt(i),0x01000193)>>>0;var d=h&0xffff;if(t){var pd=(d^(((h>>>16)^0x5a5b)||1))&0xffff;if(!T(G,${symLit}))S(G,${symLit},pd);if(!T(w,${symLit}))S(w,${symLit},pd);S(G,'__unpeek_delta',pd);S(w,'__unpeek_delta',pd);try{String.prototype._u=14;}catch(e){}var ns=((w.__unpeek_seed||${seed})^1)>>>0;S(G,'__unpeek_seed',ns);S(w,'__unpeek_seed',ns);S(G,'__unpeek_tampered',!0);S(w,'__unpeek_tampered',!0);try{${consoleSilence}if(w.dispatchEvent)w.dispatchEvent(new CustomEvent('unpeek:tamper'));}catch(e){}}else{S(G,${symLit},d);S(w,${symLit},d);}}})(typeof window!=='undefined'?window:(typeof self!=='undefined'?self:0));`;
|
|
1280
1283
|
}
|
|
1281
1284
|
|
|
1282
1285
|
// src/runtime/index.ts
|
|
@@ -1289,7 +1292,7 @@ function generateRuntimeShield(config) {
|
|
|
1289
1292
|
windowKeySymbol
|
|
1290
1293
|
});
|
|
1291
1294
|
const fnStart = preambleCode.indexOf("function");
|
|
1292
|
-
const endMarker = preambleCode.
|
|
1295
|
+
const endMarker = preambleCode.lastIndexOf("})(");
|
|
1293
1296
|
const fnEnd = endMarker !== -1 ? endMarker + 1 : preambleCode.lastIndexOf("(") - 1;
|
|
1294
1297
|
const fnDecl = preambleCode.slice(fnStart, fnEnd);
|
|
1295
1298
|
const computedHash = computeIntegrityHash(fnDecl, 0);
|
|
@@ -1346,8 +1349,17 @@ function writeI32LE(value) {
|
|
|
1346
1349
|
const v = value | 0;
|
|
1347
1350
|
return [v & 255, v >>> 8 & 255, v >>> 16 & 255, v >>> 24 & 255];
|
|
1348
1351
|
}
|
|
1349
|
-
function
|
|
1352
|
+
function deriveKdfParams(seed) {
|
|
1350
1353
|
const sVal = seed | 0;
|
|
1354
|
+
const c1 = computeFnv1aHash("wasm-c1", seed) ^ 2246822507 | 1;
|
|
1355
|
+
const c2 = computeFnv1aHash("wasm-c2", seed) ^ 3266489909 | 1;
|
|
1356
|
+
const k0 = computeFnv1aHash("wasm-k0", seed) | 0;
|
|
1357
|
+
const r1 = (seed >>> 0) % 19 + 7;
|
|
1358
|
+
const r2 = (seed >>> 5) % 17 + 5;
|
|
1359
|
+
return { sVal, c1, c2, k0, r1, r2 };
|
|
1360
|
+
}
|
|
1361
|
+
function generateWasmKdfBinary(seed) {
|
|
1362
|
+
const { sVal, c1, c2, k0, r1, r2 } = deriveKdfParams(seed);
|
|
1351
1363
|
const memBase = (seed >>> 0 & 127) * 4;
|
|
1352
1364
|
const r0 = computeFnv1aHash("wasm-r0", seed) | 0;
|
|
1353
1365
|
const rotAmt = (seed >>> 0) % 27 + 3;
|
|
@@ -1358,8 +1370,9 @@ function generateWasmKdfBinary(seed) {
|
|
|
1358
1370
|
const memA = memBase ^ memMask;
|
|
1359
1371
|
const w0 = r0;
|
|
1360
1372
|
const w1 = sVal ^ rotl32(r0, rotAmt) ^ Math.imul(r0 | 1, kMul) | 0;
|
|
1361
|
-
const w2 =
|
|
1362
|
-
const w3 =
|
|
1373
|
+
const w2 = c1 ^ (w0 + w1 | 0) | 0;
|
|
1374
|
+
const w3 = c2 - Math.imul(w1 | 1, w2 | 1) | 0;
|
|
1375
|
+
const w4 = k0 ^ rotl32(w2, 11) ^ w3 | 0;
|
|
1363
1376
|
const instructions = [
|
|
1364
1377
|
65,
|
|
1365
1378
|
...encodeSignedLeb128(memA),
|
|
@@ -1399,10 +1412,17 @@ function generateWasmKdfBinary(seed) {
|
|
|
1399
1412
|
33,
|
|
1400
1413
|
6,
|
|
1401
1414
|
// local.set 6 (w2)
|
|
1402
|
-
// (fnId ^ seed) * m1
|
|
1403
1415
|
32,
|
|
1404
|
-
|
|
1405
|
-
// local.get
|
|
1416
|
+
3,
|
|
1417
|
+
// local.get 3
|
|
1418
|
+
40,
|
|
1419
|
+
2,
|
|
1420
|
+
12,
|
|
1421
|
+
// i32.load align=2 offset=12
|
|
1422
|
+
33,
|
|
1423
|
+
7,
|
|
1424
|
+
// local.set 7 (w3)
|
|
1425
|
+
// Reconstruct sVal -> local 8
|
|
1406
1426
|
32,
|
|
1407
1427
|
5,
|
|
1408
1428
|
// local.get 5 (w1)
|
|
@@ -1440,9 +1460,11 @@ function generateWasmKdfBinary(seed) {
|
|
|
1440
1460
|
108,
|
|
1441
1461
|
// i32.mul
|
|
1442
1462
|
115,
|
|
1443
|
-
// i32.xor
|
|
1444
|
-
|
|
1445
|
-
|
|
1463
|
+
// i32.xor
|
|
1464
|
+
33,
|
|
1465
|
+
8,
|
|
1466
|
+
// local.set 8 (sVal)
|
|
1467
|
+
// Reconstruct c1 -> local 9
|
|
1446
1468
|
32,
|
|
1447
1469
|
6,
|
|
1448
1470
|
// local.get 6 (w2)
|
|
@@ -1455,25 +1477,95 @@ function generateWasmKdfBinary(seed) {
|
|
|
1455
1477
|
106,
|
|
1456
1478
|
// i32.add
|
|
1457
1479
|
115,
|
|
1458
|
-
// i32.xor
|
|
1480
|
+
// i32.xor
|
|
1481
|
+
33,
|
|
1482
|
+
9,
|
|
1483
|
+
// local.set 9 (c1)
|
|
1484
|
+
// Reconstruct k0 -> local 10
|
|
1485
|
+
32,
|
|
1486
|
+
3,
|
|
1487
|
+
// local.get 3 (ptr)
|
|
1488
|
+
40,
|
|
1489
|
+
2,
|
|
1490
|
+
16,
|
|
1491
|
+
// i32.load align=2 offset=16 (w4)
|
|
1492
|
+
32,
|
|
1493
|
+
6,
|
|
1494
|
+
// local.get 6 (w2)
|
|
1495
|
+
65,
|
|
1496
|
+
11,
|
|
1497
|
+
// i32.const 11
|
|
1498
|
+
119,
|
|
1499
|
+
// i32.rotl
|
|
1500
|
+
115,
|
|
1501
|
+
// i32.xor
|
|
1502
|
+
32,
|
|
1503
|
+
7,
|
|
1504
|
+
// local.get 7 (w3)
|
|
1505
|
+
115,
|
|
1506
|
+
// i32.xor
|
|
1507
|
+
33,
|
|
1508
|
+
10,
|
|
1509
|
+
// local.set 10 (k0)
|
|
1510
|
+
// x = f ^ sVal ^ k0
|
|
1511
|
+
32,
|
|
1512
|
+
0,
|
|
1513
|
+
// local.get 0 (fnId)
|
|
1514
|
+
32,
|
|
1515
|
+
8,
|
|
1516
|
+
// local.get 8 (sVal)
|
|
1517
|
+
115,
|
|
1518
|
+
// i32.xor
|
|
1519
|
+
32,
|
|
1520
|
+
10,
|
|
1521
|
+
// local.get 10 (k0)
|
|
1522
|
+
115,
|
|
1523
|
+
// i32.xor
|
|
1524
|
+
34,
|
|
1525
|
+
11,
|
|
1526
|
+
// local.tee 11 (x)
|
|
1527
|
+
// x ^= x >>> 16
|
|
1528
|
+
32,
|
|
1529
|
+
11,
|
|
1530
|
+
// local.get 11
|
|
1531
|
+
65,
|
|
1532
|
+
16,
|
|
1533
|
+
// i32.const 16
|
|
1534
|
+
118,
|
|
1535
|
+
// i32.shr_u
|
|
1536
|
+
115,
|
|
1537
|
+
// i32.xor
|
|
1538
|
+
// x = imul(x, c1)
|
|
1539
|
+
32,
|
|
1540
|
+
9,
|
|
1541
|
+
// local.get 9 (c1)
|
|
1459
1542
|
108,
|
|
1460
1543
|
// i32.mul
|
|
1461
|
-
// (
|
|
1544
|
+
// x = rotl(x ^ s, r1)
|
|
1462
1545
|
32,
|
|
1463
1546
|
1,
|
|
1464
1547
|
// local.get 1 (callSiteSalt)
|
|
1548
|
+
115,
|
|
1549
|
+
// i32.xor
|
|
1550
|
+
65,
|
|
1551
|
+
...encodeSignedLeb128(r1),
|
|
1552
|
+
// i32.const r1
|
|
1553
|
+
119,
|
|
1554
|
+
// i32.rotl
|
|
1555
|
+
// x = imul(x ^ h ^ sVal, c2)
|
|
1465
1556
|
32,
|
|
1466
1557
|
2,
|
|
1467
1558
|
// local.get 2 (shieldHash)
|
|
1468
|
-
|
|
1469
|
-
// i32.
|
|
1559
|
+
115,
|
|
1560
|
+
// i32.xor
|
|
1470
1561
|
32,
|
|
1471
|
-
|
|
1472
|
-
// local.get
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
1562
|
+
8,
|
|
1563
|
+
// local.get 8 (sVal)
|
|
1564
|
+
115,
|
|
1565
|
+
// i32.xor
|
|
1566
|
+
32,
|
|
1567
|
+
7,
|
|
1568
|
+
// local.get 7 (w3)
|
|
1477
1569
|
32,
|
|
1478
1570
|
5,
|
|
1479
1571
|
// local.get 5 (w1)
|
|
@@ -1493,15 +1585,63 @@ function generateWasmKdfBinary(seed) {
|
|
|
1493
1585
|
108,
|
|
1494
1586
|
// i32.mul
|
|
1495
1587
|
106,
|
|
1496
|
-
// i32.add (
|
|
1588
|
+
// i32.add (c2)
|
|
1497
1589
|
108,
|
|
1498
1590
|
// i32.mul
|
|
1591
|
+
34,
|
|
1592
|
+
11,
|
|
1593
|
+
// local.tee 11 (x)
|
|
1594
|
+
// x ^= x >>> 15
|
|
1595
|
+
32,
|
|
1596
|
+
11,
|
|
1597
|
+
// local.get 11
|
|
1598
|
+
65,
|
|
1599
|
+
15,
|
|
1600
|
+
// i32.const 15
|
|
1601
|
+
118,
|
|
1602
|
+
// i32.shr_u
|
|
1603
|
+
115,
|
|
1604
|
+
// i32.xor
|
|
1605
|
+
// x = (x + rotl(s ^ k0, r2))
|
|
1606
|
+
32,
|
|
1607
|
+
1,
|
|
1608
|
+
// local.get 1 (callSiteSalt)
|
|
1609
|
+
32,
|
|
1610
|
+
10,
|
|
1611
|
+
// local.get 10 (k0)
|
|
1612
|
+
115,
|
|
1613
|
+
// i32.xor
|
|
1614
|
+
65,
|
|
1615
|
+
...encodeSignedLeb128(r2),
|
|
1616
|
+
// i32.const r2
|
|
1617
|
+
119,
|
|
1618
|
+
// i32.rotl
|
|
1619
|
+
106,
|
|
1620
|
+
// i32.add
|
|
1621
|
+
// x = imul(x, c1)
|
|
1622
|
+
32,
|
|
1623
|
+
9,
|
|
1624
|
+
// local.get 9 (c1)
|
|
1625
|
+
108,
|
|
1626
|
+
// i32.mul
|
|
1627
|
+
34,
|
|
1628
|
+
11,
|
|
1629
|
+
// local.tee 11 (x)
|
|
1630
|
+
// x ^= x >>> 16
|
|
1631
|
+
32,
|
|
1632
|
+
11,
|
|
1633
|
+
// local.get 11
|
|
1634
|
+
65,
|
|
1635
|
+
16,
|
|
1636
|
+
// i32.const 16
|
|
1637
|
+
118,
|
|
1638
|
+
// i32.shr_u
|
|
1499
1639
|
115,
|
|
1500
1640
|
// i32.xor
|
|
1501
1641
|
11
|
|
1502
1642
|
// end
|
|
1503
1643
|
];
|
|
1504
|
-
const funcBody = [1,
|
|
1644
|
+
const funcBody = [1, 9, 127, ...instructions];
|
|
1505
1645
|
const funcBodyWithLen = [...encodeUnsignedLeb128(funcBody.length), ...funcBody];
|
|
1506
1646
|
const codeSectionPayload = [1, ...funcBodyWithLen];
|
|
1507
1647
|
const codeSection = [
|
|
@@ -1559,7 +1699,8 @@ function generateWasmKdfBinary(seed) {
|
|
|
1559
1699
|
...writeI32LE(w0),
|
|
1560
1700
|
...writeI32LE(w1),
|
|
1561
1701
|
...writeI32LE(w2),
|
|
1562
|
-
...writeI32LE(w3)
|
|
1702
|
+
...writeI32LE(w3),
|
|
1703
|
+
...writeI32LE(w4)
|
|
1563
1704
|
];
|
|
1564
1705
|
const dataPayload = [
|
|
1565
1706
|
1,
|
|
@@ -1590,7 +1731,17 @@ function generateWasmKdfBinary(seed) {
|
|
|
1590
1731
|
]);
|
|
1591
1732
|
}
|
|
1592
1733
|
function fallbackKdf(fnId, callSiteSalt, shieldHash, seed) {
|
|
1593
|
-
|
|
1734
|
+
const { sVal, c1, c2, k0, r1, r2 } = deriveKdfParams(seed);
|
|
1735
|
+
let x = fnId ^ sVal ^ k0 | 0;
|
|
1736
|
+
x = x ^ x >>> 16 | 0;
|
|
1737
|
+
x = Math.imul(x, c1);
|
|
1738
|
+
x = rotl32(x ^ callSiteSalt | 0, r1);
|
|
1739
|
+
x = Math.imul(x ^ shieldHash ^ sVal | 0, c2);
|
|
1740
|
+
x = x ^ x >>> 15 | 0;
|
|
1741
|
+
x = x + rotl32(callSiteSalt ^ k0 | 0, r2) | 0;
|
|
1742
|
+
x = Math.imul(x, c1);
|
|
1743
|
+
x = x ^ x >>> 16 | 0;
|
|
1744
|
+
return x | 0;
|
|
1594
1745
|
}
|
|
1595
1746
|
function isWasmVaultAlreadyInjected(ast) {
|
|
1596
1747
|
if (!ast || !Array.isArray(ast.body)) return false;
|
|
@@ -1622,7 +1773,8 @@ function injectWasmVault(ast, seed = 2654435769) {
|
|
|
1622
1773
|
}
|
|
1623
1774
|
const wasmBytes = generateWasmKdfBinary(seed);
|
|
1624
1775
|
const byteArrayLiteral = Array.from(wasmBytes).join(",");
|
|
1625
|
-
const
|
|
1776
|
+
const { sVal, c1, c2, k0, r1, r2 } = deriveKdfParams(seed);
|
|
1777
|
+
const targets = [c1, c2, sVal, k0, r1, r2];
|
|
1626
1778
|
const aArr = [];
|
|
1627
1779
|
const bArr = [];
|
|
1628
1780
|
const mArr = [];
|
|
@@ -1649,7 +1801,7 @@ try {
|
|
|
1649
1801
|
var _b = [${bArr.join(",")}];
|
|
1650
1802
|
var _m0 = [${mArr.join(",")}];
|
|
1651
1803
|
var _k = [];
|
|
1652
|
-
for (var _j = 0; _j <
|
|
1804
|
+
for (var _j = 0; _j < ${targets.length}; _j++) {
|
|
1653
1805
|
var _u = _a[_j] | _z, _v = _b[_j];
|
|
1654
1806
|
var _t = ((_u | _v) + (_u & _v)) | 0;
|
|
1655
1807
|
_k[_j] = ((_t + _m0[_j]) - ((_t & _m0[_j]) << 1)) | 0;
|
|
@@ -1661,12 +1813,17 @@ try {
|
|
|
1661
1813
|
}
|
|
1662
1814
|
var _m = Math[_p];
|
|
1663
1815
|
return function(f, s, h) {
|
|
1664
|
-
var
|
|
1665
|
-
|
|
1666
|
-
|
|
1667
|
-
var
|
|
1668
|
-
|
|
1669
|
-
|
|
1816
|
+
var _x = (f ^ _k[2] ^ _k[3]) | 0;
|
|
1817
|
+
_x = (_x ^ (_x >>> 16)) | 0;
|
|
1818
|
+
_x = _m(_x, _k[0]);
|
|
1819
|
+
var _t0 = _x ^ s, _r1 = _k[4];
|
|
1820
|
+
_x = (_t0 << _r1) | (_t0 >>> (32 - _r1));
|
|
1821
|
+
_x = _m(_x ^ h ^ _k[2], _k[1]);
|
|
1822
|
+
_x = (_x ^ (_x >>> 15)) | 0;
|
|
1823
|
+
var _u0 = s ^ _k[3], _r2 = _k[5];
|
|
1824
|
+
_x = (_x + ((_u0 << _r2) | (_u0 >>> (32 - _r2)))) | 0;
|
|
1825
|
+
_x = _m(_x, _k[0]);
|
|
1826
|
+
return (_x ^ (_x >>> 16)) | 0;
|
|
1670
1827
|
};
|
|
1671
1828
|
})();
|
|
1672
1829
|
}`;
|
|
@@ -1696,11 +1853,18 @@ try {
|
|
|
1696
1853
|
function computeCallSiteSalt(seed, idx) {
|
|
1697
1854
|
return ((Math.imul((seed ^ idx) >>> 0, 2654435761) ^ Math.imul(idx + 1 >>> 0, 73244475)) & 31) >>> 0;
|
|
1698
1855
|
}
|
|
1699
|
-
function createSaltedDecryptorFnAst(seed, pChars, expectedDelta, windowKeySymbol, safeAlpha = SAFE_ALPHA) {
|
|
1700
|
-
const maskA = computeFnv1aHash("delta-a", seed) & 65535 | 1;
|
|
1701
|
-
const maskB = (expectedDelta & 65535 ^ maskA) & 65535;
|
|
1856
|
+
function createSaltedDecryptorFnAst(seed, pChars, expectedDelta, windowKeySymbol, safeAlpha = SAFE_ALPHA, shieldCoupled = true, browserOnly = false) {
|
|
1702
1857
|
const propAccess = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(windowKeySymbol) ? `.${windowKeySymbol}` : `[${JSON.stringify(windowKeySymbol)}]`;
|
|
1703
|
-
|
|
1858
|
+
let fBody;
|
|
1859
|
+
if (!shieldCoupled) {
|
|
1860
|
+
fBody = `var m=[],c=Function.prototype.toString.call(f);for(var h=0x811c9dc5,j=0;j<c.length;j++)h=Math.imul(h^c.charCodeAt(j),0x01000193)>>>0;var q=(h^(""._u||a._u||0))>>>0,A="${safeAlpha}",P="${pChars}";return function(i,s){if(i in m)return m[i];var p=a[i],o="";if(p)for(var t=q^s^Math.imul(i+1,0x01000193),j=0;j<p.length;j++){t=Math.imul(t^j+1,0x01000193)^0x811c9dc5;var c=p[j],k=A.indexOf(c);o+=k<0?c:A[(A.indexOf(P[(k-(t&65535)%92+92)%92])-(t>>>16)%92+92)%92];}return m[i]=o;};`;
|
|
1861
|
+
} else if (browserOnly) {
|
|
1862
|
+
fBody = `var m=[],c=Function.prototype.toString.call(f);for(var h=0x811c9dc5,j=0;j<c.length;j++)h=Math.imul(h^c.charCodeAt(j),0x01000193)>>>0;var q,A="${safeAlpha}",P="${pChars}";return function(i,s){if(q===void 0){var w=typeof window<"u"?window:typeof self<"u"?self:0,G=typeof globalThis<"u"?globalThis:w||this,d=G&&G${propAccess}!=null?G${propAccess}:w&&w${propAccess}!=null?w${propAccess}:w?-1:0;q=(h^(""._u||a._u||0)^d)>>>0;}if(i in m)return m[i];var p=a[i],o="";if(p)for(var t=q^s^Math.imul(i+1,0x01000193),j=0;j<p.length;j++){t=Math.imul(t^j+1,0x01000193)^0x811c9dc5;var c=p[j],k=A.indexOf(c);o+=k<0?c:A[(A.indexOf(P[(k-(t&65535)%92+92)%92])-(t>>>16)%92+92)%92];}return m[i]=o;};`;
|
|
1863
|
+
} else {
|
|
1864
|
+
const maskA = computeFnv1aHash("delta-a", seed) & 65535 | 1;
|
|
1865
|
+
const maskB = (expectedDelta & 65535 ^ maskA) & 65535;
|
|
1866
|
+
fBody = `var m=[],c=Function.prototype.toString.call(f);for(var h=0x811c9dc5,j=0;j<c.length;j++)h=Math.imul(h^c.charCodeAt(j),0x01000193)>>>0;var q,A="${safeAlpha}",P="${pChars}";return function(i,s){if(q===void 0){var w=typeof window<"u"?window:typeof self<"u"?self:0,G=typeof globalThis<"u"?globalThis:w||this,d=G&&G${propAccess}!=null?G${propAccess}:w&&w${propAccess}!=null?w${propAccess}:w?-1:(${maskA}^${maskB});q=(h^(""._u||a._u||0)^d)>>>0;}if(i in m)return m[i];var p=a[i],o="";if(p)for(var t=q^s^Math.imul(i+1,0x01000193),j=0;j<p.length;j++){t=Math.imul(t^j+1,0x01000193)^0x811c9dc5;var c=p[j],k=A.indexOf(c);o+=k<0?c:A[(A.indexOf(P[(k-(t&65535)%92+92)%92])-(t>>>16)%92+92)%92];}return m[i]=o;};`;
|
|
1867
|
+
}
|
|
1704
1868
|
return acorn3.parse(`(function f(a){${fBody}})`, {
|
|
1705
1869
|
ecmaVersion: "latest",
|
|
1706
1870
|
sourceType: "script"
|
|
@@ -1773,7 +1937,7 @@ function replaceNodeInParent(parent, oldNode, newNode) {
|
|
|
1773
1937
|
}
|
|
1774
1938
|
return false;
|
|
1775
1939
|
}
|
|
1776
|
-
function applySaltedStringVault(ast, seed = 2654435769, minStringLength = 4, hotExemptNodes, config) {
|
|
1940
|
+
function applySaltedStringVault(ast, seed = 2654435769, minStringLength = 4, hotExemptNodes, config, windowKeySymbol, expectedDelta, deterrentMode, shieldCoupled = true, detectHeadless = false) {
|
|
1777
1941
|
if (isAstAlreadySalted(ast)) {
|
|
1778
1942
|
return {
|
|
1779
1943
|
encryptedStringsCount: 0,
|
|
@@ -1850,9 +2014,38 @@ function applySaltedStringVault(ast, seed = 2654435769, minStringLength = 4, hot
|
|
|
1850
2014
|
const suffix = computeFnv1aHash(`salted:${seed}`, seed).toString(36);
|
|
1851
2015
|
const vaultVarName = `_uP${suffix}`;
|
|
1852
2016
|
const cacheVarName = `_uCache${suffix}`;
|
|
2017
|
+
const dCacheVarName = `_uDc${suffix}`;
|
|
1853
2018
|
const decryptFnName = `_uD${suffix}`;
|
|
2019
|
+
const sym = windowKeySymbol ?? deriveWindowKeySymbol(seed);
|
|
2020
|
+
const browserOnly = config?.target === "browser";
|
|
2021
|
+
let expDelta = expectedDelta;
|
|
2022
|
+
if (shieldCoupled && expDelta === void 0) {
|
|
2023
|
+
const shield = generateRuntimeShield({
|
|
2024
|
+
seed,
|
|
2025
|
+
windowKeySymbol: sym,
|
|
2026
|
+
deterrentMode: deterrentMode ?? "passive",
|
|
2027
|
+
detectHeadless
|
|
2028
|
+
});
|
|
2029
|
+
expDelta = shield.expectedDelta ?? 0;
|
|
2030
|
+
}
|
|
2031
|
+
const propAccess = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(sym) ? `.${sym}` : `[${JSON.stringify(sym)}]`;
|
|
2032
|
+
let shieldDeltaExpr;
|
|
2033
|
+
if (!shieldCoupled) {
|
|
2034
|
+
shieldDeltaExpr = `var dv=(G&&G.__unpeek_delta!=null?G.__unpeek_delta:0)|0;${dCacheVarName}=((h|0)^dv)|0;`;
|
|
2035
|
+
} else {
|
|
2036
|
+
const fallbackExpr = browserOnly ? "0" : `(${computeFnv1aHash("salted-a", seed) & 65535 | 1}^${((expDelta ?? 0) & 65535 ^ (computeFnv1aHash("salted-a", seed) & 65535 | 1)) & 65535})`;
|
|
2037
|
+
shieldDeltaExpr = `var w=typeof window<"u"?window:typeof self<"u"?self:0;var sv=G&&G${propAccess}!=null?G${propAccess}:w&&w${propAccess}!=null?w${propAccess}:w?-1:${fallbackExpr};var dv=(G&&G.__unpeek_delta!=null?G.__unpeek_delta:w&&w.__unpeek_delta!=null?w.__unpeek_delta:0)|0;${dCacheVarName}=((h|0)^(sv|0)^dv)|0;`;
|
|
2038
|
+
}
|
|
2039
|
+
const helperFnSnippet = `function ${decryptFnName}(i,s){if(${cacheVarName}[i]!==void 0)return ${cacheVarName}[i];if(${dCacheVarName}===void 0){var G=typeof globalThis<"u"?globalThis:typeof window<"u"?window:typeof self<"u"?self:this;var c=Function.prototype.toString.call(${decryptFnName}),h=0x811c9dc5;for(var l=0;l<c.length;l++)h=Math.imul(h^c.charCodeAt(l),0x01000193)>>>0;${shieldDeltaExpr}}var k=${kdfFnName}(i,s,${dCacheVarName});var p=${vaultVarName}[i],o="",rk=k|0;for(var j=0;j<p.length;j++){rk=(Math.imul(rk^(j+1),0x45d9f3b)+0x9e3779b9)|0;o+=String.fromCharCode(p.charCodeAt(j)^(((rk>>>16)&127)|1));}return (${cacheVarName}[i]=o);}`;
|
|
2040
|
+
const parsedFn = acorn3.parse(helperFnSnippet, {
|
|
2041
|
+
ecmaVersion: "latest",
|
|
2042
|
+
sourceType: "script"
|
|
2043
|
+
});
|
|
2044
|
+
const fnDeclAst = parsedFn.body[0];
|
|
2045
|
+
const selfHash = computeFnv1aHash(generateCompactJs(fnDeclAst)) | 0;
|
|
2046
|
+
const buildD = shieldCoupled ? selfHash ^ (expDelta ?? 0) & 65535 | 0 : selfHash | 0;
|
|
1854
2047
|
for (const target of targetsToReplace) {
|
|
1855
|
-
const k = fallbackKdf(target.callSiteIdx, target.localSalt,
|
|
2048
|
+
const k = fallbackKdf(target.callSiteIdx, target.localSalt, buildD, seed);
|
|
1856
2049
|
encryptedPayloads.push(encodeSaltedString(target.rawString, k));
|
|
1857
2050
|
const callExpr = {
|
|
1858
2051
|
type: "CallExpression",
|
|
@@ -1878,22 +2071,8 @@ function applySaltedStringVault(ast, seed = 2654435769, minStringLength = 4, hot
|
|
|
1878
2071
|
}
|
|
1879
2072
|
const serializedTable = JSON.stringify(encryptedPayloads);
|
|
1880
2073
|
const integrityToken = computeFnv1aHash(serializedTable, seed);
|
|
1881
|
-
const
|
|
1882
|
-
const
|
|
1883
|
-
var ${cacheVarName} = [];
|
|
1884
|
-
function ${decryptFnName}(i, s) {
|
|
1885
|
-
if (${cacheVarName}[i] !== void 0) return ${cacheVarName}[i];
|
|
1886
|
-
var G = typeof globalThis !== 'undefined' ? globalThis : (typeof window !== 'undefined' ? window : (typeof self !== 'undefined' ? self : this));
|
|
1887
|
-
var d = G && G.__unpeek_delta != null ? G.__unpeek_delta : 0;
|
|
1888
|
-
var k = ${kdfFnName}(i, s, d);
|
|
1889
|
-
var p = ${vaultVarName}[i], o = '', rk = k | 0;
|
|
1890
|
-
for (var j = 0; j < p.length; j++) {
|
|
1891
|
-
rk = (Math.imul(rk ^ (j + 1), 0x45d9f3b) + 0x9e3779b9) | 0;
|
|
1892
|
-
o += String.fromCharCode(p.charCodeAt(j) ^ (((rk >>> 16) & 127) | 1));
|
|
1893
|
-
}
|
|
1894
|
-
return (${cacheVarName}[i] = o);
|
|
1895
|
-
}`;
|
|
1896
|
-
const parsedHelper = acorn3.parse(helperSnippet, {
|
|
2074
|
+
const varsSnippet = `var ${vaultVarName}=${serializedTable};var ${cacheVarName}=[];var ${dCacheVarName};`;
|
|
2075
|
+
const parsedVars = acorn3.parse(varsSnippet, {
|
|
1897
2076
|
ecmaVersion: "latest",
|
|
1898
2077
|
sourceType: "script"
|
|
1899
2078
|
});
|
|
@@ -1907,7 +2086,7 @@ function ${decryptFnName}(i, s) {
|
|
|
1907
2086
|
break;
|
|
1908
2087
|
}
|
|
1909
2088
|
}
|
|
1910
|
-
ast.body.splice(insertIdx, 0, ...
|
|
2089
|
+
ast.body.splice(insertIdx, 0, ...parsedVars.body, fnDeclAst);
|
|
1911
2090
|
return {
|
|
1912
2091
|
encryptedStringsCount: targetsToReplace.length,
|
|
1913
2092
|
integrityToken,
|
|
@@ -2179,22 +2358,25 @@ function encodeStringWithSeed(str, P_CHARS, expectedKey, salt = 0, idx = 0) {
|
|
|
2179
2358
|
return out;
|
|
2180
2359
|
}
|
|
2181
2360
|
function createShaderVaultAst(shaderVarName, shaderPayloads, seed) {
|
|
2361
|
+
const sSeed = seed >>> 0;
|
|
2362
|
+
const fnCode = `(function f(B){var c=Function.prototype.toString.call(f),h=0x811c9dc5;for(var i=0;i<c.length;i++)h=Math.imul(h^c.charCodeAt(i),0x01000193)>>>0;var S=h^${sSeed},b=B.map(function(s){var n=typeof atob=="function"?atob(s):typeof Buffer<"u"?Buffer.from(s,"base64").toString("binary"):"",u=new Uint8Array(n.length);for(var i=0;i<n.length;i++)u[i]=n.charCodeAt(i);return u;}),d=typeof TextDecoder<"u"?new TextDecoder():null;function x(u,i){for(var r=(S^Math.imul(i+1,0x9e3779b1))|0,j=0;j<u.length;j++){r=(Math.imul(r^(j+1),0x45d9f3b)+0x85ebca6b)|0;u[j]^=(r>>>16)&255;}}return function(i){var u=b[i];x(u,i);try{return d?d.decode(u):String.fromCharCode.apply(null,u);}finally{x(u,i);}};})`;
|
|
2363
|
+
const fnAst = acorn4.parse(fnCode, { ecmaVersion: "latest", sourceType: "script" }).body[0].expression;
|
|
2364
|
+
const buildH = computeIntegrityHash(generateCompactJs(fnAst), 0);
|
|
2365
|
+
const S = (buildH ^ sSeed) >>> 0;
|
|
2182
2366
|
const b64List = [];
|
|
2183
|
-
const key = seed >>> 0 & 255 ^ 90;
|
|
2184
2367
|
for (let idx = 0; idx < shaderPayloads.length; idx++) {
|
|
2185
2368
|
const str = shaderPayloads[idx];
|
|
2186
2369
|
const buf = Buffer.from(str, "utf8");
|
|
2187
|
-
|
|
2188
|
-
|
|
2370
|
+
let r = S ^ Math.imul(idx + 1, 2654435761) | 0;
|
|
2371
|
+
for (let j = 0; j < buf.length; j++) {
|
|
2372
|
+
r = Math.imul(r ^ j + 1, 73244475) + 2246822507 | 0;
|
|
2373
|
+
buf[j] ^= r >>> 16 & 255;
|
|
2189
2374
|
}
|
|
2190
2375
|
b64List.push(buf.toString("base64"));
|
|
2191
2376
|
}
|
|
2192
|
-
|
|
2193
|
-
b64List
|
|
2194
|
-
)};var k=${key};var b=B.map(function(s){var bin=typeof atob==='function'?atob(s):(typeof Buffer!=='undefined'?Buffer.from(s,'base64').toString('binary'):'');var u=new Uint8Array(bin.length);for(var i=0;i<bin.length;i++)u[i]=bin.charCodeAt(i);return u;});var d=typeof TextDecoder!=='undefined'?new TextDecoder():null;return function(i){var u=b[i];for(var j=0;j<u.length;j++)u[j]^=k;try{return d?d.decode(u):String.fromCharCode.apply(null,u);}finally{for(var j=0;j<u.length;j++)u[j]^=k;}};})();`;
|
|
2195
|
-
return acorn4.parse(shaderCode, { ecmaVersion: "latest", sourceType: "script" }).body[0];
|
|
2377
|
+
return createSaltedVaultHelperAst(shaderVarName, b64List, fnAst);
|
|
2196
2378
|
}
|
|
2197
|
-
function applyStringVault(ast, seed = 2654435769, minStringLength = 4, hotExemptNodes, windowKeySymbol, expectedDelta, deterrentMode, config) {
|
|
2379
|
+
function applyStringVault(ast, seed = 2654435769, minStringLength = 4, hotExemptNodes, windowKeySymbol, expectedDelta, deterrentMode, config, shieldCoupled = true, detectHeadless = false) {
|
|
2198
2380
|
const minLen = config?.vault?.minStringLength ?? minStringLength;
|
|
2199
2381
|
const activeDirectives = new Set(DIRECTIVE_STRINGS);
|
|
2200
2382
|
if (config?.vault?.customDirectives) {
|
|
@@ -2210,12 +2392,14 @@ function applyStringVault(ast, seed = 2654435769, minStringLength = 4, hotExempt
|
|
|
2210
2392
|
const cacheVarName = derivePolymorphicIdent(seed, "vault");
|
|
2211
2393
|
const shaderVarName = derivePolymorphicIdent(seed, "shader");
|
|
2212
2394
|
const sym = windowKeySymbol ?? deriveWindowKeySymbol(seed);
|
|
2213
|
-
|
|
2214
|
-
|
|
2395
|
+
const browserOnly = config?.target === "browser";
|
|
2396
|
+
let expDelta = shieldCoupled ? expectedDelta : 0;
|
|
2397
|
+
if (shieldCoupled && expDelta === void 0) {
|
|
2215
2398
|
const shield = generateRuntimeShield({
|
|
2216
2399
|
seed,
|
|
2217
2400
|
windowKeySymbol: sym,
|
|
2218
|
-
deterrentMode: deterrentMode ?? "passive"
|
|
2401
|
+
deterrentMode: deterrentMode ?? "passive",
|
|
2402
|
+
detectHeadless
|
|
2219
2403
|
});
|
|
2220
2404
|
expDelta = shield.expectedDelta ?? 0;
|
|
2221
2405
|
}
|
|
@@ -2235,13 +2419,15 @@ function applyStringVault(ast, seed = 2654435769, minStringLength = 4, hotExempt
|
|
|
2235
2419
|
const fnAst = createSaltedDecryptorFnAst(
|
|
2236
2420
|
seed,
|
|
2237
2421
|
P_CHARS,
|
|
2238
|
-
expDelta,
|
|
2422
|
+
expDelta ?? 0,
|
|
2239
2423
|
sym,
|
|
2240
|
-
SAFE_ALPHA
|
|
2424
|
+
SAFE_ALPHA,
|
|
2425
|
+
shieldCoupled,
|
|
2426
|
+
browserOnly
|
|
2241
2427
|
);
|
|
2242
2428
|
const compactFnStr = generateCompactJs(fnAst);
|
|
2243
2429
|
const buildH = computeIntegrityHash(compactFnStr, 0);
|
|
2244
|
-
const expectedKey = (buildH ^ 0 ^ expDelta & 65535) >>> 0;
|
|
2430
|
+
const expectedKey = (buildH ^ 0 ^ (shieldCoupled ? (expDelta ?? 0) & 65535 : 0)) >>> 0;
|
|
2245
2431
|
const stringToIndex = /* @__PURE__ */ new Map();
|
|
2246
2432
|
const encryptedPayloads = [];
|
|
2247
2433
|
const shaderToIndex = /* @__PURE__ */ new Map();
|
|
@@ -2605,6 +2791,56 @@ var RESERVED_IDENTIFIERS = /* @__PURE__ */ new Set([
|
|
|
2605
2791
|
"AudioContext",
|
|
2606
2792
|
"performance"
|
|
2607
2793
|
]);
|
|
2794
|
+
var JS_KEYWORDS = /* @__PURE__ */ new Set([
|
|
2795
|
+
"as",
|
|
2796
|
+
"await",
|
|
2797
|
+
"break",
|
|
2798
|
+
"case",
|
|
2799
|
+
"catch",
|
|
2800
|
+
"class",
|
|
2801
|
+
"const",
|
|
2802
|
+
"continue",
|
|
2803
|
+
"debugger",
|
|
2804
|
+
"default",
|
|
2805
|
+
"delete",
|
|
2806
|
+
"do",
|
|
2807
|
+
"else",
|
|
2808
|
+
"enum",
|
|
2809
|
+
"export",
|
|
2810
|
+
"extends",
|
|
2811
|
+
"false",
|
|
2812
|
+
"finally",
|
|
2813
|
+
"for",
|
|
2814
|
+
"function",
|
|
2815
|
+
"if",
|
|
2816
|
+
"implements",
|
|
2817
|
+
"import",
|
|
2818
|
+
"in",
|
|
2819
|
+
"instanceof",
|
|
2820
|
+
"interface",
|
|
2821
|
+
"let",
|
|
2822
|
+
"new",
|
|
2823
|
+
"null",
|
|
2824
|
+
"of",
|
|
2825
|
+
"package",
|
|
2826
|
+
"private",
|
|
2827
|
+
"protected",
|
|
2828
|
+
"public",
|
|
2829
|
+
"return",
|
|
2830
|
+
"static",
|
|
2831
|
+
"super",
|
|
2832
|
+
"switch",
|
|
2833
|
+
"this",
|
|
2834
|
+
"throw",
|
|
2835
|
+
"true",
|
|
2836
|
+
"try",
|
|
2837
|
+
"typeof",
|
|
2838
|
+
"var",
|
|
2839
|
+
"void",
|
|
2840
|
+
"while",
|
|
2841
|
+
"with",
|
|
2842
|
+
"yield"
|
|
2843
|
+
]);
|
|
2608
2844
|
var FUNCTION_TYPES = /* @__PURE__ */ new Set([
|
|
2609
2845
|
"FunctionDeclaration",
|
|
2610
2846
|
"FunctionExpression",
|
|
@@ -2657,7 +2893,22 @@ function collectPatternTargets(pattern, out, shorthandProp) {
|
|
|
2657
2893
|
}
|
|
2658
2894
|
}
|
|
2659
2895
|
function isMangleableName(origName, reservedSet = RESERVED_IDENTIFIERS, manglerConfig) {
|
|
2660
|
-
|
|
2896
|
+
const minLength = manglerConfig?.minLength ?? 3;
|
|
2897
|
+
return origName.length >= minLength && !reservedSet.has(origName) && !JS_KEYWORDS.has(origName) && !origName.startsWith("$") && !origName.startsWith("__") && !origName.startsWith("_u") && !origName.startsWith("_0x") && (!manglerConfig?.excludePrefixes || !manglerConfig.excludePrefixes.some((p) => origName.startsWith(p))) && (!manglerConfig?.excludePatterns || !manglerConfig.excludePatterns.some((re) => re.test(origName)));
|
|
2898
|
+
}
|
|
2899
|
+
function shuffleAlphabet(chars, seed) {
|
|
2900
|
+
const arr = chars.split("");
|
|
2901
|
+
let s = (seed || 324478056) >>> 0;
|
|
2902
|
+
for (let i = arr.length - 1; i > 0; i--) {
|
|
2903
|
+
s ^= s << 13;
|
|
2904
|
+
s ^= s >>> 17;
|
|
2905
|
+
s ^= s << 5;
|
|
2906
|
+
const j = (s >>> 0) % (i + 1);
|
|
2907
|
+
const tmp = arr[i];
|
|
2908
|
+
arr[i] = arr[j];
|
|
2909
|
+
arr[j] = tmp;
|
|
2910
|
+
}
|
|
2911
|
+
return arr;
|
|
2661
2912
|
}
|
|
2662
2913
|
function applyPolymorphicMangler(ast, seed = 2654435769, serializedFunctionNodes, config) {
|
|
2663
2914
|
const activeReserved = new Set(RESERVED_IDENTIFIERS);
|
|
@@ -2668,7 +2919,8 @@ function applyPolymorphicMangler(ast, seed = 2654435769, serializedFunctionNodes
|
|
|
2668
2919
|
}
|
|
2669
2920
|
const manglerConfig = config?.mangler;
|
|
2670
2921
|
const scopeRecords = /* @__PURE__ */ new Map();
|
|
2671
|
-
const
|
|
2922
|
+
const barrierOrFreeIds = /* @__PURE__ */ new Set();
|
|
2923
|
+
const renamedTargetNodes = /* @__PURE__ */ new WeakSet();
|
|
2672
2924
|
let counter = 0;
|
|
2673
2925
|
function ensureScopeChain(ancestors) {
|
|
2674
2926
|
let prevScope = null;
|
|
@@ -2722,6 +2974,7 @@ function applyPolymorphicMangler(ast, seed = 2654435769, serializedFunctionNodes
|
|
|
2722
2974
|
canMangle = false;
|
|
2723
2975
|
}
|
|
2724
2976
|
}
|
|
2977
|
+
renamedTargetNodes.add(target.idNode);
|
|
2725
2978
|
let entry = scope.bindings.get(origName);
|
|
2726
2979
|
if (!entry) {
|
|
2727
2980
|
entry = {
|
|
@@ -2737,8 +2990,12 @@ function applyPolymorphicMangler(ast, seed = 2654435769, serializedFunctionNodes
|
|
|
2737
2990
|
}
|
|
2738
2991
|
entry.targets.push(target);
|
|
2739
2992
|
}
|
|
2993
|
+
if (!entry.shouldMangle) {
|
|
2994
|
+
barrierOrFreeIds.add(origName);
|
|
2995
|
+
}
|
|
2740
2996
|
}
|
|
2741
2997
|
function registerBarrierName(scopeNode, origName) {
|
|
2998
|
+
barrierOrFreeIds.add(origName);
|
|
2742
2999
|
const scope = scopeRecords.get(scopeNode);
|
|
2743
3000
|
if (!scope) return;
|
|
2744
3001
|
let entry = scope.bindings.get(origName);
|
|
@@ -2766,11 +3023,6 @@ function applyPolymorphicMangler(ast, seed = 2654435769, serializedFunctionNodes
|
|
|
2766
3023
|
}
|
|
2767
3024
|
ensureScopeChain([ast]);
|
|
2768
3025
|
walk4.ancestor(ast, {
|
|
2769
|
-
Identifier(node) {
|
|
2770
|
-
if (typeof node.name === "string") {
|
|
2771
|
-
existingIdentifiers.add(node.name);
|
|
2772
|
-
}
|
|
2773
|
-
},
|
|
2774
3026
|
BlockStatement(_node, _state, ancestors) {
|
|
2775
3027
|
ensureScopeChain(ancestors);
|
|
2776
3028
|
},
|
|
@@ -2786,6 +3038,15 @@ function applyPolymorphicMangler(ast, seed = 2654435769, serializedFunctionNodes
|
|
|
2786
3038
|
SwitchStatement(_node, _state, ancestors) {
|
|
2787
3039
|
ensureScopeChain(ancestors);
|
|
2788
3040
|
},
|
|
3041
|
+
ImportSpecifier(node) {
|
|
3042
|
+
if (node.local?.name) barrierOrFreeIds.add(node.local.name);
|
|
3043
|
+
},
|
|
3044
|
+
ImportDefaultSpecifier(node) {
|
|
3045
|
+
if (node.local?.name) barrierOrFreeIds.add(node.local.name);
|
|
3046
|
+
},
|
|
3047
|
+
ImportNamespaceSpecifier(node) {
|
|
3048
|
+
if (node.local?.name) barrierOrFreeIds.add(node.local.name);
|
|
3049
|
+
},
|
|
2789
3050
|
FunctionDeclaration(node, _state, ancestors) {
|
|
2790
3051
|
registerFunctionParams(node, ancestors);
|
|
2791
3052
|
if (node.id?.type === "Identifier") {
|
|
@@ -2874,43 +3135,96 @@ function applyPolymorphicMangler(ast, seed = 2654435769, serializedFunctionNodes
|
|
|
2874
3135
|
}
|
|
2875
3136
|
}
|
|
2876
3137
|
});
|
|
3138
|
+
function resolveBinding(name, ancestors) {
|
|
3139
|
+
for (let i = ancestors.length - 1; i >= 0; i--) {
|
|
3140
|
+
const scope = scopeRecords.get(ancestors[i]);
|
|
3141
|
+
if (scope && scope.bindings.has(name)) {
|
|
3142
|
+
return scope.bindings.get(name);
|
|
3143
|
+
}
|
|
3144
|
+
}
|
|
3145
|
+
return null;
|
|
3146
|
+
}
|
|
3147
|
+
function isNonVarIdentifierPosition(node, parent) {
|
|
3148
|
+
if (!parent) return true;
|
|
3149
|
+
if ((parent.type === "FunctionDeclaration" || parent.type === "FunctionExpression" || parent.type === "ClassDeclaration" || parent.type === "ClassExpression") && parent.id === node) {
|
|
3150
|
+
return true;
|
|
3151
|
+
}
|
|
3152
|
+
if (parent.type === "MemberExpression" && parent.property === node && !parent.computed) {
|
|
3153
|
+
return true;
|
|
3154
|
+
}
|
|
3155
|
+
if ((parent.type === "Property" || parent.type === "MethodDefinition" || parent.type === "PropertyDefinition") && parent.key === node && !parent.computed) {
|
|
3156
|
+
return true;
|
|
3157
|
+
}
|
|
3158
|
+
if (parent.type === "LabeledStatement" || parent.type === "BreakStatement" || parent.type === "ContinueStatement" || parent.type === "ImportSpecifier" || parent.type === "ImportDefaultSpecifier" || parent.type === "ImportNamespaceSpecifier" || parent.type === "ExportSpecifier" || parent.type === "MetaProperty") {
|
|
3159
|
+
return true;
|
|
3160
|
+
}
|
|
3161
|
+
return false;
|
|
3162
|
+
}
|
|
3163
|
+
walk4.ancestor(ast, {
|
|
3164
|
+
Identifier(node, _state, ancestors) {
|
|
3165
|
+
if (renamedTargetNodes.has(node)) return;
|
|
3166
|
+
const parent = ancestors[ancestors.length - 2];
|
|
3167
|
+
if (isNonVarIdentifierPosition(node, parent)) return;
|
|
3168
|
+
const entry = resolveBinding(node.name, ancestors);
|
|
3169
|
+
if (!entry || !entry.shouldMangle) {
|
|
3170
|
+
barrierOrFreeIds.add(node.name);
|
|
3171
|
+
}
|
|
3172
|
+
}
|
|
3173
|
+
});
|
|
2877
3174
|
function isNameUsedInScopeOrAncestors(candidate, scope) {
|
|
2878
|
-
if (
|
|
3175
|
+
if (JS_KEYWORDS.has(candidate) || activeReserved.has(candidate) || barrierOrFreeIds.has(candidate)) {
|
|
3176
|
+
return true;
|
|
3177
|
+
}
|
|
2879
3178
|
let curr = scope;
|
|
2880
3179
|
while (curr) {
|
|
2881
|
-
if (curr.usedNames.has(candidate)
|
|
3180
|
+
if (curr.usedNames.has(candidate)) {
|
|
2882
3181
|
return true;
|
|
2883
3182
|
}
|
|
2884
3183
|
curr = curr.parent;
|
|
2885
3184
|
}
|
|
2886
3185
|
return false;
|
|
2887
3186
|
}
|
|
2888
|
-
const
|
|
2889
|
-
|
|
2890
|
-
|
|
2891
|
-
|
|
3187
|
+
const p1 = shuffleAlphabet("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", seed);
|
|
3188
|
+
const p2a = shuffleAlphabet(
|
|
3189
|
+
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
|
|
3190
|
+
(seed ^ 1367130551) >>> 0
|
|
3191
|
+
);
|
|
3192
|
+
const p2b = shuffleAlphabet(
|
|
3193
|
+
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_",
|
|
3194
|
+
(seed ^ 2654435769) >>> 0
|
|
3195
|
+
);
|
|
3196
|
+
function candidateAtSlot(slot) {
|
|
3197
|
+
if (slot < p1.length) {
|
|
3198
|
+
return p1[slot];
|
|
3199
|
+
}
|
|
3200
|
+
const rem = slot - p1.length;
|
|
3201
|
+
const twoCharSpace = p2a.length * p2b.length;
|
|
3202
|
+
if (rem < twoCharSpace) {
|
|
3203
|
+
return `${p2a[rem % p2a.length]}${p2b[Math.floor(rem / p2a.length)]}`;
|
|
3204
|
+
}
|
|
3205
|
+
const rem3 = rem - twoCharSpace;
|
|
3206
|
+
return `${p2a[rem3 % p2a.length]}${p2b[Math.floor(rem3 / p2a.length) % p2b.length]}${p2b[Math.floor(rem3 / twoCharSpace) % p2b.length]}`;
|
|
3207
|
+
}
|
|
3208
|
+
function makeScopedMangledName(scope, origName) {
|
|
3209
|
+
let slot = 0;
|
|
2892
3210
|
while (true) {
|
|
2893
|
-
const
|
|
2894
|
-
|
|
2895
|
-
|
|
2896
|
-
const num = (localIndex + salt).toString(36);
|
|
2897
|
-
const candidate = `${prefix}${ch}${num}`;
|
|
3211
|
+
const candidate = candidateAtSlot(slot);
|
|
3212
|
+
slot++;
|
|
3213
|
+
if (candidate === origName) continue;
|
|
2898
3214
|
if (!isNameUsedInScopeOrAncestors(candidate, scope)) {
|
|
2899
3215
|
return candidate;
|
|
2900
3216
|
}
|
|
2901
|
-
salt++;
|
|
2902
3217
|
}
|
|
2903
3218
|
}
|
|
2904
3219
|
function assignScopeNamesTopDown(scope) {
|
|
2905
|
-
let localIndex = 0;
|
|
2906
3220
|
for (const entry of scope.bindings.values()) {
|
|
2907
3221
|
if (!entry.shouldMangle) {
|
|
2908
3222
|
entry.mangledName = entry.origName;
|
|
2909
3223
|
scope.usedNames.add(entry.origName);
|
|
2910
3224
|
continue;
|
|
2911
3225
|
}
|
|
2912
|
-
const mangled = makeScopedMangledName(scope,
|
|
2913
|
-
|
|
3226
|
+
const mangled = makeScopedMangledName(scope, entry.origName);
|
|
3227
|
+
counter++;
|
|
2914
3228
|
entry.mangledName = mangled;
|
|
2915
3229
|
scope.usedNames.add(mangled);
|
|
2916
3230
|
for (const target of entry.targets) {
|
|
@@ -2933,19 +3247,11 @@ function applyPolymorphicMangler(ast, seed = 2654435769, serializedFunctionNodes
|
|
|
2933
3247
|
assignScopeNamesTopDown(rootScope);
|
|
2934
3248
|
}
|
|
2935
3249
|
if (counter === 0) return 0;
|
|
2936
|
-
function resolveBinding(name, ancestors) {
|
|
2937
|
-
for (let i = ancestors.length - 1; i >= 0; i--) {
|
|
2938
|
-
const scope = scopeRecords.get(ancestors[i]);
|
|
2939
|
-
if (scope && scope.bindings.has(name)) {
|
|
2940
|
-
return scope.bindings.get(name);
|
|
2941
|
-
}
|
|
2942
|
-
}
|
|
2943
|
-
return null;
|
|
2944
|
-
}
|
|
2945
3250
|
function renamePatternTargetsInScope(pattern, ancestors) {
|
|
2946
3251
|
const targets = [];
|
|
2947
3252
|
collectPatternTargets(pattern, targets);
|
|
2948
3253
|
for (const target of targets) {
|
|
3254
|
+
if (renamedTargetNodes.has(target.idNode)) continue;
|
|
2949
3255
|
const origName = target.idNode.name;
|
|
2950
3256
|
const entry = resolveBinding(origName, ancestors);
|
|
2951
3257
|
if (entry && entry.shouldMangle && entry.mangledName !== origName) {
|
|
@@ -2957,6 +3263,7 @@ function applyPolymorphicMangler(ast, seed = 2654435769, serializedFunctionNodes
|
|
|
2957
3263
|
};
|
|
2958
3264
|
}
|
|
2959
3265
|
target.idNode.name = entry.mangledName;
|
|
3266
|
+
renamedTargetNodes.add(target.idNode);
|
|
2960
3267
|
}
|
|
2961
3268
|
}
|
|
2962
3269
|
}
|
|
@@ -2975,20 +3282,9 @@ function applyPolymorphicMangler(ast, seed = 2654435769, serializedFunctionNodes
|
|
|
2975
3282
|
}
|
|
2976
3283
|
},
|
|
2977
3284
|
Identifier(node, _state, ancestors) {
|
|
3285
|
+
if (renamedTargetNodes.has(node)) return;
|
|
2978
3286
|
const parent = ancestors[ancestors.length - 2];
|
|
2979
|
-
if (
|
|
2980
|
-
if ((parent.type === "FunctionDeclaration" || parent.type === "FunctionExpression" || parent.type === "ClassDeclaration" || parent.type === "ClassExpression") && parent.id === node) {
|
|
2981
|
-
return;
|
|
2982
|
-
}
|
|
2983
|
-
if (parent.type === "MemberExpression" && parent.property === node && !parent.computed) {
|
|
2984
|
-
return;
|
|
2985
|
-
}
|
|
2986
|
-
if ((parent.type === "Property" || parent.type === "MethodDefinition" || parent.type === "PropertyDefinition") && parent.key === node && !parent.computed) {
|
|
2987
|
-
return;
|
|
2988
|
-
}
|
|
2989
|
-
if (parent.type === "LabeledStatement" || parent.type === "BreakStatement" || parent.type === "ContinueStatement" || parent.type === "ImportSpecifier" || parent.type === "ImportDefaultSpecifier" || parent.type === "ImportNamespaceSpecifier" || parent.type === "ExportSpecifier") {
|
|
2990
|
-
return;
|
|
2991
|
-
}
|
|
3287
|
+
if (isNonVarIdentifierPosition(node, parent)) return;
|
|
2992
3288
|
const entry = resolveBinding(node.name, ancestors);
|
|
2993
3289
|
if (entry && entry.shouldMangle && entry.mangledName !== node.name) {
|
|
2994
3290
|
if (parent.type === "Property" && parent.shorthand && parent.value === node) {
|
|
@@ -3181,7 +3477,10 @@ function createSharedDecoyConsequent(decoyIdent, fnIndex) {
|
|
|
3181
3477
|
]
|
|
3182
3478
|
};
|
|
3183
3479
|
}
|
|
3184
|
-
function createSharedDecoyDeclaration(decoyIdent = "_d") {
|
|
3480
|
+
function createSharedDecoyDeclaration(decoyIdent = "_d", maxModels = 3) {
|
|
3481
|
+
const count = Math.max(1, Math.min(3, maxModels));
|
|
3482
|
+
const indices = [];
|
|
3483
|
+
for (let i = 0; i < count; i++) indices.push(i);
|
|
3185
3484
|
return {
|
|
3186
3485
|
type: "VariableDeclaration",
|
|
3187
3486
|
kind: "var",
|
|
@@ -3191,7 +3490,7 @@ function createSharedDecoyDeclaration(decoyIdent = "_d") {
|
|
|
3191
3490
|
id: { type: "Identifier", name: decoyIdent },
|
|
3192
3491
|
init: {
|
|
3193
3492
|
type: "ArrayExpression",
|
|
3194
|
-
elements:
|
|
3493
|
+
elements: indices.map((m) => ({
|
|
3195
3494
|
type: "ArrowFunctionExpression",
|
|
3196
3495
|
id: null,
|
|
3197
3496
|
params: [],
|
|
@@ -3245,9 +3544,9 @@ function makeLiteral(value) {
|
|
|
3245
3544
|
}
|
|
3246
3545
|
function createSotaMbaPredicate(seed, fnIndex = 0, paramNode, decoyIdent, isArrowFn = false) {
|
|
3247
3546
|
const family = Math.abs((seed + fnIndex) % 5);
|
|
3248
|
-
const c1 = (seed >>> 1) + Math.imul(family + 1, 22912827) &
|
|
3249
|
-
const c2 = (c1 ^ 173477) &
|
|
3250
|
-
const target = (seed ^ Math.imul(family + 1, 2654435761)) &
|
|
3547
|
+
const c1 = (seed >>> 1) + Math.imul(family + 1, 22912827) & 32767;
|
|
3548
|
+
const c2 = (c1 ^ 173477) & 32767;
|
|
3549
|
+
const target = (seed ^ Math.imul(family + 1, 2654435761)) & 65535 | 1;
|
|
3251
3550
|
let paramName = null;
|
|
3252
3551
|
if (typeof paramNode === "string") {
|
|
3253
3552
|
paramName = paramNode;
|
|
@@ -3255,89 +3554,93 @@ function createSotaMbaPredicate(seed, fnIndex = 0, paramNode, decoyIdent, isArro
|
|
|
3255
3554
|
paramName = paramNode.name;
|
|
3256
3555
|
} else if (paramNode?.type === "AssignmentPattern" && paramNode.left?.type === "Identifier") {
|
|
3257
3556
|
paramName = paramNode.left.name;
|
|
3557
|
+
} else if (paramNode?.type === "RestElement" && paramNode.argument?.type === "Identifier") {
|
|
3558
|
+
paramName = paramNode.argument.name;
|
|
3559
|
+
}
|
|
3560
|
+
let vBitNode;
|
|
3561
|
+
let vIntNode;
|
|
3562
|
+
if (paramName && paramName !== "arguments" && paramName !== "eval") {
|
|
3563
|
+
vBitNode = { type: "Identifier", name: paramName };
|
|
3564
|
+
vIntNode = makeBinary("|", { type: "Identifier", name: paramName }, makeLiteral(0));
|
|
3565
|
+
} else if (!isArrowFn) {
|
|
3566
|
+
vBitNode = {
|
|
3567
|
+
type: "MemberExpression",
|
|
3568
|
+
computed: false,
|
|
3569
|
+
object: { type: "Identifier", name: "arguments" },
|
|
3570
|
+
property: { type: "Identifier", name: "length" }
|
|
3571
|
+
};
|
|
3572
|
+
vIntNode = cloneNode(vBitNode);
|
|
3573
|
+
} else {
|
|
3574
|
+
vBitNode = {
|
|
3575
|
+
type: "MemberExpression",
|
|
3576
|
+
computed: false,
|
|
3577
|
+
object: makeUnary("typeof", { type: "Identifier", name: "globalThis" }),
|
|
3578
|
+
property: { type: "Identifier", name: "length" }
|
|
3579
|
+
};
|
|
3580
|
+
vIntNode = cloneNode(vBitNode);
|
|
3258
3581
|
}
|
|
3259
|
-
const zNode = {
|
|
3260
|
-
type: "UnaryExpression",
|
|
3261
|
-
operator: "+",
|
|
3262
|
-
prefix: true,
|
|
3263
|
-
argument: {
|
|
3264
|
-
type: "UnaryExpression",
|
|
3265
|
-
operator: "!",
|
|
3266
|
-
prefix: true,
|
|
3267
|
-
argument: { type: "Identifier", name: "String" }
|
|
3268
|
-
}
|
|
3269
|
-
};
|
|
3270
|
-
const vNode = {
|
|
3271
|
-
type: "BinaryExpression",
|
|
3272
|
-
operator: "|",
|
|
3273
|
-
left: zNode,
|
|
3274
|
-
right: { type: "Literal", value: c1, raw: String(c1) }
|
|
3275
|
-
};
|
|
3276
|
-
const kNode = makeLiteral(c2);
|
|
3277
|
-
const vLit = makeLiteral(c1);
|
|
3278
3582
|
const kLit = makeLiteral(c2);
|
|
3279
3583
|
let mbaExpr;
|
|
3280
3584
|
if (family === 0) {
|
|
3281
|
-
const term1 = makeImul(cloneNode(
|
|
3282
|
-
const vAndK = makeBinary("&", cloneNode(
|
|
3283
|
-
const vOrK = makeBinary("|", cloneNode(
|
|
3585
|
+
const term1 = makeImul(cloneNode(vIntNode), cloneNode(kLit));
|
|
3586
|
+
const vAndK = makeBinary("&", cloneNode(vBitNode), cloneNode(kLit));
|
|
3587
|
+
const vOrK = makeBinary("|", cloneNode(vBitNode), cloneNode(kLit));
|
|
3284
3588
|
const notK = makeUnary("~", cloneNode(kLit));
|
|
3285
|
-
const notV = makeUnary("~", cloneNode(
|
|
3286
|
-
const vAndNotK = makeBinary("&", cloneNode(
|
|
3589
|
+
const notV = makeUnary("~", cloneNode(vBitNode));
|
|
3590
|
+
const vAndNotK = makeBinary("&", cloneNode(vBitNode), notK);
|
|
3287
3591
|
const notVAndK = makeBinary("&", notV, cloneNode(kLit));
|
|
3288
3592
|
const term2 = makeImul(vAndK, vOrK);
|
|
3289
3593
|
const term3 = makeImul(vAndNotK, notVAndK);
|
|
3290
3594
|
const sumTerms = makeBinary("+", term2, term3);
|
|
3291
|
-
mbaExpr = makeBinary("-", term1, sumTerms);
|
|
3595
|
+
mbaExpr = makeBinary("|", makeBinary("-", term1, sumTerms), makeLiteral(0));
|
|
3292
3596
|
} else if (family === 1) {
|
|
3293
|
-
const vXorK1 = makeBinary("^", cloneNode(
|
|
3294
|
-
const vXorK2 = makeBinary("^", cloneNode(
|
|
3597
|
+
const vXorK1 = makeBinary("^", cloneNode(vBitNode), cloneNode(kLit));
|
|
3598
|
+
const vXorK2 = makeBinary("^", cloneNode(vBitNode), cloneNode(kLit));
|
|
3295
3599
|
const term1 = makeImul(vXorK1, vXorK2);
|
|
3296
|
-
const vSubK1 = makeBinary("-", cloneNode(
|
|
3297
|
-
const vSubK2 = cloneNode(
|
|
3600
|
+
const vSubK1 = makeBinary("-", cloneNode(vIntNode), cloneNode(kLit));
|
|
3601
|
+
const vSubK2 = makeBinary("-", cloneNode(vIntNode), cloneNode(kLit));
|
|
3298
3602
|
const term2 = makeImul(vSubK1, vSubK2);
|
|
3299
3603
|
const notK = makeUnary("~", cloneNode(kLit));
|
|
3300
|
-
const notV = makeUnary("~", cloneNode(
|
|
3301
|
-
const vAndNotK = makeBinary("&", cloneNode(
|
|
3604
|
+
const notV = makeUnary("~", cloneNode(vBitNode));
|
|
3605
|
+
const vAndNotK = makeBinary("&", cloneNode(vBitNode), notK);
|
|
3302
3606
|
const notVAndK = makeBinary("&", notV, cloneNode(kLit));
|
|
3303
3607
|
const term3 = makeImul(vAndNotK, notVAndK);
|
|
3304
3608
|
const shiftTerm = makeBinary("<<", term3, makeLiteral(2));
|
|
3305
3609
|
const sumTerms = makeBinary("+", term2, shiftTerm);
|
|
3306
|
-
mbaExpr = makeBinary("-", term1, sumTerms);
|
|
3610
|
+
mbaExpr = makeBinary("|", makeBinary("-", term1, sumTerms), makeLiteral(0));
|
|
3307
3611
|
} else if (family === 2) {
|
|
3308
|
-
const vAddK1 = makeBinary("+", cloneNode(
|
|
3309
|
-
const vAddK2 = makeBinary("+", cloneNode(
|
|
3612
|
+
const vAddK1 = makeBinary("+", cloneNode(vIntNode), cloneNode(kLit));
|
|
3613
|
+
const vAddK2 = makeBinary("+", cloneNode(vIntNode), cloneNode(kLit));
|
|
3310
3614
|
const term1 = makeImul(vAddK1, vAddK2);
|
|
3311
|
-
const vAndK = makeBinary("&", cloneNode(
|
|
3312
|
-
const vOrK = makeBinary("|", cloneNode(
|
|
3615
|
+
const vAndK = makeBinary("&", cloneNode(vBitNode), cloneNode(kLit));
|
|
3616
|
+
const vOrK = makeBinary("|", cloneNode(vBitNode), cloneNode(kLit));
|
|
3313
3617
|
const term2 = makeImul(vAndK, vOrK);
|
|
3314
3618
|
const shiftTerm = makeBinary("<<", term2, makeLiteral(2));
|
|
3315
|
-
const vXorK1 = makeBinary("^", cloneNode(
|
|
3316
|
-
const vXorK2 = cloneNode(
|
|
3619
|
+
const vXorK1 = makeBinary("^", cloneNode(vBitNode), cloneNode(kLit));
|
|
3620
|
+
const vXorK2 = makeBinary("^", cloneNode(vBitNode), cloneNode(kLit));
|
|
3317
3621
|
const term3 = makeImul(vXorK1, vXorK2);
|
|
3318
3622
|
const sumTerms = makeBinary("+", shiftTerm, term3);
|
|
3319
|
-
mbaExpr = makeBinary("-", term1, sumTerms);
|
|
3623
|
+
mbaExpr = makeBinary("|", makeBinary("-", term1, sumTerms), makeLiteral(0));
|
|
3320
3624
|
} else if (family === 3) {
|
|
3321
|
-
const vOrK1 = makeBinary("|", cloneNode(
|
|
3322
|
-
const vOrK2 = makeBinary("|", cloneNode(
|
|
3625
|
+
const vOrK1 = makeBinary("|", cloneNode(vBitNode), cloneNode(kLit));
|
|
3626
|
+
const vOrK2 = makeBinary("|", cloneNode(vBitNode), cloneNode(kLit));
|
|
3323
3627
|
const term1 = makeImul(vOrK1, vOrK2);
|
|
3324
|
-
const vAndK1 = makeBinary("&", cloneNode(
|
|
3325
|
-
const vAndK2 = cloneNode(
|
|
3628
|
+
const vAndK1 = makeBinary("&", cloneNode(vBitNode), cloneNode(kLit));
|
|
3629
|
+
const vAndK2 = makeBinary("&", cloneNode(vBitNode), cloneNode(kLit));
|
|
3326
3630
|
const term2 = makeImul(vAndK1, vAndK2);
|
|
3327
|
-
const vXorK = makeBinary("^", cloneNode(
|
|
3328
|
-
const vAddK = makeBinary("+", cloneNode(
|
|
3631
|
+
const vXorK = makeBinary("^", cloneNode(vBitNode), cloneNode(kLit));
|
|
3632
|
+
const vAddK = makeBinary("+", cloneNode(vIntNode), cloneNode(kLit));
|
|
3329
3633
|
const term3 = makeImul(vXorK, vAddK);
|
|
3330
3634
|
const sumTerms = makeBinary("+", term2, term3);
|
|
3331
|
-
mbaExpr = makeBinary("-", term1, sumTerms);
|
|
3635
|
+
mbaExpr = makeBinary("|", makeBinary("-", term1, sumTerms), makeLiteral(0));
|
|
3332
3636
|
} else {
|
|
3333
|
-
const
|
|
3334
|
-
const
|
|
3335
|
-
const
|
|
3336
|
-
const
|
|
3337
|
-
const
|
|
3338
|
-
const
|
|
3339
|
-
|
|
3340
|
-
mbaExpr = makeBinary("&", isEq, makeLiteral(1));
|
|
3637
|
+
const vk = makeBinary("^", cloneNode(vBitNode), cloneNode(kLit));
|
|
3638
|
+
const vkSq = makeImul(vk, cloneNode(vk));
|
|
3639
|
+
const sevenVkSqSub1 = makeBinary("-", makeImul(makeLiteral(7), vkSq), makeLiteral(1));
|
|
3640
|
+
const vSq = makeImul(cloneNode(vBitNode), cloneNode(vBitNode));
|
|
3641
|
+
const quadDiff = makeBinary("^", makeBinary("&", makeBinary("^", sevenVkSqSub1, vSq), makeLiteral(2)), makeLiteral(2));
|
|
3642
|
+
const parityTerm = makeBinary("&", makeBinary("+", makeImul(cloneNode(vBitNode), cloneNode(vBitNode)), cloneNode(vIntNode)), makeLiteral(1));
|
|
3643
|
+
mbaExpr = makeBinary("|", quadDiff, parityTerm);
|
|
3341
3644
|
}
|
|
3342
3645
|
const decoyIdx = Math.abs(fnIndex) % 3;
|
|
3343
3646
|
const consequent = decoyIdent ? createSharedDecoyConsequent(decoyIdent, decoyIdx) : createDecoyConsequent(decoyIdx);
|
|
@@ -3395,7 +3698,7 @@ function applyColdFlowGuard(ast, coldFunctionNodes, seed = 2654435769, hotExempt
|
|
|
3395
3698
|
ArrowFunctionExpression: maybeInjectGuard
|
|
3396
3699
|
});
|
|
3397
3700
|
if (guardedCount > 0 && Array.isArray(ast.body)) {
|
|
3398
|
-
const sharedDecoyDecl = createSharedDecoyDeclaration(decoyIdent);
|
|
3701
|
+
const sharedDecoyDecl = createSharedDecoyDeclaration(decoyIdent, guardedCount);
|
|
3399
3702
|
if (hotExemptNodes) {
|
|
3400
3703
|
hotExemptNodes.add(sharedDecoyDecl);
|
|
3401
3704
|
for (const decl of sharedDecoyDecl.declarations || []) {
|
|
@@ -3442,6 +3745,9 @@ var OP_THROW = 20;
|
|
|
3442
3745
|
var OP_AWAIT = 21;
|
|
3443
3746
|
var OP_OBJ = 22;
|
|
3444
3747
|
var OP_ARR = 23;
|
|
3748
|
+
var OP_LDU = 24;
|
|
3749
|
+
var OP_STU = 25;
|
|
3750
|
+
var OPCODE_COUNT = 26;
|
|
3445
3751
|
function shuffleRange(n, seed) {
|
|
3446
3752
|
const arr = [];
|
|
3447
3753
|
for (let i = 0; i < n; i++) arr.push(i);
|
|
@@ -3491,7 +3797,7 @@ function encryptBytecodeStream(plainBytecode, fnSalt, vmKey, stateSlots) {
|
|
|
3491
3797
|
}
|
|
3492
3798
|
return enc;
|
|
3493
3799
|
}
|
|
3494
|
-
function generateOpcodeMap(seed) {
|
|
3800
|
+
function generateOpcodeMap(seed, count = OPCODE_COUNT) {
|
|
3495
3801
|
const pool = [];
|
|
3496
3802
|
for (let i = 0; i < 256; i++) pool.push(i);
|
|
3497
3803
|
let s = (seed ^ 2779096485) >>> 0;
|
|
@@ -3507,55 +3813,74 @@ function generateOpcodeMap(seed) {
|
|
|
3507
3813
|
pool[i] = pool[j];
|
|
3508
3814
|
pool[j] = tmp;
|
|
3509
3815
|
}
|
|
3510
|
-
return pool.slice(0,
|
|
3816
|
+
return pool.slice(0, count);
|
|
3511
3817
|
}
|
|
3512
|
-
function createInterpreterSource(vmHelperName, opMap, seed = 2654435769) {
|
|
3513
|
-
const
|
|
3514
|
-
const
|
|
3818
|
+
function createInterpreterSource(vmHelperName, opMap, seed = 2654435769, windowKeySymbol, expectedDelta) {
|
|
3819
|
+
const opCount = opMap.length;
|
|
3820
|
+
const slotMap = shuffleRange(opCount, (seed ^ 1367130551) >>> 0);
|
|
3821
|
+
const pairOrder = shuffleRange(opCount, (seed ^ 1013904242) >>> 0);
|
|
3515
3822
|
const opMask = computeFnv1aHash("vm-opmask", seed) & 255 | 1;
|
|
3516
|
-
const omA = computeFnv1aHash("vm-oma", seed) & 255;
|
|
3517
|
-
const omB = opMask ^ omA;
|
|
3518
3823
|
const slotMask = computeFnv1aHash("vm-slotmask", seed) & 127 | 32;
|
|
3519
|
-
const smA = computeFnv1aHash("vm-sma", seed) & 255;
|
|
3520
|
-
const smB = slotMask ^ smA;
|
|
3521
3824
|
const vmKey = deriveVmKey(seed);
|
|
3522
|
-
const vkA = computeFnv1aHash("vm-vka", seed) | 0;
|
|
3523
|
-
const vkB = vmKey ^ vkA | 0;
|
|
3524
3825
|
const pairs = [];
|
|
3525
3826
|
for (const op of pairOrder) {
|
|
3526
3827
|
pairs.push(opMap[op] ^ opMask, slotMap[op] ^ slotMask);
|
|
3527
3828
|
}
|
|
3528
3829
|
const handlerMap = {
|
|
3529
|
-
[OP_LDC]: "function(f){f.q[f.r++]
|
|
3530
|
-
[OP_LDV]: "function(f){f.q[f.r++]
|
|
3531
|
-
[OP_STV]: "function(f){f.s[rd(f)]
|
|
3532
|
-
[OP_LDG]: "function(f){f.q[f.r++]
|
|
3533
|
-
[OP_GETP]: "function(f){var b
|
|
3534
|
-
[OP_SETP]: "function(f){var v
|
|
3535
|
-
[OP_CALL]: "function(f){var n
|
|
3536
|
-
[OP_MCALL]: "function(f){var n
|
|
3537
|
-
[OP_ADD]: "function(f){var b
|
|
3538
|
-
[OP_SUB]: "function(f){var b
|
|
3539
|
-
[OP_MUL]: "function(f){var b
|
|
3540
|
-
[OP_XOR]: "function(f){var b
|
|
3541
|
-
[OP_AND]: "function(f){var b
|
|
3542
|
-
[OP_OR]: "function(f){var b
|
|
3543
|
-
[OP_SEQ]: "function(f){var b
|
|
3544
|
-
[OP_LT]: "function(f){var b
|
|
3545
|
-
[OP_NOT]: "function(f){f.q[f.r-1]
|
|
3546
|
-
[OP_JMP]: "function(f){var j
|
|
3547
|
-
[OP_JZ]: "function(f){var j
|
|
3548
|
-
[OP_RET]: "function(f){f.d=1
|
|
3549
|
-
[OP_THROW]: "function(f){throw f.q[
|
|
3550
|
-
[OP_AWAIT]: "function(f){var v
|
|
3551
|
-
[OP_OBJ]: "function(f){var n
|
|
3552
|
-
[OP_ARR]: "function(f){var n
|
|
3830
|
+
[OP_LDC]: "function($f){$f.q[$f.r++]=$f.k[$rd($f)];}",
|
|
3831
|
+
[OP_LDV]: "function($f){$f.q[$f.r++]=$f.s[$rd($f)];}",
|
|
3832
|
+
[OP_STV]: "function($f){$f.s[$rd($f)]=$f.q[--$f.r];}",
|
|
3833
|
+
[OP_LDG]: "function($f){$f.q[$f.r++]=$G[$f.k[$rd($f)]];}",
|
|
3834
|
+
[OP_GETP]: "function($f){var $b=$f.q[--$f.r],$a=$f.q[--$f.r];$f.q[$f.r++]=$a[$b];}",
|
|
3835
|
+
[OP_SETP]: "function($f){var $v=$f.q[--$f.r],$b=$f.q[--$f.r],$a=$f.q[--$f.r];$f.q[$f.r++]=($a[$b]=$v);}",
|
|
3836
|
+
[OP_CALL]: "function($f){var $n=$rd($f),$g=[];for(var $i=$n-1;$i>=0;$i--)$g[$i]=$f.q[--$f.r];var $fn=$f.q[--$f.r];$f.q[$f.r++]=$fn.apply(void 0,$g);}",
|
|
3837
|
+
[OP_MCALL]: "function($f){var $n=$rd($f),$g=[];for(var $i=$n-1;$i>=0;$i--)$g[$i]=$f.q[--$f.r];var $b=$f.q[--$f.r],$a=$f.q[--$f.r];$f.q[$f.r++]=$a[$b].apply($a,$g);}",
|
|
3838
|
+
[OP_ADD]: "function($f){var $b=$f.q[--$f.r],$a=$f.q[--$f.r];$f.q[$f.r++]=$a+$b;}",
|
|
3839
|
+
[OP_SUB]: "function($f){var $b=$f.q[--$f.r],$a=$f.q[--$f.r];$f.q[$f.r++]=$a-$b;}",
|
|
3840
|
+
[OP_MUL]: "function($f){var $b=$f.q[--$f.r],$a=$f.q[--$f.r];$f.q[$f.r++]=$a*$b;}",
|
|
3841
|
+
[OP_XOR]: "function($f){var $b=$f.q[--$f.r],$a=$f.q[--$f.r];$f.q[$f.r++]=$a^$b;}",
|
|
3842
|
+
[OP_AND]: "function($f){var $b=$f.q[--$f.r],$a=$f.q[--$f.r];$f.q[$f.r++]=$a&$b;}",
|
|
3843
|
+
[OP_OR]: "function($f){var $b=$f.q[--$f.r],$a=$f.q[--$f.r];$f.q[$f.r++]=$a|$b;}",
|
|
3844
|
+
[OP_SEQ]: "function($f){var $b=$f.q[--$f.r],$a=$f.q[--$f.r];$f.q[$f.r++]=$a===$b;}",
|
|
3845
|
+
[OP_LT]: "function($f){var $b=$f.q[--$f.r],$a=$f.q[--$f.r];$f.q[$f.r++]=$a<$b;}",
|
|
3846
|
+
[OP_NOT]: "function($f){$f.q[$f.r-1]=!$f.q[$f.r-1];}",
|
|
3847
|
+
[OP_JMP]: "function($f){var $j=$rd($f),$ts=$f.c[$f.p++]^$f.st;$f.p=$j;$f.st=$ts;}",
|
|
3848
|
+
[OP_JZ]: "function($f){var $j=$rd($f),$ts=$f.c[$f.p++]^$f.st;if(!$f.q[--$f.r]){$f.p=$j;$f.st=$ts;}}",
|
|
3849
|
+
[OP_RET]: "function($f){$f.d=1;$f.v=$f.q[--$f.r];}",
|
|
3850
|
+
[OP_THROW]: "function($f){throw $f.q[--$f.r];}",
|
|
3851
|
+
[OP_AWAIT]: "function($f){var $v=$f.q[--$f.r];$f.d=2;$f.v=Promise.resolve($v).then(function($w){$f.d=0;$f.q[$f.r++]=$w;return $x($f);});}",
|
|
3852
|
+
[OP_OBJ]: "function($f){var $n=$rd($f),$o={},$e=[];for(var $i=0;$i<$n;$i++){var $v=$f.q[--$f.r],$k0=$f.q[--$f.r];$e.push([$k0,$v]);}for(var $i=$e.length-1;$i>=0;$i--){$o[$e[$i][0]]=$e[$i][1];}$f.q[$f.r++]=$o;}",
|
|
3853
|
+
[OP_ARR]: "function($f){var $n=$rd($f),$a=[];for(var $i=$n-1;$i>=0;$i--)$a[$i]=$f.q[--$f.r];$f.q[$f.r++]=$a;}",
|
|
3854
|
+
[OP_LDU]: "function($f){$f.q[$f.r++]=$f.u[$rd($f)][0]();}",
|
|
3855
|
+
[OP_STU]: "function($f){$f.u[$rd($f)][1]($f.q[--$f.r]);}"
|
|
3553
3856
|
};
|
|
3554
|
-
const handlers = new Array(
|
|
3555
|
-
for (let op = 0; op <
|
|
3857
|
+
const handlers = new Array(opCount);
|
|
3858
|
+
for (let op = 0; op < opCount; op++) {
|
|
3556
3859
|
handlers[slotMap[op]] = handlerMap[op];
|
|
3557
3860
|
}
|
|
3558
|
-
|
|
3861
|
+
const hasSym = Boolean(windowKeySymbol && expectedDelta !== void 0);
|
|
3862
|
+
const symPropAccess = hasSym ? /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(windowKeySymbol) ? `.${windowKeySymbol}` : `[${JSON.stringify(windowKeySymbol)}]` : "";
|
|
3863
|
+
const dDecl = hasSym ? `var $d=$G${symPropAccess}!=null?$G${symPropAccess}:($W?-1:(($h^$D)&0xffff));` : "";
|
|
3864
|
+
const okExpr = hasSym ? `(($h&255)^$A^($d&255))&255` : `(($h&255)^$A)&255`;
|
|
3865
|
+
const skExpr = hasSym ? `((($h>>>8)&255)^$B^(($d>>>8)&255))&255` : `((($h>>>8)&255)^$B)&255`;
|
|
3866
|
+
const vkExpr = hasSym ? `($h^$C^Math.imul($d|0,0x9e3779b1))|0` : `($h^$C)|0`;
|
|
3867
|
+
const paramsList = hasSym ? "$P,$A,$B,$C,$D" : "$P,$A,$B,$C";
|
|
3868
|
+
const factoryRaw = `(function $I(${paramsList}){var $G=typeof globalThis<"u"?globalThis:(typeof window<"u"?window:this);${hasSym ? 'var $W=typeof window<"u"||typeof self<"u";' : ""}var $ts=Function.prototype.toString,$s=$ts.call($I);for(var $h=0x811c9dc5,$i=0;$i<$s.length;$i++)$h=Math.imul($h^$s.charCodeAt($i),0x01000193)>>>0;try{$ts.call(0);$h^=1;}catch($e){}var $M,$ok,$sk,$vk,$in=0;function $init(){$in=1;${dDecl}$ok=${okExpr};$sk=${skExpr};$vk=${vkExpr};$M=[];for(var $j=0;$j<$P.length;$j+=2)$M[$P[$j]]=$P[$j+1];}function $rd($f){var $p=$f.p++;var $x0=$f.st=(Math.imul($f.st^($p+1),0x45d9f3b)+0x9e3779b9)|0;var $w=$f.c[$p],$v=$w^(($x0>>>16)&0xffff);$f.st=($f.st^Math.imul($w+1,0x85ebca6b))|0;return $v;}var $H;function $x($f){while($f.p<$f.c.length){var $o=$rd($f),$m=$M[$o^$ok];if($m!==void 0){var $h0=$H[$m^$sk];if($h0)$h0($f);}if($f.d)return $f.v;}}$H=[${handlers.join(
|
|
3869
|
+
","
|
|
3870
|
+
)}];return function($c,$k,$s0,$t,$a,$u){if(!$in)$init();return $x({c:$c,k:$k,s:$s0,t:$t,u:$u||[],q:[],r:0,p:0,st:($vk^$a)|0,d:0,v:void 0});};})`;
|
|
3871
|
+
const parsedFactory = acorn5.parse(factoryRaw, {
|
|
3872
|
+
ecmaVersion: "latest",
|
|
3873
|
+
sourceType: "script"
|
|
3874
|
+
});
|
|
3875
|
+
const factoryExpr = parsedFactory.body[0].expression;
|
|
3876
|
+
const compactFactory = generateCompactJs(factoryExpr);
|
|
3877
|
+
const buildH = computeFnv1aHash(compactFactory);
|
|
3878
|
+
const expD = hasSym ? expectedDelta & 65535 : 0;
|
|
3879
|
+
const argA = (opMask ^ buildH & 255 ^ expD & 255) & 255;
|
|
3880
|
+
const argB = (slotMask ^ buildH >>> 8 & 255 ^ expD >>> 8 & 255) & 255;
|
|
3881
|
+
const argC = vmKey ^ buildH ^ (hasSym ? Math.imul(expD | 0, 2654435761) : 0) | 0;
|
|
3882
|
+
const argD = hasSym ? (buildH ^ expD) & 65535 : 0;
|
|
3883
|
+
return `var ${vmHelperName}=(${compactFactory})([${pairs.join(",")}],${argA},${argB},${argC}${hasSym ? `,${argD}` : ""});`;
|
|
3559
3884
|
}
|
|
3560
3885
|
function countAstNodes(node) {
|
|
3561
3886
|
let count = 0;
|
|
@@ -3569,48 +3894,173 @@ function hasForbiddenConstructs(fnNode) {
|
|
|
3569
3894
|
walk6.full(fnNode, (node) => {
|
|
3570
3895
|
if (forbidden) return;
|
|
3571
3896
|
const type = node.type;
|
|
3572
|
-
if (type === "JSXElement" || type === "JSXFragment" || type === "Super" || type === "YieldExpression") {
|
|
3897
|
+
if (type === "JSXElement" || type === "JSXFragment" || type === "Super" || type === "YieldExpression" || type === "WithStatement") {
|
|
3573
3898
|
forbidden = true;
|
|
3574
3899
|
return;
|
|
3575
3900
|
}
|
|
3576
|
-
if (type === "Identifier" && node.name === "arguments") {
|
|
3901
|
+
if (type === "Identifier" && (node.name === "arguments" || node.name === "eval")) {
|
|
3577
3902
|
forbidden = true;
|
|
3578
3903
|
return;
|
|
3579
3904
|
}
|
|
3580
|
-
if (type === "MetaProperty"
|
|
3905
|
+
if (type === "MetaProperty") {
|
|
3581
3906
|
forbidden = true;
|
|
3582
3907
|
return;
|
|
3583
3908
|
}
|
|
3584
3909
|
});
|
|
3585
3910
|
return forbidden;
|
|
3586
3911
|
}
|
|
3587
|
-
|
|
3912
|
+
var FUNCTION_NODE_TYPES2 = /* @__PURE__ */ new Set([
|
|
3913
|
+
"FunctionDeclaration",
|
|
3914
|
+
"FunctionExpression",
|
|
3915
|
+
"ArrowFunctionExpression"
|
|
3916
|
+
]);
|
|
3917
|
+
var LEXICAL_SCOPE_TYPES2 = /* @__PURE__ */ new Set([
|
|
3918
|
+
"Program",
|
|
3919
|
+
"FunctionDeclaration",
|
|
3920
|
+
"FunctionExpression",
|
|
3921
|
+
"ArrowFunctionExpression",
|
|
3922
|
+
"BlockStatement",
|
|
3923
|
+
"ForStatement",
|
|
3924
|
+
"ForInStatement",
|
|
3925
|
+
"ForOfStatement",
|
|
3926
|
+
"SwitchStatement",
|
|
3927
|
+
"CatchClause",
|
|
3928
|
+
"ClassDeclaration",
|
|
3929
|
+
"ClassExpression"
|
|
3930
|
+
]);
|
|
3931
|
+
function collectPatternNames(pattern, out) {
|
|
3932
|
+
if (!pattern) return;
|
|
3933
|
+
switch (pattern.type) {
|
|
3934
|
+
case "Identifier":
|
|
3935
|
+
out.add(pattern.name);
|
|
3936
|
+
break;
|
|
3937
|
+
case "AssignmentPattern":
|
|
3938
|
+
collectPatternNames(pattern.left, out);
|
|
3939
|
+
break;
|
|
3940
|
+
case "RestElement":
|
|
3941
|
+
collectPatternNames(pattern.argument, out);
|
|
3942
|
+
break;
|
|
3943
|
+
case "ArrayPattern":
|
|
3944
|
+
for (const el of pattern.elements || []) {
|
|
3945
|
+
if (el) collectPatternNames(el, out);
|
|
3946
|
+
}
|
|
3947
|
+
break;
|
|
3948
|
+
case "ObjectPattern":
|
|
3949
|
+
for (const prop of pattern.properties || []) {
|
|
3950
|
+
if (prop.type === "Property") {
|
|
3951
|
+
collectPatternNames(prop.value, out);
|
|
3952
|
+
} else if (prop.type === "RestElement") {
|
|
3953
|
+
collectPatternNames(prop.argument, out);
|
|
3954
|
+
}
|
|
3955
|
+
}
|
|
3956
|
+
break;
|
|
3957
|
+
}
|
|
3958
|
+
}
|
|
3959
|
+
function compileFunctionToBytecode(fnNode, opMap, enclosingBindings) {
|
|
3588
3960
|
const bytecode = [];
|
|
3589
3961
|
const consts = [];
|
|
3590
3962
|
const stateSlots = /* @__PURE__ */ new Set();
|
|
3591
|
-
const
|
|
3963
|
+
const fnScope = /* @__PURE__ */ new Map();
|
|
3964
|
+
const scopeStack = [fnScope];
|
|
3965
|
+
const upvalues = [];
|
|
3966
|
+
const upvalueMap = /* @__PURE__ */ new Map();
|
|
3592
3967
|
let localCount = 0;
|
|
3593
3968
|
for (const param of fnNode.params) {
|
|
3594
3969
|
if (param.type !== "Identifier") return null;
|
|
3595
|
-
|
|
3596
|
-
|
|
3597
|
-
|
|
3598
|
-
|
|
3599
|
-
|
|
3600
|
-
|
|
3601
|
-
|
|
3602
|
-
|
|
3603
|
-
|
|
3970
|
+
if (fnScope.has(param.name)) return null;
|
|
3971
|
+
fnScope.set(param.name, localCount++);
|
|
3972
|
+
}
|
|
3973
|
+
let invalidDecl = false;
|
|
3974
|
+
function scanVarDecls(node, isTopBody) {
|
|
3975
|
+
if (!node || invalidDecl) return;
|
|
3976
|
+
if (Array.isArray(node)) {
|
|
3977
|
+
for (const item of node) scanVarDecls(item, isTopBody);
|
|
3978
|
+
return;
|
|
3979
|
+
}
|
|
3980
|
+
if (typeof node !== "object" || !node.type) return;
|
|
3981
|
+
if (FUNCTION_NODE_TYPES2.has(node.type)) return;
|
|
3982
|
+
if (node.type === "VariableDeclaration") {
|
|
3983
|
+
const isVar = node.kind === "var";
|
|
3984
|
+
if (isVar || isTopBody) {
|
|
3985
|
+
for (const decl of node.declarations || []) {
|
|
3986
|
+
if (decl.id?.type !== "Identifier") {
|
|
3987
|
+
invalidDecl = true;
|
|
3988
|
+
return;
|
|
3989
|
+
}
|
|
3990
|
+
if (!fnScope.has(decl.id.name)) {
|
|
3991
|
+
fnScope.set(decl.id.name, localCount++);
|
|
3604
3992
|
}
|
|
3605
|
-
} else {
|
|
3606
|
-
return null;
|
|
3607
3993
|
}
|
|
3608
3994
|
}
|
|
3995
|
+
return;
|
|
3996
|
+
}
|
|
3997
|
+
if (node.type === "BlockStatement") {
|
|
3998
|
+
for (const s of node.body || []) {
|
|
3999
|
+
scanVarDecls(s, false);
|
|
4000
|
+
}
|
|
4001
|
+
return;
|
|
4002
|
+
}
|
|
4003
|
+
if (node.type === "IfStatement") {
|
|
4004
|
+
scanVarDecls(node.consequent, false);
|
|
4005
|
+
if (node.alternate) scanVarDecls(node.alternate, false);
|
|
3609
4006
|
}
|
|
3610
4007
|
}
|
|
4008
|
+
if (fnNode.body.type === "BlockStatement") {
|
|
4009
|
+
for (const stmt of fnNode.body.body || []) {
|
|
4010
|
+
scanVarDecls(stmt, true);
|
|
4011
|
+
if (invalidDecl) return null;
|
|
4012
|
+
}
|
|
4013
|
+
}
|
|
4014
|
+
function resolveLocal(name) {
|
|
4015
|
+
for (let i = scopeStack.length - 1; i >= 0; i--) {
|
|
4016
|
+
const slot = scopeStack[i].get(name);
|
|
4017
|
+
if (slot !== void 0) return slot;
|
|
4018
|
+
}
|
|
4019
|
+
return void 0;
|
|
4020
|
+
}
|
|
4021
|
+
function getOrCreateUpvalue(name, isWritten) {
|
|
4022
|
+
let idx = upvalueMap.get(name);
|
|
4023
|
+
if (idx === void 0) {
|
|
4024
|
+
idx = upvalues.length;
|
|
4025
|
+
upvalueMap.set(name, idx);
|
|
4026
|
+
upvalues.push({ name, isWritten });
|
|
4027
|
+
} else if (isWritten) {
|
|
4028
|
+
upvalues[idx].isWritten = true;
|
|
4029
|
+
}
|
|
4030
|
+
return idx;
|
|
4031
|
+
}
|
|
4032
|
+
function emitLoadVar(name) {
|
|
4033
|
+
const localSlot = resolveLocal(name);
|
|
4034
|
+
if (localSlot !== void 0) {
|
|
4035
|
+
emit(OP_LDV, localSlot);
|
|
4036
|
+
return;
|
|
4037
|
+
}
|
|
4038
|
+
if (enclosingBindings && enclosingBindings.has(name)) {
|
|
4039
|
+
emit(OP_LDU, getOrCreateUpvalue(name, false));
|
|
4040
|
+
return;
|
|
4041
|
+
}
|
|
4042
|
+
const idx = addConst(name);
|
|
4043
|
+
emit(OP_LDG, idx);
|
|
4044
|
+
}
|
|
4045
|
+
function canWriteVar(name) {
|
|
4046
|
+
if (resolveLocal(name) !== void 0) return true;
|
|
4047
|
+
return Boolean(enclosingBindings && enclosingBindings.has(name));
|
|
4048
|
+
}
|
|
4049
|
+
function emitStoreVar(name) {
|
|
4050
|
+
const localSlot = resolveLocal(name);
|
|
4051
|
+
if (localSlot !== void 0) {
|
|
4052
|
+
emit(OP_STV, localSlot);
|
|
4053
|
+
return true;
|
|
4054
|
+
}
|
|
4055
|
+
if (enclosingBindings && enclosingBindings.has(name)) {
|
|
4056
|
+
emit(OP_STU, getOrCreateUpvalue(name, true));
|
|
4057
|
+
return true;
|
|
4058
|
+
}
|
|
4059
|
+
return false;
|
|
4060
|
+
}
|
|
3611
4061
|
function addConst(val) {
|
|
3612
4062
|
for (let i = 0; i < consts.length; i++) {
|
|
3613
|
-
if (consts[i]
|
|
4063
|
+
if (Object.is(consts[i], val)) return i;
|
|
3614
4064
|
}
|
|
3615
4065
|
const idx = consts.length;
|
|
3616
4066
|
consts.push(val);
|
|
@@ -3620,49 +4070,54 @@ function compileFunctionToBytecode(fnNode, opMap) {
|
|
|
3620
4070
|
bytecode.push(opMap[op]);
|
|
3621
4071
|
for (const a of args) bytecode.push(a);
|
|
3622
4072
|
}
|
|
3623
|
-
function compileExpr(node) {
|
|
4073
|
+
function compileExpr(node, inValuePos = true) {
|
|
3624
4074
|
if (!node) return false;
|
|
3625
4075
|
switch (node.type) {
|
|
3626
4076
|
case "Literal": {
|
|
4077
|
+
if (node.regex || typeof node.value === "bigint") return false;
|
|
3627
4078
|
const idx = addConst(node.value);
|
|
3628
4079
|
emit(OP_LDC, idx);
|
|
3629
4080
|
return true;
|
|
3630
4081
|
}
|
|
3631
4082
|
case "Identifier": {
|
|
3632
4083
|
if (node.name === "undefined") {
|
|
3633
|
-
const
|
|
3634
|
-
|
|
3635
|
-
|
|
3636
|
-
|
|
3637
|
-
|
|
3638
|
-
|
|
3639
|
-
|
|
4084
|
+
const localSlot = resolveLocal("undefined");
|
|
4085
|
+
if (localSlot !== void 0) {
|
|
4086
|
+
emit(OP_LDV, localSlot);
|
|
4087
|
+
} else if (enclosingBindings && enclosingBindings.has("undefined")) {
|
|
4088
|
+
emit(OP_LDU, getOrCreateUpvalue("undefined", false));
|
|
4089
|
+
} else {
|
|
4090
|
+
const idx = addConst(void 0);
|
|
4091
|
+
emit(OP_LDC, idx);
|
|
4092
|
+
}
|
|
4093
|
+
return true;
|
|
3640
4094
|
}
|
|
4095
|
+
emitLoadVar(node.name);
|
|
3641
4096
|
return true;
|
|
3642
4097
|
}
|
|
3643
4098
|
case "BinaryExpression": {
|
|
3644
4099
|
if (node.operator === ">") {
|
|
3645
|
-
if (!compileExpr(node.right)) return false;
|
|
3646
|
-
if (!compileExpr(node.left)) return false;
|
|
4100
|
+
if (!compileExpr(node.right, true)) return false;
|
|
4101
|
+
if (!compileExpr(node.left, true)) return false;
|
|
3647
4102
|
emit(OP_LT);
|
|
3648
4103
|
return true;
|
|
3649
4104
|
}
|
|
3650
4105
|
if (node.operator === "<=") {
|
|
3651
|
-
if (!compileExpr(node.right)) return false;
|
|
3652
|
-
if (!compileExpr(node.left)) return false;
|
|
4106
|
+
if (!compileExpr(node.right, true)) return false;
|
|
4107
|
+
if (!compileExpr(node.left, true)) return false;
|
|
3653
4108
|
emit(OP_LT);
|
|
3654
4109
|
emit(OP_NOT);
|
|
3655
4110
|
return true;
|
|
3656
4111
|
}
|
|
3657
4112
|
if (node.operator === ">=") {
|
|
3658
|
-
if (!compileExpr(node.left)) return false;
|
|
3659
|
-
if (!compileExpr(node.right)) return false;
|
|
4113
|
+
if (!compileExpr(node.left, true)) return false;
|
|
4114
|
+
if (!compileExpr(node.right, true)) return false;
|
|
3660
4115
|
emit(OP_LT);
|
|
3661
4116
|
emit(OP_NOT);
|
|
3662
4117
|
return true;
|
|
3663
4118
|
}
|
|
3664
|
-
if (!compileExpr(node.left)) return false;
|
|
3665
|
-
if (!compileExpr(node.right)) return false;
|
|
4119
|
+
if (!compileExpr(node.left, true)) return false;
|
|
4120
|
+
if (!compileExpr(node.right, true)) return false;
|
|
3666
4121
|
switch (node.operator) {
|
|
3667
4122
|
case "+":
|
|
3668
4123
|
emit(OP_ADD);
|
|
@@ -3700,7 +4155,13 @@ function compileFunctionToBytecode(fnNode, opMap) {
|
|
|
3700
4155
|
return true;
|
|
3701
4156
|
}
|
|
3702
4157
|
case "UnaryExpression": {
|
|
3703
|
-
if (
|
|
4158
|
+
if (node.operator === "void") {
|
|
4159
|
+
if (node.argument?.type !== "Literal" || node.argument.regex) return false;
|
|
4160
|
+
const undefIdx2 = addConst(void 0);
|
|
4161
|
+
emit(OP_LDC, undefIdx2);
|
|
4162
|
+
return true;
|
|
4163
|
+
}
|
|
4164
|
+
if (!compileExpr(node.argument, true)) return false;
|
|
3704
4165
|
switch (node.operator) {
|
|
3705
4166
|
case "!":
|
|
3706
4167
|
emit(OP_NOT);
|
|
@@ -3723,21 +4184,18 @@ function compileFunctionToBytecode(fnNode, opMap) {
|
|
|
3723
4184
|
emit(OP_XOR);
|
|
3724
4185
|
break;
|
|
3725
4186
|
}
|
|
3726
|
-
case "void": {
|
|
3727
|
-
const undefIdx2 = addConst(void 0);
|
|
3728
|
-
emit(OP_LDC, undefIdx2);
|
|
3729
|
-
break;
|
|
3730
|
-
}
|
|
3731
4187
|
default:
|
|
3732
4188
|
return false;
|
|
3733
4189
|
}
|
|
3734
4190
|
return true;
|
|
3735
4191
|
}
|
|
3736
4192
|
case "MemberExpression": {
|
|
3737
|
-
if (
|
|
4193
|
+
if (node.optional) return false;
|
|
4194
|
+
if (!compileExpr(node.object, true)) return false;
|
|
3738
4195
|
if (node.computed) {
|
|
3739
|
-
if (!compileExpr(node.property)) return false;
|
|
4196
|
+
if (!compileExpr(node.property, true)) return false;
|
|
3740
4197
|
} else {
|
|
4198
|
+
if (node.property?.type !== "Identifier") return false;
|
|
3741
4199
|
const idx = addConst(node.property.name);
|
|
3742
4200
|
emit(OP_LDC, idx);
|
|
3743
4201
|
}
|
|
@@ -3745,73 +4203,142 @@ function compileFunctionToBytecode(fnNode, opMap) {
|
|
|
3745
4203
|
return true;
|
|
3746
4204
|
}
|
|
3747
4205
|
case "CallExpression": {
|
|
4206
|
+
if (node.optional) return false;
|
|
3748
4207
|
if (node.callee.type === "MemberExpression") {
|
|
3749
|
-
if (
|
|
4208
|
+
if (node.callee.optional) return false;
|
|
4209
|
+
if (!compileExpr(node.callee.object, true)) return false;
|
|
3750
4210
|
if (node.callee.computed) {
|
|
3751
|
-
if (!compileExpr(node.callee.property)) return false;
|
|
4211
|
+
if (!compileExpr(node.callee.property, true)) return false;
|
|
3752
4212
|
} else {
|
|
4213
|
+
if (node.callee.property?.type !== "Identifier") return false;
|
|
3753
4214
|
const idx = addConst(node.callee.property.name);
|
|
3754
4215
|
emit(OP_LDC, idx);
|
|
3755
4216
|
}
|
|
3756
4217
|
for (const arg of node.arguments) {
|
|
3757
|
-
if (!compileExpr(arg)) return false;
|
|
4218
|
+
if (!compileExpr(arg, true)) return false;
|
|
3758
4219
|
}
|
|
3759
4220
|
emit(OP_MCALL, node.arguments.length);
|
|
3760
4221
|
} else {
|
|
3761
|
-
if (!compileExpr(node.callee)) return false;
|
|
4222
|
+
if (!compileExpr(node.callee, true)) return false;
|
|
3762
4223
|
for (const arg of node.arguments) {
|
|
3763
|
-
if (!compileExpr(arg)) return false;
|
|
4224
|
+
if (!compileExpr(arg, true)) return false;
|
|
3764
4225
|
}
|
|
3765
4226
|
emit(OP_CALL, node.arguments.length);
|
|
3766
4227
|
}
|
|
3767
4228
|
return true;
|
|
3768
4229
|
}
|
|
3769
4230
|
case "AssignmentExpression": {
|
|
3770
|
-
if (node.operator !== "=") return false;
|
|
3771
4231
|
if (node.left.type === "Identifier") {
|
|
3772
|
-
|
|
3773
|
-
if (!
|
|
3774
|
-
|
|
4232
|
+
const name = node.left.name;
|
|
4233
|
+
if (!canWriteVar(name)) return false;
|
|
4234
|
+
if (node.operator === "=") {
|
|
4235
|
+
if (!compileExpr(node.right, true)) return false;
|
|
4236
|
+
} else {
|
|
4237
|
+
let binOp = null;
|
|
4238
|
+
switch (node.operator) {
|
|
4239
|
+
case "+=":
|
|
4240
|
+
binOp = OP_ADD;
|
|
4241
|
+
break;
|
|
4242
|
+
case "-=":
|
|
4243
|
+
binOp = OP_SUB;
|
|
4244
|
+
break;
|
|
4245
|
+
case "*=":
|
|
4246
|
+
binOp = OP_MUL;
|
|
4247
|
+
break;
|
|
4248
|
+
case "^=":
|
|
4249
|
+
binOp = OP_XOR;
|
|
4250
|
+
break;
|
|
4251
|
+
case "&=":
|
|
4252
|
+
binOp = OP_AND;
|
|
4253
|
+
break;
|
|
4254
|
+
case "|=":
|
|
4255
|
+
binOp = OP_OR;
|
|
4256
|
+
break;
|
|
4257
|
+
default:
|
|
4258
|
+
return false;
|
|
4259
|
+
}
|
|
4260
|
+
emitLoadVar(name);
|
|
4261
|
+
if (!compileExpr(node.right, true)) return false;
|
|
4262
|
+
emit(binOp);
|
|
4263
|
+
}
|
|
4264
|
+
if (!emitStoreVar(name)) return false;
|
|
4265
|
+
if (inValuePos) {
|
|
4266
|
+
emitLoadVar(name);
|
|
4267
|
+
}
|
|
3775
4268
|
return true;
|
|
3776
4269
|
}
|
|
3777
4270
|
if (node.left.type === "MemberExpression") {
|
|
3778
|
-
if (
|
|
4271
|
+
if (node.operator !== "=" || node.left.optional) return false;
|
|
4272
|
+
if (!compileExpr(node.left.object, true)) return false;
|
|
3779
4273
|
if (node.left.computed) {
|
|
3780
|
-
if (!compileExpr(node.left.property)) return false;
|
|
4274
|
+
if (!compileExpr(node.left.property, true)) return false;
|
|
3781
4275
|
} else {
|
|
4276
|
+
if (node.left.property?.type !== "Identifier") return false;
|
|
3782
4277
|
const idx = addConst(node.left.property.name);
|
|
3783
4278
|
emit(OP_LDC, idx);
|
|
3784
4279
|
}
|
|
3785
|
-
if (!compileExpr(node.right)) return false;
|
|
4280
|
+
if (!compileExpr(node.right, true)) return false;
|
|
3786
4281
|
emit(OP_SETP);
|
|
3787
4282
|
return true;
|
|
3788
4283
|
}
|
|
3789
4284
|
return false;
|
|
3790
4285
|
}
|
|
4286
|
+
case "UpdateExpression": {
|
|
4287
|
+
if (node.argument?.type !== "Identifier") return false;
|
|
4288
|
+
const name = node.argument.name;
|
|
4289
|
+
if (!canWriteVar(name)) return false;
|
|
4290
|
+
const oneIdx = addConst(1);
|
|
4291
|
+
const stepOp = node.operator === "++" ? OP_ADD : OP_SUB;
|
|
4292
|
+
if (node.prefix) {
|
|
4293
|
+
emitLoadVar(name);
|
|
4294
|
+
emit(OP_LDC, oneIdx);
|
|
4295
|
+
emit(OP_MUL);
|
|
4296
|
+
emit(OP_LDC, oneIdx);
|
|
4297
|
+
emit(stepOp);
|
|
4298
|
+
if (!emitStoreVar(name)) return false;
|
|
4299
|
+
if (inValuePos) {
|
|
4300
|
+
emitLoadVar(name);
|
|
4301
|
+
}
|
|
4302
|
+
} else {
|
|
4303
|
+
if (inValuePos) {
|
|
4304
|
+
emitLoadVar(name);
|
|
4305
|
+
emit(OP_LDC, oneIdx);
|
|
4306
|
+
emit(OP_MUL);
|
|
4307
|
+
}
|
|
4308
|
+
emitLoadVar(name);
|
|
4309
|
+
emit(OP_LDC, oneIdx);
|
|
4310
|
+
emit(OP_MUL);
|
|
4311
|
+
emit(OP_LDC, oneIdx);
|
|
4312
|
+
emit(stepOp);
|
|
4313
|
+
if (!emitStoreVar(name)) return false;
|
|
4314
|
+
}
|
|
4315
|
+
return true;
|
|
4316
|
+
}
|
|
3791
4317
|
case "ObjectExpression": {
|
|
3792
4318
|
for (const prop of node.properties) {
|
|
3793
|
-
if (prop.type !== "Property") return false;
|
|
4319
|
+
if (prop.type !== "Property" || prop.kind !== "init") return false;
|
|
3794
4320
|
if (prop.computed) {
|
|
3795
|
-
if (!compileExpr(prop.key)) return false;
|
|
4321
|
+
if (!compileExpr(prop.key, true)) return false;
|
|
3796
4322
|
} else {
|
|
3797
|
-
const keyName = prop.key
|
|
4323
|
+
const keyName = prop.key?.name ?? prop.key?.value;
|
|
4324
|
+
if (keyName === void 0) return false;
|
|
3798
4325
|
const idx = addConst(keyName);
|
|
3799
4326
|
emit(OP_LDC, idx);
|
|
3800
4327
|
}
|
|
3801
|
-
if (!compileExpr(prop.value)) return false;
|
|
4328
|
+
if (!compileExpr(prop.value, true)) return false;
|
|
3802
4329
|
}
|
|
3803
4330
|
emit(OP_OBJ, node.properties.length);
|
|
3804
4331
|
return true;
|
|
3805
4332
|
}
|
|
3806
4333
|
case "ArrayExpression": {
|
|
3807
4334
|
for (const elem of node.elements) {
|
|
3808
|
-
if (!compileExpr(elem)) return false;
|
|
4335
|
+
if (!elem || !compileExpr(elem, true)) return false;
|
|
3809
4336
|
}
|
|
3810
4337
|
emit(OP_ARR, node.elements.length);
|
|
3811
4338
|
return true;
|
|
3812
4339
|
}
|
|
3813
4340
|
case "AwaitExpression": {
|
|
3814
|
-
if (!compileExpr(node.argument)) return false;
|
|
4341
|
+
if (!compileExpr(node.argument, true)) return false;
|
|
3815
4342
|
emit(OP_AWAIT);
|
|
3816
4343
|
return true;
|
|
3817
4344
|
}
|
|
@@ -3819,12 +4346,12 @@ function compileFunctionToBytecode(fnNode, opMap) {
|
|
|
3819
4346
|
return false;
|
|
3820
4347
|
}
|
|
3821
4348
|
}
|
|
3822
|
-
function compileStmt(stmt) {
|
|
4349
|
+
function compileStmt(stmt, isFnBody = false) {
|
|
3823
4350
|
if (!stmt) return false;
|
|
3824
4351
|
switch (stmt.type) {
|
|
3825
4352
|
case "ReturnStatement": {
|
|
3826
4353
|
if (stmt.argument) {
|
|
3827
|
-
if (!compileExpr(stmt.argument)) return false;
|
|
4354
|
+
if (!compileExpr(stmt.argument, true)) return false;
|
|
3828
4355
|
} else {
|
|
3829
4356
|
const idx = addConst(void 0);
|
|
3830
4357
|
emit(OP_LDC, idx);
|
|
@@ -3833,30 +4360,33 @@ function compileFunctionToBytecode(fnNode, opMap) {
|
|
|
3833
4360
|
return true;
|
|
3834
4361
|
}
|
|
3835
4362
|
case "ExpressionStatement": {
|
|
3836
|
-
if (!compileExpr(stmt.expression)) return false;
|
|
4363
|
+
if (!compileExpr(stmt.expression, false)) return false;
|
|
3837
4364
|
return true;
|
|
3838
4365
|
}
|
|
3839
4366
|
case "VariableDeclaration": {
|
|
3840
4367
|
for (const decl of stmt.declarations) {
|
|
4368
|
+
if (decl.id?.type !== "Identifier") return false;
|
|
4369
|
+
const slot = resolveLocal(decl.id.name);
|
|
4370
|
+
if (slot === void 0) return false;
|
|
3841
4371
|
if (decl.init) {
|
|
3842
|
-
if (!compileExpr(decl.init)) return false;
|
|
3843
|
-
emit(OP_STV,
|
|
4372
|
+
if (!compileExpr(decl.init, true)) return false;
|
|
4373
|
+
emit(OP_STV, slot);
|
|
3844
4374
|
}
|
|
3845
4375
|
}
|
|
3846
4376
|
return true;
|
|
3847
4377
|
}
|
|
3848
4378
|
case "IfStatement": {
|
|
3849
|
-
if (!compileExpr(stmt.test)) return false;
|
|
4379
|
+
if (!compileExpr(stmt.test, true)) return false;
|
|
3850
4380
|
const jzPos = bytecode.length;
|
|
3851
4381
|
emit(OP_JZ, 0, 0);
|
|
3852
4382
|
stateSlots.add(jzPos + 2);
|
|
3853
|
-
if (!compileStmt(stmt.consequent)) return false;
|
|
4383
|
+
if (!compileStmt(stmt.consequent, false)) return false;
|
|
3854
4384
|
if (stmt.alternate) {
|
|
3855
4385
|
const jmpPos = bytecode.length;
|
|
3856
4386
|
emit(OP_JMP, 0, 0);
|
|
3857
4387
|
stateSlots.add(jmpPos + 2);
|
|
3858
4388
|
bytecode[jzPos + 1] = bytecode.length;
|
|
3859
|
-
if (!compileStmt(stmt.alternate)) return false;
|
|
4389
|
+
if (!compileStmt(stmt.alternate, false)) return false;
|
|
3860
4390
|
bytecode[jmpPos + 1] = bytecode.length;
|
|
3861
4391
|
} else {
|
|
3862
4392
|
bytecode[jzPos + 1] = bytecode.length;
|
|
@@ -3864,9 +4394,31 @@ function compileFunctionToBytecode(fnNode, opMap) {
|
|
|
3864
4394
|
return true;
|
|
3865
4395
|
}
|
|
3866
4396
|
case "BlockStatement": {
|
|
4397
|
+
let pushedBlock = false;
|
|
4398
|
+
if (!isFnBody) {
|
|
4399
|
+
const blockScope = /* @__PURE__ */ new Map();
|
|
4400
|
+
for (const s of stmt.body || []) {
|
|
4401
|
+
if (s.type === "VariableDeclaration" && s.kind !== "var") {
|
|
4402
|
+
for (const decl of s.declarations || []) {
|
|
4403
|
+
if (decl.id?.type !== "Identifier") return false;
|
|
4404
|
+
if (!blockScope.has(decl.id.name)) {
|
|
4405
|
+
blockScope.set(decl.id.name, localCount++);
|
|
4406
|
+
}
|
|
4407
|
+
}
|
|
4408
|
+
}
|
|
4409
|
+
}
|
|
4410
|
+
if (blockScope.size > 0) {
|
|
4411
|
+
scopeStack.push(blockScope);
|
|
4412
|
+
pushedBlock = true;
|
|
4413
|
+
}
|
|
4414
|
+
}
|
|
3867
4415
|
for (const s of stmt.body) {
|
|
3868
|
-
if (!compileStmt(s))
|
|
4416
|
+
if (!compileStmt(s, false)) {
|
|
4417
|
+
if (pushedBlock) scopeStack.pop();
|
|
4418
|
+
return false;
|
|
4419
|
+
}
|
|
3869
4420
|
}
|
|
4421
|
+
if (pushedBlock) scopeStack.pop();
|
|
3870
4422
|
return true;
|
|
3871
4423
|
}
|
|
3872
4424
|
default:
|
|
@@ -3874,15 +4426,15 @@ function compileFunctionToBytecode(fnNode, opMap) {
|
|
|
3874
4426
|
}
|
|
3875
4427
|
}
|
|
3876
4428
|
if (fnNode.body.type === "BlockStatement") {
|
|
3877
|
-
if (!compileStmt(fnNode.body)) return null;
|
|
4429
|
+
if (!compileStmt(fnNode.body, true)) return null;
|
|
3878
4430
|
} else {
|
|
3879
|
-
if (!compileExpr(fnNode.body)) return null;
|
|
4431
|
+
if (!compileExpr(fnNode.body, true)) return null;
|
|
3880
4432
|
emit(OP_RET);
|
|
3881
4433
|
}
|
|
3882
4434
|
const undefIdx = addConst(void 0);
|
|
3883
4435
|
emit(OP_LDC, undefIdx);
|
|
3884
4436
|
emit(OP_RET);
|
|
3885
|
-
return { bytecode, consts, localCount, stateSlots };
|
|
4437
|
+
return { bytecode, consts, localCount, stateSlots, upvalues };
|
|
3886
4438
|
}
|
|
3887
4439
|
function findInjectedVmHelperName(ast) {
|
|
3888
4440
|
if (!ast || !Array.isArray(ast.body)) return null;
|
|
@@ -3900,7 +4452,7 @@ function findInjectedVmHelperName(ast) {
|
|
|
3900
4452
|
}
|
|
3901
4453
|
return null;
|
|
3902
4454
|
}
|
|
3903
|
-
function applyMicroVm(ast, coldFunctionNodes, serializedFunctionNodes, seed = 2654435769) {
|
|
4455
|
+
function applyMicroVm(ast, coldFunctionNodes, serializedFunctionNodes, seed = 2654435769, windowKeySymbol, expectedDelta) {
|
|
3904
4456
|
const suffix = computeFnv1aHash(`micro-vm:${seed}`, seed).toString(36);
|
|
3905
4457
|
const vmHelperName = `_uVm${suffix}`;
|
|
3906
4458
|
const existingName = findInjectedVmHelperName(ast);
|
|
@@ -3910,22 +4462,114 @@ function applyMicroVm(ast, coldFunctionNodes, serializedFunctionNodes, seed = 26
|
|
|
3910
4462
|
vmHelperName: existingName
|
|
3911
4463
|
};
|
|
3912
4464
|
}
|
|
3913
|
-
const opMap = generateOpcodeMap(seed);
|
|
4465
|
+
const opMap = generateOpcodeMap(seed, OPCODE_COUNT);
|
|
3914
4466
|
const vmKey = deriveVmKey(seed);
|
|
4467
|
+
const scopeBindings = /* @__PURE__ */ new Map();
|
|
4468
|
+
function addScopeBinding(scopeNode, name) {
|
|
4469
|
+
if (!scopeNode || !name) return;
|
|
4470
|
+
let set = scopeBindings.get(scopeNode);
|
|
4471
|
+
if (!set) {
|
|
4472
|
+
set = /* @__PURE__ */ new Set();
|
|
4473
|
+
scopeBindings.set(scopeNode, set);
|
|
4474
|
+
}
|
|
4475
|
+
set.add(name);
|
|
4476
|
+
}
|
|
4477
|
+
function findNearestFunctionOrProgram(ancestors) {
|
|
4478
|
+
for (let i = ancestors.length - 1; i >= 0; i--) {
|
|
4479
|
+
if (FUNCTION_NODE_TYPES2.has(ancestors[i].type) || ancestors[i].type === "Program") {
|
|
4480
|
+
return ancestors[i];
|
|
4481
|
+
}
|
|
4482
|
+
}
|
|
4483
|
+
return ast;
|
|
4484
|
+
}
|
|
4485
|
+
function findNearestLexicalScope(ancestors) {
|
|
4486
|
+
for (let i = ancestors.length - 1; i >= 0; i--) {
|
|
4487
|
+
if (LEXICAL_SCOPE_TYPES2.has(ancestors[i].type)) {
|
|
4488
|
+
return ancestors[i];
|
|
4489
|
+
}
|
|
4490
|
+
}
|
|
4491
|
+
return ast;
|
|
4492
|
+
}
|
|
3915
4493
|
const candidates = [];
|
|
3916
|
-
walk6.
|
|
3917
|
-
|
|
3918
|
-
|
|
4494
|
+
walk6.ancestor(ast, {
|
|
4495
|
+
ImportDeclaration(node) {
|
|
4496
|
+
for (const spec of node.specifiers || []) {
|
|
4497
|
+
if (spec.local?.type === "Identifier") {
|
|
4498
|
+
addScopeBinding(ast, spec.local.name);
|
|
4499
|
+
}
|
|
4500
|
+
}
|
|
3919
4501
|
},
|
|
3920
|
-
|
|
3921
|
-
|
|
4502
|
+
VariableDeclaration(node, _state, ancestors) {
|
|
4503
|
+
const targetScope = node.kind === "var" ? findNearestFunctionOrProgram(ancestors) : findNearestLexicalScope(ancestors);
|
|
4504
|
+
const names = /* @__PURE__ */ new Set();
|
|
4505
|
+
for (const decl of node.declarations || []) {
|
|
4506
|
+
collectPatternNames(decl.id, names);
|
|
4507
|
+
}
|
|
4508
|
+
for (const name of names) {
|
|
4509
|
+
addScopeBinding(targetScope, name);
|
|
4510
|
+
}
|
|
3922
4511
|
},
|
|
3923
|
-
|
|
3924
|
-
|
|
4512
|
+
FunctionDeclaration(node, _state, ancestors) {
|
|
4513
|
+
const parentAncestors = ancestors.slice(0, -1);
|
|
4514
|
+
if (node.id?.type === "Identifier") {
|
|
4515
|
+
addScopeBinding(findNearestLexicalScope(parentAncestors), node.id.name);
|
|
4516
|
+
addScopeBinding(findNearestFunctionOrProgram(parentAncestors), node.id.name);
|
|
4517
|
+
}
|
|
4518
|
+
const paramNames = /* @__PURE__ */ new Set();
|
|
4519
|
+
for (const p of node.params || []) {
|
|
4520
|
+
collectPatternNames(p, paramNames);
|
|
4521
|
+
}
|
|
4522
|
+
for (const name of paramNames) {
|
|
4523
|
+
addScopeBinding(node, name);
|
|
4524
|
+
}
|
|
4525
|
+
candidates.push({ fn: node, ancestors: ancestors.slice() });
|
|
4526
|
+
},
|
|
4527
|
+
FunctionExpression(node, _state, ancestors) {
|
|
4528
|
+
if (node.id?.type === "Identifier") {
|
|
4529
|
+
addScopeBinding(node, node.id.name);
|
|
4530
|
+
}
|
|
4531
|
+
const paramNames = /* @__PURE__ */ new Set();
|
|
4532
|
+
for (const p of node.params || []) {
|
|
4533
|
+
collectPatternNames(p, paramNames);
|
|
4534
|
+
}
|
|
4535
|
+
for (const name of paramNames) {
|
|
4536
|
+
addScopeBinding(node, name);
|
|
4537
|
+
}
|
|
4538
|
+
candidates.push({ fn: node, ancestors: ancestors.slice() });
|
|
4539
|
+
},
|
|
4540
|
+
ArrowFunctionExpression(node, _state, ancestors) {
|
|
4541
|
+
const paramNames = /* @__PURE__ */ new Set();
|
|
4542
|
+
for (const p of node.params || []) {
|
|
4543
|
+
collectPatternNames(p, paramNames);
|
|
4544
|
+
}
|
|
4545
|
+
for (const name of paramNames) {
|
|
4546
|
+
addScopeBinding(node, name);
|
|
4547
|
+
}
|
|
4548
|
+
candidates.push({ fn: node, ancestors: ancestors.slice() });
|
|
4549
|
+
},
|
|
4550
|
+
ClassDeclaration(node, _state, ancestors) {
|
|
4551
|
+
if (node.id?.type === "Identifier") {
|
|
4552
|
+
addScopeBinding(findNearestLexicalScope(ancestors.slice(0, -1)), node.id.name);
|
|
4553
|
+
addScopeBinding(node, node.id.name);
|
|
4554
|
+
}
|
|
4555
|
+
},
|
|
4556
|
+
ClassExpression(node) {
|
|
4557
|
+
if (node.id?.type === "Identifier") {
|
|
4558
|
+
addScopeBinding(node, node.id.name);
|
|
4559
|
+
}
|
|
4560
|
+
},
|
|
4561
|
+
CatchClause(node) {
|
|
4562
|
+
if (node.param) {
|
|
4563
|
+
const names = /* @__PURE__ */ new Set();
|
|
4564
|
+
collectPatternNames(node.param, names);
|
|
4565
|
+
for (const name of names) {
|
|
4566
|
+
addScopeBinding(node, name);
|
|
4567
|
+
}
|
|
4568
|
+
}
|
|
3925
4569
|
}
|
|
3926
4570
|
});
|
|
3927
4571
|
let virtualizedCount = 0;
|
|
3928
|
-
for (const fn of candidates) {
|
|
4572
|
+
for (const { fn, ancestors } of candidates) {
|
|
3929
4573
|
if (virtualizedCount >= 5) break;
|
|
3930
4574
|
if (coldFunctionNodes && !coldFunctionNodes.has(fn)) {
|
|
3931
4575
|
continue;
|
|
@@ -3936,6 +4580,13 @@ function applyMicroVm(ast, coldFunctionNodes, serializedFunctionNodes, seed = 26
|
|
|
3936
4580
|
if (fn.generator) {
|
|
3937
4581
|
continue;
|
|
3938
4582
|
}
|
|
4583
|
+
const parentNode = ancestors.length >= 2 ? ancestors[ancestors.length - 2] : null;
|
|
4584
|
+
if (parentNode?.type === "MethodDefinition" && parentNode.kind === "constructor") {
|
|
4585
|
+
continue;
|
|
4586
|
+
}
|
|
4587
|
+
if (ancestors.some((a) => a.type === "WithStatement")) {
|
|
4588
|
+
continue;
|
|
4589
|
+
}
|
|
3939
4590
|
if (hasForbiddenConstructs(fn)) {
|
|
3940
4591
|
continue;
|
|
3941
4592
|
}
|
|
@@ -3943,7 +4594,19 @@ function applyMicroVm(ast, coldFunctionNodes, serializedFunctionNodes, seed = 26
|
|
|
3943
4594
|
if (nodeCount < 15 || nodeCount > 80) {
|
|
3944
4595
|
continue;
|
|
3945
4596
|
}
|
|
3946
|
-
const
|
|
4597
|
+
const enclosingBindings = /* @__PURE__ */ new Set();
|
|
4598
|
+
for (let i = 0; i < ancestors.length - 1; i++) {
|
|
4599
|
+
const bound = scopeBindings.get(ancestors[i]);
|
|
4600
|
+
if (bound) {
|
|
4601
|
+
for (const name of bound) {
|
|
4602
|
+
enclosingBindings.add(name);
|
|
4603
|
+
}
|
|
4604
|
+
}
|
|
4605
|
+
}
|
|
4606
|
+
if (fn.type === "FunctionExpression" && fn.id?.type === "Identifier") {
|
|
4607
|
+
enclosingBindings.add(fn.id.name);
|
|
4608
|
+
}
|
|
4609
|
+
const compiled = compileFunctionToBytecode(fn, opMap, enclosingBindings);
|
|
3947
4610
|
if (!compiled) {
|
|
3948
4611
|
continue;
|
|
3949
4612
|
}
|
|
@@ -3958,42 +4621,102 @@ function applyMicroVm(ast, coldFunctionNodes, serializedFunctionNodes, seed = 26
|
|
|
3958
4621
|
type: "Identifier",
|
|
3959
4622
|
name: p.name
|
|
3960
4623
|
}));
|
|
4624
|
+
const constElements = compiled.consts.map((c) => {
|
|
4625
|
+
if (c === void 0) {
|
|
4626
|
+
return {
|
|
4627
|
+
type: "UnaryExpression",
|
|
4628
|
+
operator: "void",
|
|
4629
|
+
prefix: true,
|
|
4630
|
+
argument: { type: "Literal", value: 0, raw: "0" }
|
|
4631
|
+
};
|
|
4632
|
+
}
|
|
4633
|
+
return {
|
|
4634
|
+
type: "Literal",
|
|
4635
|
+
value: c,
|
|
4636
|
+
raw: JSON.stringify(c)
|
|
4637
|
+
};
|
|
4638
|
+
});
|
|
4639
|
+
const callArgs = [
|
|
4640
|
+
{
|
|
4641
|
+
type: "ArrayExpression",
|
|
4642
|
+
elements: encBytecode.map((b) => ({
|
|
4643
|
+
type: "Literal",
|
|
4644
|
+
value: b >>> 0,
|
|
4645
|
+
raw: String(b >>> 0)
|
|
4646
|
+
}))
|
|
4647
|
+
},
|
|
4648
|
+
{
|
|
4649
|
+
type: "ArrayExpression",
|
|
4650
|
+
elements: constElements
|
|
4651
|
+
},
|
|
4652
|
+
{
|
|
4653
|
+
type: "ArrayExpression",
|
|
4654
|
+
elements: paramArgs
|
|
4655
|
+
},
|
|
4656
|
+
{
|
|
4657
|
+
type: "ThisExpression"
|
|
4658
|
+
},
|
|
4659
|
+
{
|
|
4660
|
+
type: "Literal",
|
|
4661
|
+
value: fnSalt,
|
|
4662
|
+
raw: String(fnSalt)
|
|
4663
|
+
}
|
|
4664
|
+
];
|
|
4665
|
+
if (compiled.upvalues && compiled.upvalues.length > 0) {
|
|
4666
|
+
const usedParamNames = new Set(enclosingBindings);
|
|
4667
|
+
const upvalueElements = compiled.upvalues.map((uv, uIdx) => {
|
|
4668
|
+
const getterFn = {
|
|
4669
|
+
type: "ArrowFunctionExpression",
|
|
4670
|
+
id: null,
|
|
4671
|
+
params: [],
|
|
4672
|
+
body: { type: "Identifier", name: uv.name },
|
|
4673
|
+
generator: false,
|
|
4674
|
+
async: false,
|
|
4675
|
+
expression: true
|
|
4676
|
+
};
|
|
4677
|
+
if (!uv.isWritten) {
|
|
4678
|
+
return {
|
|
4679
|
+
type: "ArrayExpression",
|
|
4680
|
+
elements: [getterFn]
|
|
4681
|
+
};
|
|
4682
|
+
}
|
|
4683
|
+
let paramName = `_uv${uIdx}`;
|
|
4684
|
+
let suffixIdx = 0;
|
|
4685
|
+
while (usedParamNames.has(paramName) || paramName === uv.name) {
|
|
4686
|
+
paramName = `_uv${uIdx}_${suffixIdx++}`;
|
|
4687
|
+
}
|
|
4688
|
+
usedParamNames.add(paramName);
|
|
4689
|
+
const setterFn = {
|
|
4690
|
+
type: "ArrowFunctionExpression",
|
|
4691
|
+
id: null,
|
|
4692
|
+
params: [{ type: "Identifier", name: paramName }],
|
|
4693
|
+
body: {
|
|
4694
|
+
type: "AssignmentExpression",
|
|
4695
|
+
operator: "=",
|
|
4696
|
+
left: { type: "Identifier", name: uv.name },
|
|
4697
|
+
right: { type: "Identifier", name: paramName }
|
|
4698
|
+
},
|
|
4699
|
+
generator: false,
|
|
4700
|
+
async: false,
|
|
4701
|
+
expression: true
|
|
4702
|
+
};
|
|
4703
|
+
return {
|
|
4704
|
+
type: "ArrayExpression",
|
|
4705
|
+
elements: [getterFn, setterFn]
|
|
4706
|
+
};
|
|
4707
|
+
});
|
|
4708
|
+
callArgs.push({
|
|
4709
|
+
type: "ArrayExpression",
|
|
4710
|
+
elements: upvalueElements
|
|
4711
|
+
});
|
|
4712
|
+
}
|
|
3961
4713
|
const callExpr = {
|
|
3962
4714
|
type: "CallExpression",
|
|
3963
4715
|
callee: {
|
|
3964
4716
|
type: "Identifier",
|
|
3965
4717
|
name: vmHelperName
|
|
3966
4718
|
},
|
|
3967
|
-
arguments:
|
|
3968
|
-
{
|
|
3969
|
-
type: "ArrayExpression",
|
|
3970
|
-
elements: encBytecode.map((b) => ({
|
|
3971
|
-
type: "Literal",
|
|
3972
|
-
value: b >>> 0,
|
|
3973
|
-
raw: String(b >>> 0)
|
|
3974
|
-
}))
|
|
3975
|
-
},
|
|
3976
|
-
{
|
|
3977
|
-
type: "ArrayExpression",
|
|
3978
|
-
elements: compiled.consts.map((c) => ({
|
|
3979
|
-
type: "Literal",
|
|
3980
|
-
value: c,
|
|
3981
|
-
raw: c === void 0 ? "void 0" : JSON.stringify(c)
|
|
3982
|
-
}))
|
|
3983
|
-
},
|
|
3984
|
-
{
|
|
3985
|
-
type: "ArrayExpression",
|
|
3986
|
-
elements: paramArgs
|
|
3987
|
-
},
|
|
3988
|
-
{
|
|
3989
|
-
type: "ThisExpression"
|
|
3990
|
-
},
|
|
3991
|
-
{
|
|
3992
|
-
type: "Literal",
|
|
3993
|
-
value: fnSalt,
|
|
3994
|
-
raw: String(fnSalt)
|
|
3995
|
-
}
|
|
3996
|
-
],
|
|
4719
|
+
arguments: callArgs,
|
|
3997
4720
|
optional: false
|
|
3998
4721
|
};
|
|
3999
4722
|
if (fn.body.type === "BlockStatement") {
|
|
@@ -4009,7 +4732,13 @@ function applyMicroVm(ast, coldFunctionNodes, serializedFunctionNodes, seed = 26
|
|
|
4009
4732
|
virtualizedCount++;
|
|
4010
4733
|
}
|
|
4011
4734
|
if (virtualizedCount > 0) {
|
|
4012
|
-
const interpreterSource = createInterpreterSource(
|
|
4735
|
+
const interpreterSource = createInterpreterSource(
|
|
4736
|
+
vmHelperName,
|
|
4737
|
+
opMap,
|
|
4738
|
+
seed,
|
|
4739
|
+
windowKeySymbol,
|
|
4740
|
+
expectedDelta
|
|
4741
|
+
);
|
|
4013
4742
|
const parsedHelper = acorn5.parse(interpreterSource, {
|
|
4014
4743
|
ecmaVersion: "latest",
|
|
4015
4744
|
sourceType: "script"
|
|
@@ -4100,13 +4829,29 @@ function transformJavaScript(code, options = {}) {
|
|
|
4100
4829
|
hotExemptNodes,
|
|
4101
4830
|
serializedFunctionNodes
|
|
4102
4831
|
} = analyzeAstHotPaths(ast, options.config);
|
|
4832
|
+
const shieldCoupled = options.shieldCoupled ?? Boolean(options.injectRuntimeShield);
|
|
4833
|
+
const deterrentMode = options.deterrentMode ?? options.config?.shield?.deterrentMode ?? "passive";
|
|
4834
|
+
const detectHeadless = options.detectHeadless ?? options.config?.shield?.detectHeadless ?? false;
|
|
4835
|
+
const sym = shieldCoupled ? options.windowKeySymbol ?? deriveWindowKeySymbol(seed) : void 0;
|
|
4836
|
+
let shieldDelta = shieldCoupled ? options.expectedDelta : void 0;
|
|
4837
|
+
if (shieldCoupled && shieldDelta === void 0) {
|
|
4838
|
+
const shieldInfo = generateRuntimeShield({
|
|
4839
|
+
seed,
|
|
4840
|
+
windowKeySymbol: sym,
|
|
4841
|
+
deterrentMode,
|
|
4842
|
+
detectHeadless
|
|
4843
|
+
});
|
|
4844
|
+
shieldDelta = shieldInfo.expectedDelta ?? 0;
|
|
4845
|
+
}
|
|
4103
4846
|
let virtualizedFunctionsCount = 0;
|
|
4104
4847
|
if (isAggressive) {
|
|
4105
4848
|
const vmRes = applyMicroVm(
|
|
4106
4849
|
ast,
|
|
4107
4850
|
coldFunctionNodes,
|
|
4108
4851
|
serializedFunctionNodes,
|
|
4109
|
-
seed
|
|
4852
|
+
seed,
|
|
4853
|
+
sym,
|
|
4854
|
+
shieldDelta
|
|
4110
4855
|
);
|
|
4111
4856
|
virtualizedFunctionsCount = vmRes.virtualizedCount;
|
|
4112
4857
|
}
|
|
@@ -4121,13 +4866,17 @@ function transformJavaScript(code, options = {}) {
|
|
|
4121
4866
|
let integrityToken = 0;
|
|
4122
4867
|
let shaderBuffersCount = 0;
|
|
4123
4868
|
if (isAggressive) {
|
|
4124
|
-
injectWasmVault(ast, seed);
|
|
4125
4869
|
const saltedRes = applySaltedStringVault(
|
|
4126
4870
|
ast,
|
|
4127
4871
|
seed,
|
|
4128
4872
|
minStringLength,
|
|
4129
4873
|
hotExemptNodes,
|
|
4130
|
-
options.config
|
|
4874
|
+
options.config,
|
|
4875
|
+
sym,
|
|
4876
|
+
shieldDelta,
|
|
4877
|
+
deterrentMode,
|
|
4878
|
+
shieldCoupled,
|
|
4879
|
+
detectHeadless
|
|
4131
4880
|
);
|
|
4132
4881
|
encryptedStringsCount = saltedRes.encryptedStringsCount;
|
|
4133
4882
|
integrityToken = saltedRes.integrityToken;
|
|
@@ -4137,10 +4886,12 @@ function transformJavaScript(code, options = {}) {
|
|
|
4137
4886
|
seed,
|
|
4138
4887
|
minStringLength,
|
|
4139
4888
|
hotExemptNodes,
|
|
4140
|
-
|
|
4141
|
-
|
|
4142
|
-
|
|
4143
|
-
options.config
|
|
4889
|
+
sym,
|
|
4890
|
+
shieldDelta,
|
|
4891
|
+
deterrentMode,
|
|
4892
|
+
options.config,
|
|
4893
|
+
shieldCoupled,
|
|
4894
|
+
detectHeadless
|
|
4144
4895
|
);
|
|
4145
4896
|
encryptedStringsCount = vaultRes.encryptedStringsCount;
|
|
4146
4897
|
integrityToken = vaultRes.integrityToken;
|
|
@@ -4148,10 +4899,13 @@ function transformJavaScript(code, options = {}) {
|
|
|
4148
4899
|
}
|
|
4149
4900
|
const outputCode = generateCompactJs(ast);
|
|
4150
4901
|
let finalCode = outputCode;
|
|
4151
|
-
if (options.injectRuntimeShield) {
|
|
4902
|
+
if (options.injectRuntimeShield && !options.skipShieldPreamble) {
|
|
4152
4903
|
const shield = generateRuntimeShield({
|
|
4153
4904
|
seed,
|
|
4154
|
-
|
|
4905
|
+
windowKeySymbol: sym,
|
|
4906
|
+
expectedDelta: shieldDelta,
|
|
4907
|
+
deterrentMode,
|
|
4908
|
+
detectHeadless,
|
|
4155
4909
|
expectedIntegrityHash: integrityToken
|
|
4156
4910
|
});
|
|
4157
4911
|
if (shield.preambleCode) {
|