Commit Graph
270 Commits
Author SHA1 Message Date
cozis 3d1368938b Progress 2025-11-09 16:20:50 +01:00
cozis 8d56a32bd1 Progress 2025-11-09 14:32:02 +01:00
cozis bc12f86598 Progress 2025-11-09 14:15:19 +01:00
cozis d2140bdb13 Progress 2025-11-09 14:12:19 +01:00
cozis c3e20eceef Progress 2025-11-09 13:20:04 +01:00
cozis 7282176a9c Progress 2025-11-09 12:58:17 +01:00
cozis a2e949e5e4 Progress 2025-11-09 12:51:51 +01:00
cozis 739d6a7aee Progress 2025-11-09 12:08:22 +01:00
cozis 88735d1595 Progress 2025-11-08 23:44:37 +01:00
Francesco CozzutoandGitHub f335c6f329 Merge pull request #7 from cozis/claude/fix-as-011CUw81oePGJUvH3eAuvBjP
Claude
2025-11-08 22:36:07 +01:00
cozis 537fbc655f Progress 2025-11-08 22:35:20 +01:00
Claude 9367202dbc Fix assertion failures in tinydfs_test
Fixed two critical bugs in the TinyDFS client code:

1. Added bounds checking in alloc_operation() to prevent buffer overflow
   when searching for a free operation slot. The while loop could go past
   the end of the operations array, causing undefined behavior.

2. Fixed all tinydfs_submit_* functions to return the operation index
   (opidx) instead of 0 on success. This was causing all operations to
   be tracked with index 0, leading to type mismatches between operation
   types and result types, which triggered the assertion:
   "Assertion `pending.type == PENDING_OPERATION_DELETE' failed"

The test now runs successfully past the original assertion point.
2025-11-08 21:28:27 +00:00
cozis 554f92c8bc Merge branch 'main' of https://github.com/cozis/TinyDFS 2025-11-08 22:17:26 +01:00
cozis c2cb833e50 Progress 2025-11-08 22:16:55 +01:00
Francesco CozzutoandGitHub 94541b8770 Merge pull request #6 from cozis/claude/fix-valgrind-errors-011CUvinWp4yT3NTHZpayAUr
Fix Valgrind Memory Errors
2025-11-08 17:35:56 +01:00
Claude 43768781c8 Fix valgrind uninitialized memory errors
Fixed multiple uninitialized memory issues detected by valgrind:

1. SHA256 struct size mismatch: Changed from 64 bytes to 32 bytes to
   match the actual output of sha256() function (which produces 256
   bits = 32 bytes).

2. File tree chunk allocation: Fixed loop that wasn't initializing
   newly allocated chunks because num_chunks was updated before the
   initialization loop. Now saves old_num_chunks first.

3. Metadata server process_client_write: Initialized results[i].num_addrs
   to 0 before use to prevent reading uninitialized value.

4. Address struct initialization: Zero-initialize Address structs before
   setting fields to ensure the entire union is initialized (union
   contains either IPv4 or IPv6, leaving unused bytes uninitialized).

5. Fixed bug in IPv6 address creation: Changed incorrect is_ipv4=true
   to is_ipv4=false for IPv6 addresses.

6. Fixed all_chunk_servers_holding_chunk: Function was incrementing
   counter for all chunk servers instead of only those containing the
   hash, causing access to uninitialized array elements.

