SerializeMessage() static private method

static private SerializeMessage ( IMessage msg ) : MemoryStream
msg IMessage
return System.IO.MemoryStream
        internal byte[] DoDispatch(byte[] reqStmBuff,
                                   SmuggledMethodCallMessage smuggledMcm,
                                   out SmuggledMethodReturnMessage smuggledMrm)
        {
            RemotingServices.LogRemotingStage(RemotingServices.SERVER_MSG_DESER);

            //*********************** DE-SERIALIZE REQ-MSG ********************

            IMessage desReqMsg = null;

            if (smuggledMcm != null)
            {
                ArrayList deserializedArgs = smuggledMcm.FixupForNewAppDomain();
                desReqMsg = new MethodCall(smuggledMcm, deserializedArgs);
            }
            else
            {
                MemoryStream reqStm = new MemoryStream(reqStmBuff);
                desReqMsg = CrossAppDomainSerializer.DeserializeMessage(reqStm);
            }

            // now we can delegate to the DispatchMessage to do the rest

            RemotingServices.LogRemotingStage(RemotingServices.SERVER_MSG_SINK_CHAIN);

            IMessage retMsg = ChannelServices.SyncDispatchMessage(desReqMsg);

            RemotingServices.LogRemotingStage(RemotingServices.SERVER_RET_SER);

            smuggledMrm = SmuggledMethodReturnMessage.SmuggleIfPossible(retMsg);
            if (smuggledMrm != null)
            {
                return(null);
            }
            else
            {
                if (retMsg != null)
                {
                    // Null out the principal since we won't use it on the other side.
                    // This is handled inside of SmuggleIfPossible for method call
                    // messages.
                    LogicalCallContext callCtx = (LogicalCallContext)
                                                 retMsg.Properties[Message.CallContextKey];
                    if (callCtx != null)
                    {
                        if (callCtx.Principal != null)
                        {
                            callCtx.Principal = null;
                        }
                    }
                    return(CrossAppDomainSerializer.SerializeMessage(retMsg).GetBuffer());
                }

                //*********************** SERIALIZE RET-MSG ********************
                return(null);
            }
        } // DoDispatch
        [System.Security.SecurityCritical]  // auto-generated
        internal static byte[] DoDispatch(byte[] reqStmBuff,
                                          SmuggledMethodCallMessage smuggledMcm,
                                          out SmuggledMethodReturnMessage smuggledMrm)
        {
            //*********************** DE-SERIALIZE REQ-MSG ********************

            IMessage desReqMsg = null;

            if (smuggledMcm != null)
            {
                ArrayList deserializedArgs = smuggledMcm.FixupForNewAppDomain();
                desReqMsg = new MethodCall(smuggledMcm, deserializedArgs);
            }
            else
            {
                MemoryStream reqStm = new MemoryStream(reqStmBuff);
                desReqMsg = CrossAppDomainSerializer.DeserializeMessage(reqStm);
            }

            LogicalCallContext lcc = Thread.CurrentThread.GetMutableExecutionContext().LogicalCallContext;

            lcc.SetData(LCC_DATA_KEY, true);
            // now we can delegate to the DispatchMessage to do the rest

            IMessage retMsg = ChannelServices.SyncDispatchMessage(desReqMsg);

            lcc.FreeNamedDataSlot(LCC_DATA_KEY);

            smuggledMrm = SmuggledMethodReturnMessage.SmuggleIfPossible(retMsg);
            if (smuggledMrm != null)
            {
                return(null);
            }
            else
            {
                if (retMsg != null)
                {
                    // Null out the principal since we won't use it on the other side.
                    // This is handled inside of SmuggleIfPossible for method call
                    // messages.
                    LogicalCallContext callCtx = (LogicalCallContext)
                                                 retMsg.Properties[Message.CallContextKey];
                    if (callCtx != null)
                    {
                        if (callCtx.Principal != null)
                        {
                            callCtx.Principal = null;
                        }
                    }

                    return(CrossAppDomainSerializer.SerializeMessage(retMsg).GetBuffer());
                }

                //*********************** SERIALIZE RET-MSG ********************
                return(null);
            }
        } // DoDispatch
        } // DoDispatch

        internal byte[] DoTransitionDispatch(
            byte[] reqStmBuff,
            SmuggledMethodCallMessage smuggledMcm,
            out SmuggledMethodReturnMessage smuggledMrm)
        {
            // Note: To be safe w.r.t. the app domain leak code, this frame
            // should have no non-agile references in it.

            ContextTransitionFrame frame = new ContextTransitionFrame();

            // change to server domain
            Thread.CurrentThread.EnterContextInternal(
                null,
                _srvContextID,
                _srvDomainID,
                ref frame);

            byte[] retBuff = null;
            smuggledMrm = null;
            try
            {
                Message.DebugOut("#### : changed to Server Domain :: " + (Thread.CurrentContext.InternalContextID).ToString("X"));

                retBuff = DoDispatch(reqStmBuff, smuggledMcm, out smuggledMrm);
            }
            catch (Exception e)
            {
                // This will catch exceptions thrown by the infrastructure,
                // Serialization/Deserialization etc
                // Those thrown by the server are already taken care of
                // and encoded in the retMsg .. so we don't come here for
                // that case.

                // We are in another appDomain, so we can't simply throw
                // the exception object across. The following marshals it
                // into a serialized return message.
                IMessage retMsg =
                    new ReturnMessage(e, new ErrorMessage());
                //*********************** SERIALIZE RET-MSG ******************
                retBuff = CrossAppDomainSerializer.SerializeMessage(retMsg).GetBuffer();
                retMsg  = null;
            }
            finally
            {
                RemotingServices.LogRemotingStage(RemotingServices.SERVER_RET_SEND);
                Thread.CurrentThread.ReturnToContext(ref frame);
                Message.DebugOut("#### : changed back to Client Domain " + (Thread.CurrentContext.InternalContextID).ToString("X"));
            }

            // System.Diagnostics.Debugger.Break();
            return(retBuff);
        } // DoTransitionDispatch
