Skip to content

Commit 3a083b5

Browse files
committed
[lldb] Fix Scripted ProcessLaunchInfo Argument nullptr deref
This patch adds a new `StructuredData::Dictionary` constructor that takes a `StructuredData::ObjectSP` as an argument. This is used to pass the opaque_ptr from the `SBStructuredData` used to initialize a ScriptedProecss, to the `ProcessLaunchInfo` class. This also updates `SBLaunchInfo::SetScriptedProcessDictionary` to reflect the formentionned changes which solves the nullptr deref. Differential Revision: https://reviews.llvm.org/D112107 Signed-off-by: Med Ismail Bennani <[email protected]>
1 parent 984f2c2 commit 3a083b5

File tree

6 files changed

+26
-13
lines changed

6 files changed

+26
-13
lines changed

lldb/include/lldb/Core/StructuredDataImpl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ class StructuredDataImpl {
152152
return (::snprintf(dst, dst_len, "%s", result.data()));
153153
}
154154

155+
StructuredData::ObjectSP GetObjectSP() const { return m_data_sp; }
156+
155157
private:
156158
lldb::StructuredDataPluginWP m_plugin_wp;
157159
StructuredData::ObjectSP m_data_sp;

lldb/include/lldb/Utility/StructuredData.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,17 @@ class StructuredData {
353353
public:
354354
Dictionary() : Object(lldb::eStructuredDataTypeDictionary), m_dict() {}
355355

356+
Dictionary(ObjectSP obj_sp)
357+
: Object(lldb::eStructuredDataTypeDictionary), m_dict() {
358+
if (!obj_sp || obj_sp->GetType() != lldb::eStructuredDataTypeDictionary) {
359+
SetType(lldb::eStructuredDataTypeInvalid);
360+
return;
361+
}
362+
363+
Dictionary *dict = obj_sp->GetAsDictionary();
364+
m_dict = dict->m_dict;
365+
}
366+
356367
~Dictionary() override = default;
357368

358369
size_t GetSize() const { return m_dict.size(); }

lldb/source/API/SBLaunchInfo.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -380,16 +380,18 @@ lldb::SBStructuredData SBLaunchInfo::GetScriptedProcessDictionary() const {
380380
void SBLaunchInfo::SetScriptedProcessDictionary(lldb::SBStructuredData dict) {
381381
LLDB_RECORD_METHOD(void, SBLaunchInfo, SetScriptedProcessDictionary,
382382
(lldb::SBStructuredData), dict);
383+
if (!dict.IsValid() || !dict.m_impl_up)
384+
return;
383385

384-
SBStream stream;
385-
SBError error = dict.GetAsJSON(stream);
386+
StructuredData::ObjectSP obj_sp = dict.m_impl_up->GetObjectSP();
386387

387-
if (error.Fail())
388+
if (!obj_sp)
388389
return;
389390

390-
StructuredData::DictionarySP dict_sp;
391-
llvm::json::OStream s(stream.ref().AsRawOstream());
392-
dict_sp->Serialize(s);
391+
StructuredData::DictionarySP dict_sp =
392+
std::make_shared<StructuredData::Dictionary>(obj_sp);
393+
if (!dict_sp || dict_sp->GetType() == lldb::eStructuredDataTypeInvalid)
394+
return;
393395

394396
m_opaque_sp->SetScriptedProcessDictionarySP(dict_sp);
395397
}

lldb/source/Plugins/Process/scripted/ScriptedProcess.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ ScriptedProcess::ScriptedProcess(
111111

112112
StructuredData::GenericSP object_sp = GetInterface().CreatePluginObject(
113113
m_scripted_process_info.GetClassName().c_str(), exe_ctx,
114-
m_scripted_process_info.GetDictionarySP());
114+
m_scripted_process_info.GetArgsSP());
115115

116116
if (!object_sp || !object_sp->IsValid()) {
117117
error.SetErrorStringWithFormat("ScriptedProcess::%s () - ERROR: %s",

lldb/source/Plugins/Process/scripted/ScriptedProcess.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,15 @@ class ScriptedProcess : public Process {
2525
public:
2626
ScriptedProcessInfo(const ProcessLaunchInfo &launch_info) {
2727
m_class_name = launch_info.GetScriptedProcessClassName();
28-
m_dictionary_sp = launch_info.GetScriptedProcessDictionarySP();
28+
m_args_sp = launch_info.GetScriptedProcessDictionarySP();
2929
}
3030

3131
std::string GetClassName() const { return m_class_name; }
32-
StructuredData::DictionarySP GetDictionarySP() const {
33-
return m_dictionary_sp;
34-
}
32+
StructuredData::DictionarySP GetArgsSP() const { return m_args_sp; }
3533

3634
private:
3735
std::string m_class_name;
38-
StructuredData::DictionarySP m_dictionary_sp;
36+
StructuredData::DictionarySP m_args_sp;
3937
};
4038

4139
public:

lldb/source/Plugins/Process/scripted/ScriptedThread.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ ScriptedThread::ScriptedThread(ScriptedProcess &process, Status &error)
5555
StructuredData::GenericSP object_sp =
5656
scripted_thread_interface->CreatePluginObject(
5757
class_name->c_str(), exe_ctx,
58-
process.m_scripted_process_info.GetDictionarySP());
58+
process.m_scripted_process_info.GetArgsSP());
5959
if (!object_sp || !object_sp->IsValid()) {
6060
error.SetErrorString("Failed to create valid script object");
6161
return;

0 commit comments

Comments
 (0)