1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "cmdline_parser.h"
18 
19 #include <numeric>
20 
21 #include "gtest/gtest.h"
22 
23 #include "base/mutex.h"
24 #include "base/utils.h"
25 #include "jdwp_provider.h"
26 #include "experimental_flags.h"
27 #include "parsed_options.h"
28 #include "runtime.h"
29 #include "runtime_options.h"
30 
31 #define EXPECT_NULL(expected) EXPECT_EQ(reinterpret_cast<const void*>(expected), \
32                                         reinterpret_cast<void*>(nullptr));
33 
34 namespace art {
35   bool UsuallyEquals(double expected, double actual);
36 
37   // This has a gtest dependency, which is why it's in the gtest only.
operator ==(const ProfileSaverOptions & lhs,const ProfileSaverOptions & rhs)38   bool operator==(const ProfileSaverOptions& lhs, const ProfileSaverOptions& rhs) {
39     return lhs.enabled_ == rhs.enabled_ &&
40         lhs.min_save_period_ms_ == rhs.min_save_period_ms_ &&
41         lhs.save_resolved_classes_delay_ms_ == rhs.save_resolved_classes_delay_ms_ &&
42         lhs.hot_startup_method_samples_ == rhs.hot_startup_method_samples_ &&
43         lhs.min_methods_to_save_ == rhs.min_methods_to_save_ &&
44         lhs.min_classes_to_save_ == rhs.min_classes_to_save_ &&
45         lhs.min_notification_before_wake_ == rhs.min_notification_before_wake_ &&
46         lhs.max_notification_before_wake_ == rhs.max_notification_before_wake_;
47   }
48 
UsuallyEquals(double expected,double actual)49   bool UsuallyEquals(double expected, double actual) {
50     using FloatingPoint = ::testing::internal::FloatingPoint<double>;
51 
52     FloatingPoint exp(expected);
53     FloatingPoint act(actual);
54 
55     // Compare with ULPs instead of comparing with ==
56     return exp.AlmostEquals(act);
57   }
58 
59   template <typename T>
UsuallyEquals(const T & expected,const T & actual,typename std::enable_if<detail::SupportsEqualityOperator<T>::value>::type * =nullptr)60   bool UsuallyEquals(const T& expected, const T& actual,
61                      typename std::enable_if<
62                          detail::SupportsEqualityOperator<T>::value>::type* = nullptr) {
63     return expected == actual;
64   }
65 
66   template <char Separator>
UsuallyEquals(const std::vector<std::string> & expected,const ParseStringList<Separator> & actual)67   bool UsuallyEquals(const std::vector<std::string>& expected,
68                      const ParseStringList<Separator>& actual) {
69     return expected == static_cast<std::vector<std::string>>(actual);
70   }
71 
72   // Try to use memcmp to compare simple plain-old-data structs.
73   //
74   // This should *not* generate false positives, but it can generate false negatives.
75   // This will mostly work except for fields like float which can have different bit patterns
76   // that are nevertheless equal.
77   // If a test is failing because the structs aren't "equal" when they really are
78   // then it's recommended to implement operator== for it instead.
79   template <typename T, typename ... Ignore>
UsuallyEquals(const T & expected,const T & actual,const Ignore &...more ATTRIBUTE_UNUSED,typename std::enable_if<std::is_pod<T>::value>::type * =nullptr,typename std::enable_if<!detail::SupportsEqualityOperator<T>::value>::type * =nullptr)80   bool UsuallyEquals(const T& expected, const T& actual,
81                      const Ignore& ... more ATTRIBUTE_UNUSED,
82                      typename std::enable_if<std::is_pod<T>::value>::type* = nullptr,
83                      typename std::enable_if<!detail::SupportsEqualityOperator<T>::value>::type* = nullptr
84                      ) {
85     return memcmp(std::addressof(expected), std::addressof(actual), sizeof(T)) == 0;
86   }
87 
UsuallyEquals(const XGcOption & expected,const XGcOption & actual)88   bool UsuallyEquals(const XGcOption& expected, const XGcOption& actual) {
89     return memcmp(std::addressof(expected), std::addressof(actual), sizeof(expected)) == 0;
90   }
91 
UsuallyEquals(const char * expected,const std::string & actual)92   bool UsuallyEquals(const char* expected, const std::string& actual) {
93     return std::string(expected) == actual;
94   }
95 
96   template <typename TMap, typename TKey, typename T>
IsExpectedKeyValue(const T & expected,const TMap & map,const TKey & key)97   ::testing::AssertionResult IsExpectedKeyValue(const T& expected,
98                                                 const TMap& map,
99                                                 const TKey& key) {
100     auto* actual = map.Get(key);
101     if (actual != nullptr) {
102       if (!UsuallyEquals(expected, *actual)) {
103         return ::testing::AssertionFailure()
104           << "expected " << detail::ToStringAny(expected) << " but got "
105           << detail::ToStringAny(*actual);
106       }
107       return ::testing::AssertionSuccess();
108     }
109 
110     return ::testing::AssertionFailure() << "key was not in the map";
111   }
112 
113   template <typename TMap, typename TKey, typename T>
IsExpectedDefaultKeyValue(const T & expected,const TMap & map,const TKey & key)114   ::testing::AssertionResult IsExpectedDefaultKeyValue(const T& expected,
115                                                        const TMap& map,
116                                                        const TKey& key) {
117     const T& actual = map.GetOrDefault(key);
118     if (!UsuallyEquals(expected, actual)) {
119       return ::testing::AssertionFailure()
120           << "expected " << detail::ToStringAny(expected) << " but got "
121           << detail::ToStringAny(actual);
122      }
123     return ::testing::AssertionSuccess();
124   }
125 
126 class CmdlineParserTest : public ::testing::Test {
127  public:
128   CmdlineParserTest() = default;
129   ~CmdlineParserTest() = default;
130 
131  protected:
132   using M = RuntimeArgumentMap;
133   using RuntimeParser = ParsedOptions::RuntimeParser;
134 
SetUpTestCase()135   static void SetUpTestCase() {
136     art::Locks::Init();
137     art::InitLogging(nullptr, art::Runtime::Abort);  // argv = null
138   }
139 
SetUp()140   void SetUp() override {
141     parser_ = ParsedOptions::MakeParser(false);  // do not ignore unrecognized options
142   }
143 
IsResultSuccessful(const CmdlineResult & result)144   static ::testing::AssertionResult IsResultSuccessful(const CmdlineResult& result) {
145     if (result.IsSuccess()) {
146       return ::testing::AssertionSuccess();
147     } else {
148       return ::testing::AssertionFailure()
149         << result.GetStatus() << " with: " << result.GetMessage();
150     }
151   }
152 
IsResultFailure(const CmdlineResult & result,CmdlineResult::Status failure_status)153   static ::testing::AssertionResult IsResultFailure(const CmdlineResult& result,
154                                                     CmdlineResult::Status failure_status) {
155     if (result.IsSuccess()) {
156       return ::testing::AssertionFailure() << " got success but expected failure: "
157           << failure_status;
158     } else if (result.GetStatus() == failure_status) {
159       return ::testing::AssertionSuccess();
160     }
161 
162     return ::testing::AssertionFailure() << " expected failure " << failure_status
163         << " but got " << result.GetStatus();
164   }
165 
166   std::unique_ptr<RuntimeParser> parser_;
167 };
168 
169 #define EXPECT_KEY_EXISTS(map, key) EXPECT_TRUE((map).Exists(key))
170 #define EXPECT_KEY_VALUE(map, key, expected) EXPECT_TRUE(IsExpectedKeyValue(expected, map, key))
171 #define EXPECT_DEFAULT_KEY_VALUE(map, key, expected) EXPECT_TRUE(IsExpectedDefaultKeyValue(expected, map, key))
172 
173 #define _EXPECT_SINGLE_PARSE_EMPTY_SUCCESS(argv)              \
174   do {                                                        \
175     EXPECT_TRUE(IsResultSuccessful(parser_->Parse(argv)));    \
176     EXPECT_EQ(0u, parser_->GetArgumentsMap().Size());         \
177 
178 #define EXPECT_SINGLE_PARSE_EMPTY_SUCCESS(argv)               \
179   _EXPECT_SINGLE_PARSE_EMPTY_SUCCESS(argv);                   \
180   } while (false)
181 
182 #define EXPECT_SINGLE_PARSE_DEFAULT_VALUE(expected, argv, key)\
183   _EXPECT_SINGLE_PARSE_EMPTY_SUCCESS(argv);                   \
184     RuntimeArgumentMap args = parser_->ReleaseArgumentsMap(); \
185     EXPECT_DEFAULT_KEY_VALUE(args, key, expected);            \
186   } while (false)                                             // NOLINT [readability/namespace] [5]
187 
188 #define _EXPECT_SINGLE_PARSE_EXISTS(argv, key)                \
189   do {                                                        \
190     EXPECT_TRUE(IsResultSuccessful(parser_->Parse(argv)));    \
191     RuntimeArgumentMap args = parser_->ReleaseArgumentsMap(); \
192     EXPECT_EQ(1u, args.Size());                               \
193     EXPECT_KEY_EXISTS(args, key);                             \
194 
195 #define EXPECT_SINGLE_PARSE_EXISTS(argv, key)                 \
196     _EXPECT_SINGLE_PARSE_EXISTS(argv, key);                   \
197   } while (false)
198 
199 #define EXPECT_SINGLE_PARSE_VALUE(expected, argv, key)        \
200     _EXPECT_SINGLE_PARSE_EXISTS(argv, key);                   \
201     EXPECT_KEY_VALUE(args, key, expected);                    \
202   } while (false)
203 
204 #define EXPECT_SINGLE_PARSE_VALUE_STR(expected, argv, key)    \
205   EXPECT_SINGLE_PARSE_VALUE(std::string(expected), argv, key)
206 
207 #define EXPECT_SINGLE_PARSE_FAIL(argv, failure_status)         \
208     do {                                                       \
209       EXPECT_TRUE(IsResultFailure(parser_->Parse(argv), failure_status));\
210       RuntimeArgumentMap args = parser_->ReleaseArgumentsMap();\
211       EXPECT_EQ(0u, args.Size());                              \
212     } while (false)
213 
TEST_F(CmdlineParserTest,TestSimpleSuccesses)214 TEST_F(CmdlineParserTest, TestSimpleSuccesses) {
215   auto& parser = *parser_;
216 
217   EXPECT_LT(0u, parser.CountDefinedArguments());
218 
219   {
220     // Test case 1: No command line arguments
221     EXPECT_TRUE(IsResultSuccessful(parser.Parse("")));
222     RuntimeArgumentMap args = parser.ReleaseArgumentsMap();
223     EXPECT_EQ(0u, args.Size());
224   }
225 
226   EXPECT_SINGLE_PARSE_EXISTS("-Xzygote", M::Zygote);
227   EXPECT_SINGLE_PARSE_VALUE(std::vector<std::string>({"/hello/world"}),
228                             "-Xbootclasspath:/hello/world",
229                             M::BootClassPath);
230   EXPECT_SINGLE_PARSE_VALUE(std::vector<std::string>({"/hello", "/world"}),
231                             "-Xbootclasspath:/hello:/world",
232                             M::BootClassPath);
233   EXPECT_SINGLE_PARSE_VALUE_STR("/hello/world", "-classpath /hello/world", M::ClassPath);
234   EXPECT_SINGLE_PARSE_VALUE(Memory<1>(234), "-Xss234", M::StackSize);
235   EXPECT_SINGLE_PARSE_VALUE(MemoryKiB(1234*MB), "-Xms1234m", M::MemoryInitialSize);
236   EXPECT_SINGLE_PARSE_VALUE(true, "-XX:EnableHSpaceCompactForOOM", M::EnableHSpaceCompactForOOM);
237   EXPECT_SINGLE_PARSE_VALUE(false, "-XX:DisableHSpaceCompactForOOM", M::EnableHSpaceCompactForOOM);
238   EXPECT_SINGLE_PARSE_VALUE(0.5, "-XX:HeapTargetUtilization=0.5", M::HeapTargetUtilization);
239   EXPECT_SINGLE_PARSE_VALUE(5u, "-XX:ParallelGCThreads=5", M::ParallelGCThreads);
240   EXPECT_SINGLE_PARSE_EXISTS("-Xno-dex-file-fallback", M::NoDexFileFallback);
241 }  // TEST_F
242 
TEST_F(CmdlineParserTest,TestSimpleFailures)243 TEST_F(CmdlineParserTest, TestSimpleFailures) {
244   // Test argument is unknown to the parser
245   EXPECT_SINGLE_PARSE_FAIL("abcdefg^%@#*(@#", CmdlineResult::kUnknown);
246   // Test value map substitution fails
247   EXPECT_SINGLE_PARSE_FAIL("-Xverify:whatever", CmdlineResult::kFailure);
248   // Test value type parsing failures
249   EXPECT_SINGLE_PARSE_FAIL("-Xsswhatever", CmdlineResult::kFailure);  // invalid memory value
250   EXPECT_SINGLE_PARSE_FAIL("-Xms123", CmdlineResult::kFailure);       // memory value too small
251   EXPECT_SINGLE_PARSE_FAIL("-XX:HeapTargetUtilization=0.0", CmdlineResult::kOutOfRange);  // toosmal
252   EXPECT_SINGLE_PARSE_FAIL("-XX:HeapTargetUtilization=2.0", CmdlineResult::kOutOfRange);  // toolarg
253   EXPECT_SINGLE_PARSE_FAIL("-XX:ParallelGCThreads=-5", CmdlineResult::kOutOfRange);  // too small
254   EXPECT_SINGLE_PARSE_FAIL("-Xgc:blablabla", CmdlineResult::kUsage);  // not a valid suboption
255 }  // TEST_F
256 
TEST_F(CmdlineParserTest,TestLogVerbosity)257 TEST_F(CmdlineParserTest, TestLogVerbosity) {
258   {
259     const char* log_args = "-verbose:"
260         "class,compiler,gc,heap,jdwp,jni,monitor,profiler,signals,simulator,startup,"
261         "third-party-jni,threads,verifier,verifier-debug";
262 
263     LogVerbosity log_verbosity = LogVerbosity();
264     log_verbosity.class_linker = true;
265     log_verbosity.compiler = true;
266     log_verbosity.gc = true;
267     log_verbosity.heap = true;
268     log_verbosity.jdwp = true;
269     log_verbosity.jni = true;
270     log_verbosity.monitor = true;
271     log_verbosity.profiler = true;
272     log_verbosity.signals = true;
273     log_verbosity.simulator = true;
274     log_verbosity.startup = true;
275     log_verbosity.third_party_jni = true;
276     log_verbosity.threads = true;
277     log_verbosity.verifier = true;
278     log_verbosity.verifier_debug = true;
279 
280     EXPECT_SINGLE_PARSE_VALUE(log_verbosity, log_args, M::Verbose);
281   }
282 
283   {
284     const char* log_args = "-verbose:"
285         "class,compiler,gc,heap,jdwp,jni,monitor";
286 
287     LogVerbosity log_verbosity = LogVerbosity();
288     log_verbosity.class_linker = true;
289     log_verbosity.compiler = true;
290     log_verbosity.gc = true;
291     log_verbosity.heap = true;
292     log_verbosity.jdwp = true;
293     log_verbosity.jni = true;
294     log_verbosity.monitor = true;
295 
296     EXPECT_SINGLE_PARSE_VALUE(log_verbosity, log_args, M::Verbose);
297   }
298 
299   EXPECT_SINGLE_PARSE_FAIL("-verbose:blablabla", CmdlineResult::kUsage);  // invalid verbose opt
300 
301   {
302     const char* log_args = "-verbose:deopt";
303     LogVerbosity log_verbosity = LogVerbosity();
304     log_verbosity.deopt = true;
305     EXPECT_SINGLE_PARSE_VALUE(log_verbosity, log_args, M::Verbose);
306   }
307 
308   {
309     const char* log_args = "-verbose:collector";
310     LogVerbosity log_verbosity = LogVerbosity();
311     log_verbosity.collector = true;
312     EXPECT_SINGLE_PARSE_VALUE(log_verbosity, log_args, M::Verbose);
313   }
314 
315   {
316     const char* log_args = "-verbose:oat";
317     LogVerbosity log_verbosity = LogVerbosity();
318     log_verbosity.oat = true;
319     EXPECT_SINGLE_PARSE_VALUE(log_verbosity, log_args, M::Verbose);
320   }
321 
322   {
323     const char* log_args = "-verbose:dex";
324     LogVerbosity log_verbosity = LogVerbosity();
325     log_verbosity.dex = true;
326     EXPECT_SINGLE_PARSE_VALUE(log_verbosity, log_args, M::Verbose);
327   }
328 }  // TEST_F
329 
330 // TODO: Enable this b/19274810
TEST_F(CmdlineParserTest,DISABLED_TestXGcOption)331 TEST_F(CmdlineParserTest, DISABLED_TestXGcOption) {
332   /*
333    * Test success
334    */
335   {
336     XGcOption option_all_true{};
337     option_all_true.collector_type_ = gc::CollectorType::kCollectorTypeCMS;
338     option_all_true.verify_pre_gc_heap_ = true;
339     option_all_true.verify_pre_sweeping_heap_ = true;
340     option_all_true.verify_post_gc_heap_ = true;
341     option_all_true.verify_pre_gc_rosalloc_ = true;
342     option_all_true.verify_pre_sweeping_rosalloc_ = true;
343     option_all_true.verify_post_gc_rosalloc_ = true;
344 
345     const char * xgc_args_all_true = "-Xgc:concurrent,"
346         "preverify,presweepingverify,postverify,"
347         "preverify_rosalloc,presweepingverify_rosalloc,"
348         "postverify_rosalloc,precise,"
349         "verifycardtable";
350 
351     EXPECT_SINGLE_PARSE_VALUE(option_all_true, xgc_args_all_true, M::GcOption);
352 
353     XGcOption option_all_false{};
354     option_all_false.collector_type_ = gc::CollectorType::kCollectorTypeMS;
355     option_all_false.verify_pre_gc_heap_ = false;
356     option_all_false.verify_pre_sweeping_heap_ = false;
357     option_all_false.verify_post_gc_heap_ = false;
358     option_all_false.verify_pre_gc_rosalloc_ = false;
359     option_all_false.verify_pre_sweeping_rosalloc_ = false;
360     option_all_false.verify_post_gc_rosalloc_ = false;
361 
362     const char* xgc_args_all_false = "-Xgc:nonconcurrent,"
363         "nopreverify,nopresweepingverify,nopostverify,nopreverify_rosalloc,"
364         "nopresweepingverify_rosalloc,nopostverify_rosalloc,noprecise,noverifycardtable";
365 
366     EXPECT_SINGLE_PARSE_VALUE(option_all_false, xgc_args_all_false, M::GcOption);
367 
368     XGcOption option_all_default{};
369 
370     const char* xgc_args_blank = "-Xgc:";
371     EXPECT_SINGLE_PARSE_VALUE(option_all_default, xgc_args_blank, M::GcOption);
372   }
373 
374   /*
375    * Test failures
376    */
377   EXPECT_SINGLE_PARSE_FAIL("-Xgc:blablabla", CmdlineResult::kUsage);  // invalid Xgc opt
378 }  // TEST_F
379 
380 /*
381  * { "-XjdwpProvider:_" }
382  */
TEST_F(CmdlineParserTest,TestJdwpProviderEmpty)383 TEST_F(CmdlineParserTest, TestJdwpProviderEmpty) {
384   {
385     EXPECT_SINGLE_PARSE_DEFAULT_VALUE(JdwpProvider::kUnset, "", M::JdwpProvider);
386   }
387 }  // TEST_F
388 
TEST_F(CmdlineParserTest,TestJdwpProviderDefault)389 TEST_F(CmdlineParserTest, TestJdwpProviderDefault) {
390   const char* opt_args = "-XjdwpProvider:default";
391   EXPECT_SINGLE_PARSE_VALUE(JdwpProvider::kDefaultJdwpProvider, opt_args, M::JdwpProvider);
392 }  // TEST_F
393 
TEST_F(CmdlineParserTest,TestJdwpProviderInternal)394 TEST_F(CmdlineParserTest, TestJdwpProviderInternal) {
395   const char* opt_args = "-XjdwpProvider:internal";
396   EXPECT_SINGLE_PARSE_VALUE(JdwpProvider::kInternal, opt_args, M::JdwpProvider);
397 }  // TEST_F
398 
TEST_F(CmdlineParserTest,TestJdwpProviderNone)399 TEST_F(CmdlineParserTest, TestJdwpProviderNone) {
400   const char* opt_args = "-XjdwpProvider:none";
401   EXPECT_SINGLE_PARSE_VALUE(JdwpProvider::kNone, opt_args, M::JdwpProvider);
402 }  // TEST_F
403 
TEST_F(CmdlineParserTest,TestJdwpProviderAdbconnection)404 TEST_F(CmdlineParserTest, TestJdwpProviderAdbconnection) {
405   const char* opt_args = "-XjdwpProvider:adbconnection";
406   EXPECT_SINGLE_PARSE_VALUE(JdwpProvider::kAdbConnection, opt_args, M::JdwpProvider);
407 }  // TEST_F
408 
TEST_F(CmdlineParserTest,TestJdwpProviderHelp)409 TEST_F(CmdlineParserTest, TestJdwpProviderHelp) {
410   EXPECT_SINGLE_PARSE_FAIL("-XjdwpProvider:help", CmdlineResult::kUsage);
411 }  // TEST_F
412 
TEST_F(CmdlineParserTest,TestJdwpProviderFail)413 TEST_F(CmdlineParserTest, TestJdwpProviderFail) {
414   EXPECT_SINGLE_PARSE_FAIL("-XjdwpProvider:blablabla", CmdlineResult::kFailure);
415 }  // TEST_F
416 
417 /*
418  * -D_ -D_ -D_ ...
419  */
TEST_F(CmdlineParserTest,TestPropertiesList)420 TEST_F(CmdlineParserTest, TestPropertiesList) {
421   /*
422    * Test successes
423    */
424   {
425     std::vector<std::string> opt = {"hello"};
426 
427     EXPECT_SINGLE_PARSE_VALUE(opt, "-Dhello", M::PropertiesList);
428   }
429 
430   {
431     std::vector<std::string> opt = {"hello", "world"};
432 
433     EXPECT_SINGLE_PARSE_VALUE(opt, "-Dhello -Dworld", M::PropertiesList);
434   }
435 
436   {
437     std::vector<std::string> opt = {"one", "two", "three"};
438 
439     EXPECT_SINGLE_PARSE_VALUE(opt, "-Done -Dtwo -Dthree", M::PropertiesList);
440   }
441 }  // TEST_F
442 
443 /*
444 * -Xcompiler-option foo -Xcompiler-option bar ...
445 */
TEST_F(CmdlineParserTest,TestCompilerOption)446 TEST_F(CmdlineParserTest, TestCompilerOption) {
447  /*
448   * Test successes
449   */
450   {
451     std::vector<std::string> opt = {"hello"};
452     EXPECT_SINGLE_PARSE_VALUE(opt, "-Xcompiler-option hello", M::CompilerOptions);
453   }
454 
455   {
456     std::vector<std::string> opt = {"hello", "world"};
457     EXPECT_SINGLE_PARSE_VALUE(opt,
458                               "-Xcompiler-option hello -Xcompiler-option world",
459                               M::CompilerOptions);
460   }
461 
462   {
463     std::vector<std::string> opt = {"one", "two", "three"};
464     EXPECT_SINGLE_PARSE_VALUE(opt,
465                               "-Xcompiler-option one -Xcompiler-option two -Xcompiler-option three",
466                               M::CompilerOptions);
467   }
468 }  // TEST_F
469 
470 /*
471 * -Xjit, -Xnojit, -Xjitcodecachesize, Xjitcompilethreshold
472 */
TEST_F(CmdlineParserTest,TestJitOptions)473 TEST_F(CmdlineParserTest, TestJitOptions) {
474  /*
475   * Test successes
476   */
477   {
478     EXPECT_SINGLE_PARSE_VALUE(true, "-Xusejit:true", M::UseJitCompilation);
479     EXPECT_SINGLE_PARSE_VALUE(false, "-Xusejit:false", M::UseJitCompilation);
480   }
481   {
482     EXPECT_SINGLE_PARSE_VALUE(
483         MemoryKiB(16 * KB), "-Xjitinitialsize:16K", M::JITCodeCacheInitialCapacity);
484     EXPECT_SINGLE_PARSE_VALUE(
485         MemoryKiB(16 * MB), "-Xjitmaxsize:16M", M::JITCodeCacheMaxCapacity);
486   }
487   {
488     EXPECT_SINGLE_PARSE_VALUE(12345u, "-Xjitthreshold:12345", M::JITCompileThreshold);
489   }
490 }  // TEST_F
491 
492 /*
493 * -Xps-*
494 */
TEST_F(CmdlineParserTest,ProfileSaverOptions)495 TEST_F(CmdlineParserTest, ProfileSaverOptions) {
496   ProfileSaverOptions opt = ProfileSaverOptions(true, 1, 2, 3, 4, 5, 6, 7, "abc", true);
497 
498   EXPECT_SINGLE_PARSE_VALUE(opt,
499                             "-Xjitsaveprofilinginfo "
500                             "-Xps-min-save-period-ms:1 "
501                             "-Xps-save-resolved-classes-delay-ms:2 "
502                             "-Xps-hot-startup-method-samples:3 "
503                             "-Xps-min-methods-to-save:4 "
504                             "-Xps-min-classes-to-save:5 "
505                             "-Xps-min-notification-before-wake:6 "
506                             "-Xps-max-notification-before-wake:7 "
507                             "-Xps-profile-path:abc "
508                             "-Xps-profile-boot-class-path",
509                             M::ProfileSaverOpts);
510 }  // TEST_F
511 
512 /* -Xexperimental:_ */
TEST_F(CmdlineParserTest,TestExperimentalFlags)513 TEST_F(CmdlineParserTest, TestExperimentalFlags) {
514   // Default
515   EXPECT_SINGLE_PARSE_DEFAULT_VALUE(ExperimentalFlags::kNone,
516                                     "",
517                                     M::Experimental);
518 
519   // Disabled explicitly
520   EXPECT_SINGLE_PARSE_VALUE(ExperimentalFlags::kNone,
521                             "-Xexperimental:none",
522                             M::Experimental);
523 }
524 
525 // -Xverify:_
TEST_F(CmdlineParserTest,TestVerify)526 TEST_F(CmdlineParserTest, TestVerify) {
527   EXPECT_SINGLE_PARSE_VALUE(verifier::VerifyMode::kNone,     "-Xverify:none",     M::Verify);
528   EXPECT_SINGLE_PARSE_VALUE(verifier::VerifyMode::kEnable,   "-Xverify:remote",   M::Verify);
529   EXPECT_SINGLE_PARSE_VALUE(verifier::VerifyMode::kEnable,   "-Xverify:all",      M::Verify);
530   EXPECT_SINGLE_PARSE_VALUE(verifier::VerifyMode::kSoftFail, "-Xverify:softfail", M::Verify);
531 }
532 
TEST_F(CmdlineParserTest,TestIgnoreUnrecognized)533 TEST_F(CmdlineParserTest, TestIgnoreUnrecognized) {
534   RuntimeParser::Builder parserBuilder;
535 
536   parserBuilder
537       .Define("-help")
538           .IntoKey(M::Help)
539       .IgnoreUnrecognized(true);
540 
541   parser_.reset(new RuntimeParser(parserBuilder.Build()));
542 
543   EXPECT_SINGLE_PARSE_EMPTY_SUCCESS("-non-existent-option");
544   EXPECT_SINGLE_PARSE_EMPTY_SUCCESS("-non-existent-option1 --non-existent-option-2");
545 }  //  TEST_F
546 
TEST_F(CmdlineParserTest,TestIgnoredArguments)547 TEST_F(CmdlineParserTest, TestIgnoredArguments) {
548   std::initializer_list<const char*> ignored_args = {
549       "-ea", "-da", "-enableassertions", "-disableassertions", "--runtime-arg", "-esa",
550       "-dsa", "-enablesystemassertions", "-disablesystemassertions", "-Xrs", "-Xint:abdef",
551       "-Xdexopt:foobar", "-Xnoquithandler", "-Xjnigreflimit:ixnay", "-Xgenregmap", "-Xnogenregmap",
552       "-Xverifyopt:never", "-Xcheckdexsum", "-Xincludeselectedop", "-Xjitop:noop",
553       "-Xincludeselectedmethod", "-Xjitblocking", "-Xjitmethod:_", "-Xjitclass:nosuchluck",
554       "-Xjitoffset:none", "-Xjitconfig:yes", "-Xjitcheckcg", "-Xjitverbose", "-Xjitprofile",
555       "-Xjitdisableopt", "-Xjitsuspendpoll", "-XX:mainThreadStackSize=1337"
556   };
557 
558   // Check they are ignored when parsed one at a time
559   for (auto&& arg : ignored_args) {
560     SCOPED_TRACE(arg);
561     EXPECT_SINGLE_PARSE_EMPTY_SUCCESS(arg);
562   }
563 
564   // Check they are ignored when we pass it all together at once
565   std::vector<const char*> argv = ignored_args;
566   EXPECT_SINGLE_PARSE_EMPTY_SUCCESS(argv);
567 }  //  TEST_F
568 
TEST_F(CmdlineParserTest,MultipleArguments)569 TEST_F(CmdlineParserTest, MultipleArguments) {
570   EXPECT_TRUE(IsResultSuccessful(parser_->Parse(
571       "-help -XX:ForegroundHeapGrowthMultiplier=0.5 "
572       "-Xmethod-trace -XX:LargeObjectSpace=map")));
573 
574   auto&& map = parser_->ReleaseArgumentsMap();
575   EXPECT_EQ(4u, map.Size());
576   EXPECT_KEY_VALUE(map, M::Help, Unit{});
577   EXPECT_KEY_VALUE(map, M::ForegroundHeapGrowthMultiplier, 0.5);
578   EXPECT_KEY_VALUE(map, M::MethodTrace, Unit{});
579   EXPECT_KEY_VALUE(map, M::LargeObjectSpace, gc::space::LargeObjectSpaceType::kMap);
580 }  //  TEST_F
581 }  // namespace art
582