LibXenon
Bare-metal Xbox 360 homebrew library
Loading...
Searching...
No Matches
mf-simplecheck.c
Go to the documentation of this file.
1#include <stdio.h>
2#include <stdlib.h>
3#include <stdint.h>
4#include "mf-runtime.h"
5
6
8{
9 /* Emit a trace message for each call. */
11
12 /* Collect and emit statistics. */
13 unsigned collect_stats;
14
15 /* Set up a SIGUSR1 -> __mf_report handler. */
17
18 /* Execute internal checking code. */
20
21 /* Age object liveness periodically. */
22 unsigned tree_aging;
23
24 /* Adapt the lookup cache to working set. */
25 unsigned adapt_cache;
26
27 /* Print list of leaked heap objects on shutdown. */
28 unsigned print_leaks;
29
30#ifdef HAVE___LIBC_FREERES
31 /* Call __libc_freeres before leak analysis. */
32 unsigned call_libc_freeres;
33#endif
34
35 /* Detect reads of uninitialized objects. */
37
38 /* Print verbose description of violations. */
40
41 /* Abbreviate duplicate object descriptions. */
42 unsigned abbreviate;
43
44 /* Emit internal tracing message. */
45 unsigned verbose_trace;
46
47 /* Wipe stack/heap objects upon unwind. */
48 unsigned wipe_stack;
49 unsigned wipe_heap;
50
51 /* Maintain a queue of this many deferred free()s,
52 to trap use of freed memory. */
54
55 /* Maintain a history of this many past unregistered objects. */
57
58 /* Pad allocated extents by this many bytes on either side. */
59 unsigned crumple_zone;
60
61 /* Maintain this many stack frames for contexts. */
62 unsigned backtrace;
63
64 /* Ignore read operations even if mode_check is in effect. */
65 unsigned ignore_reads;
66
67 /* Collect register/unregister timestamps. */
68 unsigned timestamps;
69
70#ifdef LIBMUDFLAPTH
71 /* Thread stack size. */
72 unsigned thread_stack;
73#endif
74
75 /* Major operation mode */
76#define mode_nop 0 /* Do nothing. */
77#define mode_populate 1 /* Populate tree but do not check for violations. */
78#define mode_check 2 /* Populate and check for violations (normal). */
79#define mode_violate 3 /* Trigger a violation on every call (diagnostic). */
80 unsigned mudflap_mode;
81
82 /* How to handle a violation. */
83#define viol_nop 0 /* Return control to application. */
84#define viol_segv 1 /* Signal self with segv. */
85#define viol_abort 2 /* Call abort (). */
86#define viol_gdb 3 /* Fork a debugger on self */
88
89 /* Violation heuristics selection. */
90 unsigned heur_stack_bound; /* allow current stack region */
91 unsigned heur_proc_map; /* allow & cache /proc/self/map regions. */
92 unsigned heur_start_end; /* allow _start .. _end */
93 unsigned heur_std_data; /* allow & cache stdlib data */
94};
95
97
98/* ------------------------------------------------------------------------ */
99/* Required globals. */
100
101#define LOOKUP_CACHE_MASK_DFL 1023
102#define LOOKUP_CACHE_SIZE_MAX 65536 /* Allows max CACHE_MASK 0xFFFF */
103#define LOOKUP_CACHE_SHIFT_DFL 2
104
108#define LOOKUP_CACHE_SIZE (__mf_lc_mask + 1)
109
112
114
115/* ------------------------------------------------------------------------ */
116
117#define MAX_CHECKS 256
118
119struct check_s{
120 unsigned char * addr;
121 int size;
122 int read;
123 int write;
124 int halt;
125};
126
127static int check_count=0;
128static struct check_s checks[MAX_CHECKS];
129
130void mf_check(void * addr,int size,int check_reads, int check_writes, int halt_on_access)
131{
132 if(check_count>=MAX_CHECKS-1)
133 {
134 printf("[mf-simplecheck] Too many checks!\n");
135 return;
136 }
137
138 checks[check_count].addr=addr;
139 checks[check_count].size=size;
140 checks[check_count].halt=halt_on_access;
141 checks[check_count].read=check_reads;
142 checks[check_count].write=check_writes;
143
144 check_count++;
145
146 printf("[mf-simplecheck] Will check %p, size=%d for %s %s\n",addr,size,check_reads?"read":"",check_writes?"write":"");
147}
148
150{
151 printf("[mf-simplecheck] Inited\n");
152}
153
154void __mf_check (void *ptr, size_t sz, int type, const char *location)
155{
156 int i;
157 unsigned int p=(unsigned int)ptr;
158 for(i=0;i<check_count;++i)
159 {
160 unsigned int cp=(unsigned int)checks[i].addr;
161 if (p>=cp && p<cp+checks[i].size &&
162 ((type==__MF_CHECK_READ && checks[i].read) || (type==__MF_CHECK_WRITE && checks[i].write)))
163 {
164 printf("[mf-simplecheck] %s to %p, size=%d, from %s\n",(type==__MF_CHECK_READ)?"Read":"Write",ptr,sz,location);
165 if (checks[i].halt)
166 {
167 asm volatile("sc");
168 }
169 }
170 }
171
172}
173
174void __mf_register (void *ptr, size_t sz, int type, const char *name)
175{
176// printf("[mf-simplecheck] Register %s(%p), size=%d\n",name,ptr,sz);
177}
178
179void __mf_unregister (void *ptr, size_t sz, int type)
180{
181// printf("[mf-simplecheck] Unregister %p, size=%d\n",ptr,sz);
182}
u32 ptr
Definition: iso9660.c:536
u32 size
Definition: iso9660.c:537
__SIZE_TYPE__ uintptr_t
Definition: lib_types.h:64
#define __MF_CHECK_READ
Definition: mf-runtime.h:53
#define __MF_CHECK_WRITE
Definition: mf-runtime.h:54
int __mf_starting_p
void __mf_check(void *ptr, size_t sz, int type, const char *location)
struct __mf_options __mf_opts
#define LOOKUP_CACHE_SHIFT_DFL
enum __mf_state_enum __mf_state_1
void __mf_init()
#define LOOKUP_CACHE_SIZE_MAX
struct __mf_cache __mf_lookup_cache[LOOKUP_CACHE_SIZE_MAX]
uintptr_t __mf_lc_mask
#define MAX_CHECKS
void mf_check(void *addr, int size, int check_reads, int check_writes, int halt_on_access)
#define LOOKUP_CACHE_MASK_DFL
void __mf_register(void *ptr, size_t sz, int type, const char *name)
__mf_state_enum
@ active
@ reentrant
@ in_malloc
void __mf_unregister(void *ptr, size_t sz, int type)
unsigned char __mf_lc_shift
int read(int fileDesc, void *ptr, size_t len)
Definition: newlib.c:559
unsigned check_initialization
unsigned abbreviate
unsigned collect_stats
unsigned crumple_zone
unsigned free_queue_length
unsigned print_leaks
unsigned mudflap_mode
unsigned heur_std_data
unsigned wipe_stack
unsigned violation_mode
unsigned heur_proc_map
unsigned backtrace
unsigned internal_checking
unsigned wipe_heap
unsigned tree_aging
unsigned trace_mf_calls
unsigned adapt_cache
unsigned ignore_reads
unsigned verbose_violations
unsigned persistent_count
unsigned heur_start_end
unsigned sigusr1_report
unsigned timestamps
unsigned heur_stack_bound
unsigned verbose_trace
unsigned char * addr
u8 type
Definition: xenos_edid.h:1