Skip to content

Instantly share code, notes, and snippets.

@a10y
Created March 25, 2024 15:20
Show Gist options
  • Save a10y/a48eaacece0ba1f440930326c6298929 to your computer and use it in GitHub Desktop.
Save a10y/a48eaacece0ba1f440930326c6298929 to your computer and use it in GitHub Desktop.
Exploring memory leaks with Rust
use std::hint::black_box;
fn main() {
// Leak the reference in a loop.
println!("begin leaking memory");
for _ in 0..10 {
let data = Box::new("hello world".to_string());
let _ = black_box(Box::leak(data)); // Needed to avoid allocs being optimized away by the compiler.
}
println!("done leaking memory");
}
use std::hint::black_box;
fn main() {
// Leak the reference in a loop.
println!("begin leaking memory");
for _ in 0..10 {
let data = Box::new("hello world".to_string());
let _ = black_box(data); // Needed to avoid allocs being optimized away by the compiler.
}
println!("done leaking memory");
}
@a10y
Copy link
Author

a10y commented Mar 25, 2024

Output of the squeaky clean version using Box::new() shows zero leaks b/c of Rust's RAII:

> leaks --atExit --  target/release/leaky
leaky(31160) MallocStackLogging: could not tag MSL-related memory as no_footprint, so those pages will be included in process footprint - (null)
leaky(31160) MallocStackLogging: recording malloc and VM allocation stacks using lite mode
begin leaking memory
done leaking memory
Process 31160 is not debuggable. Due to security restrictions, leaks can only show or save contents of readonly memory of restricted processes.

Process:         leaky [31160]
Path:            /Volumes/VOLUME/*/leaky
Load Address:    0x104624000
Identifier:      leaky
Version:         0
Code Type:       ARM64
Platform:        macOS
Parent Process:  leaks [31159]

Date/Time:       2024-03-25 11:19:10.343 -0400
Launch Time:     2024-03-25 11:19:10.183 -0400
OS Version:      macOS 14.1 (23B74)
Report Version:  7
Analysis Tool:   /usr/bin/leaks

Physical footprint:         3665K
Physical footprint (peak):  3665K
Idle exit:                  untracked
----

leaks Report Version: 4.0, multi-line stacks
Process 31160: 192 nodes malloced for 10 KB
Process 31160: 0 leaks for 0 total leaked bytes.

@a10y
Copy link
Author

a10y commented Mar 25, 2024

Output of the leaky.rs version with 10 explicit leaks snuck in there:

> leaks --atExit --  target/release/leaky
leaky(31273) MallocStackLogging: could not tag MSL-related memory as no_footprint, so those pages will be included in process footprint - (null)
leaky(31273) MallocStackLogging: recording malloc and VM allocation stacks using lite mode
begin leaking memory
done leaking memory
Process 31273 is not debuggable. Due to security restrictions, leaks can only show or save contents of readonly memory of restricted processes.

Process:         leaky [31273]
Path:            /Volumes/VOLUME/*/leaky
Load Address:    0x100a08000
Identifier:      leaky
Version:         0
Code Type:       ARM64
Platform:        macOS
Parent Process:  leaks [31272]

Date/Time:       2024-03-25 11:21:52.767 -0400
Launch Time:     2024-03-25 11:21:52.625 -0400
OS Version:      macOS 14.1 (23B74)
Report Version:  7
Analysis Tool:   /usr/bin/leaks

Physical footprint:         3729K
Physical footprint (peak):  3729K
Idle exit:                  untracked
----

leaks Report Version: 4.0, multi-line stacks
Process 31273: 212 nodes malloced for 11 KB
Process 31273: 20 leaks for 640 total leaked bytes.

