From 6f3aa9ef5680d334fa51a5ee5f64895c6d5a40db Mon Sep 17 00:00:00 2001 From: =?utf8?q?=EB=B0=95=EC=84=B8=ED=9D=AC/On-Device=20Lab=28SR=29/Princip?= =?utf8?q?al=20Engineer/=EC=82=BC=EC=84=B1=EC=A0=84=EC=9E=90?= Date: Thu, 31 Oct 2019 15:40:49 +0900 Subject: [PATCH] [circle-verify] Add VerifyFlatBuffers (#8648) This will add and enable VerifyFlatBuffers verify Circle file in flatbuffer file format level Signed-off-by: SaeHie Park --- compiler/circle-verify/CMakeLists.txt | 6 ++++ compiler/circle-verify/requires.cmake | 2 ++ compiler/circle-verify/src/Driver.cpp | 33 ++++++++++++++++++++-- compiler/circle-verify/src/VerifyFlatBuffers.cpp | 36 ++++++++++++++++++++++++ compiler/circle-verify/src/VerifyFlatBuffers.h | 32 +++++++++++++++++++++ 5 files changed, 106 insertions(+), 3 deletions(-) create mode 100644 compiler/circle-verify/src/VerifyFlatBuffers.cpp create mode 100644 compiler/circle-verify/src/VerifyFlatBuffers.h diff --git a/compiler/circle-verify/CMakeLists.txt b/compiler/circle-verify/CMakeLists.txt index cb145a2..2e19951 100644 --- a/compiler/circle-verify/CMakeLists.txt +++ b/compiler/circle-verify/CMakeLists.txt @@ -1,6 +1,12 @@ +if(NOT TARGET mio_circle) + return() +endif(NOT TARGET mio_circle) + file(GLOB_RECURSE SOURCES "src/*.cpp") add_executable(circle-verify ${SOURCES}) target_include_directories(circle-verify PRIVATE src) +target_link_libraries(circle-verify mio_circle) target_link_libraries(circle-verify safemain) target_link_libraries(circle-verify cwrap) +target_link_libraries(circle-verify stdex) diff --git a/compiler/circle-verify/requires.cmake b/compiler/circle-verify/requires.cmake index 283c0a2..2509b69 100644 --- a/compiler/circle-verify/requires.cmake +++ b/compiler/circle-verify/requires.cmake @@ -1,2 +1,4 @@ +require("mio-circle") require("safemain") require("cwrap") +require("stdex") diff --git a/compiler/circle-verify/src/Driver.cpp b/compiler/circle-verify/src/Driver.cpp index d6cab32..ad13e50 100644 --- a/compiler/circle-verify/src/Driver.cpp +++ b/compiler/circle-verify/src/Driver.cpp @@ -14,11 +14,38 @@ * limitations under the License. */ -#include +#include "VerifyFlatBuffers.h" + +#include + +#include +#include int entry(int argc, char **argv) { - throw std::runtime_error("NYI"); + if (argc != 2) + { + std::cerr << "ERROR: Failed to parse arguments" << std::endl; + std::cerr << std::endl; + std::cerr << "USAGE: " << argv[0] << " [circle]" << std::endl; + return 255; + } + auto verifier = stdex::make_unique(); + + std::string model_file = argv[argc - 1]; + + std::cout << "[ RUN ] Check " << model_file << std::endl; + + auto result = verifier->run(model_file); + + if (result == 0) + { + std::cout << "[ PASS ] Check " << model_file << std::endl; + } + else + { + std::cout << "[ FAIL ] Check " << model_file << std::endl; + } - return 0; + return result; } diff --git a/compiler/circle-verify/src/VerifyFlatBuffers.cpp b/compiler/circle-verify/src/VerifyFlatBuffers.cpp new file mode 100644 index 0000000..36b1668 --- /dev/null +++ b/compiler/circle-verify/src/VerifyFlatBuffers.cpp @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved + * + * 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 "VerifyFlatBuffers.h" + +#include "Model.h" + +#include + +int VerifyFlatbuffers::run(const std::string &model_file) +{ + auto modeldata = load_modeldata(model_file); + + const uint8_t *data = reinterpret_cast(modeldata->data()); + flatbuffers::Verifier verifier{data, modeldata->size()}; + + if (!circle::VerifyModelBuffer(verifier)) + { + return -1; + } + + return 0; +} diff --git a/compiler/circle-verify/src/VerifyFlatBuffers.h b/compiler/circle-verify/src/VerifyFlatBuffers.h new file mode 100644 index 0000000..c301b5b --- /dev/null +++ b/compiler/circle-verify/src/VerifyFlatBuffers.h @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved + * + * 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. + */ + +#ifndef __VERIFY_FLATBUFFERS_H__ +#define __VERIFY_FLATBUFFERS_H__ + +#include +#include + +class VerifyFlatbuffers +{ +public: + VerifyFlatbuffers() = default; + +public: + int run(const std::string &model_file); +}; + +#endif // __VERIFY_FLATBUFFERS_H__ -- 2.7.4