From nobody@FreeBSD.org  Thu May 29 21:50:58 2008
Return-Path: <nobody@FreeBSD.org>
Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34])
	by hub.freebsd.org (Postfix) with ESMTP id C891210656AD
	for <freebsd-gnats-submit@FreeBSD.org>; Thu, 29 May 2008 21:50:58 +0000 (UTC)
	(envelope-from nobody@FreeBSD.org)
Received: from www.freebsd.org (www.freebsd.org [IPv6:2001:4f8:fff6::21])
	by mx1.freebsd.org (Postfix) with ESMTP id 92FE48FC27
	for <freebsd-gnats-submit@FreeBSD.org>; Thu, 29 May 2008 21:50:58 +0000 (UTC)
	(envelope-from nobody@FreeBSD.org)
Received: from www.freebsd.org (localhost [127.0.0.1])
	by www.freebsd.org (8.14.2/8.14.2) with ESMTP id m4TLnHGJ020294
	for <freebsd-gnats-submit@FreeBSD.org>; Thu, 29 May 2008 21:49:17 GMT
	(envelope-from nobody@www.freebsd.org)
Received: (from nobody@localhost)
	by www.freebsd.org (8.14.2/8.14.1/Submit) id m4TLnHlZ020293;
	Thu, 29 May 2008 21:49:17 GMT
	(envelope-from nobody)
Message-Id: <200805292149.m4TLnHlZ020293@www.freebsd.org>
Date: Thu, 29 May 2008 21:49:17 GMT
From: Martin Simmons <martin@lispworks.com>
To: freebsd-gnats-submit@FreeBSD.org
Subject: 7.0 shared libs cannot read errno when loaded into a 6.0 pthreaded program
X-Send-Pr-Version: www-3.1
X-GNATS-Notify:

>Number:         124114
>Category:       kern
>Synopsis:       7.0 shared libs cannot read errno when loaded into a 6.0 pthreaded program
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    freebsd-bugs
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Thu May 29 22:00:10 UTC 2008
>Closed-Date:    Sun Jun 01 08:27:09 UTC 2008
>Last-Modified:  Sun Jun 01 08:27:09 UTC 2008
>Originator:     Martin Simmons
>Release:        7.0-RELEASE
>Organization:
LispWorks Ltd
>Environment:
FreeBSD heapbsd7.cam.lispworks.com 7.0-RELEASE FreeBSD 7.0-RELEASE #0: Sun Feb 24 19:59:52 UTC 2008     root@logan.cse.buffalo.edu:/usr/obj/usr/src/sys/GENERIC  i386

>Description:
There is something wrong with the way that errno is resolved when running 6.0 programs on 7.0-RELEASE with the compat6x-i386-6.3.602114.200711 pkg.

If a 7.0 shared library that uses errno is loaded into a 6.0 program that uses pthreads, then the value of errno in the library code will be incorrect.  In particular, it will access the global errno value instead of the thread-specific one.

I've included two test cases.  The X11 one is pretty much how the problem was discovered.  The libc one is doing the same thing as the libX11 function _XRead, where the problem occurs in the X11 example.

It also fails when compiled on FreeBSD 5.4 and run on FreeBSD 7.0 with compat5x.

This kind of code worked without any problems when built and run on FreeBSD 5 and also when run on FreeBSD 6 with compat5x.

>How-To-Repeat:
Test case 1: X11
----------------
a. Make the file below available on a 6.0 machine.

$ cat xerror.c
#include <X11/Xlib.h>
#include <stdio.h>
#include <pthread.h>

void *do_x11(void *arg)
{
  Display *disp;
  Atom primary;
  Window root;
  Atom actual_type;
  int actual_format;
  unsigned long nitems;
  unsigned long bytesafter;
  unsigned char *prop;
  int i;

  printf("Opening display\n");
  disp = XOpenDisplay(NULL);
  primary = XInternAtom(disp, "PRIMARY", 0);
  root = XRootWindow(disp, DefaultScreen(disp));
  printf("Atom is %d\nRoot is %d\n", (int)primary, (int)root);
  fflush(stdout);

  for (i = 0; i < 10000; i++) {
    XGetWindowProperty(disp, root, primary, 0, 0, False, primary,
		       &actual_type,
		       &actual_format,
		       &nitems,
		       &bytesafter,
		       &prop);
  }

  printf("Closing display\n");
  fflush(stdout);
  XCloseDisplay(disp);
  return NULL;
}

int main()
{
  pthread_t pt;

  pthread_create(&pt, NULL, do_x11, NULL);
  pthread_join(pt, NULL);

  return 0;
}


b. Compile it on a 6.0 machine to make a.out:
$ cc -pthread -I/usr/X11R6/include xerror.c -L/usr/X11R6/lib -lX11

