Skip to content

Instantly share code, notes, and snippets.

@ggreer
Last active August 29, 2015 14:21
Show Gist options
  • Save ggreer/8a0452d79c4324a9bd56 to your computer and use it in GitHub Desktop.
Save ggreer/8a0452d79c4324a9bd56 to your computer and use it in GitHub Desktop.
OS X `leaks` command doesn't detect leaks from anonymous mmap()s
#include <stdio.h>
#include <string.h>
#include <sys/mman.h>
#include <unistd.h>
int main() {
void *buf = mmap(NULL, 1024, PROT_WRITE, MAP_ANON | MAP_SHARED, -1, 0);
strcpy(buf, "TESTING 123");
printf("Buf is '%s'\n", buf);
buf = NULL;
if (buf != NULL) {
printf("OMG WTF");
}
unsigned int sleep_time = 100;
printf("Run...\nsudo leaks %u\n...in the next %u seconds\n", getpid(), sleep_time);
sleep(sleep_time);
printf("Done sleeping. Buh-bye!\n");
return 0;
}
ggreer@carbon:~/Desktop% gcc anon_mmap.c -o anon_mmap
ggreer@carbon:~/Desktop% ./anon_mmap
Buf is 'TESTING 123'
Run...
sudo leaks 48974
...in the next 100 seconds
ggreer@carbon:~% sudo leaks 48974
Process: anon_mmap [48974]
Path: /Users/ggreer/Desktop/anon_mmap
Load Address: 0
Identifier: anon_mmap
Version: ???
Code Type: X86-64
Parent Process: zsh [48029]
Date/Time: 2015-05-22 12:16:41.159 -0700
OS Version: Mac OS X 10.10.3 (14D136)
Report Version: 7
Analysis Tool: /Applications/Xcode.app/Contents/Developer/usr/bin/leaks
Analysis Tool Version: Xcode 6.3.2 (6D2105)
----
leaks Report Version: 2.0
Process 48974: 402 nodes malloced for 32 KB
Process 48974: 0 leaks for 0 total leaked bytes.
ggreer@carbon:~%
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment