test/java/util/logging/LogManagerAppContextDeadlock.java

Print this page




  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.lang.management.ManagementFactory;
  25 import java.lang.management.ThreadInfo;
  26 import java.security.CodeSource;
  27 import java.security.Permission;
  28 import java.security.PermissionCollection;
  29 import java.security.Permissions;
  30 import java.security.Policy;
  31 import java.security.ProtectionDomain;
  32 import java.util.Enumeration;
  33 import java.util.concurrent.Semaphore;
  34 import java.util.concurrent.atomic.AtomicBoolean;
  35 import java.util.concurrent.atomic.AtomicInteger;
  36 import java.util.logging.LogManager;
  37 import java.util.logging.Logger;


  38 
  39 /**
  40  * @test
  41  * @bug 8065991
  42  * @summary check that when LogManager is initialized, a deadlock similar
  43  *          to that described in 8065709 will not occur.
  44  * @modules java.base/sun.misc
  45  * @run main/othervm LogManagerAppContextDeadlock UNSECURE
  46  * @run main/othervm LogManagerAppContextDeadlock SECURE
  47  *
  48  * @author danielfuchs
  49  */
  50 public class LogManagerAppContextDeadlock {
  51 
  52     public static final Semaphore sem = new Semaphore(0);
  53     public static final Semaphore sem2 = new Semaphore(0);
  54     public static final Semaphore sem3 = new Semaphore(-2);
  55     public static volatile boolean goOn = true;
  56     public static volatile Exception thrown;
  57 
  58     // Emulate EventQueue
  59     static class FakeEventQueue {
  60         static final Logger logger = Logger.getLogger("foo");
  61     }
  62 
  63     // Emulate AppContext
  64     static class FakeAppContext {


  80                 // trigger a call to JavaAWTAccess - which will release
  81                 // sem, thus ensuring that Thread #2 is where we want it.
  82                 sem.acquire();
  83                 System.out.println("Sem acquired: Thread #2 has called JavaAWTAccess");
  84             } catch(InterruptedException x) {
  85                 Thread.interrupted();
  86             }
  87             queue = new FakeEventQueue();
  88         }
  89 
  90         static FakeAppContext getAppContext() {
  91             synchronized (lock) {
  92                 if (numAppContexts.get() == 0) {
  93                     return new FakeAppContext();
  94                 }
  95                 return appContext;
  96             }
  97         }
  98 
  99         static {
 100             sun.misc.SharedSecrets.setJavaAWTAccess(new sun.misc.JavaAWTAccess() {
 101                 @Override
 102                 public Object getAppletContext() {
 103                     if (numAppContexts.get() == 0) return null;
 104                     // We are in JavaAWTAccess, we can release sem and let
 105                     // FakeAppContext constructor proceeed.
 106                     System.out.println("Releasing Sem");
 107                     sem.release();
 108                     return getAppContext();
 109                 }
 110 
 111             });
 112         }
 113 
 114     }
 115 
 116 
 117     // Test with or without a security manager
 118     public static enum TestCase {
 119         UNSECURE, SECURE;
 120         public void run() throws Exception {


 324         }
 325         public Permissions toPermissions() {
 326             final PermissionsBuilder builder = new PermissionsBuilder();
 327             builder.addAll(perms);
 328             return builder.perms;
 329         }
 330     }
 331 
 332     // Policy for the test...
 333     public static class SimplePolicy extends Policy {
 334 
 335         final Permissions permissions;
 336         final Permissions allPermissions;
 337         final ThreadLocal<AtomicBoolean> allowAll; // actually: this should be in a thread locale
 338         public SimplePolicy(TestCase test, ThreadLocal<AtomicBoolean> allowAll) {
 339             this.allowAll = allowAll;
 340             // we don't actually need any permission to create our
 341             // FileHandlers because we're passing invalid parameters
 342             // which will make the creation fail...
 343             permissions = new Permissions();
 344             permissions.add(new RuntimePermission("accessClassInPackage.sun.misc"));
 345 
 346             // these are used for configuring the test itself...
 347             allPermissions = new Permissions();
 348             allPermissions.add(new java.security.AllPermission());
 349 
 350         }
 351 
 352         @Override
 353         public boolean implies(ProtectionDomain domain, Permission permission) {
 354             if (allowAll.get().get()) return allPermissions.implies(permission);
 355             return permissions.implies(permission);
 356         }
 357 
 358         @Override
 359         public PermissionCollection getPermissions(CodeSource codesource) {
 360             return new PermissionsBuilder().addAll(allowAll.get().get()
 361                     ? allPermissions : permissions).toPermissions();
 362         }
 363 
 364         @Override


  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.lang.management.ManagementFactory;
  25 import java.lang.management.ThreadInfo;
  26 import java.security.CodeSource;
  27 import java.security.Permission;
  28 import java.security.PermissionCollection;
  29 import java.security.Permissions;
  30 import java.security.Policy;
  31 import java.security.ProtectionDomain;
  32 import java.util.Enumeration;
  33 import java.util.concurrent.Semaphore;
  34 import java.util.concurrent.atomic.AtomicBoolean;
  35 import java.util.concurrent.atomic.AtomicInteger;
  36 import java.util.logging.LogManager;
  37 import java.util.logging.Logger;
  38 import jdk.internal.misc.JavaAWTAccess;
  39 import jdk.internal.misc.SharedSecrets;
  40 
  41 /**
  42  * @test
  43  * @bug 8065991
  44  * @summary check that when LogManager is initialized, a deadlock similar
  45  *          to that described in 8065709 will not occur.
  46  * @modules java.base/jdk.internal.misc
  47  * @run main/othervm LogManagerAppContextDeadlock UNSECURE
  48  * @run main/othervm LogManagerAppContextDeadlock SECURE
  49  *
  50  * @author danielfuchs
  51  */
  52 public class LogManagerAppContextDeadlock {
  53 
  54     public static final Semaphore sem = new Semaphore(0);
  55     public static final Semaphore sem2 = new Semaphore(0);
  56     public static final Semaphore sem3 = new Semaphore(-2);
  57     public static volatile boolean goOn = true;
  58     public static volatile Exception thrown;
  59 
  60     // Emulate EventQueue
  61     static class FakeEventQueue {
  62         static final Logger logger = Logger.getLogger("foo");
  63     }
  64 
  65     // Emulate AppContext
  66     static class FakeAppContext {


  82                 // trigger a call to JavaAWTAccess - which will release
  83                 // sem, thus ensuring that Thread #2 is where we want it.
  84                 sem.acquire();
  85                 System.out.println("Sem acquired: Thread #2 has called JavaAWTAccess");
  86             } catch(InterruptedException x) {
  87                 Thread.interrupted();
  88             }
  89             queue = new FakeEventQueue();
  90         }
  91 
  92         static FakeAppContext getAppContext() {
  93             synchronized (lock) {
  94                 if (numAppContexts.get() == 0) {
  95                     return new FakeAppContext();
  96                 }
  97                 return appContext;
  98             }
  99         }
 100 
 101         static {
 102             SharedSecrets.setJavaAWTAccess(new JavaAWTAccess() {
 103                 @Override
 104                 public Object getAppletContext() {
 105                     if (numAppContexts.get() == 0) return null;
 106                     // We are in JavaAWTAccess, we can release sem and let
 107                     // FakeAppContext constructor proceeed.
 108                     System.out.println("Releasing Sem");
 109                     sem.release();
 110                     return getAppContext();
 111                 }
 112 
 113             });
 114         }
 115 
 116     }
 117 
 118 
 119     // Test with or without a security manager
 120     public static enum TestCase {
 121         UNSECURE, SECURE;
 122         public void run() throws Exception {


 326         }
 327         public Permissions toPermissions() {
 328             final PermissionsBuilder builder = new PermissionsBuilder();
 329             builder.addAll(perms);
 330             return builder.perms;
 331         }
 332     }
 333 
 334     // Policy for the test...
 335     public static class SimplePolicy extends Policy {
 336 
 337         final Permissions permissions;
 338         final Permissions allPermissions;
 339         final ThreadLocal<AtomicBoolean> allowAll; // actually: this should be in a thread locale
 340         public SimplePolicy(TestCase test, ThreadLocal<AtomicBoolean> allowAll) {
 341             this.allowAll = allowAll;
 342             // we don't actually need any permission to create our
 343             // FileHandlers because we're passing invalid parameters
 344             // which will make the creation fail...
 345             permissions = new Permissions();
 346             permissions.add(new RuntimePermission("accessClassInPackage.jdk.internal.misc"));
 347 
 348             // these are used for configuring the test itself...
 349             allPermissions = new Permissions();
 350             allPermissions.add(new java.security.AllPermission());
 351 
 352         }
 353 
 354         @Override
 355         public boolean implies(ProtectionDomain domain, Permission permission) {
 356             if (allowAll.get().get()) return allPermissions.implies(permission);
 357             return permissions.implies(permission);
 358         }
 359 
 360         @Override
 361         public PermissionCollection getPermissions(CodeSource codesource) {
 362             return new PermissionsBuilder().addAll(allowAll.get().get()
 363                     ? allPermissions : permissions).toPermissions();
 364         }
 365 
 366         @Override