Ошибка сегментации в Linux при вызове POCO c ++ ServerSocket () - PullRequest
0 голосов
/ 01 декабря 2018

В Fedora 29 с POCO C ++, установленным с использованием

dnf install poco-devel

Этот код:

#include <Poco/Net/ServerSocket.h>

int main() {
    Poco::Net::ServerSocket svs(9980);
    return 0;
}

Скомпилирован с использованием этого файла Makefile:

CC = g++
CPPFLAGS += -I/usr/include
CPPFLAGS += -I/usr/include/Poco
CFLAGS += -c
CFLAGS += -g
LDFLAGS += -g
LDLIBS += -lPocoFoundation 
LDLIBS += -lPocoUtil
LDLIBS += -lPocoNet
SOURCES = test.cpp
OBJECTS = $(SOURCES:.cpp=.o)
EXECUTABLE = test

all: $(EXECUTABLE)

$(EXECUTABLE): $(OBJECTS)
    $(CC) $(LDFLAGS) $(OBJECTS) $(LDLIBS) -o $@

.cpp.o:
    $(CC) $(CPPFLAGS) $(CFLAGS) $< -o $@

install:
    @echo "Build complete!"

Дает следующее вgdb

 Thread #1 [test] 8015 [core: 3] (Suspended : Signal :
 SIGSEGV:Segmentation fault)    typeinfo name for
 Poco::Net::Impl::IPv6SocketAddressImpl() at 0x4020c0   
    Poco::Net::SocketAddress::toString[abi:cxx11]() const at
 0x7ffff7ce2239     Poco::Net::SocketImpl::bind() at 0x7ffff7ce74c8 
    Poco::Net::ServerSocket::ServerSocket() at 0x7ffff7ce0e78       main() at
 test.cpp:6 0x4011c5

И в valgrind:

 ==8034== Process terminating with default action of signal 11 (SIGSEGV): dumping core
 ==8034==  Bad permissions for mapped region at address 0x4020C0
 ==8034==    at 0x4020C0: ??? (in /path/to/test85-poco/test)
 ==8034==    by 0x4BCB238: Poco::Net::SocketAddress::toString[abi:cxx11]() const (in
 /usr/lib64/libPocoNet.so.60)
 ==8034==    by 0x4BD04C7: Poco::Net::SocketImpl::bind(Poco::Net::SocketAddress const&, bool,
 bool) (in /usr/lib64/libPocoNet.so.60)
 ==8034==    by 0x4BC9E77: Poco::Net::ServerSocket::ServerSocket(unsigned short, int) (in
 /usr/lib64/libPocoNet.so.60)
 ==8034==

Передача -v в g ++ приводит к:

g++ -I/usr/include -I/usr/include/Poco -c -g -v test.cpp -o test.o
Using built-in specs.
COLLECT_GCC=g++
OFFLOAD_TARGET_NAMES=nvptx-none
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-redhat-linux
Configured with: ../configure --enable-bootstrap --enable-languages=c,c++,fortran,objc,obj-c++,ada,go,lto --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-gcc-major-version-only --with-linker-hash-style=gnu --enable-plugin --enable-initfini-array --with-isl --enable-libmpx --enable-offload-targets=nvptx-none --without-cuda-driver --enable-gnu-indirect-function --enable-cet --with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux
Thread model: posix
gcc version 8.2.1 20181105 (Red Hat 8.2.1-5) (GCC) 
COLLECT_GCC_OPTIONS='-I' '/usr/include' '-I' '/usr/include/Poco' '-c' '-g' '-v' '-o' 'test.o' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
 /usr/libexec/gcc/x86_64-redhat-linux/8/cc1plus -quiet -v -I /usr/include -I /usr/include/Poco -D_GNU_SOURCE test.cpp -quiet -dumpbase test.cpp -mtune=generic -march=x86-64 -auxbase-strip test.o -g -version -o /tmp/ccDd0TCY.s
GNU C++14 (GCC) version 8.2.1 20181105 (Red Hat 8.2.1-5) (x86_64-redhat-linux)
    compiled by GNU C version 8.2.1 20181105 (Red Hat 8.2.1-5), GMP version 6.1.2, MPFR version 3.1.6-p2, MPC version 1.1.0, isl version isl-0.16.1-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
ignoring nonexistent directory "/usr/lib/gcc/x86_64-redhat-linux/8/include-fixed"
ignoring nonexistent directory "/usr/lib/gcc/x86_64-redhat-linux/8/../../../../x86_64-redhat-linux/include"
ignoring duplicate directory "/usr/include"
  as it is a non-system directory that duplicates a system directory
#include "..." search starts here:
#include <...> search starts here:
 /usr/include/Poco
 /usr/lib/gcc/x86_64-redhat-linux/8/../../../../include/c++/8
 /usr/lib/gcc/x86_64-redhat-linux/8/../../../../include/c++/8/x86_64-redhat-linux
 /usr/lib/gcc/x86_64-redhat-linux/8/../../../../include/c++/8/backward
 /usr/lib/gcc/x86_64-redhat-linux/8/include
 /usr/local/include
 /usr/include
End of search list.
GNU C++14 (GCC) version 8.2.1 20181105 (Red Hat 8.2.1-5) (x86_64-redhat-linux)
    compiled by GNU C version 8.2.1 20181105 (Red Hat 8.2.1-5), GMP version 6.1.2, MPFR version 3.1.6-p2, MPC version 1.1.0, isl version isl-0.16.1-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: a86aaae86aad006cc531272eca0396eb
COLLECT_GCC_OPTIONS='-I' '/usr/include' '-I' '/usr/include/Poco' '-c' '-g' '-v' '-o' 'test.o' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
 as -v -I /usr/include -I /usr/include/Poco --64 -o test.o /tmp/ccDd0TCY.s
GNU assembler version 2.31.1 (x86_64-redhat-linux) using BFD version version 2.31.1-13.fc29
COMPILER_PATH=/usr/libexec/gcc/x86_64-redhat-linux/8/:/usr/libexec/gcc/x86_64-redhat-linux/8/:/usr/libexec/gcc/x86_64-redhat-linux/:/usr/lib/gcc/x86_64-redhat-linux/8/:/usr/lib/gcc/x86_64-redhat-linux/
LIBRARY_PATH=/usr/lib/gcc/x86_64-redhat-linux/8/:/usr/lib/gcc/x86_64-redhat-linux/8/../../../../lib64/:/lib/../lib64/:/usr/lib/../lib64/:/usr/lib/gcc/x86_64-redhat-linux/8/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-I' '/usr/include' '-I' '/usr/include/Poco' '-c' '-g' '-v' '-o' 'test.o' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
g++ -g test.o -lPocoFoundation  -lPocoUtil -lPocoNet -o test

Более сложный подход дает аналогичный результат:

#include <Poco/Net/ServerSocket.h>
#include <iostream>

int main() {
    std::string Address("192.168.0.11:9980");
    Poco::Net::SocketAddress addr(Address);

    if (addr.family() == static_cast<Poco::Net::IPAddress::Family>(Poco::Net::Impl::IPAddressImpl::IPv6)) {
        std::cout << "Addr: ipv6 " << std::endl;
    }
    else if (addr.family() == static_cast<Poco::Net::IPAddress::Family>(Poco::Net::Impl::IPAddressImpl::IPv4)) {
        std::cout << "Addr: ipv4 " << std::endl;
    }
    else {
        std::cout << "Addr: something else (err)." << std::endl;
    }
    Poco::Net::ServerSocket svs(addr);
    return 0;
}

При этом на консоли:

 Addr: ipv4

трассировка стека GDB:

 Thread #1 [test] 7865 [core: 0] (Suspended : Signal :
 SIGSEGV:Segmentation fault)        typeinfo name for
 Poco::Net::Impl::IPv6SocketAddressImpl() at 0x403100   
    Poco::Net::SocketAddress::toString[abi:cxx11]() const at
 0x7ffff7ce2239     Poco::Net::SocketImpl::bind() at 0x7ffff7ce74c8 
    Poco::Net::ServerSocket::ServerSocket() at 0x7ffff7ce0db7       main() at
 test.cpp:39 0x40236a

и отчет valgrind:

 ==7616== Process terminating with default action of signal 11 (SIGSEGV): dumping core
 ==7616==  Bad permissions for mapped region at address 0x403100
 ==7616==    at 0x403100: ??? (in /path/to/test85-poco/test)
 ==7616==    by 0x4BCB238: Poco::Net::SocketAddress::toString[abi:cxx11]() const (in
 /usr/lib64/libPocoNet.so.60)
 ==7616==    by 0x4BD04C7: Poco::Net::SocketImpl::bind(Poco::Net::SocketAddress const&, bool,
 bool) (in /usr/lib64/libPocoNet.so.60)
 ==7616==    by 0x4BC9DB6: Poco::Net::ServerSocket::ServerSocket(Poco::Net::SocketAddress const&,
 int) (in /usr/lib64/libPocoNet.so.60)
 ==7616==    by 0x402375: main (test.cpp:38)

Вопрос:

Как создать ServerSocket в Linux с помощью POCO, который выполняется без ошибки сегментации?

1 Ответ

0 голосов
/ 02 декабря 2018

Построение POCO напрямую из источника решило проблему.Для сборки использовалось следующее:

sudo dnf remove poco-devel
git clone https://github.com/pocoproject/poco
cd poco/build
cmake ..
make -j4
sudo make install

Файлы POCO записываются в / usr / local / include и / usr / local / lib, поэтому модифицировал make-файл следующим образом:

CC = g++
CPPFLAGS += -I/usr/local/include
CPPFLAGS += -I/usr/local/include/Poco

CFLAGS += -c
CFLAGS += -g
LDFLAGS += -g
CFLAGS += -std=c++14
LDLIBS += -L/usr/local/lib
LDLIBS += -lPocoFoundation 
LDLIBS += -lPocoUtil
LDLIBS += -lPocoNet
LDLIBS += -Wl,-rpath=/usr/local/lib
SOURCES = test.cpp
OBJECTS = $(SOURCES:.cpp=.o)
EXECUTABLE = test

all: $(EXECUTABLE)

$(EXECUTABLE): $(OBJECTS)
    $(CC) $(LDFLAGS) $(OBJECTS) $(LDLIBS) -o $@

.cpp.o:
    $(CC) $(CPPFLAGS) $(CFLAGS) $< -o $@

install:
    @echo "Build complete!"

ServerSocket в Linux с использованием POCO теперь выполняется без ошибки сегментации.

...