To protect your data, the CISO officer has suggested users to enable GitLab 2FA as soon as possible.

UvmBundleTesterBase.scala 3.21 KB
Newer Older
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
package uvm.refimpl

import org.scalatest._
import java.io.FileReader
import uvm._
import uvm.types._
import uvm.ssavariables._
import uvm.refimpl._
import uvm.refimpl.itpr._
import uvm.ssavariables.MemoryOrder._
import uvm.ssavariables.AtomicRMWOptr._
import uvm.refimpl.mem.TypeSizes.Word
import com.typesafe.scalalogging.Logger
import org.slf4j.LoggerFactory
import ch.qos.logback.classic.{ Logger => LLogger }
import ch.qos.logback.classic.Level

object UvmBundleTesterBase {
  val logger = Logger(LoggerFactory.getLogger(getClass.getName))
}

abstract class UvmBundleTesterBase extends FlatSpec with Matchers {
  val ROOT_LOGGER_NAME = org.slf4j.Logger.ROOT_LOGGER_NAME

  def setLogLevels(settings: (String, Level)*): Unit = { // Configure logger
    import org.slf4j.LoggerFactory
    import org.slf4j.{ Logger => SLogger }
    import ch.qos.logback.classic.{ Logger => LLogger, Level }
    import ch.qos.logback.classic.Level._

    def setLevel(name: String, level: Level): Unit = {
      LoggerFactory.getLogger(name).asInstanceOf[LLogger].setLevel(level)
    }

    for ((name, lvl) <- settings) {
      setLevel(name, lvl)
    }
  }

  def makeMicroVM(): MicroVM = new MicroVM()

  val microVM = makeMicroVM()

  implicit def idOf(name: String): Int = microVM.idOf(name)
  implicit def nameOf(id: Int): String = microVM.nameOf(id)

  def preloadBundles(fileNames: String*): Unit = {
    val ca = microVM.newClientAgent()

    for (fn <- fileNames) {
      val r = new FileReader(fn)
      ca.loadBundle(r)
      r.close()
    }

    ca.close()
  }

  type TrapHandlerFunction = (ClientAgent, Handle, Handle, Int) => TrapHandlerResult

  class MockTrapHandler(thf: TrapHandlerFunction) extends TrapHandler {
    def handleTrap(ca: ClientAgent, thread: Handle, stack: Handle, watchPointID: Int): TrapHandlerResult = {
      thf(ca, thread, stack, watchPointID)
    }
  }

  def testFunc(ca: ClientAgent, func: Handle, args: Seq[Handle])(handler: TrapHandlerFunction): Unit = {
    microVM.trapManager.trapHandler = new MockTrapHandler(handler)
    val hStack = ca.newStack(func, args)
    val hThread = ca.newThread(hStack)
    microVM.threadStackManager.joinAll()
  }

  implicit class MagicalBox(vb: ValueBox) {
    def asInt: BigInt = vb.asInstanceOf[BoxInt].value
    def asSInt(l: Int): BigInt = OpHelper.prepareSigned(vb.asInstanceOf[BoxInt].value, l)
    def asUInt(l: Int): BigInt = OpHelper.prepareUnsigned(vb.asInstanceOf[BoxInt].value, l)
    def asFloat: Float = vb.asInstanceOf[BoxFloat].value
    def asDouble: Double = vb.asInstanceOf[BoxDouble].value
    def asRef: Word = vb.asInstanceOf[BoxRef].objRef
    def asIRef: (Word, Word) = { val b = vb.asInstanceOf[BoxIRef]; (b.objRef, b.offset) }
    def asIRefAddr: Word = { val b = vb.asInstanceOf[BoxIRef]; b.objRef + b.offset }
    def asStruct: Seq[ValueBox] = vb.asInstanceOf[BoxStruct].values
    def asFunc: Option[Function] = vb.asInstanceOf[BoxFunc].func
    def asThread: Option[InterpreterThread] = vb.asInstanceOf[BoxThread].thread
    def asStack: Option[InterpreterStack] = vb.asInstanceOf[BoxStack].stack
    def asTR64Box: BoxTagRef64 = vb.asInstanceOf[BoxTagRef64]
    def asTR64Raw: Long = vb.asInstanceOf[BoxTagRef64].raw
    def asVec: Seq[ValueBox] = vb.asInstanceOf[BoxVector].values
  }
}