gluck/il-repack

Bug: cannot reduce access in 2.0.15

Closed this issue · 18 comments

The following code fails when run through ilrepack on windows. the error is Unhandled Exception: System.TypeLoadException: Derived method 'Meth' in type 'TestIlRepack.ArrayHandler``1' from assembly 'x, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' cannot reduce access.

The method in Class3 is having its access changed to internal from public while the base class remains public.

using System;
using System.Threading.Tasks;

namespace TestIlRepack {
    class Program {
        static void Main(string[] args) {
            GC.KeepAlive(new Class3());
            System.Console.WriteLine("done");
        }
    }

    class Class3 : Class2<Array> {
        public override ValueTask<Array> Meth() { 
            throw new NotImplementedException();
        }
    }

    abstract class Class2<T> : Class1 {

        public abstract ValueTask<T> Meth();

        internal override ValueTask<T2> Meth<T2>() {
            throw new NotImplementedException();
        }
    }

    abstract class Class1 {
        internal abstract ValueTask<T> Meth<T>();
    }
}

gluck commented

Hi, what command/arguments do you pass to ILRepack ? also which version are you using ?

version 2.0.15

no special flags, just input and output file name.

the packed file only fails to run on windows. mono/linux seems to be tolerant of the access tightening that happens on Class3.

have you tried to reproduce?

gluck commented

Thanks.
Not yet tried, but vould be related to the logic here if you wanna peek.

looked at the code, just a guess:
...it doesn't fail if Class1 is removed, so could it be that the "base-most method" logic is confusing the 2 method signatures?

Any action was taken against this issue?

This is happening to me when packing Npgsql.

It's happening to me as well in Npgsql (v 4.1.5). Derived method 'Read' in type 'Npgsql.TypeHandlers.TextHandler' from assembly 'Npgsql, Version=4.1.5.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7' cannot reduce access.

I fixed this in my fork. Waiting for pull request #278
to be merged.

@Logerfo @titomor-outsystems I just tried merging version 4.1.5 and it worked fine. How are you invoking it?

Here's the output trying to merge it for net461, netstandard2.0 and netcore3.0

ILRepack.exe "F:\Downloads\npgsql.4.1.5\lib\net461\Npgsql.dll" /out:"F:\Downloads\npgsql.4.1.5\lib\net461\Npgsql.out.dll"
INFO: IL Repack - Version 2.1.0
INFO: ------------- IL Repack Arguments -------------
/out:F:\Downloads\npgsql.4.1.5\lib\net461\Npgsql.out.dll  F:\Downloads\npgsql.4.1.5\lib\net461\Npgsql.dll
-----------------------------------------------
INFO: Adding assembly for merge: F:\Downloads\npgsql.4.1.5\lib\net461\Npgsql.dll
INFO: Processing references
INFO: Processing types
INFO: Merging <Module>
INFO: Processing exported types
INFO: Processing resources
INFO: Fixing references
INFO: Writing output assembly to disk
INFO: Finished in 00:00:00.8895930


ILRepack.exe "F:\Downloads\npgsql.4.1.5\lib\netstandard2.0\Npgsql.dll" /out:"F:\Downloads\npgsql.4.1.5\lib\netstandard2.0\Npgsql.out.dll"
INFO: IL Repack - Version 2.1.0
INFO: ------------- IL Repack Arguments -------------
/out:F:\Downloads\npgsql.4.1.5\lib\netstandard2.0\Npgsql.out.dll  F:\Downloads\npgsql.4.1.5\lib\netstandard2.0\Npgsql.dll
-----------------------------------------------
INFO: Adding assembly for merge: F:\Downloads\npgsql.4.1.5\lib\netstandard2.0\Npgsql.dll
INFO: Processing references
INFO: Processing types
INFO: Merging <Module>
INFO: Processing exported types
INFO: Processing resources
INFO: Fixing references
INFO: Writing output assembly to disk
INFO: Finished in 00:00:00.9582638


ILRepack.exe "F:\Downloads\npgsql.4.1.5\lib\netcoreapp3.0\Npgsql.dll" /out:"F:\Downloads\npgsql.4.1.5\lib\netcoreapp3.0\Npgsql.out.dll"
INFO: IL Repack - Version 2.1.0
INFO: ------------- IL Repack Arguments -------------
/out:F:\Downloads\npgsql.4.1.5\lib\netcoreapp3.0\Npgsql.out.dll  F:\Downloads\npgsql.4.1.5\lib\netcoreapp3.0\Npgsql.dll
-----------------------------------------------
INFO: Adding assembly for merge: F:\Downloads\npgsql.4.1.5\lib\netcoreapp3.0\Npgsql.dll
INFO: Processing references
INFO: Processing types
INFO: Merging <Module>
INFO: Processing exported types
INFO: Processing resources
INFO: Fixing references
INFO: Writing output assembly to disk
INFO: Finished in 00:00:00.9131766

I ran into this for Npgsql as well, using ilrepack 2.0.18. I created a small sample project that demonstrates the problem here: https://github.com/knutdrofus/ilrepack_npgsql_issue

For me, the error appears at runtime, during a type discovery phase during startup, when we're manually feeding types to an IOC container. Not sure if this is a bug or just me using ilrepack incorrectly.

idigra commented

Also hit this on Program Synthesis: https://www.microsoft.com/en-us/research/project/prose-framework/.

In that case, it's for protected method that overrides a parent class' method, and later overriden by a deeper child.

Any plan to merge the fix and publish an official nuget?

Thank you @coderb and @knutdrofus for providing detailed repros! They were very helpful when investigating this.

I have a fix ready here:
KirillOsenkov@a8b6ad2

I've verified that it fixes both npgsql as well as the example at the top of this bug.

@idigra I also verified that PROSE works fine too.

@idigra I also verified that PROSE works fine too.

Great, thanks :) Is an official nuget with the fix planned to be released?

Yes, stay tuned

@KirillOsenkov do you plan update Mono.Cecil to latest version?

Yes, will do a bunch of modernization and go through the outstanding PRs and bugs as time allows