File: ClassLoadingHelper.java

package info (click to toggle)
openjdk-11 11.0.16%2B8-1~deb10u1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 784,576 kB
  • sloc: java: 5,128,021; xml: 1,192,224; cpp: 1,124,021; ansic: 422,433; javascript: 155,577; sh: 17,084; objc: 13,327; python: 4,522; asm: 3,570; makefile: 2,858; awk: 351; sed: 172; perl: 114; jsp: 24; csh: 3
file content (349 lines) | stat: -rw-r--r-- 14,244 bytes parent folder | download | duplicates (10)
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
/*
 * Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */
package gc.g1.unloading.loading;

import gc.g1.unloading.ExecutionTask;
import gc.g1.unloading.bytecode.*;
//import gc.g1.unloading.check.*;
import gc.g1.unloading.check.Assertion;
import gc.g1.unloading.check.ClassAssertion;
import gc.g1.unloading.check.PhantomizedAssertion;

import gc.g1.unloading.check.FinalizedAssertion;
import gc.g1.unloading.check.PhantomizationServiceThread;
import gc.g1.unloading.check.cleanup.UnusedThreadKiller;
import gc.g1.unloading.classloaders.DoItYourselfClassLoader;
import gc.g1.unloading.classloaders.FinalizableClassloader;
import gc.g1.unloading.classloaders.JNIClassloader;
import gc.g1.unloading.classloaders.ReflectionClassloader;
import gc.g1.unloading.configuration.ClassloadingMethod;
import gc.g1.unloading.configuration.KeepRefMode;
import gc.g1.unloading.configuration.TestConfiguration;
import gc.g1.unloading.keepref.*;
import nsk.share.test.ExecutionController;
import sun.hotspot.WhiteBox;
import jdk.internal.misc.Unsafe;

import java.lang.ref.*;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.LinkedList;
import java.util.Random;

/**
 * This helper performs dirty job: loads classes, instantiate objects, performs redefinition etc...
 */
public class ClassLoadingHelper {

    private static final int NATIVE_VERBOSITY = 2;

    private static final Object[] NO_CP_PATCHES = new Object[0];

    private static BytecodeFactory bf;

    private ExecutionController executionController;

    private PhantomizationServiceThread phantomizationServiceThread;

    private Random random;

    private TestConfiguration configuration;

    /**
     * Constructor that creates instance of helper. All arguments are self-explaining.
     * @param executionController
     * @param randomSeed
     * @param testConfiguration
     */
    public ClassLoadingHelper(ExecutionController executionController,
                              long randomSeed, TestConfiguration testConfiguration) {
        random = new Random(randomSeed);
        this.executionController = executionController;
        this.configuration = testConfiguration;

        phantomizationServiceThread = new PhantomizationServiceThread(executionController);
        Thread thread = new Thread(phantomizationServiceThread);
        thread.setDaemon(true);
        thread.start();

        if (configuration.isInMemoryCompilation() && !configuration.isHumongousClass() && !(configuration.getKeepRefMode() == KeepRefMode.THREAD_ITSELF)) {
            bf = new BytecodeGeneratorFactory(random.nextLong());
        } else {
            if (configuration.isHumongousClass()) {
                bf = new BytecodeMutatorFactory(HumongousTemplateClass.class.getName());
            } else if (configuration.getKeepRefMode() == KeepRefMode.THREAD_ITSELF) {
                bf = new BytecodeMutatorFactory(ThreadTemplateClass.class.getName());
            } else {
                bf = new BytecodeMutatorFactory();
            }
        }
    }

