Skip to content

Commit 65b0691

Browse files
authored
Merge pull request #71 from alanjian85/master
Fix memory access violations in queue operation functions
2 parents 3bdabc8 + f1b9775 commit 65b0691

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

src/syscall_sdl.c

+18-10
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ typedef struct {
4646
} event_t;
4747

4848
typedef struct {
49-
event_t *base;
49+
uint32_t base;
5050
size_t end;
5151
} event_queue_t;
5252

@@ -64,7 +64,7 @@ typedef struct {
6464
} submission_t;
6565

6666
typedef struct {
67-
submission_t *base;
67+
uint32_t base;
6868
size_t start;
6969
} submission_queue_t;
7070

@@ -76,27 +76,35 @@ static SDL_Texture *texture;
7676
static uint32_t queues_capacity;
7777
static uint32_t event_count;
7878
static event_queue_t event_queue = {
79-
.base = NULL,
79+
.base = 0,
8080
.end = 0,
8181
};
8282
static submission_queue_t submission_queue = {
83-
.base = NULL,
83+
.base = 0,
8484
.start = 0,
8585
};
8686

87-
static submission_t submission_pop(void)
87+
static submission_t submission_pop(struct riscv_t *rv)
8888
{
89-
submission_t submission = submission_queue.base[submission_queue.start++];
89+
state_t *s = rv_userdata(rv);
90+
submission_t submission;
91+
memory_read(
92+
s->mem, (void *) &submission,
93+
submission_queue.base + submission_queue.start * sizeof(submission_t),
94+
sizeof(submission_t));
95+
++submission_queue.start;
9096
submission_queue.start &= queues_capacity - 1;
9197
return submission;
9298
}
9399

94100
static void event_push(struct riscv_t *rv, event_t event)
95101
{
96-
event_queue.base[event_queue.end++] = event;
102+
state_t *s = rv_userdata(rv);
103+
memory_write(s->mem, event_queue.base + event_queue.end * sizeof(event_t),
104+
(void *) &event, sizeof(event_t));
105+
++event_queue.end;
97106
event_queue.end &= queues_capacity - 1;
98107

99-
state_t *s = rv_userdata(rv);
100108
uint32_t count;
101109
memory_read(s->mem, (void *) &count, event_count, sizeof(uint32_t));
102110
count += 1;
@@ -235,7 +243,7 @@ void syscall_draw_frame(struct riscv_t *rv)
235243
void syscall_setup_queue(struct riscv_t *rv)
236244
{
237245
/* setup_queue(base, capacity, event_count) */
238-
void *base = (void *) (uintptr_t) rv_get_reg(rv, rv_reg_a0);
246+
uint32_t base = rv_get_reg(rv, rv_reg_a0);
239247
queues_capacity = rv_get_reg(rv, rv_reg_a1);
240248
event_count = rv_get_reg(rv, rv_reg_a2);
241249

@@ -250,7 +258,7 @@ void syscall_submit_queue(struct riscv_t *rv)
250258
uint32_t count = rv_get_reg(rv, rv_reg_a0);
251259

252260
while (count--) {
253-
submission_t submission = submission_pop();
261+
submission_t submission = submission_pop(rv);
254262

255263
switch (submission.type) {
256264
case RELATIVE_MODE_SUBMISSION:

0 commit comments

Comments
 (0)