Commit Graph
15 Commits
Author SHA1 Message Date
libretroadmin 419ad3ef58 WII/GX: omnibus audit pass (memory safety, leaks, folds, cleanup)
Sweep of the Wii/libogc-side code:

Memory safety
- mem2: _mem2_strndup used a calloc-then-memcpy(n+1) sequence that
  (a) read past s when strlen(s) < n (UB) and (b) left the result
  without a NUL terminator when strlen(s) >= n. Rewritten via
  strnlen() with an explicit terminator and _mem2_malloc instead
  of _mem2_calloc.
- mem2: _mem2_calloc had no num*size overflow check; large pairs
  silently wrapped and produced a tiny allocation that the caller
  proceeded to write a huge amount into. Added the standard
  UINT32_MAX/size guard.
- mem2: _mem2_memalign silently downgraded any requested alignment
  to the heap's 32-byte page size. The lwp_heap can't honour
  stricter requests, so reject them (return NULL) rather than lie.
- gx_joypad: gx_joypad_button returned (pad_state[port] & (1ULL<<key))
  cast to int32_t, truncating bit >= 32 to zero and silently reporting
  "not pressed" for every Wiimote/Classic/Nunchuk binding. Cast to
  bool first.
- gx_joypad: gx_joypad_get_buttons used BITS_COPY16_PTR on a uint64_t
  pad_state, dropping every Wiimote/Classic/Nunchuk bit (28..44) and
  GX_QUIT_KEY (62). Use BITS_COPY64_PTR.
- gx_gfx: menu_tex.data was sized 240*212 uint32_t (203,520 bytes),
  but convert_texture16 writes width*height*2 bytes into it and
  gx_set_video_mode allows up to 560x240 for the 21:9 RGUI aspect
  cases, producing a ~64 KB write past the buffer. Sized for the
  worst-case 560*240 explicitly. The DCFlushRange call alongside
  passed fb_width*fb_pitch (i.e. fb_width^2 * 2), conflating width
  and height; corrected to fb_height*fb_pitch.

Leaks
- platform_gx: frontend_gx_deinit didn't release the device-thread
  mutex/cond/mutex triple created in frontend_gx_init. Re-init
  cycles (CMD_EVENT_QUIT + relaunch) leaked all three. Freed.
- gx_gfx: g_tex.data is memalign'd in init_vtx and was never freed
  by gx_free. Now released alongside the rest of the GX state.
- gx_gfx: both XFB framebuffers in gx->framebuf[] were allocated
  via MEM_K0_TO_K1(memalign(...)) in setup_video_mode and never
  freed. free() takes the cached alias - use MEM_K1_TO_K0() before
  passing.

Correctness
- platform_gx: DEFAULT_DIR_REMAP was filled from DEFAULT_DIR_MENU_CONFIG
  *before* MENU_CONFIG itself was filled, producing a remap path
  rooted at the empty/stale MENU_CONFIG value on first boot. Swapped.
- gx_joypad: check_port0_active() was called inside the per-port
  loop, four times per poll, with a partial pad_count for the first
  three iterations - which could force-enable port 0 spuriously.
  Hoisted outside the loop.
- gx_input: rvl_input_poll's realloc-grow path zeroed only x_last/y_last
  on the (possibly newly-allocated) tail entries, leaving x_abs/y_abs
  uninitialised. The next poll's x_last = x_abs picked up uninitialised
  heap data, producing a one-frame garbage delta on hot-plug. Zero
  exactly the new tail with memset; existing entries keep their
  last known position. Also dropped the shadowed inner 'unsigned i;'
  and the redundant 'gx &&' on the inner if.
- gx_input: lightgun path had a video_driver_get_viewport_info(&vp)
  call immediately followed by six lines that zeroed every field it
  populated. The dispatcher went through the video vtable on every
  lightgun query for nothing - removed.
- gx_audio: gx_audio_data was 'static volatile gx_audio_t *', making
  every member access through the pointer volatile, including the
  4 KB sample data[][] array (which is hot). The three IRQ-shared
  fields are individually volatile-qualified in the struct, which is
  sufficient. Dropped the outer volatile and the gratuitous cast in
  the DMA callback. Also published gx_audio_data only after the
  struct is fully initialised, so the IRQ never sees a partial state.

Folds / cleanup
- mem2: all _mem2_* helpers now static - they are only called from
  the __wrap_* functions in the same TU (Makefile.wii uses
  -Wl,-wrap,malloc,...). Lets the compiler inline them aggressively.
- mem2: _mem2_strdup and _mem2_strndup were using _mem2_calloc and
  then immediately memcpy'ing over the zeroed bytes. _mem2_malloc
  is enough.
