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

Fix GCC compilation on MacOS 10.15 #82921

Merged
merged 1 commit into from Jul 23, 2020
Merged

Conversation

Calvin-L
Copy link
Contributor

@Calvin-L Calvin-L commented Mar 19, 2020

Motivation for this change

Fix #73319 (GCC libstdc++ does not compile on MacOS 10.15 Catalina).

I left a comment in the diff with details about the fix.

This is a "quick-n-dirty" fix: there may be other derivations that suffer from the same issue, and this won't do anything to help them. The right change is probably going to be somewhere in the Darwin stdenv code, but I don't understand it well enough to know what to do.

Things done
  • Tested using sandboxing (nix.useSandbox on NixOS, or option sandbox in nix.conf on non-NixOS linux)
  • Built on platform(s)
    • NixOS
    • macOS
    • other Linux distributions
  • Tested via one or more NixOS test(s) if existing and applicable for the change (look inside nixos/tests)
  • Tested compilation of all pkgs that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review wip"
  • Tested execution of all binary files (usually in ./result/bin/)
  • Determined the impact on package closure size (by running nix path-info -S before and after)
  • Ensured that relevant documentation is up to date
  • Fits CONTRIBUTING.md.

MacOS 10.15 now includes "aligned_alloc".  Disagreement between the
headers and the binaries about whether aligned_alloc exists leads to
a compilation failure (see NixOS#73319 and the detailed comment in this
commit).
@ofborg ofborg bot added the 6.topic: darwin Running or building packages on Darwin label Mar 19, 2020
Copy link
Member

@veprbl veprbl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks reasonable

# regardless of their actual version. (Nix uses version 10.12 headers.)
# - Nix uses the native standard library binaries for the build system. That
# means the standard library binaries may not exactly match the standard
# library headers.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which library does this symbol come from? While we do use the native Libsystem we only export a subset of it's symbols to avoid problems like this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It isn't declared in any of the libsystem libraries in the store. It sneaks in from outside the store: libSystem.B.dylib is linked to /usr/lib/system/libsystem_malloc.dylib, which contains aligned_alloc.

$ DYLD_PRINT_LIBRARIES=1 ./result/bin/alignedalloc-test 
dyld: loaded: /Users/loncaric/Desktop/alignedalloc/./result/bin/alignedalloc-test
dyld: loaded: /opt/nix/store/b8xv2cjbqlpngwn3fx3hf5pdkaym8f0q-Libsystem-osx-10.12.6/lib/libSystem.B.dylib
dyld: loaded: /opt/nix/store/b8xv2cjbqlpngwn3fx3hf5pdkaym8f0q-Libsystem-osx-10.12.6/lib/system/libsystem_c.dylib
dyld: loaded: /opt/nix/store/b8xv2cjbqlpngwn3fx3hf5pdkaym8f0q-Libsystem-osx-10.12.6/lib/system/libsystem_kernel.dylib
dyld: loaded: /opt/nix/store/b8xv2cjbqlpngwn3fx3hf5pdkaym8f0q-Libsystem-osx-10.12.6/lib/libSystem_internal.dylib
[...]
dyld: loaded: <00190446-C6A8-3EA6-92D1-850EA85C84CE> /usr/lib/system/libsystem_malloc.dylib
[...]
allocated memory at 0x294017a0
$ fgrep -ir aligned_alloc /usr/lib/system/libsystem_malloc.dylib
Binary file /usr/lib/system/libsystem_malloc.dylib matches

I don't have a good mental model of how linkers work, but I'm not terribly surprised that one might discover aligned_alloc through this transitive link.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's the test case I put together, which compiles on Catalina but not on earlier versions of MacOS:

$ cat alignedalloc.c 

int main() {
    void* p = aligned_alloc(16, 64);
    printf("allocated memory at %p\n", p);
    return p ? 0 : 1;
}
$ cat default.nix 

with import <nixpkgs> {};

stdenv.mkDerivation rec {
  name = "alignedalloc-test";
  src = ./.;
  buildPhase = ''
    cc -v alignedalloc.c -o a.out
  '';
  doCheck = true;
  checkPhase = ''
    ./a.out
  '';
  installPhase = ''
    mkdir -p $out/bin
    mv a.out $out/bin/alignedalloc-test
  '';
}
$ nix-build
[...]
/opt/nix/store/b203gi34fvm1y5v26i4h3vgav9cdqgp8-alignedalloc-test

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@LnL7 Any remaining concerns?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess, a slightly more robust way to address this impurity would be to come up with a common list of symbols for libsystem_malloc and plug it into the https://github.com/NixOS/nixpkgs/blob/master/pkgs/os-specific/darwin/apple-source-releases/Libsystem/default.nix . A while I made a tool for doing this using travis https://github.com/veprbl/libsystem_symbol_list , it may come useful.

@veprbl
Copy link
Member

veprbl commented Apr 28, 2020

This needs to target staging branch

@Calvin-L Calvin-L changed the base branch from master to staging April 28, 2020 20:07
@Calvin-L
Copy link
Contributor Author

Thanks for catching that; I've changed the target branch from master to staging.

@veprbl veprbl added this to WIP in Staging via automation Apr 28, 2020
@veprbl veprbl moved this from WIP to Needs review in Staging Apr 28, 2020
@Gaelan
Copy link
Contributor

Gaelan commented May 17, 2020

This seems to have fallen through the cracks. Can it be merged?

@Mic92
Copy link
Member

Mic92 commented May 17, 2020

@GrahamcOfBorg eval

@Calvin-L
Copy link
Contributor Author

I'm not sure how to interpret the automated test output. It failed, but the failure looks unrelated to this change. Is there some action I should be taking here?

@veprbl
Copy link
Member

veprbl commented May 28, 2020

@GrahamcOfBorg eval

@veprbl

This comment has been minimized.

@veprbl

This comment has been minimized.

@Mic92 Mic92 merged commit 9f5d38e into NixOS:staging Jul 23, 2020
Staging automation moved this from Needs review to Done Jul 23, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Staging
  
Done
Development

Successfully merging this pull request may close these issues.

gcc7 libstdc++ on darwin fails to compile
5 participants