Generating a code for detection of stack smashing (#pragma stack_protector/#pragma no_stack_protector) [Professional Edition only] [V1.02 or later]


This generates a code for detection of stack smashing at the entry and end of a function.

[Function]

-

This allocates a 2-byte area just before the local variable area (in the direction towards address 0xFFFFF) at the entry to a function, and the value specified by num is stored. After that, the 2-byte area in which num was stored is checked for smashing at the end of the function. If smashing has occurred, the __stack_chk_fail function is called.

-

The __stack_chk_fail function needs to be provided by the user.
It cannot be specified as a static function.

void __far __stack_chk_fail(void) {
/* Processing to be executed when the stack is smashed */
}

-

A code for detection of stack smashing is not generated for a function for which #pragma no_stack_protector has been specified regardless of the -stack_protector option and -stack_protector_all option.

[Effect]

-

Stack smashing can be detected by software.

[Usage]

-

Declare #pragma stack_protector/#pragma no_stack_protector before the first declaration of a variable.

#pragma stack_protector [(]function-name[(num=number)][,function-name[(num=number)]][,...][)]
#pragma no_stack_protector [(]function-name[,...][)]

[Restrictions]

-

Specify an integer from 0 to 65535 for the number to be set in num. If "= number" is omitted, the compiler automatically specifies the integer value.

-

If this option is used simultaneously with the -stack_protector option or -stack_protector_all option, the specification by #pragma stack_protector/#pragma no_stack_protector becomes valid.

-

A compile error will occur when #pragma stack_protector and #pragma no_stack_protector are specified simultaneously for the same function within a single translation unit.

-

A compile error will occur when __inline, or other #pragma directives are specified.