Skip to content

Commit

Permalink
main: add re_thread_check() for NON-RE thread calls (#389)
Browse files Browse the repository at this point in the history
  • Loading branch information
sreimers authored Jun 8, 2022
1 parent dd83ddc commit 75b0e49
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 5 deletions.
1 change: 1 addition & 0 deletions include/re_main.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ int re_thread_init(void);
void re_thread_close(void);
void re_thread_enter(void);
void re_thread_leave(void);
int re_thread_check(void);

void re_set_mutex(void *mutexp);

Expand Down
46 changes: 44 additions & 2 deletions src/main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
#include <re_tmr.h>
#include <re_main.h>
#include <re_thread.h>
#include <re_btrace.h>
#include <re_atomic.h>
#include "main.h"


Expand Down Expand Up @@ -99,6 +101,8 @@ struct re {
#endif
mtx_t mutex; /**< Mutex for thread synchronization */
mtx_t *mutexp; /**< Pointer to active mutex */
thrd_t tid; /**< Thread id */
RE_ATOMIC bool thread_enter; /**< Thread enter is called */
};

static struct re *re_global = NULL;
Expand Down Expand Up @@ -134,6 +138,7 @@ static int re_init(void)
re->mutexp = &re->mutex;

list_init(&re->tmrl);
re->tid = thrd_current();

#ifdef HAVE_EPOLL
re->epfd = -1;
Expand Down Expand Up @@ -590,6 +595,12 @@ int fd_listen(re_sock_t fd, int flags, fd_h *fh, void *arg)

DEBUG_INFO("fd_listen: fd=%d flags=0x%02x\n", fd, flags);

#ifndef RELEASE
err = re_thread_check();
if (err)
return err;
#endif

if (fd == BAD_SOCK) {
DEBUG_WARNING("fd_listen: corrupt fd %d\n", fd);
return EBADF;
Expand Down Expand Up @@ -1212,7 +1223,10 @@ void re_thread_close(void)
*/
void re_thread_enter(void)
{
re_lock(re_get());
struct re *re = re_get();

re->thread_enter = true;
re_lock(re);
}


Expand All @@ -1223,7 +1237,10 @@ void re_thread_enter(void)
*/
void re_thread_leave(void)
{
re_unlock(re_get());
struct re *re = re_get();

re->thread_enter = false;
re_unlock(re);
}


Expand All @@ -1240,6 +1257,31 @@ void re_set_mutex(void *mutexp)
}


/**
* Check for NON-RE thread calls
*
* @return 0 if success, otherwise EPERM
*/
int re_thread_check(void)
{
struct re *re = re_get();
struct btrace trace;

if (re->thread_enter)
return 0;

if (thrd_equal(re->tid, thrd_current()))
return 0;

btrace(&trace);

DEBUG_WARNING("thread check: called from a NON-RE thread without "
"thread_enter()!\n %H", btrace_println, &trace);

return EPERM;
}


/**
* Get the timer-list for this thread
*
Expand Down
11 changes: 8 additions & 3 deletions src/tmr/tmr.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#ifdef WIN32
#include <windows.h>
#else
#ifndef WIN32
#include <time.h>
#endif
#include <re_types.h>
#include <re_list.h>
#include <re_fmt.h>
#include <re_mem.h>
#include <re_tmr.h>
#include <re_net.h>
#include <re_main.h>


#define DEBUG_MODULE "tmr"
Expand Down Expand Up @@ -257,6 +257,11 @@ void tmr_start_dbg(struct tmr *tmr, uint64_t delay, tmr_h *th, void *arg,
if (!tmr)
return;

#ifndef RELEASE
if (re_thread_check())
return;
#endif

if (tmr->th) {
list_unlink(&tmr->le);
}
Expand Down

0 comments on commit 75b0e49

Please sign in to comment.