Semantics Rewriter API

Semantics Rewriter class

SectionRewriter

class SectionRewriter(module)

Attributes:

module

The Wasm module to be rewrited.

Global Variable

appendGlobalVariable (global_type, init_value):

Append a global variable.

Parameters:

global_type – The type of the global variable. Value type:

  • I32

  • I64

  • F32

  • F64

Parameters:

init_value – The initial value of the global variable (int or float).

modifyGlobalVariable (idx, global_type, init_value):

Modify a global variable.

Parameters:
  • idx – The index of the global variable to be modified.

  • global_type – The type of the new global variable. Refer to appendGlobalVariable

  • init_value – The initial value of the new global variable (int or float).

deleteGlobalVariable (idx=None, global_type=None, init_value=None):

Delete a global variable.

Parameters:
  • idx – The index of the global variable to be deleted.

  • global_type – The type of the global variable to be deleted. Refer to appendGlobalVariable

  • init_value – The initial value of the global variable to be deleted. (int or float).

Note

There are two methods to delete the global variable. The one is to specify the index of the global variable to be deleted. The other one is to specify the tangible attributes.

insertGlobalVariable (idx, global_type, init_value):

Insert a global variable.

Parameters:
  • idx – The index of the global variable to be inserted.

  • global_type – The type of the global variable to be inserted. Refer to appendGlobalVariable

  • init_value – The initial value of the global variable to be inserted. (int or float).

Import & Export

insertImportFunction  (idx, module_name, func_name, params_type, results_type):

Insert a import function.

Parameters:
  • idx – The index of the global variable to be inserted.

  • module_name – The module name of the import function to be inserted (str).

  • func_name – The function name of the import function to be inserted (str).

  • params_type – The parameters type of the function. For example, params_type=[I32, I64].

  • results_type – The return value type of the function. For example, results_type=[F32, I64].

appendImportFunction  (module_name, func_name, params_type, results_type):

Append a import function.

Parameters:
  • module_name – Similar to insertImportFunction

  • func_name – Similar to insertImportFunction

  • params_type – Similar to insertImportFunction

  • results_type – Similar to insertImportFunction

modifyImportFunction  (idx, module_name, func_name, params_type, results_type):

Modify a import function.

Parameters:
  • idx – Similar to insertImportFunction

  • module_name – Similar to insertImportFunction

  • func_name – Similar to insertImportFunction

  • params_type – Similar to insertImportFunction

  • results_type – Similar to insertImportFunction

deleteImportFunction (idx=None, module_name=None, func_name=None):

Delete a import function.

Parameters:
  • idx – Similar to insertImportFunction

  • module_name – Similar to insertImportFunction

  • func_name – Similar to insertImportFunction

Note

It can be deleted by index or names.

insertExportFunction   (idx, func_name, funcidx):

Insert a export function.

Parameters:
  • idx – The index of the export function to be inserted.

  • func_name – The export function name.

  • funcidx – The index of the internal function to be exported.

appendExportFunction   (func_name, funcidx):

Append a export function.

Parameters:
  • func_name – Similar to insertExportFunction

  • funcidx – Similar to insertExportFunction

modifyExportFunction   (idx, func_name, funcidx):

Modify a export function

Parameters:
  • idx – Similar to insertExportFunction

  • func_name – Similar to insertExportFunction

  • funcidx – Similar to insertExportFunction

deleteExportFunction  (idx=None, func_name=None):

Delete a export function. Similar to deleteGlobalVariable.

Parameters:
  • idx – Similar to insertExportFunction

  • func_name – Similar to insertExportFunction

Linear Memory

appendLinearMemory  (offset, bytes):

Appends an initialized data at the specified offset in linear memory.

Parameters:
  • offset – The offset of the data to be appended (int).

  • bytes – Initial data (bytes).

modifyLinearMemory  (offset, bytes):

Modify the initial data of the linear memory at specified offset.

Parameters:
  • offset – Similar to appendLinearMemory.

  • bytes – Similar to appendLinearMemory.

Function

insertInternalFunction   (idx, params_type, results_type, local_vec, func_body):

Insert a internal function.

Parameters:
  • idx – The index of the function to be inserted.

  • params_type – The function parameters type. Similar to insertImportFunction

  • results_type – The function return values type. Similar to insertImportFunction

  • local_vec – The local variable of the function (Local). Note that all local variables should be indexed from 0.

  • func_body – The function body.

