Skip to content

Commit c5b19f3

Browse files
committed
Subtree merged in andrew reiter
1 parent 9dd1b44 commit c5b19f3

File tree

111 files changed

+9675
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

111 files changed

+9675
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*~
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
This repo contains some materials related to a talk
3+
on security r&d projects that in some way use LLVM
4+
for doing their work.
5+
6+
7+
- projects.md is a list of such projects
8+
- code is a set of very basic, example code to help those interested in getting up to speed with some basics of LLVM. Don't hate.
9+
- slides is the slide deck
10+
11+
#### Again on the code
12+
I must repeat myself: the provided code is only meant to be basic helpers for learning about LLVM
13+
and is not meant to be some new awesome tool (sadly, no). There is a great deal of research in
14+
dynamic and program analysis that is being done and the goal of this code is to make it so you
15+
can start to more easily read some of the code from those projects ... and then make sense of it.
16+
This is to avoid getting lost in code versus meaning.
17+
18+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Very basic example codes.
2+
3+
- bbskel: BasicBlock pass skeleton
4+
- cgspskel: CallGraphSCC pass skeleton
5+
- comminute: tool illustrating pass mgr, pass deps, and more
6+
- fpskel: FunctionPass skeleton
7+
- intflip: integer argument randomizer|bit-flipper
8+
- mpskel: ModulePass skeleton
9+
- npassert: NULL pointer check insertion
10+
- rpskel: RegionPass skeleton
11+
- visitorskel: Using a InstVisitor class
12+
13+
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
LLVM_VER=3.9
2+
LLVM_HOME=/usr/bin
3+
LLVM_CONFIG?=$(LLVM_HOME)/llvm-config-$(LLVM_VER)
4+
5+
ifndef VERBOSE
6+
QUIET:=@
7+
endif
8+
9+
SRC_DIR?=$(PWD)/src
10+
11+
CXX=$(LLVM_HOME)/clang++-$(LLVM_VER)
12+
CC=$(LLVM_HOME)/clang-$(LLVM_VER)
13+
OPT=$(LLVM_HOME)/opt-$(LLVM_VER)
14+
DIS=$(LLVM_HOME)/llvm-dis-$(LLVM_VER)
15+
LNK=$(LLVM_HOME)/llvm-link-$(LLVM_VER)
16+
17+
LDFLAGS+=$(shell $(LLVM_CONFIG) --ldflags)
18+
LDFLAGS+=-shared -Wl,-O1
19+
20+
CXXFLAGS+=-I$(shell $(LLVM_CONFIG) --includedir)
21+
CXXFLAGS+=-std=c++11 -fPIC -fvisibility-inlines-hidden
22+
CXXFLAGS+=-Wall -Wextra -g -Wno-unused-parameter -Wno-unused-variable
23+
24+
CPPFLAGS+=$(shell $(LLVM_CONFIG) --cppflags)
25+
CPPFLAGS+=-I$(SRC_DIR)
26+
27+
28+
PASS=BBSkel.so
29+
PASS_OBJECTS=BBSkel.o
30+
31+
default: prep $(PASS)
32+
33+
prep:
34+
$(QUIET)mkdir -p built
35+
36+
%.o : $(SRC_DIR)/%.cpp
37+
@echo Compiling $*.cpp
38+
$(QUIET)$(CXX) -o built/$*.o -c $(CPPFLAGS) $(CXXFLAGS) $<
39+
40+
$(PASS) : $(PASS_OBJECTS)
41+
@echo Linking $@
42+
$(QUIET)$(CXX) -o built/$@ $(LDFLAGS) $(CXXFLAGS) built/*.o
43+
44+
clean:
45+
$(QUIET)rm -rf built test/*.bc
46+
47+
48+
tests:
49+
$(CC) -emit-llvm -o test/foo.bc -c test/foo.c
50+
51+
runtests:
52+
$(OPT) -load built/BBSkel.so -mem2reg -bbskel < test/foo.bc
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
# BBSkel
3+
4+
This is a basic block pass skeleton. Note, there are also
5+
loop passes, region passes, etc.
6+
7+
8+
# Build & Run
9+
10+
First check the Makefile to set path to llvm-config and version.
11+
3.8, 3.9 should be fine, so should 4.0
12+
13+
```
14+
$ make
15+
$ opt-X.Y -load built/BBSkel.so -bbskel < file.bc
16+
...
17+
$
18+
```
19+
20+
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#include "llvm/IR/Module.h"
2+
#include "llvm/IR/Function.h"
3+
#include "llvm/IR/Instructions.h"
4+
#include "llvm/Support/raw_ostream.h"
5+
6+
using namespace llvm;
7+
8+
#include "BBSkel.h"
9+
10+
void
11+
BBSkel::getAnalysisUsage(AnalysisUsage &AU) const
12+
{
13+
// No changes to CFG, so tell the pass manager
14+
AU.setPreservesCFG();
15+
}
16+
17+
bool
18+
BBSkel::doFinalization(Function &F)
19+
{
20+
errs() << "#BBs: " << bbcount << "\n";
21+
errs() << "#Is: " << icount << "\n";
22+
return false;
23+
}
24+
25+
bool
26+
BBSkel::runOnBasicBlock(BasicBlock &B)
27+
{
28+
bbcount++;
29+
errs() << " Basic Block found:\n";
30+
for (auto &I : B) { // Iterate through instructions in the block
31+
++icount;
32+
// Note in output if instruction is a call/invoke
33+
if (isa<CallInst>(I) || isa<InvokeInst>(I)) {
34+
errs() << " C ";
35+
} else {
36+
errs() << " ";
37+
}
38+
I.dump();
39+
errs() << " used by:\n";
40+
// Go through and dump the uses for each instruction.
41+
for (auto ui = I.user_begin(); ui != I.user_end(); ++ui) {
42+
errs() << " U: ";
43+
ui->dump();
44+
}
45+
errs() << " ~~~~ \n";
46+
}
47+
errs() << " --- end of basic block ---\n";
48+
49+
// return true if CFG has changed.
50+
return false;
51+
}
52+
53+
54+
55+
/*
56+
* Register this pass to be made usable.
57+
* Needs the static ID initialized and the pass declaration given.
58+
*/
59+
char BBSkel::ID = 0;
60+
static RegisterPass<BBSkel> XX("bbskel", "BasicBlock Pass Skeleton");
61+
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#ifndef __BBSKEL_H
2+
#define __BBSKEL_H
3+
4+
struct BBSkel : public BasicBlockPass {
5+
/*
6+
* For all of your passes you will need this and to define it.
7+
* It's address is used by pass system, so the value does not matter.
8+
*/
9+
static char ID;
10+
11+
unsigned bbcount;
12+
unsigned icount;
13+
14+
BBSkel() : BasicBlockPass(ID) {
15+
bbcount = 0;
16+
icount = 0;
17+
}
18+
19+
// Called on each BasicBlock in given compilation unit
20+
virtual bool runOnBasicBlock(BasicBlock &);
21+
22+
/*
23+
* Used to help order passes by pass manager.
24+
* Declare any passes you need run prior here.. as well as
25+
* any information such as preserving CFG or similar.
26+
*/
27+
virtual void getAnalysisUsage(AnalysisUsage &) const;
28+
29+
/*
30+
* Called after each exec of a runOnBasicBlock.
31+
*/
32+
virtual bool doFinalization(Function &);
33+
};
34+
35+
#endif
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
#include <unistd.h>
4+
#include <sys/types.h>
5+
#include <sys/socket.h>
6+
#include <netdb.h>
7+
8+
void
9+
leaks_passwd()
10+
{
11+
char *p;
12+
struct addrinfo hints, *result;
13+
14+
p = getpass("enter passwd: ");
15+
memset(&hints, 0, sizeof(struct addrinfo));
16+
hints.ai_family = AF_UNSPEC;
17+
hints.ai_socktype = SOCK_DGRAM;
18+
hints.ai_flags = 0;
19+
hints.ai_protocol = 0;
20+
(void)getaddrinfo(p, "http", &hints, &result);
21+
}
22+
23+
int
24+
main(int argc, char **argv)
25+
{
26+
leaks_passwd();
27+
return 0;
28+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
LLVM_VER=3.9
2+
LLVM_HOME=/usr/bin
3+
LLVM_CONFIG?=$(LLVM_HOME)/llvm-config-$(LLVM_VER)
4+
5+
ifndef VERBOSE
6+
QUIET:=@
7+
endif
8+
9+
SRC_DIR?=$(PWD)/src
10+
11+
CXX=$(LLVM_HOME)/clang++-$(LLVM_VER)
12+
CC=$(LLVM_HOME)/clang-$(LLVM_VER)
13+
OPT=$(LLVM_HOME)/opt-$(LLVM_VER)
14+
DIS=$(LLVM_HOME)/llvm-dis-$(LLVM_VER)
15+
LNK=$(LLVM_HOME)/llvm-link-$(LLVM_VER)
16+
17+
LDFLAGS+=$(shell $(LLVM_CONFIG) --ldflags)
18+
LDFLAGS+=-shared -Wl,-O1
19+
20+
CXXFLAGS+=-I$(shell $(LLVM_CONFIG) --includedir)
21+
CXXFLAGS+=-std=c++11 -fPIC -fvisibility-inlines-hidden
22+
CXXFLAGS+=-Wall -Wextra -g -Wno-unused-parameter -Wno-unused-variable
23+
24+
CPPFLAGS+=$(shell $(LLVM_CONFIG) --cppflags)
25+
CPPFLAGS+=-I$(SRC_DIR)
26+
27+
PASS=CGSSkel.so
28+
PASS_OBJECTS=CGSSkel.o
29+
30+
default: prep $(PASS)
31+
32+
prep:
33+
$(QUIET)mkdir -p built
34+
35+
%.o : $(SRC_DIR)/%.cpp
36+
@echo Compiling $*.cpp
37+
$(QUIET)$(CXX) -o built/$*.o -c $(CPPFLAGS) $(CXXFLAGS) $<
38+
39+
$(PASS) : $(PASS_OBJECTS)
40+
@echo Linking $@
41+
$(QUIET)$(CXX) -o built/$@ $(LDFLAGS) $(CXXFLAGS) built/*.o
42+
43+
clean:
44+
$(QUIET)rm -rf built test/*.bc
45+
46+
47+
tests:
48+
$(CC) -emit-llvm -o test/foo.bc -c test/foo.c
49+
50+
runtests:
51+
$(OPT) -load built/CGSSkel.so -cgsskel < test/foo.bc
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
# CallGraphSCCPass Skeleton
3+
4+
Bottom up style to assist to use + augment for CG building
5+
6+
# Build & Run
7+
8+
First check the Makefile to set path to llvm-config and version.
9+
3.8, 3.9 should be fine, so should 4.0
10+
11+
```
12+
$ make
13+
$ opt-X.Y -load built/CGSSkel.so -cgsskel < file.bc
14+
...
15+
$
16+
```
17+
18+

0 commit comments

Comments
 (0)