Example #4
0
        internal static object DoTransitionDispatchCallback(object[] args)
        {
            byte[] reqStmBuff = (byte[])args[0];
            SmuggledMethodCallMessage   smuggledMcm = (SmuggledMethodCallMessage)args[1];
            SmuggledMethodReturnMessage smuggledMethodReturnMessage = null;

            byte[] result = null;
            try
            {
                result = CrossAppDomainSink.DoDispatch(reqStmBuff, smuggledMcm, out smuggledMethodReturnMessage);
            }
            catch (Exception e)
            {
                IMessage msg = new ReturnMessage(e, new ErrorMessage());
                result = CrossAppDomainSerializer.SerializeMessage(msg).GetBuffer();
            }
            args[2] = smuggledMethodReturnMessage;
            return(result);
        }
Example #5
0
        internal static object DoTransitionDispatchCallback(object[] args)
        {
            byte[] reqStmBuff = (byte[])args[0];
            SmuggledMethodCallMessage   smuggledMcm = (SmuggledMethodCallMessage)args[1];
            SmuggledMethodReturnMessage smuggledMrm = (SmuggledMethodReturnMessage)null;

            byte[] numArray;
            try
            {
                numArray = CrossAppDomainSink.DoDispatch(reqStmBuff, smuggledMcm, out smuggledMrm);
            }
            catch (Exception ex)
            {
                ErrorMessage errorMessage = new ErrorMessage();
                numArray = CrossAppDomainSerializer.SerializeMessage((IMessage) new ReturnMessage(ex, (IMethodCallMessage)errorMessage)).GetBuffer();
            }
            args[2] = (object)smuggledMrm;
            return((object)numArray);
        }
Example #6
0
        internal static object DoTransitionDispatchCallback(object[] args)
        {
            byte[] reqStmBuff = (byte[])args[0];
            SmuggledMethodCallMessage   smuggledMcm = (SmuggledMethodCallMessage)args[1];
            SmuggledMethodReturnMessage smuggledMrm = null;

            byte[] buffer = null;
            try
            {
                buffer = DoDispatch(reqStmBuff, smuggledMcm, out smuggledMrm);
            }
            catch (Exception exception)
            {
                IMessage msg = new ReturnMessage(exception, new ErrorMessage());
                buffer = CrossAppDomainSerializer.SerializeMessage(msg).GetBuffer();
                msg    = null;
            }
            args[2] = smuggledMrm;
            return(buffer);
        }
