Skip to content
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

[Xamarin.Android.Build.Tasks] <GenerateJavaStubs/> should open readonly in some cases #8129

Merged

Conversation

jonathanpeppers
Copy link
Member

I was investigating a failure building Android projects in Release mode inside VS Mac:

Task "GenerateJavaStubs"
    Failed to read '/usr/local/share/dotnet/packs/Microsoft.NETCore.App.Runtime.Mono.android-arm/8.0.0-preview.5.23280.8/runtimes/android-arm/lib/net8.0/Microsoft.CSharp.dll' with debugging symbols. Retrying to load it without it. Error details are logged below.
    System.UnauthorizedAccessException: Access to the path '/usr/local/share/dotnet/packs/Microsoft.NETCore.App.Runtime.Mono.android-arm/8.0.0-preview.5.23280.8/runtimes/android-arm/lib/net8.0/Microsoft.CSharp.dll' is denied.
    ---> System.IO.IOException: Permission denied
    --- End of inner exception stack trace ---
    at Interop.ThrowExceptionForIoErrno(ErrorInfo errorInfo, String path, Boolean isDirError)
    at Interop.CheckIo(Error error, String path, Boolean isDirError)
    at Microsoft.Win32.SafeHandles.SafeFileHandle.Open(String fullPath, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize, UnixFileMode openPermissions, Int64& fileLength, UnixFileMode& filePermissions, Func`4 createOpenException)
    at System.IO.Strategies.OSFileStreamStrategy..ctor(String path, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize, Nullable`1 unixCreateMode)
    at Mono.Cecil.ModuleDefinition.ReadModule(String fileName, ReaderParameters parameters) in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/external/cecil/Mono.Cecil/ModuleDefinition.cs:line 1103
    at Java.Interop.Tools.Cecil.DirectoryAssemblyResolver.LoadFromMemoryMappedFile(String file, ReaderParameters options) in /Users/runner/work/1/s/xamarin-android/external/Java.Interop/src/Java.Interop.Tools.Cecil/Java.Interop.Tools.Cecil/DirectoryAssemblyResolver.cs:line 184
    at Java.Interop.Tools.Cecil.DirectoryAssemblyResolver.ReadAssembly(String file) in /Users/runner/work/1/s/xamarin-android/external/Java.Interop/src/Java.Interop.Tools.Cecil/Java.Interop.Tools.Cecil/DirectoryAssemblyResolver.cs:line 169
    /usr/local/share/dotnet/packs/Microsoft.Android.Sdk.Darwin/34.0.0-preview.5.312/tools/Xamarin.Android.Common.targets(1513,3): error XA0009: Error while loading assembly: '/usr/local/share/dotnet/packs/Microsoft.NETCore.App.Runtime.Mono.android-arm/8.0.0-preview.5.23280.8/runtimes/android-arm/lib/net8.0/Microsoft.CSharp.dll'.
Done executing task "GenerateJavaStubs" -- FAILED.

I noticed DirectoryAssemblyResolver.LoadFromMemoryMappedFile() was not opening the file in read-only mode for Debug builds.

In 8bc7a3e, we added "marshal methods" support that unconditionally enables ReaderParameters.ReadWrite.

We should be able to toggle these settings only when "marshal methods" is enabled:

var readerParams = new ReaderParameters();
if (useMarshalMethods) {
    readerParams.ReadWrite = true;
    readerParams.InMemory = true;
}

This should improve performance for Debug builds, and avoid opening files in read/write mode.

…ly in some cases

I was investigating a failure building Android projects in `Release`
mode inside VS Mac:

    Task "GenerateJavaStubs"
        Failed to read '/usr/local/share/dotnet/packs/Microsoft.NETCore.App.Runtime.Mono.android-arm/8.0.0-preview.5.23280.8/runtimes/android-arm/lib/net8.0/Microsoft.CSharp.dll' with debugging symbols. Retrying to load it without it. Error details are logged below.
        System.UnauthorizedAccessException: Access to the path '/usr/local/share/dotnet/packs/Microsoft.NETCore.App.Runtime.Mono.android-arm/8.0.0-preview.5.23280.8/runtimes/android-arm/lib/net8.0/Microsoft.CSharp.dll' is denied.
        ---> System.IO.IOException: Permission denied
        --- End of inner exception stack trace ---
        at Interop.ThrowExceptionForIoErrno(ErrorInfo errorInfo, String path, Boolean isDirError)
        at Interop.CheckIo(Error error, String path, Boolean isDirError)
        at Microsoft.Win32.SafeHandles.SafeFileHandle.Open(String fullPath, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize, UnixFileMode openPermissions, Int64& fileLength, UnixFileMode& filePermissions, Func`4 createOpenException)
        at System.IO.Strategies.OSFileStreamStrategy..ctor(String path, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize, Nullable`1 unixCreateMode)
        at Mono.Cecil.ModuleDefinition.ReadModule(String fileName, ReaderParameters parameters) in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/external/cecil/Mono.Cecil/ModuleDefinition.cs:line 1103
        at Java.Interop.Tools.Cecil.DirectoryAssemblyResolver.LoadFromMemoryMappedFile(String file, ReaderParameters options) in /Users/runner/work/1/s/xamarin-android/external/Java.Interop/src/Java.Interop.Tools.Cecil/Java.Interop.Tools.Cecil/DirectoryAssemblyResolver.cs:line 184
        at Java.Interop.Tools.Cecil.DirectoryAssemblyResolver.ReadAssembly(String file) in /Users/runner/work/1/s/xamarin-android/external/Java.Interop/src/Java.Interop.Tools.Cecil/Java.Interop.Tools.Cecil/DirectoryAssemblyResolver.cs:line 169
        /usr/local/share/dotnet/packs/Microsoft.Android.Sdk.Darwin/34.0.0-preview.5.312/tools/Xamarin.Android.Common.targets(1513,3): error XA0009: Error while loading assembly: '/usr/local/share/dotnet/packs/Microsoft.NETCore.App.Runtime.Mono.android-arm/8.0.0-preview.5.23280.8/runtimes/android-arm/lib/net8.0/Microsoft.CSharp.dll'.
    Done executing task "GenerateJavaStubs" -- FAILED.

I noticed `DirectoryAssemblyResolver.LoadFromMemoryMappedFile()` was
not opening the file in read-only mode for `Debug` builds.

In 8bc7a3e, we added "marshal methods" support that unconditionally
enables `ReaderParameters.ReadWrite`.

We should be able to toggle these settings only when "marshal methods"
is enabled:

    var readerParams = new ReaderParameters();
    if (useMarshalMethods) {
        readerParams.ReadWrite = true;
        readerParams.InMemory = true;
    }

This should improve performance for `Debug` builds, and avoid opening
files in read/write mode.
@jonathanpeppers jonathanpeppers marked this pull request as ready for review June 14, 2023 19:05
@jonathanpeppers jonathanpeppers merged commit df64039 into dotnet:main Jun 14, 2023
@jonathanpeppers jonathanpeppers deleted the GenerateJavaStubsReaderParameters branch June 14, 2023 21:23
grendello added a commit to grendello/xamarin-android that referenced this pull request Jun 15, 2023
* main:
  [Xamarin.Android.Build.Tasks] <GenerateJavaStubs/> should open readonly in some cases (dotnet#8129)
  [Mono.Android] suppress/solve more illink warnings (dotnet#8063)
@github-actions github-actions bot locked and limited conversation to collaborators Jan 22, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants