Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

[test] Add read_model onnx fuzzer (#34433)

jszczepa337f63746c47
diff --git a/tests/fuzz/fuzz-testhelper/fuzz-utils.cc b/tests/fuzz/fuzz-testhelper/fuzz-utils.cc
index ef9bbc18155..bd6998c506a 100644
--- a/tests/fuzz/fuzz-testhelper/fuzz-utils.cc
+++ b/tests/fuzz/fuzz-testhelper/fuzz-utils.cc
@@ -1,15 +1,19 @@
// Copyright (C) 2018-2026 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
-
#include "fuzz-utils.h"
+
#include <stdexcept>
-#include <stdlib.h>
-#include <string.h>
#include <string>
+#include <atomic>
+#include <fstream>
+
#ifndef _WIN32
#include <unistd.h>
-#endif // _WIN32
+#include <stdlib.h>
+#include <string.h>
+#endif
+
MemoryFile::MemoryFile(const void *data, size_t size) {
#ifdef _WIN32
@@ -38,3 +42,59 @@ MemoryFile::~MemoryFile() {
free(m_name);
#endif // _WIN32
}
+
+
+bool write_file(const std::filesystem::path& p, std::string_view data) {
+ std::ofstream out(p, std::ios::binary);
+ if (!out) return false;
+ if (!data.empty())
+ out.write(data.data(), static_cast<std::streamsize>(data.size()));
+ return out.good();
+}
+
+uint64_t next_id() {
+ static std::atomic<uint64_t> id{0};
+ return ++id;
+}
+
+std::filesystem::path& temp_root() {
+ static std::filesystem::path root = []{
+ std::filesystem::path dir = std::filesystem::temp_directory_path() / "openvino_read_model_fuzz";
+ std::error_code ec;
+ std::filesystem::create_directories(dir, ec);
+ return ec ? std::filesystem::temp_directory_path() : dir;
+ }();
+ return root;
+}
+
+
+std::filesystem::path create_model_file(const uint8_t* data, size_t size, const std::filesystem::path& ext) {
+ auto name = std::filesystem::path("model_" + std::to_string(next_id()));
+ name += ext;
+ const auto path = temp_root() / name;
+ if (!write_file(path, std::string_view(reinterpret_cast<const char*>(data), size))) {
+ throw std::runtime_error("Write to file failed");
+ }
+ return path;
+}
+
+
+std::array<std::string_view, 2> split_data(std::string_view data, std::string_view delim) {
+ const auto pos = data.find(delim);
+ if (pos == std::string_view::npos)
+ throw std::runtime_error("Problem detected during splitting fuzzer input data");
+ return {data.substr(0, pos), data.substr(pos + delim.size())};
+}
+
+
+std::tuple<std::filesystem::path, std::filesystem::path> create_ir_model_files(const uint8_t* data, size_t size, std::string_view delim) {
+ const auto [xml_sv, bin_sv] = split_data({reinterpret_cast<const char*>(data), size}, delim);
+ const auto stem = std::string("model_") + std::to_string(next_id());
+ const auto xml_path = temp_root() / (stem + ".xml");
+ const auto bin_path = temp_root() / (stem + ".bin");
+ if (!write_file(xml_path, xml_sv))
+ throw std::runtime_error("Write to xml file failed");
+ if (!write_file(bin_path, bin_sv))
+ throw std::runtime_error("Write to bin file failed");
+ return {xml_path, bin_path};
+}
\ No newline at end of file
diff --git a/tests/fuzz/fuzz-testhelper/fuzz-utils.h b/tests/fuzz/fuzz-testhelper/fuzz-utils.h
index 85e34240b73..ee6aa732ab2 100644
--- a/tests/fuzz/fuzz-testhelper/fuzz-utils.h
+++ b/tests/fuzz/fuzz-testhelper/fuzz-utils.h
@@ -1,8 +1,15 @@
// Copyright (C) 2018-2026 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
+#pragma once
#include <stddef.h>
+#include <stdint.h>
+#include <array>
+#include <string_view>
+#include <tuple>
+#include <system_error>
+#include <filesystem>
class MemoryFile {
public:
@@ -17,3 +24,18 @@ class MemoryFile {
private:
char *m_name;
};
+
+
+struct ScopedRemove {
+ std::filesystem::path path;
+ ~ScopedRemove() {
+ std::error_code ec;
+ if (!path.empty())
+ std::filesystem::remove(path, ec);
+ }
+};
+
+
+std::array<std::string_view, 2> split_data(std::string_view data, std::string_view delim);
+std::filesystem::path create_model_file(const uint8_t* data, size_t size, const std::filesystem::path& ext);
+std::tuple<std::filesystem::path, std::filesystem::path> create_ir_model_files(const uint8_t* data, size_t size, std::string_view delim);
diff --git a/tests/fuzz/src/read_model_ir-fuzzer.cc b/tests/fuzz/src/read_model_ir-fuzzer.cc
new file mode 100644
index 00000000000..1fea1e8d9f6
--- /dev/null
+++ b/tests/fuzz/src/read_model_ir-fuzzer.cc
@@ -0,0 +1,25 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+
+#include "openvino/openvino.hpp"
+#include "fuzz-utils.h"
+
+constexpr std::string_view kSplitSequence = "FUZZ_NEXT_FIELD";
+
+extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
+ ov::Core core;
+
+ try {
+ const auto& [xml_path, bin_path] = create_ir_model_files(data, size, kSplitSequence);
+ ScopedRemove cleanup_xml{xml_path};
+ ScopedRemove cleanup_bin{bin_path};
+
+ if (const auto model = core.read_model(xml_path); model) {
+ model->get_name();
+ model->outputs();
+ model->inputs();
+ }
+ }
+ catch (...) {}
+ return 0;
+}
\ No newline at end of file
diff --git a/tests/fuzz/src/read_model_onnx-fuzzer.cc b/tests/fuzz/src/read_model_onnx-fuzzer.cc
new file mode 100644
index 00000000000..a45bd855442
--- /dev/null
+++ b/tests/fuzz/src/read_model_onnx-fuzzer.cc
@@ -0,0 +1,22 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+#include "openvino/openvino.hpp"
+#include "fuzz-utils.h"
+
+extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
+ ov::Core core;
+
+ try {
+ const auto model_file = create_model_file(data, size, ".onnx");
+ ScopedRemove cleanup{model_file};
+
+ if (const auto model = core.read_model(model_file); model) {
+ model->get_name();
+ model->outputs();
+ model->inputs();
+ }
+ } catch (...) {}
+
+ return 0;
+}
diff --git a/tests/fuzz/src/read_model_paddle-fuzzer.cc b/tests/fuzz/src/read_model_paddle-fuzzer.cc
new file mode 100644
index 00000000000..83994a0e180
--- /dev/null
+++ b/tests/fuzz/src/read_model_paddle-fuzzer.cc
@@ -0,0 +1,25 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "openvino/openvino.hpp"
+#include "fuzz-utils.h"
+
+constexpr std::string_view kSplitSequence = "FUZZ_NEXT_FIELD";
+
+extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
+ ov::Core core;
+ try {
+ const auto [net_sv, weights_sv] = split_data({reinterpret_cast<const char*>(data), size}, kSplitSequence);
+
+ std::string net{net_sv};
+ ov::Tensor weights(ov::element::u8, {weights_sv.size()}, const_cast<char*>(weights_sv.data()));
+ if (const auto model = core.read_model(net, weights); model) {
+ model->get_name();
+ model->outputs();
+ model->inputs();
+ }
+ } catch (...) {}
+
+ return 0;
+}
diff --git a/tests/fuzz/src/read_model_tflite-fuzzer.cc b/tests/fuzz/src/read_model_tflite-fuzzer.cc
new file mode 100644
index 00000000000..2002ab281fb
--- /dev/null
+++ b/tests/fuzz/src/read_model_tflite-fuzzer.cc
@@ -0,0 +1,21 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+#include "openvino/openvino.hpp"
+#include "fuzz-utils.h"
+
+extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
+ ov::Core core;
+ try {
+ const auto model_file = create_model_file(data, size, ".tflite");
+ ScopedRemove cleanup{model_file};
+
+ if (const auto model = core.read_model(model_file); model) {
+ model->get_name();
+ model->outputs();
+ model->inputs();
+ }
+ } catch (...) {}
+
+ return 0;
+}