• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2021 Huawei Technologies Co., Ltd
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 
17 #ifndef MINDSPORE_LITE_INCLUDE_REGISTRY_DEOBFPROCESSOR_H_
18 #define MINDSPORE_LITE_INCLUDE_REGISTRY_DEOBFPROCESSOR_H_
19 
20 #include <vector>
21 #include <string>
22 #include <numeric>
23 #include "src/common/prim_util.h"
24 #include "src/common/log_adapter.h"
25 #include "include/model.h"
26 #include "schema/inner/model_generated.h"
27 
28 namespace mindspore::lite {
29 
30   enum DeObfRet : uint32_t {
31     kDeObfFailed = 0,        ///<Deobfuscator failed
32     kNoObf = 1,               ///<The node has not been obfuscated
33     kDeObfSuccess = 2,        ///<Deobfuscate success
34   };
35 
36   class DeObfProcessor {
37     public:
38       DeObfProcessor() = default;
39 
40       bool GetModelDeObf(const void *meta_graph, Model *model);
41 
42       void DeObfuscate(Model *model);
43 
44       DeObfRet CreateDeObfNode(const schema::Primitive *&src_prim, int i, int schema__version);
45 
46       std::vector<uint32_t> all_prims_type_;
47       std::vector<uint32_t> all_nodes_stat_;
48       bool model_obfuscated_ = false;
49       void *model_deobf = nullptr;
50   };
51 
52   typedef void (*ObfCreateFunc)(Model &model);
53 
54   class MS_API DeObfRegister {
55     public:
56       static bool (DeObfProcessor::*GetModelDeObfReg)(const void *meta_graph, Model *model);
57       static void (DeObfProcessor::*DeObfuscateReg)(Model *model);
58       static DeObfRet (DeObfProcessor::*CreateDeObfNodeReg)(const schema::Primitive *&src_prim, int i, int schema__version);
59       static void *deobf_handle;
60 
61       DeObfRegister() = default;
62       ~DeObfRegister() = default;
63 
64       static ObfCreateFunc NewDeObfProcessor;
65 
Fail(Model & model)66       static void Fail(Model &model){MS_LOG(INFO) << "DeObfuscator not registered!";}
67 
RegisterDeObfuscator(ObfCreateFunc func)68       MS_API static void RegisterDeObfuscator(ObfCreateFunc func){
69         if(func == nullptr){
70           MS_LOG(WARNING) << "Register invalid deobfuscator";
71           return;
72         }
73         NewDeObfProcessor = func;
74       }
75   };
76 }
77 #endif
78