Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
2301_76393173
jadx
提交
9ac9c052
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,发现更多精彩内容 >>
未验证
提交
9ac9c052
编写于
1月 15, 2022
作者:
S
Skylot
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix: simplify cascading casts (#1336)
上级
b7daf79b
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
47 addition
and
10 deletion
+47
-10
jadx-core/src/main/java/jadx/core/dex/instructions/args/ArgType.java
...rc/main/java/jadx/core/dex/instructions/args/ArgType.java
+1
-1
jadx-core/src/main/java/jadx/core/dex/visitors/SimplifyVisitor.java
...src/main/java/jadx/core/dex/visitors/SimplifyVisitor.java
+18
-6
jadx-core/src/main/java/jadx/core/dex/visitors/typeinference/TypeCompare.java
...ava/jadx/core/dex/visitors/typeinference/TypeCompare.java
+8
-0
jadx-core/src/test/java/jadx/core/dex/visitors/typeinference/TypeCompareTest.java
...jadx/core/dex/visitors/typeinference/TypeCompareTest.java
+6
-1
jadx-core/src/test/java/jadx/tests/integration/inline/TestInline2.java
.../test/java/jadx/tests/integration/inline/TestInline2.java
+1
-1
jadx-core/src/test/java/jadx/tests/integration/others/TestPrimitiveCasts.java
...ava/jadx/tests/integration/others/TestPrimitiveCasts.java
+13
-1
未找到文件。
jadx-core/src/main/java/jadx/core/dex/instructions/args/ArgType.java
浏览文件 @
9ac9c052
...
...
@@ -655,7 +655,7 @@ public abstract class ArgType {
if
(
from
.
equals
(
to
))
{
return
false
;
}
TypeCompareEnum
result
=
root
.
getType
Update
().
getType
Compare
().
compareTypes
(
from
,
to
);
TypeCompareEnum
result
=
root
.
getTypeCompare
().
compareTypes
(
from
,
to
);
return
!
result
.
isNarrow
();
}
...
...
jadx-core/src/main/java/jadx/core/dex/visitors/SimplifyVisitor.java
浏览文件 @
9ac9c052
...
...
@@ -38,6 +38,7 @@ import jadx.core.dex.nodes.MethodNode;
import
jadx.core.dex.nodes.RootNode
;
import
jadx.core.dex.regions.conditions.IfCondition
;
import
jadx.core.dex.visitors.shrink.CodeShrinkVisitor
;
import
jadx.core.dex.visitors.typeinference.TypeCompareEnum
;
import
jadx.core.utils.BlockUtils
;
import
jadx.core.utils.InsnList
;
import
jadx.core.utils.InsnRemover
;
...
...
@@ -82,7 +83,7 @@ public class SimplifyVisitor extends AbstractVisitor {
for
(
int
i
=
0
;
i
<
list
.
size
();
i
++)
{
InsnNode
insn
=
list
.
get
(
i
);
int
insnCount
=
list
.
size
();
InsnNode
modInsn
=
simplifyInsn
(
mth
,
insn
);
InsnNode
modInsn
=
simplifyInsn
(
mth
,
insn
,
null
);
if
(
modInsn
!=
null
)
{
modInsn
.
rebindArgs
();
if
(
i
<
list
.
size
()
&&
list
.
get
(
i
)
==
insn
)
{
...
...
@@ -110,7 +111,7 @@ public class SimplifyVisitor extends AbstractVisitor {
for
(
InsnArg
arg
:
insn
.
getArguments
())
{
if
(
arg
.
isInsnWrap
())
{
InsnNode
wrapInsn
=
((
InsnWrapArg
)
arg
).
getWrapInsn
();
InsnNode
replaceInsn
=
simplifyInsn
(
mth
,
wrapInsn
);
InsnNode
replaceInsn
=
simplifyInsn
(
mth
,
wrapInsn
,
insn
);
if
(
replaceInsn
!=
null
)
{
arg
.
wrapInstruction
(
mth
,
replaceInsn
);
InsnRemover
.
unbindInsn
(
mth
,
wrapInsn
);
...
...
@@ -123,7 +124,7 @@ public class SimplifyVisitor extends AbstractVisitor {
}
}
private
InsnNode
simplifyInsn
(
MethodNode
mth
,
InsnNode
insn
)
{
private
InsnNode
simplifyInsn
(
MethodNode
mth
,
InsnNode
insn
,
@Nullable
InsnNode
parentInsn
)
{
if
(
insn
.
contains
(
AFlag
.
DONT_GENERATE
))
{
return
null
;
}
...
...
@@ -146,8 +147,9 @@ public class SimplifyVisitor extends AbstractVisitor {
case
SPUT:
return
convertFieldArith
(
mth
,
insn
);
case
CAST:
case
CHECK_CAST:
return
processCast
(
mth
,
(
IndexInsnNode
)
insn
);
return
processCast
(
mth
,
(
IndexInsnNode
)
insn
,
parentInsn
);
case
MOVE:
InsnArg
firstArg
=
insn
.
getArg
(
0
);
...
...
@@ -212,7 +214,7 @@ public class SimplifyVisitor extends AbstractVisitor {
return
null
;
}
private
static
InsnNode
processCast
(
MethodNode
mth
,
IndexInsnNode
castInsn
)
{
private
static
InsnNode
processCast
(
MethodNode
mth
,
IndexInsnNode
castInsn
,
@Nullable
InsnNode
parentInsn
)
{
if
(
castInsn
.
contains
(
AFlag
.
EXPLICIT_CAST
))
{
return
null
;
}
...
...
@@ -229,7 +231,8 @@ public class SimplifyVisitor extends AbstractVisitor {
ArgType
castToType
=
(
ArgType
)
castInsn
.
getIndex
();
if
(!
ArgType
.
isCastNeeded
(
mth
.
root
(),
argType
,
castToType
)
||
isCastDuplicate
(
castInsn
))
{
||
isCastDuplicate
(
castInsn
)
||
shadowedByOuterCast
(
mth
.
root
(),
castToType
,
parentInsn
))
{
InsnNode
insnNode
=
new
InsnNode
(
InsnType
.
MOVE
,
1
);
insnNode
.
setOffset
(
castInsn
.
getOffset
());
insnNode
.
setResult
(
castInsn
.
getResult
());
...
...
@@ -254,6 +257,15 @@ public class SimplifyVisitor extends AbstractVisitor {
return
false
;
}
private
static
boolean
shadowedByOuterCast
(
RootNode
root
,
ArgType
castType
,
@Nullable
InsnNode
parentInsn
)
{
if
(
parentInsn
!=
null
&&
parentInsn
.
getType
()
==
InsnType
.
CAST
)
{
ArgType
parentCastType
=
(
ArgType
)
((
IndexInsnNode
)
parentInsn
).
getIndex
();
TypeCompareEnum
result
=
root
.
getTypeCompare
().
compareTypes
(
parentCastType
,
castType
);
return
result
.
isNarrow
();
}
return
false
;
}
/**
* Simplify 'cmp' instruction in if condition
*/
...
...
jadx-core/src/main/java/jadx/core/dex/visitors/typeinference/TypeCompare.java
浏览文件 @
9ac9c052
...
...
@@ -98,6 +98,10 @@ public class TypeCompare {
||
secondPrimitiveType
==
PrimitiveType
.
BOOLEAN
)
{
return
CONFLICT
;
}
if
(
swapEquals
(
firstPrimitiveType
,
secondPrimitiveType
,
PrimitiveType
.
CHAR
,
PrimitiveType
.
BYTE
)
||
swapEquals
(
firstPrimitiveType
,
secondPrimitiveType
,
PrimitiveType
.
CHAR
,
PrimitiveType
.
SHORT
))
{
return
CONFLICT
;
}
return
firstPrimitiveType
.
compareTo
(
secondPrimitiveType
)
>
0
?
WIDER
:
NARROW
;
}
...
...
@@ -105,6 +109,10 @@ public class TypeCompare {
return
TypeCompareEnum
.
CONFLICT
;
}
private
boolean
swapEquals
(
PrimitiveType
first
,
PrimitiveType
second
,
PrimitiveType
a
,
PrimitiveType
b
)
{
return
(
first
==
a
&&
second
==
b
)
||
(
first
==
b
&&
second
==
a
);
}
private
TypeCompareEnum
compareArrayWithOtherType
(
ArgType
array
,
ArgType
other
)
{
if
(!
other
.
isTypeKnown
())
{
if
(
other
.
contains
(
PrimitiveType
.
ARRAY
))
{
...
...
jadx-core/src/test/java/jadx/core/dex/visitors/typeinference/TypeCompareTest.java
浏览文件 @
9ac9c052
...
...
@@ -63,10 +63,15 @@ public class TypeCompareTest {
public
void
comparePrimitives
()
{
check
(
INT
,
UNKNOWN_OBJECT
,
TypeCompareEnum
.
CONFLICT
);
check
(
INT
,
OBJECT
,
TypeCompareEnum
.
CONFLICT
);
check
(
INT
,
BOOLEAN
,
TypeCompareEnum
.
CONFLICT
);
check
(
INT
,
CHAR
,
TypeCompareEnum
.
WIDER
);
check
(
INT
,
SHORT
,
TypeCompareEnum
.
WIDER
);
check
(
BOOLEAN
,
INT
,
TypeCompareEnum
.
CONFLICT
);
check
(
BOOLEAN
,
CHAR
,
TypeCompareEnum
.
CONFLICT
);
check
(
CHAR
,
BYTE
,
TypeCompareEnum
.
CONFLICT
);
check
(
CHAR
,
SHORT
,
TypeCompareEnum
.
CONFLICT
);
firstIsNarrow
(
CHAR
,
NARROW_INTEGRAL
);
firstIsNarrow
(
array
(
CHAR
),
UNKNOWN_OBJECT
);
}
...
...
jadx-core/src/test/java/jadx/tests/integration/inline/TestInline2.java
浏览文件 @
9ac9c052
...
...
@@ -31,6 +31,6 @@ public class TestInline2 extends IntegrationTest {
assertThat
(
code
,
containsOne
(
"int[] a = {1, 2, 4, 6, 8};"
));
assertThat
(
code
,
containsOne
(
"for (int i = 0; i < a.length; i += 2) {"
));
assertThat
(
code
,
containsOne
(
"for (long i2 =
(long)
b; i2 > 0; i2--) {"
));
assertThat
(
code
,
containsOne
(
"for (long i2 = b; i2 > 0; i2--) {"
));
}
}
jadx-core/src/test/java/jadx/tests/integration/others/TestPrimitiveCasts.java
浏览文件 @
9ac9c052
...
...
@@ -17,16 +17,24 @@ public class TestPrimitiveCasts extends IntegrationTest {
useByte
((
byte
)
getInt
());
useChar
((
char
)
0
);
useChar
((
char
)
getInt
());
useShort
((
short
)
0L
);
useShort
((
short
)
getLong
());
useByte
((
byte
)
0L
);
useByte
((
byte
)
getLong
());
useChar
((
char
)
0L
);
useChar
((
char
)
getLong
());
useShort
((
short
)
' '
);
useShort
((
short
)
getChar
());
useByte
((
byte
)
' '
);
useByte
((
byte
)
getChar
());
useInt
((
byte
)
7
);
useInt
((
char
)
' '
);
useInt
(
getChar
());
useInt
((
int
)
2L
);
useInt
((
int
)
getLong
());
}
private
long
getLong
()
{
...
...
@@ -49,6 +57,9 @@ public class TestPrimitiveCasts extends IntegrationTest {
private
void
useShort
(
short
s
)
{
}
private
void
useInt
(
int
i
)
{
}
}
@TestWithProfiles
({
TestProfile
.
DX_J8
,
TestProfile
.
JAVA8
})
...
...
@@ -56,6 +67,7 @@ public class TestPrimitiveCasts extends IntegrationTest {
noDebugInfo
();
assertThat
(
getClassNode
(
TestCls
.
class
))
.
code
()
.
doesNotContain
(
"(0)"
);
.
doesNotContain
(
"(0)"
)
.
doesNotContain
(
") ((int) getLong())"
);
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录