Runtime enable for checked-build asserts
[mono-project.git] / mono / utils / checked-build.h
blob73325cf1758df46ff291d012c65bc1dd63a9bd81
1 /*
2 * checked-build.h: Expensive asserts used when mono is built with --with-checked-build=yes
4 * Author:
5 * Rodrigo Kumpera (kumpera@gmail.com)
7 * (C) 2015 Xamarin
8 */
10 #ifndef __CHECKED_BUILD_H__
11 #define __CHECKED_BUILD_H__
13 #include <config.h>
14 #include <mono/utils/atomic.h>
15 #include <mono/utils/mono-publib.h>
17 typedef enum {
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
24 } MonoCheckMode;
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);
51 #else
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:
66 - GC Safe
67 Can:
68 Call into foreigh functions.
69 Call GC Safe or Neutral modes functions.
70 Read from pinned managed memory.
72 Cannot:
73 Touch managed memory (read/write).
74 Be dettached.
76 What's good for?
77 Doing blocking calls.
79 - GC Unsafe
80 Can:
81 Touch managed memory (read/write).
82 Call GC Unsafe or Neutral modes functions.
84 Cannot:
85 Call foreign native code (embedder callbacks, pinvokes, etc)
86 Call into any Blocking functions/syscalls (mutexes, IO, etc)
87 Be dettached.
89 What's good for?
90 Poking into managed memory.
92 -- GC Neutral
93 Can:
94 Call other GC Neutral mode functions.
96 Cannot:
97 Touch managed memory.
98 Call foreign native code (embedder callbacks, pinvokes, etc)
99 Call into any Blocking functions/syscalls (mutexes, IO, etc)
100 Be dettached.
102 What's good for?
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 (); \
109 } while (0);
111 #define MONO_REQ_GC_UNSAFE_MODE do { \
112 assert_gc_unsafe_mode (); \
113 } while (0);
115 #define MONO_REQ_GC_NEUTRAL_MODE do { \
116 assert_gc_neutral_mode (); \
117 } while (0);
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 \
125 do { \
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); \
130 } while(0)
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(); \
135 } while(0)
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(); \
140 } while(0)
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);
151 #else
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)); \
170 (ptr) = (val); \
171 } while (0);
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)); \
176 (ptr) = (val); \
177 } while (0);
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)); \
183 } while (0);
185 void check_metadata_store(void *from, void *to);
186 void check_metadata_store_local(void *from, void *to);
188 #else
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); \
200 } while (0)
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 #else
206 #define CHECKED_BUILD_THREAD_TRANSITION(transition, info, from_state, suspend_count, next_state, suspend_count_delta)
208 #endif /* defined(ENABLE_CHECKED_BUILD_THREAD) */
210 #endif /* __CHECKED_BUILD_H__ */