crokus 0.1.1 → 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6b12095d7e84e7b72f5e38eaa2901dc98044ffd7c42b582a80a72364be03ef29
4
- data.tar.gz: 43dbd24919d3ec3e73981d8b9f22345a6f0885b5d1487ace5fbcc00d84070394
3
+ metadata.gz: b690523ea19dfb9b7eaa5f0569ab5b4d9cba857ca8e4a451907af3b28c7749c7
4
+ data.tar.gz: 2313aac03fc005f09dbee2925a64738e0c85fe0a343f1de0a426ce73a9307eab
5
5
  SHA512:
6
- metadata.gz: 43c1ab8d6bf5f8a798c8d97ca65de2b04e12ed6f6ce6e76f45ab91813c7484a8c37726c4133056c940cf2366386d8bbd3020afac5ed663dbc14809b7de58f5af
7
- data.tar.gz: ada4e46905d24b2cda56582b03f8569baac7bce9b104f548cba1fb594799c901b54c3f7722478dd3a2275b918c0a0f6b962d1f3d74df11d7d562b35ddcee6c46
6
+ metadata.gz: 3b8be391422d91f28ffc75c0d86f00098f9ba17f2d64a277a28dee774348bb725b558a1a469d11831d07ac58f9dbab3a08a37779f3c4a3672aa06f6f94374041
7
+ data.tar.gz: 89aeab45274244cadd1758cd17a2ca2364db45ec3b3f1ed748caf2c6a6840cf9c9a9a99cf0e4f6a90844a2c623f0315bb26c7195650dd55c2675367f805b164e
@@ -35,6 +35,10 @@ module Crokus
35
35
  def to_s
36
36
  @tok.val
37
37
  end
38
+
39
+ def self.create str
40
+ Ident.new(Token.create(str))
41
+ end
38
42
  end
39
43
 
40
44
  class Include < Ast
@@ -35,7 +35,7 @@ module Crokus
35
35
  @bbs << bb
36
36
  end
37
37
 
38
- def print
38
+ def print versbose=false
39
39
  CFGPrinter.new.print(self,5)
40
40
  end
41
41
 
@@ -1,5 +1,6 @@
1
1
  require_relative 'cfg'
2
2
  require_relative 'cfg_cleaner'
3
+ require_relative 'cfg_optim'
3
4
 
4
5
  require_relative 'visitor'
5
6
  require_relative 'cleaner'
@@ -24,6 +25,7 @@ module Crokus
24
25
  func.body.accept(self)
25
26
  @cfg.print
26
27
  @cfg=CFGCleaner.new.clean(@cfg)
28
+ @cfg=CFGOptimizer.new.clean(@cfg)
27
29
  @cfg.name=Ident.new(Token.create "#{@cfg.name}_clean")
28
30
  func.cfg=@cfg
29
31
  puts " "*5+"|--[+] cfg size for '#{func.name}' : #{@cfg.size}" unless $options[:mute]
@@ -126,6 +128,10 @@ module Crokus
126
128
  @current=falseBranch
127
129
  end
128
130
 
131
+ def visitFunCall fcall,args=nil
132
+ @current << fcall
133
+ end
134
+
129
135
  def visitFor for_,args=nil
130
136
  for_.init.each{|stmt| stmt.accept(self)}
131
137
  cond=for_.cond.accept(self)
@@ -148,6 +154,10 @@ module Crokus
148
154
  @current=falseBranch
149
155
  end
150
156
 
157
+ def visitReturn ret,args=nil
158
+ @current << ret
159
+ end
160
+
151
161
  def visitDoWhile dowhile,args=nil
152
162
  @cfg << cond_bb = BasicBlock.new
153
163
  @current_continue_dest = cond_bb # for continue stmt !
@@ -3,7 +3,7 @@ module Crokus
3
3
  class CFGCleaner
4
4
 
5
5
  def clean cfg
6
- puts " "*5+"|--[+] cleaning '#{cfg.name}'" unless $options[:mute]
6
+ puts " "*5+"|--[+] cleaning cfg '#{cfg.name}'" unless $options[:mute]
7
7
  @cfg=cfg
8
8
  @visited=[]
9
9
  @new_succs={}
