Skip to content

Commit

Permalink
Merge pull request #241 from Hywan/feat-runtime-c-api-strict-c-cpp
Browse files Browse the repository at this point in the history
test(runtime-c-api) Deny all warnings
  • Loading branch information
syrusakbary authored Mar 8, 2019
2 parents 60a7454 + dd3a6ad commit d3c4733
Show file tree
Hide file tree
Showing 10 changed files with 105 additions and 73 deletions.
12 changes: 6 additions & 6 deletions lib/runtime-c-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,8 @@ pub extern "C" fn wasmer_memory_grow(
/// Returns the current length in pages of the given memory
#[allow(clippy::cast_ptr_alignment)]
#[no_mangle]
pub extern "C" fn wasmer_memory_length(memory: *mut wasmer_memory_t) -> uint32_t {
let memory = unsafe { &*(memory as *mut Memory) };
pub extern "C" fn wasmer_memory_length(memory: *const wasmer_memory_t) -> uint32_t {
let memory = unsafe { &*(memory as *const Memory) };
let Pages(len) = memory.size();
len
}
Expand Down Expand Up @@ -1142,7 +1142,7 @@ pub unsafe extern "C" fn wasmer_import_func_new(
params_len: c_int,
returns: *const wasmer_value_tag,
returns_len: c_int,
) -> *const wasmer_import_func_t {
) -> *mut wasmer_import_func_t {
let params: &[wasmer_value_tag] = slice::from_raw_parts(params, params_len as usize);
let params: Vec<Type> = params.iter().cloned().map(|x| x.into()).collect();
let returns: &[wasmer_value_tag] = slice::from_raw_parts(returns, returns_len as usize);
Expand Down Expand Up @@ -1342,7 +1342,7 @@ pub unsafe extern "C" fn wasmer_export_func_call(
#[allow(clippy::cast_ptr_alignment)]
#[no_mangle]
pub extern "C" fn wasmer_instance_context_memory(
ctx: *mut wasmer_instance_context_t,
ctx: *const wasmer_instance_context_t,
_memory_idx: uint32_t,
) -> *const wasmer_memory_t {
let ctx = unsafe { &*(ctx as *const Ctx) };
Expand All @@ -1353,8 +1353,8 @@ pub extern "C" fn wasmer_instance_context_memory(
/// Gets the start pointer to the bytes within a Memory
#[allow(clippy::cast_ptr_alignment)]
#[no_mangle]
pub extern "C" fn wasmer_memory_data(mem: *mut wasmer_memory_t) -> *mut uint8_t {
let memory = mem as *mut Memory;
pub extern "C" fn wasmer_memory_data(mem: *const wasmer_memory_t) -> *mut uint8_t {
let memory = mem as *const Memory;
use std::cell::Cell;
unsafe { ((*memory).view::<u8>()[..]).as_ptr() as *mut Cell<u8> as *mut u8 }
}
Expand Down
69 changes: 44 additions & 25 deletions lib/runtime-c-api/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
cmake_minimum_required (VERSION 2.6)
project (WasmerCApiTests)
project (WasmerRuntimeCApiTests)

add_executable(test-imports test-imports.c)
add_executable(test-exports test-exports.c)
Expand All @@ -22,39 +22,58 @@ if(NOT WASMER_LIB)
message(FATAL_ERROR "wasmer library not found")
endif()

target_link_libraries(test-imports
general ${WASMER_LIB})
target_link_libraries(test-exports
general ${WASMER_LIB})
target_link_libraries(test-globals
general ${WASMER_LIB})
target_link_libraries(test-instantiate
general ${WASMER_LIB})
target_link_libraries(test-import-function
general ${WASMER_LIB})
target_link_libraries(test-memory
general ${WASMER_LIB})
target_link_libraries(test-module-imports
general ${WASMER_LIB})
target_link_libraries(test-module
general ${WASMER_LIB})
target_link_libraries(test-module-exports
general ${WASMER_LIB})
target_link_libraries(test-validate
general ${WASMER_LIB})
target_link_libraries(test-tables
general ${WASMER_LIB})

enable_testing()

set(
COMPILER_OPTIONS
# Clang or gcc
$<$<OR:$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:GNU>>:
"-Werror" >
# MSVC
$<$<CXX_COMPILER_ID:MSVC>:
"/WX" >
)

target_link_libraries(test-imports general ${WASMER_LIB})
target_compile_options(test-imports PRIVATE ${COMPILER_OPTIONS})
add_test(test-imports test-imports)

target_link_libraries(test-exports general ${WASMER_LIB})
target_compile_options(test-exports PRIVATE ${COMPILER_OPTIONS})
add_test(test-exports test-exports)

target_link_libraries(test-globals general ${WASMER_LIB})
target_compile_options(test-globals PRIVATE ${COMPILER_OPTIONS})
add_test(test-globals test-globals)

target_link_libraries(test-instantiate general ${WASMER_LIB})
target_compile_options(test-instantiate PRIVATE ${COMPILER_OPTIONS})
add_test(test-instantiate test-instantiate)

target_link_libraries(test-import-function general ${WASMER_LIB})
target_compile_options(test-import-function PRIVATE ${COMPILER_OPTIONS})
add_test(test-import-function test-import-function)

target_link_libraries(test-memory general ${WASMER_LIB})
target_compile_options(test-memory PRIVATE ${COMPILER_OPTIONS})
add_test(test-memory test-memory)

target_link_libraries(test-module-imports general ${WASMER_LIB})
target_compile_options(test-module-imports PRIVATE ${COMPILER_OPTIONS})
add_test(test-module-imports test-module-imports)

target_link_libraries(test-module general ${WASMER_LIB})
target_compile_options(test-module PRIVATE ${COMPILER_OPTIONS})
add_test(test-module test-module)

target_link_libraries(test-module-exports general ${WASMER_LIB})
target_compile_options(test-module-exports PRIVATE ${COMPILER_OPTIONS})
add_test(test-module-exports test-module-exports)

target_link_libraries(test-validate general ${WASMER_LIB})
target_compile_options(test-validate PRIVATE ${COMPILER_OPTIONS})
add_test(test-validate test-validate)
add_test(test-tables test-tables)

target_link_libraries(test-tables general ${WASMER_LIB})
target_compile_options(test-tables PRIVATE ${COMPILER_OPTIONS})
add_test(test-tables test-tables)
12 changes: 11 additions & 1 deletion lib/runtime-c-api/tests/runtime_c_api_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,47 @@ use std::process::Command;
#[test]
fn test_c_api() {
let project_tests_dir = concat!(env!("CARGO_MANIFEST_DIR"), "/tests");

run_command("cmake", project_tests_dir, Some("."));
run_command("make", project_tests_dir, None);
run_command("make", project_tests_dir, Some("-Wdev -Werror=dev"));
run_command("make", project_tests_dir, Some("test"));
}

fn run_command(command_str: &str, dir: &str, arg: Option<&str>) {
println!("Running command: `{}` arg: {:?}", command_str, arg);

let mut command = Command::new(command_str);

if let Some(a) = arg {
command.arg(a);
}

command.current_dir(dir);

let result = command.output();

match result {
Ok(r) => {
println!("output:");

if let Some(code) = r.status.code() {
println!("status: {}", code);
} else {
println!("status: None");
}

println!("stdout:");
println!("{}", String::from_utf8_lossy(&r.stdout[..]));
println!("stderr:");
println!("{}", String::from_utf8_lossy(&r.stderr[..]));

if r.status.success() {
assert!(true)
} else {
panic!("Command failed with exit status: {:?}", r.status);
}
}

Err(e) => panic!("Command failed: {}", e),
}
}
4 changes: 2 additions & 2 deletions lib/runtime-c-api/tests/test-exports.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ int main()

wasmer_import_export_kind kind = wasmer_export_kind(export);
assert(kind == WASM_FUNCTION);
wasmer_export_func_t *func = wasmer_export_to_func(export);
const wasmer_export_func_t *func = wasmer_export_to_func(export);

wasmer_byte_array name_bytes = wasmer_export_name(export);
assert(name_bytes.bytes_len == 3);
Expand Down Expand Up @@ -83,4 +83,4 @@ int main()
printf("Destroy exports\n");
wasmer_exports_destroy(exports);
return 0;
}
}
16 changes: 8 additions & 8 deletions lib/runtime-c-api/tests/test-import-function.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
#include <stdint.h>
#include <string.h>

static print_str_called = false;
static memory_len = 0;
static ptr_len = 0;
static bool print_str_called = false;
static int memory_len = 0;
static int ptr_len = 0;
static char actual_str[14] = {};

void print_str(wasmer_instance_context_t *ctx, int32_t ptr, int32_t len)
{
wasmer_memory_t *memory = wasmer_instance_context_memory(ctx, 0);
const wasmer_memory_t *memory = wasmer_instance_context_memory(ctx, 0);
uint32_t mem_len = wasmer_memory_length(memory);
uint8_t *mem_bytes = wasmer_memory_data(memory);
for (int32_t idx = 0; idx < len; idx++)
Expand All @@ -31,16 +31,16 @@ int main()
wasmer_value_tag returns_sig[] = {};

printf("Creating new func\n");
wasmer_import_func_t *func = wasmer_import_func_new(print_str, params_sig, 2, returns_sig, 0);
wasmer_import_func_t *func = wasmer_import_func_new((void (*)(void *)) print_str, params_sig, 2, returns_sig, 0);
wasmer_import_t import;

char *module_name = "env";
wasmer_byte_array module_name_bytes;
module_name_bytes.bytes = module_name;
module_name_bytes.bytes = (const uint8_t *) module_name;
module_name_bytes.bytes_len = strlen(module_name);
char *import_name = "print_str";
wasmer_byte_array import_name_bytes;
import_name_bytes.bytes = import_name;
import_name_bytes.bytes = (const uint8_t *) import_name;
import_name_bytes.bytes_len = strlen(import_name);

import.module_name = module_name_bytes;
Expand Down Expand Up @@ -88,4 +88,4 @@ int main()
printf("Destroy instance\n");
wasmer_instance_destroy(instance);
return 0;
}
}
29 changes: 15 additions & 14 deletions lib/runtime-c-api/tests/test-imports.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
#include "../wasmer.h"
#include <assert.h>
#include <stdint.h>
#include <string.h>

static print_str_called = false;
bool static print_str_called = false;

// Host function that will be imported into the Web Assembly Instance
void print_str(wasmer_instance_context_t *ctx, int32_t ptr, int32_t len)
void print_str(const wasmer_instance_context_t *ctx, int32_t ptr, int32_t len)
{
print_str_called = true;
wasmer_memory_t *memory = wasmer_instance_context_memory(ctx, 0);
const wasmer_memory_t *memory = wasmer_instance_context_memory(ctx, 0);
uint32_t mem_len = wasmer_memory_length(memory);
uint8_t *mem_bytes = wasmer_memory_data(memory);
printf("%.*s", len, mem_bytes + ptr);
Expand All @@ -31,19 +32,19 @@ int main()
// of our `print_str` host function
wasmer_value_tag params_sig[] = {WASM_I32, WASM_I32};
wasmer_value_tag returns_sig[] = {};
wasmer_import_func_t *func = wasmer_import_func_new(print_str, params_sig, 2, returns_sig, 0);
wasmer_import_func_t *func = wasmer_import_func_new((void (*)(void *)) print_str, params_sig, 2, returns_sig, 0);

// Create module name for our imports
// represented in bytes for UTF-8 compatability
char *module_name = "env";
const char *module_name = "env";
wasmer_byte_array module_name_bytes;
module_name_bytes.bytes = module_name;
module_name_bytes.bytes = (const uint8_t *) module_name;
module_name_bytes.bytes_len = strlen(module_name);

// Define a function import
char *import_name = "_print_str";
const char *import_name = "_print_str";
wasmer_byte_array import_name_bytes;
import_name_bytes.bytes = import_name;
import_name_bytes.bytes = (const uint8_t *) import_name;
import_name_bytes.bytes_len = strlen(import_name);
wasmer_import_t func_import;
func_import.module_name = module_name_bytes;
Expand All @@ -52,9 +53,9 @@ int main()
func_import.value.func = func;

// Define a memory import
char *import_memory_name = "memory";
const char *import_memory_name = "memory";
wasmer_byte_array import_memory_name_bytes;
import_memory_name_bytes.bytes = import_memory_name;
import_memory_name_bytes.bytes = (const uint8_t *) import_memory_name;
import_memory_name_bytes.bytes_len = strlen(import_memory_name);
wasmer_import_t memory_import;
memory_import.module_name = module_name_bytes;
Expand All @@ -75,9 +76,9 @@ int main()
memory_import.value.memory = memory;

// Define a global import
char *import_global_name = "__memory_base";
const char *import_global_name = "__memory_base";
wasmer_byte_array import_global_name_bytes;
import_global_name_bytes.bytes = import_global_name;
import_global_name_bytes.bytes = (const uint8_t *) import_global_name;
import_global_name_bytes.bytes_len = strlen(import_global_name);
wasmer_import_t global_import;
global_import.module_name = module_name_bytes;
Expand All @@ -90,9 +91,9 @@ int main()
global_import.value.global = global;

// Define a table import
char *import_table_name = "table";
const char *import_table_name = "table";
wasmer_byte_array import_table_name_bytes;
import_table_name_bytes.bytes = import_table_name;
import_table_name_bytes.bytes = (const uint8_t *) import_table_name;
import_table_name_bytes.bytes_len = strlen(import_table_name);
wasmer_import_t table_import;
table_import.module_name = module_name_bytes;
Expand Down
3 changes: 2 additions & 1 deletion lib/runtime-c-api/tests/test-instantiate.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "../wasmer.h"
#include <assert.h>
#include <stdint.h>
#include <string.h>

int main()
{
Expand Down Expand Up @@ -53,4 +54,4 @@ int main()
printf("Destroy instance\n");
wasmer_instance_destroy(instance);
return 0;
}
}
1 change: 1 addition & 0 deletions lib/runtime-c-api/tests/test-memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "../wasmer.h"
#include <assert.h>
#include <stdint.h>
#include <string.h>

int main()
{
Expand Down
16 changes: 8 additions & 8 deletions lib/runtime-c-api/wasmer.h
Original file line number Diff line number Diff line change
Expand Up @@ -325,11 +325,11 @@ void wasmer_import_func_destroy(wasmer_import_func_t *func);
* Creates new func
* The caller owns the object and should call `wasmer_import_func_destroy` to free it.
*/
const wasmer_import_func_t *wasmer_import_func_new(void (*func)(void *data),
const wasmer_value_tag *params,
int params_len,
const wasmer_value_tag *returns,
int returns_len);
wasmer_import_func_t *wasmer_import_func_new(void (*func)(void *data),
const wasmer_value_tag *params,
int params_len,
const wasmer_value_tag *returns,
int returns_len);

/**
* Sets the params buffer to the parameter types of the given wasmer_import_func_t
Expand Down Expand Up @@ -386,7 +386,7 @@ wasmer_result_t wasmer_instance_call(wasmer_instance_t *instance,
* Gets the memory within the context at the index `memory_idx`.
* The index is always 0 until multiple memories are supported.
*/
const wasmer_memory_t *wasmer_instance_context_memory(wasmer_instance_context_t *ctx,
const wasmer_memory_t *wasmer_instance_context_memory(const wasmer_instance_context_t *ctx,
uint32_t _memory_idx);

/**
Expand Down Expand Up @@ -442,7 +442,7 @@ int wasmer_last_error_message(char *buffer, int length);
/**
* Gets the start pointer to the bytes within a Memory
*/
uint8_t *wasmer_memory_data(wasmer_memory_t *mem);
uint8_t *wasmer_memory_data(const wasmer_memory_t *mem);

/**
* Gets the size in bytes of a Memory
Expand All @@ -465,7 +465,7 @@ wasmer_result_t wasmer_memory_grow(wasmer_memory_t *memory, uint32_t delta);
/**
* Returns the current length in pages of the given memory
*/
uint32_t wasmer_memory_length(wasmer_memory_t *memory);
uint32_t wasmer_memory_length(const wasmer_memory_t *memory);

/**
* Creates a new Memory for the given descriptor and initializes the given
Expand Down
Loading

0 comments on commit d3c4733

Please sign in to comment.