Skip to content

Add option for strict write barriers #1008

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions mono/metadata/appdomain.c
Original file line number Diff line number Diff line change
Expand Up @@ -198,13 +198,15 @@ create_domain_objects (MonoDomain *domain)
mono_error_assert_ok (&error);
mono_field_static_set_value (string_vt, string_empty_fld, empty_str);
domain->empty_string = empty_str;
mono_gc_wbarrier_generic_nostore (&domain->empty_string);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure it matters, but would replacing assignment above with mono_gc_wbarrier_generic_store be better?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, guess that is nicer. Will change and then merge.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(This and the other instances)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may make merge conflicts with upstream more frequent. Just wondering what practical difference is if anything.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there should be no practical difference

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at it, I decided not to change it. The functionality is identical, and the code did not look nicer by merging the assignment and the write barrier, as lines become very long. Merged as is.


/*
* Create an instance early since we can't do it when there is no memory.
*/
arg = mono_string_new_checked (domain, "Out of memory", &error);
mono_error_assert_ok (&error);
domain->out_of_memory_ex = mono_exception_from_name_two_strings_checked (mono_defaults.corlib, "System", "OutOfMemoryException", arg, NULL, &error);
mono_gc_wbarrier_generic_nostore (&domain->out_of_memory_ex);
mono_error_assert_ok (&error);

/*
Expand All @@ -214,14 +216,17 @@ create_domain_objects (MonoDomain *domain)
arg = mono_string_new_checked (domain, "A null value was found where an object instance was required", &error);
mono_error_assert_ok (&error);
domain->null_reference_ex = mono_exception_from_name_two_strings_checked (mono_defaults.corlib, "System", "NullReferenceException", arg, NULL, &error);
mono_gc_wbarrier_generic_nostore (&domain->null_reference_ex);
mono_error_assert_ok (&error);
arg = mono_string_new_checked (domain, "The requested operation caused a stack overflow.", &error);
mono_error_assert_ok (&error);
domain->stack_overflow_ex = mono_exception_from_name_two_strings_checked (mono_defaults.corlib, "System", "StackOverflowException", arg, NULL, &error);
mono_gc_wbarrier_generic_nostore (&domain->stack_overflow_ex);
mono_error_assert_ok (&error);

/*The ephemeron tombstone i*/
domain->ephemeron_tombstone = mono_object_new_checked (domain, mono_defaults.object_class, &error);
mono_gc_wbarrier_generic_nostore (&domain->ephemeron_tombstone);
mono_error_assert_ok (&error);