- mem2: _CPU_ISR_Disable's "level" output was being silenced with
  a (void)level; cast in __lwp_heap_block_size. Replaced with the
  same `uint32_t level = 0;` idiom the rest of the codebase uses.
- platform_gx: frontend_gx_get_free_mem computed
    SYSMEM1_SIZE - (SYSMEM1_SIZE - SYS_GetArena1Size())
  which is just SYS_GetArena1Size(). Compiler folds it, but the
  source read as if there was something subtle going on. Reduced.
- platform_gx: gx_rom_path is `extern char[]`. The `gx_rom_path &&`
  arm of the if-condition is a tautology - removed.
- platform_gx: the two FRONTEND_FORK_CORE / FRONTEND_FORK_CORE_WITH_ARGS
  switch arms had identical bodies - folded.
- platform_gx: device-name buffer build used two strlcpy()s; one
  snprintf is the same cost and one less line.
- app_booter: two-statement (p_paddr &= 0x3FFFFFFF; p_paddr |= 0x80000000;)
  bit fold; direct-init for phdrs and dolfile; collapsed
  app_booter_main's exeEntryPointAddress to a conditional initialiser.
- gx_gfx: verts[], vertex_ptr[], color_ptr[], menu_gx_resolutions[]
  had external linkage but were referenced only within gx_gfx.c, and
  none were written. Marked static const so they go in .rodata. Call
  sites to GX_SetArray (which takes void*) get explicit casts.
- gx_gfx: gx_blit_line had several declare-then-assign locals derived
  from gx-> and gx_mode; folded into direct const initialisers, with
  the gx NULL-check moved above the derefs.
- gx_audio: gx_audio_copy_swapped (which is not an endianness swap,
  it's a 16/16 word rotate for the AI hardware's L/R lane order)
  renamed to gx_audio_lr_swap and unrolled by 4 for better dispatch
  on Broadway/Gekko.
2026-05-16 22:48:59 +02:00
LibretroAdmin cde82f532c * Naming convention changes for variable names/function arguments
* Try to fit lines within ANSI 80-char limit
2025-01-19 20:19:14 +01:00
LibretroAdmin 9d15900979 Small cleanups: * Prevent some unneccessary strlcpy calls and intermediary string copies * Name local length variables for strings '_len', parameter/argument 'len' 2024-12-28 22:22:07 +01:00
Viačasłaŭ 3019b926c2 Fix typos (#17068) 2024-10-01 17:36:33 -07:00
libretroadmin 7ebab640d7 Cleanups/style nits 2023-02-23 13:15:14 +01:00
LibretroAdmin a94c4c72c2 Use size_t as type for returned value of strlen instead of int 2022-10-29 19:40:48 +02:00
Vladimir Serbinenko 4a53c6771c Several fixes and improvements do NGC and Wii (#13698)
* Support new elf2dol path in Makefile.griffin

* Fix NULL pointer dereference when no core info is available

* Fix double include of different versions of libogc

* Move ngc and wii to Makefile.common

Resulting binary is 60K *smaller*. This is surprising but probably due to
less inlining happening

* gx_gfx: Fix hang on gfx initialization

* Remove sthread_isself on NGC/Wii

It relies on pthread_equal that is not there for either legacy pthreads
or normal pthreads on those platforms

* Wii networking

* Missing includes in split

* Network fixes

* Fix logging of non-format string as a format

* Support for gx netlogger

* Network fix

* Network fixes

* Network info

* gx: Move config files to app directory

Otherwise it clashes between wiiU and vWii version with unpredictable
effects
2022-03-04 13:43:00 +01:00
gblues 6904101c44 Clean up trailing whitespace
== DETAILS

Really simple code cleanup, because my editor flags trailing whitespaces
and it's pretty annoying.
2017-12-12 00:24:18 -08:00
Francisco José García García c68325a713 (VITA) Add support for -mthumb 2016-11-14 19:13:07 +01:00
netux79 a2851ab70b (GX) Resolution Saving's Video Corruption Fix.
Also some cleanups for the RA GX version included.
2016-02-12 16:58:59 -05:00
twinaphex 9d98bfbe6b (GX) Move defines to defines/gx_defines.h 2015-11-18 12:55:28 +01:00
twinaphex d92bf9b559 (memory/wii/mem2_manager.c) Cleanups 2015-09-23 12:05:17 +02:00
twinaphex 2c325f0ea6 (memory/wii) Cleanups 2015-09-20 16:01:27 +02:00
twinaphex 07f5d2e399 (Wii) Buildfixes 2015-09-20 15:03:49 +02:00
twinaphex a7f78a8d88 move some files around - create memory/ subdir 2015-09-20 14:54:41 +02:00