1 /*
   2  * Copyright (c) 1998, 2014, 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 /* @test
  25  * @bug 4105080 8085192
  26  * @summary Activation retry during a remote method call to an activatable
  27  *          object can cause infinite recursion in some situations. The
  28  *          RemoteRef contained in the ActivatableRef should never be
  29  *          an ActivatableRef, but another type.
  30  * (Needs /othervm to evade JavaTest security manager --aecolley)
  31  * @author Ann Wollrath
  32  *
  33  * @bug 4164971
  34  * @summary allow non-public activatable class and/or constructor
  35  *          Main test class hasa non-public constructor to ensure
  36  *          functionality is in place
  37  *
  38  * @library ../../../testlibrary
  39  * @modules java.rmi/sun.rmi.registry
  40  *          java.rmi/sun.rmi.server
  41  *          java.rmi/sun.rmi.transport
  42  *          java.rmi/sun.rmi.transport.tcp
  43  * @build TestLibrary RMID ActivateMe CheckActivateRef_Stub
  44  * @run main/othervm/policy=security.policy/timeout=240 -Djava.rmi.server.ignoreStubClasses=true CheckActivateRef
  45  * @run main/othervm/policy=security.policy/timeout=240 -Djava.rmi.server.ignoreStubClasses=false CheckActivateRef
  46  * @key intermittent
  47  */
  48 
  49 import java.io.*;
  50 import java.rmi.*;
  51 import java.rmi.server.*;
  52 import java.rmi.activation.*;
  53 import sun.rmi.server.ActivatableRef;
  54 import java.lang.reflect.*;
  55 import java.util.Properties;
  56 
  57 public class CheckActivateRef
  58         extends Activatable
  59         implements ActivateMe, Runnable
  60 {
  61 
  62     private CheckActivateRef(ActivationID id, MarshalledObject obj)
  63         throws ActivationException, RemoteException
  64     {
  65         super(id, 0);
  66     }
  67 
  68     public void ping()
  69     {}
  70 
  71     /**
  72      * Spawns a thread to deactivate the object.
  73      */
  74     public void shutdown() throws Exception
  75     {
  76         (new Thread(this,"CheckActivateRef")).start();
  77     }
  78 
  79     /**
  80      * Thread to deactivate object. First attempts to make object
  81      * inactive (via the inactive method).  If that fails (the
  82      * object may still have pending/executing calls), then
  83      * unexport the object forcibly.
  84      */
  85     public void run() {
  86         ActivationLibrary.deactivate(this, getID());
  87     }
  88 
  89     public static void main(String[] args)  {
  90         /*
  91          * The following line is required with the JDK 1.2 VM so that the
  92          * VM can exit gracefully when this test completes.  Otherwise, the
  93          * conservative garbage collector will find a handle to the server
  94          * object on the native stack and not clear the weak reference to
  95          * it in the RMI runtime's object table.
  96          */
  97         Object dummy = new Object();
  98         RMID rmid = null;
  99         ActivateMe obj;
 100 
 101         // test should tolerate certain types of failures
 102         int failures = 0;
 103         int i = 0;
 104 
 105         System.err.println("\nRegression test for bug 4105080\n");
 106         System.err.println("java.security.policy = " +
 107                            System.getProperty("java.security.policy",
 108                                               "no policy"));
 109 
 110 
 111         String propValue =
 112             System.getProperty("java.rmi.server.useDynamicProxies", "false");
 113         boolean useDynamicProxies = Boolean.parseBoolean(propValue);
 114 
 115         CheckActivateRef server;
 116         try {
 117             TestLibrary.suggestSecurityManager(TestParams.defaultSecurityManager);
 118 
 119             // start an rmid.
 120             rmid = RMID.launch();


 121 
 122             /* Cause activation groups to have a security policy that will
 123              * allow security managers to be downloaded and installed
 124              */
 125             Properties p = new Properties();
 126             // this test must always set policies/managers in its
 127             // activation groups
 128             p.put("java.security.policy",
 129                   TestParams.defaultGroupPolicy);
 130             p.put("java.security.manager",
 131                   TestParams.defaultSecurityManager);
 132             p.put("java.rmi.server.useDynamicProxies", propValue);
 133 
 134             /*
 135              * Activate an object by registering its object
 136              * descriptor and invoking a method on the
 137              * stub returned from the register call.
 138              */
 139             System.err.println("Create activation group in this VM");
 140             ActivationGroupDesc groupDesc =
 141                 new ActivationGroupDesc(p, null);
 142             ActivationSystem system = ActivationGroup.getSystem();
 143             ActivationGroupID groupID = system.registerGroup(groupDesc);
 144             ActivationGroup.createGroup(groupID, groupDesc, 0);
 145             System.err.println("Creating descriptor");
 146             ActivationDesc desc =
 147                 new ActivationDesc("CheckActivateRef", null, null);
 148             System.err.println("Registering descriptor");
 149             obj = (ActivateMe) Activatable.register(desc);
 150 
 151             System.err.println("proxy = " + obj);
 152 
 153             if (useDynamicProxies && !Proxy.isProxyClass(obj.getClass()))
 154             {
 155                 throw new RuntimeException("proxy is not dynamic proxy");
 156             }
 157 
 158             /*
 159              * Loop a bunch of times to force activator to
 160              * spawn VMs (groups)
 161              */
 162             try {
 163                 for (; i < 7; i++) {
 164 
 165                     System.err.println("Activate object via method call");
 166 
 167                     /*
 168                      * Fix for 4277196: if we got an inactive group
 169                      * exception, it is likely that we accidentally
 170                      * invoked a method on an old activation
 171                      * group. Give some time for the group to go away
 172                      * and then retry the activation.
 173                      */
 174                     try {
 175                         obj.ping();
 176                     } catch (RemoteException e) {
 177                         Exception detail = (Exception) e.detail;
 178                         if ((detail != null) &&
 179                             (detail instanceof ActivationException) &&
 180                             (detail.getMessage().equals("group is inactive")))
 181                         {
 182                             try {
 183                                 Thread.sleep(5000);
 184                             } catch (InterruptedException ex) {
 185                             }
 186                             obj.ping();
 187 
 188                         } else {
 189                             throw e;
 190                         }
 191                     }
 192 
 193                     System.err.println("proxy = " + obj);
 194 
 195                     /*
 196                      * Now that object is activated, check to make sure that
 197                      * the RemoteRef inside the stub's ActivatableRef
 198                      * is *not* an ActivatableRef.
 199                      */
 200                     ActivatableRef aref;
 201                     if (obj instanceof RemoteStub) {
 202                         aref = (ActivatableRef) ((RemoteObject) obj).getRef();
 203                     } else if (Proxy.isProxyClass(obj.getClass())) {
 204                         RemoteObjectInvocationHandler handler =
 205                             (RemoteObjectInvocationHandler)
 206                             Proxy.getInvocationHandler(obj);
 207                         aref = (ActivatableRef) handler.getRef();
 208                     } else {
 209                         throw new RuntimeException("unknown proxy type");
 210                     }
 211 
 212                     final ActivatableRef ref = aref;
 213                     Field f = (Field)
 214                         java.security.AccessController.doPrivileged
 215                         (new java.security.PrivilegedExceptionAction() {
 216                             public Object run() throws Exception {
 217                                 Field ff = ref.getClass().getDeclaredField("ref");
 218                                 ff.setAccessible(true);
 219                                 return ff;
 220                             }
 221                         });
 222                     Object insideRef = f.get(ref);
 223                     System.err.println("insideRef = " + insideRef);
 224                     if (insideRef instanceof ActivatableRef) {
 225                         TestLibrary.bomb("Embedded ref is an ActivatableRef");
 226                     } else {
 227                         System.err.println("ActivatableRef's embedded ref type: " +
 228                                            insideRef.getClass().getName());
 229                     }
 230 
 231                     /*
 232                      * Clean up object too.
 233                      */
 234                     System.err.println("Deactivate object via method call");
 235                     obj.shutdown();
 236 
 237                     try {
 238                         // give activation group time to go away
 239                         Thread.sleep(3000);
 240                     } catch (InterruptedException e) {
 241                     }
 242                 }
 243             } catch (java.rmi.UnmarshalException ue) {
 244                 // account for test's activation race condition
 245                 if (ue.detail instanceof java.io.IOException) {
 246                     if ((failures ++) >= 3) {
 247                         throw ue;
 248                     }
 249                 } else {
 250                     throw ue;
 251                 }
 252             }
 253 
 254             System.err.println("\nsuccess: CheckActivateRef test passed ");
 255 
 256         } catch (java.rmi.activation.ActivationException e) {
 257             // test only needs to pass 3 times in 7
 258             if (i < 4) {
 259                 TestLibrary.bomb(e);
 260             }
 261         } catch (Exception e) {
 262             if (e instanceof java.security.PrivilegedActionException)
 263                 e = ((java.security.PrivilegedActionException)e).getException();
 264             TestLibrary.bomb("\nfailure: unexpected exception " +
 265                              e.getClass().getName(), e);
 266 
 267         } finally {
 268             rmid.cleanup();
 269             obj = null;
 270         }
 271     }
 272 }
--- EOF ---