public inbox for libc-help@sourceware.org
 help / color / mirror / Atom feed
* Is it reasonable to expect errno to be 0 when entering main?
@ 2022-09-26 12:07 Leo Liang
  2022-09-27  6:05 ` Florian Weimer
  0 siblings, 1 reply; 4+ messages in thread
From: Leo Liang @ 2022-09-26 12:07 UTC (permalink / raw)
  To: libc-help; +Cc: alankao, peterlin, dylan, ycliang

Hello guys,

According to the ANSI C standard "7.5 Errors <errno.h>" section 3[1], 
"The value of errno is zero at program startup,
but is never set to zero by any library function."

The "program startup" is also defined in ANSI C standard "5.1.2.2.1 Program startup" section 1[2],
"The function called at program startup is named main."

Therefore, it should be reasonable to expect errno be zero when main is executed.

However, we found that the following program would get non-zero errno under some circumstances.

```
/* errno.c */
#include <errno.h>
#include <stdio.h>

int main()
{
	printf("errno: %d\n", errno);
	return 0;
}


$ riscv64-linux-gcc -O0 -g -static -o errno_st errno.c

[   51.968765] random: fast init done
/mnt # ./errno
errno: 11

[   51.968765] random: fast init done
[  262.517056] crng init done
/mnt # ./errno
errno: 0
```

If the above program is statically-linked, and if the program is executed before "crng init done",
the errno will not be zero when entering main.

There seems to be syscall (__getrandom) before main is entered,
and if the crng is not initialized, the syscall will fail causing the errno to be set.
(_dl_get_origin -> __libc_malloc -> ptmalloc_init -> tcache_key_initialize -> __getrandom)

So we are wondering if we should set errno to zero before entering main.

Best regards,
Leo



The environment we are using is 
Linux Kernel 5.4
GLIBC 2.35
Binutil 2.38

Reference:
[1] ISO/IEC 9899:1999 "7.5 Errors <errno.h>": p.186:3 https://www.open-std.org/jtc1/sc22/WG14/www/docs/n1256.pdf
[2] ISO/IEC 9899:1999 "5.1.2.2.1 Program startup": p.12:1 https://www.open-std.org/jtc1/sc22/WG14/www/docs/n1256.pdf

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Is it reasonable to expect errno to be 0 when entering main?
  2022-09-26 12:07 Is it reasonable to expect errno to be 0 when entering main? Leo Liang
@ 2022-09-27  6:05 ` Florian Weimer
  2022-09-27  7:15   ` Leo Liang
  0 siblings, 1 reply; 4+ messages in thread
From: Florian Weimer @ 2022-09-27  6:05 UTC (permalink / raw)
  To: Leo Liang; +Cc: libc-help, peterlin, dylan, alankao

* Leo Liang:

> If the above program is statically-linked, and if the program is executed before "crng init done",
> the errno will not be zero when entering main.
>
> There seems to be syscall (__getrandom) before main is entered,
> and if the crng is not initialized, the syscall will fail causing the errno to be set.
> (_dl_get_origin -> __libc_malloc -> ptmalloc_init -> tcache_key_initialize -> __getrandom)
>
> So we are wondering if we should set errno to zero before entering main.

This is a bug.  It only affects static linking.  Would you please file
it in Bugzilla?  Thanks.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Is it reasonable to expect errno to be 0 when entering main?
  2022-09-27  6:05 ` Florian Weimer
@ 2022-09-27  7:15   ` Leo Liang
  2022-09-27  7:31     ` Florian Weimer
  0 siblings, 1 reply; 4+ messages in thread
From: Leo Liang @ 2022-09-27  7:15 UTC (permalink / raw)
  To: Florian Weimer; +Cc: libc-help, peterlin, dylan, alankao

Hi Florian,

On Tue, Sep 27, 2022 at 08:05:20AM +0200, Florian Weimer wrote:
> * Leo Liang:
> 
> > If the above program is statically-linked, and if the program is executed before "crng init done",
> > the errno will not be zero when entering main.
> >
> > There seems to be syscall (__getrandom) before main is entered,
> > and if the crng is not initialized, the syscall will fail causing the errno to be set.
> > (_dl_get_origin -> __libc_malloc -> ptmalloc_init -> tcache_key_initialize -> __getrandom)
> >
> > So we are wondering if we should set errno to zero before entering main.
> 
> This is a bug.  It only affects static linking.  Would you please file
> it in Bugzilla?  Thanks.

Will do! Thanks for the timely response!

Besides, Do you have any suggestion as to how this could be fixed?
We could try send out a patch as well!

Best regards,
Leo

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Is it reasonable to expect errno to be 0 when entering main?
  2022-09-27  7:15   ` Leo Liang
@ 2022-09-27  7:31     ` Florian Weimer
  0 siblings, 0 replies; 4+ messages in thread
From: Florian Weimer @ 2022-09-27  7:31 UTC (permalink / raw)
  To: Leo Liang; +Cc: libc-help, peterlin, dylan, alankao

* Leo Liang:

> Besides, Do you have any suggestion as to how this could be fixed?
> We could try send out a patch as well!

Hiding the getrandom failure is probably sufficient.  All other
failures in malloc will lead to the process failing to start, so those
errors don't need to be masked.

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2022-09-27  7:31 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-26 12:07 Is it reasonable to expect errno to be 0 when entering main? Leo Liang
2022-09-27  6:05 ` Florian Weimer
2022-09-27  7:15   ` Leo Liang
2022-09-27  7:31     ` Florian Weimer

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).