~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/lib/perf/include/internal/rc_check.h

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
  2 #ifndef __LIBPERF_INTERNAL_RC_CHECK_H
  3 #define __LIBPERF_INTERNAL_RC_CHECK_H
  4 
  5 #include <stdlib.h>
  6 #include <linux/zalloc.h>
  7 
  8 /*
  9  * Enable reference count checking implicitly with leak checking, which is
 10  * integrated into address sanitizer.
 11  */
 12 #if defined(__SANITIZE_ADDRESS__) || defined(LEAK_SANITIZER) || defined(ADDRESS_SANITIZER)
 13 #define REFCNT_CHECKING 1
 14 #elif defined(__has_feature)
 15 #if __has_feature(address_sanitizer) || __has_feature(leak_sanitizer)
 16 #define REFCNT_CHECKING 1
 17 #endif
 18 #endif
 19 
 20 /*
 21  * Shared reference count checking macros.
 22  *
 23  * Reference count checking is an approach to sanitizing the use of reference
 24  * counted structs. It leverages address and leak sanitizers to make sure gets
 25  * are paired with a put. Reference count checking adds a malloc-ed layer of
 26  * indirection on a get, and frees it on a put. A missed put will be reported as
 27  * a memory leak. A double put will be reported as a double free. Accessing
 28  * after a put will cause a use-after-free and/or a segfault.
 29  */
 30 
 31 #ifndef REFCNT_CHECKING
 32 /* Replaces "struct foo" so that the pointer may be interposed. */
 33 #define DECLARE_RC_STRUCT(struct_name)          \
 34         struct struct_name
 35 
 36 /* Declare a reference counted struct variable. */
 37 #define RC_STRUCT(struct_name) struct struct_name
 38 
 39 /*
 40  * Interpose the indirection. Result will hold the indirection and object is the
 41  * reference counted struct.
 42  */
 43 #define ADD_RC_CHK(result, object) (result = object, object)
 44 
 45 /* Strip the indirection layer. */
 46 #define RC_CHK_ACCESS(object) object
 47 
 48 /* Frees the object and the indirection layer. */
 49 #define RC_CHK_FREE(object) free(object)
 50 
 51 /* A get operation adding the indirection layer. */
 52 #define RC_CHK_GET(result, object) ADD_RC_CHK(result, object)
 53 
 54 /* A put operation removing the indirection layer. */
 55 #define RC_CHK_PUT(object) {}
 56 
 57 /* Pointer equality when the indirection may or may not be there. */
 58 #define RC_CHK_EQUAL(object1, object2) (object1 == object2)
 59 
 60 #else
 61 
 62 /* Replaces "struct foo" so that the pointer may be interposed. */
 63 #define DECLARE_RC_STRUCT(struct_name)                  \
 64         struct original_##struct_name;                  \
 65         struct struct_name {                            \
 66                 struct original_##struct_name *orig;    \
 67         };                                              \
 68         struct original_##struct_name
 69 
 70 /* Declare a reference counted struct variable. */
 71 #define RC_STRUCT(struct_name) struct original_##struct_name
 72 
 73 /*
 74  * Interpose the indirection. Result will hold the indirection and object is the
 75  * reference counted struct.
 76  */
 77 #define ADD_RC_CHK(result, object)                                      \
 78         (                                                               \
 79                 object ? (result = malloc(sizeof(*result)),             \
 80                         result ? (result->orig = object, result)        \
 81                         : (result = NULL, NULL))                        \
 82                 : (result = NULL, NULL)                                 \
 83                 )
 84 
 85 /* Strip the indirection layer. */
 86 #define RC_CHK_ACCESS(object) object->orig
 87 
 88 /* Frees the object and the indirection layer. */
 89 #define RC_CHK_FREE(object)                     \
 90         do {                                    \
 91                 zfree(&object->orig);           \
 92                 free(object);                   \
 93         } while(0)
 94 
 95 /* A get operation adding the indirection layer. */
 96 #define RC_CHK_GET(result, object) ADD_RC_CHK(result, (object ? object->orig : NULL))
 97 
 98 /* A put operation removing the indirection layer. */
 99 #define RC_CHK_PUT(object)                      \
100         do {                                    \
101                 if (object) {                   \
102                         object->orig = NULL;    \
103                         free(object);           \
104                 }                               \
105         } while(0)
106 
107 /* Pointer equality when the indirection may or may not be there. */
108 #define RC_CHK_EQUAL(object1, object2) (object1 == object2 || \
109                 (object1 && object2 && object1->orig == object2->orig))
110 
111 #endif
112 
113 #endif /* __LIBPERF_INTERNAL_RC_CHECK_H */
114 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php