Skip to main content
summaryrefslogtreecommitdiffstats
blob: a30d5989e0593af817442e915deb92975239fa28 (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
/**********************************************************************
 * This file is part of "Object Teams Dynamic Runtime Environment"
 * 
 * Copyright 2014 Stephan Herrmann.
 * 
 * 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
 * 
 * Please visit http://www.eclipse.org/objectteams for updates and contact.
 * 
 * Contributors:
 *		Stephan Herrmann - Initial API and implementation
 **********************************************************************/
package org.eclipse.objectteams.otredyn.bytecode.asm;

import static org.eclipse.objectteams.otredyn.bytecode.asm.AsmBoundClass.ASM_API;

import org.eclipse.objectteams.otredyn.transformer.names.ClassNames;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.Label;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;
import org.objectweb.asm.commons.AdviceAdapter;
import org.objectweb.asm.commons.Method;

/**
 * Add code into all direct implementors of Runnable.run() / Thread.run()
 * to inform the TeamThreadManager about new and ended threads.
 * (See org.eclipse.objectteams.otre.ThreadActivation in the old OTRE).
 */
public class AddThreadNotificationAdapter extends ClassVisitor {

	protected static final String THREAD_DESC = "L"+ClassNames.THREAD_SLASH+";";
	protected static final String VOID_DESC = "()V";

	// Runnable / Thread:
	protected static final String RUN 					= "run",				RUN_DESC					= VOID_DESC;

	// Thread:
	protected static final String CURRENT_THREAD 		= "currentThread", 		CURRENT_THREAD_DESC 		= "()"+THREAD_DESC;
	
	// TeamThreadManager:
	protected static final String NEW_THREAD_STARTED 	= "newThreadStarted",	NEW_THREAD_STARTED_DESC 	= "(Z"+THREAD_DESC+")Z";
	protected static final String THREAD_ENDED 			= "threadEnded", 		THREAD_ENDED_DESC 		= VOID_DESC;

	// any implementor:
	protected static final String INIT					= "<init>";
	
	// new field inserted by this adapter:
	protected static final String CREATION_THREAD 		= "_OT$creationThread";


	private AsmBoundClass clazz;

	public AddThreadNotificationAdapter(ClassVisitor cv, AsmBoundClass clazz) {
		super(ASM_API, cv);
		this.clazz = clazz;
	}
	
	@Override
	public void visitEnd() {
		cv.visitField(Opcodes.ACC_PRIVATE, CREATION_THREAD, THREAD_DESC, null, null);
		super.visitEnd();
	}
	
	@Override
	public MethodVisitor visitMethod(int access, String methodName, String desc, String signature, String[] exceptions) {
		if (INIT.equals(methodName)) {
			// into each constructor ...
        	final MethodVisitor methodVisitor = cv.visitMethod(access, methodName, desc, null, null);
            return new AdviceAdapter(this.api, methodVisitor, access, methodName, desc) {
            	@Override
            	public void invokeConstructor(Type type, Method method) {
            		super.invokeConstructor(type, method);
            		// ... that contains a super(..) call (rather than this(..)):
            		if (type.getInternalName().equals(clazz.getInternalSuperClassName())) {
            			// insert:
            			// this._OT$creationThread = Thread.currentThread();
            			methodVisitor.visitIntInsn(Opcodes.ALOAD, 0);
            			methodVisitor.visitMethodInsn(Opcodes.INVOKESTATIC, ClassNames.THREAD_SLASH, CURRENT_THREAD, CURRENT_THREAD_DESC, false);
            			methodVisitor.visitFieldInsn(Opcodes.PUTFIELD, clazz.getInternalName(), CREATION_THREAD, THREAD_DESC);
            		}
            	}
            };
		} else if (RUN.equals(methodName) && RUN_DESC.equals(desc)) {
        	final MethodVisitor methodVisitor = cv.visitMethod(access, methodName, desc, null, null);
            return new AdviceAdapter(this.api, methodVisitor, access, methodName, desc) {

            	Label start = new Label(); 	// start of method (scope of new local)
            	Label end = new Label();	// end of method
				int isThreadStartIdx;		// new local: boolean _OT$isThreadStart

            	@Override
            	protected void onMethodEnter() {
            		methodVisitor.visitLabel(start);
            		isThreadStartIdx=newLocal(Type.BOOLEAN_TYPE);
            		methodVisitor.visitLocalVariable("_OT$isThreadStart", "Z", null, start, end, isThreadStartIdx);
            		// TeamThreadManager.newThreadStarted(false, this._OT$creationThread)
            		methodVisitor.visitInsn(Opcodes.ICONST_0);
            		methodVisitor.visitIntInsn(Opcodes.ALOAD, 0);
            		methodVisitor.visitFieldInsn(Opcodes.GETFIELD, clazz.getInternalName(), CREATION_THREAD, THREAD_DESC);
            		methodVisitor.visitMethodInsn(Opcodes.INVOKESTATIC, ClassNames.TEAM_THREAD_MANAGER_SLASH, 
            										NEW_THREAD_STARTED, NEW_THREAD_STARTED_DESC, false);
            		methodVisitor.visitIntInsn(Opcodes.ISTORE, isThreadStartIdx);
            		// this._OT$creationThread = null; // avoid leak
            		methodVisitor.visitIntInsn(Opcodes.ALOAD, 0);
            		methodVisitor.visitInsn(Opcodes.ACONST_NULL);
            		methodVisitor.visitFieldInsn(Opcodes.PUTFIELD, clazz.getInternalName(), CREATION_THREAD, THREAD_DESC);
            	}
            	
            	@Override
            	protected void onMethodExit(int opcode) {
            		insertThreadEndedNotification();
            	}
            	
            	@Override
            	public void endMethod() {
            		methodVisitor.visitLabel(end);
            		
            		// insert another threadEnded notification as a handler for Throwable
            		Label handler = new Label();
            		methodVisitor.visitLabel(handler);
            		insertThreadEndedNotification();
            		methodVisitor.visitInsn(Opcodes.ATHROW); // rethrow caught exception
            		
            		methodVisitor.visitTryCatchBlock(start, end, handler, ClassNames.THROWABLE_SLASH);
            		methodVisitor.visitMaxs(0, 0);
            	}

				void insertThreadEndedNotification() {
					Label skip = new Label();
            		// insert:
            		// if (_OT$isThreadStart) TeamThreadManager.threadEnded();
            		methodVisitor.visitIntInsn(Opcodes.ILOAD, isThreadStartIdx);
            		methodVisitor.visitJumpInsn(Opcodes.IFEQ, skip);
            		methodVisitor.visitMethodInsn(Opcodes.INVOKESTATIC, ClassNames.TEAM_THREAD_MANAGER_SLASH, 
													THREAD_ENDED, THREAD_ENDED_DESC, false);
            		methodVisitor.visitLabel(skip);
				}
            };		
		}
		return null;
	}

	public static boolean shouldNotify(AsmWritableBoundClass clazz) {
		String[] interfaceNames = clazz.getSuperInterfaceNames();
		if (interfaceNames != null) {
			for (int i = 0; i < interfaceNames.length; i++) {
				if (ClassNames.RUNNABLE_SLASH.equals(interfaceNames[i]))
					return true;
			}
		}
		if (ClassNames.THREAD_SLASH.equals(clazz.getInternalSuperClassName()))
			return true;
		// not traversing super chains, currently. FIXME: Should indeed traverse super interfaces to find Runnable!!
		return false;
	}
}

Back to the top