Skip to content

Fix off-by-one and integer overflow in Wasmtime memory bounds check - #10

Closed
bacek wants to merge 1 commit into
masterfrom
spr/bacek/fix-off-by-one-and-integer-overflow-in-wasmtime-memory-bounds-check-2
Closed

bacek wants to merge 1 commit into
masterfrom
spr/bacek/fix-off-by-one-and-integer-overflow-in-wasmtime-memory-bounds-check-2

Conversation

@bacek

@bacek bacek commented Mar 13, 2026

Copy link
Copy Markdown
Owner

WasmTimeCompartment::getMemory used ptr + size >= memory_span.size()
which has two bugs:

  • Off-by-one: a slice that exactly reaches the end of memory
    (ptr + size == memory_span.size()) is a valid access but was
    incorrectly rejected.

  • Unsigned integer overflow: WasmPtr and WasmSizeT are both
    uint32_t. With ptr = 0xFFFFFFFF, size = 1 the addition wraps to
    0, the check passes, and memory_span[0xFFFFFFFF] is accessed far
    outside the WASM linear memory, breaking sandbox isolation.

Fix: rewrite the check as size > memory_span.size() || ptr > memory_span.size() - size,
which avoids any addition and correctly accepts the boundary case.

The WasmEdge backend is not affected (it delegates bounds checking to
WasmEdge_MemoryInstanceGetPointer).

Co-Authored-By: Claude Sonnet 4.6 [email protected]

commit-id:e31f0f25

Created using spr 1.3.7
@bacek bacek closed this Mar 13, 2026
bacek pushed a commit that referenced this pull request May 14, 2026
Under x86-64-v3 + ThinLTO the auto-vectorizer/unroller produces a 16-YMM
scratch zero-init followed by per-byte scatter writes in `stringJaccardIndex`.
The combination causes store-buffer pressure and partial store-forwarding
stalls on Intel cores, regressing the `string_similarity` perf test #10
by ~17%. Compiling this file at v2 keeps the SSE-only sequence
(`memset@plt` + XMM intersection/union loop) that matches master's
performance.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
bacek pushed a commit that referenced this pull request Nov 16, 2026
The collation sort (#2, 5M rows) and the single-column string sort (#3,
20M rows) take 1.2-1.9 s on MasterCI on both arches. They get
disabled="1" (text and number unchanged) and are appended as #9 and #10
on a quarter of the rows with max_block_size = 16384, a quarter of the
default 65409. `numbers_mt` hands out blocks of exactly max_block_size
rows, so each copy has as many blocks as its original (77 and 306): the
same number of cursors in every per-stream merge and the same 16-stream
final merge, with a quarter of the rows per block. EXPLAIN PIPELINE of
each original and its copy is identical (16 streams).

Measured on official MasterCI release builds, two servers pinned to 24
cores of one NUMA node, perf.py, 25 runs: copy / original time 0.248
(#2) and 0.252 (#3), projected CI medians 380 / 487 ms amd, 298 / 483 ms
arm. On the builds before/after ClickHouse#115869 (sorted array of cursors) #2 is
1.161x faster and its copy 1.159x; on those before/after ClickHouse#126829
(cheaper collation) 2.179x and 2.111x; #3 and its copy show no change
on either pair (1.005 / 1.008, 1.004 / 1.001), as the heap guard should.

#0 and #1 stay as they are: on this rig the ClickHouse#115869 change did not show
on them beyond the A/A resolution, so there was no measured guarantee
that copies would keep their sensitivity.

About 32 s (amd) and 29 s (arm) of query time less per master
performance job; 9 queries stay enabled, so all of them still run.

Related: ClickHouse#129258

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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.

1 participant