3 * Expensive asserts used when mono is built with --with-checked-build=yes
6 * Rodrigo Kumpera (kumpera@gmail.com)
11 #ifndef __CHECKED_BUILD_H__
12 #define __CHECKED_BUILD_H__
15 #include <mono/utils/atomic.h>
16 #include <mono/utils/mono-compiler.h>
17 #include <mono/utils/mono-publib.h>
20 MONO_CHECK_MODE_NONE = 0,
21 MONO_CHECK_MODE_GC = 0x1,
22 MONO_CHECK_MODE_METADATA = 0x2,
23 MONO_CHECK_MODE_THREAD = 0x4,
24 MONO_CHECK_MODE_ALL = MONO_CHECK_MODE_GC | MONO_CHECK_MODE_METADATA | MONO_CHECK_MODE_THREAD,
25 MONO_CHECK_MODE_UNKNOWN = 0x8
28 mono_bool mono_check_mode_enabled (MonoCheckMode query);
30 // This is for metadata writes which we have chosen not to check at the current time.
31 // Because in principle this should never happen, we still use a macro so that the exemptions will be easier to find, and remove, later.
32 // The current reason why this is needed is for pointers to constant strings, which the checker cannot verify yet.
33 #define CHECKED_METADATA_WRITE_PTR_EXEMPT(ptr, val) do { (ptr) = (val); } while (0)
35 #ifdef ENABLE_CHECKED_BUILD
37 #define g_assert_checked g_assert
40 This can be called by embedders
42 #define MONO_REQ_API_ENTRYPOINT
45 The JIT will generate code that will land on this function
47 #define MONO_REQ_RUNTIME_ENTRYPOINT
49 #define CHECKED_MONO_INIT() do { checked_build_init (); } while (0)
51 void checked_build_init (void);
55 #define g_assert_checked(...)
57 #define MONO_REQ_API_ENTRYPOINT
58 #define MONO_REQ_RUNTIME_ENTRYPOINT
60 #define CHECKED_MONO_INIT()
62 #endif /* ENABLE_CHECKED_BUILD */
64 #ifdef ENABLE_CHECKED_BUILD_GC
66 GC runtime modes rules:
70 Call into foreigh functions.
71 Call GC Safe or Neutral modes functions.
72 Read from pinned managed memory.
75 Touch managed memory (read/write).
83 Touch managed memory (read/write).
84 Call GC Unsafe or Neutral modes functions.
87 Call foreign native code (embedder callbacks, pinvokes, etc)
88 Call into any Blocking functions/syscalls (mutexes, IO, etc)
92 Poking into managed memory.
96 Call other GC Neutral mode functions.
100 Call foreign native code (embedder callbacks, pinvokes, etc)
101 Call into any Blocking functions/syscalls (mutexes, IO, etc)
105 Functions that can be called from both coop or preept modes.
109 #define MONO_REQ_GC_SAFE_MODE do { \
110 assert_gc_safe_mode (__FILE__, __LINE__); \
113 #define MONO_REQ_GC_UNSAFE_MODE do { \
114 assert_gc_unsafe_mode (__FILE__, __LINE__); \
117 #define MONO_REQ_GC_NEUTRAL_MODE do { \
118 assert_gc_neutral_mode (__FILE__, __LINE__); \
121 /* In a GC critical region, the thread is not allowed to switch to GC safe mode.
122 * For example if the thread is about to call a method that will manipulate managed objects.
123 * The GC critical region must only occur in unsafe mode.
125 #define MONO_PREPARE_GC_CRITICAL_REGION \
126 MONO_REQ_GC_UNSAFE_MODE \
128 void* __critical_gc_region_cookie = critical_gc_region_begin()
130 #define MONO_FINISH_GC_CRITICAL_REGION \
131 critical_gc_region_end(__critical_gc_region_cookie); \
134 /* Verify that the thread is not currently in a GC critical region. */
135 #define MONO_REQ_GC_NOT_CRITICAL do { \
136 assert_not_in_gc_critical_region(); \
139 /* Verify that the thread is currently in a GC critical region. */
140 #define MONO_REQ_GC_CRITICAL do { \
141 assert_in_gc_critical_region(); \
144 void assert_gc_safe_mode (const char *file, int lineno);
145 void assert_gc_unsafe_mode (const char *file, int lineno);
146 void assert_gc_neutral_mode (const char *file, int lineno);
148 void* critical_gc_region_begin(void);
149 void critical_gc_region_end(void* token);
150 void assert_not_in_gc_critical_region(void);
151 void assert_in_gc_critical_region (void);
155 #define MONO_REQ_GC_SAFE_MODE
156 #define MONO_REQ_GC_UNSAFE_MODE
157 #define MONO_REQ_GC_NEUTRAL_MODE
159 #define MONO_PREPARE_GC_CRITICAL_REGION
160 #define MONO_FINISH_GC_CRITICAL_REGION
162 #define MONO_REQ_GC_NOT_CRITICAL
163 #define MONO_REQ_GC_CRITICAL
165 #endif /* defined(ENABLE_CHECKED_BUILD_GC) */
167 #ifdef ENABLE_CHECKED_BUILD_METADATA
169 // Use when writing a pointer from one image or imageset to another.
170 #define CHECKED_METADATA_WRITE_PTR(ptr, val) do { \
171 check_metadata_store (&(ptr), (val)); \
175 // Use when writing a pointer from an image or imageset to itself.
176 #define CHECKED_METADATA_WRITE_PTR_LOCAL(ptr, val) do { \
177 check_metadata_store_local (&(ptr), (val)); \
181 // Use when writing a pointer from one image or imageset to another (atomic version).
182 #define CHECKED_METADATA_WRITE_PTR_ATOMIC(ptr, val) do { \
183 check_metadata_store (&(ptr), (val)); \
184 mono_atomic_store_release (&(ptr), (val)); \
187 void check_metadata_store(void *from, void *to);
188 void check_metadata_store_local(void *from, void *to);
190 #define CHECKED_METADATA_STORE(ptr, val) check_metadata_store ((ptr), (val))
191 #define CHECKED_METADATA_STORE_LOCAL(ptr, val) check_metadata_store_local ((ptr), (val))
195 #define CHECKED_METADATA_WRITE_PTR(ptr, val) do { (ptr) = (val); } while (0)
196 #define CHECKED_METADATA_WRITE_PTR_LOCAL(ptr, val) do { (ptr) = (val); } while (0)
197 #define CHECKED_METADATA_WRITE_PTR_ATOMIC(ptr, val) do { mono_atomic_store_release (&(ptr), (val)); } while (0)
199 #define CHECKED_METADATA_STORE(ptr, val) do { (ptr); (val); } while (0)
200 #define CHECKED_METADATA_STORE_LOCAL(ptr, val) do { (ptr); (val); } while (0)
202 #endif /* defined(ENABLE_CHECKED_BUILD_METADATA) */
204 #ifdef ENABLE_CHECKED_BUILD_THREAD
206 #define CHECKED_BUILD_THREAD_TRANSITION(transition, info, from_state, suspend_count, next_state, suspend_count_delta) do { \
207 checked_build_thread_transition (transition, info, from_state, suspend_count, next_state, suspend_count_delta); \
210 void checked_build_thread_transition(const char *transition, void *info, int from_state, int suspend_count, int next_state, int suspend_count_delta);
212 G_GNUC_NORETURN MONO_ATTR_FORMAT_PRINTF(1,2) void mono_fatal_with_history(const char *msg, ...);
216 #define CHECKED_BUILD_THREAD_TRANSITION(transition, info, from_state, suspend_count, next_state, suspend_count_delta)
218 #define mono_fatal_with_history g_error
220 #endif /* defined(ENABLE_CHECKED_BUILD_THREAD) */
222 #endif /* __CHECKED_BUILD_H__ */