From dc0cf75185aefb1e163924eb7e80e4197ce8a9ad Mon Sep 17 00:00:00 2001 From: zengyuan Date: Sun, 28 Jan 2024 14:10:50 +0000 Subject: [PATCH] Support obfuscated nameCache to save line information ISSUE:https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/I8ZH7B Signed-off-by: zengyuan Change-Id: Ic98b46ae63eacff581c44ca6827341f2a83bfc1b --- arkguard/scripts/execute_result_statistics.js | 18 +++++- arkguard/src/ArkObfuscator.ts | 54 +++++++++++++++--- .../rename/RenameIdentifierTransformer.ts | 45 ++++++++++++--- arkguard/src/utils/NameCacheUtil.ts | 2 + arkguard/src/utils/ScopeAnalyzer.ts | 56 ++++++++++++++++++- .../function_like_namecache/function_arrow.ts | 22 ++++++++ .../function_arrow_expected_cache.txt | 8 +++ .../function_constructor.ts | 28 ++++++++++ .../function_constructor_expected_cache.txt | 9 +++ .../function_expression.ts | 21 +++++++ .../function_expression_expected_cache.txt | 8 +++ .../function_in_function.ts | 25 +++++++++ .../function_in_function_expected_cache.txt | 8 +++ .../function_member_method.ts | 29 ++++++++++ .../function_member_method_expected_cache.txt | 9 +++ .../function_set_get.ts | 27 +++++++++ .../function_set_get_expected_cache.txt | 10 ++++ .../function_like_namecache/obfConfig.json | 20 +++++++ .../function_like_namecache/property_arrow.ts | 23 ++++++++ .../property_arrow_expected_cache.txt | 8 +++ .../function_like_namecache/property_exp.ts | 23 ++++++++ .../property_exp_expected_cache.txt | 8 +++ .../same_property_name.ts | 26 +++++++++ .../same_property_name_expected_cache.txt | 9 +++ 24 files changed, 475 insertions(+), 21 deletions(-) create mode 100644 arkguard/test/grammar/function_like_namecache/function_arrow.ts create mode 100644 arkguard/test/grammar/function_like_namecache/function_arrow_expected_cache.txt create mode 100644 arkguard/test/grammar/function_like_namecache/function_constructor.ts create mode 100644 arkguard/test/grammar/function_like_namecache/function_constructor_expected_cache.txt create mode 100644 arkguard/test/grammar/function_like_namecache/function_expression.ts create mode 100644 arkguard/test/grammar/function_like_namecache/function_expression_expected_cache.txt create mode 100644 arkguard/test/grammar/function_like_namecache/function_in_function.ts create mode 100644 arkguard/test/grammar/function_like_namecache/function_in_function_expected_cache.txt create mode 100644 arkguard/test/grammar/function_like_namecache/function_member_method.ts create mode 100644 arkguard/test/grammar/function_like_namecache/function_member_method_expected_cache.txt create mode 100644 arkguard/test/grammar/function_like_namecache/function_set_get.ts create mode 100644 arkguard/test/grammar/function_like_namecache/function_set_get_expected_cache.txt create mode 100644 arkguard/test/grammar/function_like_namecache/obfConfig.json create mode 100644 arkguard/test/grammar/function_like_namecache/property_arrow.ts create mode 100644 arkguard/test/grammar/function_like_namecache/property_arrow_expected_cache.txt create mode 100644 arkguard/test/grammar/function_like_namecache/property_exp.ts create mode 100644 arkguard/test/grammar/function_like_namecache/property_exp_expected_cache.txt create mode 100644 arkguard/test/grammar/function_like_namecache/same_property_name.ts create mode 100644 arkguard/test/grammar/function_like_namecache/same_property_name_expected_cache.txt diff --git a/arkguard/scripts/execute_result_statistics.js b/arkguard/scripts/execute_result_statistics.js index 2b4c16a55f..4f2a24ca31 100644 --- a/arkguard/scripts/execute_result_statistics.js +++ b/arkguard/scripts/execute_result_statistics.js @@ -82,9 +82,21 @@ function compareContent(filePath) { const sourcePath = filePath.replace('/test/local/', '/test/grammar/'); const sourcePathAndExtension = FileUtils.getFileSuffix(sourcePath); const expectationPath = sourcePathAndExtension.path + '_expected.txt'; - if (fs.existsSync(expectationPath)) { - const actual = fs.readFileSync(filePath).toString(); - const expectation = fs.readFileSync(expectationPath).toString(); + const resultPathAndExtension = FileUtils.getFileSuffix(filePath); + const resultCachePath = resultPathAndExtension.path + '.cache.json'; + const expectationCachePath = sourcePathAndExtension.path + '_expected_cache.txt'; + const hasExpectationFile = fs.existsSync(expectationPath); + const hasExpectationCache = fs.existsSync(expectationCachePath); + const hasResultCache = fs.existsSync(resultCachePath); + if (hasExpectationFile || (hasExpectationCache && hasResultCache)) { + let actual,expectation; + if (hasExpectationFile) { + actual = fs.readFileSync(filePath).toString(); + expectation = fs.readFileSync(expectationPath).toString(); + } else { + actual = fs.readFileSync(resultCachePath).toString(); + expectation = fs.readFileSync(expectationCachePath).toString(); + } if (actual === expectation) { contentcomparationSuccessCount++; } else { diff --git a/arkguard/src/ArkObfuscator.ts b/arkguard/src/ArkObfuscator.ts index b0191ede86..7e8aefb755 100644 --- a/arkguard/src/ArkObfuscator.ts +++ b/arkguard/src/ArkObfuscator.ts @@ -47,6 +47,8 @@ import { getMapFromJson, NAME_CACHE_SUFFIX, PROPERTY_CACHE_FILE, + IDENTIFIER_CACHE, + MEM_METHOD_CACHE, readCache, writeCache } from './utils/NameCacheUtil'; import {ListUtil} from './utils/ListUtil'; @@ -64,7 +66,7 @@ export let orignalFilePathForSearching: string | undefined; type ObfuscationResultType = { content: string, sourceMap?: RawSourceMap, - nameCache?: { [k: string]: string }, + nameCache?: { [k: string] : string | {} }, filePath?: string }; @@ -217,8 +219,16 @@ export class ArkObfuscator { const nameCachePath: string = path.join(outputDir, FileUtils.getFileName(sourceFile) + NAME_CACHE_SUFFIX); const nameCache: Object = readCache(nameCachePath); + let historyNameCache = new Map(); + let identifierCache = nameCache ? Reflect.get(nameCache, IDENTIFIER_CACHE) : undefined; + if (identifierCache) { + for (const [key, value] of Object.entries(identifierCache)) { + let newKey = key.includes(':') ? key.split(':')[0] : key; + historyNameCache.set(newKey, value as string); + } + } - renameIdentifierModule.historyNameCache = getMapFromJson(nameCache); + renameIdentifierModule.historyNameCache = historyNameCache; } private readPropertyCache(outputDir: string): void { @@ -235,7 +245,7 @@ export class ArkObfuscator { renamePropertyModule.historyMangledTable = getMapFromJson(propertyCache); } - private produceNameCache(namecache: { [k: string]: string }, resultPath: string): void { + private produceNameCache(namecache: { [k: string]: string | {}}, resultPath: string): void { const nameCachePath: string = resultPath + NAME_CACHE_SUFFIX; fs.writeFileSync(nameCachePath, JSON.stringify(namecache, null, JSON_TEXT_INDENT_LENGTH)); } @@ -311,6 +321,29 @@ export class ArkObfuscator { return (suffix !== 'js' && suffix !== 'ts' && suffix !== 'ets'); } + private convertLineInfoForCache(consumer: sourceMap.SourceMapConsumer, targetCache: string) : Object { + let originalCache : Map = renameIdentifierModule.nameCache.get(targetCache); + let updatedCache: Object= {}; + for (const [key, value] of originalCache) { + let newKey: string = key; + if (!key.includes(':')) { + updatedCache[newKey] = value; + continue; + } + const [scopeName, oldStartLine, oldStartColum, oldEndLine, oldEndColum] = key.split(':'); + const startPosition = consumer.originalPositionFor({line: Number(oldStartLine), column: Number(oldStartColum)}); + const startLine = startPosition.line; + const endPosition = consumer.originalPositionFor({line: Number(oldEndLine), column: Number(oldEndColum)}); + const endLine = endPosition.line; + newKey = `${scopeName}:${startLine}:${endLine}`; + // Do not save methods that do not exist in the source code, e.g. 'build' in ArkUI. + if (startLine && endLine) { + updatedCache[newKey] = value; + } + } + return updatedCache; + } + /** * Obfuscate single source file with path provided * @@ -384,7 +417,7 @@ export class ArkObfuscator { return result; } - if (historyNameCache && this.mCustomProfiles.mNameObfuscation) { + if (historyNameCache && historyNameCache.size > 0 && this.mCustomProfiles.mNameObfuscation) { renameIdentifierModule.historyNameCache = historyNameCache; } @@ -423,10 +456,15 @@ export class ArkObfuscator { sourceMapJson = await this.mergeSourceMap(previousStageSourceMap, sourceMapJson as sourceMap.RawSourceMap); } result.sourceMap = sourceMapJson; - } - - if (this.mCustomProfiles.mEnableNameCache && renameIdentifierModule.nameCache) { - result.nameCache = Object.fromEntries(renameIdentifierModule.nameCache); + let nameCache = renameIdentifierModule.nameCache; + if (this.mCustomProfiles.mEnableNameCache && nameCache) { + const consumer = await new sourceMap.SourceMapConsumer(sourceMapJson as sourceMap.RawSourceMap); + let newIdentifierCache: Object = this.convertLineInfoForCache(consumer, IDENTIFIER_CACHE); + let newMemberMethodCache: Object = this.convertLineInfoForCache(consumer, MEM_METHOD_CACHE); + nameCache.set(IDENTIFIER_CACHE, newIdentifierCache); + nameCache.set(MEM_METHOD_CACHE, newMemberMethodCache); + result.nameCache = {[IDENTIFIER_CACHE]: newIdentifierCache, [MEM_METHOD_CACHE]: newMemberMethodCache}; + } } // clear cache of text writer diff --git a/arkguard/src/transformers/rename/RenameIdentifierTransformer.ts b/arkguard/src/transformers/rename/RenameIdentifierTransformer.ts index b8475375c1..ff2a7b0f1e 100644 --- a/arkguard/src/transformers/rename/RenameIdentifierTransformer.ts +++ b/arkguard/src/transformers/rename/RenameIdentifierTransformer.ts @@ -59,6 +59,11 @@ import type { ScopeManager } from '../../utils/ScopeAnalyzer'; +import { + IDENTIFIER_CACHE, + MEM_METHOD_CACHE +} from '../../utils/NameCacheUtil'; + import type {INameGenerator, NameGeneratorOptions} from '../../generator/INameGenerator'; import type {IOptions} from '../../configs/IOptions'; import type {INameObfuscationOption} from '../../configs/INameObfuscationOption'; @@ -69,7 +74,8 @@ 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'; +import {globalMangledTable, historyMangledTable, reservedProperties} from './RenamePropertiesTransformer'; +import {memberMethodCache} from '../../utils/ScopeAnalyzer'; namespace secharmony { /** @@ -106,6 +112,7 @@ namespace secharmony { profile?.mReservedToplevelNames?.forEach(item => reservedProperties.add(item)); let mangledSymbolNames: Map = new Map(); let mangledLabelNames: Map = new Map(); + memberMethodCache.clear(); noSymbolIdentifier.clear(); let historyMangledNames: Set = undefined; @@ -132,6 +139,10 @@ namespace secharmony { return node; } + if (nameCache.size === 0) { + nameCache.set(IDENTIFIER_CACHE, new Map()); + } + const shadowSourceAst: SourceFile = TypeUtils.createNewSourceFile(node); checker = TypeUtils.createChecker(shadowSourceAst); manager.analyze(shadowSourceAst, checker, exportObfuscation); @@ -143,10 +154,6 @@ namespace secharmony { }); } - if (nameCache === undefined) { - nameCache = new Map(); - } - let root: Scope = manager.getRootScope(); renameInScope(root); root = undefined; @@ -157,6 +164,7 @@ namespace secharmony { let ret: Node = visit(node); ret = tryRemoveVirtualConstructor(ret); + nameCache.set(MEM_METHOD_CACHE, memberMethodCache); return setParentRecursive(ret, true); } @@ -218,7 +226,6 @@ namespace secharmony { const path: string = scope.loc + '#' + original; const historyName: string = historyNameCache?.get(path); - if (historyName) { mangled = historyName; } else if (Reflect.has(def, 'obfuscateAsProperty')) { @@ -228,7 +235,12 @@ namespace secharmony { } // add new names to name cache - nameCache.set(path, mangled); + let identifierCache = nameCache?.get(IDENTIFIER_CACHE); + if (identifierCache) { + const isFunction: boolean = Reflect.has(def, 'isFunction'); + const pathWithLine: string = splicePathWithLineInfo(isFunction, def, scope.loc, original); + (identifierCache as Map).set(pathWithLine, mangled); + } scope.mangledNames.add(mangled); mangledSymbolNames.set(def, mangled); }); @@ -386,6 +398,19 @@ namespace secharmony { return results; } + function splicePathWithLineInfo(isFunction: boolean, sym: Symbol, scope: string, origin: string): string { + let path: string = scope + '#' + origin; + if (!isFunction) { + return path; + } + let positions = Reflect.get(sym, 'lineInfo') as Array; + let startLine = positions[0]; + let startCharacter = positions[1]; + let endLine = positions[2]; + let endCharacter = positions[3]; + return path + ':' + startLine + ':' + startCharacter + ':' + endLine + ':' + endCharacter; + } + /** * visit each node to change identifier name to mangled name * - calculate shadow name index to find shadow node @@ -485,7 +510,9 @@ namespace secharmony { const path: string = '#' + original; const historyName: string = historyNameCache?.get(path); let mangled = historyName ?? getPropertyMangledName(original); - nameCache.set(path, mangled); + if (nameCache && nameCache.get(IDENTIFIER_CACHE)) { + (nameCache.get(IDENTIFIER_CACHE) as Map).set(path, mangled); + } return mangled; } @@ -511,7 +538,7 @@ namespace secharmony { 'createTransformerFactory': createRenameIdentifierFactory }; - export let nameCache: Map = undefined; + export let nameCache: Map> = new Map(); export let historyNameCache: Map = undefined; export let globalNameCache: Map = new Map(); } diff --git a/arkguard/src/utils/NameCacheUtil.ts b/arkguard/src/utils/NameCacheUtil.ts index dd5e132397..9c53ac0f60 100644 --- a/arkguard/src/utils/NameCacheUtil.ts +++ b/arkguard/src/utils/NameCacheUtil.ts @@ -17,6 +17,8 @@ import {FileUtils} from './FileUtils'; export const NAME_CACHE_SUFFIX: string = '.cache.json'; export const PROPERTY_CACHE_FILE: string = 'property.cache.json'; +export const IDENTIFIER_CACHE: string = 'IdentifierCache'; +export const MEM_METHOD_CACHE: string = 'MemberMethodCache'; const spaceOfNameCache: number = 2; export function writeCache(cache: Map, destFileName: string): void { diff --git a/arkguard/src/utils/ScopeAnalyzer.ts b/arkguard/src/utils/ScopeAnalyzer.ts index e33382c8b1..519306a949 100644 --- a/arkguard/src/utils/ScopeAnalyzer.ts +++ b/arkguard/src/utils/ScopeAnalyzer.ts @@ -23,7 +23,13 @@ import { isFunctionLike, isIdentifier, isMethodDeclaration, - SyntaxKind + SyntaxKind, + isVariableDeclaration, + isFunctionExpression, + isArrowFunction, + isGetAccessor, + isSetAccessor, + isPropertyDeclaration } from 'typescript'; import type { @@ -57,6 +63,7 @@ import type { import {NodeUtils} from './NodeUtils'; import {isParameterPropertyModifier, isViewPUBasedClass} from './OhsUtil'; +import {globalMangledTable} from '../transformers/rename/RenamePropertiesTransformer' /** * kind of a scope @@ -65,6 +72,7 @@ namespace secharmony { type ForLikeStatement = ForStatement | ForInOrOfStatement; type ClassLikeDeclaration = ClassDeclaration | ClassExpression; export const noSymbolIdentifier: Set = new Set(); + export let memberMethodCache: Map = new Map(); /** * type of scope @@ -737,6 +745,52 @@ namespace secharmony { scopes.push(current); } + let symbol: Symbol; + if((isFunctionExpression(node) || isArrowFunction(node)) && isVariableDeclaration(node.parent)) { + symbol = checker.getSymbolAtLocation(node.parent.name); + } else { + symbol = checker.getSymbolAtLocation(node.name); + } + const sourceFile = NodeUtils.getSourceFileOfNode(node); + const startPosition = sourceFile.getLineAndCharacterOfPosition(node.getStart()); + const endPosition = sourceFile.getLineAndCharacterOfPosition(node.getEnd()); + let startLine = startPosition.line + 1; + let startCharacter = startPosition.character + 1; + let endLine = endPosition.line + 1; + let endCharacter = endPosition.character + 1; + + if (symbol) { + Reflect.set(symbol, "isFunction", true); + Reflect.set(symbol, "lineInfo", [startLine, startCharacter, endLine, endCharacter]); + } + + let isProperty: boolean = isMethodDeclaration(node) || isGetAccessor(node) || + isSetAccessor(node) || (isConstructorDeclaration(node) && + !(isClassDeclaration(node.parent) && isViewPUBasedClass(node.parent))); + let isPropertyParent: boolean = (isFunctionExpression(node) || isArrowFunction(node)) && + isPropertyDeclaration(node.parent); + if (isProperty || isPropertyParent) { + let gotNode; + if (node.kind == SyntaxKind.Constructor || + ((node.kind == SyntaxKind.FunctionExpression || + node.kind == SyntaxKind.ArrowFunction) && + node.parent.kind == SyntaxKind.PropertyDeclaration)) { + gotNode = node.parent; + } else { + gotNode = node; + } + let valueName: string = (gotNode.name as Identifier).escapedText.toString(); + let originalName: string = valueName; + for (const [key, val] of globalMangledTable.entries()) { + if (val == valueName) { + originalName = key; + break; + } + } + let keyName = originalName + ':' + startLine + ':' + startCharacter + ':' + endLine + ':' + endCharacter; + memberMethodCache.set(keyName, valueName); + } + addSymbolInScope(node); if (node.symbol && current.parent && !current.parent.defs.has(node.symbol)) { current.parent.defs.add(node.symbol); diff --git a/arkguard/test/grammar/function_like_namecache/function_arrow.ts b/arkguard/test/grammar/function_like_namecache/function_arrow.ts new file mode 100644 index 0000000000..e17966a85d --- /dev/null +++ b/arkguard/test/grammar/function_like_namecache/function_arrow.ts @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +const arrowFunc = () => { + return 1; +}; +const blankFunc = () => {}; + +arrowFunc(); +blankFunc(); \ No newline at end of file diff --git a/arkguard/test/grammar/function_like_namecache/function_arrow_expected_cache.txt b/arkguard/test/grammar/function_like_namecache/function_arrow_expected_cache.txt new file mode 100644 index 0000000000..bae57c7d26 --- /dev/null +++ b/arkguard/test/grammar/function_like_namecache/function_arrow_expected_cache.txt @@ -0,0 +1,8 @@ +{ + "IdentifierCache": { + "#arrowFunc:16:18": "g", + "#blankFunc:19:19": "h", + "#__function": "j" + }, + "MemberMethodCache": {} +} \ No newline at end of file diff --git a/arkguard/test/grammar/function_like_namecache/function_constructor.ts b/arkguard/test/grammar/function_like_namecache/function_constructor.ts new file mode 100644 index 0000000000..a6f0ea679c --- /dev/null +++ b/arkguard/test/grammar/function_like_namecache/function_constructor.ts @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +class Person1 { + age: number; + name: string; + + constructor() { + this.age = 1; + this.name = "Tony"; + } +} + +let person = new Person1(); +console.log(person.age); +console.log(person.name); \ No newline at end of file diff --git a/arkguard/test/grammar/function_like_namecache/function_constructor_expected_cache.txt b/arkguard/test/grammar/function_like_namecache/function_constructor_expected_cache.txt new file mode 100644 index 0000000000..9637e6dc45 --- /dev/null +++ b/arkguard/test/grammar/function_like_namecache/function_constructor_expected_cache.txt @@ -0,0 +1,9 @@ +{ + "IdentifierCache": { + "#Person1": "k", + "#person": "l" + }, + "MemberMethodCache": { + "Person1:20:23": "Person1" + } +} \ No newline at end of file diff --git a/arkguard/test/grammar/function_like_namecache/function_expression.ts b/arkguard/test/grammar/function_like_namecache/function_expression.ts new file mode 100644 index 0000000000..48bb222cc1 --- /dev/null +++ b/arkguard/test/grammar/function_like_namecache/function_expression.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +const ExpFunc = function() { + let a = 1; + return a; +} + +ExpFunc(); \ No newline at end of file diff --git a/arkguard/test/grammar/function_like_namecache/function_expression_expected_cache.txt b/arkguard/test/grammar/function_like_namecache/function_expression_expected_cache.txt new file mode 100644 index 0000000000..4542dd7468 --- /dev/null +++ b/arkguard/test/grammar/function_like_namecache/function_expression_expected_cache.txt @@ -0,0 +1,8 @@ +{ + "IdentifierCache": { + "#ExpFunc:16:19": "m", + "#__function": "n", + "$0#a": "o" + }, + "MemberMethodCache": {} +} \ No newline at end of file diff --git a/arkguard/test/grammar/function_like_namecache/function_in_function.ts b/arkguard/test/grammar/function_like_namecache/function_in_function.ts new file mode 100644 index 0000000000..a66034b3a6 --- /dev/null +++ b/arkguard/test/grammar/function_like_namecache/function_in_function.ts @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +function OutFunction() { + let a = 1; + console.log("out"); + function inFunction() { + console.log("innner Function"); + } + inFunction(); +} + +OutFunction(); \ No newline at end of file diff --git a/arkguard/test/grammar/function_like_namecache/function_in_function_expected_cache.txt b/arkguard/test/grammar/function_like_namecache/function_in_function_expected_cache.txt new file mode 100644 index 0000000000..63fe85f8ac --- /dev/null +++ b/arkguard/test/grammar/function_like_namecache/function_in_function_expected_cache.txt @@ -0,0 +1,8 @@ +{ + "IdentifierCache": { + "#OutFunction:16:23": "p", + "OutFunction#inFunction:19:21": "q", + "OutFunction#a": "r" + }, + "MemberMethodCache": {} +} \ No newline at end of file diff --git a/arkguard/test/grammar/function_like_namecache/function_member_method.ts b/arkguard/test/grammar/function_like_namecache/function_member_method.ts new file mode 100644 index 0000000000..f74acc3414 --- /dev/null +++ b/arkguard/test/grammar/function_like_namecache/function_member_method.ts @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +class Person3 { + age: number = 1; + name: string = "Tony"; + + memberFunc() { + console.log('im a member method'); + } +} + +let per = new Person3(); +per.memberFunc(); + +console.log(per.age); +console.log(per.name); \ No newline at end of file diff --git a/arkguard/test/grammar/function_like_namecache/function_member_method_expected_cache.txt b/arkguard/test/grammar/function_like_namecache/function_member_method_expected_cache.txt new file mode 100644 index 0000000000..798dc31363 --- /dev/null +++ b/arkguard/test/grammar/function_like_namecache/function_member_method_expected_cache.txt @@ -0,0 +1,9 @@ +{ + "IdentifierCache": { + "#Person3": "t", + "#per": "u" + }, + "MemberMethodCache": { + "memberFunc:20:22": "j" + } +} \ No newline at end of file diff --git a/arkguard/test/grammar/function_like_namecache/function_set_get.ts b/arkguard/test/grammar/function_like_namecache/function_set_get.ts new file mode 100644 index 0000000000..17ff23eb93 --- /dev/null +++ b/arkguard/test/grammar/function_like_namecache/function_set_get.ts @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +class Person2 { + private age: number = 10; + name: string = "Tony"; + + get myage(){ + return this.age; + } + + set myage(inputage: number){ + this.age = inputage; + } +} \ No newline at end of file diff --git a/arkguard/test/grammar/function_like_namecache/function_set_get_expected_cache.txt b/arkguard/test/grammar/function_like_namecache/function_set_get_expected_cache.txt new file mode 100644 index 0000000000..ad016fc3c1 --- /dev/null +++ b/arkguard/test/grammar/function_like_namecache/function_set_get_expected_cache.txt @@ -0,0 +1,10 @@ +{ + "IdentifierCache": { + "#Person2": "v", + "Person2#o#inputage": "w" + }, + "MemberMethodCache": { + "myage:20:22": "o", + "myage:24:26": "o" + } +} \ No newline at end of file diff --git a/arkguard/test/grammar/function_like_namecache/obfConfig.json b/arkguard/test/grammar/function_like_namecache/obfConfig.json new file mode 100644 index 0000000000..6a73e349a7 --- /dev/null +++ b/arkguard/test/grammar/function_like_namecache/obfConfig.json @@ -0,0 +1,20 @@ +{ + "mCompact": false, + "mRemoveComments": false, + "mOutputDir": "../../local", + "mDisableHilog": false, + "mDisableConsole": false, + "mSimplify": false, + "mNameObfuscation": { + "mEnable": true, + "mNameGeneratorType": 1, + "mDictionaryList": [], + "mReservedNames": [], + "mRenameProperties": true, + "mReservedProperties": [ "strictEqual" ], + "mKeepStringProperty": false, + "mTopLevel": true + }, + "mEnableSourceMap": true, + "mEnableNameCache": true +} \ No newline at end of file diff --git a/arkguard/test/grammar/function_like_namecache/property_arrow.ts b/arkguard/test/grammar/function_like_namecache/property_arrow.ts new file mode 100644 index 0000000000..ece97a0391 --- /dev/null +++ b/arkguard/test/grammar/function_like_namecache/property_arrow.ts @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +class Person4 { + private age: number = 10; + name: string = "Tony"; + + prop_func = () => { + console.log('arrow') + } +} \ No newline at end of file diff --git a/arkguard/test/grammar/function_like_namecache/property_arrow_expected_cache.txt b/arkguard/test/grammar/function_like_namecache/property_arrow_expected_cache.txt new file mode 100644 index 0000000000..a93fde51a5 --- /dev/null +++ b/arkguard/test/grammar/function_like_namecache/property_arrow_expected_cache.txt @@ -0,0 +1,8 @@ +{ + "IdentifierCache": { + "#Person4": "a1" + }, + "MemberMethodCache": { + "prop_func:20:22": "q" + } +} \ No newline at end of file diff --git a/arkguard/test/grammar/function_like_namecache/property_exp.ts b/arkguard/test/grammar/function_like_namecache/property_exp.ts new file mode 100644 index 0000000000..7e6359f995 --- /dev/null +++ b/arkguard/test/grammar/function_like_namecache/property_exp.ts @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +class Person5 { + private age: number = 10; + name: string = "Tony"; + + prop_func = function () { + console.log('func_exp') + } +} \ No newline at end of file diff --git a/arkguard/test/grammar/function_like_namecache/property_exp_expected_cache.txt b/arkguard/test/grammar/function_like_namecache/property_exp_expected_cache.txt new file mode 100644 index 0000000000..4725acf85e --- /dev/null +++ b/arkguard/test/grammar/function_like_namecache/property_exp_expected_cache.txt @@ -0,0 +1,8 @@ +{ + "IdentifierCache": { + "#Person5": "b1" + }, + "MemberMethodCache": { + "prop_func:20:22": "q" + } +} \ No newline at end of file diff --git a/arkguard/test/grammar/function_like_namecache/same_property_name.ts b/arkguard/test/grammar/function_like_namecache/same_property_name.ts new file mode 100644 index 0000000000..62292e8e5a --- /dev/null +++ b/arkguard/test/grammar/function_like_namecache/same_property_name.ts @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +class MyClass { + prop1: number = 100; + prop2: string = "d"; + + a() { + console.log("im a"); + } +} + +let a = new MyClass(); +a.a(); \ No newline at end of file diff --git a/arkguard/test/grammar/function_like_namecache/same_property_name_expected_cache.txt b/arkguard/test/grammar/function_like_namecache/same_property_name_expected_cache.txt new file mode 100644 index 0000000000..81ad72937e --- /dev/null +++ b/arkguard/test/grammar/function_like_namecache/same_property_name_expected_cache.txt @@ -0,0 +1,9 @@ +{ + "IdentifierCache": { + "#MyClass": "b1", + "#a": "a" + }, + "MemberMethodCache": { + "a:20:22": "a" + } +} \ No newline at end of file -- Gitee