@@ -180,6 +180,9 @@ MEM_STATIC void ZSTD_cwksp_assert_internal_consistency(ZSTD_cwksp* ws) {
180180 assert (ws -> allocStart <= ws -> workspaceEnd );
181181 assert (ws -> initOnceStart <= ws -> workspaceEnd );
182182 assert ((size_t )ws -> allocStart % ZSTD_CWKSP_ALIGNMENT_BYTES == 0 );
183+ #if ZSTD_MEMORY_SANITIZER
184+ assert (__msan_test_shadow (ws -> initOnceStart , (U8 * )ws -> workspaceEnd - (U8 * )ws -> initOnceStart ) == -1 );
185+ #endif
183186}
184187
185188/**
@@ -316,7 +319,7 @@ ZSTD_cwksp_internal_advance_phase(ZSTD_cwksp* ws, ZSTD_cwksp_alloc_phase_e phase
316319 */
317320MEM_STATIC int ZSTD_cwksp_owns_buffer (const ZSTD_cwksp * ws , const void * ptr )
318321{
319- return (ptr != NULL ) && (ws -> workspace <= ptr ) && (ptr <= ws -> workspaceEnd );
322+ return (ptr != NULL ) && (ws -> workspace <= ptr ) && (ptr < ws -> workspaceEnd );
320323}
321324
322325/**
@@ -369,9 +372,22 @@ MEM_STATIC void* ZSTD_cwksp_reserve_aligned_init_once(ZSTD_cwksp* ws, size_t byt
369372 ZSTD_cwksp_alloc_aligned_init_once );
370373 assert (((size_t )ptr & (ZSTD_CWKSP_ALIGNMENT_BYTES - 1 ))== 0 );
371374 if (ptr && ptr < ws -> initOnceStart ) {
375+ #if !(ZSTD_ADDRESS_SANITIZER && !defined(ZSTD_ASAN_DONT_POISON_WORKSPACE ))
376+ /* We will not memset when poisoning the workspace as we already unpoison the buffer
377+ * and want to maintain poisoned memory after its location.
378+ * It would have been better to memset even in these conditions, so the memset
379+ * is better tested, but I'm not sure how to make it work correctly. */
372380 ZSTD_memset (ptr , 0 , (U8 * )ws -> initOnceStart - (U8 * )ptr );
381+ #elif ZSTD_MEMORY_SANITIZER
382+ /* We couldn't memset, so we should at least unpoison the memory to
383+ * reduce false alarms from msan */
384+ __msan_unpoison (ptr , (U8 * )ws -> initOnceStart - (U8 * )ptr );
385+ #endif
373386 ws -> initOnceStart = ptr ;
374387 }
388+ #if ZSTD_MEMORY_SANITIZER
389+ assert (__msan_test_shadow (ptr , bytes ) == -1 );
390+ #endif
375391 return ptr ;
376392}
377393
0 commit comments