1/*
2 * Copyright (C) 2016 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#pragma once
27
28#if ENABLE(WEBASSEMBLY)
29
30#if ASSERT_DISABLED
31IGNORE_RETURN_TYPE_WARNINGS_BEGIN
32#endif
33
34namespace JSC { namespace Wasm {
35
36#define FOR_EACH_KNOWN_WASM_SECTION(macro) \
37 macro(Type, 1, "Function signature declarations") \
38 macro(Import, 2, "Import declarations") \
39 macro(Function, 3, "Function declarations") \
40 macro(Table, 4, "Indirect function table and other tables") \
41 macro(Memory, 5, "Memory attributes") \
42 macro(Global, 6, "Global declarations") \
43 macro(Export, 7, "Exports") \
44 macro(Start, 8, "Start function declaration") \
45 macro(Element, 9, "Elements section") \
46 macro(Code, 10, "Function bodies (code)") \
47 macro(Data, 11, "Data segments")
48
49enum class Section : uint8_t {
50 // It's important that Begin is less than every other section number and that Custom is greater.
51 // This only works because section numbers are currently monotonically increasing.
52 // Also, Begin is not a real section but is used as a marker for validating the ordering
53 // of sections.
54 Begin = 0,
55#define DEFINE_WASM_SECTION_ENUM(NAME, ID, DESCRIPTION) NAME = ID,
56 FOR_EACH_KNOWN_WASM_SECTION(DEFINE_WASM_SECTION_ENUM)
57#undef DEFINE_WASM_SECTION_ENUM
58 Custom
59};
60static_assert(static_cast<uint8_t>(Section::Begin) < static_cast<uint8_t>(Section::Type), "Begin should come before the first known section.");
61
62template<typename Int>
63inline bool isKnownSection(Int section)
64{
65 switch (section) {
66#define VALIDATE_SECTION(NAME, ID, DESCRIPTION) case static_cast<Int>(Section::NAME): return true;
67 FOR_EACH_KNOWN_WASM_SECTION(VALIDATE_SECTION)
68#undef VALIDATE_SECTION
69 default:
70 return false;
71 }
72}
73
74inline bool decodeSection(uint8_t sectionByte, Section& section)
75{
76 section = Section::Custom;
77 if (!sectionByte)
78 return true;
79
80 if (!isKnownSection(sectionByte))
81 return false;
82
83 section = static_cast<Section>(sectionByte);
84 return true;
85}
86
87inline bool validateOrder(Section previousKnown, Section next)
88{
89 ASSERT(isKnownSection(previousKnown) || previousKnown == Section::Begin);
90 return static_cast<uint8_t>(previousKnown) < static_cast<uint8_t>(next);
91}
92
93inline const char* makeString(Section section)
94{
95 switch (section) {
96 case Section::Begin:
97 return "Begin";
98 case Section::Custom:
99 return "Custom";
100#define STRINGIFY_SECTION_NAME(NAME, ID, DESCRIPTION) case Section::NAME: return #NAME;
101 FOR_EACH_KNOWN_WASM_SECTION(STRINGIFY_SECTION_NAME)
102#undef STRINGIFY_SECTION_NAME
103 }
104 ASSERT_NOT_REACHED();
105}
106
107} } // namespace JSC::Wasm
108
109#if ASSERT_DISABLED
110IGNORE_RETURN_TYPE_WARNINGS_END
111#endif
112
113#endif // ENABLE(WEBASSEMBLY)
114