Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/lang/System/LoggerFinder/internal/BootstrapLogger/BootstrapLoggerTest.java
41161 views
1
/*
2
* Copyright (c) 2014, 2019, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
import java.io.PrintStream;
25
import java.lang.ref.Reference;
26
import java.lang.ref.ReferenceQueue;
27
import java.lang.ref.WeakReference;
28
import java.lang.reflect.Array;
29
import java.lang.reflect.Field;
30
import java.lang.reflect.Method;
31
import java.lang.System.Logger;
32
import java.lang.System.Logger.Level;
33
import java.security.AllPermission;
34
import java.security.CodeSource;
35
import java.security.Permission;
36
import java.security.PermissionCollection;
37
import java.security.Permissions;
38
import java.security.Policy;
39
import java.security.ProtectionDomain;
40
import java.util.concurrent.atomic.AtomicBoolean;
41
import java.util.List;
42
import java.util.Optional;
43
import java.util.Set;
44
import java.util.stream.Collectors;
45
import java.util.stream.Stream;
46
import jdk.internal.logger.BootstrapLogger;
47
import jdk.internal.logger.LazyLoggers;
48
49
/*
50
* @test
51
* @bug 8140364 8189291
52
* @author danielfuchs
53
* @summary JDK implementation specific unit test for JDK internal artifacts.
54
Tests the behavior of bootstrap loggers (and SimpleConsoleLoggers
55
* too).
56
* @modules java.base/jdk.internal.logger:+open
57
* java.logging
58
* @build BootstrapLoggerUtils LogStream
59
* @run main/othervm BootstrapLoggerTest NO_SECURITY
60
* @run main/othervm -Djava.security.manager=allow BootstrapLoggerTest SECURE
61
* @run main/othervm/timeout=120 -Djava.security.manager=allow BootstrapLoggerTest SECURE_AND_WAIT
62
*/
63
public class BootstrapLoggerTest {
64
65
static final Policy DEFAULT_POLICY = Policy.getPolicy();
66
static final Method isAlive;
67
static final Field logManagerInitialized;
68
static {
69
try {
70
// private reflection hook that allows us to test whether
71
// the BootstrapExecutor is alive.
72
isAlive = BootstrapLogger.class
73
.getDeclaredMethod("isAlive");
74
isAlive.setAccessible(true);
75
// private reflection hook that allows us to test whether the LogManager
76
// has initialized and registered with the BootstrapLogger class
77
logManagerInitialized = BootstrapLogger.class
78
.getDeclaredField("logManagerConfigured");
79
logManagerInitialized.setAccessible(true);
80
} catch (Exception ex) {
81
throw new ExceptionInInitializerError(ex);
82
}
83
}
84
85
static enum TestCase {
86
NO_SECURITY, SECURE, SECURE_AND_WAIT
87
}
88
89
public static void main(String[] args) throws Exception {
90
if (args == null || args.length == 0) {
91
args = new String[] { TestCase.SECURE_AND_WAIT.name() };
92
}
93
if (args.length > 1) throw new RuntimeException("Only one argument allowed");
94
TestCase test = TestCase.valueOf(args[0]);
95
System.err.println("Testing: " + test);
96
97
98
// private reflection hook that allows us to simulate a non booted VM
99
final AtomicBoolean vmBooted = new AtomicBoolean(false);
100
BootstrapLoggerUtils.setBootedHook(() -> vmBooted.get());
101
102
// We replace System.err to check the messages that have been logged
103
// by the JUL ConsoleHandler and default SimpleConsoleLogger
104
// implementaion
105
final LogStream err = new LogStream();
106
System.setErr(new PrintStream(err));
107
108
if (BootstrapLogger.isBooted()) {
109
throw new RuntimeException("VM should not be booted!");
110
}
111
Logger logger = LazyLoggers.getLogger("foo.bar", Thread.class.getModule());
112
113
if (test != TestCase.NO_SECURITY) {
114
LogStream.err.println("Setting security manager");
115
Policy.setPolicy(new SimplePolicy());
116
System.setSecurityManager(new SecurityManager());
117
}
118
119
Level[] levels = {Level.INFO, Level.WARNING, Level.INFO};
120
int index = 0;
121
logger.log(levels[index], "Early message #" + (index+1)); index++;
122
logger.log(levels[index], "Early message #" + (index+1)); index++;
123
LogStream.err.println("VM Booted: " + vmBooted.get());
124
LogStream.err.println("LogManager initialized: " + logManagerInitialized.get(null));
125
logger.log(levels[index], "Early message #" + (index+1)); index++;
126
if (err.drain().contains("Early message")) {
127
// We're expecting that logger will be a LazyLogger wrapping a
128
// BootstrapLogger. The Bootstrap logger will stack the log messages
129
// it receives until the VM is booted.
130
// Since our private hook pretend that the VM is not booted yet,
131
// the logged messages shouldn't have reached System.err yet.
132
throw new RuntimeException("Early message logged while VM is not booted!");
133
}
134
135
// Now pretend that the VM is booted. Nothing should happen yet, until
136
// we try to log a new message.
137
vmBooted.getAndSet(true);
138
LogStream.err.println("VM Booted: " + vmBooted.get());
139
LogStream.err.println("LogManager initialized: " + logManagerInitialized.get(null));
140
if (!BootstrapLogger.isBooted()) {
141
throw new RuntimeException("VM should now be booted!");
142
}
143
if (((Boolean)logManagerInitialized.get(null)).booleanValue()) {
144
throw new RuntimeException("LogManager shouldn't be initialized yet!");
145
}
146
147
// Logging a message should cause the BootstrapLogger to replace itself
148
// by a 'real' logger in the LazyLogger. But since the LogManager isn't
149
// initialized yet, this should be a SimpleConsoleLogger...
150
logger.log(Level.INFO, "LOG#4: VM now booted: {0}", vmBooted.get());
151
logger.log(Level.DEBUG, "LOG#5: hi!");
152
SimplePolicy.allowAll.set(Boolean.TRUE);
153
WeakReference<Thread> threadRef = null;
154
ReferenceQueue<Thread> queue = new ReferenceQueue<>();
155
try {
156
Set<Thread> set = Thread.getAllStackTraces().keySet().stream()
157
.filter((t) -> t.getName().startsWith("BootstrapMessageLoggerTask-"))
158
.collect(Collectors.toSet());
159
set.stream().forEach(t -> LogStream.err.println("Found: " + t));
160
if (set.size() > 1) {
161
throw new RuntimeException("Too many bootstrap threads found");
162
}
163
Optional<Thread> t = set.stream().findFirst();
164
if (t.isPresent()) {
165
threadRef = new WeakReference<>(t.get(), queue);
166
}
167
} finally{
168
SimplePolicy.allowAll.set(Boolean.FALSE);
169
}
170
if (!BootstrapLogger.isBooted()) {
171
throw new RuntimeException("VM should still be booted!");
172
}
173
if (((Boolean)logManagerInitialized.get(null)).booleanValue()) {
174
throw new RuntimeException("LogManager shouldn't be initialized yet!");
175
}
176
177
// Now check that the early messages we had printed before the VM was
178
// booted have appeared on System.err...
179
String afterBoot = err.drain();
180
for (int i=0; i<levels.length; i++) {
181
String m = levels[i].getName()+": Early message #"+(i+1);
182
if (!afterBoot.contains(m)) {
183
throw new RuntimeException("System.err does not contain: "+m);
184
}
185
}
186
// check that the message logged *after* the VM was booted also printed.
187
if (!afterBoot.contains("INFO: LOG#4")) {
188
throw new RuntimeException("System.err does not contain: "
189
+ "INFO: LOG#4");
190
}
191
// check that the debug message was not printed.
192
if (afterBoot.contains("LOG#5")) {
193
throw new RuntimeException("System.err contain: " + "LOG#5");
194
}
195
LogStream.err.println("VM Booted: " + vmBooted.get());
196
LogStream.err.println("LogManager initialized: " + logManagerInitialized.get(null));
197
if (!BootstrapLogger.isBooted()) {
198
throw new RuntimeException("VM should still be booted!");
199
}
200
if (((Boolean)logManagerInitialized.get(null)).booleanValue()) {
201
throw new RuntimeException("LogManager shouldn't be initialized yet!");
202
}
203
204
// Now we're going to use reflection to access JUL, and change
205
// the level of the "foo" logger.
206
// We're using reflection so that the test can also run in
207
// configurations where java.util.logging is not present.
208
boolean hasJUL = false;
209
SimplePolicy.allowAll.set(Boolean.TRUE);
210
try {
211
Class<?> loggerClass = Class.forName("java.util.logging.Logger");
212
Class<?> levelClass = Class.forName("java.util.logging.Level");
213
Class<?> handlerClass = Class.forName("java.util.logging.Handler");
214
215
// java.util.logging.Logger.getLogger("foo")
216
// .setLevel(java.util.logging.Level.FINEST);
217
Object fooLogger = loggerClass.getMethod("getLogger", String.class)
218
.invoke(null, "foo");
219
loggerClass.getMethod("setLevel", levelClass)
220
.invoke(fooLogger, levelClass.getField("FINEST").get(null));
221
222
// java.util.logging.Logger.getLogger("").getHandlers()[0]
223
// .setLevel(java.util.logging.Level.ALL);
224
Object rootLogger = loggerClass.getMethod("getLogger", String.class)
225
.invoke(null, "");
226
Object handlers = loggerClass.getMethod("getHandlers").
227
invoke(rootLogger);
228
handlerClass.getMethod("setLevel", levelClass)
229
.invoke(Array.get(handlers, 0), levelClass.getField("ALL")
230
.get(null));
231
232
hasJUL = true;
233
} catch (ClassNotFoundException x) {
234
LogStream.err.println("JUL is not present: class " + x.getMessage()
235
+ " not found");
236
hasJUL = false;
237
} finally {
238
SimplePolicy.allowAll.set(Boolean.FALSE);
239
}
240
241
logger.log(Level.DEBUG, "hi now!");
242
String debug = err.drain();
243
if (hasJUL) {
244
if (!((Boolean)logManagerInitialized.get(null)).booleanValue()) {
245
throw new RuntimeException("LogManager should be initialized now!");
246
}
247
if (!debug.contains("FINE: hi now!")) {
248
throw new RuntimeException("System.err does not contain: "
249
+ "FINE: hi now!");
250
}
251
} else {
252
if (debug.contains("hi now!")) {
253
throw new RuntimeException("System.err contains: " + "hi now!");
254
}
255
if (((Boolean)logManagerInitialized.get(null)).booleanValue()) {
256
throw new RuntimeException("LogManager shouldn't be initialized yet!");
257
}
258
Logger baz = System.getLogger("foo.bar.baz");
259
if (((Boolean)logManagerInitialized.get(null)).booleanValue()) {
260
throw new RuntimeException("LogManager shouldn't be initialized yet!");
261
}
262
}
263
Logger bazbaz = null;
264
SimplePolicy.allowAll.set(Boolean.TRUE);
265
try {
266
bazbaz = java.lang.System.LoggerFinder
267
.getLoggerFinder().getLogger("foo.bar.baz.baz", BootstrapLoggerTest.class.getModule());
268
} finally {
269
SimplePolicy.allowAll.set(Boolean.FALSE);
270
}
271
if (!((Boolean)logManagerInitialized.get(null)).booleanValue()) {
272
throw new RuntimeException("LogManager should be initialized now!");
273
}
274
Logger bazbaz2 = System.getLogger("foo.bar.baz.baz");
275
if (bazbaz2.getClass() != bazbaz.getClass()) {
276
throw new RuntimeException("bazbaz2.class != bazbaz.class ["
277
+ bazbaz2.getClass() + " != "
278
+ bazbaz.getClass() + "]");
279
}
280
if (hasJUL != bazbaz2.getClass().getName()
281
.equals("sun.util.logging.internal.LoggingProviderImpl$JULWrapper")) {
282
throw new RuntimeException("Unexpected class for bazbaz: "
283
+ bazbaz.getClass().getName()
284
+ "\n\t expected: "
285
+ "sun.util.logging.internal.LoggingProviderImpl$JULWrapper");
286
}
287
288
// Now we're going to check that the thread of the BootstrapLogger
289
// executor terminates, and that the Executor is GC'ed after that.
290
// This will involve a bit of waiting, hence the timeout=120 in
291
// the @run line.
292
// If this test fails in timeout - we could envisage skipping this part,
293
// or adding some System property to configure the keep alive delay
294
// of the executor.
295
SimplePolicy.allowAll.set(Boolean.TRUE);
296
try {
297
// Though unlikely, it is not impossible that the bootstrap logger
298
// executor may have released its first thread and spawned a new one.
299
// If that happened then the executor itself might have been GC'ed
300
// as well and a new one might have been created.
301
// The code below will lookup the executor threads again and
302
// join them.
303
// Only one may be active at a given time, but that might not
304
// be the one referenced by threadRef.
305
// We're just making sure all of them have stopped running
306
// before verifying that the executor is eventually GC'ed.
307
final WeakReference<Thread> previous = threadRef;
308
Stream<WeakReference<Thread>> stream = Thread.getAllStackTraces().keySet().stream()
309
.filter((t) -> t.getName().startsWith("BootstrapMessageLoggerTask-"))
310
.filter((t) -> previous == null ? true : t != previous.get())
311
.map((t) -> new WeakReference<>(t, queue));
312
List<WeakReference<Thread>> threads = stream.collect(Collectors.toList());
313
if (previous != null) threads.add(previous);
314
threads.forEach(t -> LogStream.err.println(t.get()));
315
stream = null;
316
317
if (test == TestCase.SECURE_AND_WAIT) {
318
// First wait for all executor threads to terminate
319
for (var ref : threads) {
320
Thread t = ref.get();
321
if (t != null) {
322
if (!(Boolean)isAlive.invoke(null) && t.isAlive()) {
323
throw new RuntimeException("Executor already terminated");
324
} else {
325
LogStream.err.println("Executor still alive as expected: " + t.getName());
326
}
327
LogStream.err.println("Waiting for " + t.getName() + " to terminate (join)");
328
t.join(60_000);
329
t = null;
330
} else {
331
LogStream.err.println("WeakReference<Thread> is already cleared.");
332
long count = Thread.getAllStackTraces().keySet().stream()
333
.filter((tr) -> tr.getName().startsWith("BootstrapMessageLoggerTask-"))
334
.count();
335
if (count != 0) {
336
LogStream.err.println("There are " + count + " threads still lingering.");
337
}
338
}
339
}
340
// Then wait until all the executor threads are GC'ed
341
while (!threads.isEmpty()) {
342
LogStream.err.println("Calling System.gc()");
343
System.gc();
344
LogStream.err.println("Waiting for BootstrapMessageLoggerTask to be gc'ed");
345
Reference<?> tref;
346
while ((tref = queue.remove(1000)) == null) {
347
LogStream.err.println("Calling System.gc()");
348
System.gc();
349
}
350
351
threads.remove(tref);
352
LogStream.err.println("BootstrapMessageLoggerTask has been gc'ed: "
353
+ threads.size() + " remaining...");
354
}
355
// Then wait for the executor to be gc'ed...
356
LogStream.err.println("Waiting for the executor to be gc'ed: Calling System.gc()");
357
System.gc();
358
for (int i=0; i<10; i++) {
359
if (!(Boolean)isAlive.invoke(null)) break;
360
// It would be unexpected that we reach here...
361
Thread.sleep(1000);
362
LogStream.err.println("Calling System.gc()");
363
System.gc();
364
}
365
366
if ((Boolean)isAlive.invoke(null)) {
367
throw new RuntimeException("Executor still alive");
368
} else {
369
LogStream.err.println("Executor terminated as expected.");
370
}
371
} else {
372
LogStream.err.println("Not checking executor termination for " + test);
373
}
374
} finally {
375
SimplePolicy.allowAll.set(Boolean.FALSE);
376
}
377
LogStream.err.println(test.name() + ": PASSED");
378
}
379
380
final static class SimplePolicy extends Policy {
381
static final ThreadLocal<Boolean> allowAll = new ThreadLocal<Boolean>() {
382
@Override
383
protected Boolean initialValue() {
384
return Boolean.FALSE;
385
}
386
};
387
388
Permissions getPermissions() {
389
Permissions perms = new Permissions();
390
if (allowAll.get()) {
391
perms.add(new AllPermission());
392
}
393
return perms;
394
}
395
396
@Override
397
public boolean implies(ProtectionDomain domain, Permission permission) {
398
return getPermissions(domain).implies(permission) ||
399
DEFAULT_POLICY.implies(domain, permission);
400
}
401
402
@Override
403
public PermissionCollection getPermissions(CodeSource codesource) {
404
return getPermissions();
405
}
406
407
@Override
408
public PermissionCollection getPermissions(ProtectionDomain domain) {
409
return getPermissions();
410
}
411
412
}
413
}
414
415