Skip to content

Commit

Permalink
operations/print-header Check dsc subcaches for ranges
Browse files Browse the repository at this point in the history
  • Loading branch information
suhas-pai committed Jan 1, 2025
1 parent c4b4314 commit 8ef97c3
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 38 deletions.
10 changes: 8 additions & 2 deletions include/Objects/DyldSharedCache/CacheInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,17 @@ namespace Objects {
friend struct DyldSharedCache;
protected:
ADT::MemoryMap Map;
std::string_view FileSuffix = "";

uint64_t VmOffset;
uint64_t MaxVmSize;
uint64_t FirstAddr;

constexpr explicit
DyldSharedSingleCacheInfo(const uint64_t VmOffset,
DyldSharedSingleCacheInfo(const std::string_view FileSuffix,
const uint64_t VmOffset,
const uint64_t MaxVmSize) noexcept
: VmOffset(VmOffset), MaxVmSize(MaxVmSize) {}
: FileSuffix(FileSuffix), VmOffset(VmOffset), MaxVmSize(MaxVmSize) {}

constexpr
DyldSharedSingleCacheInfo(const ADT::MemoryMap &Map,
Expand All @@ -41,6 +43,10 @@ namespace Objects {
return ADT::Range::FromSize(0, this->map().size());
}

[[nodiscard]] constexpr auto fileSuffix() const noexcept {
return this->FileSuffix;
}

[[nodiscard]] inline auto &header() const noexcept {
return *this->map().base<
::DyldSharedCache::HeaderV9, /*Verify=*/false>();
Expand Down
3 changes: 2 additions & 1 deletion src/Objects/DyldSharedCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,8 @@ namespace Objects {

auto Info =
SubCacheInfo(SubCachePath,
SingleCacheInfo(SubCacheEntry.CacheVMOffset,
SingleCacheInfo(SubCacheEntry.fileSuffix(),
SubCacheEntry.CacheVMOffset,
MaxVmSize));

if (const auto Error =
Expand Down
84 changes: 49 additions & 35 deletions src/Operations/PrintHeader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -683,14 +683,28 @@ namespace Operations {

static void
WarnIfOutOfRange(FILE *const OutFile,
const ADT::Range &DscRange,
const Objects::DyldSharedCache &Dsc,
const uint64_t Offset,
const uint64_t Size,
const bool PrintNewLine = true) noexcept
{
const auto &DscRange = Dsc.range();
const auto Range = ADT::Range::FromSize(Offset, Size);

if (!DscRange.contains(Range)) {
std::print(OutFile, " (Past EOF!)");
const auto ResultOpt = Dsc.getMapForAddrRange(Range);
if (!ResultOpt.has_value()) {
std::print(OutFile,
" (Out of range, Not entirely found in any "
"single, available sub-cache)");
return;
}

const auto &[SubCache, Map] = ResultOpt.value();
std::print(OutFile,
" (Past EOF, Found in sub-cache with "
"file-suffix \"{}\")",
SubCache.fileSuffix());
}

if (PrintNewLine) {
Expand All @@ -701,7 +715,7 @@ namespace Operations {
template <std::unsigned_integral AddrType, std::unsigned_integral SizeType>
static inline void
PrintDscSizeRange(FILE *const OutFile,
const ADT::Range &DscRange,
const Objects::DyldSharedCache &Dsc,
const std::string_view AddressName,
const std::string_view SizeName,
const AddrType Address,
Expand All @@ -724,16 +738,16 @@ namespace Operations {

if (IsOffset) {
WarnIfOutOfRange(OutFile,
DscRange,
Dsc,
Address,
Size,
/*PrintNewLine=*/false);
}

std::println(OutFile);
std::println(OutFile,
"{}{}{}",
Suffix, DscKey(SizeName), Utils::ByteSize(Size));
std::print(OutFile,
"{}{}{}",
Suffix, DscKey(SizeName), Utils::ByteSize(Size));

if (PrintNewLine) {
std::println(OutFile);
Expand Down Expand Up @@ -905,7 +919,7 @@ namespace Operations {

const auto &Header = Dsc.headerV1();
PrintDscSizeRange(OutFile,
Dsc.range(),
Dsc,
"Code-Signature Offset",
"Code-Signature Size",
Header.CodeSignatureOffset,
Expand All @@ -921,7 +935,7 @@ namespace Operations {
DscRangeKind::CodeSignature);

PrintDscSizeRange(OutFile,
Dsc.range(),
Dsc,
"Slide-Info Offset",
"Slide-Info Size",
Header.SlideInfoOffset,
Expand All @@ -947,7 +961,7 @@ namespace Operations {

const auto &Header = Dsc.headerV2();
PrintDscSizeRange(OutFile,
Dsc.range(),
Dsc,
"Local-Symbols Offset",
"Local-Symbols Size",
Header.LocalSymbolsOffset,
Expand All @@ -972,7 +986,7 @@ namespace Operations {

static void
PrintOffsetCountPair(FILE *const OutFile,
const ADT::Range &Range,
const Objects::DyldSharedCache &Dsc,
const std::string_view OffsetKey,
const std::string_view CountKey,
const OffsetType Offset,
Expand All @@ -983,7 +997,7 @@ namespace Operations {
"{}{}",
DscKey(OffsetKey), Utils::Address(Offset));

WarnIfOutOfRange(OutFile, Range, Offset, /*Size=*/1);
WarnIfOutOfRange(OutFile, Dsc, Offset, /*Size=*/1);
std::print(OutFile,
"{}{}{}",
DscKey(CountKey), Utils::FormattedNumber(Count), Suffix);
Expand Down Expand Up @@ -1024,15 +1038,15 @@ namespace Operations {

const auto &Header = Dsc.headerV4();
PrintOffsetCountPair(OutFile,
Dsc.range(),
Dsc,
"Branch-Pools Offset",
"Branch-Pools Count",
Header.BranchPoolsOffset,
Header.BranchPoolsCount,
/*Suffix=*/"\n");

PrintOffsetCountPair(OutFile,
Dsc.range(),
Dsc,
"Images-Text Offset",
"Images-Text Count",
Header.ImagesTextOffset,
Expand Down Expand Up @@ -1095,7 +1109,7 @@ namespace Operations {

const auto &Header = Dsc.headerV5();
PrintDscSizeRange(OutFile,
Dsc.range(),
Dsc,
"Patch Info Address",
"Patch Info Size",
Header.PatchInfoAddr,
Expand All @@ -1108,7 +1122,7 @@ namespace Operations {
DscRangeKind::PatchInfo);

PrintDscSizeRange(OutFile,
Dsc.range(),
Dsc,
"Other Image-Group Address",
"Other Image-Group Size",
Header.OtherImageGroupAddr,
Expand All @@ -1121,7 +1135,7 @@ namespace Operations {
DscRangeKind::OtherImageGroup);

PrintDscSizeRange(OutFile,
Dsc.range(),
Dsc,
"Program Closures Address",
"Program Closures Size",
Header.ProgClosuresAddr,
Expand All @@ -1134,7 +1148,7 @@ namespace Operations {
DscRangeKind::ProgClosures);

PrintDscSizeRange(OutFile,
Dsc.range(),
Dsc,
"Program Closures Trie Address",
"Program Closures Trie Size",
Header.ProgClosuresTrieAddr,
Expand Down Expand Up @@ -1165,7 +1179,7 @@ namespace Operations {
Header.BuiltFromChainedFixups);

PrintDscSizeRange(OutFile,
Dsc.range(),
Dsc,
"Shared-Region Start",
"Shared-Region Size",
Header.SharedRegionStart,
Expand All @@ -1191,7 +1205,7 @@ namespace Operations {

const auto &Header = Dsc.headerV6();
PrintDscSizeRange(OutFile,
Dsc.range(),
Dsc,
"Dylibs Image-Array Address",
"Dylibs Image-Array Size",
Header.DylibsImageArrayAddr,
Expand All @@ -1204,7 +1218,7 @@ namespace Operations {
DscRangeKind::PatchInfo);

PrintDscSizeRange(OutFile,
Dsc.range(),
Dsc,
"Dylibs Trie Address",
"Dylibs Trie Size",
Header.DylibsTrieAddr,
Expand All @@ -1217,7 +1231,7 @@ namespace Operations {
DscRangeKind::DylibsTrie);

PrintDscSizeRange(OutFile,
Dsc.range(),
Dsc,
"Other Image Array Address",
"Other Image Array Size",
Header.OtherImageArrayAddr,
Expand All @@ -1230,7 +1244,7 @@ namespace Operations {
DscRangeKind::OtherImageArray);

PrintDscSizeRange(OutFile,
Dsc.range(),
Dsc,
"Other Trie Address",
"Other Trie Size",
Header.OtherTrieAddr,
Expand Down Expand Up @@ -1318,7 +1332,7 @@ namespace Operations {

const auto &Header = Dsc.headerV7();
PrintOffsetCountPair(OutFile,
Dsc.range(),
Dsc,
"Mapping With Slide Info Offset",
"Mapping With Slide Info Count",
Header.MappingWithSlideOffset,
Expand Down Expand Up @@ -1354,7 +1368,7 @@ namespace Operations {
Utils::Address(Header.DylibsPBLSetAddr));

PrintDscSizeRange(OutFile,
Dsc.range(),
Dsc,
"Programs Prebuilt Loader Set Pool Address",
"Programs Prebuilt Loader Set Pool Size",
Header.ProgramsPBLSetPoolAddr,
Expand All @@ -1367,7 +1381,7 @@ namespace Operations {
DscRangeKind::ProgramsPBLSetPool);

PrintDscSizeRange(OutFile,
Dsc.range(),
Dsc,
"Program Trie Address",
"Program Trie Size",
Header.ProgramTrieAddr,
Expand All @@ -1389,7 +1403,7 @@ namespace Operations {
Header.altOsVersion());

PrintDscSizeRange(OutFile,
Dsc.range(),
Dsc,
"Swift Optimizations Offset",
"Swift Optimizations Size",
Header.SwiftOptsOffset,
Expand All @@ -1405,7 +1419,7 @@ namespace Operations {
DscRangeKind::SwiftOpts);

PrintOffsetCountPair(OutFile,
Dsc.range(),
Dsc,
"SubCache Array Offset",
"SubCache Array Count",
Header.SubCacheArrayOffset,
Expand All @@ -1422,7 +1436,7 @@ namespace Operations {
Utils::Uuid(Header.SymbolFileUUID));

PrintDscSizeRange(OutFile,
Dsc.range(),
Dsc,
"Rosetta Read-Only Address",
"Rosetta Read-Only Size",
Header.RosettaReadOnlyAddr,
Expand All @@ -1435,7 +1449,7 @@ namespace Operations {
DscRangeKind::RosettaReadOnly);

PrintDscSizeRange(OutFile,
Dsc.range(),
Dsc,
"Rosetta Read-Write Address",
"Rosetta Read-Write Size",
Header.RosettaReadWriteAddr,
Expand All @@ -1452,9 +1466,9 @@ namespace Operations {
std::print(OutFile,
"{}{}\n"
"{}{}\n",
DscKey("Images Count"),
Utils::CustomAddress(Header.ImagesOffset),
DscKey("Images Offset"),
Utils::CustomAddress(Header.ImagesOffset),
DscKey("Images Count"),
Utils::FormattedNumber(Header.ImagesCount));
}

Expand Down Expand Up @@ -1547,7 +1561,7 @@ namespace Operations {

PrintCacheKind(OutFile, "Cache Sub-Kind", Header.cacheSubKind());
PrintDscSizeRange(OutFile,
Dsc.range(),
Dsc,
"Objc Optimizations Offset",
"Objc Optimizations Size",
Header.ObjcOptsOffset,
Expand All @@ -1564,7 +1578,7 @@ namespace Operations {

PrintObjcOptsInfo(OutFile, Dsc, Header);
PrintDscSizeRange(OutFile,
Dsc.range(),
Dsc,
"Cache Atlas Offset",
"Cache Atlas Size",
Header.CacheAtlasOffset,
Expand All @@ -1580,7 +1594,7 @@ namespace Operations {
DscRangeKind::CacheAtlas);

PrintDscSizeRange(OutFile,
Dsc.range(),
Dsc,
"Dynamic Data Offset",
"Dynamic Data Max Size",
Header.DynamicDataOffset,
Expand Down

0 comments on commit 8ef97c3

Please sign in to comment.