From 2c6ce7e5c5e1d395defedf2ba3f7beba137aaafb Mon Sep 17 00:00:00 2001 From: kmannthe Date: Wed, 26 Aug 2020 12:16:22 -0700 Subject: [PATCH 1/2] Smithwatermen Memory allocation defensive code Add denfensive code where Memory Alloctions are involved. This will reduce crashes that happen when memory allocations fail in SmithWaterman. Move to _mm_free for memory allocated wtih _mm_malloc. Signed-off-by: Keith Mannthey --- .../smithwaterman/IntelSmithWaterman.cc | 5 ++++ src/main/native/smithwaterman/PairWiseSW.h | 30 +++++++++++-------- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/src/main/native/smithwaterman/IntelSmithWaterman.cc b/src/main/native/smithwaterman/IntelSmithWaterman.cc index 09cabab8..d22caf0e 100644 --- a/src/main/native/smithwaterman/IntelSmithWaterman.cc +++ b/src/main/native/smithwaterman/IntelSmithWaterman.cc @@ -56,6 +56,11 @@ JNIEXPORT jint JNICALL Java_com_intel_gkl_smithwaterman_IntelSmithWaterman_align jbyte* alternate = (jbyte*)env->GetPrimitiveArrayCritical(alt, 0); jbyte* cigarArray = (jbyte*)env->GetPrimitiveArrayCritical(cigar, 0); + if (reference == NULL | alternate == NULL | cigarArray == NULL) { + DBG("GetPrimitiveArrayCritical failed from JAVA unable to contiune"); + return -1; + } + jint count = 0; jint offset = 0; diff --git a/src/main/native/smithwaterman/PairWiseSW.h b/src/main/native/smithwaterman/PairWiseSW.h index 8c7a7708..c9b8c171 100644 --- a/src/main/native/smithwaterman/PairWiseSW.h +++ b/src/main/native/smithwaterman/PairWiseSW.h @@ -419,15 +419,21 @@ int32_t CONCAT(runSWOnePairBT_,SIMD_ENGINE)(int32_t match, int32_t mismatch, int - int32_t w_match = match; - int32_t w_mismatch = mismatch; - int32_t w_open = open; - int32_t w_extend = extend; - - int32_t *E_ = (int32_t *)_mm_malloc((6 * (MAX_SEQ_LEN+ AVX_LENGTH)) * sizeof(int32_t), 64); - int16_t *backTrack_ = (int16_t *)_mm_malloc((2 * MAX_SEQ_LEN * MAX_SEQ_LEN + 2 * AVX_LENGTH) * sizeof(int16_t), 64); - int16_t *cigarBuf_ = (int16_t *)_mm_malloc(4 * MAX_SEQ_LEN * sizeof(int16_t), 64); - + int32_t w_match = match; + int32_t w_mismatch = mismatch; + int32_t w_open = open; + int32_t w_extend = extend; + + int32_t *E_ = (int32_t *)_mm_malloc((6 * (MAX_SEQ_LEN+ AVX_LENGTH)) * sizeof(int32_t), 64); + int16_t *backTrack_ = (int16_t *)_mm_malloc((2 * MAX_SEQ_LEN * MAX_SEQ_LEN + 2 * AVX_LENGTH) * sizeof(int16_t), 64); + int16_t *cigarBuf_ = (int16_t *)_mm_malloc(4 * MAX_SEQ_LEN * sizeof(int16_t), 64); + + if (E_ == NULL | backTrack_ == NULL | cigarBuf_ == NULL) { + _mm_free(E_); + _mm_free(backTrack_); + _mm_free(cigarBuf_); + return -1; + } SeqPair p; p.seq1 = seq1; @@ -442,8 +448,8 @@ int32_t CONCAT(runSWOnePairBT_,SIMD_ENGINE)(int32_t match, int32_t mismatch, int (*cigarCount) = p.cigarCount; - free(E_); - free(backTrack_); - free(cigarBuf_); + _mm_free(E_); + _mm_free(backTrack_); + _mm_free(cigarBuf_); return p.alignmentOffset; } From 990510c736b99c2e9480e6b5e54dd6eeb783cb80 Mon Sep 17 00:00:00 2001 From: kmannthe Date: Wed, 26 Aug 2020 12:39:21 -0700 Subject: [PATCH 2/2] Fix for "Uninitialized pointer in IntelDeflater.cc #89" Add an alloction and free block struct in question. Signed-off-by: Keith Mannthey --- src/main/native/compression/IntelDeflater.cc | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/main/native/compression/IntelDeflater.cc b/src/main/native/compression/IntelDeflater.cc index 0b8b6971..b9ac93dd 100644 --- a/src/main/native/compression/IntelDeflater.cc +++ b/src/main/native/compression/IntelDeflater.cc @@ -164,9 +164,14 @@ JNIEXPORT void JNICALL Java_com_intel_gkl_compression_IntelDeflater_generateHuff jbyte* input = (jbyte*)env->GetPrimitiveArrayCritical(inputBuffer, 0); - struct isal_huff_histogram *histogram; + struct isal_huff_histogram *histogram = (struct isal_huff_histogram *) malloc(sizeof(*histogram)); struct isal_hufftables *hufftables_custom; + if (histogram == NULL) { + DBG ("Malloc failed out of memory"); + return; + } + memset(histogram, 0, sizeof(isal_huff_histogram)); isal_update_histogram((unsigned char*)input, 64*1024, histogram); isal_create_hufftables(hufftables_custom, histogram); @@ -174,7 +179,10 @@ JNIEXPORT void JNICALL Java_com_intel_gkl_compression_IntelDeflater_generateHuff env->SetLongField(obj, FID_lz_stream, (jlong)lz_stream); env->ReleasePrimitiveArrayCritical(inputBuffer, input, 0); + + free(histogram); } + } /**