Skip to content

Commit cb2f418

Browse files
authored
Added src for CFG check and standalone flag (#2182)
Added CFG check on instance creation and added "--standalone" flag
1 parent 59608e7 commit cb2f418

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

bindings/dotnet/UnicornEngine/Unicorn.fs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ and Unicorn(arch: Int32, mode: Int32, binding: IBinding) =
7171
mem.ToPointer()
7272

7373
do
74+
// check for cfg
75+
if OperatingSystem.IsWindows() then
76+
WinNativeImport.CheckCFG();
77+
7478
// initialize event list
7579
_eventMemMap
7680
|> Seq.map(fun kv -> kv.Key)

bindings/dotnet/UnicornEngine/UnicornEngine.fsproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<ProjectGuid>0c21f1c1-2725-4a46-9022-1905f85822a5</ProjectGuid>
1111
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
1212
<GenerateDocumentationFile>true</GenerateDocumentationFile>
13+
<OtherFlags>--standalone</OtherFlags>
1314
</PropertyGroup>
1415

1516
<PropertyGroup>
@@ -21,6 +22,7 @@
2122
</PropertyGroup>
2223

2324
<ItemGroup>
25+
<Compile Include="WinNativeImport.fs" />
2426
<Compile Include="Const\Arm.fs" />
2527
<Compile Include="Const\Arm64.fs" />
2628
<Compile Include="Const\Common.fs" />
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
namespace UnicornEngine
2+
3+
open System
4+
open System.Runtime.InteropServices
5+
6+
module private WinNativeImport =
7+
module private Imports =
8+
[<DllImport("kernel32.dll")>] extern bool GetProcessMitigationPolicy(IntPtr hProcess, uint32 MitigationPolicy, uint32& Buffer, UIntPtr Length)
9+
10+
let public CheckCFG() =
11+
let CurrentProcess = IntPtr(-1)
12+
let CFGFlag = 7u
13+
let mutable Flags = 0u
14+
let BufferSize = UIntPtr(uint32 sizeof<uint32>)
15+
if Imports.GetProcessMitigationPolicy(CurrentProcess, CFGFlag, &Flags, BufferSize) then
16+
if (Flags &&& 0x1u) <> 0u then
17+
raise <| ApplicationException("Control Flow Guard (CFG) is enabled. Unicorn cannot run with CFG enabled.")

0 commit comments

Comments
 (0)