On Tue, Jan 29, 2008 at 07:54:54AM -0800, David Schwartz wrote:
> 
> > There is no "global" variable named errno, it only exist in the TLS.  You
> > could say that because there is only 1 TLS, that it's global, and it
> > acts that way.  But it's not really the same as a normal global
> > variable.  You can't access the variables in the same manner you access
> > other global variables.
> 
> Is the following code legal:
> 
> void foo(void)
> {
>  static int *my_errno=NULL;
>  if(my_errno==NULL) my_errno=errno;
>  // code that uses 'my_errno' as if it were 'errno'
> }
> 
> The answer is that if you're compiled single-threaded, it's perfectly legal.
> If you're multi-threaded, it's not.

I guess by "legal" you mean that it has defined behaviour.

> This might be a perfectly sensible optimization to condition on '_REENTRANT'
> not being defined. In principle, the compiler might do such a thing
> automatically if the overhead of TLS were higher than the overhead of the
> test.

Both the C99 standard and SUS have this nice warning in it.  In C99:

               errno

       which expands to a modifiable lvalue that has type int,
       the value of which is set to a positive error number by
       several library functions.  It is unspecified whether errno
       is a macro or an identifier declared with external linkage.
       If a macro definition is suppressed in order to access an
       actual object, or a program defines an identifier with the
       name errno, the behavior is undefined.

SUS v3:
     It is unspecified whether errno is a macro or an identifier declared
     with external linkage. If a macro definition is suppressed in order
     to access an actual object, or a program defines an identifier with
     the name errno, the behavior is undefined.

I read your example, after fixing it's problems, as "suppressing
the macro definition", and so would have undefined behaviour.

Compiler people are very good in finding things that are undefined,
and they might do things with it you don't expect.  That doesn't mean
everything is undefined.

In the case the macro is expanded to a function call, there is
nothing that tell the compiler that that function always returns
the same value.  I don't see how the compiler could do such an
optimization.

> Really, it's an extraneous example. The point I'm trying to make is really
> simple -- you cannot expect threat safety if the platform provides a way to
> ask for it and you choose not to do so.

As far as I know there actually isn't a _compiler_ (cc1) option to make
something thread safe.  There are only preprocessor and linker options.

> > In case of errno on a glibc system with NPTL there is no difference in the
> > compiled code.  You always get the same function call.
> 
> Exactly. The precise same code can have a very different semantic meaning
> depending upon whether it is used in a multi-threaded or a single-threaded
> process. That's why you *must* specify appropriate compilation flags. The
> code may need to change so that it can produce *different* code to get the
> *same* semantic meaning.
> 
> > Let's assume for a moment that openssl is not "compiled for multi
> > thread", whatever you mean with that exactly.  Even when the application
> > is using threads, when openssl tries to use errno it will get the one for
> > the current thread.
> 
> Really? Even if it's coded like my 'foo' above? It will magically always get
> the right 'errno'? How?!

Not in case of your example, which you said yourself wasn't legal in
multi-threaded applications.  It's also not using errno, it's using
some other variable.  If you go and add something that has
undefined behaviour, lots of things might not work as you expect.

Like I said before, on a glibc system with NPTL errno is expanded to
a function call that that returns (the location of) errno.  That value
is stored per thread in the TLS.  That functions returns the value for
the current thread.

> Right, and the OP is saying he doesn't have to compile with -D_REENTRANT (or
> whatever else is specified on his platform as required for multi-threaded
> code) and he can still use the library in a multi-threaded process.
> 
> I'm saying he can't do this even if he thinks the code will be the same
> because the "same code" can mean something different. Different code may be
> needed to get the same *effect*.
> 
> Compiling code without the compilation flags your platform documents as
> required to get thread-safety guarantees and then using that compiled code
> in a multi-threaded process is simply off the map. Anything can happen. Here
> be dragons. This is so even in the case of Linux, where all the flags do (at
> least, on most Linux platforms, AFAICT) is add a single define to the
> compilation and a library to the linking.

As far as I know I've never said anything which suggest something else.
About the only thing I've claimed is that using errno on a recent glibc
with NPTL always gets your the errno for the current thread.


Kurt

______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
Development Mailing List                       openssl-dev@openssl.org
Automated List Manager                           [EMAIL PROTECTED]

Reply via email to