-
Notifications
You must be signed in to change notification settings - Fork 85
Add test for native to wasm value conversions #525
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
864c272
Add test for native to wasm value conversions
leonm1 363a6fc
Add remaining runtime support for float type
leonm1 75471c0
Rename bigendian test to more generic "arg passing"
leonm1 76bb5a7
Run buildifier
leonm1 2294cf7
std::strcpy -> strcpy
leonm1 5dde5c1
Expand arg passing test with host and wasm return values
leonm1 2ff0683
Fix endianness conversions for u32, f32 and f64 types
leonm1 f5b7828
Fix high bits stored in uint64 representation of Word
leonm1 ad89fc4
Add float converter for wasmtime with c-api
leonm1 15c5723
Add test for returning negative ints from wasm
leonm1 cc48307
Suppress gtest warning for arg_passing_test in nullvm
leonm1 04b391b
Remove unused import from arg_passing.rs
leonm1 4a3a92d
Fix type narrowing compile error in wasm_vm_test
leonm1 c3448aa
Address PR comments
leonm1 6352839
bit_cast -> static_cast for Word types
leonm1 4fbfdef
Address final nits
leonm1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,155 @@ | ||
| // Copyright 2026 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 "gmock/gmock.h" | ||
|
|
||
| #include <optional> | ||
|
|
||
| #include "include/proxy-wasm/context.h" | ||
| #include "include/proxy-wasm/wasm.h" | ||
|
|
||
| #include "test/utility.h" | ||
|
|
||
| namespace proxy_wasm { | ||
| namespace { | ||
|
|
||
| class EndiannessContext : public TestContext { | ||
| public: | ||
| using TestContext::TestContext; | ||
| WasmResult getHeaderMapPairs(WasmHeaderMapType /* type */, Pairs * /* result */) override { | ||
PiotrSikora marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // GetHeaderMapPairs passes this value as the hostcall return value as | ||
| // opposed to output parameter. | ||
| return static_cast<WasmResult>(3333333333U); | ||
| } | ||
| }; | ||
|
|
||
| class EndiannessWasm : public TestWasm { | ||
| public: | ||
| using TestWasm::TestWasm; | ||
| ContextBase *createVmContext() override { return new EndiannessContext(this); }; | ||
| }; | ||
|
|
||
| class EndiannessTest : public TestVm { | ||
| public: | ||
| void SetUp() { | ||
| auto source = readTestWasmFile("endianness.wasm"); | ||
| ASSERT_FALSE(source.empty()); | ||
| wasm_.emplace(std::move(vm_)); | ||
| ASSERT_TRUE(wasm_->load(source, false)); | ||
| ASSERT_TRUE(wasm_->initialize()); | ||
| context_ = dynamic_cast<EndiannessContext *>(wasm_->vm_context()); | ||
| ASSERT_NE(context_, nullptr); | ||
| } | ||
|
|
||
| std::optional<EndiannessWasm> wasm_; | ||
| EndiannessContext *context_; | ||
| }; | ||
|
|
||
| INSTANTIATE_TEST_SUITE_P(WasmEngines, EndiannessTest, testing::ValuesIn(getWasmEngines()), | ||
| [](const testing::TestParamInfo<std::string> &info) { | ||
| return info.param; | ||
| }); | ||
|
|
||
| TEST_P(EndiannessTest, WasmCallReturnsWordValue) { | ||
| WasmCallWord<0> test_return_u32; | ||
| wasm_->wasm_vm()->getFunction("test_return_u32", &test_return_u32); | ||
|
|
||
| EXPECT_EQ(test_return_u32(context_).u32(), 3333333333U) << context_->getLog(); | ||
| } | ||
|
|
||
| TEST_P(EndiannessTest, WasmCallReturnsNegativeWordValue) { | ||
| WasmCallWord<0> test_return_i32; | ||
| wasm_->wasm_vm()->getFunction("test_return_i32", &test_return_i32); | ||
|
|
||
| EXPECT_EQ(test_return_i32(context_).u32(), -1111111111) << context_->getLog(); | ||
| } | ||
|
|
||
| TEST_P(EndiannessTest, WasmCallReturnsUnsignedLongValue) { | ||
| WasmCall_lf test_return_u64; | ||
| wasm_->wasm_vm()->getFunction("test_return_u64", &test_return_u64); | ||
|
|
||
| EXPECT_EQ(test_return_u64(context_, 1.0), 11111111111111111111UL) << context_->getLog(); | ||
| } | ||
|
|
||
| TEST_P(EndiannessTest, WasmCallReturnsNegativeLongValue) { | ||
| WasmCall_lf test_return_i64; | ||
| wasm_->wasm_vm()->getFunction("test_return_i64", &test_return_i64); | ||
|
|
||
| EXPECT_EQ(test_return_i64(context_, 1.0), -111111111111111111L) << context_->getLog(); | ||
| } | ||
|
|
||
| TEST_P(EndiannessTest, WasmCallReturnsFloatValue) { | ||
| WasmCall_fff test_return_f32; | ||
| wasm_->wasm_vm()->getFunction("test_return_f32", &test_return_f32); | ||
|
|
||
| EXPECT_THAT(test_return_f32(context_, 1.0, 1.0), | ||
| testing::AllOf(testing::Lt(1112.0), testing::Gt(1110.0))) | ||
| << context_->getLog(); | ||
| } | ||
|
|
||
| TEST_P(EndiannessTest, WasmCallReturnsDoubleValue) { | ||
| WasmCall_dfff test_return_f64; | ||
| wasm_->wasm_vm()->getFunction("test_return_f64", &test_return_f64); | ||
|
|
||
| EXPECT_THAT(test_return_f64(context_, 1.0, 1.0, 1.0), | ||
| testing::AllOf(testing::Lt(1111111112.0), testing::Gt(1111111110.0))) | ||
| << context_->getLog(); | ||
| } | ||
|
|
||
| TEST_P(EndiannessTest, HostCallReturnsWordValue) { | ||
| WasmCallWord<0> test_host_return; | ||
| wasm_->wasm_vm()->getFunction("test_host_return", &test_host_return); | ||
|
|
||
| EXPECT_TRUE(test_host_return(context_)) << context_->getLog(); | ||
| } | ||
|
|
||
| TEST_P(EndiannessTest, HostPassesPrimitiveValues) { | ||
| WasmCall_WWlfd test_primitives; | ||
| wasm_->wasm_vm()->getFunction("test_primitives", &test_primitives); | ||
|
|
||
| ASSERT_TRUE(test_primitives(context_, 3333333333U, 11111111111111111111UL, 1111, 1111111111)) | ||
| << context_->getLog(); | ||
| } | ||
|
|
||
| TEST_P(EndiannessTest, HostPassesNegativePrimitiveValues) { | ||
| WasmCall_WWlfd test_negative_primitives; | ||
| wasm_->wasm_vm()->getFunction("test_negative_primitives", &test_negative_primitives); | ||
|
|
||
| ASSERT_TRUE( | ||
| test_negative_primitives(context_, -1111111111, -1111111111111111111, -1111, -1111111111)) | ||
| << context_->getLog(); | ||
| } | ||
|
|
||
| TEST_P(EndiannessTest, HostReadsPointersToWasmMemory) { | ||
| WasmCallWord<0> test_buffer_from_wasm; | ||
| wasm_->wasm_vm()->getFunction("test_buffer_from_wasm", &test_buffer_from_wasm); | ||
|
|
||
| ASSERT_TRUE(test_buffer_from_wasm(context_)) << context_->getLog(); | ||
|
|
||
| context_->isLogged("hello from wasm land!"); | ||
| } | ||
|
|
||
| TEST_P(EndiannessTest, WasmCallReadsBufferPassedByHost) { | ||
| context_->setBuffer(0, "hello from host land!"); | ||
| WasmCallWord<0> test_buffer_from_host; | ||
| wasm_->wasm_vm()->getFunction("test_buffer_from_host", &test_buffer_from_host); | ||
|
|
||
| ASSERT_TRUE(test_buffer_from_host(context_)) << context_->getLog(); | ||
| } | ||
|
|
||
| GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(EndiannessTest); | ||
|
|
||
| } // namespace | ||
| } // namespace proxy_wasm | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.