Skip to content

Commit

Permalink
Add proper error checking around GetModuleMetadata
Browse files Browse the repository at this point in the history
  • Loading branch information
kevingosse committed Sep 5, 2024
1 parent 3800b4e commit 82a0277
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,13 @@ HRESULT CorProfilerInfo::GetModuleMetaData(ModuleID moduleId, DWORD dwOpenFlags,
{
HRESULT hr;
ComPtr<IUnknown> temp;
IfFailRet(m_corProfilerInfo->GetModuleMetaData(moduleId, dwOpenFlags, riid, temp.GetAddressOf()));
hr = m_corProfilerInfo->GetModuleMetaData(moduleId, dwOpenFlags, riid, temp.GetAddressOf());

if (hr != S_OK)
{
return hr;
}

try
{
const auto metadataInterfaces = new MetadataInterfaces(temp);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,13 @@ inline HRESULT WriteILChanges(ModuleID moduleId, mdMethodDef methodToken, LPCBYT
try
{
ComPtr<IUnknown> metadataInterfaces;
IfFailRet(corProfilerInfo->GetModuleMetaData(moduleId, CorOpenFlags::ofRead, IID_IMetaDataImport,
metadataInterfaces.GetAddressOf()));
hr = corProfilerInfo->GetModuleMetaData(moduleId, CorOpenFlags::ofRead, IID_IMetaDataImport,
metadataInterfaces.GetAddressOf());

if (hr != S_OK)
{
return hr;
}

auto metadataImport = metadataInterfaces.As<IMetaDataImport>(IID_IMetaDataImport);

Expand Down
2 changes: 1 addition & 1 deletion tracer/src/Datadog.Tracer.Native/clr_helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1107,7 +1107,7 @@ HRESULT ResolveTypeInternal(ICorProfilerInfo4* info,
ComPtr<IUnknown> metadata_interfaces;
auto hr = info->GetModuleMetaData(moduleId, ofRead, IID_IMetaDataImport2,
metadata_interfaces.GetAddressOf());
if (FAILED(hr))
if (hr != S_OK)
{
Logger::Warn("[ResolveTypeInternal] GetModuleMetaData has failed with: ", shared::WSTRING(refTypeName.data()));
continue;
Expand Down
12 changes: 6 additions & 6 deletions tracer/src/Datadog.Tracer.Native/cor_profiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ HRESULT STDMETHODCALLTYPE CorProfiler::AssemblyLoadFinished(AssemblyID assembly_
auto hr = this->info_->GetModuleMetaData(assembly_info.manifest_module_id, ofRead,
IID_IMetaDataImport2, metadata_interfaces.GetAddressOf());

if (FAILED(hr))
if (hr != S_OK)
{
Logger::Warn("AssemblyLoadFinished failed to get metadata interface for module id ",
assembly_info.manifest_module_id, " from assembly ", assembly_info.name, " HRESULT=0x",
Expand Down Expand Up @@ -891,7 +891,7 @@ HRESULT CorProfiler::TryRejitModule(ModuleID module_id, std::vector<ModuleID>& m
auto hr = this->info_->GetModuleMetaData(module_id, ofRead | ofWrite, IID_IMetaDataImport2,
metadata_interfaces.GetAddressOf());

if (FAILED(hr))
if (hr != S_OK)
{
Logger::Warn("ModuleLoadFinished failed to get metadata interface for ", module_id, " ",
module_info.assembly.name);
Expand Down Expand Up @@ -989,7 +989,7 @@ HRESULT CorProfiler::TryRejitModule(ModuleID module_id, std::vector<ModuleID>& m
auto hr = this->info_->GetModuleMetaData(module_id, ofRead | ofWrite, IID_IMetaDataImport2,
metadata_interfaces.GetAddressOf());

if (FAILED(hr))
if (hr != S_OK)
{
Logger::Warn("ModuleLoadFinished failed to get metadata interface for ", module_id, " ",
module_info.assembly.name);
Expand Down Expand Up @@ -1044,7 +1044,7 @@ HRESULT CorProfiler::TryRejitModule(ModuleID module_id, std::vector<ModuleID>& m
auto hr = this->info_->GetModuleMetaData(module_id, ofRead, IID_IMetaDataImport2,
metadata_interfaces.GetAddressOf());

if (FAILED(hr))
if (hr != S_OK)
{
Logger::Warn("ModuleLoadFinished failed to get metadata interface for ", module_id, " ",
module_info.assembly.name);
Expand Down Expand Up @@ -3163,7 +3163,7 @@ HRESULT CorProfiler::GenerateVoidILStartupMethod(const ModuleID module_id, mdMet
ComPtr<IUnknown> metadata_interfaces;
auto hr = this->info_->GetModuleMetaData(module_id, ofRead | ofWrite, IID_IMetaDataImport2,
metadata_interfaces.GetAddressOf());
if (FAILED(hr))
if (hr != S_OK)
{
Logger::Warn("GenerateVoidILStartupMethod: failed to get metadata interface for ", module_id);
return hr;
Expand Down Expand Up @@ -3925,7 +3925,7 @@ HRESULT CorProfiler::AddIISPreStartInitFlags(const ModuleID module_id, const mdT
ComPtr<IUnknown> metadata_interfaces;
auto hr = this->info_->GetModuleMetaData(module_id, ofRead | ofWrite, IID_IMetaDataImport2,
metadata_interfaces.GetAddressOf());
if (FAILED(hr))
if (hr != S_OK)
{
Logger::Warn("GenerateVoidILStartupMethod: failed to get metadata interface for ", module_id);
return hr;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,7 @@ void DebuggerProbesInstrumentationRequester::ModuleLoadFinished_AddMetadataToMod
Logger::Debug(" Loading Assembly Metadata...");
auto hr = corProfilerInfo->GetModuleMetaData(moduleInfo.id, ofRead | ofWrite, IID_IMetaDataImport2,
metadataInterfaces.GetAddressOf());
if (FAILED(hr))
if (hr != S_OK)
{
Logger::Warn(
"DebuggerProbesInstrumentationRequester::sAddMetadataToModule failed to get metadata interface for ",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ ULONG DebuggerRejitPreprocessor::PreprocessLineProbes(
Logger::Debug(" Loading Assembly Metadata...");
auto hr = corProfilerInfo->GetModuleMetaData(moduleInfo.id, ofRead | ofWrite, IID_IMetaDataImport2,
metadataInterfaces.GetAddressOf());
if (FAILED(hr))
if (hr != S_OK)
{
Logger::Warn("CallTarget_RequestRejitForModule failed to get metadata interface for ", moduleInfo.id, " ",
moduleInfo.assembly.name);
Expand Down
24 changes: 12 additions & 12 deletions tracer/src/Datadog.Tracer.Native/iast/dataflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -406,45 +406,45 @@ HRESULT Dataflow::GetModuleInterfaces(ModuleID moduleId, IMetaDataImport2** ppMe
IMetaDataAssemblyEmit** ppAssemblyEmit)
{
HRESULT hr = S_OK;
if (SUCCEEDED(hr))
if (hr == S_OK)
{
IUnknown* piUnk = nullptr;
hr = _profiler->GetModuleMetaData(moduleId, ofRead | ofWrite, IID_IMetaDataImport2, &piUnk);
if (SUCCEEDED(hr))
if (hr == S_OK)
{
hr = piUnk->QueryInterface(IID_IMetaDataImport2, (void**) ppMetadataImport);
REL(piUnk);
}
REL(piUnk);
}
if (SUCCEEDED(hr))
if (hr == S_OK)
{
IUnknown* piUnk = nullptr;
hr = _profiler->GetModuleMetaData(moduleId, ofRead | ofWrite, IID_IMetaDataEmit2, &piUnk);
if (SUCCEEDED(hr))
if (hr == S_OK)
{
hr = piUnk->QueryInterface(IID_IMetaDataEmit2, (void**) ppMetadataEmit);
REL(piUnk);
}
REL(piUnk);
}
if (SUCCEEDED(hr))
if (hr == S_OK)
{
IUnknown* piUnk = nullptr;
hr = _profiler->GetModuleMetaData(moduleId, ofRead | ofWrite, IID_IMetaDataAssemblyImport, &piUnk);
if (SUCCEEDED(hr))
if (hr == S_OK)
{
hr = piUnk->QueryInterface(IID_IMetaDataAssemblyImport, (void**) ppAssemblyImport);
REL(piUnk);
}
REL(piUnk);
}
if (SUCCEEDED(hr))
if (hr == S_OK)
{
IUnknown* piUnk = nullptr;
hr = _profiler->GetModuleMetaData(moduleId, ofRead | ofWrite, IID_IMetaDataAssemblyEmit, &piUnk);
if (SUCCEEDED(hr))
if (hr == S_OK)
{
hr = piUnk->QueryInterface(IID_IMetaDataAssemblyEmit, (void**) ppAssemblyEmit);
REL(piUnk);
}
REL(piUnk);
}
return hr;
}
Expand Down
4 changes: 2 additions & 2 deletions tracer/src/Datadog.Tracer.Native/rejit_preprocessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,7 @@ ULONG RejitPreprocessor<RejitRequestDefinition>::PreprocessRejitRequests(
Logger::Debug(" Loading Assembly Metadata...");
auto hr = corProfilerInfo->GetModuleMetaData(moduleInfo.id, ofRead | ofWrite, IID_IMetaDataImport2,
metadataInterfaces.GetAddressOf());
if (FAILED(hr))
if (hr != S_OK)
{
Logger::Warn("CallTarget_RequestRejitForModule failed to get metadata interface for ",
moduleInfo.id, " ", moduleInfo.assembly.name);
Expand Down Expand Up @@ -799,7 +799,7 @@ ULONG RejitPreprocessor<RejitRequestDefinition>::PreprocessRejitRequests(
Logger::Debug(" Loading Assembly Metadata...");
auto hr = corProfilerInfo->GetModuleMetaData(moduleInfo.id, ofRead | ofWrite, IID_IMetaDataImport2,
metadataInterfaces.GetAddressOf());
if (FAILED(hr))
if (hr != S_OK)
{
Logger::Warn("CallTarget_RequestRejitForModule failed to get metadata interface for ",
moduleInfo.id, " ", moduleInfo.assembly.name);
Expand Down

0 comments on commit 82a0277

Please sign in to comment.