[8529] in Athena Bugs
xlc bug with #pragma alloca
daemon@ATHENA.MIT.EDU (John Carr)
Mon Nov 4 23:52:58 1991
To: probe@Athena.MIT.EDU
Cc: bugs@Athena.MIT.EDU
Date: Mon, 04 Nov 91 23:52:56 EST
From: John Carr <jfc@Athena.MIT.EDU>
#pragma alloca causes incorrect code generation under some conditions.
When a function called by a function which calls alloca takes more than 8
arguments, the calling function does not reserve stack space for the extra
arguments. The space allocated with alloca() overlaps the space allocated
for the additional arguments past the 8th.
Here is a sample program which shows how this can fail. This program should
print:
7,8,9,10
but it prints
7,-1,-2,-3
because the space pointed to by p overlaps the space reserved for the last 3
arguments to f().
----------------------------------------------------------------
#pragma alloca
#include <stdio.h>
static void f();
main(int argc, char *argv[])
{
int *p = alloca(32);
p[0] = 0;
p[1] = 0;
p[2] = 0;
p[3] = 0;
p[4] = 0;
p[5] = 0;
p[6] = 0;
p[7] = 0;
f(p,1,2,3,4,5,6,7,8,9,10);
}
static void f(int *p, int a1, int a2, int a3, int a4, int a5, int a6, int a7,
int a8, int a9, int a10)
{
p[0] = -1;
p[1] = -2;
p[2] = -3;
p[3] = -4;
printf("%d,%d,%d,%d\n", a7, a8, a9, a10);
}
----------------------------------------------------------------
The stack should look like:
(high address)
| main save area |
|-------------------------------|
| main variables |
|-------------------------------|
| space allocated by alloca() |
|-------------------------------|
| 11 words (size is maximum |
| number of arguments passed to |
| any function) |
|-------------------------------|
| 6 words |
|-------------------------------|
| f save area |
|-------------------------------|
| f variables |
(low address)
Instead of 11 words, main() only allocates 8 words for the outgoing
argument list. This means main() writes over the space allocated by
alloca when it generates the argument list, and f() writes over its
argument list when it uses the pointer p.