Skip to content

Commit 477e322

Browse files
committed
[SharedCache] Don't capture this within lambda passed to MMappedFileAccessor::Open
Nothing guarantees that the `SharedCache` lives long enough.
1 parent 5d0b328 commit 477e322

File tree

2 files changed

+30
-26
lines changed

2 files changed

+30
-26
lines changed

view/sharedcache/core/SharedCache.cpp

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,13 +1079,14 @@ std::shared_ptr<VM> SharedCache::GetVMMap(const CacheInfo& cacheInfo)
10791079
std::shared_ptr<VM> vm = std::make_shared<VM>(0x1000);
10801080

10811081
uint64_t baseAddress = cacheInfo.BaseAddress();
1082+
Ref<Logger> logger = m_logger;
10821083
for (const auto& cache : cacheInfo.backingCaches)
10831084
{
10841085
for (const auto& mapping : cache.mappings)
10851086
{
10861087
vm->MapPages(m_dscView, m_dscView->GetFile()->GetSessionId(), mapping.address, mapping.fileOffset, mapping.size, cache.path,
1087-
[this, vm, baseAddress](std::shared_ptr<MMappedFileAccessor> mmap){
1088-
ParseAndApplySlideInfoForFile(mmap, baseAddress);
1088+
[vm, baseAddress, logger](std::shared_ptr<MMappedFileAccessor> mmap){
1089+
ParseAndApplySlideInfoForFile(mmap, baseAddress, logger);
10891090
});
10901091
}
10911092
}
@@ -1140,7 +1141,8 @@ std::string to_hex_string(uint64_t value)
11401141
}
11411142

11421143

1143-
void SharedCache::ParseAndApplySlideInfoForFile(std::shared_ptr<MMappedFileAccessor> file, uint64_t base)
1144+
// static
1145+
void SharedCache::ParseAndApplySlideInfoForFile(std::shared_ptr<MMappedFileAccessor> file, uint64_t base, Ref<Logger> logger)
11441146
{
11451147
if (file->SlideInfoWasApplied())
11461148
return;
@@ -1158,7 +1160,7 @@ void SharedCache::ParseAndApplySlideInfoForFile(std::shared_ptr<MMappedFileAcces
11581160
auto slideInfoVersion = file->ReadUInt32(slideInfoOff);
11591161
if (slideInfoVersion != 2 && slideInfoVersion != 3)
11601162
{
1161-
m_logger->LogError("Unsupported slide info version %d", slideInfoVersion);
1163+
logger->LogError("Unsupported slide info version %d", slideInfoVersion);
11621164
throw std::runtime_error("Unsupported slide info version");
11631165
}
11641166

@@ -1181,7 +1183,7 @@ void SharedCache::ParseAndApplySlideInfoForFile(std::shared_ptr<MMappedFileAcces
11811183

11821184
if (targetHeader.mappingWithSlideCount == 0)
11831185
{
1184-
m_logger->LogDebug("No mappings with slide info found");
1186+
logger->LogDebug("No mappings with slide info found");
11851187
}
11861188

11871189
for (auto i = 0; i < targetHeader.mappingWithSlideCount; i++)
@@ -1195,7 +1197,7 @@ void SharedCache::ParseAndApplySlideInfoForFile(std::shared_ptr<MMappedFileAcces
11951197
if (mappingAndSlideInfo.size == 0)
11961198
continue;
11971199
map.slideInfoVersion = file->ReadUInt32(mappingAndSlideInfo.slideInfoFileOffset);
1198-
m_logger->LogDebug("Slide Info Version: %d", map.slideInfoVersion);
1200+
logger->LogDebug("Slide Info Version: %d", map.slideInfoVersion);
11991201
map.mappingInfo.address = mappingAndSlideInfo.address;
12001202
map.mappingInfo.size = mappingAndSlideInfo.size;
12011203
map.mappingInfo.fileOffset = mappingAndSlideInfo.fileOffset;
@@ -1218,30 +1220,30 @@ void SharedCache::ParseAndApplySlideInfoForFile(std::shared_ptr<MMappedFileAcces
12181220
}
12191221
else
12201222
{
1221-
m_logger->LogError("Unknown slide info version: %d", map.slideInfoVersion);
1223+
logger->LogError("Unknown slide info version: %d", map.slideInfoVersion);
12221224
continue;
12231225
}
12241226

12251227
uint64_t slideInfoOffset = mappingAndSlideInfo.slideInfoFileOffset;
12261228
mappings.emplace_back(slideInfoOffset, map);
1227-
m_logger->LogDebug("Filename: %s", file->Path().c_str());
1228-
m_logger->LogDebug("Slide Info Offset: 0x%llx", slideInfoOffset);
1229-
m_logger->LogDebug("Mapping Address: 0x%llx", map.mappingInfo.address);
1230-
m_logger->LogDebug("Slide Info v", map.slideInfoVersion);
1229+
logger->LogDebug("Filename: %s", file->Path().c_str());
1230+
logger->LogDebug("Slide Info Offset: 0x%llx", slideInfoOffset);
1231+
logger->LogDebug("Mapping Address: 0x%llx", map.mappingInfo.address);
1232+
logger->LogDebug("Slide Info v", map.slideInfoVersion);
12311233
}
12321234
}
12331235
}
12341236

12351237
if (mappings.empty())
12361238
{
1237-
m_logger->LogDebug("No slide info found");
1239+
logger->LogDebug("No slide info found");
12381240
file->SetSlideInfoWasApplied(true);
12391241
return;
12401242
}
12411243

12421244
for (const auto& [off, mapping] : mappings)
12431245
{
1244-
m_logger->LogDebug("Slide Info Version: %d", mapping.slideInfoVersion);
1246+
logger->LogDebug("Slide Info Version: %d", mapping.slideInfoVersion);
12451247
uint64_t extrasOffset = off;
12461248
uint64_t pageStartsOffset = off;
12471249
uint64_t pageStartCount;
@@ -1294,7 +1296,7 @@ void SharedCache::ParseAndApplySlideInfoForFile(std::shared_ptr<MMappedFileAcces
12941296
}
12951297
catch (MappingReadException& ex)
12961298
{
1297-
m_logger->LogError("Failed to read v2 slide pointer at 0x%llx\n", loc);
1299+
logger->LogError("Failed to read v2 slide pointer at 0x%llx\n", loc);
12981300
break;
12991301
}
13001302
}
@@ -1319,7 +1321,7 @@ void SharedCache::ParseAndApplySlideInfoForFile(std::shared_ptr<MMappedFileAcces
13191321
}
13201322
catch (MappingReadException& ex)
13211323
{
1322-
m_logger->LogError("Failed to read v2 slide extra at 0x%llx\n", cursor);
1324+
logger->LogError("Failed to read v2 slide extra at 0x%llx\n", cursor);
13231325
break;
13241326
}
13251327
} while (!done);
@@ -1333,7 +1335,7 @@ void SharedCache::ParseAndApplySlideInfoForFile(std::shared_ptr<MMappedFileAcces
13331335
}
13341336
catch (MappingReadException& ex)
13351337
{
1336-
m_logger->LogError("Failed to read v2 slide info at 0x%llx\n", cursor);
1338+
logger->LogError("Failed to read v2 slide info at 0x%llx\n", cursor);
13371339
}
13381340
}
13391341
}
@@ -1381,14 +1383,14 @@ void SharedCache::ParseAndApplySlideInfoForFile(std::shared_ptr<MMappedFileAcces
13811383
}
13821384
catch (MappingReadException& ex)
13831385
{
1384-
m_logger->LogError("Failed to read v3 slide pointer at 0x%llx\n", loc);
1386+
logger->LogError("Failed to read v3 slide pointer at 0x%llx\n", loc);
13851387
break;
13861388
}
13871389
} while (delta != 0);
13881390
}
13891391
catch (MappingReadException& ex)
13901392
{
1391-
m_logger->LogError("Failed to read v3 slide info at 0x%llx\n", cursor);
1393+
logger->LogError("Failed to read v3 slide info at 0x%llx\n", cursor);
13921394
}
13931395
}
13941396
}
@@ -1431,19 +1433,19 @@ void SharedCache::ParseAndApplySlideInfoForFile(std::shared_ptr<MMappedFileAcces
14311433
}
14321434
catch (MappingReadException& ex)
14331435
{
1434-
m_logger->LogError("Failed to read v5 slide pointer at 0x%llx\n", loc);
1436+
logger->LogError("Failed to read v5 slide pointer at 0x%llx\n", loc);
14351437
break;
14361438
}
14371439
} while (delta != 0);
14381440
}
14391441
catch (MappingReadException& ex)
14401442
{
1441-
m_logger->LogError("Failed to read v5 slide info at 0x%llx\n", cursor);
1443+
logger->LogError("Failed to read v5 slide info at 0x%llx\n", cursor);
14421444
}
14431445
}
14441446
}
14451447
}
1446-
// m_logger->LogDebug("Applied slide info for %s (0x%llx rewrites)", file->Path().c_str(), rewrites.size());
1448+
// logger->LogDebug("Applied slide info for %s (0x%llx rewrites)", file->Path().c_str(), rewrites.size());
14471449
file->SetSlideInfoWasApplied(true);
14481450
}
14491451

