Skip to content

Commit

Permalink
Move delarations of emscripten_builtin_malloc to heap.h. NFC
Browse files Browse the repository at this point in the history
These functions are not specific to emmalloc so don't belong in
that header.
  • Loading branch information
sbc100 committed May 29, 2022
1 parent 719b7c6 commit 5def5b8
Show file tree
Hide file tree
Showing 12 changed files with 72 additions and 88 deletions.
3 changes: 0 additions & 3 deletions system/include/emscripten/emmalloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,11 @@ void emmalloc_dump_memory_regions(void);
// Allocates size bytes with the given pow-2 alignment.
void *memalign(size_t alignment, size_t size);
void *emmalloc_memalign(size_t alignment, size_t size);
void *emscripten_builtin_memalign(size_t alignment, size_t size);
void *aligned_alloc(size_t alignment, size_t size);

// Allocates size bytes with default alignment (8 bytes)
void *malloc(size_t size);
void *emmalloc_malloc(size_t size);
void *emscripten_builtin_malloc(size_t size);

// Returns the number of bytes that are actually allocated to the given pointer ptr.
// E.g. due to alignment or size requirements, the actual size of the allocation can be
Expand All @@ -36,7 +34,6 @@ size_t emmalloc_usable_size(void *ptr);
// (emmalloc_)realloc, emmalloc_realloc_try, emmalloc_realloc_uninitialized, (emmalloc_)aligned_realloc
void free(void *ptr);
void emmalloc_free(void *ptr);
void emscripten_builtin_free(void *ptr);

// Performs a reallocation of the given memory pointer to a new size. If the memory region
// pointed by ptr cannot be resized in place, a new memory region will be allocated, old
Expand Down
7 changes: 7 additions & 0 deletions system/include/emscripten/heap.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ size_t emscripten_get_heap_size(void);
// Returns the max size of the WebAssembly heap.
size_t emscripten_get_heap_max(void);

// Direct access to the system allocator. Use these to access that underlying
// allocator when intercepting/wrapping the allocator API. Works with with both
// dlmalloc and emmalloc.
void *emscripten_builtin_memalign(size_t alignment, size_t size);
void *emscripten_builtin_malloc(size_t size);
void emscripten_builtin_free(void *ptr);

#ifdef __cplusplus
}
#endif
23 changes: 11 additions & 12 deletions system/lib/compiler-rt/lib/lsan/lsan_interceptors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@
#include "lsan_common.h"
#include "lsan_thread.h"

#if SANITIZER_EMSCRIPTEN
#define __ATTRP_C11_THREAD ((void*)(uptr)-1)
#include <emscripten/heap.h>
extern "C" {
int emscripten_builtin_pthread_create(void *thread, void *attr,
void *(*callback)(void *), void *arg);
int emscripten_builtin_pthread_join(void *th, void **ret);
int emscripten_builtin_pthread_detach(void *th);
}
#endif

#include <stddef.h>

