Skip to content

Document CGO interface #671

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
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ a.out

# direnv Nix stuff
.direnv/
reference-code/
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ CGO_ENABLED=0 go build ./types
This package contains the code binding the libwasmvm build to the Go code. All
low level FFI handling code belongs there. This package can only be built using
cgo. Using the `internal/` convention makes this package fully private.
For an overview of the exported C functions and their Go wrappers see [docs/CGO_INTERFACE.md](docs/CGO_INTERFACE.md).

#### Package github.com/CosmWasm/wasmvm

Expand Down
61 changes: 61 additions & 0 deletions docs/CGO_INTERFACE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# CGO interface

This document summarises the functions exported by the `libwasmvm` C library and their
wrappers implemented in the Go package `internal/api`. It also lists the Go data
structures from `types/` that cross the cgo boundary.

## Exported functions and Go wrappers

| C function (bindings.h) | Go wrapper |
| --- | --- |
| `init_cache` | `api.InitCache` |
| `store_code` | `api.StoreCode`/`api.StoreCodeUnchecked` |
| `remove_wasm` | `api.RemoveCode` |
| `load_wasm` | `api.GetCode` |
| `pin` | `api.Pin` |
| `unpin` | `api.Unpin` |
| `analyze_code` | `api.AnalyzeCode` |
| `get_metrics` | `api.GetMetrics` |
| `get_pinned_metrics` | `api.GetPinnedMetrics` |
| `release_cache` | `api.ReleaseCache` |
| `instantiate` | `api.Instantiate` |
| `execute` | `api.Execute` |
| `migrate` | `api.Migrate` |
| `migrate_with_info` | `api.MigrateWithInfo` |
| `sudo` | `api.Sudo` |
| `reply` | `api.Reply` |
| `query` | `api.Query` |
| `ibc_channel_open` | `api.IBCChannelOpen` |
| `ibc_channel_connect` | `api.IBCChannelConnect` |
| `ibc_channel_close` | `api.IBCChannelClose` |
| `ibc_packet_receive` | `api.IBCPacketReceive` |
| `ibc_packet_ack` | `api.IBCPacketAck` |
| `ibc_packet_timeout` | `api.IBCPacketTimeout` |
| `ibc_source_callback` | `api.IBCSourceCallback` |
| `ibc_destination_callback` | `api.IBCDestinationCallback` |
| `ibc2_packet_receive` | `api.IBC2PacketReceive` |
| `ibc2_packet_ack` | `api.IBC2PacketAck` |
| `ibc2_packet_timeout` | `api.IBC2PacketTimeout` |
| `ibc2_packet_send` | `api.IBC2PacketSend` |
| `new_unmanaged_vector` | internal helpers in `memory.go` |
| `destroy_unmanaged_vector` | internal helpers in `memory.go` |
| `version_str` | `api.LibwasmvmVersion` |

## Data structures crossing the boundary

Several types defined in the `types` package are used when calling into
`libwasmvm` or when reading its results:

- `VMConfig` – configuration passed to `InitCache`.
- `GasMeter` – interface supplying gas information for execution.
- `KVStore` and `Iterator` – database interface used by the VM.
- `GoAPI` – set of callbacks for address handling.
- `Querier` – interface for custom queries.
- `AnalysisReport` – returned from `AnalyzeCode`.
- `Metrics` and `PinnedMetrics` – returned from metric queries.
- `GasReport` – returned from execution functions.

All other data (such as contract messages, environment and info values) is
passed as byte slices and therefore does not rely on additional exported Go
structures.