Skip to content

Commit d0d0fee

Browse files
authored
added obj-c wrappers for sanitizer (#1347)
1 parent ba0d26c commit d0d0fee

File tree

10 files changed

+227
-0
lines changed

10 files changed

+227
-0
lines changed

lib/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,12 @@ if(PAL_IMPLEMENTATION STREQUAL "CPP11")
187187
../wrappers/obj-c/ODWPrivacyGuardInitConfig.mm
188188
)
189189
endif()
190+
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/modules/sanitizer/ AND BUILD_SANITIZER)
191+
list(APPEND SRCS
192+
../wrappers/obj-c/ODWSanitizerInitConfig.mm
193+
../wrappers/obj-c/ODWSanitizer.mm
194+
)
195+
endif()
190196
endif()
191197

192198
if(APPLE AND BUILD_SWIFT_WRAPPER)

wrappers/obj-c/ODWSanitizer.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//
2+
// Copyright (c) Microsoft Corporation. All rights reserved.
3+
// SPDX-License-Identifier: Apache-2.0
4+
//
5+
#include "objc_begin.h"
6+
7+
NS_ASSUME_NONNULL_BEGIN
8+
9+
/*!
10+
The <b>ODWSanitizer</b> class represents the Sanitizer inspector.
11+
*/
12+
@interface ODWSanitizer : NSObject
13+
14+
#pragma mark Behavior methods
15+
/*!
16+
@brief Set the Sanitizer enabled state.
17+
@param enabled A boolean representing the enabled state for the Sanitizer.
18+
*/
19+
+(void)setEnabled:(bool)enabled;
20+
21+
/*!
22+
@brief Check whether the Sanitizer is enabled.
23+
@return True if the Sanitizer is enabled, otherwise false.
24+
*/
25+
+(bool)enabled;
26+
27+
/*!
28+
@brief Reset the Sanitizer instance. This should be used after LogManager::FlushAndTeardown is called.
29+
*/
30+
+(void)resetSanitizerInstance;
31+
@end
32+
33+
NS_ASSUME_NONNULL_END
34+
#include "objc_end.h"

wrappers/obj-c/ODWSanitizer.mm

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
//
2+
// Copyright (c) Microsoft Corporation. All rights reserved.
3+
// SPDX-License-Identifier: Apache-2.0
4+
//
5+
#include <stdexcept>
6+
#include "ILogger.hpp"
7+
#include "LogManager.hpp"
8+
#include "Sanitizer.hpp"
9+
#import <Foundation/Foundation.h>
10+
#import "ODWLogConfiguration.h"
11+
#import "ODWSanitizer.h"
12+
#import "ODWSanitizer_private.h"
13+
#import "ODWSanitizerInitConfig.h"
14+
15+
using namespace MAT;
16+
17+
@implementation ODWSanitizer
18+
19+
std::shared_ptr<Sanitizer> _sanitizerPtr;
20+
21+
+(void)initializeSanitizer:(ILogger *)logger withODWSanitizerInitConfig:(ODWSanitizerInitConfig *)initConfigObject;
22+
{
23+
if (_sanitizerPtr != nullptr)
24+
{
25+
return;
26+
}
27+
28+
SanitizerConfiguration config(logger);
29+
30+
if ([initConfigObject notificationEventName] != nil)
31+
{
32+
config.NotificationEventName = [[initConfigObject notificationEventName] UTF8String];
33+
}
34+
35+
_sanitizerPtr = std::make_shared<Sanitizer>(config);
36+
LogManager::GetInstance()->SetDataInspector(_sanitizerPtr);
37+
}
38+
39+
+(void)setEnabled:(bool)enabled
40+
{
41+
if(_sanitizerPtr != nullptr)
42+
{
43+
_sanitizerPtr->SetEnabled(enabled);
44+
}
45+
}
46+
47+
+(bool)enabled
48+
{
49+
return _sanitizerPtr != nullptr && _sanitizerPtr->IsEnabled();
50+
}
51+
52+
+(void)resetSanitizerInstance
53+
{
54+
_sanitizerPtr = nullptr;
55+
}
56+
57+
@end
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//
2+
// Copyright (c) Microsoft Corporation. All rights reserved.
3+
// SPDX-License-Identifier: Apache-2.0
4+
//
5+
#include "objc_begin.h"
6+
7+
NS_ASSUME_NONNULL_BEGIN
8+
/*!
9+
@brief Sanitizer Initialization Configuration
10+
*/
11+
@interface ODWSanitizerInitConfig : NSObject
12+
13+
/*!
14+
@brief (OPTIONAL) Custom event name to use when logging concerns from the sanitizer. Default value is `SanitizerConcerns`.
15+
*/
16+
@property(readwrite, copy, nonatomic) NSString* notificationEventName;
17+
18+
@end
19+
NS_ASSUME_NONNULL_END
20+
21+
#include "objc_end.h"
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//
2+
// Copyright (c) Microsoft Corporation. All rights reserved.
3+
// SPDX-License-Identifier: Apache-2.0
4+
//
5+
#import <Foundation/Foundation.h>
6+
#import "ODWSanitizerInitConfig.h"
7+
8+
/*!
9+
@brief Sanitizer Initialization Configuration
10+
*/
11+
@implementation ODWSanitizerInitConfig : NSObject
12+
13+
@end
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//
2+
// Copyright (c) Microsoft Corporation. All rights reserved.
3+
// SPDX-License-Identifier: Apache-2.0
4+
//
5+
#include "objc_begin.h"
6+
#include "ILogger.hpp"
7+
#import "ODWSanitizer.h"
8+
#import "ODWSanitizerInitConfig.h"
9+
10+
NS_ASSUME_NONNULL_BEGIN
11+
12+
/*!
13+
The <b>ODWSanitizer</b> class represents the sanitizer.
14+
*/
15+
@interface ODWSanitizer (Private)
16+
17+
#pragma mark Initialization methods
18+
19+
/*!
20+
@brief Initializes the sanitizer
21+
@param logger Logger used for reporting concerns
22+
@param initConfigObject the configuration
23+
*/
24+
+(void)initializeSanitizer:(ILogger *)logger withODWSanitizerInitConfig:(ODWSanitizerInitConfig *)initConfigObject;
25+
@end
26+
27+
NS_ASSUME_NONNULL_END
28+
#include "objc_end.h"

