This is an archive of the discontinued LLVM Phabricator instance.

[SafeStack] [SSP] Use llvm.stackguard intrinsic.
Changes PlannedPublic

Authored by koriakin on Apr 22 2016, 3:12 AM.

Details

Reviewers
eugenis
timshen
Summary

This is necessary for SafeStack support on PPC64.

Diff Detail

Repository
rL LLVM

Event Timeline

koriakin retitled this revision from to [SafeStack] [SSP] Use llvm.stackguard intrinsic..
koriakin updated this object.
koriakin added reviewers: timshen, eugenis.
koriakin set the repository for this revision to rL LLVM.
koriakin added a subscriber: llvm-commits.

One thing I really don't like here is the necessity of calling insertSSPDeclarations - that should be handled by whoever is expanding the intrinsic. Eg. the following:

define i8 *@foo() {
        %1 = call i8* @llvm.stackguard();
        ret i8 *%1;
}

declare i8* @llvm.stackguard() nounwind;

currently segfaults llc on x86_64-freebsd, since it lacks __stack_chk_guard declaration. However, I don't really know where to add these global variables - the Module is const in SelectionDAGBuilder, where we expand the intrinsics. Any hints?

timshen edited edge metadata.Apr 22 2016, 11:27 AM

Sorry about insertSSPDeclarations - I don't like it neither, but I didn't know a better solution.

lib/CodeGen/SafeStack.cpp
400

You may want to take a look at "getStackGuard()" in lib/CodeGen/StackProtector.cpp and possible factor it out (SupportsSelectionDAGSP is a bit weird, sorry), since it also takes care of IR form SSP.

eugenis edited edge metadata.Apr 22 2016, 11:52 AM

Cool!

I wonder if we could get SDAG SSP for free now by updating the following code to look at SafeStack, too:
getAnalysis<StackProtector>().shouldEmitSDCheck(*LLVMBB)

I wonder if we could get SDAG SSP for free now by updating the following code to look at SafeStack, too:
getAnalysis<StackProtector>().shouldEmitSDCheck(*LLVMBB)

I'm not familiar with SafeStack, but SafeStack SSP does have a different type of canary, right? Then this line will crash: https://github.com/llvm-mirror/llvm/blob/ac94d4bd341c54f663f9622357da7eaad905a7c9/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp#L5336

Cool!

I wonder if we could get SDAG SSP for free now by updating the following code to look at SafeStack, too:
getAnalysis<StackProtector>().shouldEmitSDCheck(*LLVMBB)

I don't quite follow - the canary in SafeStack is a different beast (for one, it's on the other stack), and needs much fewer hacks (eg. we don't need to avoid spilling it).

koriakin added inline comments.Apr 27 2016, 7:21 AM
lib/CodeGen/SafeStack.cpp
400

The functions look about the same. The one in StackProtector emits a volatile load, but changing SafeStack to use one too shouldn't be much of a problem, right? However, I now see I've introduced a problem in SafeStack - I'm calling insertSSPDeclarations on TL, which may be null (in which case getIRStackGuard isn't called either, ugh). I'm not really sure how to deal with that - should we require a target machine when instantiating the SafeStack pass? It would be best if we could just emit the stackguard intrinsic there and somone lower just took care of it...

timshen added inline comments.Apr 27 2016, 11:04 AM
lib/CodeGen/SafeStack.cpp
400

I think the nullability of TL is a separate issue. If you don't have TL around then I don't know how may that happen, nor what to do.

If TL isn't nullptr, calling the factored out getStackGuard() in StackProtector.cpp seems to be good.

And as I said, I don't know any clean solution for insertSSPDeclarations. There exists very low level code (e.g. expandLoadStackGuard in X86InstrInfo.cpp) that is using the Value* form of the global variable.

The only instance of creating a global variable during SelectionDAG phase I found is in LowerToTLSEmulatedModel, and it rather unceremoniously const_cast<>s the module. I'll keep digging...

koriakin planned changes to this revision.May 1 2016, 1:10 PM