STACK OF 1 INSTANCE OF 'ROOT LEAK: <malloc in leaky::main::h79662e949cdb2515>':
6   dyld                                  0x1802b90e0 start + 2360
5   leaky                                 0x100a08bf4 main + 52
4   leaky                                 0x100a1cf84 std::rt::lang_start_internal::h8de03eeadd2581a1 + 676
3   leaky                                 0x100a087b8 std::rt::lang_start::_$u7b$$u7b$closure$u7d$$u7d$::h7aca2ccecf4eb479 + 16
2   leaky                                 0x100a087a0 std::sys_common::backtrace::__rust_begin_short_backtrace::h225866906f8512f5 + 12
1   leaky                                 0x100a0895c leaky::main::h79662e949cdb2515 + 376
0   libsystem_malloc.dylib                0x180476a44 _malloc_zone_malloc_instrumented_or_legacy + 136
====
    2 (64 bytes) ROOT LEAK: <malloc in leaky::main::h79662e949cdb2515 0x141f04430> [32]
       1 (32 bytes) <malloc in leaky::main::h79662e949cdb2515 0x141f04410> [32]

STACK OF 1 INSTANCE OF 'ROOT LEAK: <malloc in leaky::main::h79662e949cdb2515>':
6   dyld                                  0x1802b90e0 start + 2360
5   leaky                                 0x100a08bf4 main + 52
4   leaky                                 0x100a1cf84 std::rt::lang_start_internal::h8de03eeadd2581a1 + 676
3   leaky                                 0x100a087b8 std::rt::lang_start::_$u7b$$u7b$closure$u7d$$u7d$::h7aca2ccecf4eb479 + 16
2   leaky                                 0x100a087a0 std::sys_common::backtrace::__rust_begin_short_backtrace::h225866906f8512f5 + 12
1   leaky                                 0x100a0886c leaky::main::h79662e949cdb2515 + 136
0   libsystem_malloc.dylib                0x180476a44 _malloc_zone_malloc_instrumented_or_legacy + 136
====
    2 (64 bytes) ROOT LEAK: <malloc in leaky::main::h79662e949cdb2515 0x141f04370> [32]
       1 (32 bytes) <malloc in leaky::main::h79662e949cdb2515 0x141f04350> [32]

STACK OF 1 INSTANCE OF 'ROOT LEAK: <malloc in leaky::main::h79662e949cdb2515>':
6   dyld                                  0x1802b90e0 start + 2360
5   leaky                                 0x100a08bf4 main + 52
4   leaky                                 0x100a1cf84 std::rt::lang_start_internal::h8de03eeadd2581a1 + 676
3   leaky                                 0x100a087b8 std::rt::lang_start::_$u7b$$u7b$closure$u7d$$u7d$::h7aca2ccecf4eb479 + 16
2   leaky                                 0x100a087a0 std::sys_common::backtrace::__rust_begin_short_backtrace::h225866906f8512f5 + 12
1   leaky                                 0x100a08aec leaky::main::h79662e949cdb2515 + 776
0   libsystem_malloc.dylib                0x180476a44 _malloc_zone_malloc_instrumented_or_legacy + 136
====
    2 (64 bytes) ROOT LEAK: <malloc in leaky::main::h79662e949cdb2515 0x141f04570> [32]
       1 (32 bytes) <malloc in leaky::main::h79662e949cdb2515 0x141f04550> [32]

STACK OF 1 INSTANCE OF 'ROOT LEAK: <malloc in leaky::main::h79662e949cdb2515>':
6   dyld                                  0x1802b90e0 start + 2360
5   leaky                                 0x100a08bf4 main + 52
4   leaky                                 0x100a1cf84 std::rt::lang_start_internal::h8de03eeadd2581a1 + 676
3   leaky                                 0x100a087b8 std::rt::lang_start::_$u7b$$u7b$closure$u7d$$u7d$::h7aca2ccecf4eb479 + 16
2   leaky                                 0x100a087a0 std::sys_common::backtrace::__rust_begin_short_backtrace::h225866906f8512f5 + 12
1   leaky                                 0x100a0890c leaky::main::h79662e949cdb2515 + 296
0   libsystem_malloc.dylib                0x180476a44 _malloc_zone_malloc_instrumented_or_legacy + 136
====
    2 (64 bytes) ROOT LEAK: <malloc in leaky::main::h79662e949cdb2515 0x141f043f0> [32]
       1 (32 bytes) <malloc in leaky::main::h79662e949cdb2515 0x141f043d0> [32]

