-
Notifications
You must be signed in to change notification settings - Fork 4
/
Logger.java
1048 lines (899 loc) · 40.5 KB
/
Logger.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package dk.au.cs.casa.jer;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.spotify.docker.client.DefaultDockerClient;
import com.spotify.docker.client.DockerClient;
import com.spotify.docker.client.messages.ContainerConfig;
import com.spotify.docker.client.messages.ContainerCreation;
import com.spotify.docker.client.messages.HostConfig;
import java.awt.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.StandardOpenOption;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static java.lang.String.format;
/**
* Produces a log file
*/
public class Logger {
private static final boolean DEBUG = false;
private static final org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(Logger.class);
private static String tempDirectoryPrefix = Logger.class.getCanonicalName();
private final Path root;
private final Path rootRelativeMain;
private final Path instrumentationDir;
private final Path temp;
private final Path analysis;
private final Path node;
private final Path jjs;
private final Path jalangilogger;
private final List<Path> preambles;
private final Optional<Set<Path>> onlyInclude;
private final int instrumentationTimeLimit;
private final int timeLimit;
private final Environment environment;
private final Path jalangi2directory;
private final Path graalVmNode;
private Metadata metadata;
/**
* Produces a log file for the run of a main file in a directory
*/
public Logger(Path root, Path rootRelativeMain, List<Path> preambles, Optional<Set<Path>> onlyInclude, int instrumentationTimeLimit, int timeLimit, Environment environment, Path node, Path jalangilogger, Path jjs, Path graalVmNode, Metadata metadata) {
if (rootRelativeMain.isAbsolute()) {
throw new IllegalArgumentException("rootRelativeMain must be relative");
}
if ((environment == Environment.BROWSER || environment == Environment.NEW_BROWSER) && isJsFile(rootRelativeMain)) {
this.rootRelativeMain = createHTMLWrapper(root, rootRelativeMain, preambles);
} else {
this.rootRelativeMain = rootRelativeMain;
}
checkAbsolutePreambles(preambles);
this.metadata = metadata;
this.jjs = jjs;
this.environment = environment;
this.instrumentationTimeLimit = instrumentationTimeLimit;
this.timeLimit = timeLimit;
this.preambles = preambles;
this.onlyInclude = onlyInclude;
this.root = root;
this.node = node;
this.graalVmNode = graalVmNode;
this.jalangilogger = jalangilogger;
this.jalangi2directory = guessJalangiDirectory(jalangilogger);
try {
this.temp = createTempDirectory().toRealPath();
} catch (IOException e) {
throw new RuntimeException(e);
}
this.instrumentationDir = temp.resolve("instrumentation");
this.analysis = jalangilogger.resolve("src/analysis/ValueLogger.js").toAbsolutePath();
}
/**
* Produces a log file for the run of a single main file
*/
public static Logger makeLoggerForIndependentMainFile(Path main, List<Path> preambles, Optional<Set<Path>> onlyInclude, int instrumentationTimeLimit, int timeLimit, Environment environment, Path node, Path jalangilogger, Path jjs, Path graalVmNode) {
Path root = isolateInNewRoot(main);
Path rootRelativeMain = root.relativize(root.resolve(main.getFileName()));
return makeLoggerForDirectoryWithMainFile(root, rootRelativeMain, preambles, onlyInclude, instrumentationTimeLimit, timeLimit, environment, node, jalangilogger, jjs, graalVmNode);
}
public static Logger makeLoggerForDirectoryWithMainFile(Path root, Path rootRelativeMain, List<Path> preambles, Optional<Set<Path>> onlyInclude, int instrumentationTimeLimit, int timeLimit, Environment environment, Path node, Path jalangilogger, Path jjs, Path graalVmNode) {
return new Logger(root, rootRelativeMain, preambles, onlyInclude, instrumentationTimeLimit, timeLimit, environment, node, jalangilogger, jjs, graalVmNode, initMeta(root, rootRelativeMain.getFileName(), onlyInclude, environment, getEnvironmentVersion(environment, node), timeLimit));
}
private static String getEnvironmentVersion(Environment environment, Path node) {
if (environment == Environment.NODE || environment == Environment.NODE_GLOBAL) {
String nodeVersion = getFirstStdOutLine(node, "--version");
return nodeVersion;
}
return "?"; // TODO support more version schemes
}
private static String getFirstStdOutLine(Path bin, String arg) {
boolean debug = false;
String[] cmd = new String[]{bin.toAbsolutePath().toString(), arg};
try {
if (debug) {
System.out.printf("Executing: %s\n", String.join(" ", cmd));
}
final ProcessBuilder pb = new ProcessBuilder(cmd);
Process process = pb.start();
BufferedReader brStd = new BufferedReader(new InputStreamReader(process.getInputStream()));
BufferedReader brErr = new BufferedReader(new InputStreamReader(process.getErrorStream()));
String lineStd;
String lineErr = null;
List<String> lineStds = new ArrayList<>();
List<String> lineErrs = new ArrayList<>();
while ((lineStd = brStd.readLine()) != null || (lineErr = brErr.readLine()) != null) {
PrintStream stream;
String line;
if (lineStd != null) {
stream = System.out;
line = lineStd;
lineStds.add(lineStd);
} else {
stream = System.err;
line = lineErr;
lineErrs.add(lineErr);
}
if (debug) {
stream.printf("Process ::: %s%n", line);
}
}
process.waitFor();
if (process.exitValue() != 0) {
throw new RuntimeException("Process exited with exit code: " + process.exitValue());
}
if (lineStds.size() != 1) {
throw new RuntimeException("Unexpected output from process: " + lineStds);
}
return lineStds.get(0);
} catch (IOException | InterruptedException e) {
throw new RuntimeException(e);
}
}
private static Path createTempDirectory() throws IOException {
return Files.createTempDirectory(tempDirectoryPrefix);
}
/**
* Isolates a single file in a new empty directory. Relative paths behave as before.
*
* @return the new root directory
*/
private static Path isolateInNewRoot(Path main) {
try {
Path newRoot = createTempDirectory();
deleteTempRecursivelyOnExit(newRoot);
Path isolated = newRoot.resolve(main.getFileName());
Files.copy(main, isolated);
return newRoot;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private static Path createHTMLWrapper(Path root, Path rootRelativeMain, List<Path> preambles) {
List<Path> scriptSources = new ArrayList<>();
scriptSources.addAll(preambles);
scriptSources.add(rootRelativeMain);
List<String> HTMLWrap = new ArrayList<>();
HTMLWrap.addAll(Arrays.asList(
"<!DOCTYPE html>",
"<html>",
"<head>",
"</head>",
"<body>"
));
HTMLWrap.addAll(scriptSources.stream()
.map(source -> String.format("<script src=\"%s\"></script>", source))
.collect(Collectors.toList())
);
HTMLWrap.addAll(Arrays.asList(
"</body>",
"</html>"));
Path htmlWrapper = root.resolve("jalangilogger_wrapper.html");
Path htmlWrapperRelative = root.relativize(htmlWrapper);
try {
Files.write(htmlWrapper, HTMLWrap, StandardCharsets.UTF_8, StandardOpenOption.CREATE_NEW);
} catch (IOException e) {
throw new RuntimeException(e);
}
return htmlWrapperRelative;
}
private static boolean isJsFile(Path rootRelativeMain) {
return rootRelativeMain.getFileName().toString().endsWith(".js");
}
private static List<String> filterLogFile(Path inputLog) throws IOException {
List<String> lines = Files.readAllLines(inputLog).stream()
// TODO find the source of these special-cases!
.map(String::trim)
.filter(l -> !l.isEmpty())
.filter(l -> !l.contains("ValueLogger.js"))
.map(l -> l.startsWith(",") ? l.substring(1) : l)
.distinct()
.collect(Collectors.toList());
return lines;
}
private static Metadata initMeta(Path root, Path main, Optional<Set<Path>> onlyInclude, Environment environment, String environmentVersion, int timeLimit) {
Metadata metadata = new Metadata();
metadata.setTime(System.currentTimeMillis());
metadata.setTimeLimit(timeLimit);
metadata.setRoot(main.toString());
String hash;
if (onlyInclude.isPresent()) {
hash = HashUtil.shaDirOrFile(onlyInclude.get());
} else {
hash = HashUtil.shaDirOrFile(root);
}
metadata.setSha(hash);
metadata.setEnvironment(environment.toString());
metadata.setEnvironmentVersion(environmentVersion);
// Mini-changelog:
// 0.1 base
// 0.2 OtherSymbolDescription, instrumentation-timeout, timeLimit value
metadata.setLogVersion("0.2");
return metadata;
}
private static void deleteTempRecursivelyOnExit(Path dir) {
if (!dir.toString().contains(tempDirectoryPrefix)) {
throw new IllegalArgumentException("Trying to delete something non-temporary: '" + dir + "'?");
}
try {
Files.walkFileTree(dir, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
throws IOException {
delete(file);
return FileVisitResult.CONTINUE;
}
private void delete(Path file) throws IOException {
//file.toFile().deleteOnExit();
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
if (exc != null) {
throw exc;
}
delete(dir);
return FileVisitResult.CONTINUE;
}
});
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private Path guessJalangiDirectory(Path jalangilogger) {
Path jalangiloggerAsTopLevel = jalangilogger.resolve("node_modules/jalangi2");
Path jalangiloggerAsModule = jalangilogger.resolve("../jalangi2");
if (Files.exists(jalangiloggerAsTopLevel)) {
return jalangiloggerAsTopLevel;
} else if (Files.exists(jalangiloggerAsModule)) {
return jalangiloggerAsModule;
}
String message = String.format("Could not find Jalang2 installation, is it installed? (looked at: %s and %s)", jalangiloggerAsTopLevel.toAbsolutePath().toString(), jalangiloggerAsModule.toAbsolutePath().toString());
throw new RuntimeException(message);
}
private void checkAbsolutePreambles(List<Path> preambles) {
for (Path preamble : preambles) {
if (!preamble.isAbsolute()) {
throw new IllegalArgumentException(format("Preambles must be absolute %s", preamble));
}
}
}
private String makeMeta(String result) {
this.metadata.setResult(result);
Gson gson = new Gson();
return gson.toJson(this.metadata.jsonRep);
}
private Process exec(String description, Path pwd, String... cmd) throws IOException {
return exec(description, pwd, false, cmd);
}
private Process exec(String description, Path pwd, boolean redirectOutput, String... cmd) throws IOException {
ProcessBuilder pb = new ProcessBuilder(cmd);
if (redirectOutput) {
pb.redirectOutput(ProcessBuilder.Redirect.INHERIT);
pb.redirectError(ProcessBuilder.Redirect.INHERIT);
}
if (pwd != null) {
pb.directory(pwd.toFile());
}
//pb.redirectOutput(ProcessBuilder.Redirect.INHERIT);
pb.redirectError(ProcessBuilder.Redirect.INHERIT);
System.out.printf("Starting %s (at %s): %s%n", description, pwd, String.join(" ", Arrays.asList(cmd)));
Process p = pb.start();
return p;
}
public RawLogFile log() throws IOException {
return log(true);
}
public RawLogFile log(boolean cleanupWhenDone) throws IOException {
RawLogFile rawLogFile = _log();
if (cleanupWhenDone) {
deleteTempRecursivelyOnExit(temp);
}
Set<String> badLines = rawLogFile.getLines().stream().filter(l -> l.contains(tempDirectoryPrefix)).collect(Collectors.toSet());
if (!badLines.isEmpty()) {
System.err.println("Warning: Produced log that contains semi-bad entries (a likely reference to the tempDirectory):" + badLines);
}
return rawLogFile;
}
private RawLogFile _log() throws IOException {
try {
if (environment == Environment.NASHORN || environment == Environment.NODE || environment == Environment.NODE_GLOBAL || environment == Environment.NODE_PROF || environment == Environment.NODE_PROF_GLOBAL) {
return new JSLogger(environment).log();
} else if (environment == Environment.BROWSER) {
return new HTMLLogger().log();
} else if (environment == Environment.NEW_BROWSER) {
return new NewHtmlLogger(false).log();
} else if (environment == Environment.NEW_BROWSER_HEADLESS) {
return new NewHtmlLogger(true).log();
} else if (environment == Environment.DOCKER_BROWSER) {
return new DrivenHTMLLogger().log();
}
} catch (InstrumentationTimeoutException e) {
return makeInstrumentationTimeoutLog();
} catch (InstrumentationSyntaxErrorException e) {
return makeSyntaxErrorLog();
} catch (InstrumentationException e) {
throw new RuntimeException(e);
}
throw new IllegalArgumentException("Unsupported environment: " + environment);
}
private void instrument(Environment environment) throws IOException, InstrumentationSyntaxErrorException, InstrumentationTimeoutException {
Path instrument_js = jalangi2directory.resolve("src/js/commands/instrument.js").toAbsolutePath();
String script = instrument_js.toString();
Files.createDirectories(instrumentationDir);
String out = instrumentationDir.toAbsolutePath().toString();
String in = ".";
ArrayList<String> cmd = new ArrayList<>(Arrays.asList(node.toString(), script, "--inlineIID", "--analysis", analysis.toString(), "--outputDir", out));
switch (environment) {
case DOCKER_BROWSER:
case BROWSER:
cmd.add("--instrumentInline");
cmd.add("--inlineJalangi");
break;
case NEW_BROWSER:
case NEW_BROWSER_HEADLESS:
cmd.add("--instrumentInline");
cmd.add("--inlineJalangi");
break;
case NASHORN:
cmd.add("--inlineJalangiAndAnlysesInSingleJSFile");
break;
case NODE:
case NODE_GLOBAL:
break;
}
if (onlyInclude.isPresent()) {
cmd.add("--only_include");
List<String> stringPaths = onlyInclude.get().stream()
.map(p -> root.relativize(p.toAbsolutePath()))
.map(p -> p.toString())
.collect(Collectors.toList());
cmd.add(String.join(System.getProperty("path.separator"), stringPaths));
}
cmd.add(in);
Process exec = exec("instrumentation process", root, cmd.toArray(new String[]{}));
try {
boolean timedOut = !exec.waitFor(instrumentationTimeLimit, TimeUnit.SECONDS);
if (timedOut) {
throw new InstrumentationTimeoutException();
}
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
String err;
try (BufferedReader reader = new BufferedReader(new InputStreamReader(exec.getInputStream()))) {
err = reader.lines().collect(Collectors.joining("\n"));
}
if (exec.exitValue() != 0) {
throw new RuntimeException("Instrumentation failed");
}
if (err.contains("SyntaxError")) {
throw new InstrumentationSyntaxErrorException();
}
}
private List<String> postProcessLog(Path logFile) {
try {
List<String> filtered = filterLogFile(logFile);
List<String> transformed = new LogFileTransformer(root, instrumentationDir, rootRelativeMain, environment).transform(filtered);
return transformed;
} catch (IOException e) {
throw new RuntimeException("Could not post-process the log file", e);
}
}
private void addPreambles(List<String> cmd) {
for (Path preamble : preambles) {
cmd.add("--preamble");
cmd.add(preamble.toString());
}
}
private RawLogFile makeInstrumentationTimeoutLog() throws IOException {
final List<String> lines = new ArrayList<>();
lines.add(makeMeta("instrumentation-timeout"));
lines.addAll(new ArrayList<>());
return new RawLogFile(lines);
}
private RawLogFile makeSyntaxErrorLog() throws IOException {
final List<String> lines = new ArrayList<>();
lines.add(makeMeta("syntax-error"));
lines.addAll(new ArrayList<>());
return new RawLogFile(lines);
}
private RawLogFile makeRawLogFile(String status, Path unprocessedlogFileLocation) {
final List<String> lines = new ArrayList<>();
lines.add(makeMeta(status));
lines.addAll(postProcessLog(unprocessedlogFileLocation));
return new RawLogFile(lines);
}
public enum Environment {
NODE,
NODE_GLOBAL,
NASHORN,
BROWSER,
DRIVEN_BROWSER, //FIXME: Remove
NEW_BROWSER,
NEW_BROWSER_HEADLESS,
DOCKER_BROWSER,
NODE_PROF,
NODE_PROF_GLOBAL
}
private class HTMLLogger {
private final Path serverDir;
private final int hardTimeLimit;
private final Path logfile;
public HTMLLogger() {
this.serverDir = temp.resolve("server");
this.logfile = serverDir.resolve("logfile.log");
this.hardTimeLimit = (int) (timeLimit * 1.5);
}
private void startServer() {
try {
Files.createDirectories(serverDir);
} catch (IOException e) {
throw new RuntimeException(e);
}
final JettyLoggerServer.RunningServer[] runningServer = new JettyLoggerServer.RunningServer[1]; // XXX thread-safety...
final JettyLoggerServer[] loggerServer = {new JettyLoggerServer(instrumentationDir)};
Thread serverThread = new Thread(() -> {
runningServer[0] = startServerAndOpenBrowser(loggerServer[0]);
});
serverThread.start();
try {
Thread.sleep(1000); // wait for the server and browser to start properly
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
while (runningServer[0] == null) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
waitForServerToStop(runningServer[0]);
runningServer[0].persistEntries(logfile);
try {
log.debug("Waiting for server-thread to terminate...");
serverThread.join();
log.debug("Server-thread has terminated.");
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
private void waitForServerToStop(JettyLoggerServer.RunningServer runningServer) {
try {
Console c = new Console(); // TODO use similar command line interface for the JSLogger (although it will be non-interactive, and use the same stdout as this console)
c.format("%n%s%n", String.join(String.format("%n"),
Arrays.asList(
"Log recording of browser application started.",
"Interact with the browser, and stop the recording by either:",
"- pressing <p> in the browser",
"- pressing <ENTER> in this terminal",
String.format("- waiting for up to %d seconds", timeLimit)
)
));
Thread keypressThread = new Thread(() -> {
c.readLine("");
runningServer.stop();
});
keypressThread.start();
long endTime = System.currentTimeMillis() + (hardTimeLimit * 1000);
while (!runningServer.isStopped()) {
if (System.currentTimeMillis() > endTime) {
runningServer.stop();
}
Thread.sleep(500);
}
keypressThread.interrupt();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
/**
* Starts the server and open the browser.
* In headless mode this call is blocking until the page has loaded or a timeout reached
*/
private JettyLoggerServer.RunningServer startServerAndOpenBrowser(JettyLoggerServer server) {
JettyLoggerServer.RunningServer runningServer = server.startServer();
try {
String serverLocation = String.format("localhost:%d", runningServer.getURI().getPort());
String queryParameters = String.format("softTimeLimit=%d&hardTimeLimit=%d", timeLimit, hardTimeLimit);
URI uri = new URI("http", serverLocation, "/" + rootRelativeMain.toString(), queryParameters, null);
Desktop.getDesktop().browse(uri);
} catch (URISyntaxException | IOException e) {
throw new RuntimeException(e);
}
return runningServer;
}
public RawLogFile log() throws InstrumentationSyntaxErrorException, InstrumentationTimeoutException, IOException {
instrument(Environment.BROWSER);
long before = System.currentTimeMillis();
startServer(); // will wait for server to terminate
long after = System.currentTimeMillis();
long duration = after - before;
boolean timedOut = duration > 1000 * hardTimeLimit;
String status = timedOut ? "timeout" : "success";
if (!Files.exists(logfile)) {
if (!timedOut) {
System.err.println("Log file does not exist, but we did not encounter a timeout!?!");
}
try {
Files.createFile(logfile);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return makeRawLogFile(status, logfile);
}
private class Console {
BufferedReader br;
PrintStream ps;
public Console() {
br = new BufferedReader(new InputStreamReader(System.in));
ps = System.out;
}
public String readLine(String out) {
ps.format(out);
try {
return br.readLine();
} catch (IOException e) {
return null;
}
}
public PrintStream format(String format, Object... objects) {
return ps.format(format, objects);
}
}
}
private class NewHtmlLogger {
private final Path serverDir;
private final int hardTimeLimit;
private final Path logfile;
private final boolean headless;
private Thread serverThread;
private final JettyWebServer.RunningServer[] runningServer = new JettyWebServer.RunningServer[1]; // XXX thread-safety...
public NewHtmlLogger(boolean headless) {
this.serverDir = temp.resolve("server");
this.logfile = serverDir.resolve("logfile.log");
this.hardTimeLimit = (int) (timeLimit * 1.5);
this.headless = headless;
}
private String browse() {
try {
String serverLocation = String.format("localhost:%d", runningServer[0].getURI().getPort());
String queryParameters = String.format("softTimeLimit=%d&hardTimeLimit=%d", timeLimit, hardTimeLimit);
URI uri = new URI("http", serverLocation, "/" + rootRelativeMain.toString(), queryParameters, null);
return (new BrowserDriver()).browse(uri, hardTimeLimit, headless);
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
}
private void startServer() {
try {
Files.createDirectories(serverDir);
} catch (IOException e) {
throw new RuntimeException(e);
}
serverThread = new Thread(() -> {
JettyWebServer jws = new JettyWebServer(instrumentationDir);
runningServer[0] = jws.startServer();
});
serverThread.start();
while(runningServer[0] == null && serverThread.isAlive()) {
try {
Thread.sleep(500); // wait for the server to start
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
if(!serverThread.isAlive() && runningServer[0] == null)
throw new RuntimeException("Failed to start the web server");
try {
Thread.sleep(1000); // give it an additional second
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
private void stopServer() {
try {
if(runningServer[0] != null) {
runningServer[0].stop();
}
} catch (Exception e) { }
try {
serverThread.join(); // additionally wait the thread to die
} catch (Exception e) { }
}
public RawLogFile log() throws InstrumentationSyntaxErrorException, InstrumentationTimeoutException, IOException {
instrument(environment);
try {
startServer(); // will wait for server to terminate
String entries = browse();
Gson gson = new Gson();
JsonArray elements = gson.fromJson(entries, JsonArray.class);
LinkedHashSet<String> uniqueElements = new LinkedHashSet<>();
elements.forEach(el -> {
uniqueElements.add(gson.toJson(el));
});
try {
Files.write(logfile, uniqueElements, StandardOpenOption.CREATE_NEW);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
catch(Exception e) {
throw e;
}
finally {
stopServer();
}
return makeRawLogFile("success", logfile);
}
private class Console {
BufferedReader br;
PrintStream ps;
public Console() {
br = new BufferedReader(new InputStreamReader(System.in));
ps = System.out;
}
public String readLine(String out) {
ps.format(out);
try {
return br.readLine();
} catch (IOException e) {
return null;
}
}
public PrintStream format(String format, Object... objects) {
return ps.format(format, objects);
}
}
}
private class DrivenHTMLLogger {
final HostConfig hf = HostConfig.builder()
.binds(
instrumentationDir.toAbsolutePath().toString() + ":/wkd",
jalangilogger.toAbsolutePath().toString() + ":/jalangilogger"
).build();
public DrivenHTMLLogger() {
}
private void checkAllCanBeShared(DockerClient docker) throws Exception {
final ContainerConfig containerConfig = ContainerConfig.builder()
.image("busybox:latest")
.hostConfig(hf)
.cmd("sh", "-c", "ls /wkd && ls /jalangilogger")
.build();
final ContainerCreation creation = docker.createContainer(containerConfig);
String id = creation.id();
docker.startContainer(id);
if (docker.waitContainer(id).statusCode() != 0) {
String msg = String.format("Check the docker configuration, both %s and %s need to be among the 'shared folders'", instrumentationDir.toAbsolutePath().toString(), jalangilogger.toAbsolutePath().toString());
throw new RuntimeException(msg);
}
}
String mkCmd(String jalangidir, String wd) {
return String.format("cd '%s/browser_driver' && scripts/container/cycle '%s' '%s'", jalangidir, wd, rootRelativeMain.toString());
}
private void runInDocker() throws Exception {
// Create a client based on DOCKER_HOST and DOCKER_CERT_PATH env vars
final DockerClient docker = DefaultDockerClient.fromEnv().build();
checkAllCanBeShared(docker);
String imageName = "algobardo/tajsound:latest";
// Pull an image
//docker.pull(imageName);
String cmd = mkCmd("/jalangilogger", "/wkd");
final ContainerConfig containerConfig = ContainerConfig.builder()
.image(imageName)
.hostConfig(hf)
.cmd("bash", "-c", cmd)
.attachStderr(true)
.attachStdout(true)
.build();
final ContainerCreation creation = docker.createContainer(containerConfig);
final String id = creation.id();
// Start container
docker.startContainer(id);
docker.waitContainer(id);
System.out.println("Docker log:");
System.out.println(docker.logs(id,
DockerClient.LogsParam.stdout(),
DockerClient.LogsParam.stderr())
.readFully());
// Remove container
docker.removeContainer(id);
// Close the docker client
docker.close();
}
private boolean isDockerEnvironment() {
return new File("/.dockerenv").exists();
}
private void captureLog() {
try {
Process p = exec("dynamic analysis", jalangilogger.resolve("browser_driver"), true, "bash", "-c", mkCmd(jalangilogger.toAbsolutePath().toString(), instrumentationDir.toAbsolutePath().toString()));
Thread.sleep(1000);
if (!p.isAlive()) {
throw new RuntimeException();
}
int res = p.waitFor();
if (res != 0) throw new RuntimeException("Failed generating log\n" + p.getErrorStream());
} catch (IOException e) {
throw new RuntimeException(e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
public RawLogFile log() throws IOException, InstrumentationSyntaxErrorException, InstrumentationTimeoutException {
instrument(Environment.DOCKER_BROWSER);
try {
if (isDockerEnvironment()) {
captureLog();
} else {
runInDocker();
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return makeRawLogFile("success", instrumentationDir.resolve("NEW_LOG_FILE.log")); // XXX we do not know that?!
}
}
private class JSLogger {
private final Environment environment;
private JSLogger(Environment environment) {
this.environment = environment;
}
public RawLogFile log() throws IOException, InstrumentationException {
if (environment != Environment.NODE_PROF && environment != Environment.NODE_PROF_GLOBAL) {
instrument(environment);
} else { // do not instrument, when using NodeProf, but just copy the files.
copyDirectory(root, instrumentationDir);
}
String exitStatus = run();
return makeRawLogFile(exitStatus, instrumentationDir.resolve("NEW_LOG_FILE.log"));
}
private String run() throws IOException {
List<String> cmd;
switch (environment) {
case NODE:
case NODE_GLOBAL:
Path direct_js = jalangi2directory.resolve("src/js/commands/direct.js").toAbsolutePath();
String script = direct_js.toString();
Path commandLineMain = environment == Environment.NODE ? rootRelativeMain : makeGlobalifier();
cmd = new ArrayList<>(Arrays.asList(new String[]{node.toString(), script, "--analysis", analysis.toString(), commandLineMain.toString()}));
break;
case NASHORN:
cmd = new ArrayList<>(Arrays.asList(new String[]{jjs.toString(), rootRelativeMain.toString(), "--"}));
break;
case NODE_PROF:
case NODE_PROF_GLOBAL:
Path nodeprofJar = jalangilogger.resolve("../nodeprof/nodeprof.jar").normalize();
Path nodeprofJalangi = jalangilogger.resolve("../nodeprof/jalangi.js").normalize();
cmd = new ArrayList<>(Arrays.asList(new String[]{
graalVmNode.toString(),
"--jvm",
"--vm.Dtruffle.class.path.append=" + nodeprofJar,
"--experimental-options",
"--nodeprof",
"--nodeprof.Scope=module",
"--nodeprof.ExcludeSource=" + analysis + ",globalifier/main.js",
nodeprofJalangi.toString(),
"--analysis", analysis.toString(),
(environment == Environment.NODE_PROF ? rootRelativeMain : makeGlobalifier()).toString()}));
break;
default:
throw new UnsupportedOperationException("Unhandled environment kind: " + environment);
}
addPreambles(cmd);
Process p = exec("dynamic analysis", instrumentationDir, cmd.toArray(new String[cmd.size()]));
boolean timeout;
try {
timeout = !p.waitFor(timeLimit, TimeUnit.SECONDS);
} catch (InterruptedException e) {
throw new RuntimeException(e);
} finally {
try {
// XXX an attempt to kill zombie nodejs-processes for good.
p.destroy();
p.waitFor(1, TimeUnit.SECONDS);
p.destroyForcibly();
p.waitFor(1, TimeUnit.SECONDS);
} catch (InterruptedException e) {
// ignore
} finally {
if (p.isAlive()) {
throw new IllegalStateException("Could not kill process!?!");
}
}
}
if (timeout)
return "timeout";
boolean failure = p.exitValue() != 0;
p.destroy();
return failure ? "failure" : "success";
}
/**
* Makes a wrapper-file that loads the main-file in a global context (instead of the node-module context)
*/
private Path makeGlobalifier() {
try {
Path dir = instrumentationDir.resolve("globalifier");
Files.createDirectories(dir);
Path globalifier = dir.resolve("main.js");
String mainPath = instrumentationDir.resolve(rootRelativeMain).toString();
mainPath = mainPath.replace("\\", "\\\\"); // the path will be used as a string literal, sole backslashes will act as escape characters: escape them!
Files.write(globalifier, Arrays.asList(
"var fs = require('fs');",
"var code = " + String.format("fs.readFileSync('%s', 'utf-8');", mainPath),
String.format("require('vm').runInThisContext(code, '%s')", mainPath)
), StandardOpenOption.CREATE_NEW);
return globalifier;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
private class InstrumentationException extends Exception {
}
private class InstrumentationSyntaxErrorException extends InstrumentationException {
}
private class InstrumentationTimeoutException extends InstrumentationException {
}