1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) 2021 FUJITSU LIMITED. All rights reserved.
4f08c3bdfSopenharmony_ci * Author: Yang Xu <xuyang2018.jy@fujitsu.com>
5f08c3bdfSopenharmony_ci */
6f08c3bdfSopenharmony_ci
7f08c3bdfSopenharmony_ci/*\
8f08c3bdfSopenharmony_ci * [Description]
9f08c3bdfSopenharmony_ci *
10f08c3bdfSopenharmony_ci * Basic mallinfo2() test.
11f08c3bdfSopenharmony_ci *
12f08c3bdfSopenharmony_ci * Test hblkhd member of struct mallinfo2 whether overflow when setting 2G size.
13f08c3bdfSopenharmony_ci *
14f08c3bdfSopenharmony_ci * Deprecated mallinfo() overflow in this case, that was the point for creating
15f08c3bdfSopenharmony_ci * mallinfo2().
16f08c3bdfSopenharmony_ci */
17f08c3bdfSopenharmony_ci
18f08c3bdfSopenharmony_ci#include "mallinfo_common.h"
19f08c3bdfSopenharmony_ci#include "tst_safe_macros.h"
20f08c3bdfSopenharmony_ci
21f08c3bdfSopenharmony_ci#ifdef HAVE_MALLINFO2
22f08c3bdfSopenharmony_ci
23f08c3bdfSopenharmony_civoid test_mallinfo2(void)
24f08c3bdfSopenharmony_ci{
25f08c3bdfSopenharmony_ci	struct mallinfo2 info;
26f08c3bdfSopenharmony_ci	char *buf;
27f08c3bdfSopenharmony_ci	size_t size = 2UL * 1024UL * 1024UL * 1024UL;
28f08c3bdfSopenharmony_ci
29f08c3bdfSopenharmony_ci	buf = malloc(size);
30f08c3bdfSopenharmony_ci
31f08c3bdfSopenharmony_ci	if (!buf)
32f08c3bdfSopenharmony_ci		tst_brk(TCONF, "Current system can not malloc 2G space, skip it");
33f08c3bdfSopenharmony_ci
34f08c3bdfSopenharmony_ci	info = mallinfo2();
35f08c3bdfSopenharmony_ci	if (info.hblkhd < size) {
36f08c3bdfSopenharmony_ci		print_mallinfo2("Test malloc 2G", &info);
37f08c3bdfSopenharmony_ci		tst_res(TFAIL, "hblkhd member of struct mallinfo2 overflow?");
38f08c3bdfSopenharmony_ci	} else {
39f08c3bdfSopenharmony_ci		tst_res(TPASS, "hblkhd member of struct mallinfo2 doesn't overflow");
40f08c3bdfSopenharmony_ci	}
41f08c3bdfSopenharmony_ci
42f08c3bdfSopenharmony_ci	free(buf);
43f08c3bdfSopenharmony_ci}
44f08c3bdfSopenharmony_ci
45f08c3bdfSopenharmony_cistatic struct tst_test test = {
46f08c3bdfSopenharmony_ci	.test_all = test_mallinfo2,
47f08c3bdfSopenharmony_ci};
48f08c3bdfSopenharmony_ci
49f08c3bdfSopenharmony_ci#else
50f08c3bdfSopenharmony_ciTST_TEST_TCONF("system doesn't implement non-POSIX mallinfo2()");
51f08c3bdfSopenharmony_ci#endif
52