@@ -0,0 +1,31 @@
1
+ module Crokus
2
+
3
+ class CFGOptimizer
4
+
5
+ def clean cfg
6
+ puts " "*5+"|--[+] optimizing cfg '#{cfg.name}'" unless $options[:mute]
7
+ @cfg=cfg
8
+ @visited=[]
9
+ @new_succs={}
10
+ optim_rec cfg.starter
11
+ cfg
12
+ end
13
+
14
+ private
15
+
16
+ def optim_rec bb
17
+ @visited << bb
18
+ @new_succs[bb]=[]
19
+ bb.succs.each_with_index do |succ,idx|
20
+ if bb.succs.size==1 and succ.succs.size==1
21
+ bb.stmts << succ.stmts
22
+ bb.stmts.flatten!
23
+ @cfg.bbs.delete(succ)
24
+ bb.succs[0]=succ.succs.first
25
+ end
26
+ optim_rec succ unless @visited.include?(succ)
27
+ end
28
+ end
29
+
30
+ end
31
+ end
@@ -47,6 +47,7 @@ module Crokus
47
47
  while !@visited.include?(bb)
48
48
  @visited << bb
49
49
  c_code=bb.code4dot
50
+ #puts c_code
50
51
  c_code=clean4dot(c_code)
51
52
  code << "#{id(bb)} [label=\"#{c_code}\",shape=rectangle, xlabel=#{bb.label}]"
52
53
  bb.succs.each_with_index do |succ,idx|
@@ -2,15 +2,17 @@ module Crokus
2
2
 
3
3
  class PrinterC
4
4
 
5
- attr_accessor :cfg
5
+ attr_accessor :cfg, :options
6
6
 
7
- def initialize
7
+ def initialize options={}
8
+ @options=options
8
9
  @visited=[]
9
10
  @prp=PrettyPrinter.new
10
11
  end
11
12
 
12
13
  def print cfg
13
- puts " |-->[+] generating C code from cfg '#{cfg.name}'"
14
+ filename= "#{cfg.name}.c"
15
+ puts " |-->[+] generating C code from cfg '#{cfg.name}' in '#{filename}'"
14
16
  @cfg=cfg
15
17
  code=Code.new
16
18
  code << "//"+"-"*60
@@ -34,8 +36,8 @@ module Crokus
34
36
  code << "}"
35
37
  code.newline
36
38
  code << main(cfg)
37
- puts code.finalize
38
- code.save_as "#{cfg.name}.c"
39
+ puts code.finalize if options[:verbose]
40
+ code.save_as filename
39
41
  end
40
42
 
41
43
  def main cfg
@@ -13,10 +13,15 @@ end
13
13
  module Crokus
14
14
 
15
15
  class RandomGen
16
+ attr_accessor :options
16
17
  attr_accessor :cfg
17
18
 
19
+ def initialize options={}
20
+ @options=options
21
+ end
22
+
18
23
  def run params
19
- puts "[+] running random Newage code generation"
24
+ puts "[+] running random C code generation"
20
25
  puts " |-->[+] reading parameters file '#{params}'"
21
26
  @params=YAML.load(File.read(params))
22
27
  init_cfg
@@ -30,7 +35,7 @@ module Crokus
30
35
  gen_dot # to see the structure, before hacking the content
31
36
  populate_all
32
37
  generate_c
33
- #print_infos
38
+ print_infos
34
39
  end
35
40
 
36
41
 
@@ -306,7 +311,7 @@ module Crokus
306
311
  end
307
312
 
308
313
  def generate_c
309
- PrinterC.new.print(cfg)
314
+ PrinterC.new(options).print(cfg)
310
315
  end
311
316
  end
312
317
  end
@@ -1,13 +1,18 @@
1
1
  module Crokus
2
2
  class Cleaner
3
3
  def clean str_c
4
-
5
- str_c.gsub!(";;",";")
6
4
  str_c.gsub!(";)",")")
7
5
  str_c.gsub!(/\n+\s*\;/,";")
8
6
  str_c.gsub!(/\;\s*\:/,":")
