From 0cd597ab82e432a77d1ed983a24eff2d9270fccc Mon Sep 17 00:00:00 2001 From: Jon Shallow Date: Sat, 14 Mar 2026 15:17:10 +0000 Subject: [PATCH] multi-threading: Lock resource structure when calling request handlers If an app request call-back modifies any data associated with a resource, then multiple threads can attempt to unsafely modify the data at the same time. Lock the resource when calling the app request handler (as global_lock is unlocked) to prevent concurrent changes to the resource information. If the app request handler is definately known to not modify the resource information, then COAP_RESOURCE_SAFE_REQUEST_HANDLER can be set in the flags for coap_resource_init(). Usually if there is a PUT, POST, PATCH or IPATCH handler it is unsafe to set this. Setting this will reduce lock contention. --- .gitignore | 1 + examples/coap-server.c | 22 ++- include/coap3/coap_resource.h | 19 ++ include/coap3/coap_resource_internal.h | 5 +- include/coap3/coap_threadsafe_internal.h | 219 ++++++++++++++++++++--- man/Makefile.am | 3 + man/coap_locking.txt.in | 94 +++++++--- man/examples-code-check.c | 3 + src/coap_net.c | 17 +- src/coap_openssl.c | 1 + src/coap_resource.c | 35 +++- src/coap_subscribe.c | 23 ++- src/coap_threadsafe.c | 70 ++++---- tests/oss-fuzz/Makefile.ci.in | 14 +- 14 files changed, 412 insertions(+), 114 deletions(-) diff --git a/.gitignore b/.gitignore index 623f4ee9..dd9d5b1a 100644 --- a/.gitignore +++ b/.gitignore @@ -99,6 +99,7 @@ include/coap3/coap_defines.h tests/.deps tests/Makefile tests/oss-fuzz/Makefile.ci +tests/oss-fuzz/*.o tests/testdriver tests/*.o tests/test_common.h diff --git a/examples/coap-server.c b/examples/coap-server.c index 18345f61..d30d15c7 100644 --- a/examples/coap-server.c +++ b/examples/coap-server.c @@ -1027,7 +1027,9 @@ hnd_put_post_unknown(coap_resource_t *resource COAP_UNUSED, /* * Create a resource to handle the new URI - * uri_path will get deleted when the resource is removed + * uri_path will get deleted when the resource is removed. + * Not safe to set COAP_RESOURCE_SAFE_REQUEST_HANDLER here as + * hnd_put_post() manipulates resource associated information.. */ r = coap_resource_init((coap_str_const_t *)uri_path, COAP_RESOURCE_FLAGS_RELEASE_URI | resource_flags); @@ -1122,14 +1124,19 @@ init_resources(coap_context_t *ctx) { #if COAP_PROXY_SUPPORT if (reverse_proxy.entry_count) { /* Create a reverse proxy resource to handle PUTs */ - r = coap_resource_reverse_proxy_init(hnd_reverse_proxy_uri, 0); + r = coap_resource_reverse_proxy_init(hnd_reverse_proxy_uri, COAP_RESOURCE_SAFE_REQUEST_HANDLER); + if (!r) + return; coap_add_resource(ctx, r); coap_register_event_handler(ctx, proxy_event_handler); coap_register_response_handler(ctx, reverse_response_handler); coap_register_nack_handler(ctx, proxy_nack_handler); } else { #endif /* COAP_PROXY_SUPPORT */ - r = coap_resource_init(NULL, COAP_RESOURCE_FLAGS_HAS_MCAST_SUPPORT); + r = coap_resource_init(NULL, + COAP_RESOURCE_FLAGS_HAS_MCAST_SUPPORT | COAP_RESOURCE_SAFE_REQUEST_HANDLER); + if (!r) + return; coap_register_request_handler(r, COAP_REQUEST_GET, hnd_get_index); coap_add_attr(r, coap_make_str_const("ct"), coap_make_str_const("0"), 0); @@ -1139,7 +1146,10 @@ init_resources(coap_context_t *ctx) { /* store clock base to use in /time */ my_clock_base = clock_offset; - r = coap_resource_init(coap_make_str_const("time"), resource_flags); + r = coap_resource_init(coap_make_str_const("time"), + COAP_RESOURCE_SAFE_REQUEST_HANDLER | resource_flags); + if (!r) + return; coap_register_request_handler(r, COAP_REQUEST_GET, hnd_get_fetch_time); coap_register_request_handler(r, COAP_REQUEST_FETCH, hnd_get_fetch_time); coap_register_request_handler(r, COAP_REQUEST_PUT, hnd_put_time); @@ -1187,7 +1197,9 @@ init_resources(coap_context_t *ctx) { } if (proxy_host_name_count) { r = coap_resource_proxy_uri_init2(hnd_forward_proxy_uri, proxy_host_name_count, - proxy_host_name_list, 0); + proxy_host_name_list, COAP_RESOURCE_SAFE_REQUEST_HANDLER); + if (!r) + return; coap_add_resource(ctx, r); coap_register_event_handler(ctx, proxy_event_handler); coap_register_response_handler(ctx, reverse_response_handler); diff --git a/include/coap3/coap_resource.h b/include/coap3/coap_resource.h index 9ce41f54..308dccc4 100644 --- a/include/coap3/coap_resource.h +++ b/include/coap3/coap_resource.h @@ -157,6 +157,25 @@ typedef void (*coap_method_handler_t)(coap_resource_t *resource, */ #define COAP_RESOURCE_HANDLE_WELLKNOWN_CORE 0x800 +/** + * Call the block data handler for this resource if one is registered for the context + */ +#define COAP_RESOURCE_USE_BLOCK_DATA_HANDLER 0x1000 + +/** + * Hide this resource from .well-known/core + */ +#define COAP_RESOURCE_HIDE_WELLKNOWN_CORE 0x2000 + +/** + * Don't lock this resource when calling app call-back handler for requests + * as handler will not be manipulating any resource held data. + * Not locking the resource (if safe) can save lock contention on a busy system. + * Caution: If there is a POST or PUT handler, then it is likely this should + * not be set. + */ +#define COAP_RESOURCE_SAFE_REQUEST_HANDLER 0x4000 + /** * Creates a new resource object and initializes the link field to the string * @p uri_path. This function returns the new coap_resource_t object. diff --git a/include/coap3/coap_resource_internal.h b/include/coap3/coap_resource_internal.h index f7697eb2..a826cc74 100644 --- a/include/coap3/coap_resource_internal.h +++ b/include/coap3/coap_resource_internal.h @@ -17,7 +17,7 @@ #ifndef COAP_RESOURCE_INTERNAL_H_ #define COAP_RESOURCE_INTERNAL_H_ -#include "coap_internal.h" +#include "coap_threadsafe_internal.h" #include "coap_uthash_internal.h" #if COAP_SERVER_SUPPORT @@ -108,6 +108,9 @@ struct coap_resource_t { * the coap handler. */ void *user_data; +#if COAP_THREAD_SAFE + coap_lock_t lock; +#endif /* COAP_THREAD_SAFE */ }; diff --git a/include/coap3/coap_threadsafe_internal.h b/include/coap3/coap_threadsafe_internal.h index b49fd207..657ef4bd 100644 --- a/include/coap3/coap_threadsafe_internal.h +++ b/include/coap3/coap_threadsafe_internal.h @@ -110,35 +110,37 @@ typedef struct coap_lock_t { } coap_lock_t; /** - * Unlock the global_lock lock. + * Unlock the specific lock. * * If this is a nested lock (Public API - libcoap - app call-back - Public API), - * then the lock remains locked, but global_lock.in_callback is decremented. + * then the lock remains locked, but lock->in_callback is decremented. * * Note: Invoked by wrapper macro, not used directly. * + * @param lock The lock to act on. * @param file The file from which coap_lock_unlock_func() is getting called. * @param line The line no from which coap_lock_unlock_func() is getting called. */ -void coap_lock_unlock_func(const char *file, int line); +void coap_lock_unlock_func(coap_lock_t *lock, const char *file, int line); /** - * Lock the global_lock lock. + * Lock the specfic lock. * * If this is a nested lock (Public API - libcoap - app call-back - Public API), - * then increment the global_lock.in_callback. + * then increment the lock->in_callback. * * Note: Invoked by wrapper macro, not used directly. * + * @param lock The lock to act on. * @param file The file from which coap_lock_lock_func() is getting called. * @param line The line no from which coap_lock_lock_func() is getting called. * * @return @c 0 if libcoap has not started (coap_startup() not called), else @c 1. */ -int coap_lock_lock_func(const char *file, int line); +int coap_lock_lock_func(coap_lock_t *lock, const char *file, int line); /** - * libcoap library code. Lock The global_lock. + * libcoap library code. Lock the global_lock. * * Invoked when * Not locked at all @@ -154,13 +156,13 @@ int coap_lock_lock_func(const char *file, int line); * */ #define coap_lock_lock(c,failed) do { \ - if (!coap_lock_lock_func(__FILE__, __LINE__)) { \ + if (!coap_lock_lock_func(&global_lock, __FILE__, __LINE__)) { \ failed; \ } \ } while (0) /** - * libcoap library code. Unlock The global_lock. + * libcoap library code. Unlock the global_lock. * * Unlocked when * Same thread locked context @@ -169,7 +171,43 @@ int coap_lock_lock_func(const char *file, int line); * @param c Context. */ #define coap_lock_unlock(c) do { \ - coap_lock_unlock_func(__FILE__, __LINE__); \ + coap_lock_unlock_func(&global_lock, __FILE__, __LINE__); \ + } while (0) + +/** + * libcoap library code. Lock a specific lock. + * + * Invoked when + * Not locked at all + * Locked, app call-back, call from app call-back + * Locked, app call-back, call from app call-back, app call-back, call from app call-back + * Result + * global_lock locked. + * global_lock not locked if libcoap not started and @p failed is executed. @p failed must + * be code that skips doing the lock protected code. + * + * @param lock The specifc lock to lock. + * @param failed Code to execute on lock failure. + * + */ +#define coap_lock_specific_lock(lock, failed) do { \ + if (!coap_lock_lock_func(lock, __FILE__, __LINE__)) { \ + failed; \ + } \ + } while (0) + +/** + * libcoap library code. Unlock a specific lock. + * + * Unlocked when + * Same thread locked context + * Not when called from app call-back + * + * @param lock The specifc lock to unlock. + * + */ +#define coap_lock_specific_unlock(lock) do { \ + coap_lock_unlock_func(lock, __FILE__, __LINE__); \ } while (0) /** @@ -250,6 +288,32 @@ int coap_lock_lock_func(const char *file, int line); coap_lock_lock(c,failed); \ } while (0) +/** + * libcoap library code. Invoke an app callback, unlocking global_lock first, + * followed by locking specified lock, and after callback unlock specified lock + * and lock global_lock.. + * + * Called when + * Locked + * + * @param lock lock to lock before callback. + * @param func app call-back function to invoke. + * @param failed Code to execute on (re-)lock failure. + * + */ +#define coap_lock_specific_callback_release(lock,func,failed) do { \ + coap_lock_check_locked(NULL); \ + coap_lock_unlock(NULL); \ + coap_lock_specific_lock(lock, failed); \ + (lock)->in_callback++; \ + (lock)->callback_file = __FILE__; \ + (lock)->callback_line = __LINE__; \ + func; \ + (lock)->in_callback--; \ + coap_lock_specific_unlock(lock); \ + coap_lock_lock(NULL, failed); \ + } while (0) + extern coap_lock_t global_lock; # else /* ! COAP_THREAD_RECURSIVE_CHECK */ @@ -272,8 +336,10 @@ typedef struct coap_lock_t { * * Note: Invoked by wrapper macro, not used directly. * + * @param lock The lock to act on. + * */ -void coap_lock_unlock_func(void); +void coap_lock_unlock_func(coap_lock_t *lock); /** * Lock the global_lock lock. @@ -283,12 +349,14 @@ void coap_lock_unlock_func(void); * * Note: Invoked by wrapper macro, not used directly. * + * @param lock The lock to act on. + * * @return @c 0 if libcoap has not started (coap_startup() not called), else @c 1. */ -int coap_lock_lock_func(void); +int coap_lock_lock_func(coap_lock_t *lock); /** - * libcoap library code. Lock The global_lock. + * libcoap library code. Lock the global_lock. * * Invoked when * Not locked at all @@ -304,13 +372,35 @@ int coap_lock_lock_func(void); * */ #define coap_lock_lock(c,failed) do { \ - if (!coap_lock_lock_func()) { \ + if (!coap_lock_lock_func(&global_lock)) { \ failed; \ } \ } while (0) /** - * libcoap library code. Unlock The global_lock. + * libcoap library code. Lock the specific lock. + * + * Invoked when + * Not locked at all + * Locked, app call-back, call from app call-back + * Locked, app call-back, call from app call-back, app call-back, call from app call-back + * Result + * global_lock locked. + * global not locked if libcoap not started and @p failed is executed. @p failed must + * be code that skips doing the lock protected code. + * + * @param lock The specifc lock to lock. + * @param failed Code to execute on lock failure + * + */ +#define coap_lock_specific_lock(lock, failed) do { \ + if (!coap_lock_lock_func(lock)) { \ + failed; \ + } \ + } while (0) + +/** + * libcoap library code. Unlock the global_lock. * * Unlocked when * Same thread locked context. @@ -319,8 +409,21 @@ int coap_lock_lock_func(void); * @param c Context. */ #define coap_lock_unlock(c) do { \ - assert(c); \ - coap_lock_unlock_func(); \ + coap_lock_unlock_func(&global_lock); \ + } while (0) + +/** + * libcoap library code. Unlock the specific lock.. + * + * Unlocked when + * Same thread locked context. + * Not when called from app call-back. + * + * @param lock The specifc lock to unlock. + * + */ +#define coap_lock_specific_unlock(lock) do { \ + coap_lock_unlock_func(lock); \ } while (0) /** @@ -377,6 +480,30 @@ int coap_lock_lock_func(void); coap_lock_lock(c,failed); \ } while (0) +/** + * libcoap library code. Invoke an app callback, unlocking global_lock first, + * followed by locking specified lock, and after callback unlock specified lock + * and lock global_lock.. + * + * Called when + * Locked (need to unlock over app call-back) + * + * @param lock lock to lock before callback. + * @param func app call-back function to invoke. + * @param failed Code to execute on (re-)lock failure. + * + */ +#define coap_lock_specific_callback_release(lock,func,failed) do { \ + coap_lock_check_locked(NULL); \ + coap_lock_unlock(NULL); \ + coap_lock_specific_lock(lock, failed); \ + (lock)->in_callback++; \ + func; \ + (lock)->in_callback--; \ + coap_lock_specific_unlock(lock); \ + coap_lock_lock(NULL,failed); \ + } while (0) + /** * libcoap library code. Invoke an app callback that has a return value, * unlocking global_lock first. @@ -400,11 +527,11 @@ int coap_lock_lock_func(void); # endif /* ! COAP_THREAD_RECURSIVE_CHECK */ /** - * libcoap library code. Initialize the global_lock. + * libcoap library code. Initialize the specified lock. */ -#define coap_lock_init() do { \ - memset(&global_lock.mutex, 0, sizeof(global_lock.mutex)); \ - coap_mutex_init(&global_lock.mutex); \ +#define coap_lock_init(lock) do { \ + memset(&(lock)->mutex, 0, sizeof((lock)->mutex)); \ + coap_mutex_init(&(lock)->mutex); \ } while (0) /** @@ -446,7 +573,7 @@ typedef coap_mutex_t coap_lock_t; /** * Dummy for no thread-safe code * - * libcoap library code. Lock The global_lock. + * libcoap library code. Lock the global_lock. * * Invoked when * Not locked at all @@ -466,7 +593,27 @@ typedef coap_mutex_t coap_lock_t; /** * Dummy for no thread-safe code * - * libcoap library code. Unlock The global_lock. + * libcoap library code. Lock the specific lock. + * + * Invoked when + * Not locked at all + * Locked, app call-back, call from app call-back + * Locked, app call-back, call from app call-back, app call-back, call from app call-back + * Result + * global_lock locked. + * global_lock not locked if libcoap not started and @p failed is executed. @p failed must + * be code that skips doing the lock protected code. + * + * @param lock The specifc lock to lock. + * @param failed Code to execute on lock failure. + * + */ +#define coap_lock_specific_lock(lock,failed) + +/** + * Dummy for no thread-safe code + * + * libcoap library code. Unlock the global_lock. * * Unlocked when * Same thread locked context @@ -475,12 +622,24 @@ typedef coap_mutex_t coap_lock_t; */ #define coap_lock_unlock(c) +/** + * Dummy for no thread-safe code + * + * libcoap library code. Unlock the specific lock. + * + * Unlocked when + * Same thread locked context + * + * @param lock The specifc lock to unlock. + */ +#define coap_lock_specific_unlock(lock) + /** * Dummy for no thread-safe code * * libcoap library code. Initialize the global_lock. */ -#define coap_lock_init() +#define coap_lock_init(lock) /** * Dummy for no thread-safe code @@ -534,6 +693,18 @@ typedef coap_mutex_t coap_lock_t; */ #define coap_lock_callback_release(c,func,failed) func +/** + * Dummy for no thread-safe code + * + * libcoap library code. Invoke an app callback. + * + * @param lock lock to unlock before callback. + * @param func app call-back function to invoke. + * @param failed Code to execute on (re-)lock failure. + * + */ +#define coap_lock_specific_callback_release(lock,func,failed) func + /** * Dummy for no thread-safe code * diff --git a/man/Makefile.am b/man/Makefile.am index a22b025c..4a87c26e 100644 --- a/man/Makefile.am +++ b/man/Makefile.am @@ -137,6 +137,9 @@ install-man: install-man3 install-man5 install-man7 @echo ".so man3/coap_io.3" > coap_can_exit.3 @echo ".so man3/coap_locking.3" > coap_lock_callback_ret_release.3 @echo ".so man3/coap_locking.3" > coap_lock_invert.3 + @echo ".so man3/coap_locking.3" > coap_lock_specific_lock.3 + @echo ".so man3/coap_locking.3" > coap_lock_specific_unlock.3 + @echo ".so man3/coap_locking.3" > coap_lock_specific_callback_release.3 @echo ".so man3/coap_logging.3" > coap_log_info.3 @echo ".so man3/coap_logging.3" > coap_log_debug.3 @echo ".so man3/coap_logging.3" > coap_log_oscore.3 diff --git a/man/coap_locking.txt.in b/man/coap_locking.txt.in index 30f4fa4c..a19678f1 100644 --- a/man/coap_locking.txt.in +++ b/man/coap_locking.txt.in @@ -19,36 +19,45 @@ coap_lock_callback, coap_lock_callback_release, coap_lock_callback_ret, coap_lock_callback_ret_release, -coap_lock_invert +coap_lock_invert, +coap_lock_specific_lock, +coap_lock_specific_unlock, +coap_lock_specific_callback_release - Work with CoAP thread safe locking SYNOPSIS -------- *#include * -*void coap_lock_init(void);* +*void coap_lock_init(coap_lock_t *_lock_);* -*void coap_lock_lock(coap_context_t *_context_, coap_code_t _failed_statement_);* +*void coap_lock_lock(coap_code_t _failed_statement_);* -*void coap_lock_unlock(coap_context_t *_context_);* +*void coap_lock_unlock(void);* -*void coap_lock_check_locked(coap_context_t *_context_);* +*void coap_lock_check_locked(void);* -*void coap_lock_callback(coap_context_t *_context_, -coap_func_t _callback_function_);* +*void coap_lock_callback(coap_func_t _callback_function_);* -*void coap_lock_callback_ret(void *_return_value_, coap_context_t *_context_, +*void coap_lock_callback_ret(void *_return_value_, coap_func_t _callback_function_, coap_code_t _failed_statement_);* -*void coap_lock_callback_release(coap_context_t *_context_, +*void coap_lock_callback_release( coap_func_t _callback_function__, coap_code_t _failed_statement_);* -*void coap_lock_callback_ret_release(void *_return_value_, coap_context_t *_context_, +*void coap_lock_callback_ret_release(void *_return_value_, coap_func_t _callback_function_, coap_code_t _failed_statement_);* -*void coap_lock_invert(coap_context_t *_context_, coap_func_t _locking_function_, +*void coap_lock_invert(coap_func_t _locking_function_, coap_code_t _failed_statement_);* +*void coap_lock_specific_lock(coap_lock_t *_lock_, coap_code_t _failed_statement_);* + +*void coap_lock_specific_unlock(coap_lock_t *_lock_);* + +*void coap_lock_specific_callback_release(coap_lock_t *lock, +coap_func_t _callback_function__, coap_code_t _failed_statement_);* + For specific (D)TLS library support, link with *-lcoap-@LIBCOAP_API_VERSION@-notls*, *-lcoap-@LIBCOAP_API_VERSION@-gnutls*, *-lcoap-@LIBCOAP_API_VERSION@-openssl*, *-lcoap-@LIBCOAP_API_VERSION@-mbedtls*, @@ -72,20 +81,21 @@ execution of the following code that would have been under the lock protection and certainly not cause the corresponding *coap_lock_unlock*() function to be called. -Likewise, _callback_function_ is the callback handler function with all of +Likewise, _callback_function_ is the call-back handler function with all of its parameters. Several definitions can be defined with configure or cmake. These are -COAP_THREAD_SAFE If set, simply does locking at the appropriate places. If +COAP_THREAD_SAFE If set, libcoap does internal locking at the appropriate places. If not set, then no locking takes place, the code is faster (no locking code), but not multi-thread access safe. COAP_THREAD_RECURSIVE_CHECK If set, and COAP_THREAD_SAFE is set, checks that if a lock is locked, it reports that the same lock is being (re-)locked. -Currently, locking is only done at the *global_lock* level for the Public API -functions where appropriate. +Normally locking is done at the *global_lock* level for the Public API +functions where appropriate, apart from call-backs to request handler functions +where specific structures may get locked to make the call-back thread safe. In principal, libcoap code internally should only unlock *global_lock* when waiting on a *select*() or equivalent, or when calling a request handler, and then lock @@ -100,8 +110,12 @@ call a Public API when in the locked state. *coap_lock_callback_release*() (or *coap_lock_callback_ret_release*()) unlocks *global_lock* when calling app call-back. The allows the app call-back to go off -and do other slow/blocking activity. Any calls to a Public API then locks up -*global_lock* before preceding. +and do other slow/blocking activity. Any app call-back calls to a Public API then +locks up *global_lock* before proceding. + +*coap_lock_specific_callback_release*() acts as *coap_lock_callback_release*(), +but allows additional locks to be set before doing the call-back, and then +unlocking them after the call-back return. Any libcoap code that runs with *global_lock* locked should not call a Public API, but call the _lkd equivalent (if available). @@ -111,7 +125,7 @@ FUNCTIONS *Function: coap_lock_init()* -The *coap_lock_init*() function is used to initialize the *global_lock* lock +The *coap_lock_init*() function is used to initialize the specified _lock_ structure. *Function: coap_lock_lock()* @@ -128,39 +142,39 @@ thread can access libcoap and the underlying structures. *Function: coap_lock_check_lock()* The *coap_lock_check_lock*() function is used to check the internal version -(potentially has __lkd_ appended in the name) of a public AP is getting called +(potentially has __lkd_ appended in the name) of a public API is getting called with *global_lock* locked. *Function: coap_lock_callback()* -The *coap_lock_callback*() function is used whenever a callback handler is +The *coap_lock_callback*() function is used whenever a call-back handler is getting called, instead of calling the function directly. The lock information -in *global_lock* is updated so that if a public API is called from within the handler, +in *global_lock* is updated so that if a public API is called from within the handler, recursive locking is enabled for that particular thread. On return from the -callback, the lock in *global_lock* is suitably restored. _callback_function_ is the -callback handler to be called, along with all of the appropriate parameters. +call-back, the lock in *global_lock* is suitably restored. _callback_function_ is the +call-back handler to be called, along with all of the appropriate parameters. *Function: coap_lock_callback_ret()* The *coap_lock_callback_ret*() function is similar to *coap_lock_callback*(), -but in addition, it updates the return value from the callback handler function +but in addition, it updates the return value from the call-back handler function in _return_value_. *Function: coap_lock_callback_release()* -The *coap_lock_callback_release*() function is used whenever a callback handler is +The *coap_lock_callback_release*() function is used whenever a call-back handler is getting called, instead of calling the function directly. The lock information in *global_lock* is released so that if a public API is called from within the handler, it can do its own lock. The intent here is to reduce lock contention. On return -from the callback, the lock in *global_lock* is re-locked, but if there is a failure in +from the call-back, the lock in *global_lock* is re-locked, but if there is a failure in re-locking, _failed_statement_ is executed. _callback_function_ is the -callback handler to be called, along with all of the appropriate parameters. +call-back handler to be called, along with all of the appropriate parameters. *Function: coap_lock_callback_ret_release()* The *coap_lock_callback_ret_release*() function is similar to *coap_lock_callback_release*(), but in addition, it updates the return value from the -callback handler function in _return_value_. +call-back handler function in _return_value_. *Function: coap_lock_invert()* @@ -172,6 +186,30 @@ get unlocked, _locking_function_ with all of its parameters called, and then *global_lock* re-locked. If for any reason locking fails, then _failed_statement_ will get executed. +*Function: coap_lock_specific_lock()* + +The *coap_lock_specific_lock*() function is used to lock the specified _lock_ in the +appropriate structue from multiple thread access. If the locking fails for any reason, +then _failed_statement_ will get +executed. + +*Function: coap_lock_specific_unlock()* + +The *coap_lock_specific_unlock*() function is used to unlock the specified _lock_ +so that another thread can access the apprpriate structure. + +*Function: coap_lock_specific_callback_release()* + +The *coap_lock_specific_callback_release*() function is used whenever a call-back +handler is getting called, instead of calling the function directly. The lock information +in *global_lock* is released so that if a public API is called from within the handler, +it can do its own lock. The intent here is to reduce lock contention. On return +from the call-back, the lock in *global_lock* is re-locked, but if there is a failure in +re-locking, _failed_statement_ is executed. _callback_function_ is the +call-back handler to be called, along with all of the appropriate parameters. +_lock_ will get locked before entry to the call-cack and unlocked on return. _lock_ +can be recursively locked by the same thread. + SEE ALSO -------- *coap_supported*(3) diff --git a/man/examples-code-check.c b/man/examples-code-check.c index 9185b299..f87d9968 100644 --- a/man/examples-code-check.c +++ b/man/examples-code-check.c @@ -86,6 +86,9 @@ const char *define_list[] = { "coap_lock_callback_ret(", "coap_lock_callback_ret_release(", "coap_lock_invert(", + "coap_lock_specific_lock(", + "coap_lock_specific_unlock(", + "coap_lock_specific_callback_release(", }; /* xxx *function */ diff --git a/src/coap_net.c b/src/coap_net.c index 4e65e983..0024f07f 100644 --- a/src/coap_net.c +++ b/src/coap_net.c @@ -3482,10 +3482,17 @@ handle_request(coap_context_t *context, coap_session_t *session, coap_pdu_t *pdu coap_log_debug("call custom handler for resource '%*.*s' (3)\n", (int)resource->uri_path->length, (int)resource->uri_path->length, resource->uri_path->s); - coap_lock_callback_release(context, - h(resource, session, pdu, query, response), - /* context is being freed off */ - goto finish); + if (resource->flags & COAP_RESOURCE_SAFE_REQUEST_HANDLER) { + coap_lock_callback_release(context, + h(resource, session, pdu, query, response), + /* context is being freed off */ + goto finish); + } else { + coap_lock_specific_callback_release(&resource->lock, + h(resource, session, pdu, query, response), + /* context is being freed off */ + goto finish); + } } /* Check validity of response code */ @@ -4464,7 +4471,7 @@ coap_startup(void) { coap_started = 1; #if COAP_THREAD_SAFE - coap_lock_init(); + coap_lock_init(&global_lock); #endif /* COAP_THREAD_SAFE */ #if defined(HAVE_WINSOCK2_H) diff --git a/src/coap_openssl.c b/src/coap_openssl.c index 8c338cc1..b81d694d 100644 --- a/src/coap_openssl.c +++ b/src/coap_openssl.c @@ -2504,6 +2504,7 @@ tls_verify_call_back(int preverify_ok, X509_STORE_CTX *ctx) { if (base_buf) { /* base_buf2 gets moved to the end */ assert(i2d_X509(x509, &base_buf2) > 0); + (void)base_buf2; coap_lock_callback_ret(ret, c_session->context, setup_data->validate_cn_call_back(cn, base_buf, length, session, depth, preverify_ok, diff --git a/src/coap_resource.c b/src/coap_resource.c index 874da93b..b932230c 100644 --- a/src/coap_resource.c +++ b/src/coap_resource.c @@ -265,6 +265,9 @@ coap_resource_init(coap_str_const_t *uri_path, int flags) { r = (coap_resource_t *)coap_malloc_type(COAP_RESOURCE, sizeof(coap_resource_t)); if (r) { memset(r, 0, sizeof(coap_resource_t)); +#if COAP_THREAD_SAFE + coap_lock_init(&r->lock); +#endif /* COAP_THREAD_SAFE */ if (!(flags & COAP_RESOURCE_FLAGS_RELEASE_URI)) { /* Need to take a copy if caller is not providing a release request */ @@ -299,6 +302,9 @@ coap_resource_unknown_init2(coap_method_handler_t put_handler, int flags) { r = (coap_resource_t *)coap_malloc_type(COAP_RESOURCE, sizeof(coap_resource_t)); if (r) { memset(r, 0, sizeof(coap_resource_t)); +#if COAP_THREAD_SAFE + coap_lock_init(&r->lock); +#endif /* COAP_THREAD_SAFE */ r->is_unknown = 1; /* Something unlikely to be used, but it shows up in the logs */ r->uri_path = coap_new_str_const(coap_unknown_resource_uri, sizeof(coap_unknown_resource_uri)-1); @@ -333,6 +339,9 @@ coap_resource_proxy_uri_init2(coap_method_handler_t handler, if (r) { size_t i; memset(r, 0, sizeof(coap_resource_t)); +#if COAP_THREAD_SAFE + coap_lock_init(&r->lock); +#endif /* COAP_THREAD_SAFE */ r->is_proxy_uri = 1; /* Something unlikely to be used, but it shows up in the logs */ r->uri_path = coap_new_str_const(coap_proxy_resource_uri, sizeof(coap_proxy_resource_uri)-1); @@ -385,6 +394,9 @@ coap_resource_reverse_proxy_init(coap_method_handler_t handler, int flags) { r = (coap_resource_t *)coap_malloc_type(COAP_RESOURCE, sizeof(coap_resource_t)); if (r) { memset(r, 0, sizeof(coap_resource_t)); +#if COAP_THREAD_SAFE + coap_lock_init(&r->lock); +#endif /* COAP_THREAD_SAFE */ r->is_unknown = 1; /* Something unlikely to be used, but it shows up in the logs */ r->uri_path = coap_new_str_const(coap_rev_proxy_resource_uri, @@ -1215,10 +1227,25 @@ coap_notify_observers(coap_context_t *context, coap_resource_t *r, coap_log_debug("call custom handler for resource '%*.*s' (4)\n", (int)r->uri_path->length, (int)r->uri_path->length, r->uri_path->s); - coap_lock_callback_release(obs->session->context, - h(r, obs->session, obs->pdu, query, response), - /* context is being freed off */ - return); + + /* obs may get deleted during callback (potentially by another thread) */ + if (r->flags & COAP_RESOURCE_SAFE_REQUEST_HANDLER) { + coap_lock_callback_release(obs->session->context, + h(r, obs->session, obs->pdu, query, response), + /* context is being freed off */ + coap_delete_string(query); + coap_delete_pdu(response); + coap_session_release_lkd(obs_session); + return); + } else { + coap_lock_specific_callback_release(&r->lock, + h(r, obs->session, obs->pdu, query, response), + /* context is being freed off */ + coap_delete_string(query); + coap_delete_pdu(response); + coap_session_release_lkd(obs_session); + return); + } /* Check validity of response code */ if (!coap_check_code_class(obs->session, response)) { diff --git a/src/coap_subscribe.c b/src/coap_subscribe.c index 67a49c0d..bee649ce 100644 --- a/src/coap_subscribe.c +++ b/src/coap_subscribe.c @@ -952,19 +952,28 @@ coap_op_dyn_resource_load_disk(coap_context_t *ctx) { raw_packet->length, request)) { goto fail; } - if (!ctx->unknown_resource->handler[request->code-1]) + r = ctx->unknown_resource; + if (!r->handler[request->code-1]) goto fail; response = coap_pdu_init(0, 0, 0, 0); if (!response) goto fail; query = coap_get_query(request); /* Call the application handler to set up this dynamic resource */ - coap_lock_callback_release(ctx, - ctx->unknown_resource->handler[request->code-1](ctx->unknown_resource, - session, request, - query, response), - /* context is being freed off */ - goto fail); + if (r->flags & COAP_RESOURCE_SAFE_REQUEST_HANDLER) { + coap_lock_callback_release(ctx, r->handler[request->code-1](r, + session, request, + query, response), + /* context is being freed off */ + goto fail); + } else { + coap_lock_specific_callback_release(&r->lock, + r->handler[request->code-1](r, + session, request, + query, response), + /* context is being freed off */ + goto fail); + } coap_delete_string(query); query = NULL; coap_delete_pdu(request); diff --git a/src/coap_threadsafe.c b/src/coap_threadsafe.c index d9e1cdcc..f479a95b 100644 --- a/src/coap_threadsafe.c +++ b/src/coap_threadsafe.c @@ -18,66 +18,66 @@ #if COAP_THREAD_SAFE #if COAP_THREAD_RECURSIVE_CHECK void -coap_lock_unlock_func(const char *file, int line) { - assert(coap_thread_pid == global_lock.pid); - if (global_lock.in_callback) { - assert(global_lock.lock_count > 0); - global_lock.lock_count--; +coap_lock_unlock_func(coap_lock_t *lock, const char *file, int line) { + assert(coap_thread_pid == lock->pid); + if (lock->in_callback) { + assert(lock->lock_count > 0); + lock->lock_count--; } else { - global_lock.pid = 0; - global_lock.unlock_file = file; - global_lock.unlock_line = line; - coap_mutex_unlock(&global_lock.mutex); + lock->pid = 0; + lock->unlock_file = file; + lock->unlock_line = line; + coap_mutex_unlock(&lock->mutex); } } int -coap_lock_lock_func(const char *file, int line) { +coap_lock_lock_func(coap_lock_t *lock, const char *file, int line) { if (!coap_started) { /* libcoap not initialized with coap_startup() */ return 0; } - if (coap_mutex_trylock(&global_lock.mutex)) { - if (coap_thread_pid == global_lock.pid) { + if (coap_mutex_trylock(&lock->mutex)) { + if (coap_thread_pid == lock->pid) { /* This thread locked the mutex */ - if (global_lock.in_callback) { + if (lock->in_callback) { /* This is called from within an app callback */ - global_lock.lock_count++; - assert(global_lock.in_callback == global_lock.lock_count); + lock->lock_count++; + assert(lock->in_callback == lock->lock_count); return 1; } else { coap_log_alert("Thread Deadlock: Last %s: %u, this %s: %u\n", - global_lock.lock_file, global_lock.lock_line, file, line); + lock->lock_file, lock->lock_line, file, line); assert(0); } } /* Wait for the other thread to unlock */ - coap_mutex_lock(&global_lock.mutex); + coap_mutex_lock(&lock->mutex); } /* Just got the lock, so should not be in a locked callback */ - assert(!global_lock.in_callback); - global_lock.pid = coap_thread_pid; - global_lock.lock_file = file; - global_lock.lock_line = line; + assert(!lock->in_callback); + lock->pid = coap_thread_pid; + lock->lock_file = file; + lock->lock_line = line; return 1; } #else /* ! COAP_THREAD_RECURSIVE_CHECK */ void -coap_lock_unlock_func(void) { - assert(coap_thread_pid == global_lock.pid); - if (global_lock.in_callback) { - assert(global_lock.lock_count > 0); - global_lock.lock_count--; +coap_lock_unlock_func(coap_lock_t *lock) { + assert(coap_thread_pid == lock->pid); + if (lock->in_callback) { + assert(lock->lock_count > 0); + lock->lock_count--; } else { - global_lock.pid = 0; - coap_mutex_unlock(&global_lock.mutex); + lock->pid = 0; + coap_mutex_unlock(&lock->mutex); } } int -coap_lock_lock_func(void) { +coap_lock_lock_func(coap_lock_t *lock) { if (!coap_started) { /* libcoap not initialized with coap_startup() */ return 0; @@ -86,15 +86,15 @@ coap_lock_lock_func(void) { * Some OS do not have support for coap_mutex_trylock() so * cannot use that here and have to rely on lock-pid being stable */ - if (global_lock.in_callback && coap_thread_pid == global_lock.pid) { - global_lock.lock_count++; - assert(global_lock.in_callback == global_lock.lock_count); + if (lock->in_callback && coap_thread_pid == lock->pid) { + lock->lock_count++; + assert(lock->in_callback == lock->lock_count); return 1; } - coap_mutex_lock(&global_lock.mutex); + coap_mutex_lock(&lock->mutex); /* Just got the lock, so should not be in a locked callback */ - assert(!global_lock.in_callback); - global_lock.pid = coap_thread_pid; + assert(!lock->in_callback); + lock->pid = coap_thread_pid; return 1; } #endif /* ! COAP_THREAD_RECURSIVE_CHECK */ diff --git a/tests/oss-fuzz/Makefile.ci.in b/tests/oss-fuzz/Makefile.ci.in index d23aab1d..063bac46 100644 --- a/tests/oss-fuzz/Makefile.ci.in +++ b/tests/oss-fuzz/Makefile.ci.in @@ -1,6 +1,6 @@ # -*- Mode: Makefile for CI checks -*- # -# Copyright (C) 2019--2025 Olaf Bergmann and others +# Copyright (C) 2019--2026 Olaf Bergmann and others # # SPDX-License-Identifier: BSD-2-Clause # @@ -14,8 +14,8 @@ libcoap?=libcoap-@LIBCOAP_NAME_SUFFIX@ WARNING_CFLAGS?=@WARNING_CFLAGS@ DTLS_CFLAGS?=@DTLS_CFLAGS@ DTLS_LIBS?=@DTLS_LIBS@ -CPPFLAGS=-I$(top_builddir)/include -I$(top_srcdir)/include -CFLAGS=$(WARNING_CFLAGS) $(DTLS_CFLAGS) -std=c99 +CPPFLAGS=-I$(top_builddir)/include -I$(top_srcdir)/include -I$(top_srcdir) +CFLAGS+=$(WARNING_CFLAGS) $(DTLS_CFLAGS) -std=c99 CFLAGS+=-Wno-missing-prototypes -Wno-missing-declarations SOURCES:=$(wildcard *_target.c) @@ -27,12 +27,16 @@ PROGRAMS:=$(patsubst %_target.o, %_fuzzer, $(OBJECTS)) all: $(PROGRAMS) $(OUT)/%.o: %.c - $(CC) $(CPPFLAGS) $(CFLAGS) -c $^ -o $@ + $(CC) $(CPPFLAGS) $(CFLAGS) $(EXTRA_CFLAGS) -c $^ -o $@ %_fuzzer: %_target.o $(LINK.cc) $(CXXFLAGS) $^ $(top_builddir)/.libs/$(libcoap).a $(LDFLAGS) $(LDLIBS) $(DTLS_LIBS) -o $@ + @if [ -d "$(top_srcdir)/tests/oss-fuzz/$(notdir $*)_seed_corpus" ]; then \ + echo "Found seed corpus for $@, creating zip..."; \ + cd $(top_srcdir)/tests/oss-fuzz && zip -rj $@_seed_corpus.zip $(notdir $*)_seed_corpus/; \ + fi check: $(OBJECTS) clean: - @-$(RM) $(OBJECTS) $(PROGRAMS) + @-$(RM) -f $(OBJECTS) $(PROGRAMS)