public inbox for newlib@sourceware.org
 help / color / mirror / Atom feed
From: Richard Damon <Richard@Damon-Family.org>
To: newlib@sourceware.org
Subject: Re: When/how is global "errno" variable from reent.c to be used?
Date: Fri, 16 Oct 2020 12:38:32 -0400	[thread overview]
Message-ID: <f8787caf-d3a0-c648-b805-e949b59129da@Damon-Family.org> (raw)
In-Reply-To: <rmcfg6$g7l$1@ciao.gmane.io>

Software the explicitly declares errno as
extern int errno;
is broken, and violates the C standard, and creates explicitly stated
Undefined Behavior.


On 10/16/20 11:50 AM, Grant Edwards via Newlib wrote:
> [Sorry for yet another errno vs. reentrancy thread.]
>
> I've been studying errno, __errno, _impure_ptr et alia, and I think I
> understand how that's supposed to work. If you don't define
> _REENT_ONLY, then errno is defined as *(__errno()), where __errno()
> returns &_REENT->errno, and _REENT is #defined as _impure_ptr, and
> thread context switches are supposed to set _impure_ptr to point to a
> per-thread struct _reent.
>
> If you define _REENT_ONLY, then there is no 'errno' declared in
> errno.h, and you've got to mannually pass a struct _reent pointer to
> the _r versions of various lib[cm] functions.
>
> But, in the application source I've been given, the LWIP stack does
> not include newlib's errno.h.  Instead it declares "extern int errno"
> and then uses errno as usual. That external declaration is satisfied
> at link time by the global "errno" exported by libc/reent/reent.c.
>
> That seems to be non-thread-safe and won't interoperate correctly with
> newlib's library calls or with application code that includes newlib's
> errno.h.
>
> What is that global errno variable for and how is to be used?
>
> Is there a use case for single-threaded applications that I've missed
> where newlib's library calls will set/check the global 'int errno'
> variable instead of _impure_ptr->errno?
>
> Or is the LWIP configuration broken?
>
> Even if there is a single-threaded use case for newlib where everybody
> uses the global 'int errno' variable, the application in question is
> multi-threaded, so it's probably still broken. The RTOS in use does
> not set _impure_ptr on context switches, so I believe that switching
> to using the newlib errno.h which will define errno as *(__errno())
> still won't work as it should.
>
> FWIW, the toolchain I'm using is
>
>  gcc-arm-none-eabi-9-2020-q2-update-x86_64-linux.tar.bz2
>
> from
>
>  https://developer.arm.com/tools-and-software/open-source-software/developer-tools/gnu-toolchain/gnu-rm/downloads
>
> It contains v3.3.0 newlib and newlib-nano built from
>
>  git://sourceware.org/git/newlib-cygwin.git commit 6d79e0a58866548f435527798fbd4a6849d05bc7
>
> The LWIP application I'm looking at uses newlib-nano.
>
> --
> Grant
>
>
>
>
>

-- 
Richard Damon


  reply	other threads:[~2020-10-16 16:38 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-10-16 15:50 Grant Edwards
2020-10-16 16:38 ` Richard Damon [this message]
2020-10-16 18:10   ` Grant Edwards
2020-10-16 19:06     ` Richard Damon
2020-10-16 21:49       ` Grant Edwards

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=f8787caf-d3a0-c648-b805-e949b59129da@Damon-Family.org \
    --to=richard@damon-family.org \
    --cc=newlib@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).