Bug 26216

Summary: alloca() couldn't detect thread stack limit
Product: Base System Reporter: sonnet <sonnet>
Component: i386Assignee: freebsd-bugs (Nobody) <bugs>
Status: Closed FIXED    
Severity: Affects Only Me    
Priority: Normal    
Version: Unspecified   
Hardware: Any   
OS: Any   

Description sonnet 2001-03-30 14:00:01 UTC
On pthread environment, each thread has their own stack limit.
when user requested larger than current thread's stack size to alloca(),
alloca() return invalid pointer.
It must be NULL pointer.

How-To-Repeat: gcc -ansi -pedantic -g -Wall -pthread -o test test.c
./test
./test <stacksize>

--
/* test.c */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <pthread.h>

/* the stderr time ticker thread */
static void *ticker(void *_arg)
{
	int		kb = (int)_arg;
    char	*ptr;

	if (NULL != (ptr = alloca(kb * 1024))) {
		printf("alloca(%dKB) return %p\n", kb, ptr);
		memset(ptr, 0xdf, kb * 1024);
	}
	else {
		printf("alloca(%dKB) return NULL\n", kb);
	}

	return NULL;
}

int main(int argc, char *argv[])
{
	pthread_attr_t	thread_attr;
	pthread_t		ticker_thread;
	size_t			stacksize;
    int				i;

    signal(SIGPIPE, SIG_IGN);

	pthread_attr_init(&thread_attr);
	pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED);
	if (2 == argc) {
		stacksize = (size_t) strtoul(argv[1], (char **)NULL, 10);
		if (!pthread_attr_setstacksize(&thread_attr, stacksize*1024)) {
			printf("set stacksize = %dKB\n", stacksize);
		}
	}
	if (!pthread_attr_getstacksize(&thread_attr, &stacksize)) {
		printf("stack size = %dKB\n", stacksize/1024);
	}

    for (i = 4; i < 65536 ; i += 8) {
		pthread_create(&ticker_thread, &thread_attr, ticker, (void *)i);
		sleep(1);
    }

	return 0;
}
Comment 1 Daniel Eischen freebsd_committer freebsd_triage 2001-07-26 13:44:11 UTC
State Changed
From-To: open->closed

alloca() is machine dependent and its use is discouraged. 
And, from the Solaris man-page: 

Note: if the allocated block is beyond the current 
stack limit, the resulting behavior is undefined.