PengZheng commented on code in PR #536:
URL: https://github.com/apache/celix/pull/536#discussion_r1184807900


##########
libs/utils/src/celix_err.c:
##########
@@ -0,0 +1,166 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include "celix_err.h"
+
+#include <stdarg.h>
+#include <stdio.h>
+#include <string.h>
+
+#ifndef CELIX_ERR_USE_THREAD_LOCAL
+#include <stdlib.h>
+#include "celix_threads.h"
+#endif
+
+typedef struct celix_err {
+    char buffer[CELIX_ERR_BUFFER_SIZE];
+    size_t pos;
+} celix_err_t;
+
+#ifdef CELIX_ERR_USE_THREAD_LOCAL
+
+__thread celix_err_t celix_err_tssErr = { .buffer = {0}, .pos = 0 };

Review Comment:
   I found a more readable treatment of the topic: 
https://maskray.me/blog/2021-02-14-all-about-thread-local-storage
   
   
   > Dynamic loader behavior
   
   > The dynamic loader collects PT_TLS program headers from the main 
executable and immediately loaded shared objects (via transitive DT_NEEDED), 
and allocates static TLS blocks, one block for each PT_TLS. For each PT_TLS, 
the dynamic loader copies p_filesz bytes from the TLS initialization image to 
the TLS block and sets the trailing p_memsz-p_filesz bytes to zeroes.
   
   > For a new thread created by pthread_create, the static TLS blocks are 
usually allocated as part of the thread stack. Without a guard page between the 
largest address of the stack and the thread control block, this could be 
considered as vulnerable as stack overflow can overwrite the thread control 
block.
   
   The above two paragraphs give clear answer to the current question. Err's 
`__thread celix_err_t celix_err_tssErr` will be allocated as static TLS blocks 
on every thread's stack.
   
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscr...@celix.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to