I'm working on a port of the CyaSSL SSL library by WolfSSL [1] to eCos-3.0 running on a Freescale MPC5668.
The basic setup was quite simple and I was able to successfully run some of the tests.
As soon as the tests started using structs though, I ran into weird problems which turned out to be caused by memory being overwritten as soon as the memory for the structs was set to 0 using the memset-function.

The problem happens for example when I try to test the RSA-function which uses a variable "key" of type RsaKey:
RsaKey key;

The system crashes after the function memset was called with the following parameters:
s = 0x400031CC (the target address of "key")
c = 0
n = 552

This call overwrites 552 bytes of memory starting from address 0x400031CC. This results in various variables needed by eCos in being 0ed and thus corrupting the system context, causing a reboot of my hardware shortly after.
I created commented screenshots of the memory space before the memset-call [2] and after [3].

I've also made a posting about this issue on the CyaSSL forums [4], with no answers so far. Note that addresses posted there are slightly different as these were obtained by some previous debug-run.

Can someone help in understanding the issue here as I'm not an C expert? It seems like at some point different data type sizes are assumed, which leads the compiler to put the struct in some place where not enough unused memory resides!?

On 11/10/13 21:18, Daniel Zebralla wrote:
> I now solved the problem by putting all CyaSSL-related stuff and tests into
> an individual thread:
> Is there some general rule of thumb, that one should not do
memory-intensive
> work in the 'main'-thread?

Did you by any chance put your RsaKey struct on the stack, as opposed to
static or malloced? Threads in eCos often have very small stacks; the
corruption you describe sounds typical of a stack overrun.

You can find the stack size for the default ("main") thread in the .ecc
file you're using.

> Did you by any chance put your RsaKey struct on the stack, as opposed
> to static or malloced?

Yes, the RsaKey struct was put on the stack as I didn't really want to change too much inside the code provided by CyaSSL, but at one point I tried to allocate memory for the RsaKey struct using malloc and this struct then indeed got correct memory. Unfortunately the test failed someplace later then because of other structs being put on the stack as well. So I guess changing *ALL* struct declarations to use malloc'd space would have done the trick as well but would of course be quite some work and would have changed the internal structure of the CyaSSL library significantly.

> You can find the stack size for the default ("main") thread in the .ecc
> file you're using.

Thanks for the hint. I already tested this by setting the stack size to 32k (iirc), but the tests still failed (don't know anymore if it was at the same spot or later after further data was put on the stack).

I will stick to the individual thread-solution and provide a hint on this issue with the port once it's done.