Bug 837 - can't setstack in share library.
Summary: can't setstack in share library.
Status: RESOLVED INVALID
Alias: None
Product: glibc
Classification: Unclassified
Component: nptl (show other bugs)
Version: 2.3.4
: P2 critical
Target Milestone: ---
Assignee: Ulrich Drepper
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2005-04-09 04:18 UTC by ymjzxw
Modified: 2005-04-29 02:24 UTC (History)
1 user (show)

See Also:
Host: i386-pc-linux2.6.9-gnu
Target: i386-pc-linux2.6.9-gnu
Build: i386-pc-linux2.6.9-gnu
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description ymjzxw 2005-04-09 04:18:10 UTC
Following code causes "Segmentation fault" (kernel:linux2.6.9)

/*my_thread.c*/ 
#include <pthread.h> 
#include <sys/shm.h> 

#define PAGE_SIZE         4UL*1024UL*1024UL 
#define ADDR                 (0x00000000UL) 

int my_pthread_create(pthread_t * id, pthread_attr_t * attribute, void 
* (*start_routine)(void *), void * arg) 
{ 
        pthread_attr_t attr; 
        pthread_attr_init(&attr); 
        int ret;         
        int  shmid = 0; 
        void  *addr = NULL; 
        key_t key = 566; 
         
        shmid = shmget(key, PAGE_SIZE, 
SHM_HUGETLB|IPC_CREAT|SHM_R|SHM_W|SHM_RND); 
        if(shmid != -1) 
        { 
                addr = shmat(shmid, (void *)ADDR , 0); 
                if (((long)addr) == -1) 
                        return 1; 
        }         
        ret = pthread_attr_setstack (&attr, addr , PAGE_SIZE); 
        if(ret != 0 )                 
                return 1;                         
        ret=pthread_create(id,&attr,(void *)start_routine, NULL); 
        if(ret != 0 ) 
                return 1;         
        return (0); 
} 

/*test.c*/ 
#include <stdio.h> 
#include <errno.h> 
#include "my_thread.h" 

void thread1(void) 
{ 
        int i=11; 
        printf("first variable of thread1 addr = %pn", &i);         
} 

int main(void) 
{ 
        pthread_t id; 
        int ret;         
        ret = my_pthread_create(&id,NULL,(void *)thread1, NULL);         
        if(ret != 0 ) 
                printf("errno = %dn",errno);         
        pthread_join(id,NULL); 
        return (0); 
} 

/*my_thread.h*/ 
#include <pthread.h> 
#include <sys/shm.h> 
extern int my_pthread_create(pthread_t * id, pthread_attr_t * attr1, 
void * (*start_routine)(void *), void * arg); 

/*Makefile*/ 
TEST: 
        gcc -fpic -c -g my_thread.c my_thread.h 
        gcc -shared -Wl,-soname,libmy_thread.so -o libmy_thread.so 
my_thread.o 
        cp ./libmy_thread.so /lib/ 
        cp ./libmy_thread.so /usr/lib 
        gcc  test.c -o test -lmy_thread -lpthread 
         
clear: 
        rm -f *.o libmy_thread.so test
Comment 1 Ulrich Drepper 2005-04-28 15:21:05 UTC
If the shmget call fails, the program will of course crash.
Comment 2 ymjzxw 2005-04-29 00:31:31 UTC
If you run it in kernel 2.6 ang use ROOT,shmget call success
Comment 3 Ulrich Drepper 2005-04-29 02:24:18 UTC
Show me the strace output of a run where shmget is successful and the program
crashes.