GitLab will be upgraded on 30 Jan 2023 from 2.00 pm (AEDT) to 3.00 pm (AEDT). During the update, GitLab and Mattermost services will not be available. If you have any concerns with this, please talk to us at N110 (b) CSIT building.

StaticAnalysisTest.scala 5.32 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
195
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 }
      """)
  }
}