Skip to content

Commit 32955b4

Browse files
committed
Don't try to create Expressions when the process is running.
We generally prohibit this at a higher level - for instance requiring the process to be stopped for "expr". But when we trigger an expression for internal purposes (e.g. to fetch types from the ObjC runtime) we weren't checking the process state. Now we explicitly check this at the very start of the job so we don't get into bad states. Differential Revision: https://reviews.llvm.org/D137684 (cherry picked from commit b565e7f)
1 parent f42c6e5 commit 32955b4

File tree

5 files changed

+60
-13
lines changed

5 files changed

+60
-13
lines changed

lldb/source/Expression/FunctionCaller.cpp

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,17 +65,31 @@ bool FunctionCaller::WriteFunctionWrapper(
6565
ExecutionContext &exe_ctx, DiagnosticManager &diagnostic_manager) {
6666
Process *process = exe_ctx.GetProcessPtr();
6767

68-
if (!process)
68+
if (!process) {
69+
diagnostic_manager.Printf(eDiagnosticSeverityError, "no process.");
6970
return false;
70-
71+
}
72+
7173
lldb::ProcessSP jit_process_sp(m_jit_process_wp.lock());
7274

73-
if (process != jit_process_sp.get())
75+
if (process != jit_process_sp.get()) {
76+
diagnostic_manager.Printf(eDiagnosticSeverityError,
77+
"process does not match the stored process.");
7478
return false;
75-
76-
if (!m_compiled)
79+
}
80+
81+
if (process->GetState() != lldb::eStateStopped) {
82+
diagnostic_manager.Printf(eDiagnosticSeverityError,
83+
"process is not stopped");
7784
return false;
85+
}
7886

87+
if (!m_compiled) {
88+
diagnostic_manager.Printf(eDiagnosticSeverityError,
89+
"function not compiled");
90+
return false;
91+
}
92+
7993
if (m_JITted)
8094
return true;
8195

@@ -212,6 +226,17 @@ bool FunctionCaller::WriteFunctionArguments(
212226
bool FunctionCaller::InsertFunction(ExecutionContext &exe_ctx,
213227
lldb::addr_t &args_addr_ref,
214228
DiagnosticManager &diagnostic_manager) {
229+
// Since we might need to call allocate memory and maybe call code to make
230+
// the caller, we need to be stopped.
231+
Process *process = exe_ctx.GetProcessPtr();
232+
if (!process) {
233+
diagnostic_manager.PutString(eDiagnosticSeverityError, "no process");
234+
return false;
235+
}
236+
if (process->GetState() != lldb::eStateStopped) {
237+
diagnostic_manager.PutString(eDiagnosticSeverityError, "process running");
238+
return false;
239+
}
215240
if (CompileFunction(exe_ctx.GetThreadSP(), diagnostic_manager) != 0)
216241
return false;
217242
if (!WriteFunctionWrapper(exe_ctx, diagnostic_manager))

lldb/source/Expression/UserExpression.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -176,16 +176,22 @@ UserExpression::Evaluate(ExecutionContext &exe_ctx,
176176

177177
Process *process = exe_ctx.GetProcessPtr();
178178

179-
if (process == nullptr || process->GetState() != lldb::eStateStopped) {
180-
if (execution_policy == eExecutionPolicyAlways) {
181-
LLDB_LOG(log, "== [UserExpression::Evaluate] Expression may not run, but "
182-
"is not constant ==");
179+
if (process == nullptr && execution_policy == eExecutionPolicyAlways) {
180+
LLDB_LOG(log, "== [UserExpression::Evaluate] No process, but the policy is "
181+
"eExecutionPolicyAlways");
183182

184-
error.SetErrorString("expression needed to run but couldn't");
183+
error.SetErrorString("expression needed to run but couldn't: no process");
185184

186-
return execution_results;
187-
}
185+
return execution_results;
188186
}
187+
// Since we might need to call allocate memory and maybe call code to make
188+
// the caller, we need to be stopped.
189+
if (process != nullptr && process->GetState() != lldb::eStateStopped) {
190+
error.SetErrorString("Can't make a function caller while the process is "
191+
"running");
192+
return execution_results;
193+
}
194+
189195

190196
// Explicitly force the IR interpreter to evaluate the expression when the
191197
// there is no process that supports running the expression for us. Don't

lldb/source/Expression/UtilityFunction.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@ FunctionCaller *UtilityFunction::MakeFunctionCaller(
6464
error.SetErrorString("Can't make a function caller without a process.");
6565
return nullptr;
6666
}
67+
// Since we might need to call allocate memory and maybe call code to make
68+
// the caller, we need to be stopped.
69+
if (process_sp->GetState() != lldb::eStateStopped) {
70+
error.SetErrorString("Can't make a function caller while the process is "
71+
"running");
72+
return nullptr;
73+
}
6774

6875
Address impl_code_address;
6976
impl_code_address.SetOffset(StartAddress());

lldb/source/Plugins/ExpressionParser/Clang/ClangUtilityFunction.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,12 @@ bool ClangUtilityFunction::Install(DiagnosticManager &diagnostic_manager,
9999
return false;
100100
}
101101

102+
// Since we might need to call allocate memory and maybe call code to make
103+
// the caller, we need to be stopped.
104+
if (process->GetState() != lldb::eStateStopped) {
105+
diagnostic_manager.PutString(eDiagnosticSeverityError, "process running");
106+
return false;
107+
}
102108
//////////////////////////
103109
// Parse the expression
104110
//

lldb/source/Target/Process.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1431,7 +1431,10 @@ uint32_t Process::AssignIndexIDToThread(uint64_t thread_id) {
14311431
}
14321432

14331433
StateType Process::GetState() {
1434-
return m_public_state.GetValue();
1434+
if (CurrentThreadIsPrivateStateThread())
1435+
return m_private_state.GetValue();
1436+
else
1437+
return m_public_state.GetValue();
14351438
}
14361439

14371440
void Process::SetPublicState(StateType new_state, bool restarted) {

0 commit comments

Comments
 (0)