Bug 91303

Summary: program using getcontext/setcontext/swapcontext crashes if LD_LIBRARY_PATH contains /lib and application is linked with libpthread
Product: [Retired] Red Hat Linux Reporter: Mohammed Abouzour <mabouzou>
Component: glibcAssignee: Jakub Jelinek <jakub>
Status: CLOSED NOTABUG QA Contact: Brian Brock <bbrock>
Severity: high Docs Contact:
Priority: low    
Version: 8.0CC: fweimer
Target Milestone: ---   
Target Release: ---   
Hardware: All   
OS: Linux   
Whiteboard:
Fixed In Version: Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2003-06-10 03:28:29 UTC Type: ---
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:

Description Mohammed Abouzour 2003-05-21 03:44:41 UTC
Description of problem:
If LD_LIBRARY_PATH contain /lib, a program that uses 
getcontext/setcontext/swapcontext function and links with 
libpthread  will crash if it tries to access the errno value.
The crash will happen after the first swapcontext() call. If
LD_LIBRARY_PATH is unset or /lib is not in the list of directories,
program runs correctly.
The following foo.c program reproduces the problem as per the steps below:
#include <stdlib.h>
#include <stdio.h>
#include <ucontext.h>
#include <malloc.h>
 
ucontext_t func, main_func;
 
void MyFunc()
{
    printf( "MyFunc running\n" ); // this will crash 
    printf( "Now func switching back to main_func\n" );
    swapcontext( &func, &main_func );
}
 
int main()
{
    char * ss_sp = (char *)malloc( 16 * 1024 );

    // Get the current execution context
    getcontext( &func );
 
    // Modify the context to a new stack
    func.uc_link = 0;
    func.uc_stack.ss_flags = 0;        
    func.uc_stack.ss_size = (16* 1024);
    func.uc_stack.ss_sp = ss_sp;
 
    printf( "Creating thread context... \n" );
    makecontext( &func, &MyFunc, 0 );
         
    printf( "Switching to thread \n" );
    swapcontext( &main_func, &func );

    free( ss_sp );
 
    printf( "main_func done\n" );
         
    return 0;
}


Version-Release number of selected component (if applicable):


How reproducible:
Every time.

Steps to Reproduce:
1. g++ foo.c -o foo -lpthread
2. export LD_LIBRARY_PATH=/lib
3. ./foo
    
Actual results:
Creating thread context... 
Switching to thread 
Segmentation fault

Expected results:
Creating thread context... 
Switching to thread 
MyFunc running
Now func switching back to main_func
main_func done

Additional info:

Comment 1 Ulrich Drepper 2003-06-10 03:28:29 UTC
That's not a bug.  The thread library in /lib cannot possibly handle setcontext
etc since it does not use a thread register and has to rely on the stack pointer
to lead to the thread decriptor.  Use the thread library in /lib/i686 or on RHL9
in /lib/tls.