Skip to content

Fix the swift-inspect dump-generic-metadata operation. #66524

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

Merged
merged 1 commit into from
Jun 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions include/swift/RemoteInspection/GenericMetadataCacheEntry.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//===--- GenericMetadataCacheEntry.h ----------------------------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// Declares a struct that mirrors the layout of GenericCacheEntry in
// Metadata.cpp and use a static assert to check that the offset of
// the member Value match between the two.
//
//===----------------------------------------------------------------------===//

#ifndef SWIFT_REFLECTION_GENERICMETADATACACHEENTRY_H
#define SWIFT_REFLECTION_GENERICMETADATACACHEENTRY_H

#include <cstdint>

namespace swift {

template<typename StoredPointer>
struct GenericMetadataCacheEntry {
StoredPointer TrackingInfo;
uint16_t NumKeyParameters;
uint16_t NumWitnessTables;
uint16_t NumPacks;
uint16_t NumShapeClasses;
StoredPointer PackShapeDescriptors;
uint32_t Hash;
StoredPointer Value;
};

} // namespace swift

#endif // SWIFT_REFLECTION_GENERICMETADATACACHEENTRY_H
15 changes: 4 additions & 11 deletions include/swift/RemoteInspection/ReflectionContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "swift/Concurrency/Actor.h"
#include "swift/Remote/MemoryReader.h"
#include "swift/Remote/MetadataReader.h"
#include "swift/RemoteInspection/GenericMetadataCacheEntry.h"
#include "swift/RemoteInspection/Records.h"
#include "swift/RemoteInspection/RuntimeInternals.h"
#include "swift/RemoteInspection/TypeLowering.h"
Expand Down Expand Up @@ -1289,22 +1290,14 @@ class ReflectionContext
StoredPointer allocationMetadataPointer(
MetadataAllocation<Runtime> Allocation) {
if (Allocation.Tag == GenericMetadataCacheTag) {
struct GenericMetadataCacheEntry {
StoredPointer LockedStorage;
uint8_t LockedStorageKind;
uint8_t TrackingInfo;
uint16_t NumKeyParameters;
uint16_t NumWitnessTables;
uint32_t Hash;
StoredPointer Value;
};
auto AllocationBytes =
getReader().readBytes(RemoteAddress(Allocation.Ptr),
Allocation.Size);
if (!AllocationBytes)
return 0;
auto Entry = reinterpret_cast<const GenericMetadataCacheEntry *>(
AllocationBytes.get());
auto Entry =
reinterpret_cast<const GenericMetadataCacheEntry<StoredPointer> *>(
AllocationBytes.get());
return Entry->Value;
}
return 0;
Expand Down
11 changes: 11 additions & 0 deletions stdlib/public/runtime/Metadata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "swift/Basic/Range.h"
#include "swift/Basic/STLExtras.h"
#include "swift/Demangling/Demangler.h"
#include "swift/RemoteInspection/GenericMetadataCacheEntry.h"
#include "swift/Runtime/Casting.h"
#include "swift/Runtime/EnvironmentVariables.h"
#include "swift/Runtime/ExistentialContainer.h"
Expand Down Expand Up @@ -427,6 +428,16 @@ namespace {
};
} // end anonymous namespace

namespace swift {
struct StaticAssertGenericMetadataCacheEntryValueOffset {
static_assert(
offsetof(GenericCacheEntry, Value) ==
offsetof(swift::GenericMetadataCacheEntry<InProcess::StoredPointer>,
Value),
"The generic metadata cache entry layout mismatch");
};
}

namespace {
class GenericMetadataCache :
public MetadataCache<GenericCacheEntry, GenericMetadataCacheTag> {
Expand Down
2 changes: 2 additions & 0 deletions stdlib/public/runtime/MetadataCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -1583,6 +1583,8 @@ class VariadicMetadataCacheEntryBase :
bool matchesKey(const MetadataCacheKey &key) const {
return key == getKey();
}

friend struct StaticAssertGenericMetadataCacheEntryValueOffset;
};

template <class EntryType, uint16_t Tag>
Expand Down