Skip to content

Commit 1026ac7

Browse files
committed
gemm_batch/sbgemv: fix validation, trans, 64-bit stride, and BFLOAT16
Validate CBLAS order (info = 0), group_count < 0 (info = 14), and group_size[i] < 0 (info = 15) in gemm_batch.c prior to allocation to prevent heap buffer overflows on negative group sizes and malloc(0) calls. Hoist parameter validation in gemm_batch_strided.c before allocation, report ldb as parameter 11, ldc as parameter 15, and negative count as parameter 17, and widen batch stride arithmetic to BLASLONG. Accept lowercase Fortran transpose specifiers and OpenBLAS 'R'/'r' conjugate-no-transpose in gemm_batch.c and gemm_batch_strided.c, and wire sbgemm_batch_thread in CMake and both BFLOAT16 batch interfaces. Check order == CblasRowMajor explicitly in interface/sbgemv.c so invalid CBLAS order invokes xerbla with info = 0, and widen m * n to BLASLONG.
1 parent dbf2c24 commit 1026ac7

6 files changed

Lines changed: 395 additions & 195 deletions

File tree

‎driver/level3/CMakeLists.txt‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ if (USE_THREAD)
100100
endif ()
101101
endif ()
102102

103+
if (BUILD_BFLOAT16)
104+
GenerateNamedObjects("gemm_batch_thread.c" "" "gemm_batch_thread" 0 "" "" false "BFLOAT16")
105+
endif ()
106+
103107
foreach (float_type ${FLOAT_TYPES})
104108
GenerateNamedObjects("gemm_batch_thread.c" "" "gemm_batch_thread" 0 "" "" false ${float_type})
105109

‎interface/gemm_batch.c‎

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ void openblas_warning(int verbose, const char * msg);
4343
#elif defined(DOUBLE)
4444
#define ERROR_NAME "DGEMM_BATCH "
4545
#define GEMM_BATCH_THREAD dgemm_batch_thread
46+
#elif defined(BFLOAT16)
47+
#define ERROR_NAME "SBGEMM_BATCH "
48+
#define GEMM_BATCH_THREAD sbgemm_batch_thread
4649
#else
4750
#define ERROR_NAME "SGEMM_BATCH "
4851
#define GEMM_BATCH_THREAD sgemm_batch_thread
@@ -155,9 +158,10 @@ void CNAME(enum CBLAS_ORDER order, enum CBLAS_TRANSPOSE * transa_array, enum CB
155158
blas_arg_t * args_array=NULL;
156159

157160
int mode=0, group_mode=0;
158-
blasint total_num=0;
161+
BLASLONG total_num=0;
159162

160-
blasint i=0, j=0, matrix_idx=0, count=0;
163+
blasint i=0, j=0;
164+
BLASLONG matrix_idx=0, count=0;
161165

162166
int group_transa, group_transb;
163167
BLASLONG group_nrowa, group_nrowb;
@@ -174,12 +178,32 @@ void CNAME(enum CBLAS_ORDER order, enum CBLAS_TRANSPOSE * transa_array, enum CB
174178
#endif
175179

176180
PRINT_DEBUG_CNAME;
177-
181+
182+
#if defined(CBLAS)
183+
if (order != CblasColMajor && order != CblasRowMajor) {
184+
info = 0;
185+
BLASFUNC(xerbla)(ERROR_NAME, &info, sizeof(ERROR_NAME));
186+
return;
187+
}
188+
#endif
189+
if (group_count < 0) {
190+
info = 14;
191+
BLASFUNC(xerbla)(ERROR_NAME, &info, sizeof(ERROR_NAME));
192+
return;
193+
}
178194
for(i=0; i<group_count; i++){
195+
if (group_size[i] < 0) {
196+
info = 15;
197+
BLASFUNC(xerbla)(ERROR_NAME, &info, sizeof(ERROR_NAME));
198+
return;
199+
}
179200
total_num+=group_size[i];
180201
}
202+
if (group_count == 0) {
203+
return;
204+
}
181205

182-
args_array=(blas_arg_t *)malloc(total_num * sizeof(blas_arg_t));
206+
args_array=(blas_arg_t *)malloc((total_num > 0 ? (size_t)total_num : 1) * sizeof(blas_arg_t));
183207

184208
if(args_array == NULL){
185209
openblas_warning(0, "memory alloc failed!\n");
@@ -297,21 +321,30 @@ void CNAME(enum CBLAS_ORDER order, enum CBLAS_TRANSPOSE * transa_array, enum CB
297321
group_lda = lda_array[i];
298322
group_ldb = ldb_array[i];
299323
group_ldc = ldc_array[i];
300-
301-
if (transa_array[i] == 'N') group_transa = 0;
302-
if (transa_array[i] == 'T') group_transa = 1;
324+
{
325+
char ta = transa_array[i];
326+
char tb = transb_array[i];
327+
TOUPPER(ta);
328+
TOUPPER(tb);
329+
if (ta == 'N') group_transa = 0;
330+
if (ta == 'T') group_transa = 1;
303331
#ifndef COMPLEX
304-
if (transa_array[i] == 'C') group_transa = 1;
332+
if (ta == 'R') group_transa = 0;
333+
if (ta == 'C') group_transa = 1;
305334
#else
306-
if (transa_array[i] == 'C') group_transa = 3;
335+
if (ta == 'R') group_transa = 2;
336+
if (ta == 'C') group_transa = 3;
307337
#endif
308-
if (transb_array[i] == 'N') group_transb = 0;
309-
if (transb_array[i] == 'T') group_transb = 1;
338+
if (tb == 'N') group_transb = 0;
339+
if (tb == 'T') group_transb = 1;
310340
#ifndef COMPLEX
311-
if (transb_array[i] == 'C') group_transb = 1;
341+
if (tb == 'R') group_transb = 0;
342+
if (tb == 'C') group_transb = 1;
312343
#else
313-
if (transb_array[i] == 'C') group_transb = 3;
344+
if (tb == 'R') group_transb = 2;
345+
if (tb == 'C') group_transb = 3;
314346
#endif
347+
}
315348
#endif
316349

317350
group_nrowa = group_m;
@@ -353,7 +386,7 @@ void CNAME(enum CBLAS_ORDER order, enum CBLAS_TRANSPOSE * transa_array, enum CB
353386
GEMM_SMALL_MATRIX_PERMIT(group_transa, group_transb, group_m, group_n, group_k,
354387
*(FLOAT *)(group_alpha), *(FLOAT *)(group_beta))){
355388
#else
356-
if (((FLOAT *)(group_alpha))[0] != ZERO &&
389+
if ((((FLOAT *)(group_alpha))[0] != ZERO || ((FLOAT *)(group_alpha))[1] != ZERO) &&
357390
GEMM_SMALL_MATRIX_PERMIT(group_transa, group_transb, group_m, group_n, group_k,
358391
((FLOAT *)(group_alpha))[0], ((FLOAT *)(group_alpha))[1],
359392
((FLOAT *)(group_beta))[0], ((FLOAT *)(group_beta))[1])){

0 commit comments

Comments
 (0)