使用谷歌测试进行测试时出现分段错误

Segmentation fault while testing with google test

本文关键字:测试 分段 错误 谷歌      更新时间:2023-10-16

在测试装置中执行所有内存初始化后,我将复合结构指针传递给函数。但是一旦调用函数,该结构的大小就会改变。 尝试过设置观察点和一切。不要是什么问题。 需要帮助。 这是测试的代码。

sizeof(*ueCb( 在调用函数 cwRrcSendMibCfgReq 后发生变化。 该函数正在从 ueCb 复制一些参数。ueCb 内部有多个结构。在函数中访问ueCb->currContestCellForSel会引发分段错误,即使我在这里显式分配了内存。我已经检查了分配是否发生。我在 gdb 中检查 sizeof(*ueCb(,将提到的功能保留为断点。头文件是相同的。此外,ueId 在调用函数时保持不变。CW_ALLOC是分配内存的内部宏。它工作正常,我已经检查过了。

无法共享整个代码,因为它是 IP 的一部分。此代码与 5G 协议栈相关。我的工作是进行单元测试,整个团队都无法找出问题所在。

TEST(testMib1, test)
{
CwRrcUeCb* ueCb;
CW_ALLOC(CW_REG,CW_POOL,&ueCb, sizeof(CwRrcUeCb));
memset(ueCb, 0, sizeof(CwRrcUeCb));
ueCb->currContestCellForSel = (CwRrcCellCb *) 
malloc(sizeof(CwRrcCellCb));
ueCb->currContestCellForSel->phyCellId = 5;
ueCb->ueId = 5;
S16 ret = ROK;
ret = cwRrcSendMibCfgReq(ueCb); // sizeof *ueCb changes after this statement
free(ueCb->currContestCellForSel);        
CW_FREE(CW_REG, CW_POOL, ueCb, sizeof (CwRrcUeCb));
// have changed the order just to get to the main point
EXPECT_EQ(ROK, ret);
printf(" Event 9 Donennn");
}

回溯如下:

(gdb) backtrace
#0  0x000000000053a673 in cwRrcSendMibCfgReq (rrcUeCb=0x7ffff5d45320) at ../src/5gnrueapp/cw_rrc_fsm.c:2745
#1  0x000000000061dd59 in testMib1_test_Test::TestBody (this=0xa73500) at ../unittest/test_Event9Mib1.cc:79
#2  0x00007ffff71847a3 in void testing::internal::HandleExceptionsInMethodIfSupported<testing::Test, void>(testing::Test*, void (testing:
:Test::*)(), char const*) () from /lib64/libgtest.so.0
#3  0x00007ffff717ab27 in testing::Test::Run() () from /lib64/libgtest.so.0
#4  0x00007ffff717abce in testing::TestInfo::Run() () from /lib64/libgtest.so.0
#5  0x00007ffff717acd5 in testing::TestCase::Run() () from /lib64/libgtest.so.0
#6  0x00007ffff717e018 in testing::internal::UnitTestImpl::RunAllTests() () from /lib64/libgtest.so.0
#7  0x00007ffff717e2a7 in testing::UnitTest::Run() () from /lib64/libgtest.so.0
#8  0x000000000061e156 in main (argc=1, argv=0x7fffffffe1d8) at ../unittest/test_main.cc:38

我正在测试的功能

S16 cwRrcSendMibCfgReq(CwRrcUeCb * rrcUeCb)
{
CtzMibConfigRequest *mibConfig = NULLP;
CW_ALLOC(CW_REG, CW_POOL, &mibConfig, sizeof (CtzMibConfigRequest));
if(NULL == mibConfig)
{
RLOG1(L_FATAL,"Memory Allocation Failed while sending Mib config req ueId:%d",rrcUeCb->ueId);
RETVALUE(RFAILED);
}
mibConfig->pres.pres = 1;
mibConfig->systemFrameNumber       = rrcUeCb->cwMibInfo.systemFrameNumber;
mibConfig->subCarrierSpacingCommon = rrcUeCb->cwMibInfo.subCarrierSpacingCommon;
mibConfig->ssb_SubcarrierOffset    = rrcUeCb->cwMibInfo.ssb_SubcarrierOffset;
mibConfig->dmrs_TypeAPosition      = rrcUeCb->cwMibInfo.dmrs_TypeAPosition;
mibConfig->pdcch_ConfigSIB1.controlResourceSetZero =
rrcUeCb->cwMibInfo.pdcch_ConfigSIB1.controlResourceSetZero;
mibConfig->pdcch_ConfigSIB1.searchSpaceZero        = rrcUeCb->cwMibInfo.pdcch_ConfigSIB1.searchSpaceZero;
mibConfig->ueId                    =  rrcUeCb->ueId;
mibConfig->cellId                  =  rrcUeCb->currContestCellForSel->phyCellId;
RLOG0(L_DEBUG,"[CFGREQ] [SRC:RRC    ==>> DST:CL(PHY)]   : CTZ_CPHY_MIB_CFG_REQ");
printf("n[SRC:RRC    ==>> DST:CL(PHY)]   : CTZ_CPHY_MIB_CFG_REQn");
CwLiCtzCfgReq(&cwCb.ctzSapCbLst[0]->pst,CTZ_CPHY_MIB_CFG_REQ, mibConfig);
RETVALUE(ROK);
}

尝试交换这些行的顺序:

CW_FREE(CW_REG, CW_POOL, ueCb, sizeof (CwRrcUeCb));
free(ueCb->currContestCellForSel);

似乎您首先使用 CW_FREE 释放ueCb,然后使用ueCb->currContestCellForSel访问ueCb的成员指针,这可能会导致段错误。