| 1 |
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
| 2 |
* vim: set ts=8 sts=2 et sw=2 tw=80: |
| 3 |
* |
| 4 |
* Copyright 2021 Mozilla Foundation |
| 5 |
* |
| 6 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 |
* you may not use this file except in compliance with the License. |
| 8 |
* You may obtain a copy of the License at |
| 9 |
* |
| 10 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
* |
| 12 |
* Unless required by applicable law or agreed to in writing, software |
| 13 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 14 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 |
* See the License for the specific language governing permissions and |
| 16 |
* limitations under the License. |
| 17 |
*/ |
| 18 |
|
| 19 |
#include "wasm/WasmValType.h" |
| 20 |
|
| 21 |
#include "js/Conversions.h" |
| 22 |
#include "js/ErrorReport.h" |
| 23 |
#include "js/friend/ErrorMessages.h" // JSMSG_* |
| 24 |
#include "js/Printf.h" |
| 25 |
#include "js/Value.h" |
| 26 |
|
| 27 |
#include "vm/JSAtomUtils.h" // Atomize |
| 28 |
#include "vm/JSObject.h" |
| 29 |
#include "vm/StringType.h" |
| 30 |
#include "wasm/WasmFeatures.h" |
| 31 |
#include "wasm/WasmJS.h" |
| 32 |
|
| 33 |
#include "vm/JSAtomUtils-inl.h" // AtomToId |
| 34 |
#include "vm/JSObject-inl.h" |
| 35 |
|
| 36 |
using namespace js; |
| 37 |
using namespace js::wasm; |
| 38 |
|
| 39 |
RefType RefType::topType() const { |
| 40 |
switch (hierarchy()) { |
| 41 |
case wasm::RefTypeHierarchy::Any: |
| 42 |
return wasm::RefType::any(); |
| 43 |
case wasm::RefTypeHierarchy::Func: |
| 44 |
return wasm::RefType::func(); |
| 45 |
case wasm::RefTypeHierarchy::Extern: |
| 46 |
return wasm::RefType::extern_(); |
| 47 |
case wasm::RefTypeHierarchy::Exn: |
| 48 |
return wasm::RefType::exn(); |
| 49 |
default: |
| 50 |
MOZ_CRASH("switch is exhaustive"); |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
RefType RefType::bottomType() const { |
| 55 |
switch (hierarchy()) { |
| 56 |
case wasm::RefTypeHierarchy::Any: |
| 57 |
return wasm::RefType::none(); |
| 58 |
case wasm::RefTypeHierarchy::Func: |
| 59 |
return wasm::RefType::nofunc(); |
| 60 |
case wasm::RefTypeHierarchy::Extern: |
| 61 |
return wasm::RefType::noextern(); |
| 62 |
case wasm::RefTypeHierarchy::Exn: |
| 63 |
return wasm::RefType::noexn(); |
| 64 |
default: |
| 65 |
MOZ_CRASH("switch is exhaustive"); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
static RefType FirstCommonSuperType(RefType a, RefType b, |
| 70 |
std::initializer_list<RefType> supers) { |
| 71 |
for (RefType super : supers) { |
| 72 |
if (RefType::isSubTypeOf(a, super) && RefType::isSubTypeOf(b, super)) { |
| 73 |
return super; |
| 74 |
} |
| 75 |
} |
| 76 |
MOZ_CRASH("failed to find common super type"); |
| 77 |
} |
| 78 |
|
| 79 |
RefType RefType::leastUpperBound(RefType a, RefType b) { |
| 80 |
// Types in different hierarchies have no common bound. Validation should |
| 81 |
// always prevent two such types from being compared. |
| 82 |
MOZ_RELEASE_ASSERT(a.hierarchy() == b.hierarchy()); |
| 83 |
|
| 84 |
// Whether the LUB is nullable can be determined by the nullability of a and |
| 85 |
// b, regardless of their actual types. |
| 86 |
bool nullable = a.isNullable() || b.isNullable(); |
| 87 |
|
| 88 |
// If one type is a subtype of the other, the higher type is the LUB - and we |
| 89 |
// can capture nulls here too, as we know the nullability of the LUB. |
| 90 |
if (RefType::isSubTypeOf(a, b.withIsNullable(nullable))) { |
| 91 |
return b.withIsNullable(nullable); |
| 92 |
} |
| 93 |
if (RefType::isSubTypeOf(b, a.withIsNullable(nullable))) { |
| 94 |
return a.withIsNullable(nullable); |
| 95 |
} |
| 96 |
|
| 97 |
// Concrete types may share a concrete parent type. We can test b against all |
| 98 |
// of a's parent types to see if this is true. |
| 99 |
if (a.isTypeRef() && b.isTypeRef()) { |
| 100 |
const TypeDef* aSuper = a.typeDef()->superTypeDef(); |
| 101 |
while (aSuper) { |
| 102 |
if (TypeDef::isSubTypeOf(b.typeDef(), aSuper)) { |
| 103 |
return RefType(aSuper, nullable); |
| 104 |
} |
| 105 |
aSuper = aSuper->superTypeDef(); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
// Because wasm type hierarchies are pretty small and simple, we can |
| 110 |
// essentially brute-force the LUB by simply iterating over all the abstract |
| 111 |
// types bottom-to-top. The first one that is a super type of both a and b is |
| 112 |
// the LUB. We are guaranteed to find a common bound because we have verified |
| 113 |
// that the types have the same hierarchy and we will therefore at least find |
| 114 |
// the hierarchy's top type. |
| 115 |
// |
| 116 |
// We test against the nullable versions of these types, and then apply the |
| 117 |
// true nullability afterward. This is ok -- this finds the *kind* of the LUB |
| 118 |
// (which we now know to be abstract), and applying the correct nullability |
| 119 |
// will not affect this. For example, for the non-nullable types |
| 120 |
// (ref $myStruct) and (ref $myArray), we will find (ref null eq), and then |
| 121 |
// modify it to (ref eq), which is the correct LUB. |
| 122 |
RefType common; |
| 123 |
switch (a.hierarchy()) { |
| 124 |
case RefTypeHierarchy::Any: |
| 125 |
common = FirstCommonSuperType( |
| 126 |
a, b, |
| 127 |
{RefType::none(), RefType::i31(), RefType::struct_(), |
| 128 |
RefType::array(), RefType::eq(), RefType::any()}); |
| 129 |
break; |
| 130 |
case RefTypeHierarchy::Func: |
| 131 |
common = FirstCommonSuperType(a, b, {RefType::nofunc(), RefType::func()}); |
| 132 |
break; |
| 133 |
case RefTypeHierarchy::Extern: |
| 134 |
common = |
| 135 |
FirstCommonSuperType(a, b, {RefType::noextern(), RefType::extern_()}); |
| 136 |
break; |
| 137 |
case RefTypeHierarchy::Exn: |
| 138 |
common = FirstCommonSuperType(a, b, {RefType::noexn(), RefType::exn()}); |
| 139 |
break; |
| 140 |
default: |
| 141 |
MOZ_CRASH("unknown type hierarchy"); |
| 142 |
} |
| 143 |
return common.withIsNullable(nullable); |
| 144 |
} |
| 145 |
|
| 146 |
TypeDefKind RefType::typeDefKind() const { |
| 147 |
switch (kind()) { |
| 148 |
case RefType::Struct: |
| 149 |
return TypeDefKind::Struct; |
| 150 |
case RefType::Array: |
| 151 |
return TypeDefKind::Array; |
| 152 |
case RefType::Func: |
| 153 |
return TypeDefKind::Func; |
| 154 |
default: |
| 155 |
return TypeDefKind::None; |
| 156 |
} |
| 157 |
MOZ_CRASH("switch is exhaustive"); |
| 158 |
} |
| 159 |
|
| 160 |
static bool ToRefType(JSContext* cx, const JSLinearString* typeLinearStr, |
| 161 |
RefType* out) { |
| 162 |
if (StringEqualsLiteral(typeLinearStr, "anyfunc") || |
| 163 |
StringEqualsLiteral(typeLinearStr, "funcref")) { |
| 164 |
// The JS API uses "anyfunc" uniformly as the external name of funcref. We |
| 165 |
// also allow "funcref" for compatibility with code we've already shipped. |
| 166 |
*out = RefType::func(); |
| 167 |
return true; |
| 168 |
} |
| 169 |
if (StringEqualsLiteral(typeLinearStr, "externref")) { |
| 170 |
*out = RefType::extern_(); |
| 171 |
return true; |
| 172 |
} |
| 173 |
if (ExnRefAvailable(cx)) { |
| 174 |
if (StringEqualsLiteral(typeLinearStr, "exnref")) { |
| 175 |
*out = RefType::exn(); |
| 176 |
return true; |
| 177 |
} |
| 178 |
} |
| 179 |
if (StringEqualsLiteral(typeLinearStr, "anyref")) { |
| 180 |
*out = RefType::any(); |
| 181 |
return true; |
| 182 |
} |
| 183 |
if (StringEqualsLiteral(typeLinearStr, "eqref")) { |
| 184 |
*out = RefType::eq(); |
| 185 |
return true; |
| 186 |
} |
| 187 |
if (StringEqualsLiteral(typeLinearStr, "i31ref")) { |
| 188 |
*out = RefType::i31(); |
| 189 |
return true; |
| 190 |
} |
| 191 |
if (StringEqualsLiteral(typeLinearStr, "structref")) { |
| 192 |
*out = RefType::struct_(); |
| 193 |
return true; |
| 194 |
} |
| 195 |
if (StringEqualsLiteral(typeLinearStr, "arrayref")) { |
| 196 |
*out = RefType::array(); |
| 197 |
return true; |
| 198 |
} |
| 199 |
if (StringEqualsLiteral(typeLinearStr, "nullfuncref")) { |
| 200 |
*out = RefType::nofunc(); |
| 201 |
return true; |
| 202 |
} |
| 203 |
if (StringEqualsLiteral(typeLinearStr, "nullexternref")) { |
| 204 |
*out = RefType::noextern(); |
| 205 |
return true; |
| 206 |
} |
| 207 |
if (StringEqualsLiteral(typeLinearStr, "nullexnref")) { |
| 208 |
*out = RefType::noexn(); |
| 209 |
return true; |
| 210 |
} |
| 211 |
if (StringEqualsLiteral(typeLinearStr, "nullref")) { |
| 212 |
*out = RefType::none(); |
| 213 |
return true; |
| 214 |
} |
| 215 |
|
| 216 |
JS_ReportErrorNumberUTF8(cx, GetErrorMessage, nullptr, |
| 217 |
JSMSG_WASM_BAD_STRING_VAL_TYPE); |
| 218 |
return false; |
| 219 |
} |
| 220 |
|
| 221 |
enum class RefTypeResult { |
| 222 |
Failure, |
| 223 |
Parsed, |
| 224 |
Unparsed, |
| 225 |
}; |
| 226 |
|
| 227 |
bool wasm::ToValType(JSContext* cx, HandleValue v, ValType* out) { |
| 228 |
RootedString typeStr(cx, ToString(cx, v)); |
| 229 |
if (!typeStr) { |
| 230 |
return false; |
| 231 |
} |
| 232 |
|
| 233 |
Rooted<JSLinearString*> typeLinearStr(cx, typeStr->ensureLinear(cx)); |
| 234 |
if (!typeLinearStr) { |
| 235 |
return false; |
| 236 |
} |
| 237 |
|
| 238 |
if (StringEqualsLiteral(typeLinearStr, "i32")) { |
| 239 |
*out = ValType::I32; |
| 240 |
} else if (StringEqualsLiteral(typeLinearStr, "i64")) { |
| 241 |
*out = ValType::I64; |
| 242 |
} else if (StringEqualsLiteral(typeLinearStr, "f32")) { |
| 243 |
*out = ValType::F32; |
| 244 |
} else if (StringEqualsLiteral(typeLinearStr, "f64")) { |
| 245 |
*out = ValType::F64; |
| 246 |
#ifdef ENABLE_WASM_SIMD |
| 247 |
} else if (SimdAvailable(cx) && StringEqualsLiteral(typeLinearStr, "v128")) { |
| 248 |
*out = ValType::V128; |
| 249 |
#endif |
| 250 |
} else { |
| 251 |
RefType rt; |
| 252 |
if (ToRefType(cx, typeLinearStr, &rt)) { |
| 253 |
*out = ValType(rt); |
| 254 |
} else { |
| 255 |
// ToRefType will report an error when it fails, just return false |
| 256 |
return false; |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
return true; |
| 261 |
} |
| 262 |
|
| 263 |
bool wasm::ToRefType(JSContext* cx, HandleValue v, RefType* out) { |
| 264 |
RootedString typeStr(cx, ToString(cx, v)); |
| 265 |
if (!typeStr) { |
| 266 |
return false; |
| 267 |
} |
| 268 |
|
| 269 |
Rooted<JSLinearString*> typeLinearStr(cx, typeStr->ensureLinear(cx)); |
| 270 |
if (!typeLinearStr) { |
| 271 |
return false; |
| 272 |
} |
| 273 |
|
| 274 |
return ToRefType(cx, typeLinearStr, out); |
| 275 |
} |
| 276 |
|
| 277 |
UniqueChars wasm::ToString(RefType type, const TypeContext* types) { |
| 278 |
// Try to emit a shorthand version first |
| 279 |
if (type.isNullable() && !type.isTypeRef()) { |
| 280 |
const char* literal = nullptr; |
| 281 |
switch (type.kind()) { |
| 282 |
case RefType::Func: |
| 283 |
literal = "funcref"; |
| 284 |
break; |
| 285 |
case RefType::Extern: |
| 286 |
literal = "externref"; |
| 287 |
break; |
| 288 |
case RefType::Exn: |
| 289 |
literal = "exnref"; |
| 290 |
break; |
| 291 |
case RefType::Any: |
| 292 |
literal = "anyref"; |
| 293 |
break; |
| 294 |
case RefType::NoFunc: |
| 295 |
literal = "nullfuncref"; |
| 296 |
break; |
| 297 |
case RefType::NoExn: |
| 298 |
literal = "nullexnref"; |
| 299 |
break; |
| 300 |
case RefType::NoExtern: |
| 301 |
literal = "nullexternref"; |
| 302 |
break; |
| 303 |
case RefType::None: |
| 304 |
literal = "nullref"; |
| 305 |
break; |
| 306 |
case RefType::Eq: |
| 307 |
literal = "eqref"; |
| 308 |
break; |
| 309 |
case RefType::I31: |
| 310 |
literal = "i31ref"; |
| 311 |
break; |
| 312 |
case RefType::Struct: |
| 313 |
literal = "structref"; |
| 314 |
break; |
| 315 |
case RefType::Array: |
| 316 |
literal = "arrayref"; |
| 317 |
break; |
| 318 |
case RefType::TypeRef: { |
| 319 |
MOZ_CRASH("type ref should not be possible here"); |
| 320 |
} |
| 321 |
} |
| 322 |
return DuplicateString(literal); |
| 323 |
} |
| 324 |
|
| 325 |
// Emit the full reference type with heap type |
| 326 |
const char* heapType = nullptr; |
| 327 |
switch (type.kind()) { |
| 328 |
case RefType::Func: |
| 329 |
heapType = "func"; |
| 330 |
break; |
| 331 |
case RefType::Extern: |
| 332 |
heapType = "extern"; |
| 333 |
break; |
| 334 |
case RefType::Exn: |
| 335 |
heapType = "exn"; |
| 336 |
break; |
| 337 |
case RefType::Any: |
| 338 |
heapType = "any"; |
| 339 |
break; |
| 340 |
case RefType::NoFunc: |
| 341 |
heapType = "nofunc"; |
| 342 |
break; |
| 343 |
case RefType::NoExn: |
| 344 |
heapType = "noexn"; |
| 345 |
break; |
| 346 |
case RefType::NoExtern: |
| 347 |
heapType = "noextern"; |
| 348 |
break; |
| 349 |
case RefType::None: |
| 350 |
heapType = "none"; |
| 351 |
break; |
| 352 |
case RefType::Eq: |
| 353 |
heapType = "eq"; |
| 354 |
break; |
| 355 |
case RefType::I31: |
| 356 |
heapType = "i31"; |
| 357 |
break; |
| 358 |
case RefType::Struct: |
| 359 |
heapType = "struct"; |
| 360 |
break; |
| 361 |
case RefType::Array: |
| 362 |
heapType = "array"; |
| 363 |
break; |
| 364 |
case RefType::TypeRef: { |
| 365 |
if (types) { |
| 366 |
uint32_t typeIndex = types->indexOf(*type.typeDef()); |
| 367 |
return JS_smprintf("(ref %s%d)", type.isNullable() ? "null " : "", |
| 368 |
typeIndex); |
| 369 |
} |
| 370 |
return JS_smprintf("(ref %s?)", type.isNullable() ? "null " : ""); |
| 371 |
} |
| 372 |
} |
| 373 |
return JS_smprintf("(ref %s%s)", type.isNullable() ? "null " : "", heapType); |
| 374 |
} |
| 375 |
|
| 376 |
UniqueChars wasm::ToString(ValType type, const TypeContext* types) { |
| 377 |
return ToString(type.storageType(), types); |
| 378 |
} |
| 379 |
|
| 380 |
UniqueChars wasm::ToString(StorageType type, const TypeContext* types) { |
| 381 |
const char* literal = nullptr; |
| 382 |
switch (type.kind()) { |
| 383 |
case StorageType::I8: |
| 384 |
literal = "i8"; |
| 385 |
break; |
| 386 |
case StorageType::I16: |
| 387 |
literal = "i16"; |
| 388 |
break; |
| 389 |
case StorageType::I32: |
| 390 |
literal = "i32"; |
| 391 |
break; |
| 392 |
case StorageType::I64: |
| 393 |
literal = "i64"; |
| 394 |
break; |
| 395 |
case StorageType::V128: |
| 396 |
literal = "v128"; |
| 397 |
break; |
| 398 |
case StorageType::F32: |
| 399 |
literal = "f32"; |
| 400 |
break; |
| 401 |
case StorageType::F64: |
| 402 |
literal = "f64"; |
| 403 |
break; |
| 404 |
case StorageType::Ref: |
| 405 |
return ToString(type.refType(), types); |
| 406 |
} |
| 407 |
return DuplicateString(literal); |
| 408 |
} |
| 409 |
|
| 410 |
UniqueChars wasm::ToString(const mozilla::Maybe<ValType>& type, |
| 411 |
const TypeContext* types) { |
| 412 |
return type ? ToString(type.ref(), types) : JS_smprintf("%s", "void"); |
| 413 |
} |
| 414 |
|
| 415 |
UniqueChars wasm::ToString(const MaybeRefType& type, const TypeContext* types) { |
| 416 |
return type ? ToString(type.value(), types) : JS_smprintf("%s", "void"); |
| 417 |
} |