-
Notifications
You must be signed in to change notification settings - Fork 7
/
scasm.scala
208 lines (176 loc) · 5.25 KB
/
scasm.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// WARNING! Run only on MacOS 64 bits
import com.sun.jna._
import scala.util.parsing.combinator._
object Scasm {
lazy val unsafe = {
val theUnsafe = classOf[sun.misc.Unsafe].getDeclaredField("theUnsafe")
theUnsafe.setAccessible(true)
theUnsafe.get(null).asInstanceOf[sun.misc.Unsafe]
}
def lebytes(x: Long) = {
import java.nio._
val buffer = ByteBuffer.allocate(java.lang.Long.BYTES)
buffer.order(ByteOrder.LITTLE_ENDIAN)
buffer.putLong(x)
buffer.array
}
val rex = 0x40 + 0x08
sealed trait Instr {
def bytes: Seq[Byte]
}
case class Mov(to: Operand, from: Operand) extends Instr {
def bytes = (to, from) match {
case (RegisterDirect(r1), RegisterDirect(r2)) =>
Seq(rex, 0x89, 0xC0 + (r2.code << 3) + r1.code).map(_.toByte)
case (RegisterDirect(r), Immediate(v)) =>
Seq(rex, 0xb8 + r.code).map(_.toByte) ++ lebytes(v)
case _ => ???
}
}
case class Add(to: Operand, from: Operand) extends Instr {
def bytes = (to, from) match {
case (RegisterDirect(r1), RegisterDirect(r2)) =>
Seq(rex, 0x01, 0xC0 + (r2.code << 3) + r1.code).map(_.toByte)
case _ => ???
}
}
case object Syscall extends Instr {
def bytes = Seq(0x0f, 0x05).map(_.toByte)
}
case object Ret extends Instr {
def bytes = Seq(0xc3).map(_.toByte)
}
sealed trait Register {
def code: Byte
}
case object Rax extends Register {
val code = 0x0.toByte
}
case object Rdx extends Register {
val code = 0x2.toByte
}
case object Rsi extends Register {
val code = 0x6.toByte
}
case object Rdi extends Register {
val code = 0x7.toByte
}
sealed trait Operand
case class RegisterDirect(register: Register) extends Operand
case class Immediate(value: Long) extends Operand
object AsmParser extends RegexParsers {
def register =
( "rax" ^^^ Rax
| "rdx" ^^^ Rdx
| "rsi" ^^^ Rsi
| "rdi" ^^^ Rdi
)
def directRegister = register ^^ RegisterDirect.apply
def immediate = "(0x)?[0-9]+".r ^^ { case v => Immediate(java.lang.Long.decode(v)) }
def operand = directRegister | immediate
def instruction =
( "add" ~> operand ~ "," ~ operand ^^ { case to ~ _ ~ from => Add(to, from) }
| "mov" ~> operand ~ "," ~ operand ^^ { case to ~ _ ~ from => Mov(to, from) }
| "syscall" ^^^ Syscall
| "ret" ^^^ Ret
)
}
implicit class InlineAssembly(val sc: StringContext) {
def asm(args: Any*): List[Instr] = {
val code = sc.parts.zipAll(args, "", "").map { case (x,y) => x + y }.mkString
code
.split("\n")
.map(_.trim)
.filterNot(_.isEmpty)
.map(AsmParser.parse(AsmParser.instruction, _))
.collect {
case AsmParser.Success(instr, _) => instr
}
.toList
}
}
implicit class Instructions(val instr: List[Instr]) {
def dump: String = instr.flatMap(_.bytes).map(b => f"${b & 0xFF}%02x").mkString(" ")
}
def allocateAlignedMemoryPage = {
val pageSize = unsafe.pageSize
val memory = unsafe.allocateMemory(pageSize * 2)
(memory + (pageSize - memory % pageSize), pageSize)
}
def allocate(str: String) = {
val bytes = str.getBytes("utf-8")
val address = unsafe.allocateMemory(bytes.size)
bytes.zipWithIndex.foreach {
case (b, i) => unsafe.putByte(address + i, b)
}
(address, bytes.size)
}
trait Libc extends Library {
def mprotect(addr: Pointer, len: Long, prot: Int): Int
}
object Libc extends Libc {
private val lib = Native.loadLibrary("c", classOf[Libc]).asInstanceOf[Libc]
val PROT_NONE = 0
val PROT_READ = 1
val PROT_WRITE = 2
val PROT_EXEC = 4
def mprotect(addr: Pointer, len: Long, prot: Int) = lib.mprotect(addr, len, prot)
}
def nativeFunction(code: Seq[Instr]) = {
import Libc._
val (address, size) = allocateAlignedMemoryPage
assert(
0 == mprotect(new Pointer(address), size, PROT_READ | PROT_WRITE | PROT_EXEC),
"mprotect failed!"
)
code.flatMap(_.bytes).zipWithIndex.foreach {
case (b, o) => unsafe.putByte(address + o, b)
}
Function.getFunction(new Pointer(address))
}
implicit def intFunction1[A](f: Function): A => Int = { (a: A) =>
f.invokeInt(Array(a.asInstanceOf[AnyRef]))
}
implicit def intFunction2[A,B](f: Function): (A,B) => Int = { (a: A, b: B) =>
f.invokeInt(Array(a.asInstanceOf[AnyRef], b.asInstanceOf[AnyRef]))
}
implicit def voidFunction0(f: Function): () => Unit = { () =>
f.invokeVoid(Array())
}
def main(args: Array[String]) {
val add: (Int,Int) => Int = nativeFunction(
asm"""
mov rax, rdi
add rax, rsi
ret
"""
)
val result = add(5, add(3, 2))
println(result)
def adder(m: Int): Int => Int = {
nativeFunction(
asm"""
mov rax, rdi
mov rsi, $m
add rax, rsi
ret
"""
)
}
val add4 = adder(4)
println(add4(12))
def yoloPrint(msg: String): () => Unit = nativeFunction {
val (pointer, size) = allocate(msg)
asm"""
mov rax, 0x2000004
mov rdx, $size
mov rsi, $pointer
mov rdi, 1
syscall
ret
"""
}
val helloWorld = yoloPrint("Hello, DevoxxFR!\n")
helloWorld()
}
}