Skip to content
This repository has been archived by the owner on Jul 28, 2024. It is now read-only.

Memory ownership #5

Closed
taoyuanyuan opened this issue Jul 8, 2020 · 13 comments
Closed

Memory ownership #5

taoyuanyuan opened this issue Jul 8, 2020 · 13 comments

Comments

@taoyuanyuan
Copy link

https://github.com/proxy-wasm/spec/tree/master/abi-versions#memory-ownership
I see only malloc mem, how to free mem on Golang? thx

//go:export malloc
func malloc(size uint) *byte {
	buf := make([]byte, size)
	return &buf[0]
}
@mathetake
Copy link
Member

mathetake commented Jul 9, 2020

First of all, TinyGo implements the mark and sweep grabage collector [link].

The allocated pointers are assigned to variables we create when we get data from the host environment. For example, you pass the pointer of retData *byte variable (created here) to proxy_get_buffer_bytes abi call. Then, IIUC, the host environment (implementation is here) call malloc with the size of required data and pass the returned pointer to the &retData variable inside the host environment. In this way, the allocated memory can be reached from WASM's vm and can be garbage collected normally.

@taoyuanyuan
Copy link
Author

thank you ,This is a good project.
Does standard Go will support GC in this issue golang/go#31105

@mathetake
Copy link
Member

mathetake commented Jul 9, 2020

Well, I am not sure about that, but the point is Go's official binary cannot be used for proxy-wasm since;

@taoyuanyuan
Copy link
Author

I want my project is in go to support proxy-wasm,How can I implement proxy-wasm-go-host,such as https://github.com/proxy-wasm/proxy-wasm-cpp-host
Is it difficult or go runtime not support it?

@mathetake
Copy link
Member

mathetake commented Jul 9, 2020

host environments in Go can be implemented once we have WASM virtual machine and interpreter written in Go (e.g. https://github.com/mathetake/gasm, though mine is a kind of hobbiest one). That is a completely separated issue and nothing to do with Go's limitation for producing proxy-wasm ABI compatible wasm binary as we discussed above.

@mathetake
Copy link
Member

Just for my curiosity, is your project https://github.com/mosn/mosn ? 👀

@taoyuanyuan
Copy link
Author

taoyuanyuan commented Jul 9, 2020

Just for my curiosity, is your project https://github.com/mosn/mosn ? 👀

yes.
I have been studying wasm, I will have a try at gasm.
thank you!

@mathetake
Copy link
Member

gasm is just a toy VM for my learning purpose and it's far from completion. you'd better give it a shot to https://github.com/wasmerio/go-ext-wasm

Anyway, if there's anything I can do to mosn, let me know:)

@taoyuanyuan
Copy link
Author

First of all, TinyGo implements the mark and sweep grabage collector [link].

The allocated pointers are assigned to variables we create when we get data from the host environment. For example, you pass the pointer of retData *byte variable (created here) to proxy_get_buffer_bytes abi call. Then, IIUC, the host environment (implementation is here) call malloc with the size of required data and pass the returned pointer to the &retData variable inside the host environment. In this way, the allocated memory can be reached from WASM's vm and can be garbage collected normally.

I'm sorry,I have some confusion.
"the WASM GC" only exists as a proposal https://github.com/WebAssembly/gc.
How do WASM builded by tinyGo support GC?

@mathetake
Copy link
Member

mathetake commented Jul 12, 2020

TinyGo does not use the proposed WASM's gc but implements it at the language level (and this is how TinyGo supports GC on multiple platforms, not only on wasm)

@taoyuanyuan
Copy link
Author

taoyuanyuan commented Jul 12, 2020

TinyGo does not use the proposed WASM's gc but implements it at the language level (and this is how TinyGo supports GC on multiple platforms, not only on wasm)

(func $runtime.alloc (type 14) (param i32) (result i32)
    (local i32 i32 i32 i32 i32 i32)
    block  ;; label = @1
      local.get 0
      i32.eqz
      br_if 0 (;@1;)
      local.get 0
      i32.const 15
      i32.add
      i32.const 4
      i32.shr_u
      local.set 1
      i32.const 0
      i32.load offset=75528
      local.tee 2
      local.set 3
      i32.const 0
      local.set 4
      i32.const 0
      local.set 5
      loop  ;; label = @2
        block  ;; label = @3
          local.get 3
          local.get 2
          i32.ne
          br_if 0 (;@3;)
          local.get 5
          i32.const 255
          i32.and
          local.set 2
          i32.const 1
          local.set 5
          block  ;; label = @4
            block  ;; label = @5
              local.get 2
              i32.const 1
              i32.gt_u
              br_if 0 (;@5;)
              local.get 2
              br_table 2 (;@3;) 1 (;@4;) 2 (;@3;)
            end
            i32.const 66085
            i32.const 13
            call $runtime.runtimePanic
            unreachable
          end
          call $runtime.GC

oh, it call runtime.GC when runtime.alloc, the performance will be not high, is it ture?
BTW, do you know any projects that support go host implementation for proxy-wasm?
thank you very much!

@mathetake
Copy link
Member

mathetake commented Jul 13, 2020

the performance will be not high, is it ture?

yeah, compared to c++ and Rust

BTW, do you know any projects that support go host implementation for proxy-wasm?

AFAIK, there's no project for proxy-wasm host in go

@taoyuanyuan
Copy link
Author

https://github.com/wasmerio/go-ext-wasm

I have try go-ext-wasm, and it only supports integer when import func. func (imports *Imports) AppendFunction

Invalid input type for the `proxy_log` imported function; given `ptr`; only accept `int32`, `int64`, `float32`, and `float64`.

I can‘t implement proxy-wasm func by go-ext-wasm.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants