From 36d22d82aa202bb199967e9512281e9a53db42c9 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 21:33:14 +0200 Subject: Adding upstream version 115.7.0esr. Signed-off-by: Daniel Baumann --- third_party/wasm2c/src/test-binary-reader.cc | 75 ++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 third_party/wasm2c/src/test-binary-reader.cc (limited to 'third_party/wasm2c/src/test-binary-reader.cc') diff --git a/third_party/wasm2c/src/test-binary-reader.cc b/third_party/wasm2c/src/test-binary-reader.cc new file mode 100644 index 0000000000..b3e3540d4f --- /dev/null +++ b/third_party/wasm2c/src/test-binary-reader.cc @@ -0,0 +1,75 @@ +/* + * Copyright 2018 WebAssembly Community Group participants + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "gtest/gtest.h" + +#include "wabt/binary-reader-nop.h" +#include "wabt/binary-reader.h" +#include "wabt/leb128.h" +#include "wabt/opcode.h" + +using namespace wabt; + +namespace { + +struct BinaryReaderError : BinaryReaderNop { + bool OnError(const Error& error) override { + first_error = error; + return true; // Error handled. + } + + Error first_error; +}; + +} // End of anonymous namespace + +TEST(BinaryReader, DisabledOpcodes) { + // Use the default features. + ReadBinaryOptions options; + + // Loop through all opcodes. + for (uint32_t i = 0; i < static_cast(Opcode::Invalid); ++i) { + Opcode opcode(static_cast(i)); + if (opcode.IsEnabled(options.features)) { + continue; + } + + // Use a shorter name to make the clang-formatted table below look nicer. + std::vector b = opcode.GetBytes(); + assert(b.size() <= 3); + b.resize(3); + + uint8_t data[] = { + 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, // magic + version + 0x01, 0x04, 0x01, 0x60, 0x00, 0x00, // type section: 1 type, (func) + 0x03, 0x02, 0x01, 0x00, // func section: 1 func, type 0 + 0x0a, 0x07, 0x01, 0x05, 0x00, // code section: 1 func, 0 locals + b[0], b[1], b[2], // The instruction, padded with zeroes + 0x0b, // end + }; + const size_t size = sizeof(data); + + BinaryReaderError reader; + Result result = ReadBinary(data, size, &reader, options); + EXPECT_EQ(Result::Error, result); + + // This relies on the binary reader checking whether the opcode is allowed + // before reading any further data needed by the instruction. + const std::string& message = reader.first_error.message; + EXPECT_EQ(0u, message.find("unexpected opcode")) + << "Got error message: " << message; + } +} -- cgit v1.2.3