FIELD OF THE INVENTION
This invention relates generally to computer systems, and more particularly to techniques for monitoring memory operations to assure that they are valid. The invention finds particular utility for purposes of computer program debugging, i.e., the process of identifying and signalling errors in programs, including memory access violations, and for purposes of memory optimization.
BACKGROUND OF THE INVENTION
A computer memory system (or memory, for short) comprises a plurality of memory locations for the storage and retrieval of data. A memory location is the smallest unit of memory that can be individually accessed, using a corresponding address for identification that is unique within the system.
Basic and well-known memory operations include allocating, freeing, reading, writing, and initializing memory locations. Many programs allocate and free memory locations, and most all programs read data from and write data to memory locations.
Many memory operations pertain generally to the process of allocating memory. "Allocation" is the process of making a set of memory locations available to a particular program or procedure. When performed during execution of the program, the operation is called "dynamic allocation." Not all memory areas are typically allocated dynamically; some can be statically allocated with pre-initialization or without initialization (e.g., variables and arrays of fixed size needed during the entire execution of a program); and some (e.g., stack) can be allocated and de-allocated automatically, without explicit request from the program, as procedures are called. "Freeing" is the process of de-allocating locations that were previously allocated, thus permitting them to be allocated for other purposes.
Other types of memory operations pertain to accessing the contents of the memory locations. "Writing" is the process of storing data in memory locations, and "reading" is the process of retrieving data from memory locations. "Initializing" is the process of setting the contents of memory locations to an initial, pre-determined value.
Only a few sequences of these basic memory operations are "valid," i.e., are not regarded as errors or violations. Memory access violations include, for example, allocation violations, read violations, uninitialized memory reads, write violations, and memory freeing errors. To enable an understanding of the invention, these terms can be defined as follows:
An "allocation violation" occurs in the unlikely event that a system attempts to allocate a portion of memory that has been allocated (and not freed) previously.
A "read violation" occurs when a program attempts to read from a memory location that is not allocated for use by that program.
An "uninitialized memory read" occurs when a program attempts to read from a memory location before it is written.
A "write violation" occurs when a program attempts to write to a memory location that is not allocated for use by that program.
A "memory freeing error" occurs, e.g., when a program attempts to free a memory location that is not currently allocated for use by the program.
On the occurrence of any of these violations, the program may obtain incorrect data, i.e., whatever data happens to be stored at the accessed memory location. Consequently, each of these violations can result in faulty program execution, incorrect program results, or complete program failure (often called "system crash").
A programmer can employ known software-based tools for enabling monitoring of memory accesses during run-time of a program to assure validity of memory operations. Use of such tools can be particularly important during debugging of programs written in advanced programming languages such as C/C++, whose programmer-manipulatable pointers can make it difficult to track down memory-access violations.
The software tools available to a programmer include conventional instrumenting programs that can equip a program under development for self-monitoring of memory accesses. The instrumenting program can modify the program code, e.g., at link time, for invoking a routine, e.g., a memory monitoring routine, upon each preselected memory access during run-time of the program.
See, for example, U.S. Pat. No. 5,193,180 (whose disclosure is incorporated herein by reference). See, also, Amitabh Srivastava and Dave W. Wall, "A Practical System for Intermodule Code Optimization at Link-Time," Journal of Programming Languages, March, 1993, (and the same article as published by Digital Equipment Corporation, WRL Research Report 92/6, December 1992), which describes code modification using an intermediate, symbolic language.
After instrumenting, the program under development can monitor its own memory accesses. This is achieved conventionally using a memory status table for maintaining information about the status of memory locations. Typically, the table is stored separately from the memory locations normally available for access by the program.
For example, U.S. Pat. No. 5,193,180 describes a technique by which an executable program can monitor its own memory accesses using a dedicated table that maintains information about three memory states: allocated-and-initialized, allocated-and-uninitialized, and unallocated. According to that patent, the allocated-and-initialized state corresponds to a memory location having been allocated to the program, and initialized by having valid data for the program written thereto. The allocated-and-uninitialized state corresponds to a memory location having been allocated to the program but not initialized. The unallocated state corresponds to a memory location having not been allocated to the program. Using such a table, the memory monitoring routine can signal an error if a memory location having unallocated status is accessed for a write-type operation, or if a memory location having either an unallocated status or an allocated-and-uninitialized status is accessed for a read-type operation. Unfortunately, the technique described in that patent uses a significant amount of memory overhead for that table, and consumes a significant amount of processing time in checking for only read type, uninitialized-memory-read type, and write type violations.
Other, conventional, memory monitoring techniques identify only a single type of violation, e.g., an uninitialized memory read. Examples of such techniques are described in James J. Horning, "What the Compiler Should Tell the User," Compiler Construction, An Advanced Course, Springer-Verlag, 1974, pages 525-548. A first example of such a technique described in the Horning reference associates, during compiling, an extra flag bit with each variable location, indicating whether the variable contains a valid value. (A "variable location" is a memory location whose contents define a variable used in a program.) That technique can then test that value upon each memory reference during run-time. A described alternative technique uses a single value (e.g., the largest negative number) representing each uninitialized variable, which is stored in the variable location. Upon each memory reference to that location, its contents can be tested to determine whether the associated memory location remains uninitialized.
It would be desirable to provide a memory access monitoring technique that can check for a greater range of possible access violations, and can reduce significantly the memory overhead requirements associated with error signalling.
SUMMARY OF THE INVENTION
The invention resides in a memory monitoring system for equipping a computer program for monitoring its own memory accesses which employs both special values, called "VALUEA" and "VALUEB," stored as contents of the memory locations themselves and a table of write tags. The write tags, each preferably a single-bit flag, can be set to indicate that a corresponding memory location contains written data.
In practice, (i) when the write tag for a given memory location is not set, and the contents of the location are VALUEA, the location is unallocated; (ii) when the write tag is not set and the contents are VALUEB, the location is allocated and not initialized, and (iii) when the write tag is set, the memory location has been previously written.
The program so equipped can monitor each preselected memory access, including allocation, write, read, and memory freeing operations, using the combination of the location contents and the write tag table to determine valid memory operations or otherwise to signal memory access violations.
The preferred embodiment can thus provide a memory access monitoring technique that can check for a greater range than the prior art of possible access violations, while reducing significantly the memory overhead requirements associated with error signalling. The write tag table of the invention requires about half the memory space of the dedicated table required by the above-referenced patent.
Briefly, the preferred embodiment can perform the following steps in checking the validity of one or more of the following memory operations:
Allocation and Initialization
On start-up of the computer system, a memory checking ("Mem.sub.-- Check") routine sets unallocated memory locations to VALUEA, sets allocated but uninitialized locations to VALUEB, and clears the corresponding write tags, e.g., to LOGIC LOW. For all initialized locations, the write tag is set, e.g., to LOGIC HIGH.
Upon a memory location being allocated, Mem.sub.-- Check checks the setting of the corresponding write tag. If the corresponding write tag is set, and the contents of the memory location are VALUEA, Mem.sub.-- Check changes the contents of the memory location to VALUEB to indicate the ensuing valid allocation. Otherwise, Mem.sub.-- Check signals an error. For example, if the contents of the memory location are equal to VALUEB, and the write tag is not set, the memory has already been allocated (and not since freed); therefore, the attempt to allocate (or re-allocate) that location is in error, i.e., an allocation violation.
Write Operation
Before a memory location is written, Mem.sub.-- Check checks the value of the corresponding write tag. If the write tag is not set, e.g., to LOGIC HIGH, and the memory location does not contain VALUEB (i.e., the memory location has not been allocated), MEM.sub.-- Check signals an error. If the write tag is not set and contents of the memory location are VALUEB, Mem.sub.-- Check sets the write tag to indicate the ensuing valid write operation.
Read Operation
Before a memory location is read, Mem.sub.-- Check checks the corresponding write tag. If the write tag is not set, Mem.sub.-- Check signals an error because that would mean that the location does not contain written data. In all other cases, Mem.sub.-- Check permits the read operation to proceed.
Memory Freed
Before a memory location is freed, Mem.sub.-- Check checks the corresponding write tag. If the write tag is set, Mem.sub.-- Check both permits the memory location to be set to VALUEA to indicate that it is available for future allocation, and clears the write tag to indicate that the location has not been written. If the write tag is not set, and the value of the memory location is VALUEB, Mem.sub.-- Check changes the contents of the memory location to VALUEA to indicate deallocation. In all other cases, Mem.sub.-- Check signals an error.
BRIEF DESCRIPTION OF THE DRAWINGS
For a fuller understanding of the nature of the invention, reference should be had to the following detailed description taken in connection with the accompanying drawings, in which:
FIG. 1 is a block diagram of a computer system including a memory program equipped with a memory checking routine in accordance with a preferred embodiment of the invention;
FIG. 2 is a block diagram of a memory access monitoring method implemented by the memory checking routine of FIG. 1;
FIG. 3 is a block diagram of the allocation check routine of FIG. 2;
FIG. 4 is a block diagram of the memory write check routine of FIG. 2;
FIG. 5 is a block diagram of the memory read check routine of FIG. 2; and
FIG. 6 is a block diagram of the memory freed check routine of FIG. 2.
DETAILED DESCRIPTION OF PREFERRED EMBODIMENT
a) The Computer System
FIG. 1 shows a computer system 10 in accordance with the invention. The computer system 10 has a conventional, general-purpose central processing system ("CPU") 12 and an operating system 13 for controlling operation of the system 10. The CPU 12 includes a program execution module 14A for executing a plurality of computer programs, e.g., programs 15-17.
The computer system 10 also has a memory system 20, having a portion 22 that can be dynamically allocated for use by the program 17. A memory access controller 14B of the CPU 12 controls accesses to the memory system 20.
In addition, the computer system 10 has an I/O subsystem 18, which includes, e.g., a display monitor (not shown) and a keyboard (not shown), and has a main bus 23, which interconnects the CPU 12, I/O subsystem 18, and memory system 20.
b. The Memory
The memory 20 includes a plurality of (e.g., "N+1", where "N" is a positive integer) memory locations 24, each identified by an address ADDR.sub.-- 0 through ADDR.sub.-- N , and each having contents represented as MEMLOC.sub.-- 0 through MEMLOC.sub.-- N.
The memory 20 also includes a write tag table 25, having a plurality write tags 26, preferably "N+1", the same number as there are memory locations 24 tracked by the invention. Each of the write tags 26 is associated with a different one of the memory locations 24. Preferably, each write tag 26 can be formed as a single bit, which the CPU 12 can set to a value "S", e.g. , LOGIC HIGH, or cleared to a value "C", e.g., LOGIC LOW. The particular write tag values of "S" and "C" shown for the memory location addresses ADDR.sub.-- 0, . . . , ADDR.sub.-- N in FIG. 1 are purely for purposes of illustration of the principles involved.
c. The Program Instrumenting Process
For enabling a program under development, e.g., program 15, to monitor its own memory accesses, the CPU 12 employs an instrumenting program 16 to equip the program 15 with memory monitoring functionality. This functionality allows the system 10 to debug the program 15 by identifying memory access violations, and/or to optimize the memory 22. For memory optimization, the invention can be practiced to analyze memory allocation and usage patterns, e.g., excessive memory allocation to the program 15, or pointers allocated but not used by the program 15.
More specifically, the program 15 is shown as including code specifying a sequence of instructions INSTR.sub.-- 0 through INSTR.sub.-- K, where "K" is a positive integer. Some of the instructions, e.g., INSTR.sub.-- 1 and INSTR.sub.-- 2, specify memory accesses. The instrumenting program 16 modifies the code of the program 15 under development so as to generate an instrumented program 17. The instrumented program 17 has, for each instruction INSTR.sub.-- 1, INSTR.sub.-- 2 specifying a memory access, a "next" instruction 32 (i.e., an instruction executable immediately after the memory-access instruction) that specifies a call to a memory check ("Mem.sub.-- Check") routine 40.
d. Mem.sub.-- Check Routine
The Mem.sub.-- Check routine 40 supervises accesses to the memory 20, maintains the write tag table 25, reads and writes preselected values into locations 24, analyzes the write tag table entries and the location entries to determine validity of memory accesses, and signals any access violations, e.g., by generating suitable error signals.
Consequently, as the program execution module 14A executes the instrumented program 17, the Mem.sub.-- Check routine 40 may produce memory access violation signals. Responsive to these signals, a reporting facility 42 can report and otherwise provide information concerning memory access violations, e.g., to programmers and other users via the I/O subsystem 18. The program execution module 14A can also invoke fault or exception handling procedures in response to the memory access violation signals.
In some embodiments of the invention, it may be desirable for the Mem.sub.-- Check routine 40 to produce also memory access enable signals in response to valid memory operations for explicitly enabling those operations under the direction of the memory access controller 14B.
The Mem.sub.-- Check routine 40 essentially implements a memory access monitoring method 200 for checking validity of memory operations, as depicted in FIGS. 2-6.
e. Memory Access Monitoring Method
The memory access monitoring method 200 uses special values stored as contents MEMLOC.sub.-- 0, . . . MEMLOC.sub.-- N of the locations 24 in conjunction with the write tags 26 to indicate valid allocation, and other valid memory operations, including write, read and memory freeing operations.
More specifically, and with reference to FIG. 2, the memory access monitoring method 200, during start-up, in block 202, performs certain initial steps, as follows: setting all the contents MEMLOC.sub.-- 0, . . . , MEMLOC.sub.-- N of unallocated memory locations 24 (FIG. 1) to a preselected first value, called "VALUEA," in block 204; setting allocated but uninitialized memory locations 24 (FIG. 1) to a preselected, second value, called "VALUEB," in block 206; and clearing write tags 26 (FIG. 1) corresponding to uninitialized locations and set write tags associated with initialized locations in block 208.
VALUEA and VALUEB can be any integer value, as long as they are not equal to one another. In a preferred implementation, VALUEA and VALUEB are preselected constants and do not change during program execution. Alternatively, VALUEA and VALUEB can be selected to represent system parameters, and need not remain constant in value for different memory accesses. Such system parameters can include, e.g., an identification of an individual processor (useful particularly in an embodiment of the invention where system 10 has multiple processors (not shown)) that allocated the corresponding memory location, the number of times that the memory location has been accessed, or a pointer into a call stack at a selected instance of time. In still other implementations of the invention, VALUEA and/or VALUEB can be values that are not preselected, but rather computed during program execution.
In block 210, the method 200 fetches a first or next instruction that specifies a memory operation ("Mem.sub.-- OP"). Each "return" block in FIGS. 3-6 indicates that the method 200 returns to this block 210.
During run-time, the memory access monitoring method checks each instruction INSTR.sub.-- 1 and INSTR.sub.-- 2 (or a predetermined subset thereof) in the program 17 (FIG. 1) specifying a memory operation by performing the following steps:
(a) testing in block 212 whether the memory operation is an allocation, in which case the method 200 goes in block 214 to an allocation check routine 300 shown in FIG. 3;
(b) testing in block 216 whether the memory operation is a write, in which case the method 200 goes in block 218 to a write check routine 400 shown in FIG. 4;
(c) testing in block 222 whether the memory operation is a read, in which case the method 200 goes in block 224 to a read check routine 500 shown in FIG. 5; and
(d) testing in block 226 whether the memory operation is a freeing operation, in which case the method 200 goes in block 228 to a memory freed check routine shown in FIG. 6.
Since the program 17 includes calls to Mem.sub.-- Check 40 for only preselected types of memory accesses, e.g., only the types of memory operations listed in (a) through (d), above, the memory access monitoring method 200 must branch to one or another of the routines of FIGS. 3-6, as noted.
FIG. 3 shows the allocation check routine 300, which is invoked when the program instruction specifies that at least one of the memory locations 24 (FIG. 1) is to be allocated for use by the program 17 (FIG. 1). Where the memory operation that causes the instrumented program 17 to call Mem.sub.-- Check 40 specifies more than one memory location, each specified location is dealt with in the same way. Accordingly, the discussion that follows need only describe, by way of illustration, access to a single memory location.
The allocation check routine 300 starts with a block 302, which tests whether the write tag 26 (FIG. 1), which corresponds to the memory location 24 (FIG. 1) to be allocated, is set, e.g., to value S.
If the write tag 26 (FIG. 1) is not set, block 304 tests whether the contents MEMLOC.sub.-- 0, . . . , or MEMLOC.sub.-- N of the memory location 24 (FIG. 1) are equal to VALUEA. If the contents MEMLOC.sub.-- 0, . . . , or MEMLOC.sub.-- N of the location 24 are equal to that value, block 306 changes that value to VALUEB to indicate a validly allocated location, and block 308 signals that the location 24 (FIG. 1) can be allocated by generating (and asserting to the memory access controller 14B (FIG. 1)) an allocation enable ("ALLOC.sub.-- EN") signal.
On the other hand, if the tests of blocks 302 and 304 indicate that the write tag 26 (FIG. 1) is set or the contents MEMLOC.sub.-- 0, . . . , or MEMLOC.sub.-- N of the memory location 24 (FIG. 1) are not equal to VALUEA, block 312 signals an error, i.e., an allocation error.
FIG. 4 shows the memory write check routine 400, which is invoked when the program instruction specifies that data is to be written into at least one of the memory locations 24 (FIG. 1). The memory write check routine 400 starts with a block 402, which tests whether the write tag 26 (FIG. 1), which corresponds to the memory location 24 (FIG. 1), is set.
If the write tag 26 (FIG. 1) is not set, block 404 tests whether the contents MEMLOC.sub.-- 0, . . . , or MEMLOC.sub.-- N of the memory location 24 (FIG. 1) are equal to VALUEB. If the contents MEMLOC.sub.-- 0, . . . , or MEMLOC.sub.-- N are equal to that value, block 406 sets the write tag 26 (FIG. 1) to indicate a validly written location, and block 408 signals that data can be written to the memory location 24 (FIG. 1) by generating (and asserting to the memory access controller 14B (FIG. 1)) a write enable ( "WRITE.sub.-- EN") signal.
If the test of block 402 indicates that the write tag 26 (FIG. 1) is set already, the routine 400 proceeds directly to the write enable signal block 408. If block 404 comes up negative, i.e., the contents MEMLOC.sub.-- 0, . . . , or MEMLOC.sub.-- N of the location 24 are not equal to VALUEB, block 412 signals an error, i.e., a write error.
FIG. 5 shows the memory read check routine 500, which is invoked when the program instruction specifies that data is to be read from at least one of the memory locations 24 (FIG. 1).
The memory read check routine 500 starts with a block 502, which tests whether the write tag 26 (FIG. 1), which corresponds to the memory location 24 (FIG. 1), is set. If the write tag 26 (FIG. 1) is set, block 504 signals that data can be read from the memory location 24 (FIG. 1) by generating (and asserting to the memory access controller 14B (FIG. 1)) a read enable ("READ.sub.-- EN") signal, and, if not set, block 506 signals an error, i.e., a read error.
FIG. 6 shows a memory freed check routine 600, which is invoked when the program instruction specifies that at least one memory location 24 (FIG. 1) previously allocated for use by the program 16 (FIG. 1) is to be de-allocated. The memory freed check routine 600 starts with a block 602, which tests whether the write tag 26 (FIG. 1), which corresponds to the memory location 24 (FIG. 1) that the program 16 (FIG. 1) seeks to free, is set.
If the write tag 26 (FIG. 1) is set, block 604 clears the write tag 26 (FIG. 1), block 606 sets the contents MEMLOC.sub.-- 0, . . . , or MEMLOC.sub.-- N of the memory location 24 (FIG. 1) to VALUEA to indicate a validly allocated location, and block 608 signals that the memory location 24 (FIG. 1) can be freed by generating (and asserting to the memory access controller 14B (FIG. 1)) a location freed enable ("LOCFREED.sub.-- EN") signal.
If the write tag 26 (FIG. 1) is not set, block 610 tests whether the contents MEMLOC.sub.-- 0, . . . , MEMLOC.sub.-- N of the memory location 24 (FIG. 1) are equal to VALUEB. If the contents MEMLOC.sub.-- 0, . . . , or MEMLOC.sub.-- N of the location 24 are equal to VALUEB, routine 600 proceeds with block 606 where the contents MEMLOC.sub.-- 0, . . . , or MEMLOC.sub.-- N of the location 24 (FIG. 1) are set to VALUEA to indicate a validly initialized location, and if not equal to that VALUEB, block 612 signals an error, i.e., a memory freeing error.
f. Other Features and Embodiments
Many additional features and embodiments of the invention deserve attention.
(i) In some applications, it may be desirable to report statistical information about memory accesses, e.g., to aid in memory optimization. To that end, and as depicted in FIG. 1, the memory 20 can include data structures providing a plurality of counters 702 (e.g., Counter.sub.-- 0 through Counter.sub.-- M, where "M" is a positive integer), each responsible for maintaining a count of the number of memory accesses falling within a preselected, corresponding operation type, e.g., allocate, write, read, etc.
Alternatively, or additionally, each of a plurality of sets of the counters 702 can be associated with a different memory location, and can be used to count valid accesses to the associated memory location. The count can be made cumulatively (i.e., regardless of the type of memory operation for which access is sought) or can be broken down by particular type of memory operation, e.g., read or write, with each assigned to a different counter.
Accordingly, as shown in FIGS. 3-6, each access routine 300-600 of the memory access monitoring method 200 further includes a block 314, 414, 514, and 614 for causing the CPU 12 to increment the corresponding counter COUNT.sub.-- 0 through COUNT.sub.-- M, respectively, when the corresponding memory operation is determined to be valid.
While it is preferable to implement the counters 702 in this fashion, the counters can be implemented alternatively in the form of hardware registers (not shown). Either way, the counters 702 can be read and the statistic information therefrom provided to the memory access reporting facility 42 for inclusion in reports provided, e.g., to programmers and other users via the I/O subsystem 18.
(ii) In many applications, the invention will be implemented to signal for each and every one of the above-described memory operations, namely, allocation violations, read violations, uninitialized memory read errors write violations, and memory freeing errors. Such implementations can find particular utility in checking validity of operations to dynamically allocated memory locations. In other applications, for example, those with respect to accesses to statically allocated memory or to a stack, a subset of these violations can be checked, e.g., uninitialized memory reads.
(iii) As described heretofore, the FIG. 1 embodiment uses a different write tag 26 for each memory location 24.
The invention can also be practiced at a different level of granularity, e.g., by associating a different write tag with a memory block including one or more memory locations, and analyzing validity of preselected memory operations with respect to the memory blocks rather than to memory locations. Moreover, a set of the counters 702 can be used to track accesses to the memory blocks rather than to the individual memory locations.
While the invention has been described with reference to specific embodiments, this description is not meant to be construed in a limiting sense. Various modifications of the disclosed embodiments, as well as other embodiments of the invention, will be apparent to those skilled in the art upon reference to this description. For example, while it is contemplated that each block of the memory access monitoring method 200 depicted in FIGS. 2-6 can represent code of program 17 equipped for monitoring its own memory accesses, the illustrated blocks can also represent firmware or hardware for performing the same functions. Consequently, it is intended that the appended claims cover any such modifications or embodiments as fall within the spirit and scope of the invention.