2 * Copyright 2018 WebAssembly Community Group participants
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "gtest/gtest.h"
19 #include "wabt/binary-reader-nop.h"
20 #include "wabt/binary-reader.h"
21 #include "wabt/leb128.h"
22 #include "wabt/opcode.h"
28 struct BinaryReaderError
: BinaryReaderNop
{
29 bool OnError(const Error
& error
) override
{
31 return true; // Error handled.
37 } // End of anonymous namespace
39 TEST(BinaryReader
, DisabledOpcodes
) {
40 // Use the default features.
41 ReadBinaryOptions options
;
43 // Loop through all opcodes.
44 for (uint32_t i
= 0; i
< static_cast<uint32_t>(Opcode::Invalid
); ++i
) {
45 Opcode
opcode(static_cast<Opcode::Enum
>(i
));
46 if (opcode
.IsEnabled(options
.features
)) {
50 // Use a shorter name to make the clang-formatted table below look nicer.
51 std::vector
<uint8_t> b
= opcode
.GetBytes();
52 assert(b
.size() <= 3);
56 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, // magic + version
57 0x01, 0x04, 0x01, 0x60, 0x00, 0x00, // type section: 1 type, (func)
58 0x03, 0x02, 0x01, 0x00, // func section: 1 func, type 0
59 0x0a, 0x07, 0x01, 0x05, 0x00, // code section: 1 func, 0 locals
60 b
[0], b
[1], b
[2], // The instruction, padded with zeroes
63 const size_t size
= sizeof(data
);
65 BinaryReaderError reader
;
66 Result result
= ReadBinary(data
, size
, &reader
, options
);
67 EXPECT_EQ(Result::Error
, result
);
69 // This relies on the binary reader checking whether the opcode is allowed
70 // before reading any further data needed by the instruction.
71 const std::string
& message
= reader
.first_error
.message
;
72 EXPECT_EQ(0u, message
.find("unexpected opcode"))
73 << "Got error message: " << message
;