Skip to content

Commit 408c151

Browse files
ycsindkalowsk
authored andcommitted
tests: arch: riscv: make sure that gp reg can't be corrupted
Add a test to make sure that the `gp` global pointer register used for relative addressing when `CONFIG_RISCV_GP` is enabled can't be corrupted by a rogue user thread. Signed-off-by: Yong Cong Sin <[email protected]> Signed-off-by: Yong Cong Sin <[email protected]>
1 parent e30db2d commit 408c151

File tree

4 files changed

+66
-0
lines changed

4 files changed

+66
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.20.0)
4+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
5+
project(riscv_gp)
6+
7+
FILE(GLOB app_sources src/*.c)
8+
target_sources(app PRIVATE ${app_sources})
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
CONFIG_ZTEST=y
2+
CONFIG_RISCV_GP=y
3+
CONFIG_TEST_USERSPACE=y
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright (c) 2024 Meta Platforms
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <stdio.h>
8+
9+
#include <zephyr/arch/riscv/reg.h>
10+
#include <zephyr/kernel.h>
11+
#include <zephyr/ztest.h>
12+
13+
#define ROGUE_USER_STACK_SZ 2048
14+
15+
static struct k_thread rogue_user_thread;
16+
static K_THREAD_STACK_DEFINE(rogue_user_stack, ROGUE_USER_STACK_SZ);
17+
18+
static void rogue_user_fn(void *p1, void *p2, void *p3)
19+
{
20+
zassert_true(k_is_user_context());
21+
22+
reg_write(gp, 0xbad);
23+
zassert_equal(reg_read(gp), 0xbad);
24+
}
25+
26+
ZTEST_USER(riscv_gp, test_gp_value)
27+
{
28+
uintptr_t gp_val = reg_read(gp);
29+
k_tid_t th;
30+
31+
zassert_not_equal(gp_val, 0);
32+
33+
th = k_thread_create(&rogue_user_thread, rogue_user_stack, ROGUE_USER_STACK_SZ,
34+
rogue_user_fn, NULL, NULL, NULL, -1, K_USER, K_NO_WAIT);
35+
zassert_ok(k_thread_join(th, K_FOREVER));
36+
37+
zassert_equal(reg_read(gp), gp_val, "`gp` corrupted by user thread");
38+
}
39+
40+
static void *userspace_setup(void)
41+
{
42+
k_thread_access_grant(k_current_get(), &rogue_user_thread, &rogue_user_stack);
43+
44+
return NULL;
45+
}
46+
47+
ZTEST_SUITE(riscv_gp, NULL, userspace_setup, NULL, NULL, NULL);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
common:
2+
ignore_faults: true
3+
ignore_qemu_crash: true
4+
tags: kernel riscv
5+
platform_allow:
6+
- qemu_riscv64
7+
tests:
8+
arch.riscv64.riscv_gp: {}

0 commit comments

Comments
 (0)