[PATCH v4 1/5] CONFIGURE_MAXIMUM_THREAD_LOCAL_STORAGE_SIZE

Sebastian Huber sebastian.huber at embedded-brains.de
Fri Sep 11 15:34:32 UTC 2020


Add this application configuration option.  This configuration option can be
used to reserve space for the dynamic linking of modules with thread-local
storage objects.

Update #4074.
---
 cpukit/doxygen/appl-config.h                | 29 +++++++++++
 cpukit/include/rtems/confdefs/threads.h     |  7 +++
 cpukit/include/rtems/score/interr.h         |  3 +-
 cpukit/include/rtems/score/thread.h         | 10 ++++
 cpukit/sapi/src/interrtext.c                |  5 +-
 cpukit/score/src/tlsallocsize.c             | 16 ++++--
 testsuites/sptests/Makefile.am              |  9 ++++
 testsuites/sptests/configure.ac             |  1 +
 testsuites/sptests/spfatal33/init.c         | 55 +++++++++++++++++++++
 testsuites/sptests/spfatal33/spfatal33.doc  | 11 +++++
 testsuites/sptests/spfatal33/spfatal33.scn  | 16 ++++++
 testsuites/sptests/spinternalerror02/init.c |  4 +-
 12 files changed, 158 insertions(+), 8 deletions(-)
 create mode 100644 testsuites/sptests/spfatal33/init.c
 create mode 100644 testsuites/sptests/spfatal33/spfatal33.doc
 create mode 100644 testsuites/sptests/spfatal33/spfatal33.scn

diff --git a/cpukit/doxygen/appl-config.h b/cpukit/doxygen/appl-config.h
index cd4664f7c8..25dbaead7c 100644
--- a/cpukit/doxygen/appl-config.h
+++ b/cpukit/doxygen/appl-config.h
@@ -801,6 +801,35 @@
  */
 #define CONFIGURE_MAXIMUM_TASKS
 
+/**
+ * @brief This configuration option is an integer define.
+ *
+ * If the value of this configuration option is greater than zero, then it
+ * defines the maximum thread-local storage size, otherwise the thread-local
+ * storage size is defined by the linker depending on the thread-local storage
+ * objects used by the application in the statically-linked executable.
+ *
+ * @par Default Value
+ * The default value is 0.
+ *
+ * @par Value Constraints
+ * The value of this configuration option shall be greater than or equal to 0
+ * and less than or equal to <a
+ * href="https://en.cppreference.com/w/c/types/limits">SIZE_MAX</a>.
+ *
+ * @par Notes
+ * @parblock
+ * This configuration option can be used to reserve space for the dynamic
+ * linking of modules with thread-local storage objects.
+ *
+ * If the thread-local storage size defined by the thread-local storage objects
+ * used by the application in the statically-linked executable is greater than
+ * a non-zero value of this configuration option, then a fatal error will occur
+ * during system initialization.
+ * @endparblock
+ */
+#define CONFIGURE_MAXIMUM_THREAD_LOCAL_STORAGE_SIZE
+
 /**
  * @brief This configuration option is an integer define.
  *
diff --git a/cpukit/include/rtems/confdefs/threads.h b/cpukit/include/rtems/confdefs/threads.h
index 33b8c5cee2..9e34696e61 100644
--- a/cpukit/include/rtems/confdefs/threads.h
+++ b/cpukit/include/rtems/confdefs/threads.h
@@ -84,6 +84,10 @@
   #define CONFIGURE_MAXIMUM_THREAD_NAME_SIZE THREAD_DEFAULT_MAXIMUM_NAME_SIZE
 #endif
 
+#ifndef CONFIGURE_MAXIMUM_THREAD_LOCAL_STORAGE_SIZE
+  #define CONFIGURE_MAXIMUM_THREAD_LOCAL_STORAGE_SIZE 0
+#endif
+
 #ifdef __cplusplus
 extern "C" {
 #endif
@@ -125,6 +129,9 @@ typedef union {
 
 const size_t _Thread_Maximum_name_size = CONFIGURE_MAXIMUM_THREAD_NAME_SIZE;
 
+const size_t _Thread_Maximum_TLS_size =
+  CONFIGURE_MAXIMUM_THREAD_LOCAL_STORAGE_SIZE;
+
 struct Thread_Configured_control {
   Thread_Control Control;
   #if CONFIGURE_MAXIMUM_USER_EXTENSIONS > 0
diff --git a/cpukit/include/rtems/score/interr.h b/cpukit/include/rtems/score/interr.h
index 2f449deedb..1371e55653 100644
--- a/cpukit/include/rtems/score/interr.h
+++ b/cpukit/include/rtems/score/interr.h
@@ -203,7 +203,8 @@ typedef enum {
   INTERNAL_ERROR_LIBIO_STDERR_FD_OPEN_FAILED = 37,
   INTERNAL_ERROR_ILLEGAL_USE_OF_FLOATING_POINT_UNIT = 38,
   INTERNAL_ERROR_ARC4RANDOM_GETENTROPY_FAIL = 39,
-  INTERNAL_ERROR_NO_MEMORY_FOR_PER_CPU_DATA = 40
+  INTERNAL_ERROR_NO_MEMORY_FOR_PER_CPU_DATA = 40,
+  INTERNAL_ERROR_TOO_LARGE_TLS_SIZE = 41
 } Internal_errors_Core_list;
 
 typedef CPU_Uint32ptr Internal_errors_t;
diff --git a/cpukit/include/rtems/score/thread.h b/cpukit/include/rtems/score/thread.h
index 1daf6143f9..1388744548 100644
--- a/cpukit/include/rtems/score/thread.h
+++ b/cpukit/include/rtems/score/thread.h
@@ -953,6 +953,16 @@ extern const size_t _Thread_Initial_thread_count;
  */
 extern const size_t _Thread_Maximum_name_size;
 
+/**
+ * @brief If this constant is greater than zero, then it defines the maximum
+ * thread-local storage size, otherwise the thread-local storage size is defined
+ * by the linker depending on the thread-local storage objects used by the
+ * application in the statically-linked executable.
+ *
+ * This value is provided via <rtems/confdefs.h>.
+ */
+extern const size_t _Thread_Maximum_TLS_size;
+
 /**
  * @brief The configured thread control block.
  *
diff --git a/cpukit/sapi/src/interrtext.c b/cpukit/sapi/src/interrtext.c
index 2aff34487a..3d49135c44 100644
--- a/cpukit/sapi/src/interrtext.c
+++ b/cpukit/sapi/src/interrtext.c
@@ -7,7 +7,7 @@
  */
 
 /*
- * Copyright (c) 2012, 2018 embedded brains GmbH.  All rights reserved.
+ * Copyright (c) 2012, 2020 embedded brains GmbH.  All rights reserved.
  *
  *  embedded brains GmbH
  *  Dornierstr. 4
@@ -67,7 +67,8 @@ static const char *const internal_error_text[] = {
   "INTERNAL_ERROR_LIBIO_STDERR_FD_OPEN_FAILED",
   "INTERNAL_ERROR_ILLEGAL_USE_OF_FLOATING_POINT_UNIT",
   "INTERNAL_ERROR_ARC4RANDOM_GETENTROPY_FAIL",
-  "INTERNAL_ERROR_NO_MEMORY_FOR_PER_CPU_DATA"
+  "INTERNAL_ERROR_NO_MEMORY_FOR_PER_CPU_DATA",
+  "INTERNAL_ERROR_TOO_LARGE_TLS_SIZE"
 };
 
 const char *rtems_internal_error_text( rtems_fatal_code error )
diff --git a/cpukit/score/src/tlsallocsize.c b/cpukit/score/src/tlsallocsize.c
index 8fa5600602..a9f0c0cd23 100644
--- a/cpukit/score/src/tlsallocsize.c
+++ b/cpukit/score/src/tlsallocsize.c
@@ -1,7 +1,7 @@
+/* SPDX-License-Identifier: BSD-2-Clause */
+
 /*
- * SPDX-License-Identifier: BSD-2-Clause
- *
- * Copyright (C) 2014, 2019 embedded brains GmbH
+ * Copyright (C) 2014, 2020 embedded brains GmbH (http://www.embedded-brains.de)
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -30,6 +30,8 @@
 #endif
 
 #include <rtems/score/tls.h>
+#include <rtems/score/interr.h>
+#include <rtems/score/thread.h>
 
 static uintptr_t _TLS_Allocation_size;
 
@@ -65,6 +67,14 @@ uintptr_t _TLS_Get_allocation_size( void )
     allocation_size += sizeof(TLS_Dynamic_thread_vector);
 #endif
 
+    if ( _Thread_Maximum_TLS_size != 0 ) {
+      if ( allocation_size <= _Thread_Maximum_TLS_size ) {
+        allocation_size = _Thread_Maximum_TLS_size;
+      } else {
+        _Internal_error( INTERNAL_ERROR_TOO_LARGE_TLS_SIZE );
+      }
+    }
+
     _TLS_Allocation_size = allocation_size;
   }
 
diff --git a/testsuites/sptests/Makefile.am b/testsuites/sptests/Makefile.am
index 854ec3daf8..a837144914 100644
--- a/testsuites/sptests/Makefile.am
+++ b/testsuites/sptests/Makefile.am
@@ -1075,6 +1075,15 @@ spfatal32_CPPFLAGS = $(AM_CPPFLAGS) $(TEST_FLAGS_spfatal32) \
 	$(support_includes)
 endif
 
+if TEST_spfatal33
+sp_tests += spfatal33
+sp_screens += spfatal33/spfatal33.scn
+sp_docs += spfatal33/spfatal33.doc
+spfatal33_SOURCES = spfatal33/init.c
+spfatal33_CPPFLAGS = $(AM_CPPFLAGS) $(TEST_FLAGS_spfatal33) \
+	$(support_includes)
+endif
+
 if TEST_spfifo01
 sp_tests += spfifo01
 sp_screens += spfifo01/spfifo01.scn
diff --git a/testsuites/sptests/configure.ac b/testsuites/sptests/configure.ac
index 460917dd04..454459f477 100644
--- a/testsuites/sptests/configure.ac
+++ b/testsuites/sptests/configure.ac
@@ -157,6 +157,7 @@ RTEMS_TEST_CHECK([spfatal29])
 RTEMS_TEST_CHECK([spfatal30])
 RTEMS_TEST_CHECK([spfatal31])
 RTEMS_TEST_CHECK([spfatal32])
+RTEMS_TEST_CHECK([spfatal33])
 RTEMS_TEST_CHECK([spfifo01])
 RTEMS_TEST_CHECK([spfifo02])
 RTEMS_TEST_CHECK([spfifo03])
diff --git a/testsuites/sptests/spfatal33/init.c b/testsuites/sptests/spfatal33/init.c
new file mode 100644
index 0000000000..3cdce7d931
--- /dev/null
+++ b/testsuites/sptests/spfatal33/init.c
@@ -0,0 +1,55 @@
+/* SPDX-License-Identifier: BSD-2-Clause */
+
+/*
+ * Copyright (C) 2020 embedded brains GmbH (http://www.embedded-brains.de)
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include "../spfatal_support/spfatal.h"
+
+#define FATAL_ERROR_TEST_NAME       "33"
+#define FATAL_ERROR_DESCRIPTION     "provoke too large TLS size"
+#define FATAL_ERROR_EXPECTED_SOURCE INTERNAL_ERROR_CORE
+#define FATAL_ERROR_EXPECTED_ERROR  INTERNAL_ERROR_TOO_LARGE_TLS_SIZE
+
+static _Thread_local short tls;
+
+static void force_error(void)
+{
+  long var;
+
+  var = tls;
+  RTEMS_OBFUSCATE_VARIABLE( var );
+  tls = var;
+
+  /* Not reached */
+  rtems_test_assert( 0 );
+}
+
+#define CONFIGURE_MAXIMUM_THREAD_LOCAL_STORAGE_SIZE 1
+
+#include "../spfatal_support/spfatalimpl.h"
diff --git a/testsuites/sptests/spfatal33/spfatal33.doc b/testsuites/sptests/spfatal33/spfatal33.doc
new file mode 100644
index 0000000000..190e1ad4eb
--- /dev/null
+++ b/testsuites/sptests/spfatal33/spfatal33.doc
@@ -0,0 +1,11 @@
+This file describes the directives and concepts tested by this test set.
+
+test set name: spfatal33
+
+directives:
+
+  - _TLS_Get_allocation_size()
+
+concepts:
+
+  - Provoke a too large TLS size.
diff --git a/testsuites/sptests/spfatal33/spfatal33.scn b/testsuites/sptests/spfatal33/spfatal33.scn
new file mode 100644
index 0000000000..80e3dd40a4
--- /dev/null
+++ b/testsuites/sptests/spfatal33/spfatal33.scn
@@ -0,0 +1,16 @@
+*** BEGIN OF TEST SPFATAL 33 ***
+*** TEST VERSION: 6.0.0.40a6a37e27a5e112bb3e383c196c6094f3498298
+*** TEST STATE: EXPECTED_PASS
+*** TEST BUILD: RTEMS_DEBUG
+*** TEST TOOLS: 10.2.1 20200904 (RTEMS 6, RSB 47f32b8b1a597b5ed3475722bdc155249ef51115, Newlib a0d7982)
+Fatal error (provoke too large TLS size) hit
+
+*** END OF TEST SPFATAL 33 ***
+
+
+*** FATAL ***
+fatal source: 0 (INTERNAL_ERROR_CORE)
+fatal code: 41 (INTERNAL_ERROR_TOO_LARGE_TLS_SIZE)
+RTEMS version: 6.0.0.40a6a37e27a5e112bb3e383c196c6094f3498298
+RTEMS tools: 10.2.1 20200904 (RTEMS 6, RSB 47f32b8b1a597b5ed3475722bdc155249ef51115, Newlib a0d7982)
+executing thread is NULL
diff --git a/testsuites/sptests/spinternalerror02/init.c b/testsuites/sptests/spinternalerror02/init.c
index bf589473de..1b7d0b4388 100644
--- a/testsuites/sptests/spinternalerror02/init.c
+++ b/testsuites/sptests/spinternalerror02/init.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2012, 2018 embedded brains GmbH.  All rights reserved.
+ * Copyright (c) 2012, 2020 embedded brains GmbH.  All rights reserved.
  *
  *  embedded brains GmbH
  *  Donierstr. 4
@@ -36,7 +36,7 @@ static void test_internal_error_text(void)
   } while ( text != text_last );
 
   rtems_test_assert(
-    error - 3 == INTERNAL_ERROR_NO_MEMORY_FOR_PER_CPU_DATA
+    error - 3 == INTERNAL_ERROR_TOO_LARGE_TLS_SIZE
   );
 }
 
-- 
2.26.2



More information about the devel mailing list