patacongo commented on a change in pull request #3626:
URL: https://github.com/apache/incubator-nuttx/pull/3626#discussion_r629000307



##########
File path: libs/libc/pthread/pthread_exit.c
##########
@@ -0,0 +1,72 @@
+/****************************************************************************
+ * libs/libc/pthread/pthread_exit.c
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <debug.h>
+#include <sched.h>
+
+#include <nuttx/pthread.h>
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: pthread_exit
+ *
+ * Description:
+ *   Terminate execution of a thread started with pthread_create.
+ *
+ * Input Parameters:
+ *   exit_valie
+ *
+ * Returned Value:
+ *   None
+ *
+ * Assumptions:
+ *
+ ****************************************************************************/
+
+void pthread_exit(FAR void *exit_value)
+{

Review comment:
       > pthread_exit() must only be called from user space, especially after 
this change. However, I still see several calls to pthread_exit(). In the 
original code before your change there are these:
   > 
   > ```
   > sched/pthread/pthread_create.c:  pthread_exit(exit_status);
   > sched/signal/sig_default.c:      pthread_exit(NULL);
   > sched/task/task_cancelpt.c:                  
pthread_exit(PTHREAD_CANCELED);
   > sched/task/task_cancelpt.c:                  
pthread_exit(PTHREAD_CANCELED);
   > sched/task/task_setcancelstate.c:                  
pthread_exit(PTHREAD_CANCELED);
   > sched/task/task_setcanceltype.c:              
pthread_exit(PTHREAD_CANCELED);
   > ```
   > 
   > I think this is an error. We must not call pthread_exit() from within the 
OS without first dropping the privileges to user. Otherwise, the problem that 
we are trying to solve with this PR is not solved.
   > 
   > Possible solution:
   > 
   >     1. Add a pointer to pthread_exit to nx_pthread_create().  We have to 
do this because the address of pthread_exit() will not be known in the 
PROTECTED and KERNEL builds.  I think you were not seeing the build failure in 
the PROTECTED/KERNEL builds because cancellation points and default signal 
actions were not enabled.
   > 
   >     2. Save the pthread_exit() pointer in the TCB
   > 
   >     3. Replace calls to pthread_exit() in the OS to up_pthread_exit().
   > 
   >     4. up_pthread_exit should issue a trap that drops to user mode and 
calls to the saved pthread_exit() entry point.
   
   I think there is a way to accomplish this without adding a lot of new 
infrastructure.  I think we could re-use up_pthread_start() to call 
pthread_exit() indirectly:
   
   1. Call up_pthread_start() with the pthread entry point equal to NULL.
   2. up_pthread_start() will switch to user mode and call the user-space 
pthread_start() function.
   3. pthread_start() would detect the NULL pthread entry point and just call 
pthread_exit().
   




-- 
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.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to