Skip to content

Commit a5ff52b

Browse files
committed
RDKB-63722:Analyze and fix/mitigate memory leaks from curl_easy_perform calls
Reason for change: Added the LeakTest for https get and post operations wiht matching production and edge-case usage Test Procedure: please refer the ticket comments Risks: Medium Signed-off-by: Thamim Razith Abbas Ali <tabbas651@cable.comcast.com>
1 parent d1b156b commit a5ff52b

8 files changed

Lines changed: 14728 additions & 0 deletions

leaktestapp/Makefile

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
CC = gcc
2+
CFLAGS = -Wall -Wextra -g -O0
3+
LDFLAGS = -lcurl -lpthread
4+
5+
TARGET = memory_verifier
6+
7+
.PHONY: all clean run help valgrind-investigate
8+
9+
all: $(TARGET)
10+
@echo ""
11+
@echo "✓ Memory Verifier built successfully: $(TARGET)"
12+
@echo " Run with: make run"
13+
@echo ""
14+
15+
$(TARGET): memory_verifier.c
16+
$(CC) $(CFLAGS) -o $(TARGET) memory_verifier.c $(LDFLAGS)
17+
18+
run: $(TARGET)
19+
@echo "🔍 Running Memory Leak Verification..."
20+
@echo ""
21+
./$(TARGET)
22+
23+
valgrind-investigate: $(TARGET)
24+
@echo "🧪 Running SSL Memory Investigation..."
25+
@echo ""
26+
@if [ -f "investigate_ssl_memory.sh" ]; then \
27+
chmod +x investigate_ssl_memory.sh 2>/dev/null || true; \
28+
./investigate_ssl_memory.sh; \
29+
elif [ -f "investigate_ssl_memory.bat" ]; then \
30+
cmd //c investigate_ssl_memory.bat; \
31+
else \
32+
echo "❌ Investigation script not found"; \
33+
exit 1; \
34+
fi
35+
36+
clean:
37+
rm -f $(TARGET)
38+
rm -f *.log *.out *.txt valgrind_*.sh
39+
@echo "✓ Cleaned memory verifier and investigation files"
40+
41+
help:
42+
@echo "Memory Leak Verifier with Valgrind Integration"
43+
@echo "=============================================="
44+
@echo ""
45+
@echo "Standalone tool to verify curl/OpenSSL has no memory leaks"
46+
@echo ""
47+
@echo "Commands:"
48+
@echo " make - Build the verifier"
49+
@echo " make run - Build and run verification (RECOMMENDED)"
50+
@echo " make valgrind-investigate - Run SSL memory investigation (NEW)"
51+
@echo " make clean - Remove build artifacts and logs"
52+
@echo " make help - Show this help"
53+
@echo ""
54+
@echo "Quick Start:"
55+
@echo " make run # Standard memory verification"
56+
@echo " make valgrind-investigate # Investigate SSL memory increase"
57+
@echo ""
58+
@echo "Standard Tests:"
59+
@echo " ✓ Basic HTTP client memory growth"
60+
@echo " ✓ Handle reuse and connection pooling"
61+
@echo " ✓ Memory stability over time"
62+
@echo " ✓ Library version compatibility"
63+
@echo ""
64+
@echo "NEW: Valgrind Investigation (for SSL 3484KB→3608KB issue):"
65+
@echo " ✓ Pool baseline analysis"
66+
@echo " ✓ Reset fix impact analysis"
67+
@echo " ✓ Comparative leak assessment"
68+
@echo " ✓ SSL/OpenSSL deep investigation"
69+
@echo ""
70+
@echo "Individual Valgrind Tests:"
71+
@echo " ./memory_verifier --valgrind-pool # Baseline"
72+
@echo " ./memory_verifier --valgrind-reset # Reset fix"
73+
@echo " ./memory_verifier --post-analysis # Compare"

leaktestapp/README.md

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
# Memory Verifier
2+
3+
**Standalone tool to verify your curl/OpenSSL configuration has no memory leaks.**
4+
5+
## Quick Start
6+
7+
```bash
8+
cd memory-verifier
9+
make run
10+
```
11+
12+
## Purpose
13+
14+
This is a **completely separate test application** that verifies your curl 7.81.0 + OpenSSL 3.0.2 setup has no memory leaks. It's independent from the main telemetry leak detection tests.
15+
16+
17+
## What It Does
18+
19+
Runs multiple independent verification tests, matching production and edge-case usage:
20+
21+
### Standard Tests
22+
23+
- **Version Check**: Validates your libcurl and OpenSSL versions for known issues.
24+
- **Connection Pool Pattern**: Simulates production connection pool usage with detailed per-iteration memory tracking. Pool size is configurable via `T2_CONNECTION_POOL_SIZE`.
25+
- **POST Memory Leak Test**: Runs 100 POST requests to detect memory growth in typical POST usage.
26+
27+
### Valgrind-Integrated Tests
28+
29+
- **Valgrind Pool Test**: Runs the connection pool pattern under Valgrind to detect leaks and profile operational memory usage.
30+
- **Valgrind SSL/OpenSSL Investigation**: Deep-dive memory profiling of SSL/OpenSSL usage, focusing on known memory growth patterns.
31+
32+
### Logging & Analysis
33+
34+
- Per-iteration logs: `connection_pool_iterations.log` for detailed memory and operation tracking.
35+
- Valgrind and Massif outputs: `valgrind_pool_baseline.log`, `massif_pool_baseline.out`, `massif_ssl_investigation.out`, and human-readable reports if `ms_print` is available.
36+
37+
### Environment Variables
38+
39+
- `T2_CONNECTION_POOL_SIZE`: Set the connection pool size (default: 2, min: 1, max: 5).
40+
- `T2_TEST_ITERATIONS`: Set the number of pool test iterations (default: 50, max: 200).
41+
- `VALGRIND_FAST_MODE`: If set, reduces iterations and delays for faster Valgrind runs.
42+
43+
### CLI Options (see `--help`)
44+
45+
```
46+
--version, -v Run version-specific assessment
47+
--pool, -p Run connection pool pattern test
48+
--post, -o Run POST memory leak test
49+
--valgrind-pool Valgrind analysis of pool without reset
50+
--valgrind-ssl Investigate SSL/OpenSSL memory patterns
51+
--all, -a Run all standard tests (default)
52+
--all-valgrind Run all valgrind-integrated tests
53+
--help Show help message with all options
54+
```
55+
56+
See the source or run `./memory_verifier --help` for the full list of options and examples.
57+
58+
59+
## Expected Results
60+
61+
**Success (No Leaks):**
62+
```
63+
��� FINAL VERIFICATION RESULT
64+
Tests Run: <N>
65+
Passed: <N>
66+
Failed: 0
67+
68+
✅ VERDICT: NO MEMORY LEAKS DETECTED
69+
��� YOUR HTTP CLIENT IS PRODUCTION-READY!
70+
```
71+
72+
⚠️ **Acceptable (Monitor):**
73+
- Some warnings but no failures
74+
- Memory growth within acceptable thresholds (see logs for details)
75+
76+
**Issues Found:**
77+
- Test failures indicate potential memory problems
78+
- Requires investigation (see Valgrind logs and summary)
79+
80+
## Directory Structure
81+
82+
```
83+
memory-verifier/ # <- You are here
84+
├── memory_verifier.c # Standalone verification tool
85+
├── Makefile # Independent build system
86+
└── README.md # This file
87+
88+
../leak_detection/ # <- Your main test suite (separate)
89+
├── curl_leak_test.c # Main telemetry tests
90+
├── multicurlinterface.c # Your fixed HTTP client
91+
└── Makefile # Main test build system
92+
```
93+
94+
95+
## Build & Run
96+
97+
```bash
98+
make # Build only
99+
make run # Build and run (recommended)
100+
make clean # Clean build artifacts
101+
make help # Show detailed help
102+
103+
104+
# Run with custom options
105+
./memory_verifier --pool # Run connection pool test
106+
T2_CONNECTION_POOL_SIZE=3 ./memory_verifier --pool # Pool size 3
107+
T2_TEST_ITERATIONS=100 ./memory_verifier --pool # 100 iterations
108+
./memory_verifier --valgrind-pool # Valgrind pool analysis
109+
./memory_verifier --valgrind-ssl # Deep SSL/OpenSSL investigation
110+
111+
# Automated Dynamic Analysis (GDB/SSL investigation)
112+
./run_dynamic_analysis.sh # Run automated dynamic SSL/OpenSSL memory analysis
113+
./run_dynamic_analysis.sh --help # Show script options and usage
114+
```
115+
116+
117+
## Integration & Analysis
118+
119+
120+
This tool is **completely independent** from your main telemetry code.
121+
122+
- **For production verification**: Use this tool
123+
- **For telemetry testing**: Use `../leak_detection/`
124+
- **For detailed analysis**: Use `cd ../leak_detection && make valgrind`
125+
- **For automated dynamic SSL/OpenSSL analysis**: Use `run_dynamic_analysis.sh` (runs GDB-based tracing, logging, and summary for SSL memory investigation)
126+
127+
128+
### Log Files & Outputs
129+
130+
- `connection_pool_iterations.log`: Per-iteration memory and operation log
131+
- `valgrind_pool_baseline.log`: Valgrind leak report for pool test
132+
- `massif_pool_baseline.out`: Massif memory profile for pool test
133+
- `massif_ssl_investigation.out`: Massif memory profile for SSL investigation
134+
- `massif_ssl_report.txt`: Human-readable SSL memory profile (if `ms_print` is available)
135+
- `dynamic_analysis.log`: Main log for run_dynamic_analysis.sh (GDB/SSL trace summary)
136+
- `dynamic_analysis_summary.log`: High-level summary of dynamic analysis results
137+
## run_dynamic_analysis.sh
138+
139+
This script automates dynamic SSL/OpenSSL memory analysis using GDB. It runs the memory_verifier in a controlled environment, traces SSL/OpenSSL allocation functions, and summarizes memory growth and leak patterns.
140+
141+
**Usage:**
142+
143+
```bash
144+
./run_dynamic_analysis.sh # Run full dynamic analysis suite
145+
./run_dynamic_analysis.sh --help # Show all options and usage
146+
```
147+
148+
**Features:**
149+
- Automated GDB tracing of SSL/OpenSSL allocation and cleanup functions
150+
- Per-iteration and summary logging
151+
- Timeout and iteration controls (via environment variables)
152+
- Correlates memory growth with SSL connection pool operations
153+
- Produces logs for management and engineering review
154+
155+
See the script header or run with `--help` for full details and advanced options.
156+
157+
158+
## Requirements
159+
160+
- Linux environment (WSL2 supported)
161+
- libcurl development headers
162+
- Valgrind (for valgrind-integrated tests)
163+
- Internet connection (tests use httpbin.org)
164+
165+
## Troubleshooting & Tips
166+
167+
- If you see memory growth, check the logs and Valgrind output for SSL/OpenSSL or curl-related leaks.
168+
- Use environment variables to tune pool size and iteration count for your environment.
169+
- For deep SSL/OpenSSL memory analysis, use the `--valgrind-ssl` option and review `massif_ssl_report.txt`.
170+
171+
For full CLI usage and test descriptions, run:
172+
173+
```bash
174+
./memory_verifier --help
175+
```
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
=== 1. GDB Call Stack Analysis ===
2+
Running GDB dynamic analysis using gdb_batch_script.gdb...
3+
Setting 30-second timeout with 50 iterations...
4+
Setting T2_TEST_ITERATIONS=50 for this test...
5+
✅ GDB analysis completed normally
6+
=== Dynamic Analysis Summary ===
7+
Analysis completed at: Wed Mar 11 01:01:09 PM EDT 2026
8+
9+
📁 Generated Files:
10+
-rw-r--r-- 1 root root 336 Mar 11 13:01 dynamic_analysis_results/dynamic_ssl_20260311_130038_analysis.log
11+
-rw-r--r-- 1 root root 492202 Mar 11 13:01 dynamic_analysis_results/dynamic_ssl_20260311_130038_gdb_trace.log
12+
13+
📊 Quick Analysis:
14+
=== ALLOCATION ANALYSIS ===
15+
EVP_CIPHER_fetch calls: 2
16+
CRYPTO allocation calls: 6456
17+
RAND_get0_primary calls: 2
18+
=== DEALLOCATION ANALYSIS ===
19+
CRYPTO_free calls: 2242
20+
EVP_CIPHER_free calls: 2
21+
SSL_CTX_free calls: 2
22+
=== MEMORY LEAK ASSESSMENT ===
23+
⚠️ POTENTIAL LEAK: 4212 more allocations than deallocations
24+

0 commit comments

Comments
 (0)