Q: When executing a program, the following error occurs:
***Glibc
Detected
***Double
Free
OrUpload uption
: 0x0937d008 ***
Why?
A: Set the malloc_check _ environment variable and run the program again. The error message disappears.
Malloc_check _ = 0./myprogram
Red Hat EnterprisesLinux
4. glibc
Attackers can execute additional internal commands.Data
Perform health checks to detect and protect data corruption as early as possible. By default
When the data is found, similar error messages will be displayed on the standard error output (if stderr is not enabled, it will be recorded in Syslog ):
* ** Glibc detected ** Double Free or duplicate uption: 0x0937d008 ***
By default, programs that generate this error will also be aborted. However, this (and whether error messages are generated) can be controlled using the environment variable malloc_check. The followingSet
Is supported:
0-no error message is generated or this program is not aborted
1-generate error message, but do not stop this program
2-no error message is generated, but the program is aborted.
3-generate an error message and abort the program
Remarks
If malloc_check _ is set to a value other than 0, this will enable glibc to perform more checks and may affectSystem
Performance.
3.3 common Linux memoryManagement
And debugging tools
In Linux, besides GDB, there are many debugging tools, such as binutil tools, memory detection tools provided by glibc, memwatch memory error detection tools, and valgrind tools. They both have their own strengths and have different focuses. This section describes several common debugging tools.
3.3.1 mcheck Function
Mcheck is a function provided by glibc. The declaration is as follows:
int mcheck (void (*abortfn) (enum mcheck_status status)) |
This function notifies malloc to check the consistency. It can check out the memory allocation mismatch.
enum mcheck_status mprobe (void *pointer) |
You can call it explicitly to check the pointer consistency. The following are possible returned values, that is, possible errors. Mcheck_status has the following enumerated values:
Mcheck_disabled: mcheck is disabled and no check is performed.
Mcheck_ OK: no memory problems found.
Mcheck_head: overflow under an array or pointer.
Mcheck_tail: overflow on the array or pointer.
Mcheck_free:Space
Released.
Check the code in glibc/malloc to find that the above functions are implemented through a mechanism similar to memrecorder. This mechanism can be started in the following ways:
(1) Call the mcheck () function explicitly at the beginning of the main () function or at the appropriate position.
(2) Add-lmcheck to reconnect the executable program.
(3) glibc also pointed out another method to start this check mechanism: to set the value of the environment variable malloc_check.
If malloc_check _ is set to 0, no prompt is prompted when an error is detected.
If malloc_check _ is set to 1, a message is printed to the standard error output when an error is detected.
If you set malloc_check _ to 2, you can directly call abort () to stop the program when an error is detected.
[Root @ localhost root] # Cat malloc_check_example.c // source code content, storage memory management problems # Include <stdlib. h> # Include <stdio. h> Int main (void) { Char * ptr1; Char * ptr2; Ptr1. = malloc (512 ); Ptr2 = malloc (512 ); Ptr2 = ptr1; Printf ("OK/N "); Free (ptr2 ); Free (ptr1 ); Printf ("really OK? /N "); } |
The following describes how to add-lmcheck for compilation and running.
[Root @ localhost ~] # Gcc-O malloc_check_example malloc_check_example.c-lmcheck
[Root @ localhost ~] #./Malloc_check // run
OK
Memory clobbered before allocated Block
Aborted
[Root @ localhost root] # gcc-O malloc_check_example malloc_check_example.c
[Root @ localhost root] # malloc_check _ = 0./malloc_check_example // set the macro value to 0.
OK
Really OK?
[Root @ localhost root] # malloc_check _ = 1./malloc_check_example // set the macro value to 1.
Malloc: Using debugging hooks
OK
Free (): Invalid Pointer 0x8049628!
Really OK?
[Root @ localhost root] # malloc_check _ = 2./malloc_check_example // set the macro value to 2.