xref: /aosp_15_r20/external/skia/src/base/SkMSAN.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2016 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #ifndef SkMSAN_DEFINED
9 #define SkMSAN_DEFINED
10 
11 #include "include/private/base/SkAssert.h"
12 
13 #include <cstddef>
14 #include <string.h>
15 
16 // Typically declared in LLVM's msan_interface.h.  Easier for us to just re-declare.
17 extern "C" {
18     void __msan_check_mem_is_initialized(const volatile void*, size_t);
19     void __msan_unpoison                (const volatile void*, size_t);
20 }
21 
22 // Code that requires initialized inputs can call this to make it clear that
23 // the blame for use of uninitialized data belongs further up the call stack.
sk_msan_assert_initialized(const void * begin,const void * end)24 static inline void sk_msan_assert_initialized(const void* begin, const void* end) {
25 #if defined(__has_feature)
26     #if __has_feature(memory_sanitizer)
27         __msan_check_mem_is_initialized(begin, (const char*)end - (const char*)begin);
28     #endif
29 #endif
30 }
31 
32 // Lie to MSAN that this range of memory is initialized.
33 // This can hide serious problems if overused.  Every use of this should refer to a bug.
sk_msan_mark_initialized(const void * begin,const void * end,const char * skbug)34 static inline void sk_msan_mark_initialized(const void* begin, const void* end, const char* skbug) {
35     SkASSERT(skbug && 0 != strcmp(skbug, ""));
36 #if defined(__has_feature)
37     #if __has_feature(memory_sanitizer)
38         __msan_unpoison(begin, (const char*)end - (const char*)begin);
39     #endif
40 #endif
41 }
42 
43 #endif//SkMSAN_DEFINED
44