c. Copy a.out to a 7.0 machine and run it with compat6x installed.
$ ./a.out
Opening display
Atom is 1
Root is 91
XIO:  fatal IO error 0 (Unknown error: 0) on X server "heap:0.0"
      after 8 requests (7 known processed) with 0 events remaining.

d. It should return without printing any X errors


Test case 2: libc
-----------------
a. Make the two files below available on a 6.0 machine.

$ cat lib.c
#include <errno.h>
#include <unistd.h>
int readerror()
{
  char buf[1];
  read(-1, buf, 1);
  return errno;
}

$ cat user.c
#include <stdio.h>
#include <pthread.h>

extern int readerror(void);

void *do_error(void *arg)
{
  printf("errno = %d\n", readerror());
}

int main()
{
  pthread_t pt;

  pthread_create(&pt, NULL, do_error, NULL);
  pthread_join(pt, NULL);

  return 0;
}

b. Compile them on a 6.0 machine to make lib.so and a.out:
$ cc -shared -o lib.so lib.c
$ cc -pthread user.c lib.so

c. Copy a.out and lib.c to a 7.0 machine and compile lib.c again:
$ cc -shared -o lib.so lib.c

d. Run the 6.0 a.out on the 7.0 machine with compat6x installed:
$ env LD_LIBRARY_PATH=`pwd` ./a.out
errno = 0

e. It should print errno = 9 (i.e. EBADF).


>Fix:


>Release-Note:
>Audit-Trail:
State-Changed-From-To: open->feedback 
State-Changed-By: remko 
State-Changed-When: Fri May 30 14:39:36 UTC 2008 
State-Changed-Why:  
I asked for feedback. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=124114 

From: Remko Lodder <remko@FreeBSD.org>
To: Martin Simmons <martin@lispworks.com>
Cc: freebsd-gnats-submit@FreeBSD.org
Subject: Re: misc/124114: 7.0 shared libs cannot read errno when loaded into
 a	6.0 pthreaded program
Date: Fri, 30 May 2008 16:39:24 +0200

 Martin Simmons wrote:
 
 The compat packages give the original libraries that were part of the 
 6.x release that it's being based upon. Ofcourse a 7.x library wouldn't
 work with a 6.x binary (especially if the library had changed or something).
 
 Would it be possible for you to link against the compat-libraries?
 (I think they are in /usr/local/lib/compat).
 
 Thanks,
 remko
 
 -- 
 
 /"\   Best regards,                      | remko@FreeBSD.org
 \ /   Remko Lodder                       | remko@EFnet
   X    http://www.evilcoder.org/          |
 / \   ASCII Ribbon Campaign              | Against HTML Mail and News

From: Kris Kennaway <kris@FreeBSD.org>
To: Martin Simmons <martin@lispworks.com>
Cc: freebsd-gnats-submit@FreeBSD.org
Subject: Re: misc/124114: 7.0 shared libs cannot read errno when loaded into
 a	6.0 pthreaded program
Date: Fri, 30 May 2008 21:21:32 +0200

 Martin Simmons wrote:
 
 >> Description:
 > There is something wrong with the way that errno is resolved when running 6.0 programs on 7.0-RELEASE with the compat6x-i386-6.3.602114.200711 pkg.
 > 
 > If a 7.0 shared library that uses errno is loaded into a 6.0 program that uses pthreads, then the value of errno in the library code will be incorrect.  In particular, it will access the global errno value instead of the thread-specific one.
 
 This is the expected behaviour.
 
 You *cannot*, in general, expect binaries linked to a mixture of 
 libraries from different releases to work.  It is possible in restricted 
 circumstances to carefully design your a library so that this will not 
 happen, but random libraries do not have this property.
 
 Kris

From: Martin Simmons <martin@lispworks.com>
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: misc/124114: 7.0 shared libs cannot read errno when loaded into a 6.0 pthreaded program
Date: Fri, 30 May 2008 21:08:31 +0100

 Sorry for the noise.  I've misunderstood the scope of the compat packages.  I
 thought they were to designed to allow prebuilt 6.x binaries to run on 7.x,
 but I guess that only works if all of the dependencies are core FreeBSD
 libraries, not on the ports like X11.
 
 It works fine if I arrange to use the 6.x X11 libraries.  Is there a correct
 place to install these or is LD_LIBRARY_PATH the only solution?
 
 __Martin
State-Changed-From-To: feedback->closed 
State-Changed-By: remko 
State-Changed-When: Sun Jun 1 08:27:09 UTC 2008 
State-Changed-Why:  
This is actually working as designed, both Kris and me explained that to 
the submitter, who understands that. Closing the ticket. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=124114 
>Unformatted:
