couchbase / couchbase-lite-net

A lightweight, document-oriented (NoSQL), syncable database engine for .NET
http://developer.couchbase.com/mobile/
Apache License 2.0
437 stars 126 forks source link

AccessViolationException in UWP app #943

Closed rainerg2000 closed 6 years ago

rainerg2000 commented 6 years ago

I am seeing an intermittent AccessViolationExceptions in our Couchbase Lite test app. It's a UWP app running on Win 10.0.16299, using CB Lite.net DB021, it uses CB Lite encryption.

@borrrden, here's a test program that reproduces the issue https://drive.google.com/file/d/1pn-QWss3ORU9eW2erebjZhknRKEBUieX/view?usp=sharing To reproduce the issue you have to run the test case several times by clicking the 'Run Tests' button in the app.

The exception occurs when opening the DB: mDB = new Couchbase.Lite.Database(caboDbConfig.DbName, couchbaseDbConfig);

stack trace:

$exception  {System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
   at LiteCore.Interop.NativeRaw.c4slog(C4LogDomain* domain, C4LogLevel level, C4Slice msg)
   at LiteCore.Interop.Native.c4slog(C4LogDomain* domain, C4LogLevel level, String msg)
   at Couchbase.Lite.Database.Open()
   at Couchbase.Lite.Database..ctor(String name, DatabaseConfiguration configuration)
   at XXXXXXXXX.YYYY.Business.Database.YYYYDatabaseCommon.OpenOrCreateDatabase(String dbPath, YYYYDbConfig YYYYDbConfig)
   at XXXXXXXXX.YYYY.Business.Database.YYYYDatabaseCommon.Initialize()
   at XXXXXXXXX.YYYY.Shared.Container.ContainerBuilderExtensions.<>c__1`2.<RegisterTypeWithInitializer>b__1_0(TImplementer o, IComponentContext c)
   at XXXXXXXXX.YYYY.Shared.Container.AutofacContainerBuilder.<>c__DisplayClass6_0`2.<RegisterType>b__0(IActivatingEventArgs`1 e)
   at Autofac.Builder.RegistrationBuilder`3.<>c__DisplayClass30_0.<OnActivating>b__0(Object s, ActivatingEventArgs`1 e)
   at System.EventHandler`1.Invoke(Object sender, TEventArgs e)
   at Autofac.Core.Registration.ComponentRegistration.RaiseActivating(IComponentContext context, IEnumerable`1 parameters, Object& instance)
   at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable`1 parameters)
   at Autofac.Core.Resolving.InstanceLookup.<Execute>b__5_0()
   at Autofac.Core.Lifetime.LifetimeScope.GetOrCreateAndShare(Guid id, Func`1 creator)
   at Autofac.Core.Resolving.InstanceLookup.Execute()
   at Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, IComponentRegistration registration, IEnumerable`1 parameters)
   at Autofac.Core.Resolving.ResolveOperation.Execute(IComponentRegistration registration, IEnumerable`1 parameters)
   at Autofac.Core.Lifetime.LifetimeScope.ResolveComponent(IComponentRegistration registration, IEnumerable`1 parameters)
   at Autofac.Core.Container.ResolveComponent(IComponentRegistration registration, IEnumerable`1 parameters)
   at Autofac.ResolutionExtensions.TryResolveService(IComponentContext context, Service service, IEnumerable`1 parameters, Object& instance)
   at Autofac.ResolutionExtensions.ResolveService(IComponentContext context, Service service, IEnumerable`1 parameters)
   at Autofac.ResolutionExtensions.Resolve[TService](IComponentContext context, IEnumerable`1 parameters)
   at Autofac.ResolutionExtensions.Resolve[TService](IComponentContext context)
   at XXXXXXXXX.YYYY.Shared.Container.AutofacComponentContext.Resolve[TService]()
   at XXXXXXXXX.YYYY.Tests.Business.Database.DatabaseTestCases.TestInit()}  System.AccessViolationException
borrrden commented 6 years ago

I'm almost certain that this is a manifestation of #936

I'll run that test suite a few times to be sure.

borrrden commented 6 years ago

That being said, it looks like that fix was in DB021 so...boooo

rainerg2000 commented 6 years ago

If it doesn't reproduce when clicking the 'Run Tests' button repeatedly, you should also click the 'Failed Tests' button between test runs.

borrrden commented 6 years ago

I'm having trouble getting this round of tests to run properly. System.Reactive was missing and so I installed that to get it to build, but now the runtime error is "unable to find Serilog." I installed Serilog from Nuget but that did not resolve things. Perhaps I need some other nuget endpoints (the ones that are included with the project are, understandly, not usable)

rainerg2000 commented 6 years ago

All 3rd party nuget packages are available on nuget.org. Please tweak the solution's nuget.config, or your Visual Studio nuget options to include https://api.nuget.org/v3/index.json http://mobile.nuget.couchbase.com/nuget/Developer/ It compiles, runs, and reproduces the issue. I don't think it has any additional dependencies.

rainerg2000 commented 6 years ago

Here's a potential issue you might be running into: If your test target has TPM2 my test app will crash on startup. The issue is in Shared\Uwp\Security\CryptoUwp.cs. Here's the fix:

public override void Initialize()
    {
      mAliases = new List<string>();

      var device = new TbsDevice();

      try
      {
        device.Connect();
        mTpm = new Tpm2(device);
      }
      catch (Exception e)
      {
        // getting here probably means that the PC doesn't have TPM 2.0 hardware.
        Logger.Error(e, "Failed to interface with TPM on the device. Probably means the PC doesn't have TPM 2.0 hardware.");
        mFallbackToMockImplementation = true;
        mRandom = new Random();
      }

      // we are using the mock implementation even if TPM2 h/w is available on the system.
      // later we'll implement a real implemantation using the TPM2 h/w.
      mFallbackToMockImplementation = true;
      mRandom = new Random();
    }
borrrden commented 6 years ago

It wasn’t crashing and I still haven’t gotten it running but I found a potential issue related to this. I didn’t file a separate ticket about it but I noticed other platforms throwing exceptions about illegal byte sequences coming out of this method. That seems to me to be another symptom of the same problem (invalid memory access that happens to be on a valid memory page instead of an invalid one). I am hopeful that it will take care of this too. The fix will be in db022 tomorrow.

rainerg2000 commented 6 years ago

I'd be happy to test with the upcoming DB022

borrrden commented 6 years ago

I’m happy to give it to you too. I thought I would be able to put it out by now but we are in beta mode and so things are getting sucked into the QE process.

rainerg2000 commented 6 years ago

Using db022 I have confirmed this issue is fixed. Thanks.