A time shift method and device
By intercepting and modifying the time acquisition function address of the operating system, the time offset of the target process in a multi-computer system is realized, which solves the time offset error problem and is suitable for distributed system testing, reducing resource waste and improving test efficiency. efficiency.
Patent Information
- Application Number
- CN202011303628.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2020-11-19
- Publication Date
- 2024-10-11
- Estimated Expiration
- 2040-11-19
AI Technical Summary
In multi-computer systems, existing technology cannot completely avoid time offset errors, which affects the stability of applications. Especially in distributed systems, time offset can lead to unpredictable errors.
Obtain the function address of the function by obtaining the time of the operating system, and point it to a new function address containing the offset time parameter, so that the target process can offset the time when obtaining the system time. The specific steps include intercepting the target process and writing the offset time. Move the time acquisition function to the memory space, modify the function address and cancel the interception to restore the process state.
It realizes the time offset of the preset target program without affecting other applications. It is especially suitable for large-scale distributed system testing, reducing resource waste and improving testing efficiency.
Smart Images

Figure CN112416473B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of communication technology, and in particular to a time offset method and a time offset device. Background Art
[0002] In application development, there are a large number of time-sensitive applications and scenarios, such as distributed identifier generation algorithm: SnowFlake (an algorithm used to generate unique identifiers in distributed scenarios), distributed scheduled task scheduling, and global consistency snapshots of distributed databases. However, there are always deviations in the physical time of multiple computers. Whether it is NTP (Network Time Protocol) service synchronization or other methods, errors cannot be completely avoided. If the application cannot handle such situations well, unpredictable errors may occur.
[0003] For Linux operating system, the general practice in the industry is to directly use the date -s command to modify the time. Specifically, by analyzing the source code of the date command, it is found that the essence of the method is to set the operating system time by calling the settimeofday kernel time function of the operating system (a kernel function used by the Linux system to set the system time). However, the method of modifying the time by calling the kernel time function modifies the time of the entire computer operating system, which means that other programs on the computer will also be affected. Summary of the invention
[0004] In view of the above problems, embodiments of the present invention are proposed to provide a time shift method and a corresponding time shift device that overcome the above problems or at least partially solve the above problems.
[0005] In order to solve the above problem, an embodiment of the present invention discloses a time offset method, which is applied to a preset operating system, wherein the preset operating system has a time acquisition function for acquiring the current system time; the method comprises:
[0006] Acquire a first target process for a preset target program, and perform an interception operation on the first target process;
[0007] Acquire the function address contained in the time acquisition function, and acquire the target storage address of the offset time acquisition function used for time offset;
[0008] The function address contained in the time acquisition function is pointed to the target storage address; the offset time acquisition function is provided with a target offset time parameter for offsetting the current system time;
[0009] The interception operation on the first target process is canceled to restore the running state of the intercepted first target process, and the restored first target process obtains the system time after the target offset time parameter offset through the offset time function pointed to.
[0010] Optionally, obtaining a first target process for a preset target program includes:
[0011] Obtaining a first target process identifier of the preset target program, and obtaining a first target process through the first target process identifier;
[0012] The intercepting operation on the first target process includes:
[0013] The first target process is controlled to suspend operation so that the first target process is in a suspended state.
[0014] Optionally, the step of acquiring a target storage address of an offset time acquisition function for performing time offset includes:
[0015] Acquire the memory space of the intercepted first target process, and write the offset time acquisition function into the memory space of the first target process;
[0016] The memory address written in the memory space is determined to be the target storage address of the offset time acquisition function.
[0017] Optionally, writing the offset time acquisition function into the memory space of the first target process includes:
[0018] A mapped memory space having a mapping relationship with the memory space of the first target process is established, and the offset time acquisition function is written into the mapped memory space.
[0019] Optionally, the target offset time parameter includes a time value required to be offset compared with a preset system time.
[0020] Optionally, the canceling the interception operation on the first target process so as to restore the running state of the first target process after the interception includes:
[0021] The pause control of the first target process is canceled, so that the first target process is in a running state.
[0022] Optionally, the method further comprises:
[0023] Obtaining a second target process for a preset target program, and performing an interception operation on the second target process; the second target process is the target process pointed to by the function address of the function obtained after the modification time;
[0024] Acquire an original storage address of the time acquisition function for the preset system time, and point the function address of the time acquisition function to the original storage address;
[0025] The interception operation on the second target process is canceled to restore the running state of the intercepted second target process, and the restored second target process obtains the current system time according to the time acquisition function.
[0026] The embodiment of the present invention further discloses a time offset device, which is applied to a preset operating system, wherein the preset operating system has a time acquisition function for acquiring the current system time; the device comprises:
[0027] A first target process interception module, used to obtain a first target process for a preset target program and intercept the first target process;
[0028] A function address acquisition module, used to acquire the function address contained in the time acquisition function;
[0029] A target storage address acquisition module, used to acquire a target storage address of an offset time acquisition function used for time offset;
[0030] a first function address modification module, used for pointing the function address contained in the time acquisition function to the target storage address; the offset time acquisition function is provided with a target offset time parameter for offsetting the current system time;
[0031] The first target process recovery module is used to cancel the interception operation on the first target process so as to restore the running state of the intercepted first target process, and enable the restored first target process to obtain the system time after the target offset time parameter offset through the offset time function pointed to.
[0032] Optionally, the first target process interception module includes:
[0033] A first target process acquisition submodule, used to acquire a first target process identifier of the preset target program, and acquire the first target process through the first target process identifier;
[0034] The first target process interception submodule is used to control the suspension of the first target process so that the first target process is in a suspended state.
[0035] Optionally, the target storage address acquisition module includes:
[0036] An offset time acquisition function writing submodule is used to acquire the memory space of the first target process after the interception, and write the offset time acquisition function into the memory space of the first target process;
[0037] The target storage address determination submodule is used to determine the memory address written in the memory space as the target storage address of the offset time acquisition function.
[0038] Optionally, the offset time acquisition function writing submodule includes:
[0039] The offset time acquisition function writing unit is used to establish a mapping memory space having a mapping relationship with the memory space of the first target process, and write the offset time acquisition function into the mapping memory space.
[0040] Optionally, the target offset time parameter includes a time value required to be offset compared with a preset system time.
[0041] Optionally, the first target process recovery module includes:
[0042] The first target process recovery submodule is used to cancel the suspension control of the first target process so that the first target process is in a running state.
[0043] Optionally, the device further comprises:
[0044] A second target process interception module is used to obtain a second target process for a preset target program and intercept the second target process; the second target process is a target process pointed to by a function address of a function obtained after modification time;
[0045] A second function address modification module, used for acquiring the original storage address of the time acquisition function, and pointing the function address included in the time acquisition function to the original storage address;
[0046] The second target process recovery module is used to cancel the interception operation on the second target process so as to restore the running state of the intercepted second target process and enable the restored second target process to obtain the current system time according to the time acquisition function.
[0047] An embodiment of the present invention further discloses an electronic device, comprising: a processor, a memory, and a computer program stored in the memory and capable of running on the processor, wherein the computer program implements any step of the time offset method when executed by the processor.
[0048] The embodiment of the present invention further discloses a computer-readable storage medium, on which a computer program is stored. When the computer program is executed by a processor, the steps of any one of the time offset methods are implemented.
[0049] The embodiments of the present invention include the following advantages:
[0050] In an embodiment of the present invention, the preset operating system is applied, the preset system may have a time acquisition function for acquiring the current system time, and the time acquisition function may include a function address, firstly, the first target process for the preset target program may be intercepted so as to make the first target process in a suspended state, then the target storage address of the offset time acquisition function for time offset may be acquired, and the function address included in the time acquisition function originally possessed by the system may be pointed to the target storage address of the offset time acquisition function, wherein the offset time acquisition function is provided with a target offset time parameter for offsetting the current system time, so that the first target process after the recovery of the running state may obtain the system time after the target offset time parameter is offset through the offset time acquisition function pointed to. That is, for the preset target program, by modifying the function address pointing to the time acquisition function of the preset operating system itself, the function address is pointed to the offset time acquisition function provided with the target offset time parameter, so as to obtain the system time after the offset based on the current time system, so as to realize the time offset when the preset target program acquires the operating system time, without affecting other applications on the computer, which is particularly beneficial to the scenario involving large-scale distributed system testing. BRIEF DESCRIPTION OF THE DRAWINGS
[0051] Figure 1 is a flow chart of steps of a first embodiment of a time shift method of the present invention;
[0052] Figure 2 is a flow chart of steps of a second embodiment of a time shift method of the present invention;
[0053] Figure 3A-3B is a schematic diagram of an application scenario of time offset in an embodiment of the present invention;
[0054] Figure 4 It is a structural block diagram of an embodiment of a time offset device of the present invention. DETAILED DESCRIPTION
[0055] In order to make the above-mentioned objects, features and advantages of the present invention more obvious and easy to understand, the present invention is further described in detail below with reference to the accompanying drawings and specific embodiments.
[0056] In the prior art, the general practice in the industry for time offset is to directly use the date-s command to modify the time. For example, if you need to set the system date to October 1, 2020, you can use the date-s 10 / 01 / 2020 command; if you need to set the system time to 5:55:55 pm, you can use the date-s 17:55:55 command. However, the above method of using the data-s command to modify the time modifies the time on the entire computer system, which may have the following disadvantages: it will not only affect other programs on the computer, but also when testing a certain application, it is necessary to provide an exclusive computer environment for the tested application, which will cause great cost and traffic waste for large-scale distributed system testing.
[0057] Based on the problems caused by the above-mentioned method of modifying time using the data -s command (i.e., calling the settimeofday kernel function), one of the core ideas of the embodiment of the present invention is to propose a time offset method only for a preset target program, mainly by introducing a target offset time parameter for offsetting the current system time, and modifying the pointing of the function address contained in the time acquisition function when starting the preset target program, so that the preset target program uses the introduced target offset time parameter to obtain the system time, thereby achieving time offset when the preset target program obtains the operating system time without affecting other applications on the computer, which is particularly beneficial for scenarios involving large-scale distributed system testing.
[0058] To facilitate users to understand the present invention, the following explains the terms or nouns involved in the following embodiments of the present invention:
[0059] VDSO (virtual dynamic share object, a virtual .so file provided by the kernel, and located in the kernel, not on the disk) is an acceleration method. Kernel developers have discovered that some system calls only generate kernel memory read operations, but never write operations. However, for security reasons, the user address space and kernel address space are separated. Because it is read-only, kernel developers can put these addresses in an area, which belongs to the kernel address space from the perspective of the address space range, but can be equivalent to the user state address space, and the update of the value in the address is the responsibility of the kernel state, so that the user state program can read the memory in the area and access the user state program, so that the debugging tool gdb can be used to directly start the program (i.e., step into it), greatly improving the performance of the computer operating system;
[0060] Ptrace is a common means of kernel code debugging. It is a tracing function used by the Linux system for system calls, so as to enable the user space process to track the running status of the user space process. In the Linux system, the processes in the user space are independent of each other, and one user process cannot access the address space of another process. When using the ptrace debugger, it can act as a bridge between user processes debugging user processes. That is, if one of the user processes is actively / passively tracked by another process using ptrace, the tracking process can use ptrace to fall into the kernel space (when in kernel state, it can access the relevant variables of the current process) to observe the running status of the tracked process.
[0061] Reference Figure 1 , shows a flowchart of the steps of a time offset method embodiment 1 of the present invention, which is applied to a preset operating system, wherein the preset operating system has a time acquisition function for acquiring the current system time; specifically, the following steps may be included:
[0062] Step 101, obtaining a first target process for a preset target program, and performing an interception operation on the first target process;
[0063] In an embodiment of the present invention, the preset operating system applied may be a Linux operating system, and is not applicable to operating systems such as Windows and Mac OS; and the time offset method only for a preset target program proposed in an embodiment of the present invention may be applied to a pre-written time offset program for performing fault injection on the preset target program, that is, before starting the preset target program, the pre-written time offset program may be loaded into the Linux operating system, so that after the preset target program is started, the time offset setting for the preset target program may be implemented through the pre-loaded time offset program.
[0064] In one embodiment of the present invention, after the preset target program is started, the first target process corresponding to the preset target program that is started can be obtained. The first target process can be obtained by obtaining the first target process identifier of the preset target program. The process identifier can be implemented by a process ID (i.e., process identifier), wherein the process ID can refer to a value used by the kernel of most operating systems to uniquely identify a process. Specifically, the ps command under the Linux system can be used to obtain the first target process and the first target process ID corresponding to the preset target program to be started.
[0065] In a specific implementation, the Linux system has a pre-loaded time offset program for performing fault injection into a preset target program. In order to perform fault injection into the preset target program, the first target process that is currently running can be intercepted so that the first target process is in a paused state, thereby facilitating the injection of faults into the paused first target process. The fault injection here may be a first fault injection, which may refer to related steps performed by a subsequent time offset program, and may include introducing an offset time acquisition function and modifying the function address pointed to by the time acquisition function.
[0066] In practical applications, intercepting the first target process may be performed to control the running state of the first target process.
[0067] Among them, the ptrace_attach tracing function can be used to suspend the operation of the first target process, so that the first target process is in a suspended state, that is, it is achieved by calling the attach method through ptrace; wherein, ptrace can refer to a tracing function, which mainly provides a way for a parent process to monitor and control other processes, and can also change the registers and kernel images in the child process, thereby realizing breakpoint debugging and system call tracing, then in an embodiment of the present invention, a pre-written time offset program can be implemented through ptrace to monitor and control the target process of a preset target program.
[0068] Step 102, obtaining a function address included in the time acquisition function, and obtaining a target storage address of the offset time acquisition function for performing time offset;
[0069] In a preferred embodiment, the direction of obtaining the operating system time can be started from the preset target program. The Linux operating system has a clock_gettime kernel function (i.e., time acquisition function) for obtaining the system time, and the preset target program usually obtains the operating system time by calling the time acquisition function clock_gettime, and the clock_gettime kernel function is accelerated by VDSO. At this time, the ptrace method can be used to point the function address contained in the time acquisition function clock_gettime recorded in the VDSO to another specified function address to achieve the setting of the time offset.
[0070] Among them, another specified function address can refer to the target storage address of the offset time acquisition function that can be used for time offset. The offset time acquisition function is also a clock_gettime kernel function. The difference between it and the time acquisition function is that the parameters contained in the clock_gettime kernel functions of the two are different.
[0071] In order to obtain the target storage address of the offset time acquisition function used for time offset, the offset time acquisition function can be introduced first by writing the offset time acquisition function clock_gettime into a certain memory address so as to specify the function address later.
[0072] Specifically, the memory space of the intercepted first target process can be obtained, and the offset time acquisition function can be written into the memory space of the first target process, and then the memory address written into the memory space can be determined as the target storage address of the offset time acquisition function.
[0073] In actual applications, the memory space where the offset time acquisition function is written is not the memory space originally owned by the first target process, but a mapped memory space that has a mapping relationship with the first target process, that is, the ptrace function is used to establish a mapped memory space that has a mapping relationship with the memory space of the first target process, and the process_vm_writev function of the Linux system can be called to perform a system call to write the offset time acquisition function into the mapped memory space.
[0074] In a specific implementation, the method for establishing a mapped memory space can be implemented by calling the SYS_MMAP system call of the Linux system. The MMAP function is a Linux memory mapping function that can be used to map a file or other object into memory. The called SYS_MMAP can apply for a process memory mapping space for an offset time acquisition function used for time offset. The memory mapping space obtained by calling SYS_MMAP can be the established mapped memory space.
[0075] It should be noted that the reason for adopting the method of writing the offset time acquisition function into the mapped memory space is mainly to avoid the impact of any modifications made to the target process itself. Writing the offset time acquisition function into the mapped memory space can be implemented through an external process and therefore does not affect the target process itself.
[0076] In an embodiment of the present invention, the external process performed the operation may refer to the time offset program mentioned above; then for the target process (here refers to the first target process), the established mapped memory space is essentially no different from the memory space originally possessed by the first target process, that is, for the intercepted first target process, the operation of applying for memory mapping space with an external process and subsequently rewriting the function address of the time function is imperceptible.
[0077] Step 103, pointing the function address contained in the time acquisition function to the target storage address;
[0078] The reason why the parameters contained in the offset time acquisition function clock_gettime and the time acquisition function clock_gettime are different is mainly that the specified parameters can be written to the offset time acquisition function clock_gettime through the process_vm_writev function. The parameters here can refer to the target offset time parameters used to offset the current system time, that is, the offset time acquisition function clock_gettime is set with the target offset time parameters.
[0079] In one embodiment of the present invention, the ptrace tracing function can be used to modify the function address contained in the time acquisition function clock_gettime, that is, to point the function address to the target storage address of the offset acquisition function, so as to replace the time acquisition function recorded in the VDSO with the introduced offset acquisition function, thereby realizing the time offset setting for the first target process.
[0080] The target offset time parameter may include a time value required to be offset compared with the current system time, that is, the required offset time, such as 10 seconds forward or 1 minute backward.
[0081] Specifically, the recorded time acquisition function clock_gettime may include memory locations such as CLOCK_IDS_MASK, TV_SEC_DELTA, and TV_NSEC_DELTA, and the introduced time offset acquisition function may also include the above memory locations. When the specified parameters are written to the offset time acquisition function clock_gettime, the corresponding parameters may be replaced for the above memory locations, wherein CLOCK_IDS_MASK may be replaced with the current system real-time time CLOCK_REALTIME, and TV_SEC_DELTA and TV_NSEC_DELTA may be replaced with the number of seconds and nanoseconds required to be offset, respectively, to obtain the time after the offset based on the current system time. After completing the replacement of the above specified parameters, the new function address (i.e., the target storage address of the mapped memory space where the offset time acquisition function is written) may be written to the original function address through the ptracePoke function of ptrace to complete the replacement operation for the time acquisition function.
[0082] As an example, if the first target process of the preset target program needs to be 10 seconds ahead of the current system real time, TV_SEC_DELTA can be replaced with -10. Step 104, cancel the interception operation of the first target process, so that the restored first target process obtains the offset system time through the offset time function pointed to.
[0083] After the offset time of the first target process is set, the interception operation on the first target process can be canceled, so that the first target process after canceling the interception can use the offset time acquisition function pointed to by the function address to obtain the offset time after the target offset time parameter is offset when obtaining the system time.
[0084] Among them, the time acquisition function clock_gettime can be used to obtain the current system time of the operating system. The system time obtained by the offset time acquisition function is the time after the offset is performed on the basis of the current system time. For example, if the offset time acquisition function pointed to is set 10 seconds forward (that is, when TV_SEC_DELTA is replaced with -10), the system time obtained by the first target process through the offset time acquisition function can be delayed by 10 seconds compared to the current real operating system time.
[0085] In an embodiment of the present invention, canceling the interception operation on the first target process may also be manifested as controlling the running state of the first target process.
[0086] In practical applications, the ptrace_detach function can be used to cancel the suspension control of the first target process, so that the first target process is in a running state, that is, it is achieved by calling the detach method through ptrace.
[0087] In an embodiment of the present invention, the preset operating system is applied, the preset system may have a time acquisition function for acquiring the current system time, and the time acquisition function may include a function address, firstly, the first target process for the preset target program may be intercepted so as to put the first target process in a suspended state, then the target storage address of the offset time acquisition function for time offset may be acquired, and the function address included in the time acquisition function originally possessed by the system may be pointed to the target storage address of the offset time acquisition function, wherein the offset time acquisition function is provided with a target offset time parameter for offsetting the current system time, so that the first target process after the recovery of the running state may obtain the system time after the target offset time parameter is offset through the offset time acquisition function pointed to. That is, for the preset target program, by modifying the function address pointing to the time acquisition function of the preset operating system, the function address is pointed to the offset time acquisition function provided with the target offset time parameter, so as to obtain the system time after the offset based on the current time system, so as to realize the time offset when the preset target program acquires the operating system time, without affecting other applications on the computer, which is particularly beneficial to the scenario involving large-scale distributed system testing.
[0088] Reference Figure 2, shows a flow chart of the steps of a second embodiment of a time offset method of the present invention, which is applied to a preset operating system, wherein the preset operating system has a time acquisition function for acquiring the current system time; specifically, the following steps may be included:
[0089] Step 201, obtaining a second target process for a preset target program, and performing an interception operation on the second target process; the second target process is the target process pointed to by the function address of the function obtained after modification time;
[0090] After completing the time offset setting for the target process of the preset target program, the time offset setting can also be released. In an embodiment of the present invention, it can be assumed that the second target process of the preset target program is the target process pointed to by the function address of the modified time acquisition function, that is, the target process with the time offset setting, so as to release the time offset setting for the second target process.
[0091] Among them, the time offset setting can be implemented by a time offset program loaded into the Linux system for fault injection into the preset target program. Similarly, the release of the time offset setting can be implemented by a time offset cancellation program pre-written for fault injection into the preset target program; the fault required to be injected by the time offset cancellation program can be a second fault injection, which can refer to the function address pointed to by the modified time function, and the modified function address is different from the modified function address in the time offset program.
[0092] In one embodiment of the present invention, the second target process identifier (i.e., the second target process ID) of the preset target program can be obtained so as to obtain the second target process through the second target process ID, and the second target process can be intercepted so as to cancel the time offset program for the second fault injection.
[0093] The interception operation on the second target process may be manifested as controlling the running state of the second target process, specifically, pausing the running of the second target process through the ptrace_attach tracing function, so that the second target process is in a suspended state.
[0094] It should be noted that the first target process and the second target process for the preset target program are merely named differently to distinguish between the target process for which the time offset is not set and the target process for which the time offset is set. The two are essentially the same and are both target processes corresponding to when the preset target program is started; that is, when the preset target programs are the same, the first target process and the second target process can be the same process; when the preset target programs are different, the first target process and the second target process can be different, wherein the first target process can be for the preset first target program, and the second target process can be for the preset second target program.
[0095] In the embodiment of the present invention, the first target process and the second target process may be the same target process.
[0096] Step 202, obtaining the original storage address of the time acquisition function, and pointing the function address included in the time acquisition function to the original storage address;
[0097] In order to cancel the previously set time offset setting, the ptrace method can also be used to modify the function address pointed to by the time acquisition function clock_gettime recorded in the VDSO.
[0098] Among them, the function address pointed to by the current time acquisition function clock_gettime can be the target storage address of the offset time acquisition function clock_gettime. At this time, in order to point the function address contained in the time acquisition function clock_gettime to another specified function address to contact the time offset setting, the other specified function address can refer to the original storage address of the time acquisition function.
[0099] Step 203, canceling the interception operation on the second target process, so that the restored second target process obtains the current system time according to the time acquisition function.
[0100] After releasing the offset time setting for the second target process, the interception operation on the second target process can be canceled, so that the second target process after canceling the interception can use the original time acquisition function pointed to by the function address to obtain the actual system time of the current operating system when obtaining the system time.
[0101] In one embodiment of the present invention, canceling the interception operation on the second target process may also be manifested as controlling the running state of the second target process.
[0102] In actual applications, the ptrace_detach function can be used to cancel the suspension control of the second target process, so that the second target process is in a running state.
[0103] In an embodiment of the present invention, for a preset target program, by modifying the function address pointing to the time acquisition function of the preset operating system, the function address is pointed to the offset time acquisition function with the target offset time parameter, so as to obtain the system time after the offset based on the current time system, so as to achieve the time offset when the preset target program acquires the operating system time, without affecting other applications on the computer, which is particularly beneficial for scenarios involving large-scale distributed system testing. In addition, the time offset can be removed for the preset target program with the time offset set, which is also to modify the function address pointing to the time acquisition function, and point the function address to the original function address of the original time acquisition function, so as to achieve the system time of the current operating system when the preset target program acquires the operation time.
[0104] Reference Figure 3A-3B , shows a schematic diagram of the application scenario of time offset in an embodiment of the present invention. Time offset can refer to causing a deviation between the system time obtained by a preset target program from a computer operating system and the current real system time, thereby achieving an operation of making the time of a distributed application program inconsistent.
[0105] In one embodiment of the present invention, it is necessary for testers and developers to quickly simulate the time inconsistency of the computer system in order to test whether the developed application can correctly handle such a situation and improve the development and testing efficiency; then there may be a scenario for testing a certain application, that is, the above-mentioned preset target program can be the application that needs to be tested.
[0106] The processing method of the time offset may specifically involve two processes, which may include a process of setting the time offset and a process of releasing the time offset setting.
[0107] During the test, the target process corresponding to the target program can be time-shifted, such as Figure 3AAs shown, the process may include: obtaining the process ID of the target process; attaching to the target process through ptrace to put the target process in a stopped state; using ptrace to map a memory space in the target process memory, and using process_vm_writev to write the fake clock_gettime function (which may be the offset time acquisition function mentioned above) to the memory space of the target process; writing the specified parameters to the fake clock_gettime function through process_vm_writev, where the parameters refer to the time that is actually desired to be offset, that is, the time parameters that need to be offset; using ptrace to modify the address of the clock_gettime function in the VDSO to the address of the fake clock_gettime function, so that it can directly jump to the fake clock_gettime function; and using ptrace to detach the target process to restore the target process in the stopped state to a running state.
[0108] After completing the test on the target process, you can remove the time offset setting, such as Figure 3B As shown, it should be noted that the target process of the completed test can be a target process whose time acquisition function address has been modified, and the process may include: obtaining the process ID of the target process; attaching to the target process through ptrace to put the target process in a stopped state; using ptrace to modify the address of the clock_gettime function in the VDSO to let it jump to the original clock_gettime function address (that is, the original function address recorded by the VDSO); ptrace detaching the target process to restore the target process in the stopped state to a running state.
[0109] As an example, the following application scenarios may also exist:
[0110] A scheduling platform may have scheduled tasks that are performed according to time points. Among them, there is an algorithm with a unique identifier related to the time point and the algorithm is time-sensitive. In practice, the time of the two servers may be out of sync. Generally, testing means are used to test whether the time-related service processes are completed, and the incomplete service processes are screened out, and the corresponding scheduled tasks are completed by directly changing the server's operation time.
[0111] Since the server is deployed in a container and other service processes (i.e. other scheduled tasks) may exist in the container, directly changing the operation time of the server may affect the progress of other scheduled tasks. In this example, a single scheduled task (i.e. a single service process) can be intercepted through a system call and a fake address can be specified to obtain a fake time, that is, a time offset is set to determine whether the service process is successfully called and completed. The above-mentioned time offset processing method will only affect the service process that needs to be tested, and will not affect the entire operating system and other service processes.
[0112] In the embodiment of the present invention, by using the time offset method only for the preset target program, only the preset target program will be affected. When developing and testing a time-sensitive distributed system, the reliance on a dedicated computer environment can be avoided, the waste of computer resources can be greatly reduced, and the work efficiency of development and testing personnel can be greatly improved.
[0113] It should be noted that, for the sake of simplicity, the method embodiments are described as a series of action combinations, but those skilled in the art should be aware that the embodiments of the present invention are not limited by the order of the actions described, because according to the embodiments of the present invention, certain steps can be performed in other orders or simultaneously. Secondly, those skilled in the art should also be aware that the embodiments described in the specification are all preferred embodiments, and the actions involved are not necessarily required by the embodiments of the present invention.
[0114] Reference Figure 4 , shows a structural block diagram of an embodiment of a time offset device of the present invention, which is applied to a preset operating system, wherein the preset operating system has a time acquisition function for acquiring the current system time; specifically, it may include the following modules:
[0115] A first target process interception module 401 is used to obtain a first target process for a preset target program and intercept the first target process;
[0116] A function address acquisition module 402, used to acquire the function address included in the time acquisition function;
[0117] A target storage address acquisition module 403, used to acquire a target storage address of an offset time acquisition function used for time offset;
[0118] A first function address modification module 404 is used to point the function address contained in the time acquisition function to the target storage address; the offset time acquisition function is provided with a target offset time parameter for offsetting the current system time;
[0119] The first target process recovery module 405 is used to cancel the interception operation on the first target process so as to restore the running state of the intercepted first target process, and enable the restored first target process to obtain the system time after the target offset time parameter offset through the offset time function pointed to.
[0120] In one embodiment of the present invention, the first target process interception module 401 may include the following submodules:
[0121] A first target process acquisition submodule, used to acquire a first target process identifier of the preset target program, and acquire the first target process through the first target process identifier;
[0122] The first target process interception submodule is used to control the suspension of the first target process so that the first target process is in a suspended state.
[0123] In one embodiment of the present invention, the target storage address acquisition module 403 may include the following submodules:
[0124] An offset time acquisition function writing submodule is used to acquire the memory space of the first target process after the interception, and write the offset time acquisition function into the memory space of the first target process;
[0125] The target storage address determination submodule is used to determine the memory address written in the memory space as the target storage address of the offset time acquisition function.
[0126] In one embodiment of the present invention, the offset time acquisition function writing submodule may include the following units:
[0127] The offset time acquisition function writing unit is used to establish a mapping memory space having a mapping relationship with the memory space of the first target process, and write the offset time acquisition function into the mapping memory space.
[0128] In an embodiment of the present invention, the target offset time parameter includes a time value required to be offset compared with a preset system time.
[0129] In one embodiment of the present invention, the first target process recovery module 405 may include the following submodules:
[0130] The first target process recovery submodule is used to cancel the suspension control of the first target process so that the first target process is in a running state.
[0131] In one embodiment of the present invention, the device may further include the following modules:
[0132] A second target process interception module is used to obtain a second target process for a preset target program and intercept the second target process; the second target process is a target process pointed to by a function address of a function obtained after modification time;
[0133] A second function address modification module, used for acquiring the original storage address of the time acquisition function, and pointing the function address included in the time acquisition function to the original storage address;
[0134] The second target process recovery module is used to cancel the interception operation on the second target process so as to restore the running state of the intercepted second target process and enable the restored second target process to obtain the current system time according to the time acquisition function.
[0135] As for the device embodiment, since it is basically similar to the method embodiment, the description is relatively simple, and the relevant parts can be referred to the partial description of the method embodiment.
[0136] An embodiment of the present invention further provides an electronic device, including:
[0137] The invention comprises: a processor, a memory and a computer program stored in the memory and capable of running on the processor. When the computer program is executed by the processor, each process of the above-mentioned time shift method embodiment is implemented and the same technical effect can be achieved. To avoid repetition, it will not be described here.
[0138] An embodiment of the present invention further provides a computer-readable storage medium, on which a computer program is stored. When the computer program is executed by a processor, the various processes of the above-mentioned time offset method embodiment are implemented, and the same technical effect can be achieved. To avoid repetition, it will not be repeated here.
[0139] The various embodiments in this specification are described in a progressive manner, and each embodiment focuses on the differences from other embodiments. The same or similar parts between the various embodiments can be referenced to each other.
[0140] It will be appreciated by those skilled in the art that the embodiments of the present invention may be provided as methods, devices, or computer program products. Therefore, the embodiments of the present invention may take the form of a complete hardware embodiment, a complete software embodiment, or an embodiment combining software and hardware. Moreover, the embodiments of the present invention may take the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program codes.
[0141] The embodiments of the present invention are described with reference to the flowcharts and / or block diagrams of the methods, terminal devices (systems), and computer program products according to the embodiments of the present invention. It should be understood that each process and / or block in the flowchart and / or block diagram, as well as the combination of the processes and / or blocks in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing terminal device to generate a machine, so that the instructions executed by the processor of the computer or other programmable data processing terminal device generate instructions for implementing the processes in the flowchart and / or block diagram. Figure 1 A process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.
[0142] These computer program instructions may also be stored in a computer-readable memory capable of directing a computer or other programmable data processing terminal device to operate in a specific manner, so that the instructions stored in the computer-readable memory produce a manufactured product including an instruction device, which implements the process Figure 1 A process or multiple processes and / or boxes Figure 1 A function specified in one or more boxes.
[0143] These computer program instructions can also be loaded onto a computer or other programmable data processing terminal device so that a series of operating steps are executed on the computer or other programmable terminal device to produce a computer-implemented process, thereby providing instructions for executing on the computer or other programmable terminal device to implement the process. Figure 1 A process or multiple processes and / or boxes Figure 1 The steps for the functions specified in one or more boxes.
[0144] Although the preferred embodiments of the present invention have been described, those skilled in the art may make additional changes and modifications to these embodiments once they have learned the basic creative concept. Therefore, the appended claims are intended to be interpreted as including the preferred embodiments and all changes and modifications that fall within the scope of the embodiments of the present invention.
[0145] Finally, it should be noted that, in this article, relational terms such as first and second, etc. are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Moreover, the terms "include", "comprise" or any other variants thereof are intended to cover non-exclusive inclusion, so that a process, method, article or terminal device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or terminal device. In the absence of further restrictions, the elements defined by the sentence "comprise a ..." do not exclude the existence of other identical elements in the process, method, article or terminal device including the elements.
[0146] The above is a detailed introduction to a time shift method and a time shift device provided by the present invention. Specific examples are used in this article to illustrate the principle and implementation mode of the present invention. The description of the above embodiments is only used to help understand the method and core idea of the present invention. At the same time, for those skilled in the art, according to the idea of the present invention, there will be changes in the specific implementation mode and application scope. In summary, the content of this specification should not be understood as limiting the present invention.
Claims
1. A time migration method, characterized in that: Applied to a preset operating system, the preset operating system has a time acquisition function for acquiring the current system time; the method comprises: Acquire a first target process for a preset target program, and perform an interception operation on the first target process; Acquire the function address contained in the time acquisition function, and acquire the target storage address of the offset time acquisition function used for time offset; Pointing the function address contained in the time acquisition function to the target storage address; including: acquiring the memory space of the first target process after the interception, writing the offset time acquisition function into the memory space of the first target process; determining the memory address written in the memory space as the target storage address of the offset time acquisition function; the offset time acquisition function is provided with a target offset time parameter for offsetting the current system time; Cancel the interception operation on the first target process so as to restore the running state of the intercepted first target process, and enable the restored first target process to obtain the system time after the target offset time parameter is offset through the offset time function pointed to; Obtaining a second target process for a preset target program, and performing an interception operation on the second target process; the second target process is the target process pointed to by the function address of the function obtained after the modification time; Acquire the original storage address of the time acquisition function, and point the function address contained in the time acquisition function to the original storage address; The interception operation on the second target process is canceled to restore the running state of the intercepted second target process, and the restored second target process obtains the current system time according to the time acquisition function.
2. The method according to claim 1, characterized in that The obtaining of a first target process for a preset target program includes: Obtaining a first target process identifier of the preset target program, and obtaining a first target process through the first target process identifier; The intercepting operation on the first target process includes: The first target process is controlled to suspend operation so that the first target process is in a suspended state.
3. The method according to claim 1, characterized in that The step of writing the offset time acquisition function into the memory space of the first target process includes: A mapped memory space having a mapping relationship with the memory space of the first target process is established, and the offset time acquisition function is written into the mapped memory space.
4. The method according to claim 1, characterized in that: The target offset time parameter includes a time value required to be offset compared with a preset system time.
5. The method according to claim 1, characterized in that The canceling of the interception operation on the first target process so as to restore the running state of the first target process after the interception includes: The pause control of the first target process is canceled, so that the first target process is in a running state.
6. A time shift device, characterized in that: Applied to a preset operating system, the preset operating system has a time acquisition function for acquiring the current system time; the device comprises: A first target process interception module, used to obtain a first target process for a preset target program and intercept the first target process; A function address acquisition module, used to acquire the function address contained in the time acquisition function; The target storage address acquisition module is used to acquire the target storage address of the offset time acquisition function used for time offset; including: acquiring the memory space of the first target process after the interception, writing the offset time acquisition function into the memory space of the first target process; determining the memory address written in the memory space as the target storage address of the offset time acquisition function; a first function address modification module, used for pointing the function address contained in the time acquisition function to the target storage address; the offset time acquisition function is provided with a target offset time parameter for offsetting the current system time; A first target process recovery module, used to cancel the interception operation on the first target process, so as to restore the running state of the intercepted first target process, and enable the restored first target process to obtain the system time after the target offset time parameter is offset through the offset time function pointed to; A second target process interception module is used to obtain a second target process for a preset target program and intercept the second target process; the second target process is a target process pointed to by a function address of a function obtained after modification time; A second function address modification module, used for acquiring the original storage address of the time acquisition function, and pointing the function address included in the time acquisition function to the original storage address; The second target process recovery module is used to cancel the interception operation on the second target process so as to restore the running state of the intercepted second target process and enable the restored second target process to obtain the current system time according to the time acquisition function.
7. An electronic device, characterized in that: include: A processor, a memory, and a computer program stored in the memory and capable of running on the processor, wherein when the computer program is executed by the processor, the steps of the time shift method according to any one of claims 1 to 5 are implemented.
8. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the steps of the time shift method according to any one of claims 1 to 5 are implemented.
