echojoys

echojoys

I’m currently learning the interaction between wasm and elixir. I am writing wasm in rust language, but it can only return the starting position of the address of the string, which is *const u8. I can’t get the length. What is the solution? Thanks again.

Thanks! :smiley:

Showing Posts 1 to 10

kjwvanijk

kjwvanijk

TL;DR: At least on x86_64 a &str is two u64 (16 byte) long. The first u64 is a pointer and the second u64 is the length of the string.
Rust insight: memory layout of "&str" – PHIPS BLOG

So with binary patten matching you could extract the string lenght like this:
<<txt::64, length::64>> = string_binary

Be mindfull though Rust also has a String struct which is layed out differently in memory.

Hope this helps.

echojoys

echojoys OP

Hello, I don’t know what should be returned, sorry to ask you again, wasm seems to only return i32/i64 and f32/f64. I still don’t know how to combine the address and length of the memory together

echojoys

echojoys OP

Thanks, I’ve found a way to store the address and length using the high and low bits in binary. Then use bit operation to extract it, also based on your blog, thank you very much :grinning: :grin:

kjwvanijk

kjwvanijk

YW. It is not my blog though just something I found on google. The author of the blog is Philipp Schuster.

echojoys

echojoys OP

We know wasm is unable to return the string directly, of course we can use some libraries, but with elixir wasmex is unable to get directly, we usually pass in the address and length of the string through the memory function to get the string. Currently some languages already support multi-value return, but rust is currently unable to do multi-value return, so I used binary bit manipulation to solve this problem this is my github repository, I hope to get everyone’s advice

dimitarvp

dimitarvp

What’s stopping you from getting a tuple, or a dedicated struct even?

Furthermore, why must you poke in the guts of a static string slice? Can’t you just return a copied String?

Not sure what’s the problem that you seek to solve here. You only mention your dig at the problem, and not the root problem itself.

echojoys

echojoys OP

In rust, Consider the following function:

#[no_mangle]
pub extern "C" fn pair(a: u32, b: u32) -> [u32; 2] {
    [a, b]
}

He will compile the return value as a parameter
LLVM will by default compile this down into the following Wasm:

(func $pair (param i32 i32 i32)
  local.get 0
  local.get 2
  i32.store offset=4
  local.get 0
  local.get 1
  i32.store)

wasm only seems to support i32/i64 and f32/f64 at the moment, which is why I can’t return the strings directly. I probably didn’t answer very well, thanks for your comments. If it’s not the answer you want, I hope you can ask again, thank you very much

dimitarvp

dimitarvp

My question is: what are you trying to achieve exactly? Why do you want a pointer + length returned? Why not a normal copied string?

That’s what I don’t get. Not sure LLVM’s IR matters here? :thinking:

echojoys

echojoys OP

I want to use wasm written by rust to handle some data, the data is not necessarily integer and floating point but there is no string type supported in the documentation of wasm, if it is a boolean value I can use 0/1 instead, so I can’t return the string properly. wasmex is the hex library used by elixir to call wasm.

We can’t know the length like “hello world”, so we need to return both the start address and the length of the memory. That’s why I mentioned above that rust can’t return multiple values, so I had to figure out how to get his length and address when calling elixir. My idea was to combine memory and length into one integer return, and then split it up into length and memory address when elixir is called。

Thank you very much, I don’t know if this answer is the answer you want. Because my English is very poor, I may not be able to understand you correctly. I’m sorry, if you have questions you can keep asking, I want to learn more. :grinning:

tessi

tessi

Hey @echojoys, thanks for looking into wasmex and sorry for being notoriously late in replying :slight_smile:

I wanted to implement multi-value returns in wasmex exactly for the use case you have (save string returns). Back then, I couldn’t because the APIs in underlying libraries/tooling just weren’t ready. But this changed in the meantime. Have a look at: wasmtime/examples/multi.rs at main · bytecodealliance/wasmtime · GitHub Here, a tuple with two (and more) values is returned from wasm. The first part in that tuple could be the string length, the second could be the memory pointer.

I’d be happy to eventually implement support for this in wasmex - feel free to open an issue or give implementing it a try yourself if you feel like you want to contribute! :slight_smile:

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews