Program: 110872
Project: 110872
Buggy source:
/** 
 * Constructs the C++ linker actions. It generally generates two actions, one for a static library and one for a dynamic library. If PIC is required for shared libraries, but not for binaries, it additionally creates a third action to generate a PIC static library. <p>For dynamic libraries, this method can additionally create an interface shared library that can be used for linking, but doesn't contain any executable code. This increases the number of cache hits for link actions. Call   {@link #setAllowInterfaceSharedObjects(boolean)} to enablethis behavior.
 * @throws RuleErrorException
 */
public CcLinkingOutputs createCcLinkActions(CcCompilationOutputs ccOutputs,Iterable<Artifact> nonCodeLinkerInputs) throws RuleErrorException, InterruptedException {
  Preconditions.checkState(linkType.staticness() == Staticness.STATIC,"String_Node_Str");
  CcLinkingOutputs.Builder result=new CcLinkingOutputs.Builder();
  if (cppConfiguration.isLipoContextCollector()) {
    return result.build();
  }
  AnalysisEnvironment env=ruleContext.getAnalysisEnvironment();
  boolean usePicForBinaries=CppHelper.usePic(ruleContext,true);
  boolean usePicForSharedLibs=CppHelper.usePic(ruleContext,false);
  Artifact linkedArtifact=getLinkedArtifact(linkType);
  PathFragment labelName=new PathFragment(ruleContext.getLabel().getName());
  String libraryIdentifier=ruleContext.getPackageDirectory().getRelative(labelName.replaceName("String_Node_Str" + labelName.getBaseName())).getPathString();
  CppLinkAction maybePicAction=newLinkActionBuilder(linkedArtifact).addObjectFiles(ccOutputs.getObjectFiles(usePicForBinaries)).addNonCodeInputs(ccOutputs.getHeaderTokenFiles()).addNonCodeInputs(nonCodeLinkerInputs).addLTOBitcodeFiles(ccOutputs.getLtoBitcodeFiles()).setLinkType(linkType).setLinkStaticness(LinkStaticness.FULLY_STATIC).addActionInputs(linkActionInputs).setLibraryIdentifier(libraryIdentifier).addVariablesExtensions(variablesExtensions).setFeatureConfiguration(featureConfiguration).build();
  env.registerAction(maybePicAction);
  if (linkType != LinkTargetType.EXECUTABLE) {
    result.addStaticLibrary(maybePicAction.getOutputLibrary());
  }
  if (!usePicForBinaries && usePicForSharedLibs) {
    LinkTargetType picLinkType=(linkType == LinkTargetType.ALWAYS_LINK_STATIC_LIBRARY) ? LinkTargetType.ALWAYS_LINK_PIC_STATIC_LIBRARY : LinkTargetType.PIC_STATIC_LIBRARY;
    Artifact picArtifact=getLinkedArtifact(picLinkType);
    CppLinkAction picAction=newLinkActionBuilder(picArtifact).addObjectFiles(ccOutputs.getObjectFiles(true)).addNonCodeInputs(ccOutputs.getHeaderTokenFiles()).addLTOBitcodeFiles(ccOutputs.getLtoBitcodeFiles()).setLinkType(picLinkType).setLinkStaticness(LinkStaticness.FULLY_STATIC).addActionInputs(linkActionInputs).setLibraryIdentifier(libraryIdentifier).addVariablesExtensions(variablesExtensions).setFeatureConfiguration(featureConfiguration).build();
    env.registerAction(picAction);
    if (linkType != LinkTargetType.EXECUTABLE) {
      result.addPicStaticLibrary(picAction.getOutputLibrary());
    }
  }
  if (!createDynamicLibrary) {
    return result.build();
  }
  Artifact soImpl;
  String mainLibraryIdentifier;
  if (soImplArtifact == null) {
    soImpl=getLinkedArtifact(LinkTargetType.DYNAMIC_LIBRARY);
    mainLibraryIdentifier=libraryIdentifier;
  }
 else {
    soImpl=soImplArtifact;
    mainLibraryIdentifier=FileSystemUtils.removeExtension(soImpl.getRootRelativePath().getPathString());
  }
  List<String> sonameLinkopts=ImmutableList.of();
  Artifact soInterface=null;
  if (cppConfiguration.useInterfaceSharedObjects() && allowInterfaceSharedObjects) {
    soInterface=CppHelper.getLinuxLinkedArtifact(ruleContext,LinkTargetType.INTERFACE_DYNAMIC_LIBRARY);
    sonameLinkopts=ImmutableList.of("String_Node_Str" + SolibSymlinkAction.getDynamicLibrarySoname(soImpl.getRootRelativePath(),false));
  }
  CppLinkActionBuilder linkActionBuilder=newLinkActionBuilder(soImpl).setInterfaceOutput(soInterface).addObjectFiles(ccOutputs.getObjectFiles(usePicForSharedLibs)).addNonCodeInputs(ccOutputs.getHeaderTokenFiles()).addLTOBitcodeFiles(ccOutputs.getLtoBitcodeFiles()).setLinkType(LinkTargetType.DYNAMIC_LIBRARY).setLinkStaticness(LinkStaticness.DYNAMIC).addActionInputs(linkActionInputs).setLibraryIdentifier(mainLibraryIdentifier).addLinkopts(linkopts).addLinkopts(sonameLinkopts).setRuntimeInputs(ArtifactCategory.DYNAMIC_LIBRARY,CppHelper.getToolchain(ruleContext).getDynamicRuntimeLinkMiddleman(),CppHelper.getToolchain(ruleContext).getDynamicRuntimeLinkInputs()).setFeatureConfiguration(featureConfiguration).addVariablesExtensions(variablesExtensions);
  if (!ccOutputs.getLtoBitcodeFiles().isEmpty() && featureConfiguration.isEnabled(CppRuleClasses.THIN_LTO)) {
    linkActionBuilder.setLTOIndexing(true);
    CppLinkAction indexAction=linkActionBuilder.build();
    env.registerAction(indexAction);
    for (    LTOBackendArtifacts ltoArtifacts : indexAction.getAllLTOBackendArtifacts()) {
      ltoArtifacts.scheduleLTOBackendAction(ruleContext,usePicForSharedLibs);
    }
    linkActionBuilder.setLTOIndexing(false);
  }
  CppLinkAction action=linkActionBuilder.build();
  env.registerAction(action);
  if (linkType == LinkTargetType.EXECUTABLE) {
    return result.build();
  }
  LibraryToLink dynamicLibrary=action.getOutputLibrary();
  LibraryToLink interfaceLibrary=action.getInterfaceOutputLibrary();
  if (interfaceLibrary == null) {
    interfaceLibrary=dynamicLibrary;
  }
  if (neverLink) {
    result.addDynamicLibrary(interfaceLibrary);
    result.addExecutionDynamicLibrary(dynamicLibrary);
  }
 else {
    Artifact libraryLink=SolibSymlinkAction.getDynamicLibrarySymlink(ruleContext,interfaceLibrary.getArtifact(),false,false,ruleContext.getConfiguration());
    result.addDynamicLibrary(LinkerInputs.solibLibraryToLink(libraryLink,interfaceLibrary.getArtifact(),libraryIdentifier));
    Artifact implLibraryLink=SolibSymlinkAction.getDynamicLibrarySymlink(ruleContext,dynamicLibrary.getArtifact(),false,false,ruleContext.getConfiguration());
    result.addExecutionDynamicLibrary(LinkerInputs.solibLibraryToLink(implLibraryLink,dynamicLibrary.getArtifact(),libraryIdentifier));
  }
  return result.build();
}

