stellar / rs-soroban-env

Rust environment for Soroban contracts.
Apache License 2.0
61 stars 42 forks source link

Adding Strings with AssemblyScript #545

Open christian-rogobete opened 1 year ago

christian-rogobete commented 1 year ago
let a = "b" + "c";

in AS code leads to following error when executed in the soroban-cli:

error: HostError
Value: Status(VmError(Validation))

Debug events (newest first):
   0: "Validation"

The easiest way to reproduce, is to use the files that I attached to this issue.

They are compiled from the example that I prepared here: https://github.com/Soneso/as-soroban-examples/tree/main/some_issues

Code of the function add_str from the example:

export function add_str(): RawVal {
  // let a = "b" + "c";
  return fromU32(1);
}

I have compiled the example and attached following files in this issue:

One can call their add_str function as follows:

soroban invoke --wasm noadd.wasm --id 1 --fn add_str

and

soroban invoke --wasm addstr.wasm --id 2 --fn add_str

issue_addstr_files.zip

see also: https://github.com/stellar/sorobanathon/discussions/25

graydon commented 1 year ago

I'm not familiar with the AssemblyScript SDK and in any event I think this is an issue to take up with that SDK, not the soroban environment crate.

graydon commented 1 year ago

Apologies, I misunderstood -- this bug is a request to change the env to provide missing hooks that the AS SDK needs, I guess?

christian-rogobete commented 1 year ago

Hello @graydon,

Thank you for looking into this.

I'm not sure if we need the missing hooks, because I am unable to find out why the validation fails. Info about the runtime is described here. I am using stub:

... stub runtime does not provide a garbage collector at all and never frees (simple bump allocation, extremely small)....

And the hooks, described in the docs as interface are not needed, as least not in this case.

I created a very simple contract to reduce as much overhead as possible:

export function testStr(): val.RawVal {
  return 21 as val.RawVal;
}

This gives me a wasm as follows: nostr-stub.wat. Which is perfectly working.

But if I add the string op:

export function testStr(): val.RawVal {
  let a = "a" + "b"
  return 21 as val.RawVal;
}

It gives me str-stub.wat. Which looks fine for me, but fails with VM Validation error.

As you can see no hooks are used. Therefore I am not sure if we need the hooks. Unfortunately I can not find out why the validation fails.

I also tried with the option --exportRuntime. In this case it wants to use the env hooks. See: export.wat. But the wasm is huge ... and I would like to avoid that.

Wasm files attached: as-wasm.zip