summaryrefslogtreecommitdiff
path: root/src/vm/i386/remotingx86.cpp
blob: 3a9e891267cfdf53f59f522ccc9c8bfb5a8994a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
//
//
// 
// File: remotingx86.cpp
// 

// 
// 
// Purpose: Defines various remoting related functions for the x86 architecture
//

//
// 

//

#include "common.h"

#ifdef FEATURE_REMOTING

#include "excep.h"
#include "comdelegate.h"
#include "remoting.h"
#include "field.h"
#include "siginfo.hpp"
#include "stackbuildersink.h"
#include "threads.h"
#include "method.hpp"
#include "asmconstants.h"
#include "interoputil.h"
#include "virtualcallstub.h"

#ifdef FEATURE_COMINTEROP 
#include "comcallablewrapper.h"
#include "comcache.h"
#endif // FEATURE_COMINTEROP

//+----------------------------------------------------------------------------
//
//  Method:     CTPMethodTable::CreateThunkForVirtualMethod   private
//
//  Synopsis:   Creates the thunk that pushes the supplied slot number and jumps
//              to TP Stub
//
//+----------------------------------------------------------------------------
PCODE CTPMethodTable::CreateThunkForVirtualMethod(DWORD dwSlot, BYTE *startaddr)
{
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
        MODE_ANY;
        PRECONDITION(CheckPointer(startaddr));
    }
    CONTRACTL_END;

    BYTE *pCode = startaddr;

    // 0000   B8 67 45 23 01     MOV  EAX, dwSlot
    // 0005   E9 ?? ?? ?? ??     JMP  TransparentProxyStub
    *pCode++ = 0xB8;
    *((DWORD *) pCode) = dwSlot;
    pCode += sizeof(DWORD);
    *pCode++ = 0xE9;
    // self-relative call, based on the start of the next instruction.
    *((LONG *) pCode) = (LONG)((size_t)GetTPStubEntryPoint() - (size_t) (pCode + sizeof(LONG)));

    _ASSERTE(CVirtualThunkMgr::IsThunkByASM((PCODE)startaddr));

    return (PCODE)startaddr;
}


//+----------------------------------------------------------------------------
//
//  Method:     CTPMethodTable::ActivatePrecodeRemotingThunk    private
//
//  Synopsis:   Patch the precode remoting thunk to begin interception
//
//+----------------------------------------------------------------------------
void CTPMethodTable::ActivatePrecodeRemotingThunk()
{
    CONTRACTL
    {
        THROWS;
        GC_TRIGGERS;
        MODE_ANY;
    }
    CONTRACTL_END;

    // Before activation:
    // 0000 C3                  ret
    // 0001 90                  nop

    // After activation:
    // 0000 85 C9               test     ecx,ecx

    // 0002 74 XX               je       RemotingDone
    // 0004 81 39 XX XX XX XX   cmp      dword ptr [ecx],11111111h
    // 000A 74 XX               je       RemotingCheck

    // Switch offset and size of patch based on the jump opcode used.
    BYTE* pCode = (BYTE*)PrecodeRemotingThunk;

    SIZE_T mtOffset = 0x0006;
    SIZE_T size = 0x000A;

    // Patch "ret + nop" to "test ecx,ecx"
    *(UINT16 *)pCode = 0xC985;

    // Replace placeholder value with the actual address of TP method table
    _ASSERTE(*(PVOID*)(pCode+mtOffset) == (PVOID*)0x11111111);
    *(PVOID*)(pCode+mtOffset) = GetMethodTable();

    FlushInstructionCache(GetCurrentProcess(), pCode, size);
}

//+----------------------------------------------------------------------------
//
//  Method:     CVirtualThunkMgr::DoTraceStub   public
//
//  Synopsis:   Traces the stub given the starting address
//
//+----------------------------------------------------------------------------
BOOL CVirtualThunkMgr::DoTraceStub(PCODE stubStartAddress, TraceDestination *trace)
{
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
        MODE_ANY;
        PRECONDITION(stubStartAddress != NULL);
        PRECONDITION(CheckPointer(trace));
    }
    CONTRACTL_END;

    BOOL bIsStub = FALSE;

    // Find a thunk whose code address matching the starting address
    LPBYTE pThunk = FindThunk((LPBYTE)stubStartAddress);
    if(NULL != pThunk)
    {
        LPBYTE pbAddr = NULL;
        LONG destAddress = 0;
        if((LPBYTE)stubStartAddress == pThunk)
        {

            // Extract the long which gives the self relative address
            // of the destination
            pbAddr = pThunk + sizeof(BYTE) + sizeof(DWORD) + sizeof(BYTE);
            destAddress = *(LONG *)pbAddr;

            // Calculate the absolute address by adding the offset of the next
            // instruction after the call instruction
            destAddress += (LONG)(size_t)(pbAddr + sizeof(LONG));

        }

        // We cannot tell where the stub will end up until OnCall is reached.
        // So we tell the debugger to run till OnCall is reached and then
        // come back and ask us again for the actual destination address of
        // the call

        Stub *stub = Stub::RecoverStub((TADDR)destAddress);

        trace->InitForFramePush(stub->GetPatchAddress());
        bIsStub = TRUE;
    }

    return bIsStub;
}

//+----------------------------------------------------------------------------
//
//  Method:     CVirtualThunkMgr::IsThunkByASM  public
//
//  Synopsis:   Check assembly to see if this one of our thunks
//
//+----------------------------------------------------------------------------
BOOL CVirtualThunkMgr::IsThunkByASM(PCODE startaddr)
{
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
        MODE_ANY;
        PRECONDITION(startaddr != NULL);
    }
    CONTRACTL_END;

    PTR_BYTE pbCode = PTR_BYTE(startaddr);

    return ((pbCode[0] == 0xB8) &&
            (pbCode[5] == 0xe9) &&
            (rel32Decode((TADDR)(pbCode + 6)) == CTPMethodTable::GetTPStubEntryPoint()));
}

//+----------------------------------------------------------------------------
//
//  Method:     CVirtualThunkMgr::GetMethodDescByASM   public
//
//  Synopsis:   Parses MethodDesc out of assembly code
//
//+----------------------------------------------------------------------------
MethodDesc *CVirtualThunkMgr::GetMethodDescByASM(PCODE startaddr, MethodTable *pMT)
{
    CONTRACT (MethodDesc*)
    {
        NOTHROW;
        GC_NOTRIGGER;
        MODE_ANY;
        PRECONDITION(startaddr != NULL);
        PRECONDITION(CheckPointer(pMT));
        POSTCONDITION(CheckPointer(RETVAL));
    }
    CONTRACT_END;

    RETURN (pMT->GetMethodDescForSlot(*((DWORD *) (startaddr + 1))));
}

#endif// FEATURE_REMOTING