[经验分享] 【TEST测试】bad_variant_access 错误的处理 原创

诚迈_雨哥 显示全部楼层 发表于 2024-6-1 15:26:35

1. 问题

运行第 StateMachineTest_OnEvent012用例时报错

libc++abi: terminating due to uncaught exception of type std::bad_variant_access: bad_variant_access

consolelog.PNG

测试用例代码如下

HWTEST_F(CooperatePluginTest, StateMachineTest_OnEvent012, TestSize.Level0)
{
    CALL_TEST_DEBUG;
    std::string localNetworkId = g_context->dsoftbus_.GetLocalNetworkId();
    CooperateEvent bothLocalEvent(
        CooperateEventType::DSOFTBUS_START_COOPERATE,
        DSoftbusStartCooperate {
            .networkId = localNetworkId
        });
    auto env = ContextService::GetInstance();
    ASSERT_NE(env, nullptr);
    Context cooperateContext(env);
    g_stateMachine = std::make_shared<Cooperate::StateMachine>(env);
    Cooperate::CooperateIn stateIn(*g_stateMachine, env);
    ASSERT_NE(stateIn.initial_, nullptr);
    auto relay = std::make_shared<Cooperate::CooperateIn::RelayConfirmation>(stateIn, stateIn.initial_);
    ASSERT_NE(relay, nullptr);
    cooperateContext.remoteNetworkId_ = localNetworkId;
    stateIn.initial_->OnRemoteStart(cooperateContext, bothLocalEvent);
    relay->OnRemoteStart(cooperateContext, bothLocalEvent);
    bool ret = g_context->mouseLocation_.HasLocalListener();
    EXPECT_FALSE(ret);
}

2. 原因分析

打开 cppcrash-CooperatePluginTest-0-20170805230851 日志,如下片段

Generated by HiviewDFX@OpenHarmony
================================================================
Device info:OpenHarmony 3.2
Build info:OpenHarmony 5.0.0.24
Fingerprint:3c44cc1faf2a52afc8ddaa5cf406f01990018aa091a909dedc27b9a2c2507a13
Module name:CooperatePluginTest
Timestamp:2017-08-05 23:08:51.000
Pid:5038
Uid:0
Process name:./data/CooperatePluginTest
Process life time:2s
Reason:Signal:SIGSEGV(SEGV_MAPERR)@0000000000  probably caused by NULL pointer dereference
Fault thread info:
Tid:5038, Name:CooperatePlugin
#00 pc 00051e80 /system/lib/libintention_cooperate.z.so(OHOS::Msdp::DeviceStatus::Cooperate::StateMachine::GetCooperateState(OHOS::Msdp::DeviceStatus::Cooperate::Context&, OHOS::Msdp::DeviceStatus::Cooperate::CooperateEvent const&)+176)(3aa2fd1f262954e85537b789af0106df)
#01 pc 00025fb7 /data/CooperatePluginTest(OHOS::Msdp::DeviceStatus::CooperatePluginTest_StateMachineTest_OnEvent012_Test::TestBody()+366)(8bb6dbc0523b18bdc1d5b079d530c9e2)
#02 pc 0005484f /data/CooperatePluginTest(testing::Test::Run()+206)(8bb6dbc0523b18bdc1d5b079d530c9e2)
#03 pc 0005505b /data/CooperatePluginTest(testing::TestInfo::Run()+310)(8bb6dbc0523b18bdc1d5b079d530c9e2)
#04 pc 0005586b /data/CooperatePluginTest(testing::TestSuite::Run()+338)(8bb6dbc0523b18bdc1d5b079d530c9e2)
#05 pc 0005f19b /data/CooperatePluginTest(testing::internal::UnitTestImpl::RunAllTests()+1450)(8bb6dbc0523b18bdc1d5b079d530c9e2)
#06 pc 0005ead9 /data/CooperatePluginTest(testing::UnitTest::Run()+84)(8bb6dbc0523b18bdc1d5b079d530c9e2)
#07 pc 0004f25b /data/CooperatePluginTest(main+58)(8bb6dbc0523b18bdc1d5b079d530c9e2)
#08 pc 00071dd8 /system/lib/ld-musl-arm.so.1(libc_start_main_stage2+56)(0ad219058118057922ee9ce3bf9b229b)
#09 pc 0001d078 /data/CooperatePluginTest(_start_c+84)(8bb6dbc0523b18bdc1d5b079d53

用 addr2line 反编译指令输入如下指令

 ./prebuilts/clang/ohos/linux-x86_64/llvm/bin/llvm-addr2line -Cfpie ./out/rk3568/lib.unstripped/msdp/device_status/libintention_cooperate.z.so 00051e80

得到如下结果

 (inlined by) OHOS::Msdp::DeviceStatus::Cooperate::CooperateFree::Initial::OnRemoteStart(OHOS::Msdp::DeviceStatus::Cooperate::Context&, OHOS::Msdp::DeviceStatus::Cooperate::CooperateEvent const&) at /mnt/data/wangyeyu/code_t67_0515/out/rk3568/../../base/msdp/device_status/intention/cooperate/plugin/src/cooperate_free.cpp:172

DSoftbusStartCooperate notice = std::get<DSoftbusStartCooperate>(event.event) 报错

line172.PNG

查看源代码,发现构造 CooperateEvent 事件时缺少 DSoftbusStartCooperate赋值

3. 解决方案

增加DSoftbusStartCooperate赋值,问题解决

HWTEST_F(CooperatePluginTest, StateMachineTest_OnEvent012, TestSize.Level0)
{
    CALL_TEST_DEBUG;
    std::string localNetworkId = g_context->dsoftbus_.GetLocalNetworkId();
    CooperateEvent bothLocalEvent(
        CooperateEventType::DSOFTBUS_START_COOPERATE,
        DSoftbusStartCooperate {
            .networkId = localNetworkId
        });
    auto env = ContextService::GetInstance();
    ASSERT_NE(env, nullptr);
    Context cooperateContext(env);
    g_stateMachine = std::make_shared<Cooperate::StateMachine>(env);
    Cooperate::CooperateIn stateIn(*g_stateMachine, env);
    ASSERT_NE(stateIn.initial_, nullptr);
    auto relay = std::make_shared<Cooperate::CooperateIn::RelayConfirmation>(stateIn, stateIn.initial_);
    ASSERT_NE(relay, nullptr);
    cooperateContext.remoteNetworkId_ = localNetworkId;
    stateIn.initial_->OnRemoteStart(cooperateContext, bothLocalEvent);
    relay->OnRemoteStart(cooperateContext, bothLocalEvent);
    bool ret = g_context->mouseLocation_.HasLocalListener();
    EXPECT_FALSE(ret);
}

4. 小结

运行代码过程中出现crash,用addr2ling反编译指令,对lib.unstripped未压缩的so进行反编译,出错地址和so来源于crash日志文件。

  • 请使用 prebuilts目录下的llvm-addr2line 指令,否则解析不出来!

  • 此类问题如果打印了进出日志,此时只会打印进入日志,缺少离开日志,如下:

    hilog.PNG

©著作权归作者所有,转载或内容合作请联系作者

您尚未登录,无法参与评论,登录后可以:
参与开源共建问题交流
认同或收藏高质量问答
获取积分成为开源共建先驱

Copyright   ©2023  OpenHarmony开发者论坛  京ICP备2020036654号-3 |技术支持 Discuz!

返回顶部