map: harden pooled upload path + fix arena reservation aliasing
- Commit
3cb951e2d1b9c4f40e50fcf211cf6a5e007bd9bc- Author
- vayun-mathur <[email protected]>
- When
- 2026-10-17 19:57 UTC (1 month ago)
- Parents
5056c92
Diff
library/map/src/main/rust/src/tile/source/tests.rs
diff --git a/library/map/src/main/rust/src/tile/source/tests.rs b/library/map/src/main/rust/src/tile/source/tests.rsindex 1ea7c416d6..369119cf7a 100644--- a/library/map/src/main/rust/src/tile/source/tests.rs+++ b/library/map/src/main/rust/src/tile/source/tests.rs@@ -417,8 +417,13 @@ fn a_batch_read_issues_one_request_per_range_in_order() { for slot in &out { assert_eq!(slot.as_ref().unwrap().len(), 64); }+ // Result slots follow request order (handles join in order); the issue order across+ // the scoped threads is nondeterministic by design, so the log compares as a set:+ // one request per range, no merging, no duplication.+ let mut logged = r.fetcher.logged();+ logged.sort(); assert_eq!(- r.fetcher.logged(),+ logged, vec!["bytes=0-7", "bytes=100-107", "bytes=200-207"], ); }
library/map/src/main/rust/src/vulkan/renderer/upload.rs
diff --git a/library/map/src/main/rust/src/vulkan/renderer/upload.rs b/library/map/src/main/rust/src/vulkan/renderer/upload.rsindex d0561d361e..10236f776a 100644--- a/library/map/src/main/rust/src/vulkan/renderer/upload.rs+++ b/library/map/src/main/rust/src/vulkan/renderer/upload.rs@@ -42,6 +42,9 @@ impl Renderer { flat_meshes.push((&layer_mesh.vertices, &layer_mesh.indices)); } for region in &mesh.regions {+ if region.indices.is_empty() {+ continue;+ } flat_region_at.push(flat_meshes.len()); flat_meshes.push((®ion.vertices, ®ion.indices)); }
library/map/src/main/rust/src/vulkan/renderer/upload_pool.rs
diff --git a/library/map/src/main/rust/src/vulkan/renderer/upload_pool.rs b/library/map/src/main/rust/src/vulkan/renderer/upload_pool.rsindex 7533d235c3..2e00641e66 100644--- a/library/map/src/main/rust/src/vulkan/renderer/upload_pool.rs+++ b/library/map/src/main/rust/src/vulkan/renderer/upload_pool.rs@@ -176,14 +176,8 @@ fn align64(v: vk::DeviceSize) -> vk::DeviceSize { (v + 63) & !63 } -/// Reserve arena space for one packed group; `None` when no arena fits.-///-/// `None` is not an error — the caller falls back to fresh buffers and counts the-/// allocation. Pure bump math, no Vulkan calls, so this never fails for device reasons.-/// A group larger than one arena never fits: fall back rather than splitting draws-/// across arenas, which would complicate every bind site for a case (a single mesh-/// group over 8 MiB) that never occurs.-fn try_reserve(+/// Fit one packed group into an existing arena; `None` when none fits.+fn try_fit( pool: &UploadPool, meshes: &[(&[f32], &[u32])], vert_bytes: vk::DeviceSize,@@ -192,9 +186,6 @@ fn try_reserve( if vert_bytes == 0 || index_bytes == 0 { return None; }- if vert_bytes > VERTEX_ARENA_BYTES || index_bytes > VERTEX_ARENA_BYTES / 2 {- return None;- } for (n, arena) in pool.arenas.iter().enumerate() { let vo = align64(arena.used_verts); let io = align64(arena.used_indices);@@ -207,15 +198,7 @@ fn try_reserve( }); } }- if pool.arenas.len() >= MAX_ARENAS {- return None;- }- Some(PooledPacked {- vert_offset: 0,- index_offset: 0,- arena: pool.arenas.len(),- first_index: rebase_first_index(meshes),- })+ None } /// Grow the pool by one arena pair (2 fresh allocations, counted by the caller).@@ -265,7 +248,9 @@ unsafe fn commit_bytes( packed_indices: &[u32], ) -> bool { let device = &renderer.context.device;- let arena = &renderer.upload_pool.arenas[packed.arena];+ let Some(arena) = renderer.upload_pool.arenas.get(packed.arena) else {+ return false;+ }; let vert_bytes = std::mem::size_of_val(packed_verts) as vk::DeviceSize; let index_bytes = std::mem::size_of_val(packed_indices) as vk::DeviceSize; // HOST_COHERENT arena memory: map, copy, unmap — no flush. Two maps per group; the@@ -308,7 +293,16 @@ unsafe fn commit_bytes( } fn advance_arena(pool: &mut UploadPool, packed: &PooledPacked, vert_bytes: u64, index_bytes: u64) {- let arena = &mut pool.arenas[packed.arena];+ // Empty groups reserve nothing — their slice is a placeholder naming arena 0 at+ // offset 0. Advancing one would reset a live arena's cursor to zero and hand its+ // ranges back out to the next tile, stomping live draws. Only committed bytes+ // move a cursor.+ if vert_bytes == 0 && index_bytes == 0 {+ return;+ }+ let Some(arena) = pool.arenas.get_mut(packed.arena) else {+ return;+ }; arena.used_verts = packed.vert_offset + vert_bytes; arena.used_indices = packed.index_offset + index_bytes; }@@ -318,6 +312,13 @@ fn advance_arena(pool: &mut UploadPool, packed: &PooledPacked, vert_bytes: u64, /// Returns `None` for an empty group (no reservation, no slices) and when the group /// fits nowhere even after growth — the caller falls back for the whole tile. Growth /// counts 2 into `Step::UploadAllocs` (one vertex + one index arena).+///+/// The cursor moves when the range is handed out, not after its bytes land: the three+/// groups reserve in turn against one pool, so a later group must see the earlier+/// group's range as taken even though nothing is committed yet. Reserving without+/// advancing would hand every group the same range and each commit would stomp the+/// previous group's bytes. A later commit failure abandons the range (never reused,+/// never drawn — the tile falls back to fresh), exactly as `commit_bytes` documents. fn reserve_group( renderer: &mut Renderer, meshes: &[(&[f32], &[u32])],@@ -326,12 +327,26 @@ fn reserve_group( if bytes == (0, 0) { return None; }- if let Some(reserved) = try_reserve(&renderer.upload_pool, meshes, bytes.0, bytes.1) {+ // A group larger than one arena never fits: fall back rather than splitting draws+ // across arenas, which would complicate every bind site for a case (a single mesh+ // group over 8 MiB) that never occurs.+ if bytes.0 > VERTEX_ARENA_BYTES || bytes.1 > VERTEX_ARENA_BYTES / 2 {+ return None;+ }+ if let Some(reserved) = try_fit(&renderer.upload_pool, meshes, bytes.0, bytes.1) {+ advance_arena(&mut renderer.upload_pool, &reserved, bytes.0, bytes.1); return Some(reserved); }+ if renderer.upload_pool.arenas.len() >= MAX_ARENAS {+ return None;+ } grow_arenas(renderer).ok()?; UploadPool::record_allocs(renderer, 2);- try_reserve(&renderer.upload_pool, meshes, bytes.0, bytes.1)+ // The new arena is empty, so the size check above guarantees the fit; no earlier+ // arena fit, so this names the new one.+ let reserved = try_fit(&renderer.upload_pool, meshes, bytes.0, bytes.1)?;+ advance_arena(&mut renderer.upload_pool, &reserved, bytes.0, bytes.1);+ Some(reserved) } /// Commit one tile's three packed groups into the pool.@@ -390,7 +405,10 @@ pub(super) unsafe fn commit_tile_groups( }), (_, r) => r, }?;- // Pack the bytes exactly as `upload_packed` does, then commit each non-empty group.+ // Pack the bytes exactly as `upload_packed` does, then commit each non-empty+ // group into its reserved range. The cursors already moved at reserve time, so+ // there is nothing to advance here — a failed commit abandons its range (the+ // tile falls back to fresh) exactly as `commit_bytes` documents. let (fv, fi) = pack_group(fill::FLOATS_PER_VERTEX, groups.flat); let (lv, li) = pack_group(stroke::FLOATS_PER_VERTEX, groups.lines); let (rv, ri) = pack_group(ribbon::FLOATS_PER_VERTEX, groups.ribbons);@@ -403,24 +421,6 @@ pub(super) unsafe fn commit_tile_groups( if !rv.is_empty() && !commit_bytes(renderer, &ribbons, &rv, &ri) { return None; }- advance_arena(- &mut renderer.upload_pool,- &flat,- fv.len() as u64 * 4,- fi.len() as u64 * 4,- );- advance_arena(- &mut renderer.upload_pool,- &lines,- lv.len() as u64 * 4,- li.len() as u64 * 4,- );- advance_arena(- &mut renderer.upload_pool,- &ribbons,- rv.len() as u64 * 4,- ri.len() as u64 * 4,- ); Some(TileArenaSlices { flat, lines, ribbons }) }
library/map/src/main/rust/src/vulkan/renderer/upload_tail.rs
diff --git a/library/map/src/main/rust/src/vulkan/renderer/upload_tail.rs b/library/map/src/main/rust/src/vulkan/renderer/upload_tail.rsindex 9281e0db08..d1b5998e6c 100644--- a/library/map/src/main/rust/src/vulkan/renderer/upload_tail.rs+++ b/library/map/src/main/rust/src/vulkan/renderer/upload_tail.rs@@ -122,14 +122,28 @@ pub(super) fn build_layers( } /// Build the region-mask slices from the flat pool's `firstIndex` list.+///+/// Empty regions never reach the pool (`upload()` skips them when packing), so the+/// `*_at` index runs over non-empty regions only — a parallel cursor like+/// `build_carriageways`, never the enumeration position. Indexing by position was the+/// FindFamily SIGABRT: the first empty segment made `*_at` shorter than the mesh list+/// and `[0]` landed on an empty vec. pub(super) fn build_regions( mesh: &TileMesh, flat_region_at: &[usize], flat_first: &[u32], ) -> Vec<RegionBuffers> { let mut regions = Vec::with_capacity(mesh.regions.len());- for (n, region) in mesh.regions.iter().enumerate() {- let Some(&first_index) = flat_first.get(flat_region_at[n]) else {+ let mut ri = 0usize;+ for region in &mesh.regions {+ if region.indices.is_empty() {+ continue;+ }+ let Some(&at) = flat_region_at.get(ri) else {+ continue;+ };+ ri += 1;+ let Some(&first_index) = flat_first.get(at) else { continue; }; regions.push(RegionBuffers {@@ -145,17 +159,25 @@ pub(super) fn build_regions( } /// Build the live-traffic slices from the line pool's `firstIndex` list.+///+/// Same cursor contract as [`build_regions`]: empty segments are skipped at pack time,+/// so the lookup runs over non-empty segments only. pub(super) fn build_traffic( mesh: &TileMesh, line_traffic_at: &[usize], line_first: &[u32], ) -> Vec<TrafficBuffers> { let mut traffic = Vec::with_capacity(mesh.traffic.len());- for (n, segment) in mesh.traffic.iter().enumerate() {+ let mut ti = 0usize;+ for segment in &mesh.traffic { if segment.indices.is_empty() { continue; }- let Some(&first_index) = line_first.get(line_traffic_at[n]) else {+ let Some(&at) = line_traffic_at.get(ti) else {+ continue;+ };+ ti += 1;+ let Some(&first_index) = line_first.get(at) else { continue; }; traffic.push(TrafficBuffers {