Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,11 @@
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;X86;BIT86;_DEBUG;DATADOGNATIVELOADER_EXPORTS;_WINDOWS;_USRDLL;_SILENCE_STDEXT_ARR_ITERS_DEPRECATION_WARNING;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>BUILD_WITH_WORKLOAD_SELECTION;WIN32;X86;BIT86;_DEBUG;DATADOGNATIVELOADER_EXPORTS;_WINDOWS;_USRDLL;_SILENCE_STDEXT_ARR_ITERS_DEPRECATION_WARNING;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
<LanguageStandard>stdcpp20</LanguageStandard>
<AdditionalIncludeDirectories>$(LIB_INCLUDES);$(SHARED-LIB-INCLUDES);%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>$(LIB_INCLUDES);$(SHARED-LIB-INCLUDES);$(SHARED-LIB-PATH)\libpolicies\x86\libpolicies\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalOptions>/FS %(AdditionalOptions)</AdditionalOptions>
<MultiProcessorCompilation>$(ENABLE_MULTIPROCESSOR_COMPILATION)</MultiProcessorCompilation>
<MultiProcessorCompilation Condition=" '$(ENABLE_MULTIPROCESSOR_COMPILATION)' == '' ">true</MultiProcessorCompilation>
Expand All @@ -172,7 +172,7 @@
<GenerateDebugInformation>true</GenerateDebugInformation>
<ModuleDefinitionFile>.\Datadog.Trace.ClrProfiler.Native.def</ModuleDefinitionFile>
<EnableUAC>false</EnableUAC>
<AdditionalDependencies>Rpcrt4.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>Rpcrt4.lib;$(SHARED-LIB-PATH)\libpolicies\x86\libpolicies\lib\policies.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
Expand All @@ -187,11 +187,11 @@
<IntrinsicFunctions>true</IntrinsicFunctions>
<FavorSizeOrSpeed>Speed</FavorSizeOrSpeed>
<OmitFramePointers>true</OmitFramePointers>
<PreprocessorDefinitions>WIN32;X86;BIT86;NDEBUG;DATADOGNATIVELOADER_EXPORTS;_WINDOWS;_USRDLL;_SILENCE_STDEXT_ARR_ITERS_DEPRECATION_WARNING;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>BUILD_WITH_WORKLOAD_SELECTION;WIN32;X86;BIT86;NDEBUG;DATADOGNATIVELOADER_EXPORTS;_WINDOWS;_USRDLL;_SILENCE_STDEXT_ARR_ITERS_DEPRECATION_WARNING;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpp20</LanguageStandard>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<AdditionalIncludeDirectories>$(LIB_INCLUDES);$(SHARED-LIB-INCLUDES);%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>$(LIB_INCLUDES);$(SHARED-LIB-INCLUDES);$(SHARED-LIB-PATH)\libpolicies\x86\libpolicies\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalOptions>/FS %(AdditionalOptions)</AdditionalOptions>
<MultiProcessorCompilation>$(ENABLE_MULTIPROCESSOR_COMPILATION)</MultiProcessorCompilation>
<MultiProcessorCompilation Condition=" '$(ENABLE_MULTIPROCESSOR_COMPILATION)' == '' ">true</MultiProcessorCompilation>
Expand All @@ -203,7 +203,7 @@
<GenerateDebugInformation>true</GenerateDebugInformation>
<ModuleDefinitionFile>.\Datadog.Trace.ClrProfiler.Native.def</ModuleDefinitionFile>
<EnableUAC>false</EnableUAC>
<AdditionalDependencies>Rpcrt4.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>Rpcrt4.lib;$(SHARED-LIB-PATH)\libpolicies\x86\libpolicies\lib\policies.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
Expand All @@ -215,8 +215,8 @@
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpp20</LanguageStandard>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>BIT64;AMD64;_DEBUG;DATADOGNATIVELOADER_EXPORTS;_WINDOWS;_USRDLL;_SILENCE_STDEXT_ARR_ITERS_DEPRECATION_WARNING;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalIncludeDirectories>$(SHARED-LIB-INCLUDES);$(LIB_INCLUDES);%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<PreprocessorDefinitions>BUILD_WITH_WORKLOAD_SELECTION;BIT64;AMD64;_DEBUG;DATADOGNATIVELOADER_EXPORTS;_WINDOWS;_USRDLL;_SILENCE_STDEXT_ARR_ITERS_DEPRECATION_WARNING;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalIncludeDirectories>$(SHARED-LIB-INCLUDES);$(LIB_INCLUDES);$(SHARED-LIB-PATH)\libpolicies\x64\libpolicies\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalOptions>/FS %(AdditionalOptions)</AdditionalOptions>
<MultiProcessorCompilation>$(ENABLE_MULTIPROCESSOR_COMPILATION)</MultiProcessorCompilation>
<MultiProcessorCompilation Condition=" '$(ENABLE_MULTIPROCESSOR_COMPILATION)' == '' ">true</MultiProcessorCompilation>
Expand All @@ -226,7 +226,7 @@
<GenerateDebugInformation>true</GenerateDebugInformation>
<ModuleDefinitionFile>.\Datadog.Trace.ClrProfiler.Native.def</ModuleDefinitionFile>
<EnableUAC>false</EnableUAC>
<AdditionalDependencies>Rpcrt4.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>Rpcrt4.lib;$(SHARED-LIB-PATH)\libpolicies\x64\libpolicies\lib\policies.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
Expand All @@ -243,10 +243,10 @@
<LanguageStandard>stdcpp20</LanguageStandard>
<MultiProcessorCompilation>$(ENABLE_MULTIPROCESSOR_COMPILATION)</MultiProcessorCompilation>
<MultiProcessorCompilation Condition=" '$(ENABLE_MULTIPROCESSOR_COMPILATION)' == '' ">true</MultiProcessorCompilation>
<AdditionalIncludeDirectories>$(LIB_INCLUDES);$(SHARED-LIB-INCLUDES);%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>$(LIB_INCLUDES);$(SHARED-LIB-INCLUDES);$(SHARED-LIB-PATH)\libpolicies\x64\libpolicies\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalOptions>/FS %(AdditionalOptions)</AdditionalOptions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>BIT64;AMD64;NDEBUG;DATADOGNATIVELOADER_EXPORTS;_WINDOWS;_USRDLL;_SILENCE_STDEXT_ARR_ITERS_DEPRECATION_WARNING;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>BUILD_WITH_WORKLOAD_SELECTION;BIT64;AMD64;NDEBUG;DATADOGNATIVELOADER_EXPORTS;_WINDOWS;_USRDLL;_SILENCE_STDEXT_ARR_ITERS_DEPRECATION_WARNING;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
Expand All @@ -255,7 +255,7 @@
<GenerateDebugInformation>true</GenerateDebugInformation>
<ModuleDefinitionFile>.\Datadog.Trace.ClrProfiler.Native.def</ModuleDefinitionFile>
<EnableUAC>false</EnableUAC>
<AdditionalDependencies>Rpcrt4.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>Rpcrt4.lib;$(SHARED-LIB-PATH)\libpolicies\x64\libpolicies\lib\policies.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|ARM64' OR '$(Configuration)|$(Platform)'=='Debug|ARM64EC'">
Expand Down Expand Up @@ -337,6 +337,9 @@
<ClInclude Include="resource.h" />
<ClInclude Include="runtimeid_store.h" />
<ClInclude Include="single_step_guard_rails.h" />
<ClInclude Include="workload_selection.h" />
<ClInclude Include="workload_selection_noop.h" />
<ClInclude Include="workload_selection_impl.h" />
<ClInclude Include="util.h" />
</ItemGroup>
<ItemGroup>
Expand All @@ -350,6 +353,7 @@
<ClCompile Include="crashhandler.cpp" />
<ClCompile Include="dllmain.cpp" />
<ClCompile Include="dynamic_instance.cpp" />
<ClCompile Include="workload_selection_impl.cpp" />
<ClCompile Include="dynamic_dispatcher.cpp" />
<ClCompile Include="instrumented_assembly_generator\instrumented_assembly_generator_cor_profiler_function_control.cpp" />
<ClCompile Include="instrumented_assembly_generator\instrumented_assembly_generator_cor_profiler_info.cpp" />
Expand All @@ -376,4 +380,4 @@
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
</Project>
22 changes: 21 additions & 1 deletion shared/src/Datadog.Trace.ClrProfiler.Native/cor_profiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "../../../shared/src/native-src/pal.h"
#include "environment.h"
#include "single_step_guard_rails.h"
#include "workload_selection.h"
#include "instrumented_assembly_generator/instrumented_assembly_generator_cor_profiler_function_control.h"
#include "instrumented_assembly_generator/instrumented_assembly_generator_cor_profiler_info.h"
#include "instrumented_assembly_generator/instrumented_assembly_generator_helper.h"
Expand Down Expand Up @@ -316,6 +317,25 @@ namespace datadog::shared::nativeloader
return E_FAIL;
}

if (IsSingleStepInstrumentation())
{
Log::Info("CorProfiler::Initialize: Evaluating workload selection.");

if (auto policies = readPolicies(); policies)
{
WSTRING application_pool;
if (auto maybe_application_pool = GetApplicationPool())
{
application_pool = std::move(*maybe_application_pool);
}

if (isWorkloadAllowed(process_name, tokenized_command_line, application_pool, *policies, IsRunningOnIIS()) == false)
{
return CORPROF_E_PROFILER_CANCEL_ACTIVATION;
}
}
}

// Guard rails have all passed, so we enable (and flush) logs if necessary
Log::EnableAutoFlush();

Expand Down Expand Up @@ -1325,4 +1345,4 @@ namespace datadog::shared::nativeloader
return m_this->m_runtimeIdStore.Get(appDomain).c_str();
}

} // namespace datadog::shared::nativeloader
} // namespace datadog::shared::nativeloader
54 changes: 51 additions & 3 deletions shared/src/Datadog.Trace.ClrProfiler.Native/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
#ifdef _WIN32
#include <windows.h>
EXTERN_C IMAGE_DOS_HEADER __ImageBase;
#define HINST_THISCOMPONENT ((HINSTANCE) &__ImageBase)
#define HINST_THISCOMPONENT ((HINSTANCE) & __ImageBase)
#else
#define _GNU_SOURCE
#include <dlfcn.h>
#endif

#include <algorithm>
#include <optional>

#include "../../../shared/src/native-src/dd_filesystem.hpp"
// namespace fs is an alias defined in "dd_filesystem.hpp"
#include "../../../shared/src/native-src/pal.h"
Expand All @@ -26,13 +29,26 @@ static fs::path GetCurrentModuleFolderPath()
}
#else
Dl_info info;
if (dladdr((void*)GetCurrentModuleFolderPath, &info))
if (dladdr((void*) GetCurrentModuleFolderPath, &info))
{
return fs::path(info.dli_fname).remove_filename();
}
#endif
return {};
}
#ifdef _WIN32
static fs::path GetPoliciesPath()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a thought - I'd suggest that we only keep this calculation as a "fallback" path, and instead that we set this path via an env variable in fleet installer? That way if, for whatever reason, we need to move where the agent puts this, we're still backwards compatible? WDYT @bmermet ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How will it help maintaining backward compatibility? Since,FleetInstaller and the native profiler are shipped together in the OCI, it seems to me it doesn't make a big difference if the path is computed in either of them.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My thinking was that this would be the fleet installer (not the .NET FleetInstaller.exe 😅 ) that sets the path. Because the location of the flatbuffers file is essentially managed completely independently, by the agent(?), so what happens if we need to put it somewhere else, or rename it or etc? A new version of the agent/installer might need to change the paths, say for security reasons?

As an aside, purely for integration testing, it makes this actually testable in CI 😄 So I'd advocate for an env var override either way tbh, even if we want to have the fallback be the "normal" case

{
fs::path program_data_path = shared::GetEnvironmentValue(WStr("PROGRAMDATA"));

if (program_data_path.empty())
{
program_data_path = WStr(R"(C:\ProgramData)");
}

return program_data_path / "Datadog" / "workload_selection.fb";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be consistent with what's done on linux, I think this should be named: user-wls-policy.bin

}
#endif

static ::shared::WSTRING GetDatadogLogsDirectoryPath()
{
Expand Down Expand Up @@ -78,12 +94,44 @@ static fs::path GetConfigurationFilePath()
return GetCurrentModuleFolderPath() / conf_filename;
}

inline bool IsSingleStepInstrumentation()
{
const auto isSingleStepVariable = ::shared::GetEnvironmentValue(environment::single_step_instrumentation_enabled);
return !isSingleStepVariable.empty();
}

inline bool IsRunningOnIIS()
{
const auto& process_name = ::shared::GetCurrentProcessName();
return process_name == WStr("w3wp.exe") || process_name == WStr("iisexpress.exe");
}

inline std::optional<::shared::WSTRING> GetApplicationPool()
{
if (const auto& app_pool_id = ::shared::GetEnvironmentValue(environment::azure_app_services_app_pool_id);
!app_pool_id.empty())
{
return app_pool_id;
}

// Try to infer the Application Pool from the command line. w3wp.exe (IIS Worker Process)
// can be started with an Application Pool by using `-ap` argument.
const auto [_, argv] = ::shared::GetCurrentProcessCommandLine();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already grab this in cor_profiler, maybe we can just pass it in?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addressed in 813b52d

auto it = std::find(argv.cbegin(), argv.cend(), WStr("-ap"));
if (it == argv.cend())
{
return std::nullopt;
}

it = std::next(it);
if (it == argv.cend() || it->empty())
{
return std::nullopt;
}

return *it;
}

inline std::string GetCurrentOsArch(bool isRunningOnAlpine)
{
#if AMD64
Expand Down Expand Up @@ -137,4 +185,4 @@ inline std::string GetCurrentOsArch(bool isRunningOnAlpine)
#else
#error "currentOsArch not defined."
#endif
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#pragma once
#ifdef BUILD_WITH_WORKLOAD_SELECTION
#include "workload_selection_impl.h"
#else
#include "workload_selection_noop.h"
#endif
Loading
Loading