-
Notifications
You must be signed in to change notification settings - Fork 93
/
clr_helpers.h
656 lines (573 loc) · 18.7 KB
/
clr_helpers.h
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef OTEL_CLR_PROFILER_CLR_HELPERS_H_
#define OTEL_CLR_PROFILER_CLR_HELPERS_H_
#include <corhlpr.h>
#include <corprof.h>
#include <functional>
#include <utility>
#include "com_ptr.h"
#include "integration.h"
#include "util.h"
#include <set>
namespace trace
{
class ModuleMetadata;
const size_t kNameMaxSize = 1024;
const ULONG kEnumeratorMax = 256;
const auto SystemBoolean = WStr("System.Boolean");
const auto SystemChar = WStr("System.Char");
const auto SystemByte = WStr("System.Byte");
const auto SystemSByte = WStr("System.SByte");
const auto SystemUInt16 = WStr("System.UInt16");
const auto SystemInt16 = WStr("System.Int16");
const auto SystemInt32 = WStr("System.Int32");
const auto SystemUInt32 = WStr("System.UInt32");
const auto SystemInt64 = WStr("System.Int64");
const auto SystemUInt64 = WStr("System.UInt64");
const auto SystemSingle = WStr("System.Single");
const auto SystemDouble = WStr("System.Double");
const auto SystemIntPtr = WStr("System.IntPtr");
const auto SystemUIntPtr = WStr("System.UIntPtr");
const auto SystemString = WStr("System.String");
const auto SystemObject = WStr("System.Object");
const auto SystemException = WStr("System.Exception");
const auto SystemTypeName = WStr("System.Type");
const auto GetTypeFromHandleMethodName = WStr("GetTypeFromHandle");
const auto RuntimeTypeHandleTypeName = WStr("System.RuntimeTypeHandle");
const auto RuntimeMethodHandleTypeName = WStr("System.RuntimeMethodHandle");
template <typename T>
class EnumeratorIterator;
template <typename T>
class Enumerator
{
private:
const std::function<HRESULT(HCORENUM*, T[], ULONG, ULONG*)> callback_;
const std::function<void(HCORENUM)> close_;
mutable HCORENUM ptr_;
public:
Enumerator(std::function<HRESULT(HCORENUM*, T[], ULONG, ULONG*)> callback, std::function<void(HCORENUM)> close) :
callback_(callback), close_(close), ptr_(nullptr)
{
}
Enumerator(const Enumerator& other) = default;
Enumerator& operator=(const Enumerator& other) = default;
~Enumerator()
{
close_(ptr_);
}
EnumeratorIterator<T> begin() const
{
return EnumeratorIterator<T>(this, S_OK);
}
EnumeratorIterator<T> end() const
{
return EnumeratorIterator<T>(this, S_FALSE);
}
HRESULT Next(T arr[], ULONG max, ULONG* cnt) const
{
return callback_(&ptr_, arr, max, cnt);
}
};
template <typename T>
class EnumeratorIterator
{
private:
const Enumerator<T>* enumerator_;
HRESULT status_ = S_FALSE;
T arr_[kEnumeratorMax]{};
ULONG idx_ = 0;
ULONG sz_ = 0;
public:
EnumeratorIterator(const Enumerator<T>* enumerator, HRESULT status) : enumerator_(enumerator)
{
if (status == S_OK)
{
status_ = enumerator_->Next(arr_, kEnumeratorMax, &sz_);
if (status_ == S_OK && sz_ == 0)
{
status_ = S_FALSE;
}
}
else
{
status_ = status;
}
}
bool operator!=(EnumeratorIterator const& other) const
{
return enumerator_ != other.enumerator_ || (status_ == S_OK) != (other.status_ == S_OK);
}
T const& operator*() const
{
return arr_[idx_];
}
EnumeratorIterator<T>& operator++()
{
if (idx_ < sz_ - 1)
{
idx_++;
}
else
{
idx_ = 0;
status_ = enumerator_->Next(arr_, kEnumeratorMax, &sz_);
if (status_ == S_OK && sz_ == 0)
{
status_ = S_FALSE;
}
}
return *this;
}
};
static Enumerator<mdTypeDef> EnumTypeDefs(const ComPtr<IMetaDataImport2>& metadata_import)
{
return Enumerator<mdTypeDef>(
[metadata_import](HCORENUM* ptr, mdTypeDef arr[], ULONG max, ULONG* cnt) -> HRESULT {
return metadata_import->EnumTypeDefs(ptr, arr, max, cnt);
},
[metadata_import](HCORENUM ptr) -> void { metadata_import->CloseEnum(ptr); });
}
static Enumerator<mdTypeRef> EnumTypeRefs(const ComPtr<IMetaDataImport2>& metadata_import)
{
return Enumerator<mdTypeRef>(
[metadata_import](HCORENUM* ptr, mdTypeRef arr[], ULONG max, ULONG* cnt) -> HRESULT {
return metadata_import->EnumTypeRefs(ptr, arr, max, cnt);
},
[metadata_import](HCORENUM ptr) -> void { metadata_import->CloseEnum(ptr); });
}
static Enumerator<mdModuleRef> EnumModuleRefs(const ComPtr<IMetaDataImport2>& metadata_import)
{
return Enumerator<mdModuleRef>(
[metadata_import](HCORENUM* ptr, mdModuleRef arr[], ULONG max, ULONG* cnt) -> HRESULT {
return metadata_import->EnumModuleRefs(ptr, arr, max, cnt);
},
[metadata_import](HCORENUM ptr) -> void { metadata_import->CloseEnum(ptr); });
}
static Enumerator<mdAssemblyRef> EnumAssemblyRefs(const ComPtr<IMetaDataAssemblyImport>& assembly_import)
{
return Enumerator<mdAssemblyRef>(
[assembly_import](HCORENUM* ptr, mdAssemblyRef arr[], ULONG max, ULONG* cnt) -> HRESULT {
return assembly_import->EnumAssemblyRefs(ptr, arr, max, cnt);
},
[assembly_import](HCORENUM ptr) -> void { assembly_import->CloseEnum(ptr); });
}
struct RuntimeInformation
{
COR_PRF_RUNTIME_TYPE runtime_type;
USHORT major_version;
USHORT minor_version;
USHORT build_version;
USHORT qfe_version;
RuntimeInformation() :
runtime_type((COR_PRF_RUNTIME_TYPE) 0x0), major_version(0), minor_version(0), build_version(0), qfe_version(0)
{
}
RuntimeInformation(COR_PRF_RUNTIME_TYPE runtime_type, USHORT major_version, USHORT minor_version,
USHORT build_version, USHORT qfe_version) :
runtime_type(runtime_type),
major_version(major_version),
minor_version(minor_version),
build_version(build_version),
qfe_version(qfe_version)
{
}
RuntimeInformation& operator=(const RuntimeInformation& other)
{
runtime_type = other.runtime_type;
major_version = other.major_version;
minor_version = other.minor_version;
build_version = other.build_version;
qfe_version = other.qfe_version;
return *this;
}
bool is_desktop() const
{
return runtime_type == COR_PRF_DESKTOP_CLR;
}
bool is_core() const
{
return runtime_type == COR_PRF_CORE_CLR;
}
};
struct AssemblyInfo
{
const AssemblyID id;
const WSTRING name;
const ModuleID manifest_module_id;
const AppDomainID app_domain_id;
const WSTRING app_domain_name;
AssemblyInfo() : id(0), name(EmptyWStr), manifest_module_id(0), app_domain_id(0), app_domain_name(EmptyWStr)
{
}
AssemblyInfo(AssemblyID id, WSTRING name, ModuleID manifest_module_id, AppDomainID app_domain_id,
WSTRING app_domain_name) :
id(id),
name(name),
manifest_module_id(manifest_module_id),
app_domain_id(app_domain_id),
app_domain_name(app_domain_name)
{
}
bool IsValid() const
{
return id != 0;
}
};
struct AssemblyMetadata
{
const ModuleID module_id;
const WSTRING name;
const mdAssembly assembly_token;
const Version version;
AssemblyMetadata() : module_id(0), name(EmptyWStr), assembly_token(mdTokenNil)
{
}
AssemblyMetadata(ModuleID module_id, WSTRING name, mdAssembly assembly_token, USHORT major, USHORT minor,
USHORT build, USHORT revision) :
module_id(module_id),
name(name),
assembly_token(assembly_token),
version(Version(major, minor, build, revision))
{
}
AssemblyMetadata(const AssemblyMetadata& value) :
module_id(value.module_id), name(value.name), assembly_token(value.assembly_token), version(value.version)
{
}
bool IsValid() const
{
return module_id != 0;
}
};
struct AssemblyProperty
{
const void* ppbPublicKey;
ULONG pcbPublicKey;
ULONG pulHashAlgId;
ASSEMBLYMETADATA pMetaData{};
WSTRING szName;
DWORD assemblyFlags = 0;
AssemblyProperty() : ppbPublicKey(nullptr), pcbPublicKey(0), pulHashAlgId(0), szName(EmptyWStr)
{
}
AssemblyProperty(WSTRING szName, void* ppbPublicKey, ULONG pcbPublicKey, ULONG pulHashAlgId, DWORD assemblyFlags) :
szName(szName),
ppbPublicKey(ppbPublicKey),
pcbPublicKey(pcbPublicKey),
pulHashAlgId(pulHashAlgId),
assemblyFlags(assemblyFlags)
{
}
public:
AssemblyProperty WithVersion(USHORT usMajorVersion, USHORT usMinorVersion, USHORT usBuildNumber, USHORT usRevisionNumber)
{
pMetaData.usMajorVersion = usMajorVersion;
pMetaData.usMinorVersion = usMinorVersion;
pMetaData.usBuildNumber = usBuildNumber;
pMetaData.usRevisionNumber = usRevisionNumber;
return *this;
}
};
struct ModuleInfo
{
const ModuleID id;
const WSTRING path;
const AssemblyInfo assembly;
const DWORD flags;
ModuleInfo() : id(0), path(EmptyWStr), assembly({}), flags(0)
{
}
ModuleInfo(ModuleID id, WSTRING path, AssemblyInfo assembly, DWORD flags) :
id(id), path(path), assembly(assembly), flags(flags)
{
}
bool IsValid() const
{
return id != 0;
}
bool IsWindowsRuntime() const
{
return ((flags & COR_PRF_MODULE_WINDOWS_RUNTIME) != 0);
}
bool IsNGEN() const
{
return ((flags & COR_PRF_MODULE_NGEN) != 0);
}
bool IsDynamic() const
{
return ((flags & COR_PRF_MODULE_DYNAMIC) != 0);
}
bool IsResource() const
{
return ((flags & COR_PRF_MODULE_RESOURCE) != 0);
}
};
struct TypeInfo
{
const mdToken id;
const WSTRING name;
const mdTypeSpec type_spec;
const ULONG32 token_type;
std::shared_ptr<TypeInfo> extend_from;
const bool valueType;
const bool isGeneric;
std::shared_ptr<TypeInfo> parent_type;
const mdToken scopeToken;
TypeInfo() :
id(0),
name(EmptyWStr),
type_spec(0),
token_type(0),
extend_from(nullptr),
valueType(false),
isGeneric(false),
parent_type(nullptr),
scopeToken(0)
{
}
TypeInfo(mdToken id, WSTRING name, mdTypeSpec type_spec, ULONG32 token_type, std::shared_ptr<TypeInfo> extend_from,
bool valueType, bool isGeneric, std::shared_ptr<TypeInfo> parent_type, mdToken scopeToken) :
id(id),
name(name),
type_spec(type_spec),
token_type(token_type),
extend_from(extend_from),
valueType(valueType),
isGeneric(isGeneric),
parent_type(parent_type),
scopeToken(scopeToken)
{
}
bool IsValid() const
{
return id != 0;
}
};
enum MethodArgumentTypeFlag
{
TypeFlagByRef = 0x01,
TypeFlagVoid = 0x02,
TypeFlagBoxedType = 0x04
};
// Represents a segment inside a larger signature (Method Signature / Local Var Signature) of
// an Argument, Local or Return Value.
struct TypeSignature
{
ULONG offset;
ULONG length;
PCCOR_SIGNATURE pbBase;
mdToken GetTypeTok(ComPtr<IMetaDataEmit2>& pEmit, mdAssemblyRef corLibRef) const;
WSTRING GetTypeTokName(ComPtr<IMetaDataImport2>& pImport) const;
std::tuple<unsigned, int> GetElementTypeAndFlags() const;
ULONG GetSignature(PCCOR_SIGNATURE& data) const;
};
struct FunctionMethodSignature
{
private:
PCCOR_SIGNATURE pbBase;
unsigned len;
ULONG numberOfTypeArguments = 0;
ULONG numberOfArguments = 0;
TypeSignature returnValue{};
std::vector<TypeSignature> params;
public:
FunctionMethodSignature() : pbBase(nullptr), len(0)
{
}
FunctionMethodSignature(PCCOR_SIGNATURE pb, unsigned cbBuffer)
{
pbBase = pb;
len = cbBuffer;
};
ULONG NumberOfTypeArguments() const
{
return numberOfTypeArguments;
}
ULONG NumberOfArguments() const
{
return numberOfArguments;
}
WSTRING str() const
{
return HexStr(pbBase, len);
}
TypeSignature GetReturnValue() const
{
return returnValue;
}
const std::vector<TypeSignature>& GetMethodArguments() const
{
return params;
}
HRESULT TryParse();
bool operator==(const FunctionMethodSignature& other) const
{
return memcmp(pbBase, other.pbBase, len);
}
CorCallingConvention CallingConvention() const
{
return CorCallingConvention(len == 0 ? 0 : pbBase[0]);
}
bool IsEmpty() const
{
return len == 0;
}
};
struct FunctionLocalSignature
{
private:
PCCOR_SIGNATURE pbBase;
unsigned len;
ULONG numberOfLocals = 0;
std::vector<TypeSignature> locals;
public:
FunctionLocalSignature() : pbBase(nullptr), len(0) {}
FunctionLocalSignature(PCCOR_SIGNATURE pb, unsigned cbBuffer, std::vector<TypeSignature>&& localsSigs)
: pbBase(pb)
, len(cbBuffer)
, numberOfLocals(static_cast<ULONG>(localsSigs.size()))
, locals(std::move(localsSigs))
{
}
ULONG NumberOfLocals() const
{
return numberOfLocals;
}
WSTRING str() const
{
return HexStr(pbBase, len);
}
const std::vector<TypeSignature>& GetMethodLocals() const
{
return locals;
}
static HRESULT TryParse(PCCOR_SIGNATURE pbBase, unsigned len, std::vector<TypeSignature>& locals);
bool operator==(const FunctionLocalSignature& other) const
{
return memcmp(pbBase, other.pbBase, len);
}
bool IsEmpty() const
{
return len == 0;
}
};
struct FunctionInfo
{
const mdToken id;
const WSTRING name;
const TypeInfo type;
const BOOL is_generic;
const MethodSignature signature;
const MethodSignature function_spec_signature;
const mdToken method_def_id;
FunctionMethodSignature method_signature;
FunctionInfo() : id(0), name(EmptyWStr), type({}), is_generic(false), method_def_id(0), method_signature({})
{
}
FunctionInfo(mdToken id, WSTRING name, TypeInfo type, MethodSignature signature,
MethodSignature function_spec_signature, mdToken method_def_id,
FunctionMethodSignature method_signature) :
id(id),
name(name),
type(type),
is_generic(true),
signature(signature),
function_spec_signature(function_spec_signature),
method_def_id(method_def_id),
method_signature(method_signature)
{
}
FunctionInfo(mdToken id, WSTRING name, TypeInfo type, MethodSignature signature,
FunctionMethodSignature method_signature) :
id(id),
name(name),
type(type),
is_generic(false),
signature(signature),
method_def_id(0),
method_signature(method_signature)
{
}
bool IsValid() const
{
return id != 0;
}
};
struct AssemblyVersionRedirection
{
// This struct is used to check and track version against ASSEMBLYMETADATA structs
// so it uses the same naming conventions, keeping fields with the same names in both
// structs.
USHORT usMajorVersion; // Major Version.
USHORT usMinorVersion; // Minor Version.
USHORT usBuildNumber; // Build Number.
USHORT usRevisionNumber; // Revision Number.
// Redirection related fields
ULONG ulRedirectionCount; // Tracks the number of times that the redirection was applied.
AssemblyVersionRedirection() :
usMajorVersion(0), usMinorVersion(0), usBuildNumber(0), usRevisionNumber(0), ulRedirectionCount(0)
{
}
AssemblyVersionRedirection(USHORT major, USHORT minor, USHORT build, USHORT revision) : ulRedirectionCount(0)
{
usMajorVersion = major;
usMinorVersion = minor;
usBuildNumber = build;
usRevisionNumber = revision;
}
int CompareToAssemblyVersion(const ASSEMBLYMETADATA& assembly)
{
return
usMajorVersion != assembly.usMajorVersion
? (usMajorVersion > assembly.usMajorVersion ? 1 : -1) :
usMinorVersion != assembly.usMinorVersion
? (usMinorVersion > assembly.usMinorVersion ? 1 : -1) :
usBuildNumber != assembly.usBuildNumber
? (usBuildNumber > assembly.usBuildNumber ? 1 : -1) :
usRevisionNumber != assembly.usRevisionNumber
? (usRevisionNumber > assembly.usRevisionNumber ? 1 : -1) :
0;
}
WSTRING VersionStr()
{
return trace::VersionStr(usMajorVersion, usMinorVersion, usBuildNumber, usRevisionNumber);
}
};
RuntimeInformation GetRuntimeInformation(ICorProfilerInfo7* info);
AssemblyInfo GetAssemblyInfo(ICorProfilerInfo7* info, const AssemblyID& assembly_id);
AssemblyMetadata GetAssemblyImportMetadata(const ComPtr<IMetaDataAssemblyImport>& assembly_import);
AssemblyMetadata GetReferencedAssemblyMetadata(const ComPtr<IMetaDataAssemblyImport>& assembly_import,
const mdAssemblyRef& assembly_ref);
FunctionInfo GetFunctionInfo(const ComPtr<IMetaDataImport2>& metadata_import, const mdToken& token);
ModuleInfo GetModuleInfo(ICorProfilerInfo7* info, const ModuleID& module_id);
TypeInfo GetTypeInfo(const ComPtr<IMetaDataImport2>& metadata_import, const mdToken& token);
mdAssemblyRef FindAssemblyRef(const ComPtr<IMetaDataAssemblyImport>& assembly_import, const WSTRING& assembly_name);
HRESULT GetCorLibAssemblyRef(const ComPtr<IMetaDataAssemblyEmit>& assembly_emit, AssemblyProperty& corAssemblyProperty,
mdAssemblyRef* corlib_ref);
bool FindTypeDefByName(const trace::WSTRING instrumentationTargetMethodTypeName, const trace::WSTRING assemblyName,
const ComPtr<IMetaDataImport2>& metadata_import, mdTypeDef& typeDef);
// FunctionMethodSignature
bool ParseByte(PCCOR_SIGNATURE& pbCur, PCCOR_SIGNATURE pbEnd, unsigned char* pbOut);
bool ParseNumber(PCCOR_SIGNATURE& pbCur, PCCOR_SIGNATURE pbEnd, unsigned* pOut);
bool ParseTypeDefOrRefEncoded(PCCOR_SIGNATURE& pbCur, PCCOR_SIGNATURE pbEnd, unsigned char* pIndexTypeOut,
unsigned* pIndexOut);
/* we don't support
PTR CustomMod* VOID
PTR CustomMod* Type
FNPTR MethodDefSig
FNPTR MethodRefSig
ARRAY Type ArrayShape
SZARRAY CustomMod+ Type (but we do support SZARRAY Type)
*/
bool ParseType(PCCOR_SIGNATURE& pbCur, PCCOR_SIGNATURE pbEnd);
// Param ::= CustomMod* ( TYPEDBYREF | [BYREF] Type )
// CustomMod* TYPEDBYREF we don't support
bool ParseParamOrLocal(PCCOR_SIGNATURE& pbCur, PCCOR_SIGNATURE pbEnd);
// RetType ::= CustomMod* ( VOID | TYPEDBYREF | [BYREF] Type )
// CustomMod* TYPEDBYREF we don't support
bool ParseRetType(PCCOR_SIGNATURE& pbCur, PCCOR_SIGNATURE pbEnd);
} // namespace trace
#endif // OTEL_CLR_PROFILER_CLR_HELPERS_H_