简体   繁体   English

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

[英]Segmentation fault while testing with google test

I am passing a compound structure pointer to a function after doing all the memory initializations in a test fixture.在测试装置中完成所有内存初始化后,我将复合结构指针传递给函数。 But as soon as the function gets called the sizeof that struct changes.但是一旦函数被调用,该结构的 sizeof 就会发生变化。 Have tried setting watchpoints and everything.已尝试设置观察点和所有内容。 don't the what's the issue.不要有什么问题。 Need help.需要帮忙。 This is the code for the test.这是测试的代码。

sizeof(*ueCb) changes just after calling the function cwRrcSendMibCfgReq. sizeof(*ueCb) 在调用函数 cwRrcSendMibCfgReq 后发生变化。 The function is copying some parameters from ueCb.该函数正在从 ueCb 复制一些参数。 ueCb has multiple structures inside of it. ueCb 内部有多个结构。 Accessing ueCb->currContestCellForSel in the function throws a segmentation fault even though I have explicitly allocated memory here.即使我在这里显式分配了内存,在函数中访问 ueCb->currContestCellForSel 也会引发分段错误。 I have checked that the allocation occurs.我已经检查过分配是否发生。 I check sizeof(*ueCb) in gdb keeping the mentioned fucntion as a breakpoint.我在 gdb 中检查 sizeof(*ueCb) 将提到的功能作为断点。 The header files are the same.头文件是一样的。 Also ueId remains intact while calling the function.调用函数时 ueId 也保持不变。 CW_ALLOC is an internal macro which allocates memory. CW_ALLOC 是分配内存的内部宏。 it's working fine I have checked that.它工作正常我已经检查过。

Can't share the whole code because it's part of IP.无法共享整个代码,因为它是 IP 的一部分。 This code is related to 5G protocol stack.My job is to do unit testing and the entire team isn't able to figure out where the problem is.这段代码与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 Done\n\n\n");
}

The backtrace is as follows:回溯如下:

(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

the function which I'm testing我正在测试的功能

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_REQ\n");
   CwLiCtzCfgReq(&cwCb.ctzSapCbLst[0]->pst,CTZ_CPHY_MIB_CFG_REQ, mibConfig);

   RETVALUE(ROK);
}

Try to swap the order of these lines:尝试交换这些行的顺序:

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

It seems like you first free ueCb with CW_FREE, and then you access a member pointer of ueCb with ueCb->currContestCellForSel , which might cause the segfault.好像你第一次自由ueCb与CW_FREE,然后您访问的成员指针ueCbueCb->currContestCellForSel ,这可能会导致该段错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM