Skip to content

Commit

Permalink
[Media Common] [VP] Add Surface Check in MediaMemoryCopy
Browse files Browse the repository at this point in the history
Add surface check in MediaMemoryCopy in case that the source surface and target surface are invalid, which may cause page fault
  • Loading branch information
peiyigu-intel authored and intel-mediadev committed Jul 6, 2023
1 parent 98192be commit 7e19d7f
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 4 deletions.
17 changes: 17 additions & 0 deletions media_common/agnostic/common/os/mos_os.h
Original file line number Diff line number Diff line change
Expand Up @@ -1004,6 +1004,10 @@ typedef struct _MOS_INTERFACE
uint32_t bpp,
bool bOutputCompressed);

MOS_STATUS (*pfnVerifyMosSurface) (
PMOS_SURFACE mosSurface,
bool &bIsValid);

MOS_STATUS(*pfnGetMosContext) (
PMOS_INTERFACE pOsInterface,
PMOS_CONTEXT* mosContext);
Expand Down Expand Up @@ -2180,6 +2184,19 @@ __inline void Mos_SetVirtualEngineSupported(PMOS_INTERFACE pOsInterface, bool bE
}
}

//!
//! \brief Check whether the parameter of mos surface is valid for copy
//!
//! \param [in] mosSurface
//! Pointer to MosSurface
//!
//! \return bool
//! Whether the paramter of mosSurface is valid
//!
MOS_STATUS Mos_VerifyMosSurface(
PMOS_SURFACE mosSurface,
bool &bIsValid);

//!
//! \brief Check virtual engine is supported
//! \details Check virtual engine is supported
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,9 @@ MOS_STATUS MediaVeboxDecompState::MediaMemoryCopy(
PMOS_RESOURCE outputResource,
bool outputCompressed)
{
MOS_STATUS eStatus = MOS_STATUS_SUCCESS;
MOS_STATUS eStatus = MOS_STATUS_SUCCESS;
bool bValidInputSurface = false;
bool bValidOutputSurface = false;

MHW_FUNCTION_ENTER;

Expand Down Expand Up @@ -309,6 +311,14 @@ MOS_STATUS MediaVeboxDecompState::MediaMemoryCopy(
//Get context before proceeding
auto gpuContext = m_osInterface->CurrentGpuContextOrdinal;

//Check whether surface is valid, or it will cause page fault
m_osInterface->pfnVerifyMosSurface(&sourceSurface, bValidInputSurface);
m_osInterface->pfnVerifyMosSurface(&targetSurface, bValidOutputSurface);
if (!bValidInputSurface || !bValidOutputSurface)
{
VPHAL_MEMORY_DECOMP_CHK_STATUS_RETURN(MOS_STATUS_INVALID_PARAMETER);
}

// Sync for Vebox read
m_osInterface->pfnSyncOnResource(
m_osInterface,
Expand Down Expand Up @@ -342,7 +352,9 @@ MOS_STATUS MediaVeboxDecompState::MediaMemoryCopy2D(
uint32_t bpp,
bool outputCompressed)
{
MOS_STATUS eStatus = MOS_STATUS_SUCCESS;
MOS_STATUS eStatus = MOS_STATUS_SUCCESS;
bool bValidInputSurface = false;
bool bValidOutputSurface = false;

MHW_FUNCTION_ENTER;

Expand Down Expand Up @@ -427,6 +439,14 @@ MOS_STATUS MediaVeboxDecompState::MediaMemoryCopy2D(
targetSurface.dwWidth = copyWidth / pixelInByte;
targetSurface.dwHeight = copyHeight;

//Check whether surface is valid, or it will cause page fault
m_osInterface->pfnVerifyMosSurface(&sourceSurface, bValidInputSurface);
m_osInterface->pfnVerifyMosSurface(&targetSurface, bValidOutputSurface);
if (!bValidInputSurface || !bValidOutputSurface)
{
VPHAL_MEMORY_DECOMP_CHK_STATUS_RETURN(MOS_STATUS_INVALID_PARAMETER);
}

// Sync for Vebox write
m_osInterface->pfnSyncOnResource(
m_osInterface,
Expand Down
13 changes: 13 additions & 0 deletions media_softlet/agnostic/common/os/mos_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -1690,6 +1690,19 @@ class MosInterface
uint32_t bpp,
bool outputCompressed);

//!
//! \brief Check whether the parameter of mos surface is valid for copy
//!
//! \param [in] mosSurface
//! Pointer to MosSurface
//!
//! \return bool
//! Whether the paramter of mosSurface is valid
//!
static MOS_STATUS VerifyMosSurface(
PMOS_SURFACE mosSurface,
bool &bIsValid);

// GPU Status interfaces
//!
//! \brief Get Gpu Status Tag
Expand Down
8 changes: 8 additions & 0 deletions media_softlet/agnostic/common/os/mos_os.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,7 @@ MOS_STATUS Mos_InitOsInterface(
pOsInterface->pfnGetResourceHandle = Mos_GetResourceHandle;
pOsInterface->pfnGetRtLogResourceInfo = Mos_GetRtLogResourceInfo;
pOsInterface->pfnResetResource = Mos_ResetMosResource;
pOsInterface->pfnVerifyMosSurface = Mos_VerifyMosSurface;

pOsInterface->pfnCreateMhwCpInterface = Create_MhwCpInterface;
pOsInterface->pfnDeleteMhwCpInterface = Delete_MhwCpInterface;
Expand Down Expand Up @@ -1120,6 +1121,13 @@ uint64_t Mos_GetResourceHandle(
return MosInterface::GetResourceHandle(streamState, osResource);
}

MOS_STATUS Mos_VerifyMosSurface(
PMOS_SURFACE mosSurface,
bool& bIsValid)
{
return MosInterface::VerifyMosSurface(mosSurface, bIsValid);
}

void Mos_GetRtLogResourceInfo(
PMOS_INTERFACE osInterface,
PMOS_RESOURCE &osResource,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,9 @@ MOS_STATUS MediaMemDeCompNext::MemoryDecompress(PMOS_RESOURCE targetResource)

MOS_STATUS MediaMemDeCompNext::MediaMemoryCopy(PMOS_RESOURCE inputResource, PMOS_RESOURCE outputResource, bool outputCompressed)
{
MOS_STATUS eStatus = MOS_STATUS_SUCCESS;
MOS_STATUS eStatus = MOS_STATUS_SUCCESS;
bool bValidInputSurface = false;
bool bValidOutputSurface = false;

MHW_FUNCTION_ENTER;

Expand Down Expand Up @@ -232,6 +234,14 @@ MOS_STATUS MediaMemDeCompNext::MediaMemoryCopy(PMOS_RESOURCE inputResource, PMOS
return eStatus;
}

//Check whether surface is valid, or it will cause page fault
m_osInterface->pfnVerifyMosSurface(&sourceSurface, bValidInputSurface);
m_osInterface->pfnVerifyMosSurface(&targetSurface, bValidOutputSurface);
if (!bValidInputSurface || !bValidOutputSurface)
{
VPHAL_MEMORY_DECOMP_CHK_STATUS_RETURN(MOS_STATUS_INVALID_PARAMETER);
}

//Get context before proceeding
auto gpuContext = m_osInterface->CurrentGpuContextOrdinal;

Expand All @@ -257,7 +267,9 @@ MOS_STATUS MediaMemDeCompNext::MediaMemoryCopy(PMOS_RESOURCE inputResource, PMOS

MOS_STATUS MediaMemDeCompNext::MediaMemoryCopy2D(PMOS_RESOURCE inputResource, PMOS_RESOURCE outputResource, uint32_t copyWidth, uint32_t copyHeight, uint32_t copyInputOffset, uint32_t copyOutputOffset, uint32_t bpp, bool outputCompressed)
{
MOS_STATUS eStatus = MOS_STATUS_SUCCESS;
MOS_STATUS eStatus = MOS_STATUS_SUCCESS;
bool bValidInputSurface = false;
bool bValidOutputSurface = false;

MHW_FUNCTION_ENTER;

Expand Down Expand Up @@ -342,6 +354,14 @@ MOS_STATUS MediaMemDeCompNext::MediaMemoryCopy2D(PMOS_RESOURCE inputResource, PM
targetSurface.dwWidth = copyWidth / pixelInByte;
targetSurface.dwHeight = copyHeight;

//Check whether surface is valid, or it will cause page fault
m_osInterface->pfnVerifyMosSurface(&sourceSurface, bValidInputSurface);
m_osInterface->pfnVerifyMosSurface(&targetSurface, bValidOutputSurface);
if (!bValidInputSurface || !bValidOutputSurface)
{
VPHAL_MEMORY_DECOMP_CHK_STATUS_RETURN(MOS_STATUS_INVALID_PARAMETER);
}

// Sync for Vebox write
m_osInterface->pfnSyncOnResource(
m_osInterface,
Expand Down
29 changes: 29 additions & 0 deletions media_softlet/linux/common/os/mos_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2464,6 +2464,35 @@ MOS_STATUS MosInterface::DoubleBufferCopyResource(
return status;
}

MOS_STATUS MosInterface::VerifyMosSurface(
PMOS_SURFACE mosSurface,
bool &bIsValid)
{
MOS_OS_FUNCTION_ENTER;

MOS_OS_CHK_NULL_RETURN(mosSurface);
MOS_OS_CHK_NULL_RETURN(mosSurface->OsResource.pGmmResInfo);

if ((mosSurface->dwPitch * mosSurface->dwHeight > mosSurface->OsResource.pGmmResInfo->GetSizeMainSurface() && (mosSurface->Type != MOS_GFXRES_BUFFER)) ||
(mosSurface->dwPitch > mosSurface->OsResource.pGmmResInfo->GetSizeMainSurface() && (mosSurface->Type == MOS_GFXRES_BUFFER)) ||
mosSurface->dwHeight == 0 ||
mosSurface->dwPitch == 0)
{
bIsValid = false;
MOS_OS_ASSERTMESSAGE("Invalid arguments for mos surface copy: dwPitch %lu, dwHeight %lu, gmmMainSurfaceSize %llu, GFXRES Type %d",
mosSurface->dwPitch,
mosSurface->dwHeight,
mosSurface->OsResource.pGmmResInfo->GetSizeMainSurface(),
mosSurface->Type);
}
else
{
bIsValid = true;
}

return MOS_STATUS_SUCCESS;
}

MOS_STATUS MosInterface::MediaCopyResource2D(
MOS_STREAM_HANDLE streamState,
MOS_RESOURCE_HANDLE inputResource,
Expand Down

0 comments on commit 7e19d7f

Please sign in to comment.