This is the mail archive of the ecos-discuss@sources.redhat.com mailing list for the eCos project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: portability problem in pthread_create()


>>>>> "Lars" == Lars Viklund <lars.viklund@axis.com> writes:

    Lars> pthread_create() puts a pthread_info struct on the stack. If
    Lars> (sizeof(pthread_info) % sizeof(CYG_WORD)) != 0 this will
    Lars> make the stack unaligned which in turn will cause the assert
    Lars> in Cyg_HardwareThread::attach_stack() to fail.

    Lars> Suggested fix:

    Lars> Index: packages/compat/posix/current/src/pthread.cxx
    Lars> ===================================================================
    Lars> RCS file: /n/cvsroot/os/ecos/packages/compat/posix/current/src/pthread.cxx,v
    Lars> retrieving revision 1.1.1.1
    Lars> retrieving revision 1.2
    Lars> diff -u -r1.1.1.1 -r1.2
    Lars> --- packages/compat/posix/current/src/pthread.cxx	12 Dec 2001 11:55:44 -0000	1.1.1.1
    Lars> +++ packages/compat/posix/current/src/pthread.cxx	4 Mar 2002 17:36:59 -0000	1.2
    Lars> @@ -536,8 +536,9 @@
 
    Lars>      nthread = (pthread_info *)stackbase;
 
    Lars> -    stackbase += sizeof(pthread_info);
    Lars> -    stacksize -= sizeof(pthread_info);
    Lars> +    // Round size of pthread_info upward to sizeof(CYG_WORD) to keep stack aligned
    Lars> +    stackbase += (sizeof(pthread_info)+sizeof(CYG_WORD)-1) & ~(sizeof(CYG_WORD)-1);
    Lars> +    stacksize -= (sizeof(pthread_info)+sizeof(CYG_WORD)-1) & ~(sizeof(CYG_WORD)-1);
     
    Lars>      thread_table[thread_next] = nthread;

    Lars> Alternatively, fix the stack alignment assert, e.g. by
    Lars> introducing a separate platform defined macro for stack
    Lars> alignment instead of assuming that CYG_WORD alignment is
    Lars> required.

I am not convinced this is a real problem. The last field in the
pthread_info structure is a pointer and the compiler is not allowed to
rearrange fields within a structure, so the probability that
(0 != (sizeof(pthread_info) % sizeof(CYG_WORD))) is low. This
assumption could fail on strange architectures or when building eCos
with the wrong compiler flags, but I suspect an awful lot of other
code would fail as well

Also the pthread_info structure is placed at the bottom (lowest memory
location) of the stack, so for a typical descending stack it does not
really matter whether or not the stacksize is word-aligned. Instead
what really matters is that stackbase+stacksize remains word-aligned.
I suspect the stackbase assertion in
Cyg_HardwareThread::attach_stack() is there to cover potential porting
problems to hardware with ascending stacks.

Have you seen this assertion trigger under normal circumstances?

Bart

-- 
Before posting, please read the FAQ: http://sources.redhat.com/fom/ecos
and search the list archive: http://sources.redhat.com/ml/ecos-discuss


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]