metal : add checks for buffer size (#1706)

Co-authored-by: Spencer Sutton <Spencer.Sutton@precisely.com>
This commit is contained in:
Spencer Sutton 2023-06-05 23:28:17 -04:00 committed by GitHub
parent f4c55d3bd7
commit 590250f7a9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 7 deletions

View file

@ -204,6 +204,11 @@ bool ggml_metal_add_buffer(
ctx->buffers[ctx->n_buffers].name = name;
ctx->buffers[ctx->n_buffers].data = data;
ctx->buffers[ctx->n_buffers].size = size;
if (ctx->device.maxBufferLength < aligned_size) {
fprintf(stderr, "%s: buffer '%s' size %zu is larger than buffer maximum of %zu\n", __func__, name, aligned_size, ctx->device.maxBufferLength);
return false;
}
ctx->buffers[ctx->n_buffers].metal = [ctx->device newBufferWithBytesNoCopy:data length:aligned_size options:MTLResourceStorageModeShared deallocator:nil];
if (ctx->buffers[ctx->n_buffers].metal == nil) {