Skip to content

Make linking the same shared library twice a rule error#11721

Closed
agluszak wants to merge 9 commits intobazelbuild:masterfrom
agluszak:linking-shared-lib-twice-after-transition
Closed

Make linking the same shared library twice a rule error#11721
agluszak wants to merge 9 commits intobazelbuild:masterfrom
agluszak:linking-shared-lib-twice-after-transition

Conversation

@agluszak
Copy link
Copy Markdown
Contributor

@agluszak agluszak commented Jul 7, 2020

As more and more people start using the power of Starlark transitions, we are seeing more people bumping into a precondition baked deep in the C++ rules implementation which checks that a shared library being linked is in the same configuration directory. The check is there to make sure that the same library built in different configurations is not linked more than once.
Aside from the fact that it should be a proper rule error and not a crash, people are hitting this check when writing cc_import targets that have a transition applied to them. There are other ways to guarantee that the same library is not linked more than once without completely restricting the ability to link a library built in a different configuration.
@oquenchil

This PR makes linking the same shared library twice a rule error instead of a crash and it makes it possible to link a library built in a different configuration. A map from library identifiers to library paths is introduced, making sure that each shared library is linked at most once. Only dynamic linking is taken into account, for static linking see #11727

Fixes #11167 (the check which was causing the NPE was not directly related to transitions, but because it was removed, that NPE should no longer happen)

Copy link
Copy Markdown
Contributor

@oquenchil oquenchil left a comment

Choose a reason for hiding this comment

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

Can you add a description and also add "Fixes #11167" to it?

But before you add that it fixes, please verify that it indeed fixes it. In #11167 they have a link to a PR which reproduces the problem and they also have the necessary command.

Try the command with released bazel (should crash) and try with your custom built binary that has this change (should succeed)

@agluszak
Copy link
Copy Markdown
Contributor Author

But before you add that it fixes, please verify that it indeed fixes it. In #11167 they have a link to a PR which reproduces the problem and they also have the necessary command.

Try the command with released bazel (should crash) and try with your custom built binary that has this change (should succeed)

With released Bazel it crashed with an NPE, with custom built binary it fails with

ERROR: /usr/local/google/home/agluszak/code/rules_go/tests/core/c_linkmodes/BUILD.bazel:52:8: C++ compilation of rule '//tests/core/c_linkmodes:c-shared_test' failed (Exit 1): gcc failed: error executing command 
  (cd /usr/local/google/home/agluszak/.cache/bazel/_bazel_agluszak/f95e0ba868b0609c6e710465f999aad9/sandbox/linux-sandbox/38/execroot/io_bazel_rules_go && \
  exec env - \
    LD_LIBRARY_PATH=/usr/lib/mesa-diverted/x86_64-linux-gnu:/usr/lib/x86_64-linux-gnu/mesa:/usr/lib/x86_64-linux-gnu/dri:/usr/lib/x86_64-linux-gnu/gallium-pipe \
    PATH=/usr/local/google/home/agluszak/bin:/usr/lib/google-golang/bin:/usr/local/buildtools/java/jdk/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin \
    PWD=/proc/self/cwd \
  /usr/bin/gcc -U_FORTIFY_SOURCE -fstack-protector -Wall -Wunused-but-set-parameter -Wno-free-nonheap-object -fno-omit-frame-pointer -MD -MF bazel-out/k8-fastbuild/bin/tests/core/c_linkmodes/_objs/c-shared_test/add_test_shared.pic.d '-frandom-seed=bazel-out/k8-fastbuild/bin/tests/core/c_linkmodes/_objs/c-shared_test/add_test_shared.pic.o' -fPIC -iquote . -iquote bazel-out/k8-fastbuild/bin -iquote external/bazel_tools -iquote bazel-out/k8-fastbuild/bin/external/bazel_tools -Ibazel-out/k8-fastbuild-ST-1e89748556c3/bin -fno-canonical-system-headers -Wno-builtin-macro-redefined '-D__DATE__="redacted"' '-D__TIMESTAMP__="redacted"' '-D__TIME__="redacted"' -c tests/core/c_linkmodes/add_test_shared.c -o bazel-out/k8-fastbuild/bin/tests/core/c_linkmodes/_objs/c-shared_test/add_test_shared.pic.o)
Execution platform: @local_config_platform//:host

Use --sandbox_debug to see verbose messages from the sandbox gcc failed: error executing command 
  (cd /usr/local/google/home/agluszak/.cache/bazel/_bazel_agluszak/f95e0ba868b0609c6e710465f999aad9/sandbox/linux-sandbox/38/execroot/io_bazel_rules_go && \
  exec env - \
    LD_LIBRARY_PATH=/usr/lib/mesa-diverted/x86_64-linux-gnu:/usr/lib/x86_64-linux-gnu/mesa:/usr/lib/x86_64-linux-gnu/dri:/usr/lib/x86_64-linux-gnu/gallium-pipe \
    PATH=/usr/local/google/home/agluszak/bin:/usr/lib/google-golang/bin:/usr/local/buildtools/java/jdk/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin \
    PWD=/proc/self/cwd \
  /usr/bin/gcc -U_FORTIFY_SOURCE -fstack-protector -Wall -Wunused-but-set-parameter -Wno-free-nonheap-object -fno-omit-frame-pointer -MD -MF bazel-out/k8-fastbuild/bin/tests/core/c_linkmodes/_objs/c-shared_test/add_test_shared.pic.d '-frandom-seed=bazel-out/k8-fastbuild/bin/tests/core/c_linkmodes/_objs/c-shared_test/add_test_shared.pic.o' -fPIC -iquote . -iquote bazel-out/k8-fastbuild/bin -iquote external/bazel_tools -iquote bazel-out/k8-fastbuild/bin/external/bazel_tools -Ibazel-out/k8-fastbuild-ST-1e89748556c3/bin -fno-canonical-system-headers -Wno-builtin-macro-redefined '-D__DATE__="redacted"' '-D__TIMESTAMP__="redacted"' '-D__TIME__="redacted"' -c tests/core/c_linkmodes/add_test_shared.c -o bazel-out/k8-fastbuild/bin/tests/core/c_linkmodes/_objs/c-shared_test/add_test_shared.pic.o)
Execution platform: @local_config_platform//:host

Use --sandbox_debug to see verbose messages from the sandbox
tests/core/c_linkmodes/add_test_shared.c:2:10: fatal error: tests/core/c_linkmodes/adder_shared.h: No such file or directory
    2 | #include "tests/core/c_linkmodes/adder_shared.h"
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
Target //tests/core/c_linkmodes:c-shared_test failed to build

...but there's no such file indeed

@googlebot
Copy link
Copy Markdown

We found a Contributor License Agreement for you (the sender of this pull request), but were unable to find agreements for all the commit author(s) or Co-authors. If you authored these, maybe you used a different email address in the git commits than was used to sign the CLA (login here to double check)? If these were authored by someone else, then they will need to sign a CLA as well, and confirm that they're okay with these being contributed to Google.
In order to pass this check, please resolve this problem and then comment @googlebot I fixed it.. If the bot doesn't comment, it means it doesn't think anything has changed.

ℹ️ Googlers: Go here for more info.

@agluszak agluszak marked this pull request as draft July 14, 2020 09:00
@agluszak agluszak force-pushed the linking-shared-lib-twice-after-transition branch from eb489f6 to 807a48b Compare July 14, 2020 09:08
@googlebot
Copy link
Copy Markdown

CLAs look good, thanks!

ℹ️ Googlers: Go here for more info.

@agluszak agluszak marked this pull request as ready for review July 14, 2020 09:23
@agluszak agluszak marked this pull request as draft July 14, 2020 09:23
@agluszak agluszak marked this pull request as ready for review July 14, 2020 16:42
@agluszak agluszak requested a review from oquenchil July 14, 2020 16:43
agluszak added 3 commits July 20, 2020 14:03
…hared-lib-twice-after-transition

# Conflicts:
#	src/main/java/com/google/devtools/build/lib/rules/cpp/LibrariesToLinkCollector.java
@oquenchil oquenchil self-assigned this Jul 21, 2020
Copy link
Copy Markdown
Contributor

@oquenchil oquenchil left a comment

Choose a reason for hiding this comment

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

LGTM

@bazel-io bazel-io closed this in a90c408 Jul 22, 2020
ehkloaj pushed a commit to ehkloaj/bazel that referenced this pull request Aug 6, 2020
> As more and more people start using the power of Starlark transitions, we are seeing more people bumping into a precondition baked deep in the C++ rules implementation which checks that a shared library being linked is in the same configuration directory. The check is there to make sure that the same library built in different configurations is not linked more than once.
> Aside from the fact that it should be a proper rule error and not a crash, people are hitting this check when writing cc_import targets that have a transition applied to them. There are other ways to guarantee that the same library is not linked more than once without completely restricting the ability to link a library built in a different configuration.
@oquenchil

This PR makes linking the same shared library twice a rule error instead of a crash and it makes it possible to link a library built in a different configuration. A map from library identifiers to library paths is introduced, making sure that each shared library is linked at most once. Only dynamic linking is taken into account, for static linking see bazelbuild#11727

Fixes bazelbuild#11167 (the check which was causing the NPE was not directly related to transitions, but because it was removed, that NPE should no longer happen)

Closes bazelbuild#11721.

PiperOrigin-RevId: 322563028
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.

NPE when using manually created CcInfo with dynamic_library

3 participants