|
| 1 | +// Copyright 2022 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 "gtest/gtest.h" |
| 16 | + |
| 17 | +#include <memory> |
| 18 | +#include <string> |
| 19 | + |
| 20 | +#include "include/proxy-wasm/context.h" |
| 21 | +#include "include/proxy-wasm/wasm.h" |
| 22 | + |
| 23 | +#include "test/utility.h" |
| 24 | + |
| 25 | +namespace proxy_wasm { |
| 26 | + |
| 27 | +INSTANTIATE_TEST_SUITE_P(WasmEngines, TestVm, testing::ValuesIn(getWasmEngines()), |
| 28 | + [](const testing::TestParamInfo<std::string> &info) { |
| 29 | + return info.param; |
| 30 | + }); |
| 31 | + |
| 32 | +TEST_P(TestVm, MallocNoHostcalls) { |
| 33 | + if (engine_ != "v8") { |
| 34 | + return; |
| 35 | + } |
| 36 | + auto source = readTestWasmFile("bad_malloc.wasm"); |
| 37 | + ASSERT_FALSE(source.empty()); |
| 38 | + auto wasm = TestWasm(std::move(vm_)); |
| 39 | + ASSERT_TRUE(wasm.load(source, false)); |
| 40 | + ASSERT_TRUE(wasm.initialize()); |
| 41 | + |
| 42 | + uint64_t ptr = 0; |
| 43 | + void *result = wasm.allocMemory(0x1000, &ptr); |
| 44 | + EXPECT_NE(result, nullptr); |
| 45 | + EXPECT_FALSE(wasm.isFailed()); |
| 46 | + |
| 47 | + // Check application logs. |
| 48 | + auto *context = dynamic_cast<TestContext *>(wasm.vm_context()); |
| 49 | + EXPECT_TRUE(context->isLogEmpty()); |
| 50 | + // Check integration logs. |
| 51 | + auto *integration = dynamic_cast<TestIntegration *>(wasm.wasm_vm()->integration().get()); |
| 52 | + EXPECT_FALSE(integration->isErrorLogged("Function: proxy_on_memory_allocate failed")); |
| 53 | + EXPECT_FALSE(integration->isErrorLogged("restricted_callback")); |
| 54 | +} |
| 55 | + |
| 56 | +TEST_P(TestVm, MallocWithLog) { |
| 57 | + if (engine_ != "v8") { |
| 58 | + return; |
| 59 | + } |
| 60 | + auto source = readTestWasmFile("bad_malloc.wasm"); |
| 61 | + ASSERT_FALSE(source.empty()); |
| 62 | + auto wasm = TestWasm(std::move(vm_)); |
| 63 | + ASSERT_TRUE(wasm.load(source, false)); |
| 64 | + ASSERT_TRUE(wasm.initialize()); |
| 65 | + |
| 66 | + uint64_t ptr = 0; |
| 67 | + // 0xAAAA => hostcall to proxy_log (allowed). |
| 68 | + void *result = wasm.allocMemory(0xAAAA, &ptr); |
| 69 | + EXPECT_NE(result, nullptr); |
| 70 | + EXPECT_FALSE(wasm.isFailed()); |
| 71 | + |
| 72 | + // Check application logs. |
| 73 | + auto *context = dynamic_cast<TestContext *>(wasm.vm_context()); |
| 74 | + EXPECT_TRUE(context->isLogged("this is fine")); |
| 75 | + // Check integration logs. |
| 76 | + auto *integration = dynamic_cast<TestIntegration *>(wasm.wasm_vm()->integration().get()); |
| 77 | + EXPECT_FALSE(integration->isErrorLogged("Function: proxy_on_memory_allocate failed")); |
| 78 | + EXPECT_FALSE(integration->isErrorLogged("restricted_callback")); |
| 79 | +} |
| 80 | + |
| 81 | +TEST_P(TestVm, MallocWithHostcall) { |
| 82 | + if (engine_ != "v8") { |
| 83 | + return; |
| 84 | + } |
| 85 | + auto source = readTestWasmFile("bad_malloc.wasm"); |
| 86 | + ASSERT_FALSE(source.empty()); |
| 87 | + auto wasm = TestWasm(std::move(vm_)); |
| 88 | + ASSERT_TRUE(wasm.load(source, false)); |
| 89 | + ASSERT_TRUE(wasm.initialize()); |
| 90 | + |
| 91 | + uint64_t ptr = 0; |
| 92 | + // 0xBBBB => hostcall to proxy_done (not allowed). |
| 93 | + void *result = wasm.allocMemory(0xBBBB, &ptr); |
| 94 | + EXPECT_EQ(result, nullptr); |
| 95 | + EXPECT_TRUE(wasm.isFailed()); |
| 96 | + |
| 97 | + // Check application logs. |
| 98 | + auto *context = dynamic_cast<TestContext *>(wasm.vm_context()); |
| 99 | + EXPECT_TRUE(context->isLogEmpty()); |
| 100 | + // Check integration logs. |
| 101 | + auto *integration = dynamic_cast<TestIntegration *>(wasm.wasm_vm()->integration().get()); |
| 102 | + EXPECT_TRUE(integration->isErrorLogged("Function: proxy_on_memory_allocate failed")); |
| 103 | + EXPECT_TRUE(integration->isErrorLogged("restricted_callback")); |
| 104 | +} |
| 105 | + |
| 106 | +} // namespace proxy_wasm |
0 commit comments