2 * checked-build.h: Expensive asserts used when mono is built with --with-checked-build=yes
5 * Rodrigo Kumpera (kumpera@gmail.com)
10 #ifndef __CHECKED_BUILD_H__
11 #define __CHECKED_BUILD_H__
14 #include <mono/utils/atomic.h>
15 #include <mono/utils/mono-publib.h>
18 MONO_CHECK_MODE_NONE = 0,
19 MONO_CHECK_MODE_GC = 0x1,
20 MONO_CHECK_MODE_METADATA = 0x2,
21 MONO_CHECK_MODE_THREAD = 0x4,
22 MONO_CHECK_MODE_ALL = MONO_CHECK_MODE_GC | MONO_CHECK_MODE_METADATA | MONO_CHECK_MODE_THREAD,
23 MONO_CHECK_MODE_UNKNOWN = 0x8
26 mono_bool mono_check_mode_enabled (MonoCheckMode query);
28 // This is for metadata writes which we have chosen not to check at the current time.
29 // Because in principle this should never happen, we still use a macro so that the exemptions will be easier to find, and remove, later.
30 // The current reason why this is needed is for pointers to constant strings, which the checker cannot verify yet.
31 #define CHECKED_METADATA_WRITE_PTR_EXEMPT(ptr, val) do { (ptr) = (val); } while (0)
33 #ifdef ENABLE_CHECKED_BUILD
35 #define g_assert_checked g_assert
38 This can be called by embedders
40 #define MONO_REQ_API_ENTRYPOINT
43 The JIT will generate code that will land on this function
45 #define MONO_REQ_RUNTIME_ENTRYPOINT
47 #define CHECKED_MONO_INIT() do { checked_build_init (); } while (0)
49 void checked_build_init (void);
53 #define g_assert_checked(...)
55 #define MONO_REQ_API_ENTRYPOINT
56 #define MONO_REQ_RUNTIME_ENTRYPOINT
58 #define CHECKED_MONO_INIT()
60 #endif /* ENABLE_CHECKED_BUILD */
62 #ifdef ENABLE_CHECKED_BUILD_GC
64 GC runtime modes rules:
68 Call into foreigh functions.
69 Call GC Safe or Neutral modes functions.
70 Read from pinned managed memory.
73 Touch managed memory (read/write).
81 Touch managed memory (read/write).
82 Call GC Unsafe or Neutral modes functions.
85 Call foreign native code (embedder callbacks, pinvokes, etc)
86 Call into any Blocking functions/syscalls (mutexes, IO, etc)
90 Poking into managed memory.
94 Call other GC Neutral mode functions.
98 Call foreign native code (embedder callbacks, pinvokes, etc)
99 Call into any Blocking functions/syscalls (mutexes, IO, etc)
103 Functions that can be called from both coop or preept modes.
107 #define MONO_REQ_GC_SAFE_MODE do { \
108 assert_gc_safe_mode (); \
111 #define MONO_REQ_GC_UNSAFE_MODE do { \
112 assert_gc_unsafe_mode (); \
115 #define MONO_REQ_GC_NEUTRAL_MODE do { \
116 assert_gc_neutral_mode (); \
119 /* In a GC critical region, the thread is not allowed to switch to GC safe mode.
120 * For example if the thread is about to call a method that will manipulate managed objects.
121 * The GC critical region must only occur in unsafe mode.
123 #define MONO_PREPARE_GC_CRITICAL_REGION \
124 MONO_REQ_GC_UNSAFE_MODE \
126 void* __critical_gc_region_cookie = critical_gc_region_begin()
128 #define MONO_FINISH_GC_CRITICAL_REGION \
129 critical_gc_region_end(__critical_gc_region_cookie); \
132 /* Verify that the thread is not currently in a GC critical region. */
133 #define MONO_REQ_GC_NOT_CRITICAL do { \
134 assert_not_in_gc_critical_region(); \
137 /* Verify that the thread is currently in a GC critical region. */
138 #define MONO_REQ_GC_CRITICAL do { \
139 assert_in_gc_critical_region(); \
142 void assert_gc_safe_mode (void);
143 void assert_gc_unsafe_mode (void);
144 void assert_gc_neutral_mode (void);
146 void* critical_gc_region_begin(void);
147 void critical_gc_region_end(void* token);
148 void assert_not_in_gc_critical_region(void);
149 void assert_in_gc_critical_region (void);
153 #define MONO_REQ_GC_SAFE_MODE
154 #define MONO_REQ_GC_UNSAFE_MODE
155 #define MONO_REQ_GC_NEUTRAL_MODE
157 #define MONO_PREPARE_GC_CRITICAL_REGION
158 #define MONO_FINISH_GC_CRITICAL_REGION
160 #define MONO_REQ_GC_NOT_CRITICAL
161 #define MONO_REQ_GC_CRITICAL
163 #endif /* defined(ENABLE_CHECKED_BUILD_GC) */
165 #ifdef ENABLE_CHECKED_BUILD_METADATA
167 // Use when writing a pointer from one image or imageset to another.
168 #define CHECKED_METADATA_WRITE_PTR(ptr, val) do { \
169 check_metadata_store (&(ptr), (val)); \
173 // Use when writing a pointer from an image or imageset to itself.
174 #define CHECKED_METADATA_WRITE_PTR_LOCAL(ptr, val) do { \
175 check_metadata_store_local (&(ptr), (val)); \
179 // Use when writing a pointer from one image or imageset to another (atomic version).
180 #define CHECKED_METADATA_WRITE_PTR_ATOMIC(ptr, val) do { \
181 check_metadata_store (&(ptr), (val)); \
182 mono_atomic_store_release (&(ptr), (val)); \
185 void check_metadata_store(void *from, void *to);
186 void check_metadata_store_local(void *from, void *to);
190 #define CHECKED_METADATA_WRITE_PTR(ptr, val) do { (ptr) = (val); } while (0)
191 #define CHECKED_METADATA_WRITE_PTR_LOCAL(ptr, val) do { (ptr) = (val); } while (0)
192 #define CHECKED_METADATA_WRITE_PTR_ATOMIC(ptr, val) do { mono_atomic_store_release (&(ptr), (val)); } while (0)
194 #endif /* defined(ENABLE_CHECKED_BUILD_METADATA) */
196 #ifdef ENABLE_CHECKED_BUILD_THREAD
198 #define CHECKED_BUILD_THREAD_TRANSITION(transition, info, from_state, suspend_count, next_state, suspend_count_delta) do { \
199 checked_build_thread_transition (transition, info, from_state, suspend_count, next_state, suspend_count_delta); \
202 void checked_build_thread_transition(const char *transition, void *info, int from_state, int suspend_count, int next_state, int suspend_count_delta);
204 G_GNUC_NORETURN void mono_fatal_with_history(const char *msg, ...);
208 #define CHECKED_BUILD_THREAD_TRANSITION(transition, info, from_state, suspend_count, next_state, suspend_count_delta)
210 #define mono_fatal_with_history g_error
212 #endif /* defined(ENABLE_CHECKED_BUILD_THREAD) */
214 #endif /* __CHECKED_BUILD_H__ */