From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 27094 invoked by alias); 24 Mar 2005 00:12:52 -0000 Mailing-List: contact glibc-bugs-help@sources.redhat.com; run by ezmlm Precedence: bulk List-Subscribe: List-Post: List-Help: , Sender: glibc-bugs-owner@sources.redhat.com Received: (qmail 27008 invoked by uid 48); 24 Mar 2005 00:12:37 -0000 Date: Thu, 24 Mar 2005 00:12:00 -0000 From: "steve dot hawkes at motorola dot com" To: glibc-bugs@sources.redhat.com Message-ID: <20050324001227.801.steve.hawkes@motorola.com> Reply-To: sourceware-bugzilla@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 X-Bugzilla-Reason: CC X-SW-Source: 2005-03/txt/msg00118.txt.bz2 List-Id: 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 #include #include #include 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.