1 /*
   2  * Copyright (c) 2013, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.misc;
  27 
  28 import java.security.AccessControlContext;
  29 import java.security.ProtectionDomain;
  30 
  31 /**
  32  * A thread that has no permissions, is not a member of any user-defined
  33  * ThreadGroup and supports the ability to erase ThreadLocals.
  34  *
  35  * @implNote Based on the implementation of InnocuousForkJoinWorkerThread.
  36  */
  37 public final class InnocuousThread extends Thread {
  38     private static final Unsafe UNSAFE;
  39     private static final ThreadGroup THREADGROUP;
  40     private static final AccessControlContext ACC;
  41     private static final long THREADLOCALS;
  42     private static final long INHERITABLETHREADLOCALS;
  43     private static final long INHERITEDACCESSCONTROLCONTEXT;
  44 
  45     public InnocuousThread(Runnable target) {
  46         super(THREADGROUP, target, "anInnocuousThread");
  47         UNSAFE.putOrderedObject(this, INHERITEDACCESSCONTROLCONTEXT, ACC);
  48         eraseThreadLocals();
  49     }
  50 
  51     @Override
  52     public ClassLoader getContextClassLoader() {
  53         // always report system class loader
  54         return ClassLoader.getSystemClassLoader();
  55     }
  56 
  57     @Override
  58     public void setUncaughtExceptionHandler(UncaughtExceptionHandler x) {
  59         // silently fail
  60     }
  61 
  62     @Override
  63     public void setContextClassLoader(ClassLoader cl) {
  64         throw new SecurityException("setContextClassLoader");
  65     }
  66 
  67     // ensure run method is run only once
  68     private volatile boolean hasRun;
  69 
  70     @Override
  71     public void run() {
  72         if (Thread.currentThread() == this && !hasRun) {
  73             hasRun = true;
  74             super.run();
  75         }
  76     }
  77 
  78     /**
  79      * Drops all thread locals (and inherited thread locals).
  80      */
  81     public void eraseThreadLocals() {
  82         UNSAFE.putObject(this, THREADLOCALS, null);
  83         UNSAFE.putObject(this, INHERITABLETHREADLOCALS, null);
  84     }
  85 
  86     // Use Unsafe to access Thread group and ThreadGroup parent fields
  87     static {
  88         try {
  89             ACC = new AccessControlContext(new ProtectionDomain[] {
  90                 new ProtectionDomain(null, null)
  91             });
  92 
  93             // Find and use topmost ThreadGroup as parent of new group
  94             UNSAFE = Unsafe.getUnsafe();
  95             Class<?> tk = Thread.class;
  96             Class<?> gk = ThreadGroup.class;
  97 
  98             THREADLOCALS = UNSAFE.objectFieldOffset
  99                 (tk.getDeclaredField("threadLocals"));
 100             INHERITABLETHREADLOCALS = UNSAFE.objectFieldOffset
 101                 (tk.getDeclaredField("inheritableThreadLocals"));
 102             INHERITEDACCESSCONTROLCONTEXT = UNSAFE.objectFieldOffset
 103                 (tk.getDeclaredField("inheritedAccessControlContext"));
 104 
 105             long tg = UNSAFE.objectFieldOffset(tk.getDeclaredField("group"));
 106             long gp = UNSAFE.objectFieldOffset(gk.getDeclaredField("parent"));
 107             ThreadGroup group = (ThreadGroup)
 108                 UNSAFE.getObject(Thread.currentThread(), tg);
 109 
 110             while (group != null) {
 111                 ThreadGroup parent = (ThreadGroup)UNSAFE.getObject(group, gp);
 112                 if (parent == null)
 113                     break;
 114                 group = parent;
 115             }
 116             THREADGROUP = new ThreadGroup(group, "InnocuousThreadGroup");
 117         } catch (Exception e) {
 118             throw new Error(e);
 119         }
 120     }
 121 }