Skip to content

Commit d7d439e

Browse files
authored
rvvm: Randomize & align core structures
- Randomize structures: rvvm_tlb_entry_t, rvvm_jit_tlb_entry_t, rvvm_ram_t, rvvm_aia_regfile_t, rvvm_machine_t - Partially randomize rvvm_hart_t - all fields that are not used by JIT are randomized, plus TLB entry layout is already dealt with - Mark align_cacheline on rvvm_hart_t - Mark align_type(32) on rvvm_tlb_entry_t - Mark align_type(16) on rvvm_jit_tlb_entry_t - No performance penalty or breakage is observed with either GCC randstruct plugin, or Clang with -frandomize-layout-seed="seed" - There was one JIT bug regarding offsetof(rvvm_tlb_entry_t, ptr), but the fix is already upstreamed
1 parent 134d84e commit d7d439e

File tree

1 file changed

+18
-16
lines changed

1 file changed

+18
-16
lines changed

src/rvvm.h

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ file, You can obtain one at https://mozilla.org/MPL/2.0/.
3333
#define RVVM_TLB_MASK (RVVM_TLB_SIZE - 1)
3434

3535
BUILD_ASSERT(RVVM_TLB_SIZE);
36-
BUILD_ASSERT(!((RVVM_TLB_SIZE - 1) & RVVM_TLB_SIZE));
36+
BUILD_ASSERT(!(RVVM_TLB_SIZE & RVVM_TLB_MASK));
3737

3838
#define RISCV_REG_ZERO 0
3939
#define RISCV_REG_X0 0
@@ -116,16 +116,14 @@ typedef int32_t rvvm_sxlen_t;
116116
#endif
117117

118118
/*
119-
* Address translation cache
120-
* In future, it would be nice to verify if cache-line alignment
121-
* gives any profit (entries scattered between cachelines waste L1)
119+
* Address translation cache (TLB)
122120
*/
123121

124-
typedef struct {
122+
typedef randomized_struct align_type(32) {
125123
// Pointer to page (With vaddr subtracted, for faster TLB translation)
126124
size_t ptr;
127-
#if !defined(HOST_64BIT)
128-
// Make entry size a power of 2 (32 bytes)
125+
#if defined(HOST_32BIT)
126+
// Make structure size a power of 2 (32 bytes) regardless of align_type() support
129127
size_t align;
130128
#endif
131129
// Virtual page number per each op type (vaddr >> 12)
@@ -136,16 +134,19 @@ typedef struct {
136134

137135
#ifdef USE_JIT
138136

139-
typedef struct {
137+
typedef randomized_struct align_type(16) {
140138
// Pointer to code block
141139
rvjit_func_t block;
142-
#if !defined(HOST_64BIT)
140+
#if defined(HOST_32BIT)
141+
// Make structure size a power of 2 (16 bytes) regardless of align_type() support
143142
size_t align;
144143
#endif
145144
// Virtual PC of this entry
146145
rvvm_addr_t pc;
147146
} rvvm_jit_tlb_entry_t;
148147

148+
// Those structure sizes are mandatory if we are going to use JIT
149+
// For pure interpreter this doesn't matter, so obscure architectures are fine
149150
BUILD_ASSERT(sizeof(rvvm_tlb_entry_t) == 32);
150151
BUILD_ASSERT(sizeof(rvvm_jit_tlb_entry_t) == 16);
151152

@@ -155,7 +156,7 @@ BUILD_ASSERT(sizeof(rvvm_jit_tlb_entry_t) == 16);
155156
* Physical RAM region
156157
*/
157158

158-
typedef struct {
159+
typedef randomized_struct {
159160
rvvm_addr_t addr; // Physical memory base address (Should be page-aligned)
160161
size_t size; // Physical memory amount (Should be page-aligned)
161162
void* data; // Pointer to memory data (Preferably page-aligned)
@@ -169,7 +170,7 @@ typedef struct {
169170
#define RVVM_AIA_IRQ_LIMIT 256
170171
#define RVVM_AIA_ARR_LEN (RVVM_AIA_IRQ_LIMIT >> 5)
171172

172-
typedef struct {
173+
typedef randomized_struct {
173174
uint32_t eidelivery;
174175
uint32_t eithreshold;
175176
uint32_t eip[RVVM_AIA_ARR_LEN];
@@ -180,7 +181,7 @@ typedef struct {
180181
* Hart structure
181182
*/
182183

183-
struct rvvm_hart_t {
184+
struct align_cacheline rvvm_hart_t {
184185
uint32_t running;
185186

186187
rvvm_uxlen_t registers[RISCV_REGS_MAX];
@@ -195,6 +196,8 @@ struct rvvm_hart_t {
195196
#endif
196197

197198
// Everything below here isn't accessed by JIT
199+
randomized_fields_start
200+
198201
rvvm_ram_t mem;
199202
rvvm_machine_t* machine;
200203
rvvm_addr_t root_page_table;
@@ -211,7 +214,7 @@ struct rvvm_hart_t {
211214
rvvm_uxlen_t lrsc_addr;
212215
rvvm_uxlen_t lrsc_cas;
213216

214-
struct {
217+
randomized_struct {
215218
uint32_t fcsr;
216219
uint32_t hartid;
217220

@@ -257,11 +260,10 @@ struct rvvm_hart_t {
257260
uint32_t pending_events;
258261
uint32_t preempt_ms;
259262

260-
// Cacheline alignment
261-
uint8_t align[64];
263+
randomized_fields_end
262264
};
263265

264-
struct rvvm_machine_t {
266+
randomized_struct rvvm_machine_t {
265267
rvvm_ram_t mem;
266268
vector_t(rvvm_hart_t*) harts;
267269
vector_t(rvvm_mmio_dev_t*) mmio_devs;

0 commit comments

Comments
 (0)