#include "mono/utils/mono-memory-model.h"
#include "mono/utils/atomic.h"
#include <mono/utils/mono-threads.h>
+#include <mono/utils/mono-threads-coop.h>
#include <mono/utils/mono-error-internals.h>
#include <string.h>
return result;
}
+static char*
+ves_icall_mono_string_to_utf8 (MonoString *str)
+{
+ MonoError error;
+ char *result = mono_string_to_utf8_checked (str, &error);
+ mono_error_set_pending_exception (&error);
+ return result;
+}
+
void
mono_marshal_init (void)
{
register_icall (mono_string_from_byvalwstr, "mono_string_from_byvalwstr", "obj ptr int", FALSE);
register_icall (mono_string_new_wrapper, "mono_string_new_wrapper", "obj ptr", FALSE);
register_icall (mono_string_new_len_wrapper, "mono_string_new_len_wrapper", "obj ptr int", FALSE);
- register_icall (mono_string_to_utf8, "mono_string_to_utf8", "ptr obj", FALSE);
+ register_icall (ves_icall_mono_string_to_utf8, "ves_icall_mono_string_to_utf8", "ptr obj", FALSE);
register_icall (mono_string_to_lpstr, "mono_string_to_lpstr", "ptr obj", FALSE);
register_icall (mono_string_to_ansibstr, "mono_string_to_ansibstr", "ptr object", FALSE);
register_icall (mono_string_builder_to_utf8, "mono_string_builder_to_utf8", "ptr object", FALSE);
*
* Returns: a utf8 string with the contents of the StringBuilder.
*
- * The return value must be released with g_free.
+ * The return value must be released with mono_marshal_free.
*
* This is a JIT icall, it sets the pending exception and returns NULL on error.
*/
if (gerror) {
g_error_free (gerror);
- g_free (str_utf16);
+ mono_marshal_free (str_utf16);
mono_set_pending_exception (mono_get_exception_execution_engine ("Failed to convert StringBuilder from utf16 to utf8"));
return NULL;
} else {
guint len = mono_string_builder_capacity (sb) + 1;
gchar *res = (gchar *)mono_marshal_alloc (len * sizeof (gchar), &error);
if (!mono_error_ok (&error)) {
- g_free (str_utf16);
+ mono_marshal_free (str_utf16);
g_free (tmp);
mono_error_set_pending_exception (&error);
return NULL;
memcpy (res, tmp, str_len * sizeof (gchar));
res[str_len] = '\0';
- g_free (str_utf16);
+ mono_marshal_free (str_utf16);
g_free (tmp);
return res;
}
*
* Returns: a utf16 string with the contents of the StringBuilder.
*
- * The return value must not be freed.
+ * The return value must be released with mono_marshal_free.
+ *
* This is a JIT icall, it sets the pending exception and returns NULL on error.
*/
gunichar2*
return as;
}
#else
- return mono_string_to_utf8 (s);
+ MonoError error;
+ char *result = mono_string_to_utf8_checked (s, &error);
+ mono_error_set_pending_exception (&error);
+ return result;
#endif
}
void
mono_string_to_byvalstr (gpointer dst, MonoString *src, int size)
{
+ MonoError error;
char *s;
int len;
if (!src)
return;
- s = mono_string_to_utf8 (src);
+ s = mono_string_to_utf8_checked (src, &error);
+ if (mono_error_set_pending_exception (&error))
+ return;
len = MIN (size, strlen (s));
if (len >= size)
len--;
if (exc) {
if (((MonoException*)exc)->stack_trace) {
- char *strace = mono_string_to_utf8 (((MonoException*)exc)->stack_trace);
- char *tmp;
- tmp = g_strdup_printf ("%s\nException Rethrown at:\n", strace);
- g_free (strace);
- MONO_OBJECT_SETREF (((MonoException*)exc), stack_trace, mono_string_new (domain, tmp));
- g_free (tmp);
+ MonoError inner_error;
+ char *strace = mono_string_to_utf8_checked (((MonoException*)exc)->stack_trace, &inner_error);
+ if (is_ok (&inner_error)) {
+ char *tmp;
+ tmp = g_strdup_printf ("%s\nException Rethrown at:\n", strace);
+ g_free (strace);
+ MONO_OBJECT_SETREF (((MonoException*)exc), stack_trace, mono_string_new (domain, tmp));
+ g_free (tmp);
+ } else
+ mono_error_cleanup (&inner_error); /* no stack trace, but at least throw the original exception */
}
mono_set_pending_exception ((MonoException*)exc);
}
MonoClass *klass;
int i, argnum, *tmp_locals;
int type, param_shift = 0;
- static MonoMethodSignature *get_last_error_sig = NULL;
int coop_gc_stack_dummy, coop_gc_var;
memset (&m, 0, sizeof (m));
}
}
- if (MONO_TYPE_ISSTRUCT (sig->ret)) {
- MonoClass *klass = mono_class_from_mono_type (sig->ret);
- mono_class_init (klass);
- if (!(((klass->flags & TYPE_ATTRIBUTE_LAYOUT_MASK) == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT) || klass->blittable)) {
- /* This is used by emit_marshal_vtype (), but it needs to go right before the call */
- mono_mb_emit_byte (mb, MONO_CUSTOM_PREFIX);
- mono_mb_emit_byte (mb, CEE_MONO_VTADDR);
- mono_mb_emit_stloc (mb, m.vtaddr_var);
- }
- }
-
- /* Unblock before converting the result, since that can involve calls into the runtime */
- if (mono_threads_is_coop_enabled ()) {
- mono_mb_emit_ldloc (mb, coop_gc_var);
- mono_mb_emit_ldloc_addr (mb, coop_gc_stack_dummy);
- mono_mb_emit_icall (mb, mono_threads_exit_gc_safe_region_unbalanced);
- }
-
/* Set LastError if needed */
if (piinfo->piflags & PINVOKE_ATTRIBUTE_SUPPORTS_LAST_ERROR) {
+#ifdef TARGET_WIN32
+ static MonoMethodSignature *get_last_error_sig = NULL;
if (!get_last_error_sig) {
get_last_error_sig = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
get_last_error_sig->ret = &mono_defaults.int_class->byval_arg;
get_last_error_sig->pinvoke = 1;
}
-#ifdef TARGET_WIN32
/*
* Have to call GetLastError () early and without a wrapper, since various runtime components could
* clobber its value.
#endif
}
+ if (MONO_TYPE_ISSTRUCT (sig->ret)) {
+ MonoClass *klass = mono_class_from_mono_type (sig->ret);
+ mono_class_init (klass);
+ if (!(((klass->flags & TYPE_ATTRIBUTE_LAYOUT_MASK) == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT) || klass->blittable)) {
+ /* This is used by emit_marshal_vtype (), but it needs to go right before the call */
+ mono_mb_emit_byte (mb, MONO_CUSTOM_PREFIX);
+ mono_mb_emit_byte (mb, CEE_MONO_VTADDR);
+ mono_mb_emit_stloc (mb, m.vtaddr_var);
+ }
+ }
+
+ /* Unblock before converting the result, since that can involve calls into the runtime */
+ if (mono_threads_is_coop_enabled ()) {
+ mono_mb_emit_ldloc (mb, coop_gc_var);
+ mono_mb_emit_ldloc_addr (mb, coop_gc_stack_dummy);
+ mono_mb_emit_icall (mb, mono_threads_exit_gc_safe_region_unbalanced);
+ }
+
/* convert the result */
if (!sig->ret->byref) {
MonoMarshalSpec *spec = mspecs [0];
int
ves_icall_System_Runtime_InteropServices_Marshal_OffsetOf (MonoReflectionType *type, MonoString *field_name)
{
+ MonoError error;
MonoMarshalType *info;
MonoClass *klass;
char *fname;
MONO_CHECK_ARG_NULL (type, 0);
MONO_CHECK_ARG_NULL (field_name, 0);
- fname = mono_string_to_utf8 (field_name);
+ fname = mono_string_to_utf8_checked (field_name, &error);
+ if (mono_error_set_pending_exception (&error))
+ return 0;
klass = mono_class_from_mono_type (type->type);
if (!mono_class_init (klass)) {
mono_set_pending_exception (mono_class_get_exception_for_failure (klass));
gpointer
ves_icall_System_Runtime_InteropServices_Marshal_StringToHGlobalAnsi (MonoString *string)
{
+ MonoError error;
#ifdef HOST_WIN32
char* tres, *ret;
size_t len;
- tres = mono_string_to_utf8 (string);
+ tres = mono_string_to_utf8_checked (string, &error);
+ if (mono_error_set_pending_exception (&error))
+ return NULL;
if (!tres)
return tres;
return ret;
#else
- return mono_string_to_utf8 (string);
+ char *ret = mono_string_to_utf8_checked (string, &error);
+ mono_error_set_pending_exception (&error);
+ return ret;
#endif
}