Skip to content

Added CFG check and standalone flag for .NET binding #2182

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

Merged
merged 1 commit into from
May 10, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions bindings/dotnet/UnicornEngine/Unicorn.fs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ and Unicorn(arch: Int32, mode: Int32, binding: IBinding) =
mem.ToPointer()

do
// check for cfg
if OperatingSystem.IsWindows() then
WinNativeImport.CheckCFG();

// initialize event list
_eventMemMap
|> Seq.map(fun kv -> kv.Key)
Expand Down
2 changes: 2 additions & 0 deletions bindings/dotnet/UnicornEngine/UnicornEngine.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<ProjectGuid>0c21f1c1-2725-4a46-9022-1905f85822a5</ProjectGuid>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<OtherFlags>--standalone</OtherFlags>
</PropertyGroup>

<PropertyGroup>
Expand All @@ -21,6 +22,7 @@
</PropertyGroup>

<ItemGroup>
<Compile Include="WinNativeImport.fs" />
<Compile Include="Const\Arm.fs" />
<Compile Include="Const\Arm64.fs" />
<Compile Include="Const\Common.fs" />
Expand Down
17 changes: 17 additions & 0 deletions bindings/dotnet/UnicornEngine/WinNativeImport.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace UnicornEngine

open System
open System.Runtime.InteropServices

module private WinNativeImport =
module private Imports =
[<DllImport("kernel32.dll")>] extern bool GetProcessMitigationPolicy(IntPtr hProcess, uint32 MitigationPolicy, uint32& Buffer, UIntPtr Length)

let public CheckCFG() =
let CurrentProcess = IntPtr(-1)
let CFGFlag = 7u
let mutable Flags = 0u
let BufferSize = UIntPtr(uint32 sizeof<uint32>)
if Imports.GetProcessMitigationPolicy(CurrentProcess, CFGFlag, &Flags, BufferSize) then
if (Flags &&& 0x1u) <> 0u then
raise <| ApplicationException("Control Flow Guard (CFG) is enabled. Unicorn cannot run with CFG enabled.")
Loading