JavaScript identifier conflict detection and solution method based on V8 engine instrumentation
By modifying the parser and bytecode generator of the V8 engine, dynamic instrumentation records processing object information, the detection and resolution of JavaScript global identifier conflicts is solved, and the accurate detection and resolution of conflicts in multi-store environments is achieved, and the improvement of improvement of the improvement of conflicts. Inspection accuracy and efficiency.
Patent Information
- Application Number
- CN202411105761.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-08-13
- Publication Date
- 2025-01-14
- Estimated Expiration
- 2044-08-13
AI Technical Summary
The prior art has limitations in detecting and resolving JavaScript global identifier conflicts, especially in the practical applications of multiple JavaScript libraries. Static analysis methods cannot accurately reflect runtime behavior, and dynamic analysis fails to fully cover conflict detection.
By modifying the parser and bytecode generator of the V8 engine, dynamically record processing object information, collect write operation logs, detect and resolve identifier conflicts within the web page, and use shadow variables and monitoring code to cover all running codes to achieve Accurate conflict detection and resolution.
It realizes accurate detection and resolution of JavaScript global identifier conflicts, covers all running codes, ensures the accuracy and efficiency of detection results, and is suitable for multi-library environments.
Smart Images

Figure CN119311561A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to a method for detecting a JavaScript identifier conflict in the field of computer software engineering, and in particular to a method for detecting and resolving a JavaScript identifier conflict based on V8 engine plug-in. Background Art
[0002] JavaScript code is widely used in client-side web applications. In a web browser, all scripts loaded on the same page share the same namespace, so a script may read or even overwrite the global variables or global functions of other scripts, which is called a global identifier conflict. Global identifier conflicts pose a major threat to the integrity of developers' code. Severe identifier conflicts may cause the execution logic of the program to change, return incorrect values, or even crash the program. Global identifier conflicts are difficult to prevent. In order to avoid identifier conflicts, developers need to carefully check the source code before referencing third-party libraries. Due to the existence of code obfuscation and minimization, this task is very difficult. Unless there is no conflict at all in the referenced script, developers need to modify all conflicting locations. Even so, global identifier conflicts cannot be completely avoided. This is because third-party code is hosted in a remote server and can be modified by the provider of the script at any time without notifying the user. In addition, a script can dynamically introduce other scripts. This can be prevented by configuring CSP, but the usage of CSP is limited because many websites need to dynamically load additional scripts to maintain normal functions.
[0003] Previous work has studied potential global identifier conflicts between JavaScript libraries, such as using simulated clients to test whether JavaScript libraries behave differently when loaded under different settings. However, this approach only studies a limited number of JavaScript libraries in a simulated environment and can only test simple operations of the library, while a real application may include more than two libraries. Existing methods are based on a selective record-replay dynamic analysis framework, which only inserts monitoring code into the selected source code and cannot cover other dynamically loaded code, so the results cannot accurately reflect the possible conflicts in real applications.
[0004] There are at least the following problems in the prior art:
[0005] The number of JavaScript libraries studied is too small, and there may be more than two JavaScript libraries in actual applications; static methods are used for type inference, and the focus is on inferring the JavaScript type of a single script. Static analysis methods cannot infer the runtime behavior of JavaScript code; dynamic analysis of JavaScript code remains at the level of detecting abnormal functions, data competition detection, deterministic analysis, performance analysis, concurrency error detection, and crash path calculation, and dynamic analysis is not fully used for the detection of JavaScript global identifier conflicts. Summary of the invention
[0006] The purpose of the present invention is to provide a detection and solution method for JavaScript identifier conflicts based on V8 engine plug-in in view of the deficiencies of the prior art.
[0007] The objective of the present invention is achieved through the following technical solutions:
[0008] (1) Modify the V8 parser and V8 bytecode generator so that the V8 engine records the information of each processing object according to the type of the processing object during code parsing and dynamic stubbing;
[0009] (2) Run the instrumented V8 engine and dynamically collect the logs of write operations corresponding to all processing objects in the web page;
[0010] (3) When the webpage is loaded, the conflict of writing operations of different scripts to the same memory location is detected according to the record log, and the conflict result of the JavaScript identifier of the current webpage is obtained, and then the conflict information is recorded to obtain a conflict information list;
[0011] (4) Refresh the page, and then rewrite the conflicting processing objects according to the conflict information list so that all conflicts in the current conflict information are resolved. Then repeat (2)-(3) to perform conflict detection on the web page. If conflict information still exists, continue to resolve it until there is no conflict in the current web page, and the JavaScript identifier conflict is resolved.
[0012] In (1), when the processing object is a directly defined global function, monitoring code is added to the V8 parser, so that in the JS program pre-parsing step, the definition / writing operations of all global functions are captured and the global function names are checked by dynamically inserted monitoring code, and the names, unique identification IDs, definition locations, execution script IDs, and context IDs of all functions f with non-empty names are recorded as logs.
[0013] In (1), when the processing object is an assignment statement, the V8 bytecode generator is modified, specifically, by dynamically inserting monitoring code at all locations where write operations are performed by inserting function call nodes in the generated abstract syntax tree, and recording the type, value, scope, execution script ID, context ID, and shadow ID of the target variable as a log.
[0014] The monitoring code specifically includes:
[0015] Create a shadow variable v' for each target variable v of a write operation;
[0016] Replace the assignment statement;
[0017] A record-write function recordWrite(v,v',t,e) is used to infer and record the information of the target variable of the write operation; in the record-write function recordWrite(v,v',t,e), v represents the name of the target variable, v' is used to store the unique identifier of the target variable, t represents the value assigned to the target variable or the target object attribute and is recorded as an assignment parameter, which is used to infer the type of the target variable, the assignment parameter t is consistent with the type of the target variable v, and e represents the operation expression performed on the target variable; if the type of the written target variable v is a basic data type, the shadow variable v' stores the variable value, and if the written target variable v is an assignment expression containing an object type, the memory address of the target object v in the V8 engine is obtained as a shadow ID, and the shadow ID is stored as the attribute v'.__id__ of the shadow variable v'.
[0018] For the assignment statement of common variables, and the data type written is a basic data type, the writing operation methods include:
[0019] If the original assignment statement is a non-nested assignment statement, replace the original assignment statement with a nested assignment statement by changing the abstract syntax tree output by the parser; if the original assignment statement is a nested assignment statement, access its abstract syntax tree, split the original assignment statement into non-nested forms and replace them one by one with new nested assignment statements;
[0020] For the assignment statement of a common variable, and the data type written is an object type, the writing operation methods include:
[0021] Create corresponding shadow variables for the object and each attribute, and record the data value and type through each shadow variable;
[0022] For assignment statements of object properties, and the data type written is a basic data type, the writing operation methods include:
[0023] Create a shadow variable v' for the target object to be written, use the memory address of the shadow variable v' as the unique identification ID of the target object, and use the type function typeof(v'.prop) to record the written data value and data type;
[0024] For assignment statements of object properties, and the data type written is an object type, the writing operation methods include:
[0025] A shadow variable v' and a first shadow variable v1'' are created for the target object and the object property of the target object respectively, and the memory addresses of the shadow variable v' and the first shadow variable v1'' are used as the unique identification ID of the object respectively, and the left and right expressions of the assignment statement are replaced. If the data type written in the assignment statement in the written object is a basic data type, the written data value and data type are recorded through the type function typeof(v'.v1''.prop). If the data type written in the assignment statement in the written object is an object type, a second shadow variable v2' is created for it, and the memory address of the second shadow variable v2' is used as the unique identification ID of the object, and the variables in the assignment statement are replaced, and the corresponding variable values and data types are recorded.
[0026] In (3), the DOMContentLoaded event is monitored by a set event handler, so that the conflict detection function is triggered when the web page is loaded.
[0027] In (3), the JavaScript identifier conflict results include three types of conflicts, namely, function definition conflict, variable value conflict and variable type conflict.
[0028] For function definition conflicts, check whether there are multiple global function definitions with the same name, confirm whether the function scope is global, and whether the global functions with the same name are defined in different scripts at the same time;
[0029] For variable value conflicts, check whether the same global variable name is assigned different values by different scripts; for object type global variables, further check the definition conflicts of objects and object attributes respectively;
[0030] For variable type conflicts, check the assignment of the same global variable in different scripts to see whether different types of values are assigned by different scripts. In particular, a global identifier may be used as both a function name and a variable name. It is necessary to cross-check the variable and function definition records.
[0031] The above (4) is specifically:
[0032] (4.1) Refresh the page and resolve the conflict by renaming the conflicting variables or functions in the conflict information list;
[0033] (4.2) According to the whitelist provided by the user, check whether there are multiple entries of different source scripts in each group in the current conflict information list and the multiple source scripts are included in the same group of the whitelist. If so, delete the entries containing these source scripts in the current group, thereby generating the final conflict information list that needs to be resolved;
[0034] (4.3) Based on the current conflict information list, build the AST of each script, recursively check each node in the AST of the current script, replace all the locations of the functions or variables that reference the conflict with the corresponding renames, traverse all scripts in turn, and complete the renaming replacement of the conflicting locations of all scripts;
[0035] (4.4) Repeat (2)-(3) to continue to perform conflict detection on the current web page and obtain a new conflict information list. After executing (4.1) and (4.2), determine whether the updated conflict information list is empty after removing the whitelist entries. If it is empty, it indicates that all conflicts have been resolved. If it is not empty, execute (4.3) until the conflict information list is empty.
[0036] The (4.1) is specifically:
[0037] Modify the V8 bytecode generator, add the conflict resolution function rewriteResolveConflict and rewriteResolveConflict function, obtain the conflict information list and use the conflict resolution function to add a rename attribute to each conflicting object in the conflict information list. The content of the rename attribute is the original conflict function or variable name plus a randomly generated long suffix.
[0038] The beneficial effects of the present invention are:
[0039] The present invention can cover all running codes by dynamically inserting monitoring codes into the executed JavaScript code, and can uniquely identify JavaScript objects by using shadow variables and immutable identification properties, making the analysis of aliases simpler and more accurate. The present invention uses dynamic analysis for the first time to detect conflicts of all JavaScript script global identifiers, ensuring the accuracy of conflict detection, because each conflict detected must have occurred, and the effect is better than the existing method, and has the advantage of high accuracy. BRIEF DESCRIPTION OF THE DRAWINGS
[0040] Figure 1The present invention proposes a method flow chart of detecting and resolving JavaScript identifier conflicts based on V8 engine plug-in.
[0041] Figure 2 It is a diagram showing the modification positions and modification status of the tool during the parsing process of JavaScript code in the V8 engine. DETAILED DESCRIPTION
[0042] In order to enable those skilled in the art to better understand the scheme of the present invention, the present invention is further described in detail below in conjunction with the accompanying drawings and specific implementation methods. Obviously, the described embodiments are only part of the embodiments of the present invention, rather than all the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in the field without making creative work are within the scope of protection of the present invention.
[0043] The present invention dynamically inserts monitoring code into the executed JavaScript code, and uses it to monitor the writing of global variables and functions, so as to identify the writing operation conflicts of different scripts to the same global memory location.
[0044] like Figure 1 As shown, the present invention proposes a method for detecting and resolving JavaScript identifier conflicts based on V8 engine instrumentation, which includes the following steps:
[0045] (1) Modify the V8 parser and V8 bytecode generator so that the V8 engine records the information of each processing object according to the type of the processing object during code parsing and dynamic stubbing;
[0046] Depending on how developers define global functions, the processing methods are different, including the following two methods. When the processing object is a directly defined global function, such as: function f(args){stmts;}, add monitoring code to the V8 parser, so that in the JS program pre-parsing step, all global function definition / write operations and global function names are captured through dynamically inserted monitoring code, and the names, unique identification IDs, definition locations (row and column numbers), execution script IDs, and context IDs of all functions f with non-empty names are recorded as logs.
[0047] The assignment statement includes the case of assigning a function to a global variable, such as: window.f = function (args) {stmts;}, as well as the definition and assignment of other global variables.
[0048] When the processing object is an assignment statement, the V8 bytecode generator is modified. Specifically, the monitoring code is dynamically inserted into all locations where write operations are performed by inserting function call nodes in the generated abstract syntax tree (AST tree) instead of modifying the bytecode, and the type, value, scope, execution script ID, context ID, and shadow ID of the target variable are recorded as logs.
[0049] The monitoring code specifically includes:
[0050] Create a shadow variable v' for each target variable v of a write operation;
[0051] Replace the assignment statement;
[0052] Use the record-write function recordWrite(v,v',t,e) to infer and record the type, value, scope, execution script ID, context ID, shadow ID and other information of the target variable of the write operation; in the record-write function recordWrite(v,v',t,e), v represents the name of the target variable, v' is used to store the unique identifier of the target variable, t represents the value assigned to the target variable or the target object attribute and is recorded as the assignment parameter, which is used to infer the type of the target variable. The assignment parameter t is consistent with the type of the target variable v, so typeof(t) is used to infer the type of the target variable v. e represents the operation expression on the target variable; if the type of the target variable v written is a basic data type, the shadow variable v' stores the variable value. If the target variable v written is an assignment expression containing an object type, the memory address of the target object v in the V8 engine is obtained as the shadow ID, and the shadow ID is stored as the attribute v'.__id__ of the shadow variable v'.
[0053] For the assignment statement of common variables, and the data type written is a basic data type, the writing operation methods include:
[0054] If the original assignment statement is a non-nested assignment statement, a shadow variable v' is created for the written target. By changing the abstract syntax tree output by the parser, the original assignment statement is replaced with a nested assignment statement, thereby defining v and v' at the same time. That is, the inserted monitoring code is:
[0055] v'=v=e (1)
[0056] recordWrite(v,v',v',e)(2)
[0057] This avoids statements like arr[i++] being called twice. In formula (2), v' is the shadow variable of v. The value of v' is exactly the same as v. As the unique identifier of the target variable, the type of v' is exactly the same as v. Therefore, v' can be used to determine the type of v.
[0058] If a simplified write operator is used, such as +=, a v+=e type assignment statement is created for it, and the monitoring code inserted is:
[0059] v'=v+=e (3)
[0060] recordWrite(v,v',v',e)(4)
[0061] In formula (4), v' is the shadow variable of v. The value of v' is exactly the same as v. As the unique identifier of the target variable, the type of v' is exactly the same as v. Therefore, v' can be used to judge the type of v.
[0062] If the original assignment statement is a nested assignment statement, then access its abstract syntax tree, split the original assignment statement into non-nested forms and replace them one by one with new nested assignment statements according to the above replacement method, such as an assignment statement of type v1=v2=e, create shadow variables v1' and v2' for variables v1 and v2 respectively, and insert the monitoring code as:
[0063] v1'=v1=v2'=v2=e (5)
[0064] recordWrite(v2,v2',v2',e)(6)
[0065] recordWrite(v1,v1',v1',v2'=v2=e) (7)
[0066] Among them, v1 and v2 are both variables of basic data types, and the recordWrite function is called in sequence to record the write operation of the variables. Since v1 and v2 are both basic data types, the method of recording the identifier and the method of distinguishing the data type are consistent with the above method.
[0067] When the target of writing is the property of an object, the object needs to be identified. That is, for the assignment statement of a common variable, and the data type written is the object type, the writing operation methods include:
[0068] Create corresponding shadow variables for the object and each attribute, and record the data value and type through each shadow variable; use the shadow variable to obtain the shadow ID, and identify the object through the unique shadow ID to avoid re-evaluating the expression. For example, if the expression is vp=e, in this case, the inserted monitoring code will create a shadow variable v' for the parent object variable v instead of creating a shadow variable for the attribute vp. Then, the monitoring code will write through the shadow variable instead of the original variable v, that is, the inserted code is:
[0069] v '.p=e (8)
[0070] recordWrite(vp,v',v'.p,e)(9)
[0071] The monitoring code inserted at this time will obtain the memory address of the target object in the V8 engine as the shadow ID, and store the shadow ID as the attribute __id__ of the shadow variable, and uniquely identify the object according to the shadow ID. The shadow object is an alias for the original target object, so the attributes of the shadow object are the attributes of the target object. When determining the type, the type of the attribute is determined instead of the type of the object.
[0072] For assignment statements of object properties, and the data type written is a basic data type, the writing operation methods include:
[0073] Create a shadow variable v' for the target object to be written, use the memory address of the shadow variable v' as the unique identification ID of the target object, and use the type function typeof(v'.prop) to record the written data value and data type;
[0074] For assignment statements of object properties, and the data type written is an object type, the writing operation methods include:
[0075] A shadow variable v' and a first shadow variable v1'' are created for the target object and the object property of the target object respectively, and the memory addresses of the shadow variable v' and the first shadow variable v1'' are used as the unique identification ID of the object respectively, and the left and right expressions of the assignment statement are replaced. If the data type written in the assignment statement in the written object is a basic data type, the written data value and data type are recorded through the type function typeof(v'.v1''.prop). If the data type written in the assignment statement in the written object is an object type, a second shadow variable v2' is created for it, and the memory address of the second shadow variable v2' is used as the unique identification ID of the object, and the variables in the assignment statement are replaced, and the corresponding variable values and data types are recorded.
[0076] For object literals and constructor expressions, if the property p is initialized in an object literal expression, such as an expression of type {p1: e1, p2: e2...}, a shadow variable o' is created for the newly created object, and the monitoring code inserted is:
[0077] o'={p1 : e1'= e1, p2 : e2'= e2, ...} (10)
[0078] recordWrite(o'.p1,o',e1',e1)(11)
[0079] recordWrite(o'.p2,o',e2',e2)(12)
[0080] RecordWrite records the target variable name, writes the unique identifier of the target object, the attribute type, and the operation expression in sequence.
[0081] If the property p is defined in a constructor or an object method through the keyword this, such as new Obj(...){this.p=e;}, in the case of a constructor, a shadow variable o' is created for the object inside the constructor, and the writing of the property this.p is recorded, and the shadow ID of this is recorded through o'. The inserted monitoring code is:
[0082] new Obj(...){
[0083] o' = this;
[0084] o'.p = e;
[0085] recordWrite(this.p,o',o'.p,e);}(13)
[0086] The recorded information is consistent with the above operation.
[0087] (2) Run the instrumented V8 engine (Chrome V8 JavaScript engine) and dynamically collect the logs of write operations corresponding to all processing objects in the web pages opened in the browser, and store the logs in the DOMWindow class;
[0088] (3) When the webpage is loaded, the conflict of writing operations of different scripts to the same memory location is detected according to the recorded log, the conflict result of the JavaScript identifier of the current webpage is obtained, and then the conflict information is recorded to obtain a conflict information list. Among them, the DOMContentLoaded event is monitored by the set event handler, so that the conflict detection function after this step is triggered when the webpage is loaded.
[0089] JavaScript identifier conflict results include three types of conflicts, namely function definition conflict, variable value conflict and variable type conflict;
[0090] For function definition conflicts, check whether there are multiple global function definitions with the same name, confirm whether the function scope is global, and whether the global functions with the same name are defined in different scripts at the same time; in particular, a global function may be defined in two ways: function declaration and assignment statement, so it is necessary to cross-check the variable and function definition records;
[0091] For variable value conflicts, check whether the same global variable name is assigned different values by different scripts; for object type global variables, further check the definition conflicts of objects and object attributes respectively;
[0092] For variable type conflicts, check the assignment of the same global variable in different scripts to see whether different types of values are assigned by different scripts. In particular, a global identifier may be used as both a function name and a variable name. It is necessary to cross-check the variable and function definition records.
[0093] After a conflict is detected, the conflict information is stored in a file in the form of an object literal list. The two properties of the object representing the conflict information are the conflicting function or variable name and the source script. In the list, objects with the same conflicting function or variable name are merged into a group, and redundant entries with the same source script in the group are deleted to ensure that the script sources of entries in the same group are different, thereby obtaining a conflict information list.
[0094] Provide users with a configurable whitelist list. Users can freely add entries to the whitelist list and group these entries. Conflicts between scripts in the group will be ignored.
[0095] (4) Refresh the page, and then rewrite the conflicting processing objects according to the conflict information list so that all conflicts in the current conflict information are resolved. Then repeat (2)-(3) to perform conflict detection on the web page. If there is still conflict information (that is, there is a new conflict), continue to resolve it until there is no conflict in the current web page, and the JavaScript identifier conflict is resolved.
[0096] (4) Specifically:
[0097] (4.1) Refresh the page and resolve the conflict by renaming the conflicting variables or functions in the conflict information list;
[0098] (4.1) Specifically:
[0099] Modify the V8 bytecode generator, add the conflict resolution functions rewriteResolveConflict and rewriteResolveConflict, obtain the conflict information list C from the file, C includes the conflicting function or variable name, script source and other information, if C is empty, end the function;
[0100] C={(f i , r i )|i=1,2,...,n}; (14)
[0101] Among them, f i Indicates the name of the i-th conflicting function or variable, and r indicates the script source.
[0102] Use the conflict resolution function to add a rename attribute to each conflicting object in the conflict information list. The rename attribute content is the original conflicting function or variable name plus a randomly generated long suffix. The function rewriteResolveConflict(R) adds a rename attribute rename to each entry. The rewriteResolveConflict function adds a random long suffix to the original conflicting variable or function name.
[0103] R(C)={(f i , r i , rename i )|(f i , r i )∈C}; (15)
[0104] (4.2) According to the whitelist provided by the user, check whether there are multiple entries of different source scripts in each group in the current conflict information list and the multiple source scripts are included in the same group of the whitelist. If so, delete the entries containing these source scripts in the current group, thereby generating the final conflict information list that needs to be resolved;
[0105] (4.3) Based on the current conflict information list, build the AST of each script, recursively check each node in the AST of the current script, replace all the locations of the functions or variables that reference the conflict with the corresponding renames, traverse all scripts in turn, and complete the renaming replacement of the conflicting locations of all scripts;
[0106] (4.4) Repeat (2)-(3) to continue conflict detection on the current web page and obtain a new conflict information list. After executing (4.1) and (4.2), determine whether the updated conflict information list is empty after removing the whitelist entries. If it is empty, it indicates that all conflicts have been resolved and the loop ends. If it is not empty, execute (4.3) until the conflict information list is empty.
[0107] In the specific implementation, the present invention was implemented based on Chromium version 71.0.3578.98, and the present invention was used to analyze the homepages of the top 100,000 Alexa rankings, and it was found that the identifier conflict problem is very common. The tools include:
[0108] Two recording modules: record the relevant information of all global functions f with non-empty names and the information of write operations of global variables and functions. The second recording module is divided into six cases according to the statement type of the write operation, namely simple assignment statement, containing abbreviated operators, nested assignment statement, writing target as object attribute, constructing expression, and containing this keyword.
[0109] A logging module: dynamically collects logs of all write operations, stores them in the DOMWindow class, and writes them to files in real time during the page loading phase.
[0110] A discrimination module: compares the collected record logs to identify conflicts in write operations of different scripts to the same global memory location.
[0111] A resolution module: Use the function rewriteResolveConflict to add a rename attribute to the conflicting entries, ignore conflicts according to the whitelist, traverse the AST to modify the conflicting function or variable names, and iterate until all conflicts are resolved.
[0112] Figure 2 The tool detects the modification positions and modifications during the parsing process of JavaScript code in the V8 engine. The tool adds monitoring code to the syntax analysis step to capture function definitions, and adds monitoring code to the interpreter to capture variable definitions.
[0113] It should be understood that the present application is not limited to the exact construction that has been described above and shown in the drawings, and that various modifications and changes may be made without departing from the scope thereof.
Claims
1. A JavaScript identifier conflict detection and resolution method based on V8 engine instrumentation, characterized in that: The steps include: (1) Modify the V8 parser and V8 bytecode generator so that the V8 engine records the information of each processing object according to the type of the processing object during code parsing and dynamic stubbing; (2) Run the instrumented V8 engine and dynamically collect the logs of write operations corresponding to all processing objects in the web page; (3) When the webpage is loaded, the conflict of writing operations of different scripts to the same memory location is detected according to the record log, and the conflict result of the JavaScript identifier of the current webpage is obtained, and then the conflict information is recorded to obtain a conflict information list; (4) Refresh the page, and then rewrite the conflicting processing objects according to the conflict information list so that all conflicts in the current conflict information are resolved. Then repeat (2)-(3) to perform conflict detection on the web page. If conflict information still exists, continue to resolve it until there is no conflict in the current web page, and the JavaScript identifier conflict is resolved.
2. A method for detecting and resolving JavaScript identifier conflicts based on V8 engine instrumentation according to claim 1, characterized in that: In (1), when the processing object is a directly defined global function, monitoring code is added to the V8 parser, so that in the JS program pre-parsing step, the definition / writing operations of all global functions are captured and the global function names are checked by dynamically inserted monitoring code, and the names, unique identification IDs, definition locations, execution script IDs, and context IDs of all functions f with non-empty names are recorded as logs.
3. A method for detecting and resolving JavaScript identifier conflicts based on V8 engine instrumentation according to claim 1, characterized in that: In (1), when the processing object is an assignment statement, the V8 bytecode generator is modified, specifically, by dynamically inserting monitoring code at all locations where write operations are performed by inserting function call nodes in the generated abstract syntax tree, and recording the type, value, scope, execution script ID, context ID, and shadow ID of the target variable as a log.
4. A method for detecting and resolving JavaScript identifier conflicts based on V8 engine instrumentation according to claim 3, characterized in that: The monitoring code specifically includes: Create a shadow variable v' for each target variable v of a write operation; Replace the assignment statement; A record-write function recordWrite(v,v',t,e) is used to infer and record the information of the target variable of the write operation; in the record-write function recordWrite(v,v',t,e), v represents the name of the target variable, v' is used to store the unique identifier of the target variable, t represents the value assigned to the target variable or the target object attribute and is recorded as an assignment parameter, which is used to infer the type of the target variable, the assignment parameter t is consistent with the type of the target variable v, and e represents the operation expression performed on the target variable; if the type of the written target variable v is a basic data type, the shadow variable v' stores the variable value, and if the written target variable v is an assignment expression containing an object type, the memory address of the target object v in the V8 engine is obtained as a shadow ID, and the shadow ID is stored as the attribute v'.__id__ of the shadow variable v'.
5. A method for detecting and resolving JavaScript identifier conflicts based on V8 engine instrumentation according to claim 4, characterized in that: For the assignment statement of common variables, and the data type written is a basic data type, the writing operation methods include: If the original assignment statement is a non-nested assignment statement, replace the original assignment statement with a nested assignment statement by changing the abstract syntax tree output by the parser; if the original assignment statement is a nested assignment statement, access its abstract syntax tree, split the original assignment statement into non-nested forms and replace them one by one with new nested assignment statements; For the assignment statement of a common variable, and the data type written is an object type, the writing operation methods include: Create corresponding shadow variables for the object and each attribute, and record the data value and type through each shadow variable; For assignment statements of object properties, and the data type written is a basic data type, the writing operation methods include: Create a shadow variable v' for the target object to be written, use the memory address of the shadow variable v' as the unique identification ID of the target object, and use the type function typeof(v'.prop) to record the written data value and data type; For assignment statements of object properties, and the data type written is an object type, the writing operation methods include: A shadow variable v' and a first shadow variable v1'' are created for the target object and the object property of the target object respectively, and the memory addresses of the shadow variable v' and the first shadow variable v1'' are used as the unique identification ID of the object respectively, and the left and right expressions of the assignment statement are replaced. If the data type written in the assignment statement in the written object is a basic data type, the written data value and data type are recorded through the type function typeof(v'.v1''.prop). If the data type written in the assignment statement in the written object is an object type, a second shadow variable v2' is created for it, and the memory address of the second shadow variable v2' is used as the unique identification ID of the object, and the variables in the assignment statement are replaced, and the corresponding variable values and data types are recorded.
6. A method for detecting and resolving JavaScript identifier conflicts based on V8 engine instrumentation according to claim 1, characterized in that: In (3), the DOMContentLoaded event is monitored by a set event handler, so that the conflict detection function is triggered when the web page is loaded.
7. A method for detecting and resolving JavaScript identifier conflicts based on V8 engine instrumentation according to claim 1, characterized in that: In (3), the JavaScript identifier conflict results include three types of conflicts, namely, function definition conflict, variable value conflict and variable type conflict.
8. A method for detecting and resolving JavaScript identifier conflicts based on V8 engine instrumentation according to claim 7, characterized in that: For function definition conflicts, check whether there are multiple global function definitions with the same name, confirm whether the function scope is global, and whether the global functions with the same name are defined in different scripts at the same time; For variable value conflicts, check whether the same global variable name is assigned different values by different scripts; for object type global variables, further check the definition conflicts of objects and object attributes respectively; For variable type conflicts, check the assignment of the same global variable in different scripts to see whether different types of values are assigned by different scripts. In particular, a global identifier may be used as both a function name and a variable name. It is necessary to cross-check the variable and function definition records.
9. A method for detecting and resolving JavaScript identifier conflicts based on V8 engine instrumentation according to claim 1, characterized in that: The above (4) is specifically: (4.1) Refresh the page and resolve the conflict by renaming the conflicting variables or functions in the conflict information list; (4.2) According to the whitelist provided by the user, check whether there are multiple entries of different source scripts in each group in the current conflict information list and the multiple source scripts are included in the same group of the whitelist. If so, delete the entries containing these source scripts in the current group, thereby generating the final conflict information list that needs to be resolved; (4.3) Based on the current conflict information list, build the AST of each script, recursively check each node in the AST of the current script, replace all the locations of the functions or variables that reference the conflict with the corresponding renames, traverse all scripts in turn, and complete the renaming replacement of the conflicting locations of all scripts; (4.4) Repeat (2)-(3) to continue to perform conflict detection on the current web page and obtain a new conflict information list. After executing (4.1) and (4.2), determine whether the updated conflict information list is empty after removing the whitelist entries. If it is empty, it indicates that all conflicts have been resolved. If it is not empty, execute (4.3) until the conflict information list is empty.
10. A method for detecting and resolving JavaScript identifier conflicts based on V8 engine instrumentation according to claim 9, characterized in that: The (4.1) is specifically: Modify the V8 bytecode generator, add the conflict resolution function rewriteResolveConflict and rewriteResolveConflict function, obtain the conflict information list and use the conflict resolution function to add a rename attribute to each conflicting object in the conflict information list. The content of the rename attribute is the original conflict function or variable name plus a randomly generated long suffix.
Citation Information
Patent Citations
Method and device for detecting JAR packet collision
CN105630463A
Distributed JSON data automatic merging method and system based on CRDT
CN110347749A
Web intelligent test method based on knowledge graph
CN110888808A
Method and system for generating unique identifier and electronic device
CN115481293A
Hybrid granularity code merging conflict automatic solving method based on context codes
CN117827271A