diff --git a/runtime/internal/runtime/tinygogc/gc.go b/runtime/internal/runtime/tinygogc/gc.go index 5bb673b7..965bc3bf 100644 --- a/runtime/internal/runtime/tinygogc/gc.go +++ b/runtime/internal/runtime/tinygogc/gc.go @@ -4,6 +4,9 @@ package tinygogc import "unsafe" +// LLGoPackage instructs the LLGo linker to wrap C standard library memory allocation +// functions (malloc, realloc, calloc) so they use the tinygogc allocator instead. +// This ensures all memory allocations go through the GC, including C library calls. const LLGoPackage = "link: --wrap=malloc --wrap=realloc --wrap=calloc" //export __wrap_malloc @@ -12,8 +15,13 @@ func __wrap_malloc(size uintptr) unsafe.Pointer { } //export __wrap_calloc -func __wrap_calloc(size uintptr) unsafe.Pointer { - return Alloc(size) +func __wrap_calloc(nmemb, size uintptr) unsafe.Pointer { + totalSize := nmemb * size + // Check for multiplication overflow + if nmemb != 0 && totalSize/nmemb != size { + return nil // Overflow + } + return Alloc(totalSize) } //export __wrap_realloc diff --git a/runtime/internal/runtime/tinygogc/gc_tinygo.go b/runtime/internal/runtime/tinygogc/gc_tinygo.go index e57a7e19..d36f1806 100644 --- a/runtime/internal/runtime/tinygogc/gc_tinygo.go +++ b/runtime/internal/runtime/tinygogc/gc_tinygo.go @@ -16,6 +16,20 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + +// Package tinygogc implements a conservative mark-and-sweep garbage collector +// for baremetal environments where the standard Go runtime and bdwgc are unavailable. +// +// This implementation is based on TinyGo's GC and is designed for resource-constrained +// embedded systems. It uses a block-based allocator with conservative pointer scanning. +// +// Build tags: +// - baremetal: Enables this GC for baremetal targets +// - testGC: Enables testing mode with mock implementations +// +// Memory Layout: +// The heap is divided into fixed-size blocks (32 bytes on 64-bit). Metadata is stored +// at the end of the heap, using 2 bits per block to track state (free/head/tail/mark). package tinygogc import ( diff --git a/runtime/internal/runtime/tinygogc/mutex.go b/runtime/internal/runtime/tinygogc/mutex.go index 76d6fbbb..ba13813a 100644 --- a/runtime/internal/runtime/tinygogc/mutex.go +++ b/runtime/internal/runtime/tinygogc/mutex.go @@ -1,5 +1,6 @@ package tinygogc +// TODO(MeteorsLiu): mutex lock for baremetal GC type mutex struct{} func lock(m *mutex) {}