| 1 | /* |
| 2 | * Copyright (C) 2015-2017 Apple Inc. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
| 14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
| 17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | |
| 26 | #include "config.h" |
| 27 | #include "B3Validate.h" |
| 28 | |
| 29 | #if ENABLE(B3_JIT) |
| 30 | |
| 31 | #include "AirCode.h" |
| 32 | #include "B3ArgumentRegValue.h" |
| 33 | #include "B3AtomicValue.h" |
| 34 | #include "B3BasicBlockInlines.h" |
| 35 | #include "B3Dominators.h" |
| 36 | #include "B3MemoryValue.h" |
| 37 | #include "B3Procedure.h" |
| 38 | #include "B3SlotBaseValue.h" |
| 39 | #include "B3StackSlot.h" |
| 40 | #include "B3SwitchValue.h" |
| 41 | #include "B3UpsilonValue.h" |
| 42 | #include "B3ValueInlines.h" |
| 43 | #include "B3Variable.h" |
| 44 | #include "B3VariableValue.h" |
| 45 | #include "B3WasmBoundsCheckValue.h" |
| 46 | #include <wtf/HashSet.h> |
| 47 | #include <wtf/StringPrintStream.h> |
| 48 | #include <wtf/text/CString.h> |
| 49 | |
| 50 | namespace JSC { namespace B3 { |
| 51 | |
| 52 | namespace { |
| 53 | |
| 54 | class Validater { |
| 55 | public: |
| 56 | Validater(Procedure& procedure, const char* dumpBefore) |
| 57 | : m_procedure(procedure) |
| 58 | , m_dumpBefore(dumpBefore) |
| 59 | { |
| 60 | } |
| 61 | |
| 62 | #define VALIDATE(condition, message) do { \ |
| 63 | if (condition) \ |
| 64 | break; \ |
| 65 | fail(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, #condition, toCString message); \ |
| 66 | } while (false) |
| 67 | |
| 68 | void run() |
| 69 | { |
| 70 | HashSet<BasicBlock*> blocks; |
| 71 | HashSet<Value*> valueInProc; |
| 72 | HashMap<Value*, unsigned> valueInBlock; |
| 73 | HashMap<Value*, BasicBlock*> valueOwner; |
| 74 | HashMap<Value*, unsigned> valueIndex; |
| 75 | |
| 76 | for (BasicBlock* block : m_procedure) { |
| 77 | blocks.add(block); |
| 78 | for (unsigned i = 0; i < block->size(); ++i) { |
| 79 | Value* value = block->at(i); |
| 80 | valueInBlock.add(value, 0).iterator->value++; |
| 81 | valueOwner.add(value, block); |
| 82 | valueIndex.add(value, i); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | for (Value* value : m_procedure.values()) |
| 87 | valueInProc.add(value); |
| 88 | |
| 89 | for (Value* value : valueInProc) |
| 90 | VALIDATE(valueInBlock.contains(value), ("At " , *value)); |
| 91 | for (auto& entry : valueInBlock) { |
| 92 | VALIDATE(valueInProc.contains(entry.key), ("At " , *entry.key)); |
| 93 | VALIDATE(entry.value == 1, ("At " , *entry.key)); |
| 94 | } |
| 95 | |
| 96 | // Compute dominators ourselves to avoid perturbing Procedure. |
| 97 | Dominators dominators(m_procedure); |
| 98 | |
| 99 | for (Value* value : valueInProc) { |
| 100 | for (Value* child : value->children()) { |
| 101 | VALIDATE(child, ("At " , *value)); |
| 102 | VALIDATE(valueInProc.contains(child), ("At " , *value, "->" , pointerDump(child))); |
| 103 | if (valueOwner.get(child) == valueOwner.get(value)) |
| 104 | VALIDATE(valueIndex.get(value) > valueIndex.get(child), ("At " , *value, "->" , pointerDump(child))); |
| 105 | else |
| 106 | VALIDATE(dominators.dominates(valueOwner.get(child), valueOwner.get(value)), ("at " , *value, "->" , pointerDump(child))); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | HashMap<BasicBlock*, HashSet<BasicBlock*>> allPredecessors; |
| 111 | for (BasicBlock* block : blocks) { |
| 112 | VALIDATE(block->size() >= 1, ("At " , *block)); |
| 113 | for (unsigned i = 0; i < block->size() - 1; ++i) |
| 114 | VALIDATE(!block->at(i)->effects().terminal, ("At " , *block->at(i))); |
| 115 | VALIDATE(block->last()->effects().terminal, ("At " , *block->last())); |
| 116 | |
| 117 | for (BasicBlock* successor : block->successorBlocks()) { |
| 118 | allPredecessors.add(successor, HashSet<BasicBlock*>()).iterator->value.add(block); |
| 119 | VALIDATE( |
| 120 | blocks.contains(successor), ("At " , *block, "->" , pointerDump(successor))); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | // Note that this totally allows dead code. |
| 125 | for (auto& entry : allPredecessors) { |
| 126 | BasicBlock* successor = entry.key; |
| 127 | HashSet<BasicBlock*>& predecessors = entry.value; |
| 128 | VALIDATE(predecessors == successor->predecessors(), ("At " , *successor)); |
| 129 | } |
| 130 | |
| 131 | for (Value* value : m_procedure.values()) { |
| 132 | for (Value* child : value->children()) |
| 133 | VALIDATE(child->type() != Void, ("At " , *value, "->" , *child)); |
| 134 | switch (value->opcode()) { |
| 135 | case Nop: |
| 136 | case Fence: |
| 137 | VALIDATE(!value->kind().hasExtraBits(), ("At " , *value)); |
| 138 | VALIDATE(!value->numChildren(), ("At " , *value)); |
| 139 | VALIDATE(value->type() == Void, ("At " , *value)); |
| 140 | break; |
| 141 | case Identity: |
| 142 | case Opaque: |
| 143 | VALIDATE(!value->kind().hasExtraBits(), ("At " , *value)); |
| 144 | VALIDATE(value->numChildren() == 1, ("At " , *value)); |
| 145 | VALIDATE(value->type() == value->child(0)->type(), ("At " , *value)); |
| 146 | VALIDATE(value->type() != Void, ("At " , *value)); |
| 147 | break; |
| 148 | case Const32: |
| 149 | VALIDATE(!value->kind().hasExtraBits(), ("At " , *value)); |
| 150 | VALIDATE(!value->numChildren(), ("At " , *value)); |
| 151 | VALIDATE(value->type() == Int32, ("At " , *value)); |
| 152 | break; |
| 153 | case Const64: |
| 154 | VALIDATE(!value->kind().hasExtraBits(), ("At " , *value)); |
| 155 | VALIDATE(!value->numChildren(), ("At " , *value)); |
| 156 | VALIDATE(value->type() == Int64, ("At " , *value)); |
| 157 | break; |
| 158 | case ConstDouble: |
| 159 | VALIDATE(!value->kind().hasExtraBits(), ("At " , *value)); |
| 160 | VALIDATE(!value->numChildren(), ("At " , *value)); |
| 161 | VALIDATE(value->type() == Double, ("At " , *value)); |
| 162 | break; |
| 163 | case ConstFloat: |
| 164 | VALIDATE(!value->kind().hasExtraBits(), ("At " , *value)); |
| 165 | VALIDATE(!value->numChildren(), ("At " , *value)); |
| 166 | VALIDATE(value->type() == Float, ("At " , *value)); |
| 167 | break; |
| 168 | case Set: |
| 169 | VALIDATE(!value->kind().hasExtraBits(), ("At " , *value)); |
| 170 | VALIDATE(value->numChildren() == 1, ("At " , *value)); |
| 171 | VALIDATE(value->child(0)->type() == value->as<VariableValue>()->variable()->type(), ("At " , *value)); |
| 172 | break; |
| 173 | case Get: |
| 174 | VALIDATE(!value->kind().hasExtraBits(), ("At " , *value)); |
| 175 | VALIDATE(!value->numChildren(), ("At " , *value)); |
| 176 | VALIDATE(value->type() == value->as<VariableValue>()->variable()->type(), ("At " , *value)); |
| 177 | break; |
| 178 | case SlotBase: |
| 179 | case FramePointer: |
| 180 | VALIDATE(!value->kind().hasExtraBits(), ("At " , *value)); |
| 181 | VALIDATE(!value->numChildren(), ("At " , *value)); |
| 182 | VALIDATE(value->type() == pointerType(), ("At " , *value)); |
| 183 | break; |
| 184 | case ArgumentReg: |
| 185 | VALIDATE(!value->kind().hasExtraBits(), ("At " , *value)); |
| 186 | VALIDATE(!value->numChildren(), ("At " , *value)); |
| 187 | VALIDATE( |
| 188 | (value->as<ArgumentRegValue>()->argumentReg().isGPR() ? pointerType() : Double) |
| 189 | == value->type(), ("At " , *value)); |
| 190 | break; |
| 191 | case Add: |
| 192 | case Sub: |
| 193 | case Mul: |
| 194 | case Div: |
| 195 | case UDiv: |
| 196 | case Mod: |
| 197 | case UMod: |
| 198 | case BitAnd: |
| 199 | case BitOr: |
| 200 | case BitXor: |
| 201 | VALIDATE(!value->kind().traps(), ("At " , *value)); |
| 202 | switch (value->opcode()) { |
| 203 | case Div: |
| 204 | case Mod: |
| 205 | if (value->isChill()) { |
| 206 | VALIDATE(value->opcode() == Div || value->opcode() == Mod, ("At " , *value)); |
| 207 | VALIDATE(isInt(value->type()), ("At " , *value)); |
| 208 | } |
| 209 | break; |
| 210 | default: |
| 211 | VALIDATE(!value->kind().hasExtraBits(), ("At " , *value)); |
| 212 | break; |
| 213 | } |
| 214 | VALIDATE(value->numChildren() == 2, ("At " , *value)); |
| 215 | VALIDATE(value->type() == value->child(0)->type(), ("At " , *value)); |
| 216 | VALIDATE(value->type() == value->child(1)->type(), ("At " , *value)); |
| 217 | VALIDATE(value->type() != Void, ("At " , *value)); |
| 218 | break; |
| 219 | case Neg: |
| 220 | VALIDATE(!value->kind().hasExtraBits(), ("At " , *value)); |
| 221 | VALIDATE(value->numChildren() == 1, ("At " , *value)); |
| 222 | VALIDATE(value->type() == value->child(0)->type(), ("At " , *value)); |
| 223 | VALIDATE(value->type() != Void, ("At " , *value)); |
| 224 | break; |
| 225 | case Shl: |
| 226 | case SShr: |
| 227 | case ZShr: |
| 228 | case RotR: |
| 229 | case RotL: |
| 230 | VALIDATE(!value->kind().hasExtraBits(), ("At " , *value)); |
| 231 | VALIDATE(value->numChildren() == 2, ("At " , *value)); |
| 232 | VALIDATE(value->type() == value->child(0)->type(), ("At " , *value)); |
| 233 | VALIDATE(value->child(1)->type() == Int32, ("At " , *value)); |
| 234 | VALIDATE(isInt(value->type()), ("At " , *value)); |
| 235 | break; |
| 236 | case BitwiseCast: |
| 237 | VALIDATE(!value->kind().hasExtraBits(), ("At " , *value)); |
| 238 | VALIDATE(value->numChildren() == 1, ("At " , *value)); |
| 239 | VALIDATE(value->type() != value->child(0)->type(), ("At " , *value)); |
| 240 | VALIDATE( |
| 241 | (value->type() == Int64 && value->child(0)->type() == Double) |
| 242 | || (value->type() == Double && value->child(0)->type() == Int64) |
| 243 | || (value->type() == Float && value->child(0)->type() == Int32) |
| 244 | || (value->type() == Int32 && value->child(0)->type() == Float), |
| 245 | ("At " , *value)); |
| 246 | break; |
| 247 | case SExt8: |
| 248 | case SExt16: |
| 249 | VALIDATE(!value->kind().hasExtraBits(), ("At " , *value)); |
| 250 | VALIDATE(value->numChildren() == 1, ("At " , *value)); |
| 251 | VALIDATE(value->child(0)->type() == Int32, ("At " , *value)); |
| 252 | VALIDATE(value->type() == Int32, ("At " , *value)); |
| 253 | break; |
| 254 | case SExt32: |
| 255 | case ZExt32: |
| 256 | VALIDATE(!value->kind().hasExtraBits(), ("At " , *value)); |
| 257 | VALIDATE(value->numChildren() == 1, ("At " , *value)); |
| 258 | VALIDATE(value->child(0)->type() == Int32, ("At " , *value)); |
| 259 | VALIDATE(value->type() == Int64, ("At " , *value)); |
| 260 | break; |
| 261 | case Clz: |
| 262 | VALIDATE(!value->kind().hasExtraBits(), ("At " , *value)); |
| 263 | VALIDATE(value->numChildren() == 1, ("At " , *value)); |
| 264 | VALIDATE(isInt(value->child(0)->type()), ("At " , *value)); |
| 265 | VALIDATE(isInt(value->type()), ("At " , *value)); |
| 266 | break; |
| 267 | case Trunc: |
| 268 | VALIDATE(!value->kind().hasExtraBits(), ("At " , *value)); |
| 269 | VALIDATE(value->numChildren() == 1, ("At " , *value)); |
| 270 | VALIDATE( |
| 271 | (value->type() == Int32 && value->child(0)->type() == Int64) |
| 272 | || (value->type() == Float && value->child(0)->type() == Double), |
| 273 | ("At " , *value)); |
| 274 | break; |
| 275 | case Abs: |
| 276 | case Ceil: |
| 277 | case Floor: |
| 278 | case Sqrt: |
| 279 | VALIDATE(!value->kind().hasExtraBits(), ("At " , *value)); |
| 280 | VALIDATE(value->numChildren() == 1, ("At " , *value)); |
| 281 | VALIDATE(isFloat(value->child(0)->type()), ("At " , *value)); |
| 282 | VALIDATE(isFloat(value->type()), ("At " , *value)); |
| 283 | break; |
| 284 | case IToD: |
| 285 | VALIDATE(!value->kind().hasExtraBits(), ("At " , *value)); |
| 286 | VALIDATE(value->numChildren() == 1, ("At " , *value)); |
| 287 | VALIDATE(isInt(value->child(0)->type()), ("At " , *value)); |
| 288 | VALIDATE(value->type() == Double, ("At " , *value)); |
| 289 | break; |
| 290 | case IToF: |
| 291 | VALIDATE(!value->kind().hasExtraBits(), ("At " , *value)); |
| 292 | VALIDATE(value->numChildren() == 1, ("At " , *value)); |
| 293 | VALIDATE(isInt(value->child(0)->type()), ("At " , *value)); |
| 294 | VALIDATE(value->type() == Float, ("At " , *value)); |
| 295 | break; |
| 296 | case FloatToDouble: |
| 297 | VALIDATE(!value->kind().hasExtraBits(), ("At " , *value)); |
| 298 | VALIDATE(value->numChildren() == 1, ("At " , *value)); |
| 299 | VALIDATE(value->child(0)->type() == Float, ("At " , *value)); |
| 300 | VALIDATE(value->type() == Double, ("At " , *value)); |
| 301 | break; |
| 302 | case DoubleToFloat: |
| 303 | VALIDATE(!value->kind().hasExtraBits(), ("At " , *value)); |
| 304 | VALIDATE(value->numChildren() == 1, ("At " , *value)); |
| 305 | VALIDATE(value->child(0)->type() == Double, ("At " , *value)); |
| 306 | VALIDATE(value->type() == Float, ("At " , *value)); |
| 307 | break; |
| 308 | case Equal: |
| 309 | case NotEqual: |
| 310 | case LessThan: |
| 311 | case GreaterThan: |
| 312 | case LessEqual: |
| 313 | case GreaterEqual: |
| 314 | VALIDATE(!value->kind().hasExtraBits(), ("At " , *value)); |
| 315 | VALIDATE(value->numChildren() == 2, ("At " , *value)); |
| 316 | VALIDATE(value->child(0)->type() == value->child(1)->type(), ("At " , *value)); |
| 317 | VALIDATE(value->type() == Int32, ("At " , *value)); |
| 318 | break; |
| 319 | case Above: |
| 320 | case Below: |
| 321 | case AboveEqual: |
| 322 | case BelowEqual: |
| 323 | VALIDATE(!value->kind().hasExtraBits(), ("At " , *value)); |
| 324 | VALIDATE(value->numChildren() == 2, ("At " , *value)); |
| 325 | VALIDATE(value->child(0)->type() == value->child(1)->type(), ("At " , *value)); |
| 326 | VALIDATE(isInt(value->child(0)->type()), ("At " , *value)); |
| 327 | VALIDATE(value->type() == Int32, ("At " , *value)); |
| 328 | break; |
| 329 | case EqualOrUnordered: |
| 330 | VALIDATE(!value->kind().hasExtraBits(), ("At " , *value)); |
| 331 | VALIDATE(value->numChildren() == 2, ("At " , *value)); |
| 332 | VALIDATE(value->child(0)->type() == value->child(1)->type(), ("At " , *value)); |
| 333 | VALIDATE(isFloat(value->child(0)->type()), ("At " , *value)); |
| 334 | VALIDATE(value->type() == Int32, ("At " , *value)); |
| 335 | break; |
| 336 | case Select: |
| 337 | VALIDATE(!value->kind().hasExtraBits(), ("At " , *value)); |
| 338 | VALIDATE(value->numChildren() == 3, ("At " , *value)); |
| 339 | VALIDATE(isInt(value->child(0)->type()), ("At " , *value)); |
| 340 | VALIDATE(value->type() == value->child(1)->type(), ("At " , *value)); |
| 341 | VALIDATE(value->type() == value->child(2)->type(), ("At " , *value)); |
| 342 | break; |
| 343 | case Load8Z: |
| 344 | case Load8S: |
| 345 | case Load16Z: |
| 346 | case Load16S: |
| 347 | VALIDATE(!value->kind().isChill(), ("At " , *value)); |
| 348 | VALIDATE(value->numChildren() == 1, ("At " , *value)); |
| 349 | VALIDATE(value->child(0)->type() == pointerType(), ("At " , *value)); |
| 350 | VALIDATE(value->type() == Int32, ("At " , *value)); |
| 351 | validateFence(value); |
| 352 | validateStackAccess(value); |
| 353 | break; |
| 354 | case Load: |
| 355 | VALIDATE(!value->kind().isChill(), ("At " , *value)); |
| 356 | VALIDATE(value->numChildren() == 1, ("At " , *value)); |
| 357 | VALIDATE(value->child(0)->type() == pointerType(), ("At " , *value)); |
| 358 | VALIDATE(value->type() != Void, ("At " , *value)); |
| 359 | validateFence(value); |
| 360 | validateStackAccess(value); |
| 361 | break; |
| 362 | case Store8: |
| 363 | case Store16: |
| 364 | VALIDATE(!value->kind().isChill(), ("At " , *value)); |
| 365 | VALIDATE(value->numChildren() == 2, ("At " , *value)); |
| 366 | VALIDATE(value->child(0)->type() == Int32, ("At " , *value)); |
| 367 | VALIDATE(value->child(1)->type() == pointerType(), ("At " , *value)); |
| 368 | VALIDATE(value->type() == Void, ("At " , *value)); |
| 369 | validateFence(value); |
| 370 | validateStackAccess(value); |
| 371 | break; |
| 372 | case Store: |
| 373 | VALIDATE(!value->kind().isChill(), ("At " , *value)); |
| 374 | VALIDATE(value->numChildren() == 2, ("At " , *value)); |
| 375 | VALIDATE(value->child(1)->type() == pointerType(), ("At " , *value)); |
| 376 | VALIDATE(value->type() == Void, ("At " , *value)); |
| 377 | validateFence(value); |
| 378 | validateStackAccess(value); |
| 379 | break; |
| 380 | case AtomicWeakCAS: |
| 381 | VALIDATE(!value->kind().isChill(), ("At " , *value)); |
| 382 | VALIDATE(value->numChildren() == 3, ("At " , *value)); |
| 383 | VALIDATE(value->type() == Int32, ("At " , *value)); |
| 384 | VALIDATE(value->child(0)->type() == value->child(1)->type(), ("At " , *value)); |
| 385 | VALIDATE(isInt(value->child(0)->type()), ("At " , *value)); |
| 386 | VALIDATE(value->child(2)->type() == pointerType(), ("At " , *value)); |
| 387 | validateAtomic(value); |
| 388 | validateStackAccess(value); |
| 389 | break; |
| 390 | case AtomicStrongCAS: |
| 391 | VALIDATE(!value->kind().isChill(), ("At " , *value)); |
| 392 | VALIDATE(value->numChildren() == 3, ("At " , *value)); |
| 393 | VALIDATE(value->type() == value->child(0)->type(), ("At " , *value)); |
| 394 | VALIDATE(value->type() == value->child(1)->type(), ("At " , *value)); |
| 395 | VALIDATE(isInt(value->type()), ("At " , *value)); |
| 396 | VALIDATE(value->child(2)->type() == pointerType(), ("At " , *value)); |
| 397 | validateAtomic(value); |
| 398 | validateStackAccess(value); |
| 399 | break; |
| 400 | case AtomicXchgAdd: |
| 401 | case AtomicXchgAnd: |
| 402 | case AtomicXchgOr: |
| 403 | case AtomicXchgSub: |
| 404 | case AtomicXchgXor: |
| 405 | case AtomicXchg: |
| 406 | VALIDATE(!value->kind().isChill(), ("At " , *value)); |
| 407 | VALIDATE(value->numChildren() == 2, ("At " , *value)); |
| 408 | VALIDATE(value->type() == value->child(0)->type(), ("At " , *value)); |
| 409 | VALIDATE(isInt(value->type()), ("At " , *value)); |
| 410 | VALIDATE(value->child(1)->type() == pointerType(), ("At " , *value)); |
| 411 | validateAtomic(value); |
| 412 | validateStackAccess(value); |
| 413 | break; |
| 414 | case Depend: |
| 415 | VALIDATE(!value->kind().hasExtraBits(), ("At " , *value)); |
| 416 | VALIDATE(value->numChildren() == 1, ("At " , *value)); |
| 417 | VALIDATE(value->type() == value->child(0)->type(), ("At " , *value)); |
| 418 | VALIDATE(isInt(value->type()), ("At " , *value)); |
| 419 | break; |
| 420 | case WasmAddress: |
| 421 | VALIDATE(!value->kind().hasExtraBits(), ("At " , *value)); |
| 422 | VALIDATE(value->numChildren() == 1, ("At " , *value)); |
| 423 | VALIDATE(value->child(0)->type() == pointerType(), ("At " , *value)); |
| 424 | VALIDATE(value->type() == pointerType(), ("At " , *value)); |
| 425 | break; |
| 426 | case CCall: |
| 427 | VALIDATE(!value->kind().hasExtraBits(), ("At " , *value)); |
| 428 | VALIDATE(value->numChildren() >= 1, ("At " , *value)); |
| 429 | VALIDATE(value->child(0)->type() == pointerType(), ("At " , *value)); |
| 430 | break; |
| 431 | case Patchpoint: |
| 432 | VALIDATE(!value->kind().hasExtraBits(), ("At " , *value)); |
| 433 | if (value->type() == Void) |
| 434 | VALIDATE(value->as<PatchpointValue>()->resultConstraint == ValueRep::WarmAny, ("At " , *value)); |
| 435 | else |
| 436 | validateStackmapConstraint(value, ConstrainedValue(value, value->as<PatchpointValue>()->resultConstraint), ConstraintRole::Def); |
| 437 | validateStackmap(value); |
| 438 | break; |
| 439 | case CheckAdd: |
| 440 | case CheckSub: |
| 441 | case CheckMul: |
| 442 | VALIDATE(!value->kind().hasExtraBits(), ("At " , *value)); |
| 443 | VALIDATE(value->numChildren() >= 2, ("At " , *value)); |
| 444 | VALIDATE(isInt(value->child(0)->type()), ("At " , *value)); |
| 445 | VALIDATE(isInt(value->child(1)->type()), ("At " , *value)); |
| 446 | VALIDATE(value->as<StackmapValue>()->constrainedChild(0).rep() == ValueRep::WarmAny, ("At " , *value)); |
| 447 | VALIDATE(value->as<StackmapValue>()->constrainedChild(1).rep() == ValueRep::WarmAny, ("At " , *value)); |
| 448 | validateStackmap(value); |
| 449 | break; |
| 450 | case Check: |
| 451 | VALIDATE(!value->kind().hasExtraBits(), ("At " , *value)); |
| 452 | VALIDATE(value->numChildren() >= 1, ("At " , *value)); |
| 453 | VALIDATE(isInt(value->child(0)->type()), ("At " , *value)); |
| 454 | VALIDATE(value->as<StackmapValue>()->constrainedChild(0).rep() == ValueRep::WarmAny, ("At " , *value)); |
| 455 | validateStackmap(value); |
| 456 | break; |
| 457 | case WasmBoundsCheck: |
| 458 | VALIDATE(!value->kind().hasExtraBits(), ("At " , *value)); |
| 459 | VALIDATE(value->numChildren() == 1, ("At " , *value)); |
| 460 | VALIDATE(value->child(0)->type() == Int32, ("At " , *value)); |
| 461 | switch (value->as<WasmBoundsCheckValue>()->boundsType()) { |
| 462 | case WasmBoundsCheckValue::Type::Pinned: |
| 463 | VALIDATE(m_procedure.code().isPinned(value->as<WasmBoundsCheckValue>()->bounds().pinnedSize), ("At " , *value)); |
| 464 | break; |
| 465 | case WasmBoundsCheckValue::Type::Maximum: |
| 466 | break; |
| 467 | } |
| 468 | VALIDATE(m_procedure.code().wasmBoundsCheckGenerator(), ("At " , *value)); |
| 469 | break; |
| 470 | case Upsilon: |
| 471 | VALIDATE(!value->kind().hasExtraBits(), ("At " , *value)); |
| 472 | VALIDATE(value->numChildren() == 1, ("At " , *value)); |
| 473 | VALIDATE(value->as<UpsilonValue>()->phi(), ("At " , *value)); |
| 474 | VALIDATE(value->as<UpsilonValue>()->phi()->opcode() == Phi, ("At " , *value)); |
| 475 | VALIDATE(value->child(0)->type() == value->as<UpsilonValue>()->phi()->type(), ("At " , *value)); |
| 476 | VALIDATE(valueInProc.contains(value->as<UpsilonValue>()->phi()), ("At " , *value)); |
| 477 | break; |
| 478 | case Phi: |
| 479 | VALIDATE(!value->kind().hasExtraBits(), ("At " , *value)); |
| 480 | VALIDATE(!value->numChildren(), ("At " , *value)); |
| 481 | VALIDATE(value->type() != Void, ("At " , *value)); |
| 482 | break; |
| 483 | case Jump: |
| 484 | VALIDATE(!value->kind().hasExtraBits(), ("At " , *value)); |
| 485 | VALIDATE(!value->numChildren(), ("At " , *value)); |
| 486 | VALIDATE(value->type() == Void, ("At " , *value)); |
| 487 | VALIDATE(valueOwner.get(value)->numSuccessors() == 1, ("At " , *value)); |
| 488 | break; |
| 489 | case Oops: |
| 490 | VALIDATE(!value->kind().hasExtraBits(), ("At " , *value)); |
| 491 | VALIDATE(!value->numChildren(), ("At " , *value)); |
| 492 | VALIDATE(value->type() == Void, ("At " , *value)); |
| 493 | VALIDATE(!valueOwner.get(value)->numSuccessors(), ("At " , *value)); |
| 494 | break; |
| 495 | case Return: |
| 496 | VALIDATE(!value->kind().hasExtraBits(), ("At " , *value)); |
| 497 | VALIDATE(value->numChildren() <= 1, ("At " , *value)); |
| 498 | VALIDATE(value->type() == Void, ("At " , *value)); |
| 499 | VALIDATE(!valueOwner.get(value)->numSuccessors(), ("At " , *value)); |
| 500 | break; |
| 501 | case Branch: |
| 502 | VALIDATE(!value->kind().hasExtraBits(), ("At " , *value)); |
| 503 | VALIDATE(value->numChildren() == 1, ("At " , *value)); |
| 504 | VALIDATE(isInt(value->child(0)->type()), ("At " , *value)); |
| 505 | VALIDATE(value->type() == Void, ("At " , *value)); |
| 506 | VALIDATE(valueOwner.get(value)->numSuccessors() == 2, ("At " , *value)); |
| 507 | break; |
| 508 | case Switch: { |
| 509 | VALIDATE(!value->kind().hasExtraBits(), ("At " , *value)); |
| 510 | VALIDATE(value->numChildren() == 1, ("At " , *value)); |
| 511 | VALIDATE(isInt(value->child(0)->type()), ("At " , *value)); |
| 512 | VALIDATE(value->type() == Void, ("At " , *value)); |
| 513 | VALIDATE(value->as<SwitchValue>()->hasFallThrough(valueOwner.get(value)), ("At " , *value)); |
| 514 | // This validates the same thing as hasFallThrough, but more explicitly. We want to |
| 515 | // make sure that if anyone tries to change the definition of hasFallThrough, they |
| 516 | // will feel some pain here, since this is fundamental. |
| 517 | VALIDATE(valueOwner.get(value)->numSuccessors() == value->as<SwitchValue>()->numCaseValues() + 1, ("At " , *value)); |
| 518 | |
| 519 | // Check that there are no duplicate cases. |
| 520 | Vector<int64_t> caseValues = value->as<SwitchValue>()->caseValues(); |
| 521 | std::sort(caseValues.begin(), caseValues.end()); |
| 522 | for (unsigned i = 1; i < caseValues.size(); ++i) |
| 523 | VALIDATE(caseValues[i - 1] != caseValues[i], ("At " , *value, ", caseValue = " , caseValues[i])); |
| 524 | break; |
| 525 | } |
| 526 | case EntrySwitch: |
| 527 | VALIDATE(!value->kind().hasExtraBits(), ("At " , *value)); |
| 528 | VALIDATE(!value->numChildren(), ("At " , *value)); |
| 529 | VALIDATE(value->type() == Void, ("At " , *value)); |
| 530 | VALIDATE(valueOwner.get(value)->numSuccessors() == m_procedure.numEntrypoints(), ("At " , *value)); |
| 531 | break; |
| 532 | } |
| 533 | |
| 534 | VALIDATE(!(value->effects().writes && value->key()), ("At " , *value)); |
| 535 | } |
| 536 | |
| 537 | for (Variable* variable : m_procedure.variables()) |
| 538 | VALIDATE(variable->type() != Void, ("At " , *variable)); |
| 539 | |
| 540 | for (BasicBlock* block : m_procedure) { |
| 541 | // We expect the predecessor list to be de-duplicated. |
| 542 | HashSet<BasicBlock*> predecessors; |
| 543 | for (BasicBlock* predecessor : block->predecessors()) |
| 544 | predecessors.add(predecessor); |
| 545 | VALIDATE(block->numPredecessors() == predecessors.size(), ("At " , *block)); |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | private: |
| 550 | void validateStackmap(Value* value) |
| 551 | { |
| 552 | StackmapValue* stackmap = value->as<StackmapValue>(); |
| 553 | VALIDATE(stackmap, ("At " , *value)); |
| 554 | VALIDATE(stackmap->numChildren() >= stackmap->reps().size(), ("At " , *stackmap)); |
| 555 | for (ConstrainedValue child : stackmap->constrainedChildren()) |
| 556 | validateStackmapConstraint(stackmap, child); |
| 557 | } |
| 558 | |
| 559 | enum class ConstraintRole { |
| 560 | Use, |
| 561 | Def |
| 562 | }; |
| 563 | void validateStackmapConstraint(Value* context, const ConstrainedValue& value, ConstraintRole role = ConstraintRole::Use) |
| 564 | { |
| 565 | switch (value.rep().kind()) { |
| 566 | case ValueRep::WarmAny: |
| 567 | case ValueRep::SomeRegister: |
| 568 | case ValueRep::StackArgument: |
| 569 | break; |
| 570 | case ValueRep::LateColdAny: |
| 571 | case ValueRep::ColdAny: |
| 572 | VALIDATE(role == ConstraintRole::Use, ("At " , *context, ": " , value)); |
| 573 | break; |
| 574 | case ValueRep::SomeRegisterWithClobber: |
| 575 | VALIDATE(role == ConstraintRole::Use, ("At " , *context, ": " , value)); |
| 576 | VALIDATE(context->as<PatchpointValue>(), ("At " , *context)); |
| 577 | break; |
| 578 | case ValueRep::SomeEarlyRegister: |
| 579 | VALIDATE(role == ConstraintRole::Def, ("At " , *context, ": " , value)); |
| 580 | break; |
| 581 | case ValueRep::Register: |
| 582 | case ValueRep::LateRegister: |
| 583 | case ValueRep::SomeLateRegister: |
| 584 | if (value.rep().kind() == ValueRep::LateRegister) |
| 585 | VALIDATE(role == ConstraintRole::Use, ("At " , *context, ": " , value)); |
| 586 | if (value.rep().reg().isGPR()) |
| 587 | VALIDATE(isInt(value.value()->type()), ("At " , *context, ": " , value)); |
| 588 | else |
| 589 | VALIDATE(isFloat(value.value()->type()), ("At " , *context, ": " , value)); |
| 590 | break; |
| 591 | default: |
| 592 | VALIDATE(false, ("At " , *context, ": " , value)); |
| 593 | break; |
| 594 | } |
| 595 | } |
| 596 | |
| 597 | void validateFence(Value* value) |
| 598 | { |
| 599 | MemoryValue* memory = value->as<MemoryValue>(); |
| 600 | if (memory->hasFence()) |
| 601 | VALIDATE(memory->accessBank() == GP, ("Fence at " , *memory)); |
| 602 | } |
| 603 | |
| 604 | void validateAtomic(Value* value) |
| 605 | { |
| 606 | AtomicValue* atomic = value->as<AtomicValue>(); |
| 607 | |
| 608 | VALIDATE(bestType(GP, atomic->accessWidth()) == atomic->accessType(), ("At " , *value)); |
| 609 | } |
| 610 | |
| 611 | void validateStackAccess(Value* value) |
| 612 | { |
| 613 | MemoryValue* memory = value->as<MemoryValue>(); |
| 614 | SlotBaseValue* slotBase = value->lastChild()->as<SlotBaseValue>(); |
| 615 | if (!slotBase) |
| 616 | return; |
| 617 | |
| 618 | VALIDATE(memory->offset() >= 0, ("At " , *value)); |
| 619 | } |
| 620 | |
| 621 | NO_RETURN_DUE_TO_CRASH void fail( |
| 622 | const char* filename, int lineNumber, const char* function, const char* condition, |
| 623 | CString message) |
| 624 | { |
| 625 | CString failureMessage; |
| 626 | { |
| 627 | StringPrintStream out; |
| 628 | out.print("B3 VALIDATION FAILURE\n" ); |
| 629 | out.print(" " , condition, " (" , filename, ":" , lineNumber, ")\n" ); |
| 630 | out.print(" " , message, "\n" ); |
| 631 | out.print(" After " , m_procedure.lastPhaseName(), "\n" ); |
| 632 | failureMessage = out.toCString(); |
| 633 | } |
| 634 | |
| 635 | dataLog(failureMessage); |
| 636 | if (m_dumpBefore) { |
| 637 | dataLog("Before " , m_procedure.lastPhaseName(), ":\n" ); |
| 638 | dataLog(m_dumpBefore); |
| 639 | } |
| 640 | dataLog("At time of failure:\n" ); |
| 641 | dataLog(m_procedure); |
| 642 | |
| 643 | dataLog(failureMessage); |
| 644 | WTFReportAssertionFailure(filename, lineNumber, function, condition); |
| 645 | CRASH(); |
| 646 | } |
| 647 | |
| 648 | Procedure& m_procedure; |
| 649 | const char* m_dumpBefore; |
| 650 | }; |
| 651 | |
| 652 | } // anonymous namespace |
| 653 | |
| 654 | void validate(Procedure& procedure, const char* dumpBefore) |
| 655 | { |
| 656 | Validater validater(procedure, dumpBefore); |
| 657 | validater.run(); |
| 658 | } |
| 659 | |
| 660 | } } // namespace JSC::B3 |
| 661 | |
| 662 | #endif // ENABLE(B3_JIT) |
| 663 | |