4

Given a library of C functions, is there a way to automate validation if exported functions are reentrant?

Either at runtime (after instrumentation if needed) or from code-analysis. Source code is available.

Note: This is not a std-C lib, nor a well-documented GNU lib with thread-safety contract.

Slawomir
  • 3,194
  • 1
  • 30
  • 36
  • 2
    I am personally not aware of any tool for this, although that doesn't mean such doesn't exist. Since you have the source code, you can look for any local variables that have been declared `static`, check for any accesses to global variables, etc. – John Bode Feb 18 '14 at 23:00
  • Given that you have tagged this question with multithreading, I expect that you have mutex or critical code segements where global status IS modified -- testing for that is near impossible, and only careful coding and codereview will reveal problems. I suggest you re-write the question with how you intent to solve the problem, and clearly state wther the code expect to modify global state under mutex lock, or if it is expected to be pure-reentrant with no mutex locks -- the latter could probably be tested by mapping all external symbols to read-only memory -- but bottomline be more specific. – Soren Feb 23 '14 at 23:38

1 Answers1

4

A function is not considered "thread safe" if it CAN NOT be interrupted in the middle of its execution and then safely called again ("re-entered") before its previous invocations complete execution.

In the C standard library, some functions fall into this category. You do not need a tool such as Valgrind to check for thread safety, instead you should read the documentation (or man page) for the particular function you are concerned about.

Usually, but not always, C offers a thread safe counterpart if a function is not thread safe.

For example, the string tokenizer function strtok has a re-entrant version strtok_r

char *strtok(char *str, const char *delim);
char *strtok_r(char *str, const char *delim, char **saveptr);

with the difference being, your code (thread) maintains a pointer to the last tokenized string (the work in progress) instead of the function maintaining it. This allows multiple threads to call strtok_r in parallel.

In addition, here is another link on SO discussing Threadsafe vs re-entrant behavior.

--

EDIT: More directly related to the original question. I do not believe such a tool exists that can tell you if a function is re-entrant. Tools such as ltrace may help with this. My comments above were illustrating that documentation for the library should exist and I used the C standard library as an example.

Regarding Valgrind, there is a tool for it called Helgrind that can test for synchronization errors (see: http://valgrind.org/docs/manual/hg-manual.html, section 7.1)

Community
  • 1
  • 1
ffhaddad
  • 1,653
  • 13
  • 16
  • 1
    1. I didn't ask for definition of reentrancy. 2. I'm not concerned about std C lib. This is a 3rd party library and poorly documented. – Slawomir Feb 18 '14 at 23:06