Bug 15574 - Recursive KnownType (as output by slsvcutil.exe) causes stack overflow during DataContractSerializer constructor
Summary: Recursive KnownType (as output by slsvcutil.exe) causes stack overflow during...
Status: RESOLVED FIXED
Alias: None
Product: Class Libraries
Classification: Mono
Component: WCF assemblies ()
Version: 3.2.x
Hardware: PC Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2013-10-22 00:55 UTC by Brendan Zagaeski (Xamarin Team, assistant)
Modified: 2014-02-12 15:39 UTC (History)
2 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:


Attachments
Test case (2.33 KB, application/zip)
2013-10-22 00:55 UTC, Brendan Zagaeski (Xamarin Team, assistant)
Details
Example WCF host (6.61 KB, application/zip)
2013-10-22 00:59 UTC, Brendan Zagaeski (Xamarin Team, assistant)
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description Brendan Zagaeski (Xamarin Team, assistant) 2013-10-22 00:55:28 UTC
Created attachment 5199 [details]
Test case

Reported on behalf of a user.

## Summary

1. Run the attached test case in Mono to produce the following output:

> CustomAttributes:
> KnownTypesTest.MyObject[]
> Warning: Degraded allocation.  Consider increasing nursery-size if the warning persists.
> Warning: Degraded allocation.  Consider increasing nursery-size if the warning persists.
> Stack overflow in unmanaged: IP: 0x17ca6c, fault addr: 0xbf729fdc


2. Set a breakpoint on RegisterTypeAsKnown [1], and "Continue" several times to see the stack start growing. The problem is that one of MyObject's custom attributes is `MyObject[]`. This causes unlimited recursion [2] because the `MyObject[]` type never satisfies the condition `known_types.Contains (type)`.

> [1] System.Runtime.Serialization.DataContractSerializer.RegisterTypeAsKnown

> [2] https://github.com/mono/mono/blob/master/mcs/class/System.Runtime.Serialization/System.Runtime.Serialization/DataContractSerializer.cs#L245-L263


3. Building and running the test case on .NET produces no stack overflow, and completes successfully.


## "Workaround"

This test case is an out-of-context example, so it is simple to fix it by removing the line `[KnownType (typeof(MyObject[]))]`. But SlSvcUtil.exe routinely outputs this kind of recursive KnownType under two conditions:

1. The service provides a method that returns an array of some class type (for example [1]).
2. That class includes at least one `object` DataMember (for example [2]).


### [1]
public MyObject[] GetArray()
{
    return new MyObject[] { new MyObject() };
}


### [2]
[DataContract]
public class MyObject
{
    [DataMember]
    public object Data { get; set; }
}


## Version information
Mono 3.2.3
Comment 1 Brendan Zagaeski (Xamarin Team, assistant) 2013-10-22 00:59:20 UTC
Created attachment 5200 [details]
Example WCF host

The proxy that SlSvcUtil.exe generates for this example host will include a MyObject class declaration with a "recursive" `[KnownType(typeof(MyObject[]))]` attribute.
Comment 2 Brendan Zagaeski (Xamarin Team, assistant) 2013-11-26 22:57:09 UTC
https://github.com/mono/mono/pull/819

Pull request that works around the problem for the test case, and that might be acceptable as a complete fix.
Comment 3 Miguel de Icaza [MSFT] 2014-02-12 15:30:41 UTC
Landed the patch.
Comment 4 Miguel de Icaza [MSFT] 2014-02-12 15:39:58 UTC
Applied patch, and turned test case into unit test thanks!