StaticAnalysisTest.scala 7.58 KB
Newer Older
Kunshan Wang's avatar
Kunshan Wang committed
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
package uvm.staticanalysis

import org.scalatest.FlatSpec
import org.scalatest.Matchers
import org.scalatest.exceptions.TestFailedException

import uvm.GlobalBundle
import uvm.TrantientBundle
import uvm.ir.textinput.UIRTextReader
import uvm.utils.IDFactory

class StaticAnalysisTest extends FlatSpec with Matchers {

  def parseText(globalBundle: GlobalBundle, fac: Option[IDFactory]=None)(uir: String): TrantientBundle = {
    val idf = fac.getOrElse(new IDFactory(uvm.refimpl.MicroVM.FIRST_CLIENT_USABLE_ID))
    val r = new UIRTextReader(idf)
    val ir = r.read(new java.io.StringReader(uir), globalBundle)
    ir
  }

  behavior of "StaticAnalyzer"
  
  def shouldWorkFineIn(text: String): Unit = {
    val gb = new GlobalBundle()
    val b = parseText(gb)(text)
    new StaticAnalyzer().checkBundle(b, Some(gb))
  }
  
  def catchExceptionWhenAnalyzing(text: String): Unit = {
    val gb = new GlobalBundle()
    val b = parseText(gb)(text)
    try {
      new StaticAnalyzer().checkBundle(b, Some(gb))
      fail()
    } catch {
      case e: TestFailedException => throw e
      case e: Exception => // expected
        e.printStackTrace()
    }
    
  }

  it should "complain if a struct contains itself" in {
    catchExceptionWhenAnalyzing("""
      .typedef @i64 = int<64>
      .typedef @s = struct<@i64 @i64 @s @i64>
      """)
  }

  it should "complain if a struct contains its parent" in {
    catchExceptionWhenAnalyzing("""
      .typedef @i64 = int<64>
      .typedef @s = struct<@i64 @i64 @t @i64>
      .typedef @t = struct<@i64 @s @i64>
      """)
  }
  
  it should "not complain if the type is recursive on refs" in {
    shouldWorkFineIn("""
      .typedef @i64 = int<64>
      .typedef @s = struct<@i64 @i64 @t @i64>
      .typedef @t = struct<@i64 @refs @i64>
      .typedef @refs = ref<@s>
      """)
  }

  it should "complain if an array type is recursive" in {
    catchExceptionWhenAnalyzing("""
      .typedef @i64 = int<64>
      .typedef @s = struct<@i64 @i64 @a @i64>
      .typedef @a = array<@s 10>
      """)
  }

  it should "complain if a vector type is recursive" in {
    catchExceptionWhenAnalyzing("""
      .typedef @i64 = int<64>
      .typedef @v = vector<@a 2>
      .typedef @a = array<@v 10>
      """)
  }

  it should "complain if a hybrid is contained in other composite types" in {
    catchExceptionWhenAnalyzing("""
      .typedef @i64 = int<64>
      .typedef @s = struct<@i64 @i64 @h @i64>
      .typedef @h = hybrid<@i64 @i64>
      """)
  }

  it should "not complain if a hybrid contains any composite types" in {
    shouldWorkFineIn("""
      .typedef @i64 = int<64>
      .typedef @s = struct<@i64 @i64 @i64>
      .typedef @h = hybrid<@s @s>
      """)
  }

  it should "complain if a function returns void" in {
    catchExceptionWhenAnalyzing("""
      .typedef @void = void
      .funcsig @sig = () -> (@void)
      """)
  }
  
  it should "complain if a hybrid is used for value types" in {
    catchExceptionWhenAnalyzing("""
      .typedef @i8 = int<8>
      .typedef @hybrid = hybrid<@i8>
      .funcsig @sig = (@hybrid) -> ()
      """)
  }

  it should "complain if a weak reference is used for value types" in {
    catchExceptionWhenAnalyzing("""
      .typedef @i8 = int<8>
      .typedef @wr = weakref<@i8>
      .funcsig @sig = (@wr) -> ()
      """)
  }
  
  
  it should "complain if a int literal is used on non-integer or non-pointer types" in {
    catchExceptionWhenAnalyzing("""
      .typedef @double = double
      .const @C <@double> = 100
      """)
  }

  it should "complain if a float literal is used on non-float types" in {
    catchExceptionWhenAnalyzing("""
      .typedef @double = double
      .const @C <@double> = 3.14f
      """)
  }

  it should "complain if a double literal is used on non-double types" in {
    catchExceptionWhenAnalyzing("""
      .typedef @i64 = int<64>
      .const @C <@i64> = 3.14d
      """)
  }

