mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-07 11:48:30 +00:00
Have ASAN errors show origin of memory
This commit is contained in:
parent
e20fcf02c1
commit
46a3b88594
6 changed files with 256 additions and 4 deletions
53
examples/auto-memory-safety-crash.c
Normal file
53
examples/auto-memory-safety-crash.c
Normal file
|
@ -0,0 +1,53 @@
|
|||
#if 0
|
||||
/*─────────────────────────────────────────────────────────────────╗
|
||||
│ To the extent possible under law, Justine Tunney has waived │
|
||||
│ all copyright and related or neighboring rights to this file, │
|
||||
│ as it is written in the following disclaimers: │
|
||||
│ • http://unlicense.org/ │
|
||||
│ • http://creativecommons.org/publicdomain/zero/1.0/ │
|
||||
╚─────────────────────────────────────────────────────────────────*/
|
||||
#endif
|
||||
#include "libc/bits/bits.h"
|
||||
#include "libc/log/log.h"
|
||||
|
||||
/**
|
||||
* ASAN static memory safety crash example.
|
||||
*
|
||||
* make -j8 MODE=dbg o/dbg/examples/auto-memory-safety-crash.com
|
||||
* o/dbg/examples/auto-memory-safety-crash.com
|
||||
*
|
||||
* You should see:
|
||||
*
|
||||
* global redzone 1-byte store at 0x42700d shadow 0x8007ce01
|
||||
* ./o/dbg/examples/auto-memory-safety-crash.com
|
||||
* x
|
||||
* ........................................GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG
|
||||
* |0 |0 |0 |0 |5 |-18 |-18 |-18 |-18
|
||||
* f.☼▼ä f.☼▼ä f☼▼D hello
|
||||
* 000000400000-000000427000 .text
|
||||
* 000000427000-000000429000 .data ←address
|
||||
* 00007fff0000-00008000ffff
|
||||
* 000080070000-00008008ffff ←shadow
|
||||
* 0e007fff0000-0e008000ffff
|
||||
* 100047d20000-100047d3ffff
|
||||
* 6ffffffe0000-6fffffffffff
|
||||
* the memory in question belongs to the symbols
|
||||
* buffer [0x427000,0x42700c] size 13
|
||||
* the crash was caused by
|
||||
* 0x00000000004046f3: __die at libc/log/die.c:40
|
||||
* 0x0000000000404aed: __asan_report_store at libc/intrin/asan.c:1183
|
||||
* 0x0000000000402552: main at examples/auto-memory-safety-crash.c:27
|
||||
* 0x000000000040268d: cosmo at libc/runtime/cosmo.S:64
|
||||
* 0x00000000004021ae: _start at libc/crt/crt.S:77
|
||||
*
|
||||
*/
|
||||
|
||||
char buffer[13] = "hello";
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
ShowCrashReports(); /* not needed but yoinks appropriate symbols */
|
||||
int i = 13;
|
||||
asm("" : "+r"(i)); /* prevent compiler being smart */
|
||||
buffer[i] = 1;
|
||||
return 0;
|
||||
}
|
65
examples/auto-memory-safety-crash2.c
Normal file
65
examples/auto-memory-safety-crash2.c
Normal file
|
@ -0,0 +1,65 @@
|
|||
#if 0
|
||||
/*─────────────────────────────────────────────────────────────────╗
|
||||
│ To the extent possible under law, Justine Tunney has waived │
|
||||
│ all copyright and related or neighboring rights to this file, │
|
||||
│ as it is written in the following disclaimers: │
|
||||
│ • http://unlicense.org/ │
|
||||
│ • http://creativecommons.org/publicdomain/zero/1.0/ │
|
||||
╚─────────────────────────────────────────────────────────────────*/
|
||||
#endif
|
||||
#include "libc/bits/bits.h"
|
||||
#include "libc/log/log.h"
|
||||
#include "libc/mem/mem.h"
|
||||
#include "libc/str/str.h"
|
||||
|
||||
/**
|
||||
* ASAN heap memory safety crash example.
|
||||
*
|
||||
* make -j8 MODE=dbg o/dbg/examples/auto-memory-safety-crash2.com
|
||||
* o/dbg/examples/auto-memory-safety-crash2.com
|
||||
*
|
||||
* You should see:
|
||||
*
|
||||
* heap overrun 1-byte store at 0x10008004002d shadow 0x20090000005
|
||||
* ./o/dbg/examples/auto-memory-safety-crash2.com
|
||||
* x
|
||||
* OOOOOOOOOOOUUUUUUUUUUUUUUUU.............OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
|
||||
* |-7 |-6 |-6 |0 |5 |-7 |-7 |-7 |-7
|
||||
* »!@ ÿ▄:Y╩≥= S hello ∙∙∙∙∙∙∙ ♪ GT◘&@ á+@ »!@
|
||||
* 000000400000-00000042b000 .text
|
||||
* 00000042b000-00000042d000 .data
|
||||
* 00007fff0000-00008000ffff
|
||||
* 000080070000-00008008ffff
|
||||
* 02008fff0000-02009000ffff ←shadow
|
||||
* 0e007fff0000-0e008000ffff
|
||||
* 10003ab90000-10003abaffff
|
||||
* 100080000000-10008000ffff ←address
|
||||
* 6ffffffe0000-6fffffffffff
|
||||
*
|
||||
* the memory was allocated by
|
||||
* 0x100080040020 64 bytes [dlmalloc]
|
||||
* 0x100080040030 13 bytes [actual]
|
||||
* 402608 main
|
||||
* 402ba0 cosmo
|
||||
* 4021af _start
|
||||
*
|
||||
* the crash was caused by
|
||||
* 0x0000000000404793: __die at libc/log/die.c:40
|
||||
* 0x0000000000404f56: __asan_report_store at libc/intrin/asan.c:1183
|
||||
* 0x0000000000402579: main at examples/auto-memory-safety-crash2.c:30
|
||||
* 0x000000000040270f: cosmo at libc/runtime/cosmo.S:64
|
||||
* 0x00000000004021ae: _start at libc/crt/crt.S:77
|
||||
*
|
||||
*/
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
char *buffer;
|
||||
ShowCrashReports(); /* not needed but yoinks appropriate symbols */
|
||||
buffer = malloc(13);
|
||||
strcpy(buffer, "hello");
|
||||
int i = 13;
|
||||
asm("" : "+r"(i)); /* prevent compiler being smart */
|
||||
buffer[i] = 1;
|
||||
asm("" : "+r"(buffer)); /* prevent compiler being smart */
|
||||
return 0;
|
||||
}
|
32
examples/auto-memory-safety-crash3.c
Normal file
32
examples/auto-memory-safety-crash3.c
Normal file
|
@ -0,0 +1,32 @@
|
|||
#if 0
|
||||
/*─────────────────────────────────────────────────────────────────╗
|
||||
│ To the extent possible under law, Justine Tunney has waived │
|
||||
│ all copyright and related or neighboring rights to this file, │
|
||||
│ as it is written in the following disclaimers: │
|
||||
│ • http://unlicense.org/ │
|
||||
│ • http://creativecommons.org/publicdomain/zero/1.0/ │
|
||||
╚─────────────────────────────────────────────────────────────────*/
|
||||
#endif
|
||||
#include "libc/bits/bits.h"
|
||||
#include "libc/log/log.h"
|
||||
#include "libc/mem/mem.h"
|
||||
#include "libc/str/str.h"
|
||||
|
||||
/**
|
||||
* ASAN use-after-free memory safety crash example.
|
||||
*
|
||||
* make -j8 MODE=dbg o/dbg/examples/auto-memory-safety-crash3.com
|
||||
* o/dbg/examples/auto-memory-safety-crash3.com
|
||||
*
|
||||
*/
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
char *buffer;
|
||||
ShowCrashReports(); /* not needed but yoinks appropriate symbols */
|
||||
buffer = malloc(13);
|
||||
strcpy(buffer, "hello");
|
||||
free(buffer);
|
||||
asm("" : "+r"(buffer)); /* prevent compiler being smart */
|
||||
buffer[0] = 1;
|
||||
return 0;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue