This commit is contained in:
2025-11-08 13:25:17 +01:00
parent 7846607651
commit 7f3aa901f1
2 changed files with 59 additions and 8 deletions
+6 -2
View File
@@ -324,7 +324,8 @@ static int get_chunk_server(TinyDFS *tdfs, Address *addrs, int num_addrs, ByteQu
int conn_idx = tcp_index_from_tag(&tdfs->tcp, found); int conn_idx = tcp_index_from_tag(&tdfs->tcp, found);
assert(conn_idx > -1); assert(conn_idx > -1);
*output = tcp_output_buffer(&tdfs->tcp, conn_idx); if (output)
*output = tcp_output_buffer(&tdfs->tcp, conn_idx);
} }
return found; return found;
@@ -1601,7 +1602,10 @@ static void process_event_for_write(TinyDFS *tdfs,
} }
for (int i = 0; i < num_upload_results; i++) { for (int i = 0; i < num_upload_results; i++) {
upload_results[i].old_hash = tdfs->operations[opidx].hashes[i]; if (i < tdfs->operations[opidx].num_hashes)
upload_results[i].old_hash = tdfs->operations[opidx].hashes[i];
else
memset(&upload_results[i].old_hash, 0, sizeof(SHA256));
upload_results[i].num_locations = 0; upload_results[i].num_locations = 0;
} }
+53 -6
View File
@@ -1,3 +1,4 @@
#include "tcp.h"
#ifdef BUILD_TEST #ifdef BUILD_TEST
#include <assert.h> #include <assert.h>
@@ -213,11 +214,22 @@ static struct timespec simulated_time = {0, 0};
#define SOCKET_ERROR_PIPE EPIPE #define SOCKET_ERROR_PIPE EPIPE
#endif #endif
static int count_non_empty_desc(Process *p)
{
int n = 0;
for (int i = 0; i < MAX_DESCRIPTORS; i++)
if (p->desc[i].type != DESC_EMPTY)
n++;
return n;
}
#define CHECK_NON_EMPTY_DESC_INVARIANT assert(count_non_empty_desc(current_process) == current_process->num_desc);
static void process_poll_array(Process *process, static void process_poll_array(Process *process,
void **contexts, struct pollfd *polled, int num_polled) void **contexts, struct pollfd *polled, int num_polled)
{ {
for (int i = 0, j = 0; j < process->num_desc; i++) { for (int i = 0, j = 0; j < process->num_desc; i++) {
assert(i < MAX_DESCRIPTORS);
Descriptor *desc = &process->desc[i]; Descriptor *desc = &process->desc[i];
if (desc->type == DESC_EMPTY) if (desc->type == DESC_EMPTY)
continue; continue;
@@ -570,6 +582,7 @@ void update_simulation(void)
for (int j = 0, k = 0; k < current_process->num_desc; j++) { for (int j = 0, k = 0; k < current_process->num_desc; j++) {
assert(j < MAX_DESCRIPTORS);
Descriptor *desc = &current_process->desc[j]; Descriptor *desc = &current_process->desc[j];
if (desc->type == DESC_EMPTY) if (desc->type == DESC_EMPTY)
continue; continue;
@@ -645,15 +658,33 @@ void update_simulation(void)
switch (current_process->type) { switch (current_process->type) {
case PROCESS_TYPE_METADATA_SERVER: case PROCESS_TYPE_METADATA_SERVER:
printf("metadata_server_step\n"); printf("metadata_server_step\n");
num_polled = metadata_server_step(&current_process->metadata_server, contexts, polled, num_polled, &timeout); num_polled = metadata_server_step(
&current_process->metadata_server,
contexts,
polled,
num_polled,
&timeout
);
break; break;
case PROCESS_TYPE_CHUNK_SERVER: case PROCESS_TYPE_CHUNK_SERVER:
printf("chunk_server_step\n"); printf("chunk_server_step\n");
num_polled = chunk_server_step(&current_process->chunk_server, contexts, polled, num_polled, &timeout); num_polled = chunk_server_step(
&current_process->chunk_server,
contexts,
polled,
num_polled,
&timeout
);
break; break;
case PROCESS_TYPE_CLIENT: case PROCESS_TYPE_CLIENT:
//printf("simulation_client_step\n"); //printf("simulation_client_step\n");
num_polled = simulation_client_step(&current_process->simulation_client, contexts, polled, num_polled, &timeout); num_polled = simulation_client_step(
&current_process->simulation_client,
contexts,
polled,
num_polled,
&timeout
);
break; break;
} }
@@ -785,8 +816,10 @@ SOCKET mock_socket(int domain, int type, int protocol)
} }
int idx = 0; int idx = 0;
while (current_process->desc[idx].type != DESC_EMPTY) while (current_process->desc[idx].type != DESC_EMPTY) {
idx++; idx++;
assert(idx < MAX_DESCRIPTORS);
}
Descriptor *desc = &current_process->desc[idx]; Descriptor *desc = &current_process->desc[idx];
desc->type = DESC_SOCKET; desc->type = DESC_SOCKET;
@@ -796,6 +829,8 @@ SOCKET mock_socket(int domain, int type, int protocol)
desc->address = (DescriptorAddress) { .type=DESC_ADDR_VOID }; desc->address = (DescriptorAddress) { .type=DESC_ADDR_VOID };
current_process->num_desc++; current_process->num_desc++;
CHECK_NON_EMPTY_DESC_INVARIANT;
return (SOCKET) idx; return (SOCKET) idx;
} }
@@ -908,8 +943,10 @@ SOCKET mock_accept(SOCKET fd, void *addr, socklen_t *addr_len)
return INVALID_SOCKET; return INVALID_SOCKET;
} }
int new_idx = 0; int new_idx = 0;
while (current_process->desc[new_idx].type != DESC_EMPTY) while (current_process->desc[new_idx].type != DESC_EMPTY) {
new_idx++; new_idx++;
assert(new_idx < MAX_DESCRIPTORS);
}
Descriptor *new_desc = &current_process->desc[new_idx]; Descriptor *new_desc = &current_process->desc[new_idx];
new_desc->type = DESC_CONNECTION_SOCKET; new_desc->type = DESC_CONNECTION_SOCKET;
new_desc->events = 0; new_desc->events = 0;
@@ -926,6 +963,7 @@ SOCKET mock_accept(SOCKET fd, void *addr, socklen_t *addr_len)
data_queue_init(&peer->output_data, DATA_QUEUE_SIZE); data_queue_init(&peer->output_data, DATA_QUEUE_SIZE);
current_process->num_desc++; current_process->num_desc++;
CHECK_NON_EMPTY_DESC_INVARIANT;
return (SOCKET) new_idx; return (SOCKET) new_idx;
} }
@@ -1151,8 +1189,10 @@ wrap_native_file_into_desc(NATIVE_HANDLE handle)
} }
int idx = 0; int idx = 0;
while (current_process->desc[idx].type != DESC_EMPTY) while (current_process->desc[idx].type != DESC_EMPTY) {
idx++; idx++;
assert(idx < MAX_DESCRIPTORS);
}
Descriptor *desc = &current_process->desc[idx]; Descriptor *desc = &current_process->desc[idx];
@@ -1160,6 +1200,7 @@ wrap_native_file_into_desc(NATIVE_HANDLE handle)
desc->real_fd = handle; desc->real_fd = handle;
current_process->num_desc++; current_process->num_desc++;
CHECK_NON_EMPTY_DESC_INVARIANT;
return idx; return idx;
} }
@@ -1239,6 +1280,8 @@ int mock_closesocket(SOCKET fd)
} }
close_desc(desc); close_desc(desc);
current_process->num_desc--;
CHECK_NON_EMPTY_DESC_INVARIANT;
return 0; return 0;
} }
@@ -1271,6 +1314,8 @@ BOOL mock_CloseHandle(HANDLE handle)
} }
close_desc(desc); close_desc(desc);
current_process->num_desc--;
CHECK_NON_EMPTY_DESC_INVARIANT;
return TRUE; return TRUE;
} }
@@ -1485,6 +1530,8 @@ int mock_close(int fd)
} }
close_desc(desc); close_desc(desc);
current_process->num_desc--;
CHECK_NON_EMPTY_DESC_INVARIANT;
return 0; return 0;
} }