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 #include "runtime/runtime_options.h"
19 #include "runtime/parsed_options.h"
20 
21 #include "utils.h"
22 #include <numeric>
23 #include "gtest/gtest.h"
24 
25 #define EXPECT_NULL(expected) EXPECT_EQ(reinterpret_cast<const void*>(expected), \
26                                         reinterpret_cast<void*>(nullptr));
27 
28 namespace art {
29   bool UsuallyEquals(double expected, double actual);
30 
31   // This has a gtest dependency, which is why it's in the gtest only.
operator ==(const TestProfilerOptions & lhs,const TestProfilerOptions & rhs)32   bool operator==(const TestProfilerOptions& lhs, const TestProfilerOptions& rhs) {
33     return lhs.enabled_ == rhs.enabled_ &&
34         lhs.output_file_name_ == rhs.output_file_name_ &&
35         lhs.period_s_ == rhs.period_s_ &&
36         lhs.duration_s_ == rhs.duration_s_ &&
37         lhs.interval_us_ == rhs.interval_us_ &&
38         UsuallyEquals(lhs.backoff_coefficient_, rhs.backoff_coefficient_) &&
39         UsuallyEquals(lhs.start_immediately_, rhs.start_immediately_) &&
40         UsuallyEquals(lhs.top_k_threshold_, rhs.top_k_threshold_) &&
41         UsuallyEquals(lhs.top_k_change_threshold_, rhs.top_k_change_threshold_) &&
42         lhs.profile_type_ == rhs.profile_type_ &&
43         lhs.max_stack_depth_ == rhs.max_stack_depth_;
44   }
45 
UsuallyEquals(double expected,double actual)46   bool UsuallyEquals(double expected, double actual) {
47     using FloatingPoint = ::testing::internal::FloatingPoint<double>;
48 
49     FloatingPoint exp(expected);
50     FloatingPoint act(actual);
51 
52     // Compare with ULPs instead of comparing with ==
53     return exp.AlmostEquals(act);
54   }
55 
56   template <typename T>
UsuallyEquals(const T & expected,const T & actual,typename std::enable_if<detail::SupportsEqualityOperator<T>::value>::type * =0)57   bool UsuallyEquals(const T& expected, const T& actual,
58                      typename std::enable_if<
59                          detail::SupportsEqualityOperator<T>::value>::type* = 0) {
60     return expected == actual;
61   }
62 
63   // Try to use memcmp to compare simple plain-old-data structs.
64   //
65   // This should *not* generate false positives, but it can generate false negatives.
66   // This will mostly work except for fields like float which can have different bit patterns
67   // that are nevertheless equal.
68   // If a test is failing because the structs aren't "equal" when they really are
69   // then it's recommended to implement operator== for it instead.
70   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 * =0,typename std::enable_if<!detail::SupportsEqualityOperator<T>::value>::type * =0)71   bool UsuallyEquals(const T& expected, const T& actual,
72                      const Ignore& ... more ATTRIBUTE_UNUSED,
73                      typename std::enable_if<std::is_pod<T>::value>::type* = 0,
74                      typename std::enable_if<!detail::SupportsEqualityOperator<T>::value>::type* = 0
75                      ) {
76     return memcmp(std::addressof(expected), std::addressof(actual), sizeof(T)) == 0;
77   }
78 
UsuallyEquals(const XGcOption & expected,const XGcOption & actual)79   bool UsuallyEquals(const XGcOption& expected, const XGcOption& actual) {
80     return memcmp(std::addressof(expected), std::addressof(actual), sizeof(expected)) == 0;
81   }
82 
UsuallyEquals(const char * expected,std::string actual)83   bool UsuallyEquals(const char* expected, std::string actual) {
84     return std::string(expected) == actual;
85   }
86 
87   template <typename TMap, typename TKey, typename T>
IsExpectedKeyValue(const T & expected,const TMap & map,const TKey & key)88   ::testing::AssertionResult IsExpectedKeyValue(const T& expected,
89                                                 const TMap& map,
90                                                 const TKey& key) {
91     auto* actual = map.Get(key);
92     if (actual != nullptr) {
93       if (!UsuallyEquals(expected, *actual)) {
94         return ::testing::AssertionFailure()
95           << "expected " << detail::ToStringAny(expected) << " but got "
96           << detail::ToStringAny(*actual);
97       }
98       return ::testing::AssertionSuccess();
99     }
100 
101     return ::testing::AssertionFailure() << "key was not in the map";
102   }
103 
104 class CmdlineParserTest : public ::testing::Test {
105  public:
106   CmdlineParserTest() = default;
107   ~CmdlineParserTest() = default;
108 
109  protected:
110   using M = RuntimeArgumentMap;
111   using RuntimeParser = ParsedOptions::RuntimeParser;
112 
SetUpTestCase()113   static void SetUpTestCase() {
114     art::InitLogging(nullptr);  // argv = null
115   }
116 
SetUp()117   virtual void SetUp() {
118     parser_ = ParsedOptions::MakeParser(false);  // do not ignore unrecognized options
119   }
120 
IsResultSuccessful(CmdlineResult result)121   static ::testing::AssertionResult IsResultSuccessful(CmdlineResult result) {
122     if (result.IsSuccess()) {
123       return ::testing::AssertionSuccess();
124     } else {
125       return ::testing::AssertionFailure()
126         << result.GetStatus() << " with: " << result.GetMessage();
127     }
128   }
129 
IsResultFailure(CmdlineResult result,CmdlineResult::Status failure_status)130   static ::testing::AssertionResult IsResultFailure(CmdlineResult result,
131                                                     CmdlineResult::Status failure_status) {
132     if (result.IsSuccess()) {
133       return ::testing::AssertionFailure() << " got success but expected failure: "
134           << failure_status;
135     } else if (result.GetStatus() == failure_status) {
136       return ::testing::AssertionSuccess();
137     }
138 
139     return ::testing::AssertionFailure() << " expected failure " << failure_status
140         << " but got " << result.GetStatus();
141   }
142 
143   std::unique_ptr<RuntimeParser> parser_;
144 };
145 
146 #define EXPECT_KEY_EXISTS(map, key) EXPECT_TRUE((map).Exists(key))
147 #define EXPECT_KEY_VALUE(map, key, expected) EXPECT_TRUE(IsExpectedKeyValue(expected, map, key))
148 
149 #define EXPECT_SINGLE_PARSE_EMPTY_SUCCESS(argv)               \
150   do {                                                        \
151     EXPECT_TRUE(IsResultSuccessful(parser_->Parse(argv)));    \
152     EXPECT_EQ(0u, parser_->GetArgumentsMap().Size());         \
153   } while (false)
154 
155 #define _EXPECT_SINGLE_PARSE_EXISTS(argv, key)                \
156   do {                                                        \
157     EXPECT_TRUE(IsResultSuccessful(parser_->Parse(argv)));    \
158     RuntimeArgumentMap args = parser_->ReleaseArgumentsMap(); \
159     EXPECT_EQ(1u, args.Size());                               \
160     EXPECT_KEY_EXISTS(args, key);                             \
161 
162 #define EXPECT_SINGLE_PARSE_EXISTS(argv, key)                 \
163     _EXPECT_SINGLE_PARSE_EXISTS(argv, key);                   \
164   } while (false)
165 
166 #define EXPECT_SINGLE_PARSE_VALUE(expected, argv, key)        \
167     _EXPECT_SINGLE_PARSE_EXISTS(argv, key);                   \
168     EXPECT_KEY_VALUE(args, key, expected);                    \
169   } while (false)                                             // NOLINT [readability/namespace] [5]
170 
171 #define EXPECT_SINGLE_PARSE_VALUE_STR(expected, argv, key)    \
172   EXPECT_SINGLE_PARSE_VALUE(std::string(expected), argv, key)
173 
174 #define EXPECT_SINGLE_PARSE_FAIL(argv, failure_status)         \
175     do {                                                       \
176       EXPECT_TRUE(IsResultFailure(parser_->Parse(argv), failure_status));\
177       RuntimeArgumentMap args = parser_->ReleaseArgumentsMap();\
178       EXPECT_EQ(0u, args.Size());                              \
179     } while (false)
180 
TEST_F(CmdlineParserTest,TestSimpleSuccesses)181 TEST_F(CmdlineParserTest, TestSimpleSuccesses) {
182   auto& parser = *parser_;
183 
184   EXPECT_LT(0u, parser.CountDefinedArguments());
185 
186   {
187     // Test case 1: No command line arguments
188     EXPECT_TRUE(IsResultSuccessful(parser.Parse("")));
189     RuntimeArgumentMap args = parser.ReleaseArgumentsMap();
190     EXPECT_EQ(0u, args.Size());
191   }
192 
193   EXPECT_SINGLE_PARSE_EXISTS("-Xzygote", M::Zygote);
194   EXPECT_SINGLE_PARSE_VALUE_STR("/hello/world", "-Xbootclasspath:/hello/world", M::BootClassPath);
195   EXPECT_SINGLE_PARSE_VALUE("/hello/world", "-Xbootclasspath:/hello/world", M::BootClassPath);
196   EXPECT_SINGLE_PARSE_VALUE(false, "-Xverify:none", M::Verify);
197   EXPECT_SINGLE_PARSE_VALUE(true, "-Xverify:remote", M::Verify);
198   EXPECT_SINGLE_PARSE_VALUE(true, "-Xverify:all", M::Verify);
199   EXPECT_SINGLE_PARSE_VALUE(Memory<1>(234), "-Xss234", M::StackSize);
200   EXPECT_SINGLE_PARSE_VALUE(MemoryKiB(1234*MB), "-Xms1234m", M::MemoryInitialSize);
201   EXPECT_SINGLE_PARSE_VALUE(true, "-XX:EnableHSpaceCompactForOOM", M::EnableHSpaceCompactForOOM);
202   EXPECT_SINGLE_PARSE_VALUE(false, "-XX:DisableHSpaceCompactForOOM", M::EnableHSpaceCompactForOOM);
203   EXPECT_SINGLE_PARSE_VALUE(0.5, "-XX:HeapTargetUtilization=0.5", M::HeapTargetUtilization);
204   EXPECT_SINGLE_PARSE_VALUE(5u, "-XX:ParallelGCThreads=5", M::ParallelGCThreads);
205   EXPECT_SINGLE_PARSE_EXISTS("-Xno-dex-file-fallback", M::NoDexFileFallback);
206 }  // TEST_F
207 
TEST_F(CmdlineParserTest,TestSimpleFailures)208 TEST_F(CmdlineParserTest, TestSimpleFailures) {
209   // Test argument is unknown to the parser
210   EXPECT_SINGLE_PARSE_FAIL("abcdefg^%@#*(@#", CmdlineResult::kUnknown);
211   // Test value map substitution fails
212   EXPECT_SINGLE_PARSE_FAIL("-Xverify:whatever", CmdlineResult::kFailure);
213   // Test value type parsing failures
214   EXPECT_SINGLE_PARSE_FAIL("-Xsswhatever", CmdlineResult::kFailure);  // invalid memory value
215   EXPECT_SINGLE_PARSE_FAIL("-Xms123", CmdlineResult::kFailure);       // memory value too small
216   EXPECT_SINGLE_PARSE_FAIL("-XX:HeapTargetUtilization=0.0", CmdlineResult::kOutOfRange);  // toosmal
217   EXPECT_SINGLE_PARSE_FAIL("-XX:HeapTargetUtilization=2.0", CmdlineResult::kOutOfRange);  // toolarg
218   EXPECT_SINGLE_PARSE_FAIL("-XX:ParallelGCThreads=-5", CmdlineResult::kOutOfRange);  // too small
219   EXPECT_SINGLE_PARSE_FAIL("-Xgc:blablabla", CmdlineResult::kUsage);  // not a valid suboption
220 }  // TEST_F
221 
TEST_F(CmdlineParserTest,TestLogVerbosity)222 TEST_F(CmdlineParserTest, TestLogVerbosity) {
223   {
224     const char* log_args = "-verbose:"
225         "class,compiler,gc,heap,jdwp,jni,monitor,profiler,signals,startup,third-party-jni,"
226         "threads,verifier";
227 
228     LogVerbosity log_verbosity = LogVerbosity();
229     log_verbosity.class_linker = true;
230     log_verbosity.compiler = true;
231     log_verbosity.gc = true;
232     log_verbosity.heap = true;
233     log_verbosity.jdwp = true;
234     log_verbosity.jni = true;
235     log_verbosity.monitor = true;
236     log_verbosity.profiler = true;
237     log_verbosity.signals = true;
238     log_verbosity.startup = true;
239     log_verbosity.third_party_jni = true;
240     log_verbosity.threads = true;
241     log_verbosity.verifier = true;
242 
243     EXPECT_SINGLE_PARSE_VALUE(log_verbosity, log_args, M::Verbose);
244   }
245 
246   {
247     const char* log_args = "-verbose:"
248         "class,compiler,gc,heap,jdwp,jni,monitor";
249 
250     LogVerbosity log_verbosity = LogVerbosity();
251     log_verbosity.class_linker = true;
252     log_verbosity.compiler = true;
253     log_verbosity.gc = true;
254     log_verbosity.heap = true;
255     log_verbosity.jdwp = true;
256     log_verbosity.jni = true;
257     log_verbosity.monitor = true;
258 
259     EXPECT_SINGLE_PARSE_VALUE(log_verbosity, log_args, M::Verbose);
260   }
261 
262   EXPECT_SINGLE_PARSE_FAIL("-verbose:blablabla", CmdlineResult::kUsage);  // invalid verbose opt
263 
264   {
265     const char* log_args = "-verbose:oat";
266     LogVerbosity log_verbosity = LogVerbosity();
267     log_verbosity.oat = true;
268     EXPECT_SINGLE_PARSE_VALUE(log_verbosity, log_args, M::Verbose);
269   }
270 }  // TEST_F
271 
272 // TODO: Enable this b/19274810
TEST_F(CmdlineParserTest,DISABLED_TestXGcOption)273 TEST_F(CmdlineParserTest, DISABLED_TestXGcOption) {
274   /*
275    * Test success
276    */
277   {
278     XGcOption option_all_true{};  // NOLINT [readability/braces] [4]
279     option_all_true.collector_type_ = gc::CollectorType::kCollectorTypeCMS;
280     option_all_true.verify_pre_gc_heap_ = true;
281     option_all_true.verify_pre_sweeping_heap_ = true;
282     option_all_true.verify_post_gc_heap_ = true;
283     option_all_true.verify_pre_gc_rosalloc_ = true;
284     option_all_true.verify_pre_sweeping_rosalloc_ = true;
285     option_all_true.verify_post_gc_rosalloc_ = true;
286 
287     const char * xgc_args_all_true = "-Xgc:concurrent,"
288         "preverify,presweepingverify,postverify,"
289         "preverify_rosalloc,presweepingverify_rosalloc,"
290         "postverify_rosalloc,precise,"
291         "verifycardtable";
292 
293     EXPECT_SINGLE_PARSE_VALUE(option_all_true, xgc_args_all_true, M::GcOption);
294 
295     XGcOption option_all_false{};  // NOLINT [readability/braces] [4]
296     option_all_false.collector_type_ = gc::CollectorType::kCollectorTypeMS;
297     option_all_false.verify_pre_gc_heap_ = false;
298     option_all_false.verify_pre_sweeping_heap_ = false;
299     option_all_false.verify_post_gc_heap_ = false;
300     option_all_false.verify_pre_gc_rosalloc_ = false;
301     option_all_false.verify_pre_sweeping_rosalloc_ = false;
302     option_all_false.verify_post_gc_rosalloc_ = false;
303 
304     const char* xgc_args_all_false = "-Xgc:nonconcurrent,"
305         "nopreverify,nopresweepingverify,nopostverify,nopreverify_rosalloc,"
306         "nopresweepingverify_rosalloc,nopostverify_rosalloc,noprecise,noverifycardtable";
307 
308     EXPECT_SINGLE_PARSE_VALUE(option_all_false, xgc_args_all_false, M::GcOption);
309 
310     XGcOption option_all_default{};  // NOLINT [readability/braces] [4]
311 
312     const char* xgc_args_blank = "-Xgc:";
313     EXPECT_SINGLE_PARSE_VALUE(option_all_default, xgc_args_blank, M::GcOption);
314   }
315 
316   /*
317    * Test failures
318    */
319   EXPECT_SINGLE_PARSE_FAIL("-Xgc:blablabla", CmdlineResult::kUsage);  // invalid Xgc opt
320 }  // TEST_F
321 
322 /*
323  * {"-Xrunjdwp:_", "-agentlib:jdwp=_"}
324  */
TEST_F(CmdlineParserTest,TestJdwpOptions)325 TEST_F(CmdlineParserTest, TestJdwpOptions) {
326   /*
327    * Test success
328    */
329   {
330     /*
331      * "Example: -Xrunjdwp:transport=dt_socket,address=8000,server=y\n"
332      */
333     JDWP::JdwpOptions opt = JDWP::JdwpOptions();
334     opt.transport = JDWP::JdwpTransportType::kJdwpTransportSocket;
335     opt.port = 8000;
336     opt.server = true;
337 
338     const char *opt_args = "-Xrunjdwp:transport=dt_socket,address=8000,server=y";
339 
340     EXPECT_SINGLE_PARSE_VALUE(opt, opt_args, M::JdwpOptions);
341   }
342 
343   {
344     /*
345      * "Example: -agentlib:jdwp=transport=dt_socket,address=localhost:6500,server=n\n");
346      */
347     JDWP::JdwpOptions opt = JDWP::JdwpOptions();
348     opt.transport = JDWP::JdwpTransportType::kJdwpTransportSocket;
349     opt.host = "localhost";
350     opt.port = 6500;
351     opt.server = false;
352 
353     const char *opt_args = "-agentlib:jdwp=transport=dt_socket,address=localhost:6500,server=n";
354 
355     EXPECT_SINGLE_PARSE_VALUE(opt, opt_args, M::JdwpOptions);
356   }
357 
358   /*
359    * Test failures
360    */
361   EXPECT_SINGLE_PARSE_FAIL("-Xrunjdwp:help", CmdlineResult::kUsage);  // usage for help only
362   EXPECT_SINGLE_PARSE_FAIL("-Xrunjdwp:blabla", CmdlineResult::kFailure);  // invalid subarg
363   EXPECT_SINGLE_PARSE_FAIL("-agentlib:jdwp=help", CmdlineResult::kUsage);  // usage for help only
364   EXPECT_SINGLE_PARSE_FAIL("-agentlib:jdwp=blabla", CmdlineResult::kFailure);  // invalid subarg
365 }  // TEST_F
366 
367 /*
368  * -D_ -D_ -D_ ...
369  */
TEST_F(CmdlineParserTest,TestPropertiesList)370 TEST_F(CmdlineParserTest, TestPropertiesList) {
371   /*
372    * Test successes
373    */
374   {
375     std::vector<std::string> opt = {"hello"};
376 
377     EXPECT_SINGLE_PARSE_VALUE(opt, "-Dhello", M::PropertiesList);
378   }
379 
380   {
381     std::vector<std::string> opt = {"hello", "world"};
382 
383     EXPECT_SINGLE_PARSE_VALUE(opt, "-Dhello -Dworld", M::PropertiesList);
384   }
385 
386   {
387     std::vector<std::string> opt = {"one", "two", "three"};
388 
389     EXPECT_SINGLE_PARSE_VALUE(opt, "-Done -Dtwo -Dthree", M::PropertiesList);
390   }
391 }  // TEST_F
392 
393 /*
394 * -Xcompiler-option foo -Xcompiler-option bar ...
395 */
TEST_F(CmdlineParserTest,TestCompilerOption)396 TEST_F(CmdlineParserTest, TestCompilerOption) {
397  /*
398   * Test successes
399   */
400   {
401     std::vector<std::string> opt = {"hello"};
402     EXPECT_SINGLE_PARSE_VALUE(opt, "-Xcompiler-option hello", M::CompilerOptions);
403   }
404 
405   {
406     std::vector<std::string> opt = {"hello", "world"};
407     EXPECT_SINGLE_PARSE_VALUE(opt,
408                               "-Xcompiler-option hello -Xcompiler-option world",
409                               M::CompilerOptions);
410   }
411 
412   {
413     std::vector<std::string> opt = {"one", "two", "three"};
414     EXPECT_SINGLE_PARSE_VALUE(opt,
415                               "-Xcompiler-option one -Xcompiler-option two -Xcompiler-option three",
416                               M::CompilerOptions);
417   }
418 }  // TEST_F
419 
420 /*
421 * -Xjit, -Xnojit, -Xjitcodecachesize, Xjitcompilethreshold
422 */
TEST_F(CmdlineParserTest,TestJitOptions)423 TEST_F(CmdlineParserTest, TestJitOptions) {
424  /*
425   * Test successes
426   */
427   {
428     EXPECT_SINGLE_PARSE_VALUE(true, "-Xusejit:true", M::UseJIT);
429     EXPECT_SINGLE_PARSE_VALUE(false, "-Xusejit:false", M::UseJIT);
430   }
431   {
432     EXPECT_SINGLE_PARSE_VALUE(MemoryKiB(16 * KB), "-Xjitcodecachesize:16K", M::JITCodeCacheCapacity);
433     EXPECT_SINGLE_PARSE_VALUE(MemoryKiB(16 * MB), "-Xjitcodecachesize:16M", M::JITCodeCacheCapacity);
434   }
435   {
436     EXPECT_SINGLE_PARSE_VALUE(12345u, "-Xjitthreshold:12345", M::JITCompileThreshold);
437   }
438 }  // TEST_F
439 
440 /*
441 * -X-profile-*
442 */
TEST_F(CmdlineParserTest,TestProfilerOptions)443 TEST_F(CmdlineParserTest, TestProfilerOptions) {
444  /*
445   * Test successes
446   */
447 
448   {
449     TestProfilerOptions opt;
450     opt.enabled_ = true;
451 
452     EXPECT_SINGLE_PARSE_VALUE(opt,
453                               "-Xenable-profiler",
454                               M::ProfilerOpts);
455   }
456 
457   {
458     TestProfilerOptions opt;
459     // also need to test 'enabled'
460     opt.output_file_name_ = "hello_world.txt";
461 
462     EXPECT_SINGLE_PARSE_VALUE(opt,
463                               "-Xprofile-filename:hello_world.txt ",
464                               M::ProfilerOpts);
465   }
466 
467   {
468     TestProfilerOptions opt = TestProfilerOptions();
469     // also need to test 'enabled'
470     opt.output_file_name_ = "output.txt";
471     opt.period_s_ = 123u;
472     opt.duration_s_ = 456u;
473     opt.interval_us_ = 789u;
474     opt.backoff_coefficient_ = 2.0;
475     opt.start_immediately_ = true;
476     opt.top_k_threshold_ = 50.0;
477     opt.top_k_change_threshold_ = 60.0;
478     opt.profile_type_ = kProfilerMethod;
479     opt.max_stack_depth_ = 1337u;
480 
481     EXPECT_SINGLE_PARSE_VALUE(opt,
482                               "-Xprofile-filename:output.txt "
483                               "-Xprofile-period:123 "
484                               "-Xprofile-duration:456 "
485                               "-Xprofile-interval:789 "
486                               "-Xprofile-backoff:2.0 "
487                               "-Xprofile-start-immediately "
488                               "-Xprofile-top-k-threshold:50.0 "
489                               "-Xprofile-top-k-change-threshold:60.0 "
490                               "-Xprofile-type:method "
491                               "-Xprofile-max-stack-depth:1337",
492                               M::ProfilerOpts);
493   }
494 
495   {
496     TestProfilerOptions opt = TestProfilerOptions();
497     opt.profile_type_ = kProfilerBoundedStack;
498 
499     EXPECT_SINGLE_PARSE_VALUE(opt,
500                               "-Xprofile-type:stack",
501                               M::ProfilerOpts);
502   }
503 }  // TEST_F
504 
TEST_F(CmdlineParserTest,TestIgnoreUnrecognized)505 TEST_F(CmdlineParserTest, TestIgnoreUnrecognized) {
506   RuntimeParser::Builder parserBuilder;
507 
508   parserBuilder
509       .Define("-help")
510           .IntoKey(M::Help)
511       .IgnoreUnrecognized(true);
512 
513   parser_.reset(new RuntimeParser(parserBuilder.Build()));
514 
515   EXPECT_SINGLE_PARSE_EMPTY_SUCCESS("-non-existent-option");
516   EXPECT_SINGLE_PARSE_EMPTY_SUCCESS("-non-existent-option1 --non-existent-option-2");
517 }  //  TEST_F
518 
TEST_F(CmdlineParserTest,TestIgnoredArguments)519 TEST_F(CmdlineParserTest, TestIgnoredArguments) {
520   std::initializer_list<const char*> ignored_args = {
521       "-ea", "-da", "-enableassertions", "-disableassertions", "--runtime-arg", "-esa",
522       "-dsa", "-enablesystemassertions", "-disablesystemassertions", "-Xrs", "-Xint:abdef",
523       "-Xdexopt:foobar", "-Xnoquithandler", "-Xjnigreflimit:ixnay", "-Xgenregmap", "-Xnogenregmap",
524       "-Xverifyopt:never", "-Xcheckdexsum", "-Xincludeselectedop", "-Xjitop:noop",
525       "-Xincludeselectedmethod", "-Xjitblocking", "-Xjitmethod:_", "-Xjitclass:nosuchluck",
526       "-Xjitoffset:none", "-Xjitconfig:yes", "-Xjitcheckcg", "-Xjitverbose", "-Xjitprofile",
527       "-Xjitdisableopt", "-Xjitsuspendpoll", "-XX:mainThreadStackSize=1337"
528   };
529 
530   // Check they are ignored when parsed one at a time
531   for (auto&& arg : ignored_args) {
532     SCOPED_TRACE(arg);
533     EXPECT_SINGLE_PARSE_EMPTY_SUCCESS(arg);
534   }
535 
536   // Check they are ignored when we pass it all together at once
537   std::vector<const char*> argv = ignored_args;
538   EXPECT_SINGLE_PARSE_EMPTY_SUCCESS(argv);
539 }  //  TEST_F
540 
TEST_F(CmdlineParserTest,MultipleArguments)541 TEST_F(CmdlineParserTest, MultipleArguments) {
542   EXPECT_TRUE(IsResultSuccessful(parser_->Parse(
543       "-help -XX:ForegroundHeapGrowthMultiplier=0.5 "
544       "-Xnodex2oat -Xmethod-trace -XX:LargeObjectSpace=map")));
545 
546   auto&& map = parser_->ReleaseArgumentsMap();
547   EXPECT_EQ(5u, map.Size());
548   EXPECT_KEY_VALUE(map, M::Help, Unit{});  // NOLINT [whitespace/braces] [5]
549   EXPECT_KEY_VALUE(map, M::ForegroundHeapGrowthMultiplier, 0.5);
550   EXPECT_KEY_VALUE(map, M::Dex2Oat, false);
551   EXPECT_KEY_VALUE(map, M::MethodTrace, Unit{});  // NOLINT [whitespace/braces] [5]
552   EXPECT_KEY_VALUE(map, M::LargeObjectSpace, gc::space::LargeObjectSpaceType::kMap);
553 }  //  TEST_F
554 }  // namespace art
555