Fix struct sockaddr/sockaddr_in/sockaddr_un strict aliasing violations
Building gdbserver in C++ mode shows: gdb/gdbserver/tracepoint.c: In function ‘void* gdb_agent_helper_thread(void*)’: gdb/gdbserver/tracepoint.c:7190:47: error: cannot convert ‘sockaddr_un*’ to ‘sockaddr*’ for argument ‘2’ to ‘int accept(int, sockaddr*, socklen_t*)’ fd = accept (listen_fd, &sockaddr, &tmp); A few places in the tree already have an explicit cast to struct sockaddr *, but that's a strict aliasing violation. Instead of propagating invalid code, fix this by using a union instead. gdb/ChangeLog: 2015-03-07 Pedro Alves <palves@redhat.com> * common/gdb_socket.h: New file. * ser-tcp.c: Include gdb_socket.h. Don't include netinet/in.h nor sys/socket.h. (net_open): Use union gdb_sockaddr_u. gdb/gdbserver/ChangeLog: 2015-03-07 Pedro Alves <palves@redhat.com> * gdbreplay.c: No longer include <netinet/in.h>, <sys/socket.h>, or <winsock2.h> here. Instead include "gdb_socket.h". (remote_open): Use union gdb_sockaddr_u. * remote-utils.c: No longer include <netinet/in.h>, <sys/socket.h> or <winsock2.h> here. Instead include "gdb_socket.h". (handle_accept_event, remote_prepare): Use union gdb_sockaddr_u. * tracepoint.c: Include "gdb_socket.h" instead of <sys/socket.h> or <sys/un.h>. (init_named_socket, gdb_agent_helper_thread): Use union gdb_sockaddr_u.
This commit is contained in:
parent
72df25b28d
commit
366c75fc91
7 changed files with 97 additions and 52 deletions
|
@ -1,3 +1,10 @@
|
||||||
|
2015-03-07 Pedro Alves <palves@redhat.com>
|
||||||
|
|
||||||
|
* common/gdb_socket.h: New file.
|
||||||
|
* ser-tcp.c: Include gdb_socket.h. Don't include netinet/in.h nor
|
||||||
|
sys/socket.h.
|
||||||
|
(net_open): Use union gdb_sockaddr_u.
|
||||||
|
|
||||||
2015-03-07 Pedro Alves <palves@redhat.com>
|
2015-03-07 Pedro Alves <palves@redhat.com>
|
||||||
|
|
||||||
* common/common-exceptions.c [!__cplusplus] (enum catcher_state)
|
* common/common-exceptions.c [!__cplusplus] (enum catcher_state)
|
||||||
|
|
43
gdb/common/gdb_socket.h
Normal file
43
gdb/common/gdb_socket.h
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
/* Copyright (C) 2015 Free Software Foundation, Inc.
|
||||||
|
|
||||||
|
This file is part of GDB.
|
||||||
|
|
||||||
|
This program is free software; you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU General Public License as published by
|
||||||
|
the Free Software Foundation; either version 3 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
This program is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License
|
||||||
|
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||||
|
|
||||||
|
#ifndef GDB_SOCKET_H
|
||||||
|
#define GDB_SOCKET_H
|
||||||
|
|
||||||
|
#if USE_WIN32API
|
||||||
|
#include <winsock2.h>
|
||||||
|
#else
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <netinet/in.h>
|
||||||
|
#if HAVE_SYS_UN_H
|
||||||
|
#include <sys/un.h>
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* Use this union instead of casts between struct sockaddr <-> struct
|
||||||
|
sockaddr_foo to avoid strict aliasing violations. */
|
||||||
|
|
||||||
|
union gdb_sockaddr_u
|
||||||
|
{
|
||||||
|
struct sockaddr sa;
|
||||||
|
struct sockaddr_in sa_in;
|
||||||
|
#if HAVE_SYS_UN_H
|
||||||
|
struct sockaddr_un sa_un;
|
||||||
|
#endif
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif /* GDB_SOCKET_H */
|
|
@ -1,3 +1,16 @@
|
||||||
|
2015-03-07 Pedro Alves <palves@redhat.com>
|
||||||
|
|
||||||
|
* gdbreplay.c: No longer include <netinet/in.h>, <sys/socket.h>,
|
||||||
|
or <winsock2.h> here. Instead include "gdb_socket.h".
|
||||||
|
(remote_open): Use union gdb_sockaddr_u.
|
||||||
|
* remote-utils.c: No longer include <netinet/in.h>, <sys/socket.h>
|
||||||
|
or <winsock2.h> here. Instead include "gdb_socket.h".
|
||||||
|
(handle_accept_event, remote_prepare): Use union gdb_sockaddr_u.
|
||||||
|
* tracepoint.c: Include "gdb_socket.h" instead of <sys/socket.h>
|
||||||
|
or <sys/un.h>.
|
||||||
|
(init_named_socket, gdb_agent_helper_thread): Use union
|
||||||
|
gdb_sockaddr_u.
|
||||||
|
|
||||||
2015-03-07 Pedro Alves <palves@redhat.com>
|
2015-03-07 Pedro Alves <palves@redhat.com>
|
||||||
|
|
||||||
Adjust all callers of TRY_CATCH to use TRY/CATCH/END_CATCH
|
Adjust all callers of TRY_CATCH to use TRY/CATCH/END_CATCH
|
||||||
|
|
|
@ -36,24 +36,16 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#ifdef HAVE_NETINET_IN_H
|
|
||||||
#include <netinet/in.h>
|
|
||||||
#endif
|
|
||||||
#ifdef HAVE_SYS_SOCKET_H
|
|
||||||
#include <sys/socket.h>
|
|
||||||
#endif
|
|
||||||
#if HAVE_NETDB_H
|
#if HAVE_NETDB_H
|
||||||
#include <netdb.h>
|
#include <netdb.h>
|
||||||
#endif
|
#endif
|
||||||
#if HAVE_NETINET_TCP_H
|
#if HAVE_NETINET_TCP_H
|
||||||
#include <netinet/tcp.h>
|
#include <netinet/tcp.h>
|
||||||
#endif
|
#endif
|
||||||
|
#include "gdb_socket.h"
|
||||||
|
|
||||||
#include <alloca.h>
|
#include <alloca.h>
|
||||||
|
|
||||||
#if USE_WIN32API
|
|
||||||
#include <winsock2.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifndef HAVE_SOCKLEN_T
|
#ifndef HAVE_SOCKLEN_T
|
||||||
typedef int socklen_t;
|
typedef int socklen_t;
|
||||||
|
@ -188,7 +180,7 @@ remote_open (char *name)
|
||||||
#endif
|
#endif
|
||||||
char *port_str;
|
char *port_str;
|
||||||
int port;
|
int port;
|
||||||
struct sockaddr_in sockaddr;
|
union gdb_sockaddr_u sockaddr;
|
||||||
socklen_t tmp;
|
socklen_t tmp;
|
||||||
int tmp_desc;
|
int tmp_desc;
|
||||||
|
|
||||||
|
@ -215,16 +207,16 @@ remote_open (char *name)
|
||||||
setsockopt (tmp_desc, SOL_SOCKET, SO_REUSEADDR, (char *) &tmp,
|
setsockopt (tmp_desc, SOL_SOCKET, SO_REUSEADDR, (char *) &tmp,
|
||||||
sizeof (tmp));
|
sizeof (tmp));
|
||||||
|
|
||||||
sockaddr.sin_family = PF_INET;
|
sockaddr.sa_in.sin_family = PF_INET;
|
||||||
sockaddr.sin_port = htons (port);
|
sockaddr.sa_in.sin_port = htons (port);
|
||||||
sockaddr.sin_addr.s_addr = INADDR_ANY;
|
sockaddr.sa_in.sin_addr.s_addr = INADDR_ANY;
|
||||||
|
|
||||||
if (bind (tmp_desc, (struct sockaddr *) &sockaddr, sizeof (sockaddr))
|
if (bind (tmp_desc, &sockaddr.sa, sizeof (sockaddr.sa_in))
|
||||||
|| listen (tmp_desc, 1))
|
|| listen (tmp_desc, 1))
|
||||||
perror_with_name ("Can't bind address");
|
perror_with_name ("Can't bind address");
|
||||||
|
|
||||||
tmp = sizeof (sockaddr);
|
tmp = sizeof (sockaddr.sa_in);
|
||||||
remote_desc = accept (tmp_desc, (struct sockaddr *) &sockaddr, &tmp);
|
remote_desc = accept (tmp_desc, &sockaddr.sa, &tmp);
|
||||||
if (remote_desc == -1)
|
if (remote_desc == -1)
|
||||||
perror_with_name ("Accept failed");
|
perror_with_name ("Accept failed");
|
||||||
|
|
||||||
|
|
|
@ -30,12 +30,6 @@
|
||||||
#if HAVE_SYS_FILE_H
|
#if HAVE_SYS_FILE_H
|
||||||
#include <sys/file.h>
|
#include <sys/file.h>
|
||||||
#endif
|
#endif
|
||||||
#if HAVE_NETINET_IN_H
|
|
||||||
#include <netinet/in.h>
|
|
||||||
#endif
|
|
||||||
#if HAVE_SYS_SOCKET_H
|
|
||||||
#include <sys/socket.h>
|
|
||||||
#endif
|
|
||||||
#if HAVE_NETDB_H
|
#if HAVE_NETDB_H
|
||||||
#include <netdb.h>
|
#include <netdb.h>
|
||||||
#endif
|
#endif
|
||||||
|
@ -57,10 +51,7 @@
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
#endif
|
#endif
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
|
#include "gdb_socket.h"
|
||||||
#if USE_WIN32API
|
|
||||||
#include <winsock2.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if __QNX__
|
#if __QNX__
|
||||||
#include <sys/iomgr.h>
|
#include <sys/iomgr.h>
|
||||||
|
@ -153,14 +144,14 @@ enable_async_notification (int fd)
|
||||||
static int
|
static int
|
||||||
handle_accept_event (int err, gdb_client_data client_data)
|
handle_accept_event (int err, gdb_client_data client_data)
|
||||||
{
|
{
|
||||||
struct sockaddr_in sockaddr;
|
union gdb_sockaddr_u sockaddr;
|
||||||
socklen_t tmp;
|
socklen_t tmp;
|
||||||
|
|
||||||
if (debug_threads)
|
if (debug_threads)
|
||||||
debug_printf ("handling possible accept event\n");
|
debug_printf ("handling possible accept event\n");
|
||||||
|
|
||||||
tmp = sizeof (sockaddr);
|
tmp = sizeof (sockaddr.sa_in);
|
||||||
remote_desc = accept (listen_desc, (struct sockaddr *) &sockaddr, &tmp);
|
remote_desc = accept (listen_desc, &sockaddr.sa, &tmp);
|
||||||
if (remote_desc == -1)
|
if (remote_desc == -1)
|
||||||
perror_with_name ("Accept failed");
|
perror_with_name ("Accept failed");
|
||||||
|
|
||||||
|
@ -195,7 +186,7 @@ handle_accept_event (int err, gdb_client_data client_data)
|
||||||
|
|
||||||
/* Convert IP address to string. */
|
/* Convert IP address to string. */
|
||||||
fprintf (stderr, "Remote debugging from host %s\n",
|
fprintf (stderr, "Remote debugging from host %s\n",
|
||||||
inet_ntoa (sockaddr.sin_addr));
|
inet_ntoa (sockaddr.sa_in.sin_addr));
|
||||||
|
|
||||||
enable_async_notification (remote_desc);
|
enable_async_notification (remote_desc);
|
||||||
|
|
||||||
|
@ -224,7 +215,7 @@ remote_prepare (char *name)
|
||||||
static int winsock_initialized;
|
static int winsock_initialized;
|
||||||
#endif
|
#endif
|
||||||
int port;
|
int port;
|
||||||
struct sockaddr_in sockaddr;
|
union gdb_sockaddr_u sockaddr;
|
||||||
socklen_t tmp;
|
socklen_t tmp;
|
||||||
char *port_end;
|
char *port_end;
|
||||||
|
|
||||||
|
@ -269,11 +260,11 @@ remote_prepare (char *name)
|
||||||
setsockopt (listen_desc, SOL_SOCKET, SO_REUSEADDR, (char *) &tmp,
|
setsockopt (listen_desc, SOL_SOCKET, SO_REUSEADDR, (char *) &tmp,
|
||||||
sizeof (tmp));
|
sizeof (tmp));
|
||||||
|
|
||||||
sockaddr.sin_family = PF_INET;
|
sockaddr.sa_in.sin_family = PF_INET;
|
||||||
sockaddr.sin_port = htons (port);
|
sockaddr.sa_in.sin_port = htons (port);
|
||||||
sockaddr.sin_addr.s_addr = INADDR_ANY;
|
sockaddr.sa_in.sin_addr.s_addr = INADDR_ANY;
|
||||||
|
|
||||||
if (bind (listen_desc, (struct sockaddr *) &sockaddr, sizeof (sockaddr))
|
if (bind (listen_desc, &sockaddr.sa, sizeof (sockaddr.sa_in))
|
||||||
|| listen (listen_desc, 1))
|
|| listen (listen_desc, 1))
|
||||||
perror_with_name ("Can't bind address");
|
perror_with_name ("Can't bind address");
|
||||||
|
|
||||||
|
|
|
@ -6817,8 +6817,7 @@ run_inferior_command (char *cmd, int len)
|
||||||
|
|
||||||
#else /* !IN_PROCESS_AGENT */
|
#else /* !IN_PROCESS_AGENT */
|
||||||
|
|
||||||
#include <sys/socket.h>
|
#include "gdb_socket.h"
|
||||||
#include <sys/un.h>
|
|
||||||
|
|
||||||
#ifndef UNIX_PATH_MAX
|
#ifndef UNIX_PATH_MAX
|
||||||
#define UNIX_PATH_MAX sizeof(((struct sockaddr_un *) NULL)->sun_path)
|
#define UNIX_PATH_MAX sizeof(((struct sockaddr_un *) NULL)->sun_path)
|
||||||
|
@ -6837,7 +6836,7 @@ static int
|
||||||
init_named_socket (const char *name)
|
init_named_socket (const char *name)
|
||||||
{
|
{
|
||||||
int result, fd;
|
int result, fd;
|
||||||
struct sockaddr_un addr;
|
union gdb_sockaddr_u addr;
|
||||||
|
|
||||||
result = fd = socket (PF_UNIX, SOCK_STREAM, 0);
|
result = fd = socket (PF_UNIX, SOCK_STREAM, 0);
|
||||||
if (result == -1)
|
if (result == -1)
|
||||||
|
@ -6846,10 +6845,10 @@ init_named_socket (const char *name)
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
addr.sun_family = AF_UNIX;
|
addr.sa_un.sun_family = AF_UNIX;
|
||||||
|
|
||||||
strncpy (addr.sun_path, name, UNIX_PATH_MAX);
|
strncpy (addr.sa_un.sun_path, name, UNIX_PATH_MAX);
|
||||||
addr.sun_path[UNIX_PATH_MAX - 1] = '\0';
|
addr.sa_un.sun_path[UNIX_PATH_MAX - 1] = '\0';
|
||||||
|
|
||||||
result = access (name, F_OK);
|
result = access (name, F_OK);
|
||||||
if (result == 0)
|
if (result == 0)
|
||||||
|
@ -6865,7 +6864,7 @@ init_named_socket (const char *name)
|
||||||
warning ("socket %s already exists; overwriting", name);
|
warning ("socket %s already exists; overwriting", name);
|
||||||
}
|
}
|
||||||
|
|
||||||
result = bind (fd, (struct sockaddr *) &addr, sizeof (addr));
|
result = bind (fd, &addr.sa, sizeof (addr.sa_un));
|
||||||
if (result == -1)
|
if (result == -1)
|
||||||
{
|
{
|
||||||
warning ("bind failed: %s", strerror (errno));
|
warning ("bind failed: %s", strerror (errno));
|
||||||
|
@ -7164,17 +7163,17 @@ gdb_agent_helper_thread (void *arg)
|
||||||
while (1)
|
while (1)
|
||||||
{
|
{
|
||||||
socklen_t tmp;
|
socklen_t tmp;
|
||||||
struct sockaddr_un sockaddr;
|
union gdb_sockaddr_u sockaddr;
|
||||||
int fd;
|
int fd;
|
||||||
char buf[1];
|
char buf[1];
|
||||||
int ret;
|
int ret;
|
||||||
int stop_loop = 0;
|
int stop_loop = 0;
|
||||||
|
|
||||||
tmp = sizeof (sockaddr);
|
tmp = sizeof (sockaddr.sa_un);
|
||||||
|
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
fd = accept (listen_fd, &sockaddr, &tmp);
|
fd = accept (listen_fd, &sockaddr.sa, &tmp);
|
||||||
}
|
}
|
||||||
/* It seems an ERESTARTSYS can escape out of accept. */
|
/* It seems an ERESTARTSYS can escape out of accept. */
|
||||||
while (fd == -512 || (fd == -1 && errno == EINTR));
|
while (fd == -512 || (fd == -1 && errno == EINTR));
|
||||||
|
|
|
@ -37,6 +37,8 @@
|
||||||
|
|
||||||
#include <sys/time.h>
|
#include <sys/time.h>
|
||||||
|
|
||||||
|
#include "gdb_socket.h"
|
||||||
|
|
||||||
#ifdef USE_WIN32API
|
#ifdef USE_WIN32API
|
||||||
#include <winsock2.h>
|
#include <winsock2.h>
|
||||||
#ifndef ETIMEDOUT
|
#ifndef ETIMEDOUT
|
||||||
|
@ -45,10 +47,8 @@
|
||||||
#define close(fd) closesocket (fd)
|
#define close(fd) closesocket (fd)
|
||||||
#define ioctl ioctlsocket
|
#define ioctl ioctlsocket
|
||||||
#else
|
#else
|
||||||
#include <netinet/in.h>
|
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
#include <netdb.h>
|
#include <netdb.h>
|
||||||
#include <sys/socket.h>
|
|
||||||
#include <netinet/tcp.h>
|
#include <netinet/tcp.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -159,7 +159,7 @@ net_open (struct serial *scb, const char *name)
|
||||||
int n, port, tmp;
|
int n, port, tmp;
|
||||||
int use_udp;
|
int use_udp;
|
||||||
struct hostent *hostent;
|
struct hostent *hostent;
|
||||||
struct sockaddr_in sockaddr;
|
union gdb_sockaddr_u sockaddr;
|
||||||
#ifdef USE_WIN32API
|
#ifdef USE_WIN32API
|
||||||
u_long ioarg;
|
u_long ioarg;
|
||||||
#else
|
#else
|
||||||
|
@ -199,9 +199,9 @@ net_open (struct serial *scb, const char *name)
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
sockaddr.sin_family = PF_INET;
|
sockaddr.sa_in.sin_family = PF_INET;
|
||||||
sockaddr.sin_port = htons (port);
|
sockaddr.sa_in.sin_port = htons (port);
|
||||||
memcpy (&sockaddr.sin_addr.s_addr, hostent->h_addr,
|
memcpy (&sockaddr.sa_in.sin_addr.s_addr, hostent->h_addr,
|
||||||
sizeof (struct in_addr));
|
sizeof (struct in_addr));
|
||||||
|
|
||||||
retry:
|
retry:
|
||||||
|
@ -220,7 +220,7 @@ net_open (struct serial *scb, const char *name)
|
||||||
|
|
||||||
/* Use Non-blocking connect. connect() will return 0 if connected
|
/* Use Non-blocking connect. connect() will return 0 if connected
|
||||||
already. */
|
already. */
|
||||||
n = connect (scb->fd, (struct sockaddr *) &sockaddr, sizeof (sockaddr));
|
n = connect (scb->fd, &sockaddr.sa, sizeof (sockaddr.sa_in));
|
||||||
|
|
||||||
if (n < 0)
|
if (n < 0)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue