public inbox for glibc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug nptl/801] New: NPTL pthread_create siliently fails to create thread if non-zero priority used with SCHED_OTHER policy
@ 2005-03-24  0:12 steve dot hawkes at motorola dot com
  2005-03-24  1:01 ` [Bug nptl/801] " drepper at redhat dot com
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: steve dot hawkes at motorola dot com @ 2005-03-24  0:12 UTC (permalink / raw)
  To: glibc-bugs

The NPTL version of pthread_create returns a value of zero but fails to create 
a thread if the thread attributes specify non-inherited scheduling parameters, 
a non-zero scheduling priority, and the SCHED_OTHER scheduling policy. The 
function should return EINVAL under these conditions. The problem occurs 
because nptl/pthread_create.c declares a local variable 'err' in an 'if' 
statement (see line 409, " INTERNAL_SYSCALL_DECL (err);")
which shadows the function-scope 'err' variable (line 352, " int err;"). As a 
result, when the attributes specified above are used pthread_create detects 
the error (priority must be zero with SCHED_OTHER) and sets the 
innermost 'err' variable to EINVAL, but then exits the 'if' statement, causing 
the innermost 'err' variable to go out of scope and the return value of the 
function to be left uninitialized.

This problem is present in glibc 2.3.3, 2.3.4, and the latest CVS source (as 
of March 23, 2005).

It may be prudent to extend the existing glibc test cases to cover this case.

Here is an untested patch that should correct the problem:

--- glibc-2.3.4/nptl/pthread_create.c   2005-03-23 16:48:55.000000000 -0600
+++ glibc-2.3.4-fixed/nptl/pthread_create.c     2005-03-23 16:52:18.000000000 -
0600
@@ -349,7 +349,7 @@
   STACK_VARIABLES;
   const struct pthread_attr *iattr;
   struct pthread *pd;
-  int err;
+  int result;
 
   iattr = (struct pthread_attr *) attr;
   if (iattr == NULL)
@@ -357,11 +357,11 @@
        accessing far-away memory.  */
     iattr = &default_attr;
 
-  err = ALLOCATE_STACK (iattr, &pd);
-  if (__builtin_expect (err != 0, 0))
+  result = ALLOCATE_STACK (iattr, &pd);
+  if (__builtin_expect (result != 0, 0))
     /* Something went wrong.  Maybe a parameter of the attributes is
        invalid or we could not allocate memory.  */
-    return err;
+    return result;
 
 
   /* Initialize the TCB.  All initializations with zero should be
@@ -434,7 +434,7 @@
       if (pd->schedparam.sched_priority < minprio
          || pd->schedparam.sched_priority > maxprio)
        {
-         err = EINVAL;
+         result = EINVAL;
          goto errout;
        }
     }
@@ -448,8 +448,8 @@
   bool is_detached = IS_DETACHED (pd);
 
   /* Start the thread.  */
-  err = create_thread (pd, iattr, STACK_VARIABLES_ARGS);
-  if (err != 0)
+  result = create_thread (pd, iattr, STACK_VARIABLES_ARGS);
+  if (result != 0)
     {
       /* Something went wrong.  Free the resources.  */
       if (!is_detached)
@@ -457,7 +457,7 @@
        errout:
          __deallocate_stack (pd);
        }
-      return err;
+      return result;
     }
 
   return 0;

Below is an example program that illustrates the problem. The output would 
look like this if the problem were not present:

Will create thread with priority 0
Thread creation returned 0
**** Success, created thread exiting
Main thread exiting

The output with the bug looks like this:

Will create thread with priority 10
Thread creation returned 0
Main thread exiting

Here is the program:

#include <pthread.h>
#include <sched.h>
#include <stdio.h>
#include <unistd.h>


void*
myThread(void* arg)
{
    printf("**** Success, created thread exiting\n");

    return (0);
}


int
main(int	argc,
     char**	argv)
{
    pthread_attr_t	attr;
    int			priority =  10;
    struct sched_param	schedParam;
    int			status;
    pthread_t		threadId;


    pthread_attr_init(&attr);
    schedParam.sched_priority =  priority;
    if (pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED) != 0)
    {
	printf("pthread_attr_setinheritsched failed\n");
    }
    else if (pthread_attr_setschedpolicy(&attr, SCHED_OTHER) != 0)
    {
	printf("pthread_attr_setschedpolicy failed\n");
    }
    else if (pthread_attr_setschedparam(&attr, &schedParam) != 0)
    {
	printf("pthread_attr_setschedparam failed\n");
    }
    else
    {
	printf("Will create thread with priority %d\n", priority);
	status = pthread_create(&threadId, &attr, myThread, 0);
	printf("Thread creation returned %d\n", status);

	sleep(2);
    }

    printf("Main thread exiting\n");

    return (0);
}

-- 
           Summary: NPTL pthread_create siliently fails to create thread if
                    non-zero priority used with SCHED_OTHER policy
           Product: glibc
           Version: 2.3.4
            Status: NEW
          Severity: normal
          Priority: P2
         Component: nptl
        AssignedTo: drepper at redhat dot com
        ReportedBy: steve dot hawkes at motorola dot com
                CC: glibc-bugs at sources dot redhat dot com
 GCC build triplet: ppc64-yellowdog-linux
  GCC host triplet: ppc64-yellowdog-linux
GCC target triplet: ppc64-yellowdog-linux


http://sources.redhat.com/bugzilla/show_bug.cgi?id=801

------- You are receiving this mail because: -------
You are on the CC list for the bug, or are watching someone who is.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug nptl/801] NPTL pthread_create siliently fails to create thread if non-zero priority used with SCHED_OTHER policy
  2005-03-24  0:12 [Bug nptl/801] New: NPTL pthread_create siliently fails to create thread if non-zero priority used with SCHED_OTHER policy steve dot hawkes at motorola dot com
@ 2005-03-24  1:01 ` drepper at redhat dot com
  2005-03-25 23:58 ` steve dot hawkes at motorola dot com
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: drepper at redhat dot com @ 2005-03-24  1:01 UTC (permalink / raw)
  To: glibc-bugs


------- Additional Comments From drepper at redhat dot com  2005-03-24 01:01 -------
There is no bug.  The pthread_create calls fails, returns EINVAL, and that's it.
 There is

**** Success, created thread exiting

output since pthread_create doesn't create a thread.

-- 
           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |INVALID


http://sources.redhat.com/bugzilla/show_bug.cgi?id=801

------- You are receiving this mail because: -------
You are on the CC list for the bug, or are watching someone who is.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug nptl/801] NPTL pthread_create siliently fails to create thread if non-zero priority used with SCHED_OTHER policy
  2005-03-24  0:12 [Bug nptl/801] New: NPTL pthread_create siliently fails to create thread if non-zero priority used with SCHED_OTHER policy steve dot hawkes at motorola dot com
  2005-03-24  1:01 ` [Bug nptl/801] " drepper at redhat dot com
