Fix assertion using the wrong RemillArch for a module

This commit is contained in:
Duncan Ogilvie
2026-05-02 23:58:28 +02:00
parent 0c71152f69
commit e1595bef32
9 changed files with 113 additions and 18 deletions
+17
View File
@@ -0,0 +1,17 @@
## Building
To build the project:
```bash
msbuild.exe Dna.sln > build.log
```
Then check the tail in case of failure.
To build after modifying `Dna.LLVMInterop/dependencies/remill`:
```bash
cmake --build Dna.LLVMInterop/dependencies/build > build.log
```
Then check the tail in case of failure.
@@ -69,7 +69,7 @@ namespace Dna.BinaryTranslator.Lifting
)
{
var ctx = LLVMContextRef.Global;
return Translate(imagebase, arch, "C:\\Users\\colton\\Downloads\\remill-17-semantics", ctx, binaryFunction, fallthroughFromIps, callHandlingKind);
return Translate(imagebase, arch, RemillArch.GetDefaultSemanticsSearchPath(), ctx, binaryFunction, fallthroughFromIps, callHandlingKind);
}
public static (LLVMValueRef function, BlockMapping blockMapping, IReadOnlyList<LiftedSehEntry> filterFunctions) Translate(
@@ -90,7 +90,7 @@ namespace Dna.BinaryTranslator.Lifting
this.imagebase = imagebase;
this.arch = arch;
this.ctx = ctx;
module = RemillUtils.LoadArchSemantics(arch, semanticsPath);
module = arch.GetOrLoadSemantics(semanticsPath);
builder = ctx.CreateBuilder();
this.binaryFunction = binaryFunction;
this.fallthroughFromIps = fallthroughFromIps;
@@ -83,8 +83,6 @@ namespace Dna.BinaryTranslator.Unsafe
while (true)
{
arch = new RemillArch(ctx, RemillOsId.kOSWindows, RemillArchId.kArchAMD64_AVX512);
// Apply recursive descent to disassemble the control flow graph.
// When an unresolvable branch is encountered(call rax, jmp rax, etc.), the callback
// is invoked to check if any known edges exist.
@@ -99,7 +97,7 @@ namespace Dna.BinaryTranslator.Unsafe
// Lift the function using remill.
var encodedCfg = X86CfgEncoder.EncodeCfg(dna.Binary, cfg);
(var liftedFunction, var blockMapping, var filterFunctions) = CfgTranslator.Translate(dna.Binary.BaseAddress, arch, "C:\\Users\\colton\\Downloads\\remill-17-semantics", ctx, new BinaryFunction(encodedCfg, scopeTableTree, solvedTables.AsReadOnly()), fallthroughFromIps, CallHandlingKind.Normal);
(var liftedFunction, var blockMapping, var filterFunctions) = CfgTranslator.Translate(dna.Binary.BaseAddress, arch, RemillArch.GetDefaultSemanticsSearchPath(), ctx, new BinaryFunction(encodedCfg, scopeTableTree, solvedTables.AsReadOnly()), fallthroughFromIps, CallHandlingKind.Normal);
liftedFunction = FunctionIsolator.IsolateFunctionIntoNewModuleWithSehSupport(arch, liftedFunction, filterFunctions.Select(x => x.LiftedFilterFunction).ToList().AsReadOnly()).function;
liftedFunction.GlobalParent.PrintToFile(ArtifactPaths.Resolve("translatedFunction.ll"));
@@ -184,7 +182,7 @@ namespace Dna.BinaryTranslator.Unsafe
var encodedCfg = X86CfgEncoder.EncodeCfg(dna.Binary, cfg);
(var liftedFunction, var blockMapping, var filterFunctions) = CfgTranslator.Translate(dna.Binary.BaseAddress, arch, "C:\\Users\\colton\\Downloads\\remill-17-semantics", ctx, new BinaryFunction(encodedCfg, scopeTableTree, solvedTables.AsReadOnly()), fallthroughFromIps, CallHandlingKind.Normal);
(var liftedFunction, var blockMapping, var filterFunctions) = CfgTranslator.Translate(dna.Binary.BaseAddress, arch, RemillArch.GetDefaultSemanticsSearchPath(), ctx, new BinaryFunction(encodedCfg, scopeTableTree, solvedTables.AsReadOnly()), fallthroughFromIps, CallHandlingKind.Normal);
liftedFunction = FunctionIsolator.IsolateFunctionIntoNewModuleWithSehSupport(arch, liftedFunction, filterFunctions.Select(x => x.LiftedFilterFunction).ToList().AsReadOnly()).function;
liftedFunction = StripRuntime(liftedFunction);
@@ -59,7 +59,7 @@ namespace Dna.BinaryTranslator.VMProtect
var arch = new RemillArch(ctx, RemillOsId.kOSWindows, RemillArchId.kArchAMD64_AVX512);
// Load the remill semantics into a new module.
var module = RemillUtils.LoadArchSemantics(arch, Path.Combine(Directory.GetCurrentDirectory(), "Semantics"));
var module = arch.GetOrLoadSemantics();
// Lift the trace into an LLVM IR function.
var function = TraceLifter.Lift(module, arch, traceInsts);
+3 -3
View File
@@ -364,8 +364,8 @@ var ctx = LLVMContextRef.Create();
Console.WriteLine((int)RemillArchId.kArchAMD64_AVX512);
var bcPath = "C:\\Users\\colton\\Downloads\\remill-17-semantics";
ctx.TryGetBitcodeModule(LlvmUtilities.CreateMemoryBuffer(@"C:\Users\colton\Downloads\remill-17-semantics" + "\\amd64_sleigh.bc"), out LLVMModuleRef theModule, out string msg);
var bcPath = RemillArch.GetDefaultSemanticsSearchPath();
ctx.TryGetBitcodeModule(LlvmUtilities.CreateMemoryBuffer(Path.Combine(bcPath, "amd64_sleigh.bc")), out LLVMModuleRef theModule, out string msg);
theModule.WriteToLlFile("remillModule.ll");
@@ -376,7 +376,7 @@ var arch = new RemillArch(ctx, RemillOsId.kOSWindows, RemillArchId.kArchAMD64);
Console.WriteLine("Loading arch semantics");
var archModule = RemillUtils.LoadArchSemantics(arch, bcPath);
var archModule = arch.GetOrLoadSemantics(bcPath);
Console.WriteLine("Getting reg name.");
Console.WriteLine(arch.StackPointerRegisterName);
Console.WriteLine("Got reg name");
+17
View File
@@ -46,4 +46,21 @@ Then configure:
```bash
cmake -B build -G Ninja -DCMAKE_C_COMPILER=clang-cl -DCMAKE_CXX_COMPILER=clang-cl -DUSE_PRECOMPILED_LLVM=ON
cmake --build build
```
## Configuration
You need to enable long paths:
```ini
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem]
"LongPathsEnabled"=dword:00000001
```
And then enable it in git as well:
```
git config --global core.longpaths true
```
@@ -3,6 +3,7 @@ using Dna.LLVMInterop.API.Remill.Manual;
using LLVMSharp.Interop;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
@@ -19,6 +20,10 @@ namespace Dna.LLVMInterop.API.Remill.Arch
private RemillDecodingContext defaultCtx = null;
private bool hasSemanticsModule;
private LLVMModuleRef semanticsModule;
public unsafe LLVMTypeRef AddressType => NativeRemillArchApi.Arch_AddressType(this);
public unsafe LLVMTypeRef StateStructType => NativeRemillArchApi.Arch_StateStructType(this);
@@ -91,6 +96,25 @@ namespace Dna.LLVMInterop.API.Remill.Arch
public unsafe void PrepareModuleDataLayout(LLVMModuleRef module) => NativeRemillArchApi.Arch_PrepareModuleDataLayout(this, module);
public LLVMModuleRef GetOrLoadSemantics(string? path = null)
{
if (hasSemanticsModule)
return semanticsModule;
semanticsModule = RemillUtils.LoadArchSemantics(this, path ?? GetDefaultSemanticsSearchPath());
hasSemanticsModule = true;
return semanticsModule;
}
public static string GetDefaultSemanticsSearchPath()
{
var configuredPath = Environment.GetEnvironmentVariable("DNA_REMILL_SEMANTICS");
if (!string.IsNullOrWhiteSpace(configuredPath))
return configuredPath;
return Path.Combine(AppContext.BaseDirectory, "remill-semantics");
}
/// <summary>
/// Decodes a single remill instruction.
/// </summary>
+26 -6
View File
@@ -12,14 +12,34 @@ namespace Dna.Utilities
{
public static class ClangCompiler
{
// TODO: Remove hardcoded path.
private const string clangPath = @"C:\Users\colton\source\repos\cxx-common-cmake-win\cxx-common-cmake\build\install\bin\clang.exe";
private static readonly string clangPath = FindLlvmTool("clang.exe");
// TODO: Remove hardcoded path.
private const string objcpyPath = @"C:\Users\colton\source\repos\cxx-common-cmake-win\cxx-common-cmake\build\install\bin\llvm-objcopy.exe";
private static readonly string objcpyPath = FindLlvmTool("llvm-objcopy.exe");
// TODO: Remove hardcoded path.
private const string optPath = @"C:\Users\colton\source\repos\cxx-common-cmake-win\cxx-common-cmake\build\install\bin\opt.exe";
private static readonly string optPath = FindLlvmTool("opt.exe");
private static string FindLlvmTool(string toolName)
{
var envPath = Environment.GetEnvironmentVariable("DNA_LLVM_BIN");
if (!string.IsNullOrWhiteSpace(envPath))
{
var toolPath = Path.Combine(envPath, toolName);
if (File.Exists(toolPath))
return toolPath;
}
var dir = new DirectoryInfo(Directory.GetCurrentDirectory());
while (dir != null)
{
var toolPath = Path.Combine(dir.FullName, "Dna.LLVMInterop", "dependencies", "install", "bin", toolName);
if (File.Exists(toolPath))
return toolPath;
dir = dir.Parent;
}
return toolName;
}
public static unsafe string CompileToWindowsDll(LLVMValueRef targetFunction, string llPath, bool overwrite = false)
{
+21 -2
View File
@@ -9,8 +9,27 @@ namespace Dna.Utilities
{
public static class IDALoader
{
// TODO: Remove hardcoded path.
private const string idaPath = @"C:\Program Files\IDA 7.5\ida64.exe";
private static readonly string idaPath = FindIdaPath();
private static string FindIdaPath()
{
var envPath = Environment.GetEnvironmentVariable("IDA_PATH");
if (!string.IsNullOrWhiteSpace(envPath) && File.Exists(envPath))
return envPath;
var programFiles = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
if (!string.IsNullOrWhiteSpace(programFiles) && Directory.Exists(programFiles))
{
foreach (var idaDir in Directory.EnumerateDirectories(programFiles, "IDA*"))
{
var idaPath = Path.Combine(idaDir, "ida64.exe");
if (File.Exists(idaPath))
return idaPath;
}
}
return "ida64.exe";
}
public static string Load(string exePath, bool overwrite = false)
{