[Bug 856072]

Andreas Gal gal at uci.edu
Wed Sep 28 06:07:38 UTC 2011


Comment on attachment 562978
diff android 2.3 bionic code vs WIP patch

>--- getaddrinfo.c	2011-09-27 23:03:51.000000000 -0700
>+++ /Users/gal/workspace/B2G/./glue/gonk/bionic/libc/netbsd/net/getaddrinfo.c	2011-08-16 00:45:19.000000000 -0700
>@@ -31,16 +31,6 @@
>  */
> 
> /*
>- * This version of getaddrinfo.c is derived from Android 2.3 "Gingerbread",
>- * which contains uncredited changes by Android/Google developers.  It has
>- * been modified in 2011 for use in the Android build of Mozilla Firefox by
>- * Mozilla contributors (including Michael Edwards <m.k.edwards at gmail.com>).
>- * These changes are offered under the same license as the original NetBSD
>- * file, whose copyright and license are unchanged above.
>- */
>-#define ANDROID_CHANGES 1
>-
>-/*
>  * Issues to be discussed:
>  * - Thread safe-ness must be checked.
>  * - Return values.  There are nonstandard return values defined and used
>@@ -87,14 +77,10 @@
>  *	  friends.
>  */
> 
>-#include <fcntl.h>
> #include <sys/cdefs.h>
> #include <sys/types.h>
>-#include <sys/stat.h>
> #include <sys/param.h>
> #include <sys/socket.h>
>-#include <sys/un.h>
>-#include <sys/mman.h>
> #include <net/if.h>
> #include <netinet/in.h>
> #include <arpa/inet.h>
>@@ -102,10 +88,10 @@
> #include <assert.h>
> #include <ctype.h>
> #include <errno.h>
>-#include <fcntl.h>
> #include <netdb.h>
> #include "resolv_private.h"
> #include <stddef.h>
>+#include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
> #include <unistd.h>
>@@ -114,87 +100,6 @@
> #include <stdarg.h>
> #include "nsswitch.h"
> 
>-#ifdef ANDROID_CHANGES
>-#include <sys/system_properties.h>
>-#endif /* ANDROID_CHANGES */
>-
>-typedef struct _pseudo_FILE {
>-    int fd;
>-    off_t maplen;
>-    void* mapping;
>-    off_t offset;
>-} _pseudo_FILE;
>-
>-#define _PSEUDO_FILE_INITIALIZER { -1, 0, MAP_FAILED, 0 }
>-
>-static void
>-_pseudo_fclose(_pseudo_FILE* fp)
>-{
>-    fp->offset = 0;
>-    if (fp->mapping != MAP_FAILED) {
>-        (void) munmap(fp->mapping, fp->maplen);
>-        fp->mapping = MAP_FAILED;
>-    }
>-    fp->maplen = 0;
>-    if (fp->fd != -1) {
>-        (void) close(fp->fd);
>-        fp->fd = -1;
>-    }
>-}
>-
>-static _pseudo_FILE*
>-_pseudo_fopen_r(_pseudo_FILE* fp, const char* fname)
>-{
>-    struct stat statbuf;
>-    fp->fd = open(fname, O_RDONLY);
>-    if (fp->fd < 0) {
>-        fp->fd = -1;
>-        return NULL;
>-    }
>-    if ((0 != fstat(fp->fd, &statbuf)) || (statbuf.st_size <= 0)) {
>-        close(fp->fd);
>-        fp->fd = -1;
>-        return NULL;
>-    }
>-    fp->maplen = statbuf.st_size;
>-    fp->mapping = mmap(NULL, fp->maplen, PROT_READ, MAP_PRIVATE, fp->fd, 0);
>-    if (fp->mapping == MAP_FAILED) {
>-        close(fp->fd);
>-        fp->fd = -1;
>-        return NULL;
>-    }
>-    fp->offset = 0;
>-    return fp;
>-}
>-
>-static void
>-_pseudo_rewind(_pseudo_FILE* fp)
>-{
>-    fp->offset = 0;
>-}
>-
>-static char*
>-_pseudo_fgets(char* buf, int bufsize, _pseudo_FILE* fp)
>-{
>-    char* current;
>-    char* endp;
>-    int maxcopy = fp->maplen - fp->offset;
>-    if (fp->mapping == MAP_FAILED)
>-        return NULL;
>-    if (maxcopy > bufsize - 1)
>-        maxcopy = bufsize - 1;
>-    if (maxcopy <= 0)
>-        return NULL;
>-    //fprintf(stderr, "_pseudo_fgets(): copying up to %d bytes\n", maxcopy);
>-    current = ((char*) fp->mapping) + fp->offset;
>-    endp = memccpy(buf, current, '\n', maxcopy);
>-    if (endp)
>-        maxcopy = endp - buf;
>-    buf[maxcopy] = '\0';
>-    fp->offset += maxcopy;
>-    return buf;
>-}
>-
> typedef union sockaddr_union {
>     struct sockaddr     generic;
>     struct sockaddr_in  in;
>@@ -319,9 +224,9 @@
> static struct addrinfo *getanswer(const querybuf *, int, const char *, int,
> 	const struct addrinfo *);
> static int _dns_getaddrinfo(void *, void *, va_list);
>-static void _sethtent(_pseudo_FILE *);
>-static void _endhtent(_pseudo_FILE *);
>-static struct addrinfo *_gethtent(_pseudo_FILE *, const char *,
>+static void _sethtent(FILE **);
>+static void _endhtent(FILE **);
>+static struct addrinfo *_gethtent(FILE **, const char *,
>     const struct addrinfo *);
> static int _files_getaddrinfo(void *, void *, va_list);
> 
>@@ -391,20 +296,8 @@
> #define MATCH(x, y, w) 							\
> 	((x) == (y) || (/*CONSTCOND*/(w) && ((x) == ANY || (y) == ANY)))
> 
>-#pragma GCC visibility push(default)
>-
>-extern const char *
>-__wrap_gai_strerror(int ecode);
>-extern void
>-__wrap_freeaddrinfo(struct addrinfo *ai);
>-extern int
>-__wrap_getaddrinfo(const char *hostname, const char *servname,
>-    const struct addrinfo *hints, struct addrinfo **res);
>-
>-#pragma GCC visibility pop
>-
> const char *
>-__wrap_gai_strerror(int ecode)
>+gai_strerror(int ecode)
> {
> 	if (ecode < 0 || ecode > EAI_MAX)
> 		ecode = EAI_MAX;
>@@ -412,7 +305,7 @@
> }
> 
> void
>-__wrap_freeaddrinfo(struct addrinfo *ai)
>+freeaddrinfo(struct addrinfo *ai)
> {
> 	struct addrinfo *next;
> 
>@@ -499,7 +392,7 @@
> }
> 
> int
>-__wrap_getaddrinfo(const char *hostname, const char *servname,
>+getaddrinfo(const char *hostname, const char *servname,
>     const struct addrinfo *hints, struct addrinfo **res)
> {
> 	struct addrinfo sentinel;
>@@ -694,7 +587,7 @@
>  free:
>  bad:
> 	if (sentinel.ai_next)
>-		__wrap_freeaddrinfo(sentinel.ai_next);
>+		freeaddrinfo(sentinel.ai_next);
> 	*res = NULL;
> 	return error;
> }
>@@ -755,7 +648,7 @@
> 
> free:
> 	if (result)
>-		__wrap_freeaddrinfo(result);
>+		freeaddrinfo(result);
> 	return error;
> }
> 
>@@ -823,7 +716,7 @@
> 
> free:
> 	if (sentinel.ai_next)
>-		__wrap_freeaddrinfo(sentinel.ai_next);
>+		freeaddrinfo(sentinel.ai_next);
> 	return error;
> }
> 
>@@ -910,7 +803,7 @@
> free:
> bad:
> 	if (sentinel.ai_next)
>-		__wrap_freeaddrinfo(sentinel.ai_next);
>+		freeaddrinfo(sentinel.ai_next);
> 	return error;
> }
> 
>@@ -1473,6 +1366,8 @@
> 		const struct sockaddr_in6 *addr6 = (const struct sockaddr_in6 *)addr;
> 		if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr)) {
> 			return 0;
>+		} else if (IN6_IS_ADDR_ULA(&addr6->sin6_addr)) {
>+			return 1;
> 		} else if (IN6_IS_ADDR_V4MAPPED(&addr6->sin6_addr)) {
> 			return 3;
> 		} else if (IN6_IS_ADDR_6TO4(&addr6->sin6_addr)) {
>@@ -1512,6 +1407,8 @@
> 		const struct sockaddr_in6 *addr6 = (const struct sockaddr_in6 *)addr;
> 		if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr)) {
> 			return 60;
>+		} else if (IN6_IS_ADDR_ULA(&addr6->sin6_addr)) {
>+			return 50;
> 		} else if (IN6_IS_ADDR_V4MAPPED(&addr6->sin6_addr)) {
> 			return 30;
> 		} else if (IN6_IS_ADDR_6TO4(&addr6->sin6_addr)) {
>@@ -1900,24 +1797,27 @@
> }
> 
> static void
>-_sethtent(_pseudo_FILE *hostf)
>+_sethtent(FILE **hostf)
> {
> 
>-	if (hostf->mapping == MAP_FAILED)
>-		(void) _pseudo_fopen_r(hostf, _PATH_HOSTS);
>+	if (!*hostf)
>+		*hostf = fopen(_PATH_HOSTS, "r" );
> 	else
>-		_pseudo_rewind(hostf);
>+		rewind(*hostf);
> }
> 
> static void
>-_endhtent(_pseudo_FILE *hostf)
>+_endhtent(FILE **hostf)
> {
> 
>-	(void) _pseudo_fclose(hostf);
>+	if (*hostf) {
>+		(void) fclose(*hostf);
>+		*hostf = NULL;
>+	}
> }
> 
> static struct addrinfo *
>-_gethtent(_pseudo_FILE *hostf, const char *name, const struct addrinfo *pai)
>+_gethtent(FILE **hostf, const char *name, const struct addrinfo *pai)
> {
> 	char *p;
> 	char *cp, *tname, *cname;
>@@ -1926,18 +1826,15 @@
> 	const char *addr;
> 	char hostbuf[8*1024];
> 
>-	//fprintf(stderr, "_gethtent() name = '%s'\n", name);
>+//	fprintf(stderr, "_gethtent() name = '%s'\n", name);
> 	assert(name != NULL);
> 	assert(pai != NULL);
> 
>-	if (hostf->mapping == MAP_FAILED)
>-		(void) _pseudo_fopen_r(hostf, _PATH_HOSTS);
>-	if (hostf->mapping == MAP_FAILED)
>+	if (!*hostf && !(*hostf = fopen(_PATH_HOSTS, "r" )))
> 		return (NULL);
>  again:
>-	if (!(p = _pseudo_fgets(hostbuf, sizeof hostbuf, hostf)))
>+	if (!(p = fgets(hostbuf, sizeof hostbuf, *hostf)))
> 		return (NULL);
>-	//fprintf(stderr, "/etc/hosts line: %s", p);
> 	if (*p == '#')
> 		goto again;
> 	if (!(cp = strpbrk(p, "#\n")))
>@@ -1959,7 +1856,7 @@
> 		tname = cp;
> 		if ((cp = strpbrk(cp, " \t")) != NULL)
> 			*cp++ = '\0';
>-		//fprintf(stderr, "\ttname = '%s'\n", tname);
>+//		fprintf(stderr, "\ttname = '%s'", tname);
> 		if (strcasecmp(name, tname) == 0)
> 			goto found;
> 	}
>@@ -1968,7 +1865,7 @@
> found:
> 	hints = *pai;
> 	hints.ai_flags = AI_NUMERICHOST;
>-	error = __wrap_getaddrinfo(addr, NULL, &hints, &res0);
>+	error = getaddrinfo(addr, NULL, &hints, &res0);
> 	if (error)
> 		goto again;
> 	for (res = res0; res; res = res->ai_next) {
>@@ -1977,7 +1874,7 @@
> 
> 		if (pai->ai_flags & AI_CANONNAME) {
> 			if (get_canonname(pai, res, cname) != 0) {
>-				__wrap_freeaddrinfo(res0);
>+				freeaddrinfo(res0);
> 				goto again;
> 			}
> 		}
>@@ -1993,12 +1890,12 @@
> 	const struct addrinfo *pai;
> 	struct addrinfo sentinel, *cur;
> 	struct addrinfo *p;
>-	_pseudo_FILE hostf = _PSEUDO_FILE_INITIALIZER;
>+	FILE *hostf = NULL;
> 
> 	name = va_arg(ap, char *);
> 	pai = va_arg(ap, struct addrinfo *);
> 
>-	//fprintf(stderr, "_files_getaddrinfo() name = '%s'\n", name);
>+//	fprintf(stderr, "_files_getaddrinfo() name = '%s'\n", name);
> 	memset(&sentinel, 0, sizeof(sentinel));
> 	cur = &sentinel;
>

-- 
You received this bug notification because you are a member of Mozilla
Bugs, which is subscribed to Mozilla Firefox.
https://bugs.launchpad.net/bugs/856072

Title:
  Bionic domain name functions are not thread-safe on pre-3.0 Android

To manage notifications about this bug go to:
https://bugs.launchpad.net/firefox/+bug/856072/+subscriptions




More information about the Ubuntu-mozillateam-bugs mailing list