aboutsummaryrefslogtreecommitdiff
path: root/libpsn00b/psxgpu
diff options
context:
space:
mode:
authorspicyjpeg <thatspicyjpeg@gmail.com>2023-04-04 15:09:38 +0200
committerspicyjpeg <thatspicyjpeg@gmail.com>2023-04-04 15:09:38 +0200
commit870f4dca9d7b5e86544216d0e36863d17aefef62 (patch)
treef6186004da976cea19ee5764b795a51f932f77fb /libpsn00b/psxgpu
parentf7d9c309661f3027d5bfd119b3daf814e26ef589 (diff)
downloadpsn00bsdk-870f4dca9d7b5e86544216d0e36863d17aefef62.tar.gz
Add argument validation to most libpsn00b functions
Diffstat (limited to 'libpsn00b/psxgpu')
-rw-r--r--libpsn00b/psxgpu/common.c16
-rw-r--r--libpsn00b/psxgpu/env.c15
-rw-r--r--libpsn00b/psxgpu/font.c14
-rw-r--r--libpsn00b/psxgpu/image.c16
4 files changed, 56 insertions, 5 deletions
diff --git a/libpsn00b/psxgpu/common.c b/libpsn00b/psxgpu/common.c
index e354261..46f87bc 100644
--- a/libpsn00b/psxgpu/common.c
+++ b/libpsn00b/psxgpu/common.c
@@ -187,6 +187,8 @@ int EnqueueDrawOp(
uint32_t arg2,
uint32_t arg3
) {
+ _sdk_validate_args(func, -1);
+
// If GPU DMA is currently busy, append the command to the queue instead of
// executing it immediately. Note that interrupts must be disabled *prior*
// to checking if DMA is busy; disabling them afterwards would create a
@@ -263,6 +265,8 @@ void *DrawSyncCallback(void (*func)(void)) {
/* OT and primitive drawing API */
void ClearOTagR(uint32_t *ot, size_t length) {
+ _sdk_validate_args_void(ot && length);
+
DMA_MADR(DMA_OTC) = (uint32_t) &ot[length - 1];
DMA_BCR(DMA_OTC) = length & 0xffff;
DMA_CHCR(DMA_OTC) = 0x11000002;
@@ -272,6 +276,8 @@ void ClearOTagR(uint32_t *ot, size_t length) {
}
void ClearOTag(uint32_t *ot, size_t length) {
+ _sdk_validate_args_void(ot && length);
+
// DMA6 only supports writing to RAM in reverse order (last to first), so
// the OT has to be cleared in software here. This function is thus much
// slower than ClearOTagR().
@@ -285,10 +291,14 @@ void ClearOTag(uint32_t *ot, size_t length) {
}
void AddPrim(uint32_t *ot, const void *pri) {
+ _sdk_validate_args_void(ot && pri);
+
addPrim(ot, pri);
}
-void DrawPrim(const uint32_t *pri) {
+void DrawPrim(const uint32_t *pri) {
+ _sdk_validate_args_void(pri);
+
size_t length = getlen(pri);
DrawSync(0);
@@ -308,10 +318,14 @@ void DrawPrim(const uint32_t *pri) {
}
int DrawOTag(const uint32_t *ot) {
+ _sdk_validate_args(ot, -1);
+
return EnqueueDrawOp((void *) &DrawOTag2, (uint32_t) ot, 0, 0);
}
void DrawOTag2(const uint32_t *ot) {
+ _sdk_validate_args_void(ot);
+
GPU_GP1 = 0x04000002; // Enable DMA request, route to GP0
while (DMA_CHCR(DMA_GPU) & (1 << 24))
diff --git a/libpsn00b/psxgpu/env.c b/libpsn00b/psxgpu/env.c
index b90a431..cfe09c0 100644
--- a/libpsn00b/psxgpu/env.c
+++ b/libpsn00b/psxgpu/env.c
@@ -4,6 +4,7 @@
*/
#include <stdint.h>
+#include <assert.h>
#include <psxgpu.h>
#include <hwregs_c.h>
@@ -36,6 +37,8 @@ static inline uint32_t _get_window_mask(int size) {
/* Drawing API */
DRAWENV *SetDefDrawEnv(DRAWENV *env, int x, int y, int w, int h) {
+ _sdk_validate_args(env && (w > 0) && (h > 0), 0);
+
env->clip.x = x;
env->clip.y = y;
env->clip.w = w;
@@ -60,6 +63,8 @@ DRAWENV *SetDefDrawEnv(DRAWENV *env, int x, int y, int w, int h) {
}
int DrawOTagEnv(const uint32_t *ot, DRAWENV *env) {
+ _sdk_validate_args(ot && env, -1);
+
// All commands are grouped into a single display list packet for
// performance reasons using tagless primitives (the GPU does not care
// about the grouping as the display list is parsed by the CPU).
@@ -102,6 +107,8 @@ int DrawOTagEnv(const uint32_t *ot, DRAWENV *env) {
}
void PutDrawEnv(DRAWENV *env) {
+ _sdk_validate_args_void(env);
+
DrawOTagEnv((const uint32_t *) 0x00ffffff, env);
}
@@ -109,6 +116,8 @@ void PutDrawEnv(DRAWENV *env) {
// useful if the DRAWENV structure is never modified (which is the case most of
// the time).
void PutDrawEnvFast(DRAWENV *env) {
+ _sdk_validate_args_void(env);
+
if (!(env->dr_env.tag))
DrawOTagEnv((const uint32_t *) 0x00ffffff, env);
else
@@ -118,6 +127,8 @@ void PutDrawEnvFast(DRAWENV *env) {
/* Display API */
DISPENV *SetDefDispEnv(DISPENV *env, int x, int y, int w, int h) {
+ _sdk_validate_args(env && (w > 0) && (h > 0), 0);
+
env->disp.x = x;
env->disp.y = y;
env->disp.w = w;
@@ -136,6 +147,8 @@ DISPENV *SetDefDispEnv(DISPENV *env, int x, int y, int w, int h) {
}
void PutDispEnv(const DISPENV *env) {
+ _sdk_validate_args_void(env);
+
uint32_t h_range, v_range, mode, fb_pos;
mode = _gpu_video_mode << 3;
@@ -204,6 +217,8 @@ void PutDispEnv(const DISPENV *env) {
/* Deprecated "raw" display API */
void PutDispEnvRaw(const DISPENV_RAW *env) {
+ _sdk_validate_args_void(env);
+
uint32_t h_range, v_range, fb_pos;
h_range = 608 + env->vid_xpos;
diff --git a/libpsn00b/psxgpu/font.c b/libpsn00b/psxgpu/font.c
index b1c3c7a..c9d60f1 100644
--- a/libpsn00b/psxgpu/font.c
+++ b/libpsn00b/psxgpu/font.c
@@ -1,4 +1,5 @@
#include <stdint.h>
+#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
@@ -23,6 +24,7 @@ uint16_t _font_clut;
extern uint8_t _gpu_debug_font[];
void FntLoad(int x, int y) {
+ _sdk_validate_args_void((x >= 0) && (y >= 0) && (x < 1024) && (y < 1024));
RECT pos;
TIM_IMAGE tim;
@@ -66,7 +68,8 @@ void FntLoad(int x, int y) {
}
int FntOpen(int x, int y, int w, int h, int isbg, int n) {
-
+ _sdk_validate_args((w > 0) && (h > 0) && (n > 0), -1);
+
int i;
// Initialize a text stream
@@ -98,7 +101,8 @@ int FntOpen(int x, int y, int w, int h, int isbg, int n) {
}
int FntPrint(int id, const char *fmt, ...) {
-
+ _sdk_validate_args((id < _nstreams) && fmt, -1);
+
int n;
va_list ap;
@@ -124,7 +128,8 @@ int FntPrint(int id, const char *fmt, ...) {
}
char *FntFlush(int id) {
-
+ _sdk_validate_args(id < _nstreams, 0);
+
char *opri;
SPRT_8 *sprt;
DR_TPAGE *tpage;
@@ -226,7 +231,8 @@ char *FntFlush(int id) {
}
char *FntSort(uint32_t *ot, char *pri, int x, int y, const char *text) {
-
+ _sdk_validate_args(ot && pri, 0);
+
DR_TPAGE *tpage;
SPRT_8 *sprt = (SPRT_8*)pri;
int i;
diff --git a/libpsn00b/psxgpu/image.c b/libpsn00b/psxgpu/image.c
index ba5c445..63bceb6 100644
--- a/libpsn00b/psxgpu/image.c
+++ b/libpsn00b/psxgpu/image.c
@@ -69,6 +69,8 @@ static void _dma_transfer(const RECT *rect, uint32_t *data, int write) {
/* VRAM transfer API */
int LoadImage(const RECT *rect, const uint32_t *data) {
+ _sdk_validate_args(rect && data, -1);
+
int index = _next_saved_rect;
_saved_rects[index] = *rect;
@@ -83,6 +85,8 @@ int LoadImage(const RECT *rect, const uint32_t *data) {
}
int StoreImage(const RECT *rect, uint32_t *data) {
+ _sdk_validate_args(rect && data, -1);
+
int index = _next_saved_rect;
_saved_rects[index] = *rect;
@@ -97,18 +101,26 @@ int StoreImage(const RECT *rect, uint32_t *data) {
}
int MoveImage(const RECT *rect, int x, int y) {
+ _sdk_validate_args(rect, -1);
+
return EnqueueDrawOp((void *) &MoveImage2, (uint32_t) rect, x, y);
}
void LoadImage2(const RECT *rect, const uint32_t *data) {
+ _sdk_validate_args_void(rect && data);
+
_dma_transfer(rect, (uint32_t *) data, 1);
}
void StoreImage2(const RECT *rect, uint32_t *data) {
+ _sdk_validate_args_void(rect && data);
+
_dma_transfer(rect, data, 0);
}
void MoveImage2(const RECT *rect, int x, int y) {
+ _sdk_validate_args_void(rect);
+
while (!(GPU_GP1 & (1 << 26)))
__asm__ volatile("");
@@ -131,6 +143,8 @@ void MoveImage2(const RECT *rect, int x, int y) {
// returning pointers to them, which become useless once the .TIM file is
// unloaded from main RAM.
int GsGetTimInfo(const uint32_t *tim, GsIMAGE *info) {
+ _sdk_validate_args(tim && info, 1);
+
if ((*(tim++) & 0xffff) != 0x0010)
return 1;
@@ -157,6 +171,8 @@ int GsGetTimInfo(const uint32_t *tim, GsIMAGE *info) {
}
int GetTimInfo(const uint32_t *tim, TIM_IMAGE *info) {
+ _sdk_validate_args(tim && info, 1);
+
if ((*(tim++) & 0xffff) != 0x0010)
return 1;