llvm.org GIT mirror llvm / f71f43e
Merging r291858 and r291859: ------------------------------------------------------------------------ r291858 | eugenis | 2017-01-12 15:03:03 -0800 (Thu, 12 Jan 2017) | 1 line [asan] Refactor instrumentation of globals. ------------------------------------------------------------------------ ------------------------------------------------------------------------ r291859 | eugenis | 2017-01-12 15:26:20 -0800 (Thu, 12 Jan 2017) | 6 lines [asan] Don't overalign global metadata. Other than on COFF with incremental linking, global metadata should not need any extra alignment. Differential Revision: https://reviews.llvm.org/D28628 ------------------------------------------------------------------------ git-svn-id: https://llvm.org/svn/llvm-project/llvm/branches/release_40@292802 91177308-0d34-0410-b5e6-96231b3b80d8 Hans Wennborg 3 years ago
2 changed file(s) with 193 addition(s) and 150 deletion(s). Raw diff Collapse all Expand all
599599 void initializeCallbacks(Module &M);
600600
601601 bool InstrumentGlobals(IRBuilder<> &IRB, Module &M);
602 void InstrumentGlobalsCOFF(IRBuilder<> &IRB, Module &M,
603 ArrayRef ExtendedGlobals,
604 ArrayRef MetadataInitializers);
605 void InstrumentGlobalsMachO(IRBuilder<> &IRB, Module &M,
606 ArrayRef ExtendedGlobals,
607 ArrayRef MetadataInitializers);
608 void
609 InstrumentGlobalsWithMetadataArray(IRBuilder<> &IRB, Module &M,
610 ArrayRef ExtendedGlobals,
611 ArrayRef MetadataInitializers);
612
613 GlobalVariable *CreateMetadataGlobal(Module &M, Constant *Initializer,
614 StringRef OriginalName);
615 void SetComdatForGlobalMetadata(GlobalVariable *G, GlobalVariable *Metadata);
616 IRBuilder<> CreateAsanModuleDtor(Module &M);
617
602618 bool ShouldInstrumentGlobal(GlobalVariable *G);
603619 bool ShouldUseMachOGlobalsSection() const;
604620 StringRef getGlobalMetadataSection() const;
15521568
15531569 // Declare the functions that find globals in a shared object and then invoke
15541570 // the (un)register function on them.
1555 AsanRegisterImageGlobals = checkSanitizerInterfaceFunction(
1556 M.getOrInsertFunction(kAsanRegisterImageGlobalsName,
1557 IRB.getVoidTy(), IntptrTy, nullptr));
1571 AsanRegisterImageGlobals =
1572 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
1573 kAsanRegisterImageGlobalsName, IRB.getVoidTy(), IntptrTy, nullptr));
15581574 AsanRegisterImageGlobals->setLinkage(Function::ExternalLinkage);
15591575
1560 AsanUnregisterImageGlobals = checkSanitizerInterfaceFunction(
1561 M.getOrInsertFunction(kAsanUnregisterImageGlobalsName,
1562 IRB.getVoidTy(), IntptrTy, nullptr));
1576 AsanUnregisterImageGlobals =
1577 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
1578 kAsanUnregisterImageGlobalsName, IRB.getVoidTy(), IntptrTy, nullptr));
15631579 AsanUnregisterImageGlobals->setLinkage(Function::ExternalLinkage);
1580 }
1581
1582 // Put the metadata and the instrumented global in the same group. This ensures
1583 // that the metadata is discarded if the instrumented global is discarded.
1584 void AddressSanitizerModule::SetComdatForGlobalMetadata(
1585 GlobalVariable *G, GlobalVariable *Metadata) {
1586 Module &M = *G->getParent();
1587 Comdat *C = G->getComdat();
1588 if (!C) {
1589 if (!G->hasName()) {
1590 // If G is unnamed, it must be internal. Give it an artificial name
1591 // so we can put it in a comdat.
1592 assert(G->hasLocalLinkage());
1593 G->setName(Twine(kAsanGenPrefix) + "_anon_global");
1594 }
1595 C = M.getOrInsertComdat(G->getName());
1596 // Make this IMAGE_COMDAT_SELECT_NODUPLICATES on COFF.
1597 if (TargetTriple.isOSBinFormatCOFF())
1598 C->setSelectionKind(Comdat::NoDuplicates);
1599 G->setComdat(C);
1600 }
1601
1602 assert(G->hasComdat());
1603 Metadata->setComdat(G->getComdat());
1604 }
1605
1606 // Create a separate metadata global and put it in the appropriate ASan
1607 // global registration section.
1608 GlobalVariable *
1609 AddressSanitizerModule::CreateMetadataGlobal(Module &M, Constant *Initializer,
1610 StringRef OriginalName) {
1611 GlobalVariable *Metadata =
1612 new GlobalVariable(M, Initializer->getType(), false,
1613 GlobalVariable::InternalLinkage, Initializer,
1614 Twine("__asan_global_") +
1615 GlobalValue::getRealLinkageName(OriginalName));
1616 Metadata->setSection(getGlobalMetadataSection());
1617 return Metadata;
1618 }
1619
1620 IRBuilder<> AddressSanitizerModule::CreateAsanModuleDtor(Module &M) {
1621 Function *AsanDtorFunction =
1622 Function::Create(FunctionType::get(Type::getVoidTy(*C), false),
1623 GlobalValue::InternalLinkage, kAsanModuleDtorName, &M);
1624 BasicBlock *AsanDtorBB = BasicBlock::Create(*C, "", AsanDtorFunction);
1625 appendToGlobalDtors(M, AsanDtorFunction, kAsanCtorAndDtorPriority);
1626
1627 return IRBuilder<>(ReturnInst::Create(*C, AsanDtorBB));
1628 }
1629
1630 void AddressSanitizerModule::InstrumentGlobalsCOFF(
1631 IRBuilder<> &IRB, Module &M, ArrayRef ExtendedGlobals,
1632 ArrayRef MetadataInitializers) {
1633 assert(ExtendedGlobals.size() == MetadataInitializers.size());
1634 auto &DL = M.getDataLayout();
1635
1636 for (size_t i = 0; i < ExtendedGlobals.size(); i++) {
1637 Constant *Initializer = MetadataInitializers[i];
1638 GlobalVariable *G = ExtendedGlobals[i];
1639 GlobalVariable *Metadata =
1640 CreateMetadataGlobal(M, Initializer, G->getName());
1641
1642 // The MSVC linker always inserts padding when linking incrementally. We
1643 // cope with that by aligning each struct to its size, which must be a power
1644 // of two.
1645 unsigned SizeOfGlobalStruct = DL.getTypeAllocSize(Initializer->getType());
1646 assert(isPowerOf2_32(SizeOfGlobalStruct) &&
1647 "global metadata will not be padded appropriately");
1648 Metadata->setAlignment(SizeOfGlobalStruct);
1649
1650 SetComdatForGlobalMetadata(G, Metadata);
1651 }
1652 }
1653
1654 void AddressSanitizerModule::InstrumentGlobalsMachO(
1655 IRBuilder<> &IRB, Module &M, ArrayRef ExtendedGlobals,
1656 ArrayRef MetadataInitializers) {
1657 assert(ExtendedGlobals.size() == MetadataInitializers.size());
1658
1659 // On recent Mach-O platforms, use a structure which binds the liveness of
1660 // the global variable to the metadata struct. Keep the list of "Liveness" GV
1661 // created to be added to llvm.compiler.used
1662 StructType *LivenessTy = StructType::get(IntptrTy, IntptrTy, nullptr);
1663 SmallVector LivenessGlobals(ExtendedGlobals.size());
1664
1665 for (size_t i = 0; i < ExtendedGlobals.size(); i++) {
1666 Constant *Initializer = MetadataInitializers[i];
1667 GlobalVariable *G = ExtendedGlobals[i];
1668 GlobalVariable *Metadata =
1669 CreateMetadataGlobal(M, Initializer, G->getName());
1670
1671 // On recent Mach-O platforms, we emit the global metadata in a way that
1672 // allows the linker to properly strip dead globals.
1673 auto LivenessBinder = ConstantStruct::get(
1674 LivenessTy, Initializer->getAggregateElement(0u),
1675 ConstantExpr::getPointerCast(Metadata, IntptrTy), nullptr);
1676 GlobalVariable *Liveness = new GlobalVariable(
1677 M, LivenessTy, false, GlobalVariable::InternalLinkage, LivenessBinder,
1678 Twine("__asan_binder_") + G->getName());
1679 Liveness->setSection("__DATA,__asan_liveness,regular,live_support");
1680 LivenessGlobals[i] = Liveness;
1681 }
1682
1683 // Update llvm.compiler.used, adding the new liveness globals. This is
1684 // needed so that during LTO these variables stay alive. The alternative
1685 // would be to have the linker handling the LTO symbols, but libLTO
1686 // current API does not expose access to the section for each symbol.
1687 if (!LivenessGlobals.empty())
1688 appendToCompilerUsed(M, LivenessGlobals);
1689
1690 // RegisteredFlag serves two purposes. First, we can pass it to dladdr()
1691 // to look up the loaded image that contains it. Second, we can store in it
1692 // whether registration has already occurred, to prevent duplicate
1693 // registration.
1694 //
1695 // common linkage ensures that there is only one global per shared library.
1696 GlobalVariable *RegisteredFlag = new GlobalVariable(
1697 M, IntptrTy, false, GlobalVariable::CommonLinkage,
1698 ConstantInt::get(IntptrTy, 0), kAsanGlobalsRegisteredFlagName);
1699 RegisteredFlag->setVisibility(GlobalVariable::HiddenVisibility);
1700
1701 IRB.CreateCall(AsanRegisterImageGlobals,
1702 {IRB.CreatePointerCast(RegisteredFlag, IntptrTy)});
1703
1704 // We also need to unregister globals at the end, e.g., when a shared library
1705 // gets closed.
1706 IRBuilder<> IRB_Dtor = CreateAsanModuleDtor(M);
1707 IRB_Dtor.CreateCall(AsanUnregisterImageGlobals,
1708 {IRB.CreatePointerCast(RegisteredFlag, IntptrTy)});
1709 }
1710
1711 void AddressSanitizerModule::InstrumentGlobalsWithMetadataArray(
1712 IRBuilder<> &IRB, Module &M, ArrayRef ExtendedGlobals,
1713 ArrayRef MetadataInitializers) {
1714 assert(ExtendedGlobals.size() == MetadataInitializers.size());
1715 unsigned N = ExtendedGlobals.size();
1716 assert(N > 0);
1717
1718 // On platforms that don't have a custom metadata section, we emit an array
1719 // of global metadata structures.
1720 ArrayType *ArrayOfGlobalStructTy =
1721 ArrayType::get(MetadataInitializers[0]->getType(), N);
1722 auto AllGlobals = new GlobalVariable(
1723 M, ArrayOfGlobalStructTy, false, GlobalVariable::InternalLinkage,
1724 ConstantArray::get(ArrayOfGlobalStructTy, MetadataInitializers), "");
1725
1726 IRB.CreateCall(AsanRegisterGlobals,
1727 {IRB.CreatePointerCast(AllGlobals, IntptrTy),
1728 ConstantInt::get(IntptrTy, N)});
1729
1730 // We also need to unregister globals at the end, e.g., when a shared library
1731 // gets closed.
1732 IRBuilder<> IRB_Dtor = CreateAsanModuleDtor(M);
1733 IRB_Dtor.CreateCall(AsanUnregisterGlobals,
1734 {IRB.CreatePointerCast(AllGlobals, IntptrTy),
1735 ConstantInt::get(IntptrTy, N)});
15641736 }
15651737
15661738 // This function replaces all global variables with new variables that have
15791751 if (n == 0) return false;
15801752
15811753 auto &DL = M.getDataLayout();
1582 bool UseComdatMetadata = TargetTriple.isOSBinFormatCOFF();
1583 bool UseMachOGlobalsSection = ShouldUseMachOGlobalsSection();
1584 bool UseMetadataArray = !(UseComdatMetadata || UseMachOGlobalsSection);
15851754
15861755 // A global is described by a structure
15871756 // size_t beg;
15961765 StructType *GlobalStructTy =
15971766 StructType::get(IntptrTy, IntptrTy, IntptrTy, IntptrTy, IntptrTy,
15981767 IntptrTy, IntptrTy, IntptrTy, nullptr);
1599 unsigned SizeOfGlobalStruct = DL.getTypeAllocSize(GlobalStructTy);
1600 assert(isPowerOf2_32(SizeOfGlobalStruct) &&
1601 "global metadata will not be padded appropriately");
1602 SmallVector Initializers(UseMetadataArray ? n : 0);
1603
1604 // On recent Mach-O platforms, use a structure which binds the liveness of
1605 // the global variable to the metadata struct. Keep the list of "Liveness" GV
1606 // created to be added to llvm.compiler.used
1607 StructType *LivenessTy = nullptr;
1608 if (UseMachOGlobalsSection)
1609 LivenessTy = StructType::get(IntptrTy, IntptrTy, nullptr);
1610 SmallVector LivenessGlobals(
1611 UseMachOGlobalsSection ? n : 0);
1768 SmallVector NewGlobals(n);
1769 SmallVector Initializers(n);
16121770
16131771 bool HasDynamicallyInitializedGlobals = false;
16141772
16801838 ConstantExpr::getGetElementPtr(NewTy, NewGlobal, Indices2, true));
16811839 NewGlobal->takeName(G);
16821840 G->eraseFromParent();
1683 G = NewGlobal;
1684
1685 if (UseComdatMetadata) {
1686 // Get or create a COMDAT for G so that we can use it with our metadata.
1687 Comdat *C = G->getComdat();
1688 if (!C) {
1689 if (!G->hasName()) {
1690 // If G is unnamed, it must be internal. Give it an artificial name
1691 // so we can put it in a comdat.
1692 assert(G->hasLocalLinkage());
1693 G->setName(Twine(kAsanGenPrefix) + "_anon_global");
1694 }
1695 C = M.getOrInsertComdat(G->getName());
1696 // Make this IMAGE_COMDAT_SELECT_NODUPLICATES on COFF.
1697 if (TargetTriple.isOSBinFormatCOFF())
1698 C->setSelectionKind(Comdat::NoDuplicates);
1699 G->setComdat(C);
1700 }
1701 }
1841 NewGlobals[i] = NewGlobal;
17021842
17031843 Constant *SourceLoc;
17041844 if (!MD.SourceLoc.empty()) {
17491889
17501890 DEBUG(dbgs() << "NEW GLOBAL: " << *NewGlobal << "\n");
17511891
1752 // If we aren't using separate metadata globals, add it to the initializer
1753 // list and continue.
1754 if (UseMetadataArray) {
1755 Initializers[i] = Initializer;
1756 continue;
1757 }
1758
1759 // Create a separate metadata global and put it in the appropriate ASan
1760 // global registration section.
1761 GlobalVariable *Metadata = new GlobalVariable(
1762 M, GlobalStructTy, false, GlobalVariable::InternalLinkage,
1763 Initializer, Twine("__asan_global_") +
1764 GlobalValue::getRealLinkageName(G->getName()));
1765 Metadata->setSection(getGlobalMetadataSection());
1766
1767 // We don't want any padding, but we also need a reasonable alignment.
1768 // The MSVC linker always inserts padding when linking incrementally. We
1769 // cope with that by aligning each struct to its size, which must be a power
1770 // of two.
1771 Metadata->setAlignment(SizeOfGlobalStruct);
1772
1773 // On platforms that support comdats, put the metadata and the
1774 // instrumented global in the same group. This ensures that the metadata
1775 // is discarded if the instrumented global is discarded.
1776 if (UseComdatMetadata) {
1777 assert(G->hasComdat());
1778 Metadata->setComdat(G->getComdat());
1779 continue;
1780 }
1781 assert(UseMachOGlobalsSection);
1782
1783 // On recent Mach-O platforms, we emit the global metadata in a way that
1784 // allows the linker to properly strip dead globals.
1785 auto LivenessBinder = ConstantStruct::get(
1786 LivenessTy, Initializer->getAggregateElement(0u),
1787 ConstantExpr::getPointerCast(Metadata, IntptrTy), nullptr);
1788 GlobalVariable *Liveness = new GlobalVariable(
1789 M, LivenessTy, false, GlobalVariable::InternalLinkage, LivenessBinder,
1790 Twine("__asan_binder_") + G->getName());
1791 Liveness->setSection("__DATA,__asan_liveness,regular,live_support");
1792 LivenessGlobals[i] = Liveness;
1892 Initializers[i] = Initializer;
1893 }
1894
1895 if (TargetTriple.isOSBinFormatCOFF()) {
1896 InstrumentGlobalsCOFF(IRB, M, NewGlobals, Initializers);
1897 } else if (ShouldUseMachOGlobalsSection()) {
1898 InstrumentGlobalsMachO(IRB, M, NewGlobals, Initializers);
1899 } else {
1900 InstrumentGlobalsWithMetadataArray(IRB, M, NewGlobals, Initializers);
17931901 }
17941902
17951903 // Create calls for poisoning before initializers run and unpoisoning after.
17961904 if (HasDynamicallyInitializedGlobals)
17971905 createInitializerPoisonCalls(M, ModuleName);
1798
1799 // Platforms with a dedicated metadata section don't need to emit any more
1800 // code.
1801 if (UseComdatMetadata)
1802 return true;
1803
1804 GlobalVariable *AllGlobals = nullptr;
1805 GlobalVariable *RegisteredFlag = nullptr;
1806
1807 if (UseMachOGlobalsSection) {
1808 // RegisteredFlag serves two purposes. First, we can pass it to dladdr()
1809 // to look up the loaded image that contains it. Second, we can store in it
1810 // whether registration has already occurred, to prevent duplicate
1811 // registration.
1812 //
1813 // common linkage ensures that there is only one global per shared library.
1814 RegisteredFlag = new GlobalVariable(
1815 M, IntptrTy, false, GlobalVariable::CommonLinkage,
1816 ConstantInt::get(IntptrTy, 0), kAsanGlobalsRegisteredFlagName);
1817 RegisteredFlag->setVisibility(GlobalVariable::HiddenVisibility);
1818
1819 // Update llvm.compiler.used, adding the new liveness globals. This is
1820 // needed so that during LTO these variables stay alive. The alternative
1821 // would be to have the linker handling the LTO symbols, but libLTO
1822 // current API does not expose access to the section for each symbol.
1823 if (!LivenessGlobals.empty())
1824 appendToCompilerUsed(M, LivenessGlobals);
1825 } else if (UseMetadataArray) {
1826 // On platforms that don't have a custom metadata section, we emit an array
1827 // of global metadata structures.
1828 ArrayType *ArrayOfGlobalStructTy = ArrayType::get(GlobalStructTy, n);
1829 AllGlobals = new GlobalVariable(
1830 M, ArrayOfGlobalStructTy, false, GlobalVariable::InternalLinkage,
1831 ConstantArray::get(ArrayOfGlobalStructTy, Initializers), "");
1832 }
1833
1834 // Create a call to register the globals with the runtime.
1835 if (UseMachOGlobalsSection) {
1836 IRB.CreateCall(AsanRegisterImageGlobals,
1837 {IRB.CreatePointerCast(RegisteredFlag, IntptrTy)});
1838 } else {
1839 IRB.CreateCall(AsanRegisterGlobals,
1840 {IRB.CreatePointerCast(AllGlobals, IntptrTy),
1841 ConstantInt::get(IntptrTy, n)});
1842 }
1843
1844 // We also need to unregister globals at the end, e.g., when a shared library
1845 // gets closed.
1846 Function *AsanDtorFunction =
1847 Function::Create(FunctionType::get(Type::getVoidTy(*C), false),
1848 GlobalValue::InternalLinkage, kAsanModuleDtorName, &M);
1849 BasicBlock *AsanDtorBB = BasicBlock::Create(*C, "", AsanDtorFunction);
1850 IRBuilder<> IRB_Dtor(ReturnInst::Create(*C, AsanDtorBB));
1851
1852 if (UseMachOGlobalsSection) {
1853 IRB_Dtor.CreateCall(AsanUnregisterImageGlobals,
1854 {IRB.CreatePointerCast(RegisteredFlag, IntptrTy)});
1855 } else {
1856 IRB_Dtor.CreateCall(AsanUnregisterGlobals,
1857 {IRB.CreatePointerCast(AllGlobals, IntptrTy),
1858 ConstantInt::get(IntptrTy, n)});
1859 }
1860
1861 appendToGlobalDtors(M, AsanDtorFunction, kAsanCtorAndDtorPriority);
18621906
18631907 DEBUG(dbgs() << M);
18641908 return true;
1515
1616
1717 ; Find the metadata for @global:
18 ; CHECK: [[METADATA:@.+]] = internal global {{.*}} @global {{.*}} section "__DATA,__asan_globals,regular", align 64
18 ; CHECK: [[METADATA:@.+]] = internal global {{.*}} @global {{.*}} section "__DATA,__asan_globals,regular"
1919
2020 ; Find the liveness binder for @global and its metadata:
2121 ; CHECK: @__asan_binder_global = internal global {{.*}} @global {{.*}} [[METADATA]] {{.*}} section "__DATA,__asan_liveness,regular,live_support"
22
23 ; Test that there is the flag global variable:
24 ; CHECK: @__asan_globals_registered = common hidden global i64 0
2522
2623 ; The binder has to be inserted to llvm.compiler.used to avoid being stripped
2724 ; during LTO.
2825 ; CHECK: @llvm.compiler.used {{.*}} @__asan_binder_global {{.*}} section "llvm.metadata"
2926
27 ; Test that there is the flag global variable:
28 ; CHECK: @__asan_globals_registered = common hidden global i64 0
3029
3130 ; Test that __asan_register_image_globals is invoked from the constructor:
3231 ; CHECK-LABEL: define internal void @asan.module_ctor