Skip to content

Commit

Permalink
Added Mips samples
Browse files Browse the repository at this point in the history
  • Loading branch information
FICTURE7 committed Apr 29, 2018
1 parent d1cd988 commit d22ed52
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 14 deletions.
22 changes: 14 additions & 8 deletions .vscode/launch.json
@@ -1,23 +1,29 @@
{
"version": "0.2.0",
"configurations": [
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Launch (console)",
"name": "Unicorn.Net.ConsoleTests",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"preLaunchTask": "build Unicorn.Net.ConsoleTests",
"program": "${workspaceFolder}/src/Unicorn.Net.ConsoleTests/bin/Debug/netcoreapp2.0/Unicorn.Net.ConsoleTests.dll",
"args": [],
"cwd": "${workspaceFolder}/src/Unicorn.Net.ConsoleTests",
"console": "internalConsole",
"stopAtEntry": false,
"console": "internalConsole",
"internalConsoleOptions": "openOnSessionStart"
},
{
"name": ".NET Core Attach",
"name": "Unicorn.Net.Samples.Mips",
"type": "coreclr",
"request": "attach",
"processId": "${command:pickProcess}"
"request": "launch",
"preLaunchTask": "build Unicorn.Net.Samples.Mips",
"program": "${workspaceRoot}/samples/Unicorn.Net.Samples.Mips/bin/Debug/netcoreapp2.0/Unicorn.Net.Samples.Mips.dll",
"args": [],
"cwd": "${workspaceRoot}/samples/Unicorn.Net.Samples.Mips",
"stopAtEntry": false,
"console": "internalConsole",
"internalConsoleOptions": "openOnSessionStart"
}
]
}
38 changes: 37 additions & 1 deletion .vscode/tasks.json
Expand Up @@ -2,14 +2,50 @@
"version": "2.0.0",
"tasks": [
{
"label": "build",
"label": "build Unicorn.Net",
"command": "dotnet",
"type": "process",
"args": [
"build",
"-f",
"netstandard2.0",
"${workspaceFolder}/src/Unicorn.Net/Unicorn.Net.csproj"
],
"problemMatcher": "$msCompile"
},
{
"label": "build Unicorn.Net.ConsoleTests",
"command": "dotnet",
"type": "process",
"args": [
"build",
"-f",
"netcoreapp2.0",
"${workspaceFolder}/src/Unicorn.Net.ConsoleTests/Unicorn.Net.ConsoleTests.csproj"
],
"problemMatcher": "$msCompile"
},
{
"label": "build Unicorn.Net.Samples.Mips",
"command": "dotnet",
"type": "process",
"args": [
"build",
"-f",
"netcoreapp2.0",
"${workspaceFolder}/samples/Unicorn.Net.Samples.Mips/Unicorn.Net.Samples.Mips.csproj"
],
"problemMatcher": "$msCompile"
},
{
"label": "build all",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}"
],
"problemMatcher": "$msCompile"
}
]
}
2 changes: 1 addition & 1 deletion Unicorn.Net.sln
Expand Up @@ -17,7 +17,7 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Unicorn.Net.Samples.Mips",
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Unicorn.Net.Samples.Shellcode", "samples\Unicorn.Net.Samples.Shellcode\Unicorn.Net.Samples.Shellcode.csproj", "{7366613D-0132-42DF-BC16-899259FCA10B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Unicorn.Net.Samples.x86", "samples\Unicorn.Net.Samples.x86\Unicorn.Net.Samples.x86.csproj", "{2B3D5780-B146-49D6-B855-4088897DF742}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Unicorn.Net.Samples.X86", "samples\Unicorn.Net.Samples.X86\Unicorn.Net.Samples.X86.csproj", "{2B3D5780-B146-49D6-B855-4088897DF742}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
77 changes: 74 additions & 3 deletions samples/Unicorn.Net.Samples.Mips/Program.cs
@@ -1,15 +1,86 @@
using Unicorn.Mips;
using System;
using Unicorn.Mips;

namespace Unicorn.Net.Samples.Mips
{
// Similar to
// samples/sample_mips.c

public static class Program
{
public static void Main(string[] args)
// test_mips_eb
public static void TestMipsEb()
{
using (var emulator = new MipsEmulator(MipsMode.III))
Console.WriteLine("Emulate MIPS code (big-endian)");

using (var emulator = new MipsEmulator(MipsMode.b32 | MipsMode.BigEndian))
{
ulong addr = 0x10000;
byte[] mipscode =
{
0x34, 0x21, 0x34, 0x56
};

emulator.Memory.Map(addr, 2 * 1024 * 1024, MemoryPermissions.All);
emulator.Memory.Write(addr, mipscode, mipscode.Length);

emulator.Registers.AT = 0x6789;
// or
// emulator.Registers._1 = 0x6789

emulator.Hooks.Block.Add(BlockHook, null);
emulator.Hooks.Code.Add(CodeHook, addr, addr, null);
emulator.Start(addr, addr + (ulong)mipscode.Length);

Console.WriteLine(">>> Emulation done. Below is the CPU context");
Console.WriteLine($">>> R1 = 0x{emulator.Registers.AT.ToString("x2")}");
}
}

// test_mips_el
public static void TestMipsEl()
{
Console.WriteLine("===========================");
Console.WriteLine("Emulate MIPS code (little-endian)");

using (var emulator = new MipsEmulator(MipsMode.b32 | MipsMode.LittleEndian))
{
ulong addr = 0x10000;
byte[] mipscode =
{
0x56, 0x34, 0x21, 0x34
};

emulator.Memory.Map(addr, 2 * 1024 * 1024, MemoryPermissions.All);
emulator.Memory.Write(addr, mipscode, mipscode.Length);

emulator.Registers.AT = 0x6789;
// or
// emulator.Registers._1 = 0x6789

emulator.Hooks.Block.Add(BlockHook, null);
emulator.Hooks.Code.Add(CodeHook, addr, addr, null);
emulator.Start(addr, addr + (ulong)mipscode.Length);

Console.WriteLine(">>> Emulation done. Below is the CPU context");
Console.WriteLine($">>> R1 = 0x{emulator.Registers.AT.ToString("x2")}");
}
}

public static void Main(string[] args)
{
TestMipsEb();
TestMipsEl();
}

private static void BlockHook(Emulator emulator, ulong address, int size, object userToken)
{
Console.WriteLine($">>> Tracing basic block at 0x{address.ToString("x2")}, block size = 0x{size.ToString("x2")}");
}

private static void CodeHook(Emulator emulator, ulong address, int size, object userToken)
{
Console.WriteLine($">>> Tracing instruction at 0x{address.ToString("x2")}, instruction size = 0x{size.ToString("x2")}");
}
}
}
23 changes: 22 additions & 1 deletion src/Unicorn.Net.ConsoleTests/Program.cs
@@ -1,5 +1,6 @@
using System;
using Unicorn.Arm;
using Unicorn.Mips;
using Unicorn.X86;

namespace Unicorn.ConsoleTests
Expand All @@ -10,6 +11,25 @@ public static void Main(string[] args)
{
Console.WriteLine("Unicorn version - " + Version.Current);

using (var emulator = new MipsEmulator(MipsMode.b32 | MipsMode.BigEndian))
{
ulong addr = 0x10000;
byte[] mipscode =
{
0x34, 0x21, 0x34, 0x56
};

emulator.Memory.Map(addr, 2 * 1024 * 1024, MemoryPermissions.All);
emulator.Memory.Write(addr, mipscode, mipscode.Length);

emulator.Registers._1 = 0x6789;

emulator.Hooks.Code.Add(CodeHook, null);
emulator.Start(addr, addr + (ulong)mipscode.Length);

Console.WriteLine("{0}", emulator.Registers._1);
}

using (var emulator = new ArmEmulator(ArmMode.Arm))
{
ulong addr = 0x10000;
Expand Down Expand Up @@ -94,7 +114,8 @@ public static void Main(string[] args)

private static void CodeHook(Emulator emulator, ulong address, int size, object userData)
{
var eflags = ((X86Emulator)emulator).Registers.EFLAGS;
var casted = (X86Emulator)emulator;
var eflags = casted.Registers.EFLAGS;

Console.WriteLine($"-> Tracing instruction at 0x{address.ToString("x2")} of size 0x{size.ToString("x2")}.");
Console.WriteLine($"-> EFLAGS = {eflags.ToString("x2")}");
Expand Down
14 changes: 14 additions & 0 deletions src/Unicorn.Net/Mips/MipsMode.cs
Expand Up @@ -5,22 +5,36 @@ namespace Unicorn.Mips
/// </summary>
public enum MipsMode
{
/// <summary>
/// Big endian mode.
/// </summary>
BigEndian = Bindings.Mode.BigEndian,

/// <summary>
/// Little endian mode.
/// </summary>
LittleEndian = Bindings.Mode.LittleEndian,

/// <summary>
/// MicroMips mode.
/// </summary>
Micro = Bindings.Mode.MIPSMicro,

/// <summary>
/// MIPS III ISA mode.
/// </summary>
III = Bindings.Mode.MIPS3,

/// <summary>
/// MIPS32R6 ISA mode.
/// </summary>
b32R6 = Bindings.Mode.MIPS32R6,

/// <summary>
/// MIPS32 ISA mode.
/// </summary>
b32 = Bindings.Mode.MIPS32,

/// <summary>
/// MIPS64 ISA mode.
/// </summary>
Expand Down

0 comments on commit d22ed52

Please sign in to comment.