wasm-micro-runtime/samples/shared-heap/wasm-apps/test1.c
TianlongLiang ccdc8369d6
Shared heap enhancement for AOT and update tests and samples (#4226)
* shared heap enhancement: modify memory check for aot_check_memory_overflow to accomodate shared heap chain
* shared heap enhancement in AOT
* use alloca for func ctx shared heap cache value
* use correct alloca for func ctx shared heap cache value
* enable shared heap chain aot test and bug fix
* Fix a missing argument on 32bits platform, still has the shared heap chain iteration problem
* Fix shared heap chain iteration problem on 32bits platform
* fix AOT bulk memory bounds checks compliation issue
* fix AOT bulk memory bounds checks on 64 bits platform
* refactor aot memory check
* refactor AOT bulk memory bounds checks
* add more unit test for shared heap
* finished organizing unit test for shared heap and enable x86_32 for shared heap unit test
* cover a corner case for bulk memory overflow check
* try func call to replace shared heap chain traverse
* fix compilation error in JIT and potentially load nullptr
* add option for wamrc to enable single shared heap/multi shared heap, and update shared heap unit tests and sample
* cr suggestions: 1. check potiential underflow 2. refactor and use separate function for bulk memory and normal memroy 3. static assert 4. add more comments 5. remove unused code
2025-06-19 21:42:53 +08:00

72 lines
1.4 KiB
C

/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include <stdio.h>
#include <stdint.h>
#include <string.h>
extern void *
shared_heap_malloc(uint32_t size);
extern void
shared_heap_free(void *ptr);
void *
my_shared_heap_malloc(uint32_t size, uint32_t index)
{
char *buf1 = NULL, *buf2 = NULL, *buf;
buf1 = shared_heap_malloc(128);
if (!buf1)
return NULL;
buf1[0] = 'H';
buf1[1] = 'e';
buf1[2] = 'l';
buf1[3] = 'l';
buf1[4] = 'o';
buf1[5] = ',';
buf1[6] = ' ';
buf2 = shared_heap_malloc(128);
if (!buf2) {
shared_heap_free(buf1);
return NULL;
}
snprintf(buf2, 128, "this is buf %u allocated from shared heap", index);
buf = shared_heap_malloc(size);
if (!buf) {
shared_heap_free(buf1);
shared_heap_free(buf2);
return NULL;
}
memset(buf, 0, size);
memcpy(buf, buf1, strlen(buf1));
memcpy(buf + strlen(buf1), buf2, strlen(buf2));
shared_heap_free(buf1);
shared_heap_free(buf2);
return buf;
}
void
my_shared_heap_free(void *ptr)
{
shared_heap_free(ptr);
}
void *
produce_str(char *addr, uint32_t index)
{
char c;
snprintf(addr, 512, "Data: %u stores to pre-allocated shared heap", index);
/* Actually access it in wasm */
c = addr[0];
printf("In WASM: the first char is %c\n", c);
return addr;
}