Skip to content
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

[x/programs] Debug arbitrary variable in programs #892

Merged
merged 10 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 35 additions & 2 deletions x/programs/examples/imports/pstate/pstate.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ package pstate
import (
"context"
"errors"
"fmt"
"os"

"github.com/ava-labs/avalanchego/database"
"github.com/ava-labs/avalanchego/utils/logging"
Expand Down Expand Up @@ -50,8 +52,10 @@ func (i *Import) Register(link *host.Link, _ program.Context) error {
if err := wrap.RegisterAnyParamFn(Name, "get", 2, i.getFnVariadic); err != nil {
return err
}

return wrap.RegisterAnyParamFn(Name, "delete", 2, i.deleteFnVariadic)
if err := wrap.RegisterAnyParamFn(Name, "delete", 2, i.deleteFnVariadic); err != nil {
return err
}
return wrap.RegisterAnyParamFn(Name, "log", 2, i.logFnVariadic)
}

func (i *Import) putFnVariadic(caller *program.Caller, args ...int32) (*types.Val, error) {
Expand All @@ -75,6 +79,13 @@ func (i *Import) deleteFnVariadic(caller *program.Caller, args ...int32) (*types
return i.deleteFn(caller, args[0], args[1])
}

func (i *Import) logFnVariadic(caller *program.Caller, args ...int32) (*types.Val, error) {
if len(args) != 2 {
return nil, errors.New("expected 2 arguments")
}
return i.logFn(caller, args[0], args[1])
}

type putArgs struct {
ProgramID [32]byte
Key []byte
Expand Down Expand Up @@ -260,3 +271,25 @@ func (i *Import) deleteFn(caller *program.Caller, memOffset int32, size int32) (

return types.ValI32(int32(ptr)), nil
}

func (i *Import) logFn(caller *program.Caller, memOffset int32, size int32) (*types.Val, error) {
memory, err := caller.Memory()
if err != nil {
i.log.Error("failed to get memory from caller",
zap.Error(err),
)
return nil, err
}

bytes, err := memory.Range(uint32(memOffset), uint32(size))
if err != nil {
i.log.Error("failed to read args from program memory",
zap.Error(err),
)
return nil, err
}
richardpringle marked this conversation as resolved.
Show resolved Hide resolved

fmt.Fprintf(os.Stderr, "%s\n", bytes)
richardpringle marked this conversation as resolved.
Show resolved Hide resolved

return types.ValI32(0), nil
}
4 changes: 3 additions & 1 deletion x/programs/rust/wasmlanche-sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ pub mod params;
pub mod state;
pub mod types;

mod logging;
mod memory;
mod program;

pub use self::{
logging::log,
memory::from_host_ptr,
params::{serialize_param, Params},
program::Program,
Expand All @@ -26,7 +28,7 @@ pub enum Error {
Param(#[from] std::io::Error),
}

#[derive(Clone, Copy, borsh::BorshSerialize, borsh::BorshDeserialize)]
#[derive(Clone, Copy, borsh::BorshSerialize, borsh::BorshDeserialize, Debug)]
pub struct Context {
pub program: program::Program,
}
49 changes: 49 additions & 0 deletions x/programs/rust/wasmlanche-sdk/src/logging.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#[macro_export]
macro_rules! dbg {
() => {
#[cfg(debug_assertions)]
{
let as_string = format!("[{}:{}:{}]", file!(), line!(), column!());
$crate::log(as_string.as_str());
}
};
($val:expr $(,)?) => {{
match $val {
tmp => {
#[cfg(debug_assertions)]
{
let as_string = format!(
"[{}:{}:{}] {} = {:#?}",
file!(),
line!(),
column!(),
stringify!($val),
&tmp
);
$crate::log(as_string.as_str());
}
tmp
}
}
}};
($($val:expr),+ $(,)?) => {
($($crate::dbg!($val)),+,)
};
}

/// # Panics
/// Panics if there was an issue regarding memory allocation on the host
pub fn log(text: &str) {
log_bytes(text.as_bytes());
}

/// Logging facility for debugging purposes
pub(super) fn log_bytes(bytes: &[u8]) {
#[link(wasm_import_module = "state")]
extern "C" {
#[link_name = "log"]
fn ffi(ptr: *const u8, len: usize) -> i32;
}

unsafe { ffi(bytes.as_ptr(), bytes.len()) };
}
2 changes: 1 addition & 1 deletion x/programs/rust/wasmlanche-sdk/src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::hash::Hash;

/// Represents the current Program in the context of the caller. Or an external
/// program that is being invoked.
#[derive(Clone, Copy, BorshDeserialize, BorshSerialize)]
#[derive(Clone, Copy, BorshDeserialize, BorshSerialize, Debug)]
pub struct Program([u8; Self::LEN]);

impl Program {
Expand Down
Loading