This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
unwind.cpp
455 lines (391 loc) · 14.9 KB
/
unwind.cpp
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
// 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.
/*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XX XX
XX UnwindInfo XX
XX XX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
*/
#include "jitpch.h"
#ifdef _MSC_VER
#pragma hdrstop
#endif
#if FEATURE_EH_FUNCLETS
//------------------------------------------------------------------------
// Compiler::unwindGetFuncLocations: Get the start/end emitter locations for this
// function or funclet. If 'getHotSectionData' is true, get the start/end locations
// for the hot section. Otherwise, get the data for the cold section.
//
// Note that we grab these locations before the prolog and epilogs are generated, so the
// locations must remain correct after the prolog and epilogs are generated.
//
// For the prolog, instructions are put in the special, preallocated, prolog instruction group.
// We don't want to expose the emitPrologIG unnecessarily (locations are actually pointers to
// emitter instruction groups). Since we know the offset of the start of the function/funclet,
// where the prolog is, will be zero, we use a nullptr start location to indicate that.
//
// There is no instruction group beyond the end of the end of the function, so there is no
// location to indicate that. Once again, use nullptr for that.
//
// Intermediate locations point at the first instruction group of a funclet, which is a
// placeholder IG. These are converted to real IGs, not deleted and replaced, so the location
// remains valid.
//
// Arguments:
// func - main function or funclet to get locations for.
// getHotSectionData - 'true' to get the hot section data, 'false' to get the cold section data.
// ppStartLoc - OUT parameter. Set to the start emitter location.
// ppEndLoc - OUT parameter. Set to the end emitter location (the location immediately
// the range; the 'end' location is not inclusive).
//
// Notes:
// A start location of nullptr means the beginning of the code.
// An end location of nullptr means the end of the code.
//
void Compiler::unwindGetFuncLocations(FuncInfoDsc* func,
bool getHotSectionData,
/* OUT */ emitLocation** ppStartLoc,
/* OUT */ emitLocation** ppEndLoc)
{
if (func->funKind == FUNC_ROOT)
{
// Since all funclets are pulled out of line, the main code size is everything
// up to the first handler. If the function is hot/cold split, we need to get the
// appropriate sub-range.
if (getHotSectionData)
{
*ppStartLoc = nullptr; // nullptr emit location means the beginning of the code. This is to handle the first
// fragment prolog.
if (fgFirstColdBlock != nullptr)
{
// The hot section only goes up to the cold section
assert(fgFirstFuncletBB == nullptr);
*ppEndLoc = new (this, CMK_UnwindInfo) emitLocation(ehEmitCookie(fgFirstColdBlock));
}
else
{
if (fgFirstFuncletBB != nullptr)
{
*ppEndLoc = new (this, CMK_UnwindInfo) emitLocation(ehEmitCookie(fgFirstFuncletBB));
}
else
{
*ppEndLoc = nullptr; // nullptr end location means the end of the code
}
}
}
else
{
assert(fgFirstFuncletBB == nullptr); // TODO-CQ: support hot/cold splitting in functions with EH
assert(fgFirstColdBlock != nullptr); // There better be a cold section!
*ppStartLoc = new (this, CMK_UnwindInfo) emitLocation(ehEmitCookie(fgFirstColdBlock));
*ppEndLoc = nullptr; // nullptr end location means the end of the code
}
}
else
{
assert(getHotSectionData); // TODO-CQ: support funclets in cold section
EHblkDsc* HBtab = ehGetDsc(func->funEHIndex);
if (func->funKind == FUNC_FILTER)
{
assert(HBtab->HasFilter());
*ppStartLoc = new (this, CMK_UnwindInfo) emitLocation(ehEmitCookie(HBtab->ebdFilter));
*ppEndLoc = new (this, CMK_UnwindInfo) emitLocation(ehEmitCookie(HBtab->ebdHndBeg));
}
else
{
assert(func->funKind == FUNC_HANDLER);
*ppStartLoc = new (this, CMK_UnwindInfo) emitLocation(ehEmitCookie(HBtab->ebdHndBeg));
*ppEndLoc = (HBtab->ebdHndLast->bbNext == nullptr)
? nullptr
: new (this, CMK_UnwindInfo) emitLocation(ehEmitCookie(HBtab->ebdHndLast->bbNext));
}
}
}
#endif // FEATURE_EH_FUNCLETS
#if defined(_TARGET_UNIX_)
void Compiler::createCfiCode(FuncInfoDsc* func, UCHAR codeOffset, UCHAR cfiOpcode, USHORT dwarfReg, INT offset)
{
CFI_CODE cfiEntry(codeOffset, cfiOpcode, dwarfReg, offset);
func->cfiCodes->push_back(cfiEntry);
}
void Compiler::unwindPushPopCFI(regNumber reg)
{
#if defined(_TARGET_ARM_)
assert(compGeneratingEpilog);
#else
assert(compGeneratingProlog);
#endif
FuncInfoDsc* func = funCurrentFunc();
unsigned int cbProlog = 0;
if (compGeneratingProlog)
{
cbProlog = unwindGetCurrentOffset(func);
noway_assert((BYTE)cbProlog == cbProlog);
createCfiCode(func, cbProlog, CFI_ADJUST_CFA_OFFSET, DWARF_REG_ILLEGAL, REGSIZE_BYTES == 8 ? 8 : 4);
}
if ((RBM_CALLEE_SAVED & genRegMask(reg))
#if defined(UNIX_AMD64_ABI)
#if ETW_EBP_FRAMED
// In case of ETW_EBP_FRAMED defined the REG_FPBASE (RBP)
// is excluded from the callee-save register list.
// Make sure the register gets PUSH unwind info in this case,
// since it is pushed as a frame register.
|| (reg == REG_FPBASE)
#endif // ETW_EBP_FRAMED
#endif
)
{
createCfiCode(func, cbProlog, CFI_REL_OFFSET, mapRegNumToDwarfReg(reg));
}
}
template <typename T>
inline static T* allocate_any(jitstd::allocator<void>& alloc, size_t count = 5)
{
return jitstd::allocator<T>(alloc).allocate(count);
}
typedef jitstd::vector<CFI_CODE> CFICodeVector;
void Compiler::unwindBegPrologCFI()
{
assert(compGeneratingProlog);
#if FEATURE_EH_FUNCLETS
FuncInfoDsc* func = funCurrentFunc();
// There is only one prolog for a function/funclet, and it comes first. So now is
// a good time to initialize all the unwind data structures.
unwindGetFuncLocations(func, true, &func->startLoc, &func->endLoc);
if (fgFirstColdBlock != nullptr)
{
unwindGetFuncLocations(func, false, &func->coldStartLoc, &func->coldEndLoc);
}
jitstd::allocator<void> allocator(getAllocator());
func->cfiCodes = new (allocate_any<CFICodeVector>(allocator), jitstd::placement_t()) CFICodeVector(allocator);
#endif // FEATURE_EH_FUNCLETS
}
void Compiler::unwindPushPopMaskCFI(regMaskTP regMask, bool isFloat)
{
regMaskTP regBit = isFloat ? genRegMask(REG_FP_FIRST) : 1;
for (regNumber regNum = isFloat ? REG_FP_FIRST : REG_FIRST; regNum < REG_COUNT;
regNum = REG_NEXT(regNum), regBit <<= 1)
{
if (regBit > regMask)
{
break;
}
if (regBit & regMask)
{
unwindPushPopCFI(regNum);
}
}
}
void Compiler::unwindAllocStackCFI(unsigned size)
{
#if defined(_TARGET_ARM_)
assert(compGeneratingEpilog);
#else
assert(compGeneratingProlog);
#endif
FuncInfoDsc* func = funCurrentFunc();
unsigned int cbProlog = 0;
if (compGeneratingProlog)
{
cbProlog = unwindGetCurrentOffset(func);
noway_assert((BYTE)cbProlog == cbProlog);
}
createCfiCode(func, cbProlog, CFI_ADJUST_CFA_OFFSET, DWARF_REG_ILLEGAL, size);
}
//------------------------------------------------------------------------
// Compiler::unwindSetFrameRegCFI: Record a cfi info for a frame register set.
//
// Arguments:
// reg - The register being set as the frame register.
// offset - The offset from the current stack pointer that the frame pointer will point at.
//
void Compiler::unwindSetFrameRegCFI(regNumber reg, unsigned offset)
{
#if defined(_TARGET_ARM_)
assert(compGeneratingEpilog);
#else
assert(compGeneratingProlog);
#endif
FuncInfoDsc* func = funCurrentFunc();
unsigned int cbProlog = 0;
if (compGeneratingProlog)
{
cbProlog = unwindGetCurrentOffset(func);
noway_assert((BYTE)cbProlog == cbProlog);
}
createCfiCode(func, cbProlog, CFI_DEF_CFA_REGISTER, mapRegNumToDwarfReg(reg));
if (offset != 0)
{
// before: cfa = rsp + old_cfa_offset;
// rbp = rsp + offset;
// after: cfa should be based on rbp, but points to the old address:
// rsp + old_cfa_offset == rbp + old_cfa_offset + adjust;
// adjust = -offset;
int adjust = -(int)offset;
createCfiCode(func, cbProlog, CFI_ADJUST_CFA_OFFSET, DWARF_REG_ILLEGAL, adjust);
}
}
void Compiler::unwindEmitFuncCFI(FuncInfoDsc* func, void* pHotCode, void* pColdCode)
{
UNATIVE_OFFSET startOffset;
UNATIVE_OFFSET endOffset;
DWORD unwindCodeBytes = 0;
BYTE* pUnwindBlock = nullptr;
if (func->startLoc == nullptr)
{
startOffset = 0;
}
else
{
startOffset = func->startLoc->CodeOffset(genEmitter);
}
if (func->endLoc == nullptr)
{
endOffset = info.compNativeCodeSize;
}
else
{
endOffset = func->endLoc->CodeOffset(genEmitter);
}
DWORD size = (DWORD)func->cfiCodes->size();
if (size > 0)
{
unwindCodeBytes = size * sizeof(CFI_CODE);
pUnwindBlock = (BYTE*)&(*func->cfiCodes)[0];
}
#ifdef DEBUG
if (opts.dspUnwind)
{
DumpCfiInfo(true /*isHotCode*/, startOffset, endOffset, unwindCodeBytes, (const CFI_CODE* const)pUnwindBlock);
}
#endif // DEBUG
assert(endOffset <= info.compTotalHotCodeSize);
eeAllocUnwindInfo((BYTE*)pHotCode, nullptr /* pColdCode */, startOffset, endOffset, unwindCodeBytes, pUnwindBlock,
(CorJitFuncKind)func->funKind);
if (pColdCode != nullptr)
{
assert(fgFirstColdBlock != nullptr);
assert(func->funKind == FUNC_ROOT); // No splitting of funclets.
unwindCodeBytes = 0;
pUnwindBlock = nullptr;
if (func->coldStartLoc == nullptr)
{
startOffset = 0;
}
else
{
startOffset = func->coldStartLoc->CodeOffset(genEmitter);
}
if (func->coldEndLoc == nullptr)
{
endOffset = info.compNativeCodeSize;
}
else
{
endOffset = func->coldEndLoc->CodeOffset(genEmitter);
}
#ifdef DEBUG
if (opts.dspUnwind)
{
DumpCfiInfo(false /*isHotCode*/, startOffset, endOffset, unwindCodeBytes,
(const CFI_CODE* const)pUnwindBlock);
}
#endif // DEBUG
assert(startOffset >= info.compTotalHotCodeSize);
startOffset -= info.compTotalHotCodeSize;
endOffset -= info.compTotalHotCodeSize;
eeAllocUnwindInfo((BYTE*)pHotCode, (BYTE*)pColdCode, startOffset, endOffset, unwindCodeBytes, pUnwindBlock,
(CorJitFuncKind)func->funKind);
}
}
#ifdef DEBUG
//------------------------------------------------------------------------
// DumpCfiInfo: Dump the Cfi data.
//
// Arguments:
// isHotCode - true if this cfi data is for the hot section, false otherwise.
// startOffset - byte offset of the code start that this cfi data represents.
// endOffset - byte offset of the code end that this cfi data represents.
// pcFiCode - pointer to the cfi data blob.
//
void Compiler::DumpCfiInfo(bool isHotCode,
UNATIVE_OFFSET startOffset,
UNATIVE_OFFSET endOffset,
DWORD cfiCodeBytes,
const CFI_CODE* const pCfiCode)
{
printf("Cfi Info%s:\n", isHotCode ? "" : " COLD");
printf(" >> Start offset : 0x%06x \n", dspOffset(startOffset));
printf(" >> End offset : 0x%06x \n", dspOffset(endOffset));
for (int i = 0; i < (int)(cfiCodeBytes / sizeof(CFI_CODE)); i++)
{
const CFI_CODE* const pCode = &(pCfiCode[i]);
UCHAR codeOffset = pCode->CodeOffset;
SHORT dwarfReg = pCode->DwarfReg;
INT offset = pCode->Offset;
switch (pCode->CfiOpCode)
{
case CFI_REL_OFFSET:
printf(" CodeOffset: 0x%02X Op: RelOffset DwarfReg:0x%x Offset:0x%X\n", codeOffset, dwarfReg,
offset);
break;
case CFI_DEF_CFA_REGISTER:
assert(offset == 0);
printf(" CodeOffset: 0x%02X Op: DefCfaRegister DwarfReg:0x%X\n", codeOffset, dwarfReg);
break;
case CFI_ADJUST_CFA_OFFSET:
assert(dwarfReg == DWARF_REG_ILLEGAL);
printf(" CodeOffset: 0x%02X Op: AdjustCfaOffset Offset:0x%X\n", codeOffset, offset);
break;
default:
printf(" Unrecognized CFI_CODE: 0x%IX\n", *(UINT64*)pCode);
break;
}
}
}
#endif // DEBUG
#endif // _TARGET_UNIX_
//------------------------------------------------------------------------
// Compiler::unwindGetCurrentOffset: Calculate the current byte offset of the
// prolog being generated.
//
// Arguments:
// func - The main function or funclet of interest.
//
// Return Value:
// The byte offset of the prolog currently being generated.
//
UNATIVE_OFFSET Compiler::unwindGetCurrentOffset(FuncInfoDsc* func)
{
assert(compGeneratingProlog);
UNATIVE_OFFSET offset;
if (func->funKind == FUNC_ROOT)
{
offset = genEmitter->emitGetPrologOffsetEstimate();
}
else
{
#if defined(_TARGET_AMD64_)
assert(func->startLoc != nullptr);
offset = func->startLoc->GetFuncletPrologOffset(genEmitter);
#else
offset = 0; // TODO ???
#endif
}
return offset;
}
#if defined(_TARGET_AMD64_)
// See unwindAmd64.cpp
#elif defined(_TARGET_ARM64_)
// See unwindArm64.cpp
#elif defined(_TARGET_ARM_)
// See unwindArm.cpp
#elif defined(_TARGET_X86_)
// See unwindX86.cpp
#else // _TARGET_*
#error Unsupported or unset target architecture
#endif // _TARGET_*