Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

llvmPackages_{15,16,17,git}.libcxx: use LIBCXX_CXX_ABI=libcxxabi link flags on darwin #278945

Merged
merged 1 commit into from Jan 12, 2024
Merged

Conversation

ghost
Copy link

@ghost ghost commented Jan 5, 2024

Closes #269548 NixOS/nix#9640 by incorporating upstream fix: llvm/llvm-project#77218

This change fixes segfaults when multiple c++ libraries are loaded -- this happens on older macos release, eg: big sur, where the system libc++ is loaded as part of the framework, or newer macos, eg: monterey, when one libc++ library is loaded at init and some other libc++ is dlopened later.

The problem stems from not linking with the -Wl,-force_symbols_not_weak_list,${CMAKE_CURRENT_SOURCE_DIR}/../lib/notweak.exp flags which are defined only when LIBCXX_CXX_ABI=libcxxabi in the CMakeLists.txt. These link flags were defined in nixpkgs prior to llvmPackages_15. llvmPackages_15 started using system-libcxxabi, which was introduced https://reviews.llvm.org/D119539 and refined https://reviews.llvm.org/D120727, rather than just libcxxabi.

The upstream fix llvm/llvm-project#77218 treats libcxxabi and system-libcxxabi the same so both get the correct linker flags when built on darwin hosts.

testing:

following 2 tests can run without segfaulting
nix-shell -I nixpkgs=. -p nix --run 'nix-shell -I nixpkgs=. -p ponysay --dry-run'
# shell.nix
{pkgs ? import <nixpkgs> {}}:
let
  dltest = pkgs.callPackage (
    { lib
    , runCommandCC
    , writeText
    , libcxx
    }:
    let
      name = "dltest";
      code = writeText "${name}.c" ''
        #include <dlfcn.h>
        #include <stdio.h>

        int
        main(int argc, const char **argv)
        {
            int numFailed = 0;
            for (int i = 1; i < argc; i++) {
              int failed = dlopen(argv[i], RTLD_LOCAL | RTLD_LAZY) == 0;
              numFailed += failed;
              fprintf(stderr, "dlopen %s: %s\n", argv[i], failed ? "failed" : "succeeded");
            }
            return numFailed;
        }
      '';
    in
      runCommandCC name {src = code;} ''
        mkdir -p "$out"/bin
        cc -Wall -Werror -O2 -o "$out/bin/${name}" "$src" -ldl -L${lib.getLib libcxx}/lib -lc++
      ''
  ) {
    libcxx = pkgs.llvmPackages_16.libcxx;
  };

  shell =
  let
    inherit (pkgs) lib stdenv mkShell writeShellScriptBin;
    ext = stdenv.hostPlatform.extensions.sharedLibrary;
    libcxx-16 = pkgs.llvmPackages_16.libcxx;
    libcxx-15 = pkgs.llvmPackages_15.libcxx;
    getLibcxx = libcxx: "${lib.getLib libcxx}/lib/libc++${ext}" + lib.optionalString stdenv.isLinux ".1";
  in
    mkShell {
      packages = [
        dltest
        (writeShellScriptBin "runtests" ''
          DYLD_PRINT_INITIALIZERS=1 ${lib.getBin dltest}/bin/dltest \
            ${getLibcxx libcxx-15} \
            ${getLibcxx libcxx-16}
        '')
      ];
      shellHook = "runtests";
    };
in
  shell

Description of changes

Things done

  • Built on platform(s)
    • x86_64-linux
    • aarch64-linux
    • x86_64-darwin
    • aarch64-darwin
  • For non-Linux: Is sandboxing enabled in nix.conf? (See Nix manual)
    • sandbox = relaxed
    • sandbox = true
  • Tested, as applicable:
  • Tested compilation of all packages that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review rev HEAD". Note: all changes have to be committed, also see nixpkgs-review usage
  • Tested basic functionality of all binary files (usually in ./result/bin/)
  • 24.05 Release Notes (or backporting 23.05 and 23.11 Release notes)
    • (Package updates) Added a release notes entry if the change is major or breaking
    • (Module updates) Added a release notes entry if the change is significant
    • (Module addition) Added a release notes entry if adding a new NixOS module
  • Fits CONTRIBUTING.md.

Add a 👍 reaction to pull requests you find important.

this change is the result of contributions from:
@abathur @cormacrelf @reckenrode @veprbl

@ghost ghost requested a review from RaitoBezarius as a code owner January 5, 2024 15:19
@ofborg ofborg bot added the 6.topic: darwin Running or building packages on Darwin label Jan 5, 2024
@ghost ghost changed the title llvmPackages_{15,16,17}.libcxx: use LIBCXX_CXX_ABI=libcxxabi link flags on darwin llvmPackages_{15,16,17,git}.libcxx: use LIBCXX_CXX_ABI=libcxxabi link flags on darwin Jan 5, 2024
@nixos-discourse
Copy link

This pull request has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/segmentation-fault-when-running-any-nix-program-sigsegv-exit-code-139/36659/12

This change applies the upstream PR
llvm/llvm-project#77218 so the same link flags
on darwin are used as when LIBCXX_CXX_ABI=libcxxabi. Specifically, this
adds
"-Wl,-force_symbols_not_weak_list,${CMAKE_CURRENT_SOURCE_DIR}/../lib/notweak.exp"
which prevents libcxx from segfaulting on darwin
@Baltoli
Copy link
Contributor

Baltoli commented Jan 12, 2024

Sorry for the low-signal comment, but I'd like to +1 this PR @RaitoBezarius1 - it's currently blocking me from updating an important project to LLVM 17 and I'd like to avoid waiting for @a-n-n-a-l-e-e's upstream fix to come in LLVM 18 (thanks for the fix, by the way!).

Footnotes

  1. Tagging as the code owner - sorry if you're not the right person!

@veprbl veprbl merged commit 2ec4624 into NixOS:staging Jan 12, 2024
25 checks passed
Copy link
Contributor

yelite added a commit to yelite/system-config that referenced this pull request Jan 27, 2024
@ghost ghost linked an issue Feb 20, 2024 that may be closed by this pull request
@rrbutani rrbutani added the 6.topic: llvm/clang Issues related to llvmPackages, clangStdenv and related label May 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

libcxx-{15,16,etc} segfault when multiple libcxx loaded
4 participants