All valgrind errors resolved: ERROR SUMMARY: 0 errors from 0 contexts
2025-11-08 16:26:12 +00:00
cozis e5039d1774 Progress 2025-11-08 17:15:04 +01:00
cozis 7f3aa901f1 Progress 2025-11-08 13:25:17 +01:00
cozis 7846607651 Progress 2025-11-08 11:51:24 +01:00
cozis 279777fc8b Progress 2025-11-08 02:43:41 +01:00
cozis 463b1b4ba9 Progress 2025-11-08 02:16:46 +01:00
cozis 66eb81c9b9 Progress 2025-11-07 23:57:12 +01:00
cozis be2479b2be Progress 2025-11-07 22:45:40 +01:00
cozis 8ab426770f Progress 2025-11-07 22:18:05 +01:00
cozis d7cf946431 Progress 2025-11-07 20:26:15 +01:00
cozis 44fb1c4f36 Progress 2025-11-07 00:30:28 +01:00
cozis 407afffeb5 Progress 2025-11-06 21:39:22 +01:00
cozis 896bc0a23b Progress 2025-11-06 13:15:05 +01:00
cozis 0e27528d1d Progress 2025-11-06 01:23:24 +01:00
cozis e7455cc4ed Progress 2025-11-06 01:00:39 +01:00
cozis d192209c58 Progress 2025-11-05 23:14:24 +01:00
cozis 70a1914d8e Progress 2025-11-05 19:21:51 +01:00
cozis 18f1224c91 Progress 2025-11-05 17:09:38 +01:00
cozis e124952fe2 Progress 2025-11-05 14:26:57 +01:00
cozis 31a4b2cc32 Bug fixes 2025-11-03 18:32:14 +01:00
cozis e135d871c1 Progress 2025-11-03 15:52:25 +01:00
Francesco CozzutoandGitHub 21caecb91e Merge pull request #5 from cozis/claude/fix-valgrind-errors-011CUj8DsxsRQ3Udnyj8Vfv6
Fixes for Valgrind errors by Claude
2025-11-02 14:35:25 +01:00
Claude 5adf1337cf Move chunk server RequestQueue initialization to connection time
The RequestQueue for chunk servers should be initialized when the
connection is actually established in get_chunk_server_connection(),
not ahead of time during tinydfs_init().

The metadata server RequestQueue initialization remains in tinydfs_init()
since the metadata server connection is established during initialization.

Changes:
- Removed premature RequestQueue initialization for chunk servers
- Added clarifying comments about initialization timing
- RequestQueue is initialized in get_chunk_server_connection() (line 227)
2025-11-02 13:33:39 +00:00
Claude 3ae2f13610 Fix all Valgrind errors: uninitialized values and memory leak
This commit resolves all Valgrind errors reported in the test run:

1. Fixed uninitialized 'removed' array in tcp_translate_events (tcp.c:179)
   - Added memset to initialize the array to zero before use

2. Fixed uninitialized RequestQueue in client initialization (client.c)
   - Added initialization of metadata_server.reqs and chunk_servers[].reqs
   - Added forward declaration of request_queue_init function

3. Fixed uninitialized descriptor generation field (system.c:324)
   - Initialize generation to 0 when creating process descriptors
   - Prevents uninitialized value errors in handle_to_desc

4. Fixed 34-byte memory leak in tcp_context_free (tcp.c:91-103)
   - Added cleanup of connection byte queues when freeing TCP context
   - Frees both input and output ByteQueues for all connections

All Valgrind errors have been resolved:
- ERROR SUMMARY: 0 errors from 0 contexts
- All heap blocks freed - no leaks possible
2025-11-02 13:24:18 +00:00
Francesco CozzutoandGitHub eab8fd118c Merge pull request #4 from cozis/claude/implement-simulation-client-011CUj21MCf7ic4fnk94r8sS
Simulation client implementation by Claude
2025-11-02 14:14:12 +01:00
Claude 74b259ef19 Wrap entire system.c with BUILD_TEST guard instead of nested conditionals
Simplified conditional compilation by wrapping the entire system.c file
with a single #ifdef BUILD_TEST guard, removing all nested conditionals
for simulation client code.

**Rationale:**
The entire system.c file contains simulation framework code (mock functions,
Process structure, spawn_simulated_process, update_simulation, etc.) that
is only used when BUILD_TEST is defined. Having nested #ifdef BUILD_TEST
conditionals within this file made no sense.

**Changes:**

**system.c:**
- Added #ifdef BUILD_TEST at the very beginning of the file (line 1)
- Added #endif // BUILD_TEST at the very end of the file (line 1486)
- Removed #ifdef BUILD_TEST around simulation_client.h include
- Removed #ifdef BUILD_TEST around PROCESS_TYPE_CLIENT enum value
- Removed #ifdef BUILD_TEST around SimulationClient union member
- Removed #ifdef BUILD_TEST around is_client() function
- Removed #ifdef BUILD_TEST around client detection in spawn_simulated_process()
- Removed #ifdef BUILD_TEST around PROCESS_TYPE_CLIENT cases in:
  - spawn_simulated_process() init switch
  - free_process() cleanup switch
  - update_simulation() step switch

