Skip to content

RuntimeError: memory access out of bounds #2222

@chexiongsheng

Description

@chexiongsheng

how to reproduce:

  1. set up a project named test_proj: https://www.assemblyscript.org/quick-start.html
  2. add ue.ts to test_proj/assembly
export declare class Vector {
    constructor(InX: number, InY: number, InZ: number);
    get X(): number;
    set X(val:number);
    get Y(): number;
    set Y(val:number);
    get Z(): number;
    set Z(val:number);
    Set(InX: number, InY: number, InZ: number): void;
}

4.edit test_proj/assembly/index.ts

import { Vector } from './ue';

export function add(a: i32, b: i32): i32 {
  return a + b;
}

export function newVector():Vector {
  return new Vector(1, 2, 3);
}

export function passVector(v:Vector) :Vector {
  return v;
}

5.edit test_proj/index.js

const fs = require("fs");
const loader = require("@assemblyscript/loader");

const v_cache = [];
//(import "ue" "log" (func $log (param i32)))
const imports = { "ue" : 
    {
        "Vector#constructor": function(t, x, y, z) {
            v_cache.push({x:x, y:y, z:z});
            return v_cache.length - 1;
        },
        "Vector#Set": function(t, x, y, z) {
            v_cache[t].x = x;
            v_cache[t].y = y;
            v_cache[t].z = z;
        },
        
        "Vector#set:X": function(t, x) {
            v_cache[t].x = x;
        },
        
        "Vector#get:X": function(t) {
            return v_cache[t].x;
        }
    } 
};
const wasmModule = loader.instantiateSync(fs.readFileSync(__dirname + "/build/optimized.wasm"), imports);
module.exports = wasmModule.exports;

5.edit test_proj/tests/index.js

const myModule = require("..");

let v = myModule.newVector();

console.log(`memory: ${myModule.memory.buffer.byteLength}`)

myModule.passVector(v);

6.build & run test,

npm run asbuild
node tests\index.js

ouput

memory: 0
wasm://wasm/db0e9072:1



RuntimeError: memory access out of bounds
    at passVector (<anonymous>:wasm-function[4]:0xe6)
    at Object.curr.<computed> [as passVector] (D:\learn\assembly_script\test_proj\node_modules\@assemblyscript\loader\umd\index.js:545:20)
    at Object.<anonymous> (D:\learn\assembly_script\test_proj\tests\index.js:8:10)
    at Module._compile (internal/modules/cjs/loader.js:1076:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1097:10)
    at Module.load (internal/modules/cjs/loader.js:941:32)
    at Function.Module._load (internal/modules/cjs/loader.js:782:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
    at internal/main/run_main_module.js:17:47

if add some code like this in test_proj/assembly/index.ts, problom resolved.

class BBA {
  i : i32
}

export function newBBA() :BBA {
  const r = new BBA();
  r.i = 100;
  return r;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions