Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/rmi/activation/Activatable/checkActivateRef/CheckActivateRef.java
38829 views
/*1* Copyright (c) 1998, 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 410508025* @summary Activation retry during a remote method call to an activatable26* object can cause infinite recursion in some situations. The27* RemoteRef contained in the ActivatableRef should never be28* an ActivatableRef, but another type.29* (Needs /othervm to evade JavaTest security manager --aecolley)30* @author Ann Wollrath31*32* @bug 416497133* @summary allow non-public activatable class and/or constructor34* Main test class hasa non-public constructor to ensure35* functionality is in place36*37* @library ../../../testlibrary38* @build TestLibrary RMID ActivateMe CheckActivateRef_Stub39* @run main/othervm/policy=security.policy/timeout=240 -Djava.rmi.server.ignoreStubClasses=true CheckActivateRef40* @run main/othervm/policy=security.policy/timeout=240 -Djava.rmi.server.ignoreStubClasses=false CheckActivateRef41*/4243import java.io.*;44import java.rmi.*;45import java.rmi.server.*;46import java.rmi.activation.*;47import sun.rmi.server.ActivatableRef;48import java.lang.reflect.*;49import java.util.Properties;5051public class CheckActivateRef52extends Activatable53implements ActivateMe, Runnable54{5556private CheckActivateRef(ActivationID id, MarshalledObject obj)57throws ActivationException, RemoteException58{59super(id, 0);60}6162public void ping()63{}6465/**66* Spawns a thread to deactivate the object.67*/68public void shutdown() throws Exception69{70(new Thread(this,"CheckActivateRef")).start();71}7273/**74* Thread to deactivate object. First attempts to make object75* inactive (via the inactive method). If that fails (the76* object may still have pending/executing calls), then77* unexport the object forcibly.78*/79public void run() {80ActivationLibrary.deactivate(this, getID());81}8283public static void main(String[] args) {84/*85* The following line is required with the JDK 1.2 VM so that the86* VM can exit gracefully when this test completes. Otherwise, the87* conservative garbage collector will find a handle to the server88* object on the native stack and not clear the weak reference to89* it in the RMI runtime's object table.90*/91Object dummy = new Object();92RMID rmid = null;93ActivateMe obj;9495// test should tolerate certain types of failures96int failures = 0;97int i = 0;9899System.err.println("\nRegression test for bug 4105080\n");100System.err.println("java.security.policy = " +101System.getProperty("java.security.policy",102"no policy"));103104105String propValue =106System.getProperty("java.rmi.server.useDynamicProxies", "false");107boolean useDynamicProxies = Boolean.parseBoolean(propValue);108109CheckActivateRef server;110try {111TestLibrary.suggestSecurityManager(TestParams.defaultSecurityManager);112113// start an rmid.114RMID.removeLog();115rmid = RMID.createRMID();116rmid.start();117118/* Cause activation groups to have a security policy that will119* allow security managers to be downloaded and installed120*/121Properties p = new Properties();122// this test must always set policies/managers in its123// activation groups124p.put("java.security.policy",125TestParams.defaultGroupPolicy);126p.put("java.security.manager",127TestParams.defaultSecurityManager);128p.put("java.rmi.server.useDynamicProxies", propValue);129130/*131* Activate an object by registering its object132* descriptor and invoking a method on the133* stub returned from the register call.134*/135System.err.println("Create activation group in this VM");136ActivationGroupDesc groupDesc =137new ActivationGroupDesc(p, null);138ActivationSystem system = ActivationGroup.getSystem();139ActivationGroupID groupID = system.registerGroup(groupDesc);140ActivationGroup.createGroup(groupID, groupDesc, 0);141System.err.println("Creating descriptor");142ActivationDesc desc =143new ActivationDesc("CheckActivateRef", null, null);144System.err.println("Registering descriptor");145obj = (ActivateMe) Activatable.register(desc);146147System.err.println("proxy = " + obj);148149if (useDynamicProxies && !Proxy.isProxyClass(obj.getClass()))150{151throw new RuntimeException("proxy is not dynamic proxy");152}153154/*155* Loop a bunch of times to force activator to156* spawn VMs (groups)157*/158try {159for (; i < 7; i++) {160161System.err.println("Activate object via method call");162163/*164* Fix for 4277196: if we got an inactive group165* exception, it is likely that we accidentally166* invoked a method on an old activation167* group. Give some time for the group to go away168* and then retry the activation.169*/170try {171obj.ping();172} catch (RemoteException e) {173Exception detail = (Exception) e.detail;174if ((detail != null) &&175(detail instanceof ActivationException) &&176(detail.getMessage().equals("group is inactive")))177{178try {179Thread.sleep(5000);180} catch (InterruptedException ex) {181}182obj.ping();183184} else {185throw e;186}187}188189System.err.println("proxy = " + obj);190191/*192* Now that object is activated, check to make sure that193* the RemoteRef inside the stub's ActivatableRef194* is *not* an ActivatableRef.195*/196ActivatableRef aref;197if (obj instanceof RemoteStub) {198aref = (ActivatableRef) ((RemoteObject) obj).getRef();199} else if (Proxy.isProxyClass(obj.getClass())) {200RemoteObjectInvocationHandler handler =201(RemoteObjectInvocationHandler)202Proxy.getInvocationHandler(obj);203aref = (ActivatableRef) handler.getRef();204} else {205throw new RuntimeException("unknown proxy type");206}207208final ActivatableRef ref = aref;209Field f = (Field)210java.security.AccessController.doPrivileged211(new java.security.PrivilegedExceptionAction() {212public Object run() throws Exception {213Field ff = ref.getClass().getDeclaredField("ref");214ff.setAccessible(true);215return ff;216}217});218Object insideRef = f.get(ref);219System.err.println("insideRef = " + insideRef);220if (insideRef instanceof ActivatableRef) {221TestLibrary.bomb("Embedded ref is an ActivatableRef");222} else {223System.err.println("ActivatableRef's embedded ref type: " +224insideRef.getClass().getName());225}226227/*228* Clean up object too.229*/230System.err.println("Deactivate object via method call");231obj.shutdown();232233try {234// give activation group time to go away235Thread.sleep(3000);236} catch (InterruptedException e) {237}238}239} catch (java.rmi.UnmarshalException ue) {240// account for test's activation race condition241if (ue.detail instanceof java.io.IOException) {242if ((failures ++) >= 3) {243throw ue;244}245} else {246throw ue;247}248}249250System.err.println("\nsuccess: CheckActivateRef test passed ");251252} catch (java.rmi.activation.ActivationException e) {253// test only needs to pass 3 times in 7254if (i < 4) {255TestLibrary.bomb(e);256}257} catch (Exception e) {258if (e instanceof java.security.PrivilegedActionException)259e = ((java.security.PrivilegedActionException)e).getException();260TestLibrary.bomb("\nfailure: unexpected exception " +261e.getClass().getName(), e);262263} finally {264ActivationLibrary.rmidCleanup(rmid);265obj = null;266}267}268}269270271