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
|
(module
(type $struct (struct i32))
(type $extendedstruct (struct i32 f64))
(type $bytes (array (mut i8)))
(import "fuzzing-support" "log-i32" (func $log (param i32)))
(func "structs"
(local $x (ref null $struct))
(local $y (ref null $struct))
(local.set $x
(struct.new_default_with_rtt $struct
(rtt.canon $struct)
)
)
;; The value is initialized to 0
;; Note: -Oz will optimize all these to constants thanks to Precompute
(call $log
(struct.get $struct 0 (local.get $x))
)
;; Assigning a value works
(struct.set $struct 0
(local.get $x)
(i32.const 42)
)
(call $log
(struct.get $struct 0 (local.get $x))
)
;; References are references, so writing to one's value affects the other's
(local.set $y (local.get $x))
(struct.set $struct 0
(local.get $y)
(i32.const 100)
)
(call $log
(struct.get $struct 0 (local.get $x))
)
(call $log
(struct.get $struct 0 (local.get $y))
)
)
(func "arrays"
(local $x (ref null $bytes))
(local.set $x
(array.new_with_rtt $bytes
(rtt.canon $bytes)
(i32.const 50) ;; size
(i32.const 42) ;; value to splat into the array
)
)
;; The length should be 50
(call $log
(array.len $bytes (local.get $x))
)
;; The value should be 42
(call $log
(array.get_u $bytes (local.get $x) (i32.const 10))
)
;; Write a value that will be truncated into an i8
(array.set $bytes (local.get $x) (i32.const 10) (i32.const 0xff80))
;; The value should be 0x80 (-128 or 128 depending on signed/unsigned)
(call $log
(array.get_u $bytes (local.get $x) (i32.const 10))
)
(call $log
(array.get_s $bytes (local.get $x) (i32.const 10))
)
;; Other items than the one at index 10 are unaffected.
(call $log
(array.get_s $bytes (local.get $x) (i32.const 20))
)
)
(func "rtts"
(local $x (rtt $struct))
(local $y (rtt $extendedstruct))
(local $z (rtt $extendedstruct))
(local $any anyref)
(local.set $x (rtt.canon $struct))
(local.set $y (rtt.sub $extendedstruct (local.get $x)))
(local.set $z (rtt.canon $extendedstruct))
;; Casting null returns null.
(call $log (ref.is_null
(ref.cast $struct (ref.null $struct) (local.get $x))
))
;; Testing null returns 0.
(call $log
(ref.test $struct (ref.null $struct) (local.get $x))
)
;; Testing something completely wrong (struct vs array) returns 0.
(call $log
(ref.test $struct
(array.new_with_rtt $bytes
(rtt.canon $bytes)
(i32.const 10)
(i32.const 20)
)
(local.get $x)
)
)
;; Testing a thing with the same RTT returns 1.
(call $log
(ref.test $struct
(struct.new_default_with_rtt $struct
(local.get $x)
)
(local.get $x)
)
)
;; A bad downcast returns 0: we create a struct, which is not a extendedstruct.
(call $log
(ref.test $extendedstruct
(struct.new_default_with_rtt $struct
(local.get $x)
)
(local.get $z)
)
)
;; Create a extendedstruct with RTT y, and upcast statically to anyref.
(local.set $any
(struct.new_default_with_rtt $extendedstruct
(local.get $y)
)
)
;; Casting to y, the exact same RTT, works.
(call $log
(ref.test $extendedstruct
(local.get $any)
(local.get $y)
)
)
;; Casting to z, another RTT of the same data type, fails.
(call $log
(ref.test $extendedstruct
(local.get $any)
(local.get $z)
)
)
;; Casting to x, the parent of y, works.
(call $log
(ref.test $struct
(local.get $any)
(local.get $x)
)
)
)
(func "br_on_cast"
(local $any anyref)
;; create a simple $struct, store it in an anyref
(local.set $any
(struct.new_default_with_rtt $struct (rtt.canon $struct))
)
(drop
(block $block (result ($ref $struct))
(drop
(block $extendedblock (result (ref $extendedstruct))
(drop
;; second, try to cast our simple $struct to what it is, which will work
(br_on_cast $block $struct
;; first, try to cast our simple $struct to an extended, which will fail
(br_on_cast $extendedblock $extendedstruct
(local.get $any) (rtt.canon $extendedstruct)
)
(rtt.canon $struct)
)
)
(call $log (i32.const -1)) ;; we should never get here
(return)
)
)
(call $log (i32.const -2)) ;; we should never get here either
(return)
)
)
(call $log (i32.const 3)) ;; we should get here
(drop
(block $never (result (ref $extendedstruct))
;; an untaken br_on_cast, with unreachable rtt - so we cannot use the
;; RTT in binaryen IR to find the cast type.
(br_on_cast $never $extendedstruct (ref.null $struct) (unreachable))
(unreachable)
)
)
)
)
|