-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathwasm_vm_test.cc
180 lines (153 loc) · 5.29 KB
/
wasm_vm_test.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// Copyright 2020 Google LLC
//
// 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 "gtest/gtest.h"
#include <memory>
#include <string>
#include <vector>
#include "include/proxy-wasm/wasm_vm.h"
#include "test/utility.h"
namespace proxy_wasm {
namespace {
INSTANTIATE_TEST_SUITE_P(WasmEngines, TestVm, testing::ValuesIn(getWasmEngines()),
[](const testing::TestParamInfo<std::string> &info) {
return info.param;
});
TEST_P(TestVm, Init) {
std::chrono::time_point<std::chrono::steady_clock> time2;
auto time1 = std::chrono::steady_clock::now();
if (engine_ == "v8") {
#if defined(PROXY_WASM_HOST_ENGINE_V8)
EXPECT_TRUE(proxy_wasm::initV8Engine());
time2 = std::chrono::steady_clock::now();
EXPECT_TRUE(proxy_wasm::initV8Engine());
#endif
} else if (engine_ == "wamr") {
#if defined(PROXY_WASM_HOST_ENGINE_WAMR)
EXPECT_TRUE(proxy_wasm::initWamrEngine());
time2 = std::chrono::steady_clock::now();
EXPECT_TRUE(proxy_wasm::initWamrEngine());
#endif
} else if (engine_ == "wasmtime") {
#if defined(PROXY_WASM_HOST_ENGINE_WASMTIME)
EXPECT_TRUE(proxy_wasm::initWasmtimeEngine());
time2 = std::chrono::steady_clock::now();
EXPECT_TRUE(proxy_wasm::initWasmtimeEngine());
#endif
} else {
return;
}
auto time3 = std::chrono::steady_clock::now();
auto cold = std::chrono::duration_cast<std::chrono::nanoseconds>(time2 - time1).count();
auto warm = std::chrono::duration_cast<std::chrono::nanoseconds>(time3 - time2).count();
std::cout << "\"cold\" engine time: " << cold << "ns" << std::endl;
std::cout << "\"warm\" engine time: " << warm << "ns" << std::endl;
// Verify that getting a "warm" engine takes less than 10us.
EXPECT_LE(warm, 10000);
// Verify that getting a "warm" engine takes at least 50x less time than getting a "cold" one.
EXPECT_LE(warm * 50, cold);
}
TEST_P(TestVm, Basic) {
if (engine_ == "wamr") {
EXPECT_EQ(vm_->cloneable(), proxy_wasm::Cloneable::NotCloneable);
} else if (engine_ == "wasmtime" || engine_ == "v8") {
EXPECT_EQ(vm_->cloneable(), proxy_wasm::Cloneable::CompiledBytecode);
} else if (engine_ == "wavm") {
EXPECT_EQ(vm_->cloneable(), proxy_wasm::Cloneable::InstantiatedModule);
} else {
FAIL();
}
EXPECT_EQ(vm_->getEngineName(), engine_);
}
TEST_P(TestVm, Memory) {
auto source = readTestWasmFile("abi_export.wasm");
ASSERT_TRUE(vm_->load(source, {}, {}));
ASSERT_TRUE(vm_->link(""));
Word word;
ASSERT_TRUE(vm_->setWord(0x2000, Word(100)));
ASSERT_TRUE(vm_->getWord(0x2000, &word));
ASSERT_EQ(100, word.u64_);
uint32_t data[2] = {htowasm(static_cast<uint32_t>(-1)), htowasm(200)};
ASSERT_TRUE(vm_->setMemory(0x200, sizeof(int32_t) * 2, static_cast<void *>(data)));
ASSERT_TRUE(vm_->getWord(0x200, &word));
ASSERT_EQ(-1, static_cast<int32_t>(word.u64_));
ASSERT_TRUE(vm_->getWord(0x204, &word));
ASSERT_EQ(200, static_cast<int32_t>(word.u64_));
}
TEST_P(TestVm, Clone) {
if (vm_->cloneable() == proxy_wasm::Cloneable::NotCloneable) {
return;
}
auto source = readTestWasmFile("abi_export.wasm");
ASSERT_TRUE(vm_->load(source, {}, {}));
ASSERT_TRUE(vm_->link(""));
const auto address = 0x2000;
Word word;
{
auto clone = vm_->clone();
ASSERT_TRUE(clone != nullptr);
ASSERT_NE(vm_, clone);
if (clone->cloneable() != proxy_wasm::Cloneable::InstantiatedModule) {
ASSERT_TRUE(clone->link(""));
}
ASSERT_TRUE(clone->setWord(address, Word(100)));
ASSERT_TRUE(clone->getWord(address, &word));
ASSERT_EQ(100, word.u64_);
}
// check memory arrays are not overrapped
ASSERT_TRUE(vm_->getWord(address, &word));
ASSERT_NE(100, word.u64_);
}
#if defined(__linux__) && defined(__x86_64__)
TEST_P(TestVm, CloneUntilOutOfMemory) {
if (vm_->cloneable() == proxy_wasm::Cloneable::NotCloneable) {
return;
}
if (engine_ == "wavm") {
// TODO(PiotrSikora): Figure out why this fails on the CI.
return;
}
auto source = readTestWasmFile("abi_export.wasm");
ASSERT_TRUE(vm_->load(source, {}, {}));
ASSERT_TRUE(vm_->link(""));
size_t max_clones = 100000;
#if defined(__has_feature)
#if __has_feature(address_sanitizer)
max_clones = 1000;
#endif
#endif
std::vector<std::unique_ptr<WasmVm>> clones;
for (size_t i = 0; i < max_clones; i++) {
auto clone = vm_->clone();
if (clone == nullptr) {
break;
}
if (clone->cloneable() != proxy_wasm::Cloneable::InstantiatedModule) {
if (!clone->link("")) {
break;
}
}
// Prevent clone from droping out of scope and freeing memory.
clones.push_back(std::move(clone));
}
size_t min_clones = 1000;
#if defined(__has_feature)
#if __has_feature(thread_sanitizer)
min_clones = 100;
#endif
#endif
EXPECT_GE(clones.size(), min_clones);
}
#endif
} // namespace
} // namespace proxy_wasm