From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mout.perfora.net (mout.perfora.net [74.208.4.196]) by sourceware.org (Postfix) with ESMTPS id 8560A3857C61 for ; Fri, 16 Oct 2020 16:38:34 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 8560A3857C61 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=Damon-Family.org Authentication-Results: sourceware.org; spf=none smtp.mailfrom=Richard@Damon-Family.org Received: from [192.168.77.189] ([108.20.19.251]) by mrelay.perfora.net (mreueus002 [74.208.5.2]) with ESMTPA (Nemesis) id 0Lsjfz-1kI0bw1Oy9-012GQr for ; Fri, 16 Oct 2020 18:38:33 +0200 Subject: Re: When/how is global "errno" variable from reent.c to be used? To: newlib@sourceware.org References: From: Richard Damon Message-ID: Date: Fri, 16 Oct 2020 12:38:32 -0400 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:78.0) Gecko/20100101 Thunderbird/78.3.2 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit Content-Language: en-US X-Provags-ID: V03:K1:EI/VOY+9ZJ0fgiefW1TJ66TjW/1nBBGqMbwK7oGw90b2ji6yU/0 ghaJr1Zm6o61JZ+AwrcV8RHUPoKkSMeOm1chnj/2AIafs331MZZFa0z4RPFgBdtL1mT45uT B2ErLKDjL1Jw5aiJxXwjwzWBzHv6sATTKoD9QI83GKT2RPAk33uuRwLNA5HQP4F/37vQHEJ hojcCByP+M9HRB6OAnVYQ== X-UI-Out-Filterresults: notjunk:1;V03:K0:NwONp7zRo68=:XLkPyHL5adVMgO79JP2oxO dExKtinCI/eJEtEnFEse3nzpeMb45cjVUDUgjnbMsbcctB+6xn1CSAa8DdjVU9+VEKesepd4Q TkJFWu2unt3fwgS0U3KdGYJBEXIVoJn51CzeVTqJ/ugvQT5MMtpAOLXgSQJ3/qJNt4kummmY0 Nk2Q4Uz5x7Gnh1cGDCoa4z49vzfGB2d4EM0L6mZ5jGrG+mCmBFMlq+wEHOds1WOko04+79t5x 5hBx43otZjMqrH0rVMnQAXLzk0of8oFKDOagrvzjBZbeo9zQoH2Q6E82fAWz23TaBDVMJke+o y/bWcaBpyY8RK/G+rq82yZx3rrVgKEYjxYjygwcabkZ9Z5xp0E+CXA/50f8bcV18XMHGCZJ83 ieYbZ2r3PK02G2rHocXFLrovG9L/hj0CHiEKlvu/iMFb8eJfPUf1TaQjnGw6ltO60lLPxHW3W E2yanj6uvg== X-Spam-Status: No, score=-1.1 required=5.0 tests=BAYES_00, KAM_DMARC_STATUS, KAM_LAZY_DOMAIN_SECURITY, NICE_REPLY_A, RCVD_IN_DNSWL_NONE, SPF_HELO_PASS, SPF_NONE, TXREP autolearn=no autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: newlib@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Newlib mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 16 Oct 2020 16:38:36 -0000 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