-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
MiscStubs.asm
216 lines (171 loc) · 8.76 KB
/
MiscStubs.asm
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
;; Licensed to the .NET Foundation under one or more agreements.
;; The .NET Foundation licenses this file to you under the MIT license.
.586
.model flat
option casemap:none
.code
include AsmMacros.inc
EXTERN _memcpy : PROC
EXTERN _memcpyGCRefs : PROC
EXTERN _memcpyGCRefsWithWriteBarrier : PROC
EXTERN _memcpyAnyWithWriteBarrier : PROC
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; 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
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; The following helper will access ("probe") a word on each page of the stack
; starting with the page right beneath esp down to the one pointed to by eax.
; The procedure is needed to make sure that the "guard" page is pushed down below the allocated stack frame.
; The call to the helper will be emitted by JIT in the function prolog when large (larger than 0x3000 bytes) stack frame is required.
;
; NOTE: this helper will modify a value of esp and must establish the frame pointer.
PAGE_SIZE equ 1000h
_RhpStackProbe PROC public
; On entry:
; eax - the lowest address of the stack frame being allocated (i.e. [InitialSp - FrameSize])
;
; NOTE: this helper will probe at least one page below the one pointed by esp.
push ebp
mov ebp, esp
and esp, -PAGE_SIZE ; esp points to the **lowest address** on the last probed page
; This is done to make the loop end condition simpler.
ProbeLoop:
sub esp, PAGE_SIZE ; esp points to the lowest address of the **next page** to probe
test [esp], eax ; esp points to the lowest address on the **last probed** page
cmp esp, eax
jg ProbeLoop ; if esp > eax, then we need to probe at least one more page.
mov esp, ebp
pop ebp
ret
_RhpStackProbe ENDP
end