NetBSD-Bugs archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

lib/57756: Incorrect order of .fini_array indirect functions calling



>Number:         57756
>Category:       lib
>Synopsis:       Incorrect order of .fini_array indirect functions calling
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    lib-bug-people
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Tue Dec 05 20:15:00 +0000 2023
>Originator:     Dmitry Chestnykh
>Release:        trunk
>Organization:
Kaspersky Lab
>Environment:
>Description:
The `_finiarray` function in crt0-common.c has to perform calls to destructors in reverse order. Such behaviour is implemented in all libcs I have ever seen i.e in Glibc (https://sourceware.org/git/?p=glibc.git;a=blob;f=csu/libc-start.c;h=c3bb6d09bc2abfea5b4da672ad55c2633cc266d8;hb=HEAD#l194),
llvm-libc (https://github.com/llvm/llvm-project/blob/main/libc/startup/linux/x86_64/start.cpp#L140),
uClibc (https://github.com/kraj/uClibc/blob/master/libc/misc/internals/__uClibc_main.c#L303),
FreeBSD libc (https://github.com/freebsd/freebsd-src/blob/3c097b06a71715ec9ae86430ee94e25e954a1e36/lib/libc/csu/libc_start1.c#L81).
This behavior is logical because if constructors are called in direct order, then destructors are called in reverse order.
>How-To-Repeat:

>Fix:
--- a/lib/csu/common/crt0-common.c      2023-12-05 23:04:36.330759078 +0300
+++ b/lib/csu/common/crt0-common.c      2023-12-05 23:05:31.630760929 +0300
@@ -121,8 +121,9 @@
 static void
 _finiarray(void)
 {
-       for (const fptr_t *f = __fini_array_start; f < __fini_array_end; f++) {
-               (*f)();
+       size_t i = __fini_array_end - __fini_array_start;
+       while (i-- > 0) {
+               (*__fini_array_start[i])();
        }
 }



Home | Main Index | Thread Index | Old Index