Skip to content

Commit 17619ee

Browse files
committed
use byte matcher to find Config annotation descriptor instead of ClassNode
1 parent 0a088c2 commit 17619ee

3 files changed

Lines changed: 186 additions & 24 deletions

File tree

src/main/java/com/falsepattern/lib/internal/asm/transformers/ConfigOrderTransformer.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import com.falsepattern.lib.config.Config;
2626
import com.falsepattern.lib.internal.Tags;
2727
import com.falsepattern.lib.internal.impl.config.DeclOrderInternal;
28+
import com.falsepattern.lib.turboasm.BytePatternMatcher;
29+
import com.falsepattern.lib.turboasm.ClassHeaderMetadata;
2830
import com.falsepattern.lib.turboasm.ClassNodeHandle;
2931
import com.falsepattern.lib.turboasm.TurboClassTransformer;
3032
import lombok.val;
@@ -37,6 +39,8 @@ public class ConfigOrderTransformer implements TurboClassTransformer {
3739
private static final String DESC_CONFIG_IGNORE = Type.getDescriptor(Config.Ignore.class);
3840
private static final String DESC_ORDER = Type.getDescriptor(DeclOrderInternal.class);
3941

42+
final BytePatternMatcher configAnnotationMatcher = new BytePatternMatcher(DESC_CONFIG, BytePatternMatcher.Mode.Equals);
43+
4044
@Override
4145
public String name() {
4246
return "ConfigOrderTransformer";
@@ -49,17 +53,12 @@ public String owner() {
4953

5054
@Override
5155
public boolean shouldTransformClass(@NotNull String className, @NotNull ClassNodeHandle classNode) {
52-
val cn = classNode.getNode();
53-
if (cn == null)
56+
final ClassHeaderMetadata metadata = classNode.getOriginalMetadata();
57+
if (metadata == null) {
5458
return false;
55-
if (cn.visibleAnnotations != null) {
56-
for (val ann : cn.visibleAnnotations) {
57-
if (DESC_CONFIG.equals(ann.desc)) {
58-
return true;
59-
}
60-
}
6159
}
62-
return false;
60+
61+
return metadata.matchesBytes(configAnnotationMatcher);
6362
}
6463

6564
@Override
@@ -75,7 +74,9 @@ public boolean transformClass(@NotNull String className, @NotNull ClassNodeHandl
7574
|| (field.access & Opcodes.ACC_STATIC) == 0
7675
|| (field.access & Opcodes.ACC_FINAL) != 0) {
7776
continue;
78-
} else if (field.visibleAnnotations != null) {
77+
}
78+
79+
if (field.visibleAnnotations != null) {
7980
for (val ann : field.visibleAnnotations) {
8081
if (DESC_CONFIG_IGNORE.equals(ann.desc)) {
8182
continue outer;
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
package com.falsepattern.lib.turboasm;
2+
3+
import java.nio.charset.StandardCharsets;
4+
import java.util.Arrays;
5+
6+
public class BytePatternMatcher {
7+
final Mode mode;
8+
// first byte -> matched patterns
9+
final byte[][][] byFirst = new byte[256][][];
10+
int minPatternLen = Integer.MAX_VALUE;
11+
12+
public enum Mode {
13+
/** Checks if the constant pool entry contains a pattern */
14+
Contains,
15+
/** Checks if the whole constant pool entry equals a pattern */
16+
Equals,
17+
/** Checks if the constant pool entry starts with a pattern */
18+
StartsWith
19+
}
20+
21+
public BytePatternMatcher(String strPattern, Mode mode) {
22+
this(new String[] {strPattern}, mode);
23+
}
24+
25+
public BytePatternMatcher(String[] strPatterns, Mode mode) {
26+
this.mode = mode;
27+
28+
final byte[][] patterns = new byte[strPatterns.length][];
29+
final int[] bucketSizes = new int[256];
30+
final int[] bucketIndices = new int[Math.min(strPatterns.length, 256)];
31+
int bucketsCount = 0;
32+
33+
for (int i = 0; i < strPatterns.length; i++) {
34+
final byte[] pattern = strPatterns[i].getBytes(StandardCharsets.UTF_8);
35+
patterns[i] = pattern;
36+
37+
if (pattern.length < minPatternLen) {
38+
minPatternLen = pattern.length;
39+
}
40+
41+
final int bucketIndex = pattern[0] & 0xFF;
42+
if (bucketSizes[bucketIndex]++ == 0) {
43+
bucketIndices[bucketsCount++] = bucketIndex;
44+
}
45+
}
46+
47+
// Ascending sorting by length
48+
Arrays.sort(patterns, (a, b) -> Integer.compare(a.length, b.length));
49+
50+
for (int i = 0; i < bucketsCount; i++) {
51+
final int bucketIndex = bucketIndices[i];
52+
byFirst[bucketIndex] = new byte[bucketSizes[bucketIndex]][];
53+
bucketSizes[bucketIndex] = 0; // reuse as write index
54+
}
55+
56+
for (final byte[] pattern : patterns) {
57+
final int bucketIndex = pattern[0] & 0xFF;
58+
byFirst[bucketIndex][bucketSizes[bucketIndex]++] = pattern;
59+
}
60+
}
61+
62+
public boolean matches(byte[] bytes, int start, int len) {
63+
if (len < minPatternLen) {
64+
return false;
65+
}
66+
67+
return switch (mode) {
68+
case Contains -> contains(bytes, start, len);
69+
case Equals -> equals(bytes, start, len);
70+
case StartsWith -> startsWith(bytes, start, len);
71+
};
72+
}
73+
74+
private boolean contains(byte[] bytes, int start, int len) {
75+
final int end = start + len;
76+
77+
for (int pos = start; pos <= end - minPatternLen; pos++) {
78+
final byte[][] patterns = byFirst[bytes[pos] & 0xFF];
79+
if (patterns == null) {
80+
continue;
81+
}
82+
83+
for (final byte[] pattern : patterns) {
84+
if (pattern.length > end - pos) {
85+
break;
86+
}
87+
88+
int k = pattern.length - 1;
89+
while (k > 0 && bytes[pos + k] == pattern[k]) k--;
90+
if (k == 0) return true;
91+
}
92+
}
93+
94+
return false;
95+
}
96+
97+
private boolean equals(byte[] bytes, int start, int len) {
98+
final byte[][] patterns = byFirst[bytes[start] & 0xFF];
99+
if (patterns == null) {
100+
return false;
101+
}
102+
103+
for (final byte[] pattern : patterns) {
104+
if (pattern.length < len) {
105+
continue;
106+
}
107+
if (pattern.length > len) {
108+
break;
109+
}
110+
111+
int k = pattern.length - 1;
112+
while (k > 0 && bytes[start + k] == pattern[k]) k--;
113+
if (k == 0) return true;
114+
}
115+
116+
return false;
117+
}
118+
119+
private boolean startsWith(byte[] bytes, int start, int len) {
120+
final byte[][] patterns = byFirst[bytes[start] & 0xFF];
121+
if (patterns == null) {
122+
return false;
123+
}
124+
125+
for (final byte[] pattern : patterns) {
126+
if (pattern.length > len) {
127+
break;
128+
}
129+
130+
int k = pattern.length - 1;
131+
while (k > 0 && bytes[start + k] == pattern[k]) k--;
132+
if (k == 0) return true;
133+
}
134+
135+
return false;
136+
}
137+
}

src/main/java/com/falsepattern/lib/turboasm/ClassHeaderMetadata.java

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.io.DataInputStream;
3232
import java.io.IOException;
3333
import java.util.ArrayList;
34+
import java.util.Arrays;
3435
import java.util.Collections;
3536
import java.util.List;
3637

@@ -39,6 +40,7 @@
3940
*/
4041
public final class ClassHeaderMetadata implements FastClassAccessor {
4142

43+
public final byte[] classBytes;
4244
public final int minorVersion;
4345
public final int majorVersion;
4446
public final int constantPoolEntryCount;
@@ -47,6 +49,9 @@ public final class ClassHeaderMetadata implements FastClassAccessor {
4749
/** Type of each parsed constant pool entry, zero-indexed! */
4850
public final ConstantPoolEntryTypes @NotNull [] constantPoolEntryTypes;
4951

52+
/** Approximately only half of the entries are utf-8, so this array can be used to iterate over them more quickly */
53+
public final int @NotNull [] constantPoolUtf8EntryOffsets;
54+
5055
public final int constantPoolEndOffset;
5156
public final int accessFlags;
5257
public final int thisClassIndex;
@@ -66,15 +71,17 @@ public ClassHeaderMetadata(byte @NotNull [] bytes) {
6671
if (!isValidClass(bytes)) {
6772
throw new IllegalArgumentException("Invalid class detected");
6873
}
74+
this.classBytes = bytes;
6975
this.minorVersion = u16(bytes, Offsets.minorVersionU16);
7076
this.majorVersion = u16(bytes, Offsets.majorVersionU16);
7177
this.constantPoolEntryCount = u16(bytes, Offsets.constantPoolCountU16);
7278
this.constantPoolEntryOffsets = new int[constantPoolEntryCount];
7379
this.constantPoolEntryTypes = new ConstantPoolEntryTypes[constantPoolEntryCount];
7480
// scan through CP entries
75-
final int cpOff;
7681
{
7782
int off = Offsets.constantPoolStart;
83+
final int[] utf8EntryOffsets = new int[constantPoolEntryCount];
84+
int utf8Entries = 0;
7885
for (int entry = 0; entry < constantPoolEntryCount - 1; entry++) {
7986
constantPoolEntryOffsets[entry] = off;
8087
ConstantPoolEntryTypes type = ConstantPoolEntryTypes.parse(bytes, off);
@@ -84,18 +91,18 @@ public ClassHeaderMetadata(byte @NotNull [] bytes) {
8491
entry++;
8592
constantPoolEntryOffsets[entry] = off;
8693
constantPoolEntryTypes[entry] = type;
94+
} else if (type == ConstantPoolEntryTypes.Utf8) {
95+
utf8EntryOffsets[utf8Entries++] = off;
8796
}
8897
off += type.byteLength(bytes, off);
8998
}
90-
cpOff = off;
91-
this.constantPoolEndOffset = cpOff;
99+
this.constantPoolEndOffset = off;
100+
this.constantPoolUtf8EntryOffsets = Arrays.copyOf(utf8EntryOffsets, utf8Entries);
92101
}
93-
this.accessFlags = u16(bytes, cpOff + Offsets.pastCpAccessFlagsU16);
94-
this.thisClassIndex = u16(bytes, cpOff + Offsets.pastCpThisClassU16);
95-
this.superClassIndex = u16(bytes, cpOff + Offsets.pastCpSuperClassU16);
96-
this.interfacesCount = u16(bytes, cpOff + Offsets.pastCpInterfacesCountU16);
97-
this.interfaceIndices = new int[this.interfacesCount];
98-
List<String> interfaceNames = new ArrayList<>(this.interfacesCount);
102+
this.accessFlags = u16(bytes, this.constantPoolEndOffset + Offsets.pastCpAccessFlagsU16);
103+
this.thisClassIndex = u16(bytes, this.constantPoolEndOffset + Offsets.pastCpThisClassU16);
104+
this.superClassIndex = u16(bytes, this.constantPoolEndOffset + Offsets.pastCpSuperClassU16);
105+
this.interfacesCount = u16(bytes, this.constantPoolEndOffset + Offsets.pastCpInterfacesCountU16);
99106

100107
// Parse this&super names
101108
if (constantPoolEntryTypes[thisClassIndex - 1] != ConstantPoolEntryTypes.Class) {
@@ -121,8 +128,10 @@ public ClassHeaderMetadata(byte @NotNull [] bytes) {
121128
}
122129

123130
// Parse interface names
131+
List<String> interfaceNames = new ArrayList<>(this.interfacesCount);
132+
this.interfaceIndices = new int[this.interfacesCount];
124133
for (int i = 0; i < this.interfacesCount; i++) {
125-
final int interfaceOffset = cpOff + Offsets.pastCpInterfacesList + i * 2;
134+
final int interfaceOffset = this.constantPoolEndOffset + Offsets.pastCpInterfacesList + i * 2;
126135
final int interfaceIndex = u16(bytes, interfaceOffset);
127136
if (constantPoolEntryTypes[interfaceIndex - 1] != ConstantPoolEntryTypes.Class) {
128137
throw new IllegalArgumentException("Interface " + i + " index is not a class ref");
@@ -314,11 +323,26 @@ public static int majorVersion(byte @NotNull [] classBytes) {
314323
}
315324

316325
/**
317-
* Searches for a sub"string" (byte array) in a longer byte array. Not efficient for long search strings.
318-
* @param classBytes The long byte string to search in.
319-
* @param substring The short substring to search for.
320-
* @return If the substring was found somewhere in the long string.
326+
* Searches for byte patterns in the constant pool.
327+
* @param matcher A configured byte matcher with patterns to search for.
328+
* @return {@code true} if there is a match for at least one constant pool entry.
321329
*/
330+
public boolean matchesBytes(final BytePatternMatcher matcher) {
331+
for (final int offset : constantPoolUtf8EntryOffsets) {
332+
// first byte is entry type, second and third bytes are length
333+
final int length = u16(classBytes, offset + 1);
334+
final int start = offset + 3;
335+
336+
if (matcher.matches(classBytes, start, length)) {
337+
return true;
338+
}
339+
}
340+
341+
return false;
342+
}
343+
344+
/** @deprecated This method is very slow, use {@link #matchesBytes} instead */
345+
@Deprecated
322346
public static boolean hasSubstring(final byte @Nullable [] classBytes, final byte @NotNull [] substring) {
323347
if (classBytes == null) {
324348
return false;

0 commit comments

Comments
 (0)