@@ -3323,6 +3323,179 @@ void InitDSCViewType()
33233323
33243324namespace SharedCacheCore {
33253325
3326+ void SharedCache::Store (SerializationContext& context) const
3327+ {
3328+ context.doc .AddMember (" metadataVersion" , METADATA_VERSION, context.allocator );
3329+
3330+ MSS (m_viewState);
3331+ MSS_CAST (m_cacheFormat, uint8_t );
3332+ MSS (m_imageStarts);
3333+ MSS (m_baseFilePath);
3334+ rapidjson::Value headers (rapidjson::kArrayType );
3335+ for (auto & [k, v] : m_headers)
3336+ {
3337+ headers.PushBack (v.AsDocument (), context.allocator );
3338+ }
3339+ context.doc .AddMember (" headers" , headers, context.allocator );
3340+ // std::vector<std::pair<uint64_t, std::vector<std::pair<uint64_t, std::pair<BNSymbolType, std::string>>>>>
3341+ // m_exportInfos std::vector<std::pair<uint64_t, std::vector<std::pair<uint64_t, std::pair<BNSymbolType,
3342+ // std::string>>>>> exportInfos;
3343+ rapidjson::Document exportInfos (rapidjson::kArrayType );
3344+
3345+ for (const auto & pair1 : m_exportInfos)
3346+ {
3347+ rapidjson::Value subObj (rapidjson::kObjectType );
3348+ rapidjson::Value subArr (rapidjson::kArrayType );
3349+ for (const auto & pair2 : pair1.second )
3350+ {
3351+ rapidjson::Value subSubObj (rapidjson::kObjectType );
3352+ subSubObj.AddMember (" key" , pair2.first , context.allocator );
3353+ subSubObj.AddMember (" val1" , pair2.second .first , context.allocator );
3354+ subSubObj.AddMember (" val2" , pair2.second .second , context.allocator );
3355+ subArr.PushBack (subSubObj, context.allocator );
3356+ }
3357+
3358+ subObj.AddMember (" key" , pair1.first , context.allocator );
3359+ subObj.AddMember (" value" , subArr, context.allocator );
3360+
3361+ exportInfos.PushBack (subObj, context.allocator );
3362+ }
3363+ context.doc .AddMember (" exportInfos" , exportInfos, context.allocator );
3364+
3365+ rapidjson::Value backingCaches (rapidjson::kArrayType );
3366+ for (auto & bc : m_backingCaches)
3367+ {
3368+ backingCaches.PushBack (bc.AsDocument (), context.allocator );
3369+ }
3370+ context.doc .AddMember (" backingCaches" , backingCaches, context.allocator );
3371+ rapidjson::Value stubIslands (rapidjson::kArrayType );
3372+ for (auto & si : m_stubIslandRegions)
3373+ {
3374+ stubIslands.PushBack (si.AsDocument (), context.allocator );
3375+ }
3376+ rapidjson::Value images (rapidjson::kArrayType );
3377+ for (auto & img : m_images)
3378+ {
3379+ images.PushBack (img.AsDocument (), context.allocator );
3380+ }
3381+ context.doc .AddMember (" images" , images, context.allocator );
3382+ rapidjson::Value regionsMappedIntoMemory (rapidjson::kArrayType );
3383+ for (auto & r : m_regionsMappedIntoMemory)
3384+ {
3385+ regionsMappedIntoMemory.PushBack (r.AsDocument (), context.allocator );
3386+ }
3387+ context.doc .AddMember (" regionsMappedIntoMemory" , regionsMappedIntoMemory, context.allocator );
3388+ context.doc .AddMember (" stubIslands" , stubIslands, context.allocator );
3389+ rapidjson::Value dyldDataSections (rapidjson::kArrayType );
3390+ for (auto & si : m_dyldDataRegions)
3391+ {
3392+ dyldDataSections.PushBack (si.AsDocument (), context.allocator );
3393+ }
3394+ context.doc .AddMember (" dyldDataSections" , dyldDataSections, context.allocator );
3395+ rapidjson::Value nonImageRegions (rapidjson::kArrayType );
3396+ for (auto & si : m_nonImageRegions)
3397+ {
3398+ nonImageRegions.PushBack (si.AsDocument (), context.allocator );
3399+ }
3400+ context.doc .AddMember (" nonImageRegions" , nonImageRegions, context.allocator );
3401+ }
3402+
3403+ void SharedCache::Load (DeserializationContext& context)
3404+ {
3405+ if (context.doc .HasMember (" metadataVersion" ))
3406+ {
3407+ if (context.doc [" metadataVersion" ].GetUint () != METADATA_VERSION)
3408+ {
3409+ m_logger->LogError (" Shared Cache metadata version mismatch" );
3410+ return ;
3411+ }
3412+ }
3413+ else
3414+ {
3415+ m_logger->LogError (" Shared Cache metadata version missing" );
3416+ return ;
3417+ }
3418+ m_viewState = MSL_CAST (m_viewState, uint8_t , DSCViewState);
3419+ m_cacheFormat = MSL_CAST (m_cacheFormat, uint8_t , SharedCacheFormat);
3420+ m_headers.clear ();
3421+ for (auto & startAndHeader : context.doc [" headers" ].GetArray ())
3422+ {
3423+ SharedCacheMachOHeader header;
3424+ header.LoadFromValue (startAndHeader);
3425+ m_headers[header.textBase ] = header;
3426+ }
3427+ MSL (m_imageStarts);
3428+ MSL (m_baseFilePath);
3429+ m_exportInfos.clear ();
3430+ for (const auto & obj1 : context.doc [" exportInfos" ].GetArray ())
3431+ {
3432+ std::vector<std::pair<uint64_t , std::pair<BNSymbolType, std::string>>> innerVec;
3433+ for (const auto & obj2 : obj1[" value" ].GetArray ())
3434+ {
3435+ std::pair<BNSymbolType, std::string> innerPair = {
3436+ (BNSymbolType)obj2[" val1" ].GetUint64 (), obj2[" val2" ].GetString ()};
3437+ innerVec.push_back ({obj2[" key" ].GetUint64 (), innerPair});
3438+ }
3439+
3440+ m_exportInfos[obj1[" key" ].GetUint64 ()] = innerVec;
3441+ }
3442+ m_symbolInfos.clear ();
3443+ for (auto & symbolInfo : context.doc [" symbolInfos" ].GetArray ())
3444+ {
3445+ std::vector<std::pair<uint64_t , std::pair<BNSymbolType, std::string>>> symbolInfoVec;
3446+ for (auto & symbolInfoPair : symbolInfo.GetArray ())
3447+ {
3448+ symbolInfoVec.push_back ({symbolInfoPair[0 ].GetUint64 (),
3449+ {(BNSymbolType)symbolInfoPair[1 ].GetUint (), symbolInfoPair[2 ].GetString ()}});
3450+ }
3451+ m_symbolInfos[symbolInfo[0 ].GetUint64 ()] = std::move (symbolInfoVec);
3452+ }
3453+ m_backingCaches.clear ();
3454+ for (auto & bcV : context.doc [" backingCaches" ].GetArray ())
3455+ {
3456+ BackingCache bc;
3457+ bc.LoadFromValue (bcV);
3458+ m_backingCaches.push_back (std::move (bc));
3459+ }
3460+ m_images.clear ();
3461+ for (auto & imgV : context.doc [" images" ].GetArray ())
3462+ {
3463+ CacheImage img;
3464+ img.LoadFromValue (imgV);
3465+ m_images.push_back (std::move (img));
3466+ }
3467+ m_regionsMappedIntoMemory.clear ();
3468+ for (auto & rV : context.doc [" regionsMappedIntoMemory" ].GetArray ())
3469+ {
3470+ MemoryRegion r;
3471+ r.LoadFromValue (rV);
3472+ m_regionsMappedIntoMemory.push_back (std::move (r));
3473+ }
3474+ m_stubIslandRegions.clear ();
3475+ for (auto & siV : context.doc [" stubIslands" ].GetArray ())
3476+ {
3477+ MemoryRegion si;
3478+ si.LoadFromValue (siV);
3479+ m_stubIslandRegions.push_back (std::move (si));
3480+ }
3481+ m_dyldDataRegions.clear ();
3482+ for (auto & siV : context.doc [" dyldDataSections" ].GetArray ())
3483+ {
3484+ MemoryRegion si;
3485+ si.LoadFromValue (siV);
3486+ m_dyldDataRegions.push_back (std::move (si));
3487+ }
3488+ m_nonImageRegions.clear ();
3489+ for (auto & siV : context.doc [" nonImageRegions" ].GetArray ())
3490+ {
3491+ MemoryRegion si;
3492+ si.LoadFromValue (siV);
3493+ m_nonImageRegions.push_back (std::move (si));
3494+ }
3495+
3496+ m_metadataValid = true ;
3497+ }
3498+
33263499void Serialize (SerializationContext& context, std::string_view name, const mach_header_64& b)
33273500{
33283501 rapidjson::Value key (name.data (), context.allocator );
@@ -3723,4 +3896,4 @@ void Deserialize(DeserializationContext& context, std::string_view name, std::ve
37233896 }
37243897}
37253898
3726- } // namespace SharedCacheCore
3899+ } // namespace SharedCacheCore
0 commit comments