JavaScript identifier conflict detection and resolution method based on V8 engine instrumentation
By modifying the V8 engine's parser and bytecode generator, dynamically inserting JavaScript code, and recording the write operations of global functions and variables, the shortcomings of JavaScript library global identifier conflict detection in the existing technology are solved, and high-accuracy conflict detection and resolution are achieved.
Patent Information
- Application Number
- CN202411105761.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-08-13
- Publication Date
- 2025-10-14
- Estimated Expiration
- 2044-08-13
AI Technical Summary
When detecting global identifier conflicts between JavaScript libraries, existing technologies focus on a limited number of libraries, static analysis methods cannot accurately reflect conflicts caused by dynamic loading, and dynamic analysis methods fail to fully cover JavaScript global identifier conflict detection.
By modifying the V8 parser and bytecode generator, dynamically instrumenting JavaScript code, recording write operations of global functions and variables, and utilizing shadow variables and immutable identification properties, global identifier conflicts, including function definitions, variable values, and type conflicts, can be detected and resolved.
It implements comprehensive dynamic analysis of JavaScript code, ensuring the accuracy and coverage of conflict detection, and is able to discover all conflicts in real applications and effectively resolve them.
Smart Images

Figure CN119311561B_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to a JavaScript identifier conflict detection method in the field of computer software engineering, and in particular to a JavaScript identifier conflict detection and resolution method based on V8 engine instrumentation. BACKGROUND
[0002] JavaScript code is widely used in client-side web applications. In a web browser, all scripts loaded by the same page share the same namespace. Therefore, one script can read or even overwrite the global variables or global functions of other scripts, which is called global identifier conflict. Global identifier conflict is a significant threat to the code integrity of developers. Severe identifier conflicts can cause the execution logic of the program to change, return an error value, or even cause the program to crash. Global identifier conflict is difficult to prevent. In order to avoid identifier conflict, developers need to carefully check the source code before referencing third-party libraries. Due to code obfuscation and minimization, this work is very difficult. Unless there is no conflict in the referenced script, developers need to modify all conflict locations. Even so, global identifier conflict cannot be completely avoided. This is because third-party code is hosted on 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 import other scripts. This can be prevented by configuring CSP, but the use 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 simulation to test whether JavaScript libraries behave differently when loaded in different settings, but this method only studies a limited number of JavaScript libraries in a simulated environment and can only test simple operations of the library. Real applications may include more than two libraries. Existing methods are based on a selective recording-replaying dynamic analysis framework that only inserts monitoring code into selected source code and cannot cover other dynamically loaded code. Therefore, the results cannot accurately reflect the conflicts that may exist in real applications.
[0004] At least the following problems exist in the prior art:
[0005] The number of JavaScript libraries researched is too small, and there can be more than two JavaScript libraries in actual application; the type inference is performed by using a static method, and the focus is on inferring the JavaScript type of a single script, and the static analysis method cannot infer the behavior of the JavaScript code at runtime; the dynamic analysis of the JavaScript code is limited to detecting abnormal functions, data race detection, determinacy analysis, performance analysis, concurrent error detection and crash path calculation, and the dynamic analysis is not fully used for detecting the JavaScript global identifier conflict. SUMMARY
[0006] The present application aims at the deficiencies of the prior art, and provides a JavaScript identifier conflict detection and resolution method based on V8 engine instrumentation.
[0007] The present application is achieved by the following technical solutions.
[0008] (1) The V8 parser and V8 bytecode generator are modified, 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 instrumentation;
[0009] (2) The instrumented V8 engine is run, and the record logs of the write operations of all processing objects in the webpage are dynamically collected;
[0010] (3) When the webpage is loaded, the conflict of the write operations of different scripts to the same memory location is detected according to the record logs, the JavaScript identifier conflict result of the current webpage is obtained, and then the conflict information list is obtained after the conflict information is recorded;
[0011] (4) The webpage is refreshed, and the processing objects in conflict are rewritten according to the conflict information list, so that the conflicts in the current conflict information are resolved, and then steps (2)-(3) are repeated to detect the conflicts of the webpage, and if there are still conflicts, the conflicts are continued to be resolved until there is no conflict in the current webpage, and the resolution of the JavaScript identifier conflict is completed.
[0012] In the (1), when the processing object is a directly defined global function, monitoring code is added in the V8 parser, so that in the JS program pre-parsing step, the definition / write operation of all global functions is captured by the way of dynamically inserted monitoring code, and the global function name is checked, and the name, unique identification ID, definition position, execution script ID and context ID of all non-empty function f 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 the write operation;
[0016] Replace the assignment statement;
[0017] A record-write function recordWrite(v,v',t,e) is used to infer and record information about 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 for inferring 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 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'.
[0018] For assignment statements 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 the new nested assignment statements;
[0020] For ordinary variable assignment statements, where the data type written is an object type, the write 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, where the data type written is a basic data type, the write operation methods include:
[0023] A shadow variable v' is created for the target object of the write, the memory address of the shadow variable v' is used as the unique identifier ID of the target object, and the data value and data type of the write are recorded through the type function typeof(v'.prop);
[0024] For the assignment statement of the object property, and the data type of the write is the object type, the write operation mode includes:
[0025] A shadow variable v' and a first shadow variable v1" are respectively created for the target object of the write and the object property of the target object, and the memory addresses of the shadow variable v' and the first shadow variable v1" are used as the unique identifier ID of the object, respectively. The left and right expressions of the assignment statement are replaced, if the data type of the write in the assignment statement is a basic data type, the data value and data type of the write are recorded through the type function typeof(v'.v1".prop), if the data type of the write in the assignment statement is an object type, a second shadow variable v2' is created, the memory address of the second shadow variable v2' is used as the unique identifier ID of the object, and the variable in the assignment statement is replaced, and the corresponding variable value and data type are recorded.
[0026] In the (3), the event handler set by the setting listens to the DOMContentLoaded event, so that the conflict detection function is triggered when the webpage is loaded.
[0027] In the (3), the JavaScript identifier conflict result includes three conflict types, namely wield function definition conflict, variable value conflict and variable type conflict.
[0028] For function definition conflict, check whether there are multiple global function definitions with the same name, confirm whether the scope of the function is global, and whether the same global function is defined in different scripts at the same time;
[0029] For variable value conflict, check whether the same global variable name is assigned different values by different scripts; for object type global variables, further check the definition conflict of the object and the object property, respectively;
[0030] For variable type conflict, check whether the same global variable is assigned different types of values in different scripts, especially, a global identifier may be used as a function name and a variable name at the same time, and the variable and function definition records need to be cross-checked.
[0031] The (4) is specifically:
[0032] (4.1) refreshing the page, solving the conflict by renaming the conflicting variable or function in the conflict information list;
[0033] (4.2) according to the white list provided by the user, checking whether there are multiple entries of different source scripts in each group in the current conflict information list and the multiple source scripts are contained in the same group in the white list, if yes, deleting the entries containing the source scripts in the current group, thereby generating the final conflict information list to be solved;
[0034] (4.3) according to the current conflict information list, constructing the AST of each script, recursively checking each node in the AST of the current script, replacing all references to the conflicting function or variable with the corresponding renaming, and sequentially traversing all scripts to complete the renaming and replacement of the conflict position of all scripts;
[0035] (4.4) repeating (2)-(3), continuing to detect the conflict of the current webpage, obtaining a new conflict information list, and judging whether the updated conflict information list is empty after removing the white list entries, if yes, it means that all conflicts have been solved, if not, executing (4.3) until the conflict information list is empty.
[0036] The (4.1) is specifically:
[0037] The V8 bytecode generator is modified, the conflict solving function rewriteResolveConflict and rewriteResolveConflict function are added, the conflict information list is obtained, and a renaming attribute is added to each object in the conflict information list by using the conflict solving function, and the content of the renaming attribute is the original conflict function or variable name plus a randomly generated long suffix.
[0038] The present application has the beneficial effects that:
[0039] The present application can cover all running codes by dynamically inserting monitoring codes to the executed JavaScript codes, and the alias analysis is more simple and accurate by using shadow variables and immutable identifier attributes to uniquely identify JavaScript objects. The present application first uses dynamic analysis to detect the conflict of all JavaScript script global identifiers, ensures the accuracy of the conflict detection, and has the advantages of better effect and high precision than the prior art. BRIEF DESCRIPTION OF DRAWINGS
[0040] Figure 1The present invention proposes a method flow chart for detecting and resolving JavaScript identifier conflicts based on V8 engine instrumentation.
[0041] Figure 2 It is a diagram showing the modification locations and status of the tool during the JavaScript code parsing process in the V8 engine. DETAILED DESCRIPTION
[0042] In order to enable those skilled in the art to better understand the present invention, the present invention is further described in detail below in conjunction with the accompanying drawings and specific embodiments. Obviously, the embodiments described are only some embodiments of the present invention, not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those of ordinary skill in the art without making creative efforts are within the scope of protection of the present invention.
[0043] The present invention dynamically inserts monitoring code into the executed JavaScript code, and thereby monitors the writing of global variables and functions, so as to identify conflicts in the writing operations 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 instrumentation;
[0046] Depending on how developers define global functions, different methods are used to handle them, including the following two methods. When handling directly defined global functions, such as function f(args){stmts;}, monitoring code is added to the V8 parser. During the pre-parse step of the JS program, this dynamically inserted monitoring code captures all global function definition / write operations and checks global function names. The name, unique identifier ID, definition location (line and column numbers), execution script ID, and context ID of all function fs with non-empty names are logged.
[0047] Assignment statements include 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 by inserting a function call node in the generated abstract syntax tree (AST tree) to dynamically insert monitoring code at all locations where a write operation is performed, instead of modifying the bytecode, and recording the type, value, scope, execution script ID, context ID, and shadow ID of the target variable as a log.
[0049] The monitoring code specifically includes:
[0050] A shadow variable v' is created for each target variable v of the write operation;
[0051] The assignment statement is replaced;
[0052] The record-write function recordWrite(v, v', t, e) is used to infer and record the type, value, scope, execution script ID, context ID, and shadow ID 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 target object property and is recorded as an assignment parameter, which is used to infer the type of the target variable, and the assignment parameter t is consistent with the type of the target variable v, so the type of the target variable v is inferred using typeof(t). e represents the operation expression performed 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, and 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 property v'.__id__ of the shadow variable v'.
[0053] For an assignment statement of a normal variable, and the data type written is a basic data type, the write operation includes:
[0054] If the original assignment statement is a non-nested assignment statement, a shadow variable v' is created for the target written, the abstract syntax tree output by the parser is changed, the original assignment statement is replaced with a nested assignment statement, so that v and v' are defined 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 the statement like arr[i++] being called twice, v' is the shadow variable of v in formula (2), the value of v' is exactly the same as v, as the only identifier of the target variable, the type of v' is exactly the same as v, so v' can be used to judge the type of v.
[0058] If the v+=e type assignment statement is used with the shorthand assignment operator like +=, a shadow variable v' is created for it, and the inserted monitoring code is:
[0059] v' = v += e (3)
[0060] recordWrite(v, v', v', e) (4)
[0061] v' is the shadow variable of v in formula (4), the value of v' is exactly the same as v, as the only identifier of the target variable, the type of v' is exactly the same as v, so v' can be used to judge the type of v.
[0062] If the original assignment statement is a nested assignment statement, the abstract syntax tree thereof is accessed, the original assignment statement is split into a non-nested form and is replaced with a new nested assignment statement one by one according to the above replacement method, for example, the v1 = v2 = e type assignment statement, the shadow variables v1' and v2' are created for the variables v1 and v2 respectively, and the inserted monitoring code is:
[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 variables of basic data types, and the recordWrite function is called in turn to record the write operation of the variable, since v1 and v2 are basic data types, the recording identifier method and the data type judgment method are consistent with the foregoing method.
[0067] When the target of writing is the attribute of an object, the object needs to be identified. That is, for the assignment statement of a normal variable, and the data type of the writing is an object type, the writing operation method thereof includes:
[0068] A corresponding shadow variable is created for the object and each attribute, and the data value and type are recorded through each shadow variable. The shadow variable is used to obtain the shadow ID, and the object is identified by the unique shadow ID to avoid re-evaluating the expression. For example, in the expression vp = e, 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. The monitoring code will then write to the shadow variable instead of the original variable v. 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. The object is uniquely identified based on the shadow ID. The shadow object serves as 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, where the data type written is a basic data type, the write 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, where the data type written is an object type, the write operation methods include:
[0075] Create a shadow variable v' and a first shadow variable v1' for the target object and the object property of the target object respectively, and use the memory addresses of the shadow variable v' and the first shadow variable v1' as the unique identification ID of the object respectively, replace the left and right expressions of the assignment statement, if the data type written in the assignment statement in the written object is a basic data type, then use the type function typeof(v'.v1'.prop) to record the written data value and data type, if the data type written in the assignment statement in the written object is an object type, then create a second shadow variable v2' for it, use the memory address of the second shadow variable v2' as the unique identification ID of the object, replace the variables in the assignment statement, and record the corresponding variable values and data types.
[0076] For object literals and constructor expressions, if a 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 inserted monitoring code 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 using 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, as well as the shadow ID of this 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 different scripts writing to the same memory location is detected based on the recorded log, and the JavaScript identifier conflict result of the current webpage is obtained. Then, the conflict information is recorded and a conflict information list is obtained. Among them, the event handler is set to listen to the DOMContentLoaded event, so that the conflict detection function after this step is triggered when the webpage is loaded.
[0089] The JavaScript identifier conflict result includes three conflict types, namely, function definition conflict, variable value conflict and variable type conflict;
[0090] For function definition conflict, it is checked whether there are multiple global function definitions with the same name, whether the scope of the function is global, and whether the global function with the same name is defined in different scripts at the same time. In particular, a global function can be defined in two ways, function declaration and assignment statement, and the variable and function definition records need to be cross-checked.
[0091] For variable value conflict, it is checked whether the same global variable name is assigned different values by different scripts. For object type global variables, further checking of object and object property definition conflicts is performed respectively.
[0092] For variable type conflict, it is checked whether the same global variable is assigned different types of values by different scripts. In particular, a global identifier can be used as a function name and a variable name at the same time, and the variable and function definition records need to be cross-checked.
[0093] After detecting the conflict, the conflict information is stored in the form of an object literal list in a file. Two attributes of the object representing the conflict information are the conflict function or variable name and the source script. In the list, objects with the same conflict 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 scripts in the same group are different, thereby obtaining a conflict information list.
[0094] A user-configurable whitelist list is provided, and the user can freely add entries in the whitelist list and group these entries. Conflicts between scripts in the same group will be ignored.
[0095] (4) Refresh the page, and then rewrite the processing object according to the conflict information list to solve the conflict in the current conflict information, and then repeat (2)-(3) to detect the conflict of the web page. If there is still conflict information (i.e., there is new conflict), continue to solve it until there is no conflict in the current web page, and then complete the solution of the JavaScript identifier conflict.
[0096] (4) Specifically:
[0097] (4.1) Refresh the page and solve the conflict by renaming the conflicting variable or function in the conflict information list.
[0098] (4.1) Specifically:
[0099] The V8 bytecode generator is modified to add the conflict resolution function rewriteResolveConflict and rewriteResolveConflict function, obtain a conflict information list C from a file, C includes the function or variable name of the conflict, script source and other information, if C is empty, end the function;
[0100] C={(f i ,r i )|i=1,2,...,n}; (14)
[0101] Wherein, f i represents the function or variable name of the i-th conflict, and r represents the script source.
[0102] The conflict resolution function is used to add a rename attribute to each object in the conflict information list, and the content of the rename attribute is the original conflict function or variable name plus a randomly generated long suffix. The function rewriteResolveConflict(R) adds a rename attribute to each entry, and the rewriteResolveConflict function adds a random long suffix to the original conflict 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 white list 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 white list, if there are, delete the entries containing these source scripts in the current group, thereby generating a final conflict information list that needs to be resolved;
[0105] (4.3) According to the current conflict information list, construct the AST of each script, recursively check each node in the AST of the current script, replace all references to the conflicting function or variable with the corresponding rename, and sequentially traverse all scripts to complete the rename and replacement of all conflict positions in the scripts;
[0106] (4.4) Repeat (2)-(3) to continue conflict detection on the current web page, obtain a new conflict information list, and judge whether the updated conflict information list is empty after removing the white list entries, if it is empty, it means that all conflicts have been resolved, end the loop, if it is not empty, execute (4.3), until the conflict information list is empty.
[0107] In the specific implementation, the invention was implemented based on Chromium version 71.0.3578.98, and the invention was used to analyze the homepages of the top 100,000 Alexa rankings. 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 writing operations on global variables and functions. The second recording module is divided into six cases according to the statement type of the writing 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 and identifies 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 monitors the location and status of changes made to JavaScript code during parsing in the V8 engine. It adds monitoring code to the syntax analysis step to capture function definitions, and also adds monitoring code to the interpreter to capture variable definitions.
[0113] It will 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 instrumentation; (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 web page is loaded, the conflict of different scripts writing to the same memory location is detected according to the recorded log, and the JavaScript identifier conflict result of the current web page is obtained. Then, the conflict information is recorded and a conflict information list is obtained; (4) Refresh the page and 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 detect conflicts on the web page. If there is still conflict information, continue to resolve it until there is no conflict in the current web page. The JavaScript identifier conflict is resolved. Specifically, (4) is: (4.1) Refresh the page and resolve the conflict by renaming the conflicting variables or functions in the conflict information list; (4.2) Based on the whitelist provided by the user, check whether there are multiple entries of scripts from different sources in each group in the current conflict information list, and if these multiple source scripts are included in the same group in the whitelist, 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 locations of conflicting functions or variables with corresponding renames, traverse all scripts in turn, and complete the renaming replacement of conflicting locations in all scripts; (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. If it is not empty, execute (4.3) until the conflict information list is empty.
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. The 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, the monitoring code is dynamically inserted at all locations where write operations are performed by inserting function call nodes in the generated abstract syntax tree, and the type, value, scope, execution script ID, context ID, and shadow ID of the target variable are recorded as logs.
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 the write operation; Replace the assignment statement; A record-write function recordWrite(v, v', t, e) is used to infer and record information about 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 for inferring 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; 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 the 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 assignment statements 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 the new nested assignment statements; For ordinary variable assignment statements, where the data type written is an object type, the write 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, where the data type written is a basic data type, the write 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, where the data type written is an object type, the write operation methods include: Create a shadow variable v' and a first shadow variable v1'' for the target object and the object property of the target object respectively, and use the memory addresses of the shadow variable v' and the first shadow variable v1'' as the unique identification ID of the object respectively, replace the left and right expressions of the assignment statement, if the data type written in the assignment statement in the written object is a basic data type, then use the type function typeof(v'.v1''.prop) to record the written data value and data type, if the data type written in the assignment statement in the written object is an object type, then create a second shadow variable v2' for it, use the memory address of the second shadow variable v2' as the unique identification ID of the object, replace the variables in the assignment statement, and record the corresponding variable values and data types.
6. The 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 the set event handler, so that the conflict detection function is triggered when the web page is loaded.
7. The 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. The 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 function with the same name is 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 for definition conflicts between objects and object attributes; For variable type conflicts, check the assignment of the same global variable in different scripts to see if different types of values are assigned to it. In particular, a global identifier may be used as both a function name and a variable name. Therefore, you need to cross-check the variable and function definition records.
9. The method for detecting and resolving JavaScript identifier conflicts based on V8 engine instrumentation according to claim 1, characterized in that: Specifically, (4.1) is: Modify the V8 bytecode generator, add the conflict resolution functions rewriteResolveConflict and rewriteResolveConflict, 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 renamed attribute content is the original conflicting function or variable name plus a randomly generated long suffix.