!3657 修复OptMemoryContext在Debug模式下未正确设置哨兵导致内存检查失败的问题

Merge pull request !3657 from april01xxx/memopt_debug_fix
This commit is contained in:
opengauss_bot
2023-07-18 07:22:08 +00:00
committed by Gitee
2 changed files with 11 additions and 3 deletions

View File

@ -1603,10 +1603,14 @@ void GenericMemoryAllocator::AllocSetStats(MemoryContext context, int level)
void AllocSetCheckPointer(void* pointer)
{
AllocChunkData* chunk = (AllocChunkData*)(((char*)(pointer)) - ALLOC_CHUNKHDRSZ);
AllocMagicData* magic =
(AllocMagicData*)(((char*)chunk) + ALLOC_CHUNKHDRSZ + MAXALIGN(chunk->requested_size) - ALLOC_MAGICHDRSZ);
Assert(magic->aset == chunk->aset && magic->size == chunk->size && magic->posnum == PosmagicNum);
/* For opt memory context, we use sentinel instead of magic number to protect memory overflow. */
if (!IsOptAllocSetContext(chunk->aset)) {
AllocMagicData* magic =
(AllocMagicData*)(((char*)chunk) + ALLOC_CHUNKHDRSZ + MAXALIGN(chunk->requested_size) - ALLOC_MAGICHDRSZ);
Assert(magic->aset == chunk->aset && magic->size == chunk->size && magic->posnum == PosmagicNum);
}
}
/*

View File

@ -864,6 +864,8 @@ static void* opt_AllocSetRealloc(MemoryContext context, void* pointer, Size alig
if (oldsize >= size) {
#ifdef MEMORY_CONTEXT_CHECKING
chunk->requested_size = size;
if (size < oldsize)
set_sentinel(pointer, size);
#endif
return pointer;
}
@ -914,6 +916,8 @@ static void* opt_AllocSetRealloc(MemoryContext context, void* pointer, Size alig
chunk->size = chksize;
#ifdef MEMORY_CONTEXT_CHECKING
chunk->requested_size = size;
if (size < chunk->size)
set_sentinel(AllocChunkGetPointer(chunk), size);
#endif
return AllocChunkGetPointer(chunk);
}