Example #7
0
        internal static byte[] DoDispatch(byte[] reqStmBuff, SmuggledMethodCallMessage smuggledMcm, out SmuggledMethodReturnMessage smuggledMrm)
        {
            IMessage msg1;

            if (smuggledMcm != null)
            {
                ArrayList deserializedArgs = smuggledMcm.FixupForNewAppDomain();
                msg1 = (IMessage) new MethodCall(smuggledMcm, deserializedArgs);
            }
            else
            {
                msg1 = CrossAppDomainSerializer.DeserializeMessage(new MemoryStream(reqStmBuff));
            }
            LogicalCallContext logicalCallContext1 = Thread.CurrentThread.GetMutableExecutionContext().LogicalCallContext;
            string             name1 = "__xADCall";
            // ISSUE: variable of a boxed type
            __Boxed <bool> local = (ValueType)true;

            logicalCallContext1.SetData(name1, (object)local);
            IMessage msg2  = ChannelServices.SyncDispatchMessage(msg1);
            string   name2 = "__xADCall";

            logicalCallContext1.FreeNamedDataSlot(name2);
            smuggledMrm = SmuggledMethodReturnMessage.SmuggleIfPossible(msg2);
            if (smuggledMrm != null)
            {
                return((byte[])null);
            }
            if (msg2 == null)
            {
                return((byte[])null);
            }
            LogicalCallContext logicalCallContext2 = (LogicalCallContext)msg2.Properties[(object)Message.CallContextKey];

            if (logicalCallContext2 != null && logicalCallContext2.Principal != null)
            {
                logicalCallContext2.Principal = (IPrincipal)null;
            }
            return(CrossAppDomainSerializer.SerializeMessage(msg2).GetBuffer());
        }
        [System.Security.SecurityCritical]  // auto-generated
        internal static Object DoTransitionDispatchCallback(Object[] args)
        {
            byte[] reqStmBuff = (byte[])args[0];
            SmuggledMethodCallMessage   smuggledMcm = (SmuggledMethodCallMessage)args[1];
            SmuggledMethodReturnMessage smuggledMrm = null;

            byte[] retBuff = null;

            try
            {
#if !FEATURE_CORECLR
                Message.DebugOut("#### : changed to Server Domain :: " + (Thread.CurrentContext.InternalContextID).ToString("X"));
#endif
                retBuff = DoDispatch(reqStmBuff, smuggledMcm, out smuggledMrm);
            }
            catch (Exception e)
            {
                // This will catch exceptions thrown by the infrastructure,
                // Serialization/Deserialization etc
                // Those thrown by the server are already taken care of
                // and encoded in the retMsg .. so we don't come here for
                // that case.

                // We are in another appDomain, so we can't simply throw
                // the exception object across. The following marshals it
                // into a serialized return message.
                IMessage retMsg =
                    new ReturnMessage(e, new ErrorMessage());
                //*********************** SERIALIZE RET-MSG ******************
                retBuff = CrossAppDomainSerializer.SerializeMessage(retMsg).GetBuffer();
                retMsg  = null;
            }

            args[2] = smuggledMrm;

            return(retBuff);
        }
Example #9
0
        internal static byte[] DoDispatch(byte[] reqStmBuff, SmuggledMethodCallMessage smuggledMcm, out SmuggledMethodReturnMessage smuggledMrm)
        {
            IMessage msg = null;

            if (smuggledMcm != null)
            {
                ArrayList deserializedArgs = smuggledMcm.FixupForNewAppDomain();
                msg = new MethodCall(smuggledMcm, deserializedArgs);
            }
            else
            {
                MemoryStream stm = new MemoryStream(reqStmBuff);
                msg = CrossAppDomainSerializer.DeserializeMessage(stm);
            }
            LogicalCallContext logicalCallContext = CallContext.GetLogicalCallContext();

            logicalCallContext.SetData("__xADCall", true);
            IMessage message2 = ChannelServices.SyncDispatchMessage(msg);

            logicalCallContext.FreeNamedDataSlot("__xADCall");
            smuggledMrm = SmuggledMethodReturnMessage.SmuggleIfPossible(message2);
            if (smuggledMrm != null)
            {
                return(null);
            }
            if (message2 == null)
            {
                return(null);
            }
            LogicalCallContext context2 = (LogicalCallContext)message2.Properties[Message.CallContextKey];

            if ((context2 != null) && (context2.Principal != null))
            {
                context2.Principal = null;
            }
            return(CrossAppDomainSerializer.SerializeMessage(message2).GetBuffer());
        }
