1 /**
2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "verification/cache/results_cache.h"
17 #include "verification/util/synchronized.h"
18
19 #include "runtime/include/runtime.h"
20 #include "runtime/include/mem/allocator.h"
21 #include "runtime/include/mem/panda_containers.h"
22
23 #include "libpandabase/os/file.h"
24 #include "utils/logger.h"
25
26 namespace panda::verifier {
27
28 struct VerificationResultCache::Impl {
29 std::string filename;
30 Synchronized<PandaUnorderedSet<uint64_t>> verified_ok;
31 Synchronized<PandaUnorderedSet<uint64_t>> verified_fail;
32
33 template <typename It>
Implpanda::verifier::VerificationResultCache::Impl34 Impl(std::string file_name, It data_start, It data_end)
35 : filename {std::move(file_name)}, verified_ok {data_start, data_end}
36 {
37 }
38 };
39
40 VerificationResultCache::Impl *VerificationResultCache::impl {nullptr};
41
Enabled()42 bool VerificationResultCache::Enabled()
43 {
44 return impl != nullptr;
45 }
46
Initialize(const std::string & filename)47 void VerificationResultCache::Initialize(const std::string &filename)
48 {
49 if (Enabled()) {
50 return;
51 }
52 using panda::os::file::Mode;
53 using panda::os::file::Open;
54 using Data = PandaVector<uint64_t>;
55
56 auto file = Open(filename, Mode::READONLY);
57 if (!file.IsValid()) {
58 file = Open(filename, Mode::READWRITECREATE);
59 }
60 if (!file.IsValid()) {
61 LOG(INFO, VERIFIER) << "Cannot open verification cache file '" << filename << "'";
62 return;
63 }
64
65 auto size = file.GetFileSize();
66 if (!size.HasValue()) {
67 LOG(INFO, VERIFIER) << "Cannot get verification cache file size";
68 file.Close();
69 return;
70 }
71
72 Data data;
73
74 auto elements = *size / sizeof(Data::value_type);
75
76 if (elements > 0) {
77 data.resize(elements, 0);
78 if (!file.ReadAll(data.data(), *size)) {
79 LOG(INFO, VERIFIER) << "Cannot read verification cache data";
80 file.Close();
81 return;
82 }
83 }
84
85 file.Close();
86
87 impl = new (mem::AllocatorAdapter<Impl>().allocate(1)) Impl {filename, data.cbegin(), data.cend()};
88 ASSERT(Enabled());
89 }
90
Destroy(bool update_file)91 void VerificationResultCache::Destroy(bool update_file)
92 {
93 if (!Enabled()) {
94 return;
95 }
96 if (update_file) {
97 PandaVector<uint64_t> data;
98 impl->verified_ok.Apply([&data](const auto &set) {
99 data.reserve(set.size());
100 data.insert(data.begin(), set.cbegin(), set.cend());
101 });
102 using panda::os::file::Mode;
103 using panda::os::file::Open;
104 do {
105 auto file = Open(impl->filename, Mode::READWRITECREATE);
106 if (!file.IsValid()) {
107 LOG(INFO, VERIFIER) << "Cannot open verification cache file '" << impl->filename << "'";
108 break;
109 }
110 if (!file.ClearData()) {
111 LOG(INFO, VERIFIER) << "Cannot clear verification cache file '" << impl->filename << "'";
112 file.Close();
113 break;
114 }
115 if (!file.WriteAll(data.data(), data.size() * sizeof(uint64_t))) {
116 LOG(INFO, VERIFIER) << "Cannot write to verification cache file '" << impl->filename << "'";
117 file.ClearData();
118 file.Close();
119 break;
120 }
121 file.Close();
122 } while (false);
123 }
124 impl->~Impl();
125 mem::AllocatorAdapter<Impl>().deallocate(impl, 1);
126 impl = nullptr;
127 }
128
CacheResult(uint64_t method_id,bool result)129 void VerificationResultCache::CacheResult(uint64_t method_id, bool result)
130 {
131 if (Enabled()) {
132 if (result) {
133 impl->verified_ok->insert(method_id);
134 } else {
135 impl->verified_fail->insert(method_id);
136 }
137 }
138 }
139
Check(uint64_t method_id)140 VerificationResultCache::Status VerificationResultCache::Check(uint64_t method_id)
141 {
142 if (Enabled()) {
143 if (impl->verified_ok->count(method_id) > 0) {
144 return Status::OK;
145 }
146 if (impl->verified_fail->count(method_id) > 0) {
147 return Status::FAILED;
148 }
149 }
150 return Status::UNKNOWN;
151 }
152
153 } // namespace panda::verifier
154