WARNING! Access to this system is limited to authorised users only.
Unauthorised users may be subject to prosecution.
Unauthorised access to this system is a criminal offence under Australian law (Federal Crimes Act 1914 Part VIA)
It is a criminal offence to:
(1) Obtain access to data without authority. -Penalty 2 years imprisonment.
(2) Damage, delete, alter or insert data without authority. -Penalty 10 years imprisonment.
User activity is monitored and recorded. Anyone using this system expressly consents to such monitoring and recording.

To protect your data, the CISO officer has suggested users to enable 2FA as soon as possible.
Currently 2.7% of users enabled 2FA.

nativeHelpers.scala 6.42 KB
Newer Older
Kunshan Wang's avatar
Kunshan Wang committed
1
2
package uvm.refimpl.nat

3
4
5
import java.nio.ByteBuffer
import java.nio.ByteOrder
import org.slf4j.LoggerFactory
Kunshan Wang's avatar
Kunshan Wang committed
6
import com.kenai.jffi.{ Type => JType, Struct => JStruct, Function => JFunction, HeapInvocationBuffer, Invoker }
7
import com.typesafe.scalalogging.Logger
Kunshan Wang's avatar
Kunshan Wang committed
8
9
import uvm.FuncSig
import uvm.refimpl.UvmRefImplException
10
import uvm.refimpl.itpr._
Kunshan Wang's avatar
Kunshan Wang committed
11
import uvm.refimpl.itpr.ValueBox
12
import uvm.refimpl.mem.TypeSizes
Kunshan Wang's avatar
Kunshan Wang committed
13
14
15
16
import uvm.refimpl.mem.TypeSizes.Word
import uvm.types._
import uvm.types.{ Type => MType }
import uvm.utils.LazyPool
17
18
19
20
21
import uvm.utils.HexDump

object NativeHelper {
  val logger = Logger(LoggerFactory.getLogger(getClass.getName))
}
Kunshan Wang's avatar
Kunshan Wang committed
22
23
24
25
26

/**
 * Helps calling native functions. Based on JFFI.
 */
class NativeHelper {
27
28
  import NativeHelper._
  
Kunshan Wang's avatar
Kunshan Wang committed
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
  val jffiTypePool: LazyPool[MType, JType] = LazyPool {
    case TypeVoid()       => JType.VOID
    case TypeInt(8)       => JType.SINT8
    case TypeInt(16)      => JType.SINT16
    case TypeInt(32)      => JType.SINT32
    case TypeInt(64)      => JType.SINT64
    case TypeFloat()      => JType.FLOAT
    case TypeDouble()     => JType.DOUBLE
    case TypeVector(_, _) => throw new UvmRefImplException("Vectors are not implemented in native calls.")
    case TypeStruct(fields) => {
      val fieldsNativeTypes: Seq[JType] = fields.map(jffiTypePool.apply)
      val strType = JStruct.newStruct(fieldsNativeTypes: _*)
      strType
    }
    case _: AbstractPointerType => JType.POINTER
    case t                      => throw new UvmRefImplException("Type %s cannot be used in native calls.".format(t.repr))
  }

  val jffiFuncPool = LazyPool[(FuncSig, Word), JFunction] {
    case (sig, funcAddr) => {
      val jParamTypes = sig.paramTy.map(jffiTypePool.apply)
      val jRetTy = jffiTypePool(sig.retTy)
      new JFunction(funcAddr, jRetTy, jParamTypes: _*)
    }
  }

  private def putArgToBuf(buf: ByteBuffer, off: Int, mty: MType, vb: ValueBox): Unit = {
    mty match {
      case TypeInt(8)   => buf.put(off, vb.asInstanceOf[BoxInt].value.toByte)
      case TypeInt(16)  => buf.putShort(off, vb.asInstanceOf[BoxInt].value.toShort)
      case TypeInt(32)  => buf.putInt(off, vb.asInstanceOf[BoxInt].value.toInt)
      case TypeInt(64)  => buf.putLong(off, vb.asInstanceOf[BoxInt].value.toLong)
      case TypeFloat()  => buf.putFloat(off, vb.asInstanceOf[BoxFloat].value)
      case TypeDouble() => buf.putDouble(off, vb.asInstanceOf[BoxDouble].value)
      case s @ TypeStruct(flds) => {
        val fldvbs = vb.asInstanceOf[BoxStruct].values
        for (((fty, fvb), i) <- (flds zip fldvbs).zipWithIndex) {
          val off2 = TypeSizes.fieldOffsetOf(s, i)
67
          putArgToBuf(buf, off + off2.toInt, fty, fvb)
Kunshan Wang's avatar
Kunshan Wang committed
68
69
70
71
72
73
        }
      }
      case _: AbstractPointerType => buf.putLong(off, vb.asInstanceOf[BoxPointer].addr)
    }
  }

74
75
  private val FORCE_ALIGN_UP = 16L

Kunshan Wang's avatar
Kunshan Wang committed
76
77
78
79
80
81
82
83
84
  private def putArg(hib: HeapInvocationBuffer, mty: MType, vb: ValueBox): Unit = {
    mty match {
      case TypeInt(8)   => hib.putByte(vb.asInstanceOf[BoxInt].value.toByte)
      case TypeInt(16)  => hib.putShort(vb.asInstanceOf[BoxInt].value.toShort)
      case TypeInt(32)  => hib.putInt(vb.asInstanceOf[BoxInt].value.toInt)
      case TypeInt(64)  => hib.putLong(vb.asInstanceOf[BoxInt].value.toLong)
      case TypeFloat()  => hib.putFloat(vb.asInstanceOf[BoxFloat].value)
      case TypeDouble() => hib.putDouble(vb.asInstanceOf[BoxDouble].value)
      case TypeStruct(flds) => {
85
86
        val buf = ByteBuffer.allocate(TypeSizes.alignUp(TypeSizes.sizeOf(mty), FORCE_ALIGN_UP).intValue())
        buf.order(ByteOrder.LITTLE_ENDIAN)
Kunshan Wang's avatar
Kunshan Wang committed
87
        putArgToBuf(buf, 0, mty, vb)
88
        logger.debug("Hexdump:\n" + HexDump.dumpByteBuffer(buf))
Kunshan Wang's avatar
Kunshan Wang committed
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
        hib.putStruct(buf.array(), buf.arrayOffset())
      }
      case _: AbstractPointerType => hib.putAddress(vb.asInstanceOf[BoxPointer].addr)
    }
  }

