Changeset 0957f62 for libcfa


Ignore:
Timestamp:
Mar 1, 2026, 5:47:54 PM (45 hours ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
master
Children:
02e7483
Parents:
8086004
Message:

add routines stack_verify and stack_pointer, in debug mode call stack_verify on front-side of context switch and time-slicing

Location:
libcfa/src/concurrency
Files:
3 edited

Legend:

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

    r8086004 r0957f62  
    1010// Created On       : Mon Nov 28 12:27:26 2016
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Fri Apr 25 06:48:19 2025
    13 // Update Count     : 31
     12// Last Modified On : Sun Mar  1 17:36:41 2026
     13// Update Count     : 115
    1414//
    1515
     
    326326void ^?{}( ehm_cleanup & this ) { free( this.ex ); }
    327327
     328void * stack_pointer( coroutine$ * cor ) libcfa_public {
     329        if ( active_coroutine() == cor ) {                                      // accessing myself ?
     330                void * sp;                                                                              // use my current stack value
     331                #if defined( __i386__ )
     332                asm( "movl %%esp,%0" : "=m" (sp) : );
     333                #elif defined( __x86_64__ )
     334                asm( "movq %%rsp,%0" : "=m" (sp) : );
     335                #elif defined( __arm_64__ )
     336                asm( "mov x9, sp; str x9,%0" : "=m" (sp) : : "x9" );
     337                #else
     338                        #error Cforall : internal error, unsupported architecture
     339                #endif
     340                return sp;
     341        } else {                                                                                        // accessing another coroutine
     342                return cor->context.SP;
     343        } // if
     344} // stackPointer
     345
     346void * stack_pointer() libcfa_public { return stack_pointer( active_coroutine() ); }
     347
     348#define xstr(s) str(s)
     349#define str(s) #s
     350#define STACK_ERROR 4
     351#define STACK_WARNING 16
     352
     353void stack_verify( coroutine$ * cor ) libcfa_public {
     354        void * sp = stack_pointer( cor );                                       // optimizations
     355        struct __stack_t * cor_stack = __get_stack( cor );
     356        void * safelimit = (void *)((char *)cor_stack->limit + STACK_ERROR * 1024); // space needed for printing abort message and backtrace
     357
     358        if ( sp < safelimit ) {                                                         // must leave stack space to call abort
     359                abort( "Stack overflow detected: stack pointer %p below safe limit %p.\n"
     360                           "Possible cause is allocation of large stack frame(s) and/or deep call stack.",
     361                           sp, safelimit );
     362        } else if ( sp < (void *)((char *)cor_stack->limit + STACK_WARNING * 1024) ) { // must leave stack space to call abort
     363                #define STACK_WARNING_MSG "Cforall Runtime warning : within " xstr(STACK_WARNING) "K of stack limit.\n"
     364                __cfaabi_bits_write( STDERR_FILENO, STACK_WARNING_MSG, sizeof( STACK_WARNING_MSG ) - 1 );
     365        } else if ( sp > cor_stack->base ) {
     366                abort( "Stack underflow detected: stack pointer %p above base %p.\n"
     367                           "Possible cause is corrupted stack frame via overwriting memory.",
     368                           sp, cor_stack->base );
     369        } // if
     370} // verify
     371
     372void stack_verify() libcfa_public { return stack_verify( active_coroutine() ); }
     373
    328374bool poll( coroutine$ * cor ) libcfa_public {
    329375        nonlocal_exception * nl_ex = pop_ehm_head( cor );
     
    351397// user facing ehm operations
    352398forall(T & | is_coroutine(T)) {
     399        void * stack_pointer( T & cor ) libcfa_public { return stack_pointer( get_coroutine( cor ) ); }
     400        void stack_verify( T & cor ) libcfa_public { return stack_verify( get_coroutine( cor ) ); }
     401
    353402        // enable/disable non-local exceptions
    354403        void enable_ehm( T & cor ) libcfa_public { get_coroutine( cor )->ehm_state.ehm_enabled = true; }
  • libcfa/src/concurrency/coroutine.hfa

    r8086004 r0957f62  
    1010// Created On       : Mon Nov 28 12:27:26 2016
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Fri Apr 25 06:52:04 2025
    13 // Update Count     : 15
     12// Last Modified On : Sun Mar  1 17:44:11 2026
     13// Update Count     : 43
    1414//
    1515
     
    9898}
    9999
     100void stack_verify( coroutine$ * cor );
     101void stack_verify();
     102
    100103// Private wrappers for context switch and stack creation
    101 // Wrapper for co
    102104static inline void $ctx_switch( coroutine$ * src, coroutine$ * dst ) __attribute__((nonnull (1, 2))) {
    103105        // set state of current coroutine to inactive
     
    110112        /* paranoid */ verify( !athrd->corctx_flag );
    111113        athrd->corctx_flag = true;
     114
     115        #if defined( __CFA_DEBUG__ )
     116        stack_verify( src );                                                            // test on front side of context switch, backside is too late.
     117        #endif // __CFA_DEBUG__
    112118
    113119        // set new coroutine that task is executing
     
    225231
    226232// non local ehm and coroutine utility routines
     233void * stack_pointer( coroutine$ * cor );
     234void * stack_pointer();
    227235void enable_ehm();
    228236void disable_ehm();
     
    234242
    235243forall(T & | is_coroutine(T)) {
     244        void * stack_pointer( T & cor );
     245        void stack_verify( T & cor );
    236246    void enable_ehm( T & cor );         // enable checking non-local exceptions for cor via checked_poll
    237247    void disable_ehm( T & cor );        // disable checking non-local exceptions for cor via checked_poll
  • libcfa/src/concurrency/preemption.cfa

    r8086004 r0957f62  
    1010// Created On       : Mon Jun 5 14:20:42 2017
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Fri Apr 25 07:24:39 2025
    13 // Update Count     : 63
     12// Last Modified On : Sun Mar  1 10:00:18 2026
     13// Update Count     : 68
    1414//
    1515
     
    571571        __cfaabi_dbg_print_buffer_decl( " KERNEL: preempting core %p (%p @ %p).\n", __cfaabi_tls.this_processor, __cfaabi_tls.this_thread, (void *)(cxt->uc_mcontext.CFA_REG_IP) );
    572572
     573        #if defined( __CFA_DEBUG__ )
     574        stack_verify();                                                                         // good place to check for stack overflow
     575        #endif // __CFA_DEBUG__
     576
    573577        // Sync flag : prevent recursive calls to the signal handler
    574578        __cfaabi_tls.preemption_state.in_progress = true;
     
    591595        #endif
    592596
    593         force_yield( __ALARM_PREEMPTION ); // Do the actual __cfactx_switch
     597        force_yield( __ALARM_PREEMPTION );                                      // Do the actual __cfactx_switch
    594598}
    595599
Note: See TracChangeset for help on using the changeset viewer.