Skip to content

Commit

Permalink
rpc : prevent crashes on invalid input
Browse files Browse the repository at this point in the history
Add more checks which prevent RPC server from crashing if invalid input
is received from client
  • Loading branch information
rgerganov committed Aug 15, 2024
1 parent 98a532d commit 1e37894
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion ggml/src/ggml-rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1098,13 +1098,23 @@ static void rpc_serve_client(ggml_backend_t backend, sockfd_t sockfd, size_t fre
if (!recv_data(sockfd, &cmd, 1)) {
break;
}
if (cmd > GET_DEVICE_MEMORY) {
// fail fast if the command is invalid
fprintf(stderr, "Unknown command: %d\n", cmd);
break;
}
std::vector<uint8_t> input;
std::vector<uint8_t> output;
uint64_t input_size;
if (!recv_data(sockfd, &input_size, sizeof(input_size))) {
break;
}
input.resize(input_size);
try {
input.resize(input_size);
} catch (const std::bad_alloc & e) {
fprintf(stderr, "Failed to allocate input buffer of size %" PRIu64 "\n", input_size);
break;
}
if (!recv_data(sockfd, input.data(), input_size)) {
break;
}
Expand Down Expand Up @@ -1203,8 +1213,10 @@ void start_rpc_server(ggml_backend_t backend, const char * endpoint, size_t free
return;
}
printf("Accepted client connection, free_mem=%zu, total_mem=%zu\n", free_mem, total_mem);
fflush(stdout);
rpc_serve_client(backend, client_socket->fd, free_mem, total_mem);
printf("Client connection closed\n");
fflush(stdout);
}
#ifdef _WIN32
WSACleanup();
Expand Down

0 comments on commit 1e37894

Please sign in to comment.