mailing list of musl libc
 help / color / mirror / code / Atom feed
From: William Ahern <william@25thandClement.com>
To: musl@lists.openwall.com
Subject: Re: sockaddr_storage and GCC 6.1
Date: Thu, 26 May 2016 13:21:39 -0700	[thread overview]
Message-ID: <20160526202139.GA24268@wilbur.25thandClement.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 1850 bytes --]

I forgot to mention that I'm not subscribed to the list.

I was able to build GCC 6.1 last night, but on my OS X desktop. My Alpine
Linux development environment doesn't have the disk space. My test code
manually reproduced the structure definitions.

Both of the patches proposed appear to work, at least with strict aliasing
disabled. Attached is the source code I used to verify. Here's the output of
`make test`.

== ss-darwin-no-strict-aliasing ==
.ss_len:     0,1
.ss_family:  1,2
.__ss_pad1:  2,8
.__ss_align: 8,16
.__ss_pad2:  16,128
.sin_family: 1,2
.sin_port:   2,4
.sin_addr:   4,8
0x7f000001 (OKAY)

== ss-darwin-strict-aliasing ==
.ss_len:     0,1
.ss_family:  1,2
.__ss_pad1:  2,8
.__ss_align: 8,16
.__ss_pad2:  16,128
.sin_family: 1,2
.sin_port:   2,4
.sin_addr:   4,8
0x00000000 (FAIL)

== ss-musl0-no-strict-aliasing ==
.ss_family:    0,2
.__ss_align:   8,16
.__ss_padding: 16,128
.sin_family: 0,2
.sin_port:   2,4
.sin_addr:   4,8
0x00000000 (FAIL)

== ss-musl0-strict-aliasing ==
.ss_family:    0,2
.__ss_align:   8,16
.__ss_padding: 16,128
.sin_family: 0,2
.sin_port:   2,4
.sin_addr:   4,8
0x00000000 (FAIL)

== ss-musl1-no-strict-aliasing ==
.ss_family:       0,2
.__ss_family_pad: 2,8
.__ss_align:      8,16
.__ss_padding:    16,128
.sin_family: 0,2
.sin_port:   2,4
.sin_addr:   4,8
0x7f000001 (OKAY)

== ss-musl1-strict-aliasing ==
.ss_family:       0,2
.__ss_family_pad: 2,8
.__ss_align:      8,16
.__ss_padding:    16,128
.sin_family: 0,2
.sin_port:   2,4
.sin_addr:   4,8
0x00000000 (FAIL)

== ss-musl2-no-strict-aliasing ==
.ss_family:    0,2
.__ss_padding: 2,120
.__ss_align:   120,128
.sin_family: 0,2
.sin_port:   2,4
.sin_addr:   4,8
0x7f000001 (OKAY)

== ss-musl2-strict-aliasing ==
.ss_family:    0,2
.__ss_padding: 2,120
.__ss_align:   120,128
.sin_family: 0,2
.sin_port:   2,4
.sin_addr:   4,8
0x00000000 (FAIL)


