From a43f0cd714209447a263f677487a4ebd125ec160 Mon Sep 17 00:00:00 2001
From: Simon McVittie <smcv@collabora.com>
Date: Fri, 7 Feb 2025 18:14:38 +0000
Subject: [PATCH 1/8] pv-wrap: Store the real or mock root directory in the
 PvWrapContext

This makes no practical difference yet, but we can use this for more
consistent handling of a mock root directory for unit-testing in future.

Signed-off-by: Simon McVittie <smcv@collabora.com>
---
 pressure-vessel/wrap-context.c     | 71 +++++++++++++++++++++++++++++-
 pressure-vessel/wrap-context.h     |  4 +-
 pressure-vessel/wrap-setup.c       |  8 ++--
 pressure-vessel/wrap-setup.h       |  1 -
 pressure-vessel/wrap.c             | 27 ++++++------
 tests/pressure-vessel/wrap-setup.c |  3 +-
 6 files changed, 92 insertions(+), 22 deletions(-)

diff --git a/pressure-vessel/wrap-context.c b/pressure-vessel/wrap-context.c
index dd711208e..194f40994 100644
--- a/pressure-vessel/wrap-context.c
+++ b/pressure-vessel/wrap-context.c
@@ -98,9 +98,12 @@ pv_wrap_options_clear (PvWrapOptions *self)
 
 enum {
   PROP_0,
+  PROP_CURRENT_ROOT,
   N_PROPERTIES
 };
 
+static GParamSpec *properties[N_PROPERTIES] = { NULL };
+
 struct _PvWrapContextClass
 {
   GObjectClass parent_class;
@@ -120,6 +123,56 @@ pv_wrap_context_init (PvWrapContext *self)
   self->options.copy_runtime = self->is_flatpak_env;
 }
 
