Skip to content

Commit ad0aaa8

Browse files
committed
Use SHA256 hash for plugin cache
Use SHA256 hash for plugin cache key. Otherwise, a raw plugin config (concatenated with root ID and key) is stored in the key which may cause unnecessary memory consumption.
1 parent 5d76116 commit ad0aaa8

File tree

6 files changed

+95
-27
lines changed

6 files changed

+95
-27
lines changed

BUILD

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ cc_library(
6666
"src/bytecode_util.cc",
6767
"src/context.cc",
6868
"src/exports.cc",
69+
"src/hash.cc",
70+
"src/hash.h",
6971
"src/pairs_util.cc",
7072
"src/shared_data.cc",
7173
"src/shared_data.h",

include/proxy-wasm/context.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <map>
2424
#include <memory>
2525
#include <string>
26+
#include <string_view>
2627
#include <vector>
2728

2829
#include "include/proxy-wasm/context_interface.h"
@@ -53,8 +54,7 @@ struct PluginBase {
5354
std::string_view key)
5455
: name_(std::string(name)), root_id_(std::string(root_id)), vm_id_(std::string(vm_id)),
5556
engine_(std::string(engine)), plugin_configuration_(plugin_configuration),
56-
fail_open_(fail_open),
57-
key_(root_id_ + "||" + plugin_configuration_ + "||" + std::string(key)),
57+
fail_open_(fail_open), key_(makePluginKey(root_id, plugin_configuration, key)),
5858
log_prefix_(makeLogPrefix()) {}
5959

6060
const std::string name_;
@@ -69,6 +69,8 @@ struct PluginBase {
6969

7070
private:
7171
std::string makeLogPrefix() const;
72+
static std::string makePluginKey(std::string_view root_id, std::string_view plugin_configuration,
73+
std::string_view key);
7274

7375
const std::string key_;
7476
const std::string log_prefix_;

src/context.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
#include "include/proxy-wasm/context.h"
2424
#include "include/proxy-wasm/wasm.h"
25+
#include "src/hash.h"
2526
#include "src/shared_data.h"
2627
#include "src/shared_queue.h"
2728

@@ -85,6 +86,11 @@ std::string PluginBase::makeLogPrefix() const {
8586
return prefix;
8687
}
8788

89+
std::string PluginBase::makePluginKey(std::string_view root_id,
90+
std::string_view plugin_configuration, std::string_view key) {
91+
return Sha256String({root_id, "||", plugin_configuration, "||", key});
92+
}
93+
8894
ContextBase::ContextBase() : parent_context_(this) {}
8995

9096
ContextBase::ContextBase(WasmBase *wasm) : wasm_(wasm), parent_context_(this) {

src/hash.cc

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright 2023 Google LLC
2+
//
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+
#include "src/hash.h"
16+
17+
#include <string>
18+
#include <vector>
19+
20+
#include <openssl/sha.h>
21+
22+
namespace proxy_wasm {
23+
24+
namespace {
25+
26+
std::string BytesToHex(const std::vector<uint8_t> &bytes) {
27+
static const char *const hex = "0123456789ABCDEF";
28+
std::string result;
29+
result.reserve(bytes.size() * 2);
30+
for (auto byte : bytes) {
31+
result.push_back(hex[byte >> 4]);
32+
result.push_back(hex[byte & 0xf]);
33+
}
34+
return result;
35+
}
36+
37+
} // namespace
38+
39+
std::vector<uint8_t> Sha256(const std::vector<std::string_view> &parts) {
40+
uint8_t sha256[SHA256_DIGEST_LENGTH];
41+
SHA256_CTX sha_ctx;
42+
SHA256_Init(&sha_ctx);
43+
for (auto part : parts) {
44+
SHA256_Update(&sha_ctx, part.data(), part.size());
45+
}
46+
SHA256_Final(sha256, &sha_ctx);
47+
return std::vector<uint8_t>(std::begin(sha256), std::end(sha256));
48+
}
49+
50+
std::string Sha256String(const std::vector<std::string_view> &parts) {
51+
return BytesToHex(Sha256(parts));
52+
}
53+
54+
} // namespace proxy_wasm

src/hash.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2023 Google LLC
2+
//
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+
#pragma once
16+
17+
#include <string>
18+
#include <vector>
19+
20+
#include <openssl/sha.h>
21+
22+
namespace proxy_wasm {
23+
24+
std::vector<uint8_t> Sha256(const std::vector<std::string_view> &parts);
25+
std::string Sha256String(const std::vector<std::string_view> &parts);
26+
27+
} // namespace proxy_wasm

src/wasm.cc

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,10 @@
2727
#include <unordered_map>
2828
#include <utility>
2929

30-
#include <openssl/sha.h>
31-
3230
#include "include/proxy-wasm/bytecode_util.h"
3331
#include "include/proxy-wasm/signature_util.h"
3432
#include "include/proxy-wasm/vm_id_handle.h"
33+
#include "src/hash.h"
3534

3635
namespace proxy_wasm {
3736

@@ -44,33 +43,11 @@ thread_local std::unordered_map<std::string, std::weak_ptr<PluginHandleBase>> lo
4443
std::mutex base_wasms_mutex;
4544
std::unordered_map<std::string, std::weak_ptr<WasmHandleBase>> *base_wasms = nullptr;
4645

47-
std::vector<uint8_t> Sha256(const std::vector<std::string_view> &parts) {
48-
uint8_t sha256[SHA256_DIGEST_LENGTH];
49-
SHA256_CTX sha_ctx;
50-
SHA256_Init(&sha_ctx);
51-
for (auto part : parts) {
52-
SHA256_Update(&sha_ctx, part.data(), part.size());
53-
}
54-
SHA256_Final(sha256, &sha_ctx);
55-
return std::vector<uint8_t>(std::begin(sha256), std::end(sha256));
56-
}
57-
58-
std::string BytesToHex(const std::vector<uint8_t> &bytes) {
59-
static const char *const hex = "0123456789ABCDEF";
60-
std::string result;
61-
result.reserve(bytes.size() * 2);
62-
for (auto byte : bytes) {
63-
result.push_back(hex[byte >> 4]);
64-
result.push_back(hex[byte & 0xf]);
65-
}
66-
return result;
67-
}
68-
6946
} // namespace
7047

7148
std::string makeVmKey(std::string_view vm_id, std::string_view vm_configuration,
7249
std::string_view code) {
73-
return BytesToHex(Sha256({vm_id, vm_configuration, code}));
50+
return Sha256String({vm_id, "||", vm_configuration, "||", code});
7451
}
7552

7653
class WasmBase::ShutdownHandle {

0 commit comments

Comments
 (0)