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

Executable built with Bazel on MacOS Monterey with XCode 13.2 throwing seg fault #14206

Closed
demorgan104 opened this issue Nov 1, 2021 · 5 comments

Comments

@demorgan104
Copy link

demorgan104 commented Nov 1, 2021

Description of the problem / feature request:

Bazel will build a simple "Hello World" executable that is throwing a "Segmentation fault: 11" error.

Bugs: what's the simplest, easiest way to reproduce this bug? Please provide a minimal example if possible.

The build file is having the following content:

      load("@rules_cc//cc:defs.bzl", "cc_binary")
      cc_binary(
          name = "main",
          srcs = ["main.cpp"],
          visibility = ["//visibility:public"],
      )

Main.cpp:

        #include <iostream>
        int main() {
            std::cout << "Hello World";
            return 0;
        }

What operating system are you running Bazel on?

MacOS Monterey (version 12.0.1)

What's the output of bazel info release?

release 4.2.1-homebrew

Have you found anything relevant by searching the web?

I have found something similar for earlier versions of Apple Clang.
In those cases anyway the program will through segmentation faults during the execution. In my case I will get a segmentation fault before the program is executed. (basically on anything)

https://discussions.apple.com/thread/6009790?answerId=25322969022#25322969022

I am thinking this behavior might have something to do with the default flags from the default Bazel rules for macos.

Any other information, logs, or outputs that you want to share?

I tried re-installing xcode tools, I tried cleaning bazel cache.

You can find the execution log below:

$ bazel run //src:main

INFO: Analyzed target //src:main (15 packages loaded, 61 targets configured).
INFO: Found 1 target...
Target //src:main up-to-date:
  bazel-bin/src/main
INFO: Elapsed time: 1,155s, Critical Path: 0,71s
INFO: 8 processes: 6 internal, 2 darwin-sandbox.
INFO: Build completed successfully, 8 total actions
INFO: Build completed successfully, 8 total actions
zsh: segmentation fault  bazel run //src:main

$ bazel clean

INFO: Starting clean.

$ bazel run -s //src:main

INFO: Analyzed target //src:main (15 packages loaded, 61 targets configured).
INFO: Found 1 target...
SUBCOMMAND: # //src:main [action 'Compiling src/main.cpp', configuration: 51eca898bf06b62dddc4e4762e2e193fc5c16e384d78bdf88fb0dd0f0af3120c, execution platform: @local_config_platform//:host]
(cd /private/var/tmp/_bazel_timo/7b405aebb7d2c99589e84a6855442b7d/execroot/__main__ && \
  exec env - \
    PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin \
    PWD=/proc/self/cwd \
  external/local_config_cc/cc_wrapper.sh -U_FORTIFY_SOURCE -fstack-protector -Wall -Wthread-safety -Wself-assign -Wno-free-nonheap-object -fcolor-diagnostics -fno-omit-frame-pointer '-std=c++0x' -MD -MF bazel-out/darwin-fastbuild/bin/src/_objs/main/main.pic.d '-frandom-seed=bazel-out/darwin-fastbuild/bin/src/_objs/main/main.pic.o' -fPIC -iquote . -iquote bazel-out/darwin-fastbuild/bin -iquote external/bazel_tools -iquote bazel-out/darwin-fastbuild/bin/external/bazel_tools '-std=c++17' -no-canonical-prefixes -Wno-builtin-macro-redefined '-D__DATE__="redacted"' '-D__TIMESTAMP__="redacted"' '-D__TIME__="redacted"' -c src/main.cpp -o bazel-out/darwin-fastbuild/bin/src/_objs/main/main.pic.o)
SUBCOMMAND: # //src:main [action 'Linking src/main', configuration: 51eca898bf06b62dddc4e4762e2e193fc5c16e384d78bdf88fb0dd0f0af3120c, execution platform: @local_config_platform//:host]
(cd /private/var/tmp/_bazel/7b405aebb7d2c99589e84a6855442b7d/execroot/__main__ && \
  exec env - \
    PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin \
    PWD=/proc/self/cwd \
  external/local_config_cc/cc_wrapper.sh @bazel-out/darwin-fastbuild/bin/src/main-2.params)
Target //src:main up-to-date:
  bazel-bin/src/main
INFO: Elapsed time: 0,996s, Critical Path: 0,64s
INFO: 8 processes: 6 internal, 2 darwin-sandbox.
INFO: Build completed successfully, 8 total actions
INFO: Build completed successfully, 8 total actions
zsh: segmentation fault  bazel run -s //src:main

@demorgan104
Copy link
Author

Solved the problem using the following steps:

  • delete command line tools from /Library/Developer
  • install xcode from the app store
  • execute bazel clean --expunge in the workspace directory of my project
  • bazel run <my_target>

Bazel looks to be completely unreliable at least after MacOS/XCode updates and the fact that a perfectly building and running project is completely broken whenever a new OS version comes out doesn't really make it funnier....

@llivshitz-quotient
Copy link

  • delete command line tools
    @demorgan104
    Same problem, tried your proposed solution and it didn't work... :(

@maxbennedich
Copy link

Solved the problem using the following steps:

Thanks for sharing, these steps fixed the segfault for me too.

@nresare
Copy link
Contributor

nresare commented Nov 13, 2021

looking at the output of bazel build --subcommands //:main the two invocations that produces the segfaulting binary are the following (with paths being made to point to the current directory to simplify playing around with it):

/usr/bin/gcc -U_FORTIFY_SOURCE -fstack-protector -Wall -Wthread-safety -Wself-assign -fcolor-diagnostics -fno-omit-frame-pointer -std=c++0x -MD -MF main.pic.d -frandom-seed=main.pic.o -fPIC -iquote . -iquote bazel-out/darwin_arm64-fastbuild/bin -iquote external/bazel_tools -iquote bazel-out/darwin_arm64-fastbuild/bin/external/bazel_tools -no-canonical-prefixes -Wno-builtin-macro-redefined '-D__DATE__="redacted"' '-D__TIMESTAMP__="redacted"' '-D__TIME__="redacted"' -c main.cpp -o main.pic.o
/usr/bin/gcc -o main main.pic.o -Wl,-S -undefined dynamic_lookup -headerpad_max_install_names
./main
zsh: segmentation fault  ./main

@JackyMa1997
Copy link

I have the same problem with you. And the solution is to delete the command line tools and install the xcode from app store. At the same time, note the version of the bazel . I fixed the problem with the bazel-5.4.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants