Skip to content

Commit b0fe30b

Browse files
committed
fix addr_validate
1 parent adddaef commit b0fe30b

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

internal/runtime/hostfunctions.go

+13-11
Original file line numberDiff line numberDiff line change
@@ -212,15 +212,17 @@ func hostCanonicalizeAddress(ctx context.Context, mod api.Module, addrPtr, addrL
212212
}
213213

214214
// hostValidateAddress implements addr_validate
215-
func hostValidateAddress(ctx context.Context, mod api.Module, addrPtr, addrLen uint32) uint32 {
215+
func hostValidateAddress(ctx context.Context, mod api.Module, addrPtr uint32) uint32 {
216216
env := ctx.Value("env").(*RuntimeEnvironment)
217217
mem := mod.Memory()
218218

219-
addr, err := ReadMemory(mem, addrPtr, addrLen)
219+
// Read the address bytes directly (no length prefix in Rust)
220+
addr, err := ReadMemory(mem, addrPtr, 32) // Fixed size for addresses
220221
if err != nil {
221222
panic(fmt.Sprintf("failed to read address from memory: %v", err))
222223
}
223224

225+
// Convert to string and validate
224226
_, err = env.API.ValidateAddress(string(addr))
225227
if err != nil {
226228
return 0 // Return 0 for invalid address
@@ -593,22 +595,22 @@ func RegisterHostFunctions(runtime wazero.Runtime, env *RuntimeEnvironment) (waz
593595
Export("api_humanize_address")
594596

595597
builder.NewFunctionBuilder().
596-
WithFunc(func(ctx context.Context, m api.Module, addrPtr, addrLen uint32) (uint32, uint32) {
598+
WithFunc(func(ctx context.Context, m api.Module, addrPtr uint32) uint32 {
597599
ctx = context.WithValue(ctx, "env", env)
598-
return hostCanonicalizeAddress(ctx, m, addrPtr, addrLen)
600+
return hostValidateAddress(ctx, m, addrPtr)
599601
}).
600-
WithParameterNames("addr_ptr", "addr_len").
601-
WithResultNames("ptr", "len").
602-
Export("addr_canonicalize")
602+
WithParameterNames("addr_ptr").
603+
WithResultNames("result").
604+
Export("addr_validate")
603605

604606
builder.NewFunctionBuilder().
605-
WithFunc(func(ctx context.Context, m api.Module, addrPtr, addrLen uint32) uint32 {
607+
WithFunc(func(ctx context.Context, m api.Module, addrPtr, addrLen uint32) (uint32, uint32) {
606608
ctx = context.WithValue(ctx, "env", env)
607-
return hostValidateAddress(ctx, m, addrPtr, addrLen)
609+
return hostCanonicalizeAddress(ctx, m, addrPtr, addrLen)
608610
}).
609611
WithParameterNames("addr_ptr", "addr_len").
610-
WithResultNames("result").
611-
Export("addr_validate")
612+
WithResultNames("ptr", "len").
613+
Export("addr_canonicalize")
612614

613615
// Register Query functions
614616
builder.NewFunctionBuilder().

0 commit comments

Comments
 (0)