Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Segmentation fault by socket type ZMQ_SUB with static build #4535

Closed
rhymerjr opened this issue Mar 30, 2023 · 1 comment
Closed

Segmentation fault by socket type ZMQ_SUB with static build #4535

rhymerjr opened this issue Mar 30, 2023 · 1 comment

Comments

@rhymerjr
Copy link

Unfortunately I cannot understand why segfault is happening when building my program as static!
Segfault happens when the call zmq_recv is reached!

The build command:
g++ -g -static -std=c++17 main.cpp -o main.bin -Wl,--whole-archive -lpthread -Wl,--no-whole-archive -ldl /usr/local/lib/libzmq.a

But everythig is OK If i'm building it using the dynamic libraries:
g++ -g -std=c++17 main.cpp -o main.bin -lzmq

What could be the problem with static build?

Environment

  • libzmq version : 4.3.4 (also master works the same)
    installed from source:
    ./autogen.sh
    ./configure --enable-static=yes --enable-shared=yes
    make -j4 && make check && make install && ldconfig

  • installed files:
    root@deb11-dev:/home/dev/src/testings/pubsub# ls -a1 /usr/local/lib/libzmq*
    /usr/local/lib/libzmq.a
    /usr/local/lib/libzmq.la
    /usr/local/lib/libzmq.so
    /usr/local/lib/libzmq.so.5
    /usr/local/lib/libzmq.so.5.2.4
    /usr/local/lib/libzmq.so.5.2.5

  • OS: Debian 11

  • g++ version: g++ (Debian 10.2.1-6) 10.2.1 20210110

Minimal test code / Steps to reproduce the issue

#include <iostream>
#include <string>
#include <cstring>
#include <zmq.h>
#define BSIZE 512
void* context;
void* socket;
int main(int argc, char **argv)
{
    printf("dbg - 0\n");
    const std::string connstr = "tcp://127.0.0.1:5678";
    printf("dbg - 1\n");
    if (NULL==(context=zmq_ctx_new()))
    {
        printf("ERROR - Failed to create context(%d:%s)!\n",errno,zmq_strerror(errno));
        return -1;
    }
    printf("dbg - 2\n");
    if (NULL==(socket=zmq_socket (context, ZMQ_SUB)))
    {
        printf("ERROR - Failed to create socket!(%d:%s)\n",errno,zmq_strerror(errno));
        return -1;
    }
    printf("dbg - 3\n");
    if (-1==zmq_setsockopt (socket, ZMQ_SUBSCRIBE, "", 0))
    {
        printf("ERROR - Failed to set socket option(SUBSCRIBE)!(%d:%s)\n",errno,zmq_strerror(errno));
    }
    printf("dbg - 4\n");
    if (0!=zmq_connect (socket, connstr.c_str()))
    {
        printf("ERROR - Failed to connect (%d:%s)\n",errno,zmq_strerror(errno));
        return -1;
    }
    printf("dbg - 5\n");
    while (1)
    {
        printf("dbg - 6\n");
        char buf [BSIZE];
        printf("dbg - 7\n");
        memset (buf,'\0',BSIZE);
        printf("dbg - 8\n");
        int nbytes = zmq_recv (socket, buf, BSIZE, 0);
        printf("dbg - 9\n");
        if (nbytes>0)
        {
            printf("dbg - 10\n");
            printf("'%d' bytes received: '%s'\n",nbytes,buf);
        }
        else
        {
            printf("dbg - 11\n");
            printf("value of nbytes '%d'\n",nbytes);
        }
    }
    printf("dbg - 12\n");
    if (0!=zmq_disconnect (socket, connstr.c_str()))
    {
        printf("ERROR - Failed to disconnect (%d:%s)\n",errno,zmq_strerror(errno));
    }
    if (0!=zmq_close(socket))
    {
        printf("ERROR - Failed to close socket (%d:%s)\n",errno,zmq_strerror(errno));
    }
    if (0!=zmq_ctx_term(context))
    {
        printf("ERROR - Failed to terminate context (%d:%s)\n",errno,zmq_strerror(errno));
    }
    return 0;
}

What's the actual result? (include assertion message & call stack if applicable)

`Reading symbols from ./main.bin...
(gdb) run
Starting program: /home/dev/src/testings/pubsub/main.bin
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
dbg - 0
dbg - 1
dbg - 2
[New Thread 0x7ffff7ff8700 (LWP 1649)]
[New Thread 0x7ffff77f7700 (LWP 1650)]
dbg - 3
dbg - 4
dbg - 5
dbg - 6
dbg - 7
dbg - 8

Thread 3 "ZMQbg/IO/0" received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x7ffff77f7700 (LWP 1650)]
0x00000000006847b8 in socket ()
(gdb) bt
#0 0x00000000006847b8 in socket ()
#1 0x0000000000470185 in zmq::tcp_open_socket (address_=0x6a6c00 "127.0.0.1:5678", options_=..., local_=local_@entry=false,
fallback_to_ipv4_=fallback_to_ipv4_@entry=true, out_tcp_addr_=0x7ffff00013a0) at src/tcp.cpp:371
#2 0x0000000000470705 in zmq::tcp_connecter_t::open (this=0x7ffff0000b60) at src/tcp_connecter.cpp:185
#3 0x0000000000470985 in zmq::tcp_connecter_t::start_connecting (this=0x7ffff0000b60) at src/tcp_connecter.cpp:139
#4 0x00000000004273e3 in zmq::object_t::process_command (this=0x7ffff0000b60, cmd_=...) at src/object.cpp:86
#5 0x0000000000423414 in zmq::io_thread_t::in_event (this=0x6a5630) at src/io_thread.cpp:91
#6 0x00000000004223e0 in zmq::epoll_t::loop (this=0x6a5be0) at src/epoll.cpp:210
#7 0x000000000043fa53 in thread_routine (arg_=0x6a5c38) at src/thread.cpp:256
#8 0x0000000000408cc7 in start_thread (arg=) at pthread_create.c:477
#9 0x00000000005b261f in clone ()
(gdb)`

What's the expected result?

Do not segfault when building as static

coredump.tar.gz

@rhymerjr
Copy link
Author

I found that it was a scope issue with the variables: void* context; void* socket;
Don't know exactly what was the problem but moved these variables into the main function has solved the problem!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant