Skip to contentSkip to conversation

lib/eal: enforce alarm APIs parameters check

PATCH In review Jie Zhou 4 participants 9 messages Jun 24, 2021 – Jul 22, 2021

lore
Share & tools
lore

Jie Zhou

author
#0 lore raw

PATCH 1/1 lib/eal: enforce alarm APIs parameters check

From: Jie Zhou <[email protected]>

lib/eal alarm APIs rte_eal_alarm_set and rte_eal_alarm_cancel on Windows do not check parameters to fail fast for invalid parameters, which captured by DPDK UT alarm_autotest.

Enforce Windows lib/eal alarm APIs parameters check and log invalid parameter info.

---
 lib/eal/windows/eal_alarm.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
lib/eal/windows/eal_alarm.c +23 -0
index f5bf88715a..7bb79ae869 100644
@@ -4,6 +4,7 @@
 
 #include <stdatomic.h>
 #include <stdbool.h>
+#include <inttypes.h>
 
 #include <rte_alarm.h>
 #include <rte_spinlock.h>
@@ -91,6 +92,22 @@ rte_eal_alarm_set(uint64_t us, rte_eal_alarm_callback cb_fn, void *cb_arg)
 	LARGE_INTEGER deadline;
 	int ret;
 
+	/* Check if us is valid */
+	if (us < 1 || us >(UINT64_MAX - US_PER_S)) {
+		RTE_LOG(ERR, EAL, "Invalid us: %" PRIu64 "\n"
+			"Valid us range is 1 to (UINT64_MAX - US_PER_S)\n",
+			us);
+		ret = -EINVAL;
+		goto exit;
+	}
+
+	/* Check if callback is not NULL */
+	if (!cb_fn) {
+		RTE_LOG(ERR, EAL, "NULL callback\n");
+		ret = -EINVAL;
+		goto exit;
+	}
+
 	/* Calculate deadline ASAP, unit of measure = 100ns. */
 	GetSystemTimePreciseAsFileTime(&ft);
 	deadline.LowPart = ft.dwLowDateTime;
@@ -180,6 +197,12 @@ rte_eal_alarm_cancel(rte_eal_alarm_callback cb_fn, void *cb_arg)
 	bool executing;
 
 	removed = 0;
+
+	if (!cb_fn) {
+		RTE_LOG(ERR, EAL, "NULL callback\n");
+		return -EINVAL;
+	}
+
 	do {
 		executing = false;
 
Signature · 1 line
2.31.0.vfs.0.1
#1 lore raw

Hi Jie,

2021-06-23 17:36 (UTC-0700), Jie Zhou:

Quoted 5 lines

From: Jie Zhou <[email protected]>

lib/eal alarm APIs rte_eal_alarm_set and rte_eal_alarm_cancel on Windows do not check parameters to fail fast for invalid parameters, which captured by DPDK UT alarm_autotest.

Please use past tense to describe situation before the patch. A nit, but browsing the log, I see that errors are usually "caught" rather then "captured"; consistency would be nice.

Quoted 3 lines

Enforce Windows lib/eal alarm APIs parameters check and log invalid parameter info.

Fixes tag needed.

Quoted 20 lines
---
 lib/eal/windows/eal_alarm.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
lib/eal/windows/eal_alarm.c +1 -0
index f5bf88715a..7bb79ae869 100644
@@ -4,6 +4,7 @@
 
 #include <stdatomic.h>
 #include <stdbool.h>
+#include <inttypes.h>
 
 #include <rte_alarm.h>
 #include <rte_spinlock.h>

lib/eal/windows/eal_alarm.c rte_eal_alarm_set(uint64_t us, rte_eal_alarm_callback cb_fn, void *cb_arg) · line 96

Quoted 5 lines
lib/eal/windows/eal_alarm.c +2 -0
@@ -91,6 +92,22 @@ rte_eal_alarm_set(uint64_t us, rte_eal_alarm_callback cb_fn, void *cb_arg)
 	LARGE_INTEGER deadline;
 	int ret;
 
+	/* Check if us is valid */
+	if (us < 1 || us >(UINT64_MAX - US_PER_S)) {

This condition is specific to Linux EAL. In fact, it's not very useful even there, because actual upper bound for us depends on current time. No bounds are specified in API description at all.
Windows check would be different, but these considerations remain valid.

Maybe it's alarm_autotest or API description that needs adjustments, but not the implementation. I understand that you're enabling UT for Windows and not correcting tests themselves, but I'm against inserting checks known to be incorrect.

lib/eal/windows/eal_alarm.c rte_eal_alarm_set(uint64_t us, rte_eal_alarm_callback cb_fn, void *cb_arg) · line 99

Quoted 3 lines
lib/eal/windows/eal_alarm.c +3 -0
+		RTE_LOG(ERR, EAL, "Invalid us: %" PRIu64 "\n"
+			"Valid us range is 1 to (UINT64_MAX - US_PER_S)\n",
+			us);

Why does Windows need these messages, while Linux and FreeBSD don't? How will printing API contract here help the user who gets the message?

lib/eal/windows/eal_alarm.c rte_eal_alarm_set(uint64_t us, rte_eal_alarm_callback cb_fn, void *cb_arg) · line 105

Quoted 6 lines
lib/eal/windows/eal_alarm.c +6 -0
+		ret = -EINVAL;
+		goto exit;
+	}
+
+	/* Check if callback is not NULL */
+	if (!cb_fn) {

Pointers (cb_fn) must be checked for NULL explicitly. You won't need an obvious comment after that.

Quoted 11 lines
lib/eal/windows/eal_alarm.c +5 -0
+		RTE_LOG(ERR, EAL, "NULL callback\n");
+		ret = -EINVAL;
+		goto exit;
+	}
+
 	/* Calculate deadline ASAP, unit of measure = 100ns. */
 	GetSystemTimePreciseAsFileTime(&ft);
 	deadline.LowPart = ft.dwLowDateTime;
@@ -180,6 +197,12 @@ rte_eal_alarm_cancel(rte_eal_alarm_callback cb_fn, void *cb_arg)
 	bool executing;
 

lib/eal/windows/eal_alarm.c rte_eal_alarm_cancel(rte_eal_alarm_callback cb_fn, void *cb_arg) · line 208

Quoted 10 lines
lib/eal/windows/eal_alarm.c +6 -0
 	removed = 0;
+
+	if (!cb_fn) {
+		RTE_LOG(ERR, EAL, "NULL callback\n");
+		return -EINVAL;
+	}
+
 	do {
 		executing = false;
 

Please also fix other style issues:
http://mails.dpdk.org/archives/test-report/2021-June/200580.html

#2 lore raw
Quoted 50 lines · Dmitry Kozlyuk

Hi Jie,

2021-06-23 17:36 (UTC-0700), Jie Zhou:

Quoted 5 lines

From: Jie Zhou <[email protected]>

lib/eal alarm APIs rte_eal_alarm_set and rte_eal_alarm_cancel on Windows do not check parameters to fail fast for invalid parameters, which captured by DPDK UT alarm_autotest.

Please use past tense to describe situation before the patch. A nit, but browsing the log, I see that errors are usually "caught" rather then "captured"; consistency would be nice.

Quoted 3 lines

Enforce Windows lib/eal alarm APIs parameters check and log invalid parameter info.

Fixes tag needed.

Quoted 20 lines
---
 lib/eal/windows/eal_alarm.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
lib/eal/windows/eal_alarm.c +1 -0
index f5bf88715a..7bb79ae869 100644
@@ -4,6 +4,7 @@
 
 #include <stdatomic.h>
 #include <stdbool.h>
+#include <inttypes.h>
 
 #include <rte_alarm.h>
 #include <rte_spinlock.h>

lib/eal/windows/eal_alarm.c rte_eal_alarm_set(uint64_t us, rte_eal_alarm_callback cb_fn, void *cb_arg) · line 96

Quoted 5 lines
lib/eal/windows/eal_alarm.c +2 -0
@@ -91,6 +92,22 @@ rte_eal_alarm_set(uint64_t us, rte_eal_alarm_callback cb_fn, void *cb_arg)
 	LARGE_INTEGER deadline;
 	int ret;
 
+	/* Check if us is valid */
+	if (us < 1 || us >(UINT64_MAX - US_PER_S)) {

This condition is specific to Linux EAL. In fact, it's not very useful even there, because actual upper bound for us depends on current time. No bounds are specified in API description at all.
Windows check would be different, but these considerations remain valid.

Quoted 2 lines

Maybe it's alarm_autotest or API description that needs adjustments, but not the implementation.

i agree with your assessment. it's a bit silly to test a range constraint where the range is just some arbitrary range and not the real range. changing the implementation to calculate the "real" valid range isn't practical. i'm sure linux implementors would argue that catching some extremely out of range values is better than none?

i guess a correct test would would calculate the valid range and test against that but as per above the implementation won't pass the test.

so we are left with

* matching the range imposed in the linux implementation so we pass the test as is.

* don't run the test with the input data that exercises this bogus range constraint.

i guess based on your comments you prefer the latter? so is our action here to submit a patch for the test that doesn't test this range conditionally on execenv windows?

Quoted 10 lines

I understand that you're enabling UT for Windows
and not correcting tests themselves, but I'm against inserting checks known to be incorrect.

lib/eal/windows/eal_alarm.c rte_eal_alarm_set(uint64_t us, rte_eal_alarm_callback cb_fn, void *cb_arg) · line 99

Quoted 3 lines
lib/eal/windows/eal_alarm.c +3 -0
+		RTE_LOG(ERR, EAL, "Invalid us: %" PRIu64 "\n"
+			"Valid us range is 1 to (UINT64_MAX - US_PER_S)\n",
+			us);

Why does Windows need these messages, while Linux and FreeBSD don't? How will printing API contract here help the user who gets the message?

i'll discuss this subject to whether or not we remove the above range check. for now i'll defer discussion.

Quoted 35 lines

lib/eal/windows/eal_alarm.c rte_eal_alarm_set(uint64_t us, rte_eal_alarm_callback cb_fn, void *cb_arg) · line 105

Quoted 7 lines
lib/eal/windows/eal_alarm.c +6 -0
+		ret = -EINVAL;
+		goto exit;
+	}
+
+	/* Check if callback is not NULL */
+	if (!cb_fn) {

Pointers (cb_fn) must be checked for NULL explicitly. You won't need an obvious comment after that.

Quoted 11 lines
lib/eal/windows/eal_alarm.c +5 -0
+		RTE_LOG(ERR, EAL, "NULL callback\n");
+		ret = -EINVAL;
+		goto exit;
+	}
+
 	/* Calculate deadline ASAP, unit of measure = 100ns. */
 	GetSystemTimePreciseAsFileTime(&ft);
 	deadline.LowPart = ft.dwLowDateTime;
@@ -180,6 +197,12 @@ rte_eal_alarm_cancel(rte_eal_alarm_callback cb_fn, void *cb_arg)
 	bool executing;
 

lib/eal/windows/eal_alarm.c rte_eal_alarm_cancel(rte_eal_alarm_callback cb_fn, void *cb_arg) · line 208

Quoted 10 lines
lib/eal/windows/eal_alarm.c +6 -0
 	removed = 0;
+
+	if (!cb_fn) {
+		RTE_LOG(ERR, EAL, "NULL callback\n");
+		return -EINVAL;
+	}
+
 	do {
 		executing = false;
 

Please also fix other style issues:
http://mails.dpdk.org/archives/test-report/2021-June/200580.html

#3 lore raw

2021-07-01 09:21 (UTC-0700), Tyler Retzlaff:

Quoted 14 lines · Dmitry Kozlyuk
Quoted 4 lines

Hi Jie,

2021-06-23 17:36 (UTC-0700), Jie Zhou:

Quoted 1 line

From: Jie Zhou <[email protected]>

[...]

Quoted 8 lines

lib/eal/windows/eal_alarm.c rte_eal_alarm_set(uint64_t us, rte_eal_alarm_callback cb_fn, void *cb_arg) · line 96

Quoted 2 lines
lib/eal/windows/eal_alarm.c +2 -0
+	/* Check if us is valid */
+	if (us < 1 || us >(UINT64_MAX - US_PER_S)) {  

This condition is specific to Linux EAL. In fact, it's not very useful even there, because actual upper bound for us depends on current time. No bounds are specified in API description at all.
Windows check would be different, but these considerations remain valid.

Quoted 8 lines
Quoted 2 lines

Maybe it's alarm_autotest or API description that needs adjustments, but not the implementation.

i agree with your assessment. it's a bit silly to test a range constraint where the range is just some arbitrary range and not the real range. changing the implementation to calculate the "real" valid range isn't practical. i'm sure linux implementors would argue that catching some extremely out of range values is better than none?

Why we care about overflow?
1. It likely indicates a bug in the app.
2. Alarm is can to be scheduled to some time in the past and fire immediately, which means API did not do what it promises. I see the only way to tell the interval is incorrect if it gives (would give) time in the past when added to the current time. But this is an implementation detail and does not need testing. A separate patch can be submitted to change behavior for all OS.

Quoted 8 lines

i guess a correct test would would calculate the valid range and test against that but as per above the implementation won't pass the test.

so we are left with

* matching the range imposed in the linux implementation so we pass the test as is.

It would be the worst thing one could do, defying the purpose of unit tests.

Quoted 6 lines

* don't run the test with the input data that exercises this bogus range constraint.

i guess based on your comments you prefer the latter? so is our action here to submit a patch for the test that doesn't test this range conditionally on execenv windows?

Yes, please remove this check from the test as it verifies no contract.

#4 lore raw
Quoted 37 lines · Dmitry Kozlyuk

2021-07-01 09:21 (UTC-0700), Tyler Retzlaff:

Quoted 14 lines · Dmitry Kozlyuk
Quoted 4 lines

Hi Jie,

2021-06-23 17:36 (UTC-0700), Jie Zhou:

Quoted 1 line

From: Jie Zhou <[email protected]>

[...]

Quoted 8 lines

lib/eal/windows/eal_alarm.c rte_eal_alarm_set(uint64_t us, rte_eal_alarm_callback cb_fn, void *cb_arg) · line 96

Quoted 2 lines
lib/eal/windows/eal_alarm.c +2 -0
+	/* Check if us is valid */
+	if (us < 1 || us >(UINT64_MAX - US_PER_S)) {  

This condition is specific to Linux EAL. In fact, it's not very useful even there, because actual upper bound for us depends on current time. No bounds are specified in API description at all.
Windows check would be different, but these considerations remain valid.

Quoted 8 lines
Quoted 2 lines

Maybe it's alarm_autotest or API description that needs adjustments, but not the implementation.

i agree with your assessment. it's a bit silly to test a range constraint where the range is just some arbitrary range and not the real range. changing the implementation to calculate the "real" valid range isn't practical. i'm sure linux implementors would argue that catching some extremely out of range values is better than none?

Why we care about overflow?
1. It likely indicates a bug in the app.
2. Alarm is can to be scheduled to some time in the past and fire immediately, which means API did not do what it promises. I see the only way to tell the interval is incorrect if it gives (would give) time in the past when added to the current time. But this is an implementation detail and does not need testing. A separate patch can be submitted to change behavior for all OS.

Quoted 4 lines

i guess a correct test would would calculate the valid range and test against that but as per above the implementation won't pass the test.

Quoted 6 lines
Quoted 4 lines

so we are left with

* matching the range imposed in the linux implementation so we pass the test as is.

It would be the worst thing one could do, defying the purpose of unit tests.

agreed.

Quoted 9 lines
Quoted 7 lines

* don't run the test with the input data that exercises this bogus range constraint.

i guess based on your comments you prefer the latter? so is our action here to submit a patch for the test that doesn't test this range conditionally on execenv windows?

Yes, please remove this check from the test as it verifies no contract.

we'll go with this.

thanks

author
#5 lore raw
Quoted 55 lines · Tyler Retzlaff
Quoted 37 lines · Dmitry Kozlyuk

2021-07-01 09:21 (UTC-0700), Tyler Retzlaff:

Quoted 14 lines · Dmitry Kozlyuk
Quoted 4 lines

Hi Jie,

2021-06-23 17:36 (UTC-0700), Jie Zhou:

Quoted 1 line

From: Jie Zhou <[email protected]>

[...]

Quoted 8 lines

lib/eal/windows/eal_alarm.c rte_eal_alarm_set(uint64_t us, rte_eal_alarm_callback cb_fn, void *cb_arg) · line 96

Quoted 2 lines
lib/eal/windows/eal_alarm.c +2 -0
+	/* Check if us is valid */
+	if (us < 1 || us >(UINT64_MAX - US_PER_S)) {  

This condition is specific to Linux EAL. In fact, it's not very useful even there, because actual upper bound for us depends on current time. No bounds are specified in API description at all.
Windows check would be different, but these considerations remain valid.

Quoted 8 lines
Quoted 2 lines

Maybe it's alarm_autotest or API description that needs adjustments, but not the implementation.

i agree with your assessment. it's a bit silly to test a range constraint where the range is just some arbitrary range and not the real range. changing the implementation to calculate the "real" valid range isn't practical. i'm sure linux implementors would argue that catching some extremely out of range values is better than none?

Why we care about overflow?
1. It likely indicates a bug in the app.
2. Alarm is can to be scheduled to some time in the past and fire immediately, which means API did not do what it promises. I see the only way to tell the interval is incorrect if it gives (would give) time in the past when added to the current time. But this is an implementation detail and does not need testing. A separate patch can be submitted to change behavior for all OS.

Quoted 4 lines

i guess a correct test would would calculate the valid range and test against that but as per above the implementation won't pass the test.

Quoted 6 lines
Quoted 4 lines

so we are left with

* matching the range imposed in the linux implementation so we pass the test as is.

It would be the worst thing one could do, defying the purpose of unit tests.

agreed.

Quoted 8 lines
Quoted 7 lines

* don't run the test with the input data that exercises this bogus range constraint.

i guess based on your comments you prefer the latter? so is our action here to submit a patch for the test that doesn't test this range conditionally on execenv windows?

Quoted 5 lines
Quoted 1 line

Yes, please remove this check from the test as it verifies no contract.

we'll go with this.

thanks

Thanks Dmitry and Tyler. Will address these in V2.

author
#6 lore raw

PATCH v2 1/1 eal/windows: enforce alarm APIs parameter check

In reply to Jie Zhou

eal/windows alarm APIs rte_eal_alarm_set and rte_eal_alarm_cancel did not check parameters to fail fast for invalid parameters, which caught by DPDK UT alarm_autotest.

Enforce eal/windows alarm APIs parameter check to fail fast for invalid parameters.

Fixes f4cbdbc7fbd2 ("eal/windows: implement alarm API")

---
V2 changes:
- Remove API parameter check on arbitrary 'us' range
- Do explicit NULL cb_fn check

---
 lib/eal/windows/eal_alarm.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
lib/eal/windows/eal_alarm.c +12 -0
index f5bf88715a..e5dc54efb8 100644
@@ -91,6 +91,12 @@ rte_eal_alarm_set(uint64_t us, rte_eal_alarm_callback cb_fn, void *cb_arg)
 	LARGE_INTEGER deadline;
 	int ret;
 
+	if (cb_fn == NULL) {
+		RTE_LOG(ERR, EAL, "NULL callback\n");
+		ret = -EINVAL;
+		goto exit;
+	}
+
 	/* Calculate deadline ASAP, unit of measure = 100ns. */
 	GetSystemTimePreciseAsFileTime(&ft);
 	deadline.LowPart = ft.dwLowDateTime;
@@ -180,6 +186,12 @@ rte_eal_alarm_cancel(rte_eal_alarm_callback cb_fn, void *cb_arg)
 	bool executing;
 
 	removed = 0;
+
+	if (cb_fn == NULL) {
+		RTE_LOG(ERR, EAL, "NULL callback\n");
+		return -EINVAL;
+	}
+
 	do {
 		executing = false;
 
Signature · 1 line
2.31.0.vfs.0.1
Acked-by on v2 1/1
#7 lore raw

Re: [dpdk-dev] [PATCH v2] eal/windows: enforce alarm APIs parameter check

2021-07-07 13:25 (UTC-0700), Jie Zhou:

Quoted 41 lines

eal/windows alarm APIs rte_eal_alarm_set and rte_eal_alarm_cancel did not check parameters to fail fast for invalid parameters, which caught by DPDK UT alarm_autotest.

Enforce eal/windows alarm APIs parameter check to fail fast for invalid parameters.

Fixes f4cbdbc7fbd2 ("eal/windows: implement alarm API")

---
V2 changes:
- Remove API parameter check on arbitrary 'us' range
- Do explicit NULL cb_fn check

---
 lib/eal/windows/eal_alarm.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
lib/eal/windows/eal_alarm.c +6 -0
index f5bf88715a..e5dc54efb8 100644
@@ -91,6 +91,12 @@ rte_eal_alarm_set(uint64_t us, rte_eal_alarm_callback cb_fn, void *cb_arg)
 	LARGE_INTEGER deadline;
 	int ret;
 
+	if (cb_fn == NULL) {
+		RTE_LOG(ERR, EAL, "NULL callback\n");
+		ret = -EINVAL;
+		goto exit;
+	}
+
 	/* Calculate deadline ASAP, unit of measure = 100ns. */
 	GetSystemTimePreciseAsFileTime(&ft);
 	deadline.LowPart = ft.dwLowDateTime;
@@ -180,6 +186,12 @@ rte_eal_alarm_cancel(rte_eal_alarm_callback cb_fn, void *cb_arg)
 	bool executing;
 

lib/eal/windows/eal_alarm.c rte_eal_alarm_cancel(rte_eal_alarm_callback cb_fn, void *cb_arg) · line 197

Quoted 10 lines
lib/eal/windows/eal_alarm.c +6 -0
 	removed = 0;
+
+	if (cb_fn == NULL) {
+		RTE_LOG(ERR, EAL, "NULL callback\n");
+		return -EINVAL;
+	}
+
 	do {
 		executing = false;
 
#8 lore raw

Re: [dpdk-dev] [PATCH v2] eal/windows: enforce alarm APIs parameter check

21/07/2021 17:28, Dmitry Kozlyuk:

Quoted 8 lines

2021-07-07 13:25 (UTC-0700), Jie Zhou:

Quoted 7 lines

eal/windows alarm APIs rte_eal_alarm_set and rte_eal_alarm_cancel did not check parameters to fail fast for invalid parameters, which caught by DPDK UT alarm_autotest.

Enforce eal/windows alarm APIs parameter check to fail fast for invalid parameters.

Quoted 6 lines
Quoted 4 lines
Fixes f4cbdbc7fbd2 ("eal/windows: implement alarm API")

Applied with title "eal/windows: check callback parameter of alarm functions"