Skip to content

Instantly share code, notes, and snippets.

@AchalaSB
Created July 10, 2020 16:08
Show Gist options
  • Select an option

  • Save AchalaSB/a7d38c382a64a429c5663b0be884d72f to your computer and use it in GitHub Desktop.

Select an option

Save AchalaSB/a7d38c382a64a429c5663b0be884d72f to your computer and use it in GitHub Desktop.
String testing
use std::ffi::{CStr, CString};
use std::os::raw::{c_char};
use std::mem;
#[no_mangle]
pub extern fn allocate(size: usize) -> *mut c_char {
let mut data = Vec::with_capacity(size);
let subject = data.as_mut_ptr();
// Avoid memory leakage
mem::forget(data);
subject as *mut c_char
}
#[no_mangle]
pub extern fn greet(subject: *mut c_char) -> *mut c_char {
let subject_value = unsafe { CStr::from_ptr(subject).to_bytes().to_vec() };
let mut output = b"Hello ".to_vec();
output.extend(subject_value);
output.extend(b" from Wasm!");
unsafe { CString::from_vec_unchecked(output) }.into_raw()
}
fn main(){}
package main
import (
"fmt"
"strings"
wasm "github.com/wasmerio/go-ext-wasm/wasmer"
)
func main() {
// Instantiate the module.
bytes, _ := wasm.ReadBytes("../string/target/wasm32-unknown-unknown/release/string.wasm")
instance, _ := wasm.NewInstance(bytes)
defer instance.Close()
// String value input from the transaction payload
StringFromPayload := "Achala"
lengthOfStringFromPayload := len(StringFromPayload)
// Allocating linear memory for the string in the Wasm instance's memory space
allocateLinearMemoryForString, _ := instance.Exports["allocate"](lengthOfStringFromPayload + 1)
inputPointer1 := allocateLinearMemoryForString.ToI32()
// Write the StringFromPayload into the memory.
memory1 := instance.Memory.Data()[inputPointer1:]
copy(memory1, StringFromPayload)
// C-string terminates by NULL.
memory1[lengthOfStringFromPayload] = 0
// Now that string is written into the Wasm memory, we take the pointer as the input for the function
greetFunc, err := instance.Exports["greet"](inputPointer1)
if err != nil {
fmt.Println("error", err)
}
// Get the memory location for the output
outputDataPointer := greetFunc.ToI32()
// Reading the memory space of the output
outputLinearMemory := instance.Memory.Data()[outputDataPointer:]
fmt.Println("Raw output: ", string(outputLinearMemory))
// Read the output value of greet() function from linear memory and print it
nth := 0
var output strings.Builder
for {
if outputLinearMemory[nth] == 0 {
break
}
output.WriteByte(outputLinearMemory[nth])
nth++
}
fmt.Println("Processed output:", output.String())
//TODO: Understanding how Linear Memory can be corrupted
for i := 0; i < len(outputLinearMemory); i++ {
outputLinearMemory[i] = 0 // Corrupting the linear memory of the greet function's output
}
nth = 0
var testCorruptionOutput strings.Builder
for {
if outputLinearMemory[nth] == 0 {
break
}
testCorruptionOutput.WriteByte(outputLinearMemory[nth])
nth++
}
fmt.Println("Processed output after corruption:", testCorruptionOutput.String())
fmt.Println("After corrupting Linear memory: ", string(outputLinearMemory))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment