talloc-2.1.12 issues with library destructor

Lukas Slebodnik lslebodn at redhat.com
Tue Mar 27 16:44:44 UTC 2018


On (26/03/18 18:34), Andreas Schneider wrote:
>On Monday, 26 March 2018 18:12:26 CEST Ralph Böhme wrote:
>> On Mon, Mar 26, 2018 at 06:00:59PM +0200, Andreas Schneider wrote:
>> > On Monday, 26 March 2018 16:06:26 CEST Ralph Böhme wrote:
>> > > Hi Andreas,
>> > > 
>> > > On Mon, Mar 26, 2018 at 03:01:56PM +0200, Andreas Schneider via samba-
>> > 
>> > technical wrote:
>> > > > destructors are normally executed after the program returns from
>> > > > main() or
>> > > > after exit() is called. talloc tries to clean up its null context
>> > > > memory
>> > > > and you have obviously a talloc destructor defined for your kcm_data
>> > > > talloc context!
>> > > > 
>> > > > I don't thinks this is an issue in talloc, but sssd should cleanup the
>> > > > memory in orderly_shutdown() before it calls exit()!
>> > > 
>> > > while I agree that we should work on deprecating and later removing
>> > > talloc_autofree, it's currently used and the recent change in commit
>> > > 41b6810ba01f44537f470c806adb8686e1a39c48 in talloc which was aimed at
>> > > fixing long standing Samba bug 7587 seems to cause this regression.
>> > > 
>> > > We could try throwing destructor priorizy INT_MAX at the problem hoping
>> > > noone else uses such a high value.
>> > 
>> > Ah, now I got it, however it is the reverse:
>> > 
>> > +/* The initialization priority for entities for which no explicit
>> > +   initialization priority has been specified.  */
>> > +#define DEFAULT_INIT_PRIORITY 65535
>> > +
>> > +/* The maximum allowed initialization priority.  */
>> > +#define MAX_INIT_PRIORITY 65535
>> > +
>> > +/* The largest priority value reserved for use by system runtime
>> > +   libraries.  */
>> > +#define MAX_RESERVED_INIT_PRIORITY 100
>> > 
>> > So you need to set it to 101.
>> 
>> <https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-de
>> structor-function-attribute>
>> 
>>   You may provide an optional integer priority to control the order in which
>> constructor and destructor functions are run. A constructor with a smaller
>> priority number runs before a constructor with a larger priority number;
>> the opposite relationship holds for destructors.
>> 
>> As we want to ensure the talloc destructor gets kicked of first to resemble
>> atexit() ordering, iiuc we must use MAX_INIT_PRIORITY.
>
>Which doesn't make sense if the default priority for the destructor is 65535 
>and the max value too, then you can't run anything prior to the default 
>destructors of other libraries.
>
>https://github.com/gcc-mirror/gcc/blob/
>7604a798428266cbeb56501b8f0f967676fe1205/gcc/tree.h
>
>https://github.com/gcc-mirror/gcc/blob/
>da8dff89fa9398f04b107e388cb706517ced9505/gcc/tree.c
>

I tried to apply following patch which added priority
but it did not help. gcc fails with prio 65536

--- a/talloc.c
+++ b/talloc.c
@@ -431,7 +431,7 @@ void talloc_lib_init(void)
 #endif
 
 #ifdef HAVE_DESTRUCTOR_ATTRIBUTE
-void talloc_lib_fini(void) __attribute__((destructor));
+void talloc_lib_fini(void) __attribute__((destructor (65535)));
 void talloc_lib_fini(void)
 #else /* ! HAVE_DESTRUCTOR_ATTRIBUTE */
 static void talloc_lib_fini(void)


And it seems that priority for destructors are considered only within library
and destructors in different library are executed in order which was used
at linking time.

And it seems that atexit previously worked quite well because it was firstly
called quite late comparing (1st usage of talloc_autofree_context) and
therefore it was called before all library destructors.

Here is a part of output from trivial test case generated by attached shell
script (the same ouput is with gcc and clang)


      construct2 constructor in libmy1.c prio 101
      construct constructor in libmy1.c
      construct2 constructor in libmy2.c prio 101
      construct constructor in libmy2.c
      construct2 constructor in libmy3.c prio 101
      construct constructor in libmy3.c

          [ main body of program ]

                  some_function_libmy1()

                  some_function_libmy2()

                  some_function_libmy3()

          [ main before return ]

      atexit_from_some_function atexit in libmy3.c

      atexit_from_some_function atexit in libmy2.c

      atexit_from_some_function atexit in libmy1.c
      destruct destructor in libmy3.c

      constructor_atexit atexit in libmy3.c
      destruct3 destructor in libmy3.c prio 200
      destruct2 destructor in libmy3.c prio 101
      destruct destructor in libmy2.c

      constructor_atexit atexit in libmy2.c
      destruct3 destructor in libmy2.c prio 200
      destruct2 destructor in libmy2.c prio 101
      destruct destructor in libmy1.c

      constructor_atexit atexit in libmy1.c
      destruct3 destructor in libmy1.c prio 200
      destruct2 destructor in libmy1.c prio 101

LS
-------------- next part --------------
A non-text attachment was scrubbed...
Name: test.sh
Type: application/x-sh
Size: 1893 bytes
Desc: not available
URL: <http://lists.samba.org/pipermail/samba-technical/attachments/20180327/9d44eb10/test.sh>


More information about the samba-technical mailing list