vtest	"test sigsegv handler"

# Under ASAN, the stack layout is different and STACK OVERFLOW is
# never printed.
feature !asan

server s1 {
	rxreq
	txresp
} -start

vinyl v1 \
	-arg "-p feature=+no_coredump" \
	-arg "-p vcc_feature=+allow_inline_c" \
	-arg "-p thread_pool_stack=128k" \
	-vcl+backend {
	C{
#include <signal.h>
#include <unistd.h>
#include <stdio.h>

static void _accessor(volatile char *p) {
     p[0] = 'V'; p[1] = '\0';
     fprintf(stderr, "%p %s\n", p, p);
}
static void (*accessor)(volatile char *p) = _accessor;

}C
	sub vcl_recv { C{
	    #define STKKB 128
	    int i;
	    volatile char overflow[STKKB * 1024];

	    /* for downwards stack, take care to hit a single guard page */
	    for (i = (STKKB - 1) * 1024; i >= 0; i -= 1024)
		accessor(overflow + i);
	    /* NOTREACHED */
	    sleep(2);
	}C }
} -start

client c1 {
	txreq
	expect_close
} -run

delay 3

vinyl v1 -cliexpect "STACK OVERFLOW" "panic.show"
vinyl v1 -clijson "panic.show -j"

vinyl v1 -cliok "panic.clear"

# Also check without the handler
vinyl v1 -cliok "param.set sigsegv_handler off"
vinyl v1 -vcl+backend {} -start

client c1 {
	txreq
	rxresp
} -run

vinyl v1 -expectexit 0x40

####################

vinyl v2 \
	-arg "-p feature=+no_coredump" \
	-arg "-p vcc_feature=+allow_inline_c" \
	-vcl+backend {

	C{
	#include <stdio.h>
	}C

	sub vcl_recv { C{
	    int *i = (void *)VRT_GetHdr;
	    *i = 42;
	}C }
} -start

client c2 -connect ${v2_sock} {
	txreq
	expect_close
} -run

delay 3

vinyl v2 -cliexpect "[bB]us error|Segmentation [fF]ault" "panic.show"
vinyl v2 -clijson "panic.show -j"

vinyl v2 -cliok "panic.clear"

vinyl v2 -expectexit 0x40
