Since nobody is answering (I know is a too vague question) I'll do by myself, just to make clear what kind of help I would need.
- SERIAL PRINTS The most basic thing I've done is put a serial print of the function I'm in just to realize what is happening. This is particularly useful when I'm debugging threads that would overlap execution. One is done at the beginning of each function and one at the end.
- CANARIES At the entry point of every function I've put a CANARY_BEGIN and at the exit point a CANARY_END They are defined as follows:
#define CANARY_VALUE { 0x73, 0x55, 0x69 }
#define CANARY_BEGIN char canary[3] = CANARY_VALUE; #define CANARY_END canary_check (canary);
The function canary_check is:
void canary_check(char *canary) { char canary_tmp[3] = CANARY_VALUE;
if (strncmp(canary, canary_tmp, sizeof(canary_tmp))) debug_print("Buffer overflow"); }
In this way I can realize if I've made any mistake in my coding and suffering from buffer overflow.
- STACK Since in RZK stack size should be known in advance I've allocated N consecutive memory areas to be used as thread stack space for each of the N threads to be monitored (I had to use the RZKCreateThread function). This space should be enough to contain the variables and return values of the longest function call path a thread could do, otherwise stack boundaries can easily be crossed. Since each function was already calling the CANARY_END function I've adeed another check to detect boundary cross. For each thread I've saved the start address of it own stack space. In the canary_check function I compare the canary_temp address with the thread stack start address. If the former is smaller than the latter it means we have crossed boundaries and are writing onto another thread stack space. The modified function follows:
void canary_check(char *canary) { char canary_tmp[3] = CANARY_VALUE; RZK_THREADHANDLE_t tp = RZKGetCurrentThread(); int i, overflow = 0;
if (strncmp(canary, canary_tmp, sizeof(canary_tmp))) overflow = 1; // crossed buffer boundaries
for (i=0; i 24). I'm letting a browser issuing web request in an infinite loop.
It seems to me that the problem is getting less frequent since I made the thread stack size bigger (from 4096 to 8192 bytes). But I'm not even sure about that.
Many thanks for your time and patience.
Giulio Mazzoleni