This topic describes how to analyze and fix CppCrash issues by listing common cases. Before reading this document, you are advised to read Detecting App Crash Issues to understand the principles and mechanisms for the system to detect CppCrash issues, and CppCrash Issue Analysis to understand the general procedure for analyzing CppCrash issues.
In addition to this document, you can also refer to the following documents to analyze API-related CppCrash issues:
Common UI-Related Application Crash Issues
Smart pointers are used without null checks, causing null pointer dereference crashes during process execution.
The process crashes and cannot run stably, resulting in an unexpected exit.
Timestamp:1970-12-07 10:27:48.228 Pid:26984 Uid:20010045 Process name:xxx Reason:Signal:SIGSEGV(SEGV_MAPERR)@0000000000000004 probably caused by NULL pointer dereference Fault thread Info: Tid:27270, Name:xxx #00 pc 0000000000022770 /system/lib64/libapp context.z.so(OHOS::AbilityRuntime::ApplicationContext::DispatchWindowStageFocus (std::__l::shared ptr<NativeReference> consts, std::__l::shared_ptr<NativeReference> consts)+152) #01 pc 000000000014ea58 /system/lib64/libabilitykit_native.z.so(OHOS::AppExecFwk::AbilityImpl::AfterFocused()+560) #02 pc 000000000014f8ec /system/lib64/libabilitykit_native.z.so(OHOS::AppExecFwk::AbilityImpl::WindowLifeCycleImpl::AfterFocused()+92) #03 pc 000000000006acb8 /system/lib64/libwm.z.so #04 pc 000000000000e680 /system/lib64/libeventhandler.z.so(OHOS::AppExecFwk::EventHandler::DistributeEvent (std::__l::unique_ptr<OHOS::AppExecFwk::InnerEvent, void (*)(OHOS::AppExecFwk::InnerEvent*)> const&)+808) #05 pc 00000000000174cc /system/lib64/libeventhandler.z.so #06 pc 0000000000015c30 /system/lib64/libeventhandler.z.so #07 pc 0000000000018698 /system/lib64/libeventhandler.z.so #08 pc 00000000000d02a0 /system/lib64/libc.so(__pthread_start(void*)+40) #09 pc 0000000000072128 /system/lib64/libc.so(__start_thread+68)
Null pointer crashes can be identified based on the fault cause. Run the llvm-addr2line command to parse the line number. It is found that the service code does not check whether the smart pointer is null before using it. As a result, the service code accesses the null address, causing the crash.
Add protective null checks for the pointer.
Pointers should be null-checked before using it to prevent null pointers and process crashes and exits.
napi_env is still used after being released.
The key crash stack is as follows.

The env (in JavaScript environment) of the Node-API points to an invalid memory, and the crash stack is mounted to NativeEngineInterface::ClearLastError(). By using profiling loggings and analyzing service processes before the crash event based on HiLog logs, the address of the env (which is 0000007F01338CC0) is located and it is found that the env is still used after being released.
The IPC creates a remote object in thread 1 and saves the env of the current thread. 07-21 22:42:32.108 3952 4030 I CO1510/napi_remoteObject_holer:27:[crashtest],NAPIRemoteObjectHolder create, env:0000007F01338CC0, holder:0000007F01331E00, descriptor:connect-test 07-21 22:42:32.109 3952 4030 I CO1510/napi_remoteObject:161:[crashtest] NAPIRemoteObject create, desc:connect-test,this:0000007F01329F60,env:0000007F01338CC0 07-21 22:42:32.142 3952 4030 I CO1510/napi_remoteObject_holer:32:[crashtest],NAPIRemoteObjectHolder release, env:0000007F01338CC0, holder:0000007F01331E00 The engine (env) of thread 1 is destructed. 07-21 22:42:32.143 3952 4030 I C03900/NAPI:[(ark_native_engine.cpp:64)(~ArkNativeEngine)] ArkNativeEngine:~ArkNativeEngine IPC uses the previously saved env in thread 2, causing a crash. 07-21 22:42:43.034 3952 3997 I C01510/napi_remoteObject: 171:[crashtest] NAPIRemoteObject Destructor, this:0000007F01329F60,env:0000007F01338CC0
JavaScript is executed by a single thread. Therefore, any operation on env must be performed on the original thread that creates the JavaScript thread. Otherwise, unexpected problems may occur.
Do not transfer the env created by a thread to other threads.
If the stack top often crashes when env operations are performed in libraries such as libace_napi.z.so and libark_jsruntime.so, and the crash cause cannot be directly analyzed based on the call stack of the C ++ crash logs, you can enable multi-thread check to quickly locate the fault. In addition, when the STL container (such as vector, map, and set) is added or deleted by multiple threads, the SIGSEGV fault is likely to occur because the STL container is not thread-safe. Therefore, in scenarios where multiple threads perform operations on the STL container, the fault may be caused by multi-thread competition.
The crash address is in the readable and executable segment of libace_napi_ark.z.so. The cause is that the address needs to be written, but the corresponding maps segment has only the read and execute permissions. In other words, when a process attempts to access a memory area that is not allowed to be accessed, the process crashes.
7f82740000-7f8275c000 r--p 00000000 /system/lib64/libace_napi_ark.z.so 7f8275c000-7f8276e000 r-xp 0001b000 /system/lib64/libace_napi_ark.z.so <- The crash address locates within this address range. 7f8276e000-7f82773000 r--p 0002c000 /system/lib64/libace_napi_ark.z.so 7f82773000-7f82774000 rw-p 00030000 /system/lib64/libace_napi_ark.z.so
The following figure shows the crash call stack.

According to the service logic, the node should be stored in the heap and its address cannot be in the code segment of libace_napi_ark.z.so. Therefore, there is a high probability that the problem is caused by memory corruption. You can use the HWASan: Detecting Memory Errors to locate the memory corruption error. To find the regular crash scenario, you can use ASan to reproduce the problem. The result shows that the fault detected by ASan is the same as that in the crash stack. The ASan log shows that the memory corruption type is heap-use-after-free. Based on the log information, find out the process from memory application, memory release, to use of the released memory. After analysis, it can be determined that the service code repeatedly releases the same address (0x003a375eb724) and uses this address to access object members. As a result, the use-after-free issue is reported.
The core ASan logs are as follows:
=================================================================
==appspawn==2029==ERROR: AddressSanitizer: heap-use-after-free on address 0x003a375eb724 at pc 0x002029ba8514 bp 0x007fd8175710 sp 0x007fd8175708
READ of size 1 at 0x003a375eb724 thread T0 (thread name) <- The released memory is used.
# 0 0x2029ba8510 (/system/asan/lib64/platformsdk/libark_jsruntime.so+0xca8510) panda::ecmascript::Node::IsUsing() const at arkcompiler/ets_runtime/ecmascript/ecma_global_storage.h:82:16
(inlined by) panda::JSNApi::DisposeGlobalHandleAddr(panda::ecmascript::EcmaVM const*, unsigned long) at arkcompiler/ets_runtime/ecmascript/napi/jsnapi.cpp:749:67 BuildID[md5/uuid]=9a18e2ec0dc8a83216800b2f0dd7b76a
# 1 0x403ee94d30 (/system/asan/lib64/libace.z.so+0x6194d30) panda::CopyableGlobal<panda::ObjectRef>::Free() at arkcompiler/ets_runtime/ecmascript/napi/include/jsnapi.h:1520:9
(inlined by) panda::CopyableGlobal<panda::ObjectRef>::Reset() at arkcompiler/ets_runtime/ecmascript/napi/include/jsnapi.h:189:9
(inlined by) OHOS::Ace::Framework::JsiType<panda::ObjectRef>::Reset() at foundation/arkui/ace_engine/frameworks/bridge/declarative_frontend/engine/jsi/jsi_types.inl:112:13
(inlined by) OHOS::Ace::Framework::JsiWeak<OHOS::Ace::Framework::JsiObject>::~JsiWeak() at foundation/arkui/ace_engine/frameworks/bridge/declarative_frontend/engine/jsi/jsi_ref.h:167:16
(inlined by) OHOS::Ace::Framework::ViewFunctions::~ViewFunctions() at foundation/arkui/ace_engine/frameworks/bridge/declarative_frontend/jsview/js_view_functions.h:44:5 BuildID[md5/uuid]=1330f8b9be73bdb76ae18107c2a60ca1
# 2 0x403ee9296c (/system/asan/lib64/libace.z.so+0x619296c) OHOS::Ace::Framework::ViewFunctions::~ViewFunctions() at foundation/arkui/ace_engine/frameworks/bridge/declarative_frontend/jsview/js_view_functions.h:42:5
(inlined by) OHOS::Ace::Framework::ViewFunctions::~ViewFunctions() at foundation/arkui/ace_engine/frameworks/bridge/declarative_frontend/jsview/js_view_functions.h:42:5 BuildID[md5/uuid]=1330f8b9be73bdb76ae18107c2a60ca1
# 3 0x403ed9b130 (/system/asan/lib64/libace.z.so+0x609b130) OHOS::Ace::Referenced::DecRefCount() at foundation/arkui/ace_engine/frameworks/base/memory/referenced.h:76:13
(inlined by) OHOS::Ace::RefPtr<OHOS::Ace::Framework::ViewFunctions>::~RefPtr() at foundation/arkui/ace_engine/frameworks/base/memory/referenced.h:148:22 BuildID[md5/uuid]=1330f8b9be73bdb76ae18107c2a60ca1
# 4 0x403ed9b838 (/system/asan/lib64/libace.z.so+0x609b838) OHOS::Ace::RefPtr<OHOS::Ace::Framework::ViewFunctions>::Reset() at foundation/arkui/ace_engine/frameworks/base/memory/referenced.h:163:9
(inlined by) OHOS::Ace::Framework::JSViewFullUpdate::~JSViewFullUpdate() at foundation/arkui/ace_engine/frameworks/bridge/declarative_frontend/jsview/js_view.cpp:159:21 BuildID[md5/uuid]=1330f8b9be73bdb76ae18107c2a60ca1
# 5 0x403ed9bf24 (/system/asan/lib64/libace.z.so+0x609bf24) OHOS::Ace::Framework::JSViewFullUpdate::~JSViewFullUpdate() at foundation/arkui/ace_engine/frameworks/bridge/declarative_frontend/jsview/js_view.cpp:157:1
(inlined by) OHOS::Ace::Framework::JSViewFullUpdate::~JSViewFullUpdate() at foundation/arkui/ace_engine/frameworks/bridge/declarative_frontend/jsview/js_view.cpp:157:1 BuildID[md5/uuid]=1330f8b9be73bdb76ae18107c2a60ca1
...
freed by thread T0 (thread name) here: <- The memory is released.
# 0 0x2024ed3abc (/system/asan/lib64/libclang_rt.asan.so+0xd3abc)
# 1 0x2029ba8424 (/system/asan/lib64/platformsdk/libark_jsruntime.so+0xca8424) std::__h::__function::__value_func<void (unsigned long)>::operator()[abi:v15004](unsigned long&&) const at prebuilts/clang/ohos/linux-x86_64/llvm/bin/../include/libcxx-ohos/include/c++/v1/__functional/function.h:512:16
(inlined by) std::__h::function<void (unsigned long)>::operator()(unsigned long) const at prebuilts/clang/ohos/linux-x86_64/llvm/bin/../include/libcxx-ohos/include/c++/v1/__functional/function.h:1197:12
(inlined by) panda::ecmascript::JSThread::DisposeGlobalHandle(unsigned long) at arkcompiler/ets_runtime/ecmascript/js_thread.h:604:9
(inlined by) panda::JSNApi::DisposeGlobalHandleAddr(panda::ecmascript::EcmaVM const*, unsigned long) at arkcompiler/ets_runtime/ecmascript/napi/jsnapi.cpp:752:24 BuildID[md5/uuid]=9a18e2ec0dc8a83216800b2f0dd7b76a
# 2 0x403ee94b68 (/system/asan/lib64/libace.z.so+0x6194b68) panda::CopyableGlobal<panda::FunctionRef>::Free() at arkcompiler/ets_runtime/ecmascript/napi/include/jsnapi.h:1520:9
(inlined by) panda::CopyableGlobal<panda::FunctionRef>::Reset() at arkcompiler/ets_runtime/ecmascript/napi/include/jsnapi.h:189:9
(inlined by) OHOS::Ace::Framework::JsiType<panda::FunctionRef>::Reset() at foundation/arkui/ace_engine/frameworks/bridge/declarative_frontend/engine/jsi/jsi_types.inl:112:13
(inlined by) OHOS::Ace::Framework::JsiWeak<OHOS::Ace::Framework::JsiFunction>::~JsiWeak() at foundation/arkui/ace_engine/frameworks/bridge/declarative_frontend/engine/jsi/jsi_ref.h:167:16
(inlined by) OHOS::Ace::Framework::ViewFunctions::~ViewFunctions() at foundation/arkui/ace_engine/frameworks/bridge/declarative_frontend/jsview/js_view_functions.h:44:5 BuildID[md5/uuid]=1330f8b9be73bdb76ae18107c2a60ca1
# 3 0x403ee9296c (/system/asan/lib64/libace.z.so+0x619296c) OHOS::Ace::Framework::ViewFunctions::~ViewFunctions() at foundation/arkui/ace_engine/frameworks/bridge/declarative_frontend/jsview/js_view_functions.h:42:5
(inlined by) OHOS::Ace::Framework::ViewFunctions::~ViewFunctions() at foundation/arkui/ace_engine/frameworks/bridge/declarative_frontend/jsview/js_view_functions.h:42:5 BuildID[md5/uuid]=1330f8b9be73bdb76ae18107c2a60ca1
# 4 0x403ed9b130 (/system/asan/lib64/libace.z.so+0x609b130) OHOS::Ace::Referenced::DecRefCount() at foundation/arkui/ace_engine/frameworks/base/memory/referenced.h:76:13
(inlined by) OHOS::Ace::RefPtr<OHOS::Ace::Framework::ViewFunctions>::~RefPtr() at foundation/arkui/ace_engine/frameworks/base/memory/referenced.h:148:22 BuildID[md5/uuid]=1330f8b9be73bdb76ae18107c2a60ca1
...
previously allocated by thread T0 (thread name) here: <- The memory is allocated.
# 0 0x2024ed3be4 (/system/asan/lib64/libclang_rt.asan.so+0xd3be4)
# 1 0x2029ade778 (/system/asan/lib64/platformsdk/libark_jsruntime.so+0xbde778) panda::ecmascript::NativeAreaAllocator::AllocateBuffer(unsigned long) at arkcompiler/ets_runtime/ecmascript/mem/native_area_allocator.cpp:98:17 BuildID[md5/uuid]=9a18e2ec0dc8a83216800b2f0dd7b76a
# 2 0x2029a39064 (/system/asan/lib64/platformsdk/libark_jsruntime.so+0xb39064) std::__h::enable_if<!std::is_array_v<panda::ecmascript::NodeList<panda::ecmascript::WeakNode>>, panda::ecmascript::NodeList<panda::ecmascript::WeakNode>*>::type panda::ecmascript::NativeAreaAllocator::New<panda::ecmascript::NodeList<panda::ecmascript::WeakNode>>() at arkcompiler/ets_runtime/ecmascript/mem/native_area_allocator.h:61:19
(inlined by) unsigned long panda::ecmascript::EcmaGlobalStorage<panda::ecmascript::Node>::NewGlobalHandleImplement<panda::ecmascript::WeakNode>(panda::ecmascript::NodeList<panda::ecmascript::WeakNode>**, panda::ecmascript::NodeList<panda::ecmascript::WeakNode>**, unsigned long) at arkcompiler/ets_runtime/ecmascript/ecma_global_storage.h:565:34
(inlined by) panda::ecmascript::EcmaGlobalStorage<panda::ecmascript::Node>::SetWeak(unsigned long, void*, void (*)(void*), void (*)(void*)) at arkcompiler/ets_runtime/ecmascript/ecma_global_storage.h:455:26 BuildID[md5/uuid]=9a18e2ec0dc8a83216800b2f0dd7b76a
# 3 0x2029ba5620 (/system/asan/lib64/platformsdk/libark_jsruntime.so+0xca5620) std::__h::__function::__value_func<unsigned long (unsigned long, void*, void (*)(void*), void (*)(void*))>::operator()[abi:v15004](unsigned long&&, void*&&, void (*&&)(void*), void (*&&)(void*)) const at prebuilts/clang/ohos/linux-x86_64/llvm/bin/../include/libcxx-ohos/include/c++/v1/__functional/function.h:512:16
(inlined by) std::__h::function<unsigned long (unsigned long, void*, void (*)(void*), void (*)(void*))>::operator()(unsigned long, void*, void (*)(void*), void (*)(void*)) const at prebuilts/clang/ohos/linux-x86_64/llvm/bin/../include/libcxx-ohos/include/c++/v1/__functional/function.h:1197:12
(inlined by) panda::ecmascript::JSThread::SetWeak(unsigned long, void*, void (*)(void*), void (*)(void*)) at arkcompiler/ets_runtime/ecmascript/js_thread.h:610:16
(inlined by) panda::JSNApi::SetWeak(panda::ecmascript::EcmaVM const*, unsigned long) at arkcompiler/ets_runtime/ecmascript/napi/jsnapi.cpp:711:31 BuildID[md5/uuid]=9a18e2ec0dc8a83216800b2f0dd7b76a
... The call stack shows that when JsiWeak is destructed or reset, CopyableGlobal in the parent class JsiType of its member (JsiObject/JsiValue/JsiFunction) is released, as shown in the following figure.

