Ignore:
Timestamp:
Dec 4, 2019, 11:25:58 AM (4 years ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum
Children:
fa35958
Parents:
121be3e
Message:

add guard page to pthread stack in debug mode

File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/concurrency/kernel.cfa

    r121be3e r1a3040c  
    1010// Created On       : Tue Jan 17 12:27:26 2017
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Sun Dec  1 17:52:57 2019
    13 // Update Count     : 45
     12// Last Modified On : Tue Dec  3 21:46:54 2019
     13// Update Count     : 49
    1414//
    1515
     
    2727#include <unistd.h>
    2828#include <limits.h>                                                                             // PTHREAD_STACK_MIN
     29#include <sys/mman.h>                                                                   // mprotect
    2930}
    3031
     
    281282
    282283                thread_desc * readyThread = 0p;
    283                 for( unsigned int spin_count = 0; ! __atomic_load_n(&this->do_terminate, __ATOMIC_SEQ_CST); spin_count++ )
    284                 {
     284                for( unsigned int spin_count = 0; ! __atomic_load_n(&this->do_terminate, __ATOMIC_SEQ_CST); spin_count++ ) {
    285285                        readyThread = nextThread( this->cltr );
    286286
    287                         if(readyThread)
    288                         {
     287                        if(readyThread) {
    289288                                verify( ! kernelTLS.preemption_state.enabled );
    290289
     
    297296
    298297                                spin_count = 0;
    299                         }
    300                         else
    301                         {
     298                        } else {
    302299                                // spin(this, &spin_count);
    303300                                halt(this);
     
    445442
    446443static void Abort( int ret, const char * func ) {
    447         if ( ret ) {
     444        if ( ret ) {                                                                            // pthread routines return errno values
    448445                abort( "%s : internal error, error(%d) %s.", func, ret, strerror( ret ) );
    449446        } // if
     
    455452        Abort( pthread_attr_init( &attr ), "pthread_attr_init" ); // initialize attribute
    456453
     454        size_t stacksize;
     455         // default stack size, normally defined by shell limit
     456        Abort( pthread_attr_getstacksize( &attr, &stacksize ), "pthread_attr_getstacksize" );
     457        assert( stacksize >= PTHREAD_STACK_MIN );
     458
    457459#ifdef __CFA_DEBUG__
    458         size_t guardsize;
    459         Abort( pthread_attr_getguardsize( &attr, &guardsize ), "pthread_attr_getguardsize" );
    460         Abort( pthread_attr_setguardsize( &attr, guardsize ), "pthread_attr_setguardsize" );
     460        void * stack = memalign( __page_size, stacksize + __page_size );
     461        // pthread has no mechanism to create the guard page in user supplied stack.
     462        if ( mprotect( stack, __page_size, PROT_NONE ) == -1 ) {
     463                abort( "mprotect : internal error, mprotect failure, error(%d) %s.", errno, strerror( errno ) );
     464        } // if
     465#else
     466        void * stack = malloc( stacksize );
    461467#endif
    462468
    463         size_t stacksize;
    464         Abort( pthread_attr_getstacksize( &attr, &stacksize ), "pthread_attr_getstacksize" ); // default stack size, normally defined by shell limit
    465         assert( stacksize >= PTHREAD_STACK_MIN );
    466         void * stack = malloc( stacksize );
    467469        Abort( pthread_attr_setstack( &attr, stack, stacksize ), "pthread_attr_setstack" );
    468470
Note: See TracChangeset for help on using the changeset viewer.