if (domain != old_domain) {
Expand Down Expand Up @@ -292,7 +297,9 @@ mono_runtime_init_checked (MonoDomain *domain, MonoThreadStartCB start_cb, MonoT

ad->data = domain;
domain->domain = ad;
mono_gc_wbarrier_generic_nostore (&domain->domain);
domain->setup = setup;
mono_gc_wbarrier_generic_nostore (&domain->setup);

mono_thread_attach (domain);

Expand Down
63 changes: 44 additions & 19 deletions mono/metadata/boehm-gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ void *pthread_get_stackaddr_np(pthread_t);
#define MIN_BOEHM_MAX_HEAP_SIZE (MIN_BOEHM_MAX_HEAP_SIZE_IN_MB << 20)

static gboolean gc_initialized = FALSE;
static gboolean gc_strict_wbarriers = FALSE;
static mono_mutex_t mono_gc_lock;

typedef void (*GC_push_other_roots_proc)(void);
Expand Down Expand Up @@ -247,7 +248,10 @@ mono_gc_base_init (void)
#endif
}
continue;
} else {
} else if (g_str_has_prefix (opt, "strict-wbarriers")) {
gc_strict_wbarriers = TRUE;
continue;
}else {
/* Could be a parameter for sgen */
/*
fprintf (stderr, "MONO_GC_PARAMS must be a comma-delimited list of one or more of the following:\n");
Expand All @@ -273,6 +277,24 @@ mono_gc_base_init (void)
gc_initialized = TRUE;
}

void
mono_gc_dirty(void **ptr)
{
GC_dirty (ptr);
}

void
mono_gc_dirty_range(void **ptr, size_t size)
{
if (G_UNLIKELY(gc_strict_wbarriers))
{
for (int i = 0; i < size/sizeof(void*); i++)
GC_dirty(ptr + i);
}
else
GC_dirty (ptr);
}

void
mono_gc_base_cleanup (void)
{
Expand Down Expand Up @@ -647,7 +669,7 @@ mono_gc_weak_link_add (void **link_addr, MonoObject *obj, gboolean track)
{
/* libgc requires that we use HIDE_POINTER... */
*link_addr = (void*)HIDE_POINTER (obj);
GC_dirty (link_addr);
mono_gc_dirty (link_addr);
if (track)
GC_REGISTER_LONG_LINK (link_addr, obj);
else
Expand Down Expand Up @@ -892,57 +914,60 @@ void
mono_gc_wbarrier_set_field (MonoObject *obj, gpointer field_ptr, MonoObject* value)
{
*(void**)field_ptr = value;
GC_dirty (field_ptr);
mono_gc_dirty (field_ptr);
}

void
mono_gc_wbarrier_set_arrayref (MonoArray *arr, gpointer slot_ptr, MonoObject* value)
{
*(void**)slot_ptr = value;
GC_dirty (slot_ptr);
mono_gc_dirty (slot_ptr);
}

void
mono_gc_wbarrier_arrayref_copy (gpointer dest_ptr, gpointer src_ptr, int count)
{
mono_gc_memmove_aligned (dest_ptr, src_ptr, count * sizeof (gpointer));
GC_dirty (dest_ptr);
mono_gc_dirty_range (dest_ptr, count * sizeof(gpointer));
}

void
mono_gc_wbarrier_generic_store (gpointer ptr, MonoObject* value)
{
*(void**)ptr = value;
GC_dirty (ptr);
mono_gc_dirty (ptr);
}

void
mono_gc_wbarrier_generic_store_atomic (gpointer ptr, MonoObject *value)
{
mono_atomic_store_ptr ((volatile gpointer *)ptr, value);
GC_dirty (ptr);
mono_gc_dirty (ptr);
}

void
mono_gc_wbarrier_generic_nostore (gpointer ptr)
{
GC_dirty (ptr);
mono_gc_dirty (ptr);
}

void
mono_gc_wbarrier_value_copy (gpointer dest, gpointer src, int count, MonoClass *klass)
{
mono_gc_memmove_atomic (dest, src, count * mono_class_value_size (klass, NULL));
GC_dirty (dest);
size_t size = count * mono_class_value_size (klass, NULL);
mono_gc_memmove_atomic (dest, src, size);
mono_gc_dirty_range (dest, size);
}

void
mono_gc_wbarrier_object_copy (MonoObject* obj, MonoObject *src)
{
/* do not copy the sync state */
mono_gc_memmove_aligned ((char*)obj + sizeof (MonoObject), (char*)src + sizeof (MonoObject),
mono_object_class (obj)->instance_size - sizeof (MonoObject));
GC_dirty (obj);
size_t size = mono_object_class (obj)->instance_size - sizeof (MonoObject);
char * dstPtr = (char*)obj + sizeof (MonoObject);
mono_gc_memmove_aligned (dstPtr, (char*)src + sizeof (MonoObject),
size);
mono_gc_dirty_range ((void**)dstPtr, size);
}

void
Expand Down Expand Up @@ -1514,7 +1539,7 @@ void
mono_gc_wbarrier_range_copy (gpointer _dest, gpointer _src, int size)
{
memcpy (_dest, _src, size);
GC_dirty (_dest);
mono_gc_dirty_range (_dest, size);
}

void*
Expand Down Expand Up @@ -1875,7 +1900,7 @@ handle_data_grow (HandleData *handles, gboolean track)
gpointer *entries;
entries = (void **)mono_gc_alloc_fixed (sizeof (*handles->entries) * new_size, NULL, MONO_ROOT_SOURCE_GC_HANDLE, NULL, "GC Handle Table (Boehm)");
mono_gc_memmove_aligned (entries, handles->entries, sizeof (*handles->entries) * handles->size);
GC_dirty (entries);
mono_gc_dirty_range (entries, new_size * sizeof (*handles->entries));
mono_gc_free_fixed (handles->entries);
handles->entries = entries;
}
Expand Down Expand Up @@ -1908,7 +1933,7 @@ alloc_handle (HandleData *handles, MonoObject *obj, gboolean track)
mono_gc_weak_link_add (&(handles->entries [slot]), obj, track);
} else {
handles->entries [slot] = obj;
GC_dirty (handles->entries + slot);
mono_gc_dirty (handles->entries + slot);
}

