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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
|
;; 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.
.586
.model flat
option casemap:none
.code
include AsmMacros.inc
EXTERN @GetClasslibCCtorCheck@4 : PROC
EXTERN _memcpy : PROC
EXTERN _memcpyGCRefs : PROC
EXTERN _memcpyGCRefsWithWriteBarrier : PROC
EXTERN _memcpyAnyWithWriteBarrier : PROC
;;
;; Checks whether the static class constructor for the type indicated by the context structure has been
;; executed yet. If not the classlib is called via their CheckStaticClassConstruction callback which will
;; execute the cctor and update the context to record this fact.
;;
;; Input:
;; eax : Address of StaticClassConstructionContext structure
;;
;; Output:
;; All volatile registers and the condition codes may be trashed.
;;
FASTCALL_FUNC RhpCheckCctor, 4
;; Check the m_initialized field of the context. The cctor has been run only if this equals 1 (the
;; initial state is 0 and the remaining values are reserved for classlib use). This check is
;; unsynchronized; if we go down the slow path and call the classlib then it is responsible for
;; synchronizing with other threads and re-checking the value.
cmp dword ptr [eax + OFFSETOF__StaticClassConstructionContext__m_initialized], 1
jne RhpCheckCctor__SlowPath
ret
RhpCheckCctor__SlowPath:
mov edx, eax ; RhpCheckCctor2 takes the static class construction context pointer in the edx register
jmp @RhpCheckCctor2@4
FASTCALL_ENDFUNC
;;
;; Checks whether the static class constructor for the type indicated by the context structure has been
;; executed yet. If not the classlib is called via their CheckStaticClassConstruction callback which will
;; execute the cctor and update the context to record this fact.
;;
;; Input:
;; eax : Value that must be preserved in this register across the cctor check.
;; edx : Address of StaticClassConstructionContext structure
;;
;; Output:
;; All volatile registers other than eax may be trashed and the condition codes may also be trashed.
;;
FASTCALL_FUNC RhpCheckCctor2, 4
;; Check the m_initialized field of the context. The cctor has been run only if this equals 1 (the
;; initial state is 0 and the remaining values are reserved for classlib use). This check is
;; unsynchronized; if we go down the slow path and call the classlib then it is responsible for
;; synchronizing with other threads and re-checking the value.
cmp dword ptr [edx + OFFSETOF__StaticClassConstructionContext__m_initialized], 1
jne RhpCheckCctor2__SlowPath
ret
;; Input:
;; eax : Value that must be preserved in this register across the cctor check.
;; edx : Address of StaticClassConstructionContext structure
;;
;; Output:
;; All volatile registers other than eax may be trashed and the condition codes may also be trashed.
;;
RhpCheckCctor2__SlowPath:
;; Call a C++ helper to retrieve the address of the classlib callback. We need to preserve the context
;; structure address in eax since it's needed for the actual call.
push ebx
push esi
mov ebx, edx ; save cctor context pointer
mov esi, eax ; save preserved return value
;; The caller's return address is passed as the argument to the helper; it's an address in the module
;; and is used by the helper to locate the classlib.
mov ecx, [esp + 8] ; + 8 to skip past the saved ebx and esi
call @GetClasslibCCtorCheck@4
;; Eax now contains the address of the classlib method to call. The single argument is the context
;; structure address currently in ebx. Clean up and tail call to the classlib callback so we're not on
;; the stack should a GC occur (so we don't need to worry about transition frames).
mov edx, ebx
mov ecx, esi
pop esi
pop ebx
;; Tail-call the classlib cctor check function. Note that the incoming eax value is moved to ecx
;; and the classlib cctor check function is required to return that value, so that eax is preserved
;; across a RhpCheckCctor call.
jmp eax
FASTCALL_ENDFUNC
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; void* __cdecl RhpCopyMultibyteNoGCRefs(void*, void*, size_t)
;;
;; The purpose of this wrapper is to hoist the potential null reference exceptions of copying memory up to a place where
;; the stack unwinder and exception dispatch can properly transform the exception into a managed exception and dispatch
;; it to managed code.
;;
_RhpCopyMultibyteNoGCRefs PROC PUBLIC
; #locals, num_params, prolog bytes, #regs saved, use ebp, frame type (0 == FRAME_FPO)
.FPO( 0, 3, 0, 0, 0, 0)
; [esp + 0] return address
; [esp + 4] dest
; [esp + 8] src
; [esp + c] count
cmp dword ptr [esp + 0Ch], 0 ; check for a zero-length copy
jz NothingToCopy
mov ecx, [esp + 4] ; ecx <- dest
mov edx, [esp + 8] ; edx <- src
; Now check the dest and src pointers. If they AV, the EH subsystem will recognize the address of the AV,
; unwind the frame, and fixup the stack to make it look like the (managed) caller AV'ed, which will be
; translated to a managed exception as usual.
ALTERNATE_ENTRY RhpCopyMultibyteNoGCRefsDestAVLocation
cmp byte ptr [ecx], 0
ALTERNATE_ENTRY RhpCopyMultibyteNoGCRefsSrcAVLocation
cmp byte ptr [edx], 0
; tail-call to plain-old-memcpy
jmp _memcpy
NothingToCopy:
mov eax, [esp + 4] ; return dest
ret
_RhpCopyMultibyteNoGCRefs ENDP
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; void* __cdecl RhpCopyMultibyte(void*, void*, size_t)
;;
;; The purpose of this wrapper is to hoist the potential null reference exceptions of copying memory up to a place where
;; the stack unwinder and exception dispatch can properly transform the exception into a managed exception and dispatch
;; it to managed code.
;;
_RhpCopyMultibyte PROC PUBLIC
; #locals, num_params, prolog bytes, #regs saved, use ebp, frame type (0 == FRAME_FPO)
.FPO( 0, 3, 0, 0, 0, 0)
; [esp + 0] return address
; [esp + 4] dest
; [esp + 8] src
; [esp + c] count
cmp dword ptr [esp + 0Ch], 0 ; check for a zero-length copy
jz NothingToCopy
mov ecx, [esp + 4] ; ecx <- dest
mov edx, [esp + 8] ; edx <- src
; Now check the dest and src pointers. If they AV, the EH subsystem will recognize the address of the AV,
; unwind the frame, and fixup the stack to make it look like the (managed) caller AV'ed, which will be
; translated to a managed exception as usual.
ALTERNATE_ENTRY RhpCopyMultibyteDestAVLocation
cmp byte ptr [ecx], 0
ALTERNATE_ENTRY RhpCopyMultibyteSrcAVLocation
cmp byte ptr [edx], 0
; tail-call to the GC-safe memcpy implementation
; NOTE: this is also a __cdecl function
jmp _memcpyGCRefs
NothingToCopy:
mov eax, [esp + 4] ; return dest
ret
_RhpCopyMultibyte ENDP
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; void* __cdecl RhpCopyMultibyteWithWriteBarrier(void*, void*, size_t)
;;
;; The purpose of this wrapper is to hoist the potential null reference exceptions of copying memory up to a place where
;; the stack unwinder and exception dispatch can properly transform the exception into a managed exception and dispatch
;; it to managed code.
;; Runs a card table update via RhpBulkWriteBarrier after the copy
;;
_RhpCopyMultibyteWithWriteBarrier PROC PUBLIC
; #locals, num_params, prolog bytes, #regs saved, use ebp, frame type (0 == FRAME_FPO)
.FPO( 0, 3, 0, 0, 0, 0)
; [esp + 0] return address
; [esp + 4] dest
; [esp + 8] src
; [esp + c] count
cmp dword ptr [esp + 0Ch], 0 ; check for a zero-length copy
jz NothingToCopy
mov ecx, [esp + 4] ; ecx <- dest
mov edx, [esp + 8] ; edx <- src
; Now check the dest and src pointers. If they AV, the EH subsystem will recognize the address of the AV,
; unwind the frame, and fixup the stack to make it look like the (managed) caller AV'ed, which will be
; translated to a managed exception as usual.
ALTERNATE_ENTRY RhpCopyMultibyteWithWriteBarrierDestAVLocation
cmp byte ptr [ecx], 0
ALTERNATE_ENTRY RhpCopyMultibyteWithWriteBarrierSrcAVLocation
cmp byte ptr [edx], 0
; tail-call to the GC-safe memcpy implementation
; NOTE: this is also a __cdecl function
jmp _memcpyGCRefsWithWriteBarrier
NothingToCopy:
mov eax, [esp + 4] ; return dest
ret
_RhpCopyMultibyteWithWriteBarrier ENDP
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; void* __cdecl RhpCopyAnyWithWriteBarrier(void*, void*, size_t)
;;
;; The purpose of this wrapper is to hoist the potential null reference exceptions of copying memory up to a place where
;; the stack unwinder and exception dispatch can properly transform the exception into a managed exception and dispatch
;; it to managed code.
;; Runs a card table update via RhpBulkWriteBarrier after the copy if it contained GC pointers
;;
_RhpCopyAnyWithWriteBarrier PROC PUBLIC
; #locals, num_params, prolog bytes, #regs saved, use ebp, frame type (0 == FRAME_FPO)
.FPO( 0, 3, 0, 0, 0, 0)
; [esp + 0] return address
; [esp + 4] dest
; [esp + 8] src
; [esp + c] count
cmp dword ptr [esp + 0Ch], 0 ; check for a zero-length copy
jz NothingToCopy
mov ecx, [esp + 4] ; ecx <- dest
mov edx, [esp + 8] ; edx <- src
; Now check the dest and src pointers. If they AV, the EH subsystem will recognize the address of the AV,
; unwind the frame, and fixup the stack to make it look like the (managed) caller AV'ed, which will be
; translated to a managed exception as usual.
ALTERNATE_ENTRY RhpCopyAnyWithWriteBarrierDestAVLocation
cmp byte ptr [ecx], 0
ALTERNATE_ENTRY RhpCopyAnyWithWriteBarrierSrcAVLocation
cmp byte ptr [edx], 0
; tail-call to the GC-safe memcpy implementation
; NOTE: this is also a __cdecl function
jmp _memcpyAnyWithWriteBarrier
NothingToCopy:
mov eax, [esp + 4] ; return dest
ret
_RhpCopyAnyWithWriteBarrier ENDP
end
|