Skip to content

fix: [libpthreadglue] tighten error handling in OS-abstraction layer#840

Merged
uyjulian merged 1 commit into
ps2dev:masterfrom
fjtrujy:fix/libpthreadglue-osal-error-handling
May 11, 2026
Merged

fix: [libpthreadglue] tighten error handling in OS-abstraction layer#840
uyjulian merged 1 commit into
ps2dev:masterfrom
fjtrujy:fix/libpthreadglue-osal-error-handling

Conversation

@fjtrujy
Copy link
Copy Markdown
Member

@fjtrujy fjtrujy commented May 11, 2026

Summary

  • pte_osThreadCreate used to malloc the stack and call CreateThread, then NULL-check the stack and check threadId < 0 after __threadInfo[threadId] was already written — a negative id caused an OOB write. The check for the cancelSem semaphore was also missing, and the failure paths leaked cancelSem and the stack.
  • Reorder so each resource is validated immediately after it is allocated and cleaned up in reverse order on failure.
  • Propagate CreateSema failures from pte_osMutexCreate and pte_osSemaphoreCreate as PTE_OS_NO_RESOURCES instead of returning a negative handle through *pHandle as if it were a valid semaphore.

Related

Test plan

  • Build ee/libpthreadglue cleanly.
  • Run pthread-embedded test suite on PS2 — exercises thread/mutex/sema creation paths and now also the NULL-handle paths from the companion pthread-embedded PR.
  • Smoke-test a sample that creates pthreads (e.g. a port that already uses libpthreadglue).

pte_osThreadCreate previously:
- called CreateThread with a stack pointer that hadn't been validated,
  then NULL-checked it afterwards
- wrote into __threadInfo[threadId] before checking whether threadId
  was negative — a CreateThread failure caused an OOB write
- ignored the return value of CreateSema for cancelSem
- on cleanup paths, leaked the cancelSem semaphore and the stack
  allocation

Reorder so each resource is validated immediately after allocation and
cleaned up in reverse order on failure. Also propagate CreateSema
failures from pte_osMutexCreate and pte_osSemaphoreCreate as
PTE_OS_NO_RESOURCES instead of returning a negative handle as if it
were valid.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Copy link
Copy Markdown
Member

@uyjulian uyjulian left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lgtm

@uyjulian uyjulian merged commit 9904d0c into ps2dev:master May 11, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants