Module: xenomai-jki
Branch: for-forge
Commit: 07428231ea3d6c0957bc828a3e17de77d2e709e9
URL:    
http://git.xenomai.org/?p=xenomai-jki.git;a=commit;h=07428231ea3d6c0957bc828a3e17de77d2e709e9

Author: Jan Kiszka <jan.kis...@siemens.com>
Date:   Tue Apr 21 16:08:31 2015 +0200

lib/cobalt: Rework minimum stack size enforcement

The silent adjustment breaks when the user specifies a stack address.
Better enforce the minimum size by wrapping pthread_attr_setstack* and
rejecting too small values.

Signed-off-by: Jan Kiszka <jan.kis...@siemens.com>

---

 include/cobalt/pthread.h    |    6 ++++++
 include/cobalt/sys/cobalt.h |    2 --
 lib/cobalt/attr.c           |   18 +++++++++++++++++-
 lib/cobalt/cobalt.wrappers  |    2 ++
 lib/cobalt/internal.c       |   11 -----------
 lib/cobalt/thread.c         |    3 ---
 lib/cobalt/wrappers.c       |   14 ++++++++++++++
 7 files changed, 39 insertions(+), 17 deletions(-)

diff --git a/include/cobalt/pthread.h b/include/cobalt/pthread.h
index 6685276..b619746 100644
--- a/include/cobalt/pthread.h
+++ b/include/cobalt/pthread.h
@@ -38,6 +38,12 @@ typedef struct pthread_attr_ex {
 extern "C" {
 #endif
 
+COBALT_DECL(int, pthread_attr_setstack(pthread_attr_t *attr,
+                                      void *stackaddr, size_t stacksize));
+
+COBALT_DECL(int, pthread_attr_setstacksize(pthread_attr_t *attr,
+                                          size_t stacksize));
+
 COBALT_DECL(int, pthread_create(pthread_t *ptid_r,
                                const pthread_attr_t *attr,
                                void *(*start) (void *),
diff --git a/include/cobalt/sys/cobalt.h b/include/cobalt/sys/cobalt.h
index 67f419e..f124ca6 100644
--- a/include/cobalt/sys/cobalt.h
+++ b/include/cobalt/sys/cobalt.h
@@ -65,8 +65,6 @@ int cobalt_thread_stat(pid_t pid,
 
 int cobalt_serial_debug(const char *fmt, ...);
 
-size_t cobalt_get_stacksize(size_t size);
-
 void __cobalt_commit_memory(void *p, size_t len);
 
 void cobalt_thread_harden(void);
diff --git a/lib/cobalt/attr.c b/lib/cobalt/attr.c
index b3ec570..651a1c6 100644
--- a/lib/cobalt/attr.c
+++ b/lib/cobalt/attr.c
@@ -23,6 +23,22 @@
 #include <cobalt/uapi/thread.h>
 #include "internal.h"
 
+COBALT_IMPL(int, pthread_attr_setstack, (pthread_attr_t *attr, void *stackaddr,
+                                        size_t stacksize))
+{
+       if (stacksize < COBALT_STACKSIZE_MIN)
+               return -EINVAL;
+       return __STD(pthread_attr_setstack)(attr, stackaddr, stacksize);
+}
+
+COBALT_IMPL(int, pthread_attr_setstacksize, (pthread_attr_t *attr,
+                                            size_t stacksize))
+{
+       if (stacksize < COBALT_STACKSIZE_MIN)
+               return -EINVAL;
+       return __STD(pthread_attr_setstacksize)(attr, stacksize);
+}
+
 int pthread_attr_init_ex(pthread_attr_ex_t *attr_ex)
 {
        struct sched_param param;
@@ -110,7 +126,7 @@ int pthread_attr_getstacksize_ex(const pthread_attr_ex_t 
*attr_ex,
 int pthread_attr_setstacksize_ex(pthread_attr_ex_t *attr_ex,
                                 size_t stacksize)
 {
-       return pthread_attr_setstacksize(&attr_ex->std, stacksize);
+       return __WRAP(pthread_attr_setstacksize)(&attr_ex->std, stacksize);
 }
 
 int pthread_attr_getscope_ex(const pthread_attr_ex_t *attr_ex,
diff --git a/lib/cobalt/cobalt.wrappers b/lib/cobalt/cobalt.wrappers
index ee13bcf..1fc2c7b 100644
--- a/lib/cobalt/cobalt.wrappers
+++ b/lib/cobalt/cobalt.wrappers
@@ -1,3 +1,5 @@
+--wrap pthread_attr_setstack
+--wrap pthread_attr_setstacksize
 --wrap pthread_create
 --wrap pthread_setschedparam
 --wrap pthread_getschedparam
diff --git a/lib/cobalt/internal.c b/lib/cobalt/internal.c
index a75cb3c..d154f26 100644
--- a/lib/cobalt/internal.c
+++ b/lib/cobalt/internal.c
@@ -143,17 +143,6 @@ int cobalt_serial_debug(const char *fmt, ...)
        return ret;
 }
 
-size_t cobalt_get_stacksize(size_t size)
-{
-       if (size == 0)
-               size = COBALT_STACKSIZE_DEFAULT;
-
-       if (size < COBALT_STACKSIZE_MIN)
-               size = COBALT_STACKSIZE_MIN;
-
-       return size;
-}
-
 static inline
 struct cobalt_monitor_state *get_monitor_state(cobalt_monitor_t *mon)
 {
diff --git a/lib/cobalt/thread.c b/lib/cobalt/thread.c
index 8675dea..4d524ab 100644
--- a/lib/cobalt/thread.c
+++ b/lib/cobalt/thread.c
@@ -182,7 +182,6 @@ int pthread_create_ex(pthread_t *ptid_r,
        struct timespec timeout;
        pthread_attr_t attr;
        pthread_t lptid;
-       size_t stksz;
 
        if (attr_ex == NULL)
                attr_ex = &default_attr_ex;
@@ -216,8 +215,6 @@ int pthread_create_ex(pthread_t *ptid_r,
                pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED);
 
        pthread_attr_getdetachstate(&attr, &detachstate);
-       pthread_attr_getstacksize(&attr, &stksz);
-       pthread_attr_setstacksize(&attr, cobalt_get_stacksize(stksz));
        pthread_attr_getpersonality_ex(attr_ex, &iargs.personality);
 
        /*
diff --git a/lib/cobalt/wrappers.c b/lib/cobalt/wrappers.c
index 106b466..db8e99f 100644
--- a/lib/cobalt/wrappers.c
+++ b/lib/cobalt/wrappers.c
@@ -98,6 +98,20 @@ int __real_pthread_join(pthread_t ptid, void **retval)
        return pthread_join(ptid, retval);
 }
 
+/* attr */
+__weak
+int __real_pthread_attr_setstack(pthread_attr_t *attr,
+                                void *stackaddr, size_t stacksize)
+{
+       return pthread_attr_setstack(attr, stackaddr, stacksize);
+}
+
+__weak
+int __real_pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize)
+{
+       return pthread_attr_setstacksize(attr, stacksize);
+}
+
 /* semaphores */
 __weak
 int __real_sem_init(sem_t * sem, int pshared, unsigned value)


_______________________________________________
Xenomai-git mailing list
Xenomai-git@xenomai.org
http://www.xenomai.org/mailman/listinfo/xenomai-git

Reply via email to