| 982 | } |
| 983 | |
| 984 | ggml_tensor * rpc_server::deserialize_tensor(struct ggml_context * ctx, const rpc_tensor * tensor) { |
| 985 | // Validate tensor type before using it |
| 986 | if (tensor->type >= GGML_TYPE_COUNT) { |
| 987 | GGML_LOG_ERROR("[%s] invalid tensor type received: %u\n", __func__, tensor->type); |
| 988 | return nullptr; |
| 989 | } |
| 990 | |
| 991 | // Fix: Prevent division by zero if blck_size is 0 (e.g., deprecated types) |
| 992 | if (ggml_blck_size((enum ggml_type)tensor->type) == 0) { |
| 993 | GGML_LOG_ERROR("[%s] invalid tensor type received (blck_size is 0): %u\n", __func__, tensor->type); |
| 994 | return nullptr; |
| 995 | } |
| 996 | |
| 997 | ggml_tensor * result = ggml_new_tensor_4d(ctx, (ggml_type) tensor->type, |
| 998 | tensor->ne[0], tensor->ne[1], tensor->ne[2], tensor->ne[3]); |
| 999 | |
| 1000 | // ggml_new_tensor_4d might fail if dimensions are invalid, although less likely to crash than invalid type |
| 1001 | if (result == nullptr) { |
| 1002 | GGML_LOG_ERROR("[%s] ggml_new_tensor_4d failed for type %u\n", __func__, tensor->type); |
| 1003 | return nullptr; |
| 1004 | } |
| 1005 | |
| 1006 | for (uint32_t i = 0; i < GGML_MAX_DIMS; i++) { |
| 1007 | result->nb[i] = tensor->nb[i]; |
| 1008 | } |
| 1009 | result->buffer = reinterpret_cast<ggml_backend_buffer_t>(tensor->buffer); |
| 1010 | if (result->buffer && buffers.find(result->buffer) == buffers.end()) { |
| 1011 | result->buffer = nullptr; |
| 1012 | } |
| 1013 | |
| 1014 | if (result->buffer) { |
| 1015 | // require that the tensor data does not go beyond the buffer end |
| 1016 | uint64_t tensor_size = (uint64_t) ggml_nbytes(result); |
| 1017 | uint64_t buffer_start = (uint64_t) ggml_backend_buffer_get_base(result->buffer); |
| 1018 | uint64_t buffer_size = (uint64_t) ggml_backend_buffer_get_size(result->buffer); |
| 1019 | GGML_ASSERT(tensor->data + tensor_size >= tensor->data); // check for overflow |
| 1020 | GGML_ASSERT(tensor->data >= buffer_start && tensor->data + tensor_size <= buffer_start + buffer_size); |
| 1021 | } |
| 1022 | |
| 1023 | result->op = (ggml_op) tensor->op; |
| 1024 | for (uint32_t i = 0; i < GGML_MAX_OP_PARAMS / sizeof(int32_t); i++) { |
| 1025 | result->op_params[i] = tensor->op_params[i]; |
| 1026 | } |
| 1027 | result->flags = tensor->flags; |
| 1028 | result->data = reinterpret_cast<void *>(tensor->data); |
| 1029 | ggml_set_name(result, tensor->name); |
| 1030 | return result; |
| 1031 | } |
| 1032 | |
| 1033 | |
| 1034 | bool rpc_server::set_tensor(const std::vector<uint8_t> & input) { |
nothing calls this directly
no test coverage detected