Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
2301_76393173
jadx
提交
c8de7b97
J
jadx
项目概览
2301_76393173
/
jadx
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
J
jadx
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
c8de7b97
编写于
7月 21, 2019
作者:
S
Skylot
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix: instead commenting move constructor call to the top (#704)
上级
b32dc17d
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
62 addition
and
78 deletion
+62
-78
jadx-core/src/main/java/jadx/core/dex/instructions/mods/ConstructorInsn.java
...java/jadx/core/dex/instructions/mods/ConstructorInsn.java
+1
-1
jadx-core/src/main/java/jadx/core/dex/visitors/PrepareForCodeGen.java
...c/main/java/jadx/core/dex/visitors/PrepareForCodeGen.java
+51
-75
jadx-core/src/main/java/jadx/core/utils/BlockUtils.java
jadx-core/src/main/java/jadx/core/utils/BlockUtils.java
+8
-0
jadx-core/src/test/java/jadx/tests/integration/others/TestInsnsBeforeSuper.java
...a/jadx/tests/integration/others/TestInsnsBeforeSuper.java
+1
-1
jadx-core/src/test/java/jadx/tests/integration/others/TestInsnsBeforeThis.java
...va/jadx/tests/integration/others/TestInsnsBeforeThis.java
+1
-1
未找到文件。
jadx-core/src/main/java/jadx/core/dex/instructions/mods/ConstructorInsn.java
浏览文件 @
c8de7b97
...
@@ -15,7 +15,7 @@ public class ConstructorInsn extends InsnNode implements CallMthInterface {
...
@@ -15,7 +15,7 @@ public class ConstructorInsn extends InsnNode implements CallMthInterface {
private
final
CallType
callType
;
private
final
CallType
callType
;
private
final
RegisterArg
instanceArg
;
private
final
RegisterArg
instanceArg
;
p
rivate
enum
CallType
{
p
ublic
enum
CallType
{
CONSTRUCTOR
,
// just new instance
CONSTRUCTOR
,
// just new instance
SUPER
,
// super call
SUPER
,
// super call
THIS
,
// call constructor from other constructor
THIS
,
// call constructor from other constructor
...
...
jadx-core/src/main/java/jadx/core/dex/visitors/PrepareForCodeGen.java
浏览文件 @
c8de7b97
package
jadx.core.dex.visitors
;
package
jadx.core.dex.visitors
;
import
java.util.Collections
;
import
java.util.HashSet
;
import
java.util.Iterator
;
import
java.util.Iterator
;
import
java.util.List
;
import
java.util.List
;
import
java.util.Set
;
import
org.jetbrains.annotations.Nullable
;
import
jadx.core.dex.attributes.AFlag
;
import
jadx.core.dex.attributes.AFlag
;
import
jadx.core.dex.attributes.AType
;
import
jadx.core.dex.attributes.nodes.DeclareVariablesAttr
;
import
jadx.core.dex.instructions.ArithNode
;
import
jadx.core.dex.instructions.ArithNode
;
import
jadx.core.dex.instructions.ArithOp
;
import
jadx.core.dex.instructions.ArithOp
;
import
jadx.core.dex.instructions.InsnType
;
import
jadx.core.dex.instructions.InsnType
;
...
@@ -13,16 +20,16 @@ import jadx.core.dex.instructions.args.RegisterArg;
...
@@ -13,16 +20,16 @@ import jadx.core.dex.instructions.args.RegisterArg;
import
jadx.core.dex.instructions.mods.ConstructorInsn
;
import
jadx.core.dex.instructions.mods.ConstructorInsn
;
import
jadx.core.dex.instructions.mods.TernaryInsn
;
import
jadx.core.dex.instructions.mods.TernaryInsn
;
import
jadx.core.dex.nodes.BlockNode
;
import
jadx.core.dex.nodes.BlockNode
;
import
jadx.core.dex.nodes.IBlock
;
import
jadx.core.dex.nodes.InsnContainer
;
import
jadx.core.dex.nodes.IRegion
;
import
jadx.core.dex.nodes.InsnNode
;
import
jadx.core.dex.nodes.InsnNode
;
import
jadx.core.dex.nodes.MethodNode
;
import
jadx.core.dex.nodes.MethodNode
;
import
jadx.core.dex.regions.Region
;
import
jadx.core.dex.regions.conditions.IfCondition
;
import
jadx.core.dex.regions.conditions.IfCondition
;
import
jadx.core.dex.regions.conditions.IfCondition.Mode
;
import
jadx.core.dex.regions.conditions.IfCondition.Mode
;
import
jadx.core.dex.visitors.regions.AbstractRegionVisitor
;
import
jadx.core.dex.visitors.regions.DepthRegionTraversal
;
import
jadx.core.dex.visitors.regions.variables.ProcessVariables
;
import
jadx.core.dex.visitors.regions.variables.ProcessVariables
;
import
jadx.core.dex.visitors.shrink.CodeShrinkVisitor
;
import
jadx.core.dex.visitors.shrink.CodeShrinkVisitor
;
import
jadx.core.utils.BlockUtils
;
import
jadx.core.utils.InsnList
;
import
jadx.core.utils.exceptions.JadxException
;
import
jadx.core.utils.exceptions.JadxException
;
/**
/**
...
@@ -52,7 +59,7 @@ public class PrepareForCodeGen extends AbstractVisitor {
...
@@ -52,7 +59,7 @@ public class PrepareForCodeGen extends AbstractVisitor {
removeParenthesis
(
block
);
removeParenthesis
(
block
);
modifyArith
(
block
);
modifyArith
(
block
);
}
}
commentOutInsns
InConstructor
(
mth
);
moveConstructor
InConstructor
(
mth
);
}
}
private
static
void
removeInstructions
(
BlockNode
block
)
{
private
static
void
removeInstructions
(
BlockNode
block
)
{
...
@@ -179,15 +186,52 @@ public class PrepareForCodeGen extends AbstractVisitor {
...
@@ -179,15 +186,52 @@ public class PrepareForCodeGen extends AbstractVisitor {
}
}
}
}
private
void
commentOutInsnsInConstructor
(
MethodNode
mth
)
{
/**
* Check that 'super' or 'this' call in constructor is a first instruction.
* Otherwise move to top and add a warning if code breaks.
*/
private
void
moveConstructorInConstructor
(
MethodNode
mth
)
{
if
(
mth
.
isConstructor
())
{
if
(
mth
.
isConstructor
())
{
ConstructorInsn
constrInsn
=
searchConstructorCall
(
mth
);
ConstructorInsn
constrInsn
=
searchConstructorCall
(
mth
);
if
(
constrInsn
!=
null
&&
!
constrInsn
.
contains
(
AFlag
.
DONT_GENERATE
))
{
if
(
constrInsn
!=
null
&&
!
constrInsn
.
contains
(
AFlag
.
DONT_GENERATE
))
{
DepthRegionTraversal
.
traverse
(
mth
,
new
ConstructorRegionVisitor
(
constrInsn
));
Region
oldRootRegion
=
mth
.
getRegion
();
boolean
firstInsn
=
BlockUtils
.
isFirstInsn
(
mth
,
constrInsn
);
DeclareVariablesAttr
declVarsAttr
=
oldRootRegion
.
get
(
AType
.
DECLARE_VARIABLES
);
if
(
firstInsn
&&
declVarsAttr
==
null
)
{
// move not needed
return
;
}
// move constructor instruction to new root region
String
callType
=
constrInsn
.
getCallType
().
toString
().
toLowerCase
();
BlockNode
blockByInsn
=
BlockUtils
.
getBlockByInsn
(
mth
,
constrInsn
);
if
(
blockByInsn
==
null
)
{
mth
.
addWarn
(
"Failed to move "
+
callType
+
" instruction to top"
);
return
;
}
InsnList
.
remove
(
blockByInsn
,
constrInsn
);
Region
region
=
new
Region
(
null
);
region
.
add
(
new
InsnContainer
(
Collections
.
singletonList
(
constrInsn
)));
region
.
add
(
oldRootRegion
);
mth
.
setRegion
(
region
);
if
(!
firstInsn
)
{
Set
<
RegisterArg
>
regArgs
=
new
HashSet
<>();
constrInsn
.
getRegisterArgs
(
regArgs
);
regArgs
.
remove
(
mth
.
getThisArg
());
regArgs
.
removeAll
(
mth
.
getArguments
(
false
));
if
(!
regArgs
.
isEmpty
())
{
mth
.
addWarn
(
"Illegal instructions before constructor call"
);
}
else
{
mth
.
addComment
(
"JADX INFO: "
+
callType
+
" call moved to the top of the method (can break code semantics)"
);
}
}
}
}
}
}
}
}
@Nullable
private
ConstructorInsn
searchConstructorCall
(
MethodNode
mth
)
{
private
ConstructorInsn
searchConstructorCall
(
MethodNode
mth
)
{
for
(
BlockNode
block
:
mth
.
getBasicBlocks
())
{
for
(
BlockNode
block
:
mth
.
getBasicBlocks
())
{
for
(
InsnNode
insn
:
block
.
getInstructions
())
{
for
(
InsnNode
insn
:
block
.
getInstructions
())
{
...
@@ -202,72 +246,4 @@ public class PrepareForCodeGen extends AbstractVisitor {
...
@@ -202,72 +246,4 @@ public class PrepareForCodeGen extends AbstractVisitor {
}
}
return
null
;
return
null
;
}
}
private
static
final
class
ConstructorRegionVisitor
extends
AbstractRegionVisitor
{
private
final
ConstructorInsn
constrInsn
;
private
int
regionDepth
;
private
boolean
found
;
private
boolean
brokenCode
;
private
int
commentedCount
;
public
ConstructorRegionVisitor
(
ConstructorInsn
constrInsn
)
{
this
.
constrInsn
=
constrInsn
;
}
@Override
public
boolean
enterRegion
(
MethodNode
mth
,
IRegion
region
)
{
if
(
found
)
{
return
false
;
}
regionDepth
++;
return
true
;
}
@Override
public
void
leaveRegion
(
MethodNode
mth
,
IRegion
region
)
{
if
(!
found
)
{
regionDepth
--;
region
.
add
(
AFlag
.
COMMENT_OUT
);
commentedCount
++;
}
}
@Override
public
void
processBlock
(
MethodNode
mth
,
IBlock
container
)
{
if
(
found
)
{
return
;
}
for
(
InsnNode
insn
:
container
.
getInstructions
())
{
if
(
insn
==
constrInsn
)
{
found
=
true
;
addMethodMsg
(
mth
);
break
;
}
insn
.
add
(
AFlag
.
COMMENT_OUT
);
commentedCount
++;
if
(!
brokenCode
)
{
RegisterArg
resArg
=
insn
.
getResult
();
if
(
resArg
!=
null
)
{
for
(
RegisterArg
arg
:
resArg
.
getSVar
().
getUseList
())
{
if
(
arg
.
getParentInsn
()
==
constrInsn
)
{
brokenCode
=
true
;
break
;
}
}
}
}
}
}
private
void
addMethodMsg
(
MethodNode
mth
)
{
if
(
commentedCount
>
0
)
{
String
msg
=
"Illegal instructions before constructor call commented (this can break semantics)"
;
if
(
brokenCode
||
regionDepth
>
1
)
{
mth
.
addWarn
(
msg
);
}
else
{
mth
.
addComment
(
"JADX WARN: "
+
msg
);
}
}
}
}
}
}
jadx-core/src/main/java/jadx/core/utils/BlockUtils.java
浏览文件 @
c8de7b97
...
@@ -574,6 +574,14 @@ public class BlockUtils {
...
@@ -574,6 +574,14 @@ public class BlockUtils {
return
insns
;
return
insns
;
}
}
public
static
boolean
isFirstInsn
(
MethodNode
mth
,
InsnNode
insn
)
{
BlockNode
enterBlock
=
mth
.
getEnterBlock
();
if
(
enterBlock
==
null
||
enterBlock
.
getInstructions
().
isEmpty
())
{
return
false
;
}
return
enterBlock
.
getInstructions
().
get
(
0
)
==
insn
;
}
/**
/**
* Replace insn by index i in block,
* Replace insn by index i in block,
* for proper copy attributes, assume attributes are not overlap
* for proper copy attributes, assume attributes are not overlap
...
...
jadx-core/src/test/java/jadx/tests/integration/others/TestInsnsBeforeSuper.java
浏览文件 @
c8de7b97
...
@@ -37,6 +37,6 @@ public class TestInsnsBeforeSuper extends SmaliTest {
...
@@ -37,6 +37,6 @@ public class TestInsnsBeforeSuper extends SmaliTest {
ClassNode
cls
=
getClassNodeFromSmaliFiles
(
"B"
);
ClassNode
cls
=
getClassNodeFromSmaliFiles
(
"B"
);
String
code
=
cls
.
getCode
().
toString
();
String
code
=
cls
.
getCode
().
toString
();
assertThat
(
code
,
containsOne
(
"
//
checkNull(str);"
));
assertThat
(
code
,
containsOne
(
"checkNull(str);"
));
}
}
}
}
jadx-core/src/test/java/jadx/tests/integration/others/TestInsnsBeforeThis.java
浏览文件 @
c8de7b97
...
@@ -35,6 +35,6 @@ public class TestInsnsBeforeThis extends SmaliTest {
...
@@ -35,6 +35,6 @@ public class TestInsnsBeforeThis extends SmaliTest {
ClassNode
cls
=
getClassNodeFromSmali
();
ClassNode
cls
=
getClassNodeFromSmali
();
String
code
=
cls
.
getCode
().
toString
();
String
code
=
cls
.
getCode
().
toString
();
assertThat
(
code
,
containsOne
(
"
//
checkNull(str);"
));
assertThat
(
code
,
containsOne
(
"checkNull(str);"
));
}
}
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录