Line data Source code
1 : // Copyright 2017 the V8 project authors. All rights reserved.
2 : // Use of this source code is governed by a BSD-style license that can be
3 : // found in the LICENSE file.
4 :
5 : #ifndef V8_ASMJS_ASM_PARSER_H_
6 : #define V8_ASMJS_ASM_PARSER_H_
7 :
8 : #include <memory>
9 : #include <string>
10 :
11 : #include "src/asmjs/asm-scanner.h"
12 : #include "src/asmjs/asm-types.h"
13 : #include "src/base/enum-set.h"
14 : #include "src/vector.h"
15 : #include "src/wasm/wasm-module-builder.h"
16 : #include "src/zone/zone-containers.h"
17 :
18 : namespace v8 {
19 : namespace internal {
20 :
21 : class Utf16CharacterStream;
22 :
23 : namespace wasm {
24 :
25 : // A custom parser + validator + wasm converter for asm.js:
26 : // http://asmjs.org/spec/latest/
27 : // This parser intentionally avoids the portion of JavaScript parsing
28 : // that are not required to determine if code is valid asm.js code.
29 : // * It is mostly one pass.
30 : // * It bails out on unexpected input.
31 : // * It assumes strict ordering insofar as permitted by asm.js validation rules.
32 : // * It relies on a custom scanner that provides de-duped identifiers in two
33 : // scopes (local + module wide).
34 3742 : class AsmJsParser {
35 : public:
36 : // clang-format off
37 : enum StandardMember {
38 : kInfinity,
39 : kNaN,
40 : #define V(_unused1, name, _unused2, _unused3) kMath##name,
41 : STDLIB_MATH_FUNCTION_LIST(V)
42 : #undef V
43 : #define V(name, _unused1) kMath##name,
44 : STDLIB_MATH_VALUE_LIST(V)
45 : #undef V
46 : #define V(name, _unused1, _unused2, _unused3) k##name,
47 : STDLIB_ARRAY_TYPE_LIST(V)
48 : #undef V
49 : };
50 : // clang-format on
51 :
52 : using StdlibSet = base::EnumSet<StandardMember, uint64_t>;
53 :
54 : explicit AsmJsParser(Zone* zone, uintptr_t stack_limit,
55 : Utf16CharacterStream* stream);
56 : bool Run();
57 : const char* failure_message() const { return failure_message_; }
58 : int failure_location() const { return failure_location_; }
59 : WasmModuleBuilder* module_builder() { return module_builder_; }
60 : const StdlibSet* stdlib_uses() const { return &stdlib_uses_; }
61 :
62 : private:
63 : // clang-format off
64 : enum class VarKind {
65 : kUnused,
66 : kLocal,
67 : kGlobal,
68 : kSpecial,
69 : kFunction,
70 : kTable,
71 : kImportedFunction,
72 : #define V(_unused0, Name, _unused1, _unused2) kMath##Name,
73 : STDLIB_MATH_FUNCTION_LIST(V)
74 : #undef V
75 : #define V(Name, _unused1) kMath##Name,
76 : STDLIB_MATH_VALUE_LIST(V)
77 : #undef V
78 : };
79 : // clang-format on
80 :
81 : // A single import in asm.js can require multiple imports in wasm, if the
82 : // function is used with different signatures. {cache} keeps the wasm
83 : // imports for the single asm.js import of name {function_name}.
84 : struct FunctionImportInfo {
85 : Vector<const char> function_name;
86 : ZoneUnorderedMap<FunctionSig, uint32_t> cache;
87 :
88 : // Constructor.
89 : FunctionImportInfo(Vector<const char> name, Zone* zone)
90 2091 : : function_name(name), cache(zone) {}
91 : };
92 :
93 606985 : struct VarInfo {
94 : AsmType* type = AsmType::None();
95 : WasmFunctionBuilder* function_builder = nullptr;
96 : FunctionImportInfo* import = nullptr;
97 : uint32_t mask = 0;
98 : uint32_t index = 0;
99 : VarKind kind = VarKind::kUnused;
100 : bool mutable_variable = true;
101 : bool function_defined = false;
102 : };
103 :
104 : struct GlobalImport {
105 : Vector<const char> import_name;
106 : ValueType value_type;
107 : VarInfo* var_info;
108 : };
109 :
110 : enum class BlockKind { kRegular, kLoop, kOther };
111 :
112 : struct BlockInfo {
113 : BlockKind kind;
114 : AsmJsScanner::token_t label;
115 : };
116 :
117 : // Helper class to make {TempVariable} safe for nesting.
118 : class TemporaryVariableScope;
119 :
120 : template <typename T>
121 14968 : class CachedVectors {
122 : public:
123 : explicit CachedVectors(Zone* zone) : reusable_vectors_(zone) {}
124 :
125 : Zone* zone() const { return reusable_vectors_.get_allocator().zone(); }
126 :
127 : inline void fill(ZoneVector<T>* vec) {
128 133186 : if (reusable_vectors_.empty()) return;
129 : reusable_vectors_.back().swap(*vec);
130 : reusable_vectors_.pop_back();
131 : vec->clear();
132 : }
133 :
134 : inline void reuse(ZoneVector<T>* vec) {
135 133186 : reusable_vectors_.emplace_back(std::move(*vec));
136 : }
137 :
138 : private:
139 : ZoneVector<ZoneVector<T>> reusable_vectors_;
140 : };
141 :
142 : template <typename T>
143 : class CachedVector final : public ZoneVector<T> {
144 : public:
145 133186 : explicit CachedVector(CachedVectors<T>& cache)
146 133186 : : ZoneVector<T>(cache.zone()), cache_(&cache) {
147 : cache.fill(this);
148 133186 : }
149 205689 : ~CachedVector() { cache_->reuse(this); }
150 :
151 : private:
152 : CachedVectors<T>* cache_;
153 : };
154 :
155 : Zone* zone_;
156 : AsmJsScanner scanner_;
157 : WasmModuleBuilder* module_builder_;
158 : WasmFunctionBuilder* current_function_builder_;
159 : AsmType* return_type_;
160 : uintptr_t stack_limit_;
161 : StdlibSet stdlib_uses_;
162 : ZoneVector<VarInfo> global_var_info_;
163 : ZoneVector<VarInfo> local_var_info_;
164 :
165 : CachedVectors<ValueType> cached_valuetype_vectors_{zone_};
166 : CachedVectors<AsmType*> cached_asm_type_p_vectors_{zone_};
167 : CachedVectors<AsmJsScanner::token_t> cached_token_t_vectors_{zone_};
168 : CachedVectors<int32_t> cached_int_vectors_{zone_};
169 :
170 : int function_temp_locals_offset_;
171 : int function_temp_locals_used_;
172 : int function_temp_locals_depth_;
173 :
174 : // Error Handling related
175 : bool failed_;
176 : const char* failure_message_;
177 : int failure_location_;
178 :
179 : // Module Related.
180 : AsmJsScanner::token_t stdlib_name_;
181 : AsmJsScanner::token_t foreign_name_;
182 : AsmJsScanner::token_t heap_name_;
183 :
184 : static const AsmJsScanner::token_t kTokenNone = 0;
185 :
186 : // Track if parsing a heap assignment.
187 : bool inside_heap_assignment_;
188 : AsmType* heap_access_type_;
189 :
190 : ZoneVector<BlockInfo> block_stack_;
191 :
192 : // Types used for stdlib function and their set up.
193 : AsmType* stdlib_dq2d_;
194 : AsmType* stdlib_dqdq2d_;
195 : AsmType* stdlib_i2s_;
196 : AsmType* stdlib_ii2s_;
197 : AsmType* stdlib_minmax_;
198 : AsmType* stdlib_abs_;
199 : AsmType* stdlib_ceil_like_;
200 : AsmType* stdlib_fround_;
201 :
202 : // When making calls, the return type is needed to lookup signatures.
203 : // For `+callsite(..)` or `fround(callsite(..))` use this value to pass
204 : // along the coercion.
205 : AsmType* call_coercion_;
206 :
207 : // The source position associated with the above {call_coercion}.
208 : size_t call_coercion_position_;
209 :
210 : // When making calls, the coercion can also appear in the source stream
211 : // syntactically "behind" the call site. For `callsite(..)|0` use this
212 : // value to flag that such a coercion must happen.
213 : AsmType* call_coercion_deferred_;
214 :
215 : // The source position at which requesting a deferred coercion via the
216 : // aforementioned {call_coercion_deferred} is allowed.
217 : size_t call_coercion_deferred_position_;
218 :
219 : // The code position of the last heap access shift by an immediate value.
220 : // For `heap[expr >> value:NumericLiteral]` this indicates from where to
221 : // delete code when the expression is used as part of a valid heap access.
222 : // Will be set to {kNoHeapAccessShift} if heap access shift wasn't matched.
223 : size_t heap_access_shift_position_;
224 : uint32_t heap_access_shift_value_;
225 : static const size_t kNoHeapAccessShift = -1;
226 :
227 : // Used to track the last label we've seen so it can be matched to later
228 : // statements it's attached to.
229 : AsmJsScanner::token_t pending_label_;
230 :
231 : // Global imports. The list of imported variables that are copied during
232 : // module instantiation into a corresponding global variable.
233 : ZoneLinkedList<GlobalImport> global_imports_;
234 :
235 : Zone* zone() { return zone_; }
236 :
237 : inline bool Peek(AsmJsScanner::token_t token) {
238 106362 : return scanner_.Token() == token;
239 : }
240 :
241 : inline bool Check(AsmJsScanner::token_t token) {
242 20077690 : if (scanner_.Token() == token) {
243 1662349 : scanner_.Next();
244 : return true;
245 : } else {
246 : return false;
247 : }
248 : }
249 :
250 : inline bool CheckForZero() {
251 270735 : if (scanner_.IsUnsigned() && scanner_.AsUnsigned() == 0) {
252 262283 : scanner_.Next();
253 : return true;
254 : } else {
255 : return false;
256 : }
257 : }
258 :
259 : inline bool CheckForDouble(double* value) {
260 1279248 : if (scanner_.IsDouble()) {
261 : *value = scanner_.AsDouble();
262 9588 : scanner_.Next();
263 : return true;
264 : } else {
265 : return false;
266 : }
267 : }
268 :
269 : inline bool CheckForUnsigned(uint32_t* value) {
270 1656597 : if (scanner_.IsUnsigned()) {
271 : *value = scanner_.AsUnsigned();
272 1479736 : scanner_.Next();
273 : return true;
274 : } else {
275 : return false;
276 : }
277 : }
278 :
279 : inline bool CheckForUnsignedBelow(uint32_t limit, uint32_t* value) {
280 1604112 : if (scanner_.IsUnsigned() && scanner_.AsUnsigned() < limit) {
281 : *value = scanner_.AsUnsigned();
282 730403 : scanner_.Next();
283 : return true;
284 : } else {
285 : return false;
286 : }
287 : }
288 :
289 : inline AsmJsScanner::token_t Consume() {
290 : AsmJsScanner::token_t ret = scanner_.Token();
291 1341663 : scanner_.Next();
292 : return ret;
293 : }
294 :
295 : void SkipSemicolon();
296 :
297 : VarInfo* GetVarInfo(AsmJsScanner::token_t token);
298 : uint32_t VarIndex(VarInfo* info);
299 : void DeclareGlobal(VarInfo* info, bool mutable_variable, AsmType* type,
300 : ValueType vtype,
301 : const WasmInitExpr& init = WasmInitExpr());
302 : void DeclareStdlibFunc(VarInfo* info, VarKind kind, AsmType* type);
303 : void AddGlobalImport(Vector<const char> name, AsmType* type, ValueType vtype,
304 : bool mutable_variable, VarInfo* info);
305 :
306 : // Allocates a temporary local variable. The given {index} is absolute within
307 : // the function body, consider using {TemporaryVariableScope} when nesting.
308 : uint32_t TempVariable(int index);
309 :
310 : // Preserves a copy of the scanner's current identifier string in the zone.
311 : Vector<const char> CopyCurrentIdentifierString();
312 :
313 : // Use to set up block stack layers (including synthetic ones for if-else).
314 : // Begin/Loop/End below are implemented with these plus code generation.
315 : void BareBegin(BlockKind kind = BlockKind::kOther,
316 : AsmJsScanner::token_t label = 0);
317 : void BareEnd();
318 : int FindContinueLabelDepth(AsmJsScanner::token_t label);
319 : int FindBreakLabelDepth(AsmJsScanner::token_t label);
320 :
321 : // Use to set up actual wasm blocks/loops.
322 : void Begin(AsmJsScanner::token_t label = 0);
323 : void Loop(AsmJsScanner::token_t label = 0);
324 : void End();
325 :
326 : void InitializeStdlibTypes();
327 :
328 : FunctionSig* ConvertSignature(AsmType* return_type,
329 : const ZoneVector<AsmType*>& params);
330 :
331 : void ValidateModule(); // 6.1 ValidateModule
332 : void ValidateModuleParameters(); // 6.1 ValidateModule - parameters
333 : void ValidateModuleVars(); // 6.1 ValidateModule - variables
334 : void ValidateModuleVar(bool mutable_variable);
335 : void ValidateModuleVarImport(VarInfo* info, bool mutable_variable);
336 : void ValidateModuleVarStdlib(VarInfo* info);
337 : void ValidateModuleVarNewStdlib(VarInfo* info);
338 : void ValidateModuleVarFromGlobal(VarInfo* info, bool mutable_variable);
339 :
340 : void ValidateExport(); // 6.2 ValidateExport
341 : void ValidateFunctionTable(); // 6.3 ValidateFunctionTable
342 : void ValidateFunction(); // 6.4 ValidateFunction
343 : void ValidateFunctionParams(ZoneVector<AsmType*>* params);
344 : void ValidateFunctionLocals(size_t param_count,
345 : ZoneVector<ValueType>* locals);
346 : void ValidateStatement(); // 6.5 ValidateStatement
347 : void Block(); // 6.5.1 Block
348 : void ExpressionStatement(); // 6.5.2 ExpressionStatement
349 : void EmptyStatement(); // 6.5.3 EmptyStatement
350 : void IfStatement(); // 6.5.4 IfStatement
351 : void ReturnStatement(); // 6.5.5 ReturnStatement
352 : bool IterationStatement(); // 6.5.6 IterationStatement
353 : void WhileStatement(); // 6.5.6 IterationStatement - while
354 : void DoStatement(); // 6.5.6 IterationStatement - do
355 : void ForStatement(); // 6.5.6 IterationStatement - for
356 : void BreakStatement(); // 6.5.7 BreakStatement
357 : void ContinueStatement(); // 6.5.8 ContinueStatement
358 : void LabelledStatement(); // 6.5.9 LabelledStatement
359 : void SwitchStatement(); // 6.5.10 SwitchStatement
360 : void ValidateCase(); // 6.6. ValidateCase
361 : void ValidateDefault(); // 6.7 ValidateDefault
362 : AsmType* ValidateExpression(); // 6.8 ValidateExpression
363 : AsmType* Expression(AsmType* expect); // 6.8.1 Expression
364 : AsmType* NumericLiteral(); // 6.8.2 NumericLiteral
365 : AsmType* Identifier(); // 6.8.3 Identifier
366 : AsmType* CallExpression(); // 6.8.4 CallExpression
367 : AsmType* MemberExpression(); // 6.8.5 MemberExpression
368 : AsmType* AssignmentExpression(); // 6.8.6 AssignmentExpression
369 : AsmType* UnaryExpression(); // 6.8.7 UnaryExpression
370 : AsmType* MultiplicativeExpression(); // 6.8.8 MultiplicativeExpression
371 : AsmType* AdditiveExpression(); // 6.8.9 AdditiveExpression
372 : AsmType* ShiftExpression(); // 6.8.10 ShiftExpression
373 : AsmType* RelationalExpression(); // 6.8.11 RelationalExpression
374 : AsmType* EqualityExpression(); // 6.8.12 EqualityExpression
375 : AsmType* BitwiseANDExpression(); // 6.8.13 BitwiseANDExpression
376 : AsmType* BitwiseXORExpression(); // 6.8.14 BitwiseXORExpression
377 : AsmType* BitwiseORExpression(); // 6.8.15 BitwiseORExpression
378 : AsmType* ConditionalExpression(); // 6.8.16 ConditionalExpression
379 : AsmType* ParenthesizedExpression(); // 6.8.17 ParenthesiedExpression
380 : AsmType* ValidateCall(); // 6.9 ValidateCall
381 : bool PeekCall(); // 6.9 ValidateCall - helper
382 : void ValidateHeapAccess(); // 6.10 ValidateHeapAccess
383 : void ValidateFloatCoercion(); // 6.11 ValidateFloatCoercion
384 :
385 : // Used as part of {ForStatement}. Scans forward to the next `)` in order to
386 : // skip over the third expression in a for-statement. This is one piece that
387 : // makes this parser not be a pure single-pass.
388 : void ScanToClosingParenthesis();
389 :
390 : // Used as part of {SwitchStatement}. Collects all case labels in the current
391 : // switch-statement, then resets the scanner position. This is one piece that
392 : // makes this parser not be a pure single-pass.
393 : void GatherCases(ZoneVector<int32_t>* cases);
394 : };
395 :
396 : } // namespace wasm
397 : } // namespace internal
398 : } // namespace v8
399 :
400 : #endif // V8_ASMJS_ASM_PARSER_H_
|