Skip to content

Commit

Permalink
VCST-345: check bitwise for unmanaged dlls (#2756)
Browse files Browse the repository at this point in the history
  • Loading branch information
basilkot committed Feb 6, 2024
1 parent cc091f7 commit 8b81468
Showing 1 changed file with 67 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using VirtoCommerce.Platform.Core.Common;
Expand Down Expand Up @@ -151,7 +152,7 @@ public override void Validate()
.ToArray();
if (installedIncompatibilities.Any())
{
module.Errors.Add($"{ module } is incompatible with installed { string.Join(", ", installedIncompatibilities.Select(x => x.ToString())) }. You should uninstall these modules first.");
module.Errors.Add($"{module} is incompatible with installed {string.Join(", ", installedIncompatibilities.Select(x => x.ToString()))}. You should uninstall these modules first.");
}
}

Expand Down Expand Up @@ -284,7 +285,12 @@ private void CopyFile(string sourceFilePath, string targetFilePath)
}

var versionsAreSameButLaterDate = (sourceVersion == targetVersion && targetFileInfo.Exists && sourceFileInfo.Exists && targetFileInfo.LastWriteTimeUtc < sourceFileInfo.LastWriteTimeUtc);
if (!targetFileInfo.Exists || sourceVersion > targetVersion || versionsAreSameButLaterDate)

var replaceBitwiseReason = targetFileInfo.Exists
&& sourceVersion.Equals(targetVersion)
&& ReplaceBitwiseReason(sourceFilePath, targetFilePath);

if (!targetFileInfo.Exists || sourceVersion > targetVersion || versionsAreSameButLaterDate || replaceBitwiseReason)
{
var targetDirectoryPath = Path.GetDirectoryName(targetFilePath);
Directory.CreateDirectory(targetDirectoryPath);
Expand All @@ -309,6 +315,65 @@ private void CopyFile(string sourceFilePath, string targetFilePath)
}
}

private bool ReplaceBitwiseReason(string sourceFilePath, string targetFilePath)
{
if (IsManagedLibrary(targetFilePath) || !sourceFilePath.EndsWith(".dll", StringComparison.InvariantCultureIgnoreCase))
{
return false;
}

var currentIs64 = Environment.Is64BitProcess;
var targetIs64 = !Is32Bitwise(targetFilePath);

if (currentIs64 == targetIs64)
{
return false;
}

var sourceIs64 = !Is32Bitwise(sourceFilePath);
if (currentIs64 == sourceIs64)
{
return true;
}

return false;
}

private bool IsManagedLibrary(string pathToDll)
{
try
{
AssemblyName.GetAssemblyName(pathToDll);
return true;
}
catch
{
// file is unmanaged
}

return false;
}

private bool Is32Bitwise(string dllPath)
{
try
{
using var fs = new FileStream(dllPath, FileMode.Open, FileAccess.Read);
using var br = new BinaryReader(fs);
fs.Seek(0x3c, SeekOrigin.Begin);
var peOffset = br.ReadInt32();

fs.Seek(peOffset, SeekOrigin.Begin);
var peHead = br.ReadUInt32();

return peHead == 0x00004550 && br.ReadUInt16() == 0x14c;
}
catch (EndOfStreamException exception)
{
throw new PlatformException($"Failed to read file '{dllPath}'.", exception);
}
}

private bool IsAssemblyRelatedFile(string path)
{
return _options.AssemblyFileExtensions.Union(_options.AssemblyServiceFileExtensions).Any(x => path.EndsWith(x, StringComparison.OrdinalIgnoreCase));
Expand Down

0 comments on commit 8b81468

Please sign in to comment.