MdePkg: Fix mismatched casts for PEI and DXE COPY_MEM and SET_MEM

The UEFI Platform Initialization Specification and the Unified Extensible
Firmware Interface (UEFI) Specification (respectively for PEI and DXE)
determine that the signature for these methods must have a VOID return value,
but in all cases fields of the given types are initialised from methods
matching the CopyMem and SetMem declarations in BaseMemoryLib.h, which
have a VOID * return value.

There is no guarantee that a function returning a pointer can be safely
called by a caller expecting it to return void, even if this works in
practice with the calling conventions used, so wrapper methods are more
correct.

Fixing this is required to be able to compile with cast-function-type-mismatch
warning enabled for Xcode and clang toolchains, but it is at least arguably
a genuine unwanted mismatch in any case.

Signed-off-by: Mike Beaton <mjsbeaton@gmail.com>
This commit is contained in:
Mike Beaton 2025-11-10 11:27:11 +00:00
parent 17f117e8d1
commit b41cf51a3a
5 changed files with 90 additions and 8 deletions

View File

@ -7,6 +7,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#include "DxeMain.h"
#include <Library/BaseEfiMemWrapper.h>
//
// DXE Core Global Variables for Protocols from PEI
@ -87,8 +88,8 @@ EFI_BOOT_SERVICES mBootServices = {
(EFI_INSTALL_MULTIPLE_PROTOCOL_INTERFACES)CoreInstallMultipleProtocolInterfaces, // InstallMultipleProtocolInterfaces
(EFI_UNINSTALL_MULTIPLE_PROTOCOL_INTERFACES)CoreUninstallMultipleProtocolInterfaces, // UninstallMultipleProtocolInterfaces
(EFI_CALCULATE_CRC32)CoreEfiNotAvailableYetArg3, // CalculateCrc32
(EFI_COPY_MEM)CopyMem, // CopyMem
(EFI_SET_MEM)SetMem, // SetMem
(EFI_COPY_MEM)EfiCopyMem, // CopyMem
(EFI_SET_MEM)EfiSetMem, // SetMem
(EFI_CREATE_EVENT_EX)CoreCreateEventEx // CreateEventEx
};

View File

@ -7,6 +7,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#include "PeiMain.h"
#include <Library/BaseEfiMemWrapper.h>
EFI_PEI_PPI_DESCRIPTOR mMemoryDiscoveredPpi = {
(EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
@ -48,8 +49,8 @@ EFI_PEI_SERVICES gPs = {
PeiInstallPeiMemory,
PeiAllocatePages,
PeiAllocatePool,
(EFI_PEI_COPY_MEM)CopyMem,
(EFI_PEI_SET_MEM)SetMem,
EfiCopyMem,
EfiSetMem,
PeiReportStatusCode,
PeiResetSystem,

View File

@ -0,0 +1,78 @@
/** @file
Provide strictly specification compliant instances of SetMem and CopyMem
for those interfaces which need it.
Copyright (c) 2025, TianoCore and contributors. All rights reserved.<BR>
SPDX-License-Identifier: BSD-2-Clause-Patent
*/
#ifndef __BASE_EFI_MEM_WRAPPER__
#define __BASE_EFI_MEM_WRAPPER__
#include "BaseMemoryLib.h"
/**
Copies a source buffer to a destination buffer, and returns the destination buffer.
This function copies Length bytes from SourceBuffer to DestinationBuffer, and returns
DestinationBuffer. The implementation must be reentrant, and it must handle the case
where SourceBuffer overlaps DestinationBuffer.
If Length is greater than (MAX_ADDRESS - DestinationBuffer + 1), then ASSERT().
If Length is greater than (MAX_ADDRESS - SourceBuffer + 1), then ASSERT().
@param DestinationBuffer The pointer to the destination buffer of the memory copy.
@param SourceBuffer The pointer to the source buffer of the memory copy.
@param Length The number of bytes to copy from SourceBuffer to DestinationBuffer.
@return None.
**/
STATIC
VOID
EFIAPI
EfiCopyMem (
IN VOID *Destination,
IN VOID *Source,
IN UINTN Length
)
{
CopyMem (
Destination,
Source,
Length
);
}
/**
Fills a target buffer with a byte value, and returns the target buffer.
This function fills Length bytes of Buffer with Value, and returns Buffer.
If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
@param Buffer The memory to set.
@param Length The number of bytes to set.
@param Value The value with which to fill Length bytes of Buffer.
@return None.
**/
STATIC
VOID
EFIAPI
EfiSetMem (
IN VOID *Buffer,
IN UINTN Size,
IN UINT8 Value
)
{
SetMem (
Buffer,
Size,
Value
);
}
#endif

View File

@ -9,6 +9,7 @@
**/
#include "UnitTestPeiServicesTablePointerLib.h"
#include <Library/BaseEfiMemWrapper.h>
///
/// Pei service instance
@ -39,8 +40,8 @@ EFI_PEI_SERVICES mPeiServices = {
UnitTestInstallPeiMemory, // InstallPeiMemory
UnitTestAllocatePages, // AllocatePages
UnitTestAllocatePool, // AllocatePool
(EFI_PEI_COPY_MEM)CopyMem,
(EFI_PEI_SET_MEM)SetMem,
EfiCopyMem,
EfiSetMem,
UnitTestReportStatusCode, // ReportStatusCode
UnitTestResetSystem, // ResetSystem

View File

@ -15,6 +15,7 @@
**/
#include "UnitTestUefiBootServicesTableLib.h"
#include <Library/BaseEfiMemWrapper.h>
EFI_HANDLE gImageHandle = NULL;
EFI_SYSTEM_TABLE *gST = NULL;
@ -68,8 +69,8 @@ STATIC EFI_BOOT_SERVICES mBootServices = {
(EFI_INSTALL_MULTIPLE_PROTOCOL_INTERFACES)UnitTestInstallMultipleProtocolInterfaces, // InstallMultipleProtocolInterfaces
(EFI_UNINSTALL_MULTIPLE_PROTOCOL_INTERFACES)UnitTestUninstallMultipleProtocolInterfaces, // UninstallMultipleProtocolInterfaces
(EFI_CALCULATE_CRC32)UnitTestCalculateCrc32, // CalculateCrc32
(EFI_COPY_MEM)CopyMem, // CopyMem
(EFI_SET_MEM)SetMem, // SetMem
(EFI_COPY_MEM)EfiCopyMem, // CopyMem
(EFI_SET_MEM)EfiSetMem, // SetMem
(EFI_CREATE_EVENT_EX)UnitTestCreateEventEx // CreateEventEx
};