1

When heap allocating memory in Rust code compiled to Wasm, memory in a Wasm linear memory is allocated.

Do I understand correctly that the value of the pointer is the offset in that memory? If yes, is it possible and valid to get the value 0 as offset/pointer?

A simple example would be:

let data: Vec<u8> = Vec::with_capacity(123);
let data_ptr = data.as_ptr() as usize;

Can I assume data_ptr is non-zero or would that be a valid address?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Simon Warta
  • 10,850
  • 5
  • 40
  • 78
  • 3
    You can assume that the pointer isn't `std::ptr::null`. – Aplet123 May 27 '20 at 12:08
  • Just read the doc of `with_capacity` answer your question. That said, I don't understand why you want to know that and why you cast it to usize. – Stargateur May 27 '20 at 12:19
  • @Stargateur because I need to create a memory region from Wasm, convert it's address to a type that can be used as a result of an exported Wasm function, and access it from the host to override data. – Simon Warta May 27 '20 at 12:30
  • 1
    Vec guarantees that its pointer is non-null, so the example is maybe not representative of your question? The assumption that null is represented by all bits zero is pervasive in Rust, but I haven't nailed it down as a stable statement of fact (might exist somewhere). – ramslök May 27 '20 at 14:34
  • std::ptr::null is compiled to value 0 (Demo: https://gist.github.com/webmaster128/63646a5ecc3ebd2d136371e89032f63b. Is there a spec for this?) and Vec guarantees allocating at a non-null address. This is great for my use case. But it does not help for heap allocations in general, right? – Simon Warta May 27 '20 at 14:58
  • 1
    @SimonWarta Yes, it does. Rust doesn't allow null to be used in references, and doesn't put the heap there either. The only way you can get null pointer is from C bindings or from manual unsafe construction. – RReverser May 27 '20 at 16:51
  • ah you assume null is zero that false, null is null if null would be zero we would call it zero. In C, `NULL` can be non zero, there is nothing in the standard that state NULL is zero. Same in rust. – Stargateur May 27 '20 at 19:02
  • @Stargateur This is the first part of my question. If pointers in Rust compiled to wasm32-unknown-unknown are offsets in the Wasm linear memory (which experiments show is the case), there must be some pointer that matches the 0 offset. And std::ptr::null compiles to 0. I'm only interested in this environment, not Rust in general. – Simon Warta May 27 '20 at 19:51
  • I expect https://doc.rust-lang.org/std/ptr/struct.NonNull.html to behave correctly so. If you want to be sure that ptr is valid. – Stargateur May 27 '20 at 20:03

1 Answers1

1

Yes, it can be zero.

See Passing a JavaScript string to a Rust function compiled to WebAssembly for an example where it was zero and a workaround was used to avoid conflicting with the traditional NULL value.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
  • Okay, 0 is valid on Wasm level. That's a nice workaround when the host controlls the location. In my case the guest allocates memory. Do you know if I have to do the workaround in the Rust guest code as well? Or maybe the allocator does it intenally already? – Simon Warta May 27 '20 at 14:19