Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/smack/DSAWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class DSAWrapper : public llvm::ModulePass {

bool isStaticInitd(const seadsa::Node *n);
bool isMemOpd(const seadsa::Node *n);
bool isAccessed(const llvm::Value *V);
bool isRead(const llvm::Value *V);
bool isSingletonGlobal(const llvm::Value *V);
unsigned getPointedTypeSize(const llvm::Value *v);
Expand Down
3 changes: 2 additions & 1 deletion include/smack/SmackRep.h
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,8 @@ class SmackRep {
const Stmt *inverseFPCastAssume(const llvm::StoreInst *si);

// used in SmackModuleGenerator
std::list<Decl *> globalDecl(const llvm::GlobalValue *g);
std::list<Decl *> globalDecl(const llvm::GlobalValue *g,
bool allocate = true);
void addInitFunc(const llvm::Function *f);
Decl *getInitFuncs();
const Expr *declareIsExternal(const Expr *e);
Expand Down
6 changes: 6 additions & 0 deletions lib/smack/DSAWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ bool DSAWrapper::isMemOpd(const seadsa::Node *n) {
return memOpds.count(n) > 0;
}

bool DSAWrapper::isAccessed(const Value *V) {
auto node = getNode(V);
assert(node && "Global values should have nodes.");
return node->isRead() || node->isModified();
}

bool DSAWrapper::isRead(const Value *V) {
auto node = getNode(V);
assert(node && "Global values should have nodes.");
Expand Down
7 changes: 6 additions & 1 deletion lib/smack/SmackModuleGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#define DEBUG_TYPE "smack-mod-gen"
#include "smack/SmackModuleGenerator.h"
#include "smack/BoogieAst.h"
#include "smack/DSAWrapper.h"
#include "smack/Debug.h"
#include "smack/Naming.h"
#include "smack/Prelude.h"
Expand All @@ -25,6 +26,8 @@ void SmackModuleGenerator::getAnalysisUsage(llvm::AnalysisUsage &AU) const {
AU.setPreservesAll();
AU.addRequired<llvm::LoopInfoWrapperPass>();
AU.addRequired<Regions>();
if (SmackOptions::MemorySafety)
AU.addRequired<DSAWrapper>();
}

bool SmackModuleGenerator::runOnModule(llvm::Module &m) {
Expand All @@ -37,11 +40,13 @@ void SmackModuleGenerator::generateProgram(llvm::Module &M) {
Naming naming;
SmackRep rep(&M.getDataLayout(), &naming, program, &getAnalysis<Regions>());
std::list<Decl *> &decls = program->getDeclarations();
DSAWrapper *DSA =
SmackOptions::MemorySafety ? &getAnalysis<DSAWrapper>() : nullptr;

SDEBUG(errs() << "Analyzing globals...\n");

for (auto &G : M.globals()) {
auto ds = rep.globalDecl(&G);
auto ds = rep.globalDecl(&G, !DSA || DSA->isAccessed(&G));
decls.insert(decls.end(), ds.begin(), ds.end());
}

Expand Down
5 changes: 3 additions & 2 deletions lib/smack/SmackRep.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1261,7 +1261,8 @@ void SmackRep::addAllocSizeAttr(const llvm::GlobalVariable *G,
}
}

std::list<Decl *> SmackRep::globalDecl(const llvm::GlobalValue *v) {
std::list<Decl *> SmackRep::globalDecl(const llvm::GlobalValue *v,
bool allocate) {
using namespace llvm;
std::list<Decl *> decls;
std::list<const Attr *> ax;
Expand Down Expand Up @@ -1322,7 +1323,7 @@ std::list<Decl *> SmackRep::globalDecl(const llvm::GlobalValue *v) {
Expr::id(name), pointerLit(globalsOffset -= (size + globalsPadding)))));
}

if (!llvm::isa<Function>(v))
if (!llvm::isa<Function>(v) && allocate)
globalAllocations[v] = size;

return decls;
Expand Down
8 changes: 8 additions & 0 deletions test/c/memory-safety/global_alloc_alias.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include "smack.h"

// @expect verified

int x;
int *p = &x;

int main(void) { return *p; }
11 changes: 11 additions & 0 deletions test/c/memory-safety/global_alloc_unused.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "smack.h"

// @expect verified
// @checkbpl awk '/call \$galloc/ { found=1 } END { exit found }'

void loga(char *);

int main(void) {
loga("aaa");
return 0;
}
Loading