Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 83 additions & 10 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,10 @@ Task 16 supplies `/webcpp/asio//cobalt`: Cobalt's ordinary `co_main` runs on
webcpp's context. The adapter replaces both `main_promise::run_main` and
`final_suspend` from Boost 1.92's main.cpp. Do not link the original runner
beside it. The source target's issue 287 warning still applies without exceptions.
Clang additionally diagnoses Boost's missing unhandled_exception member in that
mode; the integration suppresses only that diagnostic for exception-free
Clang/Emscripten consumers, without changing exception handling.
The checked Cobalt common overlay supplies the required unhandled_exception
promise member when exceptions are disabled. Its unreachable exception path
calls std::terminate, without enabling exception handling or suppressing a
compiler diagnostic.

The Cobalt stack probe measures real Asyncify unwinds from the runner through
the yield, a Cobalt timer wait and an Asio timer wait. The timer waits cannot
Expand Down Expand Up @@ -163,7 +164,26 @@ cancellation machinery. Automatic SIGINT/SIGTERM cancellation is unavailable;
host-driven graceful shutdown requires a separate application protocol.
The runner-only compile test is supplemented by public-target runtime tests:
`cobalt_wasi`, `cobalt_network_wasi`, `cobalt_io`, the `cobalt_wasip3` status
lane and explicit `cobalt_thread` in the cooperative profile.
lane and the recurring `threads` lane in the cooperative profile.

## WASIp2 port

The owner approved P2 Asio core and supplied-stream WebSocket on 2026-11-03.
`/webcpp/asio//asio` supports `threading=single`, EH on/off, through the shared
checked Boost overlay. P2 now also supplies the source-built OpenSSL/CA targets,
Cobalt and its SSL wrapper; preserve their dedicated EH-on/off peer and
cancellation tests in `test/wasip2_tls_cobalt`. P2 threading remains unavailable
in the pinned SDK. Never infer new feature support from core support alone.
The P2 reactor reuses libc select/poll and omits the unavailable pipe interrupter.
`tools/boost/asio-wasip2` owns the checked SDK 34.0 private descriptor-table bridge
and the asynchronous DNS extension. Revalidate its libc ABI before changing SDKs.
Preserve deferred initiation, handler associations, operation cancellation and
resource cleanup. DNS must not start a worker or block inside async initiation.
The P2 CLI stream requests output flush before switching protocol direction;
keep the real large-frame regression. Cancellation cannot preempt blocking host
imports. Run `libs/asio/test/wasip2` and its `supplied-stream` lane in both exception
profiles, plus native/P3 regressions. Keep the feature matrix and AsciiDoc guide
consistent with actual runtime evidence.

## WASIp3 port

Expand All @@ -175,18 +195,23 @@ patch and its hash manifest explicitly when changing Boost versions.
`threading=single` disables Asio threads and accepts exceptions on/off.
`threading=multi` selects wasi-sdk's experimental cooperative sysroot with
`-pthread`; SDK 34.0 provides only its no-exceptions C++ runtime. Use Wasmtime
49.0.3 for the recorded local tests; 47.0.3 cannot load the cooperative binary.
50.0.1, the project-wide runtime; earlier tests found that 47.0.3 cannot load
the cooperative binary.
Cooperative threads do not promise CPU parallelism.

Preserve datagram boundaries: WASIp3 `send_to` and `async_send_to` concatenate
multiple nonempty UDP buffers into one datagram until wasi-libc provides
`sendmsg`. Gather before Asio's native iovec limit, use the context allocator
for synchronous storage and the handler's associated allocator for asynchronous
storage, and release operation storage before invoking the handler. Multiple
nonempty receive buffers and connected `send`/`async_send` remain unsupported.
The scalar/empty-buffer path must work without heap scratch buffers. TCP
partial transfers remain valid. Single-thread async DNS posts an unsupported
result; it must not try to construct a null thread or abort initiation.
storage, and release operation storage before invoking the handler. Connected
`send`/`async_send` use the same gather path; multi-buffer receives scatter one
received datagram, retaining truncation semantics. The scalar/empty-buffer path
must work without heap scratch buffers. TCP partial transfers remain valid.
Both P3 thread profiles use native asynchronous DNS and the checked private
waitable-descriptor bridge. Never block initiation or use a polling timer for
lookup progress. Validate the SDK/libc ABI snapshot before using the bridge.
Keep deferred initiation, cancellation, associated executors and allocator
release-before-callback witnesses in `test/capability_wasi`.