**Result:**
- Much cleaner code with single top-level BUILD_TEST guard
- No nested conditionals cluttering the code
- All three build targets still compile successfully:
  - tinydfs_server.out (excludes system.c entirely)
  - example_client.out (excludes system.c entirely)
  - tinydfs_test.out (includes all of system.c)
- Simulation runs correctly with client support

The file structure now correctly reflects that system.c is purely test
infrastructure and not used in production builds.
2025-11-02 13:10:17 +00:00
Claude 0d3edf9155 Initialize timeout parameter to -1 in main_server.c
Fixed uninitialized timeout variables in metadata_server_main() and
chunk_server_main() functions by setting default value to -1.

**Changes:**
- metadata_server_main(): Changed `int timeout;` to `int timeout = -1;`
- chunk_server_main(): Changed `int timeout;` to `int timeout = -1;`

This ensures the timeout parameter has a defined default value before
being passed to init functions, which is important for predictable
behavior even though the init functions set the value themselves.

All build targets (tinydfs_server.out, example_client.out,
tinydfs_test.out) compile and run successfully.
2025-11-02 13:07:06 +00:00
Claude dc4fb33be2 Fix compilation errors for non-test builds
Fixed build errors in tinydfs_server and example_client targets caused
by missing timeout parameter and simulation client references.

**Changes:**

**main_server.c:**
- Added int timeout variable declaration in metadata_server_main()
- Added int timeout variable declaration in chunk_server_main()
- Pass &timeout to metadata_server_init() call
- Pass &timeout to metadata_server_step() call
- Pass &timeout to chunk_server_init() call
- Pass &timeout to chunk_server_step() call

**system.c:**
- Guarded simulation_client.h include with #ifdef BUILD_TEST
- Guarded PROCESS_TYPE_CLIENT enum value with #ifdef BUILD_TEST
- Guarded SimulationClient member in Process union with #ifdef BUILD_TEST
- Guarded is_client() function with #ifdef BUILD_TEST
- Guarded client detection logic in spawn_simulated_process()
- Guarded PROCESS_TYPE_CLIENT cases in all switch statements:
  - spawn_simulated_process() init switch
  - free_process() cleanup switch
  - update_simulation() step switch

**Result:**
- tinydfs_server.out now compiles successfully
- example_client.out now compiles successfully
- tinydfs_test.out continues to work correctly
- Simulation client code only included when BUILD_TEST is defined
- All three build targets pass compilation with only pre-existing warnings

The simulation_client code is now properly isolated to test builds only,
preventing linker errors when building the real server and example client.
2025-11-02 13:05:34 +00:00
Claude 88a3a6e4f0 Add timeout parameter to metadata_server and chunk_server init/step functions
Unified the function signatures across all process types (metadata server,
chunk server, and simulation client) to accept an int *timeout parameter.
This provides consistency in the simulation framework and allows all
process types to control their wakeup times.

**Changes:**

**Headers (metadata_server.h, chunk_server.h):**
- Updated metadata_server_init() to accept int *timeout parameter
- Updated metadata_server_step() to accept int *timeout parameter
- Updated chunk_server_init() to accept int *timeout parameter
- Updated chunk_server_step() to accept int *timeout parameter

**Implementations (metadata_server.c, chunk_server.c):**
- Modified init functions to set *timeout = -1 (no timeout needed)
- Modified step functions to set *timeout = -1 (no timeout needed)
- Both server types currently don't require periodic wakeups, but the
  parameter allows for future timeout-based behavior

**System Integration (system.c):**
- Updated spawn_simulated_process() to declare timeout variable once
  and pass it to all process init functions uniformly
- Updated update_simulation() to declare timeout variable once and
  pass it to all process step functions uniformly
- Simplified control flow by removing special case for client timeout
  handling - now all process types use the same timeout logic
- Consolidated timeout-to-wakeup_time conversion after switch statements

**Benefits:**
- Consistent API across all process types
- Cleaner code with reduced duplication
- Future-proof for server timeout requirements
- All processes now managed uniformly by simulation framework

The simulation continues to run correctly with all process types
handling timeout parameter appropriately.
2025-11-02 12:33:40 +00:00
Claude 38fdab24d3 Implement simulation client for deterministic testing
Added a simulation client that can run inside the TinyDFS simulation
framework, enabling fully deterministic end-to-end testing of the
distributed file system.