@ 2005-03-25 23:58 ` steve dot hawkes at motorola dot com
  2005-03-26  7:50 ` jakub at redhat dot com
  2005-03-26  7:51 ` jakub at redhat dot com
  3 siblings, 0 replies; 5+ messages in thread
From: steve dot hawkes at motorola dot com @ 2005-03-25 23:58 UTC (permalink / raw)
  To: glibc-bugs


------- Additional Comments From steve dot hawkes at motorola dot com  2005-03-25 23:58 -------
Subject: RE:  NPTL pthread_create siliently fails to create 
	thread if non-zero priority used with SCHED_OTHER policy

There is a bug. The pthread_create() call does not return EINVAL. It returns
0 but does not create a thread. Please look more closely at the patch. The
problem is really quite straightforward. If the description of the problem
isn't clear enough (although I believe it is), just try compiling
pthread_create.c with -Wshadow. You will see that the declaration of 'err'
on line 409 shadows that on line 351, which is not what was intended by the
developer of the code and which causes it to fail silently.

In the example code, this line is printed from the created thread only if
the bug is fixed:

**** Success, created thread exiting

When the example code is run on the unpatched pthread_create.c, the above
line is not printed because the thread is never created, and
pthread_create() does not return EINVAL, it returns 0, as shown by this
line:

Thread creation returned 0

-----Original Message-----
From: drepper at redhat dot com
[mailto:sourceware-bugzilla@sources.redhat.com] 
Sent: Wednesday, March 23, 2005 7:02 PM
To: Hawkes Steve-FSH016
Subject: [Bug nptl/801] NPTL pthread_create siliently fails to create thread
if non-zero priority used with SCHED_OTHER policy



------- Additional Comments From drepper at redhat dot com  2005-03-24 01:01
------- There is no bug.  The pthread_create calls fails, returns EINVAL,
and that's it.  There is

**** Success, created thread exiting

output since pthread_create doesn't create a thread.

-- 
           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |INVALID


http://sources.redhat.com/bugzilla/show_bug.cgi?id=801

------- You are receiving this mail because: -------
You reported the bug, or are watching the reporter.


-- 


http://sources.redhat.com/bugzilla/show_bug.cgi?id=801

------- You are receiving this mail because: -------
You are on the CC list for the bug, or are watching someone who is.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug nptl/801] NPTL pthread_create siliently fails to create thread if non-zero priority used with SCHED_OTHER policy
  2005-03-24  0:12 [Bug nptl/801] New: NPTL pthread_create siliently fails to create thread if non-zero priority used with SCHED_OTHER policy steve dot hawkes at motorola dot com
  2005-03-24  1:01 ` [Bug nptl/801] " drepper at redhat dot com
  2005-03-25 23:58 ` steve dot hawkes at motorola dot com
@ 2005-03-26  7:50 ` jakub at redhat dot com
  2005-03-26  7:51 ` jakub at redhat dot com
  3 siblings, 0 replies; 5+ messages in thread
From: jakub at redhat dot com @ 2005-03-26  7:50 UTC (permalink / raw)
  To: glibc-bugs


------- Additional Comments From jakub at redhat dot com  2005-03-26 07:50 -------
Reopening to...

-- 
           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |REOPENED
         Resolution|INVALID                     |


http://sources.redhat.com/bugzilla/show_bug.cgi?id=801

------- You are receiving this mail because: -------
You are on the CC list for the bug, or are watching someone who is.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug nptl/801] NPTL pthread_create siliently fails to create thread if non-zero priority used with SCHED_OTHER policy
  2005-03-24  0:12 [Bug nptl/801] New: NPTL pthread_create siliently fails to create thread if non-zero priority used with SCHED_OTHER policy steve dot hawkes at motorola dot com
                   ` (2 preceding siblings ...)
  2005-03-26  7:50 ` jakub at redhat dot com
@ 2005-03-26  7:51 ` jakub at redhat dot com
  3 siblings, 0 replies; 5+ messages in thread
From: jakub at redhat dot com @ 2005-03-26  7:51 UTC (permalink / raw)
  To: glibc-bugs


------- Additional Comments From jakub at redhat dot com  2005-03-26 07:51 -------
... mark as fixed.
http://sources.redhat.com/cgi-bin/cvsweb.cgi/libc/nptl/pthread_create.c.diff?cvsroot=glibc&r1=1.41&r2=1.42

-- 
           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|REOPENED                    |RESOLVED
         Resolution|                            |FIXED


http://sources.redhat.com/bugzilla/show_bug.cgi?id=801

------- You are receiving this mail because: -------
You are on the CC list for the bug, or are watching someone who is.


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2005-03-26  7:51 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-03-24  0:12 [Bug nptl/801] New: NPTL pthread_create siliently fails to create thread if non-zero priority used with SCHED_OTHER policy steve dot hawkes at motorola dot com
2005-03-24  1:01 ` [Bug nptl/801] " drepper at redhat dot com
2005-03-25 23:58 ` steve dot hawkes at motorola dot com
2005-03-26  7:50 ` jakub at redhat dot com
2005-03-26  7:51 ` jakub at redhat dot com

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 read-only IMAP folder(s) and NNTP newsgroup(s).