Skip to content

Commit 5468b13

Browse files
committed
Address comments from copilot
1 parent 9656bae commit 5468b13

File tree

6 files changed

+27
-8
lines changed

6 files changed

+27
-8
lines changed

docs/draft/timeout.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ posixTimeoutContext posixCtx = {0};
1111
posixTimeoutConfig posixCfg = {.timeoutUs = WH_SEC_TO_USEC(5)};
1212
whTimeoutCb timeoutCbTable = POSIX_TIMEOUT_CB;
1313

14+
/* NOTE: The callback table, platform context, and expiredCtx must remain valid
15+
* for the lifetime of the whCommClient/whTimeout instance. Do not use stack
16+
* locals that go out of scope while the client is still in use. */
1417
whTimeoutConfig timeoutCfg = {
1518
.cb = &timeoutCbTable, /* platform callback table */
1619
.context = &posixCtx, /* platform context */

port/posix/posix_timeout.c

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,33 @@
2020
* port/posix/posix_timeout.c
2121
*
2222
* POSIX implementation of the wolfHSM timeout abstraction.
23-
* Uses posixGetTime() from posix_time.h for time measurement.
23+
* Uses CLOCK_MONOTONIC for time measurement.
2424
*/
2525

2626
#include "wolfhsm/wh_settings.h"
2727

2828
#ifdef WOLFHSM_CFG_ENABLE_TIMEOUT
2929

3030
#include <stddef.h>
31+
#include <time.h>
3132

3233
#include "wolfhsm/wh_error.h"
3334
#include "wolfhsm/wh_timeout.h"
3435

35-
#include "port/posix/posix_time.h"
3636
#include "port/posix/posix_timeout.h"
3737

38+
/* Use CLOCK_MONOTONIC for timeout measurement to avoid issues with wall-clock
39+
* adjustments (NTP, manual changes, etc.) that could cause spurious expirations
40+
* or overly long timeouts. */
41+
static uint64_t _getMonotonicTimeUs(void)
42+
{
43+
struct timespec ts;
44+
if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) {
45+
return 0;
46+
}
47+
return (uint64_t)ts.tv_sec * 1000000ULL + (uint64_t)(ts.tv_nsec / 1000);
48+
}
49+
3850
int posixTimeout_Init(void* context, const void* config)
3951
{
4052
posixTimeoutContext* ctx = (posixTimeoutContext*)context;
@@ -107,7 +119,7 @@ int posixTimeout_Start(void* context)
107119
return WH_ERROR_NOTREADY;
108120
}
109121

110-
ctx->startUs = posixGetTime();
122+
ctx->startUs = _getMonotonicTimeUs();
111123
ctx->started = 1;
112124

113125
return WH_ERROR_OK;
@@ -150,7 +162,7 @@ int posixTimeout_Expired(void* context, int* expired)
150162
return WH_ERROR_OK;
151163
}
152164

153-
nowUs = posixGetTime();
165+
nowUs = _getMonotonicTimeUs();
154166
*expired = ((nowUs - ctx->startUs) >= ctx->timeoutUs) ? 1 : 0;
155167

156168
return WH_ERROR_OK;

port/posix/posix_timeout.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
* port/posix/posix_timeout.h
2121
*
2222
* POSIX implementation of the wolfHSM timeout abstraction.
23-
* Uses posixGetTime() for time measurement.
23+
* Uses CLOCK_MONOTONIC for time measurement.
2424
*/
2525

2626
#ifndef PORT_POSIX_POSIX_TIMEOUT_H_

src/wh_comm.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,9 +189,13 @@ int wh_CommClient_RecvResponse(whCommClient* context,
189189
}
190190
#ifdef WOLFHSM_CFG_ENABLE_TIMEOUT
191191
else if (rc == WH_ERROR_NOTREADY) {
192-
if (wh_Timeout_Expired(&context->respTimeout)) {
192+
int expired = wh_Timeout_Expired(&context->respTimeout);
193+
if (expired > 0) {
193194
rc = WH_ERROR_TIMEOUT;
194195
}
196+
else if (expired < 0) {
197+
rc = expired;
198+
}
195199
}
196200
#endif
197201
return rc;

test/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ DEF += -DWOLFHSM_CFG_TEST_POSIX
3636
ARCHFLAGS ?=
3737

3838
# Enable extra C compiler warnings
39-
CFLAGS_EXTRA = -Werror -Wall -Wextra -g
39+
CFLAGS_EXTRA = -Werror -Wall -Wextra
4040
# Place functions / data into separate sections to allow unused code removal
4141
CFLAGS_EXTRA += -ffunction-sections -fdata-sections
4242

wolfhsm/wh_timeout.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
#include <stdint.h>
4141

4242
/* Time conversion macros */
43-
#define WH_MSEC_TO_USEC(usec) ((usec) * (1000ULL))
43+
#define WH_MSEC_TO_USEC(ms) ((ms) * (1000ULL))
4444
#define WH_SEC_TO_USEC(sec) ((sec) * (1000000ULL))
4545
#define WH_MIN_TO_USEC(min) ((min) * (WH_SEC_TO_USEC(60)))
4646

0 commit comments

Comments
 (0)