aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/ftbsc/lll/tools/PatternMatcher.java
blob: fff84dbb038d17f484865f813ffea714a7b0a259 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package ftbsc.lll.tools;

import ftbsc.lll.exceptions.PatternNotFoundException;
import org.objectweb.asm.tree.AbstractInsnNode;
import org.objectweb.asm.tree.MethodNode;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;

/**
 * Describes a pattern to match on a list of ASM instructions.
 */
public class PatternMatcher {
   /**
    * The list of predicates to match.
    */
   private final List<Predicate<AbstractInsnNode>> predicates;

   /**
    * Whether pattern search should be done from the end.
    */
   private final boolean reverse;

   /**
    * Patterns flagged with this ignore labels.
    */
   private final boolean ignoreLabels;

   /**
    * Patterns flagged with this ignore FRAME instructions.
    */
   private final boolean ignoreFrames;

   /**
    * Patterns flagged with this ignore LINENUMBER instructions.
    */
   private final boolean ignoreLineNumbers;

   /**
    * Private constructor because a PatternMatcher should only ever be initialized
    * through the builder.
    * @param predicates the list of predicates to match
    * @param reverse search direction
    * @param ignoreLabels whether LABEL instructions should be ignored
    * @param ignoreFrames whether FRAME instructions should be ignored
    * @param ignoreLineNumbers whether LINENUMBER instructions should be ignored
    */
   private PatternMatcher(List<Predicate<AbstractInsnNode>> predicates, boolean reverse,
                          boolean ignoreLabels, boolean ignoreFrames, boolean ignoreLineNumbers) {
      this.predicates = predicates;
      this.reverse = reverse;
      this.ignoreLabels = ignoreLabels;
      this.ignoreFrames = ignoreFrames;
      this.ignoreLineNumbers = ignoreLineNumbers;
   }

   /**
    * @return the Builder object for this {@link PatternMatcher}
    */
   public static Builder builder() {
      return new Builder();
   }

   /**
    * Tries to match the given pattern on a given {@link MethodNode}.
    * @param node the {@link MethodNode} to search
    * @return the InsnSequence object representing the matched pattern
    */
   public InsnSequence find(MethodNode node) {
      return find(reverse ? node.instructions.getLast() : node.instructions.getFirst());
   }

   /**
    * Tries to match the given pattern starting from a given node.
    * @param node the node to start the search on
    * @return the {@link InsnSequence} object representing the matched pattern
    */
   public InsnSequence find(AbstractInsnNode node) {
      if(node != null) {
         AbstractInsnNode first, last;
         for(AbstractInsnNode cur = node; cur != null; cur = reverse ? cur.getPrevious() : cur.getNext()) {
            if(predicates.size() == 0) return new InsnSequence(cur); //match whatever
            first = cur;
            last = cur;
            for(int match = 0; last != null && match < predicates.size(); last = reverse ? last.getPrevious() : last.getNext()) {
               if(match != 0) {
                  if(ignoreLabels && last.getType() == AbstractInsnNode.LABEL) continue;
                  if(ignoreFrames && last.getType() == AbstractInsnNode.FRAME) continue;
                  if(ignoreLineNumbers && last.getType() == AbstractInsnNode.LINE) continue;
               }
               if(!predicates.get(match).test(last)) break;
               if(match == predicates.size() - 1) {
                  if(reverse) return new InsnSequence(last, first); //we are matching backwards
                  else return new InsnSequence(first, last);
               } else match++;
            }
         }
      }
      throw new PatternNotFoundException("Failed to find pattern!");
   }

   /**
    * The Builder object for {@link PatternMatcher}.
    */
   public static class Builder {

      /**
       * List of predicates the pattern has to match.
       */
      private final List<Predicate<AbstractInsnNode>> predicates = new ArrayList<>();

      /**
       * Whether the pattern matching should proceed in reversed order.
       */
      private boolean reverse = false;

      /**
       * Patterns flagged with this ignore labels.
       */
      private boolean ignoreLabels = false;

      /**
       * Patterns flagged with this ignore FRAME instructions.
       */
      private boolean ignoreFrames = false;

      /**
       * Patterns flagged with this ignore LINENUMBER instructions.
       */
      private boolean ignoreLineNumbers = false;

      /**
       * Builds the pattern defined so far.
       * @return the built {@link PatternMatcher}
       */
      public PatternMatcher build() {
         return new PatternMatcher(predicates, reverse, ignoreLabels, ignoreFrames, ignoreLineNumbers);
      }

      /**
       * Sets the pattern to match starting from the end.
       * @return the builder's state after the operation
       */
      public Builder reverse() {
         this.reverse = true;
         return this;
      }

      /**
       * Adds a custom predicate to the list. Also used internally.
       * @param predicate the predicate to add
       * @return the builder's state after the operation
       */
      public Builder check(Predicate<AbstractInsnNode> predicate) {
         predicates.add(predicate);
         return this;
      }

      /**
       * Wildcard, matches any kind of node.
       * @return the builder's state after the operation
       */
      public Builder any() {
         return check(i -> true);
      }

      /**
       * Matches a specific opcode.
       * @param opcode opcode to match
       * @return the builder's state after the operation
       */
      public Builder opcode(int opcode) {
         return check(i -> i.getOpcode() == opcode);
      }

      /**
       * Matches a list of opcodes.
       * @param opcodes list of opcodes to match
       * @return the builder's state after the operation
       */
      public Builder opcodes(int... opcodes) {
         Builder res = this;
         for(int o : opcodes)
            res = opcode(o);
         return res;
      }

      /**
       * Matches a method invokation of any kind: one of INVOKEVIRTUAL,
       * INVOKESPECIAL, INVOKESTATIC or INVOKEINTERFACE.
       * @return the builder's state after the operation
       */
      public Builder method() {
         return check(i -> i.getType() == AbstractInsnNode.METHOD_INSN);
      }

      /**
       * Matches a field invokation of any kind: one of GETSTATIC, PUTSTATIC,
       * GETFIELD or PUTFIELD.
       * @return the builder's state after the operation
       */
      public Builder field() {
         return check(i -> i.getType() == AbstractInsnNode.FIELD_INSN);
      }

      /**
       * Matches any kind of jump instruction.
       * @return the builder's state after the operation
       */
      public Builder jump() {
         return check(i -> i.getType() == AbstractInsnNode.JUMP_INSN);
      }

      /**
       * Matches any kind of label.
       * @return the builder's state after the operation
       */
      public Builder label() {
         return check(i -> i.getType() == AbstractInsnNode.LABEL);
      }

      /**
       * Tells the pattern matcher to ignore LABEL instructions.
       * @return the builder's state after the operation
       */
      public Builder ignoreLabels() {
         this.ignoreLabels = true;
         return this;
      }

      /**
       * Tells the pattern matcher to ignore FRAME instructions.
       * @return the builder's state after the operation
       */
      public Builder ignoreFrames() {
         this.ignoreFrames = true;
         return this;
      }

      /**
       * Tells the pattern matcher to ignore LINENUMBER instructions.
       * @return the builder's state after the operation
       */
      public Builder ignoreLineNumbers() {
         this.ignoreLineNumbers = true;
         return this;
      }
   }
}