aboutsummaryrefslogtreecommitdiff
path: root/agent/src/main/java/io/perfmark/agent/MethodVisitorRecorder.java
blob: 960d0f7aac365078c156c60f9b5d855122fb4328 (plain)
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/*
 * Copyright 2021 Carl Mastrangelo
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.perfmark.agent;

import org.objectweb.asm.AnnotationVisitor;
import org.objectweb.asm.Attribute;
import org.objectweb.asm.Label;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.TypePath;

/**
 * This class records the "header" portion of the method visitor.
 */
class MethodVisitorRecorder extends MethodVisitor {

  private final int VISIT_PARAMETER = 1;
  private final int VISIT_ANNOTATION_DEFAULT = 2;

  private final int ANNOTATION_VISIT = 3;
  private final int ANNOTATION_VISIT_ENUM = 4;
  private final int ANNOTATION_VISIT_ANNOTATION = 5;
  private final int ANNOTATION_VISIT_ARRAY = 6;
  private final int ANNOTATION_VISIT_END = 7;

  private final int VISIT_ANNOTATION = 8;
  private final int VISIT_ANNOTABLE_PARAMETER_COUNT = 9;
  private final int VISIT_PARAMETER_ANNOTATION = 10;
  private final int VISIT_TYPE_ANNOTATION = 11;
  private final int VISIT_ATTRIBUTE = 12;

  private final AnnotationVisitorRecorder annotationVisitorRecorder = new AnnotationVisitorRecorder(null);

  private int opsWidx;
  private int intsWidx;
  private int stringsWidx;
  private int objectsWidx;
  private int booleansWidx;

  private int[] ops = new int[0];
  private String[] strings = new String[0];
  private int[] ints = new int[0];
  private Object[] objects = new Object[0];
  private boolean[] booleans = new boolean[0];

  int firstLine = -1;
  int lastLine = -1;

  MethodVisitorRecorder(MethodVisitor delegate) {
    // Have to pin to a specific version, since the method invocations may be different in a later release
    super(Opcodes.ASM9, delegate);
  }

  /*
   * Docs for Method Visitor Say:
   *
   * A visitor to visit a Java method. The methods of this class must be called in the following order:
   * ( visitParameter )*
   * [ visitAnnotationDefault ]
   * ( visitAnnotation | visitAnnotableParameterCount | visitParameterAnnotation visitTypeAnnotation | visitAttribute )*
   * [ visitCode (
   *     visitFrame | visit<i>X</i>Insn | visitLabel | visitInsnAnnotation | visitTryCatchBlock
   *         | visitTryCatchAnnotation | visitLocalVariable | visitLocalVariableAnnotation | visitLineNumber )*
   *     visitMaxs ]
   * visitEnd.
   *
   *
   * In addition, the visit<i>X</i>Insn and visitLabel methods must be called in the sequential order of the bytecode
   * instructions of the visited code, visitInsnAnnotation must be called after the annotated instruction,
   *  visitTryCatchBlock must be called before the labels passed as arguments have been visited,
   * visitTryCatchBlockAnnotation must be called after the corresponding try catch block has been visited, and the
   * visitLocalVariable, visitLocalVariableAnnotation and visitLineNumber methods must be called after the labels passed
   * as arguments have been visited.
   */

  @Override
  public final void visitParameter(String name, int access) {
    addOp(VISIT_PARAMETER);
    addString(name);
    addInt(access);
    super.visitParameter(name, access);
  }

  @Override
  public final AnnotationVisitor visitAnnotationDefault() {
    addOp(VISIT_ANNOTATION_DEFAULT);
    if (mv == null) {
      return annotationVisitorRecorder;
    }
    return new AnnotationVisitorRecorder(super.visitAnnotationDefault());
  }

  @Override
  public final AnnotationVisitor visitAnnotation(String descriptor, boolean visible) {
    addOp(VISIT_ANNOTATION);
    addString(descriptor);
    addBoolean(visible);
    if (mv == null) {
      return annotationVisitorRecorder;
    }
    return new AnnotationVisitorRecorder(super.visitAnnotation(descriptor, visible));
  }

  @Override
  public final void visitAnnotableParameterCount(int parameterCount, boolean visible) {
    addOp(VISIT_ANNOTABLE_PARAMETER_COUNT);
    addInt(parameterCount);
    addBoolean(visible);
    super.visitAnnotableParameterCount(parameterCount, visible);
  }

  @Override
  public final AnnotationVisitor visitParameterAnnotation(int parameter, String descriptor, boolean visible) {
    addOp(VISIT_PARAMETER_ANNOTATION);
    addInt(parameter);
    addString(descriptor);
    addBoolean(visible);
    if (mv == null) {
      return annotationVisitorRecorder;
    }
    return new AnnotationVisitorRecorder(super.visitParameterAnnotation(parameter, descriptor, visible));
  }

