Skip to content

Commit

Permalink
[Docs, Preprocess] Support other JS engines
Browse files Browse the repository at this point in the history
  • Loading branch information
leeswimming committed Sep 23, 2020
1 parent ed84127 commit 054ce89
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 13 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ on a machine running Ubuntu 20.04 with GTX Titan XP GPUs. Python 3.8 and PyTorch
1.4.0 with CUDA are required to run Montage. Please refer to (1) this
[link](https://pytorch.org/get-started/previous-versions/) for installing
PyTorch and (2) this [link](https://developer.nvidia.com/cuda-toolkit-archive)
for installing CUDA Toolkits. We currently support ChakraCore only and have a
plan to support V8, SpiderMonkey, and JavaScriptCore shortly. To get ready for
running Montage, please additionally run the following commands:
for installing CUDA Toolkits. We currently support ChakraCore, V8, SpiderMonkey,
and JavaScriptCore. To get ready for running Montage, please additionally run
the following commands:
```
$ sudo apt update
$ sudo apt install nodejs npm
Expand Down
2 changes: 1 addition & 1 deletion conf/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
sample configuration files.
- `bug_dir`: ABSPATH for saving found bugs.
- `data_dir`: ABSPATH for saving preprocessed data.
- `eng_name`: Target JS engine ("chakra").
- `eng_name`: Target JS engine ("chakra", "v8", "moz", "jsc").
- `eng_path`: ABSPATH to the JS engine.
- `max_ins`: The maximum number of fragments to append.
- `model_path`: The path to the saved model to use for fuzzing.
Expand Down
9 changes: 3 additions & 6 deletions src/preprocess/execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def write_log(self, log_path, stdout, stderr, ret):
log += b'\nMONTAGE_RETURN: %d' % (ret)
write(log_path, log)

def exec_chakra(js_path, conf):
def exec_eng(js_path, conf):
tmp_js_path = rewrite_file(js_path, conf.data_dir)

executor = Executor(conf)
Expand All @@ -70,14 +70,11 @@ def main(pool, conf):
msg = 'Start executing %d JS files' % (num_js)
print_msg(msg, 'INFO')

if conf.eng_name == 'chakra':
exec_func = exec_chakra

pool_map(pool, exec_func, js_list, conf=conf)
pool_map(pool, exec_eng, js_list, conf=conf)

def rewrite_file(js_path, tmp_dir):
dir_path = os.path.dirname(js_path)
PREFIX = b'load = function(js_path){WScript.LoadScriptFile(\'%s/\'.concat(js_path));}'
PREFIX = b'if(typeof load == \'undefined\') load = function(js_path){WScript.LoadScriptFile(\'%s/\'.concat(js_path));}'
PREFIX = PREFIX % dir_path.encode('utf-8')

code = read(js_path)
Expand Down
51 changes: 50 additions & 1 deletion src/preprocess/triage.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
ERROR = '============== STDERR ==============='

def chakra(log_path):
log = read(log_path, 'r')
log = read(log_path, 'r', encoding='ISO-8859-1')
error = get_err_msg(log)

if ('SyntaxError:' in error or
Expand All @@ -21,6 +21,49 @@ def chakra(log_path):
else:
return get_ret(log)

def jsc(log_path):
log = read(log_path, 'r', encoding='ISO-8859-1')
error = get_err_msg(log, read_err=False)

if ('Exception: SyntaxError:' in error or
'Exception: ReferenceError:' in error or
'Exception: TypeError:' in error or
'Exception: RangeError:' in error or
'Exception: URIError:' in error or
'Could not open file:' in error):
return 1
else:
return get_ret(log)

def moz(log_path):
log = read(log_path, 'r', encoding='ISO-8859-1')
error = get_err_msg(log)

if ('SyntaxError: ' in error or
'ReferenceError: ' in error or
'TypeError: ' in error or
'RangeError: ' in error or
'URIError: ' in error or
'Error: can\'t open ' in error):
return 1
else:
return get_ret(log)

def v8(log_path):
log = read(log_path, 'r', encoding='ISO-8859-1')
error = get_err_msg(log, read_err=False)

if ('\nSyntaxError:' in error or
'\nReferenceError:' in error or
'\nTypeError:' in error or
'\nRangeError:' in error or
'\nURIError:' in error or
'Error loading file' in error or
'Error executing file' in error):
return 1
else:
return get_ret(log)

def get_err_msg(log, read_err=True):
idx = 1 if read_err else 0
error = log.split(ERROR)[idx]
Expand All @@ -30,6 +73,12 @@ def get_err_msg(log, read_err=True):
def get_func(eng_name):
if eng_name == 'chakra':
return chakra
elif eng_name == 'v8':
return v8
elif eng_name == 'moz':
return moz
elif eng_name == 'jsc':
return jsc

def get_ret(log):
ret = log.split('MONTAGE_RETURN: ')
Expand Down
4 changes: 2 additions & 2 deletions src/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ def random_string(length):
rand_str = [choice(candidate) for i in range(length)]
return ''.join(rand_str)

def read(file_name, mode='rb'):
with open(file_name, mode) as f:
def read(file_name, mode='rb', encoding=None):
with open(file_name, mode, encoding=encoding) as f:
return f.read()

def store_pickle(dpath, data):
Expand Down

0 comments on commit 054ce89

Please sign in to comment.