1 //===-- Args.cpp ----------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "lldb/Utility/Args.h"
10 #include "lldb/Utility/ConstString.h"
11 #include "lldb/Utility/FileSpec.h"
12 #include "lldb/Utility/Stream.h"
13 #include "lldb/Utility/StringList.h"
14 #include "llvm/ADT/StringSwitch.h"
15 
16 using namespace lldb;
17 using namespace lldb_private;
18 
19 // A helper function for argument parsing.
20 // Parses the initial part of the first argument using normal double quote
21 // rules: backslash escapes the double quote and itself. The parsed string is
22 // appended to the second argument. The function returns the unparsed portion
23 // of the string, starting at the closing quote.
24 static llvm::StringRef ParseDoubleQuotes(llvm::StringRef quoted,
25                                          std::string &result) {
26   // Inside double quotes, '\' and '"' are special.
27   static const char *k_escapable_characters = "\"\\";
28   while (true) {
29     // Skip over over regular characters and append them.
30     size_t regular = quoted.find_first_of(k_escapable_characters);
31     result += quoted.substr(0, regular);
32     quoted = quoted.substr(regular);
33 
34     // If we have reached the end of string or the closing quote, we're done.
35     if (quoted.empty() || quoted.front() == '"')
36       break;
37 
38     // We have found a backslash.
39     quoted = quoted.drop_front();
40 
41     if (quoted.empty()) {
42       // A lone backslash at the end of string, let's just append it.
43       result += '\\';
44       break;
45     }
46 
47     // If the character after the backslash is not an allowed escapable
48     // character, we leave the character sequence untouched.
49     if (strchr(k_escapable_characters, quoted.front()) == nullptr)
50       result += '\\';
51 
52     result += quoted.front();
53     quoted = quoted.drop_front();
54   }
55 
56   return quoted;
57 }
58 
59 static size_t ArgvToArgc(const char **argv) {
60   if (!argv)
61     return 0;
62   size_t count = 0;
63   while (*argv++)
64     ++count;
65   return count;
66 }
67 
68 // Trims all whitespace that can separate command line arguments from the left
69 // side of the string.
70 static llvm::StringRef ltrimForArgs(llvm::StringRef str) {
71   static const char *k_space_separators = " \t";
72   return str.ltrim(k_space_separators);
73 }
74 
75 // A helper function for SetCommandString. Parses a single argument from the
76 // command string, processing quotes and backslashes in a shell-like manner.
77 // The function returns a tuple consisting of the parsed argument, the quote
78 // char used, and the unparsed portion of the string starting at the first
79 // unqouted, unescaped whitespace character.
80 static std::tuple<std::string, char, llvm::StringRef>
81 ParseSingleArgument(llvm::StringRef command) {
82   // Argument can be split into multiple discontiguous pieces, for example:
83   //  "Hello ""World"
84   // this would result in a single argument "Hello World" (without the quotes)
85   // since the quotes would be removed and there is not space between the
86   // strings.
87   std::string arg;
88 
89   // Since we can have multiple quotes that form a single command in a command
90   // like: "Hello "world'!' (which will make a single argument "Hello world!")
91   // we remember the first quote character we encounter and use that for the
92   // quote character.
93   char first_quote_char = '\0';
94 
95   bool arg_complete = false;
96   do {
97     // Skip over over regular characters and append them.
98     size_t regular = command.find_first_of(" \t\r\"'`\\");
99     arg += command.substr(0, regular);
100     command = command.substr(regular);
101 
102     if (command.empty())
103       break;
104 
105     char special = command.front();
106     command = command.drop_front();
107     switch (special) {
108     case '\\':
109       if (command.empty()) {
110         arg += '\\';
111         break;
112       }
113 
114       // If the character after the backslash is not an allowed escapable
115       // character, we leave the character sequence untouched.
116       if (strchr(" \t\\'\"`", command.front()) == nullptr)
117         arg += '\\';
118 
119       arg += command.front();
120       command = command.drop_front();
121 
122       break;
123 
124     case ' ':
125     case '\t':
126     case '\r':
127       // We are not inside any quotes, we just found a space after an argument.
128       // We are done.
129       arg_complete = true;
130       break;
131 
132     case '"':
133     case '\'':
134     case '`':
135       // We found the start of a quote scope.
136       if (first_quote_char == '\0')
137         first_quote_char = special;
138 
139       if (special == '"')
140         command = ParseDoubleQuotes(command, arg);
141       else {
142         // For single quotes, we simply skip ahead to the matching quote
143         // character (or the end of the string).
144         size_t quoted = command.find(special);
145         arg += command.substr(0, quoted);
146         command = command.substr(quoted);
147       }
148 
149       // If we found a closing quote, skip it.
150       if (!command.empty())
151         command = command.drop_front();
152 
153       break;
154     }
155   } while (!arg_complete);
156 
157   return std::make_tuple(arg, first_quote_char, command);
158 }
159 
160 Args::ArgEntry::ArgEntry(llvm::StringRef str, char quote) : quote(quote) {
161   size_t size = str.size();
162   ptr.reset(new char[size + 1]);
163 
164   ::memcpy(data(), str.data() ? str.data() : "", size);
165   ptr[size] = 0;
166 }
167 
168 // Args constructor
169 Args::Args(llvm::StringRef command) { SetCommandString(command); }
170 
171 Args::Args(const Args &rhs) { *this = rhs; }
172 
173 Args::Args(const StringList &list) : Args() {
174   for (const std::string &arg : list)
175     AppendArgument(arg);
176 }
177 
178 Args::Args(llvm::ArrayRef<llvm::StringRef> args) : Args() {
179   for (llvm::StringRef arg : args)
180     AppendArgument(arg);
181 }
182 
183 Args &Args::operator=(const Args &rhs) {
184   Clear();
185 
186   m_argv.clear();
187   m_entries.clear();
188   for (auto &entry : rhs.m_entries) {
189     m_entries.emplace_back(entry.ref(), entry.quote);
190     m_argv.push_back(m_entries.back().data());
191   }
192   m_argv.push_back(nullptr);
193   return *this;
194 }
195 
196 // Destructor
197 Args::~Args() {}
198 
199 void Args::Dump(Stream &s, const char *label_name) const {
200   if (!label_name)
201     return;
202 
203   int i = 0;
204   for (auto &entry : m_entries) {
205     s.Indent();
206     s.Format("{0}[{1}]=\"{2}\"\n", label_name, i++, entry.ref());
207   }
208   s.Format("{0}[{1}]=NULL\n", label_name, i);
209   s.EOL();
210 }
211 
212 bool Args::GetCommandString(std::string &command) const {
213   command.clear();
214 
215   for (size_t i = 0; i < m_entries.size(); ++i) {
216     if (i > 0)
217       command += ' ';
218     command += m_entries[i].ref();
219   }
220 
221   return !m_entries.empty();
222 }
223 
224 bool Args::GetQuotedCommandString(std::string &command) const {
225   command.clear();
226 
227   for (size_t i = 0; i < m_entries.size(); ++i) {
228     if (i > 0)
229       command += ' ';
230 
231     if (m_entries[i].quote) {
232       command += m_entries[i].quote;
233       command += m_entries[i].ref();
234       command += m_entries[i].quote;
235     } else {
236       command += m_entries[i].ref();
237     }
238   }
239 
240   return !m_entries.empty();
241 }
242 
243 void Args::SetCommandString(llvm::StringRef command) {
244   Clear();
245   m_argv.clear();
246 
247   command = ltrimForArgs(command);
248   std::string arg;
249   char quote;
250   while (!command.empty()) {
251     std::tie(arg, quote, command) = ParseSingleArgument(command);
252     m_entries.emplace_back(arg, quote);
253     m_argv.push_back(m_entries.back().data());
254     command = ltrimForArgs(command);
255   }
256   m_argv.push_back(nullptr);
257 }
258 
259 size_t Args::GetArgumentCount() const { return m_entries.size(); }
260 
261 const char *Args::GetArgumentAtIndex(size_t idx) const {
262   if (idx < m_argv.size())
263     return m_argv[idx];
264   return nullptr;
265 }
266 
267 char **Args::GetArgumentVector() {
268   assert(!m_argv.empty());
269   // TODO: functions like execve and posix_spawnp exhibit undefined behavior
270   // when argv or envp is null.  So the code below is actually wrong.  However,
271   // other code in LLDB depends on it being null.  The code has been acting
272   // this way for some time, so it makes sense to leave it this way until
273   // someone has the time to come along and fix it.
274   return (m_argv.size() > 1) ? m_argv.data() : nullptr;
275 }
276 
277 const char **Args::GetConstArgumentVector() const {
278   assert(!m_argv.empty());
279   return (m_argv.size() > 1) ? const_cast<const char **>(m_argv.data())
280                              : nullptr;
281 }
282 
283 void Args::Shift() {
284   // Don't pop the last NULL terminator from the argv array
285   if (m_entries.empty())
286     return;
287   m_argv.erase(m_argv.begin());
288   m_entries.erase(m_entries.begin());
289 }
290 
291 void Args::Unshift(llvm::StringRef arg_str, char quote_char) {
292   InsertArgumentAtIndex(0, arg_str, quote_char);
293 }
294 
295 void Args::AppendArguments(const Args &rhs) {
296   assert(m_argv.size() == m_entries.size() + 1);
297   assert(m_argv.back() == nullptr);
298   m_argv.pop_back();
299   for (auto &entry : rhs.m_entries) {
300     m_entries.emplace_back(entry.ref(), entry.quote);
301     m_argv.push_back(m_entries.back().data());
302   }
303   m_argv.push_back(nullptr);
304 }
305 
306 void Args::AppendArguments(const char **argv) {
307   size_t argc = ArgvToArgc(argv);
308 
309   assert(m_argv.size() == m_entries.size() + 1);
310   assert(m_argv.back() == nullptr);
311   m_argv.pop_back();
312   for (auto arg : llvm::makeArrayRef(argv, argc)) {
313     m_entries.emplace_back(arg, '\0');
314     m_argv.push_back(m_entries.back().data());
315   }
316 
317   m_argv.push_back(nullptr);
318 }
319 
320 void Args::AppendArgument(llvm::StringRef arg_str, char quote_char) {
321   InsertArgumentAtIndex(GetArgumentCount(), arg_str, quote_char);
322 }
323 
324 void Args::InsertArgumentAtIndex(size_t idx, llvm::StringRef arg_str,
325                                  char quote_char) {
326   assert(m_argv.size() == m_entries.size() + 1);
327   assert(m_argv.back() == nullptr);
328 
329   if (idx > m_entries.size())
330     return;
331   m_entries.emplace(m_entries.begin() + idx, arg_str, quote_char);
332   m_argv.insert(m_argv.begin() + idx, m_entries[idx].data());
333 }
334 
335 void Args::ReplaceArgumentAtIndex(size_t idx, llvm::StringRef arg_str,
336                                   char quote_char) {
337   assert(m_argv.size() == m_entries.size() + 1);
338   assert(m_argv.back() == nullptr);
339 
340   if (idx >= m_entries.size())
341     return;
342 
343   m_entries[idx] = ArgEntry(arg_str, quote_char);
344   m_argv[idx] = m_entries[idx].data();
345 }
346 
347 void Args::DeleteArgumentAtIndex(size_t idx) {
348   if (idx >= m_entries.size())
349     return;
350 
351   m_argv.erase(m_argv.begin() + idx);
352   m_entries.erase(m_entries.begin() + idx);
353 }
354 
355 void Args::SetArguments(size_t argc, const char **argv) {
356   Clear();
357 
358   auto args = llvm::makeArrayRef(argv, argc);
359   m_entries.resize(argc);
360   m_argv.resize(argc + 1);
361   for (size_t i = 0; i < args.size(); ++i) {
362     char quote =
363         ((args[i][0] == '\'') || (args[i][0] == '"') || (args[i][0] == '`'))
364             ? args[i][0]
365             : '\0';
366 
367     m_entries[i] = ArgEntry(args[i], quote);
368     m_argv[i] = m_entries[i].data();
369   }
370 }
371 
372 void Args::SetArguments(const char **argv) {
373   SetArguments(ArgvToArgc(argv), argv);
374 }
375 
376 void Args::Clear() {
377   m_entries.clear();
378   m_argv.clear();
379   m_argv.push_back(nullptr);
380 }
381 
382 std::string Args::GetShellSafeArgument(const FileSpec &shell,
383                                        llvm::StringRef unsafe_arg) {
384   struct ShellDescriptor {
385     ConstString m_basename;
386     llvm::StringRef m_escapables;
387   };
388 
389   static ShellDescriptor g_Shells[] = {{ConstString("bash"), " '\"<>()&"},
390                                        {ConstString("tcsh"), " '\"<>()&$"},
391                                        {ConstString("sh"), " '\"<>()&"}};
392 
393   // safe minimal set
394   llvm::StringRef escapables = " '\"";
395 
396   if (auto basename = shell.GetFilename()) {
397     for (const auto &Shell : g_Shells) {
398       if (Shell.m_basename == basename) {
399         escapables = Shell.m_escapables;
400         break;
401       }
402     }
403   }
404 
405   std::string safe_arg;
406   safe_arg.reserve(unsafe_arg.size());
407   // Add a \ before every character that needs to be escaped.
408   for (char c : unsafe_arg) {
409     if (escapables.contains(c))
410       safe_arg.push_back('\\');
411     safe_arg.push_back(c);
412   }
413   return safe_arg;
414 }
415 
416 lldb::Encoding Args::StringToEncoding(llvm::StringRef s,
417                                       lldb::Encoding fail_value) {
418   return llvm::StringSwitch<lldb::Encoding>(s)
419       .Case("uint", eEncodingUint)
420       .Case("sint", eEncodingSint)
421       .Case("ieee754", eEncodingIEEE754)
422       .Case("vector", eEncodingVector)
423       .Default(fail_value);
424 }
425 
426 uint32_t Args::StringToGenericRegister(llvm::StringRef s) {
427   if (s.empty())
428     return LLDB_INVALID_REGNUM;
429   uint32_t result = llvm::StringSwitch<uint32_t>(s)
430                         .Case("pc", LLDB_REGNUM_GENERIC_PC)
431                         .Case("sp", LLDB_REGNUM_GENERIC_SP)
432                         .Case("fp", LLDB_REGNUM_GENERIC_FP)
433                         .Cases("ra", "lr", LLDB_REGNUM_GENERIC_RA)
434                         .Case("flags", LLDB_REGNUM_GENERIC_FLAGS)
435                         .Case("arg1", LLDB_REGNUM_GENERIC_ARG1)
436                         .Case("arg2", LLDB_REGNUM_GENERIC_ARG2)
437                         .Case("arg3", LLDB_REGNUM_GENERIC_ARG3)
438                         .Case("arg4", LLDB_REGNUM_GENERIC_ARG4)
439                         .Case("arg5", LLDB_REGNUM_GENERIC_ARG5)
440                         .Case("arg6", LLDB_REGNUM_GENERIC_ARG6)
441                         .Case("arg7", LLDB_REGNUM_GENERIC_ARG7)
442                         .Case("arg8", LLDB_REGNUM_GENERIC_ARG8)
443                         .Default(LLDB_INVALID_REGNUM);
444   return result;
445 }
446 
447 void Args::EncodeEscapeSequences(const char *src, std::string &dst) {
448   dst.clear();
449   if (src) {
450     for (const char *p = src; *p != '\0'; ++p) {
451       size_t non_special_chars = ::strcspn(p, "\\");
452       if (non_special_chars > 0) {
453         dst.append(p, non_special_chars);
454         p += non_special_chars;
455         if (*p == '\0')
456           break;
457       }
458 
459       if (*p == '\\') {
460         ++p; // skip the slash
461         switch (*p) {
462         case 'a':
463           dst.append(1, '\a');
464           break;
465         case 'b':
466           dst.append(1, '\b');
467           break;
468         case 'f':
469           dst.append(1, '\f');
470           break;
471         case 'n':
472           dst.append(1, '\n');
473           break;
474         case 'r':
475           dst.append(1, '\r');
476           break;
477         case 't':
478           dst.append(1, '\t');
479           break;
480         case 'v':
481           dst.append(1, '\v');
482           break;
483         case '\\':
484           dst.append(1, '\\');
485           break;
486         case '\'':
487           dst.append(1, '\'');
488           break;
489         case '"':
490           dst.append(1, '"');
491           break;
492         case '0':
493           // 1 to 3 octal chars
494           {
495             // Make a string that can hold onto the initial zero char, up to 3
496             // octal digits, and a terminating NULL.
497             char oct_str[5] = {'\0', '\0', '\0', '\0', '\0'};
498 
499             int i;
500             for (i = 0; (p[i] >= '0' && p[i] <= '7') && i < 4; ++i)
501               oct_str[i] = p[i];
502 
503             // We don't want to consume the last octal character since the main
504             // for loop will do this for us, so we advance p by one less than i
505             // (even if i is zero)
506             p += i - 1;
507             unsigned long octal_value = ::strtoul(oct_str, nullptr, 8);
508             if (octal_value <= UINT8_MAX) {
509               dst.append(1, static_cast<char>(octal_value));
510             }
511           }
512           break;
513 
514         case 'x':
515           // hex number in the format
516           if (isxdigit(p[1])) {
517             ++p; // Skip the 'x'
518 
519             // Make a string that can hold onto two hex chars plus a
520             // NULL terminator
521             char hex_str[3] = {*p, '\0', '\0'};
522             if (isxdigit(p[1])) {
523               ++p; // Skip the first of the two hex chars
524               hex_str[1] = *p;
525             }
526 
527             unsigned long hex_value = strtoul(hex_str, nullptr, 16);
528             if (hex_value <= UINT8_MAX)
529               dst.append(1, static_cast<char>(hex_value));
530           } else {
531             dst.append(1, 'x');
532           }
533           break;
534 
535         default:
536           // Just desensitize any other character by just printing what came
537           // after the '\'
538           dst.append(1, *p);
539           break;
540         }
541       }
542     }
543   }
544 }
545 
546 void Args::ExpandEscapedCharacters(const char *src, std::string &dst) {
547   dst.clear();
548   if (src) {
549     for (const char *p = src; *p != '\0'; ++p) {
550       if (llvm::isPrint(*p))
551         dst.append(1, *p);
552       else {
553         switch (*p) {
554         case '\a':
555           dst.append("\\a");
556           break;
557         case '\b':
558           dst.append("\\b");
559           break;
560         case '\f':
561           dst.append("\\f");
562           break;
563         case '\n':
564           dst.append("\\n");
565           break;
566         case '\r':
567           dst.append("\\r");
568           break;
569         case '\t':
570           dst.append("\\t");
571           break;
572         case '\v':
573           dst.append("\\v");
574           break;
575         case '\'':
576           dst.append("\\'");
577           break;
578         case '"':
579           dst.append("\\\"");
580           break;
581         case '\\':
582           dst.append("\\\\");
583           break;
584         default: {
585           // Just encode as octal
586           dst.append("\\0");
587           char octal_str[32];
588           snprintf(octal_str, sizeof(octal_str), "%o", *p);
589           dst.append(octal_str);
590         } break;
591         }
592       }
593     }
594   }
595 }
596 
597 std::string Args::EscapeLLDBCommandArgument(const std::string &arg,
598                                             char quote_char) {
599   const char *chars_to_escape = nullptr;
600   switch (quote_char) {
601   case '\0':
602     chars_to_escape = " \t\\'\"`";
603     break;
604   case '"':
605     chars_to_escape = "$\"`\\";
606     break;
607   case '`':
608   case '\'':
609     return arg;
610   default:
611     assert(false && "Unhandled quote character");
612     return arg;
613   }
614 
615   std::string res;
616   res.reserve(arg.size());
617   for (char c : arg) {
618     if (::strchr(chars_to_escape, c))
619       res.push_back('\\');
620     res.push_back(c);
621   }
622   return res;
623 }
624 
625 OptionsWithRaw::OptionsWithRaw(llvm::StringRef arg_string) {
626   SetFromString(arg_string);
627 }
628 
629 void OptionsWithRaw::SetFromString(llvm::StringRef arg_string) {
630   const llvm::StringRef original_args = arg_string;
631 
632   arg_string = ltrimForArgs(arg_string);
633   std::string arg;
634   char quote;
635 
636   // If the string doesn't start with a dash, we just have no options and just
637   // a raw part.
638   if (!arg_string.startswith("-")) {
639     m_suffix = std::string(original_args);
640     return;
641   }
642 
643   bool found_suffix = false;
644   while (!arg_string.empty()) {
645     // The length of the prefix before parsing.
646     std::size_t prev_prefix_length = original_args.size() - arg_string.size();
647 
648     // Parse the next argument from the remaining string.
649     std::tie(arg, quote, arg_string) = ParseSingleArgument(arg_string);
650 
651     // If we get an unquoted '--' argument, then we reached the suffix part
652     // of the command.
653     Args::ArgEntry entry(arg, quote);
654     if (!entry.IsQuoted() && arg == "--") {
655       // The remaining line is the raw suffix, and the line we parsed so far
656       // needs to be interpreted as arguments.
657       m_has_args = true;
658       m_suffix = std::string(arg_string);
659       found_suffix = true;
660 
661       // The length of the prefix after parsing.
662       std::size_t prefix_length = original_args.size() - arg_string.size();
663 
664       // Take the string we know contains all the arguments and actually parse
665       // it as proper arguments.
666       llvm::StringRef prefix = original_args.take_front(prev_prefix_length);
667       m_args = Args(prefix);
668       m_arg_string = prefix;
669 
670       // We also record the part of the string that contains the arguments plus
671       // the delimiter.
672       m_arg_string_with_delimiter = original_args.take_front(prefix_length);
673 
674       // As the rest of the string became the raw suffix, we are done here.
675       break;
676     }
677 
678     arg_string = ltrimForArgs(arg_string);
679   }
680 
681   // If we didn't find a suffix delimiter, the whole string is the raw suffix.
682   if (!found_suffix)
683     m_suffix = std::string(original_args);
684 }
685 
686 void llvm::yaml::MappingTraits<Args::ArgEntry>::mapping(IO &io,
687                                                         Args::ArgEntry &v) {
688   MappingNormalization<NormalizedArgEntry, Args::ArgEntry> keys(io, v);
689   io.mapRequired("value", keys->value);
690   io.mapRequired("quote", keys->quote);
691 }
692 
693 void llvm::yaml::MappingTraits<Args>::mapping(IO &io, Args &v) {
694   io.mapRequired("entries", v.m_entries);
695 
696   // Recompute m_argv vector.
697   v.m_argv.clear();
698   for (auto &entry : v.m_entries)
699     v.m_argv.push_back(entry.data());
700   v.m_argv.push_back(nullptr);
701 }
702