Skip to content

Commit

Permalink
Fix FIRCLSContextInitData crash (#11699)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ken Grigsby authored Sep 6, 2023
1 parent da5a22c commit 6830729
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 52 deletions.
27 changes: 6 additions & 21 deletions Crashlytics/Crashlytics/Components/FIRCLSContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ __BEGIN_DECLS
@class FIRCLSSettings;
@class FIRCLSInstallIdentifierModel;
@class FIRCLSFileManager;
@class FIRCLSContextInitData;
#endif

typedef struct {
Expand Down Expand Up @@ -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);
Expand Down
50 changes: 24 additions & 26 deletions Crashlytics/Crashlytics/Components/FIRCLSContext.m
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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;
}
Expand All @@ -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));
}
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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 =
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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)) {
Expand Down
10 changes: 5 additions & 5 deletions Crashlytics/Crashlytics/Controllers/FIRCLSContextManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
}
Expand Down
38 changes: 38 additions & 0 deletions Crashlytics/Crashlytics/Helpers/FIRCLSContextInitData.h
Original file line number Diff line number Diff line change
@@ -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 <Foundation/Foundation.h>

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
18 changes: 18 additions & 0 deletions Crashlytics/Crashlytics/Helpers/FIRCLSContextInitData.m
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 6830729

Please sign in to comment.