From 8df6d3f82904e331183b4e4c189a084ddcc3825d Mon Sep 17 00:00:00 2001 From: dengxinyu Date: Mon, 19 Feb 2024 03:25:17 +0000 Subject: [PATCH] fixed ee9c297 from https://gitee.com/Xinyu-Deng/arkcompiler_ets_frontend/pulls/1841 Bugfix for disable console doesn't work on CaseClause, DefaultClause and ModuleBlock Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/I920X4 Signed-off-by: dengxinyu Change-Id: Ic0dcdedbb4c563f3a8059e3432110d17f9c5d5ec --- arkguard/README-cn.md | 6 +++- arkguard/README.md | 7 +++- .../layout/DisableConsoleTransformer.ts | 35 ++++++++++++++----- arkguard/test/grammar/console/blockTest.ts | 24 +++++++++++++ .../grammar/console/blockTest_expected.txt | 21 +++++++++++ arkguard/test/grammar/console/clauseTest.ts | 29 +++++++++++++++ .../grammar/console/clauseTest_expected.txt | 21 +++++++++++ arkguard/test/grammar/console/consoleTest.ts | 34 ++++++++++++++++++ .../grammar/console/consoleTest_expected.txt | 23 ++++++++++++ .../test/grammar/console/moduleBlockTest.ts | 20 +++++++++++ .../console/moduleBlockTest_expected.txt | 18 ++++++++++ arkguard/test/grammar/console/multiConsole.ts | 28 +++++++++++++++ .../grammar/console/multiConsole_expected.txt | 18 ++++++++++ arkguard/test/grammar/console/obfConfig.json | 18 ++++++++++ .../test/grammar/console/sourceFileTest.ts | 19 ++++++++++ .../console/sourceFileTest_expected.txt | 16 +++++++++ 16 files changed, 327 insertions(+), 10 deletions(-) create mode 100644 arkguard/test/grammar/console/blockTest.ts create mode 100644 arkguard/test/grammar/console/blockTest_expected.txt create mode 100644 arkguard/test/grammar/console/clauseTest.ts create mode 100644 arkguard/test/grammar/console/clauseTest_expected.txt create mode 100644 arkguard/test/grammar/console/consoleTest.ts create mode 100644 arkguard/test/grammar/console/consoleTest_expected.txt create mode 100644 arkguard/test/grammar/console/moduleBlockTest.ts create mode 100644 arkguard/test/grammar/console/moduleBlockTest_expected.txt create mode 100644 arkguard/test/grammar/console/multiConsole.ts create mode 100644 arkguard/test/grammar/console/multiConsole_expected.txt create mode 100644 arkguard/test/grammar/console/obfConfig.json create mode 100644 arkguard/test/grammar/console/sourceFileTest.ts create mode 100644 arkguard/test/grammar/console/sourceFileTest_expected.txt diff --git a/arkguard/README-cn.md b/arkguard/README-cn.md index d18353e68e..83b75f6ebd 100644 --- a/arkguard/README-cn.md +++ b/arkguard/README-cn.md @@ -150,7 +150,11 @@ Arkguard只混淆参数名和局部变量名(通过将它们重新命名为随 `-remove-log` -删除所有`console.*`语句。 +删除以下场景中对 console.* 语句的调用,要求console.*语句返回值未被调用。 +1. 文件顶层的调用 +2. 代码块Block中的调用 +3. 模块Module中的调用 +4. switch语句中的调用 `-print-namecache` filepath diff --git a/arkguard/README.md b/arkguard/README.md index fa68968969..d71357eea1 100644 --- a/arkguard/README.md +++ b/arkguard/README.md @@ -166,7 +166,12 @@ one line. `-remove-log` -Specifies to remove all `console.*` statements. +Delete the expressions involving direct calls to console.* statements in the following scenarios: +1. Calls at the toplevel level of a file. +2. Calls within a block. +3. Calls within a module. +4. Calls within a switch statement. +and the return value of console.* should not be called `-print-namecache` filepath diff --git a/arkguard/src/transformers/layout/DisableConsoleTransformer.ts b/arkguard/src/transformers/layout/DisableConsoleTransformer.ts index 475cffbef4..74ce02dda1 100644 --- a/arkguard/src/transformers/layout/DisableConsoleTransformer.ts +++ b/arkguard/src/transformers/layout/DisableConsoleTransformer.ts @@ -17,9 +17,12 @@ import { factory, isBlock, isCallExpression, + isCaseClause, + isDefaultClause, isElementAccessExpression, isExpressionStatement, isIdentifier, + isModuleBlock, isPropertyAccessExpression, isSourceFile, setParentRecursive, @@ -28,7 +31,10 @@ import { import type { Block, + CaseClause, + DefaultClause, LeftHandSideExpression, + ModuleBlock, Node, NodeArray, SourceFile, @@ -76,21 +82,34 @@ namespace secharmony { * @param node */ function visitAst(node: Node): Node { - if (isSourceFile(node)) { - const visitedAst: SourceFile = visitEachChild(node, visitAst, context); - const deletedStatements: Statement[] = deleteConsoleStatement(visitedAst.statements); + const visitedAst = visitEachChild(node, visitAst, context); + + if (!(isSourceFile(node) || isBlock(node) || isModuleBlock(node) || isCaseClause(node) || isDefaultClause(node))) { + return visitedAst; + } + //@ts-ignore + const deletedStatements: Statement[] = deleteConsoleStatement(visitedAst.statements); + + if (isSourceFile(node)) { return factory.updateSourceFile(node, deletedStatements); } - if (!isBlock(node)) { - return visitEachChild(node, visitAst, context); + if (isBlock(node)) { + return factory.createBlock(deletedStatements, true); } - const visitedBlock: Block = visitEachChild(node, visitAst, context); - const newStatements: Statement[] = deleteConsoleStatement(visitedBlock.statements); + if (isModuleBlock(node)) { + return factory.createModuleBlock(deletedStatements) + } + + if (isCaseClause(node)) { + return factory.createCaseClause(node.expression, deletedStatements); + } - return factory.createBlock(newStatements, true); + if (isDefaultClause(node)) { + return factory.createDefaultClause(deletedStatements); + } } function deleteConsoleStatement(statements: NodeArray): Statement[] { diff --git a/arkguard/test/grammar/console/blockTest.ts b/arkguard/test/grammar/console/blockTest.ts new file mode 100644 index 0000000000..651e99f380 --- /dev/null +++ b/arkguard/test/grammar/console/blockTest.ts @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2024 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. + */ + +let para3 = 10; +{ + let para1 = 10; + { + let para2 = 20; + console.log(para1 + para2); + console.log(para1 + para2); + } +} diff --git a/arkguard/test/grammar/console/blockTest_expected.txt b/arkguard/test/grammar/console/blockTest_expected.txt new file mode 100644 index 0000000000..158453f512 --- /dev/null +++ b/arkguard/test/grammar/console/blockTest_expected.txt @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2024 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. + */ +let g = 10; +{ + let h = 10; + { + let i = 20; + } +} diff --git a/arkguard/test/grammar/console/clauseTest.ts b/arkguard/test/grammar/console/clauseTest.ts new file mode 100644 index 0000000000..fce7ea58d9 --- /dev/null +++ b/arkguard/test/grammar/console/clauseTest.ts @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2024 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. + */ + +let weather = 3; +switch (weather) { + case 1: + console.log("Cloudy"); + break; + case 2: + console.log("Sunny"); + break; + case 3: + console.log("Windy"); + break; + default: + console.log("Snowy"); +} diff --git a/arkguard/test/grammar/console/clauseTest_expected.txt b/arkguard/test/grammar/console/clauseTest_expected.txt new file mode 100644 index 0000000000..2f10971d0a --- /dev/null +++ b/arkguard/test/grammar/console/clauseTest_expected.txt @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2024 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. + */ +let j = 3; +switch (j) { + case 1: break; + case 2: break; + case 3: break; + default: +} diff --git a/arkguard/test/grammar/console/consoleTest.ts b/arkguard/test/grammar/console/consoleTest.ts new file mode 100644 index 0000000000..983aeda4f4 --- /dev/null +++ b/arkguard/test/grammar/console/consoleTest.ts @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2024 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 obj = {393: 'hello'}; +console.log("hello"); + +namespace Hello { + console.log("wew"); + console.log("wew"); +} + +function say() { + let num = 121; + switch (num) { + case 121: + console.log("rig"); + break; + default: + console.log("sdsdsd"); + break; + } +} \ No newline at end of file diff --git a/arkguard/test/grammar/console/consoleTest_expected.txt b/arkguard/test/grammar/console/consoleTest_expected.txt new file mode 100644 index 0000000000..ae41880bed --- /dev/null +++ b/arkguard/test/grammar/console/consoleTest_expected.txt @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2024 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 l = { h: 'hello' }; +namespace m { } +function k() { + let n = 121; + switch (n) { + case 121: break; + default: break; + } +} diff --git a/arkguard/test/grammar/console/moduleBlockTest.ts b/arkguard/test/grammar/console/moduleBlockTest.ts new file mode 100644 index 0000000000..fa5fe9b78b --- /dev/null +++ b/arkguard/test/grammar/console/moduleBlockTest.ts @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2024 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. + */ + +module MyModule { + export function greet(name: string) { + console.log(`Hello, ${name}!`); + } +} diff --git a/arkguard/test/grammar/console/moduleBlockTest_expected.txt b/arkguard/test/grammar/console/moduleBlockTest_expected.txt new file mode 100644 index 0000000000..b87d2a88fc --- /dev/null +++ b/arkguard/test/grammar/console/moduleBlockTest_expected.txt @@ -0,0 +1,18 @@ +/* + * Copyright (c) 2024 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. + */ +module o { + export function greet(p: string) { + } +} diff --git a/arkguard/test/grammar/console/multiConsole.ts b/arkguard/test/grammar/console/multiConsole.ts new file mode 100644 index 0000000000..91a4725ed0 --- /dev/null +++ b/arkguard/test/grammar/console/multiConsole.ts @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2024 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 foo() { + console.error('This is an error message.'); + console.warn('This is a warning message.'); + console.info('This is an informational message.'); + console.debug('This is a debug message.'); + console.assert(1 === 2, 'This assertion failed.'); + const obj = { name: 'John', age: 30 }; + console.dir(obj); + const data = [{ id: 1, name: 'John' }, { id: 2, name: 'Jane' }]; + console.table(data); + console.time('timer'); + console.timeEnd('timer'); +} \ No newline at end of file diff --git a/arkguard/test/grammar/console/multiConsole_expected.txt b/arkguard/test/grammar/console/multiConsole_expected.txt new file mode 100644 index 0000000000..86d19489b6 --- /dev/null +++ b/arkguard/test/grammar/console/multiConsole_expected.txt @@ -0,0 +1,18 @@ +/* + * Copyright (c) 2024 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 q() { + const r = { name: 'John', i: 30 }; + const s = [{ id: 1, name: 'John' }, { id: 2, name: 'Jane' }]; +} diff --git a/arkguard/test/grammar/console/obfConfig.json b/arkguard/test/grammar/console/obfConfig.json new file mode 100644 index 0000000000..33493dfda1 --- /dev/null +++ b/arkguard/test/grammar/console/obfConfig.json @@ -0,0 +1,18 @@ +{ + "mCompact": false, + "mRemoveComments": false, + "mOutputDir": "../../local", + "mDisableHilog": false, + "mDisableConsole": true, + "mSimplify": false, + "mNameObfuscation": { + "mEnable": true, + "mNameGeneratorType": 1, + "mDictionaryList": [], + "mRenameProperties": true, + "mKeepStringProperty": false, + "mTopLevel": true + }, + "mEnableSourceMap": false, + "mEnableNameCache": false +} \ No newline at end of file diff --git a/arkguard/test/grammar/console/sourceFileTest.ts b/arkguard/test/grammar/console/sourceFileTest.ts new file mode 100644 index 0000000000..e5a91bc1a8 --- /dev/null +++ b/arkguard/test/grammar/console/sourceFileTest.ts @@ -0,0 +1,19 @@ +/* + * Copyright (c) 2024 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 foo() { + console.log("456798"); +} +console.log("123456"); \ No newline at end of file diff --git a/arkguard/test/grammar/console/sourceFileTest_expected.txt b/arkguard/test/grammar/console/sourceFileTest_expected.txt new file mode 100644 index 0000000000..1982827ebb --- /dev/null +++ b/arkguard/test/grammar/console/sourceFileTest_expected.txt @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2024 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 q() { +} -- Gitee