js-confuser 1.7.3 → 2.0.0-alpha.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.
Files changed (269) hide show
  1. package/.github/ISSUE_TEMPLATE/bug_report.md +6 -4
  2. package/CHANGELOG.md +70 -0
  3. package/Migration.md +57 -0
  4. package/README.md +23 -929
  5. package/dist/constants.js +65 -14
  6. package/dist/index.js +108 -160
  7. package/dist/obfuscator.js +316 -118
  8. package/dist/options.js +1 -119
  9. package/dist/order.js +30 -30
  10. package/dist/presets.js +47 -45
  11. package/dist/probability.js +25 -32
  12. package/dist/templates/bufferToStringTemplate.js +9 -0
  13. package/dist/templates/deadCodeTemplates.js +9 -0
  14. package/dist/templates/getGlobalTemplate.js +19 -0
  15. package/dist/templates/integrityTemplate.js +30 -0
  16. package/dist/templates/setFunctionLengthTemplate.js +9 -0
  17. package/dist/templates/stringCompressionTemplate.js +10 -0
  18. package/dist/templates/tamperProtectionTemplates.js +21 -0
  19. package/dist/templates/template.js +199 -184
  20. package/dist/transforms/astScrambler.js +100 -0
  21. package/dist/transforms/calculator.js +70 -127
  22. package/dist/transforms/controlFlowFlattening.js +1182 -0
  23. package/dist/transforms/deadCode.js +62 -587
  24. package/dist/transforms/dispatcher.js +300 -313
  25. package/dist/transforms/extraction/duplicateLiteralsRemoval.js +88 -189
  26. package/dist/transforms/extraction/objectExtraction.js +131 -215
  27. package/dist/transforms/finalizer.js +56 -59
  28. package/dist/transforms/flatten.js +275 -276
  29. package/dist/transforms/functionOutlining.js +230 -0
  30. package/dist/transforms/identifier/globalConcealing.js +214 -135
  31. package/dist/transforms/identifier/movedDeclarations.js +167 -91
  32. package/dist/transforms/identifier/renameVariables.js +239 -193
  33. package/dist/transforms/lock/integrity.js +61 -184
  34. package/dist/transforms/lock/lock.js +261 -387
  35. package/dist/transforms/minify.js +431 -436
  36. package/dist/transforms/opaquePredicates.js +65 -118
  37. package/dist/transforms/pack.js +160 -0
  38. package/dist/transforms/plugin.js +179 -0
  39. package/dist/transforms/preparation.js +261 -173
  40. package/dist/transforms/renameLabels.js +132 -56
  41. package/dist/transforms/rgf.js +140 -267
  42. package/dist/transforms/shuffle.js +52 -145
  43. package/dist/transforms/string/encoding.js +44 -175
  44. package/dist/transforms/string/stringCompression.js +79 -155
  45. package/dist/transforms/string/stringConcealing.js +189 -225
  46. package/dist/transforms/string/stringEncoding.js +32 -40
  47. package/dist/transforms/string/stringSplitting.js +54 -55
  48. package/dist/transforms/variableMasking.js +232 -0
  49. package/dist/utils/ControlObject.js +125 -0
  50. package/dist/utils/IntGen.js +46 -0
  51. package/dist/utils/NameGen.js +106 -0
  52. package/dist/utils/ast-utils.js +560 -0
  53. package/dist/utils/function-utils.js +56 -0
  54. package/dist/utils/gen-utils.js +48 -0
  55. package/dist/utils/node.js +77 -0
  56. package/dist/utils/object-utils.js +21 -0
  57. package/dist/utils/random-utils.js +91 -0
  58. package/dist/utils/static-utils.js +64 -0
  59. package/dist/validateOptions.js +122 -0
  60. package/index.d.ts +1 -17
  61. package/package.json +27 -22
  62. package/src/constants.ts +139 -82
  63. package/src/index.ts +70 -165
  64. package/src/obfuscationResult.ts +43 -0
  65. package/src/obfuscator.ts +328 -135
  66. package/src/options.ts +149 -658
  67. package/src/order.ts +14 -14
  68. package/src/presets.ts +39 -34
  69. package/src/probability.ts +21 -36
  70. package/src/templates/bufferToStringTemplate.ts +57 -0
  71. package/src/templates/deadCodeTemplates.ts +1185 -0
  72. package/src/templates/getGlobalTemplate.ts +72 -0
  73. package/src/templates/integrityTemplate.ts +69 -0
  74. package/src/templates/setFunctionLengthTemplate.ts +11 -0
  75. package/src/templates/stringCompressionTemplate.ts +42 -0
  76. package/src/templates/tamperProtectionTemplates.ts +116 -0
  77. package/src/templates/template.ts +149 -157
  78. package/src/transforms/astScrambler.ts +99 -0
  79. package/src/transforms/calculator.ts +96 -226
  80. package/src/transforms/controlFlowFlattening.ts +1594 -0
  81. package/src/transforms/deadCode.ts +85 -676
  82. package/src/transforms/dispatcher.ts +431 -640
  83. package/src/transforms/extraction/duplicateLiteralsRemoval.ts +147 -295
  84. package/src/transforms/extraction/objectExtraction.ts +160 -333
  85. package/src/transforms/finalizer.ts +63 -64
  86. package/src/transforms/flatten.ts +439 -557
  87. package/src/transforms/functionOutlining.ts +225 -0
  88. package/src/transforms/identifier/globalConcealing.ts +255 -266
  89. package/src/transforms/identifier/movedDeclarations.ts +228 -142
  90. package/src/transforms/identifier/renameVariables.ts +250 -271
  91. package/src/transforms/lock/integrity.ts +85 -263
  92. package/src/transforms/lock/lock.ts +338 -579
  93. package/src/transforms/minify.ts +523 -663
  94. package/src/transforms/opaquePredicates.ts +90 -229
  95. package/src/transforms/pack.ts +195 -0
  96. package/src/transforms/plugin.ts +185 -0
  97. package/src/transforms/preparation.ts +337 -231
  98. package/src/transforms/renameLabels.ts +176 -77
  99. package/src/transforms/rgf.ts +293 -424
  100. package/src/transforms/shuffle.ts +80 -254
  101. package/src/transforms/string/encoding.ts +20 -126
  102. package/src/transforms/string/stringCompression.ts +117 -307
  103. package/src/transforms/string/stringConcealing.ts +254 -342
  104. package/src/transforms/string/stringEncoding.ts +28 -47
  105. package/src/transforms/string/stringSplitting.ts +61 -75
  106. package/src/transforms/variableMasking.ts +257 -0
  107. package/src/utils/ControlObject.ts +141 -0
  108. package/src/utils/IntGen.ts +33 -0
  109. package/src/utils/NameGen.ts +106 -0
  110. package/src/utils/ast-utils.ts +667 -0
  111. package/src/utils/function-utils.ts +50 -0
  112. package/src/utils/gen-utils.ts +48 -0
  113. package/src/utils/node.ts +78 -0
  114. package/src/utils/object-utils.ts +21 -0
  115. package/src/utils/random-utils.ts +79 -0
  116. package/src/utils/static-utils.ts +66 -0
  117. package/src/validateOptions.ts +256 -0
  118. package/tsconfig.json +13 -8
  119. package/babel.config.js +0 -12
  120. package/dev.js +0 -8
  121. package/dist/compiler.js +0 -34
  122. package/dist/parser.js +0 -59
  123. package/dist/precedence.js +0 -66
  124. package/dist/templates/bufferToString.js +0 -129
  125. package/dist/templates/core.js +0 -35
  126. package/dist/templates/crash.js +0 -28
  127. package/dist/templates/es5.js +0 -137
  128. package/dist/templates/functionLength.js +0 -34
  129. package/dist/templates/globals.js +0 -9
  130. package/dist/transforms/antiTooling.js +0 -88
  131. package/dist/transforms/controlFlowFlattening/controlFlowFlattening.js +0 -1287
  132. package/dist/transforms/controlFlowFlattening/expressionObfuscation.js +0 -131
  133. package/dist/transforms/es5/antiClass.js +0 -164
  134. package/dist/transforms/es5/antiDestructuring.js +0 -193
  135. package/dist/transforms/es5/antiES6Object.js +0 -185
  136. package/dist/transforms/es5/antiSpreadOperator.js +0 -35
  137. package/dist/transforms/es5/antiTemplate.js +0 -66
  138. package/dist/transforms/es5/es5.js +0 -123
  139. package/dist/transforms/extraction/classExtraction.js +0 -83
  140. package/dist/transforms/identifier/globalAnalysis.js +0 -83
  141. package/dist/transforms/identifier/variableAnalysis.js +0 -104
  142. package/dist/transforms/lock/antiDebug.js +0 -76
  143. package/dist/transforms/stack.js +0 -349
  144. package/dist/transforms/transform.js +0 -372
  145. package/dist/traverse.js +0 -110
  146. package/dist/util/compare.js +0 -145
  147. package/dist/util/gen.js +0 -564
  148. package/dist/util/guard.js +0 -14
  149. package/dist/util/identifiers.js +0 -355
  150. package/dist/util/insert.js +0 -362
  151. package/dist/util/math.js +0 -19
  152. package/dist/util/object.js +0 -40
  153. package/dist/util/random.js +0 -156
  154. package/dist/util/scope.js +0 -20
  155. package/docs/ControlFlowFlattening.md +0 -595
  156. package/docs/Countermeasures.md +0 -70
  157. package/docs/ES5.md +0 -197
  158. package/docs/Integrity.md +0 -82
  159. package/docs/RGF.md +0 -424
  160. package/docs/RenameVariables.md +0 -116
  161. package/docs/TamperProtection.md +0 -100
  162. package/docs/Template.md +0 -117
  163. package/samples/example.js +0 -15
  164. package/samples/high.js +0 -1
  165. package/samples/input.js +0 -3
  166. package/samples/javascriptobfuscator.com.js +0 -8
  167. package/samples/jscrambler_advanced.js +0 -1894
  168. package/samples/jscrambler_light.js +0 -1134
  169. package/samples/low.js +0 -1
  170. package/samples/medium.js +0 -1
  171. package/samples/obfuscator.io.js +0 -1686
  172. package/samples/preemptive.com.js +0 -16
  173. package/src/compiler.ts +0 -35
  174. package/src/parser.ts +0 -49
  175. package/src/precedence.ts +0 -61
  176. package/src/templates/bufferToString.ts +0 -136
  177. package/src/templates/core.ts +0 -29
  178. package/src/templates/crash.ts +0 -23
  179. package/src/templates/es5.ts +0 -131
  180. package/src/templates/functionLength.ts +0 -32
  181. package/src/templates/globals.ts +0 -3
  182. package/src/transforms/antiTooling.ts +0 -102
  183. package/src/transforms/controlFlowFlattening/controlFlowFlattening.ts +0 -2153
  184. package/src/transforms/controlFlowFlattening/expressionObfuscation.ts +0 -179
  185. package/src/transforms/es5/antiClass.ts +0 -276
  186. package/src/transforms/es5/antiDestructuring.ts +0 -294
  187. package/src/transforms/es5/antiES6Object.ts +0 -267
  188. package/src/transforms/es5/antiSpreadOperator.ts +0 -56
  189. package/src/transforms/es5/antiTemplate.ts +0 -98
  190. package/src/transforms/es5/es5.ts +0 -149
  191. package/src/transforms/extraction/classExtraction.ts +0 -168
  192. package/src/transforms/identifier/globalAnalysis.ts +0 -102
  193. package/src/transforms/identifier/variableAnalysis.ts +0 -118
  194. package/src/transforms/lock/antiDebug.ts +0 -112
  195. package/src/transforms/stack.ts +0 -557
  196. package/src/transforms/transform.ts +0 -441
  197. package/src/traverse.ts +0 -120
  198. package/src/types.ts +0 -133
  199. package/src/util/compare.ts +0 -181
  200. package/src/util/gen.ts +0 -651
  201. package/src/util/guard.ts +0 -17
  202. package/src/util/identifiers.ts +0 -494
  203. package/src/util/insert.ts +0 -419
  204. package/src/util/math.ts +0 -15
  205. package/src/util/object.ts +0 -39
  206. package/src/util/random.ts +0 -221
  207. package/src/util/scope.ts +0 -21
  208. package/test/code/Cash.src.js +0 -1011
  209. package/test/code/Cash.test.ts +0 -132
  210. package/test/code/Dynamic.src.js +0 -118
  211. package/test/code/Dynamic.test.ts +0 -49
  212. package/test/code/ES6.src.js +0 -235
  213. package/test/code/ES6.test.ts +0 -42
  214. package/test/code/NewFeatures.test.ts +0 -19
  215. package/test/code/StrictMode.src.js +0 -65
  216. package/test/code/StrictMode.test.js +0 -37
  217. package/test/compare.test.ts +0 -104
  218. package/test/index.test.ts +0 -249
  219. package/test/options.test.ts +0 -150
  220. package/test/presets.test.ts +0 -22
  221. package/test/probability.test.ts +0 -44
  222. package/test/templates/template.test.ts +0 -224
  223. package/test/transforms/antiTooling.test.ts +0 -52
  224. package/test/transforms/calculator.test.ts +0 -78
  225. package/test/transforms/controlFlowFlattening/controlFlowFlattening.test.ts +0 -1274
  226. package/test/transforms/controlFlowFlattening/expressionObfuscation.test.ts +0 -192
  227. package/test/transforms/deadCode.test.ts +0 -85
  228. package/test/transforms/dispatcher.test.ts +0 -457
  229. package/test/transforms/es5/antiClass.test.ts +0 -427
  230. package/test/transforms/es5/antiDestructuring.test.ts +0 -157
  231. package/test/transforms/es5/antiES6Object.test.ts +0 -245
  232. package/test/transforms/es5/antiTemplate.test.ts +0 -116
  233. package/test/transforms/es5/es5.test.ts +0 -110
  234. package/test/transforms/extraction/classExtraction.test.ts +0 -86
  235. package/test/transforms/extraction/duplicateLiteralsRemoval.test.ts +0 -200
  236. package/test/transforms/extraction/objectExtraction.test.ts +0 -491
  237. package/test/transforms/flatten.test.ts +0 -721
  238. package/test/transforms/hexadecimalNumbers.test.ts +0 -62
  239. package/test/transforms/identifier/globalConcealing.test.ts +0 -142
  240. package/test/transforms/identifier/movedDeclarations.test.ts +0 -275
  241. package/test/transforms/identifier/renameVariables.test.ts +0 -695
  242. package/test/transforms/lock/antiDebug.test.ts +0 -66
  243. package/test/transforms/lock/browserLock.test.ts +0 -129
  244. package/test/transforms/lock/countermeasures.test.ts +0 -100
  245. package/test/transforms/lock/integrity.test.ts +0 -161
  246. package/test/transforms/lock/lock.test.ts +0 -204
  247. package/test/transforms/lock/osLock.test.ts +0 -312
  248. package/test/transforms/lock/selfDefending.test.ts +0 -68
  249. package/test/transforms/lock/tamperProtection.test.ts +0 -336
  250. package/test/transforms/minify.test.ts +0 -575
  251. package/test/transforms/opaquePredicates.test.ts +0 -43
  252. package/test/transforms/preparation.test.ts +0 -157
  253. package/test/transforms/renameLabels.test.ts +0 -95
  254. package/test/transforms/rgf.test.ts +0 -378
  255. package/test/transforms/shuffle.test.ts +0 -135
  256. package/test/transforms/stack.test.ts +0 -573
  257. package/test/transforms/string/stringCompression.test.ts +0 -120
  258. package/test/transforms/string/stringConcealing.test.ts +0 -299
  259. package/test/transforms/string/stringEncoding.test.ts +0 -95
  260. package/test/transforms/string/stringSplitting.test.ts +0 -135
  261. package/test/transforms/transform.test.ts +0 -66
  262. package/test/traverse.test.ts +0 -139
  263. package/test/util/compare.test.ts +0 -34
  264. package/test/util/gen.test.ts +0 -121
  265. package/test/util/identifiers.test.ts +0 -253
  266. package/test/util/insert.test.ts +0 -142
  267. package/test/util/math.test.ts +0 -5
  268. package/test/util/random.test.ts +0 -71
  269. /package/dist/{types.js → obfuscationResult.js} +0 -0