  @Override
  public final AnnotationVisitor visitTypeAnnotation(
      int typeRef, TypePath typePath, String descriptor, boolean visible) {
    addOp(VISIT_TYPE_ANNOTATION);
    addInt(typeRef);
    addObject(typePath);
    addString(descriptor);
    addBoolean(visible);
    if (mv == null) {
      return annotationVisitorRecorder;
    }
    return new AnnotationVisitorRecorder(super.visitTypeAnnotation(typeRef, typePath, descriptor, visible));
  }

  @Override
  public final void visitAttribute(Attribute attribute) {
    addOp(VISIT_ATTRIBUTE);
    addObject(attribute);
    super.visitAttribute(attribute);
  }

  @Override
  public final void visitLineNumber(int line, Label start) {
    if (firstLine == -1) {
      firstLine = line;
    }
    lastLine = line;
    super.visitLineNumber(line, start);
  }

  final void replay() {
    if (mv != null) {
      replay(mv);
    }
  }

  final void replay(MethodVisitor delegate) {
    if (delegate == null) {
      return;
    }
    int stringsRidx = 0;
    int intsRidx = 0;
    int objectsRidx = 0;
    int booleansRidx = 0;
    int annoWidx = 0;
    AnnotationVisitor[] visitorStack = new AnnotationVisitor[0];
    for (int opsRidx = 0; opsRidx < opsWidx; opsRidx++) {
      int op = ops[opsRidx];
      switch (op) {
        case VISIT_PARAMETER: {
          String name = getString(stringsRidx++);
          int access = getInt(intsRidx++);
          delegate.visitParameter(name, access);
          break;
        }
        case VISIT_ANNOTATION_DEFAULT: {
          AnnotationVisitor visitor = delegate.visitAnnotationDefault();
          visitorStack = addAnnotationVisitor(visitorStack, annoWidx++, visitor);
          break;
        }
        case ANNOTATION_VISIT: {
          AnnotationVisitor currentVisitor = visitorStack[annoWidx - 1];
          String name = getString(stringsRidx++);
          Object value = getObject(objectsRidx++);
          if (currentVisitor != null) {
            currentVisitor.visit(name, value);
          }
          break;
        }
        case ANNOTATION_VISIT_ENUM: {
          AnnotationVisitor currentVisitor = visitorStack[annoWidx - 1];
          String name = getString(stringsRidx++);
          String descriptor = getString(stringsRidx++);
          String value = getString(stringsRidx++);
          if (currentVisitor != null) {
            currentVisitor.visitEnum(name, descriptor, value);
          }
          break;
        }
        case ANNOTATION_VISIT_ANNOTATION: {
          AnnotationVisitor currentVisitor = visitorStack[annoWidx - 1];
          String name = getString(stringsRidx++);
          String descriptor = getString(stringsRidx++);
          AnnotationVisitor newVisitor = null;
          if (currentVisitor != null) {
            newVisitor = currentVisitor.visitAnnotation(name, descriptor);
          }
          visitorStack = addAnnotationVisitor(visitorStack, annoWidx++, newVisitor);
          break;
        }
        case ANNOTATION_VISIT_ARRAY: {
          AnnotationVisitor currentVisitor = visitorStack[annoWidx - 1];
          String name = getString(stringsRidx++);
          AnnotationVisitor newVisitor = null;
          if (currentVisitor != null) {
            newVisitor = currentVisitor.visitArray(name);
          }
          visitorStack = addAnnotationVisitor(visitorStack, annoWidx++, newVisitor);
          break;
        }
        case ANNOTATION_VISIT_END: {
          AnnotationVisitor currentVisitor = visitorStack[annoWidx - 1];
          visitorStack[--annoWidx] = null;
          if (currentVisitor != null) {
            currentVisitor.visitEnd();
          }
          break;
        }
        case VISIT_ANNOTATION: {
          String descriptor = getString(stringsRidx++);
          boolean visible = getBoolean(booleansRidx++);
          AnnotationVisitor newVisitor = delegate.visitAnnotation(descriptor, visible);
          visitorStack = addAnnotationVisitor(visitorStack, annoWidx++, newVisitor);
          break;
        }
        case VISIT_ANNOTABLE_PARAMETER_COUNT: {
          int parameterCount = getInt(intsRidx++);
          boolean visible = getBoolean(booleansRidx++);
          delegate.visitAnnotableParameterCount(parameterCount, visible);
          break;
        }
        case VISIT_PARAMETER_ANNOTATION: {
          int parameter = getInt(intsRidx++);
          String descriptor = getString(stringsRidx++);
          boolean visible = getBoolean(booleansRidx++);
          AnnotationVisitor newVisitor = delegate.visitParameterAnnotation(parameter, descriptor, visible);
          visitorStack = addAnnotationVisitor(visitorStack, annoWidx++, newVisitor);
          break;
        }
        case VISIT_TYPE_ANNOTATION: {
          // (int typeRef, TypePath typePath, String descriptor, boolean visible)
          int typeRef = getInt(intsRidx++);
          TypePath typePath = (TypePath) getObject(objectsRidx++);
          String descriptor = getString(stringsRidx++);
          boolean visible = getBoolean(booleansRidx++);
          AnnotationVisitor newVisitor = delegate.visitTypeAnnotation(typeRef, typePath, descriptor, visible);
          visitorStack = addAnnotationVisitor(visitorStack, annoWidx++, newVisitor);
          break;
        }
        case VISIT_ATTRIBUTE: {
          Attribute attribute = (Attribute) getObject(objectsRidx++);
          delegate.visitAttribute(attribute);
          break;
        }
        default:
          throw new AssertionError("Bad op " + op);
      }
    }
  }

  private void addOp(int op) {
    ops = addInt(ops, opsWidx++, op);
  }

  private void addInt(int value) {
    ints = addInt(ints, intsWidx++, value);
  }

  private static int[] addInt(int[] dest, int pos, int value) {
    if (dest.length == pos){
      int[] newDest = new int[1 + dest.length * 2];
      System.arraycopy(dest, 0, newDest, 0, dest.length);
      dest = newDest;
    }
    dest[pos] = value;
    return dest;
  }

  private void addString(String value) {
    strings = addString(strings, stringsWidx++, value);
  }

  private static String[] addString(String[] dest, int pos, String value) {
    if (dest.length == pos){
      String[] newDest = new String[1 + dest.length * 2];
      System.arraycopy(dest, 0, newDest, 0, dest.length);
      dest = newDest;
    }
    dest[pos] = value;
    return dest;
  }

  private void addObject(Object value) {
    objects = addObject(objects, objectsWidx++, value);
  }

  private static Object[] addObject(Object[] dest, int pos, Object value) {
    if (dest.length == pos){
      Object[] newDest = new Object[1 + dest.length * 2];
      System.arraycopy(dest, 0, newDest, 0, dest.length);
      dest = newDest;
    }
    dest[pos] = value;
    return dest;
  }

  private void addBoolean(boolean value) {
    booleans = addBoolean(booleans, booleansWidx++, value);
  }

  private static boolean[] addBoolean(boolean[] dest, int pos, boolean value) {
    if (dest.length == pos){
      boolean[] newDest = new boolean[1 + dest.length * 2];
      System.arraycopy(dest, 0, newDest, 0, dest.length);
      dest = newDest;
    }
    dest[pos] = value;
    return dest;
  }

  private static AnnotationVisitor[] addAnnotationVisitor(AnnotationVisitor[] dest, int pos, AnnotationVisitor value) {
    if (dest.length == pos){
      AnnotationVisitor[] newDest = new AnnotationVisitor[1 + dest.length * 2];
      System.arraycopy(dest, 0, newDest, 0, dest.length);
      dest = newDest;
    }
    dest[pos] = value;
    return dest;
  }

  private int getInt(int ridx) {
    assert ridx < intsWidx;
    return ints[ridx];
  }

  private String getString(int ridx) {
    assert ridx < stringsWidx;
    return strings[ridx];
  }

  private Object getObject(int ridx) {
    assert ridx < objectsWidx;
    return objects[ridx];
  }

  private boolean getBoolean(int ridx) {
    assert ridx < booleansWidx;
    return booleans[ridx];
  }

  private final class AnnotationVisitorRecorder extends AnnotationVisitor {

    AnnotationVisitorRecorder(AnnotationVisitor delegate) {
      super(MethodVisitorRecorder.this.api, delegate);
    }

    /*
     * Docs for AnnotationVisitor say
     *
     * A visitor to visit a Java annotation. The methods of this class must be called in the following order:
     * ( visit | visitEnum | visitAnnotation | visitArray )* visitEnd.
     */

    @Override
    public void visit(String name, Object value) {
      addOp(ANNOTATION_VISIT);
      addString(name);
      addObject(value);
      super.visit(name, value);
    }

    @Override
    public void visitEnum(String name, String descriptor, String value) {
      addOp(ANNOTATION_VISIT_ENUM);
      addString(name);
      addString(descriptor);
      addString(value);
      super.visitEnum(name, descriptor, value);
    }

    @Override
    public AnnotationVisitor visitAnnotation(String name, String descriptor) {
      addOp(ANNOTATION_VISIT_ANNOTATION);
      addString(name);
      addString(descriptor);
      if (av == null) {
        return this;
      }
      return new AnnotationVisitorRecorder(super.visitAnnotation(name, descriptor));
    }

    @Override
    public AnnotationVisitor visitArray(String name) {
      addOp(ANNOTATION_VISIT_ARRAY);
      addString(name);
      if (av == null) {
        return this;
      }
      return new AnnotationVisitorRecorder(super.visitArray(name));
    }

    @Override
    public void visitEnd() {
      addOp(ANNOTATION_VISIT_END);
      super.visitEnd();
    }
  }
}