|
| 1 | +/******************************************************************************* |
| 2 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +* you may not use this file except in compliance with the License. |
| 4 | +* You may obtain a copy of the License at |
| 5 | +* |
| 6 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +* |
| 8 | +* Unless required by applicable law or agreed to in writing, software |
| 9 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +* See the License for the specific language governing permissions and |
| 12 | +* limitations under the License. |
| 13 | +*******************************************************************************/ |
| 14 | + |
| 15 | +package net.adoptopenjdk.blockedexitagent; |
| 16 | + |
| 17 | +import java.lang.instrument.*; |
| 18 | +import java.security.ProtectionDomain; |
| 19 | +import org.objectweb.asm.*; |
| 20 | +import org.objectweb.asm.commons.LocalVariablesSorter; |
| 21 | + |
| 22 | +import static org.objectweb.asm.Opcodes.*; |
| 23 | + |
| 24 | +/* |
| 25 | + * Replace all calls to System.exit for load tests with BlockedExitException. |
| 26 | + * This ensures the test framework will not shut down before all tests |
| 27 | + * have completed. |
| 28 | + */ |
| 29 | +class BlockedExitAgent { |
| 30 | + public static void premain(String args, Instrumentation instrumentation) { |
| 31 | + instrumentation.addTransformer(new BlockedExitTransformer(), true); |
| 32 | + } |
| 33 | + static class BlockedExitTransformer implements ClassFileTransformer { |
| 34 | + @Override |
| 35 | + public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classBytes) { |
| 36 | + /* System.exit calls in LoadTest class should not be overwritten. */ |
| 37 | + if ((null != loader) && (!className.contains("net/adoptopenjdk/loadTest/LoadTest"))) { |
| 38 | + ClassReader cr = new ClassReader(classBytes); |
| 39 | + ClassWriter cw = new ClassWriter(cr, ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS); |
| 40 | + cr.accept(new BlockedExitClassVisitor(cw), ClassReader.EXPAND_FRAMES); |
| 41 | + return cw.toByteArray(); |
| 42 | + } else { |
| 43 | + return null; |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + public static class BlockedExitClassVisitor extends ClassVisitor { |
| 49 | + public BlockedExitClassVisitor(ClassVisitor cv) { |
| 50 | + super(ASM9, cv); |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public MethodVisitor visitMethod(int methodAccess, String methodName, String methodDesc, String signature, String[] exceptions) { |
| 55 | + MethodVisitor methodVisitor = cv.visitMethod(methodAccess, methodName, methodDesc, signature, exceptions); |
| 56 | + return new BlockedExitMethodVisitor(methodAccess, methodDesc, methodVisitor); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + static class BlockedExitMethodVisitor extends LocalVariablesSorter { |
| 61 | + public BlockedExitMethodVisitor(int access, String descriptor, MethodVisitor methodVisitor) { |
| 62 | + super(ASM9, access, descriptor, methodVisitor); |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public void visitMethodInsn(int opcode, String owner, String name, String descriptor, boolean isInterface) { |
| 67 | + if (isSystemExitInsn(opcode, owner, name, descriptor)) { |
| 68 | + String blockedExitException = "net/adoptopenjdk/loadTest/BlockedExitException"; |
| 69 | + /* The bytecode just before this will have loaded the exit code |
| 70 | + * onto the stack. Store it in a new local variable so it can |
| 71 | + * be passed into the new exception. |
| 72 | + */ |
| 73 | + int localId = super.newLocal(Type.INT_TYPE); |
| 74 | + |
| 75 | + super.visitVarInsn(ISTORE, localId); |
| 76 | + super.visitTypeInsn(NEW, blockedExitException); |
| 77 | + super.visitInsn(DUP); |
| 78 | + super.visitVarInsn(ILOAD, localId); |
| 79 | + super.visitMethodInsn(INVOKESPECIAL, blockedExitException, "<init>", "(I)V"); |
| 80 | + super.visitInsn(ATHROW); |
| 81 | + } else { |
| 82 | + super.visitMethodInsn(opcode, owner, name, descriptor, isInterface); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + private boolean isSystemExitInsn(int opcode, String owner, String name, String descriptor) { |
| 87 | + return (opcode == INVOKESTATIC) |
| 88 | + && "java/lang/System".equals(owner) |
| 89 | + && "exit".equals(name) |
| 90 | + && "(I)V".equals(descriptor); |
| 91 | + } |
| 92 | + } |
| 93 | +} |
0 commit comments