From 0dd251e2471bb1913663f4ac93963be47bbe62ec Mon Sep 17 00:00:00 2001 From: Randy Eckenrode Date: Sat, 1 Feb 2025 16:18:16 -0500 Subject: [PATCH 1/5] darwin.libffi: fix a memory leak See https://github.com/libffi/libffi/pull/621#discussion_r955298301 --- .../darwin/apple-source-releases/libffi/package.nix | 2 ++ .../patches/fix-tramponline-memory-leak.patch | 13 +++++++++++++ 2 files changed, 15 insertions(+) create mode 100644 pkgs/os-specific/darwin/apple-source-releases/libffi/patches/fix-tramponline-memory-leak.patch diff --git a/pkgs/os-specific/darwin/apple-source-releases/libffi/package.nix b/pkgs/os-specific/darwin/apple-source-releases/libffi/package.nix index 2a684a729f3e..5e9ea766bdd2 100644 --- a/pkgs/os-specific/darwin/apple-source-releases/libffi/package.nix +++ b/pkgs/os-specific/darwin/apple-source-releases/libffi/package.nix @@ -25,6 +25,8 @@ mkAppleDerivation (finalAttrs: { patches = [ # Clang 18 requires that no non-private symbols by defined after cfi_startproc. Apply the upstream libffi fix. ./patches/llvm-18-compatibility.patch + # Fix a memory leak when using the trampoline dylib. See https://github.com/libffi/libffi/pull/621#discussion_r955298301. + ./patches/fix-tramponline-memory-leak.patch ]; # Make sure libffi is using the trampolines dylib in this package not the system one. diff --git a/pkgs/os-specific/darwin/apple-source-releases/libffi/patches/fix-tramponline-memory-leak.patch b/pkgs/os-specific/darwin/apple-source-releases/libffi/patches/fix-tramponline-memory-leak.patch new file mode 100644 index 000000000000..9bc54c5c29ac --- /dev/null +++ b/pkgs/os-specific/darwin/apple-source-releases/libffi/patches/fix-tramponline-memory-leak.patch @@ -0,0 +1,13 @@ +diff --git a/src/closures.c b/src/closures.c +index 01f9950cd0..1dfd375cff 100644 +--- a/src/closures.c ++++ b/src/closures.c +@@ -329,7 +329,7 @@ + table->next->prev = table->prev; + + /* Deallocate pages */ +- vm_deallocate (mach_task_self (), table->config_page, PAGE_MAX_SIZE * 2); ++ vm_deallocate (mach_task_self (), table->config_page, FFI_TRAMPOLINE_ALLOCATION_PAGE_COUNT * PAGE_MAX_SIZE); + + /* Deallocate free list */ + free (table->free_list_pool); From 5fee99715e45ca513a566cb4c07f33af712d203e Mon Sep 17 00:00:00 2001 From: Randy Eckenrode Date: Sat, 1 Feb 2025 16:18:16 -0500 Subject: [PATCH 2/5] darwin.libffi: drop `--enable-pax_emutramp` configure flag MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It doesn’t appear to do anything on Darwin, so drop it. --- pkgs/os-specific/darwin/apple-source-releases/libffi/package.nix | 1 - 1 file changed, 1 deletion(-) diff --git a/pkgs/os-specific/darwin/apple-source-releases/libffi/package.nix b/pkgs/os-specific/darwin/apple-source-releases/libffi/package.nix index 5e9ea766bdd2..10ef20f67cf2 100644 --- a/pkgs/os-specific/darwin/apple-source-releases/libffi/package.nix +++ b/pkgs/os-specific/darwin/apple-source-releases/libffi/package.nix @@ -48,7 +48,6 @@ mkAppleDerivation (finalAttrs: { configureFlags = [ "--with-gcc-arch=generic" # no detection of -march= or -mtune= - "--enable-pax_emutramp" ]; # Make sure aarch64-darwin is using the trampoline dylib. From 99de8ee9a601329a63c9f72526fd514423598330 Mon Sep 17 00:00:00 2001 From: Randy Eckenrode Date: Sat, 1 Feb 2025 16:18:16 -0500 Subject: [PATCH 3/5] darwin.libffi: align trampoline dylib linker flags with Xcode project --- .../darwin/apple-source-releases/libffi/package.nix | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pkgs/os-specific/darwin/apple-source-releases/libffi/package.nix b/pkgs/os-specific/darwin/apple-source-releases/libffi/package.nix index 10ef20f67cf2..c50bbfd8c5a0 100644 --- a/pkgs/os-specific/darwin/apple-source-releases/libffi/package.nix +++ b/pkgs/os-specific/darwin/apple-source-releases/libffi/package.nix @@ -56,7 +56,8 @@ mkAppleDerivation (finalAttrs: { ''; postBuild = lib.optionalString stdenv.hostPlatform.isAarch64 '' - $CC src/aarch64/trampoline.S -dynamiclib -o libffi-trampolines.dylib \ + $CC -Os -Wl,-allowable_client,! -Wl,-not_for_dyld_shared_cache -Wl,-no_compact_unwind \ + src/aarch64/trampoline.S -dynamiclib -o libffi-trampolines.dylib \ -Iinclude -Iaarch64-apple-darwin -Iaarch64-apple-darwin/include \ -install_name "$out/lib/libffi-trampoline.dylib" -Wl,-compatibility_version,1 -Wl,-current_version,1 ''; From 9f527b1e27c6b934e5fd88aaf4063220b674f3e4 Mon Sep 17 00:00:00 2001 From: Randy Eckenrode Date: Sat, 1 Feb 2025 16:18:16 -0500 Subject: [PATCH 4/5] =?UTF-8?q?darwin.libffi:=20install=20Apple=E2=80=99s?= =?UTF-8?q?=20`ffi.h`=20header?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This header contains availability annotations for libffi functions, which some packages (such as Python’s) check to determine whether they are building against Apple’s libffi fork. --- .../darwin/apple-source-releases/libffi/package.nix | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pkgs/os-specific/darwin/apple-source-releases/libffi/package.nix b/pkgs/os-specific/darwin/apple-source-releases/libffi/package.nix index c50bbfd8c5a0..4b9250ff97cd 100644 --- a/pkgs/os-specific/darwin/apple-source-releases/libffi/package.nix +++ b/pkgs/os-specific/darwin/apple-source-releases/libffi/package.nix @@ -66,6 +66,9 @@ mkAppleDerivation (finalAttrs: { # The Darwin SDK puts the headers in `include/ffi`. Add a symlink for compatibility. '' ln -s "$dev/include" "$dev/include/ffi" + # Make sure Apple’s header with availability annotations is installed in place of the generated one. + # Use `macCatalyst` instead of `iosmac` to avoid errors due to invalid availability annotations. + substitute darwin/include/ffi.h "$dev/include/ffi.h" --replace-fail iosmac macCatalyst '' # Install the trampoline dylib since it is build manually. + lib.optionalString stdenv.hostPlatform.isAarch64 '' From 741de2bc6396974cdc03ca0f0413fae3993df2ca Mon Sep 17 00:00:00 2001 From: Randy Eckenrode Date: Sat, 15 Feb 2025 14:36:48 -0500 Subject: [PATCH 5/5] Revert "Revert "python312Packages.cffi: remove unnecessary Darwin patch"" This reverts commit 340c3f68329b191bef56ad0eafa6b0a8c9fac85c. --- .../cffi/darwin-use-libffi-closures.diff | 29 ------------------- .../python-modules/cffi/default.nix | 14 --------- 2 files changed, 43 deletions(-) delete mode 100644 pkgs/development/python-modules/cffi/darwin-use-libffi-closures.diff diff --git a/pkgs/development/python-modules/cffi/darwin-use-libffi-closures.diff b/pkgs/development/python-modules/cffi/darwin-use-libffi-closures.diff deleted file mode 100644 index 4ef51274aa28..000000000000 --- a/pkgs/development/python-modules/cffi/darwin-use-libffi-closures.diff +++ /dev/null @@ -1,29 +0,0 @@ -diff --git a/src/c/_cffi_backend.c b/src/c/_cffi_backend.c -index 537271f..9c3bf94 100644 ---- a/src/c/_cffi_backend.c -+++ b/src/c/_cffi_backend.c -@@ -103,11 +103,11 @@ - # define CFFI_CHECK_FFI_PREP_CIF_VAR 0 - # define CFFI_CHECK_FFI_PREP_CIF_VAR_MAYBE 0 - --#elif defined(__APPLE__) && defined(FFI_AVAILABLE_APPLE) -+#elif defined(__APPLE__) - --# define CFFI_CHECK_FFI_CLOSURE_ALLOC __builtin_available(macos 10.15, ios 13, watchos 6, tvos 13, *) -+# define CFFI_CHECK_FFI_CLOSURE_ALLOC 1 - # define CFFI_CHECK_FFI_CLOSURE_ALLOC_MAYBE 1 --# define CFFI_CHECK_FFI_PREP_CLOSURE_LOC __builtin_available(macos 10.15, ios 13, watchos 6, tvos 13, *) -+# define CFFI_CHECK_FFI_PREP_CLOSURE_LOC 1 - # define CFFI_CHECK_FFI_PREP_CLOSURE_LOC_MAYBE 1 --# define CFFI_CHECK_FFI_PREP_CIF_VAR __builtin_available(macos 10.15, ios 13, watchos 6, tvos 13, *) -+# define CFFI_CHECK_FFI_PREP_CIF_VAR 1 - # define CFFI_CHECK_FFI_PREP_CIF_VAR_MAYBE 1 -@@ -6422,7 +6422,7 @@ static PyObject *b_callback(PyObject *self, PyObject *args) - else - #endif - { --#if defined(__APPLE__) && defined(FFI_AVAILABLE_APPLE) && !FFI_LEGACY_CLOSURE_API -+#if defined(__APPLE__) && !FFI_LEGACY_CLOSURE_API - PyErr_Format(PyExc_SystemError, "ffi_prep_closure_loc() is missing"); - goto error; - #else diff --git a/pkgs/development/python-modules/cffi/default.nix b/pkgs/development/python-modules/cffi/default.nix index bbdd6bf08cfc..0daa51b3810a 100644 --- a/pkgs/development/python-modules/cffi/default.nix +++ b/pkgs/development/python-modules/cffi/default.nix @@ -44,20 +44,6 @@ else hash = "sha256-HDnGAWwyvEjdVFYZUOvWg24WcPKuRhKPZ89J54nFKCQ="; }; - patches = [ - # - # Trusts the libffi library inside of nixpkgs on Apple devices. - # - # Based on some analysis I did: - # - # https://groups.google.com/g/python-cffi/c/xU0Usa8dvhk - # - # I believe that libffi already contains the code from Apple's fork that is - # deemed safe to trust in cffi. - # - ./darwin-use-libffi-closures.diff - ]; - nativeBuildInputs = [ pkg-config ]; build-system = [ setuptools ];