js-confuser-vm 0.0.9 → 0.1.1
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/.gitmodules +4 -0
- package/CHANGELOG.md +125 -2
- package/README.md +128 -53
- package/bench.ts +146 -0
- package/disassemble.ts +12 -0
- package/dist/build-runtime.js +41 -15
- package/dist/compiler.js +328 -181
- package/dist/disassembler.js +317 -0
- package/dist/index.js +7 -2
- package/dist/runtime.js +255 -176
- package/dist/template.js +258 -0
- package/dist/transforms/bytecode/aliasedOpcodes.js +4 -1
- package/dist/transforms/bytecode/controlFlowFlattening.js +451 -0
- package/dist/transforms/bytecode/dispatcher.js +266 -0
- package/dist/transforms/bytecode/macroOpcodes.js +3 -3
- package/dist/transforms/bytecode/resolveConstants.js +100 -0
- package/dist/transforms/bytecode/resolveLabels.js +21 -18
- package/dist/transforms/bytecode/resolveRegisters.js +216 -0
- package/dist/transforms/bytecode/semanticOpcodes.js +162 -0
- package/dist/transforms/bytecode/specializedOpcodes.js +22 -12
- package/dist/transforms/bytecode/stringConcealing.js +110 -0
- package/dist/transforms/runtime/classObfuscation.js +43 -0
- package/dist/transforms/runtime/handlerTable.js +91 -0
- package/dist/transforms/runtime/semanticOpcodes.js +35 -0
- package/dist/transforms/runtime/specializedOpcodes.js +11 -5
- package/dist/types.js +42 -1
- package/dist/utils/ast-utils.js +14 -0
- package/dist/utils/op-utils.js +1 -2
- package/dist/utils/pass-utils.js +100 -0
- package/dist/utils/profile-utils.js +3 -0
- package/index.ts +22 -16
- package/jest.config.js +19 -2
- package/output.disassembled.js +41 -0
- package/package.json +2 -1
- package/src/build-runtime.ts +113 -78
- package/src/compiler.ts +2703 -2482
- package/src/disassembler.ts +329 -0
- package/src/index.ts +12 -2
- package/src/options.ts +8 -1
- package/src/runtime.ts +294 -180
- package/src/template.ts +265 -0
- package/src/transforms/bytecode/aliasedOpcodes.ts +5 -2
- package/src/transforms/bytecode/controlFlowFlattening.ts +566 -0
- package/src/transforms/bytecode/dispatcher.ts +292 -0
- package/src/transforms/bytecode/macroOpcodes.ts +4 -4
- package/src/transforms/bytecode/resolveLabels.ts +31 -27
- package/src/transforms/bytecode/resolveRegisters.ts +226 -0
- package/src/transforms/bytecode/specializedOpcodes.ts +27 -20
- package/src/transforms/bytecode/stringConcealing.ts +130 -0
- package/src/transforms/runtime/classObfuscation.ts +59 -0
- package/src/transforms/runtime/specializedOpcodes.ts +14 -9
- package/src/types.ts +106 -5
- package/src/utils/ast-utils.ts +19 -0
- package/src/utils/op-utils.ts +2 -2
- package/src/utils/pass-utils.ts +126 -0
- package/src/utils/profile-utils.ts +3 -0
- package/tsconfig.json +1 -1
- package/dist/transforms/utils/op-utils.js +0 -25
- package/dist/transforms/utils/random-utils.js +0 -27
- package/dist/utilts.js +0 -3
- package/src/transforms/bytecode/microOpcodes.ts +0 -291
- package/src/transforms/runtime/internalVariables.ts +0 -270
- package/src/transforms/runtime/microOpcodes.ts +0 -93
- /package/src/transforms/bytecode/{resolveContants.ts → resolveConstants.ts} +0 -0
package/src/template.ts
ADDED
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
// Template
|
|
2
|
+
// Compiles a JS code snippet into raw IR bytecode that can be spliced into the
|
|
3
|
+
// parent compiler's bytecode stream at any point before resolveRegisters /
|
|
4
|
+
// resolveLabels run.
|
|
5
|
+
//
|
|
6
|
+
// ── Usage ─────────────────────────────────────────────────────────────────────
|
|
7
|
+
//
|
|
8
|
+
// const tmpl = new Template(`
|
|
9
|
+
// function {name}(x, y) {
|
|
10
|
+
// return x + y;
|
|
11
|
+
// }
|
|
12
|
+
// `);
|
|
13
|
+
//
|
|
14
|
+
// const bc = tmpl.compile({ name: "myHelper" }, parentCompiler);
|
|
15
|
+
// result.push(...bc);
|
|
16
|
+
//
|
|
17
|
+
// ── How it works ──────────────────────────────────────────────────────────────
|
|
18
|
+
//
|
|
19
|
+
// 1. {name} placeholders are replaced with the caller-supplied string values.
|
|
20
|
+
// 2. A fresh child Compiler is created, inheriting the parent's OP table so
|
|
21
|
+
// opcode numbers match exactly (including randomizeOpcodes mappings).
|
|
22
|
+
// 3. The child compiles the snippet to raw IR (no passes, no label/register
|
|
23
|
+
// resolution).
|
|
24
|
+
// 4. Post-processing makes the child's bytecode compatible with the parent:
|
|
25
|
+
//
|
|
26
|
+
// Labels — every label string is renamed via parentCompiler._makeLabel()
|
|
27
|
+
// so names never collide with existing or future labels.
|
|
28
|
+
//
|
|
29
|
+
// FnIds — the child's main scope (fnDescriptors[0]) is mapped to
|
|
30
|
+
// targetFnId (default 0). Any inner functions (closures
|
|
31
|
+
// declared inside the template) are appended to
|
|
32
|
+
// parentCompiler.fnDescriptors with fresh indices.
|
|
33
|
+
//
|
|
34
|
+
// 5. The main function's entry defineLabel is stripped from the output — it is
|
|
35
|
+
// a synthetic wrapper added by _compileMain and is not part of the injected
|
|
36
|
+
// code. All other instructions (including the implicit RETURN at the end of
|
|
37
|
+
// the main scope and any inner-function blocks) are returned as-is so the
|
|
38
|
+
// caller can append them wherever appropriate.
|
|
39
|
+
//
|
|
40
|
+
// ── Limitations (MVP) ─────────────────────────────────────────────────────────
|
|
41
|
+
// • Variables are plain string/number interpolation only — no AST-node
|
|
42
|
+
// substitution.
|
|
43
|
+
// • Templates that reference upvalue-captured registers from the call site are
|
|
44
|
+
// not supported (inner functions closing over template-local variables work).
|
|
45
|
+
// • Opcodes with no JS equivalent (JUMP_REG, BXOR used as decode, etc.) cannot
|
|
46
|
+
// be expressed in a template; write those instruction arrays manually.
|
|
47
|
+
|
|
48
|
+
import { Compiler } from "./compiler.ts";
|
|
49
|
+
import { DEFAULT_OPTIONS } from "./options.ts";
|
|
50
|
+
import type { Bytecode, Instruction, RegisterOperand } from "./types.ts";
|
|
51
|
+
|
|
52
|
+
export class Template {
|
|
53
|
+
private readonly _source: string;
|
|
54
|
+
|
|
55
|
+
constructor(source: string) {
|
|
56
|
+
this._source = source;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// ── String interpolation ──────────────────────────────────────────────────
|
|
60
|
+
private _interpolate(variables: Record<string, string | number>): string {
|
|
61
|
+
return this._source.replace(/\{(\w+)\}/g, (match, name) => {
|
|
62
|
+
if (!(name in variables)) {
|
|
63
|
+
throw new Error(`Template: missing variable {${name}}`);
|
|
64
|
+
}
|
|
65
|
+
return String(variables[name]);
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// ── Main entry point ───────────────────────────────────────────────────────
|
|
70
|
+
/**
|
|
71
|
+
* Compile the template and return the inner (non-main) function descriptors
|
|
72
|
+
* and their bytecode blocks, ready to splice into the parent compiler's
|
|
73
|
+
* instruction stream.
|
|
74
|
+
*
|
|
75
|
+
* The template source should declare one or more named functions. The
|
|
76
|
+
* top-level ("main") scope of the template is discarded — it exists only as
|
|
77
|
+
* a syntactic wrapper so that function declarations parse correctly.
|
|
78
|
+
*
|
|
79
|
+
* Each inner function is registered in parentCompiler.fnDescriptors with a
|
|
80
|
+
* fresh fnIdx, and its bytecode block (defineLabel + body instructions) is
|
|
81
|
+
* returned so the caller can append it to the parent bytecode stream at the
|
|
82
|
+
* desired location (typically at the end, after all function bodies).
|
|
83
|
+
*
|
|
84
|
+
* @param variables Substitution map for {name} placeholders.
|
|
85
|
+
* @param parentCompiler The Compiler whose OP table, label counter, and
|
|
86
|
+
* fnDescriptors are shared.
|
|
87
|
+
*
|
|
88
|
+
* @returns
|
|
89
|
+
* functions — ordered list of inner FnDescriptors (index 0 = first named
|
|
90
|
+
* function in the template source). Use .entryLabel and
|
|
91
|
+
* ._fnIdx to build MAKE_CLOSURE operands.
|
|
92
|
+
* bytecode — IR bytecode blocks for all inner functions, ready to splice
|
|
93
|
+
* after the parent's function bodies. Does NOT include the
|
|
94
|
+
* template's main-scope instructions.
|
|
95
|
+
*/
|
|
96
|
+
compile(
|
|
97
|
+
variables: Record<string, string | number>,
|
|
98
|
+
parentCompiler: Compiler,
|
|
99
|
+
): { functions: any[]; bytecode: Bytecode } {
|
|
100
|
+
// ── 1. Interpolate ────────────────────────────────────────────────────
|
|
101
|
+
const code = this._interpolate(variables);
|
|
102
|
+
|
|
103
|
+
// ── 2. Create child compiler, inherit parent's OP table ───────────────
|
|
104
|
+
// randomizeOpcodes is disabled — we copy the parent's already-randomized
|
|
105
|
+
// mapping directly so all opcode numbers are identical.
|
|
106
|
+
const child = new Compiler({ ...DEFAULT_OPTIONS, randomizeOpcodes: false });
|
|
107
|
+
child.OP = { ...parentCompiler.OP };
|
|
108
|
+
child.OP_NAME = { ...parentCompiler.OP_NAME };
|
|
109
|
+
child.JUMP_OPS = new Set(parentCompiler.JUMP_OPS);
|
|
110
|
+
|
|
111
|
+
child._makeLabel = parentCompiler._makeLabel.bind(parentCompiler);
|
|
112
|
+
|
|
113
|
+
// Record how many descriptors the parent already has so we can find the
|
|
114
|
+
// child's main (index = startIdx) and inner functions (startIdx+1 …).
|
|
115
|
+
const startIdx = parentCompiler.fnDescriptors.length;
|
|
116
|
+
child.fnDescriptors = parentCompiler.fnDescriptors; // share — inner functions auto-register
|
|
117
|
+
|
|
118
|
+
// ── 3. Compile to raw IR (no passes) ──────────────────────────────────
|
|
119
|
+
child.compile(code);
|
|
120
|
+
|
|
121
|
+
// parentCompiler.fnDescriptors[startIdx] → child's main (discard)
|
|
122
|
+
// parentCompiler.fnDescriptors[startIdx+1…] → inner helper functions
|
|
123
|
+
const innerDescs = parentCompiler.fnDescriptors.slice(startIdx + 1);
|
|
124
|
+
|
|
125
|
+
// Build bytecode blocks for inner functions only.
|
|
126
|
+
// child.bytecode was assembled by _compileMain from ALL fnDescriptors
|
|
127
|
+
// starting at startIdx. We rebuild it here from the inner descs only.
|
|
128
|
+
const innerBytecode: Bytecode = [];
|
|
129
|
+
for (const desc of innerDescs) {
|
|
130
|
+
innerBytecode.push([
|
|
131
|
+
null,
|
|
132
|
+
{ type: "defineLabel", label: desc.entryLabel },
|
|
133
|
+
] as Instruction);
|
|
134
|
+
for (const instr of (desc as any).bytecode as Bytecode) {
|
|
135
|
+
innerBytecode.push(instr);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
return { functions: innerDescs, bytecode: innerBytecode };
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// ── Inline compilation ───────────────────────────────────────────────────
|
|
143
|
+
/**
|
|
144
|
+
* Compile the template and return the **main scope** bytecode, with all
|
|
145
|
+
* register operands remapped to belong to `targetFnId`. This allows
|
|
146
|
+
* bytecode transforms to express high-level JS control flow (while-loops,
|
|
147
|
+
* if-chains, variable declarations) via Template and splice the result
|
|
148
|
+
* directly into an existing function's instruction stream.
|
|
149
|
+
*
|
|
150
|
+
* The implicit trailing RETURN added by _compileFunctionDecl is stripped —
|
|
151
|
+
* inline code should flow into the surrounding bytecode, not return.
|
|
152
|
+
*
|
|
153
|
+
* @param variables Substitution map for {name} placeholders.
|
|
154
|
+
* @param parentCompiler The Compiler whose OP table, label counter, and
|
|
155
|
+
* fnDescriptors are shared.
|
|
156
|
+
* @param targetFnId The function whose register file the template's
|
|
157
|
+
* registers should be remapped into.
|
|
158
|
+
* @param maxId Live map of max register id per fnId — updated
|
|
159
|
+
* in-place as new registers are allocated.
|
|
160
|
+
*
|
|
161
|
+
* @returns
|
|
162
|
+
* bytecode — main-scope IR (no entry defineLabel, no trailing RETURN),
|
|
163
|
+
* ready to splice into the target function's instruction stream.
|
|
164
|
+
* registers — mapping of JS variable names → remapped RegisterOperands,
|
|
165
|
+
* so the caller can reference template-declared variables
|
|
166
|
+
* (e.g. the `state` variable in CFF).
|
|
167
|
+
* functions — inner function descriptors (same as compile()).
|
|
168
|
+
* innerBytecode — inner function bytecode blocks (same as compile()).
|
|
169
|
+
*/
|
|
170
|
+
compileInline(
|
|
171
|
+
variables: Record<string, string | number>,
|
|
172
|
+
parentCompiler: Compiler,
|
|
173
|
+
targetFnId: number,
|
|
174
|
+
maxId: Map<number, number>,
|
|
175
|
+
): {
|
|
176
|
+
bytecode: Bytecode;
|
|
177
|
+
registers: Map<string, RegisterOperand>;
|
|
178
|
+
functions: any[];
|
|
179
|
+
innerBytecode: Bytecode;
|
|
180
|
+
} {
|
|
181
|
+
const code = this._interpolate(variables);
|
|
182
|
+
|
|
183
|
+
const child = new Compiler({ ...DEFAULT_OPTIONS, randomizeOpcodes: false });
|
|
184
|
+
child.OP = { ...parentCompiler.OP };
|
|
185
|
+
child.OP_NAME = { ...parentCompiler.OP_NAME };
|
|
186
|
+
child.JUMP_OPS = new Set(parentCompiler.JUMP_OPS);
|
|
187
|
+
child._makeLabel = parentCompiler._makeLabel.bind(parentCompiler);
|
|
188
|
+
|
|
189
|
+
const startIdx = parentCompiler.fnDescriptors.length;
|
|
190
|
+
child.fnDescriptors = parentCompiler.fnDescriptors;
|
|
191
|
+
|
|
192
|
+
child.compile(code);
|
|
193
|
+
|
|
194
|
+
const mainDesc = parentCompiler.fnDescriptors[startIdx] as any;
|
|
195
|
+
const mainFnId: number = mainDesc._fnIdx;
|
|
196
|
+
const mainBc = mainDesc.bytecode as Bytecode;
|
|
197
|
+
|
|
198
|
+
// ── Remap registers from the template's main fnId → targetFnId ────────
|
|
199
|
+
// Build a mapping: old register id → new RegisterOperand in targetFnId.
|
|
200
|
+
const regRemap = new Map<number, RegisterOperand>();
|
|
201
|
+
const remapReg = (id: number): RegisterOperand => {
|
|
202
|
+
if (!regRemap.has(id)) {
|
|
203
|
+
const next = (maxId.get(targetFnId) ?? -1) + 1;
|
|
204
|
+
maxId.set(targetFnId, next);
|
|
205
|
+
regRemap.set(id, { type: "register", id: next, fnId: targetFnId });
|
|
206
|
+
}
|
|
207
|
+
return regRemap.get(id)!;
|
|
208
|
+
};
|
|
209
|
+
|
|
210
|
+
for (const instr of mainBc) {
|
|
211
|
+
for (let j = 1; j < instr.length; j++) {
|
|
212
|
+
const op = instr[j] as any;
|
|
213
|
+
if (op && typeof op === "object" && op.type === "register" && op.fnId === mainFnId) {
|
|
214
|
+
const mapped = remapReg(op.id);
|
|
215
|
+
op.id = mapped.id;
|
|
216
|
+
op.fnId = mapped.fnId;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
// ── Build variable name → remapped register mapping ───────────────────
|
|
222
|
+
const registers = new Map<string, RegisterOperand>();
|
|
223
|
+
const locals: Map<string, RegisterOperand> = mainDesc.ctx.scope._locals;
|
|
224
|
+
for (const [name, reg] of locals) {
|
|
225
|
+
const mapped = regRemap.get(reg.id);
|
|
226
|
+
if (mapped) registers.set(name, mapped);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
// ── Strip entry defineLabel and trailing implicit RETURN ───────────────
|
|
230
|
+
let bytecode = mainBc.filter((instr) => {
|
|
231
|
+
const op0 = instr[1] as any;
|
|
232
|
+
return !(
|
|
233
|
+
instr[0] === null &&
|
|
234
|
+
op0?.type === "defineLabel" &&
|
|
235
|
+
op0.label === mainDesc.entryLabel
|
|
236
|
+
);
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
// Remove trailing LOAD_CONST undefined + RETURN (implicit return added
|
|
240
|
+
// by _compileFunctionDecl).
|
|
241
|
+
const OP = parentCompiler.OP;
|
|
242
|
+
if (
|
|
243
|
+
bytecode.length >= 2 &&
|
|
244
|
+
bytecode[bytecode.length - 1][0] === OP.RETURN &&
|
|
245
|
+
bytecode[bytecode.length - 2][0] === OP.LOAD_CONST
|
|
246
|
+
) {
|
|
247
|
+
bytecode = bytecode.slice(0, -2);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
// ── Inner function bytecode (same as compile()) ───────────────────────
|
|
251
|
+
const innerDescs = parentCompiler.fnDescriptors.slice(startIdx + 1);
|
|
252
|
+
const innerBytecode: Bytecode = [];
|
|
253
|
+
for (const desc of innerDescs) {
|
|
254
|
+
innerBytecode.push([
|
|
255
|
+
null,
|
|
256
|
+
{ type: "defineLabel", label: desc.entryLabel },
|
|
257
|
+
] as Instruction);
|
|
258
|
+
for (const instr of (desc as any).bytecode as Bytecode) {
|
|
259
|
+
innerBytecode.push(instr);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
return { bytecode, registers, functions: innerDescs, innerBytecode };
|
|
264
|
+
}
|
|
265
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { Bytecode, InstrOperand, Instruction } from "../../types.ts";
|
|
2
|
-
import { Compiler, SOURCE_NODE_SYM } from "../../compiler.ts";
|
|
3
|
-
import { nextFreeSlot
|
|
2
|
+
import { Compiler, OP_ORIGINAL, SOURCE_NODE_SYM } from "../../compiler.ts";
|
|
3
|
+
import { nextFreeSlot } from "../../utils/op-utils.ts";
|
|
4
4
|
import { shuffle } from "../../utils/random-utils.ts";
|
|
5
5
|
|
|
6
6
|
// Opcodes that must not be aliased.
|
|
@@ -58,6 +58,9 @@ export function aliasedOpcodes(
|
|
|
58
58
|
const arity = instr.length - 1;
|
|
59
59
|
if (arity < 1) continue; // 0-operand opcodes have nothing to permute
|
|
60
60
|
|
|
61
|
+
const opName = compiler.OP_NAME[op];
|
|
62
|
+
if (!OP_ORIGINAL[opName]) continue; // only consider original ops, not already-specialized ones
|
|
63
|
+
|
|
61
64
|
const existing = opStats.get(op);
|
|
62
65
|
if (!existing) {
|
|
63
66
|
opStats.set(op, { freq: 1, arity });
|