Skip to content

Sharing violation on assembly after calling AssemblyDefinition.ReadAssembly #291

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
lewurm opened this issue Sep 21, 2016 · 1 comment
Closed

Comments

@lewurm
Copy link

lewurm commented Sep 21, 2016

With bumping Cecil to a more recent version (from a version from 2013 or something), we run into a problem on xamarin-android, where we get

Sharing violation on path /Users/bernhardu/work/benchmarker/tools/AndroidAgent/obj/Release/android/assets/shrunk/Mono.Android.dll

The problem starts here: https://github.com/xamarin/xamarin-android/blob/59ec488b4005a09fc0e5f330f217e78c3fa14724/src/Xamarin.Android.Build.Tasks/Tasks/RemoveRegisterAttribute.cs#L27 (we call AssemblyDefinition.ReadAssembly ()). Afterwards we do some other file operations on that same assembly. I see that the process still has a filedescriptor on the assembly in question open and thus fails with the error above. I looked a bit into the code, and I think https://github.com/mono/cecil/blob/505b07d6974d8405a63124139733c6fdc0e67bc7/Mono.Cecil.PE/ImageReader.cs#L29 needs to call Disposable () for its Stream member eventually. Here is a test case that highlights the problem:

using System;
using System.IO;
using Mono.Cecil;

namespace Cecil.Samples {
        public class MainClass {
                private void RunSample () {
                        var dataPath = Path.Combine (Path.GetDirectoryName (GetType ().Assembly.Location) ,"Mono.Cecil.dll");
                        Console.WriteLine ("datapath: {0}" ,dataPath);
                        if (!File.Exists (dataPath)) {
                                Console.Error.WriteLine ("need a valid file");
                                return;
                        }
                        var r = ModuleDefinition.ReadModule (dataPath);
                        FileStream f = File.Open (dataPath ,FileMode.Open);
                        Console.WriteLine ("do something with r: {0}" ,r);
                        Console.WriteLine ("do something with f: {0}" ,f);
                }

                public static void Main (string[] args) {
                        new MainClass ().RunSample ();
                }
        }
}
$ mono sharedviolation/bin/Debug/sharedviolation.exe
datapath: /Users/bernhardu/work/cecil-repro/cecil.samples/sharedviolation/bin/Debug/Mono.Cecil.dll

Unhandled Exception:
System.IO.IOException: Sharing violation on path /Users/bernhardu/work/cecil-repro/cecil.samples/sharedviolation/bin/Debug/Mono.Cecil.dll
  at System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize, System.Boolean anonymous, System.IO.FileOptions options) [0x0025f] in <269653efc2ae4ad59c04a426f96119ad>:0
  at System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share) [0x00000] in <269653efc2ae4ad59c04a426f96119ad>:0
  at (wrapper remoting-invoke-with-check) System.IO.FileStream:.ctor (string,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare)
  at System.IO.File.Open (System.String path, System.IO.FileMode mode) [0x00000] in <269653efc2ae4ad59c04a426f96119ad>:0
  at Cecil.Samples.MainClass.RunSample () [0x00053] in <fbfd0a81d94a4268a2289d89cc81ba19>:0
  at Cecil.Samples.MainClass.Main (System.String[] args) [0x00006] in <fbfd0a81d94a4268a2289d89cc81ba19>:0
[ERROR] FATAL UNHANDLED EXCEPTION: System.IO.IOException: Sharing violation on path /Users/bernhardu/work/cecil-repro/cecil.samples/sharedviolation/bin/Debug/Mono.Cecil.dll
  at System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize, System.Boolean anonymous, System.IO.FileOptions options) [0x0025f] in <269653efc2ae4ad59c04a426f96119ad>:0
  at System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share) [0x00000] in <269653efc2ae4ad59c04a426f96119ad>:0
  at (wrapper remoting-invoke-with-check) System.IO.FileStream:.ctor (string,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare)
  at System.IO.File.Open (System.String path, System.IO.FileMode mode) [0x00000] in <269653efc2ae4ad59c04a426f96119ad>:0
  at Cecil.Samples.MainClass.RunSample () [0x00053] in <fbfd0a81d94a4268a2289d89cc81ba19>:0
  at Cecil.Samples.MainClass.Main (System.String[] args) [0x00006] in <fbfd0a81d94a4268a2289d89cc81ba19>:0
@jbevain
Copy link
Owner

jbevain commented Sep 21, 2016

Please see the changelog for the 0.10 update which document breaking changes and how to handle them.

Your code which looks like:

            var assembly = AssemblyDefinition.ReadAssembly (mono_android);

            // Strip out [Register] attributes
            foreach (TypeDefinition type in assembly.MainModule.Types)
                ProcessType (type);

assembly.Write (mono_android);

Needs to be updated to read:

            using (var assembly = AssemblyDefinition.ReadAssembly (mono_android, new ReaderParameters { ReadWrite = true }))
            {
                // Strip out [Register] attributes
                foreach (TypeDefinition type in assembly.MainModule.Types)
                    ProcessType (type);

                assembly.Write ();
            }

@jbevain jbevain closed this as completed Sep 21, 2016
lewurm added a commit to lewurm/xamarin-android that referenced this issue Sep 21, 2016
… assembly

see jbevain/cecil#291

this fixes one of the `Sharing violation` issues, probably also #44529
lewurm added a commit to lewurm/xamarin-android that referenced this issue Sep 21, 2016
… assembly

see jbevain/cecil#291

this fixes one of the `Sharing violation` issues, probably also #44529
lewurm added a commit to lewurm/xamarin-android that referenced this issue Sep 22, 2016
… assembly

see jbevain/cecil#291

this fixes one of the `Sharing violation` issues, probably also #44529
lewurm added a commit to lewurm/xamarin-android that referenced this issue Sep 22, 2016
… assembly

see jbevain/cecil#291

this fixes one of the `Sharing violation` issues, probably also #44529
lewurm added a commit to lewurm/xamarin-android that referenced this issue Sep 22, 2016
… assembly

see jbevain/cecil#291

this fixes one of the `Sharing violation` issues, probably also #44529
akoeplinger pushed a commit to akoeplinger/cecil that referenced this issue Apr 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants