Skip to content

Commit 4a4e3ab

Browse files
Fix MSan uninitialized reads in FBGEMM depthwise convolution and INT64 GEMM packing
1 parent 67dd044 commit 4a4e3ab

3 files changed

Lines changed: 20 additions & 3 deletions

File tree

src/FbgemmI64.cc

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -461,13 +461,16 @@ void cblas_gemm_i64_i64acc(
461461
ldb = N;
462462
}
463463

464-
alignas(64) array<int64_t, MCB * KCB> packA;
465-
alignas(64) array<int64_t, KCB * NCB> packB;
466-
alignas(64) array<int64_t, MCB * NCB> packC;
464+
alignas(64) array<int64_t, MCB * KCB> packA = {};
465+
alignas(64) array<int64_t, KCB * NCB> packB = {};
466+
alignas(64) array<int64_t, MCB * NCB> packC = {};
467467

468468
for (int ic = 0; ic < M; ic += MCB) {
469469
for (int kc = 0; kc < K; kc += KCB) {
470470
// pack A
471+
if (std::min(MCB, M - ic) < MCB || std::min(K - kc, KCB) < KCB) {
472+
packA.fill(0);
473+
}
471474
for (int i = 0; i < std::min(MCB, M - ic); ++i) {
472475
memcpy(
473476
&packA[i * KCB],
@@ -477,6 +480,9 @@ void cblas_gemm_i64_i64acc(
477480

478481
for (int jc = 0; jc < N; jc += NCB) {
479482
// pack B
483+
if (std::min(K - kc, KCB) < KCB || std::min(NCB, N - jc) < NCB) {
484+
packB.fill(0);
485+
}
480486
for (int i = 0; i < std::min(KCB, K - kc); ++i) {
481487
memcpy(
482488
&packB[i * NCB],
@@ -512,6 +518,9 @@ void cblas_gemm_i64_i64acc(
512518
std::min(KCB, K - kc),
513519
NCB);
514520
} else {
521+
if (std::min(MCB, M - ic) < MCB || std::min(NCB, N - jc) < NCB) {
522+
packC.fill(0);
523+
}
515524
for (int i = 0; i < std::min(MCB, M - ic); ++i) {
516525
memcpy(
517526
&packC[i * NCB],

src/FbgemmI8Depthwise2D-inl.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "./FbgemmI8DepthwiseUtils.h" // @manual
1212
#include "./GenerateI8Depthwise.h" // @manual
1313
#include "./MaskAvx2.h" // @manual
14+
#include <cstring>
1415
#include "fbgemm/Utils.h"
1516
#include "fbgemm/UtilsAvx2.h"
1617

@@ -155,6 +156,9 @@ static ALWAYS_INLINE void depthwise_2d_(
155156
auto row_offsets_owner =
156157
makeAlignedUniquePtr<int32_t>(64, (IC + 31) / 32 * 32);
157158
int32_t* row_offsets = row_offsets_owner.get();
159+
if (row_offsets) {
160+
std::memset(row_offsets, 0, (IC + 31) / 32 * 32 * sizeof(int32_t));
161+
}
158162

159163
int64_t n_begin = 0, n_end = 0, h_begin = 0, h_end = 0, w_begin = 0,
160164
w_end = 0;

src/FbgemmI8Depthwise3D.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#define FBGEMM_EXPORTS
1010
#include "fbgemm/FbgemmI8Depthwise.h"
1111

12+
#include <cstring>
1213
#include <stdexcept> // for logic_error
1314
#include <string>
1415

@@ -170,6 +171,9 @@ static ALWAYS_INLINE void depthwise_3d_same_pad_(
170171
auto row_offsets_owner =
171172
makeAlignedUniquePtr<int32_t>(64, (IC + 31) / 32 * 32);
172173
int32_t* row_offsets = row_offsets_owner.get();
174+
if (row_offsets) {
175+
std::memset(row_offsets, 0, (IC + 31) / 32 * 32 * sizeof(int32_t));
176+
}
173177

174178
int64_t n_begin = 0, n_end = 0, t_begin = 0, t_end = 0, h_begin = 0,
175179
h_end = 0;

0 commit comments

Comments
 (0)