/*
 * sh_oom_test.c - reproduce simplehash inconsistency after an allocation
 * failure inside SH_GROW (PostgreSQL 16 simplehash.h).
 *
 * The element array allocator fails on demand (longjmp, like an
 * elog(ERROR) "out of memory" would do in the backend).  After the
 * failure the table must still be usable: tb->size must match the array
 * really allocated, iteration must visit exactly tb->members entries and
 * new inserts must work.
 *
 * Build (frontend, against a configured PostgreSQL source tree):
 *   gcc -g -fsanitize=address -DFRONTEND -I$PGSRC/src/include sh_oom_test.c \
 *       -L$PGSRC/src/common -L$PGSRC/src/port -lpgcommon -lpgport -o sh_oom_test
 */
#include "postgres_fe.h"
#include <setjmp.h>

#include "common/hashfn.h"
#include "common/logging.h"

typedef struct TestEntry
{
	uint32		key;
	char		status;
} TestEntry;

static jmp_buf oom_jmp;
static bool fail_next_alloc = false;
static uint64 allocated_elems = 0;	/* size of the last array really allocated */

#define SH_PREFIX th
#define SH_ELEMENT_TYPE TestEntry
#define SH_KEY_TYPE uint32
#define SH_KEY key
#define SH_HASH_KEY(tb, key) murmurhash32(key)
#define SH_EQUAL(tb, a, b) ((a) == (b))
#define SH_SCOPE static inline
#define SH_RAW_ALLOCATOR pg_malloc0
#define SH_USE_NONDEFAULT_ALLOCATOR
#define SH_DECLARE
#define SH_DEFINE
#include "lib/simplehash.h"

static inline void *
th_allocate(th_hash *tb, Size size)
{
	if (fail_next_alloc)
	{
		fail_next_alloc = false;
		longjmp(oom_jmp, 1);	/* simulates ereport(ERROR, out of memory) */
	}
	allocated_elems = size / sizeof(TestEntry);
	return pg_malloc0(size);
}

static inline void
th_free(th_hash *tb, void *pointer)
{
	pg_free(pointer);
}

int
main(void)
{
	th_hash    *tb = th_create(16, NULL);
	th_iterator it;
	TestEntry  *e;
	volatile uint32 next_key = 1;
	uint64		size_before;
	uint32		count = 0;
	bool		found;
	int			rc = 0;

	setvbuf(stdout, NULL, _IONBF, 0);

	/* fill the table up to its grow threshold */
	while (tb->members < tb->grow_threshold)
		th_insert(tb, next_key++, &found);

	size_before = tb->size;
	printf("before: size=%llu members=%u allocated=%llu\n",
		   (unsigned long long) tb->size, tb->members,
		   (unsigned long long) allocated_elems);

	/* next insert must grow the table: make that allocation fail */
	fail_next_alloc = true;
	if (setjmp(oom_jmp) == 0)
	{
		th_insert(tb, next_key, &found);
		printf("unexpected: grow did not fail\n");
		return 2;
	}
	printf("after OOM in SH_GROW: size=%llu sizemask=0x%x members=%u allocated=%llu\n",
		   (unsigned long long) tb->size, tb->sizemask, tb->members,
		   (unsigned long long) allocated_elems);

	if (tb->size != allocated_elems || tb->size != size_before)
	{
		printf("FAIL: tb->size (%llu) does not match the allocated array (%llu)\n",
			   (unsigned long long) tb->size, (unsigned long long) allocated_elems);
		rc = 1;
	}

	/* what pgstat_release_matching_entry_refs() does at backend exit */
	th_start_iterate(tb, &it);
	while ((e = th_iterate(tb, &it)) != NULL)
		count++;
	printf("iteration visited %u entries (members=%u)\n", count, tb->members);
	if (count != tb->members)
		rc = 1;

	/* the table must remain usable: the next grow succeeds */
	th_insert(tb, next_key++, &found);
	printf("insert after OOM: size=%llu members=%u\n",
		   (unsigned long long) tb->size, tb->members);

	th_destroy(tb);
	printf(rc == 0 ? "PASS\n" : "FAIL\n");
	return rc;
}
