This is an archive of the discontinued LLVM Phabricator instance.

[AArch64] Stack probing for dynamic allocas in GlobalISel

Authored by ostannard on Feb 4 2021, 2:30 AM.



This adds a stack probing instruction sequence for dynamic stack
allocations, to protect against stack clash attacks. The instruction
sequence used is the same one used for unknown-size allocations in
function prologues.

Diff Detail

Event Timeline

ostannard created this revision.Feb 4 2021, 2:30 AM
ostannard requested review of this revision.Feb 4 2021, 2:30 AM
Herald added a project: Restricted Project. · View Herald TranscriptFeb 4 2021, 2:30 AM
rovka added a comment.Feb 5 2021, 1:28 AM

Can you please upload with context? :) Thanks!

lkail added a subscriber: lkail.Feb 5 2021, 2:14 AM
ostannard updated this revision to Diff 321688.Feb 5 2021, 2:37 AM
rovka added a comment.Feb 5 2021, 3:41 AM

Seems fine from a GlobalISel perspective, and I'm guessing the stack clashing details will be reviewed in the SelectionDAG patch which adds the test.


Why is this custom only for {p0, s64} ? It seems all the existing tests for G_DYN_STACKALLOC are for {p0, s64}, so I'm not sure when the other case is even possible and why we can't use the probing then too.


Would it be a good idea to share some of the code between this handler and lowerDynStackAlloc? Maybe add a method to LegalizeHelper that does the allocation and returns the SPTmp. , then the code here would be just
/* check if stack probing is enabled */
SPTmp = Helper.allocateStack /* (or some other suggestive name) */
/* generate PROBED_STACKALLOC_DYN etc */


I love that this test has the same checks for GlobalISel and SelectionDAG :)

Nitpick: I think it would be nice to also test this in llvm/test/CodeGen/AArch64/GlobalISel/legalize-dyn-alloca.mir, which covers just the legalizer.

ostannard marked 3 inline comments as done.Feb 8 2021, 2:47 AM
ostannard added inline comments.

This is the only combination of types emitted by the IR translator, but I guess this will emit a non-probed alloca if that ever changes, so I'll change it to assert that the types are what we expect.

ostannard updated this revision to Diff 322060.Feb 8 2021, 2:49 AM
ostannard marked an inline comment as done.
  • Assert if types aren't what we expect, instead of emitting an un-probed SP move.
  • Share code with LegalizerHelper
  • Add MIR tests for the legalizer
rovka accepted this revision.Feb 9 2021, 4:40 AM

LGTM as far as GlobalISel is concerned.

This revision is now accepted and ready to land.Feb 9 2021, 4:40 AM
alex added a subscriber: alex.Feb 22 2021, 4:32 PM
emaste added a subscriber: emaste.Aug 23 2021, 12:04 PM
Herald added a project: Restricted Project. · View Herald TranscriptFeb 21 2023, 11:13 AM