From f72f460eead638bc2594b9cefd9a1661e330c378 Mon Sep 17 00:00:00 2001 From: yehandong Date: Fri, 18 Mar 2022 07:09:16 +0800 Subject: [PATCH 1/6] update the help info of obfuscate --- src/MapleFE/tools/obfuscate/src/main.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/MapleFE/tools/obfuscate/src/main.cpp b/src/MapleFE/tools/obfuscate/src/main.cpp index 7eb882bd04..cd9333d0f7 100644 --- a/src/MapleFE/tools/obfuscate/src/main.cpp +++ b/src/MapleFE/tools/obfuscate/src/main.cpp @@ -1,5 +1,6 @@ /* * Copyright (C) [2022] Futurewei Technologies, Inc. All rights reverved. +* Copyright (C) [2022] Tencent. All rights reverved. * * OpenArkFE is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. @@ -21,7 +22,7 @@ #include "obfuscate.h" static void help() { - std::cout << "ast2cpp a.ast[,b.ast] [options]:" << std::endl; + std::cout << "obfuscate a.ast[,b.ast] [options]:" << std::endl; std::cout << " --out=x.cpp : cpp output file" << std::endl; std::cout << " --help : print this help" << std::endl; std::cout << " --trace=n : Emit trace with 4-bit combo levels 1...15" << std::endl; -- Gitee From f419c65ce7dff48c722ef3875179cfa50dc701f6 Mon Sep 17 00:00:00 2001 From: yehandong Date: Fri, 18 Mar 2022 08:07:25 +0800 Subject: [PATCH 2/6] obfuscate: emit to file.ts.obf --- src/MapleFE/tools/obfuscate/src/main.cpp | 4 ++-- src/MapleFE/tools/obfuscate/src/obfuscate.cpp | 10 +++++++++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/MapleFE/tools/obfuscate/src/main.cpp b/src/MapleFE/tools/obfuscate/src/main.cpp index cd9333d0f7..0865f8545e 100644 --- a/src/MapleFE/tools/obfuscate/src/main.cpp +++ b/src/MapleFE/tools/obfuscate/src/main.cpp @@ -23,7 +23,7 @@ static void help() { std::cout << "obfuscate a.ast[,b.ast] [options]:" << std::endl; - std::cout << " --out=x.cpp : cpp output file" << std::endl; + std::cout << " --out=x.ts : ts output file" << std::endl; std::cout << " --help : print this help" << std::endl; std::cout << " --trace=n : Emit trace with 4-bit combo levels 1...15" << std::endl; std::cout << " 1 : Emit ast tree visits" << std::endl; @@ -44,7 +44,7 @@ int main (int argc, char *argv[]) { unsigned flags; // one or more input .ast files separated by ',' const char *inputname = argv[1]; - // output .cpp file + // output .ast file const char *outputname = nullptr; // Parse the argument diff --git a/src/MapleFE/tools/obfuscate/src/obfuscate.cpp b/src/MapleFE/tools/obfuscate/src/obfuscate.cpp index 17c750339a..cec38cbc9c 100644 --- a/src/MapleFE/tools/obfuscate/src/obfuscate.cpp +++ b/src/MapleFE/tools/obfuscate/src/obfuscate.cpp @@ -1,5 +1,6 @@ /* * Copyright (C) [2022] Futurewei Technologies, Inc. All rights reverved. +* Copyright (C) [2022] Tencent. All rights reverved. * * OpenArkFE is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. @@ -151,7 +152,14 @@ int Obfuscate::ProcessAST() { std::cout << "============= Emitter ===========" << std::endl; maplefe::Emitter emitter(handler); std::string code = emitter.Emit("Convert AST to TypeScript code"); - std::cout << code; + + // Emit to file + std::string of_name(module->GetFilename()); + of_name += ".obf"; + std::ofstream ofs; + ofs.open(of_name.c_str(), std::ofstream::out); + ofs << code; + ofs.close(); } return 0; -- Gitee From 6601594b2159cca7474da569d9a931f97f4d7187 Mon Sep 17 00:00:00 2001 From: yehandong Date: Wed, 23 Mar 2022 06:56:06 +0800 Subject: [PATCH 3/6] update one of MemberExpression. Fixed a bug in ASTBuilder::SetIsConst() --- src/MapleFE/shared/src/ast_builder.cpp | 4 +++- src/MapleFE/typescript/stmt.spec | 5 ++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/MapleFE/shared/src/ast_builder.cpp b/src/MapleFE/shared/src/ast_builder.cpp index 7cb6decc43..016d6a9ae7 100644 --- a/src/MapleFE/shared/src/ast_builder.cpp +++ b/src/MapleFE/shared/src/ast_builder.cpp @@ -1040,6 +1040,8 @@ TreeNode* ASTBuilder::SetIsConst() { } treenode->SetIsConst(); + + mLastTreeNode = treenode; return mLastTreeNode; } @@ -1672,9 +1674,9 @@ TreeNode* ASTBuilder::AddAsType() { if (tree_type) { node->AddAsTypes(tree_type); - mLastTreeNode = node; } + mLastTreeNode = node; return mLastTreeNode; } diff --git a/src/MapleFE/typescript/stmt.spec b/src/MapleFE/typescript/stmt.spec index 973ef07d7e..f8246834db 100644 --- a/src/MapleFE/typescript/stmt.spec +++ b/src/MapleFE/typescript/stmt.spec @@ -391,7 +391,7 @@ rule MemberExpression : ONEOF( IsExpression, MemberExpression + '[' + KeyOf + ']', MemberExpression + '!', - MemberExpression + '.' + JSIdentifier + "as" + "const", + MemberExpression + "as" + "const", '<' + Type + '>' + MemberExpression, PrimaryExpression + "as" + "const", MemberExpression + '.' + KeywordPropName) @@ -409,8 +409,7 @@ rule MemberExpression : ONEOF( attr.action.%8 : AddAsType(%6) attr.action.%10: BuildArrayElement(%1, %3) attr.action.%11: SetIsNonNull(%1) - attr.action.%12: BuildField(%1, %3) - attr.action.%12: SetIsConst() + attr.action.%12: SetIsConst(%1) attr.action.%13: BuildCast(%2, %4) attr.action.%14: PassChild(%1) attr.action.%14: SetIsConst() -- Gitee From b491ac3f72ab926e1a3974374255ebcd5000f40f Mon Sep 17 00:00:00 2001 From: yehandong Date: Wed, 23 Mar 2022 07:05:57 +0800 Subject: [PATCH 4/6] Removed one redundant sub-rule in MemberExpression. --- src/MapleFE/typescript/stmt.spec | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/MapleFE/typescript/stmt.spec b/src/MapleFE/typescript/stmt.spec index f8246834db..55d51f978b 100644 --- a/src/MapleFE/typescript/stmt.spec +++ b/src/MapleFE/typescript/stmt.spec @@ -393,7 +393,6 @@ rule MemberExpression : ONEOF( MemberExpression + '!', MemberExpression + "as" + "const", '<' + Type + '>' + MemberExpression, - PrimaryExpression + "as" + "const", MemberExpression + '.' + KeywordPropName) attr.action.%1 : AddAsType(%1, %2) attr.action.%2 : BuildArrayElement(%1, %3) @@ -411,9 +410,7 @@ rule MemberExpression : ONEOF( attr.action.%11: SetIsNonNull(%1) attr.action.%12: SetIsConst(%1) attr.action.%13: BuildCast(%2, %4) - attr.action.%14: PassChild(%1) - attr.action.%14: SetIsConst() - attr.action.%15 : BuildField(%1, %3) + attr.action.%14 : BuildField(%1, %3) rule IsExpression: ONEOF(PrimaryExpression + "is" + Type, ArrowFunction + "is" + Type) -- Gitee From 28f015e7bf82f92dba2195ac85e47379af13aba5 Mon Sep 17 00:00:00 2001 From: yehandong Date: Wed, 23 Mar 2022 07:23:26 +0800 Subject: [PATCH 5/6] Removed redundant ImportFunction from rule Type. --- src/MapleFE/typescript/stmt.spec | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/MapleFE/typescript/stmt.spec b/src/MapleFE/typescript/stmt.spec index 55d51f978b..a1c991fbc9 100644 --- a/src/MapleFE/typescript/stmt.spec +++ b/src/MapleFE/typescript/stmt.spec @@ -1740,11 +1740,8 @@ rule Type : ONEOF(UnionOrIntersectionOrPrimaryType, InferType, IsExpression, PrimaryType + '[' + TypeQuery + ']', - TemplateLiteral, - ImportFunction, - ImportFunction + '.' + TypeReference) + TemplateLiteral) attr.action.%7,%11 : BuildArrayElement(%1, %3) - attr.action.%14 : BuildField(%1, %3) #rule UnionOrIntersectionOrPrimaryType: ONEOF(UnionType, # IntersectionOrPrimaryType) -- Gitee From 16dad5f2cba64d5e326e5e227d307e53f4167d8a Mon Sep 17 00:00:00 2001 From: yehandong Date: Thu, 24 Mar 2022 03:37:56 +0800 Subject: [PATCH 6/6] Reduced a few redundant sub-rules in PrimaryExpression. --- src/MapleFE/typescript/stmt.spec | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/MapleFE/typescript/stmt.spec b/src/MapleFE/typescript/stmt.spec index a1c991fbc9..4bce8140eb 100644 --- a/src/MapleFE/typescript/stmt.spec +++ b/src/MapleFE/typescript/stmt.spec @@ -173,12 +173,7 @@ rule PrimaryExpression : ONEOF("this", # GeneratorExpression RegularExpression, TemplateLiteral, - ParenthesizedExpression, - Literal + "as" + "const", - ArrayLiteral + "as" + "const", - ObjectLiteral + "as" + "const") - attr.action.%11,%12,%13 : SetIsConst(%1) - attr.action.%11,%12,%13 : PassChild(%1) + ParenthesizedExpression) ##----------------------------------- ##rule CoverParenthesizedExpressionAndArrowParameterList[Yield] : -- Gitee