STACK OF 1 INSTANCE OF 'ROOT LEAK: <malloc in leaky::main::h79662e949cdb2515>':
6   dyld                                  0x1802b90e0 start + 2360
5   leaky                                 0x100a08bf4 main + 52
4   leaky                                 0x100a1cf84 std::rt::lang_start_internal::h8de03eeadd2581a1 + 676
3   leaky                                 0x100a087b8 std::rt::lang_start::_$u7b$$u7b$closure$u7d$$u7d$::h7aca2ccecf4eb479 + 16
2   leaky                                 0x100a087a0 std::sys_common::backtrace::__rust_begin_short_backtrace::h225866906f8512f5 + 12
1   leaky                                 0x100a089ac leaky::main::h79662e949cdb2515 + 456
0   libsystem_malloc.dylib                0x180476a44 _malloc_zone_malloc_instrumented_or_legacy + 136
====
    2 (64 bytes) ROOT LEAK: <malloc in leaky::main::h79662e949cdb2515 0x141f04470> [32]
       1 (32 bytes) <malloc in leaky::main::h79662e949cdb2515 0x141f04450> [32]

STACK OF 1 INSTANCE OF 'ROOT LEAK: <malloc in leaky::main::h79662e949cdb2515>':
6   dyld                                  0x1802b90e0 start + 2360
5   leaky                                 0x100a08bf4 main + 52
4   leaky                                 0x100a1cf84 std::rt::lang_start_internal::h8de03eeadd2581a1 + 676
3   leaky                                 0x100a087b8 std::rt::lang_start::_$u7b$$u7b$closure$u7d$$u7d$::h7aca2ccecf4eb479 + 16
2   leaky                                 0x100a087a0 std::sys_common::backtrace::__rust_begin_short_backtrace::h225866906f8512f5 + 12
1   leaky                                 0x100a088bc leaky::main::h79662e949cdb2515 + 216
0   libsystem_malloc.dylib                0x180476a44 _malloc_zone_malloc_instrumented_or_legacy + 136
====
    2 (64 bytes) ROOT LEAK: <malloc in leaky::main::h79662e949cdb2515 0x141f043b0> [32]
       1 (32 bytes) <malloc in leaky::main::h79662e949cdb2515 0x141f04390> [32]

STACK OF 1 INSTANCE OF 'ROOT LEAK: <malloc in leaky::main::h79662e949cdb2515>':
6   dyld                                  0x1802b90e0 start + 2360
5   leaky                                 0x100a08bf4 main + 52
4   leaky                                 0x100a1cf84 std::rt::lang_start_internal::h8de03eeadd2581a1 + 676
3   leaky                                 0x100a087b8 std::rt::lang_start::_$u7b$$u7b$closure$u7d$$u7d$::h7aca2ccecf4eb479 + 16
2   leaky                                 0x100a087a0 std::sys_common::backtrace::__rust_begin_short_backtrace::h225866906f8512f5 + 12
1   leaky                                 0x100a08a9c leaky::main::h79662e949cdb2515 + 696
0   libsystem_malloc.dylib                0x180476a44 _malloc_zone_malloc_instrumented_or_legacy + 136
====
    2 (64 bytes) ROOT LEAK: <malloc in leaky::main::h79662e949cdb2515 0x141f04530> [32]
       1 (32 bytes) <malloc in leaky::main::h79662e949cdb2515 0x141f04510> [32]

STACK OF 1 INSTANCE OF 'ROOT LEAK: <malloc in leaky::main::h79662e949cdb2515>':
6   dyld                                  0x1802b90e0 start + 2360
5   leaky                                 0x100a08bf4 main + 52
4   leaky                                 0x100a1cf84 std::rt::lang_start_internal::h8de03eeadd2581a1 + 676
3   leaky                                 0x100a087b8 std::rt::lang_start::_$u7b$$u7b$closure$u7d$$u7d$::h7aca2ccecf4eb479 + 16
2   leaky                                 0x100a087a0 std::sys_common::backtrace::__rust_begin_short_backtrace::h225866906f8512f5 + 12
1   leaky                                 0x100a08b3c leaky::main::h79662e949cdb2515 + 856
0   libsystem_malloc.dylib                0x180476a44 _malloc_zone_malloc_instrumented_or_legacy + 136
====
    2 (64 bytes) ROOT LEAK: <malloc in leaky::main::h79662e949cdb2515 0x141f045b0> [32]
       1 (32 bytes) <malloc in leaky::main::h79662e949cdb2515 0x141f04590> [32]

