summaryrefslogtreecommitdiff
path: root/components/mm/utest/lwp_mmap_map_fixed.c
blob: affc514037e7268c8bc15b1089a8e31364d9853b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
 * Copyright (c) 2006-2023, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2023-08-17     Shell        test case for aspace_map(MAP_FIXED)
 */
#include "common.h"
#include "lwp_user_mm.h"
#include "utest_assert.h"
#include <mm_aspace.h>

#include <rtthread.h>

static struct rt_lwp *lwp;
static size_t former_vsz;
static size_t former_vcount;

static void *vaddr = (void *)0x100000000;
static size_t ex_size = 0x5000;
static char *override_start;
static size_t override_size = 0x2000;
static long fd = -1;
static long pgoffset = 0;
static size_t ex_prot = PROT_NONE;
static size_t ex_flags = MAP_PRIVATE | MAP_ANONYMOUS;
static size_t override_prot = PROT_READ | PROT_WRITE;
static size_t override_flags = MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED;

static int _count_vsz(rt_varea_t varea, void *arg)
{
    rt_base_t *pvsz = arg;
    *pvsz += 1;
    return 0;
}

static rt_base_t count_vcount(rt_aspace_t aspace)
{
    rt_base_t vcount = 0;
    rt_aspace_traversal(aspace, _count_vsz, &vcount);
    return vcount;
}

static char put_data[] = "hello,world";

static void test_map_fixed(void)
{
    void *effect_override;

    former_vsz = rt_aspace_count_vsz(lwp->aspace);
    former_vcount = count_vcount(lwp->aspace);

    /* create an existed mapping */
    vaddr = lwp_mmap2(lwp, vaddr, ex_size, ex_prot, ex_flags, fd, pgoffset);
    uassert_true((long)vaddr > 0);
    utest_int_equal(former_vsz + ex_size, rt_aspace_count_vsz(lwp->aspace));
    utest_int_equal(former_vcount + 1, count_vcount(lwp->aspace));
    former_vsz += ex_size;
    former_vcount += 1;

    /* fix private in the middle */
    override_start = (char *)vaddr + 0x1000;
    effect_override = lwp_mmap2(lwp, override_start, override_size, override_prot, override_flags, fd, pgoffset);
    uassert_true(effect_override == override_start);
    utest_int_equal(former_vsz, rt_aspace_count_vsz(lwp->aspace));
    utest_int_equal(former_vcount + 2, count_vcount(lwp->aspace));
    utest_int_equal(
        lwp_data_put(lwp, effect_override, put_data, sizeof(put_data)),
        sizeof(put_data)
    );

    utest_int_equal(RT_EOK, rt_aspace_unmap_range(lwp->aspace, vaddr, ex_size));
}

static void aspace_unmap_tc(void)
{
    test_map_fixed();
}

static rt_size_t total, used, max_used;
static rt_size_t totala, useda, max_useda;
static rt_ubase_t level;
static rt_err_t utest_tc_init(void)
{
    lwp = lwp_create(0);
    if (lwp)
        lwp_user_space_init(lwp, 1);
    else
        return -RT_ENOMEM;

    /* stats */
    level = rt_heap_lock();
    rt_memory_info(&total, &used, &max_used);

    return RT_EOK;
}

static rt_err_t utest_tc_cleanup(void)
{
    lwp_ref_dec(lwp);

    /* check */
    rt_memory_info(&totala, &useda, &max_useda);
    rt_heap_unlock(level);
    utest_int_equal(total, totala);
    utest_int_less_equal(useda, used);

    return RT_EOK;
}

static void testcase(void)
{
    UTEST_UNIT_RUN(aspace_unmap_tc);
}
UTEST_TC_EXPORT(testcase, "testcases.lwp.mman.mmap_anon.fix_private", utest_tc_init, utest_tc_cleanup, 10);