Skip to content

Commit 460a2ab

Browse files
committed
util,spad: handholding to check for unmatched frames
1 parent b9cd2de commit 460a2ab

File tree

3 files changed

+51
-2
lines changed

3 files changed

+51
-2
lines changed

src/util/log/fd_log.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,11 @@
266266

267267
#define FD_TEST_CUSTOM(c,err) do { if( FD_UNLIKELY( !(c) ) ) FD_LOG_ERR(( "FAIL: %s", (err) )); } while(0)
268268

269+
/* FD_TEST_BRK is like FD_TEST but drops into a debugger on failure. */
270+
271+
#define FD_TEST_BRK(c) do { if( FD_UNLIKELY( !(c) ) ) FD_LOG_WARNING(( "FAIL: %s", #c )); __asm__("int $3"); } while(0)
272+
273+
269274
/* Macros for doing hexedit / tcpdump-like logging of memory regions.
270275
E.g.
271276

src/util/spad/fd_spad.c

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,17 @@ fd_spad_frame_hi_debug( fd_spad_t * spad ) {
6969
return SELECT_DEBUG_IMPL(fd_spad_frame_hi)( spad );
7070
}
7171

72-
void
72+
fd_spad_debug_state_t
7373
fd_spad_push_debug( fd_spad_t * spad ) {
74+
fd_spad_debug_state_t state;
75+
state.frame_free = spad->frame_free;
76+
state.mem_used = spad->mem_used;
77+
state.spad = spad;
78+
7479
if( FD_UNLIKELY( !fd_spad_frame_free( spad ) ) ) FD_LOG_CRIT(( "too many frames" ));
7580
SELECT_DEBUG_IMPL(fd_spad_push)( spad );
81+
82+
return state;
7683
}
7784

7885
void
@@ -314,3 +321,17 @@ fd_spad_vtable = {
314321
.malloc = fd_spad_valloc_malloc,
315322
.free = fd_spad_valloc_free
316323
};
324+
325+
void
326+
fd_spad_private_frame_end_debug( fd_spad_debug_state_t * _spad_state ) {
327+
fd_spad_pop( _spad_state->spad );
328+
329+
if(_spad_state->frame_free != _spad_state->spad->frame_free ) {
330+
FD_LOG_WARNING(("%lu != %lu", _spad_state->frame_free, _spad_state->spad->frame_free));
331+
FD_TEST_BRK( _spad_state->frame_free == _spad_state->spad->frame_free );
332+
}
333+
if( _spad_state->mem_used != _spad_state->spad->mem_used ) {
334+
FD_LOG_WARNING(("%lu != %lu", _spad_state->mem_used, _spad_state->spad->mem_used));
335+
FD_TEST_BRK( _spad_state->mem_used == _spad_state->spad->mem_used );
336+
}
337+
}

src/util/spad/fd_spad.h

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,13 @@ FD_STATIC_ASSERT( FD_SPAD_ALLOC_ALIGN_DEFAULT >= FD_MSAN_ALIGN,
9393

9494
/* spad internals */
9595

96+
struct fd_spad_debug_state {
97+
ulong frame_free; /* number of frames free, in [0,FD_SPAD_FRAME_MAX] */
98+
ulong mem_used; /* number of spad memory bytes used, in [0,mem_max] */
99+
fd_spad_t *spad;
100+
};
101+
typedef struct fd_spad_debug_state fd_spad_debug_state_t;
102+
96103
struct __attribute__((aligned(FD_SPAD_ALIGN))) fd_spad_private {
97104

98105
/* This point is FD_SPAD_ALIGN aligned */
@@ -360,13 +367,29 @@ fd_spad_private_frame_end( fd_spad_t ** _spad ) { /* declared here to avoid a fd
360367
fd_spad_pop( *_spad );
361368
}
362369

370+
void fd_spad_private_frame_end_debug( fd_spad_debug_state_t * _spad_state);
371+
372+
#if defined(FD_SPAD_USE_HANDHOLDING)
373+
374+
#define FD_SPAD_FRAME_BEGIN(spad) do { \
375+
__attribute__((cleanup(fd_spad_private_frame_end_debug))) fd_spad_debug_state_t _spad_state; \
376+
_spad_state = fd_spad_push_debug( spad ); \
377+
do
378+
379+
#define FD_SPAD_FRAME_END while(0); } while(0)
380+
381+
#else
382+
363383
#define FD_SPAD_FRAME_BEGIN(spad) do { \
364384
fd_spad_t * _spad __attribute__((cleanup(fd_spad_private_frame_end))) = (spad); \
365385
fd_spad_push( _spad ); \
366386
do
367387

368388
#define FD_SPAD_FRAME_END while(0); } while(0)
369389

390+
#endif
391+
392+
370393
/* fd_spad_alloc allocates sz bytes with alignment align from spad.
371394
Returns a pointer in the caller's address space to the first byte of
372395
the allocation (will be non-NULL with alignment align). Assumes spad
@@ -492,7 +515,7 @@ void * fd_spad_delete_debug ( void * shspad
492515
ulong fd_spad_alloc_max_debug( fd_spad_t const * spad, ulong align );
493516
void * fd_spad_frame_lo_debug ( fd_spad_t * spad );
494517
void * fd_spad_frame_hi_debug ( fd_spad_t * spad );
495-
void fd_spad_push_debug ( fd_spad_t * spad );
518+
fd_spad_debug_state_t fd_spad_push_debug ( fd_spad_t * spad );
496519
void fd_spad_pop_debug ( fd_spad_t * spad );
497520
void * fd_spad_alloc_check ( fd_spad_t * spad, ulong align, ulong sz );
498521
#define fd_spad_alloc_debug fd_spad_alloc_check

0 commit comments

Comments
 (0)