public inbox for glibc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "steve dot hawkes at motorola dot com" <sourceware-bugzilla@sources.redhat.com>
To: glibc-bugs@sources.redhat.com
Subject: [Bug nptl/801] New: NPTL pthread_create siliently fails to create thread if non-zero priority used with SCHED_OTHER policy
Date: Thu, 24 Mar 2005 00:12:00 -0000	[thread overview]
Message-ID: <20050324001227.801.steve.hawkes@motorola.com> (raw)

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.


             reply	other threads:[~2005-03-24  0:12 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-03-24  0:12 steve dot hawkes at motorola dot com [this message]
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

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=20050324001227.801.steve.hawkes@motorola.com \
    --to=sourceware-bugzilla@sources.redhat.com \
    --cc=glibc-bugs@sources.redhat.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.
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).