  it should "complain if a NULL literal is used on non-reference types" in {
    catchExceptionWhenAnalyzing("""
      .typedef @i64 = int<64>
      .typedef @ptri64 = uptr<@i64>
      .const @C <@ptri64> = NULL
      """)
  }

  it should "complain if a sequence literal is used on non-composite types" in {
    catchExceptionWhenAnalyzing("""
      .typedef @i64 = int<64>
      .const @C1 <@i64> = 10
      .const @C <@i64> = { @C1 @C1 @C1 }
      """)
  }

  it should "complain if a sequence literal has the wrong arity for structs" in {
    catchExceptionWhenAnalyzing("""
      .typedef @i64 = int<64>
      .typedef @s = struct<@i64 @i64 @i64>
      .const @C1 <@i64> = 10
      .const @C <@s> = { @C1 @C1 @C1 @C1}
      """)
  }

  it should "complain if a sequence literal has the wrong arity for arrays" in {
    catchExceptionWhenAnalyzing("""
      .typedef @i64 = int<64>
      .typedef @a = array<@i64 4>
      .const @C1 <@i64> = 10
      .const @C <@a> = { @C1 @C1 @C1}
      """)
  }

  it should "complain if a sequence literal has the wrong arity for vectors" in {
    catchExceptionWhenAnalyzing("""
      .typedef @i64 = int<64>
      .typedef @v = vector<@i64 4>
      .const @C1 <@i64> = 10
      .const @C <@v> = { @C1 @C1 @C1}
      """)
  }

  it should "complain if a sequence literal is recursive" in {
    catchExceptionWhenAnalyzing("""
      .typedef @i64 = int<64>
      .typedef @s = struct<@i64 @i64 @i64>
      .const @C1 <@i64> = 10
      .const @C <@s> = { @C @C @C }
      """)
  }
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288

  it should "complain if a global cell is void" in {
    catchExceptionWhenAnalyzing("""
      .typedef @void = void
      .global @g <@void>
      """)
  }

  it should "complain if a global cell is hybrid" in {
    catchExceptionWhenAnalyzing("""
      .typedef @i64 = int<64>
      .typedef @h = hybrid<@i64>
      .global @g <@h>
      """)
  }

  it should "complain if an exposed function's cookie is not int64" in {
    catchExceptionWhenAnalyzing("""
      .typedef @i32 = int<32>
      .const @c <@i32> = 42
      .funcsig @sig = () -> ()
      .funcdecl @f <@sig>
      .expose @fe = @f #DEFAULT @c
      """)
  }

  it should "complain if the entry block has the wrong number of arguments" in {
    catchExceptionWhenAnalyzing("""
      .typedef @i32 = int<32>
      .funcsig @sig = (@i32 @i32 @i32) -> ()
      .funcdef @f VERSION %v1 <@sig> {
        %entry(<@i32> %x <@i32> %y):
          RET ()
      }
      """)
  }

  it should "complain if attempt to branch to entry" in {
    catchExceptionWhenAnalyzing("""
      .funcsig @sig = () -> ()
      .funcdef @f VERSION %v1 <@sig> {
        %entry():
          BRANCH %b1()
        %b1():
          BRANCH %entry()
      }
      """)
  }

  it should "complain if branching with the wrong number of arguments" in {
    catchExceptionWhenAnalyzing("""
      .typedef @i32 = int<32>
      .const @1 <@i32> = 1
      .funcsig @sig = () -> ()
      .funcdef @f VERSION %v1 <@sig> {
        %entry():
          BRANCH %b1(@1)
        %b1():
          RET ()
      }
      """)
  }

  it should "complain if an exceptional dest does not have exc param" in {
    catchExceptionWhenAnalyzing("""
      .typedef @i32 = int<32>
      .const @1 <@i32> = 1
      .funcsig @sig = () -> ()
      .funcdef @f VERSION %v1 <@sig> {
        %entry():
          CALL <@sig> @f () EXC(%b1() %b2())
        %b1():
          RET ()
        %b2():
          RET ()
      }
      """)
  }

  it should "complain if a normal dest has exc param" in {
    catchExceptionWhenAnalyzing("""
      .typedef @i32 = int<32>
      .const @1 <@i32> = 1
      .funcsig @sig = () -> ()
      .funcdef @f VERSION %v1 <@sig> {
        %entry():
          CALL <@sig> @f () EXC(%b1() %b2())
        %b1() [%e]:
          RET ()
        %b2() [%e]:
          RET ()
      }
      """)
  }
Kunshan Wang's avatar
Kunshan Wang committed
289
}