83f30e
commit 1df71d32fe5f5905ffd5d100e5e9ca8ad6210891
83f30e
Author: Florian Weimer <fweimer@redhat.com>
83f30e
Date:   Tue Sep 20 11:00:42 2022 +0200
83f30e
83f30e
    elf: Implement force_first handling in _dl_sort_maps_dfs (bug 28937)
83f30e
    
83f30e
    The implementation in _dl_close_worker requires that the first
83f30e
    element of l_initfini is always this very map (“We are always the
83f30e
    zeroth entry, and since we don't include ourselves in the
83f30e
    dependency analysis start at 1.”).  Rather than fixing that
83f30e
    assumption, this commit adds an implementation of the force_first
83f30e
    argument to the new dependency sorting algorithm.  This also means
83f30e
    that the directly dlopen'ed shared object is always initialized last,
83f30e
    which is the least surprising behavior in the presence of cycles.
83f30e
    
83f30e
    Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>
83f30e
83f30e
diff --git a/elf/dl-sort-maps.c b/elf/dl-sort-maps.c
83f30e
index 7a586749adc3fa7d..6f5c17b47b98fbc7 100644
83f30e
--- a/elf/dl-sort-maps.c
83f30e
+++ b/elf/dl-sort-maps.c
83f30e
@@ -182,8 +182,9 @@ dfs_traversal (struct link_map ***rpo, struct link_map *map,
83f30e
 
83f30e
 static void
83f30e
 _dl_sort_maps_dfs (struct link_map **maps, unsigned int nmaps,
83f30e
-		   bool force_first __attribute__ ((unused)), bool for_fini)
83f30e
+		   bool force_first, bool for_fini)
83f30e
 {
83f30e
+  struct link_map *first_map = maps[0];
83f30e
   for (int i = nmaps - 1; i >= 0; i--)
83f30e
     maps[i]->l_visited = 0;
83f30e
 
83f30e
@@ -208,14 +209,6 @@ _dl_sort_maps_dfs (struct link_map **maps, unsigned int nmaps,
83f30e
      Adjusting the order so that maps[0] is last traversed naturally avoids
83f30e
      this problem.
83f30e
 
83f30e
-     Further, the old "optimization" of skipping the main object at maps[0]
83f30e
-     from the call-site (i.e. _dl_sort_maps(maps+1,nmaps-1)) is in general
83f30e
-     no longer valid, since traversing along object dependency-links
83f30e
-     may "find" the main object even when it is not included in the initial
83f30e
-     order (e.g. a dlopen()'ed shared object can have circular dependencies
83f30e
-     linked back to itself). In such a case, traversing N-1 objects will
83f30e
-     create a N-object result, and raise problems.
83f30e
-
83f30e
      To summarize, just passing in the full list, and iterating from back
83f30e
      to front makes things much more straightforward.  */
83f30e
 
83f30e
@@ -274,6 +267,27 @@ _dl_sort_maps_dfs (struct link_map **maps, unsigned int nmaps,
83f30e
     }
83f30e
 
83f30e
   memcpy (maps, rpo, sizeof (struct link_map *) * nmaps);
83f30e
+
83f30e
+  /* Skipping the first object at maps[0] is not valid in general,
83f30e
+     since traversing along object dependency-links may "find" that
83f30e
+     first object even when it is not included in the initial order
83f30e
+     (e.g., a dlopen'ed shared object can have circular dependencies
83f30e
+     linked back to itself).  In such a case, traversing N-1 objects
83f30e
+     will create a N-object result, and raise problems.  Instead,
83f30e
+     force the object back into first place after sorting.  This naive
83f30e
+     approach may introduce further dependency ordering violations
83f30e
+     compared to rotating the cycle until the first map is again in
83f30e
+     the first position, but as there is a cycle, at least one
83f30e
+     violation is already present.  */
83f30e
+  if (force_first && maps[0] != first_map)
83f30e
+    {
83f30e
+      int i;
83f30e
+      for (i = 0; maps[i] != first_map; ++i)
83f30e
+	;
83f30e
+      assert (i < nmaps);
83f30e
+      memmove (&maps[1], maps, i * sizeof (maps[0]));
83f30e
+      maps[0] = first_map;
83f30e
+    }
83f30e
 }
83f30e
 
83f30e
 void
83f30e
diff --git a/elf/dso-sort-tests-1.def b/elf/dso-sort-tests-1.def
83f30e
index 5f7f18ef270bc12d..4bf9052db16fb352 100644
83f30e
--- a/elf/dso-sort-tests-1.def
83f30e
+++ b/elf/dso-sort-tests-1.def
83f30e
@@ -64,3 +64,10 @@ output: b>a>{}
83f30e
 tst-bz15311: {+a;+e;+f;+g;+d;%d;-d;-g;-f;-e;-a};a->b->c->d;d=>[ba];c=>a;b=>e=>a;c=>f=>b;d=>g=>c
83f30e
 output(glibc.rtld.dynamic_sort=1): {+a[d>c>b>a>];+e[e>];+f[f>];+g[g>];+d[];%d(b(e(a()))a()g(c(a()f(b(e(a()))))));-d[];-g[];-f[];-e[];-a[
83f30e
 output(glibc.rtld.dynamic_sort=2): {+a[d>c>b>a>];+e[e>];+f[f>];+g[g>];+d[];%d(b(e(a()))a()g(c(a()f(b(e(a()))))));-d[];-g[];-f[];-e[];-a[
83f30e
+
83f30e
+# Test that even in the presence of dependency loops involving dlopen'ed
83f30e
+# object, that object is initialized last (and not unloaded prematurely).
83f30e
+# Final destructor order is indeterminate due to the cycle.
83f30e
+tst-bz28937: {+a;+b;-b;+c;%c};a->a1;a->a2;a2->a;b->b1;c->a1;c=>a1
83f30e
+output(glibc.rtld.dynamic_sort=1): {+a[a2>a1>a>];+b[b1>b>];-b[<b<b1];+c[c>];%c(a1());}
83f30e
+output(glibc.rtld.dynamic_sort=2): {+a[a2>a1>a>];+b[b1>b>];-b[<b<b1];+c[c>];%c(a1());}