Skip to content

Commit

Permalink
Revamp memory allocation (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
anuraaga authored Oct 24, 2022
1 parent fce4e41 commit 23aba71
Show file tree
Hide file tree
Showing 14 changed files with 765 additions and 63 deletions.
6 changes: 2 additions & 4 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,8 @@ jobs:
ENVOY_IMAGE=$image go run mage.go e2e
done
# Currently excluded, go-ftw fails to find the destination after a while. Locally works fine.
# - name: Run regression tests (ftw)
# shell: bash
# run: go run mage.go ftw
- name: Run regression tests (ftw)
run: go run mage.go ftw

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
Expand Down
14 changes: 14 additions & 0 deletions buildtools/mimalloc/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Copyright The OWASP Coraza contributors
# SPDX-License-Identifier: Apache-2.0

FROM ghcr.io/corazawaf/coraza-proxy-wasm/buildtools-wasi-sdk:main

RUN apt-get install -y cmake patch

RUN mkdir -p /mimalloc && curl -L https://github.com/microsoft/mimalloc/archive/refs/tags/v2.0.6.tar.gz | tar -xz --strip-components 1 -C /mimalloc
WORKDIR /mimalloc
ADD mimalloc.patch mimalloc.patch
RUN patch -p1 < mimalloc.patch
RUN mkdir -p out/release && cd out/release && cmake ../.. -DMI_BUILD_SHARED=off -DMI_BUILD_TESTS=off && make && llvm-ranlib-14 libmimalloc.a

CMD ["cp", "./out/release/libmimalloc.a", "/out/libmimalloc.a"]
34 changes: 34 additions & 0 deletions buildtools/mimalloc/mimalloc.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
diff --git a/include/mimalloc-atomic.h b/include/mimalloc-atomic.h
index 7ad5da5..3029d97 100644
--- a/include/mimalloc-atomic.h
+++ b/include/mimalloc-atomic.h
@@ -325,7 +325,7 @@ static inline void mi_atomic_yield(void) {
#elif defined(__wasi__)
#include <sched.h>
static inline void mi_atomic_yield(void) {
- sched_yield();
+ // sched_yield();
}
#else
#include <unistd.h>
diff --git a/src/alloc-override.c b/src/alloc-override.c
index e29cb4b..19f6822 100644
--- a/src/alloc-override.c
+++ b/src/alloc-override.c
@@ -256,7 +256,7 @@ int reallocarr(void* p, size_t count, size_t size) { return mi_reallocarr(p
void* memalign(size_t alignment, size_t size) { return mi_memalign(alignment, size); }
void* _aligned_malloc(size_t alignment, size_t size) { return mi_aligned_alloc(alignment, size); }

-#if defined(__GLIBC__) && defined(__linux__)
+#if defined(__GLIBC__) && defined(__linux__) || defined(__wasi__)
// forward __libc interface (needed for glibc-based Linux distributions)
void* __libc_malloc(size_t size) MI_FORWARD1(mi_malloc,size)
void* __libc_calloc(size_t count, size_t size) MI_FORWARD2(mi_calloc,count,size)
@@ -268,6 +268,7 @@ void* _aligned_malloc(size_t alignment, size_t size) { return mi_aligned_allo
void* __libc_pvalloc(size_t size) { return mi_pvalloc(size); }
void* __libc_memalign(size_t alignment, size_t size) { return mi_memalign(alignment,size); }
int __posix_memalign(void** p, size_t alignment, size_t size) { return mi_posix_memalign(p,alignment,size); }
+ void* aligned_alloc(size_t alignment, size_t size) { return mi_aligned_alloc(alignment, size); }
#endif

#ifdef __cplusplus
4 changes: 3 additions & 1 deletion init_tinygo.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@

package main

// #cgo LDFLAGS: lib/libinjection.a lib/libre2.a lib/libcre2.a lib/libc++.a lib/libc++abi.a lib/libclang_rt.builtins-wasm32.a lib/libaho_corasick.a
import _ "github.com/corazawaf/coraza-proxy-wasm/internal/gc"

// #cgo LDFLAGS: lib/libinjection.a lib/libre2.a lib/libcre2.a lib/libc++.a lib/libc++abi.a lib/libclang_rt.builtins-wasm32.a lib/libaho_corasick.a lib/libmimalloc.a
import "C"
45 changes: 0 additions & 45 deletions internal/calloc/calloc.go

This file was deleted.

6 changes: 0 additions & 6 deletions internal/calloc/doc.go

This file was deleted.

9 changes: 9 additions & 0 deletions internal/gc/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright The OWASP Coraza contributors
// SPDX-License-Identifier: Apache-2.0

// Package gc is a custom gargabe gollector for TinyGo. The main difference is instead of taking
// ownership of the entire process heap, it uses malloc to allocate blocks for the GC to then
// assign to allocated objects.
//
// Currently, only one block can be allocated meaning this has a fixed-size heap.
package gc
Loading

0 comments on commit 23aba71

Please sign in to comment.