STACK OF 1 INSTANCE OF 'ROOT LEAK: <malloc in leaky::main::h79662e949cdb2515>':
6   dyld                                  0x1802b90e0 start + 2360
5   leaky                                 0x100a08bf4 main + 52
4   leaky                                 0x100a1cf84 std::rt::lang_start_internal::h8de03eeadd2581a1 + 676
3   leaky                                 0x100a087b8 std::rt::lang_start::_$u7b$$u7b$closure$u7d$$u7d$::h7aca2ccecf4eb479 + 16
2   leaky                                 0x100a087a0 std::sys_common::backtrace::__rust_begin_short_backtrace::h225866906f8512f5 + 12
1   leaky                                 0x100a089fc leaky::main::h79662e949cdb2515 + 536
0   libsystem_malloc.dylib                0x180476a44 _malloc_zone_malloc_instrumented_or_legacy + 136
====
    2 (64 bytes) ROOT LEAK: <malloc in leaky::main::h79662e949cdb2515 0x141f044b0> [32]
       1 (32 bytes) <malloc in leaky::main::h79662e949cdb2515 0x141f04490> [32]

STACK OF 1 INSTANCE OF 'ROOT LEAK: <malloc in leaky::main::h79662e949cdb2515>':
6   dyld                                  0x1802b90e0 start + 2360
5   leaky                                 0x100a08bf4 main + 52
4   leaky                                 0x100a1cf84 std::rt::lang_start_internal::h8de03eeadd2581a1 + 676
3   leaky                                 0x100a087b8 std::rt::lang_start::_$u7b$$u7b$closure$u7d$$u7d$::h7aca2ccecf4eb479 + 16
2   leaky                                 0x100a087a0 std::sys_common::backtrace::__rust_begin_short_backtrace::h225866906f8512f5 + 12
1   leaky                                 0x100a08a4c leaky::main::h79662e949cdb2515 + 616
0   libsystem_malloc.dylib                0x180476a44 _malloc_zone_malloc_instrumented_or_legacy + 136
====
    2 (64 bytes) ROOT LEAK: <malloc in leaky::main::h79662e949cdb2515 0x141f044f0> [32]
       1 (32 bytes) <malloc in leaky::main::h79662e949cdb2515 0x141f044d0> [32]



