qira_middleware works

This commit is contained in:
George Hotz 2014-07-02 09:57:20 -07:00
parent 3555dba946
commit c0fab7e3f2
7 changed files with 72 additions and 39 deletions

20
deps.sh
View File

@ -1,4 +1,24 @@
#!/bin/bash
set -e
sudo pip install pymongo
curl -k https://install.meteor.com | /bin/sh
if [ ! -d qemu/qemu-latest ]; then
rm -rf qemu
mkdir -p qemu
cd qemu
wget http://wiki.qemu-project.org/download/qemu-2.1.0-rc0.tar.bz2
tar xf qemu-2.1.0-rc0.tar.bz2
ln -s qemu-2.1.0-rc0 qemu-latest
cd qemu-latest
mv tci.c tci.c.bak
cd ../../
fi
cd qemu/qemu-latest
rm -f tci.c
ln -s ../../qemu_mods/tci.c tci.c
./configure --target-list=i386-linux-user --enable-tcg-interpreter --enable-debug-tcg --cpu=unknown
make -j32

View File

@ -1,20 +0,0 @@
#!/bin/bash
if [ ! -d qemu/qemu-latest ]; then
rm -rf qemu
mkdir -p qemu
cd qemu
wget http://wiki.qemu-project.org/download/qemu-2.1.0-rc0.tar.bz2
tar xf qemu-2.1.0-rc0.tar.bz2
ln -s qemu-2.1.0-rc0 qemu-latest
cd qemu-latest
mv tci.c tci.c.bak
cd ../../
fi
cd qemu/qemu-latest
rm -f tci.c
ln -s ../../qemu_mods/tci.c tci.c
./configure --target-list=i386-linux-user --enable-tcg-interpreter --enable-debug-tcg --cpu=unknown
make -j32

4
go.sh
View File

@ -1,10 +1,10 @@
#!/bin/bash
set -e
#BIN=../tests/ctf/ezhp
BIN=../tests/ctf/ezhp
#BIN=../tests/ctf/hudak
#BIN=../tests/ctf/simple
SRC=../tests/hello.c
#SRC=../tests/hello.c
#SRC=../tests/algo.c
if [ $SRC != "" ]; then

View File

@ -20,15 +20,19 @@ def flag_to_type(flags):
typ = "W"
elif not flags & IS_WRITE and not flags & IS_MEM:
typ = "R"
return typ
def get_log_length(fn):
dat = open(fn).read(4)
return struct.unpack("I", dat)[0]
def read_log(fn, seek=1):
def read_log(fn, seek=1, cnt=0):
f = open(fn)
f.seek(seek*0x18)
dat = f.read()
if cnt == 0:
dat = f.read()
else:
dat = f.read(cnt * 0x18)
ret = []
for i in range(0, len(dat), 0x18):

55
scripts/qira_middleware.py Normal file → Executable file
View File

@ -1,26 +1,29 @@
#!/usr/bin/env python
from qira_log import *
from qira_memory import *
import subprocess
import time
import sys
import json
from pymongo import MongoClient
# global state for the program
instructions = {}
pmaps = {}
regs = Memory()
mem = Memory()
pmaps = {}
def process(log_entries):
global instructions, pmaps, regs, mem
db = MongoClient('localhost', 3001).meteor
Change = db.change
Pmaps = db.pmaps
db_changes = []
new_pmaps = pmaps.copy()
for (address, data, clnum, flags) in dat:
for (address, data, clnum, flags) in log_entries:
# Changes database
this_change = {'address': address, 'type': flag_to_type(flags),
'size': flags&SIZE_MASK, 'clnum': clnum, 'data': data}
@ -42,7 +45,7 @@ def process(log_entries):
# for Pmaps
page_base = address & 0xFFFFF000
if flags & IS_MEM and page_base not in addrs:
if flags & IS_MEM and page_base not in new_pmaps:
new_pmaps[page_base] = "memory"
if flags & IS_START:
new_pmaps[page_base] = "instruction"
@ -56,16 +59,25 @@ def process(log_entries):
db_pmaps = []
for i in new_pmaps:
if i not in pmaps or pmaps[i] != new_pmaps[i]:
pmaps.append({"address": i, "type": new_pmaps[i]})
Pmaps.insert(db_pmaps)
db_pmaps.append({"address": i, "type": new_pmaps[i]})
if len(db_pmaps) > 0:
Pmaps.insert(db_pmaps)
pmaps = new_pmaps
# push changes to db
Change.insert(db_changes)
if len(db_changes) > 0:
Change.insert(db_changes)
if __name__ == '__main__':
print "starting QIRA middleware"
def init():
global instructions, pmaps, regs, mem
instructions = {}
pmaps = {}
regs = Memory()
mem = Memory()
print "reset program state"
objdump_out = subprocess.Popen(
["objdump", "-d", "/tmp/qira_binary"],
stdout = subprocess.PIPE).communicate()[0]
@ -77,10 +89,14 @@ if __name__ == '__main__':
#print hex(addr), line[2]
else:
# could get names here too, but maybe useless for now
#print line
pass
print "objdump parse got",len(instructions),"instructions"
open("/tmp/qira_memdb", "wb").write(
json.dumps({"regs": regs.dump(), "mem": mem.dump()}))
print "wrote initial qira_memdb"
# connect to db, set up collections, and drop
db = MongoClient('localhost', 3001).meteor
Change = db.change
@ -89,11 +105,24 @@ if __name__ == '__main__':
Pmaps.drop()
print "dropped old databases"
# run loop run
if __name__ == '__main__':
print "starting QIRA middleware"
init()
changes_committed = 1
# run loop run
while 1:
time.sleep(0.05)
max_changes = get_log_length(LOGFILE)
if max_changes < changes_committed:
print "RESTART..."
init()
changes_committed = 1
if changes_committed < max_changes:
process(read_log(LOGFILE, changes_committed))
sys.stdout.write("going from %d to %d..." % (changes_committed,max_changes))
sys.stdout.flush()
process(read_log(LOGFILE, changes_committed, max_changes - changes_committed))
print "done"
changes_committed = max_changes

View File

@ -1,4 +1,4 @@
Change = new Meteor.Collection("change");
Program = new Meteor.Collection("program");
//Program = new Meteor.Collection("program");
Pmaps = new Meteor.Collection("pmaps");

View File

@ -47,7 +47,7 @@ function map_getbelow(map, a) {
function read_memdb() {
fs.readFile("/tmp/qira_memdb", function(err, data) {
if (err) throw err;
if (err) { console.log(err); return; }
console.log("read memdb");
var dat = JSON.parse(data);
regs = map_create(dat['regs']);