Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assembly version filtering #1342

Merged
merged 17 commits into from
Jan 24, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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
@@ -1,7 +1,3 @@
// Copyright 2020 New Relic, Inc. All rights reserved.
// SPDX-License-Identifier: Apache-2.0


// ------------------------------------------------------------------------------
// <auto-generated>
// Generated by Xsd2Code. Version 3.6.0.0
Expand Down Expand Up @@ -305,6 +301,10 @@ public partial class extensionTracerFactoryMatch

private string assemblyNameField;

private string minVersionField;

private string maxVersionField;

private string classNameField;

/// <summary>
Expand Down Expand Up @@ -344,6 +344,38 @@ public string assemblyName
}
}

/// <summary>
/// The minimum version (inclusive) for this assembly to match. e.g. "1.2.3.4"
/// </summary>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string minVersion
{
get
{
return this.minVersionField;
}
set
{
this.minVersionField = value;
}
}

/// <summary>
/// The maximum version (inclusive) for this assembly to mach. e.g. "1.2.3.4"
/// </summary>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string maxVersion
{
get
{
return this.maxVersionField;
}
set
{
this.maxVersionField = value;
}
}

/// <summary>
/// The full class name to match. This is case sensitive - use the correct mixed case.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,30 @@
</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="minVersion" use="optional">
<xs:annotation>
<xs:documentation>
The minimum version (inclusive) for this assembly to match. e.g. "1.2.3.4"
</xs:documentation>
</xs:annotation>
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[0-9]+(\.[0-9]+)?(\.[0-9]+)?(\.[0-9]+)?"></xs:pattern>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
<xs:attribute name="maxVersion" use="optional">
<xs:annotation>
<xs:documentation>
The maximum version (exclusive) for this assembly to mach. e.g. "1.2.3.4"
</xs:documentation>
</xs:annotation>
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[0-9]+(\.[0-9]+)?(\.[0-9]+)?(\.[0-9]+)?"></xs:pattern>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
<xs:attribute name="className" type="xs:string" use="required">
<xs:annotation>
<xs:documentation>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ SPDX-License-Identifier: Apache-2.0
<instrumentation>