Binary Images:
       0x100a08000 -        0x100a47ff7 +leaky (0) <BCFBE676-635C-31C6-8044-362EC08534D6> /Volumes/*/leaky
       0x100ed8000 -        0x100edbffb  libLeaksAtExit.dylib (64562.1.1) <78F1A80B-3789-3BDF-90B6-F5975F3F4A69> /usr/lib/libLeaksAtExit.dylib
       0x180264000 -        0x1802b2f08  libobjc.A.dylib (906) <49E2DCB3-F014-3FCF-949B-F5F57B3EF0A8> /usr/lib/libobjc.A.dylib
       0x1802b3000 -        0x180347317  dyld (1.0.0 - 1122.1.2) <EC7A3BA0-F9BF-3AB8-A0F4-8622E5606B20> /usr/lib/dyld
       0x180348000 -        0x18034cff8  libsystem_blocks.dylib (90) <A2904D7C-66C8-36E7-BC4B-D30025BA0351> /usr/lib/system/libsystem_blocks.dylib
       0x18034d000 -        0x180393fff  libxpc.dylib (2679.40.6) <212B4ED6-CBB2-33DE-95C0-7F8A405B3EBD> /usr/lib/system/libxpc.dylib
       0x180394000 -        0x1803aefff  libsystem_trace.dylib (1481.40.16) <91ECA044-8462-3293-AEEA-183BF841CBA2> /usr/lib/system/libsystem_trace.dylib
       0x1803af000 -        0x18044cff7  libcorecrypto.dylib (1608.40.12) <5258A992-DC2A-3A90-90F1-A64863C450A7> /usr/lib/system/libcorecrypto.dylib
       0x18044d000 -        0x180483fff  libsystem_malloc.dylib (474.0.13) <901200AA-1016-3DAA-8816-5032588ED460> /usr/lib/system/libsystem_malloc.dylib
       0x180484000 -        0x1804cafff  libdispatch.dylib (1462.0.4) <DC1D0187-7149-3100-BC63-F633AFEBEE6C> /usr/lib/system/libdispatch.dylib
       0x1804cb000 -        0x1804cdfff  libsystem_featureflags.dylib (85) <3282C86B-3BD7-353A-AC3E-09BCC631BB1F> /usr/lib/system/libsystem_featureflags.dylib
       0x1804ce000 -        0x18054cffb  libsystem_c.dylib (1583.40.7) <DECB8685-F34A-3979-AFCB-71FB55747E41> /usr/lib/system/libsystem_c.dylib
       0x18054d000 -        0x1805daff7  libc++.1.dylib (1600.151) <3702EEDE-997D-38E6-A6A1-C08EB22C375B> /usr/lib/libc++.1.dylib
       0x1805db000 -        0x1805f2fff  libc++abi.dylib (1600.151) <EDBB6DF0-72F9-3BC2-96FA-A86E05D3D305> /usr/lib/libc++abi.dylib
       0x1805f3000 -        0x18062dfef  libsystem_kernel.dylib (10002.41.9) <B7751381-1442-30B5-91B9-AD7BE461BEBE> /usr/lib/system/libsystem_kernel.dylib
       0x18062e000 -        0x18063aff3  libsystem_pthread.dylib (519) <DAF95373-5DE6-39A1-A6CE-D87F3F0629CC> /usr/lib/system/libsystem_pthread.dylib
       0x18063b000 -        0x18065ffff  libdyld.dylib (1122.1.2) <D9526862-A8B2-3424-98E7-74D5A18F9250> /usr/lib/system/libdyld.dylib
       0x180660000 -        0x180666ffb  libsystem_platform.dylib (306.0.1) <FC8CD17B-8769-348C-8DCD-1BFED022F9E4> /usr/lib/system/libsystem_platform.dylib
       0x180667000 -        0x180693ffb  libsystem_info.dylib (583.0.1) <D4F1ECDB-5AA0-3733-81D9-C9529001A13C> /usr/lib/system/libsystem_info.dylib
       0x1839d7000 -        0x1839e1ff7  libsystem_darwin.dylib (1583.40.7) <EF617F40-254D-325F-BA6B-382CB7D8BC89> /usr/lib/system/libsystem_darwin.dylib
       0x183e49000 -        0x183e59fff  libsystem_notify.dylib (317) <D86A0878-9C5C-367C-8A6D-941C5E4C11C2> /usr/lib/system/libsystem_notify.dylib
       0x185c6c000 -        0x185c85ff7  libsystem_networkextension.dylib (1838.40.8) <11D8A745-1F2F-3493-872E-C58F13BBD4D2> /usr/lib/system/libsystem_networkextension.dylib
       0x185cfc000 -        0x185d13fff  libsystem_asl.dylib (398) <9833954F-A010-3492-810A-70F96E17266E> /usr/lib/system/libsystem_asl.dylib
       0x1876b3000 -        0x1876bbff3  libsystem_symptoms.dylib (1848.40.12) <B9688985-C611-3987-9BD1-23ABCFA25045> /usr/lib/system/libsystem_symptoms.dylib
       0x18a714000 -        0x18a73dff7  libsystem_containermanager.dylib (582.40.2.0.1) <444831B7-DB95-3E3A-8C1C-97E49676B185> /usr/lib/system/libsystem_containermanager.dylib
       0x18b68e000 -        0x18b692fff  libsystem_configuration.dylib (1296.40.6) <389249EB-5A9C-362C-985F-6F470280D6F3> /usr/lib/system/libsystem_configuration.dylib
       0x18b693000 -        0x18b698ff3  libsystem_sandbox.dylib (2169.41.1) <893061BE-F3BD-3696-AC8D-4DA5E7477A20> /usr/lib/system/libsystem_sandbox.dylib
       0x18c3b1000 -        0x18c3b3ffb  libquarantine.dylib (172.40.1) <FA2FCD51-D2EB-37A7-AE47-66547DBCAB87> /usr/lib/system/libquarantine.dylib
       0x18cab7000 -        0x18cabcfff  libsystem_coreservices.dylib (152.1) <26F9E755-26EC-3974-BEC8-3B706D0254BA> /usr/lib/system/libsystem_coreservices.dylib
       0x18cdd9000 -        0x18ce0fff3  libsystem_m.dylib (3252.40.2) <072B3C9C-C54E-3F27-B231-88482F485502> /usr/lib/system/libsystem_m.dylib
       0x18ce14000 -        0x18ce1bffb  libmacho.dylib (1009) <29C5FB27-B26B-3927-89C1-917051E5D353> /usr/lib/system/libmacho.dylib
       0x18ce3c000 -        0x18ce49ff7  libcommonCrypto.dylib (600025) <4CF0B406-031F-3BC7-8A97-61DFA262C179> /usr/lib/system/libcommonCrypto.dylib
       0x18ce4a000 -        0x18ce54fff  libunwind.dylib (1600.112) <18A7097B-A9D1-34C6-8333-0D16CF789585> /usr/lib/system/libunwind.dylib
       0x18ce55000 -        0x18ce5cfff  liboah.dylib (315.1) <D927DACB-D13E-3976-8065-D41237EC7529> /usr/lib/liboah.dylib
       0x18ce5d000 -        0x18ce66ff3  libcopyfile.dylib (196) <CE4F0BED-9109-3557-8B48-45E051D43210> /usr/lib/system/libcopyfile.dylib
       0x18ce67000 -        0x18ce6afff  libcompiler_rt.dylib (103.1) <A7A07A05-6551-3867-A8AE-2935D8FA1B4E> /usr/lib/system/libcompiler_rt.dylib
       0x18ce6b000 -        0x18ce6fffb  libsystem_collections.dylib (1583.40.7) <985E00EB-F590-3B07-876A-EFE015DB79EF> /usr/lib/system/libsystem_collections.dylib
       0x18ce70000 -        0x18ce72ffb  libsystem_secinit.dylib (143) <A0DAA91D-049F-334F-917F-92DF752CD8F4> /usr/lib/system/libsystem_secinit.dylib
       0x18ce73000 -        0x18ce75ffb  libremovefile.dylib (70) <F4566B6C-20B5-334F-86E6-50E4085DADED> /usr/lib/system/libremovefile.dylib
       0x18ce76000 -        0x18ce76ffb  libkeymgr.dylib (31) <B6E3DE59-7C3B-3403-89FE-38BBFE86BA90> /usr/lib/system/libkeymgr.dylib
       0x18ce77000 -        0x18ce7fff7  libsystem_dnssd.dylib (2200.40.37.0.1) <D922E985-BD3A-3618-9189-5B931F3CD90C> /usr/lib/system/libsystem_dnssd.dylib
       0x18ce80000 -        0x18ce85fff  libcache.dylib (92) <47B56AFF-F15C-3550-B9B6-560806BC20C5> /usr/lib/system/libcache.dylib
       0x18ce86000 -        0x18ce87fff  libSystem.B.dylib (1336) <1E176743-EDDD-309B-AE12-6369F1A2029B> /usr/lib/libSystem.B.dylib
       0x1a513f000 -        0x1a514aff7  com.apple.MallocStackLogging (1.0 - 64561.91.2) <B86BFF6F-9D98-3AD4-8D01-B2E45641502B> /System/Library/PrivateFrameworks/MallocStackLogging.framework/Versions/A/MallocStackLogging
       0x21fb12000 -        0x21fb15fff  libsystem_darwindirectory.dylib (86.0.2) <8C2C7C2A-F18E-3196-BB40-B4BB95C5BFF9> /usr/lib/system/libsystem_darwindirectory.dylib

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment