forked from inspektor-gadget/inspektor-gadget
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request inspektor-gadget#3449 from inspektor-gadget/mauric…
…io/wasm-parameters wasm: Add parameters handling to wasm
- Loading branch information
Showing
17 changed files
with
312 additions
and
19 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,64 @@ | ||
// Copyright 2024 The Inspektor Gadget authors | ||
// | ||
// 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. | ||
|
||
package wasm | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/tetratelabs/wazero" | ||
wapi "github.com/tetratelabs/wazero/api" | ||
) | ||
|
||
func (i *wasmOperatorInstance) addParamsFuncs(env wazero.HostModuleBuilder) { | ||
exportFunction(env, "getParamValue", i.getParamValue, | ||
[]wapi.ValueType{ | ||
wapi.ValueTypeI64, // ParamKey | ||
}, | ||
[]wapi.ValueType{wapi.ValueTypeI64}, // Value | ||
) | ||
} | ||
|
||
// getParamValue returns the value of a param. | ||
// Params: | ||
// - stack[0] parameter key | ||
// Return value: | ||
// - Uint64 with the param's value, 0 on error | ||
func (i *wasmOperatorInstance) getParamValue(ctx context.Context, m wapi.Module, stack []uint64) { | ||
paramKeyPtr := stack[0] | ||
|
||
paramKey, err := stringFromStack(m, paramKeyPtr) | ||
if err != nil { | ||
i.logger.Warnf("getParamValue: reading string from stack: %v", err) | ||
stack[0] = 0 | ||
return | ||
} | ||
|
||
val, ok := i.paramValues[paramKey] | ||
if !ok { | ||
i.logger.Warnf("getParamValue: param %q not found", paramKey) | ||
stack[0] = 0 | ||
return | ||
} | ||
|
||
buf := []byte(val) | ||
ret, err := i.writeToGuestMemory(ctx, buf) | ||
if err != nil { | ||
i.logger.Warnf("getParamValue: writing to guest memory: %v", err) | ||
stack[0] = 0 | ||
return | ||
} | ||
|
||
stack[0] = ret | ||
} |
This file contains 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 |
---|---|---|
|
@@ -4,6 +4,7 @@ TEST_ARTIFACTS = \ | |
fields \ | ||
dataarray \ | ||
badguest \ | ||
params \ | ||
# | ||
|
||
all: $(TEST_ARTIFACTS) | ||
|
Binary file not shown.
This file contains 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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
module main | ||
|
||
go 1.22.0 | ||
go 1.22.7 | ||
|
||
require github.com/inspektor-gadget/inspektor-gadget v0.27.0 | ||
|
||
|
This file contains 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
Binary file not shown.
This file contains 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 @@ | ||
wasm: program.go |
This file contains 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,11 @@ | ||
name: params_test | ||
params: | ||
wasm: | ||
param-key: | ||
key: param-key | ||
description: param-description | ||
defaultValue: param-default-value | ||
typeHint: param-type-hint | ||
title: param-title | ||
alias: param-alias | ||
isMandatory: true |
This file contains 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,8 @@ | ||
module main | ||
|
||
go 1.22.7 | ||
|
||
require github.com/inspektor-gadget/inspektor-gadget v0.27.0 | ||
|
||
// use this to be able to compile it locally | ||
replace github.com/inspektor-gadget/inspektor-gadget => ../../../../../ |
This file contains 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 @@ | ||
// TODO: a c file is always needed by the gadget to be built |
This file contains 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,48 @@ | ||
// Copyright 2024 The Inspektor Gadget authors | ||
// | ||
// 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. | ||
|
||
// This program tries as hard as it can to break the host by calling functions | ||
// with wrong arguments. It uses the low level functions directly as the goal is | ||
// to test the host and not the wrapper API. Tests under dataarray and fields | ||
// test also the higher level API. | ||
package main | ||
|
||
import ( | ||
api "github.com/inspektor-gadget/inspektor-gadget/wasmapi/go" | ||
) | ||
|
||
//export gadgetStart | ||
func gadgetStart() int { | ||
val, err := api.GetParamValue("param-key") | ||
if err != nil { | ||
api.Errorf("failed to get param: %v", err) | ||
return 1 | ||
} | ||
|
||
const expected = "param-value" | ||
if val != expected { | ||
api.Errorf("param value should be %q, got: %q", expected, val) | ||
return 1 | ||
} | ||
|
||
_, err = api.GetParamValue("non-existing-param") | ||
if err == nil { | ||
api.Errorf("looking for non-existing-param succeded") | ||
return 1 | ||
} | ||
|
||
return 0 | ||
} | ||
|
||
func main() {} |
This file contains 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.