[MG_Backend, MG_IntegrationTest] (P7): a uniform range that ends mid-word is copied, not refused

`uniform-buffer-byte-tail@P7` is retired. It was the live boundary the Redmi
Minecraft run reported (notes/p5f/magma-inproc-fix.md #5) and, since wave 0, a
named Fatal that took the session with it.

Only the SIZE of a uniform range can land off a word. glBindBufferRange already
forces the offset onto GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT and records
GL_INVALID_VALUE otherwise (GL_Buffer.cpp ValidateBufferRangeOffsetAndSize), and
every device reports that as a multiple of four - but GL 4.6 core 6.1.1 puts no
such rule on size, so a 19-byte bind is legal and 19 % 4 is not 0. The sub-word
start is only reachable protocol-side; the new arm handles it anyway rather than
leaving a second shape to trip over later.

CopyWireBufferSubWordRangeToSlice rounds the read of the APPLICATION's store OUT
to whole words, clamped to the store's end so the widened window can never touch
a byte the application does not own, and lands it in a transient staging slice.
Only the staging -> padded-block shift is sub-word, and that happens inside our
own arena; vkCmdCopyBuffer places no alignment rule on a region's offsets or size
(unlike vkCmdUpdateBuffer / vkCmdFillBuffer), so the shift is a plain legal copy.
The block still starts at the padded slice's own offset, which is what lets the
descriptor keep a minUniformBufferOffsetAlignment-aligned dynamic offset.

red-once (R-16), all three Magma two-process arms, before the backend half:

    DirectVulkan.Split.Buffers.F1WireScenario.SubWordUniformBufferRangePadsMissingBytes
    DirectVulkan.Spawn.Buffers.F1WireScenario.SubWordUniformBufferRangePadsMissingBytes
    DirectVulkan.Tcp.Buffers.F1WireScenario.SubWordUniformBufferRangePadsMissingBytes
        Subprocess aborted - MGPipe: Fatal{UnmigratedVerb, "Magma:uniform-buffer-byte-tail@P7"}

and green after it. The case is the sibling of
ShortUniformBufferRangePadsMissingBytes with a 19-byte range; the byte just past
the range is poisoned, so a rounding that leaks it reads tail.x as 0.5f and the
red channel comes back 127 instead of 0.

Lanes: integration-magma-split 74 (was 73), -spawn 53 (52), -tcp 55 (54),
integration-magma-buffers 20 (19), all green. fatal_census unchanged at 79 abort
sites / 43 family words - these refusals stopped being bare aborts in wave 0, so
a retirement moves no census number and needs no re-baseline. link_ratchet
unchanged at 186.
This commit is contained in:
2026-09-22 16:22:40 -04:00
parent cf7ca59fab
commit 450d30759e
5 changed files with 147 additions and 4 deletions
@@ -2391,15 +2391,30 @@ namespace MobileGL::MG_Backend::DirectVulkan {
// inside the GL binding range. A readback here could retire the command buffer
// whose value BindProgramUniformBuffers is currently holding.
const VkDeviceSize copied = std::min(available, blockSize);
if (bound && ((start & 3u) != 0 || ((source.offset + start) & 3u) != 0 || (copied & 3u) != 0))
WireDescriptorFatal("uniform-buffer-byte-tail@P7");
static thread_local Vector<Uint8> zero;
zero.assign(static_cast<SizeT>(blockSize), 0);
BufferSlice padded{};
if (!m_bufferManager->UploadTransient(BufferKind::Uniform, m_wireFrameIndex, zero.data(), blockSize,
std::max<VkDeviceSize>(4, m_minDynamicOffsetAlignment), padded) || !padded.IsValid()) return false;
if (bound && copied != 0 &&
!m_bufferManager->CopyWireBufferRangeToSlice(range.Res, start, copied, padded)) return false;
if (bound && copied != 0) {
// P7 A.1 retires `uniform-buffer-byte-tail@P7`. The window is word-aligned at both
// ends on nearly every bind - glBindBufferRange already forces the offset onto
// GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT, and that is a multiple of four on every
// device - but GL 4.6 core 6.1.1 puts no such rule on the SIZE, so `copied` is free
// to end mid-word and the Redmi Minecraft run lands there
// (notes/p5f/magma-inproc-fix.md #5). An unaligned start is not reachable through
// the public API and only a protocol-level offset can produce one; the sub-word arm
// handles it anyway rather than leaving a second shape to trip over later. Both arms
// copy the same bytes to the same place: the sub-word one pays one extra transient
// slice and one extra region copy, and nothing outside [start, start + copied)
// reaches the block, which is what makes the padded zeros the visible tail.
const Bool wordWindow = ((start | copied) & 3u) == 0;
if (!(wordWindow
? m_bufferManager->CopyWireBufferRangeToSlice(range.Res, start, copied, padded)
: m_bufferManager->CopyWireBufferSubWordRangeToSlice(range.Res, start, copied,
m_wireFrameIndex, padded, 0)))
return false;
}
if (padded.offset > std::numeric_limits<Uint32>::max())
WireDescriptorFatal("uniform-buffer-dynamic-offset@P7");
out.directBindable = true;
@@ -353,6 +353,59 @@ namespace MobileGL::MG_Backend::DirectVulkan {
return true;
}
Bool VkBufferManager::CopyWireBufferSubWordRangeToSlice(MG_Pipe::MGPipeHandle res, Uint64 offset,
Uint64 size, Uint32 frameIndex,
const BufferSlice& dst, Uint64 dstSkip) {
auto* resource = FindWireBuffer(res);
if (!resource || offset > resource->size || size > resource->size - offset) return false;
if (dstSkip > dst.size || size > dst.size - dstSkip) return false;
if (size == 0) return true;
if (!m_copyProvider || dst.buffer == VK_NULL_HANDLE) return false;
// Round the read of the application's store OUT to whole words. The tail can only be
// clipped by the end of the store itself, and [offset, offset + size) already fits
// inside it, so the widened window still covers every byte the caller asked for.
const Uint64 alignedOffset = offset & ~Uint64{3};
const Uint64 stagedEnd = std::min<Uint64>((offset + size + 3) & ~Uint64{3}, resource->size);
const Uint64 headPad = offset - alignedOffset;
const Uint64 stagedSize = stagedEnd - alignedOffset;
if (stagedSize < headPad + size) return false;
// Uninitialized on purpose: the widening copy below writes every byte of it. The
// arena can grow to satisfy this, which parks (never frees) the buffer `dst` may
// already name - see BufferArena::EnsureCapacity - so the two slices are allowed to
// sit in different VkBuffers and the copy names each slice's own handle.
BufferSlice staging{};
if (!m_transientUploadArena.Allocate(frameIndex, stagedSize, 4, staging) || !staging.IsValid())
return false;
const VkCommandBuffer commands = m_copyProvider->AcquireBufferCopyCommandBuffer();
if (commands == VK_NULL_HANDLE) return false;
VkMemoryBarrier before{VK_STRUCTURE_TYPE_MEMORY_BARRIER};
before.srcAccessMask = VK_ACCESS_MEMORY_WRITE_BIT | VK_ACCESS_HOST_WRITE_BIT;
before.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT | VK_ACCESS_TRANSFER_WRITE_BIT;
vkCmdPipelineBarrier(commands, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT | VK_PIPELINE_STAGE_HOST_BIT,
VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 1, &before, 0, nullptr, 0, nullptr);
const VkBufferCopy widen{alignedOffset, staging.offset, stagedSize};
vkCmdCopyBuffer(commands, resource->buffer.GetHandle(), staging.buffer, 1, &widen);
// The shift reads what the widening copy just wrote, so it needs its own edge even
// though both halves are transfers on one command buffer.
VkMemoryBarrier staged{VK_STRUCTURE_TYPE_MEMORY_BARRIER};
staged.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
staged.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT;
vkCmdPipelineBarrier(commands, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
0, 1, &staged, 0, nullptr, 0, nullptr);
const VkBufferCopy shift{staging.offset + headPad, dst.offset + dstSkip, size};
vkCmdCopyBuffer(commands, staging.buffer, dst.buffer, 1, &shift);
VkMemoryBarrier after{VK_STRUCTURE_TYPE_MEMORY_BARRIER};
after.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
after.dstAccessMask = VK_ACCESS_MEMORY_READ_BIT | VK_ACCESS_MEMORY_WRITE_BIT;
vkCmdPipelineBarrier(commands, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
0, 1, &after, 0, nullptr, 0, nullptr);
resource->lastUseSerial = m_frameSerial;
return true;
}
void VkBufferManager::ReadbackWireBuffer(MG_Pipe::MGPipeHandle res, Uint64 offset, Uint64 size) {
Vector<Uint8> bytes(static_cast<SizeT>(size));
if (!ReadWireBuffer(res, offset, size, bytes.data())) {
@@ -113,6 +113,17 @@ namespace MobileGL::MG_Backend::DirectVulkan {
Bool ReadWireBuffer(MG_Pipe::MGPipeHandle res, Uint64 offset, Uint64 size, void* dst);
Bool CopyWireBufferRangeToSlice(MG_Pipe::MGPipeHandle res, Uint64 offset, Uint64 size,
const BufferSlice& dst);
// P7 A.1, the sub-word half of the one above: the same copy for a window whose start
// or end does not land on a four-byte boundary. The read of the APPLICATION's store is
// rounded OUT to whole words and clamped to the store's end, so the widened window can
// never touch a byte the application does not own; it lands in a transient staging
// slice, and only the staging -> dst shift is sub-word - inside our own arena, where an
// unaligned region cannot alias anything else. vkCmdCopyBuffer places no alignment rule
// on a region's offsets or size (unlike vkCmdUpdateBuffer / vkCmdFillBuffer), so the
// shift is a plain legal copy. `dstSkip` is the byte inside `dst` the window starts at.
// This retires `uniform-buffer-byte-tail@P7`.
Bool CopyWireBufferSubWordRangeToSlice(MG_Pipe::MGPipeHandle res, Uint64 offset, Uint64 size,
Uint32 frameIndex, const BufferSlice& dst, Uint64 dstSkip);
void MarkWireBufferGpuWritten(MG_Pipe::MGPipeHandle res, Uint64 offset, Uint64 size);
// Resource-op entry points. All run on the server apply owner.
@@ -2705,6 +2705,7 @@ if (MOBILEGL_BUILD_DISAGGREGATED)
F1WireScenario.EnabledVertexBufferDrawKeepsNamedP7Fatal
F1WireScenario.UniformBufferRangeAndRebindPixels
F1WireScenario.ShortUniformBufferRangePadsMissingBytes
F1WireScenario.SubWordUniformBufferRangePadsMissingBytes
F1WireScenario.ComputeWrittenVertexAndIndexBuffersDrawAndReadBack
F1WireScenario.ColorBlitToDefaultIgnoresViewportAndPreservesOrientation
F1WireScenario.ColorBlitToDefaultHonorsScissorAndReversedRect
@@ -915,6 +915,69 @@ void main() { color = value + tail; }
glDeleteProgram(program);
}
TEST_F(F1WireScenario, SubWordUniformBufferRangePadsMissingBytes) {
if (!Ready()) return;
// The sibling of ShortUniformBufferRangePadsMissingBytes whose bound range does NOT
// end on a four-byte boundary. That window is what `uniform-buffer-byte-tail@P7`
// used to refuse (a named Fatal that took the whole session with it), and it is the
// live boundary the Redmi Minecraft run reported (notes/p5f/magma-inproc-fix.md #5).
//
// Only the SIZE can land off a word here. glBindBufferRange constrains the offset to
// GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT and records GL_INVALID_VALUE otherwise
// (MG_Impl/GLImpl/Buffer/GL_Buffer.cpp ValidateBufferRangeOffsetAndSize), so an odd
// offset never reaches the backend through the public API - but GL 4.6 core 6.1.1
// puts no such rule on size, which is why 19 is a legal bind and 19 % 4 is not 0.
if (Gl().BackendName() != "DirectVulkan") GTEST_SKIP() << "Magma short-UBO compatibility policy";
Attach(GL_RGBA8);
const char* fragment = R"(#version 430 core
layout(std140, binding=3) uniform ShortColour { vec4 value; vec4 tail; };
layout(location=0) out vec4 color;
void main() { color = value + tail; }
)";
const GLuint program = BuildWireProgram({{GL_VERTEX_SHADER, kWireVertexIdTriangle},
{GL_FRAGMENT_SHADER, fragment}});
ASSERT_NE(program, 0u);
GLint alignment = 0;
glGetIntegerv(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT, &alignment);
ASSERT_GT(alignment, 0);
const size_t offset = size_t(alignment);
const GLsizeiptr boundSize = 19; // 16 bytes of `value` plus three bytes of tail.x
std::vector<GLubyte> bytes(offset + 8 * sizeof(GLfloat), 0);
const GLfloat values[] = {0, 1, 0, 1, 0, 1, 1, 1};
std::memcpy(bytes.data() + offset, values, sizeof(values));
// The byte immediately past the bound range, and the reason this case is sharper than
// its four-byte sibling: rounding the read OUT to a word pulls byte 19 into the staging
// slice, and the block must still end at byte 19. If it leaks, tail.x reads 0.5f and
// the red channel comes back 127 instead of 0.
bytes[offset + 19] = 0x3F;
GLuint ubo = 0, vao = 0;
glGenBuffers(1, &ubo);
glBindBuffer(GL_UNIFORM_BUFFER, ubo);
glBufferData(GL_UNIFORM_BUFFER, GLsizeiptr(bytes.size()), bytes.data(), GL_STATIC_DRAW);
glBindBufferRange(GL_UNIFORM_BUFFER, 3, ubo, GLintptr(offset), boundSize);
ASSERT_EQ(FirstGLError(), GLenum(GL_NO_ERROR)) << "a 19-byte uniform range is a legal bind";
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glUseProgram(program);
glViewport(0, 0, 8, 8);
glDisable(GL_DEPTH_TEST);
glClearColor(1, 0, 0, 1);
glClear(GL_COLOR_BUFFER_BIT);
glDrawArrays(GL_TRIANGLES, 0, 3);
std::array<GLubyte, 4> pixel{};
glReadPixels(4, 4, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, pixel.data());
ASSERT_EQ(FirstGLError(), GLenum(GL_NO_ERROR));
EXPECT_EQ(pixel, (std::array<GLubyte, 4>{0, 255, 0, 255}))
<< "the sub-word uniform range did not pad to the reflected block exactly";
glBindBufferBase(GL_UNIFORM_BUFFER, 3, 0);
glBindBuffer(GL_UNIFORM_BUFFER, 0);
glUseProgram(0);
glBindVertexArray(0);
glDeleteBuffers(1, &ubo);
glDeleteVertexArrays(1, &vao);
glDeleteProgram(program);
}
TEST_F(F1WireScenario, ComputeWrittenVertexAndIndexBuffersDrawAndReadBack) {
if (!Ready()) return;
Attach(GL_RGBA8);