wrappers/obj-c/OneDsCppSdk.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#import "ODWLogger.h"
1313
#import "ODWLogManager.h"
1414
#import "ODWPrivacyGuard.h"
15+
#import "ODWSanitizer.h"
1516
#import "ODWSemanticContext.h"
1617

1718
#endif

wrappers/swift/Headers/ObjCModule-Bridging-Header.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,6 @@
1414
#import "../../obj-c/ODWLogManager.h"
1515
#import "../../obj-c/ODWPrivacyGuard.h"
1616
#import "../../obj-c/ODWPrivacyGuardInitConfig.h"
17+
#import "../../obj-c/ODWSanitizer.h"
18+
#import "../../obj-c/ODWSanitizerInitConfig.h"
1719
#import "../../obj-c/ODWSemanticContext.h"
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//
2+
// Copyright (c) Microsoft Corporation. All rights reserved.
3+
// SPDX-License-Identifier: Apache-2.0
4+
//
5+
6+
import ObjCModule
7+
8+
/// Wrapper to `ODWSanitizer` representing the Sanitizer.
9+
public final class Sanitizer {
10+
11+
/**
12+
Set Sanitizer enabled.
13+
14+
- Parameters:
15+
- enable: `True` to enable the Sanitizer, `False` otherwise.
16+
*/
17+
public static func setEnabled(_ enable: Bool) {
18+
ODWSanitizer.setEnabled(enable)
19+
}
20+
21+
/// Checks if the Sanitizer is enabled, returns `True` if it is, `False` otherwise.
22+
public static func enabled() -> Bool {
23+
return ODWSanitizer.enabled()
24+
}
25+
26+
/// Resets the Sanitizer instance.
27+
public static func resetPrivacyGuardInstance() {
28+
ODWSanitizer.resetSanitizerInstance()
29+
}
30+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//
2+
// Copyright (c) Microsoft Corporation. All rights reserved.
3+
// SPDX-License-Identifier: Apache-2.0
4+
//
5+
6+
import ObjCModule
7+
8+
public final class SanitizerInitConfig {
9+
let odwSanitizerInitConfig: ODWSanitizerInitConfig
10+
11+
/// Default constructor.
12+
public init() {
13+
odwSanitizerInitConfig = ODWSanitizerInitConfig()
14+
}
15+
16+
/// (OPTIONAL) Custom event name to use when logging concerns from the sanitizer. Default value is `SanitizerConcerns`.
17+
public var notificationEventName: String {
18+
get {
19+
odwSanitizerInitConfig.notificationEventName
20+
}
21+
set {
22+
odwSanitizerInitConfig.notificationEventName = newValue
23+
}
24+
}
25+
26+
/**
27+
Returns the Obj-C object of the wrapper.
28+
29+
- Return:
30+
`ODWSanitizerInitConfig` object which class is wrapped around.
31+
*/
32+
func getODWSanitizerInitConfig() -> ODWSanitizerInitConfig {
33+
return odwSanitizerInitConfig
34+
}
35+
}

0 commit comments

Comments
 (0)