Add validate function and test

This commit is contained in:
Brandon Fish 2019-02-05 00:01:01 -06:00
parent 8d8dea7ec8
commit 309246e0d6
6 changed files with 42 additions and 0 deletions

View File

@ -85,6 +85,17 @@ pub struct wasmer_limits_t {
pub max: uint32_t,
}
#[allow(clippy::cast_ptr_alignment)]
#[no_mangle]
pub unsafe extern "C" fn wasmer_validate(wasm_bytes: *mut uint8_t, wasm_bytes_len: uint32_t) -> bool {
if wasm_bytes.is_null() {
return false;
}
let bytes: &[u8] =
unsafe { ::std::slice::from_raw_parts_mut(wasm_bytes, wasm_bytes_len as usize) };
wasmer_runtime_core::validate(bytes)
}
#[no_mangle]
pub extern "C" fn wasmer_import_object_new() -> *mut wasmer_import_object_t {
Box::into_raw(Box::new(ImportObject::new())) as *mut wasmer_import_object_t

View File

@ -12,4 +12,5 @@ _deps
test-instantiate
test-import-function
test-memory
test-validate
rust-build

View File

@ -4,6 +4,7 @@ project (WasmerCApiTests)
add_executable(test-instantiate test-instantiate.c)
add_executable(test-import-function test-import-function.c)
add_executable(test-memory test-memory.c)
add_executable(test-validate test-validate.c)
find_library(
WASMER_LIB NAMES libwasmer_runtime_c_api.dylib libwasmer_runtime_c_api.so libwasmer_runtime_c_api.dll
@ -20,9 +21,12 @@ target_link_libraries(test-import-function
general ${WASMER_LIB})
target_link_libraries(test-memory
general ${WASMER_LIB})
target_link_libraries(test-validate
general ${WASMER_LIB})
enable_testing()
add_test(test-instantiate test-instantiate)
add_test(test-import-function test-import-function)
add_test(test-memory test-memory)
add_test(test-validate test-validate)

View File

@ -0,0 +1,22 @@
#include <stdio.h>
#include "../wasmer.h"
#include <assert.h>
#include <stdint.h>
int main()
{
// Read the wasm file bytes
FILE *file = fopen("sum.wasm", "r");
fseek(file, 0, SEEK_END);
long len = ftell(file);
uint8_t *bytes = malloc(len);
fseek(file, 0, SEEK_SET);
fread(bytes, 1, len, file);
fclose(file);
bool result = wasmer_validate(bytes, len);
printf("Result: %d", result);
assert(result);
return 0;
}

View File

@ -87,3 +87,5 @@ void wasmer_memory_destroy(wasmer_memory_t *memory);
uint32_t wasmer_memory_length(wasmer_memory_t *memory);
wasmer_memory_result_t wasmer_memory_new(wasmer_memory_t **memory, wasmer_limits_t limits);
bool wasmer_validate(uint8_t *wasm_bytes, uint32_t wasm_bytes_len);

View File

@ -88,4 +88,6 @@ uint32_t wasmer_memory_length(wasmer_memory_t *memory);
wasmer_memory_result_t wasmer_memory_new(wasmer_memory_t **memory, wasmer_limits_t limits);
bool wasmer_validate(uint8_t *wasm_bytes, uint32_t wasm_bytes_len);
} // extern "C"