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

VCST-345: check bitwise for unmanaged dlls #2756

Merged
merged 4 commits into from
Feb 6, 2024
Merged
Changes from 2 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 @@ -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,11 @@ 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 correspondBitwise = !targetFileInfo.Exists || !sourceVersion.Equals(targetVersion)
|| ReplaceBitwiseReason(sourceFilePath, targetFilePath);

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

private bool ReplaceBitwiseReason(string sourceFilePath, string targetFilePath)
{
if (IsManagedLibrary(targetFilePath))
{
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)
{
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;
}

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