Skip to content

Commit 0a6a3ea

Browse files
Fix fork safety in keysinuse by recreating locks
1 parent 692df1b commit 0a6a3ea

12 files changed

+1622
-3
lines changed

DEBUG_SYMBOLS.md

Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
# Debug Symbols Guide for SymCrypt OpenSSL Provider
2+
3+
## Understanding Debug Symbols
4+
5+
### Symbol Types
6+
7+
1. **No Symbols (Stripped)**
8+
- Binary has no function names or debug info
9+
- `file` output: "stripped"
10+
- GDB can't show function names or source code
11+
12+
2. **Basic Symbols**
13+
- Has function names in `.symtab` section
14+
- `file` output: "not stripped"
15+
- GDB can set breakpoints by function name
16+
- ❌ No source code viewing
17+
- ❌ No variable inspection
18+
- ❌ No line-by-line stepping
19+
20+
3. **Debug Symbols (DWARF)**
21+
- Has `.debug_*` sections
22+
- Includes source file/line mapping
23+
- ✅ Full source code viewing in GDB
24+
- ✅ Variable inspection
25+
- ✅ Line-by-line stepping
26+
- ✅ Type information
27+
28+
## How to Add Debug Symbols
29+
30+
### Method 1: Use CMAKE_BUILD_TYPE (Recommended)
31+
32+
```bash
33+
# Clean rebuild with debug symbols
34+
cd /home/jasjivsingh/SymCrypt-OpenSSL
35+
./build_debug.sh
36+
```
37+
38+
Or manually:
39+
```bash
40+
cd /home/jasjivsingh/SymCrypt-OpenSSL/build
41+
cmake -DCMAKE_BUILD_TYPE=Debug ..
42+
make -j$(nproc)
43+
```
44+
45+
**Build Types:**
46+
- `Debug`: `-g3 -O0` (no optimization, max debug info)
47+
- `RelWithDebInfo`: `-g -O2` (optimized + debug symbols)
48+
- `Release`: `-O3` (optimized, no debug by default)
49+
50+
### Method 2: Add Debug Flags Manually
51+
52+
```bash
53+
cd /home/jasjivsingh/SymCrypt-OpenSSL/build
54+
cmake -DCMAKE_C_FLAGS="-g3" -DCMAKE_CXX_FLAGS="-g3" ..
55+
make -j$(nproc)
56+
```
57+
58+
### Method 3: Modify CMakeLists.txt
59+
60+
Already done! The file now has:
61+
```cmake
62+
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -g3 -O0 -DDEBUG")
63+
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g3 -O0 -DDEBUG")
64+
```
65+
66+
## Debug Flag Options
67+
68+
| Flag | Description |
69+
|------|-------------|
70+
| `-g` | Include basic debug information |
71+
| `-g3` | Include macro definitions and extra debug info |
72+
| `-ggdb` | Generate debug info for GDB specifically |
73+
| `-O0` | No optimization (easier to debug) |
74+
| `-O2` | Optimize (harder to debug, variables optimized away) |
75+
| `-fno-omit-frame-pointer` | Keep frame pointer (better backtraces) |
76+
77+
## Verify Debug Symbols
78+
79+
### Check if symbols exist:
80+
```bash
81+
file symcryptprovider.so
82+
# Should say: "not stripped"
83+
```
84+
85+
### Check for debug sections:
86+
```bash
87+
objdump -h symcryptprovider.so | grep debug
88+
# Should show: .debug_info, .debug_line, .debug_str, etc.
89+
```
90+
91+
### Check symbol table:
92+
```bash
93+
nm -D symcryptprovider.so | grep scossl_provider_init
94+
# Should show: function symbols
95+
```
96+
97+
### Detailed debug info:
98+
```bash
99+
readelf -w symcryptprovider.so | head -100
100+
# Shows DWARF debug information
101+
```
102+
103+
## Using Debug Symbols in GDB
104+
105+
### Load Symbols
106+
```gdb
107+
# GDB should auto-load symbols when attaching
108+
# If not, manually load:
109+
sharedlibrary symcryptprovider
110+
111+
# Check if loaded:
112+
info sharedlibrary symcrypt
113+
```
114+
115+
### Set Source Directories
116+
```gdb
117+
# Tell GDB where source files are:
118+
directory /home/jasjivsingh/SymCrypt-OpenSSL/SymCryptProvider/src
119+
directory /home/jasjivsingh/SymCrypt-OpenSSL/ScosslCommon/src
120+
```
121+
122+
### Verify Symbols Loaded
123+
```gdb
124+
# List functions:
125+
info functions scossl_
126+
127+
# Show source for a function:
128+
list scossl_provider_init
129+
130+
# Check debug info:
131+
maint info symtabs
132+
```
133+
134+
## Troubleshooting Symbol Loading
135+
136+
### Problem: GDB says "No debugging symbols found"
137+
138+
**Solution 1:** Check if debug symbols exist
139+
```bash
140+
objdump -h /usr/lib/x86_64-linux-gnu/ossl-modules/symcryptprovider.so | grep debug
141+
```
142+
143+
**Solution 2:** Rebuild with debug symbols
144+
```bash
145+
./build_debug.sh
146+
```
147+
148+
**Solution 3:** Point GDB to build directory
149+
```gdb
150+
set debug-file-directory /home/jasjivsingh/SymCrypt-OpenSSL/build
151+
```
152+
153+
### Problem: "No source file named ..."
154+
155+
**Solution:** Add source directories
156+
```gdb
157+
directory /home/jasjivsingh/SymCrypt-OpenSSL/SymCryptProvider/src
158+
```
159+
160+
### Problem: Variables show "<optimized out>"
161+
162+
**Cause:** Code was compiled with optimization (`-O2`, `-O3`)
163+
164+
**Solution:** Use Debug build type (`-O0`)
165+
```bash
166+
cmake -DCMAKE_BUILD_TYPE=Debug ..
167+
```
168+
169+
## Best Practices
170+
171+
### For Development/Debugging
172+
```bash
173+
cmake -DCMAKE_BUILD_TYPE=Debug ..
174+
# Pros: Full debug info, no optimization
175+
# Cons: Slower execution
176+
```
177+
178+
### For Production Debugging
179+
```bash
180+
cmake -DCMAKE_BUILD_TYPE=RelWithDebInfo ..
181+
# Pros: Optimized + debug symbols
182+
# Cons: Some variables optimized away
183+
```
184+
185+
### For Production Release
186+
```bash
187+
cmake -DCMAKE_BUILD_TYPE=Release ..
188+
# Then optionally strip:
189+
strip --strip-debug symcryptprovider.so
190+
```
191+
192+
## Quick Reference
193+
194+
| Task | Command |
195+
|------|---------|
196+
| Build with debug | `./build_debug.sh` |
197+
| Check for debug symbols | `objdump -h *.so \| grep debug` |
198+
| Check if stripped | `file *.so` |
199+
| List symbols | `nm -D *.so` |
200+
| Debug with GDB | `./debug_nginx_provider.sh attach` |
201+
202+
## File Sizes Comparison
203+
204+
Typical sizes for the provider:
205+
- **Stripped Release**: ~500KB
206+
- **Release with symbols**: ~1MB
207+
- **Debug**: ~2-3MB (includes debug sections)
208+
209+
The debug sections add overhead but provide invaluable debugging capability.

