summaryrefslogtreecommitdiff
path: root/na/test/test_uaccess.cc
blob: c5a8c6ca62642456ca45f2ebdfe7e45df7e34cc0 (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
/* SPDX-License-Identifier: GPL-2.0-or-later */
/* Copyright (C) 2026 Richard Qin */

#include <lume/selftest.h>
#include <lume/uaccess.h>
#include <lume/errno.h>

#include <lume/vmm.h>
#include <lume/pmm.h>
#include <lume/tlb.h>
#include <lume/mm_layout.h>
#include <arch/pmap.h>

void selftest_uaccess()
{
    /* To test valid user addresses (< kKernelVaBase), we need a writable page mapped there.
     * We cannot use VmSpace::create() because it does not yet inherit kernel mappings, 
     * so switching to it hangs. We also cannot map it with the U bit because S-mode 
     * would need the SUM bit enabled.
    * So we manually allocate a physical page and map it into the CURRENT page table 
     * at 0x100000 without the U bit, just for this test.
     */
    uint64 mock_user_addr = 0x100000;
    
    uint64 satp;
    asm volatile("csrr %0, satp" : "=r"(satp));
    uint64 root_pa = (satp & 0xFFFFFFFFFFFULL) << 12;
    
    Page* page = pmm_alloc_page();
    ST_ASSERT(page != nullptr);
    uint64 paddr = page_to_pa(page).raw;
    
    pmap::map(root_pa, mock_user_addr, paddr, pmap::PTE_R | pmap::PTE_W | pmap::PTE_V | pmap::PTE_A | pmap::PTE_D);
    
    /* Flush TLB to see the new mapping */
    tlb_shootdown_all();

    st_begin("uaccess: copy_to_user valid range");
    char kernel_buf[32] = "Hello User Space!";
    int ret = copy_to_user(mock_user_addr, kernel_buf, 18);
    ST_ASSERT_EQ(ret, 0);
    st_pass();

    st_begin("uaccess: copy_from_user valid range");
    char read_buf[32] = {0};
    /* We wrote "Hello User Space!" to mock_user_addr previously */
    ret = copy_from_user(read_buf, mock_user_addr, 18);
    ST_ASSERT_EQ(ret, 0);
    ST_ASSERT(read_buf[0] == 'H');
    ST_ASSERT(read_buf[1] == 'e');
    ST_ASSERT(read_buf[6] == 'U');
    st_pass();

    st_begin("uaccess: strncpy_from_user valid range");
    char str_buf[32] = {0};
    int64 len = strncpy_from_user(str_buf, mock_user_addr, 32);
    ST_ASSERT_EQ(len, 17); /* "Hello User Space!" is 17 chars long */
    ST_ASSERT(str_buf[0] == 'H');
    st_pass();

    st_begin("uaccess: strncpy_from_user max_len truncation");
    char trunc_buf[32] = {0};
    len = strncpy_from_user(trunc_buf, mock_user_addr, 6);
    ST_ASSERT_EQ(len, 5); /* Will copy 5 chars and NUL terminate */
    ST_ASSERT(trunc_buf[4] == 'o');
    ST_ASSERT(trunc_buf[5] == '\0');
    st_pass();

    st_begin("uaccess: copy_to_user invalid range (crosses boundary)");
    ret = copy_to_user(kKernelVaBase - 10, kernel_buf, 20);
    ST_ASSERT_EQ(ret, -EFAULT);
    st_pass();

    st_begin("uaccess: copy_to_user invalid range (entirely in kernel)");
    ret = copy_to_user(kKernelVaBase + 0x1000, kernel_buf, 20);
    ST_ASSERT_EQ(ret, -EFAULT);
    st_pass();

    st_begin("uaccess: copy_to_user invalid range (overflow)");
    ret = copy_to_user(0xFFFFFFFFFFFFFFF0ULL, kernel_buf, 0x100);
    ST_ASSERT_EQ(ret, -EFAULT);
    st_pass();

    st_begin("uaccess: copy_from_user invalid ranges");
    ret = copy_from_user(read_buf, kKernelVaBase - 5, 10);
    ST_ASSERT_EQ(ret, -EFAULT);
    
    ret = copy_from_user(read_buf, kKernelVaBase, 10);
    ST_ASSERT_EQ(ret, -EFAULT);
    st_pass();

    st_begin("uaccess: strncpy_from_user invalid range");
    uint64 invalid_addr = kKernelVaBase + 0x1000;
    len = strncpy_from_user(str_buf, invalid_addr, 10);
    ST_ASSERT_EQ(len, -EFAULT);
    st_pass();
}