<tracerFactory>
<match assemblyName="System.Net.Http" className="System.Net.Http.HttpClient">
<match assemblyName="System.Net.Http" className="System.Net.Http.HttpClient" maxVersion="5.0">
<exactMethodMatcher methodName="SendAsync" parameters="System.Net.Http.HttpRequestMessage,System.Net.Http.HttpCompletionOption,System.Threading.CancellationToken" />
</match>
<match assemblyName="System.Net.Http" className="System.Net.Http.SocketsHttpHandler">
<match assemblyName="System.Net.Http" className="System.Net.Http.SocketsHttpHandler" minVersion="5.0">
<exactMethodMatcher methodName="SendAsync" parameters="System.Net.Http.HttpRequestMessage,System.Threading.CancellationToken" />
</match>
<match assemblyName="System.Net.Http" className="System.Net.Http.SocketsHttpHandler">
<match assemblyName="System.Net.Http" className="System.Net.Http.SocketsHttpHandler" minVersion="5.0">
<exactMethodMatcher methodName="Send" parameters="System.Net.Http.HttpRequestMessage,System.Threading.CancellationToken" />
</match>
</tracerFactory>
Expand Down
192 changes: 192 additions & 0 deletions src/Agent/NewRelic/Profiler/Common/AssemblyVersion.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
/*
* Copyright 2020 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include "../Configuration/Strings.h"
#include <vector>
#include <sstream>
#include <cor.h>
#include <limits>
#include "../Logging/Logger.h"

namespace NewRelic { namespace Profiler
{
/// <summary>
/// Class to represent and compare assembly versions
/// </summary>
class AssemblyVersion
{
public:
unsigned short Major;
unsigned short Minor;
unsigned short Build;
unsigned short Revision;

/// <summary>
/// Constructor
/// </summary>
AssemblyVersion(unsigned short major,
unsigned short minor = 0,
unsigned short build = 0,
unsigned short rev = 0)
{
Major = major;
Minor = minor;
Build = build;
Revision = rev;
}

/// <summary>
/// Constructor given an existing assembly
/// </summary>
/// <param name="metadata">Assembly metadata</param>
AssemblyVersion(ASSEMBLYMETADATA metadata)
{
Major = metadata.usMajorVersion;
Minor = metadata.usMinorVersion;
Build = metadata.usBuildNumber;
Revision = metadata.usRevisionNumber;
}

/// <summary>
/// Copy constructor
/// </summary>
/// <param name="other"></param>
AssemblyVersion(const AssemblyVersion& other)
{
Major = other.Major;
Minor = other.Minor;
Build = other.Build;
Revision = other.Revision;
}

/// <summary>
/// Helper function to create an AssemblyVersion from a version string. If the
/// string is invalid it returns null
/// </summary>
/// <param name="version">Version string. Only digits and periods are allowed.
/// Partial versions (e.g. "2.3" or even just "1") are fine, but prerelease,
/// beta, etc. identifiers are not valid</param>
/// <returns>An AssemblyVersion or null</returns>
static AssemblyVersion* Create(xstring_t version)
{
if (version.size() == 0)
{
return nullptr;
}
auto identifiers = Configuration::Strings::Split(version, '.');
int major = 0;
int minor = 0;
int build = 0;
int revision = 0;

// We could just take the first four numbers but safer to treat as an error
if (identifiers.size() > 4)
{
LogWarn(version, L" is not a valid version");
return nullptr;
}

try
{
if (identifiers.size() > 0) major = xstoi(identifiers[0]);
if (identifiers.size() > 1) minor = xstoi(identifiers[1]);
if (identifiers.size() > 2) build = xstoi(identifiers[2]);
if (identifiers.size() > 3) revision = xstoi(identifiers[3]);
}
catch (...)
{
// stoi throws if it can't parse the string
LogWarn(version, L" is not a valid version");
return nullptr;
}

// Negative numbers aren't valid
if ((major < 0) || (minor < 0) || (build < 0) || (revision < 0))
{
LogWarn(version, L" is not a valid version");
return nullptr;
}

// Nor is anything over 65535
constexpr int maxValid = (std::numeric_limits<unsigned short>::max)();
if ((major > maxValid) || (minor > maxValid) || (build > maxValid) || (revision > maxValid))
{
LogWarn(version, L" is not a valid version");
return nullptr;
}

// Or 0.0.0.0
if ((major == 0) && (minor == 0) && (build == 0) && (revision == 0))
{
return nullptr;
}

return new AssemblyVersion((unsigned short)major, (unsigned short)minor, (unsigned short)build, (unsigned short)revision);
}

/// <summary>
/// Comparison operators
/// </summary>

bool operator==(const AssemblyVersion& other) const
{
return (this->Major == other.Major) &&
(this->Minor == other.Minor) &&
(this->Build == other.Build) &&
(this->Revision == other.Revision);
}

bool operator<(const AssemblyVersion& other) const
{
if (this->Major < other.Major) return true;
if (this->Major > other.Major) return false;

if (this->Minor < other.Minor) return true;
if (this->Minor > other.Minor) return false;

if (this->Build < other.Build) return true;
if (this->Build > other.Build) return false;

if (this->Revision < other.Revision) return true;
if (this->Revision > other.Revision) return false;

// All equal
return false;
}

bool operator>(const AssemblyVersion& other) const
{
if (this->Major > other.Major) return true;
if (this->Major < other.Major) return false;

if (this->Minor > other.Minor) return true;
if (this->Minor < other.Minor) return false;

if (this->Build > other.Build) return true;
if (this->Build < other.Build) return false;

if (this->Revision > other.Revision) return true;
if (this->Revision < other.Revision) return false;

// All equal
return false;
}

bool operator<=(const AssemblyVersion& other) const
{
return (*this < other) || (*this == other);
}

bool operator>=(const AssemblyVersion& other) const
{
return (*this > other) || (*this == other);
}

xstring_t ToString()
{
return to_xstring(Major) + _X(".") + to_xstring(Minor) + _X(".") + to_xstring(Build) + _X(".") + to_xstring(Revision);
}
};
}}
1 change: 1 addition & 0 deletions src/Agent/NewRelic/Profiler/Common/Common.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="CorStandIn.h" />
<ClInclude Include="AssemblyVersion.h" />
<ClInclude Include="Macros.h" />
<ClInclude Include="OnDestruction.h" />
<ClInclude Include="Strings.h" />
Expand Down
4 changes: 4 additions & 0 deletions src/Agent/NewRelic/Profiler/Common/Common.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,9 @@
<ClInclude Include="Strings.h" />
<ClInclude Include="OnDestruction.h" />
<ClInclude Include="xplat.h" />
<ClInclude Include="AssemblyVersion.h" />
</ItemGroup>
<ItemGroup>
<None Include="$(MSBuildThisFileDirectory)newrelic-icon.png" />
</ItemGroup>
</Project>
9 changes: 8 additions & 1 deletion src/Agent/NewRelic/Profiler/Common/xplat.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ make_unique(Args &&...) = delete;
typedef std::u16string::value_type xchar_t;
typedef std::u16string xstring_t;


inline xstring_t to_xstring(unsigned short val)
{
auto str = std::to_string(val);
return xstring_t(str.begin(), str.end());
}
inline xstring_t to_xstring(unsigned int val)
{
auto str = std::to_string(val);
Expand Down Expand Up @@ -104,6 +110,7 @@ inline int gmtime_s(struct tm* _tm, const time_t* time)
typedef wchar_t xchar_t;
typedef std::basic_string<xchar_t> xstring_t;

inline xstring_t to_xstring(unsigned short val) { return std::to_wstring(val); }
inline xstring_t to_xstring(unsigned int val) { return std::to_wstring(val); }
inline xstring_t to_xstring(unsigned long val) { return std::to_wstring(val); }
inline int xstoi(xstring_t str)
Expand Down Expand Up @@ -143,4 +150,4 @@ inline xstring_t to_hex_string(_T value, int width = 0, bool showbase=false)
oss << value;
const auto str = oss.str();
return xstring_t(str.cbegin(), str.cend());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@
</ClCompile>
<ClCompile Include="StringsTest.cpp" />
<ClCompile Include="TestModuleAttributes.cpp" />
<ClCompile Include="VersionTest.cpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,9 @@
<ClCompile Include="stdafx.cpp" />
<ClCompile Include="TestModuleAttributes.cpp" />
<ClCompile Include="StringsTest.cpp" />
<ClCompile Include="VersionTest.cpp" />
</ItemGroup>
<ItemGroup>
<None Include="$(MSBuildThisFileDirectory)newrelic-icon.png" />
</ItemGroup>
</Project>
Loading