Skip to contentSkip to conversation

examples/vm_power_manager: fix overflowed value

PATCH Applied Reshma Pattan 3 participants 3 messages Apr 11, 2019 – Apr 22, 2019

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 examples/vm_power_manager: fix overflowed value

Fix the data type of last_branches, last_branch_misses from unit32_t to uint64_t, and for hits_diff, miss_diff from int to int64_t respectively to fix possible overflow or truncation.

Coverity issue: 337677

Fixes 4b1a631b8a ("examples/vm_power: add oob monitoring functions")
---
 examples/vm_power_manager/oob_monitor_x86.c | 23 ++++++++++++++++-----
 1 file changed, 18 insertions(+), 5 deletions(-)
examples/vm_power_manager/oob_monitor_x86.c +18 -5
index 589c604e5..ebd96b205 100644
@@ -33,10 +33,10 @@ static float
 apply_policy(int core)
 {
 	struct core_info *ci;
-	uint64_t counter;
+	uint64_t counter = 0;
 	uint64_t branches, branch_misses;
-	uint32_t last_branches, last_branch_misses;
-	int hits_diff, miss_diff;
+	uint64_t last_branches, last_branch_misses;
+	int64_t hits_diff, miss_diff;
 	float ratio;
 	int ret;
 
@@ -54,6 +54,7 @@ apply_policy(int core)
 				core);
 	branches = counter;
 
+	counter = 0;
 	ret = pread(ci->cd[core].msr_fd, &counter,
 			sizeof(counter), IA32_PERFCTR1);
 	if (ret < 0)
@@ -66,13 +67,25 @@ apply_policy(int core)
 	ci->cd[core].last_branches = branches;
 	ci->cd[core].last_branch_misses = branch_misses;
 
-	hits_diff = (int)branches - (int)last_branches;
+	/*
+	 * Intentional right shift to make MSB 0 to avoid
+	 * possible signed overflow or truncation.
+	 */
+	branches >>= 1;
+	last_branches >>= 1;
+	hits_diff = (int64_t)branches - (int64_t)last_branches;
 	if (hits_diff <= 0) {
 		/* Likely a counter overflow condition, skip this round */
 		return -1.0;
 	}
 
-	miss_diff = (int)branch_misses - (int)last_branch_misses;
+	/*
+	 * Intentional right shift to make MSB 0 to avoid
+	 * possible signed overflow or truncation.
+	 */
+	branch_misses >>= 1;
+	last_branch_misses >>= 1;
+	miss_diff = (int64_t)branch_misses - (int64_t)last_branch_misses;
 	if (miss_diff <= 0) {
 		/* Likely a counter overflow condition, skip this round */
 		return -1.0;
Signature · 1 line
2.17.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 5 lines · Reshma Pattan

Fix the data type of last_branches, last_branch_misses from unit32_t to uint64_t, and for hits_diff, miss_diff from int to int64_t respectively to fix possible overflow or truncation.

Quoted 7 lines

Coverity issue: 337677

Fixes 4b1a631b8a ("examples/vm_power: add oob monitoring functions")

---

Signature · 2 lines
Thanks,
Anatoly
#2 lore raw

11/04/2019 14:07, Burakov, Anatoly:

Quoted 6 lines · Reshma Pattan
Quoted 5 lines

Fix the data type of last_branches, last_branch_misses from unit32_t to uint64_t, and for hits_diff, miss_diff from int to int64_t respectively to fix possible overflow or truncation.

Quoted 8 lines
Quoted 6 lines

Coverity issue: 337677

Fixes 4b1a631b8a ("examples/vm_power: add oob monitoring functions")

Applied, thanks