@@ -1,640 +1,431 @@
1
- import { walk } from "../traverse";
2
- import {
3
- ArrayExpression,
4
- AssignmentExpression,
5
- BinaryExpression,
6
- CallExpression,
7
- ExpressionStatement,
8
- FunctionDeclaration,
9
- FunctionExpression,
10
- Identifier,
11
- IfStatement,
12
- Literal,
13
- Node,
14
- Location,
15
- MemberExpression,
16
- ObjectExpression,
17
- Property,
18
- ReturnStatement,
19
- VariableDeclaration,
20
- SequenceExpression,
21
- NewExpression,
22
- UnaryExpression,
23
- BlockStatement,
24
- LogicalExpression,
25
- ThisExpression,
26
- VariableDeclarator,
27
- RestElement,
28
- } from "../util/gen";
29
- import { getIdentifierInfo } from "../util/identifiers";
30
- import {
31
- deleteDirect,
32
- getBlockBody,
33
- getVarContext,
34
- isVarContext,
35
- prepend,
36
- append,
37
- computeFunctionLength,
38
- isFunction,
39
- } from "../util/insert";
40
- import Transform from "./transform";
41
- import { isInsideType } from "../util/compare";
42
- import { choice, shuffle } from "../util/random";
43
- import { ComputeProbabilityMap } from "../probability";
44
- import { predictableFunctionTag, reservedIdentifiers } from "../constants";
45
- import { ObfuscateOrder } from "../order";
46
- import Template from "../templates/template";
47
- import { FunctionLengthTemplate } from "../templates/functionLength";
48
- import { ObjectDefineProperty } from "../templates/globals";
49
- import { getLexicalScope } from "../util/scope";
50
- import { isJSConfuserVar } from "../util/guard";
51
-
52
- /**
53
- * A Dispatcher processes function calls. All the function declarations are brought into a dictionary.
54
- *
55
- * ```js
56
- * var param1;
57
- * function dispatcher(key){
58
- * var fns = {
59
- * 'fn1': function(){
60
- * var [arg1] = [param1];
61
- * console.log(arg1);
62
- * }
63
- * }
64
- * return fns[key]();
65
- * };
66
- * param1 = "Hello World";
67
- * dispatcher('fn1'); // > "Hello World"
68
- * ```
69
- *
70
- * Can break code with:
71
- *
72
- * 1. testing function equality,
73
- * 2. using `arguments.callee`,
74
- * 3. using `this`
75
- */
76
- export default class Dispatcher extends Transform {
77
- // Debug mode preserves function names
78
- isDebug = false;
79
- count: number;
80
-
81
- functionLengthName: string;
82
-
83
- constructor(o) {
84
- super(o, ObfuscateOrder.Dispatcher);
85
-
86
- this.count = 0;
87
- }
88
-
89
- apply(tree: Node): void {
90
- super.apply(tree);
91
-
92
- if (this.options.preserveFunctionLength && this.functionLengthName) {
93
- prepend(
94
- tree,
95
- FunctionLengthTemplate.single({
96
- name: this.functionLengthName,
97
- ObjectDefineProperty: this.createInitVariable(ObjectDefineProperty, [
98
- tree,
99
- ]),
100
- })
101
- );
102
- }
103
- }
104
-
105
- match(object: Node, parents: Node[]) {
106
- if (isInsideType("AwaitExpression", object, parents)) {
107
- return false;
108
- }
109
-
110
- return (
111
- isVarContext(object) &&
112
- object.type !== "ArrowFunctionExpression" &&
113
- !object.$multiTransformSkip &&
114
- !parents.find((x) => x.$multiTransformSkip)
115
- );
116
- }
117
-
118
- transform(object: Node, parents: Node[]) {
119
- return () => {
120
- if (ComputeProbabilityMap(this.options.dispatcher, (mode) => mode)) {
121
- if (object.type != "Program" && object.body.type != "BlockStatement") {
122
- return;
123
- }
124
-
125
- // Map of FunctionDeclarations
126
- var functionDeclarations: { [name: string]: Location } =
127
- Object.create(null);
128
-
129
- // Array of Identifier nodes
130
- var identifiers: Location[] = [];
131
- var illegalFnNames: Set<string> = new Set();
132
-
133
- // New Names for Functions
134
- var newFnNames: { [name: string]: string } = Object.create(null); // [old name]: randomized name
135
-
136
- var context = isVarContext(object)
137
- ? object
138
- : getVarContext(object, parents);
139
-
140
- var lexicalScope = isFunction(context) ? context.body : context;
141
-
142
- walk(object, parents, (o: Node, p: Node[]) => {
143
- if (object == o) {
144
- // Fix 1
145
- return;
146
- }
147
-
148
- var c = getVarContext(o, p);
149
- if (o.type == "FunctionDeclaration") {
150
- c = getVarContext(p[0], p.slice(1));
151
- }
152
-
153
- if (context === c) {
154
- if (o.type == "FunctionDeclaration" && o.id.name) {
155
- var name = o.id.name;
156
-
157
- if (
158
- o.$requiresEval ||
159
- o.async ||
160
- o.generator ||
161
- p.find(
162
- (x) => x.$multiTransformSkip || x.type == "MethodDefinition"
163
- ) ||
164
- o.body.type != "BlockStatement"
165
- ) {
166
- illegalFnNames.add(name);
167
- return;
168
- }
169
-
170
- // Must defined in the same block as the current function being scanned
171
- // Solves 'let' and 'class' declaration issue
172
- var ls = getLexicalScope(o, p);
173
- if (ls !== lexicalScope) {
174
- illegalFnNames.add(name);
175
- return;
176
- }
177
-
178
- // If dupe, no routing
179
- if (functionDeclarations[name]) {
180
- illegalFnNames.add(name);
181
- return;
182
- }
183
-
184
- walk(o, p, (oo, pp) => {
185
- if (
186
- (oo.type == "Identifier" && oo.name == "arguments") ||
187
- oo.type == "ThisExpression" ||
188
- oo.type == "Super"
189
- ) {
190
- if (getVarContext(oo, pp) === o) {
191
- illegalFnNames.add(name);
192
- return "EXIT";
193
- }
194
- }
195
-
196
- // Avoid functions with function expressions as they have a different scope
197
- if (
198
- (oo.type === "FunctionExpression" ||
199
- oo.type === "ArrowFunctionExpression") &&
200
- pp.find((x) => x == o.params)
201
- ) {
202
- illegalFnNames.add(name);
203
- return "EXIT";
204
- }
205
- });
206
-
207
- functionDeclarations[name] = [o, p];
208
- }
209
- }
210
-
211
- if (o.type == "Identifier") {
212
- if (reservedIdentifiers.has(o.name)) {
213
- return;
214
- }
215
- var info = getIdentifierInfo(o, p);
216
- if (!info.spec.isReferenced) {
217
- return;
218
- }
219
- if (isJSConfuserVar(p)) {
220
- illegalFnNames.add(o.name);
221
- }
222
- if (info.spec.isDefined) {
223
- if (info.isFunctionDeclaration) {
224
- if (
225
- p[0].id &&
226
- (!functionDeclarations[p[0].id.name] ||
227
- functionDeclarations[p[0].id.name][0] !== p[0])
228
- ) {
229
- illegalFnNames.add(o.name);
230
- }
231
- } else {
232
- illegalFnNames.add(o.name);
233
- }
234
- } else if (info.spec.isModified) {
235
- illegalFnNames.add(o.name);
236
- } else {
237
- identifiers.push([o, p]);
238
- }
239
- }
240
- });
241
-
242
- illegalFnNames.forEach((name) => {
243
- delete functionDeclarations[name];
244
- });
245
-
246
- // map original name->new game
247
- var gen = this.getGenerator();
248
- Object.keys(functionDeclarations).forEach((name) => {
249
- newFnNames[name] = this.isDebug
250
- ? "_dispatcher_" + this.count + "_" + name
251
- : gen.generate();
252
- });
253
- // set containing new name
254
- var set = new Set(Object.keys(newFnNames));
255
-
256
- // Only make a dispatcher function if it caught any functions
257
- if (set.size > 0) {
258
- if (!this.functionLengthName) {
259
- this.functionLengthName = this.getPlaceholder();
260
- }
261
-
262
- var payloadArg =
263
- this.getPlaceholder() + "_dispatcher_" + this.count + "_payload";
264
-
265
- var dispatcherFnName =
266
- this.getPlaceholder() +
267
- "_dispatcher_" +
268
- this.count +
269
- predictableFunctionTag;
270
-
271
- this.log(dispatcherFnName, set);
272
- this.count++;
273
-
274
- var expectedGet = gen.generate();
275
- var expectedClearArgs = gen.generate();
276
- var expectedNew = gen.generate();
277
-
278
- var returnProp = gen.generate();
279
- var newReturnMemberName = gen.generate();
280
-
281
- var shuffledKeys = shuffle(Object.keys(functionDeclarations));
282
- var mapName = this.getPlaceholder();
283
-
284
- var cacheName = this.getPlaceholder();
285
-
286
- // creating the dispatcher function
287
- // 1. create function map
288
- var map = VariableDeclaration(
289
- VariableDeclarator(
290
- mapName,
291
- ObjectExpression(
292
- shuffledKeys.map((name) => {
293
- var [def, defParents] = functionDeclarations[name];
294
- var body = getBlockBody(def.body);
295
-
296
- var functionExpression: Node = {
297
- ...def,
298
- expression: false,
299
- type: "FunctionExpression",
300
- id: null,
301
- params: [],
302
- [predictableFunctionTag]: true,
303
- };
304
- this.addComment(functionExpression, name);
305
-
306
- if (def.params.length > 0) {
307
- const fixParam = (param: Node) => {
308
- return param;
309
- };
310
-
311
- var variableDeclaration = VariableDeclaration(
312
- VariableDeclarator(
313
- {
314
- type: "ArrayPattern",
315
- elements: def.params.map(fixParam),
316
- },
317
- Identifier(payloadArg)
318
- )
319
- );
320
-
321
- prepend(def.body, variableDeclaration);
322
- }
323
-
324
- // For logging purposes
325
- var signature =
326
- name +
327
- "(" +
328
- def.params.map((x) => x.name || "<>").join(",") +
329
- ")";
330
- this.log("Added", signature);
331
-
332
- // delete ref in block
333
- if (defParents.length) {
334
- deleteDirect(def, defParents[0]);
335
- }
336
-
337
- this.addComment(functionExpression, signature);
338
- return Property(
339
- Literal(newFnNames[name]),
340
- functionExpression,
341
- false
342
- );
343
- })
344
- )
345
- )
346
- );
347
-
348
- var getterArgName = this.getPlaceholder();
349
-
350
- var x = this.getPlaceholder();
351
- var y = this.getPlaceholder();
352
- var z = this.getPlaceholder();
353
-
354
- function getAccessor() {
355
- return MemberExpression(Identifier(mapName), Identifier(x), true);
356
- }
357
-
358
- // 2. define it
359
- var fn = FunctionDeclaration(
360
- dispatcherFnName,
361
- [Identifier(x), Identifier(y), Identifier(z)],
362
- [
363
- // Define map of callable functions
364
- map,
365
-
366
- // Set returning variable to undefined
367
- VariableDeclaration(VariableDeclarator(returnProp)),
368
-
369
- // Arg to clear the payload
370
- IfStatement(
371
- BinaryExpression(
372
- "==",
373
- Identifier(y),
374
- Literal(expectedClearArgs)
375
- ),
376
- [
377
- ExpressionStatement(
378
- AssignmentExpression(
379
- "=",
380
- Identifier(payloadArg),
381
- ArrayExpression([])
382
- )
383
- ),
384
- ],
385
- null
386
- ),
387
-
388
- VariableDeclaration(
389
- VariableDeclarator(
390
- Identifier("lengths"),
391
- ObjectExpression(
392
- !this.options.preserveFunctionLength
393
- ? []
394
- : shuffledKeys
395
- .map((name) => {
396
- var [def, defParents] = functionDeclarations[name];
397
-
398
- return {
399
- key: newFnNames[name],
400
- value: computeFunctionLength(def.params),
401
- };
402
- })
403
- .filter((item) => item.value !== 0)
404
- .map((item) =>
405
- Property(Literal(item.key), Literal(item.value))
406
- )
407
- )
408
- )
409
- ),
410
-
411
- new Template(`
412
- function makeFn${predictableFunctionTag}(){
413
- var fn = function(...args){
414
- ${payloadArg} = args;
415
- return ${mapName}[${x}].call(this)
416
- }, a = lengths[${x}]
417
-
418
- ${
419
- this.options.preserveFunctionLength
420
- ? `if(a){
421
- return ${this.functionLengthName}(fn, a)
422
- }`
423
- : ""
424
- }
425
-
426
- return fn
427
- }
428
- `).single(),
429
-
430
- // Arg to get a function reference
431
- IfStatement(
432
- BinaryExpression("==", Identifier(y), Literal(expectedGet)),
433
- [
434
- // Getter flag: return the function object
435
- ExpressionStatement(
436
- AssignmentExpression(
437
- "=",
438
- Identifier(returnProp),
439
- LogicalExpression(
440
- "||",
441
- MemberExpression(
442
- Identifier(cacheName),
443
- Identifier(x),
444
- true
445
- ),
446
- AssignmentExpression(
447
- "=",
448
- MemberExpression(
449
- Identifier(cacheName),
450
- Identifier(x),
451
- true
452
- ),
453
- CallExpression(
454
- Identifier(`makeFn${predictableFunctionTag}`),
455
- []
456
- )
457
- )
458
- )
459
- )
460
- ),
461
- ],
462
- [
463
- // Call the function, return result
464
- ExpressionStatement(
465
- AssignmentExpression(
466
- "=",
467
- Identifier(returnProp),
468
- CallExpression(getAccessor(), [])
469
- )
470
- ),
471
- ]
472
- ),
473
-
474
- // Check how the function was invoked (new () vs ())
475
- IfStatement(
476
- BinaryExpression("==", Identifier(z), Literal(expectedNew)),
477
- [
478
- // Wrap in object
479
- ReturnStatement(
480
- ObjectExpression([
481
- Property(
482
- Identifier(newReturnMemberName),
483
- Identifier(returnProp),
484
- false
485
- ),
486
- ])
487
- ),
488
- ],
489
- [
490
- // Return raw result
491
- ReturnStatement(Identifier(returnProp)),
492
- ]
493
- ),
494
- ]
495
- );
496
-
497
- append(object, fn);
498
-
499
- if (payloadArg) {
500
- prepend(
501
- object,
502
- VariableDeclaration(
503
- VariableDeclarator(payloadArg, ArrayExpression([]))
504
- )
505
- );
506
- }
507
-
508
- identifiers.forEach(([o, p]) => {
509
- if (o.type != "Identifier") {
510
- return;
511
- }
512
-
513
- var newName = newFnNames[o.name];
514
- if (!newName || typeof newName !== "string") {
515
- return;
516
- }
517
-
518
- if (!functionDeclarations[o.name]) {
519
- this.error(new Error("newName, missing function declaration"));
520
- }
521
-
522
- var info = getIdentifierInfo(o, p);
523
- if (
524
- info.isFunctionCall &&
525
- p[0].type == "CallExpression" &&
526
- p[0].callee === o
527
- ) {
528
- // Invoking call expression: `a();`
529
-
530
- if (o.name == dispatcherFnName) {
531
- return;
532
- }
533
-
534
- this.log(
535
- `${o.name}(${p[0].arguments
536
- .map((_) => "<>")
537
- .join(",")}) -> ${dispatcherFnName}('${newName}')`
538
- );
539
-
540
- var assignmentExpressions: Node[] = [];
541
- var dispatcherArgs: Node[] = [Literal(newName)];
542
-
543
- if (p[0].arguments.length) {
544
- assignmentExpressions = [
545
- AssignmentExpression(
546
- "=",
547
- Identifier(payloadArg),
548
- ArrayExpression(p[0].arguments)
549
- ),
550
- ];
551
- } else {
552
- dispatcherArgs.push(Literal(expectedClearArgs));
553
- }
554
-
555
- var type = choice(["CallExpression", "NewExpression"]);
556
- var callExpression = null;
557
-
558
- switch (type) {
559
- case "CallExpression":
560
- callExpression = CallExpression(
561
- Identifier(dispatcherFnName),
562
- dispatcherArgs
563
- );
564
- break;
565
-
566
- case "NewExpression":
567
- if (dispatcherArgs.length == 1) {
568
- dispatcherArgs.push(Identifier("undefined"));
569
- }
570
- callExpression = MemberExpression(
571
- NewExpression(Identifier(dispatcherFnName), [
572
- ...dispatcherArgs,
573
- Literal(expectedNew),
574
- ]),
575
- Identifier(newReturnMemberName),
576
- false
577
- );
578
- break;
579
- }
580
-
581
- this.addComment(
582
- callExpression,
583
- "Calling " +
584
- o.name +
585
- "(" +
586
- p[0].arguments.map((x) => x.name).join(", ") +
587
- ")"
588
- );
589
-
590
- var expr: Node = assignmentExpressions.length
591
- ? SequenceExpression([...assignmentExpressions, callExpression])
592
- : callExpression;
593
-
594
- // Replace the parent call expression
595
- this.replace(p[0], expr);
596
- } else {
597
- // Non-invoking reference: `a`
598
-
599
- if (info.spec.isDefined) {
600
- if (info.isFunctionDeclaration) {
601
- this.log(
602
- "Skipped getter " + o.name + " (function declaration)"
603
- );
604
- } else {
605
- this.log("Skipped getter " + o.name + " (defined)");
606
- }
607
- return;
608
- }
609
- if (info.spec.isModified) {
610
- this.log("Skipped getter " + o.name + " (modified)");
611
- return;
612
- }
613
-
614
- this.log(
615
- `(getter) ${o.name} -> ${dispatcherFnName}('${newName}')`
616
- );
617
- this.replace(
618
- o,
619
- CallExpression(Identifier(dispatcherFnName), [
620
- Literal(newName),
621
- Literal(expectedGet),
622
- ])
623
- );
624
- }
625
- });
626
-
627
- prepend(
628
- object,
629
- VariableDeclaration(
630
- VariableDeclarator(
631
- Identifier(cacheName),
632
- new Template(`Object.create(null)`).single().expression
633
- )
634
- )
635
- );
636
- }
637
- }
638
- };
639
- }
640
- }
1
+ import { PluginArg, PluginObject } from "./plugin";
2
+ import { NodePath } from "@babel/traverse";
3
+ import * as t from "@babel/types";
4
+ import Template from "../templates/template";
5
+ import { ok } from "assert";
6
+ import { chance, getRandomString } from "../utils/random-utils";
7
+ import { computeProbabilityMap } from "../probability";
8
+ import { Order } from "../order";
9
+ import { NodeSymbol, PREDICTABLE, UNSAFE } from "../constants";
10
+ import {
11
+ computeFunctionLength,
12
+ isVariableFunctionIdentifier,
13
+ } from "../utils/function-utils";
14
+ import { SetFunctionLengthTemplate } from "../templates/setFunctionLengthTemplate";
15
+ import { numericLiteral } from "../utils/node";
16
+ import {
17
+ isStrictMode,
18
+ isVariableIdentifier,
19
+ prependProgram,
20
+ } from "../utils/ast-utils";
21
+
22
+ export default ({ Plugin }: PluginArg): PluginObject => {
23
+ const me = Plugin(Order.Dispatcher, {
24
+ changeData: {
25
+ functions: 0,
26
+ },
27
+ });
28
+ let dispatcherCounter = 0;
29
+
30
+ const setFunctionLength = me.getPlaceholder("d_fnLength");
31
+
32
+ // in Debug mode, function names are preserved
33
+ const isDebug = false;
34
+
35
+ return {
36
+ visitor: {
37
+ "Program|Function": {
38
+ exit(_path) {
39
+ const blockPath = _path as NodePath<t.Program | t.Function>;
40
+
41
+ if (blockPath.isProgram()) {
42
+ blockPath.scope.crawl();
43
+
44
+ // Don't insert function length code when disabled
45
+ // Instead insert empty function as the identifier is still referenced
46
+ var insertNode = t.functionDeclaration(
47
+ t.identifier(setFunctionLength),
48
+ [],
49
+ t.blockStatement([])
50
+ );
51
+
52
+ if (me.options.preserveFunctionLength) {
53
+ // Insert function length code
54
+ insertNode = SetFunctionLengthTemplate.single({
55
+ fnName: setFunctionLength,
56
+ });
57
+ }
58
+
59
+ me.skip(prependProgram(_path, insertNode));
60
+ }
61
+
62
+ if ((blockPath.node as NodeSymbol)[UNSAFE]) return;
63
+
64
+ // For testing
65
+ // if (!blockPath.isProgram()) return;
66
+
67
+ var blockStatement: NodePath<t.Block> = blockPath.isProgram()
68
+ ? blockPath
69
+ : (blockPath.get("body") as NodePath<t.BlockStatement>);
70
+
71
+ // Track functions and illegal ones
72
+ // A function is illegal if:
73
+ // - the function is async or generator
74
+ // - the function is redefined
75
+ // - the function uses 'this', 'eval', or 'arguments'
76
+ var functionPaths = new Map<
77
+ string,
78
+ NodePath<t.FunctionDeclaration>
79
+ >();
80
+ var illegalNames = new Set<string>();
81
+
82
+ // Scan for function declarations
83
+ blockPath.traverse({
84
+ // Check for reassigned / redefined functions
85
+ BindingIdentifier: {
86
+ exit(path: NodePath<t.Identifier>) {
87
+ if (!isVariableIdentifier(path)) return;
88
+
89
+ const name = path.node.name;
90
+ if (!path.parentPath?.isFunctionDeclaration()) {
91
+ illegalNames.add(name);
92
+ }
93
+ },
94
+ },
95
+ // Find functions eligible for dispatching
96
+ FunctionDeclaration: {
97
+ exit(path: NodePath<t.FunctionDeclaration>) {
98
+ const name = path.node.id.name;
99
+ // If the function is not named, we can't dispatch it
100
+ if (!name) {
101
+ return;
102
+ }
103
+
104
+ // Do not apply to async or generator functions
105
+ if (path.node.async || path.node.generator) {
106
+ return;
107
+ }
108
+
109
+ // Do not apply to functions in nested scopes
110
+ if (path.parentPath !== blockStatement || me.isSkipped(path)) {
111
+ illegalNames.add(name);
112
+ return;
113
+ }
114
+
115
+ if (isStrictMode(path)) {
116
+ illegalNames.add(name);
117
+ return;
118
+ }
119
+
120
+ // Do not apply to unsafe functions, redefined functions, or internal obfuscator functions
121
+ if (
122
+ (path.node as NodeSymbol)[UNSAFE] ||
123
+ functionPaths.has(name) ||
124
+ me.obfuscator.isInternalVariable(name)
125
+ ) {
126
+ illegalNames.add(name);
127
+ return;
128
+ }
129
+
130
+ // Functions with default parameters are not fully supported
131
+ // (Could be a Function Expression referencing outer scope)
132
+ if (
133
+ path.node.params.find((x) => x.type === "AssignmentPattern")
134
+ ) {
135
+ illegalNames.add(name);
136
+ return;
137
+ }
138
+
139
+ functionPaths.set(name, path);
140
+ },
141
+ },
142
+ });
143
+
144
+ for (let name of illegalNames) {
145
+ functionPaths.delete(name);
146
+ }
147
+
148
+ for (var name of functionPaths.keys()) {
149
+ if (!computeProbabilityMap(me.options.dispatcher, name)) {
150
+ functionPaths.delete(name);
151
+ }
152
+ }
153
+
154
+ // No functions here to change
155
+ if (functionPaths.size === 0) {
156
+ return;
157
+ }
158
+
159
+ me.changeData.functions += functionPaths.size;
160
+
161
+ const dispatcherName =
162
+ me.getPlaceholder() + "_dispatcher_" + dispatcherCounter++;
163
+ const payloadName = me.getPlaceholder() + "_payload";
164
+ const cacheName = me.getPlaceholder() + "_cache";
165
+ const newNameMapping = new Map<string, string>();
166
+
167
+ const keys = {
168
+ placeholderNoMeaning: isDebug ? "noMeaning" : getRandomString(10),
169
+ clearPayload: isDebug ? "clearPayload" : getRandomString(10),
170
+ nonCall: isDebug ? "nonCall" : getRandomString(10),
171
+ returnAsObject: isDebug ? "returnAsObject" : getRandomString(10),
172
+ returnAsObjectProperty: isDebug
173
+ ? "returnAsObjectProperty"
174
+ : getRandomString(10),
175
+ };
176
+
177
+ for (var name of functionPaths.keys()) {
178
+ newNameMapping.set(
179
+ name,
180
+ isDebug ? "_" + name : getRandomString(6) /** "_" + name */
181
+ );
182
+ }
183
+
184
+ // Find identifiers calling/referencing the functions
185
+ blockPath.traverse({
186
+ ReferencedIdentifier: {
187
+ exit(path: NodePath<t.Identifier | t.JSXIdentifier>) {
188
+ if (path.isJSX()) return;
189
+ if (isVariableFunctionIdentifier(path)) return;
190
+ const name = path.node.name;
191
+
192
+ var fnPath = functionPaths.get(name);
193
+ if (!fnPath) return;
194
+
195
+ var newName = newNameMapping.get(name);
196
+
197
+ // Do not replace if not referencing the actual function
198
+ if (path.scope.getBinding(name).path !== fnPath) {
199
+ return;
200
+ }
201
+
202
+ const createDispatcherCall = (name, flagArg?) => {
203
+ var dispatcherArgs = [t.stringLiteral(name)];
204
+ if (flagArg) {
205
+ dispatcherArgs.push(t.stringLiteral(flagArg));
206
+ }
207
+
208
+ var asObject = chance(50);
209
+
210
+ if (asObject) {
211
+ if (dispatcherArgs.length < 2) {
212
+ dispatcherArgs.push(
213
+ t.stringLiteral(keys.placeholderNoMeaning)
214
+ );
215
+ }
216
+ dispatcherArgs.push(t.stringLiteral(keys.returnAsObject));
217
+ }
218
+
219
+ var callExpression: t.CallExpression | t.NewExpression =
220
+ t.callExpression(
221
+ t.identifier(dispatcherName),
222
+ dispatcherArgs
223
+ );
224
+
225
+ if (!asObject) {
226
+ return callExpression;
227
+ }
228
+
229
+ if (chance(50)) {
230
+ (callExpression as t.Node).type = "NewExpression";
231
+ }
232
+
233
+ return t.memberExpression(
234
+ callExpression,
235
+ t.stringLiteral(keys.returnAsObjectProperty),
236
+ true
237
+ );
238
+ };
239
+
240
+ // Replace the identifier with a call to the function
241
+ const parentPath = path.parentPath;
242
+ if (path.key === "callee" && parentPath?.isCallExpression()) {
243
+ var expressions: t.Expression[] = [];
244
+ var callArguments = parentPath.node.arguments;
245
+
246
+ if (callArguments.length === 0) {
247
+ expressions.push(
248
+ // Call the function
249
+ createDispatcherCall(newName, keys.clearPayload)
250
+ );
251
+ } else {
252
+ expressions.push(
253
+ // Prepare the payload arguments
254
+ t.assignmentExpression(
255
+ "=",
256
+ t.identifier(payloadName),
257
+ t.arrayExpression(callArguments as t.Expression[])
258
+ ),
259
+
260
+ // Call the function
261
+ createDispatcherCall(newName)
262
+ );
263
+ }
264
+
265
+ const output =
266
+ expressions.length === 1
267
+ ? expressions[0]
268
+ : t.sequenceExpression(expressions);
269
+
270
+ if (!parentPath.container) return;
271
+ parentPath.replaceWith(output);
272
+ } else {
273
+ if (!path.container) return;
274
+ // Replace non-invocation references with a 'cached' version of the function
275
+ path.replaceWith(createDispatcherCall(newName, keys.nonCall));
276
+ }
277
+ },
278
+ },
279
+ });
280
+
281
+ const fnLengthProperties = [];
282
+
283
+ // Create the dispatcher function
284
+ const objectExpression = t.objectExpression(
285
+ Array.from(newNameMapping).map(([name, newName]) => {
286
+ const originalPath = functionPaths.get(name);
287
+ const originalFn = originalPath.node;
288
+
289
+ if (me.options.preserveFunctionLength) {
290
+ const fnLength = computeFunctionLength(originalPath);
291
+
292
+ if (fnLength >= 1) {
293
+ // 0 is already the default
294
+ fnLengthProperties.push(
295
+ t.objectProperty(
296
+ t.stringLiteral(newName),
297
+ numericLiteral(fnLength)
298
+ )
299
+ );
300
+ }
301
+ }
302
+
303
+ const newBody = [...originalFn.body.body];
304
+ ok(Array.isArray(newBody));
305
+
306
+ // Unpack parameters
307
+ if (originalFn.params.length > 0) {
308
+ newBody.unshift(
309
+ t.variableDeclaration("var", [
310
+ t.variableDeclarator(
311
+ t.arrayPattern([...originalFn.params]),
312
+ t.identifier(payloadName)
313
+ ),
314
+ ])
315
+ );
316
+ }
317
+
318
+ // Add debug label
319
+ if (isDebug) {
320
+ newBody.unshift(
321
+ t.expressionStatement(
322
+ t.stringLiteral(`Dispatcher: ${name} -> ${newName}`)
323
+ )
324
+ );
325
+ }
326
+
327
+ const functionExpression = t.functionExpression(
328
+ null,
329
+ [],
330
+ t.blockStatement(newBody)
331
+ );
332
+
333
+ for (var symbol of Object.getOwnPropertySymbols(originalFn)) {
334
+ (functionExpression as any)[symbol] = (originalFn as any)[
335
+ symbol
336
+ ];
337
+ }
338
+
339
+ (functionExpression as NodeSymbol)[PREDICTABLE] = true;
340
+
341
+ return t.objectProperty(
342
+ t.stringLiteral(newName),
343
+
344
+ functionExpression
345
+ );
346
+ })
347
+ );
348
+
349
+ const fnLengths = t.objectExpression(fnLengthProperties);
350
+
351
+ const dispatcher = new Template(`
352
+ function ${dispatcherName}(name, flagArg, returnTypeArg, fnLengths = {fnLengthsObjectExpression}) {
353
+ var output;
354
+ var fns = {objectExpression};
355
+
356
+ if(flagArg === "${keys.clearPayload}") {
357
+ ${payloadName} = [];
358
+ }
359
+ if(flagArg === "${keys.nonCall}") {
360
+ function createFunction(){
361
+ var fn = function(...args){
362
+ ${payloadName} = args;
363
+ return fns[name].apply(this);
364
+ }
365
+
366
+ var fnLength = fnLengths[name];
367
+ if(fnLength) {
368
+ ${setFunctionLength}(fn, fnLength);
369
+ }
370
+
371
+ return fn;
372
+ }
373
+ output = ${cacheName}[name] || (${cacheName}[name] = createFunction());
374
+ } else {
375
+ output = fns[name]();
376
+ }
377
+
378
+ if(returnTypeArg === "${keys.returnAsObject}") {
379
+ return { "${keys.returnAsObjectProperty}": output };
380
+ } else {
381
+ return output;
382
+ }
383
+ }
384
+ `).single({
385
+ objectExpression,
386
+ fnLengthsObjectExpression: fnLengths,
387
+ });
388
+
389
+ (dispatcher as NodeSymbol)[PREDICTABLE] = true;
390
+
391
+ /**
392
+ * Prepends the node into the block. (And registers the declaration)
393
+ * @param node
394
+ */
395
+ function prepend(node: t.Statement) {
396
+ var newPath = blockStatement.unshiftContainer<any, any, any>(
397
+ "body",
398
+ node
399
+ )[0];
400
+ blockStatement.scope.registerDeclaration(newPath);
401
+ }
402
+
403
+ // Insert the dispatcher function
404
+ prepend(dispatcher);
405
+
406
+ // Insert the payload variable
407
+ prepend(
408
+ t.variableDeclaration("var", [
409
+ t.variableDeclarator(t.identifier(payloadName)),
410
+ ])
411
+ );
412
+
413
+ // Insert the cache variable
414
+ prepend(
415
+ t.variableDeclaration("var", [
416
+ t.variableDeclarator(
417
+ t.identifier(cacheName),
418
+ new Template(`Object["create"](null)`).expression()
419
+ ),
420
+ ])
421
+ );
422
+
423
+ // Remove original functions
424
+ for (let path of functionPaths.values()) {
425
+ path.remove();
426
+ }
427
+ },
428
+ },
429
+ },
430
+ };
431
+ };