Adrian Prantl 6eaedbb52f Make CompilerType safe
When a process gets restarted TypeSystem objects associated with it
may get deleted, and any CompilerType objects holding on to a
reference to that type system are a use-after-free in waiting. Because
of the SBAPI, we don't have tight control over where CompilerTypes go
and when they are used. This is particularly a problem in the Swift
plugin, where the scratch TypeSystem can be restarted while the
process is still running. The Swift plugin has a lock to prevent
abuse, but where there's a lock there can be bugs.

This patch changes CompilerType to store a std::weak_ptr<TypeSystem>.
Most of the std::weak_ptr<TypeSystem>* uglyness is hidden by
introducing a wrapper class CompilerType::WrappedTypeSystem that has a
dyn_cast_or_null() method. The only sites that need to know about the
weak pointer implementation detail are the ones that deal with
creating TypeSystems.

rdar://101505232

Differential Revision: https://reviews.llvm.org/D136650
2022-11-16 15:51:26 -08:00

89 lines
2.7 KiB
C++

//===-- CoreMedia.cpp -----------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "CoreMedia.h"
#include "lldb/Utility/Flags.h"
#include "lldb/Utility/Log.h"
#include "lldb/Symbol/TypeSystem.h"
#include "lldb/Target/Target.h"
#include <cinttypes>
using namespace lldb;
using namespace lldb_private;
using namespace lldb_private::formatters;
bool lldb_private::formatters::CMTimeSummaryProvider(
ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
CompilerType type = valobj.GetCompilerType();
if (!type.IsValid())
return false;
auto type_system = type.GetTypeSystem();
if (!type_system)
return false;
// fetch children by offset to compensate for potential lack of debug info
auto int64_ty =
type_system->GetBuiltinTypeForEncodingAndBitSize(eEncodingSint, 64);
auto int32_ty =
type_system->GetBuiltinTypeForEncodingAndBitSize(eEncodingSint, 32);
auto value_sp(valobj.GetSyntheticChildAtOffset(0, int64_ty, true));
auto timescale_sp(valobj.GetSyntheticChildAtOffset(8, int32_ty, true));
auto flags_sp(valobj.GetSyntheticChildAtOffset(12, int32_ty, true));
if (!value_sp || !timescale_sp || !flags_sp)
return false;
auto value = value_sp->GetValueAsUnsigned(0);
auto timescale = (int32_t)timescale_sp->GetValueAsUnsigned(
0); // the timescale specifies the fraction of a second each unit in the
// numerator occupies
auto flags = Flags(flags_sp->GetValueAsUnsigned(0) &
0x00000000000000FF); // the flags I need sit in the LSB
const unsigned int FlagPositiveInf = 4;
const unsigned int FlagNegativeInf = 8;
const unsigned int FlagIndefinite = 16;
if (flags.AnySet(FlagIndefinite)) {
stream.Printf("indefinite");
return true;
}
if (flags.AnySet(FlagPositiveInf)) {
stream.Printf("+oo");
return true;
}
if (flags.AnySet(FlagNegativeInf)) {
stream.Printf("-oo");
return true;
}
switch (timescale) {
case 0:
return false;
case 1:
stream.Printf("%" PRId64 " seconds", value);
return true;
case 2:
stream.Printf("%" PRId64 " half seconds", value);
return true;
case 3:
stream.Printf("%" PRId64 " third%sof a second", value,
value == 1 ? " " : "s ");
return true;
default:
stream.Printf("%" PRId64 " %" PRId32 "th%sof a second", value, timescale,
value == 1 ? " " : "s ");
return true;
}
}