Fix partition mapping bug


After installing a VABC OTA and device reboots, merge happens. Some OTA states are cached in memory during the merge process. When the 2nd OTA comes, update_engine will incorrectly think the 2nd OTA is also a VABC update, resulting in incorrectly mapped partitions. Then update_engine will extract data from OTA and write them to source partitions, causing verity error and bricking the device. This CL fixes the corruption bug by create a new instance of SnapshotManager for each new OTA. This prevents
us from reading stale data cached in SnapshotManager member variables.


Bug: 229708688
Bug: 236348265
Test: Apply VABC OTA, reboot, then apply VAB OTA
Change-Id: If4d1de8393ce0329b825c06e567b985e9d89680b
(cherry picked from commit c675fc0aaa6f9e4d88d040847435f2487ff9a2d3)
Merged-In: If4d1de8393ce0329b825c06e567b985e9d89680b
diff --git a/aosp/dynamic_partition_control_android.cc b/aosp/dynamic_partition_control_android.cc
index 27d1d54..334ccdc 100644
--- a/aosp/dynamic_partition_control_android.cc
+++ b/aosp/dynamic_partition_control_android.cc
@@ -98,7 +98,8 @@
 constexpr std::chrono::milliseconds kMapSnapshotTimeout{10000};
 
 DynamicPartitionControlAndroid::~DynamicPartitionControlAndroid() {
-  Cleanup();
+  UnmapAllPartitions();
+  metadata_device_.reset();
 }
 
 static FeatureFlag GetFeatureFlag(const char* enable_prop,
@@ -312,6 +313,12 @@
 void DynamicPartitionControlAndroid::Cleanup() {
   UnmapAllPartitions();
   metadata_device_.reset();
+  if (GetVirtualAbFeatureFlag().IsEnabled()) {
+    snapshot_ = SnapshotManager::New();
+  } else {
+    snapshot_ = SnapshotManagerStub::New();
+  }
+  CHECK(snapshot_ != nullptr) << "Cannot initialize SnapshotManager.";
 }
 
 bool DynamicPartitionControlAndroid::DeviceExists(const std::string& path) {