Commit Graph

14924 Commits

Author SHA1 Message Date
Jack Mordaunt 2d12e265cc tests/core/sync/chan: add test for contended try_send
This test ensures that contending threads racing to try_send against a
single blocking read will result in exactly one winner without any
senders blocking.
2025-06-12 17:35:48 -03:00
Jack Mordaunt c29168f76f core/sync/chan.try_send: avoid blocking if no reader is available
This changes the semantics of try_send to be consistently non-blocking.

That is, if the buffered is full OR there are no readers it returns
false.

The previous behaviour was such that it would block in the latter case
of no reader, and it would wait for a reader. That is problematic
because it produces inconsistent behaviour between buffered and
unbuffered channels which is astonishing and adds complexity to the
caller.

To illustrate the problem with the old behaviour, consider the
try_select operation: if a send-channel happens to be unbuffered the
try_select (which wants to never block) can now block, that unbuffered
send channel is selected (at random) and there is no reader on the other
side. Thus we have unpredictable blocking behaviour, which breaks the
guarantee that try_select never blocks.

If you want a blocking send you can just call "send" (the blocking
variant).

In addition, there is some reader/writer math done inside
can_{send,recv} such that they only report true if there is sufficient
reader/writer capacity. If there is contention we need to ensure that
each reader is paired to exactly one writer.

Consider try_send: if there is a single reader we can send. If there is
a single reader and a single writer, then we cannot send, as that reader
will be paired with the existing writer. Therefore can_send is only true
if there are more readers than writers at the time of check.

NOTE: The original tests don't need to use wait-looping with thread.yield()
or heuristic sleep. Instead we can just use blocking channel operations
rather than non-blocking operations.
2025-06-12 17:35:48 -03:00
Jack Mordaunt 52d38f1788 test/core/sync/chan: serialize try_select tests
These tests will race access to __global_context_for_test, which can
cause the test suite to flake. Even though only a single test actually
references the variable, the logic in try_select consumes it.
2025-06-12 17:15:37 -03:00
Laytan fc7fc4d5cd Merge pull request #5289 from JackMordaunt/jfm-sync_chan_refactor
Jfm sync chan refactor
2025-06-12 21:51:34 +02:00
Jack Mordaunt 3c3fd6e580 tests/core/sync/chan: move global state into test
While this state is not actually needed by more than one test, we can
just make it a static variable.
2025-06-12 16:14:52 -03:00
Jack Mordaunt c1cd525d9d core/sync/chan.select_raw: call try_select_raw with deprecation warning
Eventually select_raw should be a blocking select operation, but for now
we need to migrate people away.
2025-06-12 16:14:52 -03:00
Jack Mordaunt 96b91849a9 core/sync/chan.try_select_raw: fix doc comment typo
Signed-off-by: Jack Mordaunt <jackmordaunt.dev@gmail.com>
2025-06-12 16:14:52 -03:00
Jack Mordaunt 4d7c182f7d tests/core/sync/chan: test harness for chan.try_select_raw
This test harness ensures consistent non-blocking semantics and
validates that we have solved the toctou condition.

The __global_context_for_test is a bit of a hack to fuse together the
test supplied proc and the executing logic in packaage chan.
2025-06-12 16:14:52 -03:00
Jack Mordaunt faae81ba61 core/sync/chan.try_select_raw: test hook for testing the toctou
This is necessary because we need to allow the test guarantee against a
rare condition: where a third-party thread steals a value between the
validity checks can_{send,recv} and the channel operation
try_{send,recv}.
2025-06-12 16:14:52 -03:00
Jack Mordaunt 4043be8567 core/sync/chan.try_select_raw: skip nil input messages
This makes the proc easier and safer to call by letting the caller nil
out messages to skip sends.
2025-06-12 16:14:52 -03:00
Jack Mordaunt fb39e5a2f8 core/sync/chan.try_select_raw: clarify loop control flow
Use a label to clarify the continue statements.
2025-06-12 16:14:52 -03:00
Jack Mordaunt d5b7302ac0 core/sync.try_select_raw: fix TOCTOU
Fixes a TOCTOU where the channel could be used between the call to
can_{recv,send} and {recv,send} causing an unexpected blocking
operation.

To do this we use the non-blocking try_{recv,send} and retry the check
in a loop. This guarantees non-blocking select behaviour, at the cost of
spinning if the input channels are highly contended.

Signed-off-by: Jack Mordaunt <jackmordaunt.dev@gmail.com>
2025-06-12 16:14:52 -03:00
Jack Mordaunt be873af003 core/sync.select_raw: rename to try_select_raw
This follows the convention where non-blocking operations are prefixed
with "try" to indicate as much.

Since select_raw in it's current form doesn't block, it should be
try_select_raw, and allow select_raw to name a blocking implementation.
2025-06-12 16:14:52 -03:00
Jack Mordaunt 7f9589922d core/sync.select_raw: return a useful index
This fixes a flaw in the original implementation: the returned index is
actually useless to the caller.