example:

.. code-block:: python

   # Initialize a semantics rewriter of the function semantics
   function_rewriter = SemanticRewriter.Function(binary)
   # Define the instructions of function
   funcbody = [Instruction(LocalGet, 0), Instruction(LocalGet, 1), Instruction(I32Add, 0), Instruction(Nop)]
   # Insert a internal function in the binary
   function_rewriter.insert_internal_function(idx=1, params_type=[I32, I32], results_type=[I32], local_vec=[Local(0, I32), Local(1, I64)], funcbody)
insertIndirectFunction   (idx, params_type, results_type, local_vec, func_body):

Insert a indirect function.

Parameters:
  • idx – The index of the function to be inserted.

  • params_type – The function parameters type. Similar to insertImportFunction

  • results_type – The function return values type. Similar to insertImportFunction

  • local_vec – The local variable of the function (Local). Note that all local variables should be indexed from 0.

  • func_body – The function body.

insertHookFunction   (hooked_funcidx, idx, params_type, results_type, locals_vec, func_body):

Insert a indirect function.

Parameters:
  • hooked_funcidx – The index of the hooked function.

  • idx – The index of the hook function to be inserted.

  • params_type – The function parameters type. Similar to insertImportFunction

  • results_type – The function return values type. Similar to insertImportFunction

  • local_vec – The local variable of the function (Local). Note that all local variables should be indexed from 0.

  • func_body – The function body.

deleteFuncInstr   (funcidx, offset):

Delete a instruction of a function by offset.

Parameters:
  • funcidx – The function index of the instruction to be deleted.

  • offset – The flattening offset of an instruction in a function.

Note

There is no need to consider the nest relation of instructions. Just flatten all the instructions. For example, [Instruction(Block), Instruction(I32Const, 1), Instruction(Drop), Instruction(End)]. The offset of the Drop is 2.

appendFuncInstrs    (funcidx, instrs: list):

Append a list of instructions to an internal function.

Parameters:
  • funcidx – The function index.

  • instrs – The instruction list. For example, [Instruction(Block), Instruction(I32Const, 1), Instruction(Drop), Instruction(End)].

insertFuncInstrs    (funcidx, offset, instrs: list):

Insert a list of instructions to an internal function.

Parameters:
  • funcidx – The function index.

  • offset – The offset of the instrunctions to be inserted.

  • instrs – The instruction list. For example, [Instruction(Block), Instruction(I32Const, 1), Instruction(Drop), Instruction(End)].

modifyFuncInstr   (funcidx, instr, instrs: list):

Modify all the specified instructions of an internal function with instructions. This function enables batch replacement of instructions in functions.

Parameters:
  • funcidx – The function index.

  • instr – The specified instruction to be replaced.

  • instrs – The instruction list. For example, [Instruction(Block), Instruction(I32Const, 1), Instruction(Drop), Instruction(End)].

appendFuncLocal  (funcidx, valtype):

Append a local variable for a function.

Parameters:
  • funcidx – The function index.

  • valtype – The value type of the local variable to be appended.

Custom Content

modifyFuncName    (funcidx, name):

Modify the function debug name.

Parameters:
  • funcidx – The function index.

  • name – The debug name.

deleteFuncName    (funcidx):

Delete the function debug name.

Parameters:

funcidx – The function index.

insertFuncName    (funcidx, name):

Insert a function debug name.

Parameters:
  • funcidx – The function index.

  • name – The debug name.

modifyGlobalName    (globalidx, name):

Modify the debug name of a global variable.

param globalidx:

The index of the global variable.

param name:

The debug name.

deleteGlobalName     (globalidx):

Delete the debug name of a global variable.

Parameters:

globalidx – The index of the global variable.

insertGlobalName     (globalidx, name):

Insert a debug name for a global variable.

Parameters:
  • globalidx – The index of the global variable.

  • name – The debug name.

insertDataName    (dataidx, name):

Insert a debug name for a linear memory.

Parameters:
  • dataidx – The index of the data.

  • name – The debug name.

modifyDataName   (dataidx, name):

Modify the debug name of a linear memory.

Parameters:
  • dataidx – The index of the data.

  • name – The new debug name.

deleteDataName    (dataidx):

Delete the debug name of a linear memory.

Parameters:

dataidx – The index of the data.