aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/cart.c59
-rw-r--r--src/cart.h5
-rw-r--r--src/env.c9
-rw-r--r--src/fs.c10
-rw-r--r--src/input.c2
-rw-r--r--src/menu.c25
-rw-r--r--src/menu.h1
-rw-r--r--src/player.c3
8 files changed, 78 insertions, 36 deletions
diff --git a/src/cart.c b/src/cart.c
index f639aec..8899cd4 100644
--- a/src/cart.c
+++ b/src/cart.c
@@ -10,7 +10,7 @@ static bool cart_file_path(struct pw_cart_file *file, char const *dir,
char const *filename, char const *ext) {
if (!pw_pathjoin(file->path, PW_CART_PATH_MAX, dir, filename, NULL) ||
!pw_stripext(file->name, PW_CART_NAME_MAX, filename, ext)) {
- SDL_SetError("invalid cart file name %s", filename);
+ SDL_SetError("invalid file name %s", filename);
return false;
}
@@ -48,22 +48,24 @@ struct pw_carts *pw_carts_list(char const *dir) {
goto error;
}
- size_t count = (size_t)icount;
- if (count > SIZE_MAX / sizeof(struct pw_cart_file)) {
+ size_t max_count = (size_t)icount;
+ if (max_count > SIZE_MAX / sizeof(struct pw_cart_file)) {
goto error;
}
- carts = SDL_malloc(sizeof(*carts) + count * sizeof(*carts->files));
+ carts = SDL_malloc(sizeof(*carts) + max_count * sizeof(*carts->files));
if (!carts) {
goto error;
}
- carts->count = 0;
- for (size_t i = 0; i < count; i++) {
- struct pw_cart_file *file = &carts->files[i];
+ size_t count = 0;
+ for (size_t i = 0; i < max_count; i++) {
+ struct pw_cart_file *file = &carts->files[count];
if (!cart_file_path(file, dir, cartfiles[i], ".png")) {
- goto error;
+ SDL_LogWarn(0, "unsupported cart file: %s", SDL_GetError());
+ continue;
}
+ count++;
}
carts->count = count;
@@ -140,20 +142,10 @@ struct pw_cart_png *pw_cart_png_init(SDL_Renderer *renderer) {
goto error;
}
- union pw_png_pixel *image = pw_png_load(
- pw_sysroot_unknown_p8_png.data, pw_sysroot_unknown_p8_png.size,
- PW_CART_PNG_WIDTH, PW_CART_PNG_HEIGHT);
- if (!image) {
+ if (!pw_cart_png_reset(png)) {
goto error;
}
- bool ok = blit_image(image, png->texture);
- SDL_free(image);
- if (!ok) {
- goto error;
- }
-
- SDL_zeroa(png->rom);
return png;
error:
@@ -247,17 +239,38 @@ cleanup:
bool pw_cart_png_load(struct pw_cart_file *file, struct pw_cart_png *png) {
if (!file) {
- SDL_InvalidParamError("file");
- return false;
+ return SDL_InvalidParamError("file");
}
if (!png) {
- SDL_InvalidParamError("png");
- return false;
+ return SDL_InvalidParamError("png");
}
return load_cart_png(file->path, png->texture, png->rom);
}
+bool pw_cart_png_reset(struct pw_cart_png *png) {
+ if (!png) {
+ return SDL_InvalidParamError("png");
+ }
+
+ union pw_png_pixel *image = pw_png_load(
+ pw_sysroot_unknown_p8_png.data, pw_sysroot_unknown_p8_png.size,
+ PW_CART_PNG_WIDTH, PW_CART_PNG_HEIGHT);
+ if (!image) {
+ SDL_LogCritical(0, "unexpected error parsing builtin unknown cart PNG");
+ return false;
+ }
+
+ bool ok = blit_image(image, png->texture);
+ SDL_free(image);
+ if (!ok) {
+ return false;
+ }
+
+ SDL_zeroa(png->rom);
+ return true;
+}
+
static struct pw_fs_node_buffer *embedded_cart(struct pw_fs_entry *e,
char const **filename) {
static char const cart_dir[] = "user_data/carts/";
diff --git a/src/cart.h b/src/cart.h
index b43c6a0..5873d2e 100644
--- a/src/cart.h
+++ b/src/cart.h
@@ -93,6 +93,11 @@ void pw_cart_png_free(struct pw_cart_png *png);
bool pw_cart_png_load(struct pw_cart_file *file, struct pw_cart_png *png);
/**
+ * Resets a cart to its initial empty state.
+ */
+bool pw_cart_png_reset(struct pw_cart_png *png);
+
+/**
* Loads a cart from a file, including mounting its user data file-system.
*
* Optionally specify a loaded cart PNG to copy the ROM from instead of
diff --git a/src/env.c b/src/env.c
index 9a41eb6..5f03bb2 100644
--- a/src/env.c
+++ b/src/env.c
@@ -729,11 +729,17 @@ uint32_t w2c_env_0x5F_0x5Fsyscall146(struct w2c_env *env, uint32_t which,
for (uint32_t i = 0; i < iovcnt; i++) {
uint32_t buffer = wasm_rt_memory_load32(&env->memory, iov + 8 * i);
uint32_t len = wasm_rt_memory_load32(&env->memory, iov + 8 * i + 4);
+
+ // Make sure to check the source range before writing, this ensures that
+ // we don't modify the storage file-system when we would trap anyway,
+ // leaving it in a bad state.
+ wasm_rt_memory_check(&env->memory, buffer, len);
+
uint8_t *contents = pw_fs_write(env->context.cart->storage, fd, len);
if (!contents) {
return -pw_fs_errno(env->context.cart->storage);
}
- wasm_rt_memory_load(&env->memory, buffer, contents, len);
+ wasm_rt_memory_load_unchecked(&env->memory, buffer, contents, len);
}
if (!pw_fs_flush(env->context.cart->storage, fd)) {
@@ -763,6 +769,7 @@ uint32_t w2c_env_0x5F_0x5Fsyscall195(struct w2c_env *env, uint32_t which,
// of the fields. If ever this causes issues with PICO-8 things (which I
// have not noticed for any cart), we can implement something.
+ wasm_rt_memory_check(&env->memory, stat, 88);
MEM_WRITE(32, env, stat, 0); // st_dev
MEM_WRITE(64, env, stat, 0); // st_ino
MEM_WRITE(32, env, stat, s.mode);
diff --git a/src/fs.c b/src/fs.c
index 3b450e8..77b34ee 100644
--- a/src/fs.c
+++ b/src/fs.c
@@ -829,10 +829,14 @@ uint8_t *pw_fs_write(struct pw_fs *fs, pw_fd fd, uint32_t len) {
w->capacity = capacity;
}
+ // We cannot know for sure that the caller will actually write the
+ // data to the buffer, which means that it is possible for us to sync
+ // uninitialized malloc data which would be undefined behaviour. To
+ // prevent this, always clear the new portions of the buffer. Note that
// POSIX requires holes to be filled with 0s (in case the file was
- // seeked past the end).
- if (s->pos > w->size) {
- SDL_memset(w->data + w->size, 0, s->pos - w->size);
+ // seeked past the end), so we satisfy that as well.
+ if (end > w->size) {
+ SDL_memset(w->data + w->size, 0, end - w->size);
}
uint8_t *buffer = w->data + s->pos;
diff --git a/src/input.c b/src/input.c
index 8e2ee33..0cc2f78 100644
--- a/src/input.c
+++ b/src/input.c
@@ -458,7 +458,7 @@ enum pw_window_flags pw_input_window(struct pw_input *input) {
SDL_WindowFlags flags = SDL_GetWindowFlags(input->window);
if (!(flags &
- (SDL_WINDOW_MINIMIZED | SDL_WINDOW_HIDDEN | SDL_WINDOW_MINIMIZED))) {
+ (SDL_WINDOW_OCCLUDED | SDL_WINDOW_HIDDEN | SDL_WINDOW_MINIMIZED))) {
result |= PW_WINDOW_VISIBLE;
}
if (flags & SDL_WINDOW_INPUT_FOCUS) {
diff --git a/src/menu.c b/src/menu.c
index e89020e..ee83736 100644
--- a/src/menu.c
+++ b/src/menu.c
@@ -3,6 +3,18 @@
#include "input.h"
#include "menu.h"
+static bool load_cart(struct pw_menu *menu, size_t i) {
+ struct pw_cart_file *file = &menu->carts->files[i];
+ menu->selection.valid = pw_cart_png_load(file, menu->png);
+ if (!menu->selection.valid) {
+ SDL_LogWarn(0, "malformed cart '%s': %s", file->name, SDL_GetError());
+ if (!pw_cart_png_reset(menu->png)) {
+ return false;
+ }
+ }
+ return true;
+}
+
struct pw_menu *pw_menu_init(SDL_Renderer *renderer, char const *carts,
enum pw_scale_mode mode) {
if (!renderer) {
@@ -14,11 +26,10 @@ struct pw_menu *pw_menu_init(SDL_Renderer *renderer, char const *carts,
return NULL;
}
- struct pw_menu *menu = SDL_malloc(sizeof(*menu));
+ struct pw_menu *menu = SDL_calloc(1, sizeof(*menu));
if (!menu) {
return NULL;
}
- SDL_zerop(menu);
menu->carts = pw_carts_list(carts);
if (!menu->carts) {
@@ -32,7 +43,8 @@ struct pw_menu *pw_menu_init(SDL_Renderer *renderer, char const *carts,
if (!menu->carts->count) {
SDL_LogWarn(0, "empty or missing carts directory");
- } else if (!pw_cart_png_load(&menu->carts->files[0], menu->png)) {
+ menu->selection.valid = false;
+ } else if (!load_cart(menu, 0)) {
goto error;
}
@@ -187,23 +199,22 @@ bool pw_menu_iterate(struct pw_menu *menu, struct pw_cart **cart) {
}
if (menu->selection.current != menu->selection.next) {
- if (!pw_cart_png_load(&menu->carts->files[menu->selection.next],
- menu->png)) {
+ if (!load_cart(menu, menu->selection.next)) {
return false;
}
menu->selection.current = menu->selection.next;
}
- if (menu->selection.selected) {
+ if (menu->selection.valid && menu->selection.selected) {
*cart =
pw_cart_load(&menu->carts->files[menu->selection.current], menu->png);
if (!*cart) {
return false;
}
- menu->selection.selected = false;
} else {
*cart = NULL;
}
+ menu->selection.selected = false;
return true;
}
diff --git a/src/menu.h b/src/menu.h
index 9f839d8..a247a5a 100644
--- a/src/menu.h
+++ b/src/menu.h
@@ -17,6 +17,7 @@ struct pw_menu {
struct {
size_t current;
size_t next;
+ bool valid;
bool selected;
} selection;
};
diff --git a/src/player.c b/src/player.c
index a4d7a75..06545af 100644
--- a/src/player.c
+++ b/src/player.c
@@ -43,7 +43,8 @@ static bool add_gamepad(struct pw_players *players,
players->gamepads[i] = SDL_OpenGamepad(event->which);
if (!players->gamepads[i]) {
- return false;
+ SDL_LogWarn(0, "failed to open gamepad: %s", SDL_GetError());
+ return true;
}
SDL_LogDebug(0, "connected gamepad for player %zu", i + 1);