Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2c9e483

Browse files
authored
Return int.MaxValue in GC.GetGeneration(nongc_obj) and same for profiler's GetObjectGeneration (#85017)
1 parent e8c4971 commit 2c9e483

File tree

16 files changed

+222
-9
lines changed

16 files changed

+222
-9
lines changed

src/coreclr/gc/gc.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46118,8 +46118,16 @@ unsigned int GCHeap::WhichGeneration (Object* object)
4611846118
#ifdef FEATURE_BASICFREEZE
4611946119
if (!((o < g_gc_highest_address) && (o >= g_gc_lowest_address)))
4612046120
{
46121-
return max_generation;
46121+
return INT32_MAX;
46122+
}
46123+
#ifndef USE_REGIONS
46124+
if (GCHeap::IsInFrozenSegment (object))
46125+
{
46126+
// in case if the object belongs to an in-range frozen segment
46127+
// For regions those are never in-range.
46128+
return INT32_MAX;
4612246129
}
46130+
#endif
4612346131
#endif //FEATURE_BASICFREEZE
4612446132
gc_heap* hp = gc_heap::heap_of (o);
4612546133
unsigned int g = hp->object_gennum (o);
@@ -48971,6 +48979,9 @@ CFinalize::UpdatePromotedGenerations (int gen, BOOL gen_0_empty_p)
4897148979
int new_gen = g_theGCHeap->WhichGeneration (*po);
4897248980
if (new_gen != i)
4897348981
{
48982+
// We never promote objects to a non-GC heap
48983+
assert (new_gen <= max_generation);
48984+
4897448985
dprintf (3, ("Moving object %p->%p from gen %d to gen %d", po, *po, i, new_gen));
4897548986

4897648987
if (new_gen > i)

src/coreclr/gc/gcimpl.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,8 @@ class GCHeap : public IGCHeapInternal
150150
//Unregister an object for finalization
151151
void SetFinalizationRun (Object* obj);
152152

153-
//returns the generation number of an object (not valid during relocation)
153+
// returns the generation number of an object (not valid during relocation) or
154+
// INT32_MAX if the object belongs to a non-GC heap.
154155
unsigned WhichGeneration (Object* object);
155156
// returns TRUE is the object is ephemeral
156157
bool IsEphemeral (Object* object);

src/coreclr/gc/gcinterface.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,7 @@ class IGCHeap {
722722

723723
// Returns the generation in which obj is found. Also used by the VM
724724
// in some places, in particular syncblk code.
725+
// Returns INT32_MAX if obj belongs to a non-GC heap.
725726
virtual unsigned WhichGeneration(Object* obj) PURE_VIRTUAL
726727

727728
// Returns the number of GCs that have transpired in the given generation
@@ -991,7 +992,7 @@ void updateGCShadow(Object** ptr, Object* val);
991992
#define GC_CALL_INTERIOR 0x1
992993
#define GC_CALL_PINNED 0x2
993994

994-
// keep in sync with GC_ALLOC_FLAGS in GC.cs
995+
// keep in sync with GC_ALLOC_FLAGS in GC.CoreCLR.cs
995996
enum GC_ALLOC_FLAGS
996997
{
997998
GC_ALLOC_NO_FLAGS = 0,

src/coreclr/inc/corerror.xml

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,8 @@
198198

199199
<HRESULT NumericValue="0x80131058">
200200
<SymbolicName>COR_E_LOADING_REFERENCE_ASSEMBLY</SymbolicName>
201-
<Message>"Reference assemblies cannot not be loaded for execution."</Message>
202-
<Comment> Reference assemblies cannot not be loaded for execution. </Comment>
201+
<Message>"Reference assemblies cannot be loaded for execution."</Message>
202+
<Comment> Reference assemblies cannot be loaded for execution </Comment>
203203
</HRESULT>
204204

205205
<HRESULT NumericValue="0x8013106A">
@@ -1182,6 +1182,12 @@
11821182
<Comment> The runtime cannot be suspened since a suspension is already in progress. </Comment>
11831183
</HRESULT>
11841184

1185+
<HRESULT NumericValue="0x80131389">
1186+
<SymbolicName>CORPROF_E_NOT_GC_OBJECT</SymbolicName>
1187+
<Message>"This object belongs to a non-gc heap."</Message>
1188+
<Comment> This object belongs to a non-gc heap </Comment>
1189+
</HRESULT>
1190+
11851191
<HRESULT NumericValue="0x80131416">
11861192
<SymbolicName>CORSEC_E_POLICY_EXCEPTION</SymbolicName>
11871193
<Message>"PolicyException thrown."</Message>
@@ -1788,8 +1794,8 @@
17881794

17891795
<HRESULT NumericValue="0x80131c14">
17901796
<SymbolicName>CORDBG_E_NGEN_NOT_SUPPORTED</SymbolicName>
1791-
<Message>"NGEN must be supported to perform the requested operation."</Message>
1792-
<Comment> NGEN must be supported to perform the requested operation </Comment>
1797+
<Message>"NGEN is not supported."</Message>
1798+
<Comment> NGEN is not supported </Comment>
17931799
</HRESULT>
17941800

17951801
<HRESULT NumericValue="0x80131c15">

src/coreclr/pal/prebuilt/corerror/mscorurt.rc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ BEGIN
147147
MSG_FOR_URT_HR(CORPROF_E_NOT_YET_AVAILABLE) "Requested information is not yet available."
148148
MSG_FOR_URT_HR(CORPROF_E_TYPE_IS_PARAMETERIZED) "The given type is a generic and cannot be used with this method."
149149
MSG_FOR_URT_HR(CORPROF_E_FUNCTION_IS_PARAMETERIZED) "The given function is a generic and cannot be used with this method."
150+
MSG_FOR_URT_HR(CORPROF_E_NOT_GC_OBJECT) "This object belongs to a non-gc heap."
150151
MSG_FOR_URT_HR(CORSEC_E_POLICY_EXCEPTION) "PolicyException thrown."
151152
MSG_FOR_URT_HR(CORSEC_E_MIN_GRANT_FAIL) "Failed to grant minimum permission requests."
152153
MSG_FOR_URT_HR(CORSEC_E_NO_EXEC_PERM) "Failed to grant permission to execute."
@@ -288,6 +289,7 @@ BEGIN
288289
MSG_FOR_URT_HR(CORDBG_E_MISSING_DEBUGGER_EXPORTS) "The debuggee memory space does not have the expected debugging export table."
289290
MSG_FOR_URT_HR(CORDBG_E_DATA_TARGET_ERROR) "Failure when calling a data target method."
290291
MSG_FOR_URT_HR(CORDBG_E_UNSUPPORTED_DELEGATE) "The delegate contains a delegate currently not supported by the API."
292+
MSG_FOR_URT_HR(CORDBG_E_ASSEMBLY_UPDATES_APPLIED) "The operation is not supported because assembly updates have been applied."
291293
MSG_FOR_URT_HR(PEFMT_E_64BIT) "File is PE32+."
292294
MSG_FOR_URT_HR(PEFMT_E_32BIT) "File is PE32"
293295
MSG_FOR_URT_HR(CLR_E_BIND_ASSEMBLY_VERSION_TOO_LOW) "The bound assembly has a version that is lower than that of the request."

src/coreclr/pal/prebuilt/inc/corerror.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@
214214
#define CORDIAGIPC_E_UNKNOWN_MAGIC EMAKEHR(0x1386)
215215
#define CORDIAGIPC_E_UNKNOWN_ERROR EMAKEHR(0x1387)
216216
#define CORPROF_E_SUSPENSION_IN_PROGRESS EMAKEHR(0x1388)
217+
#define CORPROF_E_NOT_GC_OBJECT EMAKEHR(0x1389)
217218
#define CORSEC_E_POLICY_EXCEPTION EMAKEHR(0x1416)
218219
#define CORSEC_E_MIN_GRANT_FAIL EMAKEHR(0x1417)
219220
#define CORSEC_E_NO_EXEC_PERM EMAKEHR(0x1418)

src/coreclr/vm/proftoeeinterfaceimpl.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9141,6 +9141,15 @@ HRESULT ProfToEEInterfaceImpl::GetObjectGeneration(ObjectID objectId,
91419141

91429142
IGCHeap *hp = GCHeapUtilities::GetGCHeap();
91439143

9144+
if (hp->IsInFrozenSegment((Object*)objectId))
9145+
{
9146+
range->generation = (COR_PRF_GC_GENERATION)INT32_MAX;
9147+
range->rangeStart = 0;
9148+
range->rangeLength = 0;
9149+
range->rangeLengthReserved = 0;
9150+
return CORPROF_E_NOT_GC_OBJECT;
9151+
}
9152+
91449153
uint8_t* pStart;
91459154
uint8_t* pAllocated;
91469155
uint8_t* pReserved;

src/coreclr/vm/threads.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5533,6 +5533,7 @@ void ThreadStore::TriggerGCForDeadThreadsIfNecessary()
55335533
}
55345534

55355535
unsigned exposedObjectGeneration = gcHeap->WhichGeneration(exposedObject);
5536+
_ASSERTE(exposedObjectGeneration != INT32_MAX);
55365537
SIZE_T newDeadThreadGenerationCount = ++s_DeadThreadGenerationCounts[exposedObjectGeneration];
55375538
if (exposedObjectGeneration > gcGenerationToTrigger && newDeadThreadGenerationCount >= generationCountThreshold)
55385539
{

src/libraries/System.Runtime/tests/System/GCTests.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ public static void Collect_Int()
3535
{
3636
GC.Collect(i);
3737
}
38+
// Also, expect GC.Collect(int.MaxValue) to work without exception since int.MaxValue represents
39+
// a nongc heap generation (that is exactly what GC.GetGeneration returns for a non-gc heap object)
40+
GC.Collect(int.MaxValue);
3841
}
3942

4043
[Fact]

src/tests/nativeaot/SmokeTests/Preinitialization/Preinitialization.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -986,9 +986,9 @@ public static void Run()
986986
}
987987

988988
{
989-
// Expecting this to be a frozen array, and reported as Gen2 by the GC
989+
// Expecting this to be a frozen array, and reported as int.MaxValue by the GC
990990
object val = AccessArray<C1>();
991-
Assert.AreEqual(2, GC.GetGeneration(val));
991+
Assert.AreEqual(int.MaxValue, GC.GetGeneration(val));
992992

993993
val = typeof(ClassWithTemplate<>).MakeGenericType(typeof(C4)).GetField("Array").GetValue(null);
994994
Assert.AreEqual(0, GC.GetGeneration(val));

0 commit comments

Comments
 (0)