Skip to content

Commit b565e7f

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
1 parent 453c287 commit b565e7f

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
@@ -66,17 +66,31 @@ bool FunctionCaller::WriteFunctionWrapper(
6666
ExecutionContext &exe_ctx, DiagnosticManager &diagnostic_manager) {
6767
Process *process = exe_ctx.GetProcessPtr();
6868

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

74-
if (process != jit_process_sp.get())
76+
if (process != jit_process_sp.get()) {
77+
diagnostic_manager.Printf(eDiagnosticSeverityError,
78+
"process does not match the stored process.");
7579
return false;
76-
77-
if (!m_compiled)
80+
}
81+
82+
if (process->GetState() != lldb::eStateStopped) {
83+
diagnostic_manager.Printf(eDiagnosticSeverityError,
84+
"process is not stopped");
7885
return false;
86+
}
7987

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

@@ -213,6 +227,17 @@ bool FunctionCaller::WriteFunctionArguments(
213227
bool FunctionCaller::InsertFunction(ExecutionContext &exe_ctx,
214228
lldb::addr_t &args_addr_ref,
215229
DiagnosticManager &diagnostic_manager) {
230+
// Since we might need to call allocate memory and maybe call code to make
231+
// the caller, we need to be stopped.
232+
Process *process = exe_ctx.GetProcessPtr();
233+
if (!process) {
234+
diagnostic_manager.PutString(eDiagnosticSeverityError, "no process");
235+
return false;
236+
}
237+
if (process->GetState() != lldb::eStateStopped) {
238+
diagnostic_manager.PutString(eDiagnosticSeverityError, "process running");
239+
return false;
240+
}
216241
if (CompileFunction(exe_ctx.GetThreadSP(), diagnostic_manager) != 0)
217242
return false;
218243
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
@@ -194,16 +194,22 @@ UserExpression::Evaluate(ExecutionContext &exe_ctx,
194194

195195
Process *process = exe_ctx.GetProcessPtr();
196196

197-
if (process == nullptr || process->GetState() != lldb::eStateStopped) {
198-
if (execution_policy == eExecutionPolicyAlways) {
199-
LLDB_LOG(log, "== [UserExpression::Evaluate] Expression may not run, but "
200-
"is not constant ==");
197+
if (process == nullptr && execution_policy == eExecutionPolicyAlways) {
198+
LLDB_LOG(log, "== [UserExpression::Evaluate] No process, but the policy is "
199+
"eExecutionPolicyAlways");
201200

202-
error.SetErrorString("expression needed to run but couldn't");
201+
error.SetErrorString("expression needed to run but couldn't: no process");
203202

204-
return execution_results;
205-
}
203+
return execution_results;
206204
}
205+
// Since we might need to call allocate memory and maybe call code to make
206+
// the caller, we need to be stopped.
207+
if (process != nullptr && process->GetState() != lldb::eStateStopped) {
208+
error.SetErrorString("Can't make a function caller while the process is "
209+
"running");
210+
return execution_results;
211+
}
212+
207213

208214
// Explicitly force the IR interpreter to evaluate the expression when the
209215
// 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
@@ -1293,7 +1293,10 @@ uint32_t Process::AssignIndexIDToThread(uint64_t thread_id) {
12931293
}
12941294

12951295
StateType Process::GetState() {
1296-
return m_public_state.GetValue();
1296+
if (CurrentThreadIsPrivateStateThread())
1297+
return m_private_state.GetValue();
1298+
else
1299+
return m_public_state.GetValue();
12971300
}
12981301

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

0 commit comments

Comments
 (0)