Compiler program, compilation method, and compilation system
Patent Information
- Authority / Receiving Office
- JP · JP
- Patent Type
- Applications
- Current Assignee / Owner
- ROHM CO LTD
- Filing Date
- 2025-01-22
- Publication Date
- 2026-08-03
Smart Images

Figure 2026125479000001_ABST
Abstract
Description
Technical Field
[0001] The present disclosure relates to a compiler program, a compilation method, and a compilation system.
Background Art
[0002] Stack overflow is one of the causes of program execution malfunction, and it is common to use interrupts as a method for detecting this stack overflow.
[0003] For example, in the technique of Patent Document 1, when a program is executed, if the stack overflow detection address and the stack pointer address preset in the stack overflow register and the stack pointer register match, an interrupt request is generated, and it is disclosed that a debugger executes a break process using the head address of the break process preset in the overflow interrupt vector area.
Prior Art Documents
Patent Documents
[0004]
Patent Document 1
[0005] [Summary] In the technique of Patent Document 1, stack overflow could not be detected unless the program was executed. Therefore, it was not suitable for an environment that was likely to lead to failure if stopped midway, such as the software for motor control.
[0006] Therefore, the present disclosure has been made in consideration of the above facts, and an object thereof is to provide a compiler program, a compilation method, and a compilation system capable of detecting stack overflow without executing a program.
[0007] The compiler program described herein causes a computer to compile a user-created program, calculate stack usage, calculate a first maximum stack value, and then execute a process to notify a computer of a stack overflow based on a comparison between the calculated first maximum stack value and a second maximum stack value set in advance by the user. [Brief explanation of the drawing]
[0008] [Figure 1] Figure 1 is a block diagram showing the schematic configuration of the compilation system according to this embodiment. [Figure 2] Figure 2 shows the process in the compilation system according to the first embodiment, from the time a C source file is compiled until a stack overflow is detected and notified to the user. [Figure 3] Figure 3 shows a conceptual diagram of the compilation system for calculating maximum stack usage. [Figure 4] Figure 4 shows an example of a C source file that has code to call a function via a function pointer. [Figure 5] Figure 5 shows an example of a C source file that describes the behavior of a function called by a C source file that has code to call a function via a function pointer. [Figure 6] Figure 6 shows an example of an assembly file resulting from compiling main.c. [Figure 7] Figure 7 shows an example of an assembly file resulting from compiling func.c. [Figure 8] Figure 8 shows an example of a call tree in main.c. [Figure 9] Figure 9 shows an example of a call tree in func.c. [Figure 10] Figure 10 shows an example of a file that lists the stack usage of main.c. [Figure 11] Figure 11 shows an example of a file that lists the stack usage of func.c. [Figure 12] Figure 12 shows an example of a call tree with function point information added. [Figure 13] Figure 13 shows an example of the maximum stack usage calculated for each function path. [Figure 14] Figure 14 shows the process in the compilation system according to the second embodiment, from the time a C source file is compiled until a stack overflow is detected and notified to the user. [Detailed explanation]
[0009] Hereinafter, an example of an embodiment of the present disclosure will be described in detail with reference to the drawings.
[0010] (First Embodiment) The compilation system according to this embodiment is software that functions as a compiler program that converts a computer program written in a specified language into a set of instructions in another format or language.
[0011] Figure 1 is a block diagram illustrating the schematic configuration of the compilation system according to this embodiment. Figure 2 is a diagram showing the process in the compilation system according to this embodiment, from compiling a C source file to detecting a stack overflow and notifying the user.
[0012] The compilation system 10 according to this embodiment includes the functions of an acquisition unit 12, a calculation unit 14, a storage unit 16, a setting unit 18, and a comparison notification unit 20. Each unit is realized when a computer executes a program as the compilation system 10.
[0013] The acquisition unit 12 acquires C source files 24 and 26, which are created in C language, as an example of a program created by the user.
[0014] The calculation unit 14 compiles the C source files 24 and 26 acquired by the acquisition unit 12 using the maximum stack usage calculation compilation system 22, which will be described later, to calculate the stack usage and calculate the maximum stack value 54.
[0015] The memory unit 16 stores the binary files generated by the calculation unit 14 when executing compilation on the C source files 24 and 26.
[0016] The setting unit 18 sets the stack maximum value 56 when the user inputs the stack maximum value 56 before compilation.
[0017] The comparison and notification unit 20 compares the stack maximum value 54 calculated by the calculation unit 14 with the stack maximum value 56 set by the user through the setting unit 18 by the comparison system 58. And based on the comparison result, it notifies of a stack overflow.
[0018] Subsequently, the above-mentioned maximum stack usage calculation and compilation system will be described. FIG. 3 shows a conceptual diagram of the maximum stack usage calculation and compilation system.
[0019] Here, an example of calculating the stack usage of function pointers in two source files 24 and 26 described in the C language is shown.
[0020] The C source file 24 is a source file having code for calling a function via a function pointer. For example, an example of the C source file 24 is shown in FIG. 4. FIG. 4 is a diagram showing an example of the C source file 24 having code for calling a function via a function pointer. <00…
[0023] Assembly files 30 and 32 are files that convert the C language processing described in C source files 24 and 26 into assembly language, and are shown as examples in Figures 6 and 7. Figure 6 shows an example of an assembly file resulting from compiling main.c, and Figure 7 shows an example of an assembly file resulting from compiling func.c.
[0024] Call tree 34 is a file that describes the function call path of the C source file 24 shown in Figure 4. In this case, the main function in Figure 4 calls arr. However, since the functions contained in arr itself cannot be determined at the time of compilation, call tree 34 outputs only arr, as shown in Figure 8. Figure 8 is an example of call tree 34 for main.c.
[0025] The call tree 36 is a file that describes the function call paths of the C source file 26 shown in Figure 5. An example is shown in Figure 9. Figure 9 is a diagram showing an example of the call tree 36 of func.c.
[0026] The stack usage for each function 38 and 40 are files that list the stack usage for each function, and are as shown in Figures 10 and 11 as examples. Figure 10 is an example of a file that lists the stack usage for main.c, and Figure 11 is an example of a file that lists the stack usage for func.c.
[0027] The assembler linker 42 is an assembler and linker that assembles and links assembly files 30 and 32, generating an executable file 44 and a map file 46 that shows which functions are located at which addresses.
[0028] The call tree information addition tool 48 has the function of reading assembly files 30 and 32, call trees 34 and 36, and map file 46, and outputting a call tree 50 with function pointer information added. Details of the function pointer information addition function will be described later. For example, the file shown in Figure 12 is applied to the call tree 50 with function pointer information added. Figure 12 is a diagram showing an example of a call tree 50 with function pointer information added.
[0029] The maximum stack usage calculation tool 52 reads the stack usage 38, 40 and call tree 50 for each function and calculates the maximum stack usage (maximum stack value) 54 for each function path. An example is shown in Figure 13. Figure 13 is a diagram showing an example of the maximum stack usage 54 calculated for each function path.
[0030] Next, we will explain the process for calculating the maximum stack size in the maximum stack usage calculation compiler system 22.
[0031] The compiler 28 reads the C source files 24 and 26 and generates assembly files 30 and 32, call trees 34 and 36, and stack usage figures 38 and 40 for each function. The call tree 36 generated at this time (Figure 8) contains information that the main function calls arr, but it does not contain information about what is stored in arr.
[0032] The assembler and linker 42 read assembly files 30 and 32 and generate an executable file 44 and a map file 46.
[0033] The call tree information addition tool 48 takes assembly files 30 and 32, call trees 34 and 36, and map file 46 as input and generates a call tree file 50. Here, we will explain the flow from file input to output as a detail of the function pointer information addition function described above. (1) Extract the functions that are being called from call tree file 50. In this case, arr is extracted. (2) Determine what type the arr extracted in (1) is from the debug information of assembly file 30. Furthermore, assembly file 30 contains a type arr as debug information (as shown in (a) of "Symbol Debug Information Assembly" in Figure 6), and it is determined from this debug information that arr is a function pointer array. (3) If the function extracted in (1) is a function pointer array based on the determination result in (2), the following process is performed. (3-1) Analyze the assembly description of assembly file 30 line by line. (3-2) Identify the address value being called at timing 60 when the assembly of arr[i]() is executed. (4) Identify the function being called from the address value identified in (3) and the map file 46. Here, we can see that arr[i](); is being called with fn0 to fn9 stored within it. (5) Add the information of the function identified in (4) to call trees 34 and 36, and output the updated call tree 50.
[0034] The maximum stack calculation tool 52 reads the stack usage 38, 40 and call tree 50 for each function, calculates the maximum stack usage for each function path, and outputs the result. The output maximum stack usage 54 is shown in Figure 13.
[0035] By having the maximum stack usage calculation compiler system 22 perform this processing, it becomes possible to calculate the maximum stack usage without changing the source code, even for source code that has descriptions via function pointers, and an improvement in development efficiency can be expected.
[0036] Next, referring to Figure 2, the process from the compilation of C source files 24 and 26 in the compilation system 10 according to this embodiment to the detection of a stack overflow and notification to the user will be explained.
[0037] First, the user-created C source files 24 and 26 are acquired by the acquisition unit 12, and the acquired C source files 24 and 26 are output to the calculation unit 14.
[0038] In the calculation unit 14, the C source files 24 and 26 acquired by the acquisition unit 12 are compiled by the maximum stack usage calculation compilation system 22 described above to calculate the stack usage and the maximum stack value 54. The binary file generated by the compilation is stored in the storage unit 16. The calculated maximum stack value 54 is also output to the comparison notification unit 20.
[0039] In the comparison notification unit 20, the comparison system 58 compares the maximum stack value 54 calculated by the calculation unit 14 with the maximum stack value 56 that the user has previously set in the setting unit 18. If the maximum stack value 54 calculated by the maximum stack usage calculation compilation system 22 is greater than the maximum stack value 56 that the user has set in the setting unit 18, the user is notified that a stack overflow has been detected.
[0040] Thus, by utilizing the compilation system 10 according to this embodiment, the user can obtain the maximum stack size 54 during compilation.
[0041] Then, a comparison system 58 compares the maximum stack value 56, which is pre-set by the user in the setting unit 18, with the maximum stack value 54 calculated by the maximum stack usage calculation compilation system 22, making it possible to receive notification that a stack overflow has been detected during compilation.
[0042] Therefore, in the compilation system 10 according to this embodiment, the user can detect a stack overflow without having to execute the program.
[0043] (Second Embodiment) Next, the compilation system 10 according to the second embodiment will be described. The functional configuration of the compilation system 10 according to this embodiment is basically the same as that of the first embodiment shown in Figure 1, with only some processes differing, so the same reference numerals will be used for the description.
[0044] In the first embodiment, a stack overload can be detected, but it is not possible to determine which function caused the detection. Therefore, in this embodiment, information about which function caused the stack overload is further notified to the first embodiment.
[0045] The acquisition unit 12 acquires C source files 24 and 26, which are created in C language, as an example of a program created by the user.
[0046] The calculation unit 14 compiles the C source files 24 and 26 acquired by the acquisition unit 12 using the maximum stack usage calculation compilation system 22 to calculate stack usage, calculates the maximum stack value for each function, and calculates a call tree 50 that shows the function read history.
[0047] The memory unit 16 stores the binary files generated by the calculation unit 14 when it compiles the C source files 24 and 26.
[0048] The setting unit 18 sets the maximum stack size 56 by allowing the user to input the maximum stack size before compilation.
[0049] The comparison notification unit 20 uses a comparison system to calculate the maximum stack usage for each function call history from the call tree 50, which shows the maximum stack size for each function and the function call history. It then compares the calculation result with the maximum stack size 56 set by the user by the setting unit 18. Based on the comparison result, it detects which function has exceeded the maximum stack size set by the user by the setting unit 18 and notifies the user.
[0050] Next, we will explain the process by which the compilation system 10 according to this embodiment detects a stack overflow and notifies the user when compiling C source files 24 and 26. Figure 14 is a diagram showing the process by which the compilation system 10 according to this embodiment detects a stack overflow and notifies the user when compiling C source files 24 and 26.
[0051] First, the user-created C source files 24 and 26 are acquired by the acquisition unit 12, and the acquired C source files 24 and 26 are output to the calculation unit 14.
[0052] In the calculation unit 14, the maximum stack usage calculation compilation system 22 compiles the C source files 24 and 26 acquired by the acquisition unit 12 to calculate the stack usage and the maximum stack size 55 for each function. In addition, the calculation unit 14 uses the maximum stack usage calculation compilation system 22 to calculate a call tree 50 that shows the function call history. The binary file generated by the compilation is stored in the storage unit 16. The calculated maximum stack size 55 for each function and the call tree 50 showing the function call history are output to the comparison notification unit 20.
[0053] In the comparison notification unit 20, the comparison system 58 calculates the maximum stack usage for each function call history from the maximum stack size 55 for each function and the call tree 50 which shows the function call history. The calculation result is then compared with the maximum stack size set by the user by the setting unit 18, and based on the comparison result, the setting unit 18 detects which function has exceeded the maximum stack size set by the user and notifies the user.
[0054] In this way, by utilizing the compilation system 10 according to this embodiment, the user can obtain the maximum stack size 55 for each function during compilation.
[0055] Furthermore, based on the call tree 50 which shows the function call history, the maximum stack size 55 for each function, and the maximum stack size 56 which is set in advance by the user via the setting unit 18, it becomes possible to receive notification of which function and route were used to cause a stack overflow.
[0056] Therefore, in this embodiment, the compilation system 10 can notify the compiler at compile time which function has detected a stack overflow.
[0057] Furthermore, it becomes possible to detect all potential locations where overflows could occur, helping to prevent verification omissions.
[0058] In each of the above embodiments, the maximum stack usage calculation compilation system 22 is provided with a call tree information addition tool 48 to generate the call tree 50, but this function may also be provided in the compiler 28.
[0059] Furthermore, although the compilation system 10 was described as software that executes a program in the above embodiment, the functions of the compilation system 10 may also be implemented in hardware. If implemented in hardware, the processing may be performed by hardware such as a GPU (Graphics Processing Unit), ASIC (Application Specific Integrated Circuit), and FPGA (Field-Programmable Gate Array). Alternatively, the processing may be a combination of both software and hardware. In the case of software processing, the program may be stored in various storage media and distributed from there.
[0060] Furthermore, the above embodiments are merely illustrative, and any modifications or improvements may be applied.
[0061] Furthermore, one or more elements included in one of the above multiple embodiments can be combined with one or more elements included in other embodiments of the above multiple embodiments.
[0062] The following additional information is disclosed regarding the embodiments described above.
[0063] (Note 1) On the computer, The program created by the user is compiled to calculate the stack usage, and the maximum value of the first stack is calculated. A compiler program that performs a process to notify of a stack overflow based on the comparison result between the calculated first stack maximum value and the second stack maximum value set in advance by the user.
[0064] (Note 2) On the computer, The program created by the user is compiled to calculate the stack usage, the maximum first stack value for each conditional function is calculated, and a call tree showing the function call history is generated. A compiler program that performs a process to notify of a stack overflow based on a comparison between the calculated first stack maximum value and the maximum stack usage for each function call history calculated based on the call tree, and a second stack maximum value set in advance by the user.
[0065] (Note 3) The compiler program described in Appendix 1 or Appendix 2 generates a call tree that identifies functions that call function pointers from assembly files and map files obtained by compiling the program, and calculates the first stack maximum value using the said call tree.
[0066] (Note 4) Computers The program created by the user is compiled to calculate the stack usage, and the maximum value of the first stack is calculated. A compilation method that notifies of a stack overflow based on a comparison between the calculated first stack maximum value and a second stack maximum value pre-set by the user.
[0067] (Note 5) Computers The program created by the user is compiled to calculate the stack usage, the maximum first stack value for each conditional function is calculated, and a call tree showing the function call history is generated. A compilation method that notifies of a stack overflow based on a comparison between the calculated first stack maximum value and the maximum stack usage for each function call history calculated based on the call tree, and a second stack maximum value set in advance by the user.
[0068] (Note 6) The compilation method described in Appendix 4 or Appendix 5 involves generating a call tree that identifies functions that call function pointers from assembly files and map files obtained by compiling the program, and calculating the first stack maximum value using the said call tree.
[0069] (Note 7) A calculation unit that compiles a program created by the user, calculates the stack usage, and calculates the first maximum stack size, A notification unit notifies of a stack overflow based on the comparison result between the first stack maximum value calculated by the calculation unit and the second stack maximum value set in advance by the user. A compilation system that includes this.
[0070] (Note 8) A calculation unit compiles user-created programs to calculate stack usage, calculates the maximum first stack value for each conditional function, and calculates a call tree that shows the function call history. A notification unit notifies of a stack overflow based on a comparison between the first stack maximum value calculated by the calculation unit and the maximum stack usage for each function call history calculated based on the call tree, and a second stack maximum value set in advance by the user. A compilation system that includes this.
[0071] (Note 9) The compilation system described in Appendix 7 or Appendix 8, wherein the calculation unit generates a call tree that identifies functions that call function pointers from assembly files and map files obtained by compiling the program, and calculates the maximum stack value using the call tree. [Explanation of symbols]
[0072] 10 Compilation System 12 Acquisition Department 14 Calculation Section 18. Settings Section 20 Comparison Notification Section 22. Compilation system for calculating maximum stack usage 24, 26 C source files 50 Coal Tree 54. Maximum stack size (system calculated) 56. Maximum stack size (user-defined value) 58 Comparison System
Claims
1. On the computer, The program created by the user is compiled to calculate the stack usage, and the maximum value of the first stack is calculated. A compiler program that performs a process to notify of a stack overflow based on the result of comparing the calculated first stack maximum value with a second stack maximum value set in advance by the user.
2. On the computer, The program created by the user is compiled to calculate the stack usage, the maximum first stack value for each conditional function is calculated, and a call tree showing the function call history is generated. A compiler program that performs a process to notify of a stack overflow based on a comparison between the calculated first stack maximum value and the maximum stack usage for each function call history calculated based on the call tree, and a second stack maximum value set in advance by the user.
3. The compiler program according to claim 1 or 2, wherein the first stack maximum value is calculated using the call tree, which is generated by compiling the program and identifies a function that calls a function pointer from an assembly file and a map file.
4. Computers The program created by the user is compiled to calculate the stack usage, and the maximum value of the first stack is calculated. A compilation method that notifies of a stack overflow based on a comparison between the calculated first stack maximum value and a second stack maximum value set in advance by the user.
5. Computers The program created by the user is compiled to calculate the stack usage, the maximum first stack value for each conditional function is calculated, and a call tree showing the function call history is generated. A compilation method that notifies of a stack overflow based on a comparison between the first maximum stack value calculated and the maximum stack usage for each function call history calculated based on the call tree, and a second maximum stack value set in advance by the user.
6. The compilation method according to claim 4 or 5, wherein the first stack maximum value is calculated using the call tree, which is generated by compiling the program and identifies functions that call function pointers from the assembly file and map file.
7. A calculation unit that compiles a program created by the user, calculates the stack usage, and calculates the first maximum stack size, A notification unit notifies of a stack overflow based on the comparison result between the first stack maximum value calculated by the calculation unit and the second stack maximum value set in advance by the user. A compilation system that includes this.
8. A calculation unit compiles user-created programs to calculate stack usage, calculates the maximum first stack value for each conditional function, and calculates a call tree that shows the function call history. A notification unit notifies of a stack overflow based on a comparison between the first stack maximum value calculated by the calculation unit and the maximum stack usage for each function call history calculated based on the call tree, and a second stack maximum value set in advance by the user. A compilation system that includes this.
9. The compilation system according to claim 7 or 8, wherein the first stack maximum value is calculated using a call tree that identifies functions that call function pointers from assembly files and map files obtained by compiling the program.