Skip to content

Commit

Permalink
Stack overflow checks are done when tracing is enabled
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Sustrik <sustrik@250bpm.com>
  • Loading branch information
sustrik committed Jun 21, 2015
1 parent 7866cca commit ab49f0f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
6 changes: 6 additions & 0 deletions debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "cr.h"
#include "libmill.h"
#include "list.h"
#include "stack.h"
#include "utils.h"

#include <assert.h>
Expand Down Expand Up @@ -199,6 +200,11 @@ void mill_trace_(const char *location, const char *format, ...) {
if(mill_fast(mill_tracelevel <= 0))
return;

/* TODO: At the moment, stack overflow checking is done only if tracing
is enabled. Think of a better way to do it. */
if(mill_running != &mill_main)
mill_checkstack(mill_running + 1);

char buf[256];

/* First print the timestamp. */
Expand Down
12 changes: 9 additions & 3 deletions stack.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
/* 8 bytes written to the bottom of the stack to guard for stack overflows. */
#define MILL_STACK_GUARD 0xdeadbeefbadcafe0

static volatile int mill_stack_unoptimisable1 = 1;
static volatile void *mill_stack_unoptimisable2 = NULL;

/* A stack of unused coroutine stacks. This allows for extra-fast allocation
of a new stack. The FIFO nature of this structure minimises cache misses.
When the stack is cached its mill_slist_item is placed on its top rather
Expand Down Expand Up @@ -69,9 +72,12 @@ void mill_freestack(void *stack) {
++mill_num_cached_stacks;
}

void mill_checkstack(void *stack, void *ptr) {
void mill_checkstack(void *stack) {
int anchor[mill_stack_unoptimisable1];
mill_stack_unoptimisable2 = &anchor;
char *bottom = ((char*) stack) - MILL_STACK_SIZE;
assert (ptr <= stack && ptr > (void*)(bottom + 8) &&
*((uint64_t*)bottom) == MILL_STACK_GUARD);
assert(((void*)&anchor) <= stack);
assert(((void*)&anchor) > (void*)(bottom + 8));
assert(*((uint64_t*)bottom) == MILL_STACK_GUARD);
}

5 changes: 2 additions & 3 deletions stack.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ void *mill_allocstack(void);
/* Deallocates a stack. The argument is pointer to the top of the stack. */
void mill_freestack(void *stack);

/* Checks whether the supplied pointer is within the stack and whether the
stack itself is not corrupted. */
void mill_checkstack(void *stack, void *ptr);
/* Checks for the signs of stack overflow. */
void mill_checkstack(void *stack);

#endif

0 comments on commit ab49f0f

Please sign in to comment.