During Garbage Collection (GC), IterateWeakEcmaGlobalStorage calls DisposeGlobalHandle to release WeakNodes without callbacks, as shown in the following figure.

Therefore, for the same WeakNode, there may be two ways for release. Since no callback is sent to JsiWeak for clearing the released WeakNode, JsiWeak still stores a reference to the released WeakNode, that is, CopyableGlobal. In this case, when the NodeList containing the WeakNode is released and the CopyableGlobal reserved in JsiWeak is released, the memory is repeatedly released.

Set a callback for JsiWeak using SetWeakCallback. Therefore, the callback can notify JsiWeak to reset CopyableGlobal when IterateWeakEcmaGlobalStorage releases the WeakNode, ensuring the same address is not repeatedly released.
When using memory, consider whether the memory is repeatedly released or not released. Additionally, when locating memory access crashes (usually SIGSEGV crashes), use HWASan to reproduce the fault if there is no clue based on the call stack analysis.
Lifecycle issues occur when the memory of an object is accessed beyond its lifecycle. Generally, lifecycle issues are caused by improper use of raw pointers. A raw pointer does not have encapsulation or automatic memory management features. It is a simple pointer that points to a memory address without protecting or managing the memory. A raw pointer can directly access the memory, but it may cause memory leaks and null pointer references. To avoid potential security problems, use raw pointers with caution. You are advised to use smart pointers to manage memory.
When you create the napi_value, you need to use it with napi_handle_scope. napi_handle_scope is used to manage the lifecycle of napi_value. napi_value can be used only within napi_handle_scope, otherwise, the lifecycle of napi_value and its JS objects is no longer protected. If the reference count is 0, napi_value is collected by GC. If you use napi_value at this time, you may access released memory and cause problems.
napi_value is a raw pointer (a struct pointer). It is used to hold JS objects and maintain the lifecycle of JS objects to ensure that JS objects are not collected by GC. When napi_value is used outside napi_handle_scope, napi_value is collected by GC, and it does not hold the JS object (the lifecycle of the JS object is not protected).
Based on the call stack, locate the upper-layer API of the faulty Node-API, find the problematic napi_value, and check whether it is used outside napi_handle_scope.
napi_value is used outside napi_handle_scope in the following example.
napi_value g_Values;
// By default, the Add method is within the scope of napi_handle_scope. After the method is executed, the scope is closed.
static napi_value Add(napi_env env, napi_callback_info info)
{
size_t argc = 3;
napi_value args[3] = {nullptr};
napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
napi_valuetype valuetype0;
napi_typeof(env, args[0], &valuetype0);
std::vector<napi_value> dataList;
for (int i = 0; i < argc; i++) {
napi_value data = args[i];
dataList.push_back(data);
}
void* buffer = nullptr;
napi_value arrayBuffer;
napi_create_arraybuffer(env, dataList.size(), &buffer, &arrayBuffer);
memcpy(buffer, dataList.data(), dataList.size());
napi_value typedArray;
napi_create_typedarray(
env, napi_int8_array, dataList.size(), arrayBuffer, 0, &typedArray
);
g_Values = typedArray; // After the Add method is executed, the memory of typedArray is reclaimed because it leaves the scope.
return nullptr;
}
static napi_value Get(napi_env env, napi_callback_info info)
{
return g_Values; // The reclaimed memory is still in use, causing a crash.
} On the JS side, data is added using the Add(), and on the native side, napi_value is saved to a vector. On the JS side, data is obtained using the get API, and on the native side, the saved napi_value is returned as an array. The JS side then reads the properties of the data. The error message "Can not get Prototype on non ECMA Object" is displayed. The cross-NAPI napi_value is not saved using napi_ref. As a result, the napi_value is invalid.
You can manage the lifecycle of napi_value using napi_handle_scope, which starts before the native method is called and ends after the native method is executed. For details, see Performing Lifecycle Management Using Node-API.
SIGABRT crashes usually occur when the process calls the abort function of the standard function library. They are triggered when the program detects exceptions, such as thread creation failure or file descriptor usage exceptions, particularly when basic libraries (such as the C library) perform verifications.
static napi_value TriggerCrash(napi_env env, napi_callback_info info)
{
OH_LOG_FATAL("LOG_APP", "test fatal log.");
abort();
return 0;
}
This example constructs a scenario where the abort function is proactively called to describe how to analyze the SIGABRT crash. As shown in the preceding figure, LastFatalMessage provides the cause of the abnormal exits for the SIGABRT crash, which helps to locate the fault. Skip the call stack of the C library, find the call stack (#02 call stack in the figure) that calls the abort function, and analyze the call stack based on LastFatalMessage.
In C++, the assert function is also used for exception handling. When the assert function is called to verify data during the execution of the current function and the verification fails, the process exits automatically. In this case, the method for analyzing the problem is the same as that for the abort function.
static napi_value TriggerCrash(napi_env env, napi_callback_info info)
{
#if 0 //If the value is 0, an error will be reported. If it is 1, it is normal
void *pc = malloc(1024);
#else
void *pc = nullptr;
#endif
assert(pc != nullptr);
return 0;
} 
Using llvm-objdump
llvm-objdump is a disassembler provided by the system. It is archived in [SDK DIR PATH]/default/HarmonyOS/native/llvm/bin/llvm-objdump.exe. You can run the following command to use it:
llvm-objdump.exe -d -l libark_jsruntime.so > dump.txt
This command is used to export the full assembly instructions of libark_jsruntime.so to the dump.txt file.
Cases
The core CppCrash logs are as follows:
Process name:xxx Process life time:13402s Process Memory(kB): 11902(Rss) Device Memory(kB): Total 1935820, Free 516244, Available 1205608 Reason:SIGSEGV(SEGV_MAPERR)@0x0000005b3b46c000 Fault thread info: Tid:48552, Name:xxx #00 pc 00000000000a87e4 /system/lib/ld-musl-aarch64.so.1(memcpy+356)(3c3e7fb27680dc2ee99aa08dd0f81e85) ...
Analysis process:
Based on the PC register address, find the corresponding assembly instruction and determine the instruction that crashes when it is executed.
In the CppCrash log file, find the PC address at the stack top and disassemble the corresponding binary codes.
For example, if a crash occurs when the instruction corresponding to the address 00000000000a87e4 is executed, disassemble the 0xa87e4 offset address in the ld-musl-aarch64.so.1 file to display the instruction information.
xxx/../../third_party/optimized-routines/string/aarch64/memcpy.S:175 <- Line number of the source code a87e4: a94371aa ldp x10, x11, [x1, #48] Address: Value Assembly instruction
ldp indicates an LDP instruction, which is used to load two 64-bit data from the memory to two different registers at the same time.
ldp x10, x11, [x1, #48] LDP Target register 1 Target register 2 <source address>
This instruction reads two consecutive 64-bit data from the specified position (determined by the address in register x1 plus a 48-byte offset) in the memory and store the data to registers x10 and x11.
Check the code of the memcpy.S source file corresponding to line 175:
L(loop64): line 170 stp A_l, A_h, [dst, 16] line 171 ldp A_l, A_h, [src, 16] line 172 stp B_l, B_h, [dst, 32] line 173 ldp B_l, B_h, [src, 32] line 174 stp C_l, C_h, [dst, 48] line 175 ldp C_l, C_h, [src, 48] <- The crash instruction line 176 stp D_l, D_h, [dst, 64] line 177 ldp D_l, D_h, [src, 64] line 178 subs count, count, 64 line 179 b.hi L(loop64)
Based on the register value and context, determine which object causes the fault.
For a non-class member function, the x0 register loads the first parameter of the function, x1 loads the second parameter, x2 loads the third parameter, and so on. For a class member function, x0 loads the pointer to the class instance object, and x1, x2, and x3 load parameters. Note that if the number of function parameters exceeds 5, the parameters are pushed into the stack. For the parameters of void* memcpy(void* restrict dest, void* restrict src, size_t n) in the stack top, x0 loads dest (destination address), x1 loads src (source address), and x2 loads n (number of copied bytes).
Register: x0:000005b50c3e3c4 x1:000005b3b46bfcc x2:0000000000007e88 x3:000005b50c42380 ...
Based on the three register values in CppCrash logs and the crash address 0x0000005b3b46c000, it is determined that the faulty parameter is the second parameter (source address) of the memcpy function.
Determine the fault type based on the memory features near the crash address.
Check Memory near registers in the CppCrash logs:
x1(xxxx):
0000005b3b46bfbc 3e99fedbc0a9b5e9
0000005b3b46bfc4 a91ab9d327969682
0000005b3b46bfcc 83906d9c18cdb9c1
0000005b3b46bfd4 627dd75ab9335eb0
0000005b3b46bfdc aabe2bb1b00f2c03
0000005b3b46bfe4 f981e4acb716cbc1
0000005b3b46bfec 806b3d5730d281ee
0000005b3b46bff4 3e99fedbc0a9b5e9
0000005b3b46bffc ffffffffffffffff -> The memory value is ffffffffffffffff, indicating that address 0000005b3b46bffc is invalid and out-of-bounds read occurs.
0000005b3b46c004 ffffffffffffffff
0000005b3b46c00c ffffffffffffffff
0000005b3b46c014 ffffffffffffffff
0000005b3b46c01c ffffffffffffffff
0000005b3b46c024 ffffffffffffffff
0000005b3b46c02c ffffffffffffffff
0000005b3b46c034 ffffffffffffffff
... According to the log, an out-of-bound read problem occurs. You only need to analyze the input parameters src (pointer to the source memory) and n (number of copied bytes) of memcpy.
Track the parameter source of the problematic object and locate the problem based on the code and HiLog logs.
Check whether the parameter object is valid and whether the value range is valid. For example, check whether the size of the source memory is the same as the value of n.
Check whether the lifecycle of the parameter object is valid. For example, check whether the source memory has been released and whether there are multi-thread operation objects.
Check the improper operation logic of parameters based on the function context. For example, trace the operation logic of buf and bufsize, add debugging information, and locate the improper operation logic. The context segment is as follows:
static int xxxFunc(const uint8_t *buf, uint32_t bufSize)
{
// ...
uint32_t srcOffset = appendOffset - bufSize;
auto ret = memcpy(cache + srcOffset, buf, bufSize);
if (ret == nullptr) {
return -1;
}
// ...
} By continuously tracing the sources of buf and bufSize, it is found that the value of bufSize is greater than that of buf after continuous copy. As a result, an out-of-bounds read problem occurs.
The ILL_ILLPACCFI crash occurs only when an exception is detected during pointer verification after the pointer verification function is enabled. Therefore, you need to analyze the cause of the pointer exception that leads to the pointer verification failure.
After the process crashes, the Reason field in the generated cppcrash file is ILL_ILLPACCFI. The core content of the crash log is as follows:
Generated by HiviewDFX@OpenHarmony Device info:xxx Build info:xxx Fingerprint:2e5b4fa2280718c81a0ad9597d1c86676b6cce4f6beeda75d7068cafbafd9f86 Module name:fpac10 Timestamp:2025-06-12 14:19:29.750 Pid:540 Uid:0 Process name:./fpacl0 Process life time:ls ReaSon:Signal:SIGILL(ILL_ILLPACCFI) @0x0000005615e449a8 <- The Reason field is ILL_ILLPACCFI. Fault thread info: Tid:540, Name:fpac10. #00 pc 00000000000019a8 /data/kernel test/fpac10(main+84) <- Location where the exception occurs #01 pc 00000000000a1574 /system/lib/ld-musl-aarch64.so.1(libc_start _main_stage2+80) (aca211c0cbb78f65c624199913847e19) Registers: <- Register information x0:0000007fc581cad0 x1:0000007fc581cad0 x2:000000000000000c x3:0000000000006000 x4:0000005615e46cfc x5:0000007fc581cadc x6:0000216f6c6c6568 x7:000000000000216f x8:0 0000005a00000000x9: 000000000000a51f xl0:00000000ffffffe0 xll:ffffffffffffffd8 x12:0000007fc581c940 x13:0000005a193527ec x14:0000000000000001 x15:0000000000000000 x16:0000005615e45cd8 x17:0000005a193cba80 x18:0000000000000000 x19:0000007fc581cb68 x20:0000000000000001 x21:0000005615e44954 x22:0000007fc581cb78 x23:00000000000000e8 X24:0000005a1951d880 x25:0000005a1951e1a0 x26:0000000000000001 x27:0000005a1951a000 x28:0000005a1951d8c8 x29:0000007fc581caf0 lr:0000005615e4499c sp:0000007fc581cac0 pc:0000005615e449a8
According to the stack information provided by the CppCrash log, the exception occurs at 0x19a8 in the fpac10 binary file.
char g_ori[12] = "hello!";
int main()
{
unsigned long context = 0xa51f;
void *addr = (void*)(&SupportPac::SupportPacPrint);
char a[8] = {};
memcpy(a, &g_ori, sizeof(g_ori)); // Forge a memory overflow attack and tamper with the addr content.
#ifdef __ARM_FEATURE_PAUTH
__asm__ __volatile__("autia %0, %1\n\t"
"blr %0\n\t"
: "+r"(addr) : "r"(context):);
#endif
std::cout << a;
return 0;
} 0000000000001954 <main>: 1954: d10103ff sub sP, sp, #64 1958: a9037bfd stp x29, x30, [sp, #48] 195c: 9100c3fd add x29, sp, #48 1960: 2a1f03e8 mov w8, wzr 1964: b9000fes str w8, [sp, #12] 1968: b81fc3bf stur wzr,[x29, #-4] 196c: d294a3e8 mov x8, #42271 1970: f81f03a8 stur x8, [x29, #-16] 1974: bo00008 adrp x8, 0x2000 <main+0x24> 1978: f9464d08 ldr x8, [x8, #3224] 197c: f9000fe8 str x8, [sp, #24] 1980: 910043e0 add x0, sp, #16 1984: f90003e0 str x0,[sp] 1988: f90oobff str xzr, [sp, #16] 198c: d503201f nop 1990: 10011b01 adr x1, #9056 1994: d2800182 mov x2, #12 1998: 9400003a bl 0x1a80 <memcpy@plt> 199c: f94003e1 ldr x1, [sp] 19a0: f9400fe8 ldr x8, [sp, #24] 19a4: f85f03a9 ldur. x9. [x29, #-16] 19a8: dac11128 autia x8, x9 <- Exception Location 19ac: d03f0100 blr x8 19b0: f9000fe8 str x8, [sp, #24] 19b4: d503201f nop 19b8: 10ff6c40 adr x0, #-4728 19bc: 94000035 bl 0x1a90 <printf@plt> 19c0: b9400fe0 ldr w0, [sp, #12] 19c4: a9437bfd ldp x29, x30,[sp, #48] 19c8: 910103ff add sp, sp, #64 19cc: d65f03c0 ret
When using the memcpy function to copy memory, ensure that the size of the copied memory does not exceed the size of the destination buffer. It is recommended that the memcpy_s function be used instead of memcpy.