| 1 |
|
| 2 |
|
| 3 |
require 'optparse' |
| 4 |
require "open3" |
| 5 |
require 'date' |
| 6 |
require 'timeout' |
| 7 |
|
| 8 |
def run_command_with_heartbeat (beat_time, cmd) |
| 9 |
start = Time.now |
| 10 |
line = nil |
| 11 |
puts "+ #{cmd}" |
| 12 |
Open3.popen3(cmd) do |stdin, stdout, stderr, thread| |
| 13 |
loop do |
| 14 |
begin |
| 15 |
Timeout::timeout(beat_time) {line = stdout.gets} |
| 16 |
break unless line |
| 17 |
puts line |
| 18 |
rescue Timeout::Error |
| 19 |
STDERR.puts "+++++++ running for #{(Time.now - start).round} seconds" |
| 20 |
end |
| 21 |
end |
| 22 |
end |
| 23 |
end |
| 24 |
|
| 25 |
def run_command cmd |
| 26 |
puts cmd |
| 27 |
puts `#{cmd}` |
| 28 |
end |
| 29 |
|
| 30 |
def get_parameters |
| 31 |
params = { |
| 32 |
shadowingArgs: nil, |
| 33 |
sources: [], |
| 34 |
entries: [], |
| 35 |
dry: false, |
| 36 |
compile: true, |
| 37 |
product: true, |
| 38 |
verify: true, |
| 39 |
time: nil, |
| 40 |
unroll: nil, |
| 41 |
printModel: nil, |
| 42 |
full_self_comp: nil, |
| 43 |
a: 'a.bpl', |
| 44 |
b: 'b.bpl', |
| 45 |
clang_options: ["-I#{__dir__}/../include"] |
| 46 |
} |
| 47 |
|
| 48 |
OptionParser.new do |opts| |
| 49 |
opts.banner = "Usage: #{File.basename $0} [options] FILE(s)" |
| 50 |
|
| 51 |
opts.on("-h", "--help", "Show this message") do |v| |
| 52 |
puts opts |
| 53 |
exit |
| 54 |
end |
| 55 |
|
| 56 |
opts.on('-n', "--dry-run", "Just pretend.") do |d| |
| 57 |
params[:dry] = d |
| 58 |
end |
| 59 |
|
| 60 |
opts.on('-e', '--shadowingArgs args', "The args to the shadowing pass.") do |p| |
| 61 |
params[:shadowingArgs] = p |
| 62 |
end |
| 63 |
|
| 64 |
opts.on('-e', '--entry-point PROC', "Entry-point procedures.") do |p| |
| 65 |
params[:entries] << p |
| 66 |
end |
| 67 |
|
| 68 |
opts.on('--clang-options STRING', "Flags to pass to clang.") do |s| |
| 69 |
params[:clang_options] << s unless s.empty? |
| 70 |
end |
| 71 |
|
| 72 |
opts.on('-t', '--time-limit SECONDS', "Time limit.") do |t| |
| 73 |
params[:time] = t |
| 74 |
end |
| 75 |
|
| 76 |
opts.on('--print-model flag', "Print the Model.") do |u| |
| 77 |
params[:printModel] = u |
| 78 |
end |
| 79 |
|
| 80 |
opts.on('--full-self-comp flag', "Perform full self composition.") do |u| |
| 81 |
params[:full_self_comp] = u |
| 82 |
end |
| 83 |
|
| 84 |
opts.on('-u', '--unroll-limit NUMBER', "Unroll limit.") do |u| |
| 85 |
params[:unroll] = u |
| 86 |
end |
| 87 |
|
| 88 |
opts.on('-l', '--loop-limit NUMBER', "Loop analysis limit.") do |l| |
| 89 |
params[:loop] = l |
| 90 |
end |
| 91 |
|
| 92 |
opts.on('-a FILE', "Intermediate file after Boogie translation.") do |f| |
| 93 |
params[:a] = f |
| 94 |
end |
| 95 |
|
| 96 |
opts.on('-b FILE', "Intermediate file after product construction.") do |f| |
| 97 |
params[:b] = f |
| 98 |
end |
| 99 |
|
| 100 |
opts.separator "" |
| 101 |
opts.separator "phase selection" |
| 102 |
|
| 103 |
opts.on('--[no-]compile', "Compile the input program?") do |p| |
| 104 |
params[:compile] = p |
| 105 |
end |
| 106 |
|
| 107 |
opts.on('--[no-]product', "Do the product construction?") do |p| |
| 108 |
params[:product] = p |
| 109 |
end |
| 110 |
|
| 111 |
opts.on('--[no-]verify', "Do the verification?") do |p| |
| 112 |
params[:verify] = p |
| 113 |
end |
| 114 |
|
| 115 |
opts.on('--timing-analysis', "Perform timing analysis") do |p| |
| 116 |
params[:timing] = p |
| 117 |
end |
| 118 |
|
| 119 |
|
| 120 |
end.parse! |
| 121 |
params[:sources] = ARGV |
| 122 |
|
| 123 |
raise "Input FILES required; see --help." if params[:sources].empty? |
| 124 |
params[:sources].each do |f| |
| 125 |
raise "File #{f} not found." unless File.exists? f |
| 126 |
end |
| 127 |
if params[:compile] |
| 128 |
raise "Entry-points PROCS required see --help." if params[:entries].empty? |
| 129 |
else |
| 130 |
raise "Too many input FILES given." if params[:sources].count > 1 |
| 131 |
end |
| 132 |
|
| 133 |
params[:a] = params[:sources].first unless params[:compile] |
| 134 |
params[:b] = params[:a] unless params[:product] |
| 135 |
|
| 136 |
return params |
| 137 |
end |
| 138 |
|
| 139 |
begin |
| 140 |
params = get_parameters |
| 141 |
echo = params[:dry] ? "echo" : "" |
| 142 |
|
| 143 |
inputs = params[:sources] |
| 144 |
temp_files = [] |
| 145 |
|
| 146 |
SPECIAL_FUNCTIONS = [ |
| 147 |
"__SMACK", |
| 148 |
"__VERIFIER", |
| 149 |
"__builtin", |
| 150 |
"llvm\.", |
| 151 |
"public_in", |
| 152 |
"public_out", |
| 153 |
"declassified_out", |
| 154 |
"public_invariant", |
| 155 |
"benign", |
| 156 |
"__disjoint_regions" |
| 157 |
] |
| 158 |
|
| 159 |
INLINE_ASM_PATTERN = /\basm\b/ |
| 160 |
UNDEFINED_PATTERN = /\bdeclare\b .* @(?!(#{SPECIAL_FUNCTIONS * "|"}))([^(]*)/ |
| 161 |
|
| 162 |
if params[:compile] |
| 163 |
flags = ["-t"] |
| 164 |
flags << "--clang-options=\"#{params[:clang_options] * " "}\"" if params[:clang_options].any? |
| 165 |
flags << "--loop-limit #{params[:loop]}" if params[:loop] |
| 166 |
flags << "--verifier boogie" |
| 167 |
flags << "--entry-points #{params[:entries] * ","}" |
| 168 |
flags << "-ll #{params[:a]}.ll" |
| 169 |
flags << "-bpl #{params[:a]}" |
| 170 |
flags << "--timing-annotations" if params[:timing] |
| 171 |
puts `which clang` |
| 172 |
puts `which llvm-link` |
| 173 |
puts `clang --version` |
| 174 |
puts `llvm-link --version` |
| 175 |
puts "#{flags * " "} #{params[:sources] * " "}" |
| 176 |
puts `#{echo} smack #{flags * " "} #{params[:sources] * " "}` |
| 177 |
raise "failed to compile #{params[:sources] * ", "}" unless $?.success? |
| 178 |
warn "warning: module contains inline assembly" \ |
| 179 |
if File.readlines("#{params[:a]}.ll").grep(INLINE_ASM_PATTERN).any? |
| 180 |
|
| 181 |
ufs = File.readlines("#{params[:a]}.ll") |
| 182 |
.map{|s| s.match UNDEFINED_PATTERN} |
| 183 |
.compact |
| 184 |
.map{|m| m[2]} |
| 185 |
|
| 186 |
warn "warning: module contains undefined functions: #{ufs * ", "}" if ufs.any? |
| 187 |
end |
| 188 |
|
| 189 |
if params[:product] |
| 190 |
full = if params[:full_self_comp] then "full" else "selective" end |
| 191 |
shadowingArgs = "self_comp_mode=#{full}" |
| 192 |
shadowingArgs = "#{shadowingArgs},#{params[:shadowingArgs]}" if params[:shadowingArgs] |
| 193 |
shadowingArgs.gsub(/\s+/, "") |
| 194 |
flags = [] |
| 195 |
flags << "--cost-modeling" if params[:timing] |
| 196 |
flags << "--shadowing #{shadowingArgs}" |
| 197 |
flags << "--pruning" |
| 198 |
puts `#{echo} bam -q -i #{params[:a]} #{flags * " "} -o #{params[:b]}` |
| 199 |
raise "failed to construct product program" unless $?.success? |
| 200 |
end |
| 201 |
|
| 202 |
if params[:verify] |
| 203 |
flags = [] |
| 204 |
flags << "/printModel 4" if params[:printModel] |
| 205 |
flags << "/doModSetAnalysis" |
| 206 |
flags << "/loopUnroll:#{params[:unroll]}" if params[:unroll] |
| 207 |
flags << "/timeLimit:#{params[:time]}" if params[:time] |
| 208 |
warn "warning: only unrolling up to #{params[:unroll]}" if params[:unroll] |
| 209 |
run_command_with_heartbeat(30, "#{echo} boogie #{flags * " "} #{params[:b]}") |
| 210 |
|
| 211 |
end |
| 212 |
|
| 213 |
rescue Interrupt |
| 214 |
puts "Got interrupt." |
| 215 |
|
| 216 |
rescue => e |
| 217 |
puts "#{e}" |
| 218 |
exit(-1) |
| 219 |
|
| 220 |
ensure |
| 221 |
|
| 222 |
end |