using namespace __lsan;
Expand Down Expand Up @@ -403,18 +414,6 @@ INTERCEPTOR(int, pthread_atfork, void (*prepare)(), void (*parent)(),
#define LSAN_MAYBE_INTERCEPT_PTHREAD_ATFORK
#endif

#if SANITIZER_EMSCRIPTEN
#define __ATTRP_C11_THREAD ((void*)(uptr)-1)
extern "C" {
int emscripten_builtin_pthread_create(void *thread, void *attr,
void *(*callback)(void *), void *arg);
int emscripten_builtin_pthread_join(void *th, void **ret);
int emscripten_builtin_pthread_detach(void *th);
void *emscripten_builtin_malloc(size_t size);
void emscripten_builtin_free(void *);
}
#endif

#if SANITIZER_INTERCEPT_STRERROR
INTERCEPTOR(char *, strerror, int errnum) {
__lsan::ScopedInterceptorDisabler disabler;
Expand Down
1 change: 0 additions & 1 deletion system/lib/libc/emscripten_mmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#include <sys/mman.h>

#include <emscripten/heap.h>
#include <emscripten/emmalloc.h>

#include "lock.h"
#include "syscall.h"
Expand Down
4 changes: 1 addition & 3 deletions system/lib/libc/musl/src/env/__environ.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ weak_alias(__environ, environ);
#ifdef __EMSCRIPTEN__
#include <stdlib.h>
#include <wasi/api.h>
// Included for emscripten_builtin_free / emscripten_builtin_malloc
// TODO(sbc): Should these be in their own header to avoid emmalloc here?
#include <emscripten/emmalloc.h>
#include <emscripten/heap.h>

// We use emscripten_builtin_malloc here because this memory is never freed and
// and we don't want LSan to consider this a leak.
Expand Down
4 changes: 1 addition & 3 deletions system/lib/pthread/emscripten_tls_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
* found in the LICENSE file.
*/

// Included for emscripten_builtin_free / emscripten_builtin_malloc
// TODO(sbc): Should these be in their own header to avoid emmalloc here?
#include <emscripten/emmalloc.h>
#include <emscripten/heap.h>
#include <emscripten/threading.h>

#include "pthread_impl.h"
Expand Down
4 changes: 1 addition & 3 deletions system/lib/pthread/pthread_create.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@
#include <string.h>
#include <threads.h>
#include <unistd.h>
// Included for emscripten_builtin_free / emscripten_builtin_malloc
// TODO(sbc): Should these be in their own header to avoid emmalloc here?
#include <emscripten/emmalloc.h>
#include <emscripten/heap.h>

#define STACK_ALIGN 16

Expand Down
4 changes: 1 addition & 3 deletions system/lib/pthread/thread_profiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
#include <stdbool.h>
#include "pthread_impl.h"
#include <emscripten/threading.h>
// Included for emscripten_builtin_free / emscripten_builtin_malloc
// TODO(sbc): Should these be in their own header to avoid emmalloc here?
#include <emscripten/emmalloc.h>
#include <emscripten/heap.h>

static bool enabled = false;

Expand Down
48 changes: 48 additions & 0 deletions tests/core/test_wrap_malloc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright 2016 The Emscripten Authors. All rights reserved.
// Emscripten is available under two separate licenses, the MIT license and the
// University of Illinois/NCSA Open Source License. Both these licenses can be
// found in the LICENSE file.

#include <assert.h>
#include <emscripten/console.h>
#include <emscripten/heap.h>
#include <stdio.h>
#include <stdlib.h>

static int totalAllocs;
static int totalFrees;

void *malloc(size_t size) {
++totalAllocs;
void *ptr = emscripten_builtin_malloc(size);
emscripten_console_logf("Allocated %zu bytes, got %p. %d pointers allocated total.", size, ptr, totalAllocs);
return ptr;
}

void free(void *ptr) {
++totalFrees;
emscripten_builtin_free(ptr);
emscripten_console_logf("Freed ptr %p, %d pointers freed total.", ptr, totalFrees);
}

int main() {
// Reset these globals here, to ignore any allocations during startup by the
// system
totalAllocs = 0;
totalFrees = 0;

for (int i = 0; i < 20; ++i) {
void *ptr = malloc(1024 * 1024);
// Make sure we do something with the pointer do ensure the optimizer
// doesn't completely remove the allocation.
emscripten_console_logf("alloacted: %p", ptr);
free(ptr);
}

emscripten_console_logf("totalAllocs: %d", totalAllocs);
emscripten_console_logf("totalFrees: %d", totalFrees);
assert(totalAllocs == 20);
assert(totalFrees == 20);
emscripten_console_logf("OK.");
return 0;
}
2 changes: 1 addition & 1 deletion tests/pthread/test_pthread_proxying_refcount.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <emscripten/console.h>
#include <emscripten/emscripten.h>
#include <emscripten/proxying.h>
#include <emscripten/heap.h>
#include <pthread.h>
#include <stdbool.h>
#include <unistd.h>
Expand All @@ -21,7 +22,6 @@ em_proxying_queue* queues[4];

int queues_freed[4] = {};

extern void emscripten_builtin_free(void* mem);
void __attribute__((noinline)) free(void* ptr) {
for (int i = 0; i < 4; i++) {
if (ptr && queues[i] == ptr) {
Expand Down
2 changes: 1 addition & 1 deletion tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -8475,7 +8475,7 @@ def test_mallinfo(self):
@no_asan('cannot replace malloc/free with ASan')
@no_lsan('cannot replace malloc/free with LSan')
def test_wrap_malloc(self):
self.do_runf(test_file('wrap_malloc.cpp'), 'OK.')
self.do_runf(test_file('core/test_wrap_malloc.c'), 'OK.')

def test_environment(self):
self.set_setting('ASSERTIONS')
Expand Down
58 changes: 0 additions & 58 deletions tests/wrap_malloc.cpp

This file was deleted.

0 comments on commit 5def5b8

Please sign in to comment.