    /**
     * Load class that's supposed to live. Method returns collection of assertions to check it will live.
     * @param className_
     * @return
     */
    public Collection<Assertion> loadClassThatGonnaLive(String className_) {
        Bytecode kit = bf.createBytecode(className_);
        String className = kit.getClassName();
        byte[] bytecode = kit.getBytecode();
        Class<?> clazz = loadClass(className, bytecode);
        Object object = instantiateObject(clazz);
        Object referenceToKeep = configuration.getWhatToKeep().decideUponRefToKeep(clazz, clazz.getClassLoader(), object);

        redefineIfNeeded(bytecode, clazz);

        warmUpClassIfNeeded(object);
        Assertion assertion;
        // The JVM prepends the host class's package to the anonymous class name.
        if (configuration.getClassloadingMethod() != ClassloadingMethod.ANONYMOUS_CLASSLOADER) {
            assertion = new ClassAssertion(className, true);
        } else {
            assertion = new ClassAssertion("gc/g1/unloading/loading/" + className, true);
        }
        switch (configuration.getKeepRefMode()) {
            case STRONG_REFERENCE:
                assertion.keepLink(referenceToKeep);
                break;
            case SOFT_REFERENCE:
                assertion.keepLink(new SoftReference<Object>(referenceToKeep));
                break;
            case STATIC_FIELD:
                RefHolder holder1 = new InStaticFieldHolder();
                assertion.keepLink(holder1.hold(referenceToKeep));
                break;
            case STACK_LOCAL:
                RefHolder holder2 = new InStackLocalHolder(); // UnusedThreadKiller
                assertion.keepLink(holder2.hold(referenceToKeep));
                break;
            case THREAD_FIELD:
                RefHolder holder3 = new InThreadFieldHolder(); // UnusedThreadKiller
                assertion.keepLink(holder3.hold(referenceToKeep));
                break;
            case THREAD_ITSELF:
                Thread objectThread = (Thread) object;
                objectThread.setDaemon(true);
                objectThread.start();
                assertion.keepLink(new UnusedThreadKiller(objectThread.getId())); // UnusedThreadKiller
                break;
            case STATIC_FIELD_OF_ROOT_CLASS:
                RefHolder holder4 = new NullClassloaderHolder();
                Object keep = holder4.hold(referenceToKeep);
                if (keep != null) {
                    assertion.keepLink(keep);
                }
                break;
            case JNI_GLOBAL_REF:
                JNIGlobalRefHolder holder5 = new JNIGlobalRefHolder();
                assertion.keepLink(holder5.hold(referenceToKeep));
                break;
            case JNI_LOCAL_REF:
                JNILocalRefHolder holder6 = new JNILocalRefHolder();
                assertion.keepLink(holder6.hold(referenceToKeep));
                break;
        }

        Collection<Assertion> returnValue = new LinkedList<>();
        returnValue.add(assertion);
        return returnValue;
    }

    /**
     * Load class that's supposed to be unloaded. Method returns collection of assertions to check it will be unloaded.
     * @param className_
     * @return
     */
    public Collection<Assertion> loadClassThatGonnaDie(String className_) {
        Collection<Assertion> returnValue = new LinkedList<>();
        Bytecode kit = bf.createBytecode(className_);
        String className = kit.getClassName();
        byte[] bytecode = kit.getBytecode();
        Class<?> clazz = loadClass(className, bytecode);
        FinalizableClassloader cl = null;
        if (clazz.getClassLoader() instanceof FinalizableClassloader) {
            cl = (FinalizableClassloader) clazz.getClassLoader();
        }
        Object object = instantiateObject(clazz);
        Object referenceToKeep = configuration.getWhatToKeep().decideUponRefToKeep(clazz, clazz.getClassLoader(), object);

        redefineIfNeeded(bytecode, clazz);

        warmUpClassIfNeeded(object);
        Assertion assertion;
        // The JVM prepends the host class's package to the anonymous class name.
        if (configuration.getClassloadingMethod() != ClassloadingMethod.ANONYMOUS_CLASSLOADER) {
            assertion = new ClassAssertion(className, false);
        } else {
            assertion = new ClassAssertion("gc/g1/unloading/loading/" + className, false);
        }
        switch (configuration.getReleaseRefMode()) {
            case WEAK:
                assertion.keepLink(new WeakReference<Object>(referenceToKeep));
                break;
            case PHANTOM:
                final ReferenceQueue queue = new ReferenceQueue<Object>();
                assertion.keepLink(new PhantomReference<Object>(referenceToKeep, queue));
                new Thread(new ReferenceCleaningThread(executionController, queue)).start();
                break;
        }
        returnValue.add(assertion);

        if (cl != null) {
            // Check that classloader will be finalized
            FinalizedAssertion finalizedAssertion = new FinalizedAssertion();
            cl.setFinalizedAssertion(finalizedAssertion);
            returnValue.add(finalizedAssertion);

            // Check that classloader will be phantomized
            PhantomizedAssertion phantomizedAssertion = new PhantomizedAssertion();
            PhantomReference phantomReference = new PhantomReference<Object>(cl, phantomizationServiceThread.getQueue());
            phantomizationServiceThread.add(phantomReference, phantomizedAssertion);
            returnValue.add(phantomizedAssertion);
        }
        return returnValue;
    }

