From 2dd8bc7977aae7c5f545f8f61415f0bcfc30e02b Mon Sep 17 00:00:00 2001 From: sterni Date: Sun, 15 Aug 2021 13:15:38 +0200 Subject: fix(nix/buildLisp): prevent image loader from parsing arguments MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit CCL and SBCL create executables by dumping their image. As a consequence, some part of the respective compiler is embedded in the resulting executable which is executed and loads the image. For CCL and SBCL this piece of software seems to unconditionally parse arguments which can't be prevented since it happens before any lisp is loaded. Luckily in both cases the parsing stops at `--`, so we can just pass this via the wrapper — we just need to work around the problem that this will of course be left in argv and confuse any later code. This can be rectified by deleting everything prior to the first `--` in the global argument list on startup in both cases. In cases we do want to pass arguments to the image loader, we can use the special NIX_BUILDLISP_LISP_ARGS environment variable which is understood by the wrapper. Note: This fix doesn't interfere with ECL since it is not using the wrapper script at the moment. Fixes b/136. Change-Id: I3f95aa61e945e51428021ca18232ff15c923f870 Reviewed-on: https://cl.tvl.fyi/c/depot/+/3357 Tested-by: BuildkiteCI Reviewed-by: tazjin Reviewed-by: grfn --- nix/buildLisp/default.nix | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) (limited to 'nix') diff --git a/nix/buildLisp/default.nix b/nix/buildLisp/default.nix index 8f95c76916..7e38c0283e 100644 --- a/nix/buildLisp/default.nix +++ b/nix/buildLisp/default.nix @@ -305,7 +305,15 @@ let :directory bindir))) (save-lisp-and-die outpath :executable t - :toplevel (function ${main}) + :toplevel + (lambda () + ;; Filter out everything prior to the `--` we + ;; insert in the wrapper to prevent SBCL from + ;; parsing arguments at startup + (replace sb-ext:*posix-argv* + (cdr (member "--" sb-ext:*posix-argv* + :test #'string=))) + (${main})) :purify t)) ''; @@ -539,7 +547,16 @@ let (save-application executable :purify t :error-handler :quit - :toplevel-function (function ${main}) + :toplevel-function + (lambda () + ;; Filter out everything prior to the `--` we + ;; insert in the wrapper to prevent SBCL from + ;; parsing arguments at startup + (replace ccl:*command-line-argument-list* + (cdr (member "--" + ccl:*command-line-argument-list* + :test #'string=))) + (${main})) :mode #o755 ;; TODO(sterni): use :native t on macOS :prepend-kernel t)) @@ -676,7 +693,9 @@ let } } '' + lib.optionalString implementation.wrapProgram '' - wrapProgram $out/bin/${name} --prefix LD_LIBRARY_PATH : "${libPath}" + wrapProgram $out/bin/${name} \ + --prefix LD_LIBRARY_PATH : "${libPath}" \ + --add-flags "\$NIX_BUILDLISP_LISP_ARGS --" '')); # 'bundled' creates a "library" which makes a built-in package available, -- cgit 1.4.1