Skip to content

mono sgen problem  #85318

Closed
Closed

Description

@srxqds

we have hit the sgen proble when gc collection, the whole call stack show below :

>	mono-2.0-sgen.dll!copy_object_no_checks(_MonoObject * obj, _SgenGrayQueue * queue) Line 69	C
 	[Inline Frame] mono-2.0-sgen.dll!major_copy_or_mark_object_with_evacuation(_MonoObject * * obj, _MonoObject *) Line 86	C
 	mono-2.0-sgen.dll!major_scan_object_with_evacuation(_MonoObject * full_object, unsigned __int64 desc, _SgenGrayQueue * queue) Line 64	C
 	[Inline Frame] mono-2.0-sgen.dll!SGEN_LOAD_VTABLE_UNCHECKED(_MonoObject *) Line 23	C
 	[Inline Frame] mono-2.0-sgen.dll!sgen_safe_object_get_size(_MonoObject *) Line 757	C
 	mono-2.0-sgen.dll!sgen_cardtable_scan_object(_MonoObject * obj, unsigned __int64 block_obj_size, unsigned char * cards, ScanCopyContext ctx) Line 594	C
 	mono-2.0-sgen.dll!scan_card_table_for_block(_MSBlockInfo * block, CardTableScanType scan_type, ScanCopyContext ctx) Line 2625	C
 	mono-2.0-sgen.dll!major_scan_card_table(CardTableScanType scan_type, ScanCopyContext ctx, int job_index, int job_split_count, int block_count) Line 2667	C
 	mono-2.0-sgen.dll!job_scan_major_mod_union_card_table(void * worker_data_untyped, _SgenThreadPoolJob * job) Line 1483	C
 	mono-2.0-sgen.dll!sgen_workers_enqueue_job(int generation, _SgenThreadPoolJob * job, int enqueue) Line 185	C
 	mono-2.0-sgen.dll!major_copy_or_mark_from_roots(_SgenGrayQueue * gc_thread_gray_queue, unsigned __int64 * old_next_pin_slot, CopyOrMarkFromRootsMode mode, SgenObjectOperations * object_ops_nopar, SgenObjectOperations * object_ops_par) Line 2107	C
 	mono-2.0-sgen.dll!major_finish_collection(_SgenGrayQueue * gc_thread_gray_queue, const char * reason, int is_overflow, unsigned __int64 old_next_pin_slot, int forced) Line 2212	C
 	[Inline Frame] mono-2.0-sgen.dll!major_finish_concurrent_collection(int) Line 2460	C
 	mono-2.0-sgen.dll!sgen_perform_collection_inner(unsigned __int64 requested_size, int generation_to_collect, const char * reason, int forced_serial, int stw) Line 2549	C
 	[Inline Frame] mono-2.0-sgen.dll!sgen_perform_collection(unsigned __int64) Line 2636	C
 	mono-2.0-sgen.dll!sgen_ensure_free_space(unsigned __int64 size, int generation) Line 2511	C
 	mono-2.0-sgen.dll!sgen_alloc_obj_nolock(MonoVTable * vtable, unsigned __int64 size) Line 241	C
 	mono-2.0-sgen.dll!mono_gc_alloc_vector(MonoVTable * vtable, unsigned __int64 size, unsigned __int64 max_length) Line 1317	C

the vt is nullptr

