add explicit barriers for buffer overread and overrwrite (#6484)

Co-authored-by: Ori Levari <orlevari@microsoft.com>
This commit is contained in:
Ori Levari 2021-01-29 14:59:56 -08:00 committed by GitHub
parent 7f5731741d
commit 76f5d9edc6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 8 deletions

View file

@ -41,7 +41,6 @@ namespace ImageTestHelper {
wf::IMemoryBufferReference reference = spBitmapBuffer.CreateReference();
auto spByteAccess = reference.as<::Windows::Foundation::IMemoryBufferByteAccess>();
spByteAccess->GetBuffer(&pData, &size);
uint32_t height = softwareBitmap.PixelHeight();
uint32_t width = softwareBitmap.PixelWidth();
@ -53,14 +52,15 @@ namespace ImageTestHelper {
com_ptr<ITensorNative> itn = tf.as<ITensorNative>();
itn->GetBuffer(reinterpret_cast<BYTE**>(&pCPUTensor), &uCapacity);
if (BitmapPixelFormat::Bgra8 == GetPixelFormat(modelPixelFormat)) {
for (UINT32 i = 0; i < size; i += 4) {
// loop condition is i < size - 2 to avoid potential for extending past the memory buffer
for (UINT32 i = 0; i < size - 2; i += 4) {
UINT32 pixelInd = i / 4;
pCPUTensor[pixelInd] = (float)pData[i];
pCPUTensor[(height * width) + pixelInd] = (float)pData[i + 1];
pCPUTensor[(height * width * 2) + pixelInd] = (float)pData[i + 2];
}
} else if (BitmapPixelFormat::Rgba8 == GetPixelFormat(modelPixelFormat)) {
for (UINT32 i = 0; i < size; i += 4) {
for (UINT32 i = 0; i < size - 2; i += 4) {
UINT32 pixelInd = i / 4;
pCPUTensor[pixelInd] = (float)pData[i + 2];
pCPUTensor[(height * width) + pixelInd] = (float)pData[i + 1];
@ -99,14 +99,15 @@ namespace ImageTestHelper {
uint32_t height = softwareBitmap.PixelHeight();
uint32_t width = softwareBitmap.PixelWidth();
if (BitmapPixelFormat::Bgra8 == GetPixelFormat(modelPixelFormat)) {
for (UINT32 i = 0; i < size; i += 4) {
// loop condition is i < size - 2 to avoid potential for extending past the memory buffer
for (UINT32 i = 0; i < size - 2; i += 4) {
UINT32 pixelInd = i / 4;
pCPUTensor[pixelInd] = (float)pData[i];
pCPUTensor[(height * width) + pixelInd] = (float)pData[i + 1];
pCPUTensor[(height * width * 2) + pixelInd] = (float)pData[i + 2];
}
} else if (BitmapPixelFormat::Rgba8 == GetPixelFormat(modelPixelFormat)) {
for (UINT32 i = 0; i < size; i += 4) {
for (UINT32 i = 0; i < size - 2; i += 4) {
UINT32 pixelInd = i / 4;
pCPUTensor[pixelInd] = (float)pData[i + 2];
pCPUTensor[(height * width) + pixelInd] = (float)pData[i + 1];

View file

@ -816,7 +816,8 @@ static void Scenario22ImageBindingAsCPUTensor() {
uint32_t height = softwareBitmap.PixelHeight();
uint32_t width = softwareBitmap.PixelWidth();
for (UINT32 i = 0; i < size; i += 4) {
for (UINT32 i = 0; i < size - 2; i += 4) {
// loop condition is i < size - 2 to avoid potential for extending past the memory buffer
UINT32 pixelInd = i / 4;
pCPUTensor[pixelInd] = (float)pData[i];
pCPUTensor[(height * width) + pixelInd] = (float)pData[i + 1];
@ -887,7 +888,8 @@ static void Scenario22ImageBindingAsGPUTensor() {
uint32_t height = softwareBitmap.PixelHeight();
uint32_t width = softwareBitmap.PixelWidth();
for (UINT32 i = 0; i < size; i += 4) {
for (UINT32 i = 0; i < size - 2; i += 4) {
// loop condition is i < size - 2 to avoid potential for extending past the memory buffer
UINT32 pixelInd = i / 4;
pCPUTensor[pixelInd] = (FLOAT)pData[i];
pCPUTensor[(height * width) + pixelInd] = (FLOAT)pData[i + 1];
@ -1494,7 +1496,8 @@ static void BindMultipleCPUBuffersAsInputs(LearningModelDeviceKind kind) {
green_byteaccess->GetBuffer(reinterpret_cast<BYTE**>(&green_data), &frame_size);
blue_byteaccess->GetBuffer(reinterpret_cast<BYTE**>(&blue_data), &frame_size);
for (UINT32 i = 0; i < size; i += 4) {
for (UINT32 i = 0; i < size - 2 && i / 4 < frame_size; i += 4) {
// loop condition is i < size - 2 to avoid potential for extending past the memory buffer
UINT32 pixelInd = i / 4;
red_data[pixelInd] = (float)data[i];
green_data[pixelInd] = (float)data[i + 1];