1 /*
  2  * Copyright (c) 2016, 2023, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 
 24 package requires;
 25 
 26 import java.io.BufferedInputStream;
 27 import java.io.FileInputStream;
 28 import java.io.IOException;
 29 import java.io.InputStream;
 30 import java.io.File;
 31 import java.nio.charset.Charset;
 32 import java.nio.file.Files;
 33 import java.nio.file.Path;
 34 import java.nio.file.Paths;
 35 import java.nio.file.StandardOpenOption;
 36 import java.time.Instant;
 37 import java.util.ArrayList;
 38 import java.util.Collections;
 39 import java.util.HashMap;
 40 import java.util.List;
 41 import java.util.Map;
 42 import java.util.Properties;
 43 import java.util.Set;
 44 import java.util.concurrent.Callable;
 45 import java.util.concurrent.TimeUnit;
 46 import java.util.function.Supplier;
 47 import java.util.regex.Matcher;
 48 import java.util.regex.Pattern;
 49 
 50 import jdk.test.whitebox.code.Compiler;
 51 import jdk.test.whitebox.cpuinfo.CPUInfo;
 52 import jdk.test.whitebox.gc.GC;
 53 import jdk.test.whitebox.WhiteBox;
 54 import jdk.test.lib.Platform;
 55 import jdk.test.lib.Container;
 56 
 57 /**
 58  * The Class to be invoked by jtreg prior Test Suite execution to
 59  * collect information about VM.
 60  * Do not use any APIs that may not be available in all target VMs.
 61  * Properties set by this Class will be available in the @requires expressions.
 62  */
 63 public class VMProps implements Callable<Map<String, String>> {
 64     // value known to jtreg as an indicator of error state
 65     private static final String ERROR_STATE = "__ERROR__";
 66 
 67     private static final WhiteBox WB = WhiteBox.getWhiteBox();
 68 
 69     private static class SafeMap {
 70         private final Map<String, String> map = new HashMap<>();
 71 
 72         public void put(String key, Supplier<String> s) {
 73             String value;
 74             try {
 75                 value = s.get();
 76             } catch (Throwable t) {
 77                 System.err.println("failed to get value for " + key);
 78                 t.printStackTrace(System.err);
 79                 value = ERROR_STATE + t;
 80             }
 81             map.put(key, value);
 82         }
 83     }
 84 
 85     /**
 86      * Collects information about VM properties.
 87      * This method will be invoked by jtreg.
 88      *
 89      * @return Map of property-value pairs.
 90      */
 91     @Override
 92     public Map<String, String> call() {
 93         log("Entering call()");
 94         SafeMap map = new SafeMap();
 95         map.put("vm.flavor", this::vmFlavor);
 96         map.put("vm.compMode", this::vmCompMode);
 97         map.put("vm.bits", this::vmBits);
 98         map.put("vm.flightRecorder", this::vmFlightRecorder);
 99         map.put("vm.simpleArch", this::vmArch);
100         map.put("vm.debug", this::vmDebug);
101         map.put("vm.jvmci", this::vmJvmci);
102         map.put("vm.emulatedClient", this::vmEmulatedClient);
103         // vm.hasSA is "true" if the VM contains the serviceability agent
104         // and jhsdb.
105         map.put("vm.hasSA", this::vmHasSA);
106         // vm.hasJFR is "true" if JFR is included in the build of the VM and
107         // so tests can be executed.
108         map.put("vm.hasJFR", this::vmHasJFR);
109         map.put("vm.hasDTrace", this::vmHasDTrace);
110         map.put("vm.jvmti", this::vmHasJVMTI);
111         map.put("vm.cpu.features", this::cpuFeatures);
112         map.put("vm.pageSize", this::vmPageSize);
113         map.put("vm.rtm.cpu", this::vmRTMCPU);
114         map.put("vm.rtm.compiler", this::vmRTMCompiler);
115         // vm.cds is true if the VM is compiled with cds support.
116         map.put("vm.cds", this::vmCDS);
117         map.put("vm.cds.custom.loaders", this::vmCDSForCustomLoaders);
118         map.put("vm.cds.write.archived.java.heap", this::vmCDSCanWriteArchivedJavaHeap);
119         // vm.graal.enabled is true if Graal is used as JIT
120         map.put("vm.graal.enabled", this::isGraalEnabled);
121         map.put("vm.compiler1.enabled", this::isCompiler1Enabled);
122         map.put("vm.compiler2.enabled", this::isCompiler2Enabled);
123         map.put("docker.support", this::dockerSupport);
124         map.put("vm.musl", this::isMusl);
125         map.put("release.implementor", this::implementor);
126         map.put("jdk.containerized", this::jdkContainerized);
127         map.put("vm.flagless", this::isFlagless);
128         vmGC(map); // vm.gc.X = true/false
129         vmOptFinalFlags(map);
130 
131         dump(map.map);
132         log("Leaving call()");
133         return map.map;
134     }
135 
136     /**
137      * Print a stack trace before returning error state;
138      * Used by the various helper functions which parse information from
139      * VM properties in the case where they don't find an expected property
140      * or a property doesn't conform to an expected format.
141      *
142      * @return {@link #ERROR_STATE}
143      */
144     private String errorWithMessage(String message) {
145         new Exception(message).printStackTrace();
146         return ERROR_STATE + message;
147     }
148 
149     /**
150      * @return vm.simpleArch value of "os.simpleArch" property of tested JDK.
151      */
152     protected String vmArch() {
153         String arch = System.getProperty("os.arch");
154         if (arch.equals("x86_64") || arch.equals("amd64")) {
155             return "x64";
156         } else if (arch.contains("86")) {
157             return "x86";
158         } else {
159             return arch;
160         }
161     }
162 
163     /**
164      * @return VM type value extracted from the "java.vm.name" property.
165      */
166     protected String vmFlavor() {
167         // E.g. "Java HotSpot(TM) 64-Bit Server VM"
168         String vmName = System.getProperty("java.vm.name");
169         if (vmName == null) {
170             return errorWithMessage("Can't get 'java.vm.name' property");
171         }
172 
173         Pattern startP = Pattern.compile(".* (\\S+) VM");
174         Matcher m = startP.matcher(vmName);
175         if (m.matches()) {
176             return m.group(1).toLowerCase();
177         }
178         return errorWithMessage("Can't get VM flavor from 'java.vm.name'");
179     }
180 
181     /**
182      * @return VM compilation mode extracted from the "java.vm.info" property.
183      */
184     protected String vmCompMode() {
185         // E.g. "mixed mode"
186         String vmInfo = System.getProperty("java.vm.info");
187         if (vmInfo == null) {
188             return errorWithMessage("Can't get 'java.vm.info' property");
189         }
190         vmInfo = vmInfo.toLowerCase();
191         if (vmInfo.contains("mixed mode")) {
192             return "Xmixed";
193         } else if (vmInfo.contains("compiled mode")) {
194             return "Xcomp";
195         } else if (vmInfo.contains("interpreted mode")) {
196             return "Xint";
197         } else {
198             return errorWithMessage("Can't get compilation mode from 'java.vm.info'");
199         }
200     }
201 
202     /**
203      * @return VM bitness, the value of the "sun.arch.data.model" property.
204      */
205     protected String vmBits() {
206         String dataModel = System.getProperty("sun.arch.data.model");
207         if (dataModel != null) {
208             return dataModel;
209         } else {
210             return errorWithMessage("Can't get 'sun.arch.data.model' property");
211         }
212     }
213 
214     /**
215      * @return "true" if Flight Recorder is enabled, "false" if is disabled.
216      */
217     protected String vmFlightRecorder() {
218         Boolean isFlightRecorder = WB.getBooleanVMFlag("FlightRecorder");
219         String startFROptions = WB.getStringVMFlag("StartFlightRecording");
220         if (isFlightRecorder != null && isFlightRecorder) {
221             return "true";
222         }
223         if (startFROptions != null && !startFROptions.isEmpty()) {
224             return "true";
225         }
226         return "false";
227     }
228 
229     /**
230      * @return debug level value extracted from the "jdk.debug" property.
231      */
232     protected String vmDebug() {
233         String debug = System.getProperty("jdk.debug");
234         if (debug != null) {
235             return "" + debug.contains("debug");
236         } else {
237             return errorWithMessage("Can't get 'jdk.debug' property");
238         }
239     }
240 
241     /**
242      * @return true if VM supports JVMCI and false otherwise
243      */
244     protected String vmJvmci() {
245         // builds with jvmci have this flag
246         if (WB.getBooleanVMFlag("EnableJVMCI") == null) {
247             return "false";
248         }
249 
250         // Not all GCs have full JVMCI support
251         if (!WB.isJVMCISupportedByGC()) {
252           return "false";
253         }
254 
255         // Interpreted mode cannot enable JVMCI
256         if (vmCompMode().equals("Xint")) {
257           return "false";
258         }
259 
260         return "true";
261     }
262 
263     /**
264      * @return true if VM runs in emulated-client mode and false otherwise.
265      */
266     protected String vmEmulatedClient() {
267         String vmInfo = System.getProperty("java.vm.info");
268         if (vmInfo == null) {
269             return errorWithMessage("Can't get 'java.vm.info' property");
270         }
271         return "" + vmInfo.contains(" emulated-client");
272     }
273 
274     /**
275      * @return supported CPU features
276      */
277     protected String cpuFeatures() {
278         return CPUInfo.getFeatures().toString();
279     }
280 
281     /**
282      * For all existing GC sets vm.gc.X property.
283      * Example vm.gc.G1=true means:
284      *    VM supports G1
285      *    User either set G1 explicitely (-XX:+UseG1GC) or did not set any GC
286      *    G1 can be selected, i.e. it doesn't conflict with other VM flags
287      *
288      * @param map - property-value pairs
289      */
290     protected void vmGC(SafeMap map) {
291         var isJVMCIEnabled = Compiler.isJVMCIEnabled();
292         for (GC gc: GC.values()) {
293             map.put("vm.gc." + gc.name(),
294                     () -> "" + (gc.isSupported()
295                             && (!isJVMCIEnabled || gc.isSupportedByJVMCICompiler())
296                             && (gc.isSelected() || GC.isSelectedErgonomically())));
297         }
298     }
299 
300     /**
301      * Selected final flag.
302      *
303      * @param map - property-value pairs
304      * @param flagName - flag name
305      */
306     private void vmOptFinalFlag(SafeMap map, String flagName) {
307         map.put("vm.opt.final." + flagName,
308                 () -> String.valueOf(WB.getBooleanVMFlag(flagName)));
309     }
310 
311     /**
312      * Selected sets of final flags.
313      *
314      * @param map - property-value pairs
315      */
316     protected void vmOptFinalFlags(SafeMap map) {
317         vmOptFinalFlag(map, "ClassUnloading");
318         vmOptFinalFlag(map, "ClassUnloadingWithConcurrentMark");
319         vmOptFinalFlag(map, "UseCompressedOops");
320         vmOptFinalFlag(map, "UseVectorizedMismatchIntrinsic");
321         vmOptFinalFlag(map, "EnableJVMCI");
322         vmOptFinalFlag(map, "EliminateAllocations");
323         vmOptFinalFlag(map, "UseVtableBasedCHA");
324     }
325 
326     /**
327      * @return "true" if VM has a serviceability agent.
328      */
329     protected String vmHasSA() {
330         return "" + Platform.hasSA();
331     }
332 
333     /**
334      * @return "true" if the VM is compiled with Java Flight Recorder (JFR)
335      * support.
336      */
337     protected String vmHasJFR() {
338         return "" + WB.isJFRIncluded();
339     }
340 
341     /**
342      * @return "true" if the VM is compiled with JVMTI
343      */
344     protected String vmHasJVMTI() {
345         return "" + WB.isJVMTIIncluded();
346     }
347 
348     /**
349      * @return "true" if the VM is compiled with DTrace
350      */
351     protected String vmHasDTrace() {
352         return "" + WB.isDTraceIncluded();
353     }
354 
355     /**
356      * @return true if compiler in use supports RTM and false otherwise.
357      */
358     protected String vmRTMCompiler() {
359         boolean isRTMCompiler = false;
360 
361         if (Compiler.isC2Enabled() &&
362             (Platform.isX86() || Platform.isX64() || Platform.isPPC())) {
363             isRTMCompiler = true;
364         }
365         return "" + isRTMCompiler;
366     }
367 
368     /**
369      * @return true if VM runs RTM supported CPU and false otherwise.
370      */
371     protected String vmRTMCPU() {
372         return "" + CPUInfo.hasFeature("rtm");
373     }
374 
375     /**
376      * Check for CDS support.
377      *
378      * @return true if CDS is supported by the VM to be tested.
379      */
380     protected String vmCDS() {
381         return "" + WB.isCDSIncluded();
382     }
383 
384     /**
385      * Check for CDS support for custom loaders.
386      *
387      * @return true if CDS provides support for customer loader in the VM to be tested.
388      */
389     protected String vmCDSForCustomLoaders() {
390         return "" + ("true".equals(vmCDS()) && Platform.areCustomLoadersSupportedForCDS());
391     }
392 
393     /**
394      * @return true if this VM can write Java heap objects into the CDS archive
395      */
396     protected String vmCDSCanWriteArchivedJavaHeap() {
397         return "" + ("true".equals(vmCDS()) && WB.canWriteJavaHeapArchive());
398     }
399 
400     /**
401      * @return System page size in bytes.
402      */
403     protected String vmPageSize() {
404         return "" + WB.getVMPageSize();
405     }
406 
407     /**
408      * Check if Graal is used as JIT compiler.
409      *
410      * @return true if Graal is used as JIT compiler.
411      */
412     protected String isGraalEnabled() {
413         return "" + Compiler.isGraalEnabled();
414     }
415 
416     /**
417      * Check if Compiler1 is present.
418      *
419      * @return true if Compiler1 is used as JIT compiler, either alone or as part of the tiered system.
420      */
421     protected String isCompiler1Enabled() {
422         return "" + Compiler.isC1Enabled();
423     }
424 
425     /**
426      * Check if Compiler2 is present.
427      *
428      * @return true if Compiler2 is used as JIT compiler, either alone or as part of the tiered system.
429      */
430     protected String isCompiler2Enabled() {
431         return "" + Compiler.isC2Enabled();
432     }
433 
434    /**
435      * A simple check for docker support
436      *
437      * @return true if docker is supported in a given environment
438      */
439     protected String dockerSupport() {
440         log("Entering dockerSupport()");
441 
442         boolean isSupported = false;
443         if (Platform.isLinux()) {
444            // currently docker testing is only supported for Linux,
445            // on certain platforms
446 
447            String arch = System.getProperty("os.arch");
448 
449            if (Platform.isX64()) {
450               isSupported = true;
451            } else if (Platform.isAArch64()) {
452               isSupported = true;
453            } else if (Platform.isS390x()) {
454               isSupported = true;
455            } else if (arch.equals("ppc64le")) {
456               isSupported = true;
457            }
458         }
459 
460         log("dockerSupport(): platform check: isSupported = " + isSupported);
461 
462         if (isSupported) {
463            try {
464               isSupported = checkDockerSupport();
465            } catch (Exception e) {
466               isSupported = false;
467            }
468          }
469 
470         log("dockerSupport(): returning isSupported = " + isSupported);
471         return "" + isSupported;
472     }
473 
474     // Configures process builder to redirect process stdout and stderr to a file.
475     // Returns file names for stdout and stderr.
476     private Map<String, String> redirectOutputToLogFile(String msg, ProcessBuilder pb, String fileNameBase) {
477         Map<String, String> result = new HashMap<>();
478         String timeStamp = Instant.now().toString().replace(":", "-").replace(".", "-");
479 
480         String stdoutFileName = String.format("./%s-stdout--%s.log", fileNameBase, timeStamp);
481         pb.redirectOutput(new File(stdoutFileName));
482         log(msg + ": child process stdout redirected to " + stdoutFileName);
483         result.put("stdout", stdoutFileName);
484 
485         String stderrFileName = String.format("./%s-stderr--%s.log", fileNameBase, timeStamp);
486         pb.redirectError(new File(stderrFileName));
487         log(msg + ": child process stderr redirected to " + stderrFileName);
488         result.put("stderr", stderrFileName);
489 
490         return result;
491     }
492 
493     private void printLogfileContent(Map<String, String> logFileNames) {
494         logFileNames.entrySet().stream()
495             .forEach(entry ->
496                 {
497                     log("------------- " + entry.getKey());
498                     try {
499                         Files.lines(Path.of(entry.getValue()))
500                             .forEach(line -> log(line));
501                     } catch (IOException ie) {
502                         log("Exception while reading file: " + ie);
503                     }
504                     log("-------------");
505                 });
506     }
507 
508     private boolean checkDockerSupport() throws IOException, InterruptedException {
509         log("checkDockerSupport(): entering");
510         ProcessBuilder pb = new ProcessBuilder("which", Container.ENGINE_COMMAND);
511         Map<String, String> logFileNames =
512             redirectOutputToLogFile("checkDockerSupport(): which <container-engine>",
513                                                       pb, "which-container");
514         Process p = pb.start();
515         p.waitFor(10, TimeUnit.SECONDS);
516         int exitValue = p.exitValue();
517 
518         log(String.format("checkDockerSupport(): exitValue = %s, pid = %s", exitValue, p.pid()));
519         if (exitValue != 0) {
520             printLogfileContent(logFileNames);
521         }
522 
523         return (exitValue == 0);
524     }
525 
526     /**
527      * Checks musl libc.
528      *
529      * @return true if musl libc is used.
530      */
531     protected String isMusl() {
532         return Boolean.toString(WB.getLibcName().contains("musl"));
533     }
534 
535     private String implementor() {
536         try (InputStream in = new BufferedInputStream(new FileInputStream(
537                 System.getProperty("java.home") + "/release"))) {
538             Properties properties = new Properties();
539             properties.load(in);
540             String implementorProperty = properties.getProperty("IMPLEMENTOR");
541             if (implementorProperty != null) {
542                 return implementorProperty.replace("\"", "");
543             }
544             return errorWithMessage("Can't get 'IMPLEMENTOR' property from 'release' file");
545         } catch (IOException e) {
546             e.printStackTrace();
547             return errorWithMessage("Failed to read 'release' file " + e);
548         }
549     }
550 
551     private String jdkContainerized() {
552         String isEnabled = System.getenv("TEST_JDK_CONTAINERIZED");
553         return "" + "true".equalsIgnoreCase(isEnabled);
554     }
555 
556     /**
557      * Checks if we are in <i>almost</i> out-of-box configuration, i.e. the flags
558      * which JVM is started with don't affect its behavior "significantly".
559      * {@code TEST_VM_FLAGLESS} enviroment variable can be used to force this
560      * method to return true and allow any flags.
561      *
562      * @return true if there are no JVM flags
563      */
564     private String isFlagless() {
565         boolean result = true;
566         if (System.getenv("TEST_VM_FLAGLESS") != null) {
567             return "" + result;
568         }
569 
570         List<String> allFlags = new ArrayList<String>();
571         Collections.addAll(allFlags, System.getProperty("test.vm.opts", "").trim().split("\\s+"));
572         Collections.addAll(allFlags, System.getProperty("test.java.opts", "").trim().split("\\s+"));
573 
574         // check -XX flags
575         var ignoredXXFlags = Set.of(
576                 // added by run-test framework
577                 "MaxRAMPercentage",
578                 // added by test environment
579                 "CreateCoredumpOnCrash"
580         );
581         result &= allFlags.stream()
582                           .filter(s -> s.startsWith("-XX:"))
583                           // map to names:
584                               // remove -XX:
585                               .map(s -> s.substring(4))
586                               // remove +/- from bool flags
587                               .map(s -> s.charAt(0) == '+' || s.charAt(0) == '-' ? s.substring(1) : s)
588                               // remove =.* from others
589                               .map(s -> s.contains("=") ? s.substring(0, s.indexOf('=')) : s)
590                           // skip known-to-be-there flags
591                           .filter(s -> !ignoredXXFlags.contains(s))
592                           .findAny()
593                           .isEmpty();
594 
595         // check -X flags
596         var ignoredXFlags = Set.of(
597                 // default, yet still seen to be explicitly set
598                 "mixed",
599                 // -XmxmNNNm added by run-test framework for non-hotspot tests
600                 "mx"
601         );
602         result &= allFlags.stream()
603                           .filter(s -> s.startsWith("-X") && !s.startsWith("-XX:"))
604                           // map to names:
605                           // remove -X
606                           .map(s -> s.substring(2))
607                           // remove :.* from flags with values
608                           .map(s -> s.contains(":") ? s.substring(0, s.indexOf(':')) : s)
609                           // remove size like 4G, 768m which might be set for non-hotspot tests
610                           .map(s -> s.replaceAll("(\\d+)[mMgGkK]", ""))
611                           // skip known-to-be-there flags
612                           .filter(s -> !ignoredXFlags.contains(s))
613                           .findAny()
614                           .isEmpty();
615 
616         return "" + result;
617     }
618 
619     /**
620      * Dumps the map to the file if the file name is given as the property.
621      * This functionality could be helpful to know context in the real
622      * execution.
623      *
624      * @param map
625      */
626     protected static void dump(Map<String, String> map) {
627         String dumpFileName = System.getProperty("vmprops.dump");
628         if (dumpFileName == null) {
629             return;
630         }
631         List<String> lines = new ArrayList<>();
632         map.forEach((k, v) -> lines.add(k + ":" + v));
633         try {
634             Files.write(Paths.get(dumpFileName), lines,
635                     StandardOpenOption.APPEND, StandardOpenOption.CREATE);
636         } catch (IOException e) {
637             throw new RuntimeException("Failed to dump properties into '"
638                     + dumpFileName + "'", e);
639         }
640     }
641 
642     /**
643      * Log diagnostic message.
644      *
645      * @param msg
646      */
647     protected static void log(String msg) {
648         // Always log to a file.
649         logToFile(msg);
650 
651         // Also log to stderr; guarded by property to avoid excessive verbosity.
652         // By jtreg design stderr produced here will be visible
653         // in the output of a parent process. Note: stdout should not be used
654         // for logging as jtreg parses that output directly and only echoes it
655         // in the event of a failure.
656         if (Boolean.getBoolean("jtreg.log.vmprops")) {
657             System.err.println("VMProps: " + msg);
658         }
659     }
660 
661     /**
662      * Log diagnostic message to a file.
663      *
664      * @param msg
665      */
666     protected static void logToFile(String msg) {
667         String fileName = "./vmprops.log";
668         try {
669             Files.writeString(Paths.get(fileName), msg + "\n", Charset.forName("ISO-8859-1"),
670                     StandardOpenOption.APPEND, StandardOpenOption.CREATE);
671         } catch (IOException e) {
672             throw new RuntimeException("Failed to log into '" + fileName + "'", e);
673         }
674     }
675 
676     /**
677      * This method is for the testing purpose only.
678      *
679      * @param args
680      */
681     public static void main(String args[]) {
682         Map<String, String> map = new VMProps().call();
683         map.forEach((k, v) -> System.out.println(k + ": '" + v + "'"));
684     }
685 }