+static void
+pv_wrap_context_get_property (GObject *object,
+                              guint prop_id,
+                              GValue *value,
+                              GParamSpec *pspec)
+{
+  PvWrapContext *self = PV_WRAP_CONTEXT (object);
+
+  switch (prop_id)
+    {
+      case PROP_CURRENT_ROOT:
+        g_value_set_object (value, self->current_root);
+        break;
+
+      default:
+        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
+
+static void
+pv_wrap_context_set_property (GObject *object,
+                              guint prop_id,
+                              const GValue *value,
+                              GParamSpec *pspec)
+{
+  PvWrapContext *self = PV_WRAP_CONTEXT (object);
+
+  switch (prop_id)
+    {
+      case PROP_CURRENT_ROOT:
+        /* Construct-only */
+        g_return_if_fail (self->current_root == NULL);
+        self->current_root = g_value_dup_object (value);
+        break;
+
+      default:
+        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
+
+static void
+pv_wrap_context_dispose (GObject *object)
+{
+  PvWrapContext *self = PV_WRAP_CONTEXT (object);
+
+  g_clear_object (&self->current_root);
+
+  G_OBJECT_CLASS (pv_wrap_context_parent_class)->dispose (object);
+}
+
 static void
 pv_wrap_context_finalize (GObject *object)
 {
@@ -139,13 +192,29 @@ pv_wrap_context_class_init (PvWrapContextClass *cls)
 {
   GObjectClass *object_class = G_OBJECT_CLASS (cls);
 
+  object_class->get_property = pv_wrap_context_get_property;
+  object_class->set_property = pv_wrap_context_set_property;
+  object_class->dispose = pv_wrap_context_dispose;
   object_class->finalize = pv_wrap_context_finalize;
+
+  properties[PROP_CURRENT_ROOT] =
+    g_param_spec_object ("current-root", "Current root",
+                         ("Real or mock root directory for the current "
+                          "filesystem namespace"),
+                         SRT_TYPE_SYSROOT,
+                         (G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
+                          G_PARAM_STATIC_STRINGS));
+
+  g_object_class_install_properties (object_class, N_PROPERTIES, properties);
 }
 
 PvWrapContext *
-pv_wrap_context_new (GError **error)
+pv_wrap_context_new (SrtSysroot *current_root,
+                     GError **error)
 {
+  /* Can't actually fail right now */
   return g_object_new (PV_TYPE_WRAP_CONTEXT,
+                       "current-root", current_root,
                        NULL);
 }
 
diff --git a/pressure-vessel/wrap-context.h b/pressure-vessel/wrap-context.h
index 17d2db2c2..a8d8ce1ed 100644
--- a/pressure-vessel/wrap-context.h
+++ b/pressure-vessel/wrap-context.h
@@ -93,6 +93,7 @@ struct _PvWrapContext
   GObject parent_instance;
 
   GHashTable *paths_not_exported;
+  SrtSysroot *current_root;
   gchar **original_argv;
   gchar **original_environ;
 
@@ -118,7 +119,8 @@ struct _PvWrapContext
 GType pv_wrap_context_get_type (void);
 G_DEFINE_AUTOPTR_CLEANUP_FUNC (PvWrapContext, g_object_unref)
 
-PvWrapContext *pv_wrap_context_new (GError **error);
+PvWrapContext *pv_wrap_context_new (SrtSysroot *current_root,
+                                    GError **error);
 
 gboolean pv_wrap_options_parse_environment (PvWrapOptions *self,
                                             GError **error);
diff --git a/pressure-vessel/wrap-setup.c b/pressure-vessel/wrap-setup.c
index 8570ea535..f02b3a926 100644
--- a/pressure-vessel/wrap-setup.c
+++ b/pressure-vessel/wrap-setup.c
@@ -1161,7 +1161,6 @@ static const EnvMount known_required_env[] =
 
 static void
 bind_and_propagate_from_environ (PvWrapContext *self,
-                                 SrtSysroot *sysroot,
                                  PvHomeMode home_mode,
                                  FlatpakExports *exports,
                                  SrtEnvOverlay *container_env,
@@ -1171,14 +1170,18 @@ bind_and_propagate_from_environ (PvWrapContext *self,
 {
   g_auto(GStrv) values = NULL;
   FlatpakFilesystemMode mode = FLATPAK_FILESYSTEM_MODE_READ_WRITE;
+  SrtSysroot *sysroot;
   const char *value;
   const char *before;
   const char *after;
   gboolean changed = FALSE;
   gsize i;
 
+  g_return_if_fail (PV_IS_WRAP_CONTEXT (self));
   g_return_if_fail (exports != NULL);
   g_return_if_fail (variable != NULL);
+  sysroot = self->current_root;
+  g_return_if_fail (sysroot != NULL);
 
   if (home_mode != PV_HOME_MODE_SHARED
       && (flags & ENV_MOUNT_FLAGS_IF_HOME_SHARED))
@@ -1266,7 +1269,6 @@ bind_and_propagate_from_environ (PvWrapContext *self,
  */
 void
 pv_bind_and_propagate_from_environ (PvWrapContext *self,
-                                    SrtSysroot *sysroot,
                                     PvHomeMode home_mode,
                                     FlatpakExports *exports,
                                     SrtEnvOverlay *container_env)
@@ -1285,7 +1287,7 @@ pv_bind_and_propagate_from_environ (PvWrapContext *self,
         {
           /* If we're using bubblewrap directly, we can and must make
            * sure that all required directories are bind-mounted */
-          bind_and_propagate_from_environ (self, sysroot, home_mode,
+          bind_and_propagate_from_environ (self, home_mode,
                                            exports, container_env,
                                            name,
                                            known_required_env[i].flags,
diff --git a/pressure-vessel/wrap-setup.h b/pressure-vessel/wrap-setup.h
index 936314a4d..59e914ff9 100644
--- a/pressure-vessel/wrap-setup.h
+++ b/pressure-vessel/wrap-setup.h
@@ -100,7 +100,6 @@ void pv_wrap_detect_virtualization (SrtSysroot **interpreter_root_out,
 void pv_share_temp_dir (FlatpakExports *exports,
                         SrtEnvOverlay *container_env);
 void pv_bind_and_propagate_from_environ (PvWrapContext *self,
-                                         SrtSysroot *sysroot,
                                          PvHomeMode home_mode,
                                          FlatpakExports *exports,
                                          SrtEnvOverlay *container_env);
diff --git a/pressure-vessel/wrap.c b/pressure-vessel/wrap.c
index eddd59e00..dbfa23ab5 100644
--- a/pressure-vessel/wrap.c
+++ b/pressure-vessel/wrap.c
@@ -130,7 +130,18 @@ main (int argc,
       goto out;
     }
 
-  self = pv_wrap_context_new (error);
+  /* FEX-Emu transparently rewrites most file I/O to check its "rootfs"
+   * first, and only use the real root if the corresponding file
+   * doesn't exist in the "rootfs". In many places we actively don't want
+   * this, because we're inspecting paths in order to pass them to bwrap,
+   * which will use them to set up bind-mounts, which are not subject to
+   * FEX-Emu's rewriting; so bypass it here. */
+  real_root = _srt_sysroot_new_real_root (error);
+
+  if (real_root == NULL)
+    return FALSE;
+
+  self = pv_wrap_context_new (real_root, error);
 
   if (self == NULL)
     goto out;
@@ -438,17 +449,6 @@ main (int argc,
       goto out;
     }
 
-  /* FEX-Emu transparently rewrites most file I/O to check its "rootfs"
-   * first, and only use the real root if the corresponding file
-   * doesn't exist in the "rootfs". In many places we actively don't want
-   * this, because we're inspecting paths in order to pass them to bwrap,
-   * which will use them to set up bind-mounts, which are not subject to
-   * FEX-Emu's rewriting; so bypass it here. */
-  real_root = _srt_sysroot_new_real_root (error);
-
-  if (real_root == NULL)
-    return FALSE;
-
   /* Invariant: we are in exactly one of these two modes */
   g_assert (((flatpak_subsandbox != NULL)
              + (!self->is_flatpak_env))
@@ -853,8 +853,7 @@ main (int argc,
     }
   while (0);
 
-  pv_bind_and_propagate_from_environ (self, real_root, home_mode,
-                                      exports, container_env);
+  pv_bind_and_propagate_from_environ (self, home_mode, exports, container_env);
 
   if (flatpak_subsandbox == NULL)
     {
diff --git a/tests/pressure-vessel/wrap-setup.c b/tests/pressure-vessel/wrap-setup.c
index c238b8af0..7ac1b8b06 100644
--- a/tests/pressure-vessel/wrap-setup.c
+++ b/tests/pressure-vessel/wrap-setup.c
@@ -243,7 +243,7 @@ setup (Fixture *f,
   glnx_opendirat (AT_FDCWD, f->var, TRUE, &f->var_fd, &local_error);
   g_assert_no_error (local_error);
 
-  f->context = pv_wrap_context_new (&local_error);
+  f->context = pv_wrap_context_new (f->mock_host, &local_error);
   g_assert_no_error (local_error);
   f->bwrap = flatpak_bwrap_new (flatpak_bwrap_empty_env);
   f->env = g_get_environ ();
@@ -2141,7 +2141,6 @@ test_use_home_shared (Fixture *f,
   /* Don't crash on warnings here */
   was_fatal = g_log_set_always_fatal (G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL);
   pv_bind_and_propagate_from_environ (f->context,
-                                      f->mock_host,
                                       PV_HOME_MODE_SHARED,
                                       env_exports,
                                       container_env);
-- 
GitLab


From 135f38dd47f41fb3ad876a348b50976942fbdb8d Mon Sep 17 00:00:00 2001
From: Simon McVittie <smcv@collabora.com>
Date: Fri, 7 Feb 2025 15:19:31 +0000
Subject: [PATCH 2/8] pv-wrap: Make it more obvious that we're using the
 environment read-only

We're just looking at this, not altering it (not that we would be able
to alter it with correct memory management under this calling
convention, but that isn't necessarily immediately obvious to a reader).

Signed-off-by: Simon McVittie <smcv@collabora.com>
---
 pressure-vessel/wrap-setup.c       | 12 ++++++------
 pressure-vessel/wrap-setup.h       |  2 +-
 pressure-vessel/wrap.c             |  2 +-
 tests/pressure-vessel/wrap-setup.c |  2 +-
 4 files changed, 9 insertions(+), 9 deletions(-)

diff --git a/pressure-vessel/wrap-setup.c b/pressure-vessel/wrap-setup.c
index f02b3a926..22c73f167 100644
--- a/pressure-vessel/wrap-setup.c
+++ b/pressure-vessel/wrap-setup.c
@@ -551,7 +551,7 @@ append_preload_internal (GPtrArray *argv,
                          gsize abi_index,
                          const char *export_path,
                          const char *original_path,
-                         GStrv env,
+                         const char * const *env,
                          PvAppendPreloadFlags flags,
                          PvRuntime *runtime,
                          FlatpakExports *exports)
@@ -580,7 +580,7 @@ append_preload_internal (GPtrArray *argv,
 
       if (exports != NULL && export_path != NULL && export_path[0] == '/')
         {
-          const gchar *steam_path = g_environ_getenv (env, "STEAM_COMPAT_CLIENT_INSTALL_PATH");
+          const gchar *steam_path = _srt_environ_getenv (env, "STEAM_COMPAT_CLIENT_INSTALL_PATH");
 
           if (steam_path != NULL
               && flatpak_has_path_prefix (export_path, steam_path))
@@ -623,7 +623,7 @@ static void
 append_preload_unsupported_token (GPtrArray *argv,
                                   PvPreloadVariableIndex which,
                                   const char *preload,
-                                  GStrv env,
+                                  const char * const *env,
                                   PvAppendPreloadFlags flags,
                                   PvRuntime *runtime,
                                   FlatpakExports *exports)
@@ -698,7 +698,7 @@ static void
 append_preload_per_architecture (GPtrArray *argv,
                                  PvPreloadVariableIndex which,
                                  const char *preload,
-                                 GStrv env,
+                                 const char * const *env,
                                  PvAppendPreloadFlags flags,
                                  PvRuntime *runtime,
                                  FlatpakExports *exports)
@@ -797,7 +797,7 @@ static void
 append_preload_basename (GPtrArray *argv,
                          PvPreloadVariableIndex which,
                          const char *preload,
-                         GStrv env,
+                         const char * const *env,
                          PvAppendPreloadFlags flags,
                          PvRuntime *runtime,
                          FlatpakExports *exports)
@@ -883,7 +883,7 @@ void
 pv_wrap_append_preload (GPtrArray *argv,
                         PvPreloadVariableIndex which,
                         const char *preload,
-                        GStrv env,
+                        const char * const *env,
                         PvAppendPreloadFlags flags,
                         PvRuntime *runtime,
                         FlatpakExports *exports)
diff --git a/pressure-vessel/wrap-setup.h b/pressure-vessel/wrap-setup.h
index 59e914ff9..97c193c66 100644
--- a/pressure-vessel/wrap-setup.h
+++ b/pressure-vessel/wrap-setup.h
@@ -87,7 +87,7 @@ typedef enum
 void pv_wrap_append_preload (GPtrArray *argv,
                              PvPreloadVariableIndex which,
                              const char *preload,
-                             GStrv env,
+                             const char * const *env,
                              PvAppendPreloadFlags flags,
                              PvRuntime *runtime,
                              FlatpakExports *exports);
diff --git a/pressure-vessel/wrap.c b/pressure-vessel/wrap.c
index dbfa23ab5..838ebb637 100644
--- a/pressure-vessel/wrap.c
+++ b/pressure-vessel/wrap.c
@@ -845,7 +845,7 @@ main (int argc,
           pv_wrap_append_preload (adverb_preload_argv,
                                   module->which,
                                   module->preload,
-                                  environ,
+                                  _srt_const_strv (environ),
                                   append_preload_flags,
                                   runtime,
                                   exports);
diff --git a/tests/pressure-vessel/wrap-setup.c b/tests/pressure-vessel/wrap-setup.c
index 7ac1b8b06..4f83040d2 100644
--- a/tests/pressure-vessel/wrap-setup.c
+++ b/tests/pressure-vessel/wrap-setup.c
@@ -1614,7 +1614,7 @@ populate_ld_preload (Fixture *f,
       pv_wrap_append_preload (argv,
                               PV_PRELOAD_VARIABLE_INDEX_LD_PRELOAD,
                               preloads[i].string,
-                              f->env,
+                              _srt_const_strv (f->env),
                               flags | PV_APPEND_PRELOAD_FLAGS_IN_UNIT_TESTS,
                               runtime,
                               exports);
-- 
GitLab


From 1810e830afcc2976e029940c37ef8b5c52519bd8 Mon Sep 17 00:00:00 2001
From: Simon McVittie <smcv@collabora.com>
Date: Fri, 7 Feb 2025 15:40:49 +0000
Subject: [PATCH 3/8] tests: Store the mock environment in PvWrapContext
 instead of duplicating

This makes the tests a little bit closer to reality.

Signed-off-by: Simon McVittie <smcv@collabora.com>
---
 tests/pressure-vessel/wrap-setup.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)

diff --git a/tests/pressure-vessel/wrap-setup.c b/tests/pressure-vessel/wrap-setup.c
index 4f83040d2..0cd08b609 100644
--- a/tests/pressure-vessel/wrap-setup.c
+++ b/tests/pressure-vessel/wrap-setup.c
@@ -83,7 +83,6 @@ typedef struct
   gchar *tmpdir;
   gchar *mock_runtime;
   gchar *var;
-  GStrv env;
   int tmpdir_fd;
   int mock_runtime_fd;
   int var_fd;
@@ -246,7 +245,13 @@ setup (Fixture *f,
   f->context = pv_wrap_context_new (f->mock_host, &local_error);
   g_assert_no_error (local_error);
   f->bwrap = flatpak_bwrap_new (flatpak_bwrap_empty_env);
-  f->env = g_get_environ ();
+
+  /* Some tests need to know where Steam is installed;
+   * pretend that we have it installed in /steam */
+  f->context->original_environ = g_environ_setenv (f->context->original_environ,
+                                                   "STEAM_COMPAT_CLIENT_INSTALL_PATH",
+                                                   "/steam",
+                                                   TRUE);
 }
 
 static void
@@ -285,9 +290,6 @@ setup_ld_preload (Fixture *f,
       fixture_populate_dir (f, f->mock_host->fd, touch_i386, G_N_ELEMENTS (touch_i386));
 #endif
     }
-
-  f->env = g_environ_setenv (f->env, "STEAM_COMPAT_CLIENT_INSTALL_PATH",
-                             "/steam", TRUE);
 }
 
 static void
@@ -311,7 +313,6 @@ teardown (Fixture *f,
   g_clear_pointer (&f->mock_runtime, g_free);
   g_clear_pointer (&f->tmpdir, g_free);
   g_clear_pointer (&f->var, g_free);
-  g_clear_pointer (&f->env, g_strfreev);
   g_clear_pointer (&f->bwrap, flatpak_bwrap_free);
 
   tests_check_fd_leaks_leave (f->old_fds);
@@ -1614,7 +1615,7 @@ populate_ld_preload (Fixture *f,
       pv_wrap_append_preload (argv,
                               PV_PRELOAD_VARIABLE_INDEX_LD_PRELOAD,
                               preloads[i].string,
-                              _srt_const_strv (f->env),
+                              _srt_const_strv (f->context->original_environ),
                               flags | PV_APPEND_PRELOAD_FLAGS_IN_UNIT_TESTS,
                               runtime,
                               exports);
-- 
GitLab


From 73733519166816e23156cb13448a0390f22123f8 Mon Sep 17 00:00:00 2001
From: Simon McVittie <smcv@collabora.com>
Date: Fri, 7 Feb 2025 15:46:56 +0000
Subject: [PATCH 4/8] pv-wrap: Use the PvWrapContext to inspect the original
 environment

This keeps more of the things we have to mock for unit testing together,
avoiding needing to thread a copy of the environment through the
LD_PRELOAD setup code.

Signed-off-by: Simon McVittie <smcv@collabora.com>
---
 pressure-vessel/wrap-setup.c       | 58 +++++++++++++++---------------
 pressure-vessel/wrap-setup.h       |  4 +--
 pressure-vessel/wrap.c             |  4 +--
 tests/pressure-vessel/wrap-setup.c |  4 +--
 4 files changed, 35 insertions(+), 35 deletions(-)

diff --git a/pressure-vessel/wrap-setup.c b/pressure-vessel/wrap-setup.c
index 22c73f167..8aafb6cc3 100644
--- a/pressure-vessel/wrap-setup.c
+++ b/pressure-vessel/wrap-setup.c
@@ -546,12 +546,12 @@ pv_wrap_move_into_scope (const char *steam_app_id)
 }
 
 static void
-append_preload_internal (GPtrArray *argv,
+append_preload_internal (PvWrapContext *context,
+                         GPtrArray *argv,
                          PvPreloadVariableIndex which,
                          gsize abi_index,
                          const char *export_path,
                          const char *original_path,
-                         const char * const *env,
                          PvAppendPreloadFlags flags,
                          PvRuntime *runtime,
                          FlatpakExports *exports)
@@ -580,7 +580,7 @@ append_preload_internal (GPtrArray *argv,
 
       if (exports != NULL && export_path != NULL && export_path[0] == '/')
         {
-          const gchar *steam_path = _srt_environ_getenv (env, "STEAM_COMPAT_CLIENT_INSTALL_PATH");
+          const gchar *steam_path = g_environ_getenv (context->original_environ, "STEAM_COMPAT_CLIENT_INSTALL_PATH");
 
           if (steam_path != NULL
               && flatpak_has_path_prefix (export_path, steam_path))
@@ -620,10 +620,10 @@ append_preload_internal (GPtrArray *argv,
  * Arguments are the same as for pv_wrap_append_preload().
  */
 static void
-append_preload_unsupported_token (GPtrArray *argv,
+append_preload_unsupported_token (PvWrapContext *context,
+                                  GPtrArray *argv,
                                   PvPreloadVariableIndex which,
                                   const char *preload,
-                                  const char * const *env,
                                   PvAppendPreloadFlags flags,
                                   PvRuntime *runtime,
                                   FlatpakExports *exports)
@@ -673,12 +673,12 @@ append_preload_unsupported_token (GPtrArray *argv,
                preload);
     }
 
-  append_preload_internal (argv,
+  append_preload_internal (context,
+                           argv,
                            which,
                            PV_UNSPECIFIED_ABI,
                            export_path,
                            preload,
-                           env,
                            flags,
                            runtime,
                            exports);
@@ -695,10 +695,10 @@ append_preload_unsupported_token (GPtrArray *argv,
  * Arguments are the same as for pv_wrap_append_preload().
  */
 static void
-append_preload_per_architecture (GPtrArray *argv,
+append_preload_per_architecture (PvWrapContext *context,
+                                 GPtrArray *argv,
                                  PvPreloadVariableIndex which,
                                  const char *preload,
-                                 const char * const *env,
                                  PvAppendPreloadFlags flags,
                                  PvRuntime *runtime,
                                  FlatpakExports *exports)
@@ -775,12 +775,12 @@ append_preload_per_architecture (GPtrArray *argv,
         {
           g_debug ("Found %s version of %s at %s",
                    multiarch_tuple, preload, path);
-          append_preload_internal (argv,
+          append_preload_internal (context,
+                                   argv,
                                    which,
                                    i,
                                    path,
                                    path,
-                                   env,
                                    flags,
                                    runtime,
                                    exports);
@@ -794,10 +794,10 @@ append_preload_per_architecture (GPtrArray *argv,
 }
 
 static void
-append_preload_basename (GPtrArray *argv,
+append_preload_basename (PvWrapContext *context,
+                         GPtrArray *argv,
                          PvPreloadVariableIndex which,
                          const char *preload,
-                         const char * const *env,
                          PvAppendPreloadFlags flags,
                          PvRuntime *runtime,
                          FlatpakExports *exports)
@@ -832,12 +832,12 @@ append_preload_basename (GPtrArray *argv,
                "passing %s through as-is",
                preload,
                pv_preload_variables[which].variable);
-      append_preload_internal (argv,
+      append_preload_internal (context,
+                               argv,
                                which,
                                PV_UNSPECIFIED_ABI,
                                NULL,
                                preload,
-                               env,
                                flags,
                                runtime,
                                NULL);
@@ -851,10 +851,10 @@ append_preload_basename (GPtrArray *argv,
       g_debug ("Did not find \"%s\" in runtime or graphics stack provider, "
                "splitting architectures",
                preload);
-      append_preload_per_architecture (argv,
+      append_preload_per_architecture (context,
+                                       argv,
                                        which,
                                        preload,
-                                       env,
                                        flags,
                                        runtime,
                                        exports);
@@ -863,14 +863,13 @@ append_preload_basename (GPtrArray *argv,
 
 /**
  * pv_wrap_append_preload:
+ * @context: Context we are running in
  * @argv: (element-type filename): Array of command-line options to populate
  * @which: Either `LD_AUDIT` or `LD_PRELOAD`
  * @preload: (type filename): Path of preloadable module in current
  *  namespace, possibly including special ld.so tokens such as `$LIB`,
  *  or basename of a preloadable module to be found in the standard
  *  library search path
- * @env: (array zero-terminated=1) (element-type filename): Environment
- *  variables to be used instead of `environ`
  * @flags: Flags to adjust behaviour
  * @runtime: (nullable): Runtime to be used in container
  * @exports: (nullable): Used to configure extra paths that need to be
@@ -880,10 +879,10 @@ append_preload_basename (GPtrArray *argv,
  * to @argv.
  */
 void
-pv_wrap_append_preload (GPtrArray *argv,
+pv_wrap_append_preload (PvWrapContext *context,
+                        GPtrArray *argv,
                         PvPreloadVariableIndex which,
                         const char *preload,
-                        const char * const *env,
                         PvAppendPreloadFlags flags,
                         PvRuntime *runtime,
                         FlatpakExports *exports)
@@ -892,6 +891,7 @@ pv_wrap_append_preload (GPtrArray *argv,
   SrtLoadableFlags loadable_flags;
   const char *variable;
 
+  g_return_if_fail (PV_IS_WRAP_CONTEXT (context));
   g_return_if_fail (argv != NULL);
   g_return_if_fail (preload != NULL);
   g_return_if_fail (runtime == NULL || PV_IS_RUNTIME (runtime));
@@ -921,10 +921,10 @@ pv_wrap_append_preload (GPtrArray *argv,
       case SRT_LOADABLE_KIND_BASENAME:
         /* Basenames can't have dynamic string tokens. */
         g_warn_if_fail ((loadable_flags & SRT_LOADABLE_FLAGS_DYNAMIC_TOKENS) == 0);
-        append_preload_basename (argv,
+        append_preload_basename (context,
+                                 argv,
                                  which,
                                  preload,
-                                 env,
                                  flags,
                                  runtime,
                                  exports);
@@ -935,10 +935,10 @@ pv_wrap_append_preload (GPtrArray *argv,
         if (loadable_flags & (SRT_LOADABLE_FLAGS_ORIGIN
                               | SRT_LOADABLE_FLAGS_UNKNOWN_TOKENS))
           {
-            append_preload_unsupported_token (argv,
+            append_preload_unsupported_token (context,
+                                              argv,
                                               which,
                                               preload,
-                                              env,
                                               flags,
                                               runtime,
                                               exports);
@@ -947,10 +947,10 @@ pv_wrap_append_preload (GPtrArray *argv,
           {
             g_debug ("Found $LIB or $PLATFORM in \"%s\", splitting architectures",
                      preload);
-            append_preload_per_architecture (argv,
+            append_preload_per_architecture (context,
+                                             argv,
                                              which,
                                              preload,
-                                             env,
                                              flags,
                                              runtime,
                                              exports);
@@ -960,12 +960,12 @@ pv_wrap_append_preload (GPtrArray *argv,
             /* All dynamic tokens should be handled above, so we can
              * assume that preload is a concrete filename */
             g_warn_if_fail ((loadable_flags & SRT_LOADABLE_FLAGS_DYNAMIC_TOKENS) == 0);
-            append_preload_internal (argv,
+            append_preload_internal (context,
+                                     argv,
                                      which,
                                      PV_UNSPECIFIED_ABI,
                                      preload,
                                      preload,
-                                     env,
                                      flags,
                                      runtime,
                                      exports);
diff --git a/pressure-vessel/wrap-setup.h b/pressure-vessel/wrap-setup.h
index 97c193c66..7d7bd525c 100644
--- a/pressure-vessel/wrap-setup.h
+++ b/pressure-vessel/wrap-setup.h
@@ -84,10 +84,10 @@ typedef enum
   PV_APPEND_PRELOAD_FLAGS_NONE = 0
 } PvAppendPreloadFlags;
 
-void pv_wrap_append_preload (GPtrArray *argv,
+void pv_wrap_append_preload (PvWrapContext *context,
+                             GPtrArray *argv,
                              PvPreloadVariableIndex which,
                              const char *preload,
-                             const char * const *env,
                              PvAppendPreloadFlags flags,
                              PvRuntime *runtime,
                              FlatpakExports *exports);
diff --git a/pressure-vessel/wrap.c b/pressure-vessel/wrap.c
index 838ebb637..aec7f4412 100644
--- a/pressure-vessel/wrap.c
+++ b/pressure-vessel/wrap.c
@@ -842,10 +842,10 @@ main (int argc,
 
           g_assert (module->which >= 0);
           g_assert (module->which < G_N_ELEMENTS (pv_preload_variables));
-          pv_wrap_append_preload (adverb_preload_argv,
+          pv_wrap_append_preload (self,
+                                  adverb_preload_argv,
                                   module->which,
                                   module->preload,
-                                  _srt_const_strv (environ),
                                   append_preload_flags,
                                   runtime,
                                   exports);
diff --git a/tests/pressure-vessel/wrap-setup.c b/tests/pressure-vessel/wrap-setup.c
index 0cd08b609..325493a04 100644
--- a/tests/pressure-vessel/wrap-setup.c
+++ b/tests/pressure-vessel/wrap-setup.c
@@ -1612,10 +1612,10 @@ populate_ld_preload (Fixture *f,
                                  preloads[i].warning);
         }
 
-      pv_wrap_append_preload (argv,
+      pv_wrap_append_preload (f->context,
+                              argv,
                               PV_PRELOAD_VARIABLE_INDEX_LD_PRELOAD,
                               preloads[i].string,
-                              _srt_const_strv (f->context->original_environ),
                               flags | PV_APPEND_PRELOAD_FLAGS_IN_UNIT_TESTS,
                               runtime,
                               exports);
-- 
GitLab


From 6557a5e2790539bc120bea44de8c7b2eb2bd1c86 Mon Sep 17 00:00:00 2001
From: Simon McVittie <smcv@collabora.com>
Date: Fri, 7 Feb 2025 16:09:34 +0000
Subject: [PATCH 5/8] PvWrapContext: Store the PvRuntime in here

This lets us pass it to various functions as one parameter rather
than two.

Signed-off-by: Simon McVittie <smcv@collabora.com>
---
 pressure-vessel/wrap-context.c     |  1 +
 pressure-vessel/wrap-context.h     |  2 +
 pressure-vessel/wrap-setup.c       | 21 ++------
 pressure-vessel/wrap-setup.h       |  1 -
 pressure-vessel/wrap.c             | 58 ++++++++++-----------
 tests/pressure-vessel/wrap-setup.c | 82 +++++++++++++++---------------
 6 files changed, 76 insertions(+), 89 deletions(-)

diff --git a/pressure-vessel/wrap-context.c b/pressure-vessel/wrap-context.c
index 194f40994..abcab14a2 100644
--- a/pressure-vessel/wrap-context.c
+++ b/pressure-vessel/wrap-context.c
@@ -169,6 +169,7 @@ pv_wrap_context_dispose (GObject *object)
   PvWrapContext *self = PV_WRAP_CONTEXT (object);
 
   g_clear_object (&self->current_root);
+  g_clear_object (&self->runtime);
 
   G_OBJECT_CLASS (pv_wrap_context_parent_class)->dispose (object);
 }
diff --git a/pressure-vessel/wrap-context.h b/pressure-vessel/wrap-context.h
index a8d8ce1ed..4b2863352 100644
--- a/pressure-vessel/wrap-context.h
+++ b/pressure-vessel/wrap-context.h
@@ -13,6 +13,7 @@
 
 #include "pressure-vessel/adverb-preload.h"
 #include "pressure-vessel/flatpak-exports-private.h"
+#include "pressure-vessel/runtime.h"
 
 #include "pressure-vessel/wrap-interactive.h"
 
@@ -93,6 +94,7 @@ struct _PvWrapContext
   GObject parent_instance;
 
   GHashTable *paths_not_exported;
+  PvRuntime *runtime;
   SrtSysroot *current_root;
   gchar **original_argv;
   gchar **original_environ;
diff --git a/pressure-vessel/wrap-setup.c b/pressure-vessel/wrap-setup.c
index 8aafb6cc3..8e1f3da16 100644
--- a/pressure-vessel/wrap-setup.c
+++ b/pressure-vessel/wrap-setup.c
@@ -553,7 +553,6 @@ append_preload_internal (PvWrapContext *context,
                          const char *export_path,
                          const char *original_path,
                          PvAppendPreloadFlags flags,
-                         PvRuntime *runtime,
                          FlatpakExports *exports)
 {
   g_auto(PvAdverbPreloadModule) module = PV_ADVERB_PRELOAD_MODULE_INIT;
@@ -563,7 +562,7 @@ append_preload_internal (PvWrapContext *context,
   module.index_in_preload_variables = which;
   module.abi_index = abi_index;
 
-  if (runtime != NULL
+  if (context->runtime != NULL
       && (g_str_has_prefix (original_path, "/usr/")
           || g_str_has_prefix (original_path, "/lib")
           || (flatpak_subsandbox && g_str_has_prefix (original_path, "/app/"))))
@@ -625,7 +624,6 @@ append_preload_unsupported_token (PvWrapContext *context,
                                   PvPreloadVariableIndex which,
                                   const char *preload,
                                   PvAppendPreloadFlags flags,
-                                  PvRuntime *runtime,
                                   FlatpakExports *exports)
 {
   g_autofree gchar *export_path = NULL;
@@ -680,7 +678,6 @@ append_preload_unsupported_token (PvWrapContext *context,
                            export_path,
                            preload,
                            flags,
-                           runtime,
                            exports);
 }
 
@@ -700,7 +697,6 @@ append_preload_per_architecture (PvWrapContext *context,
                                  PvPreloadVariableIndex which,
                                  const char *preload,
                                  PvAppendPreloadFlags flags,
-                                 PvRuntime *runtime,
                                  FlatpakExports *exports)
 {
   g_autoptr(SrtSystemInfo) system_info = srt_system_info_new (NULL);
@@ -782,7 +778,6 @@ append_preload_per_architecture (PvWrapContext *context,
                                    path,
                                    path,
                                    flags,
-                                   runtime,
                                    exports);
         }
       else
@@ -799,13 +794,12 @@ append_preload_basename (PvWrapContext *context,
                          PvPreloadVariableIndex which,
                          const char *preload,
                          PvAppendPreloadFlags flags,
-                         PvRuntime *runtime,
                          FlatpakExports *exports)
 {
   gboolean runtime_has_library = FALSE;
 
-  if (runtime != NULL)
-    runtime_has_library = pv_runtime_has_library (runtime, preload);
+  if (context->runtime != NULL)
+    runtime_has_library = pv_runtime_has_library (context->runtime, preload);
 
   if (flags & PV_APPEND_PRELOAD_FLAGS_IN_UNIT_TESTS)
     {
@@ -839,7 +833,6 @@ append_preload_basename (PvWrapContext *context,
                                NULL,
                                preload,
                                flags,
-                               runtime,
                                NULL);
     }
   else
@@ -856,7 +849,6 @@ append_preload_basename (PvWrapContext *context,
                                        which,
                                        preload,
                                        flags,
-                                       runtime,
                                        exports);
     }
 }
@@ -871,7 +863,6 @@ append_preload_basename (PvWrapContext *context,
  *  or basename of a preloadable module to be found in the standard
  *  library search path
  * @flags: Flags to adjust behaviour
- * @runtime: (nullable): Runtime to be used in container
  * @exports: (nullable): Used to configure extra paths that need to be
  *  exported into the container
  *
@@ -884,7 +875,6 @@ pv_wrap_append_preload (PvWrapContext *context,
                         PvPreloadVariableIndex which,
                         const char *preload,
                         PvAppendPreloadFlags flags,
-                        PvRuntime *runtime,
                         FlatpakExports *exports)
 {
   SrtLoadableKind kind;
@@ -894,7 +884,6 @@ pv_wrap_append_preload (PvWrapContext *context,
   g_return_if_fail (PV_IS_WRAP_CONTEXT (context));
   g_return_if_fail (argv != NULL);
   g_return_if_fail (preload != NULL);
-  g_return_if_fail (runtime == NULL || PV_IS_RUNTIME (runtime));
   g_return_if_fail (which >= 0);
   g_return_if_fail (which < G_N_ELEMENTS (pv_preload_variables));
 
@@ -926,7 +915,6 @@ pv_wrap_append_preload (PvWrapContext *context,
                                  which,
                                  preload,
                                  flags,
-                                 runtime,
                                  exports);
         break;
 
@@ -940,7 +928,6 @@ pv_wrap_append_preload (PvWrapContext *context,
                                               which,
                                               preload,
                                               flags,
-                                              runtime,
                                               exports);
           }
         else if (loadable_flags & SRT_LOADABLE_FLAGS_ABI_DEPENDENT)
@@ -952,7 +939,6 @@ pv_wrap_append_preload (PvWrapContext *context,
                                              which,
                                              preload,
                                              flags,
-                                             runtime,
                                              exports);
           }
         else
@@ -967,7 +953,6 @@ pv_wrap_append_preload (PvWrapContext *context,
                                      preload,
                                      preload,
                                      flags,
-                                     runtime,
                                      exports);
           }
         break;
diff --git a/pressure-vessel/wrap-setup.h b/pressure-vessel/wrap-setup.h
index 7d7bd525c..f89c1f5d9 100644
--- a/pressure-vessel/wrap-setup.h
+++ b/pressure-vessel/wrap-setup.h
@@ -89,7 +89,6 @@ void pv_wrap_append_preload (PvWrapContext *context,
                              PvPreloadVariableIndex which,
                              const char *preload,
                              PvAppendPreloadFlags flags,
-                             PvRuntime *runtime,
                              FlatpakExports *exports);
 
 gboolean pv_wrap_maybe_load_nvidia_modules (GError **error);
diff --git a/pressure-vessel/wrap.c b/pressure-vessel/wrap.c
index aec7f4412..de465d657 100644
--- a/pressure-vessel/wrap.c
+++ b/pressure-vessel/wrap.c
@@ -97,7 +97,6 @@ main (int argc,
   g_autofree gchar *private_home = NULL;
   const gchar *home;
   g_autofree gchar *tools_dir = NULL;
-  g_autoptr(PvRuntime) runtime = NULL;
   glnx_autofd int original_stdout = -1;
   glnx_autofd int original_stderr = -1;
   const char *graphics_provider_mount_point = NULL;
@@ -678,20 +677,20 @@ main (int argc,
           goto out;
         }
 
-      runtime = pv_runtime_new (runtime_path,
-                                self->options.variable_dir,
-                                bwrap_executable,
-                                graphics_provider,
-                                interpreter_host_provider,
-                                _srt_const_strv (self->original_environ),
-                                flags,
-                                workarounds,
-                                error);
-
-      if (runtime == NULL)
+      self->runtime = pv_runtime_new (runtime_path,
+                                      self->options.variable_dir,
+                                      bwrap_executable,
+                                      graphics_provider,
+                                      interpreter_host_provider,
+                                      _srt_const_strv (self->original_environ),
+                                      flags,
+                                      workarounds,
+                                      error);
+
+      if (self->runtime == NULL)
         goto out;
 
-      if (!pv_runtime_bind (runtime,
+      if (!pv_runtime_bind (self->runtime,
                             exports,
                             bwrap_filesystem_arguments,
                             container_env,
@@ -700,8 +699,8 @@ main (int argc,
 
       if (flatpak_subsandbox != NULL)
         {
-          const char *app = pv_runtime_get_modified_app (runtime);
-          const char *usr = pv_runtime_get_modified_usr (runtime);
+          const char *app = pv_runtime_get_modified_app (self->runtime);
+          const char *usr = pv_runtime_get_modified_usr (self->runtime);
 
           flatpak_bwrap_add_args (flatpak_subsandbox,
                                   "--app-path", app == NULL ? "" : app,
@@ -847,7 +846,6 @@ main (int argc,
                                   module->which,
                                   module->preload,
                                   append_preload_flags,
-                                  runtime,
                                   exports);
         }
     }
@@ -867,14 +865,14 @@ main (int argc,
        * udev directly. We only do that when the host's version of libudev.so.1
        * is in use, because there is no guarantees that the container's libudev
        * is compatible with the host's udevd. */
-      if (runtime != NULL)
+      if (self->runtime != NULL)
         {
           for (i = 0; i < PV_N_SUPPORTED_ARCHITECTURES; i++)
             {
               GStatBuf ignored;
               g_autofree gchar *override = NULL;
 
-              override = g_build_filename (pv_runtime_get_overrides (runtime),
+              override = g_build_filename (pv_runtime_get_overrides (self->runtime),
                                            "lib", pv_multiarch_tuples[i],
                                            "libudev.so.1", NULL);
 
@@ -960,7 +958,7 @@ main (int argc,
 
   /* Put Steam Runtime environment variables back, if /usr is mounted
    * from the host. */
-  if (runtime == NULL)
+  if (self->runtime == NULL)
     {
       g_debug ("Making Steam Runtime available...");
 
@@ -1034,7 +1032,7 @@ main (int argc,
 
       sharing_bwrap = pv_wrap_share_sockets (container_env,
                                              _srt_const_strv (self->original_environ),
-                                             (runtime != NULL),
+                                             (self->runtime != NULL),
                                              self->is_flatpak_env);
       g_warn_if_fail (g_strv_length (sharing_bwrap->envp) == 0);
 
@@ -1048,9 +1046,9 @@ main (int argc,
       pv_wrap_set_icons_env_vars (container_env, _srt_const_strv (self->original_environ));
     }
 
-  if (runtime != NULL)
+  if (self->runtime != NULL)
     {
-      if (!pv_runtime_use_shared_sockets (runtime, bwrap, container_env,
+      if (!pv_runtime_use_shared_sockets (self->runtime, bwrap, container_env,
                                           error))
         goto out;
     }
@@ -1142,11 +1140,11 @@ main (int argc,
 
       adverb_argv = flatpak_bwrap_new (flatpak_bwrap_empty_env);
 
-      if (runtime != NULL)
+      if (self->runtime != NULL)
         {
           /* This includes the arguments necessary to regenerate the
            * ld.so cache */
-          if (!pv_runtime_get_adverb (runtime, adverb_argv, error))
+          if (!pv_runtime_get_adverb (self->runtime, adverb_argv, error))
             goto out;
         }
       else
@@ -1337,11 +1335,11 @@ main (int argc,
 
   if (_srt_util_is_debugging ())
     {
-      if (runtime != NULL && (pv_log_flags & PV_WRAP_LOG_FLAGS_OVERRIDES))
-        pv_runtime_log_overrides (runtime);
+      if (self->runtime != NULL && (pv_log_flags & PV_WRAP_LOG_FLAGS_OVERRIDES))
+        pv_runtime_log_overrides (self->runtime);
 
-      if (runtime != NULL && (pv_log_flags & PV_WRAP_LOG_FLAGS_CONTAINER))
-        pv_runtime_log_container (runtime);
+      if (self->runtime != NULL && (pv_log_flags & PV_WRAP_LOG_FLAGS_CONTAINER))
+        pv_runtime_log_container (self->runtime);
 
       g_debug ("Final command to execute:");
 
@@ -1365,8 +1363,8 @@ main (int argc,
     }
 
   /* Clean up temporary directory before running our long-running process */
-  if (runtime != NULL)
-    pv_runtime_cleanup (runtime);
+  if (self->runtime != NULL)
+    pv_runtime_cleanup (self->runtime);
 
   flatpak_bwrap_finish (final_argv);
 
diff --git a/tests/pressure-vessel/wrap-setup.c b/tests/pressure-vessel/wrap-setup.c
index 325493a04..88ec05bbe 100644
--- a/tests/pressure-vessel/wrap-setup.c
+++ b/tests/pressure-vessel/wrap-setup.c
@@ -181,15 +181,16 @@ fixture_create_exports (Fixture *f)
   return g_steal_pointer (&exports);
 }
 
-static PvRuntime *
+static void
 fixture_create_runtime (Fixture *f,
                         PvRuntimeFlags flags)
 {
   g_autoptr(GError) local_error = NULL;
   g_autoptr(PvGraphicsProvider) graphics_provider = NULL;
-  g_autoptr(PvRuntime) runtime = NULL;
   const char *gfx_in_container;
 
+  g_assert_null (f->context->runtime);
+
   if (flags & PV_RUNTIME_FLAGS_FLATPAK_SUBSANDBOX)
     gfx_in_container = "/run/parent";
   else
@@ -200,20 +201,19 @@ fixture_create_runtime (Fixture *f,
   g_assert_no_error (local_error);
   g_assert_nonnull (graphics_provider);
 
-  runtime = pv_runtime_new (f->mock_runtime,
-                            f->var,
-                            NULL,
-                            graphics_provider,
-                            NULL,
-                            _srt_peek_environ_nonnull (),
-                            (flags
-                             | PV_RUNTIME_FLAGS_VERBOSE
-                             | PV_RUNTIME_FLAGS_SINGLE_THREAD),
-                            PV_WORKAROUND_FLAGS_NONE,
-                            &local_error);
+  f->context->runtime = pv_runtime_new (f->mock_runtime,
+                                        f->var,
+                                        NULL,
+                                        graphics_provider,
+                                        NULL,
+                                        _srt_peek_environ_nonnull (),
+                                        (flags
+                                         | PV_RUNTIME_FLAGS_VERBOSE
+                                         | PV_RUNTIME_FLAGS_SINGLE_THREAD),
+                                        PV_WORKAROUND_FLAGS_NONE,
+                                        &local_error);
   g_assert_no_error (local_error);
-  g_assert_nonnull (runtime);
-  return g_steal_pointer (&runtime);
+  g_assert_nonnull (f->context->runtime);
 }
 
 static void
@@ -377,15 +377,15 @@ test_bind_into_container (Fixture *f,
                           gconstpointer context)
 {
   const Config *config = context;
-  g_autoptr(PvRuntime) runtime = NULL;
   g_autoptr(GError) error = NULL;
   gboolean ok;
 
-  runtime = fixture_create_runtime (f, config->runtime_flags);
+  fixture_create_runtime (f, config->runtime_flags);
+  g_assert_nonnull (f->context->runtime);
 
   /* Successful cases */
 
-  ok = pv_runtime_bind_into_container (runtime, f->bwrap,
+  ok = pv_runtime_bind_into_container (f->context->runtime, f->bwrap,
                                        "/etc/machine-id", NULL, 0,
                                        "/etc/machine-id",
                                        PV_RUNTIME_EMULATION_ROOTS_BOTH,
@@ -393,7 +393,7 @@ test_bind_into_container (Fixture *f,
   g_assert_no_error (error);
   g_assert_true (ok);
 
-  ok = pv_runtime_bind_into_container (runtime, f->bwrap,
+  ok = pv_runtime_bind_into_container (f->context->runtime, f->bwrap,
                                        "/etc/arm-file", NULL, 0,
                                        "/etc/arm-file",
                                        PV_RUNTIME_EMULATION_ROOTS_REAL_ONLY,
@@ -401,7 +401,7 @@ test_bind_into_container (Fixture *f,
   g_assert_no_error (error);
   g_assert_true (ok);
 
-  ok = pv_runtime_bind_into_container (runtime, f->bwrap,
+  ok = pv_runtime_bind_into_container (f->context->runtime, f->bwrap,
                                        "/fex/etc/x86-file", NULL, 0,
                                        "/etc/x86-file",
                                        PV_RUNTIME_EMULATION_ROOTS_INTERPRETER_ONLY,
@@ -411,7 +411,7 @@ test_bind_into_container (Fixture *f,
 
   /* Error cases */
 
-  ok = pv_runtime_bind_into_container (runtime, f->bwrap,
+  ok = pv_runtime_bind_into_container (f->context->runtime, f->bwrap,
                                        "/nope", NULL, 0,
                                        "/nope",
                                        PV_RUNTIME_EMULATION_ROOTS_REAL_ONLY,
@@ -421,7 +421,7 @@ test_bind_into_container (Fixture *f,
   g_test_message ("Editing /nope not allowed, as expected: %s", error->message);
   g_clear_error (&error);
 
-  ok = pv_runtime_bind_into_container (runtime, f->bwrap,
+  ok = pv_runtime_bind_into_container (f->context->runtime, f->bwrap,
                                        "/usr/foo", NULL, 0,
                                        "/usr/foo",
                                        PV_RUNTIME_EMULATION_ROOTS_BOTH,
@@ -1022,12 +1022,12 @@ test_make_symlink_in_container (Fixture *f,
 {
   const Config *config = context;
   g_autoptr(GError) error = NULL;
-  g_autoptr(PvRuntime) runtime = NULL;
   gboolean ok;
   SrtSysroot *mutable_sysroot;
 
-  runtime = fixture_create_runtime (f, config->runtime_flags);
-  mutable_sysroot = pv_runtime_get_mutable_sysroot (runtime);
+  fixture_create_runtime (f, config->runtime_flags);
+  g_assert_nonnull (f->context->runtime);
+  mutable_sysroot = pv_runtime_get_mutable_sysroot (f->context->runtime);
 
   if (config->runtime_flags & PV_RUNTIME_FLAGS_COPY_RUNTIME)
     g_assert_nonnull (mutable_sysroot);
@@ -1036,7 +1036,7 @@ test_make_symlink_in_container (Fixture *f,
 
   /* Successful cases */
 
-  ok = pv_runtime_make_symlink_in_container (runtime, f->bwrap,
+  ok = pv_runtime_make_symlink_in_container (f->context->runtime, f->bwrap,
                                              "../usr/lib/os-release",
                                              "/etc/os-release",
                                              PV_RUNTIME_EMULATION_ROOTS_BOTH,
@@ -1044,7 +1044,7 @@ test_make_symlink_in_container (Fixture *f,
   g_assert_no_error (error);
   g_assert_true (ok);
 
-  ok = pv_runtime_make_symlink_in_container (runtime, f->bwrap,
+  ok = pv_runtime_make_symlink_in_container (f->context->runtime, f->bwrap,
                                              "/run/host/foo",
                                              "/var/foo",
                                              PV_RUNTIME_EMULATION_ROOTS_REAL_ONLY,
@@ -1052,7 +1052,7 @@ test_make_symlink_in_container (Fixture *f,
   g_assert_no_error (error);
   g_assert_true (ok);
 
-  ok = pv_runtime_make_symlink_in_container (runtime, f->bwrap,
+  ok = pv_runtime_make_symlink_in_container (f->context->runtime, f->bwrap,
                                              "/run/x86/bar",
                                              "/var/bar",
                                              PV_RUNTIME_EMULATION_ROOTS_INTERPRETER_ONLY,
@@ -1062,7 +1062,7 @@ test_make_symlink_in_container (Fixture *f,
 
   /* Conditionally OK, if there is an on-disk directory we can edit */
 
-  ok = pv_runtime_make_symlink_in_container (runtime, f->bwrap,
+  ok = pv_runtime_make_symlink_in_container (f->context->runtime, f->bwrap,
                                              "/run/host/foo",
                                              "/usr/foo",
                                              PV_RUNTIME_EMULATION_ROOTS_REAL_ONLY,
@@ -1091,7 +1091,7 @@ test_make_symlink_in_container (Fixture *f,
 
     }
 
-  ok = pv_runtime_make_symlink_in_container (runtime, f->bwrap,
+  ok = pv_runtime_make_symlink_in_container (f->context->runtime, f->bwrap,
                                              "/run/x86/bar",
                                              "/usr/bar",
                                              PV_RUNTIME_EMULATION_ROOTS_INTERPRETER_ONLY,
@@ -1111,7 +1111,7 @@ test_make_symlink_in_container (Fixture *f,
       g_assert_true (ok);
     }
 
-  ok = pv_runtime_make_symlink_in_container (runtime, f->bwrap,
+  ok = pv_runtime_make_symlink_in_container (f->context->runtime, f->bwrap,
                                              "/run/baz",
                                              "/usr/baz",
                                              PV_RUNTIME_EMULATION_ROOTS_BOTH,
@@ -1141,7 +1141,7 @@ test_make_symlink_in_container (Fixture *f,
 
   /* Error cases */
 
-  ok = pv_runtime_make_symlink_in_container (runtime, f->bwrap,
+  ok = pv_runtime_make_symlink_in_container (f->context->runtime, f->bwrap,
                                              "/nope",
                                              "/nope",
                                              PV_RUNTIME_EMULATION_ROOTS_REAL_ONLY,
@@ -1556,7 +1556,6 @@ static void
 populate_ld_preload (Fixture *f,
                      GPtrArray *argv,
                      PvAppendPreloadFlags flags,
-                     PvRuntime *runtime,
                      FlatpakExports *exports)
 {
   static const struct
@@ -1617,7 +1616,6 @@ populate_ld_preload (Fixture *f,
                               PV_PRELOAD_VARIABLE_INDEX_LD_PRELOAD,
                               preloads[i].string,
                               flags | PV_APPEND_PRELOAD_FLAGS_IN_UNIT_TESTS,
-                              runtime,
                               exports);
 
       /* If we modified the fatal mask, put back the old value. */
@@ -1698,7 +1696,6 @@ test_remap_ld_preload (Fixture *f,
   g_autoptr(FlatpakExports) exports = fixture_create_exports (f);
   g_autoptr(GPtrArray) argv = g_ptr_array_new_with_free_func (g_free);
   g_autoptr(GPtrArray) filtered = filter_expected_paths (config);
-  g_autoptr(PvRuntime) runtime = fixture_create_runtime (f, PV_RUNTIME_FLAGS_NONE);
   gsize i;
   gboolean expect_i386 = FALSE;
 
@@ -1707,7 +1704,9 @@ test_remap_ld_preload (Fixture *f,
     expect_i386 = TRUE;
 #endif
 
-  populate_ld_preload (f, argv, config->preload_flags, runtime, exports);
+  fixture_create_runtime (f, PV_RUNTIME_FLAGS_NONE);
+  g_assert_nonnull (f->context->runtime);
+  populate_ld_preload (f, argv, config->preload_flags, exports);
 
   g_assert_cmpuint (argv->len, ==, filtered->len);
 
@@ -1787,13 +1786,14 @@ test_remap_ld_preload_flatpak (Fixture *f,
   const Config *config = context;
   g_autoptr(GPtrArray) argv = g_ptr_array_new_with_free_func (g_free);
   g_autoptr(GPtrArray) filtered = filter_expected_paths (config);
-  g_autoptr(PvRuntime) runtime = fixture_create_runtime (f, PV_RUNTIME_FLAGS_FLATPAK_SUBSANDBOX);
   gsize i;
 
+  fixture_create_runtime (f, PV_RUNTIME_FLAGS_FLATPAK_SUBSANDBOX);
+  g_assert_nonnull (f->context->runtime);
   populate_ld_preload (f, argv,
                        (config->preload_flags
                         | PV_APPEND_PRELOAD_FLAGS_FLATPAK_SUBSANDBOX),
-                       runtime, NULL);
+                       NULL);
 
   g_assert_cmpuint (argv->len, ==, filtered->len);
 
@@ -1838,10 +1838,11 @@ test_remap_ld_preload_no_runtime (Fixture *f,
     expect_i386 = TRUE;
 #endif
 
+  g_assert_null (f->context->runtime);
   populate_ld_preload (f, argv,
                        (config->preload_flags
                         | PV_APPEND_PRELOAD_FLAGS_REMOVE_GAME_OVERLAY),
-                       NULL, exports);
+                       exports);
 
   g_assert_cmpuint (argv->len, ==, filtered->len - 1);
 
@@ -1921,10 +1922,11 @@ test_remap_ld_preload_flatpak_no_runtime (Fixture *f,
   g_autoptr(GPtrArray) filtered = filter_expected_paths (config);
   gsize i;
 
+  g_assert_null (f->context->runtime);
   populate_ld_preload (f, argv,
                        (config->preload_flags
                         | PV_APPEND_PRELOAD_FLAGS_FLATPAK_SUBSANDBOX),
-                       NULL, NULL);
+                       NULL);
 
   g_assert_cmpuint (argv->len, ==, filtered->len);
 
-- 
GitLab


From 0755e37d7b4de9919c55cdff1d638d7992825b0d Mon Sep 17 00:00:00 2001
From: Simon McVittie <smcv@collabora.com>
Date: Fri, 7 Feb 2025 16:16:05 +0000
Subject: [PATCH 6/8] pv-wrap: Take "remove game overlay" option from
 PvWrapContext

It's slightly simpler without duplicating 1 bit of information like this.

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

diff --git a/pressure-vessel/wrap-setup.c b/pressure-vessel/wrap-setup.c
index 8e1f3da16..d7f481941 100644
--- a/pressure-vessel/wrap-setup.c
+++ b/pressure-vessel/wrap-setup.c
@@ -896,7 +896,7 @@ pv_wrap_append_preload (PvWrapContext *context,
       return;
     }
 
-  if ((flags & PV_APPEND_PRELOAD_FLAGS_REMOVE_GAME_OVERLAY)
+  if (context->options.remove_game_overlay
       && g_str_has_suffix (preload, "/gameoverlayrenderer.so"))
     {
       g_info ("Disabling Steam Overlay: %s", preload);
diff --git a/pressure-vessel/wrap-setup.h b/pressure-vessel/wrap-setup.h
index f89c1f5d9..c96393e80 100644
--- a/pressure-vessel/wrap-setup.h
+++ b/pressure-vessel/wrap-setup.h
@@ -66,7 +66,6 @@ void pv_wrap_move_into_scope (const char *steam_app_id);
  * PvAppendPreloadFlags:
  * @PV_APPEND_PRELOAD_FLAGS_FLATPAK_SUBSANDBOX: The game will be run in
  *  a Flatpak subsandbox
- * @PV_APPEND_PRELOAD_FLAGS_REMOVE_GAME_OVERLAY: Disable the Steam Overlay
  * @PV_APPEND_PRELOAD_FLAGS_IN_UNIT_TESTS: Normalize $LIB and $PLATFORM,
  *  for unit testing
  * @PV_APPEND_PRELOAD_FLAGS_ONE_ARCHITECTURE: Behave as though there is
@@ -78,7 +77,6 @@ void pv_wrap_move_into_scope (const char *steam_app_id);
 typedef enum
 {
   PV_APPEND_PRELOAD_FLAGS_FLATPAK_SUBSANDBOX = (1 << 0),
-  PV_APPEND_PRELOAD_FLAGS_REMOVE_GAME_OVERLAY = (1 << 1),
   PV_APPEND_PRELOAD_FLAGS_IN_UNIT_TESTS = (1 << 2),
   PV_APPEND_PRELOAD_FLAGS_ONE_ARCHITECTURE = (1 << 3),
   PV_APPEND_PRELOAD_FLAGS_NONE = 0
diff --git a/pressure-vessel/wrap.c b/pressure-vessel/wrap.c
index de465d657..0e20c0997 100644
--- a/pressure-vessel/wrap.c
+++ b/pressure-vessel/wrap.c
@@ -820,9 +820,6 @@ main (int argc,
 
   adverb_preload_argv = g_ptr_array_new_with_free_func (g_free);
 
-  if (self->options.remove_game_overlay)
-    append_preload_flags |= PV_APPEND_PRELOAD_FLAGS_REMOVE_GAME_OVERLAY;
-
   /* We need the LD_PRELOADs from Steam visible at the paths that were
    * used for them, which might be their physical rather than logical
    * locations. Steam doesn't generally use LD_AUDIT, but the Steam app
diff --git a/tests/pressure-vessel/wrap-setup.c b/tests/pressure-vessel/wrap-setup.c
index 88ec05bbe..af7762be6 100644
--- a/tests/pressure-vessel/wrap-setup.c
+++ b/tests/pressure-vessel/wrap-setup.c
@@ -1819,8 +1819,7 @@ test_remap_ld_preload_flatpak (Fixture *f,
 
 /*
  * In addition to testing the rare case where there's no runtime,
- * this one also exercises PV_APPEND_PRELOAD_FLAGS_REMOVE_GAME_OVERLAY,
- * which is the implementation of --remove-game-overlay.
+ * this one also exercises --remove-game-overlay.
  */
 static void
 test_remap_ld_preload_no_runtime (Fixture *f,
@@ -1833,16 +1832,15 @@ test_remap_ld_preload_no_runtime (Fixture *f,
   gsize i, j;
   gboolean expect_i386 = FALSE;
 
+  f->context->options.remove_game_overlay = TRUE;
+
 #if defined(__i386__) || defined(__x86_64__)
   if (!(config->preload_flags & PV_APPEND_PRELOAD_FLAGS_ONE_ARCHITECTURE))
     expect_i386 = TRUE;
 #endif
 
   g_assert_null (f->context->runtime);
-  populate_ld_preload (f, argv,
-                       (config->preload_flags
-                        | PV_APPEND_PRELOAD_FLAGS_REMOVE_GAME_OVERLAY),
-                       exports);
+  populate_ld_preload (f, argv, config->preload_flags, exports);
 
   g_assert_cmpuint (argv->len, ==, filtered->len - 1);
 
@@ -1855,7 +1853,7 @@ test_remap_ld_preload_no_runtime (Fixture *f,
       argument += strlen("--ld-preload=");
 
       /* /steam/lib/gameoverlayrenderer.so is missing because we used the
-       * REMOVE_GAME_OVERLAY flag */
+       * equivalent of --remove-game-overlay */
       if (g_str_has_suffix (expected, "/gameoverlayrenderer.so"))
         {
           /* We expect to skip only one element */
-- 
GitLab


From 1bbff5faaedf067fde8de427ad64daca3484d421 Mon Sep 17 00:00:00 2001
From: Simon McVittie <smcv@collabora.com>
Date: Fri, 7 Feb 2025 16:33:27 +0000
Subject: [PATCH 7/8] pv-wrap: Store FlatpakExports in the PvWrapContext

This means we don't have to thread it through so much of wrap-setup as
an explicit function parameter.

Signed-off-by: Simon McVittie <smcv@collabora.com>
---
 pressure-vessel/wrap-context.c     |  7 ++-
 pressure-vessel/wrap-context.h     |  2 +-
 pressure-vessel/wrap-setup.c       | 59 ++++++++----------------
 pressure-vessel/wrap-setup.h       |  4 +-
 pressure-vessel/wrap.c             | 48 ++++++++++----------
 tests/pressure-vessel/wrap-setup.c | 72 +++++++++++++++++++-----------
 6 files changed, 93 insertions(+), 99 deletions(-)

diff --git a/pressure-vessel/wrap-context.c b/pressure-vessel/wrap-context.c
index abcab14a2..9cc0db7b9 100644
--- a/pressure-vessel/wrap-context.c
+++ b/pressure-vessel/wrap-context.c
@@ -181,6 +181,7 @@ pv_wrap_context_finalize (GObject *object)
 
   pv_wrap_options_clear (&self->options);
 
+  g_clear_pointer (&self->exports, flatpak_exports_free);
   g_clear_pointer (&self->paths_not_exported, g_hash_table_unref);
   g_strfreev (self->original_argv);
   g_strfreev (self->original_environ);
@@ -1103,7 +1104,6 @@ export_not_allowed (PvWrapContext *self,
 /*
  * pv_wrap_context_export_if_allowed:
  * @self: The context
- * @exports: List of exported paths
  * @export_mode: Mode with which to add @path
  * @path: The path we propose to export, as an absolute path within the
  *  current execution environment
@@ -1122,7 +1122,6 @@ export_not_allowed (PvWrapContext *self,
  */
 gboolean
 pv_wrap_context_export_if_allowed (PvWrapContext *self,
-                                   FlatpakExports *exports,
                                    FlatpakFilesystemMode export_mode,
                                    const char *path,
                                    const char *host_path,
@@ -1135,7 +1134,7 @@ pv_wrap_context_export_if_allowed (PvWrapContext *self,
   size_t i;
 
   g_return_val_if_fail (PV_IS_WRAP_CONTEXT (self), FALSE);
-  g_return_val_if_fail (exports != NULL, FALSE);
+  g_return_val_if_fail (self->exports != NULL, FALSE);
   g_return_val_if_fail (export_mode > FLATPAK_FILESYSTEM_MODE_NONE, FALSE);
   g_return_val_if_fail (export_mode <= FLATPAK_FILESYSTEM_MODE_LAST, FALSE);
   g_return_val_if_fail (g_path_is_absolute (path), FALSE);
@@ -1176,7 +1175,7 @@ pv_wrap_context_export_if_allowed (PvWrapContext *self,
 
   /* This generally shouldn't fail in practice, because we already checked
    * against reserved_paths[] above */
-  pv_exports_expose_or_log (exports, export_mode, path);
+  pv_exports_expose_or_log (self->exports, export_mode, path);
 
   return TRUE;
 }
diff --git a/pressure-vessel/wrap-context.h b/pressure-vessel/wrap-context.h
index 4b2863352..d98ece381 100644
--- a/pressure-vessel/wrap-context.h
+++ b/pressure-vessel/wrap-context.h
@@ -93,6 +93,7 @@ struct _PvWrapContext
 {
   GObject parent_instance;
 
+  FlatpakExports *exports;
   GHashTable *paths_not_exported;
   PvRuntime *runtime;
   SrtSysroot *current_root;
@@ -142,7 +143,6 @@ gboolean pv_wrap_options_parse_environment_after_argv (PvWrapOptions *self,
                                                        GError **error);
 
 gboolean pv_wrap_context_export_if_allowed (PvWrapContext *self,
-                                            FlatpakExports *exports,
                                             FlatpakFilesystemMode export_mode,
                                             const char *path,
                                             const char *host_path,
diff --git a/pressure-vessel/wrap-setup.c b/pressure-vessel/wrap-setup.c
index d7f481941..08c710134 100644
--- a/pressure-vessel/wrap-setup.c
+++ b/pressure-vessel/wrap-setup.c
@@ -552,8 +552,7 @@ append_preload_internal (PvWrapContext *context,
                          gsize abi_index,
                          const char *export_path,
                          const char *original_path,
-                         PvAppendPreloadFlags flags,
-                         FlatpakExports *exports)
+                         PvAppendPreloadFlags flags)
 {
   g_auto(PvAdverbPreloadModule) module = PV_ADVERB_PRELOAD_MODULE_INIT;
   g_autofree gchar *arg = NULL;
@@ -577,7 +576,7 @@ append_preload_internal (PvWrapContext *context,
       module.name = g_strdup (original_path);
       g_debug ("%s -> unmodified", original_path);
 
-      if (exports != NULL && export_path != NULL && export_path[0] == '/')
+      if (context->exports != NULL && export_path != NULL && export_path[0] == '/')
         {
           const gchar *steam_path = g_environ_getenv (context->original_environ, "STEAM_COMPAT_CLIENT_INSTALL_PATH");
 
@@ -591,7 +590,7 @@ append_preload_internal (PvWrapContext *context,
           else
             {
               g_debug ("%s needs adding to exports", export_path);
-              pv_exports_expose_or_log (exports,
+              pv_exports_expose_or_log (context->exports,
                                         FLATPAK_FILESYSTEM_MODE_READ_ONLY,
                                         export_path);
             }
@@ -623,8 +622,7 @@ append_preload_unsupported_token (PvWrapContext *context,
                                   GPtrArray *argv,
                                   PvPreloadVariableIndex which,
                                   const char *preload,
-                                  PvAppendPreloadFlags flags,
-                                  FlatpakExports *exports)
+                                  PvAppendPreloadFlags flags)
 {
   g_autofree gchar *export_path = NULL;
   char *dollar;
@@ -677,8 +675,7 @@ append_preload_unsupported_token (PvWrapContext *context,
                            PV_UNSPECIFIED_ABI,
                            export_path,
                            preload,
-                           flags,
-                           exports);
+                           flags);
 }
 
 /*
@@ -696,8 +693,7 @@ append_preload_per_architecture (PvWrapContext *context,
                                  GPtrArray *argv,
                                  PvPreloadVariableIndex which,
                                  const char *preload,
-                                 PvAppendPreloadFlags flags,
-                                 FlatpakExports *exports)
+                                 PvAppendPreloadFlags flags)
 {
   g_autoptr(SrtSystemInfo) system_info = srt_system_info_new (NULL);
   gsize n_supported_architectures = PV_N_SUPPORTED_ARCHITECTURES;
@@ -777,8 +773,7 @@ append_preload_per_architecture (PvWrapContext *context,
                                    i,
                                    path,
                                    path,
-                                   flags,
-                                   exports);
+                                   flags);
         }
       else
         {
@@ -793,8 +788,7 @@ append_preload_basename (PvWrapContext *context,
                          GPtrArray *argv,
                          PvPreloadVariableIndex which,
                          const char *preload,
-                         PvAppendPreloadFlags flags,
-                         FlatpakExports *exports)
+                         PvAppendPreloadFlags flags)
 {
   gboolean runtime_has_library = FALSE;
 
@@ -832,8 +826,7 @@ append_preload_basename (PvWrapContext *context,
                                PV_UNSPECIFIED_ABI,
                                NULL,
                                preload,
-                               flags,
-                               NULL);
+                               flags);
     }
   else
     {
@@ -848,8 +841,7 @@ append_preload_basename (PvWrapContext *context,
                                        argv,
                                        which,
                                        preload,
-                                       flags,
-                                       exports);
+                                       flags);
     }
 }
 
@@ -863,8 +855,6 @@ append_preload_basename (PvWrapContext *context,
  *  or basename of a preloadable module to be found in the standard
  *  library search path
  * @flags: Flags to adjust behaviour
- * @exports: (nullable): Used to configure extra paths that need to be
- *  exported into the container
  *
  * Adjust @preload to be valid for the container and append it
  * to @argv.
@@ -874,8 +864,7 @@ pv_wrap_append_preload (PvWrapContext *context,
                         GPtrArray *argv,
                         PvPreloadVariableIndex which,
                         const char *preload,
-                        PvAppendPreloadFlags flags,
-                        FlatpakExports *exports)
+                        PvAppendPreloadFlags flags)
 {
   SrtLoadableKind kind;
   SrtLoadableFlags loadable_flags;
@@ -914,8 +903,7 @@ pv_wrap_append_preload (PvWrapContext *context,
                                  argv,
                                  which,
                                  preload,
-                                 flags,
-                                 exports);
+                                 flags);
         break;
 
       case SRT_LOADABLE_KIND_PATH:
@@ -927,8 +915,7 @@ pv_wrap_append_preload (PvWrapContext *context,
                                               argv,
                                               which,
                                               preload,
-                                              flags,
-                                              exports);
+                                              flags);
           }
         else if (loadable_flags & SRT_LOADABLE_FLAGS_ABI_DEPENDENT)
           {
@@ -938,8 +925,7 @@ pv_wrap_append_preload (PvWrapContext *context,
                                              argv,
                                              which,
                                              preload,
-                                             flags,
-                                             exports);
+                                             flags);
           }
         else
           {
@@ -952,8 +938,7 @@ pv_wrap_append_preload (PvWrapContext *context,
                                      PV_UNSPECIFIED_ABI,
                                      preload,
                                      preload,
-                                     flags,
-                                     exports);
+                                     flags);
           }
         break;
 
@@ -1147,7 +1132,6 @@ static const EnvMount known_required_env[] =
 static void
 bind_and_propagate_from_environ (PvWrapContext *self,
                                  PvHomeMode home_mode,
-                                 FlatpakExports *exports,
                                  SrtEnvOverlay *container_env,
                                  const char *variable,
                                  EnvMountFlags flags,
@@ -1163,7 +1147,7 @@ bind_and_propagate_from_environ (PvWrapContext *self,
   gsize i;
 
   g_return_if_fail (PV_IS_WRAP_CONTEXT (self));
-  g_return_if_fail (exports != NULL);
+  g_return_if_fail (self->exports != NULL);
   g_return_if_fail (variable != NULL);
   sysroot = self->current_root;
   g_return_if_fail (sysroot != NULL);
@@ -1220,7 +1204,6 @@ bind_and_propagate_from_environ (PvWrapContext *self,
       value_host = pv_current_namespace_path_to_host_path (canon);
 
       if (!pv_wrap_context_export_if_allowed (self,
-                                              exports,
                                               mode,
                                               canon,
                                               value_host,
@@ -1248,14 +1231,9 @@ bind_and_propagate_from_environ (PvWrapContext *self,
     }
 }
 
-/*
- * @exports: (nullable): List of exported directories, or %NULL if running
- *  a Flatpak subsandbox
- */
 void
 pv_bind_and_propagate_from_environ (PvWrapContext *self,
                                     PvHomeMode home_mode,
-                                    FlatpakExports *exports,
                                     SrtEnvOverlay *container_env)
 {
   gsize i;
@@ -1268,13 +1246,12 @@ pv_bind_and_propagate_from_environ (PvWrapContext *self,
     {
       const char *name = known_required_env[i].name;
 
-      if (exports != NULL)
+      if (self->exports != NULL)
         {
           /* If we're using bubblewrap directly, we can and must make
            * sure that all required directories are bind-mounted */
           bind_and_propagate_from_environ (self, home_mode,
-                                           exports, container_env,
-                                           name,
+                                           container_env, name,
                                            known_required_env[i].flags,
                                            known_required_env[i].export_flags);
         }
diff --git a/pressure-vessel/wrap-setup.h b/pressure-vessel/wrap-setup.h
index c96393e80..26d1c7e40 100644
--- a/pressure-vessel/wrap-setup.h
+++ b/pressure-vessel/wrap-setup.h
@@ -86,8 +86,7 @@ void pv_wrap_append_preload (PvWrapContext *context,
                              GPtrArray *argv,
                              PvPreloadVariableIndex which,
                              const char *preload,
-                             PvAppendPreloadFlags flags,
-                             FlatpakExports *exports);
+                             PvAppendPreloadFlags flags);
 
 gboolean pv_wrap_maybe_load_nvidia_modules (GError **error);
 
@@ -98,5 +97,4 @@ void pv_share_temp_dir (FlatpakExports *exports,
                         SrtEnvOverlay *container_env);
 void pv_bind_and_propagate_from_environ (PvWrapContext *self,
                                          PvHomeMode home_mode,
-                                         FlatpakExports *exports,
                                          SrtEnvOverlay *container_env);
diff --git a/pressure-vessel/wrap.c b/pressure-vessel/wrap.c
index 0e20c0997..35a64dfb5 100644
--- a/pressure-vessel/wrap.c
+++ b/pressure-vessel/wrap.c
@@ -86,7 +86,6 @@ main (int argc,
   g_autoptr(FlatpakBwrap) bwrap_home_arguments = NULL;
   g_autoptr(FlatpakBwrap) argv_in_container = NULL;
   g_autoptr(FlatpakBwrap) final_argv = NULL;
-  g_autoptr(FlatpakExports) exports = NULL;
   g_autoptr(SrtSysroot) real_root = NULL;
   g_autoptr(SrtSysroot) interpreter_root = NULL;
   g_autofree gchar *bwrap_executable = NULL;
@@ -460,7 +459,7 @@ main (int argc,
       g_assert (bwrap_executable != NULL);
       flatpak_bwrap_add_arg (bwrap, bwrap_executable);
       bwrap_filesystem_arguments = flatpak_bwrap_new (flatpak_bwrap_empty_env);
-      exports = flatpak_exports_new ();
+      self->exports = flatpak_exports_new ();
     }
   else
     {
@@ -468,7 +467,7 @@ main (int argc,
     }
 
   /* Invariant: we have bwrap or exports iff we also have the other */
-  g_assert ((bwrap != NULL) == (exports != NULL));
+  g_assert ((bwrap != NULL) == (self->exports != NULL));
   g_assert ((bwrap != NULL) == (bwrap_filesystem_arguments != NULL));
   g_assert ((bwrap != NULL) == (bwrap_executable != NULL));
 
@@ -478,7 +477,7 @@ main (int argc,
     {
       FlatpakFilesystemMode sysfs_mode = FLATPAK_FILESYSTEM_MODE_READ_ONLY;
 
-      g_assert (exports != NULL);
+      g_assert (self->exports != NULL);
       g_assert (bwrap_filesystem_arguments != NULL);
 
       if ((_srt_util_get_log_flags () & SRT_LOG_FLAGS_LEVEL)
@@ -691,7 +690,7 @@ main (int argc,
         goto out;
 
       if (!pv_runtime_bind (self->runtime,
-                            exports,
+                            self->exports,
                             bwrap_filesystem_arguments,
                             container_env,
                             error))
@@ -725,10 +724,10 @@ main (int argc,
       g_assert (!self->is_flatpak_env);
       g_assert (bwrap != NULL);
       g_assert (bwrap_filesystem_arguments != NULL);
-      g_assert (exports != NULL);
+      g_assert (self->exports != NULL);
 
       if (!pv_wrap_use_host_os (_srt_sysroot_get_fd (real_root),
-                                exports, bwrap_filesystem_arguments,
+                                self->exports, bwrap_filesystem_arguments,
                                 cmp, error))
         goto out;
     }
@@ -738,9 +737,9 @@ main (int argc,
    * so that it can be overridden by --filesystem=/home or
    * pv_wrap_use_home(), and so that it is sorted correctly with
    * respect to all the other home-directory-related exports. */
-  if (exports != NULL
+  if (self->exports != NULL
       && g_file_test ("/home", G_FILE_TEST_EXISTS))
-    pv_exports_mask_or_log (exports, "/home");
+    pv_exports_mask_or_log (self->exports, "/home");
 
   g_debug ("Making home directory available...");
 
@@ -765,12 +764,12 @@ main (int argc,
       g_assert (!self->is_flatpak_env);
       g_assert (bwrap != NULL);
       g_assert (bwrap_filesystem_arguments != NULL);
-      g_assert (exports != NULL);
+      g_assert (self->exports != NULL);
 
       bwrap_home_arguments = flatpak_bwrap_new (flatpak_bwrap_empty_env);
 
       if (!pv_wrap_use_home (home_mode, home, private_home,
-                             exports, bwrap_home_arguments, container_env,
+                             self->exports, bwrap_home_arguments, container_env,
                              error))
         goto out;
     }
@@ -792,8 +791,8 @@ main (int argc,
         }
     }
 
-  if (exports != NULL)
-    pv_share_temp_dir (exports, container_env);
+  if (self->exports != NULL)
+    pv_share_temp_dir (self->exports, container_env);
 
   if (flatpak_subsandbox != NULL)
     {
@@ -842,13 +841,12 @@ main (int argc,
                                   adverb_preload_argv,
                                   module->which,
                                   module->preload,
-                                  append_preload_flags,
-                                  exports);
+                                  append_preload_flags);
         }
     }
   while (0);
 
-  pv_bind_and_propagate_from_environ (self, home_mode, exports, container_env);
+  pv_bind_and_propagate_from_environ (self, home_mode, container_env);
 
   if (flatpak_subsandbox == NULL)
     {
@@ -856,7 +854,7 @@ main (int argc,
 
       g_assert (bwrap != NULL);
       g_assert (bwrap_filesystem_arguments != NULL);
-      g_assert (exports != NULL);
+      g_assert (self->exports != NULL);
 
       /* Bind-mount /run/udev to support games that detect joysticks by using
        * udev directly. We only do that when the host's version of libudev.so.1
@@ -876,7 +874,7 @@ main (int argc,
               if (g_lstat (override, &ignored) == 0)
                 {
                   g_debug ("We are using the host's version of \"libudev.so.1\", trying to bind-mount /run/udev too...");
-                  pv_exports_expose_or_log (exports,
+                  pv_exports_expose_or_log (self->exports,
                                             FLATPAK_FILESYSTEM_MODE_READ_ONLY,
                                             "/run/udev");
                   break;
@@ -894,7 +892,7 @@ main (int argc,
             g_warning ("Not sharing %s with container to work around %s",
                        framework_paths->path, framework_paths->bug);
           else
-            pv_exports_expose_or_log (exports,
+            pv_exports_expose_or_log (self->exports,
                                       FLATPAK_FILESYSTEM_MODE_READ_ONLY,
                                       framework_paths->path);
         }
@@ -906,7 +904,7 @@ main (int argc,
           g_debug ("Processing --filesystem arguments...");
 
           for (i = 0; self->options.filesystems[i] != NULL; i++)
-            pv_wrap_context_export_if_allowed (self, exports,
+            pv_wrap_context_export_if_allowed (self,
                                                FLATPAK_FILESYSTEM_MODE_READ_WRITE,
                                                self->options.filesystems[i],
                                                self->options.filesystems[i],
@@ -935,7 +933,7 @@ main (int argc,
            * current namespace as well as in the host, because it's
            * either in our ~/.var/app/$FLATPAK_ID, or a --filesystem that
            * was exposed from the host. */
-          pv_exports_expose_or_warn (exports,
+          pv_exports_expose_or_warn (self->exports,
                                      FLATPAK_FILESYSTEM_MODE_READ_WRITE,
                                      cwd_p_host);
         }
@@ -971,10 +969,10 @@ main (int argc,
 
               /* $STEAM_RUNTIME is functionally necessary if used, so
                * always warn if it cannot be exposed */
-              if (exports != NULL
+              if (self->exports != NULL
                   && g_str_has_prefix (self->options.env_if_host[i],
                                        "STEAM_RUNTIME=/"))
-                pv_exports_expose_or_warn (exports,
+                pv_exports_expose_or_warn (self->exports,
                                            FLATPAK_FILESYSTEM_MODE_READ_ONLY,
                                            equals + 1);
 
@@ -989,7 +987,7 @@ main (int argc,
     }
 
   /* Convert the exported directories into extra bubblewrap arguments */
-  if (exports != NULL)
+  if (self->exports != NULL)
     {
       g_autoptr(FlatpakBwrap) exports_bwrap =
         flatpak_bwrap_new (flatpak_bwrap_empty_env);
@@ -1008,7 +1006,7 @@ main (int argc,
           g_clear_pointer (&bwrap_home_arguments, flatpak_bwrap_free);
         }
 
-      flatpak_exports_append_bwrap_args (exports, exports_bwrap);
+      flatpak_exports_append_bwrap_args (self->exports, exports_bwrap);
       g_warn_if_fail (g_strv_length (exports_bwrap->envp) == 0);
       if (!pv_bwrap_append_adjusted_exports (bwrap, exports_bwrap, home,
                                              interpreter_root, workarounds,
diff --git a/tests/pressure-vessel/wrap-setup.c b/tests/pressure-vessel/wrap-setup.c
index af7762be6..bd6e7b7a5 100644
--- a/tests/pressure-vessel/wrap-setup.c
+++ b/tests/pressure-vessel/wrap-setup.c
@@ -171,14 +171,14 @@ fixture_populate_dir (Fixture *f,
     }
 }
 
-static FlatpakExports *
+static void
 fixture_create_exports (Fixture *f)
 {
-  g_autoptr(FlatpakExports) exports = flatpak_exports_new ();
   glnx_autofd int fd = open_or_die (f->mock_host->path, O_RDONLY | O_DIRECTORY, 0755);
 
-  flatpak_exports_take_host_fd (exports, g_steal_fd (&fd));
-  return g_steal_pointer (&exports);
+  g_return_if_fail (f->context->exports == NULL);
+  f->context->exports = flatpak_exports_new ();
+  flatpak_exports_take_host_fd (f->context->exports, g_steal_fd (&fd));
 }
 
 static void
@@ -618,18 +618,20 @@ test_export_root_dirs (Fixture *f,
     "usr/local/",
     "var/tmp/",
   };
-  g_autoptr(FlatpakExports) exports = fixture_create_exports (f);
   g_autoptr(GError) local_error = NULL;
   gboolean ret;
 
+  fixture_create_exports (f);
+  g_assert_nonnull (f->context->exports);
   fixture_populate_dir (f, f->mock_host->fd, paths, G_N_ELEMENTS (paths));
-  ret = pv_export_root_dirs_like_filesystem_host (f->mock_host->fd, exports,
+  ret = pv_export_root_dirs_like_filesystem_host (f->mock_host->fd,
+                                                  f->context->exports,
                                                   FLATPAK_FILESYSTEM_MODE_READ_WRITE,
                                                   _srt_dirent_strcmp,
                                                   &local_error);
   g_assert_no_error (local_error);
   g_assert_true (ret);
-  flatpak_exports_append_bwrap_args (exports, f->bwrap);
+  flatpak_exports_append_bwrap_args (f->context->exports, f->bwrap);
 
   dump_bwrap (f->bwrap);
 
@@ -1555,8 +1557,7 @@ test_passwd (Fixture *f,
 static void
 populate_ld_preload (Fixture *f,
                      GPtrArray *argv,
-                     PvAppendPreloadFlags flags,
-                     FlatpakExports *exports)
+                     PvAppendPreloadFlags flags)
 {
   static const struct
   {
@@ -1589,6 +1590,11 @@ populate_ld_preload (Fixture *f,
   };
   gsize i;
 
+  if (flags & PV_APPEND_PRELOAD_FLAGS_FLATPAK_SUBSANDBOX)
+    g_assert_null (f->context->exports);
+  else
+    g_assert_nonnull (f->context->exports);
+
   for (i = 0; i < G_N_ELEMENTS (preloads); i++)
     {
       GLogLevelFlags old_fatal_mask = G_LOG_FATAL_MASK;
@@ -1615,8 +1621,7 @@ populate_ld_preload (Fixture *f,
                               argv,
                               PV_PRELOAD_VARIABLE_INDEX_LD_PRELOAD,
                               preloads[i].string,
-                              flags | PV_APPEND_PRELOAD_FLAGS_IN_UNIT_TESTS,
-                              exports);
+                              flags | PV_APPEND_PRELOAD_FLAGS_IN_UNIT_TESTS);
 
       /* If we modified the fatal mask, put back the old value. */
       if (preloads[i].warning != NULL)
@@ -1693,7 +1698,7 @@ test_remap_ld_preload (Fixture *f,
                        gconstpointer context)
 {
   const Config *config = context;
-  g_autoptr(FlatpakExports) exports = fixture_create_exports (f);
+  FlatpakExports *exports;
   g_autoptr(GPtrArray) argv = g_ptr_array_new_with_free_func (g_free);
   g_autoptr(GPtrArray) filtered = filter_expected_paths (config);
   gsize i;
@@ -1704,9 +1709,13 @@ test_remap_ld_preload (Fixture *f,
     expect_i386 = TRUE;
 #endif
 
+  fixture_create_exports (f);
+  exports = f->context->exports;
+  g_assert_nonnull (exports);
+
   fixture_create_runtime (f, PV_RUNTIME_FLAGS_NONE);
   g_assert_nonnull (f->context->runtime);
-  populate_ld_preload (f, argv, config->preload_flags, exports);
+  populate_ld_preload (f, argv, config->preload_flags);
 
   g_assert_cmpuint (argv->len, ==, filtered->len);
 
@@ -1792,8 +1801,7 @@ test_remap_ld_preload_flatpak (Fixture *f,
   g_assert_nonnull (f->context->runtime);
   populate_ld_preload (f, argv,
                        (config->preload_flags
-                        | PV_APPEND_PRELOAD_FLAGS_FLATPAK_SUBSANDBOX),
-                       NULL);
+                        | PV_APPEND_PRELOAD_FLAGS_FLATPAK_SUBSANDBOX));
 
   g_assert_cmpuint (argv->len, ==, filtered->len);
 
@@ -1828,7 +1836,7 @@ test_remap_ld_preload_no_runtime (Fixture *f,
   const Config *config = context;
   g_autoptr(GPtrArray) argv = g_ptr_array_new_with_free_func (g_free);
   g_autoptr(GPtrArray) filtered = filter_expected_paths (config);
-  g_autoptr(FlatpakExports) exports = fixture_create_exports (f);
+  FlatpakExports *exports;
   gsize i, j;
   gboolean expect_i386 = FALSE;
 
@@ -1839,8 +1847,12 @@ test_remap_ld_preload_no_runtime (Fixture *f,
     expect_i386 = TRUE;
 #endif
 
+  fixture_create_exports (f);
+  exports = f->context->exports;
+  g_assert_nonnull (exports);
+
   g_assert_null (f->context->runtime);
-  populate_ld_preload (f, argv, config->preload_flags, exports);
+  populate_ld_preload (f, argv, config->preload_flags);
 
   g_assert_cmpuint (argv->len, ==, filtered->len - 1);
 
@@ -1923,8 +1935,7 @@ test_remap_ld_preload_flatpak_no_runtime (Fixture *f,
   g_assert_null (f->context->runtime);
   populate_ld_preload (f, argv,
                        (config->preload_flags
-                        | PV_APPEND_PRELOAD_FLAGS_FLATPAK_SUBSANDBOX),
-                       NULL);
+                        | PV_APPEND_PRELOAD_FLAGS_FLATPAK_SUBSANDBOX));
 
   g_assert_cmpuint (argv->len, ==, filtered->len);
 
@@ -2066,13 +2077,17 @@ test_use_home_shared (Fixture *f,
     NULL
   };
   g_autoptr(FlatpakBwrap) env_bwrap = NULL;
-  g_autoptr(FlatpakExports) exports = fixture_create_exports (f);
-  g_autoptr(FlatpakExports) env_exports = fixture_create_exports (f);
+  FlatpakExports *exports;
+  FlatpakExports *env_exports;
   g_autoptr(GError) local_error = NULL;
   g_autoptr(SrtEnvOverlay) container_env = _srt_env_overlay_new ();
   GLogLevelFlags was_fatal;
   gboolean ret;
 
+  fixture_create_exports (f);
+  exports = f->context->exports;
+  g_assert_nonnull (exports);
+
   fixture_populate_dir (f, f->mock_host->fd, paths, G_N_ELEMENTS (paths));
   ret = pv_wrap_use_home (PV_HOME_MODE_SHARED, "/home/user", NULL, exports,
                           f->bwrap, container_env, &local_error);
@@ -2139,11 +2154,17 @@ test_use_home_shared (Fixture *f,
   g_clear_pointer (&f->context->original_environ, g_strfreev);
   f->context->original_environ = _srt_strdupv (mock_environ);
 
+  g_clear_pointer (&f->context->exports, flatpak_exports_free);
+  exports = NULL;
+
+  fixture_create_exports (f);
+  env_exports = f->context->exports;
+  g_assert_nonnull (env_exports);
+
   /* Don't crash on warnings here */
   was_fatal = g_log_set_always_fatal (G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL);
   pv_bind_and_propagate_from_environ (f->context,
                                       PV_HOME_MODE_SHARED,
-                                      env_exports,
                                       container_env);
   g_log_set_always_fatal (was_fatal);
 
@@ -2210,16 +2231,17 @@ test_use_host_os (Fixture *f,
     "usr/local/",
     "var/tmp/",
   };
-  g_autoptr(FlatpakExports) exports = fixture_create_exports (f);
   g_autoptr(GError) local_error = NULL;
   gboolean ret;
 
+  fixture_create_exports (f);
+  g_assert_nonnull (f->context->exports);
   fixture_populate_dir (f, f->mock_host->fd, paths, G_N_ELEMENTS (paths));
-  ret = pv_wrap_use_host_os (f->mock_host->fd, exports, f->bwrap,
+  ret = pv_wrap_use_host_os (f->mock_host->fd, f->context->exports, f->bwrap,
                              _srt_dirent_strcmp, &local_error);
   g_assert_no_error (local_error);
   g_assert_true (ret);
-  flatpak_exports_append_bwrap_args (exports, f->bwrap);
+  flatpak_exports_append_bwrap_args (f->context->exports, f->bwrap);
 
   dump_bwrap (f->bwrap);
 
-- 
GitLab


From 2ad3a246ed64dfd668eb8201ae9970c041023d63 Mon Sep 17 00:00:00 2001
From: Simon McVittie <smcv@collabora.com>
Date: Fri, 7 Feb 2025 13:12:45 +0000
Subject: [PATCH 8/8] pv-wrap: Store the home directory in the PvWrapContext

This can be used to set a mock home directory for unit testing, which we
now do in the wrap-setup test (although it isn't used for anything yet).

Signed-off-by: Simon McVittie <smcv@collabora.com>
---
 pressure-vessel/wrap-context.c     | 34 ++++++++++++++++++++++++++++++
 pressure-vessel/wrap-context.h     |  2 ++
 pressure-vessel/wrap.c             | 19 ++++++++---------
 tests/pressure-vessel/wrap-setup.c |  2 +-
 4 files changed, 46 insertions(+), 11 deletions(-)

diff --git a/pressure-vessel/wrap-context.c b/pressure-vessel/wrap-context.c
index 9cc0db7b9..8216431fa 100644
--- a/pressure-vessel/wrap-context.c
+++ b/pressure-vessel/wrap-context.c
@@ -98,6 +98,7 @@ pv_wrap_options_clear (PvWrapOptions *self)
 
 enum {
   PROP_0,
+  PROP_CURRENT_HOME,
   PROP_CURRENT_ROOT,
   N_PROPERTIES
 };
@@ -133,6 +134,10 @@ pv_wrap_context_get_property (GObject *object,
 
   switch (prop_id)
     {
+      case PROP_CURRENT_HOME:
+        g_value_set_string (value, self->current_home);
+        break;
+
       case PROP_CURRENT_ROOT:
         g_value_set_object (value, self->current_root);
         break;
@@ -152,6 +157,12 @@ pv_wrap_context_set_property (GObject *object,
 
   switch (prop_id)
     {
+      case PROP_CURRENT_HOME:
+        /* Construct-only */
+        g_return_if_fail (self->current_home == NULL);
+        self->current_home = g_value_dup_string (value);
+        break;
+
       case PROP_CURRENT_ROOT:
         /* Construct-only */
         g_return_if_fail (self->current_root == NULL);
@@ -163,6 +174,17 @@ pv_wrap_context_set_property (GObject *object,
     }
 }
 
+static void
+pv_wrap_context_constructed (GObject *object)
+{
+  PvWrapContext *self = PV_WRAP_CONTEXT (object);
+
+  G_OBJECT_CLASS (pv_wrap_context_parent_class)->constructed (object);
+
+  if (self->current_home == NULL)
+    self->current_home = g_strdup (g_get_home_dir ());
+}
+
 static void
 pv_wrap_context_dispose (GObject *object)
 {
@@ -185,6 +207,7 @@ pv_wrap_context_finalize (GObject *object)
   g_clear_pointer (&self->paths_not_exported, g_hash_table_unref);
   g_strfreev (self->original_argv);
   g_strfreev (self->original_environ);
+  g_free (self->current_home);
 
   G_OBJECT_CLASS (pv_wrap_context_parent_class)->finalize (object);
 }
@@ -196,9 +219,18 @@ pv_wrap_context_class_init (PvWrapContextClass *cls)
 
   object_class->get_property = pv_wrap_context_get_property;
   object_class->set_property = pv_wrap_context_set_property;
+  object_class->constructed = pv_wrap_context_constructed;
   object_class->dispose = pv_wrap_context_dispose;
   object_class->finalize = pv_wrap_context_finalize;
 
+  properties[PROP_CURRENT_HOME] =
+    g_param_spec_string ("current-home", "Current $HOME",
+                         ("Path to real or mock home directory "
+                          "within current-root"),
+                         NULL,
+                         (G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
+                          G_PARAM_STATIC_STRINGS));
+
   properties[PROP_CURRENT_ROOT] =
     g_param_spec_object ("current-root", "Current root",
                          ("Real or mock root directory for the current "
@@ -212,10 +244,12 @@ pv_wrap_context_class_init (PvWrapContextClass *cls)
 
 PvWrapContext *
 pv_wrap_context_new (SrtSysroot *current_root,
+                     const char *current_home,
                      GError **error)
 {
   /* Can't actually fail right now */
   return g_object_new (PV_TYPE_WRAP_CONTEXT,
+                       "current-home", current_home,
                        "current-root", current_root,
                        NULL);
 }
diff --git a/pressure-vessel/wrap-context.h b/pressure-vessel/wrap-context.h
index d98ece381..68cc601a1 100644
--- a/pressure-vessel/wrap-context.h
+++ b/pressure-vessel/wrap-context.h
@@ -99,6 +99,7 @@ struct _PvWrapContext
   SrtSysroot *current_root;
   gchar **original_argv;
   gchar **original_environ;
+  gchar *current_home;
 
   PvWrapOptions options;
 
@@ -123,6 +124,7 @@ GType pv_wrap_context_get_type (void);
 G_DEFINE_AUTOPTR_CLEANUP_FUNC (PvWrapContext, g_object_unref)
 
 PvWrapContext *pv_wrap_context_new (SrtSysroot *current_root,
+                                    const char *current_home,
                                     GError **error);
 
 gboolean pv_wrap_options_parse_environment (PvWrapOptions *self,
diff --git a/pressure-vessel/wrap.c b/pressure-vessel/wrap.c
index 35a64dfb5..de714d16c 100644
--- a/pressure-vessel/wrap.c
+++ b/pressure-vessel/wrap.c
@@ -94,7 +94,6 @@ main (int argc,
   g_autofree gchar *cwd_l = NULL;
   g_autofree gchar *cwd_p_host = NULL;
   g_autofree gchar *private_home = NULL;
-  const gchar *home;
   g_autofree gchar *tools_dir = NULL;
   glnx_autofd int original_stdout = -1;
   glnx_autofd int original_stderr = -1;
@@ -139,7 +138,7 @@ main (int argc,
   if (real_root == NULL)
     return FALSE;
 
-  self = pv_wrap_context_new (real_root, error);
+  self = pv_wrap_context_new (real_root, g_get_home_dir (), error);
 
   if (self == NULL)
     goto out;
@@ -241,8 +240,6 @@ main (int argc,
   else
     steam_app_id = _srt_get_steam_app_id ();
 
-  home = g_get_home_dir ();
-
   if (self->options.share_home == TRISTATE_YES)
     {
       home_mode = PV_HOME_MODE_SHARED;
@@ -259,7 +256,7 @@ main (int argc,
   else if (self->options.freedesktop_app_id)
     {
       home_mode = PV_HOME_MODE_PRIVATE;
-      private_home = g_build_filename (home, ".var", "app",
+      private_home = g_build_filename (self->current_home, ".var", "app",
                                        self->options.freedesktop_app_id,
                                        NULL);
     }
@@ -268,7 +265,7 @@ main (int argc,
       home_mode = PV_HOME_MODE_PRIVATE;
       self->options.freedesktop_app_id = g_strdup_printf ("com.steampowered.App%s",
                                                           steam_app_id);
-      private_home = g_build_filename (home, ".var", "app",
+      private_home = g_build_filename (self->current_home, ".var", "app",
                                        self->options.freedesktop_app_id,
                                        NULL);
     }
@@ -768,7 +765,7 @@ main (int argc,
 
       bwrap_home_arguments = flatpak_bwrap_new (flatpak_bwrap_empty_env);
 
-      if (!pv_wrap_use_home (home_mode, home, private_home,
+      if (!pv_wrap_use_home (home_mode, self->current_home, private_home,
                              self->exports, bwrap_home_arguments, container_env,
                              error))
         goto out;
@@ -918,7 +915,7 @@ main (int argc,
 
       cwd_p_host = pv_current_namespace_path_to_host_path (cwd_p);
 
-      if (_srt_is_same_file (home, cwd_p))
+      if (_srt_is_same_file (self->current_home, cwd_p))
         {
           g_info ("Not making physical working directory \"%s\" available to "
                   "container because it is the home directory",
@@ -1008,7 +1005,8 @@ main (int argc,
 
       flatpak_exports_append_bwrap_args (self->exports, exports_bwrap);
       g_warn_if_fail (g_strv_length (exports_bwrap->envp) == 0);
-      if (!pv_bwrap_append_adjusted_exports (bwrap, exports_bwrap, home,
+      if (!pv_bwrap_append_adjusted_exports (bwrap, exports_bwrap,
+                                             self->current_home,
                                              interpreter_root, workarounds,
                                              error))
         goto out;
@@ -1031,7 +1029,8 @@ main (int argc,
                                              self->is_flatpak_env);
       g_warn_if_fail (g_strv_length (sharing_bwrap->envp) == 0);
 
-      if (!pv_bwrap_append_adjusted_exports (bwrap, sharing_bwrap, home,
+      if (!pv_bwrap_append_adjusted_exports (bwrap, sharing_bwrap,
+                                             self->current_home,
                                              interpreter_root, workarounds,
                                              error))
         goto out;
diff --git a/tests/pressure-vessel/wrap-setup.c b/tests/pressure-vessel/wrap-setup.c
index bd6e7b7a5..0dfada919 100644
--- a/tests/pressure-vessel/wrap-setup.c
+++ b/tests/pressure-vessel/wrap-setup.c
@@ -242,7 +242,7 @@ setup (Fixture *f,
   glnx_opendirat (AT_FDCWD, f->var, TRUE, &f->var_fd, &local_error);
   g_assert_no_error (local_error);
 
-  f->context = pv_wrap_context_new (f->mock_host, &local_error);
+  f->context = pv_wrap_context_new (f->mock_host, "/home/me", &local_error);
   g_assert_no_error (local_error);
   f->bwrap = flatpak_bwrap_new (flatpak_bwrap_empty_env);
 
-- 
GitLab