@@ -3561,10 +3563,12 @@ uint64_t SharedCache::GetObjCRelativeMethodBaseAddress(const VMReader& reader) c
35613563

35623564
std::shared_ptr<MMappedFileAccessor> SharedCache::MapFile(const std::string& path)
35633565
{
3564-
return MMappedFileAccessor::
3565-
Open(m_dscView, m_dscView->GetFile()->GetSessionId(), path, [this](std::shared_ptr<MMappedFileAccessor> mmap) {
3566-
ParseAndApplySlideInfoForFile(mmap, m_cacheInfo->BaseAddress());
3567-
})->lock();
3566+
uint64_t baseAddress = m_cacheInfo->BaseAddress();
3567+
return MMappedFileAccessor::Open(m_dscView, m_dscView->GetFile()->GetSessionId(), path,
3568+
[baseAddress, logger = m_logger](std::shared_ptr<MMappedFileAccessor> mmap) {
3569+
ParseAndApplySlideInfoForFile(mmap, baseAddress, logger);
3570+
})
3571+
->lock();
35683572
}
35693573

35703574
std::shared_ptr<MMappedFileAccessor> SharedCache::MapFileWithoutApplyingSlide(const std::string& path)

view/sharedcache/core/SharedCache.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,7 @@ namespace SharedCacheCore {
596596
bool SaveCacheInfoToDSCView(std::lock_guard<std::mutex>&);
597597
bool SaveModifiedStateToDSCView(std::lock_guard<std::mutex>&);
598598

599-
void ParseAndApplySlideInfoForFile(std::shared_ptr<MMappedFileAccessor> file, uint64_t baseAddress);
599+
static void ParseAndApplySlideInfoForFile(std::shared_ptr<MMappedFileAccessor> file, uint64_t baseAddress, Ref<Logger> logger);
600600
std::optional<uint64_t> GetImageStart(std::string_view installName);
601601
const SharedCacheMachOHeader* HeaderForAddress(uint64_t);
602602
bool LoadImageWithInstallName(std::string installName, bool skipObjC);

0 commit comments

Comments
 (0)