下面代码出现段错误,错误在哪里?

问题描述 投票:0回答:1

代码:

#include <list>
#include <thread>
#include <sys/poll.h>

struct xsk_ring_stats {
    unsigned long rx_frags;
    unsigned long rx_npkts;
    unsigned long tx_frags;
    unsigned long tx_npkts;
    unsigned long rx_dropped_npkts;
    unsigned long rx_invalid_npkts;
    unsigned long tx_invalid_npkts;
    unsigned long rx_full_npkts;
    unsigned long rx_fill_empty_npkts;
    unsigned long tx_empty_npkts;
    unsigned long prev_rx_frags;
    unsigned long prev_rx_npkts;
    unsigned long prev_tx_frags;
    unsigned long prev_tx_npkts;
    unsigned long prev_rx_dropped_npkts;
    unsigned long prev_rx_invalid_npkts;
    unsigned long prev_tx_invalid_npkts;
    unsigned long prev_rx_full_npkts;
    unsigned long prev_rx_fill_empty_npkts;
    unsigned long prev_tx_empty_npkts;
};

struct xsk_app_stats {
    unsigned long rx_empty_polls {};
    unsigned long fill_fail_polls {};
    unsigned long copy_tx_sendtos {};
    unsigned long tx_wakeup_sendtos {};
    unsigned long opt_polls {};
    unsigned long prev_rx_empty_polls {};
    unsigned long prev_fill_fail_polls {};
    unsigned long prev_copy_tx_sendtos {};
    unsigned long prev_tx_wakeup_sendtos {};
    unsigned long prev_opt_polls {};
};

struct xsk_driver_stats {
    unsigned long intrs;
    unsigned long prev_intrs;
};

struct xsk_umem_info {
    struct xsk_ring_prod fq;
    struct xsk_ring_cons cq;
    struct xsk_umem *umem;
    void *buffer;
};

struct xsk_socket_info {
    xsk_ring_cons rx {};
    xsk_ring_prod tx {};
    xsk_umem_info *umem {};
    xsk_socket *xsk {};
    xsk_ring_stats ring_stats {};
    xsk_app_stats app_stats {};
    xsk_driver_stats drv_stats {};
    uint32_t outstanding_tx {};
};

#define NUM_SOCKS 12

static int num_socks = 0;

static xsk_socket_info** xsks = new xsk_socket_info*[NUM_SOCKS];

static void l2fwd_all(int i)
{
    pollfd fds[1] = {};

    auto xsk = xsks[i];

    fds[i].fd = xsk_socket__fd(xsk->xsk);
    fds[i].events = POLLIN;
    xsk->app_stats.opt_polls++; //HERE IS THE PROBLEM!
}

int main(){
    for (int i = 0; i < NUM_SOCKS; i++)
        xsks[num_socks++] = new xsk_socket_info {};

    std::list<std::thread> threads {};

    for(int i = 0; i < num_socks; i++)
        l2fwd_all(i);
}

问题:

xsk->app_stats.opt_polls++;

行出现 SIGSEGV(分段错误)

我正在绞尽脑汁试图找到这个问题的解决方案,我相信这与我分配内存的方式有关。

老实说,我找不到问题所在,我已经陷入这样的困境6个小时了

解决代码问题

我相信这篇文章在解释错误方面没有改进的余地,因为它非常小

但是如果我失败了,请在负面评论之前留下评论。

在线演示: https://replit.com/@LucasPaixaoPaix/FirsthandWorthyGlobalarrays#main.cpp

奇怪的是,replit 中没有返回错误,仅在我的 64 位 Linux 服务器上

----- 使用 -fsanitize=address -g -W -Wall 调试后 -----

/tmp/tmp.zc9dBNHYKe/cmake-build-debug/CesKit -i eth0 -l -N -p -b 256 -M -Q
Signal: SIGSEGV (Segmentation fault)
AddressSanitizer:DEADLYSIGNAL
=================================================================
==66457==ERROR: AddressSanitizer: SEGV on unknown address 0x000100000130 (pc 0x00000040c548 bp 0xfffffffff350 sp 0xfffffffff330 T0)
==66457==The signal is caused by a READ memory access.
    #0 0x40c548 in l2fwd_all ../test.cpp:95
    #1 0x40c610 in main ../test.cpp:105
    #2 0x400001060798 in __libc_start_call_main (/lib64/libc.so.6+0x2c798)
    #3 0x400001060868 in __libc_start_main_alias_2 (/lib64/libc.so.6+0x2c868)
    #4 0x402dac in _start (/tmp/tmp.zc9dBNHYKe/cmake-build-debug/CesKit+0x402dac)

AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: SEGV ../test.cpp:95 in l2fwd_all
==66457==ABORTING

Process finished with exit code 1
c++ segmentation-fault
1个回答
0
投票

xsks
指针分配和访问没有问题。问题就出在这里:

pollfd fds[1] = {};

fds
是大小为
1
的数组,对于大于
i
的任何
1
值,它将尝试访问超出其大小(即 UB)的数组。

© www.soinside.com 2019 - 2024. All rights reserved.