Skip to content

Commit fd4d0a9

Browse files
authored
[cdac] Tighten cdac_data friend declarations (#108525)
tighten cdac_data friend declarations Previously classes exposed internals to all cdac_data specializations. By changing the friend declarations to the exact specialization which needs will access the internals, we can better verify where the internals are accessed and ensure cdac_data<T> accesses the correct class.
1 parent 0ab7efe commit fd4d0a9

File tree

11 files changed

+39
-31
lines changed

11 files changed

+39
-31
lines changed

src/coreclr/inc/slist.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ class SList
120120
PTR_SLink m_pHead;
121121
PTR_SLink m_pTail;
122122

123+
// as a generic data structure, friend to all specializations of cdac_data
123124
template<typename U> friend struct ::cdac_data;
124125

125126
// get the list node within the object

src/coreclr/vm/cdacdata.h

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,23 @@
77
// See datadescriptor.h
88
//
99
// This struct enables exposing information that is private to a class to the cDAC. For example,
10-
// if class C has private information that must be provided, declare cdac_data<T> as a friend:
10+
// if class C has private information that must be provided, declare cdac_data<D> as a friend of C
11+
// where D is the specialization of cdac_data that will expose the information. Then provide a
12+
// specialization cdac_data<D> with constexpr members exposing the information.
1113
//
12-
// template<typename T> friend struct ::cdac_data;
14+
// Note: in the common case, type D will be type C.
1315
//
14-
// and provide a specialization cdac_data<C> with constexpr members exposing the information.
15-
// For example, if the offset of field F is required:
16+
// For example, if the offset of field F in class C is required:
17+
//
18+
// class C {
19+
// private:
20+
// int F;
21+
// friend struct ::cdac_data<C>;
22+
// };
23+
// template<> struct cdac_data<C> {
24+
// static constexpr size_t F_Offset = offsetof(C, F);
25+
// };
1626
//
17-
// template<> struct cdac_data<C> {
18-
// static constexpr size_t F_Offset = offsetof(C, F);
19-
// };
2027
template<typename T>
2128
struct cdac_data
2229
{

src/coreclr/vm/ceeload.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ struct DynamicMetadata
9090
{
9191
uint32_t Size;
9292
BYTE Data[0];
93-
template<typename T> friend struct ::cdac_data;
93+
friend struct ::cdac_data<DynamicMetadata>;
9494
};
9595

9696
template<>
@@ -1634,7 +1634,7 @@ class Module : public ModuleBase
16341634
uint32_t GetNativeMetadataAssemblyCount();
16351635
#endif // !defined(DACCESS_COMPILE)
16361636

1637-
template<typename T> friend struct ::cdac_data;
1637+
friend struct ::cdac_data<Module>;
16381638
};
16391639

16401640
template<>

src/coreclr/vm/class.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1798,7 +1798,7 @@ class EEClass // DO NOT CREATE A NEW EEClass USING NEW!
17981798
}
17991799
#endif // !DACCESS_COMPILE
18001800

1801-
template<typename T> friend struct ::cdac_data;
1801+
friend struct ::cdac_data<EEClass>;
18021802
};
18031803

18041804
template<> struct cdac_data<EEClass>
@@ -1974,7 +1974,7 @@ class ArrayClass : public EEClass
19741974
BOOL fForStubAsIL
19751975
);
19761976

1977-
template<typename T> friend struct ::cdac_data;
1977+
friend struct ::cdac_data<ArrayClass>;
19781978
};
19791979

19801980
template<> struct cdac_data<ArrayClass>

src/coreclr/vm/exstate.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class ThreadExceptionState
5151
// ExceptionTracker or the ExInfo as appropriate for the platform
5252
friend class ProfToEEInterfaceImpl;
5353

54-
template<typename T> friend struct ::cdac_data;
54+
friend struct ::cdac_data<Thread>;
5555

5656
#ifdef FEATURE_EH_FUNCLETS
5757
friend class ExceptionTracker;

src/coreclr/vm/method.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1907,7 +1907,7 @@ class MethodDesc
19071907
static void Init();
19081908
#endif
19091909

1910-
template<typename T> friend struct ::cdac_data;
1910+
friend struct ::cdac_data<MethodDesc>;
19111911
};
19121912

19131913
template<> struct cdac_data<MethodDesc>
@@ -2349,7 +2349,7 @@ class MethodDescChunk
23492349

23502350
// Followed by array of method descs...
23512351

2352-
template<typename T> friend struct ::cdac_data;
2352+
friend struct ::cdac_data<MethodDescChunk>;
23532353
};
23542354

23552355
template<>
@@ -2436,7 +2436,7 @@ class StoredSigMethodDesc : public MethodDesc
24362436
#ifdef DACCESS_COMPILE
24372437
void EnumMemoryRegions(CLRDataEnumMemoryFlags flags);
24382438
#endif
2439-
template<typename T> friend struct ::cdac_data;
2439+
friend struct ::cdac_data<StoredSigMethodDesc>;
24402440
};
24412441