This is because the index returned refers to the internal "candidate"
list. This list is dynamic, and may not have all of the input channels
(if they weren't ready according to chan.can_{recv,send}). That means
the index is not guaranteed to mean anything to the caller.

The fix introduced here is to return the index into the input slice
(recvs,sends) and an enum to specify which input slice that is.

If no selection was made, then (-1, .None) is returned to communicate as
much.

Signed-off-by: Jack Mordaunt <jackmordaunt.dev@gmail.com>
2025-06-12 16:14:51 -03:00
Laytan Laats 0ed6cdc98e mem/tlsf: fix asan reporting poisoning of already poisoned region
free on tlsf poisons the entire block, while alloc might only unpoison a
part of it (cause it's size is aligned up). This causes free to
potentially poison an already poisoned portion, which is a
use-after-poison.

Because this is "fine" and intended, I opted to just
@no_sanitize_address it.
2025-06-12 19:37:24 +02:00
Jeroen van Rijn 74f70bfbcb Comment 2025-06-12 17:09:33 +02:00
Jeroen van Rijn 298bab99a4 Merge pull request #5326 from Kelimion/fix-5321
Revert changes to thread_unix.odin
2025-06-12 16:56:29 +02:00
Jeroen van Rijn 5aa377e64e Revert changes to thread_unix.odin 2025-06-12 16:36:40 +02:00
Jeroen van Rijn b119ca0261 Merge pull request #5325 from Kelimion/fix-5321
Fix #5321
2025-06-12 16:14:01 +02:00
Jeroen van Rijn 59ba37f232 Remove transmute 2025-06-12 16:05:34 +02:00
Jeroen van Rijn 994b5a2a6c Remove c import 2025-06-12 16:01:31 +02:00
Jeroen van Rijn caf41aa046 Fix #5321 2025-06-12 16:00:24 +02:00
Jeroen van Rijn eb1b5f7d3a Merge pull request #5322 from Kelimion/fix-5321
Fix #5321 (on Windows)
2025-06-12 11:52:38 +02:00
Jeroen van Rijn ac5f3746cf Fix #5321 2025-06-12 11:30:36 +02:00
Jeroen van Rijn 50ff3fabdc Merge pull request #5317 from Feoramund/fixup-container-queue
Tidy up `core:container/queue`
2025-06-12 00:00:34 +02:00
Jeroen van Rijn a2be547dfd Merge exclude paths 2025-06-11 21:00:38 +02:00
Jeroen van Rijn 8a08edbdc7 Merge pull request #5319 from Kelimion/coverage
Add code coverage
2025-06-11 20:39:48 +02:00
Jeroen van Rijn 828d4c71d2 Now? 2025-06-11 20:33:12 +02:00
Jeroen van Rijn 921414aa74 Let's use the latest Ubuntu and a recent LLVM 2025-06-11 20:04:07 +02:00
Jeroen van Rijn 3965b2e4b4 kcov --version 2025-06-11 19:58:01 +02:00
Jeroen van Rijn 05cba0aca5 Change kcov url 2025-06-11 19:53:49 +02:00
Jeroen van Rijn d37a93cd7d Add code coverage 2025-06-11 19:43:42 +02:00
Jeroen van Rijn 7bfd70a828 Add code coverage check
Inspired by PR #4928
2025-06-11 19:34:24 +02:00
Feoramund 23c1ce8722 container/queue: Remove trailing whitespace 2025-06-11 11:57:38 -04:00
Feoramund 66b2acbf24 container/queue: Add tests 2025-06-11 11:55:30 -04:00
Feoramund 638a1529a3 container/queue: Add shrink 2025-06-11 11:55:30 -04:00
Feoramund 040d79e1b9 container/queue: Let queues be re-initialized with different allocators 2025-06-11 11:55:30 -04:00
Feoramund 81f5763482 container/queue: Add common aliases enqueue and dequeue 2025-06-11 11:55:30 -04:00
Feoramund 6cb84e467b container/queue: Document the package 2025-06-11 11:55:30 -04:00
Feoramund 862442511a container/queue: Reorganize 2025-06-11 11:55:29 -04:00
Feoramund 58bda1209a container/queue: Deprecate peek_*
The `*_ptr` and `peek_*` procedures did the same thing, except `peek_*`
was over-cautiously putting the index through a modulo when all
assignments to `q.offset` are already wrapped.
2025-06-11 11:54:52 -04:00
Feoramund 27cd508571 container/queue: Fix and add more bounds checking 2025-06-11 11:54:47 -04:00
gingerBill 4aec582a05 Add system:dl to foreign import for dlfcn.odin 2025-06-11 15:27:00 +01:00
Jeroen van Rijn 5bf180d8f9 Merge pull request #5311 from Kelimion/big-tests
Turn `core:math/bìg` tests into regular `core:testing` tests.
2025-06-11 00:56:21 +02:00
Jeroen van Rijn 0f90a610a2 Delete duplicate test vectors. 2025-06-11 00:49:51 +02:00
Jeroen van Rijn 9dafd77bc0 Turn core:math/bìg tests into regular core:testing tests.
`core:math/big` has been verified against Python's big integer implementation long enough.
Turn it into a regular regression test using the `core:testing` framework, testing against
a generated corpus of test vectors.
2025-06-11 00:40:52 +02:00
gingerBill 2a292b588a Merge pull request #5305 from Feoramund/fix-4362
Guard against invalid proc types in parameter list
2025-06-10 17:15:13 +01:00
Jeroen van Rijn ca65444007 Merge pull request #5310 from Kelimion/big-rat-tests
Add initial tests for big rationals
2025-06-10 17:16:19 +02:00
Jeroen van Rijn 4e50b9d331 Fix path 2025-06-10 16:55:03 +02:00
Jeroen van Rijn 4f4839ecc5 Add initial tests for big rationals 2025-06-10 16:46:12 +02:00