Coverage Report

Created: 2023-11-11 10:31

/Users/buildslave/jenkins/workspace/coverage/llvm-project/lldb/source/Plugins/Process/Utility/InferiorCallPOSIX.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- InferiorCallPOSIX.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 "InferiorCallPOSIX.h"
10
#include "lldb/Core/Address.h"
11
#include "lldb/Core/Module.h"
12
#include "lldb/Core/ValueObject.h"
13
#include "lldb/Expression/DiagnosticManager.h"
14
#include "lldb/Host/Config.h"
15
#include "lldb/Symbol/SymbolContext.h"
16
#include "lldb/Symbol/TypeSystem.h"
17
#include "lldb/Target/ExecutionContext.h"
18
#include "lldb/Target/Platform.h"
19
#include "lldb/Target/Process.h"
20
#include "lldb/Target/Target.h"
21
#include "lldb/Target/ThreadPlanCallFunction.h"
22
23
#if LLDB_ENABLE_POSIX
24
#include <sys/mman.h>
25
#else
26
// define them
27
#define PROT_NONE 0
28
#define PROT_READ 1
29
#define PROT_WRITE 2
30
#define PROT_EXEC 4
31
#endif
32
33
using namespace lldb;
34
using namespace lldb_private;
35
36
bool lldb_private::InferiorCallMmap(Process *process, addr_t &allocated_addr,
37
                                    addr_t addr, addr_t length, unsigned prot,
38
0
                                    unsigned flags, addr_t fd, addr_t offset) {
39
0
  Thread *thread =
40
0
      process->GetThreadList().GetExpressionExecutionThread().get();
41
0
  if (thread == nullptr)
42
0
    return false;
43
44
0
  ModuleFunctionSearchOptions function_options;
45
0
  function_options.include_symbols = true;
46
0
  function_options.include_inlines = false;
47
48
0
  SymbolContextList sc_list;
49
0
  process->GetTarget().GetImages().FindFunctions(
50
0
      ConstString("mmap"), eFunctionNameTypeFull, function_options, sc_list);
51
0
  const uint32_t count = sc_list.GetSize();
52
0
  if (count > 0) {
53
0
    SymbolContext sc;
54
0
    if (sc_list.GetContextAtIndex(0, sc)) {
55
0
      const uint32_t range_scope =
56
0
          eSymbolContextFunction | eSymbolContextSymbol;
57
0
      const bool use_inline_block_range = false;
58
0
      EvaluateExpressionOptions options;
59
0
      options.SetStopOthers(true);
60
0
      options.SetUnwindOnError(true);
61
0
      options.SetIgnoreBreakpoints(true);
62
0
      options.SetTryAllThreads(true);
63
0
      options.SetDebug(false);
64
0
      options.SetTimeout(process->GetUtilityExpressionTimeout());
65
0
      options.SetTrapExceptions(false);
66
67
0
      addr_t prot_arg;
68
0
      if (prot == eMmapProtNone)
69
0
        prot_arg = PROT_NONE;
70
0
      else {
71
0
        prot_arg = 0;
72
0
        if (prot & eMmapProtExec)
73
0
          prot_arg |= PROT_EXEC;
74
0
        if (prot & eMmapProtRead)
75
0
          prot_arg |= PROT_READ;
76
0
        if (prot & eMmapProtWrite)
77
0
          prot_arg |= PROT_WRITE;
78
0
      }
79
80
0
      AddressRange mmap_range;
81
0
      if (sc.GetAddressRange(range_scope, 0, use_inline_block_range,
82
0
                             mmap_range)) {
83
0
        auto type_system_or_err =
84
0
            process->GetTarget().GetScratchTypeSystemForLanguage(
85
0
                eLanguageTypeC);
86
0
        if (!type_system_or_err) {
87
0
          llvm::consumeError(type_system_or_err.takeError());
88
0
          return false;
89
0
        }
90
0
        auto ts = *type_system_or_err;
91
0
        if (!ts)
92
0
          return false;
93
0
        CompilerType void_ptr_type =
94
0
            ts->GetBasicTypeFromAST(eBasicTypeVoid).GetPointerType();
95
0
        const ArchSpec arch = process->GetTarget().GetArchitecture();
96
0
        MmapArgList args =
97
0
            process->GetTarget().GetPlatform()->GetMmapArgumentList(
98
0
                arch, addr, length, prot_arg, flags, fd, offset);
99
0
        lldb::ThreadPlanSP call_plan_sp(
100
0
            new ThreadPlanCallFunction(*thread, mmap_range.GetBaseAddress(),
101
0
                                       void_ptr_type, args, options));
102
0
        if (call_plan_sp) {
103
0
          DiagnosticManager diagnostics;
104
105
0
          StackFrame *frame = thread->GetStackFrameAtIndex(0).get();
106
0
          if (frame) {
107
0
            ExecutionContext exe_ctx;
108
0
            frame->CalculateExecutionContext(exe_ctx);
109
0
            ExpressionResults result = process->RunThreadPlan(
110
0
                exe_ctx, call_plan_sp, options, diagnostics);
111
0
            if (result == eExpressionCompleted) {
112
113
0
              allocated_addr =
114
0
                  call_plan_sp->GetReturnValueObject()->GetValueAsUnsigned(
115
0
                      LLDB_INVALID_ADDRESS);
116
0
              if (process->GetAddressByteSize() == 4) {
117
0
                if (allocated_addr == UINT32_MAX)
118
0
                  return false;
119
0
              } else if (process->GetAddressByteSize() == 8) {
120
0
                if (allocated_addr == UINT64_MAX)
121
0
                  return false;
122
0
              }
123
0
              return true;
124
0
            }
125
0
          }
126
0
        }
127
0
      }
128
0
    }
129
0
  }
130
131
0
  return false;
132
0
}
133
134
bool lldb_private::InferiorCallMunmap(Process *process, addr_t addr,
135
0
                                      addr_t length) {
136
0
  Thread *thread =
137
0
      process->GetThreadList().GetExpressionExecutionThread().get();
138
0
  if (thread == nullptr)
139
0
    return false;
140
141
0
  ModuleFunctionSearchOptions function_options;
142
0
  function_options.include_symbols = true;
143
0
  function_options.include_inlines = false;
144
145
0
  SymbolContextList sc_list;
146
0
  process->GetTarget().GetImages().FindFunctions(
147
0
      ConstString("munmap"), eFunctionNameTypeFull, function_options, sc_list);
148
0
  const uint32_t count = sc_list.GetSize();
149
0
  if (count > 0) {
150
0
    SymbolContext sc;
151
0
    if (sc_list.GetContextAtIndex(0, sc)) {
152
0
      const uint32_t range_scope =
153
0
          eSymbolContextFunction | eSymbolContextSymbol;
154
0
      const bool use_inline_block_range = false;
155
0
      EvaluateExpressionOptions options;
156
0
      options.SetStopOthers(true);
157
0
      options.SetUnwindOnError(true);
158
0
      options.SetIgnoreBreakpoints(true);
159
0
      options.SetTryAllThreads(true);
160
0
      options.SetDebug(false);
161
0
      options.SetTimeout(process->GetUtilityExpressionTimeout());
162
0
      options.SetTrapExceptions(false);
163
164
0
      AddressRange munmap_range;
165
0
      if (sc.GetAddressRange(range_scope, 0, use_inline_block_range,
166
0
                             munmap_range)) {
167
0
        lldb::addr_t args[] = {addr, length};
168
0
        lldb::ThreadPlanSP call_plan_sp(
169
0
            new ThreadPlanCallFunction(*thread, munmap_range.GetBaseAddress(),
170
0
                                       CompilerType(), args, options));
171
0
        if (call_plan_sp) {
172
0
          DiagnosticManager diagnostics;
173
174
0
          StackFrame *frame = thread->GetStackFrameAtIndex(0).get();
175
0
          if (frame) {
176
0
            ExecutionContext exe_ctx;
177
0
            frame->CalculateExecutionContext(exe_ctx);
178
0
            ExpressionResults result = process->RunThreadPlan(
179
0
                exe_ctx, call_plan_sp, options, diagnostics);
180
0
            if (result == eExpressionCompleted) {
181
0
              return true;
182
0
            }
183
0
          }
184
0
        }
185
0
      }
186
0
    }
187
0
  }
188
189
0
  return false;
190
0
}