Abstract: Introduced that a dynamic memory management module, may examine in effectively the C procedure memory divulging and writes the memory to cross the border and so on mistakes, is suitable in has standard C language development environment each kind of platform.
Key word: C language dynamic memory memory divulging writes crosses the border
Introduction
Presently, in the overwhelming majority embedded platform’s software uses the C language compilation. Is succinct besides the code, the movement is highly effective, operates the memory ability is the C language significant feature nimbly. However, the inappropriate memory operation usually is also one of wrong roots. Like “memory divulging” - - cannot release already the dynamic memory which correctly assigns, is one kind very difficulty with examines saves the mistake. Continues memory divulging can cause the program performance to drop to cannot move completely finally, then affects to possesses other to have the dynamic memory requirements procedure, will even hinder operating system’s revolution in certain relatively simple embedded platforms. Moreover “writes the memory to cross the border”, one kind illegal writes the memory operation, extremely possibly destroys to this procedure in other data which uses, serious time also possibly to other procedure even overall system which moves is having the influence. Therefore, this article introduced a enhancement, may have custom-made the dynamic memory management module (below might as well is called Fense), in the C language provides in the memory allocation function foundation, increased to the dynamic memory management function; Can in the logging software movement process appear the memory divulging information, simultaneously also has certain monitor memory operation ability; May discover that the overwhelming majority writes to the dynamic memory crosses the border the mistake.
1 Fense principle of design
Fense through sets up a bidirectional chain table (struct Head *stHead) to preserve possesses the dynamic memory block information which assigns. The source filename which and the store as well as releases, Fense from st_Head deletes it, inspects in st_Head the node then not to obtain this node numerical check which and so on chain table each node corresponds a dynamic memory block, in the node including this memory size, the assignment occurs when is at releases. Fense inserts each assignment’s dynamic memory block to chain table st_Head in; At this time in depositing memory block information. The chain table node structure definition is as follows:
struct Head {
char file; /distribution point in source filename */
unsigned long line; /* distribution point in store */
size_t size; /* assignment memory size */
int checksum; /* chain table node verification and */
struct Head prev, next; /* double chain table around node indicator */
};
/* overall situation bidirectional chain table */
struct Head *st_Head=NULL;
In order to examine the mistake which writes crosses the border, Fense increased certain size memory achievement in around the user application’s memory to monitor the region respectively, and initial turned into the predetermined value. Thus, when the procedure occurs crosses the border writes the operation, the predetermined value will have the change, Fense then examines wrongly.
Dynamic memory structure as shown in Figure which assigns through Fense 1. Thus it may be known, what Fense_Malloc (the Fense memory allocation function) returns gives the user the indicator ptr direction is the user application memory region reference. After before chain table node,/, monitors the region is the Fense interior use, is the user not obviously.
2 users have custom-made the option
Fense has 5 groups of great definitions to provide for the user carries on to the function has custom-made. Each group of option control significance is as follows:
When WARN_ON_ZERO_MALLOC user application zero assignment space warns the information.
When FILL_ON_MALLOC assignment initialization memory block
Time FILL_ON_MALLOC_VAL assignment initialization predetermined value
When FILL_ON_FREE release fills the memory block
When FILL_ON_FREE_VAL release fills the memory block predetermined value
The above 4 option’s major function was the memory which the initialization just assigned and just the memory which released was a predetermined value, because the mistake which avoided presenting the use as far as possible initial not to have passed through the memory which initiates.
Before the FENSE_FRONT_SIZE definition, monitors the region size
Before the FENSE_FRONT_VAL definition, monitors the region the predetermined value
After FENSE_END_SIZE definition, monitors the region size
After FENSE_END_VAL definition, supervises the rodman territory the predetermined value
In the Fense work process, crosses the border to the memory writes the operation the examination is monitors the region through the comparison the current value to determine with this monitor region predetermined value. Obviously cannot rule out this kind of kind of possibility: Namely occurs is monitoring the region to cross the border writes the value which the operation reads in with to monitor the region the predetermined value to be exactly same, this time, Fense is unable to discover the wrong occurrence. Regarding this kind of situation, the user may monitor the region predetermined value through the change (FENSE_FRONT_VAL and FENSE_END_VAL) and monitors the region size (FENSE_FRONT_SIZE and FENSE_END_SIZE) is many group of different values tests repeatedly, like this may enhance the monitor large scale the accuracy.
VALIDATE_FREE
free inspects this memory block whether in chain table
CHECK_ALL_MEMORY_ON_FREE
when free inspects in the chain table all memory block
Because has this kind of kind of situation: Wrote the operation to memory block A to present the crossing the border mistake, has written about in another memory block B region. This time, merely inspects memory block The validity to be unable to find the problem, if simultaneously inspects all dynamic memory block, then has the possibility to discover that wrong is. The above option namely supposes for this reason.
FENSE_LOCK gain to the chain table st_Head operation power
FENSE_UNLOCK release to the chain table st_Head operation power
Considered in the multi-thread environment, possibly has many threads simultaneously to use Fense to carry on the memory management, but Fense use chain table st_Head is the global variable, therefore provided above 2 great to realize visit to st_Head incompatible. The great concrete definition relies on the software environment which is in the user, the user may realize voluntarily. Regarding the single thread system, only need these 2 great definitions for spatial then.
In order to be advantageous for the use, in the Fense head document has also included the following definition, causes the user not to need the modification existing source code to be possible basically to introduce Fense.
#define malloc(size) Fense_Malloc(size,_FILE_,_LINE_)
#define free(ptr) Fense_Free(ptr,_FILE_,_LINE_)
#define realloc(ptr, new_size) Fense_Realloc(ptr, new_size,_FILE_,_LINE_)
#define colloc(num, size) Fense_Calloc(num, size,_FILE_,_LINE_)
when 3 movements controls
The Fense monitor memory’s function may in the movement dynamic switch. This function through is zero or the non-vanishing realizes the global variable st_Disbaled evaluation. In the debugging process, may revise st_Disabled in the debugger the value to control Fense immediately the behavior, has omitted the heavy translation source code need. Required the massive compiling time regarding these the big-scale work or the overlapping platform development software project, this was very advantageous.
4 Fense concrete realizations
Fense provides memory management functions and so on Fense_Malloc, Fense_Free, Fense_Realloc and Fense_Calloc, the function and the transfer form and in C language malloc, free, realloc and calloc maintains consistent. As space is limited, here only realizes the process to Fense_Malloc and Fense_Free to make a simple description, realizes the audience this publication network supplement version specifically. http://www.dpj.com.cn
/* memory allocation function */
void *Fense_Malloc (size_t size, char *file, unsigned long line)
{
// inspects Fense when the movement switch, if Fense is closed, then transfers malloc
// assigns and returns
// inspects whether zero assignment, if has, then after prompt warning information, returns to 0 (user to have custom-made option)
// assignment memory, after chain table node region and first/monitors the region
// initialization chain table node, preserves the assignment memory the information, including assignment size, in filename and store
// this node insertion chain table st_Head
// for this node region computation verification sum
// after predetermined value initialization/monitors the region
// (user has custom-made option) with the predetermined value packing user memory region
// returns user memory region reference
}
/* memory release function */
void Fense_Free (void *uptr, char *file, unsigned long line)
{
// inspects Fense when the movement switch, if at the beginning of Fense closes, then transfers free to release translates and returns
// inspects under all Fense management the dynamic memory (user to have custom-made option)
// judges the current memory block whether in chain table st_Head, if not, then prompts
// police depends on the information, the withdrawal (user has custom-made option)
// inspects the current memory block whether to exist crosses the border the operation
// deletes the current memory block’s corresponding chain table node from st_Head
// recomputation current node around neighbor node verification sum
// fills with the predetermined value the memory area which releases (user is had custom-made option)
// transfers free to release the current memory block
}
(in article code in Visual C 6.0, Borland C 3.1 and CrossCode in the C 7.4 environment translates passes)
Concluding remark
Takes the memory carries on the monitor wrongly to the C program run time the code module, Fense can discover that nearly all memory divulging and the overwhelming majority cross the border the operation, and has recorded the information which as far as possible the correction program error needs; Reduced programmers’ debug time effectively, has made the very good progress in the actual embedded product development.