24422442
template<>
@@ -2694,7 +2694,7 @@ class DynamicMethodDesc : public StoredSigMethodDesc
26942694
// following implementations defined in DynamicMethod.cpp
26952695
//
26962696
void Destroy();
2697-
template<typename T> friend struct ::cdac_data;
2697+
friend struct ::cdac_data<DynamicMethodDesc>;
26982698
};
26992699

27002700
template<>
@@ -3453,7 +3453,7 @@ class InstantiatedMethodDesc final : public MethodDesc
34533453
MethodDesc* pSharedMDescForStub,
34543454
Instantiation methodInst,
34553455
BOOL getSharedNotStub);
3456-
template<typename T> friend struct ::cdac_data;
3456+
friend struct ::cdac_data<InstantiatedMethodDesc>;
34573457
};
34583458

34593459
template<> struct cdac_data<InstantiatedMethodDesc>

src/coreclr/vm/methodtable.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3980,7 +3980,7 @@ public :
39803980

39813981
static void GetStaticsOffsets(StaticsOffsetType staticsOffsetType, bool fGenericsStatics, uint32_t *dwGCOffset, uint32_t *dwNonGCOffset);
39823982

3983-
template<typename T> friend struct ::cdac_data;
3983+
friend struct ::cdac_data<MethodTable>;
39843984
}; // class MethodTable
39853985

39863986
template<> struct cdac_data<MethodTable>

src/coreclr/vm/object.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ class Object
462462
private:
463463
VOID ValidateInner(BOOL bDeep, BOOL bVerifyNextHeader, BOOL bVerifySyncBlock);
464464

465-
template<typename T> friend struct ::cdac_data;
465+
friend struct ::cdac_data<Object>;
466466
};
467467

468468
template<>
@@ -638,7 +638,7 @@ class ArrayBase : public Object
638638
inline static unsigned GetBoundsOffset(MethodTable* pMT);
639639
inline static unsigned GetLowerBoundsOffset(MethodTable* pMT);
640640

641-
template<typename T> friend struct ::cdac_data;
641+
friend struct ::cdac_data<ArrayBase>;
642642
};
643643

644644
#ifndef DACCESS_COMPILE
@@ -950,7 +950,7 @@ class StringObject : public Object
950950
static STRINGREF* EmptyStringRefPtr;
951951
static bool EmptyStringIsFrozen;
952952

953-
template<typename T> friend struct ::cdac_data;
953+
friend struct ::cdac_data<StringObject>;
954954
};
955955

956956
template<>
@@ -2437,7 +2437,7 @@ class ExceptionObject : public Object
24372437
INT32 _xcode;
24382438
INT32 _HResult;
24392439

2440-
template<typename T> friend struct ::cdac_data;
2440+
friend struct ::cdac_data<ExceptionObject>;
24412441
};
24422442

24432443
template<>

src/coreclr/vm/syncblk.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -970,7 +970,7 @@ class InteropSyncBlockInfo
970970
BYTE m_taggedAlloc[2 * sizeof(void*)];
971971
#endif // FEATURE_OBJCMARSHAL
972972

973-
template<typename T> friend struct ::cdac_data;
973+
friend struct ::cdac_data<InteropSyncBlockInfo>;
974974
};
975975

976976
template<>
@@ -1286,7 +1286,7 @@ class SyncBlock
12861286
}
12871287
#endif // defined(ENABLE_CONTRACTS_IMPL)
12881288

1289-
template<typename T> friend struct ::cdac_data;
1289+
friend struct ::cdac_data<SyncBlock>;
12901290
};
12911291

12921292
template<>
@@ -1674,7 +1674,7 @@ class ObjHeader
16741674

16751675
BOOL Validate (BOOL bVerifySyncBlkIndex = TRUE);
16761676

1677-
template<typename T> friend struct ::cdac_data;
1677+
friend struct ::cdac_data<ObjHeader>;
16781678
};
16791679

16801680
template<>

src/coreclr/vm/threads.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3965,7 +3965,7 @@ class Thread
39653965
private:
39663966
bool m_hasPendingActivation;
39673967

3968-
template<typename T> friend struct ::cdac_data;
3968+
friend struct ::cdac_data<Thread>;
39693969
};
39703970

39713971
template<>
@@ -4247,7 +4247,7 @@ class ThreadStore
42474247
bool ShouldTriggerGCForDeadThreads();
42484248
void TriggerGCForDeadThreadsIfNecessary();
42494249

4250-
template<typename T> friend struct ::cdac_data;
4250+
friend struct ::cdac_data<ThreadStore>;
42514251
};
42524252

42534253
template<>

0 commit comments

Comments
 (0)