Skip to content

Commit

Permalink
Allow wild pointers in zero-sized memcpy with bulkmemory (#22789)
Browse files Browse the repository at this point in the history
  • Loading branch information
sbc100 authored Oct 25, 2024
1 parent b8831d3 commit 75caed4
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion system/lib/libc/emscripten_memcpy.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ static void *__memcpy(void *dest, const void *src, size_t n) {
#elif defined(__wasm_bulk_memory__)

static void *__memcpy(void *restrict dest, const void *restrict src, size_t n) {
return _emscripten_memcpy_bulkmem(dest, src, n);
return n ? _emscripten_memcpy_bulkmem(dest, src, n) : dest;
}

#else
Expand Down
11 changes: 11 additions & 0 deletions test/core/test_memcpy_zero_bytes.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <stdio.h>
#include <string.h>

#include <emscripten/heap.h>

int main() {
char buf[1024];
void* oob_address = (void*)(emscripten_get_heap_size() + 10);
memcpy(buf, oob_address, 0);
printf("done\n");
}
1 change: 1 addition & 0 deletions test/core/test_memcpy_zero_bytes.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
done
7 changes: 7 additions & 0 deletions test/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2686,6 +2686,13 @@ def test_copyop(self):
# (llvm-gcc copies items one by one).
self.do_core_test('test_copyop.cpp')

@parameterized({
'': ([],),
'bulkmem': (['-mbulk-memory'],),
})
def test_memcpy_zero_bytes(self, args):
self.do_core_test('test_memcpy_zero_bytes.c', emcc_args=['-fno-builtin'] + args)

def test_memcpy2(self):
self.do_core_test('test_memcpy2.c')

Expand Down

0 comments on commit 75caed4

Please sign in to comment.