Fixed version (ground truth):
/** 
 * Constructs the C++ linker actions. It generally generates two actions, one for a static library and one for a dynamic library. If PIC is required for shared libraries, but not for binaries, it additionally creates a third action to generate a PIC static library. <p>For dynamic libraries, this method can additionally create an interface shared library that can be used for linking, but doesn't contain any executable code. This increases the number of cache hits for link actions. Call   {@link #setAllowInterfaceSharedObjects(boolean)} to enablethis behavior.
 * @throws RuleErrorException
 */
public CcLinkingOutputs createCcLinkActions(CcCompilationOutputs ccOutputs,Iterable<Artifact> nonCodeLinkerInputs) throws RuleErrorException, InterruptedException {
  Preconditions.checkState(linkType.staticness() == Staticness.STATIC,"String_Node_Str");
  CcLinkingOutputs.Builder result=new CcLinkingOutputs.Builder();
  if (cppConfiguration.isLipoContextCollector()) {
    return result.build();
  }
  AnalysisEnvironment env=ruleContext.getAnalysisEnvironment();
  boolean usePicForBinaries=CppHelper.usePic(ruleContext,true);
  boolean usePicForSharedLibs=CppHelper.usePic(ruleContext,false);
  Artifact linkedArtifact=getLinkedArtifact(linkType);
  PathFragment labelName=new PathFragment(ruleContext.getLabel().getName());
  String libraryIdentifier=ruleContext.getPackageDirectory().getRelative(labelName.replaceName("String_Node_Str" + labelName.getBaseName())).getPathString();
  CppLinkAction maybePicAction=newLinkActionBuilder(linkedArtifact).addObjectFiles(ccOutputs.getObjectFiles(usePicForBinaries)).addNonCodeInputs(nonCodeLinkerInputs).addLTOBitcodeFiles(ccOutputs.getLtoBitcodeFiles()).setLinkType(linkType).setLinkStaticness(LinkStaticness.FULLY_STATIC).addActionInputs(linkActionInputs).setLibraryIdentifier(libraryIdentifier).addVariablesExtensions(variablesExtensions).setFeatureConfiguration(featureConfiguration).build();
  env.registerAction(maybePicAction);
  if (linkType != LinkTargetType.EXECUTABLE) {
    result.addStaticLibrary(maybePicAction.getOutputLibrary());
  }
  if (!usePicForBinaries && usePicForSharedLibs) {
    LinkTargetType picLinkType=(linkType == LinkTargetType.ALWAYS_LINK_STATIC_LIBRARY) ? LinkTargetType.ALWAYS_LINK_PIC_STATIC_LIBRARY : LinkTargetType.PIC_STATIC_LIBRARY;
    Artifact picArtifact=getLinkedArtifact(picLinkType);
    CppLinkAction picAction=newLinkActionBuilder(picArtifact).addObjectFiles(ccOutputs.getObjectFiles(true)).addLTOBitcodeFiles(ccOutputs.getLtoBitcodeFiles()).setLinkType(picLinkType).setLinkStaticness(LinkStaticness.FULLY_STATIC).addActionInputs(linkActionInputs).setLibraryIdentifier(libraryIdentifier).addVariablesExtensions(variablesExtensions).setFeatureConfiguration(featureConfiguration).build();
    env.registerAction(picAction);
    if (linkType != LinkTargetType.EXECUTABLE) {
      result.addPicStaticLibrary(picAction.getOutputLibrary());
    }
  }
  if (!createDynamicLibrary) {
    return result.build();
  }
  Artifact soImpl;
  String mainLibraryIdentifier;
  if (soImplArtifact == null) {
    soImpl=getLinkedArtifact(LinkTargetType.DYNAMIC_LIBRARY);
    mainLibraryIdentifier=libraryIdentifier;
  }
 else {
    soImpl=soImplArtifact;
    mainLibraryIdentifier=FileSystemUtils.removeExtension(soImpl.getRootRelativePath().getPathString());
  }
  List<String> sonameLinkopts=ImmutableList.of();
  Artifact soInterface=null;
  if (cppConfiguration.useInterfaceSharedObjects() && allowInterfaceSharedObjects) {
    soInterface=CppHelper.getLinuxLinkedArtifact(ruleContext,LinkTargetType.INTERFACE_DYNAMIC_LIBRARY);
    sonameLinkopts=ImmutableList.of("String_Node_Str" + SolibSymlinkAction.getDynamicLibrarySoname(soImpl.getRootRelativePath(),false));
  }
  CppLinkActionBuilder linkActionBuilder=newLinkActionBuilder(soImpl).setInterfaceOutput(soInterface).addObjectFiles(ccOutputs.getObjectFiles(usePicForSharedLibs)).addNonCodeInputs(ccOutputs.getHeaderTokenFiles()).addLTOBitcodeFiles(ccOutputs.getLtoBitcodeFiles()).setLinkType(LinkTargetType.DYNAMIC_LIBRARY).setLinkStaticness(LinkStaticness.DYNAMIC).addActionInputs(linkActionInputs).setLibraryIdentifier(mainLibraryIdentifier).addLinkopts(linkopts).addLinkopts(sonameLinkopts).setRuntimeInputs(ArtifactCategory.DYNAMIC_LIBRARY,CppHelper.getToolchain(ruleContext).getDynamicRuntimeLinkMiddleman(),CppHelper.getToolchain(ruleContext).getDynamicRuntimeLinkInputs()).setFeatureConfiguration(featureConfiguration).addVariablesExtensions(variablesExtensions);
  if (!ccOutputs.getLtoBitcodeFiles().isEmpty() && featureConfiguration.isEnabled(CppRuleClasses.THIN_LTO)) {
    linkActionBuilder.setLTOIndexing(true);
    CppLinkAction indexAction=linkActionBuilder.build();
    env.registerAction(indexAction);
    for (    LTOBackendArtifacts ltoArtifacts : indexAction.getAllLTOBackendArtifacts()) {
      ltoArtifacts.scheduleLTOBackendAction(ruleContext,usePicForSharedLibs);
    }
    linkActionBuilder.setLTOIndexing(false);
  }
  CppLinkAction action=linkActionBuilder.build();
  env.registerAction(action);
  if (linkType == LinkTargetType.EXECUTABLE) {
    return result.build();
  }
  LibraryToLink dynamicLibrary=action.getOutputLibrary();
  LibraryToLink interfaceLibrary=action.getInterfaceOutputLibrary();
  if (interfaceLibrary == null) {
    interfaceLibrary=dynamicLibrary;
  }
  if (neverLink) {
    result.addDynamicLibrary(interfaceLibrary);
    result.addExecutionDynamicLibrary(dynamicLibrary);
  }
 else {
    Artifact libraryLink=SolibSymlinkAction.getDynamicLibrarySymlink(ruleContext,interfaceLibrary.getArtifact(),false,false,ruleContext.getConfiguration());
    result.addDynamicLibrary(LinkerInputs.solibLibraryToLink(libraryLink,interfaceLibrary.getArtifact(),libraryIdentifier));
    Artifact implLibraryLink=SolibSymlinkAction.getDynamicLibrarySymlink(ruleContext,dynamicLibrary.getArtifact(),false,false,ruleContext.getConfiguration());
    result.addExecutionDynamicLibrary(LinkerInputs.solibLibraryToLink(implLibraryLink,dynamicLibrary.getArtifact(),libraryIdentifier));
  }
  return result.build();
}