9
- #str_c.gsub!(/\n\s+\{/,"{")
7
+ str_c.gsub!(/\n\s+\{/,"{")
8
+ str_c.gsub!(/\;+/,";")
10
9
  str_c
11
10
  end
11
+
12
+ def debug str_c
13
+ puts "hit a key"
14
+ $stdin.gets.chomp
15
+ puts str_c
16
+ end
12
17
  end
13
18
  end
@@ -28,13 +28,15 @@ module Crokus
28
28
  parse(filename)
29
29
  return true if options[:parse_only]
30
30
 
31
+ draw_ast(@ast) if options[:ast]
32
+
31
33
  build_cfg
32
34
  return true if options[:cfg]
33
35
 
34
36
  pretty_print
35
37
 
36
38
  if options[:trojan]
37
- return_code=insert_trojan
39
+ return_code=insert_trojan()
38
40
  return return_code
39
41
  end
40
42
 
@@ -55,7 +57,7 @@ module Crokus
55
57
  end
56
58
 
57
59
  def draw_ast tree=nil,filename=nil
58
- dotname=filename || "#{base_name}.dot"
60
+ dotname=filename || "#{base_name}_ast.dot"
59
61
  puts " |--[+] drawing AST '#{dotname}'" unless options[:mute]
60
62
  ast_ = tree || @ast
61
63
  dot=AstPrinter.new.print(ast_)
@@ -105,7 +107,7 @@ module Crokus
105
107
 
106
108
  def insert_trojan
107
109
  puts "[+] inserting trojan" unless options[:mute]
108
- infected_ast=TrojanInserter.new.insert(ast)
110
+ infected_ast=TrojanInserter.new(@options).insert(ast)
109
111
  if infected_ast
110
112
  code=PrettyPrinter.new.visit(infected_ast)
111
113
  pp_c=@base_name+"_troj.c"
@@ -68,6 +68,7 @@ module Crokus
68
68
  begin
69
69
  @str=str
70
70
  @tokens=Lexer.new.tokenize(str)
71
+ #pp @tokens
71
72
  @tokens=@tokens.reject{|tok| tok==[nil,nil,nil]}
72
73
  @tokens=remove_comments()
73
74
  warnings=@tokens.select{|tok| tok.is? :lexer_warning}
@@ -848,7 +849,7 @@ module Crokus
848
849
  def is_casting?
849
850
  #puts "is_casting? : #{pp @tokens[0..1]}"
850
851
  cond1= @tokens[0].is?(:lparen)
851
- cond2= @tokens[1].is?([:int,:uint,:short,:byte,:float,:long])
852
+ cond2= @tokens[1].is?([:int,:uint,:short,:byte,:float,:long,:double])
852
853
  cond1 and cond2
853
854
  end
854
855
 
@@ -194,7 +194,7 @@ module Crokus
194
194
  if assign.rhs.is_a? Parenth
195
195
  rhs=assign.rhs.expr.accept(self)
196
196
  end
197
- ret="#{lhs} #{op} #{rhs}"
197
+ ret="#{lhs} #{op} #{rhs};"
198
198
  ret
199
199
  end
200
200
 
@@ -69,12 +69,14 @@ module Crokus
69
69
  options[:random] = params_filename
70
70
  end
71
71
 
72
- # parser.on('--trojan PARAMS', "generates random c files, using parameters", String) do |params_filename|
73
- # options[:trojan] = params_filename
74
- # end
75
-
76
- parser.on('--trojan', "generates random c files, using parameters") do
72
+ # optional argument for --trojan
73
+ parser.on('--trojan FUNC', "insert Syracuse Trojan in function FUNC") do |target_func|
74
+ if target_func.end_with?(".c")
75
+ puts "wrong argument for --trojan . It requires a function name as argument."
76
+ abort
77
+ end
77
78
  options[:trojan] = true
79
+ options[:trojan_target_func]=target_func
78
80
  end
79
81
 
80
82
  parser.on("--vv", "verbose") do
@@ -5,6 +5,10 @@ module Crokus
5
5
 
6
6
  class TrojanInserter < Transformer
7
7
 
8
+ def initialize options={}
9
+ @options=options
10
+ end
11
+
8
12
  def insert ast
9
13
  @nb_trojans=0
10
14
  new_ast=transform(ast)
@@ -13,6 +17,12 @@ module Crokus
13
17
  return new_ast
14
18
  else
15
19
  puts " "*1+"|--[?] insertion failed"
20
+ if func=@options[:trojan_target_func] and @target_reached.nil?
21
+ puts " "*5+"|-- no function named '#{func}' found"
22
+ end
23
+ if @failure_reason
24
+ puts " "*5+"|-- #{@failure_reason}"
25
+ end
16
26
  end
17
27
  nil
18
28
  end
@@ -25,12 +35,20 @@ module Crokus
25
35
  end
26
36
 
27
37
  def visitFunction func,args=nil
28
- puts " "*1+"|--[+] func #{func.name}"
29
38
  func_troj=super(func,args)
30
- success=insert_trojan(func_troj)
31
- if success
32
- @nb_trojans+=1
33
- func_troj.name=Ident.new(Token.create "#{func.name}_troj")
39
+ if @options[:trojan_target_func].nil? or (name=@options[:trojan_target_func] and target_reached=(func.name.to_s==name))
40
+ puts " "*1+"|--[+] func #{func.name}"
41
+ if target_reached
42
+ @target_reached=true
43
+ end
44
+ success=insert_trojan(func_troj)
45
+ if success
46
+ # hannah request : add a _troj to the function :
47
+ func_troj.name=Ident.create(func.name.to_s+'_troj')
48
+ @rename_funcs||={} # take cares of future calls to func !
49
+ @rename_funcs[func.name.to_s]=func_troj.name.to_s
50
+ @nb_trojans+=1
51
+ end
34
52
  end
35
53
  func_troj
36
54
  end
@@ -38,7 +56,7 @@ module Crokus
38
56
  def insert_trojan func
39
57
  if trojan=build_trojan(func)
40
58
  bodies=bodies_collect(func)
41
- puts "\t#bodies = #{bodies.size}"
59
+ #puts "\t#bodies = #{bodies.size}"
42
60
  target_body=bodies.sample
43
61
  stmts=target_body.stmts
44
62
  nb_decls=stmts.select{|stmt| stmt.is_a? Decl}.size
@@ -49,6 +67,18 @@ module Crokus
49
67
  success=false
50
68
  end
51
69
 
70
+ def visitFunCall fcall,args=nil
71
+ name=fcall.name.accept(self)
72
+ if @rename_funcs # propagate func renaming applied during visitFunction
73
+ if @rename_funcs.keys.include?(name.to_s)
74
+ new_name=@rename_funcs[name.to_s]
75
+ name=Ident.create(new_name)
76
+ end
77
+ end
78
+ args=fcall.args.collect{|arg| arg.accept(self)}
79
+ FunCall.new(name,args)
80
+ end
81
+
52
82
  def bodies_collect func
53
83
  bodies=[]
54
84
  bodies << func.body
@@ -87,7 +117,10 @@ module Crokus
87
117
  def build_trojan func
88
118
  trojan=Body.new
89
119
  anchor_var=choose_anchor(func)
90
- return unless anchor_var
120
+ if anchor_var.nil?
121
+ @failure_reason="no int type variable found in function local declarations, needed in the trojan insertion process."
122
+ return
123
+ end
91
124
  u_=Ident.new(Token.create("u_"))
92
125
  v_=Ident.new(Token.create("v_"))
93
126
  i_=Ident.new(Token.create("i_"))
@@ -125,8 +158,13 @@ module Crokus
125
158
  def build_trigger func
126
159
  args=find_int_arg(func)
127
160
  arg_names=get_arg_names(args)
128
- return unless arg_names.size>1
129
- cond=Binary.new(Parenth.new(Binary.new(arg_names[0],AND,arg_names[1])),EQUAL,T42)
161
+ unless arg_names.size>1
162
+ @failure_reason="not enough args of type int in func '#{func.name}' to build a trigger."
163
+ return
164
+ end
165
+ arg1,arg2=arg_names.shuffle[0..1]
166
+ puts " "*5+"|--> trigger variables are : #{arg1},#{arg2}"
167
+ cond=Binary.new(Parenth.new(Binary.new(arg1,AND,arg2)),EQUAL,T42)
130
168
  If.new(cond,nil)
131
169
  end
132
170
 
@@ -1,3 +1,3 @@
1
1
  module Crokus
2
- VERSION="0.1.1"
2
+ VERSION="0.1.7"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: crokus
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jean-Christophe Le Lann
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-02-11 00:00:00.000000000 Z
11
+ date: 2020-12-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: distribution
@@ -53,6 +53,7 @@ files:
53
53
  - lib/crokus/cfg.rb
54
54
  - lib/crokus/cfg_builder.rb
55
55
  - lib/crokus/cfg_cleaner.rb
56
+ - lib/crokus/cfg_optim.rb
56
57
  - lib/crokus/cfg_printer.rb
57
58
  - lib/crokus/cfg_printer_c.rb
58
59
  - lib/crokus/cfg_random_gen.rb