#ifndef DISABLE_PERFCOUNTERS
Expand Down Expand Up @@ -2026,7 +2051,7 @@ mono_gchandle_set_target (guint32 gchandle, MonoObject *obj)
handles->domain_ids [slot] = (obj ? mono_object_get_domain (obj) : mono_domain_get ())->domain_id;
} else {
handles->entries [slot] = obj;
GC_dirty (handles->entries + slot);
mono_gc_dirty (handles->entries + slot);
}
} else {
/* print a warning? */
Expand Down Expand Up @@ -2105,7 +2130,7 @@ mono_gchandle_free (guint32 gchandle)
mono_gc_weak_link_remove (&handles->entries [slot], handles->type == HANDLE_WEAK_TRACK);
} else {
handles->entries [slot] = NULL;
GC_dirty (handles->entries + slot);
mono_gc_dirty (handles->entries + slot);
}
vacate_slot (handles, slot);
} else {
Expand Down Expand Up @@ -2148,7 +2173,7 @@ mono_gchandle_free_domain (MonoDomain *domain)
if (handles->entries [slot] && mono_object_domain (handles->entries [slot]) == domain) {
vacate_slot (handles, slot);
handles->entries [slot] = NULL;
GC_dirty (handles->entries + slot);
mono_gc_dirty (handles->entries + slot);
}
}
}
Expand Down
1 change: 1 addition & 0 deletions mono/metadata/domain.c
Original file line number Diff line number Diff line change
Expand Up @@ -923,6 +923,7 @@ mono_domain_set_internal_with_options (MonoDomain *domain, gboolean migrate_exce

SET_APPDOMAIN (domain);
SET_APPCONTEXT (domain->default_context);
mono_gc_wbarrier_generic_nostore (&domain->default_context);

if (migrate_exception) {
thread = mono_thread_internal_current ();
Expand Down
1 change: 1 addition & 0 deletions mono/metadata/icall.c
Original file line number Diff line number Diff line change
Expand Up @@ -2479,6 +2479,7 @@ ves_icall_RuntimeType_GetInterfaces (MonoReflectionTypeHandle ref_type, MonoErro
g_hash_table_destroy (iface_hash);
if (!domain->empty_types) {
domain->empty_types = mono_array_new_cached (domain, mono_defaults.runtimetype_class, 0, error);
mono_gc_wbarrier_generic_nostore (&domain->empty_types);
goto_if_nok (error, fail);
}
return MONO_HANDLE_NEW (MonoArray, domain->empty_types);
Expand Down
3 changes: 3 additions & 0 deletions mono/metadata/reflection.c
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,10 @@ mono_type_get_object_checked (MonoDomain *domain, MonoType *type, MonoError *err
mono_g_hash_table_insert (domain->type_hash, type, res);

if (type->type == MONO_TYPE_VOID)
{
domain->typeof_void = (MonoObject*)res;
mono_gc_wbarrier_generic_nostore (&domain->typeof_void);
}

mono_domain_unlock (domain);
mono_loader_unlock ();
Expand Down
8 changes: 8 additions & 0 deletions mono/metadata/threads.c
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,7 @@ set_current_thread_for_domain (MonoDomain *domain, MonoInternalThread *thread, M

g_assert (!*current_thread_ptr);
*current_thread_ptr = current;
mono_gc_wbarrier_generic_nostore (current_thread_ptr);
}

static MonoThread*
Expand Down Expand Up @@ -1781,6 +1782,7 @@ mono_thread_current (void)
if (!*current_thread_ptr) {
g_assert (domain != mono_get_root_domain ());
*current_thread_ptr = create_thread_object (domain, internal);
mono_gc_wbarrier_generic_nostore (current_thread_ptr);
}
return *current_thread_ptr;
}
Expand All @@ -1798,6 +1800,7 @@ mono_thread_current_for_thread (MonoInternalThread *internal)
if (!*current_thread_ptr) {
g_assert (domain != mono_get_root_domain ());
*current_thread_ptr = create_thread_object (domain, internal);
mono_gc_wbarrier_generic_nostore (current_thread_ptr);
}
return *current_thread_ptr;
}
Expand Down Expand Up @@ -4139,10 +4142,13 @@ mono_alloc_static_data (gpointer **static_data_ptr, guint32 offset, void *alloc_
if (mono_gc_user_markers_supported ())
static_data [i] = g_malloc0 (static_data_size [i]);
else
{
static_data [i] = mono_gc_alloc_fixed (static_data_size [i], MONO_GC_DESCRIPTOR_NULL,
threadlocal ? MONO_ROOT_SOURCE_THREAD_STATIC : MONO_ROOT_SOURCE_CONTEXT_STATIC,
alloc_key,
threadlocal ? "ThreadStatic Fields" : "ContextStatic Fields");
mono_gc_wbarrier_generic_nostore (static_data + i);
}
}
}

Expand Down Expand Up @@ -4221,6 +4227,7 @@ context_adjust_static_data (MonoAppContext *ctx)
if (context_static_info.offset || context_static_info.idx > 0) {
guint32 offset = MAKE_SPECIAL_STATIC_OFFSET (context_static_info.idx, context_static_info.offset, 0);
mono_alloc_static_data (&ctx->static_data, offset, ctx, FALSE);
mono_gc_wbarrier_generic_nostore (&ctx->static_data);
ctx->data->static_data = ctx->static_data;
}
}
Expand Down Expand Up @@ -4250,6 +4257,7 @@ alloc_context_static_data_helper (gpointer key, gpointer value, gpointer user)

guint32 offset = GPOINTER_TO_UINT (user);
mono_alloc_static_data (&ctx->static_data, offset, ctx, FALSE);
mono_gc_wbarrier_generic_nostore (&ctx->static_data);
ctx->data->static_data = ctx->static_data;
}

Expand Down