Skip to content

Commit

Permalink
Limit the use of memory page
Browse files Browse the repository at this point in the history
  • Loading branch information
mohanson committed Dec 23, 2020
1 parent 81b45c0 commit c07db6c
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 1 deletion.
2 changes: 2 additions & 0 deletions pywasm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ def __init__(self, module: binary.Module, imps: typing.Dict = None, opts: typing
if isinstance(e.desc, binary.MemoryType):
addr = execution.MemoryAddress(len(self.store.memory_list))
memory = imps[e.module][e.name]
if self.machine.opts.pages_limit > 0 and e.desc.limits.n > self.machine.opts.pages_limit:
raise Exception('pywasm: out of memory limit')
memory.grow(e.desc.limits.n)
self.store.memory_list.append(memory)
extern_value_list.append(addr)
Expand Down
2 changes: 2 additions & 0 deletions pywasm/execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -1032,6 +1032,8 @@ def grow_memory(config: Configuration, i: binary.Instruction):
memory = config.store.memory_list[memory_addr]
size = memory.size
r = config.stack.pop().i32()
if config.opts.pages_limit > 0 and memory.size + r > config.opts.pages_limit:
raise Exception('pywasm: out of memory limit')
try:
memory.grow(r)
config.stack.append(Value.from_i32(size))
Expand Down
2 changes: 2 additions & 0 deletions pywasm/option.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ def __init__(self):
self.cycle_limit = 0
# Count the currently used cycles
self.cycle = 0
# Memory page limit. 1 page = 65536 bytes
self.pages_limit = 0
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

setuptools.setup(
name='pywasm',
version='1.0.2',
version='1.0.4',
url='https://github.com/mohanson/pywasm',
license='WTFPL',
author='mohanson',
Expand Down

0 comments on commit c07db6c

Please sign in to comment.