Skip to content

Commit 3298a08

Browse files
authored
Merge pull request #3725 from felixhandte/msan-unpoison-cwksp-on-free
Unpoison Workspace Memory Before Custom-Free
2 parents 969e54f + 9987d2f commit 3298a08

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

lib/compress/zstd_cwksp.h

+14-9
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,15 @@ MEM_STATIC void ZSTD_cwksp_clear(ZSTD_cwksp* ws) {
636636
ZSTD_cwksp_assert_internal_consistency(ws);
637637
}
638638

639+
MEM_STATIC size_t ZSTD_cwksp_sizeof(const ZSTD_cwksp* ws) {
640+
return (size_t)((BYTE*)ws->workspaceEnd - (BYTE*)ws->workspace);
641+
}
642+
643+
MEM_STATIC size_t ZSTD_cwksp_used(const ZSTD_cwksp* ws) {
644+
return (size_t)((BYTE*)ws->tableEnd - (BYTE*)ws->workspace)
645+
+ (size_t)((BYTE*)ws->workspaceEnd - (BYTE*)ws->allocStart);
646+
}
647+
639648
/**
640649
* The provided workspace takes ownership of the buffer [start, start+size).
641650
* Any existing values in the workspace are ignored (the previously managed
@@ -667,6 +676,11 @@ MEM_STATIC size_t ZSTD_cwksp_create(ZSTD_cwksp* ws, size_t size, ZSTD_customMem
667676
MEM_STATIC void ZSTD_cwksp_free(ZSTD_cwksp* ws, ZSTD_customMem customMem) {
668677
void *ptr = ws->workspace;
669678
DEBUGLOG(4, "cwksp: freeing workspace");
679+
#if ZSTD_MEMORY_SANITIZER && !defined(ZSTD_MSAN_DONT_POISON_WORKSPACE)
680+
if (ptr != NULL && customMem.customFree != NULL) {
681+
__msan_unpoison(ptr, ZSTD_cwksp_sizeof(ws));
682+
}
683+
#endif
670684
ZSTD_memset(ws, 0, sizeof(ZSTD_cwksp));
671685
ZSTD_customFree(ptr, customMem);
672686
}
@@ -680,15 +694,6 @@ MEM_STATIC void ZSTD_cwksp_move(ZSTD_cwksp* dst, ZSTD_cwksp* src) {
680694
ZSTD_memset(src, 0, sizeof(ZSTD_cwksp));
681695
}
682696

683-
MEM_STATIC size_t ZSTD_cwksp_sizeof(const ZSTD_cwksp* ws) {
684-
return (size_t)((BYTE*)ws->workspaceEnd - (BYTE*)ws->workspace);
685-
}
686-
687-
MEM_STATIC size_t ZSTD_cwksp_used(const ZSTD_cwksp* ws) {
688-
return (size_t)((BYTE*)ws->tableEnd - (BYTE*)ws->workspace)
689-
+ (size_t)((BYTE*)ws->workspaceEnd - (BYTE*)ws->allocStart);
690-
}
691-
692697
MEM_STATIC int ZSTD_cwksp_reserve_failed(const ZSTD_cwksp* ws) {
693698
return ws->allocFailed;
694699
}

0 commit comments

Comments
 (0)