dotnet / android

.NET for Android provides open-source bindings of the Android SDK for use with .NET managed languages such as C#
MIT License
1.92k stars 526 forks source link

Creating a MemoryMappedFile with fixed capacity throws ArgumentException, fatal crash during attempt to write #5423

Closed NightOwl888 closed 4 months ago

NightOwl888 commented 3 years ago

Steps to Reproduce

Run the following NUnit tests on Xamarin.Android using Visual Studio 2019 on Windows with the Android emulator.

Note the tests are in a .NET Standard 2.0 library that is referenced by a Xamarin.Android project. I tried a couple of different emulator configurations x86 and x86_64 with similar results. All other tests are running, most passing.

        [Test]
        public void TestMemoryMappedXamarin()
        {
            var tmp = new FileInfo(Path.GetTempFileName());
            try
            {
                MemoryMappedViewAccessor accessor;

                using (var fileStream = new FileStream(tmp.FullName, FileMode.Open))
                // Throws ArgumentException on capacity parameter (with no message)
                using (var f = MemoryMappedFile.CreateFromFile(fileStream, mapName: null, capacity: 100L, 
                    access: MemoryMappedFileAccess.ReadWrite,
                    HandleInheritability.None, leaveOpen: false))
                {
                    accessor = f.CreateViewAccessor(offset: 0L, size: 100L, access: MemoryMappedFileAccess.ReadWrite);
                }

                Store(accessor, 1, 1);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }

        [Test]
        public void TestMemoryMappedXamarin2()
        {
            var tmp = new FileInfo(Path.GetTempFileName());
            try
            {
                MemoryMappedViewAccessor accessor;

                using (var fileStream = new FileStream(tmp.FullName, FileMode.Open))
                // Capacity 0 doesn't throw, but then the MemoryMappedFile is the wrong size
                using (var f = MemoryMappedFile.CreateFromFile(fileStream, mapName: null, capacity: 0L, 
                    access: MemoryMappedFileAccess.ReadWrite,
                    HandleInheritability.None, leaveOpen: false))
                {
                    accessor = f.CreateViewAccessor(0L, 100L, MemoryMappedFileAccess.ReadWrite);
                }

                // Fatal crash when trying to write to the accessor
                Store(accessor, 1, 1);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }

        [Test]
        public void TestMemoryMappedXamarin3()
        {
            var tmp = new FileInfo(Path.GetTempFileName());
            try
            {
                MemoryMappedViewAccessor accessor;

                // Setting the length of the FileStream and capacity to 0 should cause the MemoryMappedFile to inherit 
                // the size of the stream.
                // In theory, this should be the same behavior as TestMemoryMappedXamarin() with alternate syntax,
                // but it makes no difference in terms of writing to the view accessor.
                using (var fileStream = new FileStream(path: tmp.FullName, mode: FileMode.Open, 
                    access: FileAccess.ReadWrite, share: FileShare.Read, bufferSize: 100))
                using (var f = MemoryMappedFile.CreateFromFile(fileStream, mapName: null, capacity: 0L, 
                    access: MemoryMappedFileAccess.ReadWrite,
                    HandleInheritability.None, leaveOpen: false))
                {
                    accessor = f.CreateViewAccessor(0L, 100L, MemoryMappedFileAccess.ReadWrite);
                }

                // Fatal crash when trying to write to the accessor
                Store(accessor, 1, 1);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }

        private void Store(MemoryMappedViewAccessor accessor, int index, int value)
        {
            int baseOffset = index;

            // Write big endian int32
            for (int i = 3; i >= 0; i--)
            {
                accessor.Write(baseOffset + i, (byte)(value & 0xFF));
                value >>= 8;
            }
        }

Expected Behavior

  1. When calling MemoryMappedFile.CreateFromFile(), it should be possible to set a fixed capacity.
  2. Writing to a writable view accessor should either succeed or throw a managed exception.

Actual Behavior

  1. When creating a MemoryMappedFile with a fixed capacity, an ArgumentException is thrown. There is no message in the exception, so it is not clear why the exception is being thrown, only that it refers to the capacity parameter. On .NET Framework 4.8, .NET Core 3.1, and .NET 5.0 there is no exception in this case.
  2. When writing to an open view accesor, a fatal crash occurs. This exact same code works on .NET Framework 4.8, .NET Core 3.1, and .NET 5.0 when running as either x86 or x64.

Version Information

Click to expand Microsoft Visual Studio Community 2019 Version 16.8.2 VisualStudio.16.Release/16.8.2+30717.126 Microsoft .NET Framework Version 4.8.04084 Installed Version: Community Visual C++ 2019 00435-60000-00000-AA990 Microsoft Visual C++ 2019 ADL Tools Service Provider 1.0 This package contains services used by Data Lake tools ASA Service Provider 1.0 ASP.NET and Web Tools 2019 16.8.553.28003 ASP.NET and Web Tools 2019 ASP.NET Core Razor Language Services 16.1.0.2052803+84e121f1403378489b842e1797df2f3f5a49ac3c Provides languages services for ASP.NET Core Razor. ASP.NET Web Frameworks and Tools 2019 16.8.553.28003 For additional information, visit https://www.asp.net/ Azure App Service Tools v3.0.0 16.8.553.28003 Azure App Service Tools v3.0.0 Azure Data Lake Node 1.0 This package contains the Data Lake integration nodes for Server Explorer. Azure Data Lake Tools for Visual Studio 2.6.2000.0 Microsoft Azure Data Lake Tools for Visual Studio Azure DevOps Status Monitor 1.4 Monitor status of Azure DevOps Service inside Visual Studio Azure Functions and Web Jobs Tools 16.8.553.28003 Azure Functions and Web Jobs Tools Azure Logic Apps Tools for Visual Studio 1.0 Add-in for the Azure Resource Group project to support the Logic App Designer and template creation. Azure Stream Analytics Tools for Visual Studio 2.6.2000.0 Microsoft Azure Stream Analytics Tools for Visual Studio Bundler & Minifier 2.8.396 Adds support for bundling and minifying JavaScript, CSS and HTML files in any project. C# Tools 3.8.0-5.20567.16+53c5d7d3cf13d88978744a32a27c5f8350a8400a C# components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used. Common Azure Tools 1.10 Provides common services for use by Azure Mobile Services and Microsoft Azure Tools. ConfigurationTransform 1.3 Automatically transform app.config during build process. Once the transformation is set, it will run on other build machines without the extension. Cookiecutter 16.8.20241.2 Provides tools for finding, instantiating and customizing templates in cookiecutter format. CreateUnitTestBoilerplateCommand Extension 1.0 CreateUnitTestBoilerplateCommand Visual Studio Extension Detailed Info Extensibility Message Bus 1.2.6 (master@34d6af2) Provides common messaging-based MEF services for loosely coupled Visual Studio extension components communication and integration. Fabric.DiagnosticEvents 1.0 Fabric Diagnostic Events File Icons 2.7 Adds icons for files that are not recognized by Solution Explorer Git Tools 2019 3.1.2 This extension provides a git changes window, and menus to launch Git Bash, Git Extenstions and TortoiseGit. GitExtensions 1.0 Git Extensions is a graphical user interface for Git that allows you to control Git without using the command-line Image Optimizer 4.0.132 Uses industry standard tools to optimize any JPEG, PNG and Gifs - including animated Gifs. Can do both lossy and lossless optimization. IntelliCode Extension 1.0 IntelliCode Visual Studio Extension Detailed Info JetBrains ReSharper Ultimate 2020.1.3 Build 201.0.20200515.122017 JetBrains ReSharper Ultimate package for Microsoft Visual Studio. For more information about ReSharper Ultimate, visit http://www.jetbrains.com/resharper. Copyright © 2020 JetBrains, Inc. Microsoft Azure HDInsight Azure Node 2.6.2000.0 HDInsight Node under Azure Node Microsoft Azure Hive Query Language Service 2.6.2000.0 Language service for Hive query Microsoft Azure Service Fabric Tools for Visual Studio 16.0 Microsoft Azure Service Fabric Tools for Visual Studio Microsoft Azure Stream Analytics Language Service 2.6.2000.0 Language service for Azure Stream Analytics Microsoft Azure Stream Analytics Node 1.0 Azure Stream Analytics Node under Azure Node Microsoft Azure Tools 2.9 Microsoft Azure Tools for Microsoft Visual Studio 2019 - v2.9.30924.1 Microsoft Continuous Delivery Tools for Visual Studio 0.4 Simplifying the configuration of Azure DevOps pipelines from within the Visual Studio IDE. Microsoft JVM Debugger 1.0 Provides support for connecting the Visual Studio debugger to JDWP compatible Java Virtual Machines Microsoft Library Manager 2.1.113+g422d40002e.RR Install client-side libraries easily to any web project Microsoft MI-Based Debugger 1.0 Provides support for connecting Visual Studio to MI compatible debuggers Microsoft Visual C++ Wizards 1.0 Microsoft Visual C++ Wizards Microsoft Visual Studio Tools for Containers 1.1 Develop, run, validate your ASP.NET Core applications in the target environment. F5 your application directly into a container with debugging, or CTRL + F5 to edit & refresh your app without having to rebuild the container. Microsoft Visual Studio VC Package 1.0 Microsoft Visual Studio VC Package Mono Debugging for Visual Studio 16.8.43 (00471f8) Support for debugging Mono processes with Visual Studio. Node.js Tools 1.5.20902.1 Commit Hash:b474efcb6f92db52a8f8e2e6a8cb9648476885cc Adds support for developing and debugging Node.js apps in Visual Studio NuGet Package Manager 5.8.0 NuGet Package Manager in Visual Studio. For more information about NuGet, visit https://docs.nuget.org/ Package Installer 2.0.103 Makes it easier, faster and more convenient than ever to install Bower, npm, Yarn, JSPM, TSD, Typings and NuGet packages to any project Package Load Explorer 1.0.999 Shows information about how VS packages are loaded in Visual Studio. PowerShell Pro Tools for Visual Studio 1.0 A set of tools for developing and debugging PowerShell scripts and modules in Visual Studio. ProjectServicesPackage Extension 1.0 ProjectServicesPackage Visual Studio Extension Detailed Info Python 16.8.20241.2 Provides IntelliSense, projects, templates, debugging, interactive windows, and other support for Python developers. Python - Conda support 16.8.20241.2 Conda support for Python projects. Python - Django support 16.8.20241.2 Provides templates and integration for the Django web framework. Python - IronPython support 16.8.20241.2 Provides templates and integration for IronPython-based projects. Python - Profiling support 16.8.20241.2 Profiling support for Python projects. SQL Server Data Tools 16.0.62010.06180 Microsoft SQL Server Data Tools Syntax Visualizer 1.0 An extension for visualizing Roslyn SyntaxTrees. Test Adapter for Boost.Test 1.0 Enables Visual Studio's testing tools with unit tests written for Boost.Test. The use terms and Third Party Notices are available in the extension installation directory. Test Adapter for Google Test 1.0 Enables Visual Studio's testing tools with unit tests written for Google Test. The use terms and Third Party Notices are available in the extension installation directory. ToolWindowHostedEditor 1.0 Hosting json editor into a tool window TypeScript Tools 16.0.21016.2001 TypeScript Tools for Microsoft Visual Studio Visual Basic Tools 3.8.0-5.20567.16+53c5d7d3cf13d88978744a32a27c5f8350a8400a Visual Basic components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used. Visual F# Tools 16.8.0-beta.20507.4+da6be68280c89131cdba2045525b80890401defd Microsoft Visual F# Tools Visual Studio Code Debug Adapter Host Package 1.0 Interop layer for hosting Visual Studio Code debug adapters in Visual Studio Visual Studio Container Tools Extensions 1.0 View, manage, and diagnose containers within Visual Studio. Visual Studio Tools for CMake 1.0 Visual Studio Tools for CMake Visual Studio Tools for Containers 1.0 Visual Studio Tools for Containers Visual Studio Tools for Kubernetes 1.0 Visual Studio Tools for Kubernetes Visual Studio Tools for Unity 4.8.2.0 Visual Studio Tools for Unity VisualStudio.DeviceLog 1.0 Information about my package VisualStudio.Mac 1.0 Mac Extension for Visual Studio Xamarin 16.8.000.255 (d16-8@d002176) Visual Studio extension to enable development for Xamarin.iOS and Xamarin.Android. Xamarin Designer 16.8.0.507 (remotes/origin/d16-8@e87b24884) Visual Studio extension to enable Xamarin Designer tools in Visual Studio. Xamarin Templates 16.8.112 (86385a3) Templates for building iOS, Android, and Windows apps with Xamarin and Xamarin.Forms. Xamarin.Android SDK 11.1.0.17 (d16-8/c0e2b8e) Xamarin.Android Reference Assemblies and MSBuild support. Mono: be2226b Java.Interop: xamarin/java.interop/d16-8@79d9533 ProGuard: Guardsquare/proguard/proguard6.2.2@ebe9000 SQLite: xamarin/sqlite/3.32.1@1a3276b Xamarin.Android Tools: xamarin/xamarin-android-tools/d16-8@2fb1cbc Xamarin.iOS and Xamarin.Mac SDK 14.4.1.3 (e30c41de3) Xamarin.iOS and Xamarin.Mac Reference Assemblies and MSBuild support.

Log File

No log available via adb command, but here is the output from the debug session when calling TestMemoryMappedXamarin3():

crash.txt

jpobst commented 3 years ago

@grendello Is this a Mono thing? If so, can you please route it to the correct place/people?

grendello commented 3 years ago

This appears to be a segfault:

=================================================================
    Native Crash Reporting
=================================================================
Got a SEGV while executing native code. This usually indicates
a fatal error in the mono runtime or one of the native libraries 
used by your application.
=================================================================

No native Android stacktrace (see debuggerd output).

=================================================================
    Basic Fault Address Reporting
=================================================================
Memory around native instruction pointer (0xcaf97872):0xcaf97862  03 c1 8b 4d 0c 8b 55 10 8b d1 03 c1 0f b6 4d 14  ...M..U.......M.
0xcaf97872  88 08 8b 4d 
f4 8d 64 24 00 90 90 90 c7 45 e8 00  ...M..d$.....E..
0xcaf97882  00 00 00 83 ec 0c e8 14 00 00 00 83 c4 0c 83 7d  ...............}
0xcaf97892  e8 00 74 06 90 e8 58 d6 85 1c e9 69 00 00 00 89  ..t...X....i....

=================================================================
    Managed Stacktrace:
=================================================================
      at System.IO.UnmanagedMemoryAccessor:Write <0x000e2>
      at J2N.IO.MemoryMappedFiles.TestMemoryMappedViewByteBuffer:Store <0x000bf>
      at J2N.IO.MemoryMappedFiles.TestMemoryMappedViewByteBuffer:TestMemoryMappedXamarin3 <0x0033f>
      at System.Object:runtime_invoke_void__this__ <0x00104>
      at <unknown> <0xffffffff>
      at System.Reflection.RuntimeMethodInfo:InternalInvoke <0x00012>
      at System.Reflection.RuntimeMethodInfo:Invoke <0x0033f>
      at System.Reflection.MethodBase:Invoke <0x0007b>
      at NUnit.Framework.Internal.Reflect:InvokeMethod <0x000ab>
      at NUnit.Framework.Internal.MethodWrapper:Invoke <0x00067>
      at NUnit.Framework.Internal.Commands.
TestMethodCommand:RunNonAsyncTestMethod <0x000b1>
      at NUnit.Framework.Internal.Commands.TestMethodCommand:RunTestMethod <0x000f3>
      at NUnit.Framework.Internal.Commands.TestMethodCommand:Execute <0x0004b>
      at NUnit.Framework.Internal.Commands.BeforeAndAfterTestCommand:Execute <0x001d5>
      at NUnit.Framework.Internal.Execution.SimpleWorkItem:PerformWork <0x0008a>
      at NUnit.Framework.Internal.Execution.WorkItem:RunOnCurrentThread <0x002d3>
      at NUnit.Framework.Internal.Execution.WorkItem:Execute <0x00377>
      at NUnit.Framework.Internal.Execution.ParallelWorkItemDispatcher:Dispatch <0x00182>
      at NUnit.Framework.Internal.Execution.ParallelWorkItemDispatcher:Dispatch <0x00077>
      at NUnit.Framework.Internal.Execution.CompositeWorkItem:RunChildren <0x00536>
      at NUnit.Framework.Internal.Execution.CompositeWorkItem:PerformWork <0x0036b>
      at NUnit.Framework.Internal.Execution.WorkItem:RunOnCurrentThread <0x002d3>
      at NUnit.Framework.Internal.Execution.WorkItem:Execute <0x00377>
      at NUnit.Framework.In
ternal.Execution.TestWorker:TestWorkerThreadProc <0x002eb>
      at System.Threading.ThreadHelper:ThreadStart_Context <0x000de>
      at System.Threading.ExecutionContext:RunInternal <0x0035f>
      at System.Threading.ExecutionContext:Run <0x0005b>
      at System.Threading.ExecutionContext:Run <0x00147>
      at System.Threading.ThreadHelper:ThreadStart <0x0007f>
      at System.Object:runtime_invoke_void__this__ <0x00104>
=================================================================
12-15 12:16:52.603 F/libc    ( 8123): Fatal signal 7 (SIGBUS), code 2, fault addr 0xf00af004 in tid 8158 (NonParallelWork), pid 8123 (N.Tests.Android)

Considering the last frame, it's most likely a Mono runtime error (UnmanagedMemoryAccessor comes from reference sources but it uses runtime helpers in Mono)

@NightOwl888 would you mind recording and attaching a full bug report on this crash? To do so, run the tests and when they crash, run adb bugreport - it should give the Mono team more info on the crash (hopefully), thanks!

NightOwl888 commented 3 years ago

@NightOwl888 would you mind recording and attaching a full bug report on this crash? To do so, run the tests and when they crash, run adb bugreport - it should give the Mono team more info on the crash (hopefully), thanks!

I did a factory reset on the emulator, then ran TestMemoryMappedXamarin3() and then adb bugreport after it crashed. Attached is the result.

Let me know if you require anything else. Thanks.

bugreport-sdk_gphone_x86_64-QSR1.200715.002-2021-01-10-22-14-10.zip

jpobst commented 4 months ago

With support for Classic Xamarin.Android ended May 1st, 2024, this issue is likely no longer relevant.

If this still persists in .NET 8+, please open a new issue with updated information based on net8.0-android or greater. Please include a link to this issue for context.