[-- Attachment #2: Makefile --]
[-- Type: text/plain, Size: 995 bytes --]

CC=/usr/local/gcc6/bin/gcc
CPPFLAGS=
CFLAGS=-O2 -Wall -Wextra -std=c11

all: ss-darwin-strict-aliasing ss-darwin-no-strict-aliasing
all: ss-musl0-strict-aliasing ss-musl0-no-strict-aliasing
all: ss-musl1-strict-aliasing ss-musl1-no-strict-aliasing
all: ss-musl2-strict-aliasing ss-musl2-no-strict-aliasing

ss-darwin-strict-aliasing ss-darwin-no-strict-aliasing: ss.c
	$(CC) -o $@ ss.c $(CPPFLAGS) $(CFLAGS) -f$(@:ss-darwin-%=%)

ss-musl0-strict-aliasing ss-musl0-no-strict-aliasing: ss.c
	$(CC) -o $@ ss.c $(CPPFLAGS) -DMUSL_PATCH=0 $(CFLAGS) -f$(@:ss-musl0-%=%)

ss-musl1-strict-aliasing ss-musl1-no-strict-aliasing: ss.c
	$(CC) -o $@ ss.c $(CPPFLAGS) -DMUSL_PATCH=1 $(CFLAGS) -f$(@:ss-musl1-%=%)

ss-musl2-strict-aliasing ss-musl2-no-strict-aliasing: ss.c
	$(CC) -o $@ ss.c $(CPPFLAGS) -DMUSL_PATCH=2 $(CFLAGS) -f$(@:ss-musl2-%=%)

test: all
	@for T in ss-darwin* ss-musl*; do \
		printf "== %s ==\n" "$${T}"; \
		"./$${T}"; \
		printf "\n"; \
	done

clean:
	rm -f -- ./ss-darwin* ./ss-musl*

[-- Attachment #3: ss.c --]
[-- Type: text/plain, Size: 3376 bytes --]

#include <stddef.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <err.h>

#define vectorof(T, m) offsetof(T, m), (offsetof(T, m) + sizeof (*(T *)0).m)
#define ss_vectorof(m) vectorof(struct sockaddr_storage, m)
#define sin_vectorof(m) vectorof(struct sockaddr_in, m)

#if !defined MUSL_PATCH
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>

#if !__APPLE__
#error exected Darwin
#endif

static void
ss_layout(void)
{
	printf(".ss_len:     %zu,%zu\n", ss_vectorof(ss_len));
	printf(".ss_family:  %zu,%zu\n", ss_vectorof(ss_family));
	printf(".__ss_pad1:  %zu,%zu\n", ss_vectorof(__ss_pad1));
	printf(".__ss_align: %zu,%zu\n", ss_vectorof(__ss_align));
	printf(".__ss_pad2:  %zu,%zu\n", ss_vectorof(__ss_pad2));
}
#else

typedef unsigned socklen_t;
typedef unsigned short sa_family_t;

#if MUSL_PATCH == 2
struct sockaddr_storage {
	sa_family_t ss_family;
	char __ss_padding[128-sizeof(long)-sizeof(sa_family_t)];
	unsigned long __ss_align;
};

static void
ss_layout(void)
{
	printf(".ss_family:    %zu,%zu\n", ss_vectorof(ss_family));
	printf(".__ss_padding: %zu,%zu\n", ss_vectorof(__ss_padding));
	printf(".__ss_align:   %zu,%zu\n", ss_vectorof(__ss_align));
}
#elif MUSL_PATCH == 1
struct sockaddr_storage {
	sa_family_t ss_family;
	char __ss_family_pad[sizeof(long)-sizeof(sa_family_t)];
	unsigned long __ss_align;
	char __ss_padding[128-2*sizeof(unsigned long)];
};

static void
ss_layout(void)
{
	printf(".ss_family:       %zu,%zu\n", ss_vectorof(ss_family));
	printf(".__ss_family_pad: %zu,%zu\n", ss_vectorof(__ss_family_pad));
	printf(".__ss_align:      %zu,%zu\n", ss_vectorof(__ss_align));
	printf(".__ss_padding:    %zu,%zu\n", ss_vectorof(__ss_padding));
}
#else
struct sockaddr_storage {
	sa_family_t ss_family;
	unsigned long __ss_align;
	char __ss_padding[128-2*sizeof(unsigned long)];
};

static void
ss_layout(void)
{
	printf(".ss_family:    %zu,%zu\n", ss_vectorof(ss_family));
	printf(".__ss_align:   %zu,%zu\n", ss_vectorof(__ss_align));
	printf(".__ss_padding: %zu,%zu\n", ss_vectorof(__ss_padding));
}
#endif
                                        
typedef uint16_t in_port_t;
typedef uint32_t in_addr_t;
struct in_addr { in_addr_t s_addr; };
struct sockaddr_in {
	sa_family_t sin_family;
	in_port_t sin_port;
	struct in_addr sin_addr;
	uint8_t sin_zero[8];
};
#define INADDR_LOOPBACK UINT32_C(0x7f000001)
#define AF_INET 2
#undef htonl
#undef htons
#undef ntohl
#undef ntohs

uint32_t htonl(uint32_t);
uint16_t htons(uint16_t);
uint32_t ntohl(uint32_t);
uint16_t ntohs(uint16_t);
#endif

static void
sin_layout(void)
{
	printf(".sin_family: %zu,%zu\n", sin_vectorof(sin_family));
	printf(".sin_port:   %zu,%zu\n", sin_vectorof(sin_port));
	printf(".sin_addr:   %zu,%zu\n", sin_vectorof(sin_addr));
}

struct dbg_listener {
	struct sockaddr_storage addr;
};

int
main(void)
{
	struct dbg_listener *l;
	struct sockaddr_storage ss, *ss2, ss3;

	ss_layout();
	sin_layout();

	memset(&ss3, 0, sizeof ss3);
	ss3.ss_family = AF_INET;
	((struct sockaddr_in *)&ss3)->sin_addr.s_addr = htonl(INADDR_LOOPBACK);

	ss2 = &ss3;
	ss = *ss2;

	l = calloc(1, sizeof *l);
	l->addr = ss;

	printf("0x%.8x (%s)\n",
	    ntohl(((struct sockaddr_in *)(&l->addr))->sin_addr.s_addr),
	    (ntohl(((struct sockaddr_in *)(&l->addr))->sin_addr.s_addr) == INADDR_LOOPBACK)?
	        "OKAY" : "FAIL");

	return 0;
}

             reply	other threads:[~2016-05-26 20:21 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-05-26 20:21 William Ahern [this message]
  -- strict thread matches above, loose matches on Subject: below --
2016-05-24 22:07 William Ahern
2016-05-24 22:36 ` Rich Felker
2016-05-24 22:55   ` Rich Felker
2016-05-24 23:21     ` Rich Felker

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20160526202139.GA24268@wilbur.25thandClement.com \
    --to=william@25thandclement.com \
    --cc=musl@lists.openwall.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).