Keep `doc/capabilities.adoc` current for every feature/target/variant change.
`/webcpp/asio//cobalt` supports WASIp3 through the superproject's checked
Expand Down Expand Up @@ -233,3 +258,51 @@ Run `tools/ca_certificates/run.py --fresh` locally for the native/WASIp3 matrix.
Its WASIp3 clients have no certificate filesystem grants. Preserve that witness.
Embedded updates require rebuilding and redeploying. Do not silently download
roots during builds or runtime, or present a PEM export as Firefox's full policy.

## Windows context-owned thread allocations

The checked Boost 1.92 overlay in tools/boost/asio-windows forwards the context
allocator into both IOCP-owned thread constructors, including the lazily started
timer thread. It is selected only for target-os=windows and reaches compiled
Cobalt sources as well as consumers. Keep its original and patched byte hashes
pinned. Do not replace the allocation regression with a warm-up or an allowance
for extra global allocations.

## Joint Asio and Beast closure

Preserve deferred initiation and owning resolver input lifetimes. Resolver host
and service storage uses the initiating token's associated allocator; an allocator
bound only when a deferred operation is launched cannot retroactively own that
captured storage. Keep the positive controls for unbound handlers and runtime
allocation attribution; never replace them with a permitted allocation count.

The owner clarified on 2026-11-03 that Asio and Beast are intended foundations
for a later Trystero migration. Never ban their combination to avoid a dependency
conflict. All native consumers, including libdatachannel, use the same pinned
source-built OpenSSL provider. Trystero's current native backend requires threads.

The `tls-integration` own lane runs real native and WASIp3 TLS peers, state and
cancellation checks. The native `openssl-upstream` lane additionally requests
`webcpp-openssl-tests=on` to test the exact source-built archives before use.
OpenSSL's native Perl harness is not evidence for its WASI configuration.

Use the repository's `webcpp-documentation` skill for Diataxis organization,
AsciiDoc and generated MrDocs reference. The maintained `doc/changelog.adoc`
is the final appendix; never edit generated reference or matrix files by hand.

## Capability completion and browser workers (2026-11-03)

`config.hpp` owns read-only numeric `WEBCPP_ASIO_HAS_*` capabilities. Optional
`WEBCPP_ASIO_REQUIRE_*` switches assert compile-visible requirements without
banning portable headers. Selected `WEBCPP_ASIO_HAS_OPENSSL=1` remains distinct
from platform `HAS_OPENSSL_BACKEND`. Runtime network grants, TLS identities,
page isolation and blocking host imports are not compile-time facts.

Emscripten explicitly supports S and M with JSPI/Asyncify and both EH choices.
Only S disables Asio threads. The M poll wrapper preserves suspension on the
actual main thread; do not silently enable PROXY_TO_PTHREAD. Run a browser
context only on its creator thread, post between threads, and keep DOM values
on main. `browser::main_thread_context` callbacks are nonsuspending. Its context
must outlive executor calls. See the public worker guide and the real-browser
`browser-workers` lane. The versioned raw pthread-return probe documents the
pinned Asyncify SDK limitation; it is not a passing feature test.
59 changes: 22 additions & 37 deletions build.jam
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ project /webcpp/asio ;

import feature ;
import numbers ;
import errors ;
import webcpp ;
import webcpp-target ;

use-project /webcpp/asio-wasip3 : ../../tools/boost/asio-wasip3 ;
use-project /webcpp/asio-wasip2 : ../../tools/boost/asio-wasip2 ;
use-project /webcpp/asio-windows : ../../tools/boost/asio-windows ;

# The stack Asyncify saves a suspension in, in bytes: 8192 unless a program
# asks for more with <asyncify-stack-size>, propagated so that asio's usage
Expand Down Expand Up @@ -61,14 +62,14 @@ rule asyncify-stack ( properties * )
# (include/webcpp/asio/browser/src.hpp), linked into every program on
# emscripten by asio's usage requirements below.
obj browser-src : src/browser_src.cpp : <include>include <library>/boost//headers
<toolset>emscripten:<define>BOOST_ASIO_DISABLE_THREADS ;
<library>/webcpp/config//config
<toolset>emscripten,<threading>single:<define>BOOST_ASIO_DISABLE_THREADS ;
explicit browser-src ;

