Bug 31969 - Add support for DisablePrivateReflectionAttribute
Summary: Add support for DisablePrivateReflectionAttribute
Status: CONFIRMED
Alias: None
Product: Runtime
Classification: Mono
Component: Reflection (show other bugs)
Version: unspecified
Hardware: PC Mac OS
: --- normal
Target Milestone: ---
Assignee: Aleksey Kliger
URL:
Depends on:
Blocks:
 
Reported: 2015-07-15 10:26 UTC by Marek Safar
Modified: 2015-10-20 18:00 UTC (History)
2 users (show)

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


Attachments
Example using DisablePrivateReflectionAttribute (6.62 KB, application/zip)
2015-10-16 14:32 UTC, Aleksey Kliger
Details
DisablePrivateReflection example (3.37 KB, application/octet-stream)
2015-10-20 18:00 UTC, Aleksey Kliger
Details

Comment 1 Aleksey Kliger 2015-10-16 14:32:58 UTC
Created attachment 13385 [details]
Example using DisablePrivateReflectionAttribute

What's the correct way to use DisablePrivateReflectionAttribute to get it to actually work on .NET?

Tried the attached project in VS2015 and it happily dumped private fields and methods.  Tried both reflecting on the current assmebly, on a loaded one, and on a dynamic one.
Comment 2 Marek Safar 2015-10-16 14:38:39 UTC
Did you try it not within same assembly?
Comment 3 Aleksey Kliger 2015-10-16 14:43:43 UTC
Yes.  Attached project has two assemblies.  Bug31369 has some classes and has the DisablePrivateReflection attribute.   ReproRuntime is a separate assembly that  calls Assembly.Load() to bring in Bug31369 and then dumps all the fields and methods from one of its classes.  You get everything.

(Tried it both in Debug and Release modes in VS and checked that I was using .NET 4.6).

I guess the one experiment I haven't tried is not using the reflection API to bring in the second assembly, but just using it directly in the main project.
Comment 4 Marek Safar 2015-10-20 07:40:44 UTC
It looks like it's somehow related to IVT attribute

https://github.com/dotnet/coreclr/blob/master/src/vm/assembly.cpp#L2161
Comment 5 Aleksey Kliger 2015-10-20 10:11:03 UTC
Thanks, that looks promising. I'll try to cook up a testcase.
Comment 6 Aleksey Kliger 2015-10-20 16:00:06 UTC
My mistake.  Just enumerating the names of class methods and members doesn't trigger access restrictions.  You have to actually try to call them.
Comment 7 Aleksey Kliger 2015-10-20 18:00:29 UTC
Created attachment 13466 [details]
DisablePrivateReflection example

Okay, I've got a working example.

Reproduction steps:

mcs dprExample.cs
mono dprExample.exe

Expected output:
{{ MethodAccessException thrown when trying to invoke hello_private }}

Actual output:
 - hello_public;
  attempting to invoke hello_public.
  invoked hello_public
 - hello_private;
  attempting to invoke hello_private.
  invoked hello_private

Note You need to log in before you can comment on or make changes to this bug.