  def callNative(sig: FuncSig, func: Word, args: Seq[ValueBox], retBox: ValueBox): Unit = {
    val jFunc = jffiFuncPool((sig, func))

    val hib = new HeapInvocationBuffer(jFunc)

    for ((mty, vb) <- (sig.paramTy zip args)) {
      putArg(hib, mty, vb)
    }

    val inv = Invoker.getInstance

    sig.retTy match {
107
108
109
      case TypeVoid() => {
        inv.invokeLong(jFunc, hib)
      }
Kunshan Wang's avatar
Kunshan Wang committed
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
      case TypeInt(8) => {
        val rv = inv.invokeInt(jFunc, hib).toByte
        retBox.asInstanceOf[BoxInt].value = OpHelper.trunc(BigInt(rv), 8)
      }
      case TypeInt(16) => {
        val rv = inv.invokeInt(jFunc, hib).toShort
        retBox.asInstanceOf[BoxInt].value = OpHelper.trunc(BigInt(rv), 16)
      }
      case TypeInt(32) => {
        val rv = inv.invokeInt(jFunc, hib)
        retBox.asInstanceOf[BoxInt].value = OpHelper.trunc(BigInt(rv), 32)
      }
      case TypeInt(64) => {
        val rv = inv.invokeLong(jFunc, hib)
        retBox.asInstanceOf[BoxInt].value = OpHelper.trunc(BigInt(rv), 64)
      }
      case TypeFloat() => {
        val rv = inv.invokeFloat(jFunc, hib)
        retBox.asInstanceOf[BoxFloat].value = rv
      }
      case TypeDouble() => {
        val rv = inv.invokeDouble(jFunc, hib)
        retBox.asInstanceOf[BoxDouble].value = rv
      }
      case TypeStruct(flds) => {
        val rv = inv.invokeStruct(jFunc, hib)
136
137
        val buf = ByteBuffer.wrap(rv).order(ByteOrder.LITTLE_ENDIAN)
        logger.debug("Hexdump:\n" + HexDump.dumpByteBuffer(buf))
Kunshan Wang's avatar
Kunshan Wang committed
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
        getArgFromBuf(buf, 0, sig.retTy, retBox)
      }
      case _: AbstractPointerType => {
        val rv = inv.invokeAddress(jFunc, hib)
        retBox.asInstanceOf[BoxPointer].addr = rv
      }
    }
  }

  private def getArgFromBuf(buf: ByteBuffer, off: Int, mty: MType, vb: ValueBox): Unit = {
    mty match {
      case TypeInt(8)   => vb.asInstanceOf[BoxInt].value = OpHelper.trunc(buf.get(off), 8)
      case TypeInt(16)  => vb.asInstanceOf[BoxInt].value = OpHelper.trunc(buf.getShort(off), 16)
      case TypeInt(32)  => vb.asInstanceOf[BoxInt].value = OpHelper.trunc(buf.getInt(off), 32)
      case TypeInt(64)  => vb.asInstanceOf[BoxInt].value = OpHelper.trunc(buf.getLong(off), 64)
      case TypeFloat()  => vb.asInstanceOf[BoxFloat].value = buf.getFloat(off)
      case TypeDouble() => vb.asInstanceOf[BoxDouble].value = buf.getDouble(off)
      case s @ TypeStruct(flds) => {
        val fldvbs = vb.asInstanceOf[BoxStruct].values
        for (((fty, fvb), i) <- (flds zip fldvbs).zipWithIndex) {
          val off2 = TypeSizes.fieldOffsetOf(s, i)
159
          getArgFromBuf(buf, off + off2.toInt, fty, fvb)
Kunshan Wang's avatar
Kunshan Wang committed
160
161
162
163
164
165
        }
      }
      case _: AbstractPointerType => vb.asInstanceOf[BoxPointer].addr = buf.getLong(off)
    }
  }
}