1 //===-- ClangUserExpression.cpp ---------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include <stdio.h>
11 #if HAVE_SYS_TYPES_H
12 #include <sys/types.h>
13 #endif
14 
15 #include <cstdlib>
16 #include <map>
17 #include <string>
18 
19 #include "ClangUserExpression.h"
20 
21 #include "ASTResultSynthesizer.h"
22 #include "ClangDiagnostic.h"
23 #include "ClangExpressionDeclMap.h"
24 #include "ClangExpressionParser.h"
25 #include "ClangModulesDeclVendor.h"
26 #include "ClangPersistentVariables.h"
27 
28 #include "lldb/Core/Debugger.h"
29 #include "lldb/Core/Module.h"
30 #include "lldb/Core/StreamFile.h"
31 #include "lldb/Core/ValueObjectConstResult.h"
32 #include "lldb/Expression/ExpressionSourceCode.h"
33 #include "lldb/Expression/IRExecutionUnit.h"
34 #include "lldb/Expression/IRInterpreter.h"
35 #include "lldb/Expression/Materializer.h"
36 #include "lldb/Host/HostInfo.h"
37 #include "lldb/Symbol/Block.h"
38 #include "lldb/Symbol/ClangASTContext.h"
39 #include "lldb/Symbol/ClangExternalASTSourceCommon.h"
40 #include "lldb/Symbol/Function.h"
41 #include "lldb/Symbol/ObjectFile.h"
42 #include "lldb/Symbol/SymbolVendor.h"
43 #include "lldb/Symbol/Type.h"
44 #include "lldb/Symbol/VariableList.h"
45 #include "lldb/Target/ExecutionContext.h"
46 #include "lldb/Target/Process.h"
47 #include "lldb/Target/StackFrame.h"
48 #include "lldb/Target/Target.h"
49 #include "lldb/Target/ThreadPlan.h"
50 #include "lldb/Target/ThreadPlanCallUserExpression.h"
51 #include "lldb/Utility/ConstString.h"
52 #include "lldb/Utility/Log.h"
53 #include "lldb/Utility/StreamString.h"
54 
55 #include "clang/AST/DeclCXX.h"
56 #include "clang/AST/DeclObjC.h"
57 
58 using namespace lldb_private;
59 
60 ClangUserExpression::ClangUserExpression(
61     ExecutionContextScope &exe_scope, llvm::StringRef expr,
62     llvm::StringRef prefix, lldb::LanguageType language,
63     ResultType desired_type, const EvaluateExpressionOptions &options)
64     : LLVMUserExpression(exe_scope, expr, prefix, language, desired_type,
65                          options),
66       m_type_system_helper(*m_target_wp.lock().get(),
67                            options.GetExecutionPolicy() ==
68                                eExecutionPolicyTopLevel),
69       m_result_delegate(exe_scope.CalculateTarget()) {
70   switch (m_language) {
71   case lldb::eLanguageTypeC_plus_plus:
72     m_allow_cxx = true;
73     break;
74   case lldb::eLanguageTypeObjC:
75     m_allow_objc = true;
76     break;
77   case lldb::eLanguageTypeObjC_plus_plus:
78   default:
79     m_allow_cxx = true;
80     m_allow_objc = true;
81     break;
82   }
83 }
84 
85 ClangUserExpression::~ClangUserExpression() {}
86 
87 void ClangUserExpression::ScanContext(ExecutionContext &exe_ctx, Status &err) {
88   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
89 
90   if (log)
91     log->Printf("ClangUserExpression::ScanContext()");
92 
93   m_target = exe_ctx.GetTargetPtr();
94 
95   if (!(m_allow_cxx || m_allow_objc)) {
96     if (log)
97       log->Printf("  [CUE::SC] Settings inhibit C++ and Objective-C");
98     return;
99   }
100 
101   StackFrame *frame = exe_ctx.GetFramePtr();
102   if (frame == NULL) {
103     if (log)
104       log->Printf("  [CUE::SC] Null stack frame");
105     return;
106   }
107 
108   SymbolContext sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction |
109                                                   lldb::eSymbolContextBlock);
110 
111   if (!sym_ctx.function) {
112     if (log)
113       log->Printf("  [CUE::SC] Null function");
114     return;
115   }
116 
117   // Find the block that defines the function represented by "sym_ctx"
118   Block *function_block = sym_ctx.GetFunctionBlock();
119 
120   if (!function_block) {
121     if (log)
122       log->Printf("  [CUE::SC] Null function block");
123     return;
124   }
125 
126   CompilerDeclContext decl_context = function_block->GetDeclContext();
127 
128   if (!decl_context) {
129     if (log)
130       log->Printf("  [CUE::SC] Null decl context");
131     return;
132   }
133 
134   if (clang::CXXMethodDecl *method_decl =
135           ClangASTContext::DeclContextGetAsCXXMethodDecl(decl_context)) {
136     if (m_allow_cxx && method_decl->isInstance()) {
137       if (m_enforce_valid_object) {
138         lldb::VariableListSP variable_list_sp(
139             function_block->GetBlockVariableList(true));
140 
141         const char *thisErrorString = "Stopped in a C++ method, but 'this' "
142                                       "isn't available; pretending we are in a "
143                                       "generic context";
144 
145         if (!variable_list_sp) {
146           err.SetErrorString(thisErrorString);
147           return;
148         }
149 
150         lldb::VariableSP this_var_sp(
151             variable_list_sp->FindVariable(ConstString("this")));
152 
153         if (!this_var_sp || !this_var_sp->IsInScope(frame) ||
154             !this_var_sp->LocationIsValidForFrame(frame)) {
155           err.SetErrorString(thisErrorString);
156           return;
157         }
158       }
159 
160       m_in_cplusplus_method = true;
161       m_needs_object_ptr = true;
162     }
163   } else if (clang::ObjCMethodDecl *method_decl =
164                  ClangASTContext::DeclContextGetAsObjCMethodDecl(
165                      decl_context)) {
166     if (m_allow_objc) {
167       if (m_enforce_valid_object) {
168         lldb::VariableListSP variable_list_sp(
169             function_block->GetBlockVariableList(true));
170 
171         const char *selfErrorString = "Stopped in an Objective-C method, but "
172                                       "'self' isn't available; pretending we "
173                                       "are in a generic context";
174 
175         if (!variable_list_sp) {
176           err.SetErrorString(selfErrorString);
177           return;
178         }
179 
180         lldb::VariableSP self_variable_sp =
181             variable_list_sp->FindVariable(ConstString("self"));
182 
183         if (!self_variable_sp || !self_variable_sp->IsInScope(frame) ||
184             !self_variable_sp->LocationIsValidForFrame(frame)) {
185           err.SetErrorString(selfErrorString);
186           return;
187         }
188       }
189 
190       m_in_objectivec_method = true;
191       m_needs_object_ptr = true;
192 
193       if (!method_decl->isInstanceMethod())
194         m_in_static_method = true;
195     }
196   } else if (clang::FunctionDecl *function_decl =
197                  ClangASTContext::DeclContextGetAsFunctionDecl(decl_context)) {
198     // We might also have a function that said in the debug information that it
199     // captured an object pointer.  The best way to deal with getting to the
200     // ivars at present is by pretending that this is a method of a class in
201     // whatever runtime the debug info says the object pointer belongs to.  Do
202     // that here.
203 
204     ClangASTMetadata *metadata =
205         ClangASTContext::DeclContextGetMetaData(decl_context, function_decl);
206     if (metadata && metadata->HasObjectPtr()) {
207       lldb::LanguageType language = metadata->GetObjectPtrLanguage();
208       if (language == lldb::eLanguageTypeC_plus_plus) {
209         if (m_enforce_valid_object) {
210           lldb::VariableListSP variable_list_sp(
211               function_block->GetBlockVariableList(true));
212 
213           const char *thisErrorString = "Stopped in a context claiming to "
214                                         "capture a C++ object pointer, but "
215                                         "'this' isn't available; pretending we "
216                                         "are in a generic context";
217 
218           if (!variable_list_sp) {
219             err.SetErrorString(thisErrorString);
220             return;
221           }
222 
223           lldb::VariableSP this_var_sp(
224               variable_list_sp->FindVariable(ConstString("this")));
225 
226           if (!this_var_sp || !this_var_sp->IsInScope(frame) ||
227               !this_var_sp->LocationIsValidForFrame(frame)) {
228             err.SetErrorString(thisErrorString);
229             return;
230           }
231         }
232 
233         m_in_cplusplus_method = true;
234         m_needs_object_ptr = true;
235       } else if (language == lldb::eLanguageTypeObjC) {
236         if (m_enforce_valid_object) {
237           lldb::VariableListSP variable_list_sp(
238               function_block->GetBlockVariableList(true));
239 
240           const char *selfErrorString =
241               "Stopped in a context claiming to capture an Objective-C object "
242               "pointer, but 'self' isn't available; pretending we are in a "
243               "generic context";
244 
245           if (!variable_list_sp) {
246             err.SetErrorString(selfErrorString);
247             return;
248           }
249 
250           lldb::VariableSP self_variable_sp =
251               variable_list_sp->FindVariable(ConstString("self"));
252 
253           if (!self_variable_sp || !self_variable_sp->IsInScope(frame) ||
254               !self_variable_sp->LocationIsValidForFrame(frame)) {
255             err.SetErrorString(selfErrorString);
256             return;
257           }
258 
259           Type *self_type = self_variable_sp->GetType();
260 
261           if (!self_type) {
262             err.SetErrorString(selfErrorString);
263             return;
264           }
265 
266           CompilerType self_clang_type = self_type->GetForwardCompilerType();
267 
268           if (!self_clang_type) {
269             err.SetErrorString(selfErrorString);
270             return;
271           }
272 
273           if (ClangASTContext::IsObjCClassType(self_clang_type)) {
274             return;
275           } else if (ClangASTContext::IsObjCObjectPointerType(
276                          self_clang_type)) {
277             m_in_objectivec_method = true;
278             m_needs_object_ptr = true;
279           } else {
280             err.SetErrorString(selfErrorString);
281             return;
282           }
283         } else {
284           m_in_objectivec_method = true;
285           m_needs_object_ptr = true;
286         }
287       }
288     }
289   }
290 }
291 
292 // This is a really nasty hack, meant to fix Objective-C expressions of the
293 // form (int)[myArray count].  Right now, because the type information for
294 // count is not available, [myArray count] returns id, which can't be directly
295 // cast to int without causing a clang error.
296 static void ApplyObjcCastHack(std::string &expr) {
297 #define OBJC_CAST_HACK_FROM "(int)["
298 #define OBJC_CAST_HACK_TO "(int)(long long)["
299 
300   size_t from_offset;
301 
302   while ((from_offset = expr.find(OBJC_CAST_HACK_FROM)) != expr.npos)
303     expr.replace(from_offset, sizeof(OBJC_CAST_HACK_FROM) - 1,
304                  OBJC_CAST_HACK_TO);
305 
306 #undef OBJC_CAST_HACK_TO
307 #undef OBJC_CAST_HACK_FROM
308 }
309 
310 namespace {
311 // Utility guard that calls a callback when going out of scope.
312 class OnExit {
313 public:
314   typedef std::function<void(void)> Callback;
315 
316   OnExit(Callback const &callback) : m_callback(callback) {}
317 
318   ~OnExit() { m_callback(); }
319 
320 private:
321   Callback m_callback;
322 };
323 } // namespace
324 
325 bool ClangUserExpression::Parse(DiagnosticManager &diagnostic_manager,
326                                 ExecutionContext &exe_ctx,
327                                 lldb_private::ExecutionPolicy execution_policy,
328                                 bool keep_result_in_memory,
329                                 bool generate_debug_info) {
330   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
331 
332   Status err;
333 
334   InstallContext(exe_ctx);
335 
336   if (Target *target = exe_ctx.GetTargetPtr()) {
337     if (PersistentExpressionState *persistent_state =
338             target->GetPersistentExpressionStateForLanguage(
339                 lldb::eLanguageTypeC)) {
340       m_result_delegate.RegisterPersistentState(persistent_state);
341     } else {
342       diagnostic_manager.PutString(
343           eDiagnosticSeverityError,
344           "couldn't start parsing (no persistent data)");
345       return false;
346     }
347   } else {
348     diagnostic_manager.PutString(eDiagnosticSeverityError,
349                                  "error: couldn't start parsing (no target)");
350     return false;
351   }
352 
353   ScanContext(exe_ctx, err);
354 
355   if (!err.Success()) {
356     diagnostic_manager.PutString(eDiagnosticSeverityWarning, err.AsCString());
357   }
358 
359   ////////////////////////////////////
360   // Generate the expression
361   //
362 
363   ApplyObjcCastHack(m_expr_text);
364 
365   std::string prefix = m_expr_prefix;
366 
367   if (ClangModulesDeclVendor *decl_vendor =
368           m_target->GetClangModulesDeclVendor()) {
369     const ClangModulesDeclVendor::ModuleVector &hand_imported_modules =
370         llvm::cast<ClangPersistentVariables>(
371             m_target->GetPersistentExpressionStateForLanguage(
372                 lldb::eLanguageTypeC))
373             ->GetHandLoadedClangModules();
374     ClangModulesDeclVendor::ModuleVector modules_for_macros;
375 
376     for (ClangModulesDeclVendor::ModuleID module : hand_imported_modules) {
377       modules_for_macros.push_back(module);
378     }
379 
380     if (m_target->GetEnableAutoImportClangModules()) {
381       if (StackFrame *frame = exe_ctx.GetFramePtr()) {
382         if (Block *block = frame->GetFrameBlock()) {
383           SymbolContext sc;
384 
385           block->CalculateSymbolContext(&sc);
386 
387           if (sc.comp_unit) {
388             StreamString error_stream;
389 
390             decl_vendor->AddModulesForCompileUnit(
391                 *sc.comp_unit, modules_for_macros, error_stream);
392           }
393         }
394       }
395     }
396   }
397 
398   lldb::LanguageType lang_type = lldb::eLanguageTypeUnknown;
399 
400   if (m_options.GetExecutionPolicy() == eExecutionPolicyTopLevel) {
401     m_transformed_text = m_expr_text;
402   } else {
403     std::unique_ptr<ExpressionSourceCode> source_code(
404         ExpressionSourceCode::CreateWrapped(prefix.c_str(),
405                                             m_expr_text.c_str()));
406 
407     if (m_in_cplusplus_method)
408       lang_type = lldb::eLanguageTypeC_plus_plus;
409     else if (m_in_objectivec_method)
410       lang_type = lldb::eLanguageTypeObjC;
411     else
412       lang_type = lldb::eLanguageTypeC;
413 
414     if (!source_code->GetText(m_transformed_text, lang_type, m_in_static_method,
415                               exe_ctx)) {
416       diagnostic_manager.PutString(eDiagnosticSeverityError,
417                                    "couldn't construct expression body");
418       return false;
419     }
420   }
421 
422   if (log)
423     log->Printf("Parsing the following code:\n%s", m_transformed_text.c_str());
424 
425   ////////////////////////////////////
426   // Set up the target and compiler
427   //
428 
429   Target *target = exe_ctx.GetTargetPtr();
430 
431   if (!target) {
432     diagnostic_manager.PutString(eDiagnosticSeverityError, "invalid target");
433     return false;
434   }
435 
436   //////////////////////////
437   // Parse the expression
438   //
439 
440   m_materializer_ap.reset(new Materializer());
441 
442   ResetDeclMap(exe_ctx, m_result_delegate, keep_result_in_memory);
443 
444   OnExit on_exit([this]() { ResetDeclMap(); });
445 
446   if (!DeclMap()->WillParse(exe_ctx, m_materializer_ap.get())) {
447     diagnostic_manager.PutString(
448         eDiagnosticSeverityError,
449         "current process state is unsuitable for expression parsing");
450     return false;
451   }
452 
453   if (m_options.GetExecutionPolicy() == eExecutionPolicyTopLevel) {
454     DeclMap()->SetLookupsEnabled(true);
455   }
456 
457   Process *process = exe_ctx.GetProcessPtr();
458   ExecutionContextScope *exe_scope = process;
459 
460   if (!exe_scope)
461     exe_scope = exe_ctx.GetTargetPtr();
462 
463   // We use a shared pointer here so we can use the original parser - if it
464   // succeeds or the rewrite parser we might make if it fails.  But the
465   // parser_sp will never be empty.
466 
467   ClangExpressionParser parser(exe_scope, *this, generate_debug_info);
468 
469   unsigned num_errors = parser.Parse(diagnostic_manager);
470 
471   // Check here for FixItHints.  If there are any try to apply the fixits and
472   // set the fixed text in m_fixed_text before returning an error.
473   if (num_errors) {
474     if (diagnostic_manager.HasFixIts()) {
475       if (parser.RewriteExpression(diagnostic_manager)) {
476         size_t fixed_start;
477         size_t fixed_end;
478         const std::string &fixed_expression =
479             diagnostic_manager.GetFixedExpression();
480         if (ExpressionSourceCode::GetOriginalBodyBounds(
481                 fixed_expression, lang_type, fixed_start, fixed_end))
482           m_fixed_text =
483               fixed_expression.substr(fixed_start, fixed_end - fixed_start);
484       }
485     }
486     return false;
487   }
488 
489   //////////////////////////////////////////////////////////////////////////////////////////
490   // Prepare the output of the parser for execution, evaluating it statically
491   // if possible
492   //
493 
494   {
495     Status jit_error = parser.PrepareForExecution(
496         m_jit_start_addr, m_jit_end_addr, m_execution_unit_sp, exe_ctx,
497         m_can_interpret, execution_policy);
498 
499     if (!jit_error.Success()) {
500       const char *error_cstr = jit_error.AsCString();
501       if (error_cstr && error_cstr[0])
502         diagnostic_manager.PutString(eDiagnosticSeverityError, error_cstr);
503       else
504         diagnostic_manager.PutString(eDiagnosticSeverityError,
505                                      "expression can't be interpreted or run");
506       return false;
507     }
508   }
509 
510   if (exe_ctx.GetProcessPtr() && execution_policy == eExecutionPolicyTopLevel) {
511     Status static_init_error =
512         parser.RunStaticInitializers(m_execution_unit_sp, exe_ctx);
513 
514     if (!static_init_error.Success()) {
515       const char *error_cstr = static_init_error.AsCString();
516       if (error_cstr && error_cstr[0])
517         diagnostic_manager.Printf(eDiagnosticSeverityError,
518                                   "couldn't run static initializers: %s\n",
519                                   error_cstr);
520       else
521         diagnostic_manager.PutString(eDiagnosticSeverityError,
522                                      "couldn't run static initializers\n");
523       return false;
524     }
525   }
526 
527   if (m_execution_unit_sp) {
528     bool register_execution_unit = false;
529 
530     if (m_options.GetExecutionPolicy() == eExecutionPolicyTopLevel) {
531       register_execution_unit = true;
532     }
533 
534     // If there is more than one external function in the execution unit, it
535     // needs to keep living even if it's not top level, because the result
536     // could refer to that function.
537 
538     if (m_execution_unit_sp->GetJittedFunctions().size() > 1) {
539       register_execution_unit = true;
540     }
541 
542     if (register_execution_unit) {
543       llvm::cast<PersistentExpressionState>(
544           exe_ctx.GetTargetPtr()->GetPersistentExpressionStateForLanguage(
545               m_language))
546           ->RegisterExecutionUnit(m_execution_unit_sp);
547     }
548   }
549 
550   if (generate_debug_info) {
551     lldb::ModuleSP jit_module_sp(m_execution_unit_sp->GetJITModule());
552 
553     if (jit_module_sp) {
554       ConstString const_func_name(FunctionName());
555       FileSpec jit_file;
556       jit_file.GetFilename() = const_func_name;
557       jit_module_sp->SetFileSpecAndObjectName(jit_file, ConstString());
558       m_jit_module_wp = jit_module_sp;
559       target->GetImages().Append(jit_module_sp);
560     }
561   }
562 
563   if (process && m_jit_start_addr != LLDB_INVALID_ADDRESS)
564     m_jit_process_wp = lldb::ProcessWP(process->shared_from_this());
565   return true;
566 }
567 
568 bool ClangUserExpression::AddArguments(ExecutionContext &exe_ctx,
569                                        std::vector<lldb::addr_t> &args,
570                                        lldb::addr_t struct_address,
571                                        DiagnosticManager &diagnostic_manager) {
572   lldb::addr_t object_ptr = LLDB_INVALID_ADDRESS;
573   lldb::addr_t cmd_ptr = LLDB_INVALID_ADDRESS;
574 
575   if (m_needs_object_ptr) {
576     lldb::StackFrameSP frame_sp = exe_ctx.GetFrameSP();
577     if (!frame_sp)
578       return true;
579 
580     ConstString object_name;
581 
582     if (m_in_cplusplus_method) {
583       object_name.SetCString("this");
584     } else if (m_in_objectivec_method) {
585       object_name.SetCString("self");
586     } else {
587       diagnostic_manager.PutString(
588           eDiagnosticSeverityError,
589           "need object pointer but don't know the language");
590       return false;
591     }
592 
593     Status object_ptr_error;
594 
595     object_ptr = GetObjectPointer(frame_sp, object_name, object_ptr_error);
596 
597     if (!object_ptr_error.Success()) {
598       exe_ctx.GetTargetRef().GetDebugger().GetAsyncOutputStream()->Printf(
599           "warning: `%s' is not accessible (substituting 0)\n",
600           object_name.AsCString());
601       object_ptr = 0;
602     }
603 
604     if (m_in_objectivec_method) {
605       ConstString cmd_name("_cmd");
606 
607       cmd_ptr = GetObjectPointer(frame_sp, cmd_name, object_ptr_error);
608 
609       if (!object_ptr_error.Success()) {
610         diagnostic_manager.Printf(
611             eDiagnosticSeverityWarning,
612             "couldn't get cmd pointer (substituting NULL): %s",
613             object_ptr_error.AsCString());
614         cmd_ptr = 0;
615       }
616     }
617 
618     args.push_back(object_ptr);
619 
620     if (m_in_objectivec_method)
621       args.push_back(cmd_ptr);
622 
623     args.push_back(struct_address);
624   } else {
625     args.push_back(struct_address);
626   }
627   return true;
628 }
629 
630 lldb::ExpressionVariableSP ClangUserExpression::GetResultAfterDematerialization(
631     ExecutionContextScope *exe_scope) {
632   return m_result_delegate.GetVariable();
633 }
634 
635 void ClangUserExpression::ClangUserExpressionHelper::ResetDeclMap(
636     ExecutionContext &exe_ctx,
637     Materializer::PersistentVariableDelegate &delegate,
638     bool keep_result_in_memory) {
639   m_expr_decl_map_up.reset(
640       new ClangExpressionDeclMap(keep_result_in_memory, &delegate, exe_ctx));
641 }
642 
643 clang::ASTConsumer *
644 ClangUserExpression::ClangUserExpressionHelper::ASTTransformer(
645     clang::ASTConsumer *passthrough) {
646   m_result_synthesizer_up.reset(
647       new ASTResultSynthesizer(passthrough, m_top_level, m_target));
648 
649   return m_result_synthesizer_up.get();
650 }
651 
652 void ClangUserExpression::ClangUserExpressionHelper::CommitPersistentDecls() {
653   if (m_result_synthesizer_up.get()) {
654     m_result_synthesizer_up->CommitPersistentDecls();
655   }
656 }
657 
658 ConstString ClangUserExpression::ResultDelegate::GetName() {
659   auto prefix = m_persistent_state->GetPersistentVariablePrefix();
660   return m_persistent_state->GetNextPersistentVariableName(*m_target_sp,
661                                                            prefix);
662 }
663 
664 void ClangUserExpression::ResultDelegate::DidDematerialize(
665     lldb::ExpressionVariableSP &variable) {
666   m_variable = variable;
667 }
668 
669 void ClangUserExpression::ResultDelegate::RegisterPersistentState(
670     PersistentExpressionState *persistent_state) {
671   m_persistent_state = persistent_state;
672 }
673 
674 lldb::ExpressionVariableSP &ClangUserExpression::ResultDelegate::GetVariable() {
675   return m_variable;
676 }
677