Skip to content

Commit

Permalink
Merge pull request #780 from wasmx/capi-memory-hard-limit
Browse files Browse the repository at this point in the history
capi: Add memory_pages_limit parameter to fizzy_instantiate
  • Loading branch information
gumb0 authored Apr 19, 2021
2 parents c17fb2a + 1339c5c commit 5027151
Show file tree
Hide file tree
Showing 6 changed files with 207 additions and 79 deletions.
1 change: 1 addition & 0 deletions bindings/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ impl Module {
std::ptr::null(),
std::ptr::null(),
0,
sys::FizzyMemoryPagesLimitDefault,
err.as_mut_ptr(),
)
};
Expand Down
13 changes: 11 additions & 2 deletions include/fizzy/fizzy.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ typedef enum FizzyErrorCode
FizzyErrorOther
} FizzyErrorCode;

enum
{
/// Default hard limit of the memory size (256MB) to call fizzy_instantiate() and
/// fizzy_resolve_instantiate().
FizzyMemoryPagesLimitDefault = 4096
};

/// Error information.
typedef struct FizzyError
{
Expand Down Expand Up @@ -410,6 +417,7 @@ bool fizzy_module_has_start_function(const FizzyModule* module) FIZZY_NOEXCEPT;
/// @param imported_globals Pointer to the imported globals array. Can be NULL iff
/// @p imported_globals_size equals 0.
/// @param imported_globals_size Size of the imported global array. Can be zero.
/// @param memory_pages_limit Hard limit for memory growth in pages. Cannot be above 65536.
/// @param error Pointer to store detailed error information at. Can be NULL
/// if error information is not required.
/// @return non-NULL pointer to instance in case of success,
Expand All @@ -430,7 +438,7 @@ FizzyInstance* fizzy_instantiate(const FizzyModule* module,
const FizzyExternalFunction* imported_functions, size_t imported_functions_size,
const FizzyExternalTable* imported_table, const FizzyExternalMemory* imported_memory,
const FizzyExternalGlobal* imported_globals, size_t imported_globals_size,
FizzyError* error) FIZZY_NOEXCEPT;
uint32_t memory_pages_limit, FizzyError* error) FIZZY_NOEXCEPT;

/// Instantiate a module resolving imported functions.
///
Expand All @@ -454,6 +462,7 @@ FizzyInstance* fizzy_instantiate(const FizzyModule* module,
/// @param imported_globals Pointer to the imported globals array. Can be NULL iff
/// @p imported_globals_size equals 0.
/// @param imported_globals_size Size of the imported global array. Can be zero.
/// @param memory_pages_limit Hard limit for memory growth in pages. Cannot be above 65536.
/// @param error Pointer to store detailed error information at. Can be NULL
/// if error information is not required.
/// @return non-NULL pointer to instance in case of success,
Expand All @@ -478,7 +487,7 @@ FizzyInstance* fizzy_resolve_instantiate(const FizzyModule* module,
const FizzyImportedFunction* imported_functions, size_t imported_functions_size,
const FizzyExternalTable* imported_table, const FizzyExternalMemory* imported_memory,
const FizzyImportedGlobal* imported_globals, size_t imported_globals_size,
FizzyError* error) FIZZY_NOEXCEPT;
uint32_t memory_pages_limit, FizzyError* error) FIZZY_NOEXCEPT;

/// Free resources associated with the instance.
///
Expand Down
9 changes: 5 additions & 4 deletions lib/fizzy/capi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ FizzyInstance* fizzy_instantiate(const FizzyModule* module,
const FizzyExternalFunction* imported_functions, size_t imported_functions_size,
const FizzyExternalTable* imported_table, const FizzyExternalMemory* imported_memory,
const FizzyExternalGlobal* imported_globals, size_t imported_globals_size,
FizzyError* error) noexcept
uint32_t memory_pages_limit, FizzyError* error) noexcept
{
try
{
Expand All @@ -622,7 +622,8 @@ FizzyInstance* fizzy_instantiate(const FizzyModule* module,
auto globals = unwrap(imported_globals, imported_globals_size);

auto instance = fizzy::instantiate(std::unique_ptr<const fizzy::Module>(unwrap(module)),
std::move(functions), std::move(table), std::move(memory), std::move(globals));
std::move(functions), std::move(table), std::move(memory), std::move(globals),
memory_pages_limit);

set_success(error);
return wrap(instance.release());
Expand All @@ -638,7 +639,7 @@ FizzyInstance* fizzy_resolve_instantiate(const FizzyModule* c_module,
const FizzyImportedFunction* c_imported_functions, size_t imported_functions_size,
const FizzyExternalTable* imported_table, const FizzyExternalMemory* imported_memory,
const FizzyImportedGlobal* c_imported_globals, size_t imported_globals_size,
FizzyError* error) noexcept
uint32_t memory_pages_limit, FizzyError* error) noexcept
{
try
{
Expand All @@ -652,7 +653,7 @@ FizzyInstance* fizzy_resolve_instantiate(const FizzyModule* c_module,
auto resolved_globals = fizzy::resolve_imported_globals(*module, imported_globals);

auto instance = fizzy::instantiate(std::move(module), std::move(resolved_imports),
std::move(table), std::move(memory), std::move(resolved_globals));
std::move(table), std::move(memory), std::move(resolved_globals), memory_pages_limit);

set_success(error);
return wrap(instance.release());
Expand Down
1 change: 1 addition & 0 deletions lib/fizzy/limits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ static_assert(MaxMemoryPagesLimit == 65536);

/// The default hard limit of the memory size (256MB) as number of pages.
constexpr uint32_t DefaultMemoryPagesLimit = (256 * 1024 * 1024ULL) / PageSize;
static_assert(DefaultMemoryPagesLimit == 4096);

/// The limit of the size of the call stack, i.e. how many calls are allowed to be stacked up
/// in a single execution thread. Allowed values for call depth levels are [0, CallStackLimit-1].
Expand Down
Loading

0 comments on commit 5027151

Please sign in to comment.