[Mono-dev] Generics sharing issues

Mark Probst mark.probst at gmail.com
Tue Dec 11 04:13:14 EST 2007


Hi Kamil!

> Just to note - specification explicitly states that all instantiations
> must be finite. The above code is invalid (though csc has a bug
> allowing it to compile, it will still fail in runtime on MS.NET).
> The exact description is in ECMA 355 Part II 9.2  Generics and
> recursive inheritance graphs

Thanks for the info!  Unfortunately, it doesn't make our lives much
easier, i.e., it doesn't save us from implementing a check when
accessing at least some fields in the rgctx, because of the other
example:

class A<T> {
  public static int test (int n) {
    if (n <= 1)
      return 123;
    else
      return A<A<T>>.test (n - 1);
  }
}

This example successfully runs in the MS runtime with values for n of
up to 100.  101 fails.  That means they're either instantiating the
nested generic class lazily and just do a nesting depth check when
creating a new instantiation, or they're instantiating eagerly but
have a marker for those cases where instantiation failed (because the
nesting depth limit was reached).  In either case a check at method
run-time is necessary.

Something I forgot: We also discussed doing the check via the MMU,
i.e. putting NULL in the not-yet-instantiated fields and just deref
the field as a check.  In the NULL case we'll get a SEGV and the
handler can reconstruct what happened.  Hairy to implement but almost
cost-free at run-time.

> This probably does not help solving the problem, since runtime has no
> way to easily detect the wrong cycles - the algorithm described in
> spec seems a bit costy.

Actually it's a pretty cheap algorithm, but the MS runtime doesn't
seem to use it.  They just instantiate types until they hit the
nesting depth of 100, at which point they throw an exception.

Mark



More information about the Mono-devel-list mailing list