**New Files:**
- src/simulation_client.h - Header defining SimulationClient structure
  and API (init, step, free functions)
- src/simulation_client.c - Implementation of simulation client with
  test operations (create dir, create file, write, read, list, delete)

**Key Changes:**

**System Framework (src/system.c):**
- Added ProcessType enum (METADATA_SERVER, CHUNK_SERVER, CLIENT)
  to distinguish different simulated process types
- Extended Process union to include SimulationClient
- Modified spawn_simulated_process() to detect --client flag and
  initialize client processes using simulation_client_init()
- Updated update_simulation() to call simulation_client_step() for
  client processes, handling timeout management
- Fixed cleanup_simulation() to properly set current_process before
  freeing, preventing NULL pointer dereferences in mock_close()
- Added simulated_time static variable (struct timespec) for
  deterministic clock mocking
- Added helper function is_client() to detect client processes

**API Updates (src/system.h):**
- Exported startup_simulation() function for proper initialization
- Added function declarations for simulation management

**Test Updates (src/main_test.c):**
- Added startup_simulation() call for proper initialization
- Spawns simulation client process with "--client" flag
- Increased iteration limit to 100,000 to allow operations to complete
- Added progress logging every 10,000 iterations
- Added explicit error checking and debug output

**Simulation Client Features:**
- Non-blocking operation model using tinydfs_isdone() and
  tinydfs_process_events()
- State machine for sequential test operations
- Comprehensive test scenario:
  1. Create directory (/test_dir)
  2. Create file (/test_dir/test_file.txt)
  3. Write data to file
  4. Read data back
  5. List directory contents
  6. Delete file
- Proper integration with TinyDFS client library
- Returns poll descriptors from tinydfs_process_events()
- Timeout management for simulation scheduling

**Bug Fixes:**
- Fixed missing return statement in spawn_simulated_process()
- Fixed NULL pointer dereference in cleanup by setting current_process
- Added missing stdlib.h include for atoi()

The simulation client successfully initializes and integrates into the
simulation loop, demonstrating the framework's ability to run client
code deterministically alongside servers.
2025-11-02 12:05:53 +00:00
cozis d267c0760e Bug fixes 2025-11-02 12:50:21 +01:00
Francesco CozzutoandGitHub ba34102453 Merge pull request #3 from cozis/claude/continue-deterministic-simulation-011CUiNFqeQQknvcwfkzVKME
Progress on DST using Claude
2025-11-02 11:46:12 +01:00
Claude d959306719 Add deterministic QueryPerformanceCounter/Frequency mocks
Implemented Windows high-resolution timing mocks for deterministic simulation:

**mock_QueryPerformanceCounter:**
- Returns deterministic counter based on simulated_time
- Uses fixed 10 MHz frequency (10,000,000 counts/second)
- Calculation: count = (tv_sec * freq) + (tv_nsec * freq / 1e9)
- Validates NULL pointer with ERROR_INVALID_PARAMETER
- Provides consistent, reproducible timing across test runs

**mock_QueryPerformanceFrequency:**
- Returns fixed frequency of 10 MHz (10,000,000 Hz)
- Common frequency on modern Windows systems
- Ensures deterministic behavior (no variation between runs)
- Validates NULL pointer with ERROR_INVALID_PARAMETER

**Integration:**
- Added function declarations to system.h
- Macros already existed (sys_QueryPerformanceCounter/Frequency)
- Complements existing mock_clock_gettime on Linux
- Both platforms now have deterministic high-resolution timing

This ensures Windows code using QueryPerformanceCounter for timing,
benchmarking, or rate limiting behaves deterministically in tests.
The fixed 10 MHz frequency provides nanosecond-level precision
(100ns per tick) suitable for most timing needs.
2025-11-02 10:44:20 +00:00
Claude aa4be1e203 Fix socket error handling: WSASetLastError on Windows and recv() return value
Corrected two critical issues with socket mock precision:

**1. Windows Socket Error Handling:**
- Socket functions on Windows use WSAGetLastError/WSASetLastError, NOT errno
- Added SET_SOCKET_ERROR macro that calls:
  - WSASetLastError() on Windows
  - errno assignment on Linux
