【问题标题】:memory access out of bounds – but why?内存访问越界——但为什么呢?
【发布时间】:2021-05-16 22:03:09
【问题描述】:

我在 js 和 wasm 之间共享一块内存,使用“导入”内存。在我的汇编脚本代码中,我什至没有访问预分配的内存并且仍然得到RuntimeError: memory access out of bounds。我想知道为什么。

js:

const [width, height] = [100, 100];
const arraySize = width * height;
const pageSize = 64 * 1024;
const nPages = Math.ceil(arraySize / pageSize);
const memory = new WebAssembly.Memory({ 
  initial: nPages 
});
WebAssembly
  .instantiateStreaming(fetch('/build/optimized.wasm'), {
    env: {
      memory,
      abort: (_msg, _file, line, column) => {
        console.error(`Abort at ${line}:${column}`)
      },
    }
  })
  .then(({instance}) => {
    const bytes = new Uint8ClampedArray(memory.buffer);
    
    // A
    for (let i = 0; i < arraySize; i++) {
      bytes[i] = 1;
    }

    instance.exports.asdf(width, height);
  });

汇编脚本:

export function asdf(width: i32, height: i32): void {
  // B
  const arr = new Uint8Array(width * height);
}

当我删除 AB 时,它会起作用。


编辑:

奇怪:设置const [width, height] = [39, 39]; 也不会产生错误。


编辑 2:

我正在使用导入的内存,因为我找到的所有示例都是这样做的。我是否应该按照它们在此处显示的方式创建数组等? https://www.assemblyscript.org/loader.html#creating-arrays

【问题讨论】:

  • 您是否为asc 使用了--importMemory 标志?
  • 是的,我确实这样做了。

标签: webassembly assemblyscript


【解决方案1】:

我没有看到任何错误。见playground

另外请记住,当您在 AssemblyScript 中分配内存时,您可能会根据您的示例在主机端覆盖之前初始化的内存。

【讨论】:

    猜你喜欢
    • 2021-10-06
    • 2020-09-03
    • 1970-01-01
    • 2013-10-11
    • 1970-01-01
    • 2021-06-15
    • 2012-01-24
    • 1970-01-01
    • 2011-04-29
    相关资源
    最近更新 更多