+ } else if (atype == ATYPE_STRING) {
+ int pos;
+
+ /*
+ * a string allocator method takes the args: (vtable, len)
+ *
+ * bytes = sizeof (MonoString) + ((len + 1) * 2)
+ *
+ * condition:
+ *
+ * bytes <= INT32_MAX - (SGEN_ALLOC_ALIGN - 1)
+ *
+ * therefore:
+ *
+ * sizeof (MonoString) + ((len + 1) * 2) <= INT32_MAX - (SGEN_ALLOC_ALIGN - 1)
+ * len <= (INT32_MAX - (SGEN_ALLOC_ALIGN - 1) - sizeof (MonoString)) / 2 - 1
+ */
+ mono_mb_emit_ldarg (mb, 1);
+ mono_mb_emit_icon (mb, (INT32_MAX - (SGEN_ALLOC_ALIGN - 1) - sizeof (MonoString)) / 2 - 1);
+ pos = mono_mb_emit_short_branch (mb, MONO_CEE_BLE_UN_S);
+
+ mono_mb_emit_byte (mb, MONO_CUSTOM_PREFIX);
+ mono_mb_emit_byte (mb, CEE_MONO_NOT_TAKEN);
+ mono_mb_emit_exception (mb, "OutOfMemoryException", NULL);
+ mono_mb_patch_short_branch (mb, pos);
+
+ mono_mb_emit_ldarg (mb, 1);
+ mono_mb_emit_icon (mb, 1);
+ mono_mb_emit_byte (mb, MONO_CEE_SHL);
+ //WE manually fold the above + 2 here
+ mono_mb_emit_icon (mb, sizeof (MonoString) + 2);
+ mono_mb_emit_byte (mb, CEE_ADD);
+ mono_mb_emit_stloc (mb, size_var);