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

MemoryDataScanner.scala 5.82 KB
Newer Older
1
2
package uvm.refimpl.mem.scanning

Kunshan Wang's avatar
Kunshan Wang committed
3
4
5
6
7
8
9
import org.slf4j.LoggerFactory

import com.typesafe.scalalogging.Logger
import com.typesafe.scalalogging.StrictLogging

import uvm.Function
import uvm.ir.irbuilder.IRBuilder
10
11
12
import uvm.refimpl._
import uvm.refimpl.itpr._
import uvm.refimpl.mem._
Kunshan Wang's avatar
Kunshan Wang committed
13
import uvm.refimpl.mem.TypeSizes._
14
15
16
17
18
19
import uvm.types._

object MemoryDataScanner extends StrictLogging {

  val paranoiaLogger = Logger(LoggerFactory.getLogger(getClass.getName() + ".paranoia"))

Kunshan Wang's avatar
Kunshan Wang committed
20
21
22
23
  /**
   * Scan an allocation unit. In this implementation, heap objects, alloca cells and global
   * cells all have the same layout.
   */
24
  def scanAllocUnit(objRef: Word, handler: MemoryFieldHandler)(implicit microVM: MicroVM, memorySupport: MemorySupport) {
25
    val tag = HeaderUtils.getTag(objRef)
26
    logger.debug("Obj 0x%x, tag 0x%x".format(objRef, tag))
27
    val ty = HeaderUtils.getType(microVM, tag)
Kunshan Wang's avatar
Kunshan Wang committed
28
29
30
31
32
33
34
35
36
    logger.debug {
      if (ty.isInstanceOf[TypeHybrid]) {
        val len = HeaderUtils.getVarLength(objRef)
        val size = TypeSizes.hybridSizeOf(ty.asInstanceOf[TypeHybrid], len)
        "Type: %s, varLen: %d, hybridSizeOf: %d".format(ty.repr, len, size)
      } else {
        "Type: %s".format(ty.repr)
      }
    }
37
    scanField(ty, objRef, objRef, handler)
38
39
  }

40
  def scanField(ty: Type, objRef: Word, iRef: Word, handler: MemoryFieldHandler)(implicit microVM: MicroVM, memorySupport: MemorySupport) {
Kunshan Wang's avatar
Kunshan Wang committed
41
    def logField(kind: String, toObj: Word): String = "%s field [0x%x + 0x%x] = 0x%x -> 0x%x".format(kind, objRef, iRef - objRef, iRef, toObj)
42
43
    def logIRefField(kind: String, toObj: Word, offset: Word): String = "%s field [0x%x + 0x%x] = 0x%x -> 0x%x + 0x%x = 0x%x".format(
        kind, objRef, iRef - objRef, iRef, toObj, offset, toObj + offset)
44
    ty match {
45
46
47
48
49
      case t: TypeUPtr => {
        val toAddr = memorySupport.loadLong(iRef, false)
        logger.debug(logField("UPtr", toAddr))
        handler.visitUPtrField(objRef, iRef, toAddr)
      }
Kunshan Wang's avatar
Kunshan Wang committed
50
51
52
53
54
      case t: TypeUFuncPtr => {
        val toAddr = memorySupport.loadLong(iRef, false)
        logger.debug(logField("UFuncPtr", toAddr))
        handler.visitUFPField(objRef, iRef, toAddr)
      }
55
      case t: TypeRef => {
56
        val toObj = memorySupport.loadLong(iRef)
Kunshan Wang's avatar
Kunshan Wang committed
57
        logger.debug(logField("Ref", toObj))
58
        handler.visitRefField(objRef, iRef, toObj, false)
59
60
      }
      case t: TypeIRef => {
61
        val toObj = memorySupport.loadLong(iRef)
62
63
        val offset = memorySupport.loadLong(iRef + WORD_SIZE_BYTES)
        logger.debug(logIRefField("IRef", toObj, offset))
64
        handler.visitIRefField(objRef, iRef, toObj, offset)
65
66
      }
      case t: TypeWeakRef => {
67
        val toObj = memorySupport.loadLong(iRef)
Kunshan Wang's avatar
Kunshan Wang committed
68
        logger.debug(logField("WeakRef", toObj))
69
        handler.visitRefField(objRef, iRef, toObj, true)
70
71
      }
      case t: TypeTagRef64 => {
72
        val bits = memorySupport.loadLong(iRef)
73
74
        if (paranoiaLogger.underlying.isDebugEnabled()) {
          paranoiaLogger.debug(s"Tagref bits ${bits}")
Kunshan Wang's avatar
Kunshan Wang committed
75
76
          if (OpHelper.tr64IsFP(bits)) {
            paranoiaLogger.debug("Tagref is FP: %f".format(OpHelper.tr64ToFP(bits)))
77
          } else if (OpHelper.tr64IsInt(bits)) {
Kunshan Wang's avatar
Kunshan Wang committed
78
            paranoiaLogger.debug("Tagref is Int: %d".format(OpHelper.tr64ToInt(bits)))
79
          } else if (OpHelper.tr64IsRef(bits)) {
Kunshan Wang's avatar
Kunshan Wang committed
80
            paranoiaLogger.debug("Tagref is Ref: 0x%x tag: %d".format(OpHelper.tr64ToRef(bits), OpHelper.tr64ToTag(bits)))
81
82
83
84
          }
        }
        if (OpHelper.tr64IsRef(bits)) {
          val toObj = OpHelper.tr64ToRef(bits)
Kunshan Wang's avatar
Kunshan Wang committed
85
          logger.debug(logField("TagRef64", toObj))
86
          handler.visitTagRefField(objRef, iRef, toObj)
87
88
89
90
        }
      }
      case t: TypeStruct => {
        var fieldAddr = iRef
91
        for (fieldTy <- t.fieldTys) {
92
93
          val fieldAlign = TypeSizes.alignOf(fieldTy)
          fieldAddr = TypeSizes.alignUp(fieldAddr, fieldAlign)
94
          scanField(fieldTy, objRef, fieldAddr, handler)
95
96
97
98
99
100
101
102
103
          fieldAddr += TypeSizes.sizeOf(fieldTy)
        }
      }
      case t: TypeArray => {
        val elemTy = t.elemTy
        val elemSize = TypeSizes.sizeOf(elemTy)
        val elemAlign = TypeSizes.alignOf(elemTy)
        var elemAddr = iRef
        for (i <- 0L until t.len) {
104
          scanField(elemTy, objRef, elemAddr, handler)
105
106
107
108
109
110
111
112
113
          elemAddr = TypeSizes.alignUp(elemAddr + elemSize, elemAlign)
        }

      }
      case t: TypeHybrid => {
        val varTy = t.varTy
        val varSize = TypeSizes.sizeOf(varTy)
        val varAlign = TypeSizes.alignOf(varTy)
        val varLength = HeaderUtils.getVarLength(iRef)
Kunshan Wang's avatar
Kunshan Wang committed
114
115
116
117
118
119
120
        var curAddr = iRef
        for (fieldTy <- t.fieldTys) {
          val fieldAlign = TypeSizes.alignOf(fieldTy)
          curAddr = TypeSizes.alignUp(curAddr, fieldAlign)
          scanField(fieldTy, objRef, curAddr, handler)
          curAddr += TypeSizes.sizeOf(fieldTy)
        }
Kunshan Wang's avatar
Kunshan Wang committed
121
        curAddr = TypeSizes.alignUp(curAddr, varAlign)
122
        for (i <- 0L until varLength) {
Kunshan Wang's avatar
Kunshan Wang committed
123
          scanField(varTy, objRef, curAddr, handler)
Kunshan Wang's avatar
Kunshan Wang committed
124
          curAddr = TypeSizes.alignUp(curAddr + varSize, varAlign)
125
126
        }
      }
127
      case t: TypeStackRef => {
128
129
        val obj = memorySupport.loadEntity[InterpreterStack](iRef)
        handler.visitStackRefField(objRef, iRef, obj)
Kunshan Wang's avatar
Kunshan Wang committed
130
      }
131
      case t: TypeFuncRef => {
132
133
134
135
136
137
138
139
140
141
142
        val obj = memorySupport.loadEntity[Function](iRef)
        handler.visitFuncRefField(objRef, iRef, obj)
      }
      case t: TypeThreadRef => {
        val obj = memorySupport.loadEntity[InterpreterThread](iRef)
        handler.visitThreadRefField(objRef, iRef, obj)
      }
      case t: TypeFrameCursorRef => {
        val obj = memorySupport.loadEntity[FrameCursor](iRef)
        handler.visitFCRefField(objRef, iRef, obj)
      }
Kunshan Wang's avatar
Kunshan Wang committed
143
144
145
      case t: TypeIRBuilderRef => {
        val obj = memorySupport.loadEntity[IRBuilder](iRef)
        handler.visitIRBuilderRefField(objRef, iRef, obj)
146
      }
Kunshan Wang's avatar
Kunshan Wang committed
147
      case _ => // Ignore non-reference fields.
148
149
150
    }
  }
}