Search in sources :

Example 1 with MarshalException

use of java.rmi.MarshalException in project jdk8u_jdk by JetBrains.

the class HandshakeTimeout method main.

public static void main(String[] args) throws Exception {
    System.setProperty("sun.rmi.transport.tcp.handshakeTimeout", String.valueOf(TIMEOUT / 2));
    /*
         * Listen on port, but never process connections made to it.
         */
    ServerSocket serverSocket = new ServerSocket(PORT);
    /*
         * Attempt RMI call to port in separate thread.
         */
    Registry registry = LocateRegistry.getRegistry(PORT);
    Connector connector = new Connector(registry);
    Thread t = new Thread(connector);
    t.setDaemon(true);
    t.start();
    /*
         * Wait for call attempt to finished, and analyze result.
         */
    t.join(TIMEOUT);
    synchronized (connector) {
        if (connector.success) {
            throw new RuntimeException("TEST FAILED: remote call succeeded??");
        }
        if (connector.exception == null) {
            throw new RuntimeException("TEST FAILED: remote call did not time out");
        } else {
            System.err.println("remote call failed with exception:");
            connector.exception.printStackTrace();
            System.err.println();
            if (connector.exception instanceof MarshalException) {
                System.err.println("TEST FAILED: MarshalException thrown, expecting " + "java.rmi.ConnectException or ConnectIOException");
            } else if (connector.exception instanceof ConnectException || connector.exception instanceof ConnectIOException) {
                System.err.println("TEST PASSED: java.rmi.ConnectException or " + "ConnectIOException thrown");
            } else {
                throw new RuntimeException("TEST FAILED: unexpected Exception thrown", connector.exception);
            }
        }
    }
}
Also used : MarshalException(java.rmi.MarshalException) ConnectIOException(java.rmi.ConnectIOException) ServerSocket(java.net.ServerSocket) LocateRegistry(java.rmi.registry.LocateRegistry) Registry(java.rmi.registry.Registry) ConnectException(java.rmi.ConnectException)

Example 2 with MarshalException

use of java.rmi.MarshalException in project jdk8u_jdk by JetBrains.

the class PinLastArguments method main.

public static void main(String[] args) throws Exception {
    System.err.println("\nRegression test for bug 6332349\n");
    Ping impl = new PingImpl();
    Reference<?> ref = new WeakReference<Ping>(impl);
    try {
        Ping stub = (Ping) UnicastRemoteObject.exportObject(impl, 0);
        Object notSerializable = new Object();
        stub.ping(impl, null);
        try {
            stub.ping(impl, notSerializable);
        } catch (MarshalException e) {
            if (e.getCause() instanceof NotSerializableException) {
                System.err.println("ping invocation failed as expected");
            } else {
                throw e;
            }
        }
    } finally {
        UnicastRemoteObject.unexportObject(impl, true);
    }
    impl = null;
    // expected we will hang here until timed out by the test harness.
    while (true) {
        System.gc();
        Thread.sleep(20);
        if (ref.get() == null) {
            break;
        }
    }
    System.err.println("TEST PASSED");
}
Also used : MarshalException(java.rmi.MarshalException) NotSerializableException(java.io.NotSerializableException) WeakReference(java.lang.ref.WeakReference) UnicastRemoteObject(java.rmi.server.UnicastRemoteObject)

Example 3 with MarshalException

use of java.rmi.MarshalException in project jdk8u_jdk by JetBrains.

the class UnicastRef method invoke.

/**
     * Invoke a method. This form of delegating method invocation
     * to the reference allows the reference to take care of
     * setting up the connection to the remote host, marshalling
     * some representation for the method and parameters, then
     * communicating the method invocation to the remote host.
     * This method either returns the result of a method invocation
     * on the remote object which resides on the remote host or
     * throws a RemoteException if the call failed or an
     * application-level exception if the remote invocation throws
     * an exception.
     *
     * @param obj the proxy for the remote object
     * @param method the method to be invoked
     * @param params the parameter list
     * @param opnum  a hash that may be used to represent the method
     * @since 1.2
     */
public Object invoke(Remote obj, Method method, Object[] params, long opnum) throws Exception {
    if (clientRefLog.isLoggable(Log.VERBOSE)) {
        clientRefLog.log(Log.VERBOSE, "method: " + method);
    }
    if (clientCallLog.isLoggable(Log.VERBOSE)) {
        logClientCall(obj, method);
    }
    Connection conn = ref.getChannel().newConnection();
    RemoteCall call = null;
    boolean reuse = true;
    /* If the call connection is "reused" early, remember not to
         * reuse again.
         */
    boolean alreadyFreed = false;
    try {
        if (clientRefLog.isLoggable(Log.VERBOSE)) {
            clientRefLog.log(Log.VERBOSE, "opnum = " + opnum);
        }
        // create call context
        call = new StreamRemoteCall(conn, ref.getObjID(), -1, opnum);
        // marshal parameters
        try {
            ObjectOutput out = call.getOutputStream();
            marshalCustomCallData(out);
            Class<?>[] types = method.getParameterTypes();
            for (int i = 0; i < types.length; i++) {
                marshalValue(types[i], params[i], out);
            }
        } catch (IOException e) {
            clientRefLog.log(Log.BRIEF, "IOException marshalling arguments: ", e);
            throw new MarshalException("error marshalling arguments", e);
        }
        // unmarshal return
        call.executeCall();
        try {
            Class<?> rtype = method.getReturnType();
            if (rtype == void.class)
                return null;
            ObjectInput in = call.getInputStream();
            /* StreamRemoteCall.done() does not actually make use
                 * of conn, therefore it is safe to reuse this
                 * connection before the dirty call is sent for
                 * registered refs.
                 */
            Object returnValue = unmarshalValue(rtype, in);
            /* we are freeing the connection now, do not free
                 * again or reuse.
                 */
            alreadyFreed = true;
            /* if we got to this point, reuse must have been true. */
            clientRefLog.log(Log.BRIEF, "free connection (reuse = true)");
            /* Free the call's connection early. */
            ref.getChannel().free(conn, true);
            return returnValue;
        } catch (IOException e) {
            clientRefLog.log(Log.BRIEF, "IOException unmarshalling return: ", e);
            throw new UnmarshalException("error unmarshalling return", e);
        } catch (ClassNotFoundException e) {
            clientRefLog.log(Log.BRIEF, "ClassNotFoundException unmarshalling return: ", e);
            throw new UnmarshalException("error unmarshalling return", e);
        } finally {
            try {
                call.done();
            } catch (IOException e) {
                /* WARNING: If the conn has been reused early,
                     * then it is too late to recover from thrown
                     * IOExceptions caught here. This code is relying
                     * on StreamRemoteCall.done() not actually
                     * throwing IOExceptions.
                     */
                reuse = false;
            }
        }
    } catch (RuntimeException e) {
        /*
             * Need to distinguish between client (generated by the
             * invoke method itself) and server RuntimeExceptions.
             * Client side RuntimeExceptions are likely to have
             * corrupted the call connection and those from the server
             * are not likely to have done so.  If the exception came
             * from the server the call connection should be reused.
             */
        if ((call == null) || (((StreamRemoteCall) call).getServerException() != e)) {
            reuse = false;
        }
        throw e;
    } catch (RemoteException e) {
        /*
             * Some failure during call; assume connection cannot
             * be reused.  Must assume failure even if ServerException
             * or ServerError occurs since these failures can happen
             * during parameter deserialization which would leave
             * the connection in a corrupted state.
             */
        reuse = false;
        throw e;
    } catch (Error e) {
        /* If errors occurred, the connection is most likely not
             *  reusable.
             */
        reuse = false;
        throw e;
    } finally {
        /* alreadyFreed ensures that we do not log a reuse that
             * may have already happened.
             */
        if (!alreadyFreed) {
            if (clientRefLog.isLoggable(Log.BRIEF)) {
                clientRefLog.log(Log.BRIEF, "free connection (reuse = " + reuse + ")");
            }
            ref.getChannel().free(conn, reuse);
        }
    }
}
Also used : MarshalException(java.rmi.MarshalException) ObjectOutput(java.io.ObjectOutput) Connection(sun.rmi.transport.Connection) IOException(java.io.IOException) StreamRemoteCall(sun.rmi.transport.StreamRemoteCall) UnmarshalException(java.rmi.UnmarshalException) RemoteObject(java.rmi.server.RemoteObject) ObjectInput(java.io.ObjectInput) RemoteException(java.rmi.RemoteException) StreamRemoteCall(sun.rmi.transport.StreamRemoteCall) RemoteCall(java.rmi.server.RemoteCall)

Example 4 with MarshalException

use of java.rmi.MarshalException in project jdk8u_jdk by JetBrains.

the class UnicastServerRef method dispatch.

/**
     * Call to dispatch to the remote object (on the server side).
     * The up-call to the server and the marshalling of return result
     * (or exception) should be handled before returning from this
     * method.
     * @param obj the target remote object for the call
     * @param call the "remote call" from which operation and
     * method arguments can be obtained.
     * @exception IOException If unable to marshal return result or
     * release input or output streams
     */
public void dispatch(Remote obj, RemoteCall call) throws IOException {
    // positive operation number in 1.1 stubs;
    // negative version number in 1.2 stubs and beyond...
    int num;
    long op;
    try {
        // read remote call header
        ObjectInput in;
        try {
            in = call.getInputStream();
            num = in.readInt();
            if (num >= 0) {
                if (skel != null) {
                    oldDispatch(obj, call, num);
                    return;
                } else {
                    throw new UnmarshalException("skeleton class not found but required " + "for client version");
                }
            }
            op = in.readLong();
        } catch (Exception readEx) {
            throw new UnmarshalException("error unmarshalling call header", readEx);
        }
        /*
             * Since only system classes (with null class loaders) will be on
             * the execution stack during parameter unmarshalling for the 1.2
             * stub protocol, tell the MarshalInputStream not to bother trying
             * to resolve classes using its superclasses's default method of
             * consulting the first non-null class loader on the stack.
             */
        MarshalInputStream marshalStream = (MarshalInputStream) in;
        marshalStream.skipDefaultResolveClass();
        Method method = hashToMethod_Map.get(op);
        if (method == null) {
            throw new UnmarshalException("unrecognized method hash: " + "method not supported by remote object");
        }
        // if calls are being logged, write out object id and operation
        logCall(obj, method);
        // unmarshal parameters
        Object[] params = null;
        try {
            unmarshalCustomCallData(in);
            params = unmarshalParameters(obj, method, marshalStream);
        } catch (java.io.IOException e) {
            throw new UnmarshalException("error unmarshalling arguments", e);
        } catch (ClassNotFoundException e) {
            throw new UnmarshalException("error unmarshalling arguments", e);
        } finally {
            call.releaseInputStream();
        }
        // make upcall on remote object
        Object result;
        try {
            result = method.invoke(obj, params);
        } catch (InvocationTargetException e) {
            throw e.getTargetException();
        }
        // marshal return value
        try {
            ObjectOutput out = call.getResultStream(true);
            Class<?> rtype = method.getReturnType();
            if (rtype != void.class) {
                marshalValue(rtype, result, out);
            }
        } catch (IOException ex) {
            throw new MarshalException("error marshalling return", ex);
        /*
                 * This throw is problematic because when it is caught below,
                 * we attempt to marshal it back to the client, but at this
                 * point, a "normal return" has already been indicated,
                 * so marshalling an exception will corrupt the stream.
                 * This was the case with skeletons as well; there is no
                 * immediately obvious solution without a protocol change.
                 */
        }
    } catch (Throwable e) {
        logCallException(e);
        ObjectOutput out = call.getResultStream(false);
        if (e instanceof Error) {
            e = new ServerError("Error occurred in server thread", (Error) e);
        } else if (e instanceof RemoteException) {
            e = new ServerException("RemoteException occurred in server thread", (Exception) e);
        }
        if (suppressStackTraces) {
            clearStackTraces(e);
        }
        out.writeObject(e);
    } finally {
        // in case skeleton doesn't
        call.releaseInputStream();
        call.releaseOutputStream();
    }
}
Also used : MarshalException(java.rmi.MarshalException) ServerException(java.rmi.ServerException) ObjectOutput(java.io.ObjectOutput) ServerError(java.rmi.ServerError) ServerError(java.rmi.ServerError) IOException(java.io.IOException) Method(java.lang.reflect.Method) IOException(java.io.IOException) UnmarshalException(java.rmi.UnmarshalException) ServerException(java.rmi.ServerException) ExportException(java.rmi.server.ExportException) SkeletonNotFoundException(java.rmi.server.SkeletonNotFoundException) ServerNotActiveException(java.rmi.server.ServerNotActiveException) MarshalException(java.rmi.MarshalException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) RemoteException(java.rmi.RemoteException) InvocationTargetException(java.lang.reflect.InvocationTargetException) UnmarshalException(java.rmi.UnmarshalException) ObjectInput(java.io.ObjectInput) RemoteException(java.rmi.RemoteException)