Example #10
0
        public virtual IMessage SyncProcessMessage(IMessage reqMsg)
        {
            IMessage message = InternalSink.ValidateMessage(reqMsg);

            if (message != null)
            {
                return(message);
            }
            IPrincipal principal = null;
            IMessage   message2  = null;

            try
            {
                IMethodCallMessage methodCallMessage = reqMsg as IMethodCallMessage;
                if (methodCallMessage != null)
                {
                    LogicalCallContext logicalCallContext = methodCallMessage.LogicalCallContext;
                    if (logicalCallContext != null)
                    {
                        principal = logicalCallContext.RemovePrincipalIfNotSerializable();
                    }
                }
                MemoryStream memoryStream = null;
                SmuggledMethodCallMessage smuggledMethodCallMessage = SmuggledMethodCallMessage.SmuggleIfPossible(reqMsg);
                if (smuggledMethodCallMessage == null)
                {
                    memoryStream = CrossAppDomainSerializer.SerializeMessage(reqMsg);
                }
                LogicalCallContext          logicalCallContext2 = CallContext.SetLogicalCallContext(null);
                byte[]                      array = null;
                SmuggledMethodReturnMessage smuggledMethodReturnMessage;
                try
                {
                    if (smuggledMethodCallMessage != null)
                    {
                        array = this.DoTransitionDispatch(null, smuggledMethodCallMessage, out smuggledMethodReturnMessage);
                    }
                    else
                    {
                        array = this.DoTransitionDispatch(memoryStream.GetBuffer(), null, out smuggledMethodReturnMessage);
                    }
                }
                finally
                {
                    CallContext.SetLogicalCallContext(logicalCallContext2);
                }
                if (smuggledMethodReturnMessage != null)
                {
                    ArrayList deserializedArgs = smuggledMethodReturnMessage.FixupForNewAppDomain();
                    message2 = new MethodResponse((IMethodCallMessage)reqMsg, smuggledMethodReturnMessage, deserializedArgs);
                }
                else if (array != null)
                {
                    MemoryStream stm = new MemoryStream(array);
                    message2 = CrossAppDomainSerializer.DeserializeMessage(stm, reqMsg as IMethodCallMessage);
                }
            }
            catch (Exception e)
            {
                try
                {
                    message2 = new ReturnMessage(e, reqMsg as IMethodCallMessage);
                }
                catch (Exception)
                {
                }
            }
            if (principal != null)
            {
                IMethodReturnMessage methodReturnMessage = message2 as IMethodReturnMessage;
                if (methodReturnMessage != null)
                {
                    LogicalCallContext logicalCallContext3 = methodReturnMessage.LogicalCallContext;
                    logicalCallContext3.Principal = principal;
                }
            }
            return(message2);
        }
        [System.Security.SecurityCritical]  // auto-generated
        public virtual IMessage SyncProcessMessage(IMessage reqMsg)
        {
            Message.DebugOut("\n::::::::::::::::::::::::: CrossAppDomain Channel: [....] call starting");
            IMessage errMsg = InternalSink.ValidateMessage(reqMsg);

            if (errMsg != null)
            {
                return(errMsg);
            }


            // currentPrincipal is used to save the current principal. It should be
            //   restored on the reply message.
            IPrincipal currentPrincipal = null;


            IMessage desRetMsg = null;

            try
            {
                IMethodCallMessage mcmReqMsg = reqMsg as IMethodCallMessage;
                if (mcmReqMsg != null)
                {
                    LogicalCallContext lcc = mcmReqMsg.LogicalCallContext;
                    if (lcc != null)
                    {
                        // Special case Principal since if might not be serializable
                        currentPrincipal = lcc.RemovePrincipalIfNotSerializable();
                    }
                }

                MemoryStream reqStm = null;
                SmuggledMethodCallMessage smuggledMcm = SmuggledMethodCallMessage.SmuggleIfPossible(reqMsg);

                if (smuggledMcm == null)
                {
                    //*********************** SERIALIZE REQ-MSG ****************
                    // Deserialization of objects requires permissions that users
                    // of remoting are not guaranteed to possess. Since remoting
                    // can guarantee that it's users can't abuse deserialization
                    // (since it won't allow them to pass in raw blobs of
                    // serialized data), it should assert the permissions
                    // necessary before calling the deserialization code. This
                    // will terminate the security stackwalk caused when
                    // serialization checks for the correct permissions at the
                    // remoting stack frame so the check won't continue on to
                    // the user and fail. <EMAIL>[from [....]]</EMAIL>
                    // We will hold off from doing this for x-process channels
                    // until the big picture of distributed security is finalized.

                    reqStm = CrossAppDomainSerializer.SerializeMessage(reqMsg);
                }

                // Retrieve calling caller context here, where it is safe from the view
                // of app domain checking code
                LogicalCallContext oldCallCtx = CallContext.SetLogicalCallContext(null);

                // Call helper method here, to avoid confusion with stack frames & app domains
                MemoryStream retStm        = null;
                byte[]       responseBytes = null;
                SmuggledMethodReturnMessage smuggledMrm;

                try
                {
                    if (smuggledMcm != null)
                    {
                        responseBytes = DoTransitionDispatch(null, smuggledMcm, out smuggledMrm);
                    }
                    else
                    {
                        responseBytes = DoTransitionDispatch(reqStm.GetBuffer(), null, out smuggledMrm);
                    }
                }
                finally
                {
                    CallContext.SetLogicalCallContext(oldCallCtx);
                }

                if (smuggledMrm != null)
                {
                    ArrayList deserializedArgs = smuggledMrm.FixupForNewAppDomain();
                    desRetMsg = new MethodResponse((IMethodCallMessage)reqMsg,
                                                   smuggledMrm,
                                                   deserializedArgs);
                }
                else
                {
                    if (responseBytes != null)
                    {
                        retStm = new MemoryStream(responseBytes);

                        Message.DebugOut("::::::::::::::::::::::::::: CrossAppDomain Channel: [....] call returning!!\n");
                        //*********************** DESERIALIZE RET-MSG **************
                        desRetMsg = CrossAppDomainSerializer.DeserializeMessage(retStm, reqMsg as IMethodCallMessage);
                    }
                }
            }
            catch (Exception e)
            {
                Message.DebugOut("Arrgh.. XAppDomainSink::throwing exception " + e + "\n");
                try
                {
                    desRetMsg = new ReturnMessage(e, (reqMsg as IMethodCallMessage));
                }
                catch (Exception)
                {
                    // Fatal Error .. can't do much here
                }
            }

            // restore the principal if necessary.
            if (currentPrincipal != null)
            {
                IMethodReturnMessage mrmRetMsg = desRetMsg as IMethodReturnMessage;
                if (mrmRetMsg != null)
                {
                    LogicalCallContext lcc = mrmRetMsg.LogicalCallContext;
                    lcc.Principal = currentPrincipal;
                }
            }

            return(desRetMsg);
        }
