[llvm] r239761 - Protection against stack-based memory corruption errors using SafeStack
Pete Cooper
peter_cooper at apple.com
Mon Jun 15 14:31:28 PDT 2015
Hey Peter
You probably want to add your attribute to include/llvm-c/Core.h at the bottom of the LLVMAttribute enum.
I know this means just adding it to a commented out FIXME, but probably best to have it there for completeness.
Also, as safestack is only allowed on function attributes, can you please add a test which verifies we throw errors at parsing time if it is attached to either a return or parameter attribute.
Cheers,
Pete
> On Jun 15, 2015, at 2:07 PM, Peter Collingbourne <peter at pcc.me.uk> wrote:
>
> Author: pcc
> Date: Mon Jun 15 16:07:11 2015
> New Revision: 239761
>
> URL: http://llvm.org/viewvc/llvm-project?rev=239761&view=rev
> Log:
> Protection against stack-based memory corruption errors using SafeStack
>
> This patch adds the safe stack instrumentation pass to LLVM, which separates
> the program stack into a safe stack, which stores return addresses, register
> spills, and local variables that are statically verified to be accessed
> in a safe way, and the unsafe stack, which stores everything else. Such
> separation makes it much harder for an attacker to corrupt objects on the
> safe stack, including function pointers stored in spilled registers and
> return addresses. You can find more information about the safe stack, as
> well as other parts of or control-flow hijack protection technique in our
> OSDI paper on code-pointer integrity (http://dslab.epfl.ch/pubs/cpi.pdf)
> and our project website (http://levee.epfl.ch).
>
> The overhead of our implementation of the safe stack is very close to zero
> (0.01% on the Phoronix benchmarks). This is lower than the overhead of
> stack cookies, which are supported by LLVM and are commonly used today,
> yet the security guarantees of the safe stack are strictly stronger than
> stack cookies. In some cases, the safe stack improves performance due to
> better cache locality.
>
> Our current implementation of the safe stack is stable and robust, we
> used it to recompile multiple projects on Linux including Chromium, and
> we also recompiled the entire FreeBSD user-space system and more than 100
> packages. We ran unit tests on the FreeBSD system and many of the packages
> and observed no errors caused by the safe stack. The safe stack is also fully
> binary compatible with non-instrumented code and can be applied to parts of
> a program selectively.
>
> This patch is our implementation of the safe stack on top of LLVM. The
> patches make the following changes:
>
> - Add the safestack function attribute, similar to the ssp, sspstrong and
> sspreq attributes.
>
> - Add the SafeStack instrumentation pass that applies the safe stack to all
> functions that have the safestack attribute. This pass moves all unsafe local
> variables to the unsafe stack with a separate stack pointer, whereas all
> safe variables remain on the regular stack that is managed by LLVM as usual.
>
> - Invoke the pass as the last stage before code generation (at the same time
> the existing cookie-based stack protector pass is invoked).
>
> - Add unit tests for the safe stack.
>
> Original patch by Volodymyr Kuznetsov and others at the Dependable Systems
> Lab at EPFL; updates and upstreaming by myself.
>
> Differential Revision: http://reviews.llvm.org/D6094
>
> Added:
> llvm/trunk/lib/Transforms/Instrumentation/SafeStack.cpp
> llvm/trunk/test/Transforms/SafeStack/
> llvm/trunk/test/Transforms/SafeStack/addr-taken.ll
> llvm/trunk/test/Transforms/SafeStack/array-aligned.ll
> llvm/trunk/test/Transforms/SafeStack/array.ll
> llvm/trunk/test/Transforms/SafeStack/call.ll
> llvm/trunk/test/Transforms/SafeStack/cast.ll
> llvm/trunk/test/Transforms/SafeStack/constant-gep-call.ll
> llvm/trunk/test/Transforms/SafeStack/constant-gep.ll
> llvm/trunk/test/Transforms/SafeStack/constant-geps.ll
> llvm/trunk/test/Transforms/SafeStack/dynamic-alloca.ll
> llvm/trunk/test/Transforms/SafeStack/escape-addr-pointer.ll
> llvm/trunk/test/Transforms/SafeStack/escape-bitcast-store.ll
> llvm/trunk/test/Transforms/SafeStack/escape-bitcast-store2.ll
> llvm/trunk/test/Transforms/SafeStack/escape-call.ll
> llvm/trunk/test/Transforms/SafeStack/escape-casted-pointer.ll
> llvm/trunk/test/Transforms/SafeStack/escape-gep-call.ll
> llvm/trunk/test/Transforms/SafeStack/escape-gep-invoke.ll
> llvm/trunk/test/Transforms/SafeStack/escape-gep-negative.ll
> llvm/trunk/test/Transforms/SafeStack/escape-gep-ptrtoint.ll
> llvm/trunk/test/Transforms/SafeStack/escape-gep-store.ll
> llvm/trunk/test/Transforms/SafeStack/escape-phi-call.ll
> llvm/trunk/test/Transforms/SafeStack/escape-select-call.ll
> llvm/trunk/test/Transforms/SafeStack/escape-vector.ll
> llvm/trunk/test/Transforms/SafeStack/invoke.ll
> llvm/trunk/test/Transforms/SafeStack/no-attr.ll
> llvm/trunk/test/Transforms/SafeStack/phi-cycle.ll
> llvm/trunk/test/Transforms/SafeStack/setjmp.ll
> llvm/trunk/test/Transforms/SafeStack/setjmp2.ll
> llvm/trunk/test/Transforms/SafeStack/struct.ll
> Modified:
> llvm/trunk/docs/LangRef.rst
> llvm/trunk/include/llvm/Bitcode/LLVMBitCodes.h
> llvm/trunk/include/llvm/IR/Attributes.h
> llvm/trunk/include/llvm/InitializePasses.h
> llvm/trunk/include/llvm/LinkAllPasses.h
> llvm/trunk/include/llvm/Transforms/Instrumentation.h
> llvm/trunk/lib/AsmParser/LLLexer.cpp
> llvm/trunk/lib/AsmParser/LLParser.cpp
> llvm/trunk/lib/AsmParser/LLToken.h
> llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp
> llvm/trunk/lib/Bitcode/Writer/BitcodeWriter.cpp
> llvm/trunk/lib/CodeGen/LLVMBuild.txt
> llvm/trunk/lib/CodeGen/Passes.cpp
> llvm/trunk/lib/IR/Attributes.cpp
> llvm/trunk/lib/IR/Verifier.cpp
> llvm/trunk/lib/Target/CppBackend/CPPBackend.cpp
> llvm/trunk/lib/Transforms/IPO/Inliner.cpp
> llvm/trunk/lib/Transforms/Instrumentation/CMakeLists.txt
> llvm/trunk/lib/Transforms/Instrumentation/Instrumentation.cpp
>
> Modified: llvm/trunk/docs/LangRef.rst
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/docs/LangRef.rst?rev=239761&r1=239760&r2=239761&view=diff
> ==============================================================================
> --- llvm/trunk/docs/LangRef.rst (original)
> +++ llvm/trunk/docs/LangRef.rst Mon Jun 15 16:07:11 2015
> @@ -1319,6 +1319,15 @@ example:
> ``setjmp`` is an example of such a function. The compiler disables
> some optimizations (like tail calls) in the caller of these
> functions.
> +``safestack``
> + This attribute indicates that
> + `SafeStack <http://clang.llvm.org/docs/SafeStack.html>`_
> + protection is enabled for this function.
> +
> + If a function that has a ``safestack`` attribute is inlined into a
> + function that doesn't have a ``safestack`` attribute or which has an
> + ``ssp``, ``sspstrong`` or ``sspreq`` attribute, then the resulting
> + function will have a ``safestack`` attribute.
> ``sanitize_address``
> This attribute indicates that AddressSanitizer checks
> (dynamic address safety analysis) are enabled for this function.
>
> Modified: llvm/trunk/include/llvm/Bitcode/LLVMBitCodes.h
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Bitcode/LLVMBitCodes.h?rev=239761&r1=239760&r2=239761&view=diff
> ==============================================================================
> --- llvm/trunk/include/llvm/Bitcode/LLVMBitCodes.h (original)
> +++ llvm/trunk/include/llvm/Bitcode/LLVMBitCodes.h Mon Jun 15 16:07:11 2015
> @@ -403,7 +403,8 @@ namespace bitc {
> ATTR_KIND_JUMP_TABLE = 40,
> ATTR_KIND_DEREFERENCEABLE = 41,
> ATTR_KIND_DEREFERENCEABLE_OR_NULL = 42,
> - ATTR_KIND_CONVERGENT = 43
> + ATTR_KIND_CONVERGENT = 43,
> + ATTR_KIND_SAFESTACK = 44,
> };
>
> enum ComdatSelectionKindCodes {
>
> Modified: llvm/trunk/include/llvm/IR/Attributes.h
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/IR/Attributes.h?rev=239761&r1=239760&r2=239761&view=diff
> ==============================================================================
> --- llvm/trunk/include/llvm/IR/Attributes.h (original)
> +++ llvm/trunk/include/llvm/IR/Attributes.h Mon Jun 15 16:07:11 2015
> @@ -108,6 +108,7 @@ public:
> StackProtect, ///< Stack protection.
> StackProtectReq, ///< Stack protection required.
> StackProtectStrong, ///< Strong Stack protection.
> + SafeStack, ///< Safe Stack protection.
> StructRet, ///< Hidden pointer to structure to return
> SanitizeAddress, ///< AddressSanitizer is on.
> SanitizeThread, ///< ThreadSanitizer is on.
>
> Modified: llvm/trunk/include/llvm/InitializePasses.h
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/InitializePasses.h?rev=239761&r1=239760&r2=239761&view=diff
> ==============================================================================
> --- llvm/trunk/include/llvm/InitializePasses.h (original)
> +++ llvm/trunk/include/llvm/InitializePasses.h Mon Jun 15 16:07:11 2015
> @@ -242,6 +242,7 @@ void initializeRegionOnlyViewerPass(Pass
> void initializeRegionPrinterPass(PassRegistry&);
> void initializeRegionViewerPass(PassRegistry&);
> void initializeRewriteStatepointsForGCPass(PassRegistry&);
> +void initializeSafeStackPass(PassRegistry&);
> void initializeSCCPPass(PassRegistry&);
> void initializeSROAPass(PassRegistry&);
> void initializeSROA_DTPass(PassRegistry&);
>
> Modified: llvm/trunk/include/llvm/LinkAllPasses.h
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/LinkAllPasses.h?rev=239761&r1=239760&r2=239761&view=diff
> ==============================================================================
> --- llvm/trunk/include/llvm/LinkAllPasses.h (original)
> +++ llvm/trunk/include/llvm/LinkAllPasses.h Mon Jun 15 16:07:11 2015
> @@ -131,6 +131,7 @@ namespace {
> (void) llvm::createRegionPrinterPass();
> (void) llvm::createRegionViewerPass();
> (void) llvm::createSCCPPass();
> + (void) llvm::createSafeStackPass();
> (void) llvm::createScalarReplAggregatesPass();
> (void) llvm::createSingleLoopExtractorPass();
> (void) llvm::createStripSymbolsPass();
>
> Modified: llvm/trunk/include/llvm/Transforms/Instrumentation.h
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Transforms/Instrumentation.h?rev=239761&r1=239760&r2=239761&view=diff
> ==============================================================================
> --- llvm/trunk/include/llvm/Transforms/Instrumentation.h (original)
> +++ llvm/trunk/include/llvm/Transforms/Instrumentation.h Mon Jun 15 16:07:11 2015
> @@ -132,6 +132,10 @@ inline ModulePass *createDataFlowSanitiz
> // checking on loads, stores, and other memory intrinsics.
> FunctionPass *createBoundsCheckingPass();
>
> +/// \brief This pass splits the stack into a safe stack and an unsafe stack to
> +/// protect against stack-based overflow vulnerabilities.
> +FunctionPass *createSafeStackPass();
> +
> } // End llvm namespace
>
> #endif
>
> Modified: llvm/trunk/lib/AsmParser/LLLexer.cpp
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/AsmParser/LLLexer.cpp?rev=239761&r1=239760&r2=239761&view=diff
> ==============================================================================
> --- llvm/trunk/lib/AsmParser/LLLexer.cpp (original)
> +++ llvm/trunk/lib/AsmParser/LLLexer.cpp Mon Jun 15 16:07:11 2015
> @@ -628,6 +628,7 @@ lltok::Kind LLLexer::LexIdentifier() {
> KEYWORD(ssp);
> KEYWORD(sspreq);
> KEYWORD(sspstrong);
> + KEYWORD(safestack);
> KEYWORD(sanitize_address);
> KEYWORD(sanitize_thread);
> KEYWORD(sanitize_memory);
>
> Modified: llvm/trunk/lib/AsmParser/LLParser.cpp
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/AsmParser/LLParser.cpp?rev=239761&r1=239760&r2=239761&view=diff
> ==============================================================================
> --- llvm/trunk/lib/AsmParser/LLParser.cpp (original)
> +++ llvm/trunk/lib/AsmParser/LLParser.cpp Mon Jun 15 16:07:11 2015
> @@ -958,6 +958,7 @@ bool LLParser::ParseFnAttributeValuePair
> case lltok::kw_ssp: B.addAttribute(Attribute::StackProtect); break;
> case lltok::kw_sspreq: B.addAttribute(Attribute::StackProtectReq); break;
> case lltok::kw_sspstrong: B.addAttribute(Attribute::StackProtectStrong); break;
> + case lltok::kw_safestack: B.addAttribute(Attribute::SafeStack); break;
> case lltok::kw_sanitize_address: B.addAttribute(Attribute::SanitizeAddress); break;
> case lltok::kw_sanitize_thread: B.addAttribute(Attribute::SanitizeThread); break;
> case lltok::kw_sanitize_memory: B.addAttribute(Attribute::SanitizeMemory); break;
> @@ -1267,6 +1268,7 @@ bool LLParser::ParseOptionalParamAttrs(A
> case lltok::kw_ssp:
> case lltok::kw_sspreq:
> case lltok::kw_sspstrong:
> + case lltok::kw_safestack:
> case lltok::kw_uwtable:
> HaveError |= Error(Lex.getLoc(), "invalid use of function-only attribute");
> break;
> @@ -1343,6 +1345,7 @@ bool LLParser::ParseOptionalReturnAttrs(
> case lltok::kw_ssp:
> case lltok::kw_sspreq:
> case lltok::kw_sspstrong:
> + case lltok::kw_safestack:
> case lltok::kw_uwtable:
> HaveError |= Error(Lex.getLoc(), "invalid use of function-only attribute");
> break;
>
> Modified: llvm/trunk/lib/AsmParser/LLToken.h
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/AsmParser/LLToken.h?rev=239761&r1=239760&r2=239761&view=diff
> ==============================================================================
> --- llvm/trunk/lib/AsmParser/LLToken.h (original)
> +++ llvm/trunk/lib/AsmParser/LLToken.h Mon Jun 15 16:07:11 2015
> @@ -135,6 +135,7 @@ namespace lltok {
> kw_ssp,
> kw_sspreq,
> kw_sspstrong,
> + kw_safestack,
> kw_sret,
> kw_sanitize_thread,
> kw_sanitize_memory,
>
> Modified: llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp?rev=239761&r1=239760&r2=239761&view=diff
> ==============================================================================
> --- llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp (original)
> +++ llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp Mon Jun 15 16:07:11 2015
> @@ -1146,6 +1146,8 @@ static Attribute::AttrKind getAttrFromCo
> return Attribute::StackProtectReq;
> case bitc::ATTR_KIND_STACK_PROTECT_STRONG:
> return Attribute::StackProtectStrong;
> + case bitc::ATTR_KIND_SAFESTACK:
> + return Attribute::SafeStack;
> case bitc::ATTR_KIND_STRUCT_RET:
> return Attribute::StructRet;
> case bitc::ATTR_KIND_SANITIZE_ADDRESS:
>
> Modified: llvm/trunk/lib/Bitcode/Writer/BitcodeWriter.cpp
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Bitcode/Writer/BitcodeWriter.cpp?rev=239761&r1=239760&r2=239761&view=diff
> ==============================================================================
> --- llvm/trunk/lib/Bitcode/Writer/BitcodeWriter.cpp (original)
> +++ llvm/trunk/lib/Bitcode/Writer/BitcodeWriter.cpp Mon Jun 15 16:07:11 2015
> @@ -232,6 +232,8 @@ static uint64_t getAttrKindEncoding(Attr
> return bitc::ATTR_KIND_STACK_PROTECT_REQ;
> case Attribute::StackProtectStrong:
> return bitc::ATTR_KIND_STACK_PROTECT_STRONG;
> + case Attribute::SafeStack:
> + return bitc::ATTR_KIND_SAFESTACK;
> case Attribute::StructRet:
> return bitc::ATTR_KIND_STRUCT_RET;
> case Attribute::SanitizeAddress:
>
> Modified: llvm/trunk/lib/CodeGen/LLVMBuild.txt
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/LLVMBuild.txt?rev=239761&r1=239760&r2=239761&view=diff
> ==============================================================================
> --- llvm/trunk/lib/CodeGen/LLVMBuild.txt (original)
> +++ llvm/trunk/lib/CodeGen/LLVMBuild.txt Mon Jun 15 16:07:11 2015
> @@ -22,4 +22,4 @@ subdirectories = AsmPrinter SelectionDAG
> type = Library
> name = CodeGen
> parent = Libraries
> -required_libraries = Analysis Core MC Scalar Support Target TransformUtils
> +required_libraries = Analysis Core Instrumentation MC Scalar Support Target TransformUtils
>
> Modified: llvm/trunk/lib/CodeGen/Passes.cpp
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/Passes.cpp?rev=239761&r1=239760&r2=239761&view=diff
> ==============================================================================
> --- llvm/trunk/lib/CodeGen/Passes.cpp (original)
> +++ llvm/trunk/lib/CodeGen/Passes.cpp Mon Jun 15 16:07:11 2015
> @@ -24,6 +24,7 @@
> #include "llvm/Support/Debug.h"
> #include "llvm/Support/ErrorHandling.h"
> #include "llvm/Support/raw_ostream.h"
> +#include "llvm/Transforms/Instrumentation.h"
> #include "llvm/Transforms/Scalar.h"
> #include "llvm/Transforms/Utils/SymbolRewriter.h"
>
> @@ -456,6 +457,9 @@ void TargetPassConfig::addCodeGenPrepare
> void TargetPassConfig::addISelPrepare() {
> addPreISel();
>
> + // Add both the safe stack and the stack protection passes: each of them will
> + // only protect functions that have corresponding attributes.
> + addPass(createSafeStackPass());
> addPass(createStackProtectorPass(TM));
>
> if (PrintISelInput)
>
> Modified: llvm/trunk/lib/IR/Attributes.cpp
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/IR/Attributes.cpp?rev=239761&r1=239760&r2=239761&view=diff
> ==============================================================================
> --- llvm/trunk/lib/IR/Attributes.cpp (original)
> +++ llvm/trunk/lib/IR/Attributes.cpp Mon Jun 15 16:07:11 2015
> @@ -252,6 +252,8 @@ std::string Attribute::getAsString(bool
> return "sspreq";
> if (hasAttribute(Attribute::StackProtectStrong))
> return "sspstrong";
> + if (hasAttribute(Attribute::SafeStack))
> + return "safestack";
> if (hasAttribute(Attribute::StructRet))
> return "sret";
> if (hasAttribute(Attribute::SanitizeThread))
> @@ -437,6 +439,7 @@ uint64_t AttributeImpl::getAttrMask(Attr
> case Attribute::NonNull: return 1ULL << 44;
> case Attribute::JumpTable: return 1ULL << 45;
> case Attribute::Convergent: return 1ULL << 46;
> + case Attribute::SafeStack: return 1ULL << 47;
> case Attribute::Dereferenceable:
> llvm_unreachable("dereferenceable attribute not supported in raw format");
> break;
>
> Modified: llvm/trunk/lib/IR/Verifier.cpp
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/IR/Verifier.cpp?rev=239761&r1=239760&r2=239761&view=diff
> ==============================================================================
> --- llvm/trunk/lib/IR/Verifier.cpp (original)
> +++ llvm/trunk/lib/IR/Verifier.cpp Mon Jun 15 16:07:11 2015
> @@ -1251,6 +1251,7 @@ void Verifier::VerifyAttributeTypes(Attr
> I->getKindAsEnum() == Attribute::StackProtect ||
> I->getKindAsEnum() == Attribute::StackProtectReq ||
> I->getKindAsEnum() == Attribute::StackProtectStrong ||
> + I->getKindAsEnum() == Attribute::SafeStack ||
> I->getKindAsEnum() == Attribute::NoRedZone ||
> I->getKindAsEnum() == Attribute::NoImplicitFloat ||
> I->getKindAsEnum() == Attribute::Naked ||
>
> Modified: llvm/trunk/lib/Target/CppBackend/CPPBackend.cpp
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/CppBackend/CPPBackend.cpp?rev=239761&r1=239760&r2=239761&view=diff
> ==============================================================================
> --- llvm/trunk/lib/Target/CppBackend/CPPBackend.cpp (original)
> +++ llvm/trunk/lib/Target/CppBackend/CPPBackend.cpp Mon Jun 15 16:07:11 2015
> @@ -513,6 +513,7 @@ void CppWriter::printAttributes(const At
> HANDLE_ATTR(StackProtect);
> HANDLE_ATTR(StackProtectReq);
> HANDLE_ATTR(StackProtectStrong);
> + HANDLE_ATTR(SafeStack);
> HANDLE_ATTR(NoCapture);
> HANDLE_ATTR(NoRedZone);
> HANDLE_ATTR(NoImplicitFloat);
>
> Modified: llvm/trunk/lib/Transforms/IPO/Inliner.cpp
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/IPO/Inliner.cpp?rev=239761&r1=239760&r2=239761&view=diff
> ==============================================================================
> --- llvm/trunk/lib/Transforms/IPO/Inliner.cpp (original)
> +++ llvm/trunk/lib/Transforms/IPO/Inliner.cpp Mon Jun 15 16:07:11 2015
> @@ -93,19 +93,26 @@ static void AdjustCallerSSPLevel(Functio
> // clutter to the IR.
> AttrBuilder B;
> B.addAttribute(Attribute::StackProtect)
> - .addAttribute(Attribute::StackProtectStrong);
> + .addAttribute(Attribute::StackProtectStrong)
> + .addAttribute(Attribute::StackProtectReq);
> AttributeSet OldSSPAttr = AttributeSet::get(Caller->getContext(),
> AttributeSet::FunctionIndex,
> B);
>
> - if (Callee->hasFnAttribute(Attribute::StackProtectReq)) {
> + if (Callee->hasFnAttribute(Attribute::SafeStack)) {
> + Caller->removeAttributes(AttributeSet::FunctionIndex, OldSSPAttr);
> + Caller->addFnAttr(Attribute::SafeStack);
> + } else if (Callee->hasFnAttribute(Attribute::StackProtectReq) &&
> + !Caller->hasFnAttribute(Attribute::SafeStack)) {
> Caller->removeAttributes(AttributeSet::FunctionIndex, OldSSPAttr);
> Caller->addFnAttr(Attribute::StackProtectReq);
> } else if (Callee->hasFnAttribute(Attribute::StackProtectStrong) &&
> + !Caller->hasFnAttribute(Attribute::SafeStack) &&
> !Caller->hasFnAttribute(Attribute::StackProtectReq)) {
> Caller->removeAttributes(AttributeSet::FunctionIndex, OldSSPAttr);
> Caller->addFnAttr(Attribute::StackProtectStrong);
> } else if (Callee->hasFnAttribute(Attribute::StackProtect) &&
> + !Caller->hasFnAttribute(Attribute::SafeStack) &&
> !Caller->hasFnAttribute(Attribute::StackProtectReq) &&
> !Caller->hasFnAttribute(Attribute::StackProtectStrong))
> Caller->addFnAttr(Attribute::StackProtect);
>
> Modified: llvm/trunk/lib/Transforms/Instrumentation/CMakeLists.txt
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Instrumentation/CMakeLists.txt?rev=239761&r1=239760&r2=239761&view=diff
> ==============================================================================
> --- llvm/trunk/lib/Transforms/Instrumentation/CMakeLists.txt (original)
> +++ llvm/trunk/lib/Transforms/Instrumentation/CMakeLists.txt Mon Jun 15 16:07:11 2015
> @@ -6,6 +6,7 @@ add_llvm_library(LLVMInstrumentation
> MemorySanitizer.cpp
> Instrumentation.cpp
> InstrProfiling.cpp
> + SafeStack.cpp
> SanitizerCoverage.cpp
> ThreadSanitizer.cpp
>
>
> Modified: llvm/trunk/lib/Transforms/Instrumentation/Instrumentation.cpp
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Instrumentation/Instrumentation.cpp?rev=239761&r1=239760&r2=239761&view=diff
> ==============================================================================
> --- llvm/trunk/lib/Transforms/Instrumentation/Instrumentation.cpp (original)
> +++ llvm/trunk/lib/Transforms/Instrumentation/Instrumentation.cpp Mon Jun 15 16:07:11 2015
> @@ -30,6 +30,7 @@ void llvm::initializeInstrumentation(Pas
> initializeThreadSanitizerPass(Registry);
> initializeSanitizerCoverageModulePass(Registry);
> initializeDataFlowSanitizerPass(Registry);
> + initializeSafeStackPass(Registry);
> }
>
> /// LLVMInitializeInstrumentation - C binding for
>
> Added: llvm/trunk/lib/Transforms/Instrumentation/SafeStack.cpp
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Instrumentation/SafeStack.cpp?rev=239761&view=auto
> ==============================================================================
> --- llvm/trunk/lib/Transforms/Instrumentation/SafeStack.cpp (added)
> +++ llvm/trunk/lib/Transforms/Instrumentation/SafeStack.cpp Mon Jun 15 16:07:11 2015
> @@ -0,0 +1,608 @@
> +//===-- SafeStack.cpp - Safe Stack Insertion ------------------------------===//
> +//
> +// The LLVM Compiler Infrastructure
> +//
> +// This file is distributed under the University of Illinois Open Source
> +// License. See LICENSE.TXT for details.
> +//
> +//===----------------------------------------------------------------------===//
> +//
> +// This pass splits the stack into the safe stack (kept as-is for LLVM backend)
> +// and the unsafe stack (explicitly allocated and managed through the runtime
> +// support library).
> +//
> +// http://clang.llvm.org/docs/SafeStack.html
> +//
> +//===----------------------------------------------------------------------===//
> +
> +#include "llvm/Transforms/Instrumentation.h"
> +#include "llvm/ADT/Statistic.h"
> +#include "llvm/ADT/Triple.h"
> +#include "llvm/Analysis/AliasAnalysis.h"
> +#include "llvm/Analysis/TargetTransformInfo.h"
> +#include "llvm/IR/Constants.h"
> +#include "llvm/IR/DataLayout.h"
> +#include "llvm/IR/DerivedTypes.h"
> +#include "llvm/IR/DIBuilder.h"
> +#include "llvm/IR/Function.h"
> +#include "llvm/IR/InstIterator.h"
> +#include "llvm/IR/Instructions.h"
> +#include "llvm/IR/IntrinsicInst.h"
> +#include "llvm/IR/Intrinsics.h"
> +#include "llvm/IR/IRBuilder.h"
> +#include "llvm/IR/Module.h"
> +#include "llvm/Pass.h"
> +#include "llvm/Support/CommandLine.h"
> +#include "llvm/Support/Debug.h"
> +#include "llvm/Support/Format.h"
> +#include "llvm/Support/MathExtras.h"
> +#include "llvm/Support/raw_os_ostream.h"
> +#include "llvm/Transforms/Utils/Local.h"
> +#include "llvm/Transforms/Utils/ModuleUtils.h"
> +
> +using namespace llvm;
> +
> +#define DEBUG_TYPE "safestack"
> +
> +namespace llvm {
> +
> +STATISTIC(NumFunctions, "Total number of functions");
> +STATISTIC(NumUnsafeStackFunctions, "Number of functions with unsafe stack");
> +STATISTIC(NumUnsafeStackRestorePointsFunctions,
> + "Number of functions that use setjmp or exceptions");
> +
> +STATISTIC(NumAllocas, "Total number of allocas");
> +STATISTIC(NumUnsafeStaticAllocas, "Number of unsafe static allocas");
> +STATISTIC(NumUnsafeDynamicAllocas, "Number of unsafe dynamic allocas");
> +STATISTIC(NumUnsafeStackRestorePoints, "Number of setjmps and landingpads");
> +
> +} // namespace llvm
> +
> +namespace {
> +
> +/// Check whether a given alloca instruction (AI) should be put on the safe
> +/// stack or not. The function analyzes all uses of AI and checks whether it is
> +/// only accessed in a memory safe way (as decided statically).
> +bool IsSafeStackAlloca(const AllocaInst *AI) {
> + // Go through all uses of this alloca and check whether all accesses to the
> + // allocated object are statically known to be memory safe and, hence, the
> + // object can be placed on the safe stack.
> +
> + SmallPtrSet<const Value *, 16> Visited;
> + SmallVector<const Instruction *, 8> WorkList;
> + WorkList.push_back(AI);
> +
> + // A DFS search through all uses of the alloca in bitcasts/PHI/GEPs/etc.
> + while (!WorkList.empty()) {
> + const Instruction *V = WorkList.pop_back_val();
> + for (const Use &UI : V->uses()) {
> + auto I = cast<const Instruction>(UI.getUser());
> + assert(V == UI.get());
> +
> + switch (I->getOpcode()) {
> + case Instruction::Load:
> + // Loading from a pointer is safe.
> + break;
> + case Instruction::VAArg:
> + // "va-arg" from a pointer is safe.
> + break;
> + case Instruction::Store:
> + if (V == I->getOperand(0))
> + // Stored the pointer - conservatively assume it may be unsafe.
> + return false;
> + // Storing to the pointee is safe.
> + break;
> +
> + case Instruction::GetElementPtr:
> + if (!cast<const GetElementPtrInst>(I)->hasAllConstantIndices())
> + // GEP with non-constant indices can lead to memory errors.
> + // This also applies to inbounds GEPs, as the inbounds attribute
> + // represents an assumption that the address is in bounds, rather than
> + // an assertion that it is.
> + return false;
> +
> + // We assume that GEP on static alloca with constant indices is safe,
> + // otherwise a compiler would detect it and warn during compilation.
> +
> + if (!isa<const ConstantInt>(AI->getArraySize()))
> + // However, if the array size itself is not constant, the access
> + // might still be unsafe at runtime.
> + return false;
> +
> + /* fallthrough */
> +
> + case Instruction::BitCast:
> + case Instruction::IntToPtr:
> + case Instruction::PHI:
> + case Instruction::PtrToInt:
> + case Instruction::Select:
> + // The object can be safe or not, depending on how the result of the
> + // instruction is used.
> + if (Visited.insert(I).second)
> + WorkList.push_back(cast<const Instruction>(I));
> + break;
> +
> + case Instruction::Call:
> + case Instruction::Invoke: {
> + // FIXME: add support for memset and memcpy intrinsics.
> + ImmutableCallSite CS(I);
> +
> + // LLVM 'nocapture' attribute is only set for arguments whose address
> + // is not stored, passed around, or used in any other non-trivial way.
> + // We assume that passing a pointer to an object as a 'nocapture'
> + // argument is safe.
> + // FIXME: a more precise solution would require an interprocedural
> + // analysis here, which would look at all uses of an argument inside
> + // the function being called.
> + ImmutableCallSite::arg_iterator B = CS.arg_begin(), E = CS.arg_end();
> + for (ImmutableCallSite::arg_iterator A = B; A != E; ++A)
> + if (A->get() == V && !CS.doesNotCapture(A - B))
> + // The parameter is not marked 'nocapture' - unsafe.
> + return false;
> + continue;
> + }
> +
> + default:
> + // The object is unsafe if it is used in any other way.
> + return false;
> + }
> + }
> + }
> +
> + // All uses of the alloca are safe, we can place it on the safe stack.
> + return true;
> +}
> +
> +/// The SafeStack pass splits the stack of each function into the
> +/// safe stack, which is only accessed through memory safe dereferences
> +/// (as determined statically), and the unsafe stack, which contains all
> +/// local variables that are accessed in unsafe ways.
> +class SafeStack : public FunctionPass {
> + const DataLayout *DL;
> +
> + Type *StackPtrTy;
> + Type *IntPtrTy;
> + Type *Int32Ty;
> + Type *Int8Ty;
> +
> + Constant *UnsafeStackPtr;
> +
> + /// Unsafe stack alignment. Each stack frame must ensure that the stack is
> + /// aligned to this value. We need to re-align the unsafe stack if the
> + /// alignment of any object on the stack exceeds this value.
> + ///
> + /// 16 seems like a reasonable upper bound on the alignment of objects that we
> + /// might expect to appear on the stack on most common targets.
> + enum { StackAlignment = 16 };
> +
> + /// \brief Build a constant representing a pointer to the unsafe stack
> + /// pointer.
> + Constant *getOrCreateUnsafeStackPtr(Module &M);
> +
> + /// \brief Find all static allocas, dynamic allocas, return instructions and
> + /// stack restore points (exception unwind blocks and setjmp calls) in the
> + /// given function and append them to the respective vectors.
> + void findInsts(Function &F, SmallVectorImpl<AllocaInst *> &StaticAllocas,
> + SmallVectorImpl<AllocaInst *> &DynamicAllocas,
> + SmallVectorImpl<ReturnInst *> &Returns,
> + SmallVectorImpl<Instruction *> &StackRestorePoints);
> +
> + /// \brief Allocate space for all static allocas in \p StaticAllocas,
> + /// replace allocas with pointers into the unsafe stack and generate code to
> + /// restore the stack pointer before all return instructions in \p Returns.
> + ///
> + /// \returns A pointer to the top of the unsafe stack after all unsafe static
> + /// allocas are allocated.
> + Value *moveStaticAllocasToUnsafeStack(Function &F,
> + ArrayRef<AllocaInst *> StaticAllocas,
> + ArrayRef<ReturnInst *> Returns);
> +
> + /// \brief Generate code to restore the stack after all stack restore points
> + /// in \p StackRestorePoints.
> + ///
> + /// \returns A local variable in which to maintain the dynamic top of the
> + /// unsafe stack if needed.
> + AllocaInst *
> + createStackRestorePoints(Function &F,
> + ArrayRef<Instruction *> StackRestorePoints,
> + Value *StaticTop, bool NeedDynamicTop);
> +
> + /// \brief Replace all allocas in \p DynamicAllocas with code to allocate
> + /// space dynamically on the unsafe stack and store the dynamic unsafe stack
> + /// top to \p DynamicTop if non-null.
> + void moveDynamicAllocasToUnsafeStack(Function &F, Value *UnsafeStackPtr,
> + AllocaInst *DynamicTop,
> + ArrayRef<AllocaInst *> DynamicAllocas);
> +
> +public:
> + static char ID; // Pass identification, replacement for typeid.
> + SafeStack() : FunctionPass(ID), DL(nullptr) {
> + initializeSafeStackPass(*PassRegistry::getPassRegistry());
> + }
> +
> + virtual void getAnalysisUsage(AnalysisUsage &AU) const {
> + AU.addRequired<AliasAnalysis>();
> + }
> +
> + virtual bool doInitialization(Module &M) {
> + DL = &M.getDataLayout();
> +
> + StackPtrTy = Type::getInt8PtrTy(M.getContext());
> + IntPtrTy = DL->getIntPtrType(M.getContext());
> + Int32Ty = Type::getInt32Ty(M.getContext());
> + Int8Ty = Type::getInt8Ty(M.getContext());
> +
> + UnsafeStackPtr = getOrCreateUnsafeStackPtr(M);
> +
> + return false;
> + }
> +
> + bool runOnFunction(Function &F);
> +
> +}; // class SafeStack
> +
> +Constant *SafeStack::getOrCreateUnsafeStackPtr(Module &M) {
> + // The unsafe stack pointer is stored in a global variable with a magic name.
> + const char *kUnsafeStackPtrVar = "__safestack_unsafe_stack_ptr";
> +
> + auto UnsafeStackPtr =
> + dyn_cast_or_null<GlobalVariable>(M.getNamedValue(kUnsafeStackPtrVar));
> +
> + if (!UnsafeStackPtr) {
> + // The global variable is not defined yet, define it ourselves.
> + // We use the initial-exec TLS model because we do not support the variable
> + // living anywhere other than in the main executable.
> + UnsafeStackPtr = new GlobalVariable(
> + /*Module=*/M, /*Type=*/StackPtrTy,
> + /*isConstant=*/false, /*Linkage=*/GlobalValue::ExternalLinkage,
> + /*Initializer=*/0, /*Name=*/kUnsafeStackPtrVar,
> + /*InsertBefore=*/nullptr,
> + /*ThreadLocalMode=*/GlobalValue::InitialExecTLSModel);
> + } else {
> + // The variable exists, check its type and attributes.
> + if (UnsafeStackPtr->getValueType() != StackPtrTy) {
> + report_fatal_error(Twine(kUnsafeStackPtrVar) + " must have void* type");
> + }
> +
> + if (!UnsafeStackPtr->isThreadLocal()) {
> + report_fatal_error(Twine(kUnsafeStackPtrVar) + " must be thread-local");
> + }
> + }
> +
> + return UnsafeStackPtr;
> +}
> +
> +void SafeStack::findInsts(Function &F,
> + SmallVectorImpl<AllocaInst *> &StaticAllocas,
> + SmallVectorImpl<AllocaInst *> &DynamicAllocas,
> + SmallVectorImpl<ReturnInst *> &Returns,
> + SmallVectorImpl<Instruction *> &StackRestorePoints) {
> + for (Instruction &I : inst_range(&F)) {
> + if (auto AI = dyn_cast<AllocaInst>(&I)) {
> + ++NumAllocas;
> +
> + if (IsSafeStackAlloca(AI))
> + continue;
> +
> + if (AI->isStaticAlloca()) {
> + ++NumUnsafeStaticAllocas;
> + StaticAllocas.push_back(AI);
> + } else {
> + ++NumUnsafeDynamicAllocas;
> + DynamicAllocas.push_back(AI);
> + }
> + } else if (auto RI = dyn_cast<ReturnInst>(&I)) {
> + Returns.push_back(RI);
> + } else if (auto CI = dyn_cast<CallInst>(&I)) {
> + // setjmps require stack restore.
> + if (CI->getCalledFunction() && CI->canReturnTwice())
> + StackRestorePoints.push_back(CI);
> + } else if (auto LP = dyn_cast<LandingPadInst>(&I)) {
> + // Exception landing pads require stack restore.
> + StackRestorePoints.push_back(LP);
> + } else if (auto II = dyn_cast<IntrinsicInst>(&I)) {
> + if (II->getIntrinsicID() == Intrinsic::gcroot)
> + llvm::report_fatal_error(
> + "gcroot intrinsic not compatible with safestack attribute");
> + }
> + }
> +}
> +
> +AllocaInst *
> +SafeStack::createStackRestorePoints(Function &F,
> + ArrayRef<Instruction *> StackRestorePoints,
> + Value *StaticTop, bool NeedDynamicTop) {
> + if (StackRestorePoints.empty())
> + return nullptr;
> +
> + IRBuilder<> IRB(StaticTop
> + ? cast<Instruction>(StaticTop)->getNextNode()
> + : (Instruction *)F.getEntryBlock().getFirstInsertionPt());
> +
> + // We need the current value of the shadow stack pointer to restore
> + // after longjmp or exception catching.
> +
> + // FIXME: On some platforms this could be handled by the longjmp/exception
> + // runtime itself.
> +
> + AllocaInst *DynamicTop = nullptr;
> + if (NeedDynamicTop)
> + // If we also have dynamic alloca's, the stack pointer value changes
> + // throughout the function. For now we store it in an alloca.
> + DynamicTop = IRB.CreateAlloca(StackPtrTy, /*ArraySize=*/nullptr,
> + "unsafe_stack_dynamic_ptr");
> +
> + if (!StaticTop)
> + // We need the original unsafe stack pointer value, even if there are
> + // no unsafe static allocas.
> + StaticTop = IRB.CreateLoad(UnsafeStackPtr, false, "unsafe_stack_ptr");
> +
> + if (NeedDynamicTop)
> + IRB.CreateStore(StaticTop, DynamicTop);
> +
> + // Restore current stack pointer after longjmp/exception catch.
> + for (Instruction *I : StackRestorePoints) {
> + ++NumUnsafeStackRestorePoints;
> +
> + IRB.SetInsertPoint(cast<Instruction>(I->getNextNode()));
> + Value *CurrentTop = DynamicTop ? IRB.CreateLoad(DynamicTop) : StaticTop;
> + IRB.CreateStore(CurrentTop, UnsafeStackPtr);
> + }
> +
> + return DynamicTop;
> +}
> +
> +Value *
> +SafeStack::moveStaticAllocasToUnsafeStack(Function &F,
> + ArrayRef<AllocaInst *> StaticAllocas,
> + ArrayRef<ReturnInst *> Returns) {
> + if (StaticAllocas.empty())
> + return nullptr;
> +
> + IRBuilder<> IRB(F.getEntryBlock().getFirstInsertionPt());
> + DIBuilder DIB(*F.getParent());
> +
> + // We explicitly compute and set the unsafe stack layout for all unsafe
> + // static alloca instructions. We save the unsafe "base pointer" in the
> + // prologue into a local variable and restore it in the epilogue.
> +
> + // Load the current stack pointer (we'll also use it as a base pointer).
> + // FIXME: use a dedicated register for it ?
> + Instruction *BasePointer =
> + IRB.CreateLoad(UnsafeStackPtr, false, "unsafe_stack_ptr");
> + assert(BasePointer->getType() == StackPtrTy);
> +
> + for (ReturnInst *RI : Returns) {
> + IRB.SetInsertPoint(RI);
> + IRB.CreateStore(BasePointer, UnsafeStackPtr);
> + }
> +
> + // Compute maximum alignment among static objects on the unsafe stack.
> + unsigned MaxAlignment = 0;
> + for (AllocaInst *AI : StaticAllocas) {
> + Type *Ty = AI->getAllocatedType();
> + unsigned Align =
> + std::max((unsigned)DL->getPrefTypeAlignment(Ty), AI->getAlignment());
> + if (Align > MaxAlignment)
> + MaxAlignment = Align;
> + }
> +
> + if (MaxAlignment > StackAlignment) {
> + // Re-align the base pointer according to the max requested alignment.
> + assert(isPowerOf2_32(MaxAlignment));
> + IRB.SetInsertPoint(cast<Instruction>(BasePointer->getNextNode()));
> + BasePointer = cast<Instruction>(IRB.CreateIntToPtr(
> + IRB.CreateAnd(IRB.CreatePtrToInt(BasePointer, IntPtrTy),
> + ConstantInt::get(IntPtrTy, ~uint64_t(MaxAlignment - 1))),
> + StackPtrTy));
> + }
> +
> + // Allocate space for every unsafe static AllocaInst on the unsafe stack.
> + int64_t StaticOffset = 0; // Current stack top.
> + for (AllocaInst *AI : StaticAllocas) {
> + IRB.SetInsertPoint(AI);
> +
> + auto CArraySize = cast<ConstantInt>(AI->getArraySize());
> + Type *Ty = AI->getAllocatedType();
> +
> + uint64_t Size = DL->getTypeAllocSize(Ty) * CArraySize->getZExtValue();
> + if (Size == 0)
> + Size = 1; // Don't create zero-sized stack objects.
> +
> + // Ensure the object is properly aligned.
> + unsigned Align =
> + std::max((unsigned)DL->getPrefTypeAlignment(Ty), AI->getAlignment());
> +
> + // Add alignment.
> + // NOTE: we ensure that BasePointer itself is aligned to >= Align.
> + StaticOffset += Size;
> + StaticOffset = RoundUpToAlignment(StaticOffset, Align);
> +
> + Value *Off = IRB.CreateGEP(BasePointer, // BasePointer is i8*
> + ConstantInt::get(Int32Ty, -StaticOffset));
> + Value *NewAI = IRB.CreateBitCast(Off, AI->getType(), AI->getName());
> + if (AI->hasName() && isa<Instruction>(NewAI))
> + cast<Instruction>(NewAI)->takeName(AI);
> +
> + // Replace alloc with the new location.
> + replaceDbgDeclareForAlloca(AI, NewAI, DIB, /*Deref=*/true);
> + AI->replaceAllUsesWith(NewAI);
> + AI->eraseFromParent();
> + }
> +
> + // Re-align BasePointer so that our callees would see it aligned as
> + // expected.
> + // FIXME: no need to update BasePointer in leaf functions.
> + StaticOffset = RoundUpToAlignment(StaticOffset, StackAlignment);
> +
> + // Update shadow stack pointer in the function epilogue.
> + IRB.SetInsertPoint(cast<Instruction>(BasePointer->getNextNode()));
> +
> + Value *StaticTop =
> + IRB.CreateGEP(BasePointer, ConstantInt::get(Int32Ty, -StaticOffset),
> + "unsafe_stack_static_top");
> + IRB.CreateStore(StaticTop, UnsafeStackPtr);
> + return StaticTop;
> +}
> +
> +void SafeStack::moveDynamicAllocasToUnsafeStack(
> + Function &F, Value *UnsafeStackPtr, AllocaInst *DynamicTop,
> + ArrayRef<AllocaInst *> DynamicAllocas) {
> + DIBuilder DIB(*F.getParent());
> +
> + for (AllocaInst *AI : DynamicAllocas) {
> + IRBuilder<> IRB(AI);
> +
> + // Compute the new SP value (after AI).
> + Value *ArraySize = AI->getArraySize();
> + if (ArraySize->getType() != IntPtrTy)
> + ArraySize = IRB.CreateIntCast(ArraySize, IntPtrTy, false);
> +
> + Type *Ty = AI->getAllocatedType();
> + uint64_t TySize = DL->getTypeAllocSize(Ty);
> + Value *Size = IRB.CreateMul(ArraySize, ConstantInt::get(IntPtrTy, TySize));
> +
> + Value *SP = IRB.CreatePtrToInt(IRB.CreateLoad(UnsafeStackPtr), IntPtrTy);
> + SP = IRB.CreateSub(SP, Size);
> +
> + // Align the SP value to satisfy the AllocaInst, type and stack alignments.
> + unsigned Align = std::max(
> + std::max((unsigned)DL->getPrefTypeAlignment(Ty), AI->getAlignment()),
> + (unsigned)StackAlignment);
> +
> + assert(isPowerOf2_32(Align));
> + Value *NewTop = IRB.CreateIntToPtr(
> + IRB.CreateAnd(SP, ConstantInt::get(IntPtrTy, ~uint64_t(Align - 1))),
> + StackPtrTy);
> +
> + // Save the stack pointer.
> + IRB.CreateStore(NewTop, UnsafeStackPtr);
> + if (DynamicTop)
> + IRB.CreateStore(NewTop, DynamicTop);
> +
> + Value *NewAI = IRB.CreateIntToPtr(SP, AI->getType());
> + if (AI->hasName() && isa<Instruction>(NewAI))
> + NewAI->takeName(AI);
> +
> + replaceDbgDeclareForAlloca(AI, NewAI, DIB, /*Deref=*/true);
> + AI->replaceAllUsesWith(NewAI);
> + AI->eraseFromParent();
> + }
> +
> + if (!DynamicAllocas.empty()) {
> + // Now go through the instructions again, replacing stacksave/stackrestore.
> + for (inst_iterator It = inst_begin(&F), Ie = inst_end(&F); It != Ie;) {
> + Instruction *I = &*(It++);
> + auto II = dyn_cast<IntrinsicInst>(I);
> + if (!II)
> + continue;
> +
> + if (II->getIntrinsicID() == Intrinsic::stacksave) {
> + IRBuilder<> IRB(II);
> + Instruction *LI = IRB.CreateLoad(UnsafeStackPtr);
> + LI->takeName(II);
> + II->replaceAllUsesWith(LI);
> + II->eraseFromParent();
> + } else if (II->getIntrinsicID() == Intrinsic::stackrestore) {
> + IRBuilder<> IRB(II);
> + Instruction *SI = IRB.CreateStore(II->getArgOperand(0), UnsafeStackPtr);
> + SI->takeName(II);
> + assert(II->use_empty());
> + II->eraseFromParent();
> + }
> + }
> + }
> +}
> +
> +bool SafeStack::runOnFunction(Function &F) {
> + auto AA = &getAnalysis<AliasAnalysis>();
> +
> + DEBUG(dbgs() << "[SafeStack] Function: " << F.getName() << "\n");
> +
> + if (!F.hasFnAttribute(Attribute::SafeStack)) {
> + DEBUG(dbgs() << "[SafeStack] safestack is not requested"
> + " for this function\n");
> + return false;
> + }
> +
> + if (F.isDeclaration()) {
> + DEBUG(dbgs() << "[SafeStack] function definition"
> + " is not available\n");
> + return false;
> + }
> +
> + {
> + // Make sure the regular stack protector won't run on this function
> + // (safestack attribute takes precedence).
> + AttrBuilder B;
> + B.addAttribute(Attribute::StackProtect)
> + .addAttribute(Attribute::StackProtectReq)
> + .addAttribute(Attribute::StackProtectStrong);
> + F.removeAttributes(
> + AttributeSet::FunctionIndex,
> + AttributeSet::get(F.getContext(), AttributeSet::FunctionIndex, B));
> + }
> +
> + if (AA->onlyReadsMemory(&F)) {
> + // XXX: we don't protect against information leak attacks for now.
> + DEBUG(dbgs() << "[SafeStack] function only reads memory\n");
> + return false;
> + }
> +
> + ++NumFunctions;
> +
> + SmallVector<AllocaInst *, 16> StaticAllocas;
> + SmallVector<AllocaInst *, 4> DynamicAllocas;
> + SmallVector<ReturnInst *, 4> Returns;
> +
> + // Collect all points where stack gets unwound and needs to be restored
> + // This is only necessary because the runtime (setjmp and unwind code) is
> + // not aware of the unsafe stack and won't unwind/restore it prorerly.
> + // To work around this problem without changing the runtime, we insert
> + // instrumentation to restore the unsafe stack pointer when necessary.
> + SmallVector<Instruction *, 4> StackRestorePoints;
> +
> + // Find all static and dynamic alloca instructions that must be moved to the
> + // unsafe stack, all return instructions and stack restore points.
> + findInsts(F, StaticAllocas, DynamicAllocas, Returns, StackRestorePoints);
> +
> + if (StaticAllocas.empty() && DynamicAllocas.empty() &&
> + StackRestorePoints.empty())
> + return false; // Nothing to do in this function.
> +
> + if (!StaticAllocas.empty() || !DynamicAllocas.empty())
> + ++NumUnsafeStackFunctions; // This function has the unsafe stack.
> +
> + if (!StackRestorePoints.empty())
> + ++NumUnsafeStackRestorePointsFunctions;
> +
> + // The top of the unsafe stack after all unsafe static allocas are allocated.
> + Value *StaticTop = moveStaticAllocasToUnsafeStack(F, StaticAllocas, Returns);
> +
> + // Safe stack object that stores the current unsafe stack top. It is updated
> + // as unsafe dynamic (non-constant-sized) allocas are allocated and freed.
> + // This is only needed if we need to restore stack pointer after longjmp
> + // or exceptions, and we have dynamic allocations.
> + // FIXME: a better alternative might be to store the unsafe stack pointer
> + // before setjmp / invoke instructions.
> + AllocaInst *DynamicTop = createStackRestorePoints(
> + F, StackRestorePoints, StaticTop, !DynamicAllocas.empty());
> +
> + // Handle dynamic allocas.
> + moveDynamicAllocasToUnsafeStack(F, UnsafeStackPtr, DynamicTop,
> + DynamicAllocas);
> +
> + DEBUG(dbgs() << "[SafeStack] safestack applied\n");
> + return true;
> +}
> +
> +} // end anonymous namespace
> +
> +char SafeStack::ID = 0;
> +INITIALIZE_PASS_BEGIN(SafeStack, "safe-stack",
> + "Safe Stack instrumentation pass", false, false)
> +INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
> +INITIALIZE_PASS_END(SafeStack, "safe-stack", "Safe Stack instrumentation pass",
> + false, false)
> +
> +FunctionPass *llvm::createSafeStackPass() { return new SafeStack(); }
>
> Added: llvm/trunk/test/Transforms/SafeStack/addr-taken.ll
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/SafeStack/addr-taken.ll?rev=239761&view=auto
> ==============================================================================
> --- llvm/trunk/test/Transforms/SafeStack/addr-taken.ll (added)
> +++ llvm/trunk/test/Transforms/SafeStack/addr-taken.ll Mon Jun 15 16:07:11 2015
> @@ -0,0 +1,22 @@
> +; RUN: opt -safe-stack -S -mtriple=i386-pc-linux-gnu < %s -o - | FileCheck %s
> +; RUN: opt -safe-stack -S -mtriple=x86_64-pc-linux-gnu < %s -o - | FileCheck %s
> +
> + at .str = private unnamed_addr constant [4 x i8] c"%s\0A\00", align 1
> +
> +; Address-of local taken (j = &a)
> +; Requires protector.
> +
> +define void @foo() nounwind uwtable safestack {
> +entry:
> + ; CHECK: __safestack_unsafe_stack_ptr
> + %retval = alloca i32, align 4
> + %a = alloca i32, align 4
> + %j = alloca i32*, align 8
> + store i32 0, i32* %retval
> + %0 = load i32, i32* %a, align 4
> + %add = add nsw i32 %0, 1
> + store i32 %add, i32* %a, align 4
> + store i32* %a, i32** %j, align 8
> + ret void
> +}
> +
>
> Added: llvm/trunk/test/Transforms/SafeStack/array-aligned.ll
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/SafeStack/array-aligned.ll?rev=239761&view=auto
> ==============================================================================
> --- llvm/trunk/test/Transforms/SafeStack/array-aligned.ll (added)
> +++ llvm/trunk/test/Transforms/SafeStack/array-aligned.ll Mon Jun 15 16:07:11 2015
> @@ -0,0 +1,39 @@
> +; RUN: opt -safe-stack -S -mtriple=i386-pc-linux-gnu < %s -o - | FileCheck %s
> +; RUN: opt -safe-stack -S -mtriple=x86_64-pc-linux-gnu < %s -o - | FileCheck %s
> +
> + at .str = private unnamed_addr constant [4 x i8] c"%s\0A\00", align 1
> +
> +; array of [16 x i8]
> +
> +define void @foo(i8* %a) nounwind uwtable safestack {
> +entry:
> + ; CHECK: %[[USP:.*]] = load i8*, i8** @__safestack_unsafe_stack_ptr
> +
> + ; CHECK: %[[USST:.*]] = getelementptr i8, i8* %[[USP]], i32 -16
> +
> + ; CHECK: store i8* %[[USST]], i8** @__safestack_unsafe_stack_ptr
> +
> + ; CHECK: %[[AADDR:.*]] = alloca i8*, align 8
> + %a.addr = alloca i8*, align 8
> +
> + ; CHECK: %[[BUFPTR:.*]] = getelementptr i8, i8* %[[USP]], i32 -16
> + ; CHECK: %[[BUFPTR2:.*]] = bitcast i8* %[[BUFPTR]] to [16 x i8]*
> + %buf = alloca [16 x i8], align 16
> +
> + ; CHECK: store i8* {{.*}}, i8** %[[AADDR]], align 8
> + store i8* %a, i8** %a.addr, align 8
> +
> + ; CHECK: %[[GEP:.*]] = getelementptr inbounds [16 x i8], [16 x i8]* %[[BUFPTR2]], i32 0, i32 0
> + %gep = getelementptr inbounds [16 x i8], [16 x i8]* %buf, i32 0, i32 0
> +
> + ; CHECK: %[[A2:.*]] = load i8*, i8** %[[AADDR]], align 8
> + %a2 = load i8*, i8** %a.addr, align 8
> +
> + ; CHECK: call i8* @strcpy(i8* %[[GEP]], i8* %[[A2]])
> + %call = call i8* @strcpy(i8* %gep, i8* %a2)
> +
> + ; CHECK: store i8* %[[USP]], i8** @__safestack_unsafe_stack_ptr
> + ret void
> +}
> +
> +declare i8* @strcpy(i8*, i8*)
>
> Added: llvm/trunk/test/Transforms/SafeStack/array.ll
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/SafeStack/array.ll?rev=239761&view=auto
> ==============================================================================
> --- llvm/trunk/test/Transforms/SafeStack/array.ll (added)
> +++ llvm/trunk/test/Transforms/SafeStack/array.ll Mon Jun 15 16:07:11 2015
> @@ -0,0 +1,38 @@
> +; RUN: opt -safe-stack -S -mtriple=i386-pc-linux-gnu < %s -o - | FileCheck %s
> +; RUN: opt -safe-stack -S -mtriple=x86_64-pc-linux-gnu < %s -o - | FileCheck %s
> +
> +; array [4 x i8]
> +; Requires protector.
> +
> +define void @foo(i8* %a) nounwind uwtable safestack {
> +entry:
> + ; CHECK: %[[USP:.*]] = load i8*, i8** @__safestack_unsafe_stack_ptr
> +
> + ; CHECK: %[[USST:.*]] = getelementptr i8, i8* %[[USP]], i32 -16
> +
> + ; CHECK: store i8* %[[USST]], i8** @__safestack_unsafe_stack_ptr
> +
> + ; CHECK: %[[AADDR:.*]] = alloca i8*, align 8
> + %a.addr = alloca i8*, align 8
> +
> + ; CHECK: %[[BUFPTR:.*]] = getelementptr i8, i8* %[[USP]], i32 -4
> + ; CHECK: %[[BUFPTR2:.*]] = bitcast i8* %[[BUFPTR]] to [4 x i8]*
> + %buf = alloca [4 x i8], align 1
> +
> + ; CHECK: store i8* {{.*}}, i8** %[[AADDR]], align 8
> + store i8* %a, i8** %a.addr, align 8
> +
> + ; CHECK: %[[GEP:.*]] = getelementptr inbounds [4 x i8], [4 x i8]* %[[BUFPTR2]], i32 0, i32 0
> + %gep = getelementptr inbounds [4 x i8], [4 x i8]* %buf, i32 0, i32 0
> +
> + ; CHECK: %[[A2:.*]] = load i8*, i8** %[[AADDR]], align 8
> + %a2 = load i8*, i8** %a.addr, align 8
> +
> + ; CHECK: call i8* @strcpy(i8* %[[GEP]], i8* %[[A2]])
> + %call = call i8* @strcpy(i8* %gep, i8* %a2)
> +
> + ; CHECK: store i8* %[[USP]], i8** @__safestack_unsafe_stack_ptr
> + ret void
> +}
> +
> +declare i8* @strcpy(i8*, i8*)
>
> Added: llvm/trunk/test/Transforms/SafeStack/call.ll
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/SafeStack/call.ll?rev=239761&view=auto
> ==============================================================================
> --- llvm/trunk/test/Transforms/SafeStack/call.ll (added)
> +++ llvm/trunk/test/Transforms/SafeStack/call.ll Mon Jun 15 16:07:11 2015
> @@ -0,0 +1,20 @@
> +; RUN: opt -safe-stack -S -mtriple=i386-pc-linux-gnu < %s -o - | FileCheck %s
> +; RUN: opt -safe-stack -S -mtriple=x86_64-pc-linux-gnu < %s -o - | FileCheck %s
> +
> + at .str = private unnamed_addr constant [4 x i8] c"%s\0A\00", align 1
> +
> +; no arrays / no nested arrays
> +; Requires no protector.
> +
> +; CHECK-LABEL: @foo(
> +define void @foo(i8* %a) nounwind uwtable safestack {
> +entry:
> + ; CHECK-NOT: __safestack_unsafe_stack_ptr
> + %a.addr = alloca i8*, align 8
> + store i8* %a, i8** %a.addr, align 8
> + %0 = load i8*, i8** %a.addr, align 8
> + %call = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i32 0, i32 0), i8* %0)
> + ret void
> +}
> +
> +declare i32 @printf(i8*, ...)
>
> Added: llvm/trunk/test/Transforms/SafeStack/cast.ll
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/SafeStack/cast.ll?rev=239761&view=auto
> ==============================================================================
> --- llvm/trunk/test/Transforms/SafeStack/cast.ll (added)
> +++ llvm/trunk/test/Transforms/SafeStack/cast.ll Mon Jun 15 16:07:11 2015
> @@ -0,0 +1,17 @@
> +; RUN: opt -safe-stack -S -mtriple=i386-pc-linux-gnu < %s -o - | FileCheck %s
> +; RUN: opt -safe-stack -S -mtriple=x86_64-pc-linux-gnu < %s -o - | FileCheck %s
> +
> + at .str = private unnamed_addr constant [4 x i8] c"%s\0A\00", align 1
> +
> +; PtrToInt/IntToPtr Cast
> +; Requires no protector.
> +
> +; CHECK-LABEL: @foo(
> +define void @foo() nounwind uwtable safestack {
> +entry:
> + ; CHECK-NOT: __safestack_unsafe_stack_ptr
> + %a = alloca i32, align 4
> + %0 = ptrtoint i32* %a to i64
> + %1 = inttoptr i64 %0 to i32*
> + ret void
> +}
>
> Added: llvm/trunk/test/Transforms/SafeStack/constant-gep-call.ll
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/SafeStack/constant-gep-call.ll?rev=239761&view=auto
> ==============================================================================
> --- llvm/trunk/test/Transforms/SafeStack/constant-gep-call.ll (added)
> +++ llvm/trunk/test/Transforms/SafeStack/constant-gep-call.ll Mon Jun 15 16:07:11 2015
> @@ -0,0 +1,26 @@
> +; RUN: opt -safe-stack -S -mtriple=i386-pc-linux-gnu < %s -o - | FileCheck %s
> +; RUN: opt -safe-stack -S -mtriple=x86_64-pc-linux-gnu < %s -o - | FileCheck %s
> +
> +%struct.nest = type { %struct.pair, %struct.pair }
> +%struct.pair = type { i32, i32 }
> +
> + at .str = private unnamed_addr constant [4 x i8] c"%s\0A\00", align 1
> +
> +; Nested structure, no arrays, no address-of expressions.
> +; Verify that the resulting gep-of-gep does not incorrectly trigger
> +; a safe stack protector.
> +; safestack attribute
> +; Requires no protector.
> +; CHECK-LABEL: @foo(
> +define void @foo() nounwind uwtable safestack {
> +entry:
> + ; CHECK-NOT: __safestack_unsafe_stack_ptr
> + %c = alloca %struct.nest, align 4
> + %b = getelementptr inbounds %struct.nest, %struct.nest* %c, i32 0, i32 1
> + %_a = getelementptr inbounds %struct.pair, %struct.pair* %b, i32 0, i32 0
> + %0 = load i32, i32* %_a, align 4
> + %call = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i32 0, i32 0), i32 %0)
> + ret void
> +}
> +
> +declare i32 @printf(i8*, ...)
>
> Added: llvm/trunk/test/Transforms/SafeStack/constant-gep.ll
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/SafeStack/constant-gep.ll?rev=239761&view=auto
> ==============================================================================
> --- llvm/trunk/test/Transforms/SafeStack/constant-gep.ll (added)
> +++ llvm/trunk/test/Transforms/SafeStack/constant-gep.ll Mon Jun 15 16:07:11 2015
> @@ -0,0 +1,20 @@
> +; RUN: opt -safe-stack -S -mtriple=i386-pc-linux-gnu < %s -o - | FileCheck %s
> +; RUN: opt -safe-stack -S -mtriple=x86_64-pc-linux-gnu < %s -o - | FileCheck %s
> +
> +%class.A = type { [2 x i8] }
> +
> + at .str = private unnamed_addr constant [4 x i8] c"%s\0A\00", align 1
> +
> +; [2 x i8] in a class
> +; safestack attribute
> +; Requires no protector.
> +; CHECK-LABEL: @foo(
> +define signext i8 @foo() nounwind uwtable safestack {
> +entry:
> + ; CHECK-NOT: __safestack_unsafe_stack_ptr
> + %a = alloca %class.A, align 1
> + %array = getelementptr inbounds %class.A, %class.A* %a, i32 0, i32 0
> + %arrayidx = getelementptr inbounds [2 x i8], [2 x i8]* %array, i32 0, i64 0
> + %0 = load i8, i8* %arrayidx, align 1
> + ret i8 %0
> +}
>
> Added: llvm/trunk/test/Transforms/SafeStack/constant-geps.ll
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/SafeStack/constant-geps.ll?rev=239761&view=auto
> ==============================================================================
> --- llvm/trunk/test/Transforms/SafeStack/constant-geps.ll (added)
> +++ llvm/trunk/test/Transforms/SafeStack/constant-geps.ll Mon Jun 15 16:07:11 2015
> @@ -0,0 +1,28 @@
> +; RUN: opt -safe-stack -S -mtriple=i386-pc-linux-gnu < %s -o - | FileCheck %s
> +; RUN: opt -safe-stack -S -mtriple=x86_64-pc-linux-gnu < %s -o - | FileCheck %s
> +
> +%struct.deep = type { %union.anon }
> +%union.anon = type { %struct.anon }
> +%struct.anon = type { %struct.anon.0 }
> +%struct.anon.0 = type { %union.anon.1 }
> +%union.anon.1 = type { [2 x i8] }
> +
> + at .str = private unnamed_addr constant [4 x i8] c"%s\0A\00", align 1
> +
> +; [2 x i8] nested in several layers of structs and unions
> +; safestack attribute
> +; Requires no protector.
> +; CHECK-LABEL: @foo(
> +define signext i8 @foo() nounwind uwtable safestack {
> +entry:
> + ; CHECK-NOT: __safestack_unsafe_stack_ptr
> + %x = alloca %struct.deep, align 1
> + %b = getelementptr inbounds %struct.deep, %struct.deep* %x, i32 0, i32 0
> + %c = bitcast %union.anon* %b to %struct.anon*
> + %d = getelementptr inbounds %struct.anon, %struct.anon* %c, i32 0, i32 0
> + %e = getelementptr inbounds %struct.anon.0, %struct.anon.0* %d, i32 0, i32 0
> + %array = bitcast %union.anon.1* %e to [2 x i8]*
> + %arrayidx = getelementptr inbounds [2 x i8], [2 x i8]* %array, i32 0, i64 0
> + %0 = load i8, i8* %arrayidx, align 1
> + ret i8 %0
> +}
>
> Added: llvm/trunk/test/Transforms/SafeStack/dynamic-alloca.ll
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/SafeStack/dynamic-alloca.ll?rev=239761&view=auto
> ==============================================================================
> --- llvm/trunk/test/Transforms/SafeStack/dynamic-alloca.ll (added)
> +++ llvm/trunk/test/Transforms/SafeStack/dynamic-alloca.ll Mon Jun 15 16:07:11 2015
> @@ -0,0 +1,21 @@
> +; RUN: opt -safe-stack -S -mtriple=i386-pc-linux-gnu < %s -o - | FileCheck %s
> +; RUN: opt -safe-stack -S -mtriple=x86_64-pc-linux-gnu < %s -o - | FileCheck %s
> +
> + at .str = private unnamed_addr constant [4 x i8] c"%s\0A\00", align 1
> +
> +; Variable sized alloca
> +; safestack attribute
> +; Requires protector.
> +define void @foo(i32 %n) nounwind uwtable safestack {
> +entry:
> + ; CHECK: __safestack_unsafe_stack_ptr
> + %n.addr = alloca i32, align 4
> + %a = alloca i32*, align 8
> + store i32 %n, i32* %n.addr, align 4
> + %0 = load i32, i32* %n.addr, align 4
> + %conv = sext i32 %0 to i64
> + %1 = alloca i8, i64 %conv
> + %2 = bitcast i8* %1 to i32*
> + store i32* %2, i32** %a, align 8
> + ret void
> +}
>
> Added: llvm/trunk/test/Transforms/SafeStack/escape-addr-pointer.ll
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/SafeStack/escape-addr-pointer.ll?rev=239761&view=auto
> ==============================================================================
> --- llvm/trunk/test/Transforms/SafeStack/escape-addr-pointer.ll (added)
> +++ llvm/trunk/test/Transforms/SafeStack/escape-addr-pointer.ll Mon Jun 15 16:07:11 2015
> @@ -0,0 +1,23 @@
> +; RUN: opt -safe-stack -S -mtriple=i386-pc-linux-gnu < %s -o - | FileCheck %s
> +; RUN: opt -safe-stack -S -mtriple=x86_64-pc-linux-gnu < %s -o - | FileCheck %s
> +
> + at .str = private unnamed_addr constant [4 x i8] c"%s\0A\00", align 1
> +
> +; Addr-of a pointer
> +; safestack attribute
> +; Requires protector.
> +define void @foo() nounwind uwtable safestack {
> +entry:
> + ; CHECK: __safestack_unsafe_stack_ptr
> + %a = alloca i32*, align 8
> + %b = alloca i32**, align 8
> + %call = call i32* @getp()
> + store i32* %call, i32** %a, align 8
> + store i32** %a, i32*** %b, align 8
> + %0 = load i32**, i32*** %b, align 8
> + call void @funcall2(i32** %0)
> + ret void
> +}
> +
> +declare void @funcall2(i32**)
> +declare i32* @getp()
>
> Added: llvm/trunk/test/Transforms/SafeStack/escape-bitcast-store.ll
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/SafeStack/escape-bitcast-store.ll?rev=239761&view=auto
> ==============================================================================
> --- llvm/trunk/test/Transforms/SafeStack/escape-bitcast-store.ll (added)
> +++ llvm/trunk/test/Transforms/SafeStack/escape-bitcast-store.ll Mon Jun 15 16:07:11 2015
> @@ -0,0 +1,23 @@
> +; RUN: opt -safe-stack -S -mtriple=i386-pc-linux-gnu < %s -o - | FileCheck %s
> +; RUN: opt -safe-stack -S -mtriple=x86_64-pc-linux-gnu < %s -o - | FileCheck %s
> +
> + at .str = private unnamed_addr constant [4 x i8] c"%s\0A\00", align 1
> +
> +; Addr-of a local cast to a ptr of a different type
> +; (e.g., int a; ... ; float *b = &a;)
> +; safestack attribute
> +; Requires protector.
> +define void @foo() nounwind uwtable safestack {
> +entry:
> + ; CHECK: __safestack_unsafe_stack_ptr
> + %a = alloca i32, align 4
> + %b = alloca float*, align 8
> + store i32 0, i32* %a, align 4
> + %0 = bitcast i32* %a to float*
> + store float* %0, float** %b, align 8
> + %1 = load float*, float** %b, align 8
> + %call = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i32 0, i32 0), float* %1)
> + ret void
> +}
> +
> +declare i32 @printf(i8*, ...)
>
> Added: llvm/trunk/test/Transforms/SafeStack/escape-bitcast-store2.ll
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/SafeStack/escape-bitcast-store2.ll?rev=239761&view=auto
> ==============================================================================
> --- llvm/trunk/test/Transforms/SafeStack/escape-bitcast-store2.ll (added)
> +++ llvm/trunk/test/Transforms/SafeStack/escape-bitcast-store2.ll Mon Jun 15 16:07:11 2015
> @@ -0,0 +1,20 @@
> +; RUN: opt -safe-stack -S -mtriple=i386-pc-linux-gnu < %s -o - | FileCheck %s
> +; RUN: opt -safe-stack -S -mtriple=x86_64-pc-linux-gnu < %s -o - | FileCheck %s
> +
> + at .str = private unnamed_addr constant [4 x i8] c"%s\0A\00", align 1
> +
> +; Addr-of a local cast to a ptr of a different type (optimized)
> +; (e.g., int a; ... ; float *b = &a;)
> +; safestack attribute
> +; Requires protector.
> +define void @foo() nounwind uwtable safestack {
> +entry:
> + ; CHECK: __safestack_unsafe_stack_ptr
> + %a = alloca i32, align 4
> + store i32 0, i32* %a, align 4
> + %0 = bitcast i32* %a to float*
> + call void @funfloat(float* %0) nounwind
> + ret void
> +}
> +
> +declare void @funfloat(float*)
>
> Added: llvm/trunk/test/Transforms/SafeStack/escape-call.ll
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/SafeStack/escape-call.ll?rev=239761&view=auto
> ==============================================================================
> --- llvm/trunk/test/Transforms/SafeStack/escape-call.ll (added)
> +++ llvm/trunk/test/Transforms/SafeStack/escape-call.ll Mon Jun 15 16:07:11 2015
> @@ -0,0 +1,16 @@
> +; RUN: opt -safe-stack -S -mtriple=i386-pc-linux-gnu < %s -o - | FileCheck %s
> +; RUN: opt -safe-stack -S -mtriple=x86_64-pc-linux-gnu < %s -o - | FileCheck %s
> +
> + at .str = private unnamed_addr constant [4 x i8] c"%s\0A\00", align 1
> +
> +; Passing addr-of to function call
> +; Requires protector.
> +define void @foo() nounwind uwtable safestack {
> +entry:
> + ; CHECK: __safestack_unsafe_stack_ptr
> + %b = alloca i32, align 4
> + call void @funcall(i32* %b) nounwind
> + ret void
> +}
> +
> +declare void @funcall(i32*)
>
> Added: llvm/trunk/test/Transforms/SafeStack/escape-casted-pointer.ll
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/SafeStack/escape-casted-pointer.ll?rev=239761&view=auto
> ==============================================================================
> --- llvm/trunk/test/Transforms/SafeStack/escape-casted-pointer.ll (added)
> +++ llvm/trunk/test/Transforms/SafeStack/escape-casted-pointer.ll Mon Jun 15 16:07:11 2015
> @@ -0,0 +1,24 @@
> +; RUN: opt -safe-stack -S -mtriple=i386-pc-linux-gnu < %s -o - | FileCheck %s
> +; RUN: opt -safe-stack -S -mtriple=x86_64-pc-linux-gnu < %s -o - | FileCheck %s
> +
> + at .str = private unnamed_addr constant [4 x i8] c"%s\0A\00", align 1
> +
> +; Addr-of a casted pointer
> +; safestack attribute
> +; Requires protector.
> +define void @foo() nounwind uwtable safestack {
> +entry:
> + ; CHECK: __safestack_unsafe_stack_ptr
> + %a = alloca i32*, align 8
> + %b = alloca float**, align 8
> + %call = call i32* @getp()
> + store i32* %call, i32** %a, align 8
> + %0 = bitcast i32** %a to float**
> + store float** %0, float*** %b, align 8
> + %1 = load float**, float*** %b, align 8
> + call void @funfloat2(float** %1)
> + ret void
> +}
> +
> +declare void @funfloat2(float**)
> +declare i32* @getp()
>
> Added: llvm/trunk/test/Transforms/SafeStack/escape-gep-call.ll
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/SafeStack/escape-gep-call.ll?rev=239761&view=auto
> ==============================================================================
> --- llvm/trunk/test/Transforms/SafeStack/escape-gep-call.ll (added)
> +++ llvm/trunk/test/Transforms/SafeStack/escape-gep-call.ll Mon Jun 15 16:07:11 2015
> @@ -0,0 +1,20 @@
> +; RUN: opt -safe-stack -S -mtriple=i386-pc-linux-gnu < %s -o - | FileCheck %s
> +; RUN: opt -safe-stack -S -mtriple=x86_64-pc-linux-gnu < %s -o - | FileCheck %s
> +
> +%struct.pair = type { i32, i32 }
> +
> + at .str = private unnamed_addr constant [4 x i8] c"%s\0A\00", align 1
> +
> +; Addr-of struct element, GEP followed by callinst.
> +; safestack attribute
> +; Requires protector.
> +define void @foo() nounwind uwtable safestack {
> +entry:
> + ; CHECK: __safestack_unsafe_stack_ptr
> + %c = alloca %struct.pair, align 4
> + %y = getelementptr inbounds %struct.pair, %struct.pair* %c, i64 0, i32 1
> + %call = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32* %y) nounwind
> + ret void
> +}
> +
> +declare i32 @printf(i8*, ...)
>
> Added: llvm/trunk/test/Transforms/SafeStack/escape-gep-invoke.ll
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/SafeStack/escape-gep-invoke.ll?rev=239761&view=auto
> ==============================================================================
> --- llvm/trunk/test/Transforms/SafeStack/escape-gep-invoke.ll (added)
> +++ llvm/trunk/test/Transforms/SafeStack/escape-gep-invoke.ll Mon Jun 15 16:07:11 2015
> @@ -0,0 +1,34 @@
> +; RUN: opt -safe-stack -S -mtriple=i386-pc-linux-gnu < %s -o - | FileCheck %s
> +; RUN: opt -safe-stack -S -mtriple=x86_64-pc-linux-gnu < %s -o - | FileCheck %s
> +
> +%struct.pair = type { i32, i32 }
> +
> + at .str = private unnamed_addr constant [4 x i8] c"%s\0A\00", align 1
> +
> +; Addr-of a struct element passed into an invoke instruction.
> +; (GEP followed by an invoke)
> +; safestack attribute
> +; Requires protector.
> +define i32 @foo() uwtable safestack {
> +entry:
> + ; CHECK: __safestack_unsafe_stack_ptr
> + %c = alloca %struct.pair, align 4
> + %exn.slot = alloca i8*
> + %ehselector.slot = alloca i32
> + %a = getelementptr inbounds %struct.pair, %struct.pair* %c, i32 0, i32 0
> + store i32 0, i32* %a, align 4
> + %a1 = getelementptr inbounds %struct.pair, %struct.pair* %c, i32 0, i32 0
> + invoke void @_Z3exceptPi(i32* %a1)
> + to label %invoke.cont unwind label %lpad
> +
> +invoke.cont:
> + ret i32 0
> +
> +lpad:
> + %0 = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*)
> + catch i8* null
> + ret i32 0
> +}
> +
> +declare void @_Z3exceptPi(i32*)
> +declare i32 @__gxx_personality_v0(...)
>
> Added: llvm/trunk/test/Transforms/SafeStack/escape-gep-negative.ll
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/SafeStack/escape-gep-negative.ll?rev=239761&view=auto
> ==============================================================================
> --- llvm/trunk/test/Transforms/SafeStack/escape-gep-negative.ll (added)
> +++ llvm/trunk/test/Transforms/SafeStack/escape-gep-negative.ll Mon Jun 15 16:07:11 2015
> @@ -0,0 +1,18 @@
> +; RUN: opt -safe-stack -S -mtriple=i386-pc-linux-gnu < %s -o - | FileCheck %s
> +; RUN: opt -safe-stack -S -mtriple=x86_64-pc-linux-gnu < %s -o - | FileCheck %s
> +
> + at .str = private unnamed_addr constant [4 x i8] c"%s\0A\00", align 1
> +
> +; Addr-of a local, optimized into a GEP (e.g., &a - 12)
> +; safestack attribute
> +; Requires protector.
> +define void @foo() nounwind uwtable safestack {
> +entry:
> + ; CHECK: __safestack_unsafe_stack_ptr
> + %a = alloca i32, align 4
> + %add.ptr5 = getelementptr inbounds i32, i32* %a, i64 -12
> + %call = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32* %add.ptr5) nounwind
> + ret void
> +}
> +
> +declare i32 @printf(i8*, ...)
>
> Added: llvm/trunk/test/Transforms/SafeStack/escape-gep-ptrtoint.ll
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/SafeStack/escape-gep-ptrtoint.ll?rev=239761&view=auto
> ==============================================================================
> --- llvm/trunk/test/Transforms/SafeStack/escape-gep-ptrtoint.ll (added)
> +++ llvm/trunk/test/Transforms/SafeStack/escape-gep-ptrtoint.ll Mon Jun 15 16:07:11 2015
> @@ -0,0 +1,22 @@
> +; RUN: opt -safe-stack -S -mtriple=i386-pc-linux-gnu < %s -o - | FileCheck %s
> +; RUN: opt -safe-stack -S -mtriple=x86_64-pc-linux-gnu < %s -o - | FileCheck %s
> +
> +%struct.pair = type { i32, i32 }
> +
> + at .str = private unnamed_addr constant [4 x i8] c"%s\0A\00", align 1
> +
> +; Addr-of struct element, GEP followed by ptrtoint.
> +; safestack attribute
> +; Requires protector.
> +define void @foo() nounwind uwtable safestack {
> +entry:
> + ; CHECK: __safestack_unsafe_stack_ptr
> + %c = alloca %struct.pair, align 4
> + %b = alloca i32*, align 8
> + %y = getelementptr inbounds %struct.pair, %struct.pair* %c, i32 0, i32 1
> + %0 = ptrtoint i32* %y to i64
> + %call = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i32 0, i32 0), i64 %0)
> + ret void
> +}
> +
> +declare i32 @printf(i8*, ...)
>
> Added: llvm/trunk/test/Transforms/SafeStack/escape-gep-store.ll
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/SafeStack/escape-gep-store.ll?rev=239761&view=auto
> ==============================================================================
> --- llvm/trunk/test/Transforms/SafeStack/escape-gep-store.ll (added)
> +++ llvm/trunk/test/Transforms/SafeStack/escape-gep-store.ll Mon Jun 15 16:07:11 2015
> @@ -0,0 +1,23 @@
> +; RUN: opt -safe-stack -S -mtriple=i386-pc-linux-gnu < %s -o - | FileCheck %s
> +; RUN: opt -safe-stack -S -mtriple=x86_64-pc-linux-gnu < %s -o - | FileCheck %s
> +
> +%struct.pair = type { i32, i32 }
> +
> + at .str = private unnamed_addr constant [4 x i8] c"%s\0A\00", align 1
> +
> +; Addr-of struct element. (GEP followed by store).
> +; safestack attribute
> +; Requires protector.
> +define void @foo() nounwind uwtable safestack {
> +entry:
> + ; CHECK: __safestack_unsafe_stack_ptr
> + %c = alloca %struct.pair, align 4
> + %b = alloca i32*, align 8
> + %y = getelementptr inbounds %struct.pair, %struct.pair* %c, i32 0, i32 1
> + store i32* %y, i32** %b, align 8
> + %0 = load i32*, i32** %b, align 8
> + %call = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i32 0, i32 0), i32* %0)
> + ret void
> +}
> +
> +declare i32 @printf(i8*, ...)
>
> Added: llvm/trunk/test/Transforms/SafeStack/escape-phi-call.ll
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/SafeStack/escape-phi-call.ll?rev=239761&view=auto
> ==============================================================================
> --- llvm/trunk/test/Transforms/SafeStack/escape-phi-call.ll (added)
> +++ llvm/trunk/test/Transforms/SafeStack/escape-phi-call.ll Mon Jun 15 16:07:11 2015
> @@ -0,0 +1,36 @@
> +; RUN: opt -safe-stack -S -mtriple=i386-pc-linux-gnu < %s -o - | FileCheck %s
> +; RUN: opt -safe-stack -S -mtriple=x86_64-pc-linux-gnu < %s -o - | FileCheck %s
> +
> + at .str = private unnamed_addr constant [4 x i8] c"%s\0A\00", align 1
> +
> +; Addr-of in phi instruction
> +; Requires protector.
> +define void @foo() nounwind uwtable safestack {
> +entry:
> + ; CHECK: __safestack_unsafe_stack_ptr
> + %x = alloca double, align 8
> + %call = call double @testi_aux() nounwind
> + store double %call, double* %x, align 8
> + %cmp = fcmp ogt double %call, 3.140000e+00
> + br i1 %cmp, label %if.then, label %if.else
> +
> +if.then: ; preds = %entry
> + %call1 = call double @testi_aux() nounwind
> + store double %call1, double* %x, align 8
> + br label %if.end4
> +
> +if.else: ; preds = %entry
> + %cmp2 = fcmp ogt double %call, 1.000000e+00
> + br i1 %cmp2, label %if.then3, label %if.end4
> +
> +if.then3: ; preds = %if.else
> + br label %if.end4
> +
> +if.end4: ; preds = %if.else, %if.then3, %if.then
> + %y.0 = phi double* [ null, %if.then ], [ %x, %if.then3 ], [ null, %if.else ]
> + %call5 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), double* %y.0) nounwind
> + ret void
> +}
> +
> +declare double @testi_aux()
> +declare i32 @printf(i8*, ...)
>
> Added: llvm/trunk/test/Transforms/SafeStack/escape-select-call.ll
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/SafeStack/escape-select-call.ll?rev=239761&view=auto
> ==============================================================================
> --- llvm/trunk/test/Transforms/SafeStack/escape-select-call.ll (added)
> +++ llvm/trunk/test/Transforms/SafeStack/escape-select-call.ll Mon Jun 15 16:07:11 2015
> @@ -0,0 +1,22 @@
> +; RUN: opt -safe-stack -S -mtriple=i386-pc-linux-gnu < %s -o - | FileCheck %s
> +; RUN: opt -safe-stack -S -mtriple=x86_64-pc-linux-gnu < %s -o - | FileCheck %s
> +
> + at .str = private unnamed_addr constant [4 x i8] c"%s\0A\00", align 1
> +
> +; Addr-of in select instruction
> +; safestack attribute
> +; Requires protector.
> +define void @foo() nounwind uwtable safestack {
> +entry:
> + ; CHECK: __safestack_unsafe_stack_ptr
> + %x = alloca double, align 8
> + %call = call double @testi_aux() nounwind
> + store double %call, double* %x, align 8
> + %cmp2 = fcmp ogt double %call, 0.000000e+00
> + %y.1 = select i1 %cmp2, double* %x, double* null
> + %call2 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i32 0, i32 0), double* %y.1)
> + ret void
> +}
> +
> +declare double @testi_aux()
> +declare i32 @printf(i8*, ...)
>
> Added: llvm/trunk/test/Transforms/SafeStack/escape-vector.ll
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/SafeStack/escape-vector.ll?rev=239761&view=auto
> ==============================================================================
> --- llvm/trunk/test/Transforms/SafeStack/escape-vector.ll (added)
> +++ llvm/trunk/test/Transforms/SafeStack/escape-vector.ll Mon Jun 15 16:07:11 2015
> @@ -0,0 +1,21 @@
> +; RUN: opt -safe-stack -S -mtriple=i386-pc-linux-gnu < %s -o - | FileCheck %s
> +; RUN: opt -safe-stack -S -mtriple=x86_64-pc-linux-gnu < %s -o - | FileCheck %s
> +
> +%struct.vec = type { <4 x i32> }
> +
> + at .str = private unnamed_addr constant [4 x i8] c"%s\0A\00", align 1
> +
> +; Addr-of a vector nested in a struct
> +; safestack attribute
> +; Requires protector.
> +define void @foo() nounwind uwtable safestack {
> +entry:
> + ; CHECK: __safestack_unsafe_stack_ptr
> + %c = alloca %struct.vec, align 16
> + %y = getelementptr inbounds %struct.vec, %struct.vec* %c, i64 0, i32 0
> + %add.ptr = getelementptr inbounds <4 x i32>, <4 x i32>* %y, i64 -12
> + %call = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), <4 x i32>* %add.ptr) nounwind
> + ret void
> +}
> +
> +declare i32 @printf(i8*, ...)
>
> Added: llvm/trunk/test/Transforms/SafeStack/invoke.ll
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/SafeStack/invoke.ll?rev=239761&view=auto
> ==============================================================================
> --- llvm/trunk/test/Transforms/SafeStack/invoke.ll (added)
> +++ llvm/trunk/test/Transforms/SafeStack/invoke.ll Mon Jun 15 16:07:11 2015
> @@ -0,0 +1,33 @@
> +; RUN: opt -safe-stack -S -mtriple=i386-pc-linux-gnu < %s -o - | FileCheck %s
> +; RUN: opt -safe-stack -S -mtriple=x86_64-pc-linux-gnu < %s -o - | FileCheck %s
> +
> + at .str = private unnamed_addr constant [4 x i8] c"%s\0A\00", align 1
> +
> +; Addr-of a variable passed into an invoke instruction.
> +; safestack attribute
> +; Requires protector and stack restore after landing pad.
> +define i32 @foo() uwtable safestack {
> +entry:
> + ; CHECK: %[[SP:.*]] = load i8*, i8** @__safestack_unsafe_stack_ptr
> + ; CHECK: %[[STATICTOP:.*]] = getelementptr i8, i8* %[[SP]], i32 -16
> + %a = alloca i32, align 4
> + %exn.slot = alloca i8*
> + %ehselector.slot = alloca i32
> + store i32 0, i32* %a, align 4
> + invoke void @_Z3exceptPi(i32* %a)
> + to label %invoke.cont unwind label %lpad
> +
> +invoke.cont:
> + ret i32 0
> +
> +lpad:
> + ; CHECK: landingpad
> + ; CHECK-NEXT: catch
> + %0 = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*)
> + catch i8* null
> + ; CHECK-NEXT: store i8* %[[STATICTOP]], i8** @__safestack_unsafe_stack_ptr
> + ret i32 0
> +}
> +
> +declare void @_Z3exceptPi(i32*)
> +declare i32 @__gxx_personality_v0(...)
>
> Added: llvm/trunk/test/Transforms/SafeStack/no-attr.ll
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/SafeStack/no-attr.ll?rev=239761&view=auto
> ==============================================================================
> --- llvm/trunk/test/Transforms/SafeStack/no-attr.ll (added)
> +++ llvm/trunk/test/Transforms/SafeStack/no-attr.ll Mon Jun 15 16:07:11 2015
> @@ -0,0 +1,25 @@
> +; RUN: opt -safe-stack -S -mtriple=i386-pc-linux-gnu < %s -o - | FileCheck %s
> +; RUN: opt -safe-stack -S -mtriple=x86_64-pc-linux-gnu < %s -o - | FileCheck %s
> +
> + at .str = private unnamed_addr constant [4 x i8] c"%s\0A\00", align 1
> +
> +; no safestack attribute
> +; Requires no protector.
> +
> +; CHECK: @foo
> +define void @foo(i8* %a) nounwind uwtable {
> +entry:
> + ; CHECK-NOT: __safestack_unsafe_stack_ptr
> + %a.addr = alloca i8*, align 8
> + %buf = alloca [16 x i8], align 16
> + store i8* %a, i8** %a.addr, align 8
> + %arraydecay = getelementptr inbounds [16 x i8], [16 x i8]* %buf, i32 0, i32 0
> + %0 = load i8*, i8** %a.addr, align 8
> + %call = call i8* @strcpy(i8* %arraydecay, i8* %0)
> + %arraydecay1 = getelementptr inbounds [16 x i8], [16 x i8]* %buf, i32 0, i32 0
> + %call2 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i32 0, i32 0), i8* %arraydecay1)
> + ret void
> +}
> +
> +declare i8* @strcpy(i8*, i8*)
> +declare i32 @printf(i8*, ...)
>
> Added: llvm/trunk/test/Transforms/SafeStack/phi-cycle.ll
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/SafeStack/phi-cycle.ll?rev=239761&view=auto
> ==============================================================================
> --- llvm/trunk/test/Transforms/SafeStack/phi-cycle.ll (added)
> +++ llvm/trunk/test/Transforms/SafeStack/phi-cycle.ll Mon Jun 15 16:07:11 2015
> @@ -0,0 +1,50 @@
> +; RUN: opt -safe-stack -S -mtriple=i386-pc-linux-gnu < %s -o - | FileCheck %s
> +; RUN: opt -safe-stack -S -mtriple=x86_64-pc-linux-gnu < %s -o - | FileCheck %s
> +
> +%struct.small = type { i8 }
> +
> + at .str = private unnamed_addr constant [4 x i8] c"%s\0A\00", align 1
> +
> +; Address-of a structure taken in a function with a loop where
> +; the alloca is an incoming value to a PHI node and a use of that PHI
> +; node is also an incoming value.
> +; Verify that the address-of analysis does not get stuck in infinite
> +; recursion when chasing the alloca through the PHI nodes.
> +; Requires protector.
> +define i32 @foo(i32 %arg) nounwind uwtable safestack {
> +bb:
> + ; CHECK: __safestack_unsafe_stack_ptr
> + %tmp = alloca %struct.small*, align 8
> + %tmp1 = call i32 (...) @dummy(%struct.small** %tmp) nounwind
> + %tmp2 = load %struct.small*, %struct.small** %tmp, align 8
> + %tmp3 = ptrtoint %struct.small* %tmp2 to i64
> + %tmp4 = trunc i64 %tmp3 to i32
> + %tmp5 = icmp sgt i32 %tmp4, 0
> + br i1 %tmp5, label %bb6, label %bb21
> +
> +bb6: ; preds = %bb17, %bb
> + %tmp7 = phi %struct.small* [ %tmp19, %bb17 ], [ %tmp2, %bb ]
> + %tmp8 = phi i64 [ %tmp20, %bb17 ], [ 1, %bb ]
> + %tmp9 = phi i32 [ %tmp14, %bb17 ], [ %tmp1, %bb ]
> + %tmp10 = getelementptr inbounds %struct.small, %struct.small* %tmp7, i64 0, i32 0
> + %tmp11 = load i8, i8* %tmp10, align 1
> + %tmp12 = icmp eq i8 %tmp11, 1
> + %tmp13 = add nsw i32 %tmp9, 8
> + %tmp14 = select i1 %tmp12, i32 %tmp13, i32 %tmp9
> + %tmp15 = trunc i64 %tmp8 to i32
> + %tmp16 = icmp eq i32 %tmp15, %tmp4
> + br i1 %tmp16, label %bb21, label %bb17
> +
> +bb17: ; preds = %bb6
> + %tmp18 = getelementptr inbounds %struct.small*, %struct.small** %tmp, i64 %tmp8
> + %tmp19 = load %struct.small*, %struct.small** %tmp18, align 8
> + %tmp20 = add i64 %tmp8, 1
> + br label %bb6
> +
> +bb21: ; preds = %bb6, %bb
> + %tmp22 = phi i32 [ %tmp1, %bb ], [ %tmp14, %bb6 ]
> + %tmp23 = call i32 (...) @dummy(i32 %tmp22) nounwind
> + ret i32 undef
> +}
> +
> +declare i32 @dummy(...)
>
> Added: llvm/trunk/test/Transforms/SafeStack/setjmp.ll
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/SafeStack/setjmp.ll?rev=239761&view=auto
> ==============================================================================
> --- llvm/trunk/test/Transforms/SafeStack/setjmp.ll (added)
> +++ llvm/trunk/test/Transforms/SafeStack/setjmp.ll Mon Jun 15 16:07:11 2015
> @@ -0,0 +1,37 @@
> +; RUN: opt -safe-stack -S -mtriple=i386-pc-linux-gnu < %s -o - | FileCheck %s
> +; RUN: opt -safe-stack -S -mtriple=x86_64-pc-linux-gnu < %s -o - | FileCheck %s
> +
> +%struct.__jmp_buf_tag = type { [8 x i64], i32, %struct.__sigset_t }
> +%struct.__sigset_t = type { [16 x i64] }
> +
> + at .str = private unnamed_addr constant [4 x i8] c"%s\0A\00", align 1
> + at buf = internal global [1 x %struct.__jmp_buf_tag] zeroinitializer, align 16
> +
> +; setjmp/longjmp test.
> +; Requires protector.
> +define i32 @foo() nounwind uwtable safestack {
> +entry:
> + ; CHECK: %[[SP:.*]] = load i8*, i8** @__safestack_unsafe_stack_ptr
> + ; CHECK: %[[STATICTOP:.*]] = getelementptr i8, i8* %[[SP]], i32 -16
> + %retval = alloca i32, align 4
> + %x = alloca i32, align 4
> + store i32 0, i32* %retval
> + store i32 42, i32* %x, align 4
> + %call = call i32 @_setjmp(%struct.__jmp_buf_tag* getelementptr inbounds ([1 x %struct.__jmp_buf_tag], [1 x %struct.__jmp_buf_tag]* @buf, i32 0, i32 0)) returns_twice
> + ; CHECK: setjmp
> + ; CHECK-NEXT: store i8* %[[STATICTOP]], i8** @__safestack_unsafe_stack_ptr
> + %tobool = icmp ne i32 %call, 0
> + br i1 %tobool, label %if.else, label %if.then
> +if.then: ; preds = %entry
> + call void @funcall(i32* %x)
> + br label %if.end
> +if.else: ; preds = %entry
> + call i32 (...) @dummy()
> + br label %if.end
> +if.end: ; preds = %if.else, %if.then
> + ret i32 0
> +}
> +
> +declare i32 @_setjmp(%struct.__jmp_buf_tag*)
> +declare void @funcall(i32*)
> +declare i32 @dummy(...)
>
> Added: llvm/trunk/test/Transforms/SafeStack/setjmp2.ll
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/SafeStack/setjmp2.ll?rev=239761&view=auto
> ==============================================================================
> --- llvm/trunk/test/Transforms/SafeStack/setjmp2.ll (added)
> +++ llvm/trunk/test/Transforms/SafeStack/setjmp2.ll Mon Jun 15 16:07:11 2015
> @@ -0,0 +1,42 @@
> +; RUN: opt -safe-stack -S -mtriple=i386-pc-linux-gnu < %s -o - | FileCheck %s
> +; RUN: opt -safe-stack -S -mtriple=x86_64-pc-linux-gnu < %s -o - | FileCheck %s
> +
> +%struct.__jmp_buf_tag = type { [8 x i64], i32, %struct.__sigset_t }
> +%struct.__sigset_t = type { [16 x i64] }
> +
> + at .str = private unnamed_addr constant [4 x i8] c"%s\0A\00", align 1
> + at buf = internal global [1 x %struct.__jmp_buf_tag] zeroinitializer, align 16
> +
> +; setjmp/longjmp test with dynamically sized array.
> +; Requires protector.
> +; CHECK: @foo(i32 %[[ARG:.*]])
> +define i32 @foo(i32 %size) nounwind uwtable safestack {
> +entry:
> + ; CHECK: %[[DYNPTR:.*]] = alloca i8*
> + ; CHECK-NEXT: %[[SP:.*]] = load i8*, i8** @__safestack_unsafe_stack_ptr
> + ; CHECK-NEXT: store i8* %[[SP]], i8** %[[DYNPTR]]
> +
> + ; CHECK-NEXT: %[[ZEXT:.*]] = zext i32 %[[ARG]] to i64
> + ; CHECK-NEXT: %[[MUL:.*]] = mul i64 %[[ZEXT]], 4
> + ; CHECK-NEXT: %[[SP2:.*]] = load i8*, i8** @__safestack_unsafe_stack_ptr
> + ; CHECK-NEXT: %[[PTRTOINT:.*]] = ptrtoint i8* %[[SP2]] to i64
> + ; CHECK-NEXT: %[[SUB:.*]] = sub i64 %[[PTRTOINT]], %[[MUL]]
> + ; CHECK-NEXT: %[[AND:.*]] = and i64 %[[SUB]], -16
> + ; CHECK-NEXT: %[[INTTOPTR:.*]] = inttoptr i64 %[[AND]] to i8*
> + ; CHECK-NEXT: store i8* %[[INTTOPTR]], i8** @__safestack_unsafe_stack_ptr
> + ; CHECK-NEXT: store i8* %[[INTTOPTR]], i8** %unsafe_stack_dynamic_ptr
> + ; CHECK-NEXT: %[[ALLOCA:.*]] = inttoptr i64 %[[SUB]] to i32*
> + %a = alloca i32, i32 %size
> +
> + ; CHECK: setjmp
> + ; CHECK-NEXT: %[[LOAD:.*]] = load i8*, i8** %[[DYNPTR]]
> + ; CHECK-NEXT: store i8* %[[LOAD]], i8** @__safestack_unsafe_stack_ptr
> + %call = call i32 @_setjmp(%struct.__jmp_buf_tag* getelementptr inbounds ([1 x %struct.__jmp_buf_tag], [1 x %struct.__jmp_buf_tag]* @buf, i32 0, i32 0)) returns_twice
> +
> + ; CHECK: call void @funcall(i32* %[[ALLOCA]])
> + call void @funcall(i32* %a)
> + ret i32 0
> +}
> +
> +declare i32 @_setjmp(%struct.__jmp_buf_tag*)
> +declare void @funcall(i32*)
>
> Added: llvm/trunk/test/Transforms/SafeStack/struct.ll
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/SafeStack/struct.ll?rev=239761&view=auto
> ==============================================================================
> --- llvm/trunk/test/Transforms/SafeStack/struct.ll (added)
> +++ llvm/trunk/test/Transforms/SafeStack/struct.ll Mon Jun 15 16:07:11 2015
> @@ -0,0 +1,41 @@
> +; RUN: opt -safe-stack -S -mtriple=i386-pc-linux-gnu < %s -o - | FileCheck %s
> +; RUN: opt -safe-stack -S -mtriple=x86_64-pc-linux-gnu < %s -o - | FileCheck %s
> +
> +%struct.foo = type { [16 x i8] }
> +
> + at .str = private unnamed_addr constant [4 x i8] c"%s\0A\00", align 1
> +
> +; struct { [16 x i8] }
> +
> +define void @foo(i8* %a) nounwind uwtable safestack {
> +entry:
> + ; CHECK: %[[USP:.*]] = load i8*, i8** @__safestack_unsafe_stack_ptr
> +
> + ; CHECK: %[[USST:.*]] = getelementptr i8, i8* %[[USP]], i32 -16
> +
> + ; CHECK: store i8* %[[USST]], i8** @__safestack_unsafe_stack_ptr
> +
> + ; CHECK: %[[AADDR:.*]] = alloca i8*, align 8
> + %a.addr = alloca i8*, align 8
> +
> + ; CHECK: %[[BUFPTR:.*]] = getelementptr i8, i8* %[[USP]], i32 -16
> + ; CHECK: %[[BUFPTR2:.*]] = bitcast i8* %[[BUFPTR]] to %struct.foo*
> + %buf = alloca %struct.foo, align 1
> +
> + ; CHECK: store i8* {{.*}}, i8** %[[AADDR]], align 8
> + store i8* %a, i8** %a.addr, align 8
> +
> + ; CHECK: %[[GEP:.*]] = getelementptr inbounds %struct.foo, %struct.foo* %[[BUFPTR2]], i32 0, i32 0, i32 0
> + %gep = getelementptr inbounds %struct.foo, %struct.foo* %buf, i32 0, i32 0, i32 0
> +
> + ; CHECK: %[[A:.*]] = load i8*, i8** %[[AADDR]], align 8
> + %a2 = load i8*, i8** %a.addr, align 8
> +
> + ; CHECK: call i8* @strcpy(i8* %[[GEP]], i8* %[[A]])
> + %call = call i8* @strcpy(i8* %gep, i8* %a2)
> +
> + ; CHECK: store i8* %[[USP]], i8** @__safestack_unsafe_stack_ptr
> + ret void
> +}
> +
> +declare i8* @strcpy(i8*, i8*)
>
>
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits
More information about the llvm-commits
mailing list