From 5f117347d37d99bc13b72110a3f43421c86fa358 Mon Sep 17 00:00:00 2001 From: dengxinyu Date: Fri, 19 Jan 2024 08:27:36 +0000 Subject: [PATCH] Description: Deleted shadow and all testcases passed except export_obfuscation_declaration_2.d.ts. Signed-off-by: dengxinyu Change-Id: Ibaa1c2f70f031e9a5135613b995fbdd9be8e9695 --- arkguard/src/ArkObfuscator.ts | 8 ++++- .../rename/RenameIdentifierTransformer.ts | 34 ++++++------------- arkguard/src/utils/TypeUtils.ts | 21 ++++++++++++ 3 files changed, 38 insertions(+), 25 deletions(-) diff --git a/arkguard/src/ArkObfuscator.ts b/arkguard/src/ArkObfuscator.ts index 158f4dc29c..6d461de317 100644 --- a/arkguard/src/ArkObfuscator.ts +++ b/arkguard/src/ArkObfuscator.ts @@ -53,6 +53,8 @@ import {ListUtil} from './utils/ListUtil'; import {needReadApiInfo, readProjectProperties} from './common/ApiReader'; import {ApiExtractor} from './common/ApiExtractor'; import es6Info from './configs/preset/es6_reserved_properties.json'; +import { Extension, PathAndExtension } from './common/type'; +import { TypeUtils } from './utils/TypeUtils'; export const renameIdentifierModule = require('./transformers/rename/RenameIdentifierTransformer'); export const renamePropertyModule = require('./transformers/rename/RenamePropertiesTransformer'); @@ -358,7 +360,7 @@ export class ArkObfuscator { } if (typeof content === 'string') { - ast = createSourceFile(sourceFilePath, content, ScriptTarget.ES2015, true); + ast = TypeUtils.createObfSourceFile(sourceFilePath, content); } else { ast = content; } @@ -393,6 +395,10 @@ export class ArkObfuscator { sourceMapGenerator = getSourceMapGenerator(sourceFilePath); } + //@ts-ignore + if (ast.isJs) { + TypeUtils.tsToJs(ast); + } this.createObfsPrinter(ast.isDeclarationFile).writeFile(ast, this.mTextWriter, sourceMapGenerator); result.filePath = ast.fileName; diff --git a/arkguard/src/transformers/rename/RenameIdentifierTransformer.ts b/arkguard/src/transformers/rename/RenameIdentifierTransformer.ts index b8475375c1..c10713f4f8 100644 --- a/arkguard/src/transformers/rename/RenameIdentifierTransformer.ts +++ b/arkguard/src/transformers/rename/RenameIdentifierTransformer.ts @@ -66,7 +66,6 @@ import type {TransformPlugin} from '../TransformPlugin'; import {TransformerOrder} from '../TransformPlugin'; import {getNameGenerator, NameGeneratorType} from '../../generator/NameFactory'; import {TypeUtils} from '../../utils/TypeUtils'; -import {collectIdentifiersAndStructs} from '../../utils/TransformUtil'; import {NodeUtils} from '../../utils/NodeUtils'; import {ApiExtractor} from '../../common/ApiExtractor'; import { globalMangledTable, historyMangledTable, reservedProperties } from './RenamePropertiesTransformer'; @@ -115,11 +114,7 @@ namespace secharmony { let checker: TypeChecker = undefined; let manager: ScopeManager = createScopeManager(); - let shadowIdentifiers: Identifier[] = undefined; - let shadowStructs: StructDeclaration[] = undefined; - let identifierIndex: number = 0; - let structIndex: number = 0; return renameTransformer; /** @@ -132,9 +127,8 @@ namespace secharmony { return node; } - const shadowSourceAst: SourceFile = TypeUtils.createNewSourceFile(node); - checker = TypeUtils.createChecker(shadowSourceAst); - manager.analyze(shadowSourceAst, checker, exportObfuscation); + checker = TypeUtils.createChecker(node); + manager.analyze(node, checker, exportObfuscation); // the reservedNames of manager contain the struct name. if (!exportObfuscation) { @@ -151,9 +145,6 @@ namespace secharmony { renameInScope(root); root = undefined; // collect all identifiers of shadow sourceFile - const identifiersAndStructs = collectIdentifiersAndStructs(shadowSourceAst, context); - shadowIdentifiers = identifiersAndStructs.shadowIdentifiers; - shadowStructs = identifiersAndStructs.shadowStructs; let ret: Node = visit(node); ret = tryRemoveVirtualConstructor(ret); @@ -397,26 +388,21 @@ namespace secharmony { } if (isLabeledStatement(node.parent) || isBreakOrContinueStatement(node.parent)) { - identifierIndex += 1; return updateLabelNode(node); } - const shadowNode: Identifier = shadowIdentifiers[identifierIndex]; - identifierIndex += 1; - return updateNameNode(node, shadowNode); + return updateNameNode(node); } function tryRemoveVirtualConstructor(node: Node): Node { if (isStructDeclaration(node)) { - const shadowNode: StructDeclaration = shadowStructs[structIndex]; - structIndex++; - const sourceFile = NodeUtils.getSourceFileOfNode(shadowNode); + const sourceFile = NodeUtils.getSourceFileOfNode(node); const tempStructMembers: ClassElement[] = []; if (sourceFile && sourceFile.isDeclarationFile) { for (let index = 0; index < node.members.length; index++) { const member = node.members[index]; // @ts-ignore - if (isConstructorDeclaration(member) && shadowNode.members[index].virtual) { + if (isConstructorDeclaration(member) && node.members[index].virtual) { continue; } tempStructMembers.push(member); @@ -429,7 +415,7 @@ namespace secharmony { return visitEachChild(node, tryRemoveVirtualConstructor, context); } - function updateNameNode(node: Identifier, shadowNode: Identifier): Node { + function updateNameNode(node: Identifier): Node { // skip property in property access expression if (NodeUtils.isPropertyAccessNode(node)) { return node; @@ -439,11 +425,11 @@ namespace secharmony { return node; } - let sym: Symbol | undefined = checker.getSymbolAtLocation(shadowNode); + let sym: Symbol | undefined = checker.getSymbolAtLocation(node); let mangledPropertyNameOfNoSymbolImportExport = ''; - if ((!sym || sym.name === 'default')) { - if (exportObfuscation && noSymbolIdentifier.has(shadowNode.escapedText as string) && trySearchImportExportSpecifier(shadowNode)) { - mangledPropertyNameOfNoSymbolImportExport = mangleNoSymbolImportExportPropertyName(shadowNode.escapedText as string); + if (node && (!sym || sym.name === 'default')) { + if (exportObfuscation && noSymbolIdentifier.has(node.escapedText as string) && trySearchImportExportSpecifier(node)) { + mangledPropertyNameOfNoSymbolImportExport = mangleNoSymbolImportExportPropertyName(node.escapedText as string); } else { return node; } diff --git a/arkguard/src/utils/TypeUtils.ts b/arkguard/src/utils/TypeUtils.ts index 81b1fc8dfc..451a2fb58c 100644 --- a/arkguard/src/utils/TypeUtils.ts +++ b/arkguard/src/utils/TypeUtils.ts @@ -90,4 +90,25 @@ export class TypeUtils { let program: Program = createProgram([ast.fileName], option, customHost); return program.getTypeChecker(); } + + public static createObfSourceFile(sourceFilePath: string, content: string): SourceFile { + const pathOrExtension: PathAndExtension = FileUtils.getFileSuffix(sourceFilePath); + const fileSuffix = pathOrExtension.ext === Extension.DETS ? Extension.DETS : Extension.TS; + const { dir, name } = path.parse(sourceFilePath); + const targetName: string = path.join(dir, name) + fileSuffix; + let ast : SourceFile = createSourceFile(targetName, content, ScriptTarget.ES2015, true); + if(pathOrExtension.ext === Extension.JS){ + //@ts-ignore + ast.isJs = true; + } + return ast; + } + + public static tsToJs(ast: SourceFile) { + const filePath = ast.fileName; + const fileSuffix = Extension.JS; + const { dir, name } = path.parse(filePath); + const targetName: string = path.join(dir, name) + fileSuffix; + ast.fileName = targetName; + } } -- Gitee