Ignore:
Timestamp:
Mar 1, 2026, 5:47:54 PM (4 days 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

File:
1 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; }
Note: See TracChangeset for help on using the changeset viewer.