Skip to content

dynamicAlloc() doesn't work with memory growth #8637

Closed

Description

@kichikuou

Emscripten 1.38.31

main.c:

int main() {
}

post.js:

for (var i = 0; i < 3; i++) {
    var buf = getMemory(10000000);
    console.log(buf);
}
$ emcc -s ALLOW_MEMORY_GROWTH=1 main.c --post-js post.js -o out.js
$ node out.js
5246848
15246848
15246848

Note that second and third getMemory() calls return the same pointer.

This is because dynamicAlloc() doesn't update HEAP32[DYNAMICTOP_PTR>>2] when the heap is resized.

if (end <= _emscripten_get_heap_size()) {
HEAP32[DYNAMICTOP_PTR>>2] = end;
} else {
#if ALLOW_MEMORY_GROWTH
var success = _emscripten_resize_heap(end);
if (!success) return 0;
#else
return 0;
#endif
}
return ret;
}

It seems like a bug introduced by #7897.

Activity

  1. added a commit that references this issue on May 20, 2019
    0e08236
  2. kripken commented on May 20, 2019

    @kripken
    Member

    Thanks, I opened #8643 to throw a clear error on this. More details there.

  3. added a commit that references this issue on May 21, 2019
    eddcbda
  4. added a commit that references this issue on May 21, 2019
    a8c8706
  5. added a commit that references this issue on Dec 23, 2020
    aa6bc44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions