Skip to content

Commit

Permalink
Add PerformanceCounters Testing (dotnet#24812)
Browse files Browse the repository at this point in the history
Add PerformanceCounters Testing
  • Loading branch information
michellemcdaniel authored Nov 8, 2017
1 parent 9089ff2 commit 5617d39
Show file tree
Hide file tree
Showing 17 changed files with 1,116 additions and 185 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,6 @@
<Reference Include="System.Runtime.Extensions" />
<Reference Include="System.Runtime.InteropServices" />
<Reference Include="System.Security.Claims" />
<Reference Include="System.Security.Permissions" />
<Reference Include="System.Security.Principal.Windows" />
<Reference Include="System.Security.Principal" />
<Reference Include="System.Threading" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.ComponentModel;
using System.IO;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using System.Globalization;

namespace System.Diagnostics
Expand Down Expand Up @@ -235,9 +234,8 @@ private static void LoadPerfCounterDll()
if (s_perfCounterDllLoaded)
return;

new FileIOPermission(PermissionState.Unrestricted).Assert();

string installPath = SharedUtils.GetLatestBuildDllDirectory(".");

string perfcounterPath = Path.Combine(installPath, "perfcounter.dll");
if (Interop.Kernel32.LoadLibrary(perfcounterPath) == IntPtr.Zero)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,6 @@ public string CounterHelp
string currentCategoryName = _categoryName;
string currentMachineName = _machineName;

PerformanceCounterPermission permission = new PerformanceCounterPermission(PerformanceCounterPermissionAccess.Read, currentMachineName, currentCategoryName);
permission.Demand();
Initialize();

if (_helpMsg == null)
Expand Down Expand Up @@ -212,9 +210,6 @@ public PerformanceCounterType CounterType

// This is the same thing that NextSample does, except that it doesn't try to get the actual counter
// value. If we wanted the counter value, we would need to have an instance name.
PerformanceCounterPermission permission = new PerformanceCounterPermission(PerformanceCounterPermissionAccess.Read, currentMachineName, currentCategoryName);
permission.Demand();

Initialize();
CategorySample categorySample = PerformanceCounterLib.GetCategorySample(currentMachineName, currentCategoryName);
CounterDefinitionSample counterSample = categorySample.GetCounterDefinitionSample(_counterName);
Expand Down Expand Up @@ -369,8 +364,6 @@ public void Close()
/// </summary>
public static void CloseSharedResources()
{
PerformanceCounterPermission permission = new PerformanceCounterPermission(PerformanceCounterPermissionAccess.Read, ".", "*");
permission.Demand();
PerformanceCounterLib.CloseAllLibraries();
}

Expand Down Expand Up @@ -466,7 +459,6 @@ private void Initialize()
private void InitializeImpl()
{
bool tookLock = false;
RuntimeHelpers.PrepareConstrainedRegions();
try
{
Monitor.Enter(InstanceLockObject, ref tookLock);
Expand All @@ -483,10 +475,6 @@ private void InitializeImpl()

if (ReadOnly)
{
PerformanceCounterPermission permission = new PerformanceCounterPermission(PerformanceCounterPermissionAccess.Read, currentMachineName, currentCategoryName);

permission.Demand();

if (!PerformanceCounterLib.CounterExists(currentMachineName, currentCategoryName, _counterName))
throw new InvalidOperationException(SR.Format(SR.CounterExists, currentCategoryName, _counterName));

Expand All @@ -509,9 +497,6 @@ private void InitializeImpl()
}
else
{
PerformanceCounterPermission permission = new PerformanceCounterPermission(PerformanceCounterPermissionAccess.Write, currentMachineName, currentCategoryName);
permission.Demand();

if (currentMachineName != "." && !string.Equals(currentMachineName, PerformanceCounterLib.ComputerName, StringComparison.OrdinalIgnoreCase))
throw new InvalidOperationException(SR.Format(SR.RemoteWriting));

Expand Down Expand Up @@ -556,9 +541,6 @@ public CounterSample NextSample()
string currentCategoryName = _categoryName;
string currentMachineName = _machineName;

PerformanceCounterPermission permission = new PerformanceCounterPermission(PerformanceCounterPermissionAccess.Read, currentMachineName, currentCategoryName);
permission.Demand();

Initialize();
CategorySample categorySample = PerformanceCounterLib.GetCategorySample(currentMachineName, currentCategoryName);
CounterDefinitionSample counterSample = categorySample.GetCounterDefinitionSample(_counterName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,6 @@ public PerformanceCounterCategory(string categoryName, string machineName)
if (!SyntaxCheck.CheckMachineName(machineName))
throw new ArgumentException(SR.Format(SR.InvalidParameter, nameof(machineName), machineName), nameof(machineName));

PerformanceCounterPermission permission = new PerformanceCounterPermission(PerformanceCounterPermissionAccess.Read, machineName, categoryName);
permission.Demand();

_categoryName = categoryName;
_machineName = machineName;
}
Expand All @@ -82,9 +79,6 @@ public string CategoryName
// checks depend on both pieces of info.
lock (this)
{
PerformanceCounterPermission permission = new PerformanceCounterPermission(PerformanceCounterPermissionAccess.Read, _machineName, value);
permission.Demand();

_categoryName = value;
}
}
Expand Down Expand Up @@ -147,12 +141,6 @@ public string MachineName
// checks depend on both pieces of info.
lock (this)
{
if (_categoryName != null)
{
PerformanceCounterPermission permission = new PerformanceCounterPermission(PerformanceCounterPermissionAccess.Read, value, _categoryName);
permission.Demand();
}

_machineName = value;
}
}
Expand Down Expand Up @@ -197,9 +185,6 @@ public static bool CounterExists(string counterName, string categoryName, string
if (!SyntaxCheck.CheckMachineName(machineName))
throw new ArgumentException(SR.Format(SR.InvalidParameter, nameof(machineName), machineName), nameof(machineName));

PerformanceCounterPermission permission = new PerformanceCounterPermission(PerformanceCounterPermissionAccess.Read, machineName, categoryName);
permission.Demand();

return PerformanceCounterLib.CounterExists(machineName, categoryName, counterName);
}

Expand Down Expand Up @@ -243,11 +228,7 @@ public static PerformanceCounterCategory Create(string categoryName, string cate
}
string machineName = ".";

PerformanceCounterPermission permission = new PerformanceCounterPermission(PerformanceCounterPermissionAccess.Administer, machineName, categoryName);
permission.Demand();

Mutex mutex = null;
RuntimeHelpers.PrepareConstrainedRegions();
try
{
SharedUtils.EnterMutex(PerfMutexName, ref mutex);
Expand Down Expand Up @@ -401,13 +382,9 @@ public static void Delete(string categoryName)
CheckValidCategory(categoryName);
string machineName = ".";

PerformanceCounterPermission permission = new PerformanceCounterPermission(PerformanceCounterPermissionAccess.Administer, machineName, categoryName);
permission.Demand();

categoryName = categoryName.ToLower(CultureInfo.InvariantCulture);

Mutex mutex = null;
RuntimeHelpers.PrepareConstrainedRegions();
try
{
SharedUtils.EnterMutex(PerfMutexName, ref mutex);
Expand Down Expand Up @@ -451,9 +428,6 @@ public static bool Exists(string categoryName, string machineName)
if (!SyntaxCheck.CheckMachineName(machineName))
throw new ArgumentException(SR.Format(SR.InvalidParameter, nameof(machineName), machineName), nameof(machineName));

PerformanceCounterPermission permission = new PerformanceCounterPermission(PerformanceCounterPermissionAccess.Read, machineName, categoryName);
permission.Demand();

if (PerformanceCounterLib.IsCustomCategory(machineName, categoryName))
return true;

Expand All @@ -466,9 +440,6 @@ public static bool Exists(string categoryName, string machineName)
/// <internalonly/>
internal static string[] GetCounterInstances(string categoryName, string machineName)
{
PerformanceCounterPermission permission = new PerformanceCounterPermission(PerformanceCounterPermissionAccess.Read, machineName, categoryName);
permission.Demand();

CategorySample categorySample = PerformanceCounterLib.GetCategorySample(machineName, categoryName);
if (categorySample._instanceNameTable.Count == 0)
return Array.Empty<string>();
Expand Down Expand Up @@ -530,9 +501,6 @@ public static PerformanceCounterCategory[] GetCategories(string machineName)
if (!SyntaxCheck.CheckMachineName(machineName))
throw new ArgumentException(SR.Format(SR.InvalidParameter, nameof(machineName), machineName), nameof(machineName));

PerformanceCounterPermission permission = new PerformanceCounterPermission(PerformanceCounterPermissionAccess.Read, machineName, "*");
permission.Demand();

string[] categoryNames = PerformanceCounterLib.GetCategories(machineName);
PerformanceCounterCategory[] categories = new PerformanceCounterCategory[categoryNames.Length];
for (int index = 0; index < categories.Length; index++)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

using System.Runtime.InteropServices;
using System.Globalization;
using System.Security.Permissions;
using System.Security;
using System.Text;
using System.Threading;
Expand Down Expand Up @@ -210,19 +209,12 @@ private static string IniFilePath
{
if (s_iniFilePath == null)
{
// Need to assert Environment permissions here
// the environment check is not exposed as a public
// method
EnvironmentPermission environmentPermission = new EnvironmentPermission(PermissionState.Unrestricted);
environmentPermission.Assert();
try
{
s_iniFilePath = Path.GetTempFileName();
}
finally
{
EnvironmentPermission.RevertAssert();
}
{ }
}
}
}
Expand Down Expand Up @@ -261,24 +253,14 @@ private static string SymbolFilePath
{
string tempPath;

EnvironmentPermission environmentPermission = new EnvironmentPermission(PermissionState.Unrestricted);
environmentPermission.Assert();
tempPath = Path.GetTempPath();
EnvironmentPermission.RevertAssert();

// We need both FileIOPermission EvironmentPermission
PermissionSet ps = new PermissionSet(PermissionState.None);
ps.AddPermission(new EnvironmentPermission(PermissionState.Unrestricted));
ps.AddPermission(new FileIOPermission(FileIOPermissionAccess.Write, tempPath));
ps.Assert();
try
{
s_symbolFilePath = Path.GetTempFileName();
}
finally
{
PermissionSet.RevertAssert();
}
{ }
}
}
}
Expand Down Expand Up @@ -406,9 +388,6 @@ private bool CounterExists(string category, string counter, ref bool categoryExi

private static void CreateIniFile(string categoryName, string categoryHelp, CounterCreationDataCollection creationData, string[] languageIds)
{
//SECREVIEW: PerformanceCounterPermission must have been demanded before
FileIOPermission permission = new FileIOPermission(PermissionState.Unrestricted);
permission.Assert();
try
{
StreamWriter iniWriter = new StreamWriter(IniFilePath, false, Encoding.Unicode);
Expand Down Expand Up @@ -501,9 +480,7 @@ private static void CreateIniFile(string categoryName, string categoryHelp, Coun
}
}
finally
{
FileIOPermission.RevertAssert();
}
{ }
}

private static void CreateRegistryEntry(string categoryName, PerformanceCounterCategoryType categoryType, CounterCreationDataCollection creationData, ref bool iniRegistered)
Expand All @@ -512,11 +489,6 @@ private static void CreateRegistryEntry(string categoryName, PerformanceCounterC
RegistryKey serviceKey = null;
RegistryKey linkageKey = null;

//SECREVIEW: Whoever is able to call this function, must already
// have demmanded PerformanceCounterPermission
// we can therefore assert the RegistryPermission.
RegistryPermission registryPermission = new RegistryPermission(PermissionState.Unrestricted);
registryPermission.Assert();
try
{
serviceParentKey = Registry.LocalMachine.OpenSubKey(ServicePath, true);
Expand Down Expand Up @@ -565,16 +537,11 @@ private static void CreateRegistryEntry(string categoryName, PerformanceCounterC

if (serviceParentKey != null)
serviceParentKey.Close();

RegistryPermission.RevertAssert();
}
}

private static void CreateSymbolFile(CounterCreationDataCollection creationData)
{
//SECREVIEW: PerformanceCounterPermission must have been demanded before
FileIOPermission permission = new FileIOPermission(PermissionState.Unrestricted);
permission.Assert();
try
{
StreamWriter symbolWriter = new StreamWriter(SymbolFilePath);
Expand Down Expand Up @@ -604,20 +571,13 @@ private static void CreateSymbolFile(CounterCreationDataCollection creationData)
}
}
finally
{
FileIOPermission.RevertAssert();
}
{ }
}

private static void DeleteRegistryEntry(string categoryName)
{
RegistryKey serviceKey = null;

//SECREVIEW: Whoever is able to call this function, must already
// have demmanded PerformanceCounterPermission
// we can therefore assert the RegistryPermission.
RegistryPermission registryPermission = new RegistryPermission(PermissionState.Unrestricted);
registryPermission.Assert();
try
{
serviceKey = Registry.LocalMachine.OpenSubKey(ServicePath, true);
Expand Down Expand Up @@ -646,8 +606,6 @@ private static void DeleteRegistryEntry(string categoryName)
{
if (serviceKey != null)
serviceKey.Close();

RegistryPermission.RevertAssert();
}
}

Expand Down Expand Up @@ -757,7 +715,6 @@ internal bool FindCustomCategory(string category, out PerformanceCounterCategory
key.Close();
if (baseKey != null)
baseKey.Close();
PermissionSet.RevertAssert();
}
}
return false;
Expand Down Expand Up @@ -991,19 +948,10 @@ private string GetCounterHelp(string category, string counter, ref bool category
return help;
}

internal string GetCounterName(int index)
{
if (NameTable.ContainsKey(index))
return (string)NameTable[index];

return "";
}

private static string[] GetLanguageIds()
{
RegistryKey libraryParentKey = null;
string[] ids = Array.Empty<string>();
new RegistryPermission(PermissionState.Unrestricted).Assert();
try
{
libraryParentKey = Registry.LocalMachine.OpenSubKey(PerflibPath);
Expand All @@ -1015,8 +963,6 @@ private static string[] GetLanguageIds()
{
if (libraryParentKey != null)
libraryParentKey.Close();

RegistryPermission.RevertAssert();
}

return ids;
Expand Down Expand Up @@ -1355,7 +1301,6 @@ internal byte[] GetData(string item)
int error = 0;

// no need to revert here since we'll fall off the end of the method
new RegistryPermission(PermissionState.Unrestricted).Assert();
while (waitRetries > 0)
{
try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ namespace System.Configuration {

using System.Collections.Specialized;
using System.Security;
using System.Security.Permissions;

[ConfigurationPermission(SecurityAction.Assert, Unrestricted=true)]

internal static class PrivilegedConfigurationManager {
internal static ConnectionStringSettingsCollection ConnectionStrings {
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
Expand Down
Loading

0 comments on commit 5617d39

Please sign in to comment.