wasm-micro-runtime/core/iwasm/libraries/lib-socket/test/nslookup.c
Marcin Kolny 6ed4c9c908
Increase default native stack size (#2332)
Calling `__wasi_sock_addr_resolve` syscall causes native stack overflow.
Given this is a standard function available in WAMR, we should have at least
the default stack size large enough to handle this case.

The socket tests were updated so they also run in separate thread, but
the simple retro program is:

```C
void *th(void *p)
{
    struct addrinfo *res;
    getaddrinfo("amazon.com", NULL, NULL, &res);
    return NULL;
}

int main(int argc, char **argv)
{
    pthread_t pt;
    pthread_create(&pt, NULL, th, NULL);
    pthread_join(pt, NULL);
    return 0;
}
```
2023-07-03 21:02:10 +08:00

68 lines
1.4 KiB
C

/*
* Copyright (C) 2023 Amazon.com Inc. or its affiliates. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include <assert.h>
#include <string.h>
#include <stdio.h>
#include <pthread.h>
#ifdef __wasi__
#include <wasi/api.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <wasi_socket_ext.h>
#else
#include <netdb.h>
#endif
void
test_nslookup(int af)
{
struct addrinfo *res;
int count = 0;
struct addrinfo hints;
char *url = "google-public-dns-a.google.com";
memset(&hints, 0, sizeof(hints));
hints.ai_family = af;
hints.ai_socktype = SOCK_STREAM;
int ret = getaddrinfo(url, 0, &hints, &res);
assert(ret == 0);
struct addrinfo *address = res;
while (address) {
assert(address->ai_family == af);
assert(address->ai_socktype == SOCK_STREAM);
count++;
address = address->ai_next;
}
assert(count > 0);
freeaddrinfo(res);
}
void *
test_nslookup_mt(void *params)
{
int *af = (int *)params;
test_nslookup(*af);
}
int
main()
{
int afs[] = { AF_INET, AF_INET6 };
for (int i = 0; i < sizeof(afs) / sizeof(afs[0]); i++) {
pthread_t th;
printf("Testing %d in main thread...\n", afs[i]);
test_nslookup(afs[i]);
printf("Testing %d in a new thread...\n", afs[i]);
pthread_create(&th, NULL, test_nslookup_mt, &afs[i]);
pthread_join(th, NULL);
}
return 0;
}