wasm-micro-runtime/samples/native-stack-overflow/wasm-apps/testapp.c
YAMAMOTO Takashi d6e8d224ce
Add native-stack-overflow sample (#3321)
This is a test code to examine native stack overflow detection logic.

The current output on my environment (macOS amd64):

```shell
====== Interpreter
 stack size   | fail?  | leak?  | exception
---------------------------------------------------------------------------
    0 - 14704 | failed | leaked | Exception: native stack overflow
14704 - 17904 | failed |     ok | Exception: native stack overflow
17904 - 24576 |     ok |     ok |

====== AOT
 stack size   | fail?  | leak?  | exception
---------------------------------------------------------------------------
    0 - 18176 | failed | leaked | Exception: native stack overflow
18176 - 24576 |     ok |     ok |

====== AOT WAMR_DISABLE_HW_BOUND_CHECK=1
 stack size   | fail?  | leak?  | exception
---------------------------------------------------------------------------
    0 -  1968 | failed |     ok | Exception: native stack overflow
 1968 - 24576 |     ok |     ok |
```

This is a preparation to work on relevant issues, including:

https://github.com/bytecodealliance/wasm-micro-runtime/issues/3325
https://github.com/bytecodealliance/wasm-micro-runtime/issues/3320
https://github.com/bytecodealliance/wasm-micro-runtime/issues/3314
https://github.com/bytecodealliance/wasm-micro-runtime/issues/3297
2024-04-19 15:49:09 +08:00

55 lines
1.2 KiB
C

/*
* Copyright (C) 2024 Midokura Japan KK. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include <stdio.h>
#include <stdint.h>
uint32_t
host_consume_stack_and_call_indirect(int (*)(int), uint32_t, uint32_t);
uint32_t host_consume_stack(uint32_t);
int
cb(int x)
{
return x * x;
}
int
consume_stack_cb(int x)
{
/*
* intentions:
*
* - consume native stack by making recursive calls
*
* - avoid tail-call optimization (either by the C compiler or
* aot-compiler)
*/
if (x == 0) {
return 0;
}
return consume_stack_cb(x - 1) + 1;
}
int
host_consume_stack_cb(int x)
{
return host_consume_stack(x);
}
__attribute__((export_name("test"))) uint32_t
test(uint32_t native_stack, uint32_t recurse_count)
{
uint32_t ret;
ret = host_consume_stack_and_call_indirect(cb, 321, native_stack);
ret = host_consume_stack_and_call_indirect(consume_stack_cb, recurse_count,
native_stack);
#if 0 /* notyet */
ret = host_consume_stack_and_call_indirect(host_consume_stack_cb, 1000000,
native_stack);
#endif
return 42;
}