[openssl-dev] [openssl-team] Discussion: design issue: async and -lpthread

Nico Williams nico at cryptonector.com
Mon Nov 23 18:28:44 UTC 2015


On Mon, Nov 23, 2015 at 01:53:47AM +0000, Viktor Dukhovni wrote:

[NetBSD header commentary extracts:]

> /*
>  * Use macros to rename many pthread functions to the corresponding
>  * libc symbols which are either trivial/no-op stubs or the real

No renaming is necessary if one's link-editor and RTLD support
filters...

  An ELF filter is a forwarding saying that the implementation of a
  symbol in the filter object is to be found elsewhere, e.g., in some
  other object.

  In Solaris/Illumos this is used to maintain backwards compatibility
  when symbols get moved from one library to another.

  E.g., libpthread and libdl moved into libc, but they remain as filters
  so that objects linked with those old libraries will a) still find
  them, b) still find the symbols the expect in them, c) get the correct
  implementations of those symbols from the object now providing them
  (here: libc).

  Filters are awesome.  Lack of universal support for them is very
  frustrating.  On Linux, for example, it's possible to create filters
  with strong link-editor-fu, but the RTLD does not support them.

>  * thing, depending on whether libpthread is linked in to the
>  * program. This permits code, particularly libraries that do not
>  * directly use threads but want to be thread-safe in the presence of
>  * threaded callers, to use pthread mutexes and the like without
>  * unnecessairly including libpthread in their linkage.

Just move these into libc, lock stock and barrel, and if you want to
have fast versions for the single-threaded case, just arrange for slower
versions to get hot-patched-in when pthread_create() is first called.

Or even just use a branch/computed jump (whichever is faster) to avoid
having to hot-patch.

It's important that pthread_mutex_init/lock/trylock/unlock/destroy work
correctly even in the optimized single-threaded case.  The main thread
might init and acquire some locks then create a second thread that will
block acquiring those locks.

>  * Left out of this list are functions that can't sensibly be trivial
>  * or no-op stubs in a single-threaded process (pthread_create,
>  * pthread_kill, pthread_detach), functions that normally block and
>  * wait for another thread to do something (pthread_join), and

Just move them into libc anyways.

>  * functions that don't make sense without the previous functions
>  * (pthread_attr_*). The pthread_cond_wait and pthread_cond_timedwait
>  * functions are useful in implementing certain protection mechanisms,
>  * though a non-buggy app shouldn't end up calling them in
>  * single-threaded mode.
>  *
>  * The rename is done as:
>  * #define pthread_foo	__libc_foo
>  * instead of
>  * #define pthread_foo(x) __libc_foo((x))
>  * in order that taking the address of the function ("func =
>  * &pthread_foo;") continue to work.
>  *
>  * POSIX/SUSv3 requires that its functions exist as functions (even if
>  * macro versions exist) and specifically that "#undef pthread_foo" is
>  * legal and should not break anything. Code that does such will not
>  * successfully get the stub behavior implemented here and will
>  * require libpthread to be linked in.
>  */

All the more reason to not rename these symbols!  All you need is ELF
filter support.

Nico
-- 


More information about the openssl-dev mailing list