Skip to content

array.length set to lower value should remove items properly #900

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

Closed
vird opened this issue Oct 11, 2019 · 4 comments · Fixed by #923
Closed

array.length set to lower value should remove items properly #900

vird opened this issue Oct 11, 2019 · 4 comments · Fixed by #923
Labels

Comments

@vird
Copy link

vird commented Oct 11, 2019

array.length manipulations allowed in JS/TS so I tried this

class Struct {
  a:i32;
}
export function report(): i32 {
  let res = 0;
  let arr:Struct[] = [];
  
  for(let i=0;i<10;i++) {
    arr.push(new Struct());
  }
  arr.length = 0; // will throw here
  
  return res;
}

It produces Uncaught Error: abort: Index out of range at ~lib/rt.ts:22:27

Also setting length to higher values should allocate more memory, but fill with 0 (because on array deallocation it would try free pointer to garbage)

@MaxGraey
Copy link
Member

MaxGraey commented Oct 11, 2019

Can't reproduce this
https://webassembly.studio/?f=toghxldo0tl

@MaxGraey
Copy link
Member

And that's always works properly and often used. Could you recheck?

@vird
Copy link
Author

vird commented Oct 11, 2019

Reproduced with following steps
1 create index.ts

class Struct {
  a:i32;
}
export function report(): i32 {
  let res = 0;
  let arr:Struct[] = [];
  
  for(let i=0;i<10000;i++) {
    arr.push(new Struct());
  }
  arr.length = 0;
  
  return res;
}

2 ./node_modules/.bin/asc ./index.ts -b ./index.wasm -t ./index.wat --validate --use abort=
used ./node_modules/.bin/asc --version 0.7.0
node -v v11.15.0
3 wasm_launch.js

 fs = require('fs');
 cont = fs.readFileSync('index.wasm');
 WebAssembly.instantiate(cont, {imports:{}})
   .then(module=>{
     module.instance.exports.report()
   })

result

(node:28807) UnhandledPromiseRejectionWarning: RuntimeError: unreachable
    at wasm-function[18]:17
    at wasm-function[24]:104
    at wasm-function[25]:13
    at wasm-function[33]:38
    at wasm-function[34]:75
    at WebAssembly.instantiate.then.module (/data/nodejs/experiments/as_hotreload_env/wasm_launch.js:5:29)

https://gist.github.com/vird/e0fb47a79696fd87a54b920fa58d02ac

@MaxGraey
Copy link
Member

MaxGraey commented Oct 11, 2019

That's strange. I reproduced this only locally with --runtime full but not with runtime --half. In WebAssembly Studio even with --runtime full this not reproduced.

Output with (asc --debug):

  RuntimeError: memory access out of bounds
    at ~lib/rt/pure/decrement (wasm-function[25]:0x1886)
    at ~lib/rt/pure/__release (wasm-function[26]:0x195b)
    at ~lib/array/Array<index/Struct>#set:length (wasm-function[34]:0x1dbe)
    at index/report (wasm-function[36]:0x1e2e)
    at ./.../reproduce-as-bug/run.js:10:27

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants