🐛 End the process at Shared heaps and CPU depth textures, as x86 Macs

Guest-visible: an app's newHeapWithDescriptor: with Shared storage now ends
the process with Metal's words ("Heap Descriptor Validation\nShared storage
mode disallowed"), where the bundle made a heap over host memory; and a depth,
stencil or depth-stencil texture in Shared or Managed storage, from the
device's, a heap's or a buffer's texture creators, ends it with Metal's
"Depth, Stencil, DepthStencil textures cannot be allocated with
MTLStorageModeShared or MTLStorageModeManaged on this device.", where the
bundle made one. Neither sends the host anything.

The x86 lane imitates an x86 Mac's GPU, and both oracles, the AMD 5300M and
the Intel 630 under macOS 26.7.1, end the process there: at a Shared heap
(oracle_bundle heapbuffers), at a Depth32Float texture in Shared storage over
a buffer (buftex.depth), and at the device's Depth32Float in Shared or Managed
storage and Stencil8 in Managed (devtex.*), making the Private one. The M3 Max
makes the heap and the texture over a buffer, but is no oracle of this lane.
The heap's region argument is now unused.
This commit is contained in:
Anees Iqbal committed 2026-11-05 21:59:33 -08:00
1 parent 881c8c80b7
commit 0f9108bdfa
7 files changed
+125 -95

No files matched your search

+3
View File
@@ -666,6 +666,9 @@ RGFX_RESOURCE_SPI
offset:(NSUInteger)offset
bytesPerRow:(NSUInteger)bytesPerRow
{
// Where Metal's descriptor validation ends the process, so does the bundle: a depth texture
// over the buffer's bytes, as the x86 Macs end it (rgfxEndAtInvalidTextureDescriptor).
rgfxEndAtInvalidTextureDescriptor(descriptor);
if (descriptor != nil && descriptor.mipmapLevelCount > 1) {
RgfxMetalAssertionFailed(
"_mtlValidateStrideTextureParameters",
+6 -7
View File
@@ -57,15 +57,14 @@ NS_ASSUME_NONNULL_BEGIN
serializer:(nullable RgfxSerializer *)serializer;
#ifdef __cplusplus
/// The heap `newHeapWithDescriptor:` makes: a Shared one over a zeroed block of `region` in
/// Metal's 16 KiB pages (heapOverHostMemoryWithDescriptor:), which the host sees in place, and
/// a heap of any other mode the host's own (heapWithDescriptor:).
/// The heap `newHeapWithDescriptor:` makes: the host's own (heapWithDescriptor:), of any mode
/// but Shared. A Shared one ends the process with Metal's words, sending nothing, as the heap
/// descriptor validation of the AMD 5300M and the Intel 630 under macOS 26.7.1 does (oracle_bundle
/// heapbuffers; the M3 Max makes one); `region` is unused.
///
/// Nil where Metal makes none (measured on the M3 Max, macOS 27.0.1, plain and under
/// MTL_DEBUG_LAYER, Shared and Private, Automatic and Placement): for a size of 0, and for one
/// past `maxLength` (the device's maxBufferLength, at which Metal makes one; 0 holds none
/// back). Nil too where there is no `region`, or it has no room for a Shared one, as Metal
/// makes no Shared heap without its memory, never one whose buffers the GPU does not see.
/// MTL_DEBUG_LAYER, Private, Automatic and Placement): for a size of 0, and for one past
/// `maxLength` (the device's maxBufferLength, at which Metal makes one; 0 holds none back).
+ (nullable instancetype)heapForDeviceWithDescriptor:(nullable MTLHeapDescriptor *)descriptor
maxLength:(NSUInteger)maxLength
region:(nullable RgfxRegion *)region
+7 -22
View File
@@ -125,31 +125,16 @@ MTLSizeAndAlign rgfxHeapTextureSizeAndAlign(RgfxSerializer *serializer,
device:(id<MTLDevice>)device
serializer:(RgfxSerializer *)serializer
{
// The x86 Macs' heap descriptor validation ends the process at a Shared heap (oracle_bundle
// heapbuffers, AMD 5300M and Intel 630, macOS 26.7.1; the M3 Max makes one), sending nothing.
if (descriptor != nil && descriptor.storageMode == MTLStorageModeShared)
RgfxMetalAssertionFailed("-[MTLHeapDescriptorInternal validateWithDevice:]",
@"Heap Descriptor Validation\nShared storage mode disallowed\n");
if (descriptor == nil || descriptor.size == 0 ||
(maxLength != 0 && descriptor.size > maxLength))
return nil;
if (descriptor.storageMode != MTLStorageModeShared)
return [RgfxHeap heapWithDescriptor:descriptor device:device serializer:serializer];
if (region == nullptr || serializer == nullptr)
return nil;
// The heap's memory is the block, on the host too, whole pages of Metal's 16 KiB.
const uint64_t size = static_cast<uint64_t>(descriptor.size);
RgfxRegion::Block block = region->allocate((size + kHeapPage - 1) & ~(kHeapPage - 1), kHeapPage);
if (!block.ok)
return nil;
uint8_t *bytes = const_cast<uint8_t *>(block.ptr);
std::memset(bytes, 0, block.size);
// The block goes back to the region once the host has dropped the heap.
const uint64_t offset = block.offset;
RgfxHeap *heap = [RgfxHeap heapOverHostMemoryWithDescriptor:descriptor
regionOffset:offset
contents:bytes
deallocator:^{ region->release(offset); }
device:device
serializer:serializer];
if (heap == nil)
region->release(offset); // no heap lies over the block
return heap;
(void)region; // no heap the device makes lies in the region: a Shared one ends the process
return [RgfxHeap heapWithDescriptor:descriptor device:device serializer:serializer];
}
+ (instancetype)heapOverHostMemoryWithDescriptor:(MTLHeapDescriptor *)descriptor
+5 -2
View File
@@ -127,8 +127,11 @@ NS_ASSUME_NONNULL_BEGIN
/// newTextureWithDescriptor:, docs/graphics-kext/dgpu-oracle.md, Q12): one of a three-channel
/// format (45 to 48, 95 to 100 and 120 to 122), which Metal's setter takes and Metal describes no
/// format for, and a multisample one of a compressed or 4:2:2 format, or of
/// MTLPixelFormatUnspecialized. The device's and the heaps' texture creators call it before they
/// send the host anything; it returns for any other descriptor.
/// MTLPixelFormatUnspecialized. And, as the AMD 5300M and the Intel 630 under macOS 26.7.1 end it
/// (oracle_bundle buftex.depth and devtex.*: Depth32Float in Shared storage over a buffer, and in
/// Shared or Managed from the device, Stencil8 in Managed; Private passes; the M3 Max makes the
/// one over a buffer), at a depth, stencil or depth-stencil texture in Shared or Managed storage. The device's, the heaps' and a buffer's texture creators
/// call it before they send the host anything; it returns for any other descriptor.
void rgfxEndAtInvalidTextureDescriptor(MTLTextureDescriptor *descriptor);
/// Ends the process, with Metal's words, where `minimumLinearTextureAlignmentForPixelFormat:` ends
+17
View File
@@ -477,6 +477,23 @@ void rgfxEndAtInvalidTextureDescriptor(MTLTextureDescriptor *descriptor)
return;
}
const uint64_t format = descriptor.pixelFormat;
// A depth or stencil texture the CPU reaches: the x86 Macs' validation keeps their textures
// Private (oracle_bundle buftex.depth and devtex.*, AMD 5300M and Intel 630, macOS 26.7.1).
const bool depthOrStencil = format == MTLPixelFormatDepth16Unorm ||
format == MTLPixelFormatDepth32Float ||
format == MTLPixelFormatStencil8 ||
format == MTLPixelFormatDepth24Unorm_Stencil8 ||
format == MTLPixelFormatDepth32Float_Stencil8 ||
format == MTLPixelFormatX32_Stencil8 ||
format == MTLPixelFormatX24_Stencil8;
if (depthOrStencil && (descriptor.storageMode == MTLStorageModeShared ||
descriptor.storageMode == MTLStorageModeManaged)) {
RgfxMetalAssertionFailed(
"-[MTLTextureDescriptorInternal validateWithDevice:]",
@"Texture Descriptor Validation\nMTLTextureDescriptor: Depth, Stencil, DepthStencil "
@"textures cannot be allocated with MTLStorageModeShared or MTLStorageModeManaged on "
@"this device.\n");
}
const MTLTextureType type = descriptor.textureType;
const bool multisample =
type == MTLTextureType2DMultisample || type == MTLTextureType2DMultisampleArray;
+11 -24
View File
@@ -201,9 +201,8 @@ void test_refused_buffer_create_is_nil()
CHECK(r.taken.size() == 4);
}
// A device heap and a Shared one over a region block are nil, the block back; a buffer at an offset
// of a Placement heap, and one an Automatic heap places, are nil, the Automatic heap's bytes
// free again.
// A device heap is nil; a buffer at an offset of a Placement heap, and one an Automatic heap
// places, are nil, the Automatic heap's bytes free again.
void test_refused_heap_create_is_nil()
{
Refusing &r = *new Refusing();
@@ -212,26 +211,13 @@ void test_refused_heap_create_is_nil()
hd.size = 0x10000;
hd.storageMode = MTLStorageModePrivate;
hd.type = MTLHeapTypePlacement;
MTLHeapDescriptor *shared = [hd copy];
shared.storageMode = MTLStorageModeShared;
MTLHeapDescriptor *automatic = [hd copy];
automatic.type = MTLHeapTypeAutomatic;
@autoreleasepool {
r.allow = 2;
r.allow = 1;
CHECK([RgfxHeap heapWithDescriptor:hd device:nil serializer:&r.ser] != nil);
CHECK([RgfxHeap heapForDeviceWithDescriptor:shared
maxLength:0
region:&b.region
device:nil
serializer:&r.ser] != nil);
}
CHECK(b.live.empty());
CHECK([RgfxHeap heapWithDescriptor:hd device:nil serializer:&r.ser] == nil);
CHECK([RgfxHeap heapForDeviceWithDescriptor:shared
maxLength:0
region:&b.region
device:nil
serializer:&r.ser] == nil);
CHECK(b.live.empty());
r.allow = 2;
@@ -250,8 +236,8 @@ void test_refused_heap_create_is_nil()
nil);
CHECK([placing newBufferWithLength:0x1000 options:MTLResourceStorageModePrivate] == nil);
CHECK([placing maxAvailableSizeWithAlignment:1] == room);
CHECK(r.taken == std::vector<uint32_t>({RGFX_CMD_HEAP_CREATE, RGFX_CMD_HEAP_OVER_HOST_MEMORY,
RGFX_CMD_HEAP_CREATE, RGFX_CMD_HEAP_CREATE,
CHECK(r.taken == std::vector<uint32_t>({RGFX_CMD_HEAP_CREATE, RGFX_CMD_HEAP_CREATE,
RGFX_CMD_HEAP_CREATE,
RGFX_CMD_BUFFER_AT_OFFSET,
RGFX_CMD_BUFFER_AT_OFFSET}));
}
@@ -468,11 +454,12 @@ void test_refused_create_gives_its_gpu_address_back()
case 1:
return [RgfxHeap heapWithDescriptor:hd device:nil serializer:&r.ser];
case 2:
return [RgfxHeap heapForDeviceWithDescriptor:shared
maxLength:0
region:&b.region
device:nil
serializer:&r.ser];
return [RgfxHeap heapOverHostMemoryWithDescriptor:shared
regionOffset:RGFX_ALLOCATIONS_OFFSET
contents:b.mem.data() + RGFX_ALLOCATIONS_OFFSET
deallocator:nil
device:nil
serializer:&r.ser];
case 3:
return [RgfxBuffer bufferOverHostMemoryWithContents:b.mem.data() + RGFX_ALLOCATIONS_OFFSET
regionOffset:RGFX_ALLOCATIONS_OFFSET
+76 -40
View File
@@ -530,69 +530,41 @@ void test_managed_buffers_place_their_cpu_copies_in_heaps()
!source.heaped(MTLResourceStorageModePrivate));
}
// The device's heaps answer as Metal's (measured on the M3 Max, macOS 27.0.1, plain and under
// MTL_DEBUG_LAYER: every mode and type made at maxBufferLength, nil a 16 KiB page past it): a
// Shared one lies over a zeroed block of the region in Metal's 16 KiB pages, its buffers the
// block's bytes, and a Private one is the host's heap. None of 0 bytes (plain Metal's nil; its
// validation stops one) or past maxBufferLength, in any mode or type; none where the region has
// no room for a Shared one, or there is no region, as Metal makes no Shared heap without its
// memory (no device heap whose buffers the host would never see). A refusal posts nothing.
// A heap the device makes is the host's, in Private storage, of any type. None of 0 bytes (plain
// Metal's nil; its validation stops one) or past maxBufferLength, in any type. A refusal posts
// nothing. A Shared one ends the process
// (test_shared_heaps_and_cpu_depth_textures_end_the_process).
void test_device_heaps_answer_as_metal()
{
Fixture fx;
std::vector<uint8_t> mem(RGFX_ALLOCATIONS_OFFSET + 0x20000, 0xee);
RgfxRegion region(mem.data(), mem.size());
auto make = [&](uint64_t size, MTLStorageMode mode, MTLHeapType type, NSUInteger maxLength,
RgfxRegion *in) {
auto make = [&](uint64_t size, MTLStorageMode mode, MTLHeapType type, NSUInteger maxLength) {
MTLHeapDescriptor *d = [MTLHeapDescriptor new];
d.size = size;
d.storageMode = mode;
d.type = type;
return [RgfxHeap heapForDeviceWithDescriptor:d
maxLength:maxLength
region:in
region:nullptr
device:nil
serializer:&fx.ser];
};
@autoreleasepool {
RgfxHeap *shared =
make(0x1000, MTLStorageModeShared, MTLHeapTypePlacement, 0x10000, &region);
CHECK(shared != nil);
auto over = expectMessage<RgfxHeapOverHostMemory>(fx.ring, RGFX_CMD_HEAP_OVER_HOST_MEMORY);
uint8_t *bytes = static_cast<uint8_t *>(shared.rgfxContents);
CHECK(bytes >= mem.data() + RGFX_ALLOCATIONS_OFFSET &&
bytes + 0x4000 <= mem.data() + mem.size());
CHECK(over.obj_id == shared.rgfxObjId &&
over.region_offset == static_cast<uint64_t>(bytes - mem.data()));
CHECK(bytes[0] == 0 && bytes[0x3fff] == 0);
id<MTLBuffer> placed = [shared newBufferWithLength:0x100
options:MTLResourceStorageModeShared
offset:0x800];
CHECK(placed != nil && [placed contents] == bytes + 0x800);
(void)expectMessage<RgfxBufferAtOffset>(fx.ring, RGFX_CMD_BUFFER_AT_OFFSET);
RgfxHeap *priv = make(0x1000, MTLStorageModePrivate, MTLHeapTypeAutomatic, 0x10000, &region);
RgfxHeap *priv = make(0x1000, MTLStorageModePrivate, MTLHeapTypeAutomatic, 0x10000);
CHECK(priv != nil && priv.rgfxContents == nullptr);
(void)expectMessage<RgfxHeapCreate>(fx.ring, RGFX_CMD_HEAP_CREATE);
// No limit known: none held back.
RgfxHeap *unlimited = make(0x20000, MTLStorageModePrivate, MTLHeapTypePlacement, 0, &region);
RgfxHeap *unlimited = make(0x20000, MTLStorageModePrivate, MTLHeapTypePlacement, 0);
CHECK(unlimited != nil);
(void)expectMessage<RgfxHeapCreate>(fx.ring, RGFX_CMD_HEAP_CREATE);
CHECK(ringEmpty(fx.ring));
const MTLStorageMode modes[] = { MTLStorageModeShared, MTLStorageModePrivate };
const MTLHeapType types[] = { MTLHeapTypeAutomatic, MTLHeapTypePlacement };
for (MTLStorageMode mode : modes) {
for (MTLHeapType type : types) {
CHECK(make(0, mode, type, 0x10000, &region) == nil);
CHECK(make(0x10001, mode, type, 0x10000, &region) == nil);
}
for (MTLHeapType type : types) {
CHECK(make(0, MTLStorageModePrivate, type, 0x10000) == nil);
CHECK(make(0x10001, MTLStorageModePrivate, type, 0x10000) == nil);
}
// A Shared heap with no region, or too large for what it has left.
CHECK(make(0x1000, MTLStorageModeShared, MTLHeapTypePlacement, 0x10000, nullptr) == nil);
CHECK(make(0x40000, MTLStorageModeShared, MTLHeapTypeAutomatic, 0, &region) == nil);
CHECK(ringEmpty(fx.ring));
} // the heaps go while the region lives
}
drainRing(fx.ring);
}
@@ -1168,6 +1140,44 @@ static int dies_case(const char *name)
status:&status];
(void)[lib newFunctionWithName:@"rgfx_exported"];
}
if (std::strcmp(name, "heap-shared") == 0) {
MTLHeapDescriptor *d = [MTLHeapDescriptor new];
d.size = 0x1000;
d.storageMode = MTLStorageModeShared;
(void)[RgfxHeap heapForDeviceWithDescriptor:d
maxLength:0
region:nullptr
device:nil
serializer:&fx.ser];
}
if (std::strcmp(name, "buffer-texture-depth") == 0) {
// oracle_bundle buftex.depth's: Depth32Float, 64x64, 256 bytes a row, over a Shared buffer.
std::vector<uint8_t> memory(0x10000, 0);
RgfxBuffer *buf =
[RgfxBuffer bufferOverHostMemoryWithContents:memory.data()
regionOffset:0x10000
length:memory.size()
options:MTLResourceStorageModeShared
deallocator:nil
device:nil
serializer:&fx.ser];
MTLTextureDescriptor *desc =
[MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatDepth32Float
width:64
height:64
mipmapped:NO];
desc.storageMode = MTLStorageModeShared;
(void)[buf newTextureWithDescriptor:desc offset:0 bytesPerRow:256];
}
if (std::strcmp(name, "texture-stencil-managed") == 0) {
MTLTextureDescriptor *desc =
[MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatStencil8
width:64
height:64
mipmapped:NO];
desc.storageMode = MTLStorageModeManaged;
rgfxEndAtInvalidTextureDescriptor(desc);
}
const NSRange one = NSMakeRange(0, 1);
if (std::strcmp(name, "view-no-format") == 0)
viewOf(MTLPixelFormatInvalid, MTLTextureType2DArray, one, one, false);
@@ -1272,6 +1282,31 @@ void test_buffer_texture_types_end_the_process()
}
}
// A Shared heap the device is asked for, and a depth or stencil texture the CPU reaches (over a
// Shared buffer's bytes, or of a Managed descriptor), end the process with Metal's words before
// the host is sent anything, as the heap and texture descriptor validation of the AMD 5300M and
// the Intel 630 under macOS 26.7.1 does (oracle_bundle heapbuffers, buftex.depth and devtex.*).
// A depth texture in Private storage passes, as there.
void test_shared_heaps_and_cpu_depth_textures_end_the_process()
{
checkDies("heap-shared", "-[MTLHeapDescriptorInternal validateWithDevice:]: failed assertion "
"`Heap Descriptor Validation\nShared storage mode disallowed\n'");
const char *const kDepth =
"-[MTLTextureDescriptorInternal validateWithDevice:]: failed assertion `Texture Descriptor "
"Validation\nMTLTextureDescriptor: Depth, Stencil, DepthStencil textures cannot be allocated "
"with MTLStorageModeShared or MTLStorageModeManaged on this device.\n'";
checkDies("buffer-texture-depth", kDepth);
checkDies("texture-stencil-managed", kDepth);
MTLTextureDescriptor *priv =
[MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatDepth32Float
width:64
height:64
mipmapped:NO];
priv.storageMode = MTLStorageModePrivate;
rgfxEndAtInvalidTextureDescriptor(priv);
g_checks++;
}
// minimumLinearTextureAlignmentForPixelFormat: ends the process with Metal's words where both x86
// Macs' Metal does (oracle_caps formats: compressed and 4:2:2, depth and stencil, and the formats
// the device lacks, named Invalid), and answers every other format, those of 256 and those no
@@ -12294,6 +12329,7 @@ int main(int argc, char **argv)
test_device_buffers_take_pages_of_slabs();
test_managed_buffers_place_their_cpu_copies_in_heaps();
test_device_heaps_answer_as_metal();
test_shared_heaps_and_cpu_depth_textures_end_the_process();
test_second_buffer_is_monotonic();
test_wrapper_guards_and_base();
test_event_routing();