beautypg.com

Stack pointer overflow – Zilog ZUSBOPTS User Manual

Page 214

background image

Stack Pointer Overflow

UM017105-0511

186

Zilog Developer Studio II – ZNEO™
User Manual

The interrupt status, which is returned from

TDI()

, consists of the flags register extended

to 16 bits for efficient stack storage.

This function is an intrinsic function and is inline-expanded by default. If the

–redu-

ceopt

compiler option is selected, then this function is not inline-expanded and is imple-

mented as a regular function instead.

Synopsis

#include

intrinsic unsigned short TDI(void);

Example

#include

void main(void)

{

unsigned short istat;

istat = TDI(); /* Test and Disable Interrupts */

/* Do Something */

RI(istat); /* Restore Interrupts */

}

Stack Pointer Overflow

The run-time library for the ZNEO C-Compiler manipulates the SPOV register to protect
program and allocated data. The default action is:

The SPOV register is initialized to the end of the initialized data segment. Therefore,
if the Stack Pointer is decremented below SPOV, it results in the Stack overflow
exception.

When

malloc()

is called, the SPOV register is increased to the highest address of

allocated data;

malloc()

returns NULL if changing the SPOV results in an immedi-

ate stack overflow.

Calling

free()

returns memory for possible use by

malloc()

but does not return

memory for possible use as stack; that is, the SPOV register is not updated.

However, if you modify the SPOV register directly,

malloc()

leaves SPOV where

you have put it and does not allocate data on the stack side of SPOV.

The run-time library does not supply a handler for the stack overflow exception (or any
other exception). If there is any possibility of your released application overflowing its
stack, you must decide how to recover from the situation and write your own handler.

This manual is related to the following products: