Skip to content

Commit 0f10065

Browse files
committed
[SMALI] Registers & Numbers validation
1 parent 0977916 commit 0f10065

55 files changed

Lines changed: 1462 additions & 1459 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/main/java/com/reandroid/dex/common/OperandType.java

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,67 @@
1515
*/
1616
package com.reandroid.dex.common;
1717

18+
import com.reandroid.dex.id.IdItem;
19+
import com.reandroid.dex.sections.SectionType;
20+
1821
public class OperandType {
1922

2023
public static final OperandType NONE;
2124
public static final OperandType HEX;
2225
public static final OperandType DECIMAL;
2326
public static final OperandType LABEL;
24-
public static final OperandType KEY;
25-
public static final OperandType DUAL_KEY;
27+
public static final OperandType STRING;
28+
public static final OperandType TYPE;
29+
public static final OperandType FIELD;
30+
public static final OperandType PROTO;
31+
public static final OperandType METHOD;
32+
public static final OperandType METHOD_HANDLE;
33+
public static final OperandType CALL_SITE;
34+
public static final OperandType METHOD_AND_PROTO;
2635

2736
static {
2837
NONE = new OperandType("NONE");
2938
HEX = new OperandType("HEX");
3039
DECIMAL = new OperandType("DECIMAL");
3140
LABEL = new OperandType("LABEL");
32-
KEY = new OperandType("KEY");
33-
DUAL_KEY = new OperandType("DUAL_KEY");
41+
STRING = new OperandType("STRING", SectionType.STRING_ID);
42+
TYPE = new OperandType("TYPE", SectionType.TYPE_ID);
43+
FIELD = new OperandType("FIELD", SectionType.FIELD_ID);
44+
PROTO = new OperandType("PROTO", SectionType.PROTO_ID);
45+
METHOD = new OperandType("METHOD", SectionType.METHOD_ID);
46+
METHOD_HANDLE = new OperandType("METHOD_HANDLE", SectionType.METHOD_HANDLE);
47+
CALL_SITE = new OperandType("CALL_SITE", SectionType.CALL_SITE_ID);
48+
METHOD_AND_PROTO = new OperandType("METHOD_AND_PROTO", SectionType.METHOD_ID, SectionType.PROTO_ID);
3449
}
3550

3651
private final String name;
52+
private final SectionType<? extends IdItem> sectionType;
53+
private final SectionType<? extends IdItem> sectionType2;
3754

38-
private OperandType(String name){
55+
private OperandType(String name) {
56+
this(name, null, null);
57+
}
58+
private OperandType(String name, SectionType<? extends IdItem> sectionType) {
59+
this(name, sectionType, null);
60+
}
61+
private OperandType(String name, SectionType<? extends IdItem> sectionType,
62+
SectionType<? extends IdItem> sectionType2) {
3963
this.name = name;
64+
this.sectionType = sectionType;
65+
this.sectionType2 = sectionType2;
66+
}
67+
68+
public SectionType<? extends IdItem> getSectionType() {
69+
return sectionType;
70+
}
71+
public SectionType<? extends IdItem> getSectionType2() {
72+
return sectionType2;
73+
}
74+
public boolean hasSectionId() {
75+
return sectionType != null;
76+
}
77+
public boolean hasSectionId2() {
78+
return sectionType2 != null;
4079
}
4180

4281
@Override

src/main/java/com/reandroid/dex/common/RegisterFormat.java

Lines changed: 162 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,115 @@
1818
public class RegisterFormat {
1919

2020
public static final RegisterFormat NONE;
21-
public static final RegisterFormat READ;
22-
public static final RegisterFormat WRITE;
23-
public static final RegisterFormat READ_READ;
24-
public static final RegisterFormat READ_WRITE;
25-
public static final RegisterFormat READ_READ_READ;
26-
public static final RegisterFormat WRITE_READ;
27-
public static final RegisterFormat WRITE_READ_READ;
21+
22+
public static final RegisterFormat READ8;
23+
public static final RegisterFormat READ8W;
24+
public static final RegisterFormat READ16;
25+
public static final RegisterFormat WRITE4;
26+
public static final RegisterFormat WRITE8;
27+
public static final RegisterFormat WRITE8W;
28+
29+
public static final RegisterFormat READ4_READ4;
30+
public static final RegisterFormat READ4W_READ4;
31+
32+
public static final RegisterFormat READ8_READ8_READ8;
33+
public static final RegisterFormat READ8W_READ8_READ8;
34+
35+
public static final RegisterFormat RW4_READ4;
36+
public static final RegisterFormat RW4W_READ4;
37+
public static final RegisterFormat RW4W_READ4W;
38+
39+
public static final RegisterFormat WRITE4_READ4;
40+
public static final RegisterFormat WRITE4_READ4W;
41+
public static final RegisterFormat WRITE4W_READ4;
42+
public static final RegisterFormat WRITE4W_READ4W;
43+
44+
public static final RegisterFormat WRITE8_READ8;
45+
public static final RegisterFormat WRITE8_READ16;
46+
public static final RegisterFormat WRITE8W_READ16W;
47+
public static final RegisterFormat WRITE16_READ16;
48+
public static final RegisterFormat WRITE16W_READ16W;
49+
50+
public static final RegisterFormat WRITE8_READ8_READ8;
51+
public static final RegisterFormat WRITE8_READ8W_READ8W;
52+
public static final RegisterFormat WRITE8W_READ8_READ8;
53+
public static final RegisterFormat WRITE8W_READ8W_READ8;
54+
public static final RegisterFormat WRITE8W_READ8W_READ8W;
55+
2856
public static final RegisterFormat OUT;
2957
public static final RegisterFormat OUT_RANGE;
3058

3159
static {
3260

33-
NONE = new RegisterFormat("NONE", new RegisterType[0]);
34-
READ = new RegisterFormat("READ", new RegisterType[]{RegisterType.READ});
35-
WRITE = new RegisterFormat("WRITE", new RegisterType[]{RegisterType.WRITE});
36-
READ_READ = new RegisterFormat("READ_READ", new RegisterType[]{RegisterType.READ, RegisterType.READ});
37-
READ_WRITE = new RegisterFormat("READ_WRITE", new RegisterType[]{RegisterType.READ, RegisterType.WRITE});
38-
READ_READ_READ = new RegisterFormat("READ_READ_READ", new RegisterType[]{RegisterType.READ, RegisterType.READ, RegisterType.READ});
39-
WRITE_READ = new RegisterFormat("WRITE_READ", new RegisterType[]{RegisterType.WRITE, RegisterType.READ});
40-
WRITE_READ_READ = new RegisterFormat("WRITE_READ_READ", new RegisterType[]{RegisterType.WRITE, RegisterType.READ, RegisterType.READ});
61+
RegisterType[] read = new RegisterType[]{RegisterType.READ};
62+
RegisterType[] readRead = new RegisterType[]{RegisterType.READ, RegisterType.READ};
63+
RegisterType[] readReadRead = new RegisterType[]{RegisterType.READ, RegisterType.READ, RegisterType.READ};
64+
65+
RegisterType[] rwRead = new RegisterType[]{RegisterType.RW, RegisterType.READ};
66+
67+
RegisterType[] write = new RegisterType[]{ RegisterType.WRITE};
68+
RegisterType[] writeRead = new RegisterType[]{RegisterType.WRITE, RegisterType.READ};
69+
RegisterType[] writeReadRead = new RegisterType[]{RegisterType.WRITE, RegisterType.READ, RegisterType.READ};
70+
71+
int[] limits_f = new int[]{0x0f};
72+
int[] limits_ff = new int[]{0xff};
73+
int[] limits_f_f = new int[]{0x0f, 0x0f};
74+
int[] limits_ff_ff = new int[]{0xff, 0xff};
75+
int[] limits_ff_ffff = new int[]{0xff, 0xffff};
76+
int[] limits_ffff_ffff = new int[]{0xffff, 0xffff};
77+
int[] limits_ff_ff_ff = new int[]{0xff, 0xff, 0xff};
78+
79+
boolean[] wide_t = new boolean[] {true};
80+
boolean[] wide_t_f = new boolean[] {true, false};
81+
boolean[] wide_f_t = new boolean[] {false, true};
82+
boolean[] wide_t_t = new boolean[] {true, true};
83+
boolean[] wide_f_t_t = new boolean[] {false, true, true};
84+
boolean[] wide_t_f_f = new boolean[] {true, false, false};
85+
boolean[] wide_t_t_f = new boolean[] {true, true, false};
86+
boolean[] wide_t_t_t = new boolean[] {true, true, true};
87+
88+
NONE = new RegisterFormat("NONE", null, null);
89+
READ8 = new RegisterFormat("READ8", read, limits_ff);
90+
READ8W = new RegisterFormat("READ8W", read, limits_ff, wide_t);
91+
READ16 = new RegisterFormat("READ16", read, new int[]{0xffff});
92+
WRITE4 = new RegisterFormat("WRITE4", write, limits_f);
93+
WRITE8 = new RegisterFormat("WRITE8", write, limits_ff);
94+
WRITE8W = new RegisterFormat("WRITE8W", write, limits_ff, wide_t);
95+
96+
READ4_READ4 = new RegisterFormat("READ4_READ4", readRead, limits_f_f);
97+
READ4W_READ4 = new RegisterFormat("READ4W_READ4", readRead, limits_f_f, wide_t_f);
98+
READ8_READ8_READ8 = new RegisterFormat("READ8_READ8_READ8", readReadRead, limits_ff_ff_ff);
99+
READ8W_READ8_READ8 = new RegisterFormat("READ8W_READ8_READ8",
100+
readReadRead, limits_ff_ff_ff, wide_t_f_f);
101+
102+
RW4_READ4 = new RegisterFormat("RW4_READ4", rwRead, limits_f_f);
103+
RW4W_READ4 = new RegisterFormat("RW4W_READ4", rwRead, limits_f_f, wide_t_f);
104+
RW4W_READ4W = new RegisterFormat("RW4W_READ4W", rwRead, limits_f_f, wide_t_t);
105+
106+
WRITE4_READ4 = new RegisterFormat("WRITE4_READ4", writeRead, limits_f_f);
107+
WRITE4_READ4W = new RegisterFormat("WRITE4_READ4W", writeRead, limits_f_f, wide_f_t);
108+
WRITE4W_READ4 = new RegisterFormat("WRITE4W_READ4", writeRead, limits_f_f, wide_t_f);
109+
WRITE4W_READ4W = new RegisterFormat("WRITE4W_READ4W", writeRead, limits_f_f, wide_t_t);
110+
111+
WRITE8_READ8 = new RegisterFormat("WRITE8_READ8", writeRead, limits_ff_ff);
112+
WRITE8_READ16 = new RegisterFormat("WRITE8_READ16", writeRead, limits_ff_ffff);
113+
WRITE8W_READ16W = new RegisterFormat("WRITE8W_READ16W",
114+
writeRead, limits_ff_ffff, wide_t_t);
115+
WRITE16_READ16 = new RegisterFormat("WRITE16_READ16", writeRead, limits_ffff_ffff);
116+
WRITE16W_READ16W = new RegisterFormat("WRITE16W_READ16W",
117+
writeRead, limits_ffff_ffff, wide_t_t);
41118

42-
OUT = new RegisterFormat("OUT", new RegisterType[0]){
119+
WRITE8_READ8_READ8 = new RegisterFormat("WRITE8_READ8_READ8", writeReadRead, limits_ff_ff_ff);
120+
WRITE8_READ8W_READ8W = new RegisterFormat("WRITE8_READ8W_READ8W",
121+
writeReadRead, limits_ff_ff_ff, wide_f_t_t);
122+
WRITE8W_READ8_READ8 = new RegisterFormat("WRITE8W_READ8_READ8",
123+
writeReadRead, limits_ff_ff_ff, wide_t_f_f);
124+
WRITE8W_READ8W_READ8 = new RegisterFormat("WRITE8W_READ8W_READ8",
125+
writeReadRead, limits_ff_ff_ff, wide_t_t_f);
126+
WRITE8W_READ8W_READ8W = new RegisterFormat("WRITE8W_READ8W_READ8W",
127+
writeReadRead, limits_ff_ff_ff, wide_t_t_t);
128+
129+
OUT = new RegisterFormat("OUT", null, null) {
43130
@Override
44131
public RegisterType get(int i) {
45132
return RegisterType.READ;
@@ -48,8 +135,20 @@ public RegisterType get(int i) {
48135
public boolean isOut() {
49136
return true;
50137
}
138+
@Override
139+
public int limit(int i) {
140+
return 0xf;
141+
}
142+
@Override
143+
public boolean isWide(int i) {
144+
return false;
145+
}
51146
};
52-
OUT_RANGE = new RegisterFormat("OUT_RANGE", new RegisterType[]{RegisterType.READ, RegisterType.READ}){
147+
OUT_RANGE = new RegisterFormat("OUT_RANGE", null, null) {
148+
@Override
149+
public RegisterType get(int i) {
150+
return RegisterType.READ;
151+
}
53152
@Override
54153
public boolean isOut() {
55154
return true;
@@ -58,41 +157,64 @@ public boolean isOut() {
58157
public boolean isRange() {
59158
return true;
60159
}
160+
@Override
161+
public int limit(int i) {
162+
return 0xffff;
163+
}
164+
@Override
165+
public boolean isWide(int i) {
166+
return false;
167+
}
61168
};
62169
}
63170

64171
private final String name;
65172
private final RegisterType[] types;
66-
private final int hash;
173+
private final int[] limits;
174+
private final boolean[] wide;
67175

68-
private RegisterFormat(String name, RegisterType[] types){
176+
RegisterFormat(String name, RegisterType[] types, int[] limits) {
177+
this(name, types, limits, null);
178+
}
179+
RegisterFormat(String name, RegisterType[] types, int[] limits, boolean[] wide) {
69180
this.name = name;
70181
this.types = types;
71-
int h = 1;
72-
if(types != null){
73-
h = h + 31 * types.length;
74-
}
75-
h = h + 31 * name.hashCode();
76-
this.hash = h;
182+
this.limits = limits;
183+
this.wide = wide;
77184
}
78185

79-
public RegisterType get(int i){
80-
if(isOut()){
81-
return RegisterType.READ;
186+
public RegisterType get(int i) {
187+
RegisterType[] types = this.types;
188+
if (types != null && i >= 0 && i < types.length) {
189+
return types[i];
82190
}
83-
return types[i];
191+
return null;
84192
}
85-
public int size(){
193+
public int size() {
86194
RegisterType[] types = this.types;
87-
if(types != null){
195+
if (types != null) {
88196
return types.length;
89197
}
90198
return 0;
91199
}
92-
public boolean isOut(){
200+
public boolean isOut() {
93201
return false;
94202
}
95-
public boolean isRange(){
203+
public boolean isRange() {
204+
return false;
205+
}
206+
public int limit(int i) {
207+
int[] limits = this.limits;
208+
if (limits != null && i >= 0 && i <= limits.length) {
209+
return limits[i];
210+
}
211+
return 0;
212+
}
213+
public boolean isWide(int i) {
214+
boolean[] wide = this.wide;
215+
if (wide != null && i >= 0 && i <= wide.length) {
216+
return wide[i];
217+
}
96218
return false;
97219
}
98220
@Override
@@ -101,7 +223,13 @@ public boolean equals(Object obj) {
101223
}
102224
@Override
103225
public int hashCode() {
104-
return hash;
226+
int h = 31;
227+
RegisterType[] types = this.types;
228+
if (types != null) {
229+
h = h * 31 + types.length;
230+
}
231+
h = h * 31 + name.hashCode();
232+
return h;
105233
}
106234

107235
@Override

src/main/java/com/reandroid/dex/ins/Ins11x.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,6 @@ public int getRegister(int index) {
4545
public void setRegister(int index, int value) {
4646
setByte(1, value);
4747
}
48-
@Override
49-
public int getRegisterLimit(int index){
50-
return 0xff;
51-
}
5248

5349
@Override
5450
public void appendCode(SmaliWriter writer) throws IOException {

src/main/java/com/reandroid/dex/ins/Ins12x.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,7 @@ public int getRegister(int index) {
4444
public void setRegister(int index, int value) {
4545
setNibble(2 + index, value);
4646
}
47-
@Override
48-
public int getRegisterLimit(int index){
49-
return 0x0f;
50-
}
47+
5148
@Override
5249
void appendOperand(SmaliWriter writer) {
5350
}

src/main/java/com/reandroid/dex/ins/Ins21c.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,6 @@ public int getRegister(int index) {
3535
public void setRegister(int index, int value) {
3636
setByte(1, value);
3737
}
38-
@Override
39-
public int getRegisterLimit(int index){
40-
return 0xff;
41-
}
4238

4339
@Override
4440
public int getData(){

src/main/java/com/reandroid/dex/ins/Ins21ih.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,6 @@ public void setRegister(int index, int value) {
3838
setByte(1, value);
3939
}
4040

41-
@Override
42-
public int getRegisterLimit(int index){
43-
return 0xff;
44-
}
45-
4641
@Override
4742
public int getData() {
4843
InsConst insConst = this.mReplaced;

src/main/java/com/reandroid/dex/ins/Ins21lh.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,6 @@ public int getRegister(int index) {
4040
public void setRegister(int index, int value) {
4141
setByte(1, value);
4242
}
43-
@Override
44-
public int getRegisterLimit(int index){
45-
return 0xff;
46-
}
4743

4844
@Override
4945
public int getData() {

src/main/java/com/reandroid/dex/ins/Ins21s.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,6 @@ public int getRegister(int index) {
3535
public void setRegister(int index, int value) {
3636
setByte(1, value);
3737
}
38-
@Override
39-
public int getRegisterLimit(int index){
40-
return 0xff;
41-
}
4238

4339
@Override
4440
public int getData(){

0 commit comments

Comments
 (0)