From b1b6309d429c2bbbe6ce76d2e866969cd137f7ea Mon Sep 17 00:00:00 2001
From: Simon McVittie <smcv@collabora.com>
Date: Wed, 4 Aug 2021 16:38:15 +0100
Subject: [PATCH] pv_wrap_append_preload: Classify loadable modules and behave
 accordingly

This doesn't make any practical difference yet, but it sets us up to be
able to carry out finer-grained processing based on the loadable_flags.

Signed-off-by: Simon McVittie <smcv@collabora.com>
---
 pressure-vessel/wrap-setup.c       | 57 ++++++++++++++++++++++++------
 tests/pressure-vessel/wrap-setup.c |  6 ++--
 2 files changed, 50 insertions(+), 13 deletions(-)

diff --git a/pressure-vessel/wrap-setup.c b/pressure-vessel/wrap-setup.c
index b28da0605..c0b89790d 100644
--- a/pressure-vessel/wrap-setup.c
+++ b/pressure-vessel/wrap-setup.c
@@ -21,6 +21,7 @@
 #include "wrap-setup.h"
 
 #include "steam-runtime-tools/glib-backports-internal.h"
+#include "steam-runtime-tools/libdl-internal.h"
 #include "steam-runtime-tools/utils-internal.h"
 #include "libglnx/libglnx.h"
 
@@ -416,14 +417,14 @@ pv_wrap_append_preload (GPtrArray *argv,
                         PvRuntime *runtime,
                         FlatpakExports *exports)
 {
+  SrtLoadableKind kind;
+  SrtLoadableFlags loadable_flags;
+
   g_return_if_fail (argv != NULL);
   g_return_if_fail (option != NULL);
   g_return_if_fail (preload != NULL);
   g_return_if_fail (runtime == NULL || PV_IS_RUNTIME (runtime));
 
-  if (*preload == '\0')
-    return;
-
   if (strstr (preload, "gtk3-nocsd") != NULL)
     {
       g_warning ("Disabling gtk3-nocsd %s: it is known to cause crashes.",
@@ -438,11 +439,47 @@ pv_wrap_append_preload (GPtrArray *argv,
       return;
     }
 
-  append_preload_internal (argv,
-                           option,
-                           preload,
-                           env,
-                           flags,
-                           runtime,
-                           exports);
+  kind = _srt_loadable_classify (preload, &loadable_flags);
+
+  switch (kind)
+    {
+      case SRT_LOADABLE_KIND_BASENAME:
+        /* Basenames can't have dynamic string tokens. */
+        g_warn_if_fail ((loadable_flags & SRT_LOADABLE_FLAGS_DYNAMIC_TOKENS) == 0);
+        /* Ideally we would handle this more cleverly, but for now,
+         * pass preload through to the container without adjustment.
+         * It isn't useful to add it to the FlatpakExports, because it
+         * isn't an absolute filename. */
+        append_preload_internal (argv,
+                                 option,
+                                 preload,
+                                 env,
+                                 flags,
+                                 runtime,
+                                 NULL);
+        break;
+
+      case SRT_LOADABLE_KIND_PATH:
+        /* Ideally we would handle this more cleverly, but for now,
+         * assume preload is a filename: adjust the beginning if it's
+         * in /usr/ etc., and add it to the exports if absolute. */
+        append_preload_internal (argv,
+                                 option,
+                                 preload,
+                                 env,
+                                 flags,
+                                 runtime,
+                                 exports);
+        break;
+
+      case SRT_LOADABLE_KIND_ERROR:
+      default:
+        /* Empty string or similar syntactically invalid token:
+         * ignore with a warning. Since steam-runtime-tools!352 and
+         * steamlinuxruntime!64, the wrapper scripts don't give us
+         * an empty argument any more. */
+        g_warning ("Ignoring invalid loadable module \"%s\"", preload);
+
+        break;
+    }
 }
diff --git a/tests/pressure-vessel/wrap-setup.c b/tests/pressure-vessel/wrap-setup.c
index 0aa8ce66a..daf82aec1 100644
--- a/tests/pressure-vessel/wrap-setup.c
+++ b/tests/pressure-vessel/wrap-setup.c
@@ -207,8 +207,8 @@ populate_ld_preload (Fixture *f,
     const char *warning;
   } preloads[] =
   {
-    { "" },
-    { "" },
+    { "", .warning = "Ignoring invalid loadable module \"\"" },
+    { "", .warning = "Ignoring invalid loadable module \"\"" },
     { "/app/lib/libpreloadA.so" },
     { "/platform/plat-$PLATFORM/libpreloadP.so" },
     { "/opt/${LIB}/libpreloadL.so" },
@@ -226,7 +226,7 @@ populate_ld_preload (Fixture *f,
       "/usr/local/lib/libgtk3-nocsd.so.0",
       .warning = "Disabling gtk3-nocsd LD_PRELOAD: it is known to cause crashes.",
     },
-    { "" },
+    { "", .warning = "Ignoring invalid loadable module \"\"" },
   };
   gsize i;
 
-- 
GitLab