blob: 1e40000a9432028b18c43421f9d46d5aaefd149d [file] [log] [blame]
Gordon Henriksend930f912008-08-17 18:44:35 +00001//===-- ShadowStackGC.cpp - GC support for uncooperative targets ----------===//
Gordon Henriksen6047b6e2008-01-07 01:30:53 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements lowering for the llvm.gc* intrinsics for targets that do
11// not natively support them (which includes the C backend). Note that the code
Gordon Henriksend930f912008-08-17 18:44:35 +000012// generated is not quite as efficient as algorithms which generate stack maps
Gordon Henriksen6047b6e2008-01-07 01:30:53 +000013// to identify roots.
14//
15// This pass implements the code transformation described in this paper:
16// "Accurate Garbage Collection in an Uncooperative Environment"
17// Fergus Henderson, ISMM, 2002
18//
19// In runtime/GC/SemiSpace.cpp is a prototype runtime which is compatible with
Gordon Henriksend930f912008-08-17 18:44:35 +000020// ShadowStackGC.
Gordon Henriksen6047b6e2008-01-07 01:30:53 +000021//
22// In order to support this particular transformation, all stack roots are
23// coallocated in the stack. This allows a fully target-independent stack map
24// while introducing only minor runtime overhead.
25//
26//===----------------------------------------------------------------------===//
27
Chandler Carruthed0881b2012-12-03 16:50:05 +000028#include "llvm/CodeGen/GCs.h"
Philip Reames56a03932015-01-26 18:26:35 +000029#include "llvm/CodeGen/GCStrategy.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000030#include "llvm/ADT/StringExtras.h"
Chandler Carruth219b89b2014-03-04 11:01:28 +000031#include "llvm/IR/CallSite.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000032#include "llvm/IR/IRBuilder.h"
33#include "llvm/IR/IntrinsicInst.h"
34#include "llvm/IR/Module.h"
Gordon Henriksen6047b6e2008-01-07 01:30:53 +000035
36using namespace llvm;
37
Chandler Carruth1b9dde02014-04-22 02:02:50 +000038#define DEBUG_TYPE "shadowstackgc"
39
Gordon Henriksen6047b6e2008-01-07 01:30:53 +000040namespace {
Philip Reames36319532015-01-16 23:16:12 +000041class ShadowStackGC : public GCStrategy {
Philip Reames36319532015-01-16 23:16:12 +000042public:
43 ShadowStackGC();
Philip Reames36319532015-01-16 23:16:12 +000044};
Dan Gohmand78c4002008-05-13 00:00:25 +000045}
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +000046
Gordon Henriksend930f912008-08-17 18:44:35 +000047static GCRegistry::Add<ShadowStackGC>
Philip Reames36319532015-01-16 23:16:12 +000048 X("shadow-stack", "Very portable GC for uncooperative code generators");
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +000049
Philip Reames36319532015-01-16 23:16:12 +000050void llvm::linkShadowStackGC() {}
Gordon Henriksen6047b6e2008-01-07 01:30:53 +000051
Philip Reames23cf2e22015-01-28 19:28:03 +000052ShadowStackGC::ShadowStackGC() {
Gordon Henriksen6047b6e2008-01-07 01:30:53 +000053 InitRoots = true;
54 CustomRoots = true;
55}