Skip to content

Commit eb3e935

Browse files
committed
Implement check for sufficient execution stack.
Enable across platforms and expose to embedding API.
1 parent 067ea2f commit eb3e935

File tree

3 files changed

+33
-34
lines changed

3 files changed

+33
-34
lines changed

mono/metadata/icall.c

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,40 +1017,7 @@ ves_icall_System_Runtime_CompilerServices_RuntimeHelpers_RunModuleConstructor (M
10171017
ICALL_EXPORT MonoBoolean
10181018
ves_icall_System_Runtime_CompilerServices_RuntimeHelpers_SufficientExecutionStack (void)
10191019
{
1020-
#if defined(TARGET_WIN32) || defined(HOST_WIN32)
1021-
// It does not work on win32
1022-
#elif defined(TARGET_ANDROID) || defined(__linux__)
1023-
// No need for now
1024-
#else
1025-
guint8 *stack_addr;
1026-
guint8 *current;
1027-
size_t stack_size;
1028-
int min_size;
1029-
MonoInternalThread *thread;
1030-
1031-
mono_thread_info_get_stack_bounds (&stack_addr, &stack_size);
1032-
/* if we have no info we are optimistic and assume there is enough room */
1033-
if (!stack_addr)
1034-
return TRUE;
1035-
1036-
thread = mono_thread_internal_current ();
1037-
// .net seems to check that at least 50% of stack is available
1038-
min_size = thread->stack_size / 2;
1039-
1040-
// TODO: It's not always set
1041-
if (!min_size)
1042-
return TRUE;
1043-
1044-
current = (guint8 *)&stack_addr;
1045-
if (current > stack_addr) {
1046-
if ((current - stack_addr) < min_size)
1047-
return FALSE;
1048-
} else {
1049-
if (current - (stack_addr - stack_size) < min_size)
1050-
return FALSE;
1051-
}
1052-
#endif
1053-
return TRUE;
1020+
return mono_thread_has_sufficient_execution_stack ();
10541021
}
10551022

10561023
ICALL_EXPORT MonoObject *

mono/metadata/threads.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5678,3 +5678,33 @@ MonoException* mono_unity_thread_check_exception()
56785678
unlock_thread(thread);
56795679
return NULL;
56805680
}
5681+
5682+
mono_bool mono_thread_has_sufficient_execution_stack (void)
5683+
{
5684+
guint8* stack_addr;
5685+
guint8* current;
5686+
size_t stack_size;
5687+
size_t min_size;
5688+
5689+
mono_thread_info_get_stack_bounds (&stack_addr, &stack_size);
5690+
/* if we have no info we are optimistic and assume there is enough room */
5691+
if (!stack_addr || !stack_size)
5692+
return TRUE;
5693+
5694+
min_size = stack_size / 2;
5695+
5696+
// TODO: It's not always set
5697+
if (!min_size)
5698+
return TRUE;
5699+
5700+
current = (guint8*)&stack_addr;
5701+
if (current > stack_addr) {
5702+
if ((current - stack_addr) < min_size)
5703+
return FALSE;
5704+
}
5705+
else {
5706+
if (current - (stack_addr - stack_size) < min_size)
5707+
return FALSE;
5708+
}
5709+
return TRUE;
5710+
}

mono/metadata/threads.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ MONO_API mono_bool mono_thread_is_foreign (MonoThread *thread);
5757

5858
extern MONO_API mono_bool mono_thread_detach_if_exiting (void);
5959

60+
MONO_API mono_bool mono_thread_has_sufficient_execution_stack (void);
61+
6062
MONO_END_DECLS
6163

6264
#endif /* _MONO_METADATA_THREADS_H_ */

0 commit comments

Comments
 (0)