1 /* 2 * Copyright (c) 2004, Intel Corporation. All rights reserved. 3 * Created by: crystal.xiong REMOVE-THIS AT intel DOT com 4 * This file is licensed under the GPL license. For the full content 5 * of this license, see the COPYING file at the top level of this 6 * source tree. 7 8 * Test pthread_attr_setstack() 9 * 10 * Steps: 11 * 1. Initialize pthread_attr_t object (attr) 12 * 2. set the stackaddr and stacksize to attr 13 * 3. create a thread with the attr 14 * 4. Call a function in the created thread, 15 * which can be treated as reading and writing the stack 16 */ 17 18 #include <pthread.h> 19 #include <limits.h> 20 #include <stdio.h> 21 #include <string.h> 22 #include <stdlib.h> 23 #include <sys/param.h> 24 #include <errno.h> 25 #include <unistd.h> 26 #include "posixtest.h" 27 28 #define TEST "4-1" 29 #define FUNCTION "pthread_attr_setstack" 30 #define ERROR_PREFIX "unexpected error: " FUNCTION " " TEST ": " 31 32 #define STACKADDROFFSET 0x8000000 33 34 static void *stack_addr; 35 size_t stack_size; 36 37 int teststack() 38 { 39 return 0; 40 } 41 42 void *thread_func() 43 { 44 /* execute a function to test the read/right of the stack */ 45 if (teststack() != 0) { 46 perror(ERROR_PREFIX "stack wrong"); 47 exit(PTS_FAIL); 48 } 49 pthread_exit(0); 50 return NULL; 51 } 52 53 int main(void) 54 { 55 pthread_t new_th; 56 pthread_attr_t attr; 57 size_t ssize; 58 void *saddr; 59 int rc; 60 61 /* Initialize attr */ 62 rc = pthread_attr_init(&attr); 63 if (rc != 0) { 64 perror(ERROR_PREFIX "pthread_attr_init"); 65 exit(PTS_UNRESOLVED); 66 } 67 68 /* Get the default stack_addr and stack_size value */ 69 rc = pthread_attr_getstack(&attr, &stack_addr, &stack_size); 70 if (rc != 0) { 71 perror(ERROR_PREFIX "pthread_attr_getstack"); 72 exit(PTS_UNRESOLVED); 73 } 74 /* printf("stack_addr = %p, stack_size = %u\n", stack_addr, stack_size); */ 75 76 stack_size = PTHREAD_STACK_MIN; 77 78 if (posix_memalign(&stack_addr, sysconf(_SC_PAGE_SIZE), 79 stack_size) != 0) { 80 perror(ERROR_PREFIX "out of memory while " 81 "allocating the stack memory"); 82 exit(PTS_UNRESOLVED); 83 } 84 /* printf("stack_addr = %p, stack_size = %u\n", stack_addr, stack_size); */ 85 86 rc = pthread_attr_setstack(&attr, stack_addr, stack_size); 87 if (rc != 0) { 88 perror(ERROR_PREFIX "pthread_attr_setstack"); 89 exit(PTS_UNRESOLVED); 90 } 91 92 rc = pthread_attr_getstack(&attr, &saddr, &ssize); 93 if (rc != 0) { 94 perror(ERROR_PREFIX "pthread_attr_getstack"); 95 exit(PTS_UNRESOLVED); 96 } 97 /* printf("saddr = %p, ssize = %u\n", saddr, ssize); */ 98 99 if (ssize != stack_size || saddr != stack_addr) { 100 perror(ERROR_PREFIX "got the wrong stacksize or stackaddr"); 101 exit(PTS_FAIL); 102 } 103 104 rc = pthread_create(&new_th, &attr, thread_func, NULL); 105 if (rc != 0) { 106 perror(ERROR_PREFIX "failed to create a thread"); 107 exit(PTS_FAIL); 108 } 109 110 rc = pthread_join(new_th, NULL); 111 if (rc != 0) { 112 perror(ERROR_PREFIX "pthread_join"); 113 exit(PTS_UNRESOLVED); 114 } 115 116 rc = pthread_attr_destroy(&attr); 117 if (rc != 0) { 118 perror(ERROR_PREFIX "pthread_attr_destroy"); 119 exit(PTS_UNRESOLVED); 120 } 121 122 printf("Test PASSED\n"); 123 return PTS_PASS; 124 } 125