ImageTool: Exclusively use OC/User allocators

This commit is contained in:
Marvin 2023-06-14 16:08:55 +02:00 committed by MikhailKrichanov
parent 5fb01f8f29
commit e234e4ea63
8 changed files with 95 additions and 44 deletions

View File

@ -159,7 +159,7 @@ ConstructHii (
CopyGuid (&HiiPackageListHeader.PackageListGuid, HiiGuid);
HiiPackageData = calloc (1, HiiPackageListHeader.PackageLength);
HiiPackageData = AllocateZeroPool (HiiPackageListHeader.PackageLength);
if (HiiPackageData == NULL) {
return RETURN_OUT_OF_RESOURCES;
}

View File

@ -29,7 +29,7 @@ ImageToolBufferExpand (
{
bool Overflow;
uint32_t NewAllocatedSize;
void *NewMemory;
uint8_t *NewMemory;
uint32_t Offset;
assert (Buffer->DataSize <= Buffer->AllocatedSize);
@ -56,12 +56,26 @@ ImageToolBufferExpand (
return MAX_UINT32;
}
NewMemory = realloc (Buffer->Memory, NewAllocatedSize);
NewMemory = AllocatePool (NewAllocatedSize);
if (NewMemory == NULL) {
DEBUG_RAISE ();
return MAX_UINT32;
}
if (Buffer->DataSize != 0) {
memmove (NewMemory, Buffer->Memory, Buffer->DataSize);
}
memset (
NewMemory + Buffer->DataSize,
0,
NewAllocatedSize - Buffer->DataSize
);
if (Buffer->Memory != NULL) {
FreePool (Buffer->Memory);
}
Buffer->Memory = NewMemory;
Buffer->AllocatedSize = NewAllocatedSize;
}
@ -181,12 +195,11 @@ ImageToolBufferDump (
DataSize = ImageToolBufferGetSize (Buffer);
Data = malloc (DataSize);
Data = AllocateCopyPool (DataSize, Buffer->Memory);
if (Data == NULL) {
return NULL;
}
memmove (Data, Buffer->Memory, DataSize);
*Size = DataSize;
return Data;
@ -197,6 +210,9 @@ ImageToolBufferFree (
image_tool_dynamic_buffer *Buffer
)
{
free (Buffer->Memory);
if (Buffer->Memory != NULL) {
FreePool (Buffer->Memory);
}
ImageToolBufferInit (Buffer);
}

View File

@ -579,7 +579,7 @@ CreateIntermediate (
return RETURN_VOLUME_CORRUPTED;
}
Segments = calloc (1, sizeof (*Segments) * ImageInfo->SegmentInfo.NumSegments);
Segments = AllocateZeroPool (sizeof (*Segments) * ImageInfo->SegmentInfo.NumSegments);
if (Segments == NULL) {
fprintf (stderr, "ImageTool: Could not allocate memory for Segments\n");
return RETURN_OUT_OF_RESOURCES;
@ -588,7 +588,7 @@ CreateIntermediate (
ImageInfo->SegmentInfo.Segments = Segments;
if (NumRelocs != 0) {
Relocs = calloc (1, sizeof (*Relocs) * NumRelocs);
Relocs = AllocateZeroPool (sizeof (*Relocs) * NumRelocs);
if (Relocs == NULL) {
fprintf (stderr, "ImageTool: Could not allocate memory for Relocs\n");
return RETURN_OUT_OF_RESOURCES;
@ -614,7 +614,7 @@ CreateIntermediate (
return RETURN_VOLUME_CORRUPTED;
}
Segments[SIndex].Name = calloc (1, strlen (Name) + 1);
Segments[SIndex].Name = AllocateZeroPool (strlen (Name) + 1);
if (Segments[SIndex].Name == NULL) {
fprintf (stderr, "ImageTool: Could not allocate memory for Segment #%d Name\n", Index);
return RETURN_OUT_OF_RESOURCES;
@ -628,7 +628,7 @@ CreateIntermediate (
Segments[SIndex].Write = (Shdr->sh_flags & SHF_WRITE) != 0;
Segments[SIndex].Execute = (Shdr->sh_flags & SHF_EXECINSTR) != 0;
Segments[SIndex].Data = calloc (1, Segments[SIndex].ImageSize);
Segments[SIndex].Data = AllocateZeroPool (Segments[SIndex].ImageSize);
if (Segments[SIndex].Data == NULL) {
fprintf (stderr, "ImageTool: Could not allocate memory for Segment #%d Data\n", Index);
return RETURN_OUT_OF_RESOURCES;
@ -704,11 +704,11 @@ ScanElf (
return RETURN_INCOMPATIBLE_VERSION;
}
ImageInfo->DebugInfo.SymbolsPath = malloc (ImageInfo->DebugInfo.SymbolsPathLen + 1);
ImageInfo->DebugInfo.SymbolsPath = AllocatePool (ImageInfo->DebugInfo.SymbolsPathLen + 1);
if (ImageInfo->DebugInfo.SymbolsPath == NULL) {
fprintf (stderr, "ImageTool: Could not allocate memory for Debug Data\n");
return RETURN_OUT_OF_RESOURCES;
};
}
if (SymbolsPath != NULL) {
memmove (ImageInfo->DebugInfo.SymbolsPath, SymbolsPath, ImageInfo->DebugInfo.SymbolsPathLen + 1);

View File

@ -358,16 +358,31 @@ ToolImageDestruct (
if (Image->SegmentInfo.Segments != NULL) {
for (Index = 0; Index < Image->SegmentInfo.NumSegments; ++Index) {
free (Image->SegmentInfo.Segments[Index].Name);
free (Image->SegmentInfo.Segments[Index].Data);
if (Image->SegmentInfo.Segments[Index].Name != NULL) {
FreePool (Image->SegmentInfo.Segments[Index].Name);
}
if (Image->SegmentInfo.Segments[Index].Data != NULL) {
FreePool (Image->SegmentInfo.Segments[Index].Data);
}
}
free (Image->SegmentInfo.Segments);
if (Image->SegmentInfo.Segments != NULL) {
FreePool (Image->SegmentInfo.Segments);
}
}
free (Image->HiiInfo.Data);
free (Image->RelocInfo.Relocs);
free (Image->DebugInfo.SymbolsPath);
if (Image->HiiInfo.Data != NULL) {
FreePool (Image->HiiInfo.Data);
}
if (Image->RelocInfo.Relocs != NULL) {
FreePool (Image->RelocInfo.Relocs);
}
if (Image->DebugInfo.SymbolsPath != NULL) {
FreePool (Image->DebugInfo.SymbolsPath);
}
memset (Image, 0, sizeof (*Image));
}
@ -688,7 +703,11 @@ ToolImageStripRelocs (
)
{
Image->RelocInfo.NumRelocs = 0;
free (Image->RelocInfo.Relocs);
if (Image->RelocInfo.Relocs != NULL) {
FreePool (Image->RelocInfo.Relocs);
}
Image->RelocInfo.Relocs = NULL;
Image->RelocInfo.RelocsStripped = TRUE;

View File

@ -51,7 +51,7 @@ HiiSrc (
FilePtr = fopen (HiiName, "w");
if (FilePtr == NULL) {
free (Hii);
FreePool (Hii);
return RETURN_NO_MEDIA;
}
@ -109,7 +109,7 @@ HiiSrc (
" (CONST MODULE_HII_PACKAGE_LIST *)&mModuleHiiPackageList;\n"
);
free (Hii);
FreePool (Hii);
return RETURN_SUCCESS;
}
@ -417,7 +417,7 @@ GenExecutable (
UserWriteFile (OutputFileName, OutputFile, OutputFileSize);
free (OutputFile);
FreePool (OutputFile);
return RETURN_SUCCESS;
}

View File

@ -595,14 +595,12 @@ ToolImageEmitPeFile (
return false;
}
PeHdr = malloc (SizeOfPeHeaders);
PeHdr = AllocateZeroPool (SizeOfPeHeaders);
if (PeHdr == NULL) {
DEBUG_RAISE ();
return false;
}
memset (PeHdr, 0, SizeOfPeHeaders);
SectionHeaders = (EFI_IMAGE_SECTION_HEADER *)((UINT8 *)PeHdr + SectionHeadersOffset);
PeHdr->CommonHeader.Signature = EFI_IMAGE_NT_SIGNATURE;
@ -629,14 +627,14 @@ ToolImageEmitPeFile (
Success = ToolImageEmitPeSections (Buffer, PeHdr, SectionHeaders, Image, Xip);
if (!Success) {
free (PeHdr);
FreePool (PeHdr);
DEBUG_RAISE ();
return false;
}
BufferPeHdr = ImageToolBufferGetPointer (Buffer, PeOffset);
memmove (BufferPeHdr, PeHdr, SizeOfPeHeaders);
free (PeHdr);
FreePool (PeHdr);
BufferPeHdr = NULL;
return true;

View File

@ -29,6 +29,7 @@ ScanPeGetRelocInfo (
const char *ImageBuffer;
UINT16 RelocType;
UINT16 RelocOffset;
uint32_t ToolRelocsSize;
// FIXME: PE/COFF context access
RelocBlockRva = Context->RelocDirRva;
@ -40,7 +41,22 @@ ScanPeGetRelocInfo (
return true;
}
RelocInfo->Relocs = calloc (RelocDirSize / sizeof (UINT16), sizeof (*RelocInfo->Relocs));
STATIC_ASSERT (
sizeof (*RelocInfo->Relocs) % sizeof (UINT16) == 0,
"The division below is inaccurate."
);
Overflow = BaseOverflowMulU32 (
RelocDirSize,
sizeof (*RelocInfo->Relocs) / sizeof (UINT16),
&ToolRelocsSize
);
if (Overflow) {
DEBUG_RAISE ();
return false;
}
RelocInfo->Relocs = AllocateZeroPool (ToolRelocsSize);
if (RelocInfo->Relocs == NULL) {
fprintf (stderr, "ImageTool: Could not allocate memory for Relocs[]\n");
return false;
@ -157,7 +173,14 @@ ScanPeGetSegmentInfo (
NumSections = PeCoffGetSectionTable (Context, &Section);
SegmentInfo->Segments = calloc (NumSections, sizeof (*SegmentInfo->Segments));
STATIC_ASSERT (
sizeof (*SegmentInfo->Segments) <= sizeof (*Section),
"The multiplication below may overflow."
);
SegmentInfo->Segments = AllocateZeroPool (
NumSections * sizeof (*SegmentInfo->Segments)
);
if (SegmentInfo->Segments == NULL) {
fprintf (stderr, "ImageTool: Could not allocate memory for Segments[]\n");
return false;
@ -178,7 +201,7 @@ ScanPeGetSegmentInfo (
&& memcmp (Section->Name, PE_COFF_SECT_NAME_RELOC, sizeof (Section->Name)) != 0
&& memcmp (Section->Name, PE_COFF_SECT_NAME_RESRC, sizeof (Section->Name)) != 0
&& memcmp (Section->Name, PE_COFF_SECT_NAME_DEBUG, sizeof (Section->Name)) != 0) {
ImageSegment->Name = calloc (1, sizeof (Section->Name));
ImageSegment->Name = AllocateZeroPool (sizeof (Section->Name));
if (ImageSegment->Name == NULL) {
fprintf (stderr, "ImageTool: Could not allocate memory for Segment Name\n");
return false;
@ -192,19 +215,16 @@ ScanPeGetSegmentInfo (
ImageSegment->Write = (Section->Characteristics & EFI_IMAGE_SCN_MEM_WRITE) != 0;
ImageSegment->Execute = (Section->Characteristics & EFI_IMAGE_SCN_MEM_EXECUTE) != 0;
ImageSegment->Data = malloc (ImageSegment->ImageSize);
ImageSegment->Data = AllocateCopyPool (
ImageSegment->ImageSize,
ImageBuffer + Section->VirtualAddress
);
if (ImageSegment->Data == NULL) {
fprintf (stderr, "ImageTool: Could not allocate memory for Segment Data\n");
free (ImageSegment->Name);
FreePool (ImageSegment->Name);
return false;
}
memmove (
ImageSegment->Data,
ImageBuffer + Section->VirtualAddress,
ImageSegment->ImageSize
);
++SegmentInfo->NumSegments;
++ImageSegment;
}

View File

@ -115,13 +115,12 @@ ScanUefiImageGetDebugInfo (
assert (SymbolsPathSize >= 1);
DebugInfo->SymbolsPath = malloc (SymbolsPathSize + 1);
DebugInfo->SymbolsPath = AllocateCopyPool (SymbolsPathSize, SymbolsPath);
if (DebugInfo->SymbolsPath == NULL) {
fprintf (stderr, "ImageTool: Could not allocate memory for SymbolsPath\n");
return false;
}
memmove (DebugInfo->SymbolsPath, SymbolsPath, SymbolsPathSize);
assert (DebugInfo->SymbolsPath[SymbolsPathSize - 1] == '\0');
DebugInfo->SymbolsPathLen = SymbolsPathSize - 1;
@ -149,15 +148,14 @@ ScanUefiImageGetHiiInfo (
return false;
}
HiiInfo->Data = malloc (HiiSize);
ImageBuffer = (char *)UefiImageLoaderGetImageAddress (Context);
HiiInfo->Data = AllocateCopyPool (HiiSize, ImageBuffer + HiiRva);
if (HiiInfo->Data == NULL) {
fprintf (stderr, "ImageTool: Could not allocate memory for HiiInfo Data\n");
return false;
}
ImageBuffer = (char *)UefiImageLoaderGetImageAddress (Context);
memmove (HiiInfo->Data, ImageBuffer + HiiRva, HiiSize);
HiiInfo->DataSize = HiiSize;
return true;