34983
|
1 |
#
|
|
2 |
# Author: Sascha Boehme, TU Muenchen
|
|
3 |
#
|
|
4 |
# Cache for prover results, based on discriminating problems using MD5.
|
|
5 |
|
|
6 |
use strict;
|
|
7 |
use warnings;
|
|
8 |
use Digest::MD5;
|
|
9 |
use LWP;
|
|
10 |
|
|
11 |
|
|
12 |
# arguments
|
|
13 |
|
|
14 |
my $certs_file = shift @ARGV;
|
|
15 |
my $location = shift @ARGV;
|
|
16 |
my $result_file = pop @ARGV;
|
|
17 |
my $problem_file = $ARGV[-1];
|
|
18 |
|
|
19 |
my $use_certs = not ($certs_file eq "-");
|
|
20 |
|
|
21 |
|
|
22 |
# create MD5 problem digest
|
|
23 |
|
|
24 |
my $problem_digest = "";
|
|
25 |
if ($use_certs) {
|
|
26 |
my $md5 = Digest::MD5->new;
|
|
27 |
open FILE, "<$problem_file" or
|
|
28 |
die "ERROR: Failed to open '$problem_file' ($!)";
|
|
29 |
$md5->addfile(*FILE);
|
|
30 |
close FILE;
|
|
31 |
$problem_digest = $md5->b64digest;
|
|
32 |
}
|
|
33 |
|
|
34 |
|
|
35 |
# lookup problem digest among existing certificates and
|
|
36 |
# return a cached result (if there is a certificate for the problem)
|
|
37 |
|
|
38 |
if ($use_certs and -e $certs_file) {
|
|
39 |
my $cached = 0;
|
|
40 |
open CERTS, "<$certs_file" or die "ERROR: Failed to open '$certs_file' ($!)";
|
|
41 |
while (<CERTS>) {
|
|
42 |
if (m/(\S+) (\d+)/) {
|
|
43 |
if ($1 eq $problem_digest) {
|
|
44 |
my $start = tell CERTS;
|
|
45 |
open FILE, ">$result_file" or
|
|
46 |
die "ERROR: Failed to open '$result_file ($!)";
|
|
47 |
while ((tell CERTS) - $start < $2) {
|
|
48 |
my $line = <CERTS>;
|
|
49 |
print FILE $line;
|
|
50 |
}
|
|
51 |
close FILE;
|
|
52 |
$cached = 1;
|
|
53 |
last;
|
|
54 |
}
|
|
55 |
else { seek CERTS, $2, 1; }
|
|
56 |
}
|
|
57 |
else { die "ERROR: Invalid file format in '$certs_file'"; }
|
|
58 |
}
|
|
59 |
close CERTS;
|
|
60 |
if ($cached) { exit 0; }
|
|
61 |
}
|
|
62 |
|
|
63 |
|
|
64 |
# invoke (remote or local) prover
|
|
65 |
|
|
66 |
if ($location eq "remote") {
|
|
67 |
# arguments
|
|
68 |
my $solver = $ARGV[0];
|
|
69 |
my @options = @ARGV[1 .. ($#ARGV - 1)];
|
|
70 |
|
|
71 |
# call solver
|
|
72 |
my $agent = LWP::UserAgent->new;
|
|
73 |
$agent->agent("SMT-Request");
|
|
74 |
$agent->timeout(180);
|
|
75 |
my $response = $agent->post($ENV{"REMOTE_SMT_URL"}, [
|
|
76 |
"Solver" => $solver,
|
|
77 |
"Options" => join(" ", @options),
|
|
78 |
"Problem" => [$problem_file] ],
|
|
79 |
"Content_Type" => "form-data");
|
|
80 |
if (not $response->is_success) { die "HTTP-Error: " . $response->message; }
|
|
81 |
else {
|
|
82 |
open FILE, ">$result_file" or
|
|
83 |
die "ERROR: Failed to create '$result_file' ($!)";
|
|
84 |
print FILE $response->content;
|
|
85 |
close FILE;
|
|
86 |
}
|
|
87 |
}
|
|
88 |
elsif ($location eq "local") {
|
|
89 |
system "@ARGV >$result_file 2>&1";
|
|
90 |
}
|
|
91 |
else { die "ERROR: No SMT solver invoked"; }
|
|
92 |
|
|
93 |
|
|
94 |
# cache result
|
|
95 |
|
|
96 |
if ($use_certs) {
|
|
97 |
open CERTS, ">>$certs_file" or
|
|
98 |
die "ERROR: Failed to open '$certs_file' ($!)";
|
|
99 |
print CERTS
|
|
100 |
("$problem_digest " . ((-s $result_file) + (length "\n")) . "\n");
|
|
101 |
|
|
102 |
open FILE, "<$result_file" or
|
|
103 |
die "ERROR: Failed to open '$result_file' ($!)";
|
|
104 |
foreach (<FILE>) { print CERTS $_; }
|
|
105 |
close FILE;
|
|
106 |
|
|
107 |
print CERTS "\n";
|
|
108 |
close CERTS;
|
|
109 |
}
|
|
110 |
|