!2234 Fix snapshot bug

Merge pull request !2234 from dingwen/master
This commit is contained in:
openharmony_ci 2022-08-31 09:45:18 +00:00 committed by Gitee
commit ea511cab43
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
4 changed files with 23 additions and 21 deletions

View File

@ -27,14 +27,6 @@ void Space::AddRegion(Region *region)
IncreaseObjectSize(region->GetSize()); IncreaseObjectSize(region->GetSize());
} }
void Space::AddRegionToFront(Region *region)
{
LOG_ECMA_MEM(DEBUG) << "Add region:" << region << " to " << ToSpaceTypeName(spaceType_);
regionList_.AddNodeToFront(region);
IncreaseCommitted(region->GetCapacity());
IncreaseObjectSize(region->GetSize());
}
void Space::RemoveRegion(Region *region) void Space::RemoveRegion(Region *region)
{ {
LOG_ECMA_MEM(DEBUG) << "Remove region:" << region << " to " << ToSpaceTypeName(spaceType_); LOG_ECMA_MEM(DEBUG) << "Remove region:" << region << " to " << ToSpaceTypeName(spaceType_);

View File

@ -185,7 +185,6 @@ public:
inline void EnumerateRegionsWithRecord(const Callback &cb) const; inline void EnumerateRegionsWithRecord(const Callback &cb) const;
inline void AddRegion(Region *region); inline void AddRegion(Region *region);
inline void AddRegionToFront(Region *region);
inline void RemoveRegion(Region *region); inline void RemoveRegion(Region *region);
virtual void Initialize() {}; virtual void Initialize() {};

View File

@ -1122,6 +1122,7 @@ void SnapshotProcessor::DeserializeObjectExcludeString(uintptr_t oldSpaceBegin,
auto nonMovableSpace = heap->GetNonMovableSpace(); auto nonMovableSpace = heap->GetNonMovableSpace();
auto machineCodeSpace = heap->GetMachineCodeSpace(); auto machineCodeSpace = heap->GetMachineCodeSpace();
auto snapshotSpace = heap->GetSnapshotSpace(); auto snapshotSpace = heap->GetSnapshotSpace();
DeserializeSpaceObject(oldSpaceBegin, oldSpace, oldSpaceObjSize); DeserializeSpaceObject(oldSpaceBegin, oldSpace, oldSpaceObjSize);
DeserializeSpaceObject(nonMovableBegin, nonMovableSpace, nonMovableObjSize); DeserializeSpaceObject(nonMovableBegin, nonMovableSpace, nonMovableObjSize);
DeserializeSpaceObject(machineCodeBegin, machineCodeSpace, machineCodeObjSize); DeserializeSpaceObject(machineCodeBegin, machineCodeSpace, machineCodeObjSize);
@ -1144,35 +1145,38 @@ void SnapshotProcessor::DeserializeSpaceObject(uintptr_t beginAddr, Space* space
uint32_t regionIndex = *(reinterpret_cast<GCBitset *>(oldMarkGCBitsetAddr)->Words()); uint32_t regionIndex = *(reinterpret_cast<GCBitset *>(oldMarkGCBitsetAddr)->Words());
regionIndexMap_.emplace(regionIndex, region); regionIndexMap_.emplace(regionIndex, region);
size_t copyBytes = fileRegion->highWaterMark_ - fileRegion->packedData_.begin_; size_t liveObjectSize = 0;
if (space->GetSpaceType() == MemSpaceType::SNAPSHOT_SPACE) {
liveObjectSize = fileRegion->highWaterMark_ - fileRegion->packedData_.begin_;
} else {
liveObjectSize = fileRegion->AliveObject();
}
// Retrieve the data beginning address based on the serialized data format. // Retrieve the data beginning address based on the serialized data format.
uintptr_t copyFrom = oldMarkGCBitsetAddr + uintptr_t copyFrom = oldMarkGCBitsetAddr +
(fileRegion->packedData_.begin_ - ToUintPtr(fileRegion->packedData_.markGCBitset_)); (fileRegion->packedData_.begin_ - ToUintPtr(fileRegion->packedData_.markGCBitset_));
ASSERT(copyBytes <= region->end_ - region->packedData_.begin_); ASSERT(liveObjectSize <= region->end_ - region->packedData_.begin_);
if (memcpy_s(ToVoidPtr(region->packedData_.begin_), if (memcpy_s(ToVoidPtr(region->packedData_.begin_),
copyBytes, liveObjectSize,
ToVoidPtr(copyFrom), ToVoidPtr(copyFrom),
copyBytes) != EOK) { liveObjectSize) != EOK) {
LOG_FULL(FATAL) << "memcpy_s failed"; LOG_FULL(FATAL) << "memcpy_s failed";
UNREACHABLE(); UNREACHABLE();
} }
region->highWaterMark_ = region->packedData_.begin_ + copyBytes;
// Other information like aliveObject size, wasted size etc. in the region object to restore. // Other information like aliveObject size, wasted size etc. in the region object to restore.
region->aliveObject_ = fileRegion->AliveObject(); region->aliveObject_ = liveObjectSize;
region->wasted_ = fileRegion->wasted_; region->wasted_ = fileRegion->wasted_;
region->highWaterMark_ = region->packedData_.begin_ + liveObjectSize;
region->SetGCFlag(RegionGCFlags::NEED_RELOCATE); region->SetGCFlag(RegionGCFlags::NEED_RELOCATE);
size_t liveObjectSize = region->GetHighWaterMark() - region->GetBegin();
if (space->GetSpaceType() != MemSpaceType::SNAPSHOT_SPACE) { if (space->GetSpaceType() != MemSpaceType::SNAPSHOT_SPACE) {
auto sparseSpace = reinterpret_cast<SparseSpace *>(space); auto sparseSpace = reinterpret_cast<SparseSpace *>(space);
region->InitializeFreeObjectSets(); region->InitializeFreeObjectSets();
sparseSpace->FreeLiveRange(region, region->GetHighWaterMark(), region->GetEnd(), true); sparseSpace->FreeLiveRange(region, region->GetHighWaterMark(), region->GetEnd(), true);
sparseSpace->IncreaseLiveObjectSize(liveObjectSize); sparseSpace->IncreaseLiveObjectSize(liveObjectSize);
sparseSpace->IncreaseAllocatedSize(liveObjectSize); sparseSpace->IncreaseAllocatedSize(liveObjectSize);
sparseSpace->AddRegionToFront(region); sparseSpace->AddRegion(region);
} else { } else {
auto snapshotSpace = reinterpret_cast<SnapshotSpace *>(space); auto snapshotSpace = reinterpret_cast<SnapshotSpace *>(space);
snapshotSpace->IncreaseLiveObjectSize(liveObjectSize); snapshotSpace->IncreaseLiveObjectSize(liveObjectSize);
@ -1455,7 +1459,14 @@ void SnapshotProcessor::DeserializeTaggedField(uint64_t *value)
} }
if (encodeBit.IsReference() && !encodeBit.IsSpecial()) { if (encodeBit.IsReference() && !encodeBit.IsSpecial()) {
Region *rootRegion = Region::ObjectAddressToRange((uintptr_t)value);
uintptr_t taggedObjectAddr = TaggedObjectEncodeBitToAddr(encodeBit); uintptr_t taggedObjectAddr = TaggedObjectEncodeBitToAddr(encodeBit);
Region *valueRegion = Region::ObjectAddressToRange(taggedObjectAddr);
if (!rootRegion->InYoungSpace() && valueRegion->InYoungSpace()) {
// Should align with '8' in 64 and 32 bit platform
ASSERT((value % static_cast<uint8_t>(MemAlignment::MEM_ALIGN_OBJECT)) == 0);
rootRegion->InsertOldToNewRSet((uintptr_t)value);
}
*value = taggedObjectAddr; *value = taggedObjectAddr;
return; return;
} }

View File

@ -90,7 +90,7 @@ HWTEST_F_L0(SnapshotTest, SerializeConstPool)
Snapshot snapshotDeserialize(ecmaVm); Snapshot snapshotDeserialize(ecmaVm);
snapshotDeserialize.Deserialize(SnapshotType::VM_ROOT, fileName); snapshotDeserialize.Deserialize(SnapshotType::VM_ROOT, fileName);
auto beginRegion = const_cast<Heap *>(ecmaVm->GetHeap())->GetOldSpace()->GetFirstRegion(); auto beginRegion = const_cast<Heap *>(ecmaVm->GetHeap())->GetOldSpace()->GetCurrentRegion();
auto constpool1 = reinterpret_cast<ConstantPool *>(beginRegion->GetBegin()); auto constpool1 = reinterpret_cast<ConstantPool *>(beginRegion->GetBegin());
EXPECT_EQ(constpool->GetClass()->SizeFromJSHClass(*constpool), EXPECT_EQ(constpool->GetClass()->SizeFromJSHClass(*constpool),
constpool1->GetClass()->SizeFromJSHClass(constpool1)); constpool1->GetClass()->SizeFromJSHClass(constpool1));
@ -135,7 +135,7 @@ HWTEST_F_L0(SnapshotTest, SerializeDifferentSpace)
Snapshot snapshotDeserialize(ecmaVm); Snapshot snapshotDeserialize(ecmaVm);
snapshotDeserialize.Deserialize(SnapshotType::VM_ROOT, fileName); snapshotDeserialize.Deserialize(SnapshotType::VM_ROOT, fileName);
auto beginRegion = const_cast<Heap *>(ecmaVm->GetHeap())->GetOldSpace()->GetFirstRegion(); auto beginRegion = const_cast<Heap *>(ecmaVm->GetHeap())->GetOldSpace()->GetCurrentRegion();
auto constpool1 = reinterpret_cast<ConstantPool *>(beginRegion->GetBegin()); auto constpool1 = reinterpret_cast<ConstantPool *>(beginRegion->GetBegin());
EXPECT_EQ(constpool->GetClass()->SizeFromJSHClass(*constpool), EXPECT_EQ(constpool->GetClass()->SizeFromJSHClass(*constpool),
constpool1->GetClass()->SizeFromJSHClass(constpool1)); constpool1->GetClass()->SizeFromJSHClass(constpool1));
@ -191,7 +191,7 @@ HWTEST_F_L0(SnapshotTest, SerializeMultiFile)
snapshotDeserialize.Deserialize(SnapshotType::VM_ROOT, fileName1); snapshotDeserialize.Deserialize(SnapshotType::VM_ROOT, fileName1);
snapshotDeserialize.Deserialize(SnapshotType::VM_ROOT, fileName2); snapshotDeserialize.Deserialize(SnapshotType::VM_ROOT, fileName2);
auto beginRegion = const_cast<Heap *>(ecmaVm->GetHeap())->GetOldSpace()->GetFirstRegion(); auto beginRegion = const_cast<Heap *>(ecmaVm->GetHeap())->GetOldSpace()->GetCurrentRegion();
auto constpool = reinterpret_cast<ConstantPool *>(beginRegion->GetBegin()); auto constpool = reinterpret_cast<ConstantPool *>(beginRegion->GetBegin());
EXPECT_TRUE(constpool->Get(0).IsTaggedArray()); EXPECT_TRUE(constpool->Get(0).IsTaggedArray());
EXPECT_TRUE(constpool->Get(100).IsTaggedArray()); EXPECT_TRUE(constpool->Get(100).IsTaggedArray());