From bb1215a955eabf6a65f82b80c4f1f40bb8da70a9 Mon Sep 17 00:00:00 2001 From: Mike McLaughlin Date: Thu, 8 Sep 2022 14:07:10 -0700 Subject: [PATCH 1/3] Enable int overflow/underflow checking --- src/Directory.Build.props | 4 ++-- .../ImageMappingMemoryService.cs | 2 +- .../ModuleService.cs | 2 +- .../ModuleServiceFromDataReader.cs | 4 ++-- .../Utilities.cs | 2 ++ 5 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/Directory.Build.props b/src/Directory.Build.props index 0e76e8af5f..c901bc12d0 100644 --- a/src/Directory.Build.props +++ b/src/Directory.Build.props @@ -11,11 +11,11 @@ - false + true - false + true diff --git a/src/Microsoft.Diagnostics.DebugServices.Implementation/ImageMappingMemoryService.cs b/src/Microsoft.Diagnostics.DebugServices.Implementation/ImageMappingMemoryService.cs index bc994a00ce..4a2ae83e94 100644 --- a/src/Microsoft.Diagnostics.DebugServices.Implementation/ImageMappingMemoryService.cs +++ b/src/Microsoft.Diagnostics.DebugServices.Implementation/ImageMappingMemoryService.cs @@ -267,7 +267,7 @@ private void ApplyRelocations(IModule module, PEReader reader, int dataVA, byte[ if ((offset + sizeof(uint)) <= data.Length) { uint value = BitConverter.ToUInt32(data, offset); - value += (uint)baseDelta; + value += unchecked((uint)baseDelta); byte[] source = BitConverter.GetBytes(value); Array.Copy(source, 0, data, offset, source.Length); } diff --git a/src/Microsoft.Diagnostics.DebugServices.Implementation/ModuleService.cs b/src/Microsoft.Diagnostics.DebugServices.Implementation/ModuleService.cs index b69ac6312b..bd437e85e7 100644 --- a/src/Microsoft.Diagnostics.DebugServices.Implementation/ModuleService.cs +++ b/src/Microsoft.Diagnostics.DebugServices.Implementation/ModuleService.cs @@ -494,7 +494,7 @@ internal bool Read(ulong address, byte[] buffer, int bufferSize, out int bytesRe return _memoryService.ReadMemory(address, buffer, bufferSize, out bytesRead); } - if (!_cacheValid || (address < _startCache) || (address > (_startCache + (ulong)(_cacheSize - bufferSize)))) + if (!_cacheValid || (address < _startCache) || (address > (_startCache + (ulong)_cacheSize - (ulong)bufferSize))) { _cacheValid = false; _startCache = address; diff --git a/src/Microsoft.Diagnostics.DebugServices.Implementation/ModuleServiceFromDataReader.cs b/src/Microsoft.Diagnostics.DebugServices.Implementation/ModuleServiceFromDataReader.cs index 60f6dc7138..0ad0019c47 100644 --- a/src/Microsoft.Diagnostics.DebugServices.Implementation/ModuleServiceFromDataReader.cs +++ b/src/Microsoft.Diagnostics.DebugServices.Implementation/ModuleServiceFromDataReader.cs @@ -47,9 +47,9 @@ public ModuleFromDataReader(ModuleServiceFromDataReader moduleService, int modul public override ulong ImageSize => _imageSize; - public override uint? IndexFileSize => _moduleInfo.IndexTimeStamp == InvalidTimeStamp ? null : (uint)_moduleInfo.IndexFileSize; + public override uint? IndexFileSize => _moduleInfo.IndexTimeStamp == InvalidTimeStamp ? null : unchecked((uint)_moduleInfo.IndexFileSize); - public override uint? IndexTimeStamp => _moduleInfo.IndexTimeStamp == InvalidTimeStamp ? null : (uint)_moduleInfo.IndexTimeStamp; + public override uint? IndexTimeStamp => _moduleInfo.IndexTimeStamp == InvalidTimeStamp ? null : unchecked((uint)_moduleInfo.IndexTimeStamp); public override ImmutableArray BuildId { diff --git a/src/Microsoft.Diagnostics.DebugServices.Implementation/Utilities.cs b/src/Microsoft.Diagnostics.DebugServices.Implementation/Utilities.cs index 0876574724..6eba2ce406 100644 --- a/src/Microsoft.Diagnostics.DebugServices.Implementation/Utilities.cs +++ b/src/Microsoft.Diagnostics.DebugServices.Implementation/Utilities.cs @@ -127,6 +127,7 @@ public static object InvokeConstructor(Type type, IServiceProvider provider, boo } catch (TargetInvocationException ex) { + Trace.TraceError(ex.ToString()); throw ex.InnerException; } } @@ -148,6 +149,7 @@ public static object Invoke(MethodBase method, object instance, IServiceProvider } catch (TargetInvocationException ex) { + Trace.TraceError(ex.ToString()); throw ex.InnerException; } } From 696315621f1bd2eb904558036c7b66cd02424062 Mon Sep 17 00:00:00 2001 From: Mike McLaughlin Date: Fri, 9 Sep 2022 11:49:32 -0700 Subject: [PATCH 2/3] Fix SOS test failure --- .../ImageMappingMemoryService.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.Diagnostics.DebugServices.Implementation/ImageMappingMemoryService.cs b/src/Microsoft.Diagnostics.DebugServices.Implementation/ImageMappingMemoryService.cs index 4a2ae83e94..605b19862c 100644 --- a/src/Microsoft.Diagnostics.DebugServices.Implementation/ImageMappingMemoryService.cs +++ b/src/Microsoft.Diagnostics.DebugServices.Implementation/ImageMappingMemoryService.cs @@ -267,7 +267,7 @@ private void ApplyRelocations(IModule module, PEReader reader, int dataVA, byte[ if ((offset + sizeof(uint)) <= data.Length) { uint value = BitConverter.ToUInt32(data, offset); - value += unchecked((uint)baseDelta); + unchecked { value += (uint)baseDelta; } byte[] source = BitConverter.GetBytes(value); Array.Copy(source, 0, data, offset, source.Length); } From 94bfbe4637d18961c7b4401fcc1d6b17d39e57ff Mon Sep 17 00:00:00 2001 From: Mike McLaughlin Date: Fri, 9 Sep 2022 15:08:27 -0700 Subject: [PATCH 3/3] Fix x86 SOS test failures --- .../ImageMappingMemoryService.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.Diagnostics.DebugServices.Implementation/ImageMappingMemoryService.cs b/src/Microsoft.Diagnostics.DebugServices.Implementation/ImageMappingMemoryService.cs index 605b19862c..1ceffe6044 100644 --- a/src/Microsoft.Diagnostics.DebugServices.Implementation/ImageMappingMemoryService.cs +++ b/src/Microsoft.Diagnostics.DebugServices.Implementation/ImageMappingMemoryService.cs @@ -225,7 +225,7 @@ private void ApplyRelocations(IModule module, PEReader reader, int dataVA, byte[ PEMemoryBlock relocations = reader.GetSectionData(".reloc"); if (relocations.Length > 0) { - ulong baseDelta = module.ImageBase - reader.PEHeaders.PEHeader.ImageBase; + ulong baseDelta = unchecked(module.ImageBase - reader.PEHeaders.PEHeader.ImageBase); #if TRACE_VERBOSE Trace.TraceInformation("ApplyRelocations: dataVA {0:X8} dataCB {1} baseDelta: {2:X16}", dataVA, data.Length, baseDelta); #endif