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