Class: Crokus::Compiler

Inherits:
Object
  • Object
show all
Defined in:
lib/crokus/compiler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCompiler

Returns a new instance of Compiler.



23
24
25
26
27
# File 'lib/crokus/compiler.rb', line 23

def initialize
  @options={}
  $options=@options
  @parser=Parser.new
end

Instance Attribute Details

#astObject

Returns the value of attribute ast.



20
21
22
# File 'lib/crokus/compiler.rb', line 20

def ast
  @ast
end

#base_nameObject

Returns the value of attribute base_name.



21
22
23
# File 'lib/crokus/compiler.rb', line 21

def base_name
  @base_name
end

#optionsObject

Returns the value of attribute options.



18
19
20
# File 'lib/crokus/compiler.rb', line 18

def options
  @options
end

#parserObject

Returns the value of attribute parser.



19
20
21
# File 'lib/crokus/compiler.rb', line 19

def parser
  @parser
end

Instance Method Details

#build_cfgObject



99
100
101
102
103
# File 'lib/crokus/compiler.rb', line 99

def build_cfg
  puts "[+] building CFGs" unless options[:mute]
  builder=CFGBuilder.new
  builder.build(ast)
end

#build_tacObject



105
106
107
108
109
# File 'lib/crokus/compiler.rb', line 105

def build_tac
  puts "[+] building TAC" unless options[:mute]
  builder=TACBuilder.new
  builder.visit(ast)
end

#compile(filename) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/crokus/compiler.rb', line 29

def compile filename
  parse(filename)
  return true if options[:parse_only]

  draw_ast(@ast) if options[:ast]

  build_cfg
  return true if options[:cfg]

  if options[:print_cfg_dot]
    print_cfg_only(:dot)
    return true
  end

  if options[:print_cfg_json]
    print_cfg_only(:json)
    return true
  end

  pretty_print

  if options[:trojan]
    return_code=insert_trojan()
    return return_code
  end

  build_tac
  return true if options[:tac]

  emit_ir
  return true
end

#draw_ast(tree = nil, filename = nil) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/crokus/compiler.rb', line 71

def draw_ast tree=nil,filename=nil
  dotname=filename || "#{base_name}_ast.dot"
  puts "   |--[+] drawing AST '#{dotname}'" unless options[:mute]
  ast_ = tree || @ast
  dot=AstPrinter.new.print(ast_)
  dot.save_as dotname
end

#emit_irObject



111
112
113
114
# File 'lib/crokus/compiler.rb', line 111

def emit_ir
  puts "[+] emit textual IR " unless options[:mute]
  IRDumper.new.visit(ast)
end

#execute(params) ⇒ Object



126
127
128
# File 'lib/crokus/compiler.rb', line 126

def execute params
  RandomGen.new.run(params)
end

#insert_trojanObject



130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/crokus/compiler.rb', line 130

def insert_trojan
  puts "[+] inserting trojan" unless options[:mute]
  infected_ast=TrojanInserter.new(@options).insert(ast)
  if infected_ast
    code=PrettyPrinter.new.visit(infected_ast)
    pp_c=@base_name+"_troj.c"
    File.open(pp_c,'w'){|f| f.puts code}
    puts " "*5+"|--> saved as #{pp_c}" unless options[:mute]
    return true
  else
    return false
  end
end

#parse(filename) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/crokus/compiler.rb', line 62

def parse filename
  @base_name=File.basename(filename, ".c")
  code=IO.read(filename)
  puts "[+] parsing #{filename}" unless options[:mute]
  @ast=Parser.new.parse(code)
  draw_ast     if options[:draw_ast]
  pretty_print if options[:pp]
end

#pretty_printObject



91
92
93
94
95
96
97
# File 'lib/crokus/compiler.rb', line 91

def pretty_print
  puts "[+] pretty_print" unless options[:mute]
  code=PrettyPrinter.new.visit(ast)
  pp_c=@base_name+"_pp.c"
  File.open(pp_c,'w'){|f| f.puts code}
  puts " "*1+"|--[+] saved as #{pp_c}" unless options[:mute]
end


116
117
118
119
120
121
122
123
124
# File 'lib/crokus/compiler.rb', line 116

def print_cfg_only format
  puts "[+] print textual CFG only format #{format}" unless options[:mute]
  case format
  when :dot
    CFGOnlyPrinterDot.new.visit(ast)
  when :json
    CFGOnlyPrinterJson.new.visit(ast)
  end
end

#transformObject



79
80
81
82
83
84
# File 'lib/crokus/compiler.rb', line 79

def transform
  puts "[+] dummy transform" unless options[:mute]
  ast_t= Transformer.new.transform(ast)
  dotname="#{base_name}_trans.dot"
  draw_ast ast_t,dotname
end

#visitObject



86
87
88
89
# File 'lib/crokus/compiler.rb', line 86

def visit
  puts "[+] dummy visit" unless options[:mute]
  Visitor.new.visit(ast)
end