Skip to content

Commit 1704bfb

Browse files
authored
Fix GC handling string heap block (#2818)
1 parent 3f2e321 commit 1704bfb

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

src/CLR/Core/CLR_RT_HeapBlock.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2399,10 +2399,8 @@ void CLR_RT_HeapBlock::Relocate__HeapBlock()
23992399
void CLR_RT_HeapBlock::Relocate_String()
24002400
{
24012401
NATIVE_PROFILE_CLR_CORE();
2402-
CLR_RT_GarbageCollector::Heap_Relocate((void **)&m_data.string.m_text);
2403-
#if !defined(NANOCLR_NO_ASSEMBLY_STRINGS)
2404-
CLR_RT_GarbageCollector::Heap_Relocate((void **)&m_data.string.m_assm);
2405-
#endif
2402+
2403+
CLR_RT_GarbageCollector::Heap_Relocate((void **)&m_data);
24062404
}
24072405

24082406
void CLR_RT_HeapBlock::Relocate_Obj()

src/CLR/Core/CLR_RT_HeapBlock_String.cpp

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
CLR_RT_HeapBlock_String *CLR_RT_HeapBlock_String::CreateInstance(CLR_RT_HeapBlock &reference, CLR_UINT32 length)
1111
{
1212
NATIVE_PROFILE_CLR_CORE();
13+
14+
// compute required size for the string object (header + string length + null terminator)
1315
CLR_UINT32 totLength = sizeof(CLR_RT_HeapBlock_String) + length + 1;
1416
CLR_RT_HeapBlock_String *str;
1517

@@ -18,8 +20,11 @@ CLR_RT_HeapBlock_String *CLR_RT_HeapBlock_String::CreateInstance(CLR_RT_HeapBloc
1820
str = (CLR_RT_HeapBlock_String *)g_CLR_RT_ExecutionEngine.ExtractHeapBytesForObjects(DATATYPE_STRING, 0, totLength);
1921
if (str)
2022
{
23+
// grab a pointer to the string storage area (after the CLR_RT_HeapBlock_String header)
2124
char *szText = (char *)&str[1];
22-
szText[0] = 0;
25+
26+
// zero out the string storage area
27+
memset(szText, 0, CONVERTFROMSIZETOHEAPBLOCKS(totLength - sizeof(CLR_RT_HeapBlock_String)));
2328

2429
#if defined(NANOCLR_NO_ASSEMBLY_STRINGS)
2530
str->SetStringText(szText);
@@ -39,7 +44,9 @@ HRESULT CLR_RT_HeapBlock_String::CreateInstance(CLR_RT_HeapBlock &reference, con
3944
NANOCLR_HEADER();
4045

4146
if (!szText)
47+
{
4248
szText = "";
49+
}
4350

4451
NANOCLR_SET_AND_LEAVE(CLR_RT_HeapBlock_String::CreateInstance(reference, szText, (CLR_UINT32)hal_strlen_s(szText)));
4552

@@ -57,8 +64,10 @@ HRESULT CLR_RT_HeapBlock_String::CreateInstance(CLR_RT_HeapBlock &reference, con
5764
str = CreateInstance(reference, length);
5865
CHECK_ALLOCATION(str);
5966

67+
// grab a pointer to the string storage area (after the CLR_RT_HeapBlock_String header)
6068
szTextDst = (char *)str->StringText();
6169

70+
// copy the string to the storage area
6271
memcpy(szTextDst, szText, length);
6372
szTextDst[length] = 0;
6473

@@ -70,21 +79,27 @@ HRESULT CLR_RT_HeapBlock_String::CreateInstance(CLR_RT_HeapBlock &reference, con
7079
NATIVE_PROFILE_CLR_CORE();
7180
NANOCLR_HEADER();
7281

82+
#if defined(NANOCLR_NO_ASSEMBLY_STRINGS)
83+
84+
NANOCLR_CHECK_HRESULT(CLR_RT_HeapBlock_String::CreateInstance(reference, szText));
85+
86+
#else
87+
7388
CLR_RT_HeapBlock_String *str;
7489

7590
reference.SetObjectReference(NULL);
7691

92+
// get heap block for the string object
93+
// only the header is required as we're just pointing to the string stored in the assembly
7794
str = (CLR_RT_HeapBlock_String *)
7895
g_CLR_RT_ExecutionEngine.ExtractHeapBytesForObjects(DATATYPE_STRING, 0, sizeof(CLR_RT_HeapBlock_String));
7996
CHECK_ALLOCATION(str);
8097

8198
reference.SetObjectReference(str);
8299

83-
#if defined(NANOCLR_NO_ASSEMBLY_STRINGS)
84-
85-
NANOCLR_CHECK_HRESULT(CLR_RT_HeapBlock_String::CreateInstance(reference, szText));
86-
#else
100+
// store the pointers to the string and assembly
87101
str->SetStringText(szText, assm);
102+
88103
#endif
89104

90105
NANOCLR_NOCLEANUP();

0 commit comments

Comments
 (0)