    private void redefineIfNeeded(byte[] bytecode, Class<?> clazz) {
        if (configuration.isRedefineClasses()) {
            BytecodePatcher.patch(bytecode);
            makeRedefinition(NATIVE_VERBOSITY, clazz, bytecode);

            // This will call class's method
            instantiateObject(clazz);
        }
    }

    private Class<?> loadClass(String className, byte[] bytecode) {
        try {
            switch (configuration.getClassloadingMethod()) {
                case PLAIN:
                    DoItYourselfClassLoader loader1 = new DoItYourselfClassLoader();
                    return loader1.defineClass(className, bytecode);
                case REFLECTION:
                    return Class.forName(className, true, new ReflectionClassloader(bytecode, className));
                case JNI:
                    return JNIClassloader.loadThroughJNI(className, bytecode);
                case ANONYMOUS_CLASSLOADER:
                    return getUnsafe().defineAnonymousClass(ClassLoadingHelper.class, bytecode, NO_CP_PATCHES);
            }
            return null;
        } catch (ClassNotFoundException e) {
            throw new RuntimeException("Test bug!", e);
        }
    }

    private Object instantiateObject(Class<?> clazz) {
        try {
            Object object = clazz.newInstance();

            // Call method just for fun
            for (Method m : clazz.getMethods()) {
                if (m.getName().equals("main")) {
                    m.invoke(object);
                }
            }
            return object;
        } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
            throw new RuntimeException("Test bug!", e);
        }
    }

    private void warmUpClassIfNeeded(Object object) {
        if (configuration.getCompilationLevel() < 1 || configuration.getCompilationNumber() == 0) {
            return;
        }
        Method m = null;
        for (Method method : object.getClass().getMethods()) {
            if (method.getName().equalsIgnoreCase("methodForCompilation")) {
                m = method;
            }
        }
        WhiteBox wb = WhiteBox.getWhiteBox();
        if (!wb.isMethodCompilable(m)) {
            throw new RuntimeException("Test bug! Method occured to be not compilable. Requires investigation.");
        }

        for (int i = configuration.getCompilationNumber(); i >= 0 && executionController.continueExecution(); i--) {
            if (!wb.isMethodCompilable(m, configuration.getCompilationLevel())) {
              continue;
            }
            wb.enqueueMethodForCompilation(m, configuration.getCompilationLevel());
            while (!wb.isMethodCompiled(m) && executionController.continueExecution()) {
                sleep(50);
                try {
                    m.invoke(object, new Object());
                } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
                    throw new RuntimeException("Something went wrong while compilation", e);
                }
            }
            if (i > 0) {
                wb.deoptimizeMethod(m);
            }
        }
    }

    native static int makeRedefinition0(int verbose, Class<?> redefClass, byte[] classBytes);

    private static void makeRedefinition(int verbose, Class<?> redefClass, byte[] classBytes) {
        new LibLoader().hashCode();
        if (makeRedefinition0(verbose, redefClass, classBytes) != 0) {
            throw new RuntimeException("Test bug: native method \"makeRedefinition\" return nonzero");
        }
    }

    private static void sleep(long millis) {
        try {
            Thread.sleep(millis);
        } catch (InterruptedException e) {
            throw new RuntimeException("Got InterruptedException while sleeping.", e);
        }
    }

    private static Unsafe getUnsafe() {
        return Unsafe.getUnsafe();
    }

}

class ReferenceCleaningThread extends ExecutionTask {

    private ReferenceQueue<?> queue;

    public ReferenceCleaningThread(ExecutionController executionController, ReferenceQueue<?> queue) {
        super(executionController);
        this.queue = queue;
    }

    @Override
    protected void task() throws Exception {
        Reference<?> ref = queue.remove(100);
        if (ref != null) {
            ref.clear();
            return;
        }
    }

}