Where Online Learning is simpler!
The C and C++ Include Header Files
cat -n /usr/include/wabt/shared-validator.h
1 /* 2 * Copyright 2020 WebAssembly Community Group participants 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef WABT_SHARED_VALIDATOR_H_ 18 #define WABT_SHARED_VALIDATOR_H_ 19 20 #include <map> 21 #include <set> 22 #include <string> 23 #include <vector> 24 25 #include "wabt/common.h" 26 #include "wabt/error.h" 27 #include "wabt/feature.h" 28 #include "wabt/ir.h" 29 #include "wabt/opcode.h" 30 #include "wabt/type-checker.h" 31 32 #include "wabt/binary-reader.h" // For TypeMut. 33 34 namespace wabt { 35 36 struct ValidateOptions { 37 ValidateOptions() = default; 38 ValidateOptions(const Features& features) : features(features) {} 39 40 Features features; 41 }; 42 43 class SharedValidator { 44 public: 45 WABT_DISALLOW_COPY_AND_ASSIGN(SharedValidator); 46 SharedValidator(Errors*, const ValidateOptions& options); 47 48 // TODO: Move into SharedValidator? 49 using Label = TypeChecker::Label; 50 size_t type_stack_size() const { return typechecker_.type_stack_size(); } 51 Result GetLabel(Index depth, Label** out_label) { 52 return typechecker_.GetLabel(depth, out_label); 53 } 54 Result GetCatchCount(Index depth, Index* out_count) { 55 return typechecker_.GetCatchCount(depth, out_count); 56 } 57 58 Result WABT_PRINTF_FORMAT(3, 4) 59 PrintError(const Location& loc, const char* fmt, ...); 60 61 void OnTypecheckerError(const char* msg); 62 63 Index GetLocalCount() const; 64 65 Result EndModule(); 66 67 Result OnFuncType(const Location&, 68 Index param_count, 69 const Type* param_types, 70 Index result_count, 71 const Type* result_types, 72 Index type_index); 73 Result OnStructType(const Location&, Index field_count, TypeMut* fields); 74 Result OnArrayType(const Location&, TypeMut field); 75 76 Result OnFunction(const Location&, Var sig_var); 77 Result OnTable(const Location&, Type elem_type, const Limits&); 78 Result OnMemory(const Location&, const Limits&); 79 Result OnGlobalImport(const Location&, Type type, bool mutable_); 80 Result OnGlobal(const Location&, Type type, bool mutable_); 81 Result OnTag(const Location&, Var sig_var); 82 83 Result OnExport(const Location&, 84 ExternalKind, 85 Var item_var, 86 std::string_view name); 87 88 Result OnStart(const Location&, Var func_var); 89 90 Result OnElemSegment(const Location&, Var table_var, SegmentKind); 91 Result OnElemSegmentElemType(const Location&, Type elem_type); 92 93 void OnDataCount(Index count); 94 Result OnDataSegment(const Location&, Var memory_var, SegmentKind); 95 96 Result BeginInitExpr(const Location&, Type type); 97 Result EndInitExpr(); 98 99 Result BeginFunctionBody(const Location&, Index func_index); 100 Result EndFunctionBody(const Location&); 101 Result OnLocalDecl(const Location&, Index count, Type type); 102 103 Result OnAtomicFence(const Location&, uint32_t consistency_model); 104 Result OnAtomicLoad(const Location&, 105 Opcode, 106 Var memidx, 107 Address align, 108 Address offset); 109 Result OnAtomicNotify(const Location&, 110 Opcode, 111 Var memidx, 112 Address align, 113 Address offset); 114 Result OnAtomicRmwCmpxchg(const Location&, 115 Opcode, 116 Var memidx, 117 Address align, 118 Address offset); 119 Result OnAtomicRmw(const Location&, 120 Opcode, 121 Var memidx, 122 Address align, 123 Address offset); 124 Result OnAtomicStore(const Location&, 125 Opcode, 126 Var memidx, 127 Address align, 128 Address offset); 129 Result OnAtomicWait(const Location&, 130 Opcode, 131 Var memidx, 132 Address align, 133 Address offset); 134 Result OnBinary(const Location&, Opcode); 135 Result OnBlock(const Location&, Type sig_type); 136 Result OnBr(const Location&, Var depth); 137 Result OnBrIf(const Location&, Var depth); 138 Result BeginBrTable(const Location&); 139 Result OnBrTableTarget(const Location&, Var depth); 140 Result EndBrTable(const Location&); 141 Result OnCall(const Location&, Var func_var); 142 Result OnCallIndirect(const Location&, Var sig_var, Var table_var); 143 Result OnCallRef(const Location&, Index* function_type_index); 144 Result OnCatch(const Location&, Var tag_var, bool is_catch_all); 145 Result OnCompare(const Location&, Opcode); 146 Result OnConst(const Location&, Type); 147 Result OnConvert(const Location&, Opcode); 148 Result OnDataDrop(const Location&, Var segment_var); 149 Result OnDelegate(const Location&, Var depth); 150 Result OnDrop(const Location&); 151 Result OnElemDrop(const Location&, Var segment_var); 152 Result OnElse(const Location&); 153 Result OnEnd(const Location&); 154 Result OnGlobalGet(const Location&, Var); 155 Result OnGlobalSet(const Location&, Var); 156 Result OnIf(const Location&, Type sig_type); 157 Result OnLoad(const Location&, Opcode, Var memidx, Address align, Address offset); 158 Result OnLoadSplat(const Location&, 159 Opcode, 160 Var memidx, 161 Address align, 162 Address offset); 163 Result OnLoadZero(const Location&, 164 Opcode, 165 Var memidx, 166 Address align, 167 Address offset); 168 Result OnLocalGet(const Location&, Var); 169 Result OnLocalSet(const Location&, Var); 170 Result OnLocalTee(const Location&, Var); 171 Result OnLoop(const Location&, Type sig_type); 172 Result OnMemoryCopy(const Location&, Var destmemidx, Var srcmemidx); 173 Result OnMemoryFill(const Location&, Var memidx); 174 Result OnMemoryGrow(const Location&, Var memidx); 175 Result OnMemoryInit(const Location&, Var segment_var, Var memidx); 176 Result OnMemorySize(const Location&, Var memidx); 177 Result OnNop(const Location&); 178 Result OnRefFunc(const Location&, Var func_var); 179 Result OnRefIsNull(const Location&); 180 Result OnRefNull(const Location&, Type type); 181 Result OnRethrow(const Location&, Var depth); 182 Result OnReturnCall(const Location&, Var func_var); 183 Result OnReturnCallIndirect(const Location&, Var sig_var, Var table_var); 184 Result OnReturn(const Location&); 185 Result OnSelect(const Location&, Index result_count, Type* result_types); 186 Result OnSimdLaneOp(const Location&, Opcode, uint64_t lane_idx); 187 Result OnSimdLoadLane(const Location&, 188 Opcode, 189 Var memidx, 190 Address align, 191 Address offset, 192 uint64_t lane_idx); 193 Result OnSimdStoreLane(const Location&, 194 Opcode, 195 Var memidx, 196 Address align, 197 Address offset, 198 uint64_t lane_idx); 199 Result OnSimdShuffleOp(const Location&, Opcode, v128 lane_idx); 200 Result OnStore(const Location&, 201 Opcode, 202 Var memidx, 203 Address align, 204 Address offset); 205 Result OnTableCopy(const Location&, Var dst_var, Var src_var); 206 Result OnTableFill(const Location&, Var table_var); 207 Result OnTableGet(const Location&, Var table_var); 208 Result OnTableGrow(const Location&, Var table_var); 209 Result OnTableInit(const Location&, Var segment_var, Var table_var); 210 Result OnTableSet(const Location&, Var table_var); 211 Result OnTableSize(const Location&, Var table_var); 212 Result OnTernary(const Location&, Opcode); 213 Result OnThrow(const Location&, Var tag_var); 214 Result OnTry(const Location&, Type sig_type); 215 Result OnUnary(const Location&, Opcode); 216 Result OnUnreachable(const Location&); 217 218 private: 219 struct FuncType { 220 FuncType() = default; 221 FuncType(const TypeVector& params, 222 const TypeVector& results, 223 Index type_index) 224 : params(params), results(results), type_index(type_index) {} 225 226 TypeVector params; 227 TypeVector results; 228 Index type_index; 229 }; 230 231 struct StructType { 232 StructType() = default; 233 StructType(const TypeMutVector& fields) : fields(fields) {} 234 235 TypeMutVector fields; 236 }; 237 238 struct ArrayType { 239 ArrayType() = default; 240 ArrayType(TypeMut field) : field(field) {} 241 242 TypeMut field; 243 }; 244 245 struct TableType { 246 TableType() = default; 247 TableType(Type element, Limits limits) : element(element), limits(limits) {} 248 249 Type element = Type::Any; 250 Limits limits; 251 }; 252 253 struct MemoryType { 254 MemoryType() = default; 255 MemoryType(Limits limits) : limits(limits) {} 256 257 Limits limits; 258 }; 259 260 struct GlobalType { 261 GlobalType() = default; 262 GlobalType(Type type, bool mutable_) : type(type), mutable_(mutable_) {} 263 264 Type type = Type::Any; 265 bool mutable_ = true; 266 }; 267 268 struct TagType { 269 TypeVector params; 270 }; 271 272 struct ElemType { 273 ElemType() = default; 274 ElemType(Type element, bool is_active, Type table_type) 275 : element(element), is_active(is_active), table_type(table_type) {} 276 277 Type element; 278 bool is_active; 279 Type table_type; 280 }; 281 282 struct LocalDecl { 283 Type type; 284 Index end; 285 }; 286 287 bool ValidInitOpcode(Opcode opcode) const; 288 Result CheckInstr(Opcode opcode, const Location& loc); 289 Result CheckType(const Location&, 290 Type actual, 291 Type expected, 292 const char* desc); 293 Result CheckLimits(const Location&, 294 const Limits&, 295 uint64_t absolute_max, 296 const char* desc); 297 298 Result CheckLocalIndex(Var local_var, Type* out_type); 299 300 Result CheckDeclaredFunc(Var func_var); 301 302 Result CheckIndex(Var var, Index max_index, const char* desc); 303 template <typename T> 304 Result CheckIndexWithValue(Var var, 305 const std::vector<T>& values, 306 T* out, 307 const char* desc); 308 Result CheckFuncTypeIndex(Var sig_var, FuncType* out = nullptr); 309 Result CheckFuncIndex(Var func_var, FuncType* out = nullptr); 310 Result CheckTableIndex(Var table_var, TableType* out = nullptr); 311 Result CheckMemoryIndex(Var memory_var, MemoryType* out = nullptr); 312 Result CheckGlobalIndex(Var global_var, GlobalType* out = nullptr); 313 Result CheckTagIndex(Var tag_var, TagType* out = nullptr); 314 Result CheckElemSegmentIndex(Var elem_segment_var, ElemType* out = nullptr); 315 Result CheckDataSegmentIndex(Var data_segment_var); 316 317 Result CheckAlign(const Location&, Address align, Address natural_align); 318 Result CheckOffset(const Location&, Address offset, const Limits& limits); 319 Result CheckAtomicAlign(const Location&, 320 Address align, 321 Address natural_align); 322 323 Result CheckBlockSignature(const Location&, 324 Opcode, 325 Type sig_type, 326 TypeVector* out_param_types, 327 TypeVector* out_result_types); 328 329 Index GetFunctionTypeIndex(Index func_index) const; 330 331 TypeVector ToTypeVector(Index count, const Type* types); 332 333 ValidateOptions options_; 334 Errors* errors_; 335 TypeChecker typechecker_; // TODO: Move into SharedValidator. 336 // Cached for access by OnTypecheckerError. 337 Location expr_loc_ = Location(kInvalidOffset); 338 bool in_init_expr_ = false; 339 340 Index num_types_ = 0; 341 std::map<Index, FuncType> func_types_; 342 std::map<Index, StructType> struct_types_; 343 std::map<Index, ArrayType> array_types_; 344 345 std::vector<FuncType> funcs_; // Includes imported and defined. 346 std::vector<TableType> tables_; // Includes imported and defined. 347 std::vector<MemoryType> memories_; // Includes imported and defined. 348 std::vector<GlobalType> globals_; // Includes imported and defined. 349 std::vector<TagType> tags_; // Includes imported and defined. 350 std::vector<ElemType> elems_; 351 Index starts_ = 0; 352 Index num_imported_globals_ = 0; 353 Index data_segments_ = 0; 354 355 // Includes parameters, since this is only used for validating 356 // local.{get,set,tee} instructions. 357 std::vector<LocalDecl> locals_; 358 359 std::set<std::string> export_names_; // Used to check for duplicates. 360 std::set<Index> declared_funcs_; // TODO: optimize? 361 std::vector<Var> check_declared_funcs_; 362 }; 363 364 } // namespace wabt 365 366 #endif // WABT_SHARED_VALIDATOR_H_