Bug 801 - NPTL pthread_create siliently fails to create thread if non-zero priority used with SCHED_OTHER policy
Summary: NPTL pthread_create siliently fails to create thread if non-zero priority use...
Status: RESOLVED FIXED
Alias: None
Product: glibc
Classification: Unclassified
Component: nptl (show other bugs)
Version: 2.3.4
: P2 normal
Target Milestone: ---
Assignee: Ulrich Drepper
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2005-03-24 00:12 UTC by Steve Hawkes
Modified: 2018-04-19 14:58 UTC (History)
1 user (show)

See Also:
Host: ppc64-yellowdog-linux
Target: ppc64-yellowdog-linux
Build: ppc64-yellowdog-linux
Last reconfirmed:
fweimer: security-


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Steve Hawkes 2005-03-24 00:12:29 UTC
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);
}
Comment 1 Ulrich Drepper 2005-03-24 01:01:41 UTC
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.
Comment 2 Steve Hawkes 2005-03-25 23:58:41 UTC
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.
Comment 3 Jakub Jelinek 2005-03-26 07:50:49 UTC
Reopening to...