static MONO_NEVER_INLINE GCObject *
copy_object_no_checks (GCObject *obj, SgenGrayQueue *queue)
{
	GCVTable vt = SGEN_LOAD_VTABLE_UNCHECKED (obj);
	gboolean has_references = SGEN_VTABLE_HAS_REFERENCES (vt);

how can I figure out the reason about it, can you give some advice or tricks to find what's wrong with it.

Activity

  1. ghost added
    needs-area-label
    on Apr 25, 2023
  2. ghost added
    untriaged
    on Apr 25, 2023
  3. srxqds commented on Apr 25, 2023

    @srxqds
    ContributorAuthor

    or any debug settings?

  4. added and removed
    needs-area-label
    on Apr 25, 2023
  5. srxqds commented on Apr 28, 2023

    @srxqds
    ContributorAuthor

    hi, @lambdageek @BrzVlad can you help me?
    are there any settings to help me check this problem?
    it's not reproduced stable but more common

  6. BrzVlad commented on Apr 28, 2023

    @BrzVlad
    Member

    Seems like a missing write barrier. You could try running with MONO_GC_DEBUG=check-remset-consistency. This will look up missing cards for major->minor references.

    Concurrent collector requires also marking cards for major->major references. Running with MONO_GC_PARAMS=major=marksweep might make the crash go away.

    The crash seems to happen during a collection compacting the heap. MONO_GC_PARAMS=no-evacuation might also influence the crash.

  7. srxqds commented on Apr 28, 2023

    @srxqds
    ContributorAuthor

    Seems like a missing write barrier. You could try running with MONO_GC_DEBUG=check-remset-consistency. This will look up missing cards for major->minor references.

    Concurrent collector requires also marking cards for major->major references. Running with MONO_GC_PARAMS=major=marksweep might make the crash go away.

    The crash seems to happen during a collection compacting the heap. MONO_GC_PARAMS=no-evacuation might also influence the crash.

    we are using default gc settings

  8. removed
    untriaged
    on Apr 28, 2023
  9. steveisok commented on Apr 28, 2023

    @steveisok
    Member

    @srxqds what's the environment where you're hitting this? Am I right in assuming .NET 8?

  10. added this to the 8.0.0 milestone on Apr 28, 2023
  11. srxqds commented on Apr 29, 2023

    @srxqds
    ContributorAuthor

    @srxqds what's the environment where you're hitting this? Am I right in assuming .NET 8?

    we are using the mono-6.0 version from mono/mono, the dotnet/runtime 7.0 version seams also have this problem.

    so I need to find out what's the problem. I will try the advice from @BrzVlad

  12. srxqds commented on May 2, 2023

    @srxqds
    ContributorAuthor

    hi, @BrzVlad When I use MONO_GC_DEBUG=check-remset-consistency, it check failed

    2023-05-02 08:50:44 Fragment creation: 33468 usecs, 146856 bytes available
    2023-05-02 08:50:44 Heap size: 118281840, LOS size: 75502064
    2023-05-02 08:50:44 nursery collection didn't find enough room for 4096 alloc (0 pinned)
    2023-05-02 08:50:44 restarted (pause time: 124169 usec, max: 226658)
    2023-05-02 08:51:50 Start nursery collection 58 000001FA82000000-000001FA82400000, size: 4194304
    2023-05-02 08:51:50 Scanning pinned roots (1672887 bytes, 3195/18 entries)
    2023-05-02 08:51:50 Begin heap consistency check...
    2023-05-02 08:51:50 Oldspace->newspace reference 000001FA8210C8C8 at offset 256 in object 000002029AC7CCB8 (UnrealEngine.Game.UI.UUI_CommonClick) not found in remsets.
    2023-05-02 08:51:50 Oldspace->newspace reference 000001FA8210C8C8 at offset 256 in object 000002029AC7CE00 (UnrealEngine.Game.UI.UUI_CommonClick) not found in remsets.
    2023-05-02 08:51:50 Heap consistency check done.
    
  13. BrzVlad commented on May 2, 2023

    @BrzVlad
    Member

    This basically means that, into an object of type UnrealEngine.Game.UI.UUI_CommonClick at offset of 256 a object reference is stored without the proper write barrier. You could figure out the field from C# definition, although it would be best if you could break in the debugger when this heap check fails so you can investigate the type of the object in the nursery. You would want then to lookup into the code base to see where this storing happens and what goes wrong. Especially if this is done in native code.

  14. 2 remaining items

  15. srxqds commented on May 4, 2023

    @srxqds
    ContributorAuthor

    and another check:

    2023-05-04 08:41:32 Oldspace->newspace reference 0000025E8AAAFA60 at offset 3984 in object 0000026393902070 (.Entry[]) not found in remsets.
    2023-05-04 08:41:32 Oldspace->newspace reference 0000025E8AAAFA38 at offset 3992 in object 0000026393902070 (.Entry[]) not found in remsets.
    

    we couldn't find the Entry type.

  16. BrzVlad commented on May 4, 2023

    @BrzVlad
    Member

    You can use mono_value_copy if you want to copy an entire struct (containing also object fields) into another managed object. For your scenario you just need to do mono_gc_wbarrier_generic_store (Value, Result).

    If you are not storing inside GC memory, there is no need for a write barrier. However, you would need to allocate a pinned GC handle for that object (mono_gchandle_new). Otherwise, since the GC doesn't now that you are holding a reference to it in native code, it can just collect it and that object becomes corrupted.

    Not sure about this other Entry type. Can you attach with native debugger and obtain more information about this object ?

  17. srxqds commented on May 5, 2023

    @srxqds
    ContributorAuthor

    if I do show as below, is there any problem?

    internal_call(void* Ptr, MonoClass* Klass)
    {
    void* placeholderPtr = malloc();
    MonoObject* result = create(); 
    mono_gc_wbarrier_generic_store (placeholderPtr , result);
    mono_gc_wbarrier_generic_store (Ptr, placeholderPtr );
    }

    I have call two mono_gc_wbarrier_generic_store call, will this has any ill effect?

  18. srxqds commented on May 5, 2023

    @srxqds
    ContributorAuthor

    and another solution is set out variable at c# code:

                    [MethodImpl(MethodImplOptions.InternalCall)]
    		private static extern bool Internal_GetUObjectPropertyGenericValue<T>(UObject self, string Name, out T Value, bool bSearchScriptOnly, Type T_typeof);
    
    		public bool GetUObjectPropertyGenericValue<T>(string Name, out T Value, bool bSearchScriptOnly) 
    		{
                            T OutValue;
    			bool Result = Internal_GetUObjectPropertyGenericValue(this, Name, out OutValue, bSearchScriptOnly, typeof(T));
                            Value = OutValue;
                           return Result;
    		}

    this implement will solve the problem?

    @BrzVlad could you give me some time to help me with these two questions?

  19. BrzVlad commented on May 8, 2023

    @BrzVlad
    Member

    In the first example, both of the write barriers are incorrect. mono_gc_wbarrier_generic_store (placeholderPtr , result); is marking some cards that are never scanned by the GC, since placeholderPtr is not inside managed heap, so it is equivalent to *(MonoObject**)placeholderPtr = result. For mono_gc_wbarrier_generic_store (Ptr, placeholderPtr );, placeholderPtr is not a MonoObject* so it makes no sense to emit a write barrier when storing it anywhere. Also, in this example, nothing might be keeping result alive.

    The second solution looks correct.

  20. srxqds commented on May 8, 2023

    @srxqds
    ContributorAuthor

    In the first example, both of the write barriers are incorrect. mono_gc_wbarrier_generic_store (placeholderPtr , result); is marking some cards that are never scanned by the GC, since placeholderPtr is not inside managed heap, so it is equivalent to *(MonoObject**)placeholderPtr = result. For mono_gc_wbarrier_generic_store (Ptr, placeholderPtr );, placeholderPtr is not a MonoObject* so it makes no sense to emit a write barrier when storing it anywhere. Also, in this example, nothing might be keeping result alive.

    The second solution looks correct.

    how about change mono_gc_wbarrier_generic_store (Ptr, placeholderPtr ); to mono_gc_wbarrier_generic_store (Ptr, *(MonoObject**)placeholderPtr );

    I have written wrong, if it is ok, we will choose the first solution to solve, because the second solution is make copy twice times.

  21. BrzVlad commented on May 8, 2023

    @BrzVlad
    Member

    What is the point of placeholderPtr then ? I assume you want to do :

    internal_call(void* Ptr, MonoClass* Klass)
    {
    MonoObject* result = create(); 
    mono_gc_wbarrier_generic_store (Ptr, result);
    }
    

    Which should also be correct, assuming the result object is kept alive up until being store into Ptr (which I assume is part of managed object)

  22. srxqds commented on May 8, 2023

    @srxqds
    ContributorAuthor

    mono_gc_wbarrier_generic_store

    yeah, the ptr is from managed object like out T value

    the placeholderPtr is the intermediate variable, sometimes we need pre-allocated and use it to restore the monoobject* value from the intermediate function call.

  23. modified the milestones: 8.0.0, 9.0.0 on Jul 25, 2023
  24. modified the milestones: 9.0.0, Future on Jul 19, 2024
  25. BrzVlad commented on Oct 21, 2024

    @BrzVlad
    Member

    seems like the questions in this issue were addressed

  26. locked and limited conversation to collaborators on Nov 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions