1 /*
2  * Copyright 2017 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_BINARY_READER_LOGGING_H_
18 #define WABT_BINARY_READER_LOGGING_H_
19 
20 #include "src/binary-reader.h"
21 
22 namespace wabt {
23 
24 class Stream;
25 
26 class BinaryReaderLogging : public BinaryReaderDelegate {
27  public:
28   BinaryReaderLogging(Stream*, BinaryReaderDelegate* forward);
29 
30   bool OnError(const Error&) override;
31   void OnSetState(const State* s) override;
32 
33   Result BeginModule(uint32_t version) override;
34   Result EndModule() override;
35 
36   Result BeginSection(Index section_index,
37                       BinarySection section_type,
38                       Offset size) override;
39 
40   Result BeginCustomSection(Index section_index,
41                             Offset size,
42                             string_view section_name) override;
43   Result EndCustomSection() override;
44 
45   Result BeginTypeSection(Offset size) override;
46   Result OnTypeCount(Index count) override;
47   Result OnFuncType(Index index,
48                     Index param_count,
49                     Type* param_types,
50                     Index result_count,
51                     Type* result_types) override;
52   Result OnStructType(Index index, Index field_count, TypeMut* fields) override;
53   Result OnArrayType(Index index, TypeMut field) override;
54   Result EndTypeSection() override;
55 
56   Result BeginImportSection(Offset size) override;
57   Result OnImportCount(Index count) override;
58   Result OnImport(Index index,
59                   ExternalKind kind,
60                   string_view module_name,
61                   string_view field_name) override;
62   Result OnImportFunc(Index import_index,
63                       string_view module_name,
64                       string_view field_name,
65                       Index func_index,
66                       Index sig_index) override;
67   Result OnImportTable(Index import_index,
68                        string_view module_name,
69                        string_view field_name,
70                        Index table_index,
71                        Type elem_type,
72                        const Limits* elem_limits) override;
73   Result OnImportMemory(Index import_index,
74                         string_view module_name,
75                         string_view field_name,
76                         Index memory_index,
77                         const Limits* page_limits) override;
78   Result OnImportGlobal(Index import_index,
79                         string_view module_name,
80                         string_view field_name,
81                         Index global_index,
82                         Type type,
83                         bool mutable_) override;
84   Result OnImportTag(Index import_index,
85                      string_view module_name,
86                      string_view field_name,
87                      Index tag_index,
88                      Index sig_index) override;
89   Result EndImportSection() override;
90 
91   Result BeginFunctionSection(Offset size) override;
92   Result OnFunctionCount(Index count) override;
93   Result OnFunction(Index index, Index sig_index) override;
94   Result EndFunctionSection() override;
95 
96   Result BeginTableSection(Offset size) override;
97   Result OnTableCount(Index count) override;
98   Result OnTable(Index index,
99                  Type elem_type,
100                  const Limits* elem_limits) override;
101   Result EndTableSection() override;
102 
103   Result BeginMemorySection(Offset size) override;
104   Result OnMemoryCount(Index count) override;
105   Result OnMemory(Index index, const Limits* limits) override;
106   Result EndMemorySection() override;
107 
108   Result BeginGlobalSection(Offset size) override;
109   Result OnGlobalCount(Index count) override;
110   Result BeginGlobal(Index index, Type type, bool mutable_) override;
111   Result BeginGlobalInitExpr(Index index) override;
112   Result EndGlobalInitExpr(Index index) override;
113   Result EndGlobal(Index index) override;
114   Result EndGlobalSection() override;
115 
116   Result BeginExportSection(Offset size) override;
117   Result OnExportCount(Index count) override;
118   Result OnExport(Index index,
119                   ExternalKind kind,
120                   Index item_index,
121                   string_view name) override;
122   Result EndExportSection() override;
123 
124   Result BeginStartSection(Offset size) override;
125   Result OnStartFunction(Index func_index) override;
126   Result EndStartSection() override;
127 
128   Result BeginCodeSection(Offset size) override;
129   Result OnFunctionBodyCount(Index count) override;
130   Result BeginFunctionBody(Index index, Offset size) override;
131   Result OnLocalDeclCount(Index count) override;
132   Result OnLocalDecl(Index decl_index, Index count, Type type) override;
133 
134   Result OnOpcode(Opcode opcode) override;
135   Result OnOpcodeBare() override;
136   Result OnOpcodeIndex(Index value) override;
137   Result OnOpcodeIndexIndex(Index value, Index value2) override;
138   Result OnOpcodeUint32(uint32_t value) override;
139   Result OnOpcodeUint32Uint32(uint32_t value, uint32_t value2) override;
140   Result OnOpcodeUint32Uint32Uint32(uint32_t value,
141                                     uint32_t value2,
142                                     uint32_t value3) override;
143   Result OnOpcodeUint64(uint64_t value) override;
144   Result OnOpcodeF32(uint32_t value) override;
145   Result OnOpcodeF64(uint64_t value) override;
146   Result OnOpcodeV128(v128 value) override;
147   Result OnOpcodeBlockSig(Type sig_type) override;
148   Result OnOpcodeType(Type type) override;
149   Result OnAtomicLoadExpr(Opcode opcode,
150                           Address alignment_log2,
151                           Address offset) override;
152   Result OnAtomicStoreExpr(Opcode opcode,
153                            Address alignment_log2,
154                            Address offset) override;
155   Result OnAtomicRmwExpr(Opcode opcode,
156                          Address alignment_log2,
157                          Address offset) override;
158   Result OnAtomicRmwCmpxchgExpr(Opcode opcode,
159                                 Address alignment_log2,
160                                 Address offset) override;
161   Result OnBinaryExpr(Opcode opcode) override;
162   Result OnBlockExpr(Type sig_type) override;
163   Result OnBrExpr(Index depth) override;
164   Result OnBrIfExpr(Index depth) override;
165   Result OnBrTableExpr(Index num_targets,
166                        Index* target_depths,
167                        Index default_target_depth) override;
168   Result OnCallExpr(Index func_index) override;
169   Result OnCatchExpr(Index tag_index) override;
170   Result OnCatchAllExpr() override;
171   Result OnCallIndirectExpr(Index sig_index, Index table_index) override;
172   Result OnCallRefExpr() override;
173   Result OnCompareExpr(Opcode opcode) override;
174   Result OnConvertExpr(Opcode opcode) override;
175   Result OnDelegateExpr(Index depth) override;
176   Result OnDropExpr() override;
177   Result OnElseExpr() override;
178   Result OnEndExpr() override;
179   Result OnEndFunc() override;
180   Result OnF32ConstExpr(uint32_t value_bits) override;
181   Result OnF64ConstExpr(uint64_t value_bits) override;
182   Result OnV128ConstExpr(v128 value_bits) override;
183   Result OnGlobalGetExpr(Index global_index) override;
184   Result OnGlobalSetExpr(Index global_index) override;
185   Result OnI32ConstExpr(uint32_t value) override;
186   Result OnI64ConstExpr(uint64_t value) override;
187   Result OnIfExpr(Type sig_type) override;
188   Result OnLoadExpr(Opcode opcode,
189                     Address alignment_log2,
190                     Address offset) override;
191   Result OnLocalGetExpr(Index local_index) override;
192   Result OnLocalSetExpr(Index local_index) override;
193   Result OnLocalTeeExpr(Index local_index) override;
194   Result OnLoopExpr(Type sig_type) override;
195   Result OnMemoryCopyExpr() override;
196   Result OnDataDropExpr(Index segment_index) override;
197   Result OnMemoryFillExpr() override;
198   Result OnMemoryGrowExpr() override;
199   Result OnMemoryInitExpr(Index segment_index) override;
200   Result OnMemorySizeExpr() override;
201   Result OnTableCopyExpr(Index dst_index, Index src_index) override;
202   Result OnElemDropExpr(Index segment_index) override;
203   Result OnTableInitExpr(Index segment_index, Index table_index) override;
204   Result OnTableGetExpr(Index table) override;
205   Result OnTableSetExpr(Index table) override;
206   Result OnTableGrowExpr(Index table) override;
207   Result OnTableSizeExpr(Index table) override;
208   Result OnTableFillExpr(Index table) override;
209   Result OnRefFuncExpr(Index index) override;
210   Result OnRefNullExpr(Type type) override;
211   Result OnRefIsNullExpr() override;
212   Result OnNopExpr() override;
213   Result OnRethrowExpr(Index depth) override;
214   Result OnReturnCallExpr(Index func_index) override;
215   Result OnReturnCallIndirectExpr(Index sig_index, Index table_index) override;
216   Result OnReturnExpr() override;
217   Result OnSelectExpr(Index result_count, Type* result_types) override;
218   Result OnStoreExpr(Opcode opcode,
219                      Address alignment_log2,
220                      Address offset) override;
221   Result OnThrowExpr(Index tag_index) override;
222   Result OnTryExpr(Type sig_type) override;
223   Result OnUnaryExpr(Opcode opcode) override;
224   Result OnTernaryExpr(Opcode opcode) override;
225   Result OnUnreachableExpr() override;
226   Result OnAtomicWaitExpr(Opcode opcode,
227                           Address alignment_log2,
228                           Address offset) override;
229   Result OnAtomicFenceExpr(uint32_t consistency_model) override;
230   Result OnAtomicNotifyExpr(Opcode opcode,
231                             Address alignment_log2,
232                             Address offset) override;
233   Result EndFunctionBody(Index index) override;
234   Result EndCodeSection() override;
235   Result OnSimdLaneOpExpr(Opcode opcode, uint64_t value) override;
236   Result OnSimdLoadLaneExpr(Opcode opcode,
237                             Address alignment_log2,
238                             Address offset,
239                             uint64_t value) override;
240   Result OnSimdStoreLaneExpr(Opcode opcode,
241                              Address alignment_log2,
242                              Address offset,
243                              uint64_t value) override;
244   Result OnSimdShuffleOpExpr(Opcode opcode, v128 value) override;
245   Result OnLoadSplatExpr(Opcode opcode,
246                          Address alignment_log2,
247                          Address offset) override;
248   Result OnLoadZeroExpr(Opcode opcode,
249                         Address alignment_log2,
250                         Address offset) override;
251 
252   Result BeginElemSection(Offset size) override;
253   Result OnElemSegmentCount(Index count) override;
254   Result BeginElemSegment(Index index,
255                           Index table_index,
256                           uint8_t flags) override;
257   Result BeginElemSegmentInitExpr(Index index) override;
258   Result EndElemSegmentInitExpr(Index index) override;
259   Result OnElemSegmentElemType(Index index, Type elem_type) override;
260   Result OnElemSegmentElemExprCount(Index index, Index count) override;
261   Result OnElemSegmentElemExpr_RefNull(Index segment_index, Type type) override;
262   Result OnElemSegmentElemExpr_RefFunc(Index segment_index,
263                                        Index func_index) override;
264   Result EndElemSegment(Index index) override;
265   Result EndElemSection() override;
266 
267   Result BeginDataSection(Offset size) override;
268   Result OnDataSegmentCount(Index count) override;
269   Result BeginDataSegment(Index index,
270                           Index memory_index,
271                           uint8_t flags) override;
272   Result BeginDataSegmentInitExpr(Index index) override;
273   Result EndDataSegmentInitExpr(Index index) override;
274   Result OnDataSegmentData(Index index,
275                            const void* data,
276                            Address size) override;
277   Result EndDataSegment(Index index) override;
278   Result EndDataSection() override;
279 
280   Result BeginDataCountSection(Offset size) override;
281   Result OnDataCount(Index count) override;
282   Result EndDataCountSection() override;
283 
284   Result BeginNamesSection(Offset size) override;
285   Result OnModuleNameSubsection(Index index,
286                                 uint32_t name_type,
287                                 Offset subsection_size) override;
288   Result OnModuleName(string_view name) override;
289   Result OnFunctionNameSubsection(Index index,
290                                   uint32_t name_type,
291                                   Offset subsection_size) override;
292   Result OnFunctionNamesCount(Index num_functions) override;
293   Result OnFunctionName(Index function_index,
294                         string_view function_name) override;
295   Result OnLocalNameSubsection(Index index,
296                                uint32_t name_type,
297                                Offset subsection_size) override;
298   Result OnLocalNameFunctionCount(Index num_functions) override;
299   Result OnLocalNameLocalCount(Index function_index, Index num_locals) override;
300   Result OnLocalName(Index function_index,
301                      Index local_index,
302                      string_view local_name) override;
303   Result OnNameSubsection(Index index,
304                           NameSectionSubsection subsection_type,
305                           Offset subsection_size) override;
306   Result OnNameEntry(NameSectionSubsection type,
307                      Index index,
308                      string_view name) override;
309   Result OnNameCount(Index num_names) override;
310   Result EndNamesSection() override;
311 
312   Result BeginRelocSection(Offset size) override;
313   Result OnRelocCount(Index count, Index section_index) override;
314   Result OnReloc(RelocType type,
315                  Offset offset,
316                  Index index,
317                  uint32_t addend) override;
318   Result EndRelocSection() override;
319 
320   Result BeginDylinkSection(Offset size) override;
321   Result OnDylinkInfo(uint32_t mem_size,
322                       uint32_t mem_align,
323                       uint32_t table_size,
324                       uint32_t table_align) override;
325   Result OnDylinkNeededCount(Index count) override;
326   Result OnDylinkNeeded(string_view needed) override;
327   Result EndDylinkSection() override;
328 
329   Result BeginLinkingSection(Offset size) override;
330   Result OnSymbolCount(Index count) override;
331   Result OnDataSymbol(Index index,
332                       uint32_t flags,
333                       string_view name,
334                       Index segment,
335                       uint32_t offset,
336                       uint32_t size) override;
337   Result OnFunctionSymbol(Index index,
338                           uint32_t flags,
339                           string_view name,
340                           Index func_index) override;
341   Result OnGlobalSymbol(Index index,
342                         uint32_t flags,
343                         string_view name,
344                         Index global_index) override;
345   Result OnSectionSymbol(Index index,
346                          uint32_t flags,
347                          Index section_index) override;
348   Result OnTagSymbol(Index index,
349                      uint32_t flags,
350                      string_view name,
351                      Index tag_index) override;
352   Result OnTableSymbol(Index index,
353                        uint32_t flags,
354                        string_view name,
355                        Index tag_index) override;
356   Result OnSegmentInfoCount(Index count) override;
357   Result OnSegmentInfo(Index index,
358                        string_view name,
359                        Address alignment,
360                        uint32_t flags) override;
361   Result OnInitFunctionCount(Index count) override;
362   Result OnInitFunction(uint32_t priority, Index function_index) override;
363   Result OnComdatCount(Index count) override;
364   Result OnComdatBegin(string_view name, uint32_t flags, Index count) override;
365   Result OnComdatEntry(ComdatType kind, Index index) override;
366   Result EndLinkingSection() override;
367 
368   Result BeginTagSection(Offset size) override;
369   Result OnTagCount(Index count) override;
370   Result OnTagType(Index index, Index sig_index) override;
371   Result EndTagSection() override;
372 
373   Result OnInitExprF32ConstExpr(Index index, uint32_t value) override;
374   Result OnInitExprF64ConstExpr(Index index, uint64_t value) override;
375   Result OnInitExprV128ConstExpr(Index index, v128 value) override;
376   Result OnInitExprGlobalGetExpr(Index index, Index global_index) override;
377   Result OnInitExprI32ConstExpr(Index index, uint32_t value) override;
378   Result OnInitExprI64ConstExpr(Index index, uint64_t value) override;
379   Result OnInitExprRefNull(Index index, Type type) override;
380   Result OnInitExprRefFunc(Index index, Index func_index) override;
381 
382  private:
383   void Indent();
384   void Dedent();
385   void WriteIndent();
386   void LogType(Type type);
387   void LogTypes(Index type_count, Type* types);
388   void LogTypes(TypeVector& types);
389   void LogField(TypeMut field);
390 
391   Stream* stream_;
392   BinaryReaderDelegate* reader_;
393   int indent_;
394 };
395 
396 }  // namespace wabt
397 
398 #endif  // WABT_BINARY_READER_LOGGING_H_
399