# On emscripten Asio needs BOOST_ASIO_DISABLE_THREADS: without it,
# <boost/asio/io_context.hpp> stops at "Only Windows and POSIX are
# supported!" (<boost/asio/detail/signal_blocker.hpp>, line 27 in Boost
# 1.92). Its inline namespace differs with and without threads, so a
# translation unit that missed the define fails at link. The browser's
# Single-thread Emscripten needs BOOST_ASIO_DISABLE_THREADS. A pthread build
# supplies the POSIX thread implementation instead. Boost's inline namespace
# differs with and without threads, so every translation unit must agree.
# The browser's
# values reach C++ through embind: -lembind, a free feature, which a usage
# requirement keeps.
#
Expand All @@ -84,72 +85,56 @@ explicit browser-src ;
# not otherwise need them; JavaScript calls both through stackSave/Restore.
# tag::asio[]
alias asio : : : : <include>include <library>/boost//headers
<library>/webcpp/config//config
<target-os>windows:<library>/webcpp/asio-windows//headers
<toolset>clang-wasip2:<library>/webcpp/asio-wasip2//headers
<toolset>clang-wasip2:<source>/webcpp/asio-wasip2//pollable
<toolset>clang-wasip3:<library>/webcpp/asio-wasip3//headers
<toolset>emscripten:<define>BOOST_ASIO_DISABLE_THREADS
<toolset>emscripten,<threading>single:<define>BOOST_ASIO_DISABLE_THREADS
<toolset>emscripten:<linkflags>-lembind
<toolset>emscripten:<linkflags>-Wl,--export=emscripten_stack_get_current
<toolset>emscripten:<linkflags>-Wl,--export=_emscripten_stack_restore
<toolset>emscripten,<threading>multi:<linkflags>-Wl,--wrap=__syscall_poll
<toolset>emscripten:<source>browser-src
<toolset>emscripten,<webcpp-suspend>jspi:<linkflags>-sJSPI
<toolset>emscripten,<webcpp-suspend>asyncify:<linkflags>-sASYNCIFY
<conditional>@asyncify-stack
<toolset>emscripten:<tag>@webcpp%suspend-tag ;
# end::asio[]

# WASIp3 uses the checked Cobalt/Asio overlays; WASIp2 has no async port.
rule cobalt-platform ( properties * )
{
if [ webcpp-target.target-of $(properties) ] = wasip2
{
errors.user-error "webcpp::asio: Cobalt requires native, Emscripten or WASIp3." ;
}
}

# WASIp2 and WASIp3 use the same checked Cobalt capability overlays.
# Cobalt's entry point, with webcpp's bounded application event loop.
# tag::cobalt[]
lib cobalt-main : src/cobalt_main.cpp
: <link>static <library>asio <library>/webcpp//boost_cobalt ;
explicit cobalt-main ;
# Boost omits main_promise::unhandled_exception without exceptions. Clang
# diagnoses the entry coroutine anyway; suppress only that diagnostic, and
# only in the explicitly exception-free Cobalt consumer configuration.
alias cobalt : : <conditional>@cobalt-platform : : <library>asio <library>cobalt-main <library>/webcpp//boost_cobalt
<toolset>clang,<exception-handling>off:<cxxflags>-Wno-coroutine-missing-unhandled-exception
<toolset>emscripten,<exception-handling>off:<cxxflags>-Wno-coroutine-missing-unhandled-exception ;
# The checked common Cobalt patch supplies the promise member required even
# when a compiler builds the coroutine without exception handling.
alias cobalt : : : : <library>asio <library>cobalt-main <library>/webcpp//boost_cobalt ;
# end::cobalt[]

# tag::openssl[]
rule openssl-required ( properties * )
{
if [ webcpp-target.target-of $(properties) ] = wasip2
{
errors.user-error "webcpp::asio: OpenSSL requires native or WASIp3; WASIp2 is not supported." ;
}
}

rule openssl-linked ( properties * )
{
if [ webcpp-target.target-of $(properties) ] in native wasip3
if [ webcpp-target.target-of $(properties) ] in native wasip2 wasip3
{
return <library>/webcpp/third_party//openssl
<define>WEBCPP_ASIO_HAS_OPENSSL=1 ;
}
}

# Refused on WebAssembly: every source of a program built for emscripten
# Refused on Emscripten: every source of a program built for emscripten
# that names it stops at the forced include, whose message says what to
# write. Quoted, as the Jamroot quotes Boost's directory, for a path that
# holds a space.
path-constant WEBCPP_ASIO_ROOT : . ;
alias openssl : : <conditional>@openssl-required
: : <conditional>@openssl-linked
alias openssl : : : : <conditional>@openssl-linked
<toolset>emscripten:<cflags>"-include \"$(WEBCPP_ASIO_ROOT)/src/openssl_on_webassembly.hpp\""
<toolset>emscripten:<cxxflags>"-include \"$(WEBCPP_ASIO_ROOT)/src/openssl_on_webassembly.hpp\"" ;
# end::openssl[]

# Browser tests exercise the settings adapter without linking OpenSSL.
alias openssl-tests : : <conditional>@openssl-required
: : <conditional>@openssl-linked ;
alias openssl-tests : : : : <conditional>@openssl-linked ;
explicit openssl-tests ;

# An explicit portable trust policy, separate from the raw OpenSSL context.
Expand Down
2 changes: 1 addition & 1 deletion doc/Jamfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ webcpp.doc asio : asio.adoc ;
# (/webcpp//emscripten-reference), with the define of build.jam that a
# requirement conditioned on the emscripten toolset does not give it.
webcpp.reference asio : <library>/webcpp//emscripten-reference
<define>BOOST_ASIO_DISABLE_THREADS ;
<define>BOOST_ASIO_DISABLE_THREADS <define>WEBCPP_ASIO_REFERENCE ;
22 changes: 18 additions & 4 deletions doc/allocation.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
The application supplies an allocator to the `io_context` through its
`std::allocator_arg` constructor. Boost.Asio uses it for the context's
services and their I/O state. On Emscripten the bridge's registration map
uses the context's allocator too.
uses the context's allocator too. On Windows, a checked Boost 1.92 overlay
forwards that allocator into IOCP's internal worker and timer thread state.
This includes the first timer operation, without warming up its thread or
allowing an extra global allocation.

An asynchronous operation uses its completion handler's associated
allocator. Giving an allocator to the context does not associate it with
Expand All @@ -37,9 +40,17 @@ operation's associated allocator:
* Asyncify allocates its unwind data when a call suspends.
* Boost.Asio's `basic_resolver_results` allocates its result entries with
`new`. The measured operation scenario above does not resolve a name.
Forward asynchronous resolution also owns copies of the input names so a
deferred operation does not borrow temporary string storage. These result
and input strings use the standard allocator, not the handler allocator.
Forward asynchronous resolution owns copies of its input names through the
initiating token's associated allocator, preserving temporary input lifetimes.
Bind the allocator when creating a deferred operation; binding one only when
later launching it cannot retroactively change its already-owned input storage.
* WASI libc allocates pipe descriptors and message storage for Asio's wakeup pipe.
The test attributes only calls creating that pipe and writes to its tracked
writer descriptors, removes descriptors on close, and exercises a real pipe
as a positive control. Ordinary user writes and allocations remain counted.
* OpenSSL owns context, trust-store and cryptographic allocations. Its memory
hooks are process-global and must be set before any OpenSSL allocation; an
Asio context cannot install a per-operation allocator for other OpenSSL users.

The allocation test replaces Emscripten's `select` in the test executable
with a version whose poll array is on the stack. It marks only the call to
Expand All @@ -61,6 +72,9 @@ runtime initialization, not the process's cold-start memory usage.
`allocation_native` runs natively, with `allocation_native_release` also
checking an optimized build; `allocation` joins the Node-based
WebAssembly tests with JSPI and Asyncify, each with and without exceptions.
`allocation_wasip3` checks the single-thread profile with exceptions on/off;
`allocation_wasip3_threads` checks the supported cooperative profile without
exceptions through the `threads` lane.
A counting allocator services context construction, a timer, ten posts and
on Emscripten the bridge's two event deliveries. The measurement includes
object destruction. It requires allocations through the supplied allocator
Expand Down
13 changes: 10 additions & 3 deletions doc/asio.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// accompanying file LICENSE_1_0.txt or copy at
// https://www.boost.org/LICENSE_1_0.txt)

= asio: Boost.Asio in the browser's event loop for {cpp}
= asio: Boost.Asio and Cobalt for {cpp} and WebAssembly
:toc: left
:toclevels: 1
:idprefix:
Expand All @@ -21,8 +21,11 @@ lacks there, the configuration Asio needs to build, what wakes `run` from a
browser event, and a limit on the handlers one turn runs, so that the page
keeps rendering.

WASIp3 additionally provides timers and plain TCP/UDP, with single-thread and
experimental cooperative-thread profiles. See <<asio-capabilities>> for their
WASIp2 provides single-threaded timers, TCP/UDP, pollable-driven forward DNS,
supplied CLI streams, Cobalt and optional source-built TLS.
See <<asio-wasip2>> for its explicit limitations.
WASIp3 provides timers, TCP/UDP, waitable-driven forward DNS, Cobalt and the same
optional TLS provider, with single-thread and experimental cooperative-thread profiles. See <<asio-capabilities>> for their
limits and the features available in each target variant.

[#asio-reading]
Expand Down Expand Up @@ -53,6 +56,8 @@ include::capabilities.adoc[]

include::building.adoc[]

include::wasip2.adoc[]

include::wasip3.adoc[]

include::run.adoc[]
Expand All @@ -79,3 +84,5 @@ include::testing.adoc[]
== Reference

include::{reference}[leveloffset=+1]

include::changelog.adoc[]
Loading
Loading