8248486: SafeThread illegal access to java.lang private fields should be removed

Reviewed-by: chegar, rriggs, mchung, alanb
This commit is contained in:
Joe Wang 2020-07-13 18:19:02 +00:00
parent 83a458c5f7
commit 8f8ff52cae
2 changed files with 13 additions and 38 deletions

View file

@ -193,7 +193,6 @@ module java.base {
java.net.http, java.net.http,
java.rmi, java.rmi,
java.security.jgss, java.security.jgss,
java.xml,
jdk.attach, jdk.attach,
jdk.charsets, jdk.charsets,
jdk.compiler, jdk.compiler,

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2015, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -24,34 +24,30 @@
*/ */
package com.sun.org.apache.xml.internal.utils; package com.sun.org.apache.xml.internal.utils;
import java.util.concurrent.atomic.AtomicInteger;
/** /**
* This is a combination of ThreadControllerWrapper's inner class SafeThread * Represents a safe thread that does not inherit thread-locals and runs only
* that was introduced as a fix for CR 6607339 * once.
* and sun.misc.ManagedLocalsThread, a thread that has it's thread locals, and
* inheritable thread locals erased on construction. Except the run method,
* it is identical to sun.misc.ManagedLocalsThread.
*/ */
public class SafeThread extends Thread { public class SafeThread extends Thread {
private static final jdk.internal.misc.Unsafe UNSAFE;
private static final long THREAD_LOCALS;
private static final long INHERITABLE_THREAD_LOCALS;
private volatile boolean ran = false; private volatile boolean ran = false;
private static final AtomicInteger threadNumber = new AtomicInteger(1);
private static String threadName() {
return "SafeThread-" + threadNumber.getAndIncrement();
}
public SafeThread(Runnable target) { public SafeThread(Runnable target) {
super(target); this(null, target, threadName());
eraseThreadLocals();
} }
public SafeThread(Runnable target, String name) { public SafeThread(Runnable target, String name) {
super(target, name); this(null, target, name);
eraseThreadLocals();
} }
public SafeThread(ThreadGroup group, Runnable target, String name) { public SafeThread(ThreadGroup group, Runnable target, String name) {
super(group, target, name); super(group, target, name, 0, false);
eraseThreadLocals();
} }
public final void run() { public final void run() {
@ -69,24 +65,4 @@ public class SafeThread extends Thread {
} }
super.run(); super.run();
} }
/**
* Drops all thread locals (and inherited thread locals).
*/
public final void eraseThreadLocals() {
UNSAFE.putReference(this, THREAD_LOCALS, null);
UNSAFE.putReference(this, INHERITABLE_THREAD_LOCALS, null);
}
static {
UNSAFE = jdk.internal.misc.Unsafe.getUnsafe();
Class<?> t = Thread.class;
try {
THREAD_LOCALS = UNSAFE.objectFieldOffset(t.getDeclaredField("threadLocals"));
INHERITABLE_THREAD_LOCALS = UNSAFE.objectFieldOffset(t.getDeclaredField("inheritableThreadLocals"));
} catch (Exception e) {
throw new Error(e);
}
}
} }