LCOV - code coverage report
Current view: top level - util/sanitize - fd_msan.h (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 2 3 66.7 %
Date: 2025-01-08 12:08:44 Functions: 9 2031 0.4 %

          Line data    Source code
       1             : #ifndef HEADER_fd_src_util_sanitize_fd_msan_h
       2             : #define HEADER_fd_src_util_sanitize_fd_msan_h
       3             : 
       4             : #include "../fd_util_base.h"
       5             : 
       6             : /* MemorySanitizer (MSan) detects uninitialized access via a global
       7             :    bitmap.
       8             : 
       9             :    More info on MSan:
      10             :      - https://clang.llvm.org/docs/MemorySanitizer.html
      11             :      - https://github.com/google/sanitizers/wiki/MemorySanitizer */
      12             : 
      13             : /* Based on https://github.com/llvm/llvm-project/blob/main/compiler-rt/include/sanitizer/msan_interface.h
      14             : 
      15             :    Part of the LLVM Project, under the Apache License v2.0 with LLVM
      16             :    Exceptions.  See https://llvm.org/LICENSE.txt for license
      17             :    information.  SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
      18             : 
      19             :    This file was originally part of MemorySanitizer (MSan). */
      20             : 
      21             : #ifndef FD_HAS_MSAN
      22             : #if defined(__has_feature)
      23             : #define FD_HAS_MSAN __has_feature(memory_sanitizer)
      24             : #else
      25             : #define FD_HAS_MSAN 0
      26             : #endif
      27             : #endif
      28             : 
      29             : #if FD_HAS_MSAN
      30             : #define FD_FN_NO_MSAN __attribute__((no_sanitize("memory")))
      31             : #else
      32             : #define FD_FN_NO_MSAN
      33             : #endif
      34             : 
      35             : #define FD_MSAN_ALIGN (4UL)
      36             : 
      37             : FD_PROTOTYPES_BEGIN
      38             : 
      39             : #if FD_HAS_MSAN
      40             : 
      41             : /* These are for internal use only */
      42             : 
      43             : void __msan_poison                  ( void const volatile * addr, ulong sz );
      44             : void __msan_unpoison                ( void const volatile * addr, ulong sz );
      45             : void __msan_check_mem_is_initialized( void const volatile * addr, ulong sz );
      46             : 
      47             : 
      48             : /* fd_msan_poison marks a region of memory as uninitialized.  MSAN
      49             :    detects uninitalized memory when it is used in a conditional branch,
      50             :    for memory accesses, as a direct argument to a function call, or
      51             :    as a direct return value. */
      52             : static inline void * fd_msan_poison  ( void *       addr, ulong sz ) { __msan_poison  ( addr, sz ); return addr; }
      53             : 
      54             : /* fd_msan_unpoison marks a region of memory as initialized.
      55             :    Use cases:
      56             :    - Marking memory initalized that MSAN can not track, most notably
      57             :      memory initialized by handwritten assembly.
      58             :    - Avoiding false positives where an uninitialized value is used
      59             :      in a scenario described by the  fd_msan_poison doc comment, but the
      60             :      use is actually correct in the application logic. */
      61             : static inline void * fd_msan_unpoison( void *       addr, ulong sz ) { __msan_unpoison( addr, sz ); return addr; }
      62             : 
      63             : /* fd_msan_check checks if a region of memory is initialized.  If it is
      64             :    not, MSAN will report an error.  For making sure memory is
      65             :    initalized in cases beyond those describe in the fd_msan poison
      66             :    comment.  Furthermore, it is useful for debugging MSAN crashes. */
      67             : static inline void   fd_msan_check   ( void const * addr, ulong sz ) { __msan_check_mem_is_initialized( addr, sz ); }
      68             : 
      69             : #else
      70             : 
      71           0 : static inline void * fd_msan_poison  ( void *       addr, ulong sz ) { (void)sz; return addr; }
      72   305373865 : static inline void * fd_msan_unpoison( void *       addr, ulong sz ) { (void)sz; return addr; }
      73   559847761 : static inline void   fd_msan_check   ( void const * addr, ulong sz ) { (void)addr; (void)sz; }
      74             : 
      75             : #endif
      76             : 
      77             : FD_PROTOTYPES_END
      78             : 
      79             : #endif /* HEADER_fd_src_util_sanitize_fd_msan_h */

Generated by: LCOV version 1.14