Example #12
0
        public virtual IMessage SyncProcessMessage(IMessage reqMsg)
        {
            IMessage message = InternalSink.ValidateMessage(reqMsg);

            if (message != null)
            {
                return(message);
            }
            IPrincipal principal = null;
            IMessage   message2  = null;

            try
            {
                SmuggledMethodReturnMessage message5;
                IMethodCallMessage          message3 = reqMsg as IMethodCallMessage;
                if (message3 != null)
                {
                    LogicalCallContext logicalCallContext = message3.LogicalCallContext;
                    if (logicalCallContext != null)
                    {
                        principal = logicalCallContext.RemovePrincipalIfNotSerializable();
                    }
                }
                MemoryStream stream = null;
                SmuggledMethodCallMessage smuggledMcm = SmuggledMethodCallMessage.SmuggleIfPossible(reqMsg);
                if (smuggledMcm == null)
                {
                    stream = CrossAppDomainSerializer.SerializeMessage(reqMsg);
                }
                LogicalCallContext callCtx = CallContext.SetLogicalCallContext(null);
                MemoryStream       stm     = null;
                byte[]             buffer  = null;
                try
                {
                    if (smuggledMcm != null)
                    {
                        buffer = this.DoTransitionDispatch(null, smuggledMcm, out message5);
                    }
                    else
                    {
                        buffer = this.DoTransitionDispatch(stream.GetBuffer(), null, out message5);
                    }
                }
                finally
                {
                    CallContext.SetLogicalCallContext(callCtx);
                }
                if (message5 != null)
                {
                    ArrayList deserializedArgs = message5.FixupForNewAppDomain();
                    message2 = new MethodResponse((IMethodCallMessage)reqMsg, message5, deserializedArgs);
                }
                else if (buffer != null)
                {
                    stm      = new MemoryStream(buffer);
                    message2 = CrossAppDomainSerializer.DeserializeMessage(stm, reqMsg as IMethodCallMessage);
                }
            }
            catch (Exception exception)
            {
                try
                {
                    message2 = new ReturnMessage(exception, reqMsg as IMethodCallMessage);
                }
                catch (Exception)
                {
                }
            }
            if (principal != null)
            {
                IMethodReturnMessage message6 = message2 as IMethodReturnMessage;
                if (message6 != null)
                {
                    message6.LogicalCallContext.Principal = principal;
                }
            }
            return(message2);
        }
Example #13
0
        public virtual IMessage SyncProcessMessage(IMessage reqMsg)
        {
            IMessage message1 = InternalSink.ValidateMessage(reqMsg);

            if (message1 != null)
            {
                return(message1);
            }
            IPrincipal principal = (IPrincipal)null;
            IMessage   message2  = (IMessage)null;

            try
            {
                IMethodCallMessage methodCallMessage = reqMsg as IMethodCallMessage;
                if (methodCallMessage != null)
                {
                    LogicalCallContext logicalCallContext = methodCallMessage.LogicalCallContext;
                    if (logicalCallContext != null)
                    {
                        principal = logicalCallContext.RemovePrincipalIfNotSerializable();
                    }
                }
                MemoryStream memoryStream             = (MemoryStream)null;
                SmuggledMethodCallMessage smuggledMcm = SmuggledMethodCallMessage.SmuggleIfPossible(reqMsg);
                if (smuggledMcm == null)
                {
                    memoryStream = CrossAppDomainSerializer.SerializeMessage(reqMsg);
                }
                LogicalCallContext          callCtx = CallContext.SetLogicalCallContext((LogicalCallContext)null);
                byte[]                      buffer  = (byte[])null;
                SmuggledMethodReturnMessage smuggledMrm;
                try
                {
                    buffer = smuggledMcm == null?this.DoTransitionDispatch(memoryStream.GetBuffer(), (SmuggledMethodCallMessage)null, out smuggledMrm) : this.DoTransitionDispatch((byte[])null, smuggledMcm, out smuggledMrm);
                }
                finally
                {
                    CallContext.SetLogicalCallContext(callCtx);
                }
                if (smuggledMrm != null)
                {
                    ArrayList deserializedArgs = smuggledMrm.FixupForNewAppDomain();
                    message2 = (IMessage) new MethodResponse((IMethodCallMessage)reqMsg, smuggledMrm, deserializedArgs);
                }
                else if (buffer != null)
                {
                    message2 = CrossAppDomainSerializer.DeserializeMessage(new MemoryStream(buffer), reqMsg as IMethodCallMessage);
                }
            }
            catch (Exception ex1)
            {
                try
                {
                    message2 = (IMessage) new ReturnMessage(ex1, reqMsg as IMethodCallMessage);
                }
                catch (Exception ex2)
                {
                }
            }
            if (principal != null)
            {
                IMethodReturnMessage methodReturnMessage = message2 as IMethodReturnMessage;
                if (methodReturnMessage != null)
                {
                    methodReturnMessage.LogicalCallContext.Principal = principal;
                }
            }
            return(message2);
        }