- Mapped all POSIX error codes to WSA equivalents:
  - EWOULDBLOCK → WSAEWOULDBLOCK
  - EAFNOSUPPORT → WSAEAFNOSUPPORT
  - EMFILE → WSAEMFILE
  - EBADF → WSAEBADF
  - ENOTSOCK → WSAENOTSOCK
  - EINVAL → WSAEINVAL
  - EADDRINUSE → WSAEADDRINUSE
  - EDESTADDRREQ → WSAEDESTADDRREQ
  - ECONNABORTED → WSAECONNABORTED
  - EISCONN → WSAEISCONN
  - EINPROGRESS → WSAEINPROGRESS
  - EOPNOTSUPP → WSAEOPNOTSUPP
  - ENOTCONN → WSAENOTCONN
  - ECONNRESET → WSAECONNRESET
  - ENOPROTOOPT → WSAENOPROTOOPT
  - EPIPE → WSAESHUTDOWN (Windows equivalent)

**2. Fixed mock_recv() Return Value:**
- Now returns 0 when peer closes connection (orderly shutdown)
- Previously returned -1 with ECONNRESET
- This matches standard POSIX/Winsock behavior:
  - 0 = peer closed connection gracefully
  - -1 = error occurred (check errno/WSAGetLastError)
  - >0 = bytes received

**Updated Functions:**
- mock_socket, mock_bind, mock_listen, mock_accept
- mock_connect, mock_recv, mock_send
- mock_getsockopt, mock_setsockopt

All socket functions now use correct error reporting mechanism
for their platform, improving simulation accuracy.
2025-11-02 10:33:44 +00:00
Claude 14cc295c30 Improve mocking system precision with proper errno handling
Enhanced the deterministic simulation framework with comprehensive error handling:

**Socket Operations:**
- mock_socket: Set EAFNOSUPPORT, EMFILE
- mock_bind: Set EBADF, ENOTSOCK, EINVAL, EADDRINUSE (with address collision detection)
- mock_listen: Set EBADF, ENOTSOCK, EDESTADDRREQ (check socket is bound)
- mock_accept: Set EBADF, EINVAL, EWOULDBLOCK, ECONNABORTED, EMFILE
- mock_connect: Set EBADF, EISCONN, EINVAL, EINPROGRESS
- mock_recv: Set EBADF, EOPNOTSUPP, ENOTCONN, ECONNRESET, EWOULDBLOCK
- mock_send: Set EBADF, EOPNOTSUPP, ENOTCONN, EPIPE, EWOULDBLOCK
- mock_getsockopt: Set EBADF, ENOPROTOOPT, EINVAL (implemented SO_ERROR)
- mock_setsockopt: Set EBADF, ENOPROTOOPT (no-op but validates input)

**File Operations (Linux):**
- mock_close: Set EBADF with bounds checking
- mock_flock: Set EBADF with validation
- mock_fsync: Set EBADF, EINVAL
- mock_read: Set EBADF with descriptor type checking
- mock_write: Set EBADF with descriptor type checking
- mock_fstat: Set EBADF with validation
- All file ops forward errno from real syscalls when appropriate

**File Operations (Windows):**
- mock_CloseHandle: SetLastError ERROR_INVALID_HANDLE
- mock_LockFile/UnlockFile: SetLastError ERROR_INVALID_HANDLE
- mock_FlushFileBuffers: SetLastError ERROR_INVALID_HANDLE
- mock_ReadFile/WriteFile: SetLastError ERROR_INVALID_HANDLE
- mock_GetFileSizeEx: SetLastError ERROR_INVALID_HANDLE
- All Windows file ops forward errors from real API calls

**Simulated Clock:**
- Implemented mock_clock_gettime with proper errno handling
- Returns simulated time for deterministic behavior
- Validates clock ID (CLOCK_REALTIME, CLOCK_MONOTONIC)
- Sets EFAULT for NULL pointer, EINVAL for invalid clock ID

**Improvements:**
- Added comprehensive bounds checking on all file descriptors
- Proper descriptor type validation before operations
- Peer connection validation in socket operations
- Address-in-use detection for bind operations
- Consistent error code semantics matching POSIX/Windows standards

All file I/O operations continue to forward to real OS as intended,
with mocking layer providing precise error simulation and validation.
2025-11-02 03:51:59 +00:00