Coverage Report

Created: 2022-01-18 06:27

/Users/buildslave/jenkins/workspace/coverage/llvm-project/lldb/source/Plugins/ExpressionParser/Clang/ClangFunctionCaller.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- ClangFunctionCaller.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 "ClangFunctionCaller.h"
10
11
#include "ASTStructExtractor.h"
12
#include "ClangExpressionParser.h"
13
14
#include "clang/AST/ASTContext.h"
15
#include "clang/AST/RecordLayout.h"
16
#include "clang/CodeGen/CodeGenAction.h"
17
#include "clang/CodeGen/ModuleBuilder.h"
18
#include "clang/Frontend/CompilerInstance.h"
19
#include "llvm/ADT/StringRef.h"
20
#include "llvm/ADT/Triple.h"
21
#include "llvm/ExecutionEngine/ExecutionEngine.h"
22
#include "llvm/IR/Module.h"
23
24
#include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
25
#include "lldb/Core/Module.h"
26
#include "lldb/Core/ValueObject.h"
27
#include "lldb/Core/ValueObjectList.h"
28
#include "lldb/Expression/IRExecutionUnit.h"
29
#include "lldb/Interpreter/CommandReturnObject.h"
30
#include "lldb/Symbol/Function.h"
31
#include "lldb/Symbol/Type.h"
32
#include "lldb/Target/ExecutionContext.h"
33
#include "lldb/Target/Process.h"
34
#include "lldb/Target/RegisterContext.h"
35
#include "lldb/Target/Target.h"
36
#include "lldb/Target/Thread.h"
37
#include "lldb/Target/ThreadPlan.h"
38
#include "lldb/Target/ThreadPlanCallFunction.h"
39
#include "lldb/Utility/DataExtractor.h"
40
#include "lldb/Utility/Log.h"
41
#include "lldb/Utility/State.h"
42
43
using namespace lldb_private;
44
45
char ClangFunctionCaller::ID;
46
47
// ClangFunctionCaller constructor
48
ClangFunctionCaller::ClangFunctionCaller(ExecutionContextScope &exe_scope,
49
                                         const CompilerType &return_type,
50
                                         const Address &functionAddress,
51
                                         const ValueList &arg_value_list,
52
                                         const char *name)
53
    : FunctionCaller(exe_scope, return_type, functionAddress, arg_value_list,
54
                     name),
55
2.51k
      m_type_system_helper(*this) {
56
2.51k
  m_jit_process_wp = lldb::ProcessWP(exe_scope.CalculateProcess());
57
  // Can't make a ClangFunctionCaller without a process.
58
2.51k
  assert(m_jit_process_wp.lock());
59
2.51k
}
60
61
// Destructor
62
2.46k
ClangFunctionCaller::~ClangFunctionCaller() = default;
63
64
unsigned
65
66
ClangFunctionCaller::CompileFunction(lldb::ThreadSP thread_to_use_sp,
67
5.21k
                                     DiagnosticManager &diagnostic_manager) {
68
5.21k
  if (m_compiled)
69
2.70k
    return 0;
70
71
  // Compilation might call code, make sure to keep on the thread the caller
72
  // indicated.
73
2.51k
  ThreadList::ExpressionExecutionThreadPusher execution_thread_pusher(
74
2.51k
      thread_to_use_sp);
75
76
  // FIXME: How does clang tell us there's no return value?  We need to handle
77
  // that case.
78
2.51k
  unsigned num_errors = 0;
79
80
2.51k
  std::string return_type_str(
81
2.51k
      m_function_return_type.GetTypeName().AsCString(""));
82
83
  // Cons up the function we're going to wrap our call in, then compile it...
84
  // We declare the function "extern "C"" because the compiler might be in C++
85
  // mode which would mangle the name and then we couldn't find it again...
86
2.51k
  m_wrapper_function_text.clear();
87
2.51k
  m_wrapper_function_text.append("extern \"C\" void ");
88
2.51k
  m_wrapper_function_text.append(m_wrapper_function_name);
89
2.51k
  m_wrapper_function_text.append(" (void *input)\n{\n    struct ");
90
2.51k
  m_wrapper_function_text.append(m_wrapper_struct_name);
91
2.51k
  m_wrapper_function_text.append(" \n  {\n");
92
2.51k
  m_wrapper_function_text.append("    ");
93
2.51k
  m_wrapper_function_text.append(return_type_str);
94
2.51k
  m_wrapper_function_text.append(" (*fn_ptr) (");
95
96
  // Get the number of arguments.  If we have a function type and it is
97
  // prototyped, trust that, otherwise use the values we were given.
98
99
  // FIXME: This will need to be extended to handle Variadic functions.  We'll
100
  // need
101
  // to pull the defined arguments out of the function, then add the types from
102
  // the arguments list for the variable arguments.
103
104
2.51k
  uint32_t num_args = UINT32_MAX;
105
2.51k
  bool trust_function = false;
106
  // GetArgumentCount returns -1 for an unprototyped function.
107
2.51k
  CompilerType function_clang_type;
108
2.51k
  if (m_function_ptr) {
109
0
    function_clang_type = m_function_ptr->GetCompilerType();
110
0
    if (function_clang_type) {
111
0
      int num_func_args = function_clang_type.GetFunctionArgumentCount();
112
0
      if (num_func_args >= 0) {
113
0
        trust_function = true;
114
0
        num_args = num_func_args;
115
0
      }
116
0
    }
117
0
  }
118
119
2.51k
  if (num_args == UINT32_MAX)
120
2.51k
    num_args = m_arg_values.GetSize();
121
122
2.51k
  std::string args_buffer; // This one stores the definition of all the args in
123
                           // "struct caller".
124
2.51k
  std::string args_list_buffer; // This one stores the argument list called from
125
                                // the structure.
126
14.5k
  for (size_t i = 0; i < num_args; 
i++12.0k
) {
127
12.0k
    std::string type_name;
128
129
12.0k
    if (trust_function) {
130
0
      type_name = function_clang_type.GetFunctionArgumentTypeAtIndex(i)
131
0
                      .GetTypeName()
132
0
                      .AsCString("");
133
12.0k
    } else {
134
12.0k
      CompilerType clang_qual_type =
135
12.0k
          m_arg_values.GetValueAtIndex(i)->GetCompilerType();
136
12.0k
      if (clang_qual_type) {
137
12.0k
        type_name = clang_qual_type.GetTypeName().AsCString("");
138
12.0k
      } else {
139
0
        diagnostic_manager.Printf(
140
0
            eDiagnosticSeverityError,
141
0
            "Could not determine type of input value %" PRIu64 ".",
142
0
            (uint64_t)i);
143
0
        return 1;
144
0
      }
145
12.0k
    }
146
147
12.0k
    m_wrapper_function_text.append(type_name);
148
12.0k
    if (i < num_args - 1)
149
9.58k
      m_wrapper_function_text.append(", ");
150
151
12.0k
    char arg_buf[32];
152
12.0k
    args_buffer.append("    ");
153
12.0k
    args_buffer.append(type_name);
154
12.0k
    snprintf(arg_buf, 31, "arg_%" PRIu64, (uint64_t)i);
155
12.0k
    args_buffer.push_back(' ');
156
12.0k
    args_buffer.append(arg_buf);
157
12.0k
    args_buffer.append(";\n");
158
159
12.0k
    args_list_buffer.append("__lldb_fn_data->");
160
12.0k
    args_list_buffer.append(arg_buf);
161
12.0k
    if (i < num_args - 1)
162
9.58k
      args_list_buffer.append(", ");
163
12.0k
  }
164
2.51k
  m_wrapper_function_text.append(
165
2.51k
      ");\n"); // Close off the function calling prototype.
166
167
2.51k
  m_wrapper_function_text.append(args_buffer);
168
169
2.51k
  m_wrapper_function_text.append("    ");
170
2.51k
  m_wrapper_function_text.append(return_type_str);
171
2.51k
  m_wrapper_function_text.append(" return_value;");
172
2.51k
  m_wrapper_function_text.append("\n  };\n  struct ");
173
2.51k
  m_wrapper_function_text.append(m_wrapper_struct_name);
174
2.51k
  m_wrapper_function_text.append("* __lldb_fn_data = (struct ");
175
2.51k
  m_wrapper_function_text.append(m_wrapper_struct_name);
176
2.51k
  m_wrapper_function_text.append(" *) input;\n");
177
178
2.51k
  m_wrapper_function_text.append(
179
2.51k
      "  __lldb_fn_data->return_value = __lldb_fn_data->fn_ptr (");
180
2.51k
  m_wrapper_function_text.append(args_list_buffer);
181
2.51k
  m_wrapper_function_text.append(");\n}\n");
182
183
2.51k
  Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
184
2.51k
  LLDB_LOGF(log, "Expression: \n\n%s\n\n", m_wrapper_function_text.c_str());
185
186
  // Okay, now compile this expression
187
188
2.51k
  lldb::ProcessSP jit_process_sp(m_jit_process_wp.lock());
189
2.51k
  if (jit_process_sp) {
190
2.51k
    const bool generate_debug_info = true;
191
2.51k
    auto *clang_parser = new ClangExpressionParser(jit_process_sp.get(), *this,
192
2.51k
                                                   generate_debug_info);
193
2.51k
    num_errors = clang_parser->Parse(diagnostic_manager);
194
2.51k
    m_parser.reset(clang_parser);
195
2.51k
  } else {
196
0
    diagnostic_manager.PutString(eDiagnosticSeverityError,
197
0
                                 "no process - unable to inject function");
198
0
    num_errors = 1;
199
0
  }
200
201
2.51k
  m_compiled = (num_errors == 0);
202
203
2.51k
  if (!m_compiled)
204
0
    return num_errors;
205
206
2.51k
  return num_errors;
207
2.51k
}
208
209
clang::ASTConsumer *
210
ClangFunctionCaller::ClangFunctionCallerHelper::ASTTransformer(
211
2.51k
    clang::ASTConsumer *passthrough) {
212
2.51k
  m_struct_extractor = std::make_unique<ASTStructExtractor>(
213
2.51k
      passthrough, m_owner.GetWrapperStructName(), m_owner);
214
215
2.51k
  return m_struct_extractor.get();
216
2.51k
}