From nobody@FreeBSD.org  Fri Mar 30 04:52:27 2001
Return-Path: <nobody@FreeBSD.org>
Received: from freefall.freebsd.org (freefall.freebsd.org [216.136.204.21])
	by hub.freebsd.org (Postfix) with ESMTP id 098E337B71D
	for <freebsd-gnats-submit@FreeBSD.org>; Fri, 30 Mar 2001 04:52:27 -0800 (PST)
	(envelope-from nobody@FreeBSD.org)
Received: (from nobody@localhost)
	by freefall.freebsd.org (8.11.1/8.11.1) id f2UCqR831862;
	Fri, 30 Mar 2001 04:52:27 -0800 (PST)
	(envelope-from nobody)
Message-Id: <200103301252.f2UCqR831862@freefall.freebsd.org>
Date: Fri, 30 Mar 2001 04:52:27 -0800 (PST)
From: sonnet@teatime.org
To: freebsd-gnats-submit@FreeBSD.org
Subject: alloca() couldn't detect thread stack limit
X-Send-Pr-Version: www-1.0

>Number:         26216
>Category:       i386
>Synopsis:       alloca() couldn't detect thread stack limit
>Confidential:   no
>Severity:       serious
>Priority:       high
>Responsible:    freebsd-bugs
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Fri Mar 30 05:00:01 PST 2001
>Closed-Date:    Thu Jul 26 05:44:11 PDT 2001
>Last-Modified:  Thu Jul 26 05:47:07 PDT 2001
>Originator:     Soonmyung Hong
>Release:        3-Stable/4-Stable
>Organization:
Innovay Inc
>Environment:
FreeBSD alfheim.innovay.com 3.5-STABLE FreeBSD 3.5-STABLE #3: Wed Jul 26 19:37:39 KST 2000     root@alfheim.innovay.com:/usr/src/sys/compile/ALFHEIM  i386
FreeBSD hel.innovay.com 4.2-STABLE FreeBSD 4.2-STABLE #2: Wed Feb 28 17:22:54 KST 2001     root@hel.innovay.com:/usr/src/sys/compile/HEL  i386
>Description:
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;
}

>Fix:

>Release-Note:
>Audit-Trail:
State-Changed-From-To: open->closed 
State-Changed-By: deischen 
State-Changed-When: Thu Jul 26 05:44:11 PDT 2001 
State-Changed-Why:  
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. 


http://www.FreeBSD.org/cgi/query-pr.cgi?pr=26216 
>Unformatted:
