@@ -1962,6 +1962,41 @@ static bool IsPrivateNSClass(NodePointer node) {
1962
1962
return false ;
1963
1963
}
1964
1964
1965
+ CompilerType SwiftLanguageRuntime::GetDynamicTypeAndAddress_EmbeddedClass (
1966
+ uint64_t instance_ptr, CompilerType class_type) {
1967
+ ThreadSafeReflectionContext reflection_ctx = GetReflectionContext ();
1968
+ if (!reflection_ctx)
1969
+ return {};
1970
+ // / If this is an embedded Swift type, there is no metadata, and the
1971
+ // / pointer points to the type's vtable. We can still resolve the type by
1972
+ // / reading the vtable's symbol name.
1973
+ auto pointer = reflection_ctx->ReadPointer (instance_ptr);
1974
+ if (!pointer)
1975
+ return {};
1976
+ llvm::StringRef symbol_name;
1977
+ if (pointer->isResolved ()) {
1978
+ // Find the symbol name at this address.
1979
+ Address address;
1980
+ address.SetLoadAddress (pointer->getOffset (), &GetProcess ().GetTarget ());
1981
+ Symbol *symbol = address.CalculateSymbolContextSymbol ();
1982
+ if (!symbol)
1983
+ return {};
1984
+ Mangled mangled = symbol->GetMangled ();
1985
+ if (!mangled)
1986
+ return {};
1987
+ symbol_name = symbol->GetMangled ().GetMangledName ().GetStringRef ();
1988
+ } else {
1989
+ symbol_name = pointer->getSymbol ();
1990
+ }
1991
+ TypeSystemSwiftTypeRefSP ts = class_type.GetTypeSystem ()
1992
+ .dyn_cast_or_null <TypeSystemSwift>()
1993
+ ->GetTypeSystemSwiftTypeRef ();
1994
+ // The symbol name will be something like "type metadata for Class", extract
1995
+ // "Class" from that name.
1996
+ auto dynamic_type = ts->GetTypeFromTypeMetadataNode (symbol_name);
1997
+ return dynamic_type;
1998
+ }
1999
+
1965
2000
bool SwiftLanguageRuntime::GetDynamicTypeAndAddress_Class (
1966
2001
ValueObject &in_value, CompilerType class_type,
1967
2002
lldb::DynamicValueType use_dynamic, TypeAndOrName &class_type_or_name,
@@ -2023,42 +2058,46 @@ bool SwiftLanguageRuntime::GetDynamicTypeAndAddress_Class(
2023
2058
if (!reflection_ctx)
2024
2059
return false ;
2025
2060
2026
- const swift::reflection::TypeRef *typeref = nullptr ;
2027
- {
2028
- auto typeref_or_err = reflection_ctx->ReadTypeFromInstance (
2029
- instance_ptr, ts->GetDescriptorFinder (), true );
2030
- if (typeref_or_err)
2031
- typeref = &*typeref_or_err;
2032
- else
2033
- LLDB_LOG_ERRORV (GetLog (LLDBLog::Types), typeref_or_err.takeError (),
2034
- " {0}" );
2035
- }
2061
+ CompilerType dynamic_type;
2062
+ if (SwiftLanguageRuntime::GetManglingFlavor (
2063
+ class_type.GetMangledTypeName ()) ==
2064
+ swift::Mangle::ManglingFlavor::Default) {
2036
2065
2037
- // If we couldn't find the typeref from the instance, the best we can do is
2038
- // use the static type. This is a valid use case when the binary doesn't
2039
- // contain any metadata (for example, embedded Swift).
2040
- if (!typeref) {
2041
- auto typeref_or_err = reflection_ctx->GetTypeRef (
2042
- class_type.GetMangledTypeName (), ts->GetDescriptorFinder ());
2043
- if (typeref_or_err)
2044
- typeref = &*typeref_or_err;
2045
- else
2046
- LLDB_LOG_ERRORV (GetLog (LLDBLog::Types), typeref_or_err.takeError (),
2047
- " {0}" );
2048
- }
2066
+ const swift::reflection::TypeRef *typeref = nullptr ;
2067
+ {
2068
+ auto typeref_or_err = reflection_ctx->ReadTypeFromInstance (
2069
+ instance_ptr, ts->GetDescriptorFinder (), true );
2070
+ if (typeref_or_err)
2071
+ typeref = &*typeref_or_err;
2072
+ else
2073
+ LLDB_LOG_ERRORV (GetLog (LLDBLog::Types), typeref_or_err.takeError (),
2074
+ " {0}" );
2075
+ }
2049
2076
2050
- if (!typeref) {
2051
- HEALTH_LOG (" could not read typeref for type: {0} (instance_ptr = {0:x})" ,
2052
- class_type.GetMangledTypeName (), instance_ptr);
2053
- return false ;
2054
- }
2077
+ if (!typeref) {
2078
+ HEALTH_LOG (
2079
+ " could not read typeref for type: {0} (instance_ptr = {1:x})" ,
2080
+ class_type.GetMangledTypeName (), instance_ptr);
2081
+ return false ;
2082
+ }
2055
2083
2056
- auto flavor = SwiftLanguageRuntime::GetManglingFlavor (
2057
- class_type.GetMangledTypeName ());
2084
+ auto flavor = SwiftLanguageRuntime::GetManglingFlavor (
2085
+ class_type.GetMangledTypeName ());
2086
+
2087
+ swift::Demangle::Demangler dem;
2088
+ swift::Demangle::NodePointer node = typeref->getDemangling (dem);
2089
+ dynamic_type = ts->RemangleAsType (dem, node, flavor);
2090
+ } else {
2091
+ dynamic_type =
2092
+ GetDynamicTypeAndAddress_EmbeddedClass (instance_ptr, class_type);
2093
+ if (!dynamic_type) {
2094
+ HEALTH_LOG (" could not resolve dynamic type of embedded swift class: "
2095
+ " {0} (instance_ptr = {1:x})" ,
2096
+ class_type.GetMangledTypeName (), instance_ptr);
2097
+ return false ;
2098
+ }
2099
+ }
2058
2100
2059
- swift::Demangle::Demangler dem;
2060
- swift::Demangle::NodePointer node = typeref->getDemangling (dem);
2061
- CompilerType dynamic_type = ts->RemangleAsType (dem, node, flavor);
2062
2101
LLDB_LOG (log, " dynamic type of instance_ptr {0:x} is {1}" , instance_ptr,
2063
2102
class_type.GetMangledTypeName ());
2064
2103
class_type_or_name.SetCompilerType (dynamic_type);
@@ -2207,39 +2246,77 @@ bool SwiftLanguageRuntime::GetDynamicTypeAndAddress_Existential(
2207
2246
auto tr_ts = tss->GetTypeSystemSwiftTypeRef ();
2208
2247
if (!tr_ts)
2209
2248
return false ;
2210
- auto pair = reflection_ctx->ProjectExistentialAndUnwrapClass (
2211
- remote_existential, *existential_typeref,
2212
- tr_ts->GetDescriptorFinder ());
2213
- if (use_local_buffer)
2214
- PopLocalBuffer ();
2215
-
2216
- if (!pair) {
2217
- if (log)
2218
- log->Printf (" Runtime failed to get dynamic type of existential" );
2219
- return false ;
2220
- }
2221
2249
2222
- const swift::reflection::TypeRef *typeref;
2223
- swift::remote::RemoteAddress out_address (nullptr );
2224
- std::tie (typeref, out_address) = *pair;
2225
- auto ts = tss->GetTypeSystemSwiftTypeRef ();
2226
- if (!ts)
2227
- return false ;
2228
- swift::Demangle::Demangler dem;
2229
- swift::Demangle::NodePointer node = typeref->getDemangling (dem);
2230
2250
auto flavor = SwiftLanguageRuntime::GetManglingFlavor (
2231
2251
existential_type.GetMangledTypeName ());
2252
+ CompilerType dynamic_type;
2253
+ uint64_t dynamic_address = 0 ;
2254
+ if (flavor == swift::Mangle::ManglingFlavor::Default) {
2255
+ auto pair = reflection_ctx->ProjectExistentialAndUnwrapClass (
2256
+ remote_existential, *existential_typeref, tr_ts->GetDescriptorFinder ());
2257
+
2258
+ if (!pair) {
2259
+ if (log)
2260
+ log->Printf (" Runtime failed to get dynamic type of existential" );
2261
+ return false ;
2262
+ }
2263
+
2264
+ const swift::reflection::TypeRef *typeref;
2265
+ swift::remote::RemoteAddress out_address (nullptr );
2266
+ std::tie (typeref, out_address) = *pair;
2267
+
2268
+ auto ts = tss->GetTypeSystemSwiftTypeRef ();
2269
+ if (!ts)
2270
+ return false ;
2271
+ swift::Demangle::Demangler dem;
2272
+ swift::Demangle::NodePointer node = typeref->getDemangling (dem);
2273
+ dynamic_type = ts->RemangleAsType (dem, node, flavor);
2274
+ dynamic_address = out_address.getAddressData ();
2275
+ } else {
2276
+ // In the embedded Swift case, the existential container just points to the
2277
+ // instance.
2278
+ auto reflection_ctx = GetReflectionContext ();
2279
+ if (!reflection_ctx)
2280
+ return false ;
2281
+ auto maybe_addr_or_symbol =
2282
+ reflection_ctx->ReadPointer (existential_address);
2283
+ if (!maybe_addr_or_symbol)
2284
+ return false ;
2285
+
2286
+ uint64_t address = 0 ;
2287
+ if (maybe_addr_or_symbol->isResolved ()) {
2288
+ address = maybe_addr_or_symbol->getOffset ();
2289
+ } else {
2290
+ SymbolContextList sc_list;
2291
+ auto &module_list = GetProcess ().GetTarget ().GetImages ();
2292
+ module_list.FindSymbolsWithNameAndType (
2293
+ ConstString (maybe_addr_or_symbol->getSymbol ()), eSymbolTypeAny,
2294
+ sc_list);
2295
+ if (sc_list.GetSize () != 1 )
2296
+ return false ;
2297
+
2298
+ SymbolContext sc = sc_list[0 ];
2299
+ Symbol *symbol = sc.symbol ;
2300
+ address = symbol->GetLoadAddress (&GetProcess ().GetTarget ());
2301
+ }
2302
+
2303
+ dynamic_type =
2304
+ GetDynamicTypeAndAddress_EmbeddedClass (address, existential_type);
2305
+ if (!dynamic_type)
2306
+ return false ;
2307
+ dynamic_address = maybe_addr_or_symbol->getOffset ();
2308
+ }
2309
+ if (use_local_buffer)
2310
+ PopLocalBuffer ();
2232
2311
2233
- class_type_or_name.SetCompilerType (ts-> RemangleAsType (dem, node, flavor) );
2234
- address.SetRawAddress (out_address. getAddressData () );
2312
+ class_type_or_name.SetCompilerType (dynamic_type );
2313
+ address.SetRawAddress (dynamic_address );
2235
2314
2236
2315
#ifndef NDEBUG
2237
2316
if (ModuleList::GetGlobalModuleListProperties ()
2238
2317
.GetSwiftValidateTypeSystem ()) {
2239
2318
auto reference_pair = GetDynamicTypeAndAddress_ExistentialRemoteAST (
2240
2319
in_value, existential_type, use_local_buffer, existential_address);
2241
- assert (pair.has_value () >= reference_pair.has_value () &&
2242
- " RemoteAST and runtime diverge" );
2243
2320
2244
2321
if (reference_pair) {
2245
2322
CompilerType ref_type = std::get<CompilerType>(*reference_pair);
0 commit comments