Example 5 with MarshalException

use of java.rmi.MarshalException in project ysoserial by frohoff.

the class JRMPListener method doCall.

private void doCall(DataInputStream in, DataOutputStream out, Object payload) throws Exception {
    ObjectInputStream ois = new ObjectInputStream(in) {

        @Override
        protected Class<?> resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException {
            if ("[Ljava.rmi.server.ObjID;".equals(desc.getName())) {
                return ObjID[].class;
            } else if ("java.rmi.server.ObjID".equals(desc.getName())) {
                return ObjID.class;
            } else if ("java.rmi.server.UID".equals(desc.getName())) {
                return UID.class;
            }
            throw new IOException("Not allowed to read object");
        }
    };
    ObjID read;
    try {
        read = ObjID.read(ois);
    } catch (java.io.IOException e) {
        throw new MarshalException("unable to read objID", e);
    }
    if (read.hashCode() == 2) {
        // method
        ois.readInt();
        // hash
        ois.readLong();
        System.err.println("Is DGC call for " + Arrays.toString((ObjID[]) ois.readObject()));
    }
    System.err.println("Sending return with payload for obj " + read);
    // transport op
    out.writeByte(TransportConstants.Return);
    ObjectOutputStream oos = new JRMPClient.MarshalOutputStream(out, this.classpathUrl);
    oos.writeByte(TransportConstants.ExceptionalReturn);
    new UID().write(oos);
    BadAttributeValueExpException ex = new BadAttributeValueExpException(null);
    Reflections.setFieldValue(ex, "val", payload);
    oos.writeObject(ex);
    oos.flush();
    out.flush();
    this.hadConnection = true;
    synchronized (this.waitLock) {
        this.waitLock.notifyAll();
    }
}
Also used : UID(java.rmi.server.UID) MarshalException(java.rmi.MarshalException) ObjID(java.rmi.server.ObjID) IOException(java.io.IOException) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) ObjectStreamClass(java.io.ObjectStreamClass) BadAttributeValueExpException(javax.management.BadAttributeValueExpException) ObjectInputStream(java.io.ObjectInputStream)

Aggregations

MarshalException (java.rmi.MarshalException)9 IOException (java.io.IOException)6 RemoteException (java.rmi.RemoteException)4 ObjectOutput (java.io.ObjectOutput)3 ObjectInput (java.io.ObjectInput)2 ServerSocket (java.net.ServerSocket)2 ConnectException (java.rmi.ConnectException)2 ConnectIOException (java.rmi.ConnectIOException)2 UnmarshalException (java.rmi.UnmarshalException)2 LocateRegistry (java.rmi.registry.LocateRegistry)2 Registry (java.rmi.registry.Registry)2 ObjID (java.rmi.server.ObjID)2 RemoteCall (java.rmi.server.RemoteCall)2 ServerNotActiveException (java.rmi.server.ServerNotActiveException)2 Connection (sun.rmi.transport.Connection)2 StreamRemoteCall (sun.rmi.transport.StreamRemoteCall)2 FileNotFoundException (java.io.FileNotFoundException)1 NotSerializableException (java.io.NotSerializableException)1 ObjectInputStream (java.io.ObjectInputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1