1# Subscribing to Address Sanitizer Events (ArkTS) 2 3## Available APIs 4 5For details about how to use the APIs, see [@ohos.hiviewdfx.hiAppEvent (Application Event Logging)](../reference/apis-performance-analysis-kit/js-apis-hiviewdfx-hiappevent.md). 6 7| API| **Description**| 8| -------- | -------- | 9| addWatcher(watcher: Watcher): AppEventPackageHolder | Adds a watcher to subscribe to application events.| 10| removeWatcher(watcher: Watcher): void | Removes a watcher to unsubscribe from application events.| 11 12## **How to Develop** 13 14The following describes how to subscribe an address sanitizer event for an array bounds write. 15 16### Step 1: Creating a Project 17 181. Create a native C++ project. The directory structure is as follows: 19 20 ```yml 21 entry: 22 src: 23 main: 24 cpp: 25 - types: 26 libentry: 27 - index.d.ts 28 - CMakeLists.txt 29 - napi_init.cpp 30 ets: 31 - entryability: 32 - EntryAbility.ets 33 - pages: 34 - Index.ets 35 ``` 36 372. In the **entry/src/main/ets/entryability/EntryAbility.ets** file of the project, import the dependent modules. 38 39 ```ts 40 import { hiAppEvent, hilog } from '@kit.PerformanceAnalysisKit'; 41 ``` 42 43### Step 2: Subscribing to Address Sanitizer Events 44 451. In the **entry/src/main/ets/entryability/EntryAbility.ets** file of the project, add a watcher in **onCreate()** to subscribe to system events. The sample code is as follows: 46 47 ```ts 48 hiAppEvent.addWatcher({ 49 // Set the watcher name. The system identifies different watchers based on their names. 50 name: "watcher", 51 // You can subscribe to system events that you are interested in. For example, the address sanitizer event. 52 appEventFilters: [ 53 54 domain: hiAppEvent.domain.OS, 55 names: [hiAppEvent.event.ADDRESS_SANITIZER] 56 } 57 ], 58 // Implement a callback for the registered system event so that you can apply custom processing to the event data obtained. 59 onReceive: (domain: string, appEventGroups: Array<hiAppEvent.AppEventGroup>) => { 60 hilog.info(0x0000, 'testTag', `HiAppEvent onReceive: domain=${domain}`); 61 for (const eventGroup of appEventGroups) { 62 // The event name uniquely identifies a system event. 63 hilog.info(0x0000, 'testTag', `HiAppEvent eventName=${eventGroup.name}`); 64 for (const eventInfo of eventGroup.appEventInfos) { 65 // Customize how to process the event data obtained, for example, print the event data in the log. 66 hilog.info(0x0000, 'testTag', `HiAppEvent eventInfo.domain=${eventInfo.domain}`); 67 hilog.info(0x0000, 'testTag', `HiAppEvent eventInfo.name=${eventInfo.name}`); 68 hilog.info(0x0000, 'testTag', `HiAppEvent eventInfo.eventType=${eventInfo.eventType}`); 69 hilog.info(0x0000, 'testTag', `HiAppEvent eventInfo.time=${eventInfo.params['time']}`); 70 hilog.info(0x0000, 'testTag', `HiAppEvent eventInfo.bundle_version=${eventInfo.params['bundle_version']}`); 71 hilog.info(0x0000, 'testTag', `HiAppEvent eventInfo.bundle_name=${eventInfo.params['bundle_name']}`); 72 hilog.info(0x0000, 'testTag', `HiAppEvent eventInfo.pid=${eventInfo.params['pid']}`); 73 hilog.info(0x0000, 'testTag', `HiAppEvent eventInfo.uid=${eventInfo.params['uid']}`); 74 hilog.info(0x0000, 'testTag', `HiAppEvent eventInfo.type=${eventInfo.params['type']}`); 75 hilog.info(0x0000, 'testTag', `HiAppEvent eventInfo.external_log=${JSON.stringify(eventInfo.params['external_log'])}`); 76 hilog.info(0x0000, 'testTag', `HiAppEvent eventInfo.log_over_limit=${eventInfo.params['log_over_limit']}`); 77 } 78 } 79 } 80 }); 81 ``` 82 83### Step 3: Constructing an Address Sanitizer Error 84 851. In the **entry/src/main/cpp/napi_init.cpp** file, implement the array bounds write scenario and provide Node-API for the application layer code to call. The sample code is as follows: 86 87 ```c++ 88 #include "napi/native_api.h" 89 90 static napi_value Test(napi_env env, napi_callback_info info) 91 { 92 int a[10]; 93 // Construct the array bounds write. 94 a[10] = 1; 95 return {}; 96 } 97 98 EXTERN_C_START 99 static napi_value Init(napi_env env, napi_value exports) 100 { 101 napi_property_descriptor desc[] = { 102 { "test", nullptr, Test, nullptr, nullptr, nullptr, napi_default, nullptr } 103 }; 104 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); 105 return exports; 106 } 107 EXTERN_C_END 108 109 static napi_module demoModule = { 110 .nm_version = 1, 111 .nm_flags = 0, 112 .nm_filename = nullptr, 113 .nm_register_func = Init, 114 .nm_modname = "entry", 115 .nm_priv = ((void*)0), 116 .reserved = { 0 } 117 }; 118 119 extern "C" __attribute__((constructor)) void RegisterEntryModule(void) 120 { 121 napi_module_register(&demoModule); 122 } 123 ``` 124 1252. Edit the **entry/src/main/cpp/types/libentry/index.d.ts** file. The sample code is as follows: 126 127 ```ts 128 export const test: () => void; 129 ``` 130 1313. In the **entry > src > main > ets > pages > Index.ets** file, add a button to trigger an address sanitizer event. 132 133 ```ts 134 import testNapi from 'libentry.so'; 135 136 @Entry 137 @Component 138 struct Index { 139 build() { 140 Row() { 141 Column() { 142 Button("address-sanitizer").onClick(() => { 143 testNapi.test(); 144 }) 145 } 146 .width('100%') 147 } 148 .height('100%') 149 } 150 } 151 ``` 152 1534. In DevEco Studio, choose **entry**, click **Edit Configurations**, click **Diagnostics**, select **Address Sanitizer**, and click **OK**. Click the **Run** button to run the project. Then, click the **address-sanitizer** button to trigger an address sanitizer event. The application crashes. After restarting the application, you can view the following event information in the **Log** window. 154 155 ```text 156 HiAppEvent onReceive: domain=OS 157 HiAppEvent eventName=ADDRESS_SANITIZER 158 HiAppEvent eventInfo.domain=OS 159 HiAppEvent eventInfo.name=ADDRESS_SANITIZER 160 HiAppEvent eventInfo.eventType=1 161 HiAppEvent eventInfo.time=1713161197957 162 HiAppEvent eventInfo.bundle_version=1.0.0 163 HiAppEvent eventInfo.bundle_name=com.example.myapplication 164 HiAppEvent eventInfo.pid=12889 165 HiAppEvent eventInfo.uid=20020140 166 HiAppEvent eventInfo.type=stack-buffer-overflow 167 HiAppEvent eventInfo.external_log=["/data/storage/el2/log/hiappevent/ADDRESS_SANITIZER_1713161197960_12889.log"] 168 HiAppEvent eventInfo.log_over_limit=false 169 ``` 170