diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index 09a6daf4a..e9ed262c0 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -187,6 +187,12 @@ if(PAL_IMPLEMENTATION STREQUAL "CPP11") ../wrappers/obj-c/ODWPrivacyGuardInitConfig.mm ) endif() + if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/modules/sanitizer/ AND BUILD_SANITIZER) + list(APPEND SRCS + ../wrappers/obj-c/ODWSanitizerInitConfig.mm + ../wrappers/obj-c/ODWSanitizer.mm + ) + endif() endif() if(APPLE AND BUILD_SWIFT_WRAPPER) diff --git a/wrappers/obj-c/ODWSanitizer.h b/wrappers/obj-c/ODWSanitizer.h new file mode 100644 index 000000000..eb536e606 --- /dev/null +++ b/wrappers/obj-c/ODWSanitizer.h @@ -0,0 +1,34 @@ +// +// Copyright (c) Microsoft Corporation. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 +// +#include "objc_begin.h" + +NS_ASSUME_NONNULL_BEGIN + +/*! + The ODWSanitizer class represents the Sanitizer inspector. +*/ +@interface ODWSanitizer : NSObject + +#pragma mark Behavior methods +/*! + @brief Set the Sanitizer enabled state. + @param enabled A boolean representing the enabled state for the Sanitizer. + */ ++(void)setEnabled:(bool)enabled; + +/*! + @brief Check whether the Sanitizer is enabled. + @return True if the Sanitizer is enabled, otherwise false. +*/ ++(bool)enabled; + +/*! + @brief Reset the Sanitizer instance. This should be used after LogManager::FlushAndTeardown is called. + */ ++(void)resetSanitizerInstance; +@end + +NS_ASSUME_NONNULL_END +#include "objc_end.h" \ No newline at end of file diff --git a/wrappers/obj-c/ODWSanitizer.mm b/wrappers/obj-c/ODWSanitizer.mm new file mode 100644 index 000000000..2cf83fe84 --- /dev/null +++ b/wrappers/obj-c/ODWSanitizer.mm @@ -0,0 +1,57 @@ +// +// Copyright (c) Microsoft Corporation. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 +// +#include +#include "ILogger.hpp" +#include "LogManager.hpp" +#include "Sanitizer.hpp" +#import +#import "ODWLogConfiguration.h" +#import "ODWSanitizer.h" +#import "ODWSanitizer_private.h" +#import "ODWSanitizerInitConfig.h" + +using namespace MAT; + +@implementation ODWSanitizer + +std::shared_ptr _sanitizerPtr; + ++(void)initializeSanitizer:(ILogger *)logger withODWSanitizerInitConfig:(ODWSanitizerInitConfig *)initConfigObject; +{ + if (_sanitizerPtr != nullptr) + { + return; + } + + SanitizerConfiguration config(logger); + + if ([initConfigObject notificationEventName] != nil) + { + config.NotificationEventName = [[initConfigObject notificationEventName] UTF8String]; + } + + _sanitizerPtr = std::make_shared(config); + LogManager::GetInstance()->SetDataInspector(_sanitizerPtr); +} + ++(void)setEnabled:(bool)enabled +{ + if(_sanitizerPtr != nullptr) + { + _sanitizerPtr->SetEnabled(enabled); + } +} + ++(bool)enabled +{ + return _sanitizerPtr != nullptr && _sanitizerPtr->IsEnabled(); +} + ++(void)resetSanitizerInstance +{ + _sanitizerPtr = nullptr; +} + +@end \ No newline at end of file diff --git a/wrappers/obj-c/ODWSanitizerInitConfig.h b/wrappers/obj-c/ODWSanitizerInitConfig.h new file mode 100644 index 000000000..4e526815b --- /dev/null +++ b/wrappers/obj-c/ODWSanitizerInitConfig.h @@ -0,0 +1,21 @@ +// +// Copyright (c) Microsoft Corporation. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 +// +#include "objc_begin.h" + +NS_ASSUME_NONNULL_BEGIN +/*! + @brief Sanitizer Initialization Configuration + */ +@interface ODWSanitizerInitConfig : NSObject + +/*! + @brief (OPTIONAL) Custom event name to use when logging concerns from the sanitizer. Default value is `SanitizerConcerns`. + */ +@property(readwrite, copy, nonatomic) NSString* notificationEventName; + +@end +NS_ASSUME_NONNULL_END + +#include "objc_end.h" diff --git a/wrappers/obj-c/ODWSanitizerInitConfig.mm b/wrappers/obj-c/ODWSanitizerInitConfig.mm new file mode 100644 index 000000000..cc3790cf5 --- /dev/null +++ b/wrappers/obj-c/ODWSanitizerInitConfig.mm @@ -0,0 +1,13 @@ +// +// Copyright (c) Microsoft Corporation. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 +// +#import +#import "ODWSanitizerInitConfig.h" + +/*! + @brief Sanitizer Initialization Configuration + */ +@implementation ODWSanitizerInitConfig : NSObject + +@end \ No newline at end of file diff --git a/wrappers/obj-c/ODWSanitizer_private.h b/wrappers/obj-c/ODWSanitizer_private.h new file mode 100644 index 000000000..291c71f25 --- /dev/null +++ b/wrappers/obj-c/ODWSanitizer_private.h @@ -0,0 +1,28 @@ +// +// Copyright (c) Microsoft Corporation. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 +// +#include "objc_begin.h" +#include "ILogger.hpp" +#import "ODWSanitizer.h" +#import "ODWSanitizerInitConfig.h" + +NS_ASSUME_NONNULL_BEGIN + +/*! + The ODWSanitizer class represents the sanitizer. +*/ +@interface ODWSanitizer (Private) + +#pragma mark Initialization methods + +/*! + @brief Initializes the sanitizer + @param logger Logger used for reporting concerns + @param initConfigObject the configuration + */ ++(void)initializeSanitizer:(ILogger *)logger withODWSanitizerInitConfig:(ODWSanitizerInitConfig *)initConfigObject; +@end + +NS_ASSUME_NONNULL_END +#include "objc_end.h" \ No newline at end of file diff --git a/wrappers/obj-c/OneDsCppSdk.h b/wrappers/obj-c/OneDsCppSdk.h index 51eff5b9c..c7da249db 100644 --- a/wrappers/obj-c/OneDsCppSdk.h +++ b/wrappers/obj-c/OneDsCppSdk.h @@ -12,6 +12,7 @@ #import "ODWLogger.h" #import "ODWLogManager.h" #import "ODWPrivacyGuard.h" +#import "ODWSanitizer.h" #import "ODWSemanticContext.h" #endif diff --git a/wrappers/swift/Headers/ObjCModule-Bridging-Header.h b/wrappers/swift/Headers/ObjCModule-Bridging-Header.h index c0e11bcb8..2de4f6a56 100644 --- a/wrappers/swift/Headers/ObjCModule-Bridging-Header.h +++ b/wrappers/swift/Headers/ObjCModule-Bridging-Header.h @@ -14,4 +14,6 @@ #import "../../obj-c/ODWLogManager.h" #import "../../obj-c/ODWPrivacyGuard.h" #import "../../obj-c/ODWPrivacyGuardInitConfig.h" +#import "../../obj-c/ODWSanitizer.h" +#import "../../obj-c/ODWSanitizerInitConfig.h" #import "../../obj-c/ODWSemanticContext.h" diff --git a/wrappers/swift/Sources/OneDSSwift/Sanitizer.swift b/wrappers/swift/Sources/OneDSSwift/Sanitizer.swift new file mode 100644 index 000000000..3aefd3563 --- /dev/null +++ b/wrappers/swift/Sources/OneDSSwift/Sanitizer.swift @@ -0,0 +1,30 @@ +// +// Copyright (c) Microsoft Corporation. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 +// + +import ObjCModule + +/// Wrapper to `ODWSanitizer` representing the Sanitizer. +public final class Sanitizer { + + /** + Set Sanitizer enabled. + + - Parameters: + - enable: `True` to enable the Sanitizer, `False` otherwise. + */ + public static func setEnabled(_ enable: Bool) { + ODWSanitizer.setEnabled(enable) + } + + /// Checks if the Sanitizer is enabled, returns `True` if it is, `False` otherwise. + public static func enabled() -> Bool { + return ODWSanitizer.enabled() + } + + /// Resets the Sanitizer instance. + public static func resetPrivacyGuardInstance() { + ODWSanitizer.resetSanitizerInstance() + } +} \ No newline at end of file diff --git a/wrappers/swift/Sources/OneDSSwift/SanitizerInitConfig.swift b/wrappers/swift/Sources/OneDSSwift/SanitizerInitConfig.swift new file mode 100644 index 000000000..5882e4a64 --- /dev/null +++ b/wrappers/swift/Sources/OneDSSwift/SanitizerInitConfig.swift @@ -0,0 +1,35 @@ +// +// Copyright (c) Microsoft Corporation. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 +// + +import ObjCModule + +public final class SanitizerInitConfig { + let odwSanitizerInitConfig: ODWSanitizerInitConfig + + /// Default constructor. + public init() { + odwSanitizerInitConfig = ODWSanitizerInitConfig() + } + + /// (OPTIONAL) Custom event name to use when logging concerns from the sanitizer. Default value is `SanitizerConcerns`. + public var notificationEventName: String { + get { + odwSanitizerInitConfig.notificationEventName + } + set { + odwSanitizerInitConfig.notificationEventName = newValue + } + } + + /** + Returns the Obj-C object of the wrapper. + + - Return: + `ODWSanitizerInitConfig` object which class is wrapped around. + */ + func getODWSanitizerInitConfig() -> ODWSanitizerInitConfig { + return odwSanitizerInitConfig + } +} \ No newline at end of file