From 6830729d2fb8f9a30c2920767f0a8f278204c3a4 Mon Sep 17 00:00:00 2001 From: Ken Grigsby Date: Wed, 6 Sep 2023 14:46:36 -0400 Subject: [PATCH] Fix FIRCLSContextInitData crash (#11699) --- .../Crashlytics/Components/FIRCLSContext.h | 27 +++------- .../Crashlytics/Components/FIRCLSContext.m | 50 +++++++++---------- .../Controllers/FIRCLSContextManager.m | 10 ++-- .../Helpers/FIRCLSContextInitData.h | 38 ++++++++++++++ .../Helpers/FIRCLSContextInitData.m | 18 +++++++ 5 files changed, 91 insertions(+), 52 deletions(-) create mode 100644 Crashlytics/Crashlytics/Helpers/FIRCLSContextInitData.h create mode 100644 Crashlytics/Crashlytics/Helpers/FIRCLSContextInitData.m diff --git a/Crashlytics/Crashlytics/Components/FIRCLSContext.h b/Crashlytics/Crashlytics/Components/FIRCLSContext.h index e25cf2ed8cf..f2ee4250a3f 100644 --- a/Crashlytics/Crashlytics/Components/FIRCLSContext.h +++ b/Crashlytics/Crashlytics/Components/FIRCLSContext.h @@ -38,6 +38,7 @@ __BEGIN_DECLS @class FIRCLSSettings; @class FIRCLSInstallIdentifierModel; @class FIRCLSFileManager; +@class FIRCLSContextInitData; #endif typedef struct { @@ -80,29 +81,13 @@ typedef struct { FIRCLSReadWriteContext* writable; FIRCLSAllocatorRef allocator; } FIRCLSContext; - -typedef struct { - const char* customBundleId; - const char* rootPath; - const char* previouslyCrashedFileRootPath; - const char* sessionId; - const char* appQualitySessionId; - const char* betaToken; - bool errorsEnabled; - bool customExceptionsEnabled; - uint32_t maxCustomExceptions; - uint32_t maxErrorLogSize; - uint32_t maxLogSize; - uint32_t maxKeyValues; -} FIRCLSContextInitData; - #ifdef __OBJC__ bool FIRCLSContextInitialize(FIRCLSContextInitData* initData, FIRCLSFileManager* fileManager); -FIRCLSContextInitData FIRCLSContextBuildInitData(FIRCLSInternalReport* report, - FIRCLSSettings* settings, - FIRCLSFileManager* fileManager, - NSString* appQualitySessionId); -bool FIRCLSContextRecordMetadata(NSString* rootPath, const FIRCLSContextInitData* initData); +FIRCLSContextInitData* FIRCLSContextBuildInitData(FIRCLSInternalReport* report, + FIRCLSSettings* settings, + FIRCLSFileManager* fileManager, + NSString* appQualitySessionId); +bool FIRCLSContextRecordMetadata(NSString* rootPath, FIRCLSContextInitData* initData); #endif void FIRCLSContextBaseInit(void); diff --git a/Crashlytics/Crashlytics/Components/FIRCLSContext.m b/Crashlytics/Crashlytics/Components/FIRCLSContext.m index b12f9ee5c3d..dd0e416492c 100644 --- a/Crashlytics/Crashlytics/Components/FIRCLSContext.m +++ b/Crashlytics/Crashlytics/Components/FIRCLSContext.m @@ -28,6 +28,7 @@ #include "Crashlytics/Crashlytics/Components/FIRCLSCrashedMarkerFile.h" #include "Crashlytics/Crashlytics/Components/FIRCLSGlobals.h" #include "Crashlytics/Crashlytics/Components/FIRCLSProcess.h" +#include "Crashlytics/Crashlytics/Helpers/FIRCLSContextInitData.h" #include "Crashlytics/Crashlytics/Helpers/FIRCLSDefines.h" #include "Crashlytics/Crashlytics/Helpers/FIRCLSFeatures.h" #include "Crashlytics/Crashlytics/Helpers/FIRCLSUtility.h" @@ -50,30 +51,27 @@ static const char* FIRCLSContextAppendToRoot(NSString* root, NSString* component); static void FIRCLSContextAllocate(FIRCLSContext* context); -FIRCLSContextInitData FIRCLSContextBuildInitData(FIRCLSInternalReport* report, - FIRCLSSettings* settings, - FIRCLSFileManager* fileManager, - NSString* appQualitySessionId) { +FIRCLSContextInitData* FIRCLSContextBuildInitData(FIRCLSInternalReport* report, + FIRCLSSettings* settings, + FIRCLSFileManager* fileManager, + NSString* appQualitySessionId) { // Because we need to start the crash reporter right away, // it starts up either with default settings, or cached settings // from the last time they were fetched - FIRCLSContextInitData initData; - - memset(&initData, 0, sizeof(FIRCLSContextInitData)); - + FIRCLSContextInitData* initData = [[FIRCLSContextInitData alloc] init]; initData.customBundleId = nil; - initData.sessionId = [[report identifier] UTF8String]; - initData.appQualitySessionId = [appQualitySessionId UTF8String]; - initData.rootPath = [[report path] UTF8String]; - initData.previouslyCrashedFileRootPath = [[fileManager rootPath] UTF8String]; + initData.sessionId = [report identifier]; + initData.appQualitySessionId = appQualitySessionId; + initData.rootPath = [report path]; + initData.previouslyCrashedFileRootPath = [fileManager rootPath]; initData.errorsEnabled = [settings errorReportingEnabled]; initData.customExceptionsEnabled = [settings customExceptionsEnabled]; initData.maxCustomExceptions = [settings maxCustomExceptions]; initData.maxErrorLogSize = [settings errorLogBufferSize]; initData.maxLogSize = [settings logBufferSize]; initData.maxKeyValues = [settings maxCustomKeys]; - initData.betaToken = ""; + initData.betaToken = @""; return initData; } @@ -88,15 +86,15 @@ bool FIRCLSContextInitialize(FIRCLSContextInitData* initData, FIRCLSFileManager* dispatch_group_t group = dispatch_group_create(); dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); - if (!FIRCLSIsValidPointer(initData->rootPath)) { + if (!FIRCLSIsValidPointer(initData.rootPath)) { return false; } - NSString* rootPath = [NSString stringWithUTF8String:initData->rootPath]; + NSString* rootPath = initData.rootPath; // setup our SDK log file synchronously, because other calls may depend on it _firclsContext.readonly->logPath = FIRCLSContextAppendToRoot(rootPath, @"sdk.log"); - _firclsContext.readonly->initialReportPath = FIRCLSDupString(initData->rootPath); + _firclsContext.readonly->initialReportPath = FIRCLSDupString([[initData rootPath] UTF8String]); if (!FIRCLSUnlinkIfExists(_firclsContext.readonly->logPath)) { FIRCLSErrorLog(@"Unable to write initialize SDK write paths %s", strerror(errno)); } @@ -111,7 +109,7 @@ bool FIRCLSContextInitialize(FIRCLSContextInitData* initData, FIRCLSFileManager* dispatch_group_async(group, queue, ^{ _firclsContext.readonly->logging.errorStorage.maxSize = 0; _firclsContext.readonly->logging.errorStorage.maxEntries = - initData->errorsEnabled ? initData->maxCustomExceptions : 0; + initData.errorsEnabled ? initData.maxCustomExceptions : 0; _firclsContext.readonly->logging.errorStorage.restrictBySize = false; _firclsContext.readonly->logging.errorStorage.entryCount = &_firclsContext.writable->logging.errorsCount; @@ -120,7 +118,7 @@ bool FIRCLSContextInitialize(FIRCLSContextInitData* initData, FIRCLSFileManager* _firclsContext.readonly->logging.errorStorage.bPath = FIRCLSContextAppendToRoot(rootPath, FIRCLSReportErrorBFile); - _firclsContext.readonly->logging.logStorage.maxSize = initData->maxLogSize; + _firclsContext.readonly->logging.logStorage.maxSize = initData.maxLogSize; _firclsContext.readonly->logging.logStorage.maxEntries = 0; _firclsContext.readonly->logging.logStorage.restrictBySize = true; _firclsContext.readonly->logging.logStorage.entryCount = NULL; @@ -135,11 +133,11 @@ bool FIRCLSContextInitialize(FIRCLSContextInitData* initData, FIRCLSFileManager* _firclsContext.readonly->logging.customExceptionStorage.maxSize = 0; _firclsContext.readonly->logging.customExceptionStorage.restrictBySize = false; _firclsContext.readonly->logging.customExceptionStorage.maxEntries = - initData->maxCustomExceptions; + initData.maxCustomExceptions; _firclsContext.readonly->logging.customExceptionStorage.entryCount = &_firclsContext.writable->exception.customExceptionCount; - _firclsContext.readonly->logging.userKVStorage.maxCount = initData->maxKeyValues; + _firclsContext.readonly->logging.userKVStorage.maxCount = initData.maxKeyValues; _firclsContext.readonly->logging.userKVStorage.incrementalPath = FIRCLSContextAppendToRoot(rootPath, FIRCLSReportUserIncrementalKVFile); _firclsContext.readonly->logging.userKVStorage.compactedPath = @@ -162,7 +160,7 @@ bool FIRCLSContextInitialize(FIRCLSContextInitData* initData, FIRCLSFileManager* }); dispatch_group_async(group, queue, ^{ - NSString* rootPath = [NSString stringWithUTF8String:initData->previouslyCrashedFileRootPath]; + NSString* rootPath = initData.previouslyCrashedFileRootPath; NSString* fileName = [NSString stringWithUTF8String:FIRCLSCrashedMarkerFileName]; _firclsContext.readonly->previouslyCrashedFileFullPath = FIRCLSContextAppendToRoot(rootPath, fileName); @@ -194,7 +192,7 @@ bool FIRCLSContextInitialize(FIRCLSContextInitData* initData, FIRCLSFileManager* _firclsContext.readonly->exception.path = FIRCLSContextAppendToRoot(rootPath, FIRCLSReportExceptionFile); _firclsContext.readonly->exception.maxCustomExceptions = - initData->customExceptionsEnabled ? initData->maxCustomExceptions : 0; + initData.customExceptionsEnabled ? initData.maxCustomExceptions : 0; FIRCLSExceptionInitialize(&_firclsContext.readonly->exception, &_firclsContext.writable->exception); @@ -401,10 +399,10 @@ static bool FIRCLSContextRecordApplication(FIRCLSFile* file, const char* customB } bool FIRCLSContextRecordMetadata(NSString* rootPath, const FIRCLSContextInitData* initData) { - const char* sessionId = initData->sessionId; - const char* betaToken = initData->betaToken; - const char* customBundleId = initData->customBundleId; - const char* appQualitySessionId = initData->appQualitySessionId; + const char* sessionId = [[initData sessionId] UTF8String]; + const char* betaToken = [[initData betaToken] UTF8String]; + const char* customBundleId = [[initData customBundleId] UTF8String]; + const char* appQualitySessionId = [[initData appQualitySessionId] UTF8String]; const char* path = [[rootPath stringByAppendingPathComponent:FIRCLSReportMetadataFile] fileSystemRepresentation]; if (!FIRCLSUnlinkIfExists(path)) { diff --git a/Crashlytics/Crashlytics/Controllers/FIRCLSContextManager.m b/Crashlytics/Crashlytics/Controllers/FIRCLSContextManager.m index 3bc8157b022..260b05fd1b9 100644 --- a/Crashlytics/Crashlytics/Controllers/FIRCLSContextManager.m +++ b/Crashlytics/Crashlytics/Controllers/FIRCLSContextManager.m @@ -49,8 +49,8 @@ - (BOOL)setupContextWithReport:(FIRCLSInternalReport *)report _hasInitializedContext = true; - FIRCLSContextInitData initDataObj = self.buildInitData; - return FIRCLSContextInitialize(&initDataObj, self.fileManager); + FIRCLSContextInitData *initDataObj = self.buildInitData; + return FIRCLSContextInitialize(initDataObj, self.fileManager); } - (void)setAppQualitySessionId:(NSString *)appQualitySessionId { @@ -64,13 +64,13 @@ - (void)setAppQualitySessionId:(NSString *)appQualitySessionId { return; } - FIRCLSContextInitData initDataObj = self.buildInitData; - if (!FIRCLSContextRecordMetadata(self.report.path, &initDataObj)) { + FIRCLSContextInitData *initDataObj = self.buildInitData; + if (!FIRCLSContextRecordMetadata(self.report.path, initDataObj)) { FIRCLSErrorLog(@"Failed to write context file while updating App Quality Session ID"); } } -- (FIRCLSContextInitData)buildInitData { +- (FIRCLSContextInitData *)buildInitData { return FIRCLSContextBuildInitData(self.report, self.settings, self.fileManager, self.appQualitySessionId); } diff --git a/Crashlytics/Crashlytics/Helpers/FIRCLSContextInitData.h b/Crashlytics/Crashlytics/Helpers/FIRCLSContextInitData.h new file mode 100644 index 00000000000..d08ff492f97 --- /dev/null +++ b/Crashlytics/Crashlytics/Helpers/FIRCLSContextInitData.h @@ -0,0 +1,38 @@ +// Copyright 2023 Google +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#pragma once + +#import + +NS_ASSUME_NONNULL_BEGIN + +@interface FIRCLSContextInitData : NSObject + +@property(nonatomic, copy, nullable) NSString* customBundleId; +@property(nonatomic, copy) NSString* rootPath; +@property(nonatomic, copy) NSString* previouslyCrashedFileRootPath; +@property(nonatomic, copy) NSString* sessionId; +@property(nonatomic, copy) NSString* appQualitySessionId; +@property(nonatomic, copy) NSString* betaToken; +@property(nonatomic) BOOL errorsEnabled; +@property(nonatomic) BOOL customExceptionsEnabled; +@property(nonatomic) uint32_t maxCustomExceptions; +@property(nonatomic) uint32_t maxErrorLogSize; +@property(nonatomic) uint32_t maxLogSize; +@property(nonatomic) uint32_t maxKeyValues; + +@end + +NS_ASSUME_NONNULL_END diff --git a/Crashlytics/Crashlytics/Helpers/FIRCLSContextInitData.m b/Crashlytics/Crashlytics/Helpers/FIRCLSContextInitData.m new file mode 100644 index 00000000000..5fb39cfe002 --- /dev/null +++ b/Crashlytics/Crashlytics/Helpers/FIRCLSContextInitData.m @@ -0,0 +1,18 @@ +// Copyright 2023 Google +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#import "Crashlytics/Crashlytics/Helpers/FIRCLSContextInitData.h" + +@implementation FIRCLSContextInitData +@end