il-repack icon indicating copy to clipboard operation
il-repack copied to clipboard

Bug: cannot reduce access in 2.0.15

Open coderb opened this issue 6 years ago • 10 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>();
    }
}

coderb avatar Feb 12 '18 22:02 coderb

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

gluck avatar Feb 13 '18 08:02 gluck

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?

coderb avatar Feb 13 '18 08:02 coderb

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

gluck avatar Feb 13 '18 08:02 gluck

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?

coderb avatar Feb 13 '18 08:02 coderb

Any action was taken against this issue?

skarllot avatar Nov 22 '18 13:11 skarllot

This is happening to me when packing Npgsql.

Logerfo avatar Sep 30 '19 20:09 Logerfo

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.

titomor-outsystems avatar Oct 21 '20 11:10 titomor-outsystems

I fixed this in my fork. Waiting for pull request https://github.com/gluck/il-repack/pull/278 to be merged.

titomor-outsystems avatar Oct 21 '20 11:10 titomor-outsystems

@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

timotei avatar Mar 09 '21 16:03 timotei

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.

knutdrofus avatar Mar 07 '22 14:03 knutdrofus

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?

idigra avatar Dec 18 '23 08:12 idigra

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

I have a fix ready here: https://github.com/KirillOsenkov/il-repack/commit/a8b6ad2aaf41630e9bc47cbdcdac531f56ffefd0

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

KirillOsenkov avatar Dec 22 '23 01:12 KirillOsenkov

@idigra I also verified that PROSE works fine too.

KirillOsenkov avatar Dec 22 '23 02:12 KirillOsenkov

@idigra I also verified that PROSE works fine too.

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

MaMazav avatar Dec 22 '23 07:12 MaMazav

Yes, stay tuned

KirillOsenkov avatar Dec 22 '23 17:12 KirillOsenkov

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

deniszykov avatar Dec 22 '23 18:12 deniszykov

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

KirillOsenkov avatar Dec 22 '23 18:12 KirillOsenkov

Try this fix out: https://www.nuget.org/packages/ILRepack/2.0.22

KirillOsenkov avatar Jan 02 '24 23:01 KirillOsenkov