aboutsummaryrefslogtreecommitdiff
path: root/org.jacoco.core/src/org/jacoco/core/internal/instr/FrameTracker.java
blob: ced09d26c62fab0cb14cba26228fd59ac6503ac9 (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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
/*******************************************************************************
 * Copyright (c) 2009, 2013 Mountainminds GmbH & Co. KG and Contributors
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Marc R. Hoffmann - initial API and implementation
 *    
 *******************************************************************************/
package org.jacoco.core.internal.instr;

import org.jacoco.core.JaCoCo;
import org.objectweb.asm.Handle;
import org.objectweb.asm.Label;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;

/**
 * This method adapter tracks the state of the local variable and stack types.
 * With insertFrame() additional frames can then be added. The adapter is only
 * intended to be used with class file versions >= {@link Opcodes#V1_6}.
 */
class FrameTracker extends MethodVisitor implements IFrameInserter {

	private final String owner;

	private Object[] local;
	private int localSize;
	private Object[] stack;
	private int stackSize;

	public FrameTracker(final String owner, final int access,
			final String name, final String desc, final MethodVisitor mv) {
		super(JaCoCo.ASM_API_VERSION, mv);
		this.owner = owner;
		local = new Object[8];
		localSize = 0;
		stack = new Object[8];
		stackSize = 0;

		if ((access & Opcodes.ACC_STATIC) == 0) {
			if ("<init>".equals(name)) {
				set(localSize, Opcodes.UNINITIALIZED_THIS);
			} else {
				set(localSize, owner);
			}
		}
		for (final Type t : Type.getArgumentTypes(desc)) {
			set(localSize, t);
		}

	}

	public void insertFrame() {
		// Reduced types do not need more space than expanded types:
		final Object[] local = new Object[this.localSize];
		final Object[] stack = new Object[this.stackSize];
		final int localSize = reduce(this.local, this.localSize, local);
		final int stackSize = reduce(this.stack, this.stackSize, stack);
		mv.visitFrame(Opcodes.F_NEW, localSize, local, stackSize, stack);
	}

	@Override
	public void visitFrame(final int type, final int nLocal,
			final Object[] local, final int nStack, final Object[] stack) {

		if (type != Opcodes.F_NEW) {
			throw new IllegalArgumentException(
					"ClassReader.accept() should be called with EXPAND_FRAMES flag");
		}

		// expanded types need at most twice the size
		this.local = ensureSize(this.local, nLocal * 2);
		this.stack = ensureSize(this.stack, nStack * 2);
		this.localSize = expand(local, nLocal, this.local);
		this.stackSize = expand(stack, nStack, this.stack);

		mv.visitFrame(type, nLocal, local, nStack, stack);
	}

	@Override
	public void visitInsn(final int opcode) {
		final Object t1, t2, t3, t4;
		switch (opcode) {
		case Opcodes.NOP:
		case Opcodes.RETURN:
			break;
		case Opcodes.ARETURN:
		case Opcodes.ATHROW:
		case Opcodes.FRETURN:
		case Opcodes.IRETURN:
		case Opcodes.MONITORENTER:
		case Opcodes.MONITOREXIT:
		case Opcodes.POP:
			pop(1);
			break;
		case Opcodes.DRETURN:
		case Opcodes.LRETURN:
		case Opcodes.POP2:
			pop(2);
			break;
		case Opcodes.AASTORE:
		case Opcodes.BASTORE:
		case Opcodes.CASTORE:
		case Opcodes.FASTORE:
		case Opcodes.IASTORE:
		case Opcodes.SASTORE:
			pop(3);
			break;
		case Opcodes.LASTORE:
		case Opcodes.DASTORE:
			pop(4);
			break;
		case Opcodes.ICONST_M1:
		case Opcodes.ICONST_0:
		case Opcodes.ICONST_1:
		case Opcodes.ICONST_2:
		case Opcodes.ICONST_3:
		case Opcodes.ICONST_4:
		case Opcodes.ICONST_5:
			push(Opcodes.INTEGER);
			break;
		case Opcodes.ARRAYLENGTH:
		case Opcodes.F2I:
		case Opcodes.I2B:
		case Opcodes.I2C:
		case Opcodes.I2S:
		case Opcodes.INEG:
			pop(1);
			push(Opcodes.INTEGER);
			break;
		case Opcodes.BALOAD:
		case Opcodes.CALOAD:
		case Opcodes.D2I:
		case Opcodes.FCMPG:
		case Opcodes.FCMPL:
		case Opcodes.IADD:
		case Opcodes.IALOAD:
		case Opcodes.IAND:
		case Opcodes.IDIV:
		case Opcodes.IMUL:
		case Opcodes.IOR:
		case Opcodes.IREM:
		case Opcodes.ISHL:
		case Opcodes.ISHR:
		case Opcodes.ISUB:
		case Opcodes.IUSHR:
		case Opcodes.IXOR:
		case Opcodes.L2I:
		case Opcodes.SALOAD:
			pop(2);
			push(Opcodes.INTEGER);
			break;
		case Opcodes.DCMPG:
		case Opcodes.DCMPL:
		case Opcodes.LCMP:
			pop(4);
			push(Opcodes.INTEGER);
			break;
		case Opcodes.FCONST_0:
		case Opcodes.FCONST_1:
		case Opcodes.FCONST_2:
			push(Opcodes.FLOAT);
			break;
		case Opcodes.FNEG:
		case Opcodes.I2F:
			pop(1);
			push(Opcodes.FLOAT);
			break;
		case Opcodes.D2F:
		case Opcodes.FADD:
		case Opcodes.FALOAD:
		case Opcodes.FDIV:
		case Opcodes.FMUL:
		case Opcodes.FREM:
		case Opcodes.FSUB:
		case Opcodes.L2F:
			pop(2);
			push(Opcodes.FLOAT);
			break;
		case Opcodes.LCONST_0:
		case Opcodes.LCONST_1:
			push(Opcodes.LONG);
			push(Opcodes.TOP);
			break;
		case Opcodes.F2L:
		case Opcodes.I2L:
			pop(1);
			push(Opcodes.LONG);
			push(Opcodes.TOP);
			break;
		case Opcodes.D2L:
		case Opcodes.LALOAD:
		case Opcodes.LNEG:
			pop(2);
			push(Opcodes.LONG);
			push(Opcodes.TOP);
			break;
		case Opcodes.LSHL:
		case Opcodes.LSHR:
		case Opcodes.LUSHR:
			pop(3);
			push(Opcodes.LONG);
			push(Opcodes.TOP);
			break;
		case Opcodes.LADD:
		case Opcodes.LAND:
		case Opcodes.LDIV:
		case Opcodes.LMUL:
		case Opcodes.LOR:
		case Opcodes.LREM:
		case Opcodes.LSUB:
		case Opcodes.LXOR:
			pop(4);
			push(Opcodes.LONG);
			push(Opcodes.TOP);
			break;
		case Opcodes.DCONST_0:
		case Opcodes.DCONST_1:
			push(Opcodes.DOUBLE);
			push(Opcodes.TOP);
			break;
		case Opcodes.F2D:
		case Opcodes.I2D:
			pop(1);
			push(Opcodes.DOUBLE);
			push(Opcodes.TOP);
			break;
		case Opcodes.DALOAD:
		case Opcodes.DNEG:
		case Opcodes.L2D:
			pop(2);
			push(Opcodes.DOUBLE);
			push(Opcodes.TOP);
			break;
		case Opcodes.DADD:
		case Opcodes.DDIV:
		case Opcodes.DMUL:
		case Opcodes.DREM:
		case Opcodes.DSUB:
			pop(4);
			push(Opcodes.DOUBLE);
			push(Opcodes.TOP);
			break;
		case Opcodes.ACONST_NULL:
			push(Opcodes.NULL);
			break;
		case Opcodes.AALOAD:
			pop(1);
			t1 = pop();
			push(Type.getType(((String) t1).substring(1)));
			break;
		case Opcodes.DUP:
			t1 = pop();
			push(t1);
			push(t1);
			break;
		case Opcodes.DUP_X1:
			t1 = pop();
			t2 = pop();
			push(t1);
			push(t2);
			push(t1);
			break;
		case Opcodes.DUP_X2:
			t1 = pop();
			t2 = pop();
			t3 = pop();
			push(t1);
			push(t3);
			push(t2);
			push(t1);
			break;
		case Opcodes.DUP2:
			t1 = pop();
			t2 = pop();
			push(t2);
			push(t1);
			push(t2);
			push(t1);
			break;
		case Opcodes.DUP2_X1:
			t1 = pop();
			t2 = pop();
			t3 = pop();
			push(t2);
			push(t1);
			push(t3);
			push(t2);
			push(t1);
			break;
		case Opcodes.DUP2_X2:
			t1 = pop();
			t2 = pop();
			t3 = pop();
			t4 = pop();
			push(t2);
			push(t1);
			push(t4);
			push(t3);
			push(t2);
			push(t1);
			break;
		case Opcodes.SWAP:
			t1 = pop();
			t2 = pop();
			push(t1);
			push(t2);
			break;
		default:
			throw new IllegalArgumentException();
		}
		mv.visitInsn(opcode);
	}

	@Override
	public void visitIntInsn(final int opcode, final int operand) {
		switch (opcode) {
		case Opcodes.BIPUSH:
		case Opcodes.SIPUSH:
			push(Opcodes.INTEGER);
			break;
		case Opcodes.NEWARRAY:
			pop(1);
			switch (operand) {
			case Opcodes.T_BOOLEAN:
				push("[Z");
				break;
			case Opcodes.T_CHAR:
				push("[C");
				break;
			case Opcodes.T_FLOAT:
				push("[F");
				break;
			case Opcodes.T_DOUBLE:
				push("[D");
				break;
			case Opcodes.T_BYTE:
				push("[B");
				break;
			case Opcodes.T_SHORT:
				push("[S");
				break;
			case Opcodes.T_INT:
				push("[I");
				break;
			case Opcodes.T_LONG:
				push("[J");
				break;
			default:
				throw new IllegalArgumentException();
			}
			break;
		default:
			throw new IllegalArgumentException();
		}
		mv.visitIntInsn(opcode, operand);
	}

	@Override
	public void visitVarInsn(final int opcode, final int var) {
		final Object t;
		switch (opcode) {
		case Opcodes.ALOAD:
			push(get(var));
			break;
		case Opcodes.ILOAD:
			push(Opcodes.INTEGER);
			break;
		case Opcodes.FLOAD:
			push(Opcodes.FLOAT);
			break;
		case Opcodes.LLOAD:
			push(Opcodes.LONG);
			push(Opcodes.TOP);
			break;
		case Opcodes.DLOAD:
			push(Opcodes.DOUBLE);
			push(Opcodes.TOP);
			break;
		case Opcodes.ASTORE:
		case Opcodes.ISTORE:
		case Opcodes.FSTORE:
			t = pop();
			set(var, t);
			break;
		case Opcodes.LSTORE:
		case Opcodes.DSTORE:
			pop(1);
			t = pop();
			set(var, t);
			set(var + 1, Opcodes.TOP);
			break;
		default:
			throw new IllegalArgumentException();
		}
		mv.visitVarInsn(opcode, var);
	}

	@Override
	public void visitTypeInsn(final int opcode, final String type) {
		switch (opcode) {
		case Opcodes.NEW:
			final Label label = new Label();
			mv.visitLabel(label);
			push(label);
			break;
		case Opcodes.ANEWARRAY:
			pop(1);
			push('[' + Type.getObjectType(type).getDescriptor());
			break;
		case Opcodes.CHECKCAST:
			pop(1);
			push(type);
			break;
		case Opcodes.INSTANCEOF:
			pop(1);
			push(Opcodes.INTEGER);
			break;
		default:
			throw new IllegalArgumentException();
		}
		mv.visitTypeInsn(opcode, type);
	}

	@Override
	public void visitFieldInsn(final int opcode, final String owner,
			final String name, final String desc) {
		final Type t = Type.getType(desc);
		switch (opcode) {
		case Opcodes.PUTSTATIC:
			pop(t);
			break;
		case Opcodes.PUTFIELD:
			pop(t);
			pop(1);
			break;
		case Opcodes.GETSTATIC:
			push(t);
			break;
		case Opcodes.GETFIELD:
			pop(1);
			push(t);
			break;
		default:
			throw new IllegalArgumentException();
		}
		mv.visitFieldInsn(opcode, owner, name, desc);
	}

	@Override
	public void visitMethodInsn(final int opcode, final String owner,
			final String name, final String desc) {
		for (final Type t : Type.getArgumentTypes(desc)) {
			pop(t);
		}
		if (opcode != Opcodes.INVOKESTATIC) {
			final Object target = pop();
			if (target == Opcodes.UNINITIALIZED_THIS) {
				replace(Opcodes.UNINITIALIZED_THIS, this.owner);
			} else if (target instanceof Label) {
				replace(target, owner);
			}
		}
		push(Type.getReturnType(desc));

		mv.visitMethodInsn(opcode, owner, name, desc);
	}

	@Override
	public void visitInvokeDynamicInsn(final String name, final String desc,
			final Handle bsm, final Object... bsmArgs) {
		for (final Type t : Type.getArgumentTypes(desc)) {
			pop(t);
		}
		push(Type.getReturnType(desc));

		mv.visitInvokeDynamicInsn(name, desc, bsm, bsmArgs);
	}

	@Override
	public void visitLdcInsn(final Object cst) {
		if (cst instanceof Integer) {
			push(Opcodes.INTEGER);
		} else if (cst instanceof Float) {
			push(Opcodes.FLOAT);
		} else if (cst instanceof Long) {
			push(Opcodes.LONG);
			push(Opcodes.TOP);
		} else if (cst instanceof Double) {
			push(Opcodes.DOUBLE);
			push(Opcodes.TOP);
		} else if (cst instanceof String) {
			push("java/lang/String");
		} else if (cst instanceof Type) {
			push("java/lang/Class");
		} else {
			throw new IllegalArgumentException();
		}
		mv.visitLdcInsn(cst);
	}

	@Override
	public void visitJumpInsn(final int opcode, final Label label) {
		switch (opcode) {
		case Opcodes.GOTO:
			break;
		case Opcodes.IFEQ:
		case Opcodes.IFNE:
		case Opcodes.IFLT:
		case Opcodes.IFGE:
		case Opcodes.IFGT:
		case Opcodes.IFLE:
		case Opcodes.IFNULL:
		case Opcodes.IFNONNULL:
			pop(1);
			break;
		case Opcodes.IF_ICMPEQ:
		case Opcodes.IF_ICMPNE:
		case Opcodes.IF_ICMPLT:
		case Opcodes.IF_ICMPGE:
		case Opcodes.IF_ICMPGT:
		case Opcodes.IF_ICMPLE:
		case Opcodes.IF_ACMPEQ:
		case Opcodes.IF_ACMPNE:
			pop(2);
			break;
		default:
			throw new IllegalArgumentException();
		}
		mv.visitJumpInsn(opcode, label);
	}

	@Override
	public void visitIincInsn(final int var, final int increment) {
		set(var, Opcodes.INTEGER);
		mv.visitIincInsn(var, increment);
	}

	@Override
	public void visitTableSwitchInsn(final int min, final int max,
			final Label dflt, final Label... labels) {
		pop(1);
		mv.visitTableSwitchInsn(min, max, dflt, labels);
	}

	@Override
	public void visitLookupSwitchInsn(final Label dflt, final int[] keys,
			final Label[] labels) {
		pop(1);
		mv.visitLookupSwitchInsn(dflt, keys, labels);
	}

	@Override
	public void visitMultiANewArrayInsn(final String desc, final int dims) {
		pop(dims);
		push(desc);
		mv.visitMultiANewArrayInsn(desc, dims);
	}

	private void push(final Object type) {
		stack = ensureSize(stack, stackSize + 1);
		stack[stackSize] = type;
		stackSize++;
	}

	private void push(final Type type) {
		switch (type.getSort()) {
		case Type.VOID:
			break;
		case Type.BOOLEAN:
		case Type.BYTE:
		case Type.CHAR:
		case Type.INT:
		case Type.SHORT:
			push(Opcodes.INTEGER);
			break;
		case Type.FLOAT:
			push(Opcodes.FLOAT);
			break;
		case Type.LONG:
			push(Opcodes.LONG);
			push(Opcodes.TOP);
			break;
		case Type.DOUBLE:
			push(Opcodes.DOUBLE);
			push(Opcodes.TOP);
			break;
		case Type.ARRAY:
		case Type.OBJECT:
			push(type.getInternalName());
			break;
		default:
			throw new AssertionError(type);
		}
	}

	private Object pop() {
		stackSize--;
		assertValidFrames(stackSize);
		return stack[stackSize];
	}

	private void pop(final int count) {
		stackSize -= count;
		assertValidFrames(stackSize);
	}

	private void assertValidFrames(final int stackSize) {
		if (stackSize < 0) {
			throw new IllegalStateException(
					"Missing or invalid stackmap frames.");
		}
	}

	private void pop(final Type type) {
		pop(type.getSize());
	}

	private void set(final int pos, final Object type) {
		local = ensureSize(local, pos + 1);
		// fill gaps:
		for (int i = localSize; i < pos; i++) {
			local[i] = Opcodes.TOP;
		}
		localSize = Math.max(localSize, pos + 1);
		local[pos] = type;
	}

	private void set(final int pos, final Type type) {
		switch (type.getSort()) {
		case Type.BOOLEAN:
		case Type.BYTE:
		case Type.CHAR:
		case Type.INT:
		case Type.SHORT:
			set(pos, Opcodes.INTEGER);
			break;
		case Type.FLOAT:
			set(pos, Opcodes.FLOAT);
			break;
		case Type.LONG:
			set(pos, Opcodes.LONG);
			set(pos + 1, Opcodes.TOP);
			break;
		case Type.DOUBLE:
			set(pos, Opcodes.DOUBLE);
			set(pos + 1, Opcodes.TOP);
			break;
		case Type.ARRAY:
		case Type.OBJECT:
			set(pos, type.getInternalName());
			break;
		default:
			throw new AssertionError(type);
		}
	}

	private Object get(final int pos) {
		if (localSize <= pos) {
			throw new IllegalStateException(
					"Missing or invalid stackmap frames.");
		}
		return local[pos];
	}

	private Object[] ensureSize(final Object[] array, final int size) {
		if (array.length >= size) {
			return array;
		}
		int newLength = array.length;
		while (newLength < size) {
			newLength *= 2;
		}
		final Object[] newArray = new Object[newLength];
		System.arraycopy(array, 0, newArray, 0, array.length);
		return newArray;
	}

	/**
	 * Expand double word types into two slots.
	 */
	private int expand(final Object[] source, final int size,
			final Object[] target) {
		int targetIdx = 0;
		for (int sourceIdx = 0; sourceIdx < size; sourceIdx++) {
			final Object type = source[sourceIdx];
			target[targetIdx++] = type;
			if (type == Opcodes.LONG || type == Opcodes.DOUBLE) {
				target[targetIdx++] = Opcodes.TOP;
			}
		}
		return targetIdx;
	}

	/**
	 * Reduce double word types into a single slot.
	 */
	private int reduce(final Object[] source, final int size,
			final Object[] target) {
		int targetIdx = 0;
		for (int sourceIdx = 0; sourceIdx < size; sourceIdx++) {
			final Object type = source[sourceIdx];
			target[targetIdx++] = type;
			if (type == Opcodes.LONG || type == Opcodes.DOUBLE) {
				sourceIdx++;
			}
		}
		return targetIdx;
	}

	/**
	 * Replaces a type in the locals and on the stack. This is used for
	 * uninitialized objects.
	 * 
	 * @param oldtype
	 *            type to replace
	 * @param newtype
	 *            replacement type
	 */
	private void replace(final Object oldtype, final Object newtype) {
		for (int i = 0; i < localSize; i++) {
			if (oldtype.equals(local[i])) {
				local[i] = newtype;
			}
		}
		for (int i = 0; i < stackSize; i++) {
			if (oldtype.equals(stack[i])) {
				stack[i] = newtype;
			}
		}
	}

}