@@ -723,6 +723,12 @@ ComputeQueue::KernelDispatchAqlToPm4(char *cpu, hsa_kernel_dispatch_packet_t *pa
723723 else
724724 i += cmd_util.BuildWriteData64Command (cpu + i, (uint64_t *)ring_rptr, cmdbuf_aql_frame_write_index + 1 );
725725
726+ // Check if we exceeded the frame size
727+ if ((i - ib_size) > cmdbuf_aql_frame_size) {
728+ pr_err (" PM4 command buffer overflow in KernelDispatch: used %d bytes, limit %d bytes\n " , i - ib_size, cmdbuf_aql_frame_size);
729+ return HSA_STATUS_ERROR_OUT_OF_RESOURCES;
730+ }
731+
726732 ib_size = i;
727733 cmdbuf_aql_frame_write_index++;
728734 packet->header = HSA_PACKET_TYPE_INVALID;
@@ -811,6 +817,12 @@ ComputeQueue::BarrierGenericAqlToPm4(char *cpu, hsa_barrier_and_packet_t *packet
811817 else
812818 i += cmd_util.BuildWriteData64Command (cpu + i, (uint64_t *)ring_rptr, cmdbuf_aql_frame_write_index + 1 );
813819
820+ // Check if we exceeded the frame size
821+ if ((i - ib_size) > cmdbuf_aql_frame_size) {
822+ pr_err (" PM4 command buffer overflow in BarrierGeneric: used %d bytes, limit %d bytes\n " , i - ib_size, cmdbuf_aql_frame_size);
823+ return HSA_STATUS_ERROR_OUT_OF_RESOURCES;
824+ }
825+
814826 ib_size = i;
815827 cmdbuf_aql_frame_write_index++;
816828 packet->header = HSA_PACKET_TYPE_INVALID;
@@ -881,6 +893,12 @@ hsa_status_t ComputeQueue::VendorSpecificAqlToPm4(char *cpu, amd_aql_pm4_ib *pac
881893 else
882894 i += cmd_util.BuildWriteData64Command (cpu + i, (uint64_t *)ring_rptr, cmdbuf_aql_frame_write_index + 1 );
883895
896+ // Check if we exceeded the frame size
897+ if ((i - ib_size) > cmdbuf_aql_frame_size) {
898+ pr_err (" PM4 command buffer overflow in VendorSpecific: used %d bytes, limit %d bytes\n " , i - ib_size, cmdbuf_aql_frame_size);
899+ return HSA_STATUS_ERROR_OUT_OF_RESOURCES;
900+ }
901+
884902 ib_size = i;
885903 cmdbuf_aql_frame_write_index++;
886904 packet->header = HSA_PACKET_TYPE_INVALID;
0 commit comments