-
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
Open
leonm1
wants to merge
15
commits into
proxy-wasm:main
Choose a base branch
from
leonm1:test/bigendian-values
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
c1fa381
Add test for native to wasm value conversions
leonm1 66059ad
Add remaining runtime support for float type
leonm1 6d85d34
Rename bigendian test to more generic "arg passing"
leonm1 8d53459
Run buildifier
leonm1 e703840
std::strcpy -> strcpy
leonm1 66ea330
Expand arg passing test with host and wasm return values
leonm1 23f7c36
Fix endianness conversions for u32, f32 and f64 types
leonm1 bf28c18
Fix high bits stored in uint64 representation of Word
leonm1 87e4f14
Add float converter for wasmtime with c-api
leonm1 1ef54cc
Add test for returning negative ints from wasm
leonm1 8dc77e7
Suppress gtest warning for arg_passing_test in nullvm
leonm1 0cdd8b3
Remove unused import from arg_passing.rs
leonm1 37ec622
Fix type narrowing compile error in wasm_vm_test
leonm1 ef77f96
Address PR comments
leonm1 4cc0ca7
bit_cast -> static_cast for Word types
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -49,6 +49,7 @@ template <> WasmEdge_Value makeVal(uint64_t t) { | |
| return WasmEdge_ValueGenI64(static_cast<int64_t>(t)); | ||
| } | ||
| template <> WasmEdge_Value makeVal(double t) { return WasmEdge_ValueGenF64(t); } | ||
| template <> WasmEdge_Value makeVal(float t) { return WasmEdge_ValueGenF32(t); } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: order. |
||
|
|
||
| // Helper function to print values. | ||
| std::string printValue(const WasmEdge_Value &value) { | ||
|
|
@@ -143,20 +144,24 @@ template <> WasmEdge_ValType convArgToValType<Word>() { return WasmEdge_ValTypeG | |
| template <> WasmEdge_ValType convArgToValType<uint32_t>() { return WasmEdge_ValTypeGenI32(); } | ||
| template <> WasmEdge_ValType convArgToValType<int64_t>() { return WasmEdge_ValTypeGenI64(); } | ||
| template <> WasmEdge_ValType convArgToValType<uint64_t>() { return WasmEdge_ValTypeGenI64(); } | ||
| template <> WasmEdge_ValType convArgToValType<float>() { return WasmEdge_ValTypeGenF32(); } | ||
| template <> WasmEdge_ValType convArgToValType<double>() { return WasmEdge_ValTypeGenF64(); } | ||
|
|
||
| // Helper templates to convert valtype to arg. | ||
| template <typename T> T convValTypeToArg(WasmEdge_Value val); | ||
| template <> uint32_t convValTypeToArg<uint32_t>(WasmEdge_Value val) { | ||
| return static_cast<uint32_t>(WasmEdge_ValueGetI32(val)); | ||
| } | ||
| template <> Word convValTypeToArg<Word>(WasmEdge_Value val) { return WasmEdge_ValueGetI32(val); } | ||
| template <> Word convValTypeToArg<Word>(WasmEdge_Value val) { | ||
| return static_cast<uint32_t>(WasmEdge_ValueGetI32(val)); | ||
| } | ||
| template <> int64_t convValTypeToArg<int64_t>(WasmEdge_Value val) { | ||
| return WasmEdge_ValueGetI64(val); | ||
| } | ||
| template <> uint64_t convValTypeToArg<uint64_t>(WasmEdge_Value val) { | ||
| return static_cast<uint64_t>(WasmEdge_ValueGetI64(val)); | ||
| } | ||
| template <> float convValTypeToArg<float>(WasmEdge_Value val) { return WasmEdge_ValueGetF32(val); } | ||
| template <> double convValTypeToArg<double>(WasmEdge_Value val) { | ||
| return WasmEdge_ValueGetF64(val); | ||
| } | ||
|
|
||
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,148 @@ | ||
| // 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 ArgPassingContext : 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 ArgPassingWasm : public TestWasm { | ||
| public: | ||
| using TestWasm::TestWasm; | ||
| ContextBase *createVmContext() override { return new ArgPassingContext(this); }; | ||
| }; | ||
|
|
||
| class ArgPassingTest : public TestVm { | ||
| public: | ||
| void SetUp() { | ||
| auto source = readTestWasmFile("arg_passing.wasm"); | ||
| ASSERT_FALSE(source.empty()); | ||
| wasm_.emplace(std::move(vm_)); | ||
| ASSERT_TRUE(wasm_->load(source, false)); | ||
| ASSERT_TRUE(wasm_->initialize()); | ||
| context_ = dynamic_cast<ArgPassingContext *>(wasm_->vm_context()); | ||
| ASSERT_NE(context_, nullptr); | ||
| } | ||
|
|
||
| std::optional<ArgPassingWasm> wasm_; | ||
| ArgPassingContext *context_; | ||
| }; | ||
|
|
||
| INSTANTIATE_TEST_SUITE_P(WasmEngines, ArgPassingTest, testing::ValuesIn(getWasmEngines()), | ||
| [](const testing::TestParamInfo<std::string> &info) { | ||
| return info.param; | ||
| }); | ||
|
|
||
| TEST_P(ArgPassingTest, 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(ArgPassingTest, 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(ArgPassingTest, WasmCallReturnsLongValue) { | ||
| 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(ArgPassingTest, 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(ArgPassingTest, 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(ArgPassingTest, 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(ArgPassingTest, 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(ArgPassingTest, 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(ArgPassingTest, 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(ArgPassingTest, 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(ArgPassingTest); | ||
|
|
||
| } // 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -57,6 +57,11 @@ wasm_rust_binary( | |
| srcs = ["trap.rs"], | ||
| ) | ||
|
|
||
| wasm_rust_binary( | ||
| name = "arg_passing.wasm", | ||
| srcs = ["arg_passing.rs"], | ||
|
Comment on lines
+61
to
+62
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be really called |
||
| ) | ||
|
|
||
| wasm_rust_binary( | ||
| name = "resource_limits.wasm", | ||
| srcs = ["resource_limits.rs"], | ||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: order.