DIAGRAMS.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
sequenceDiagram
2+
autonumber
3+
participant Parent
4+
participant System
5+
participant Child
6+
7+
Note over Parent: Normal Execution
8+
9+
Parent->>Parent: keysinuse_atfork_prepare()
10+
Parent->>Parent: LOCK EVERYTHING (Mutex + RWLocks)
11+
12+
Parent->>System: fork()
13+
14+
System->>Child: Copy Memory (Locks are LOCKED)
15+
16+
par Parent Resume
17+
Parent->>Parent: keysinuse_atfork_parent()
18+
Parent->>Parent: UNLOCK EVERYTHING
19+
Note over Parent: Continues...
20+
and Child Resume
21+
Child->>Child: keysinuse_atfork_child()
22+
Note right of Child: Cannot unlock RWLocks (Wrong Owner)
23+
Child->>Child: DESTROY Old RWLocks
24+
Child->>Child: CREATE New RWLocks
25+
Child->>Child: UNLOCK Mutex
26+
Child->>Child: Start New Logging Thread
27+
sequenceDiagram
28+
autonumber
29+
participant Parent
30+
participant System
31+
participant Child
32+
33+
Note over Parent: Normal Execution
34+
35+
Parent->>Parent: keysinuse_atfork_prepare()
36+
Parent->>Parent: LOCK EVERYTHING (Mutex + RWLocks)
37+
38+
Parent->>System: fork()
39+
40+
System->>Child: Copy Memory (Locks are LOCKED)
41+
42+
par Parent Resume
43+
Parent->>Parent: keysinuse_atfork_parent()
44+
Parent->>Parent: UNLOCK EVERYTHING
45+
Note over Parent: Continues...
46+
and Child Resume
47+
Child->>Child: keysinuse_atfork_child()
48+
Note right of Child: Cannot unlock RWLocks (Wrong Owner)
49+
Child->>Child: DESTROY Old RWLocks
50+
Child->>Child: CREATE New RWLocks
51+
Child->>Child: UNLOCK Mutex
52+
Child->>Child: Start New Logging Thread
53+
end

0 commit comments

Comments
 (0)