Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Public C API (validation only) #530

Merged
merged 5 commits into from
Sep 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ cable_configure_compiler()
add_compile_options(
-Wcast-qual
-Wcast-align
-Wextra-semi
-Wmissing-declarations
-Wold-style-cast
$<$<COMPILE_LANGUAGE:CXX>:-Wextra-semi>
$<$<COMPILE_LANGUAGE:CXX>:-Wold-style-cast>
)
cable_add_cxx_compiler_flag_if_supported(-Wfinal-dtor-non-final-class)
cable_add_cxx_compiler_flag_if_supported(-Wnewline-eof)
Expand Down Expand Up @@ -86,7 +86,8 @@ if(FIZZY_FUZZING)
add_link_options(${fuzzing_flags})
endif()

set(include_dir ${PROJECT_SOURCE_DIR}/include) # Public include directory.
# Public include directory.
set(FIZZY_INCLUDE_DIR $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)

set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib)
Expand All @@ -98,3 +99,6 @@ if(FIZZY_TESTING)
enable_testing() # Enable CTest. Must be done in main CMakeLists.txt.
add_subdirectory(test)
endif()

install(TARGETS fizzy EXPORT fizzyTargets)
install(DIRECTORY include/fizzy TYPE INCLUDE)
18 changes: 18 additions & 0 deletions include/fizzy/fizzy.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Fizzy: A fast WebAssembly interpreter
// Copyright 2020 The Fizzy Authors.
// SPDX-License-Identifier: Apache-2.0
#pragma once

#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>

#ifdef __cplusplus
extern "C" {
#endif

bool fizzy_validate(const uint8_t* wasm_binary, size_t wasm_binary_size);

#ifdef __cplusplus
}
#endif
5 changes: 4 additions & 1 deletion lib/fizzy/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@

add_library(fizzy)
add_library(fizzy::fizzy ALIAS fizzy)
target_compile_features(fizzy PUBLIC cxx_std_17)
target_include_directories(fizzy PUBLIC ${FIZZY_INCLUDE_DIR})

target_sources(
fizzy PRIVATE
${FIZZY_INCLUDE_DIR}/fizzy/fizzy.h
cxx20/bit.hpp
cxx20/span.hpp
bytes.hpp
capi.cpp
constexpr_vector.hpp
exceptions.cpp
exceptions.hpp
Expand All @@ -32,4 +36,3 @@ target_sources(
utf8.hpp
value.hpp
)
target_compile_features(fizzy PUBLIC cxx_std_17)
21 changes: 21 additions & 0 deletions lib/fizzy/capi.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Fizzy: A fast WebAssembly interpreter
// Copyright 2020 The Fizzy Authors.
// SPDX-License-Identifier: Apache-2.0

#include "parser.hpp"
#include <fizzy/fizzy.h>

extern "C" {
bool fizzy_validate(const uint8_t* wasm_binary, size_t wasm_binary_size)
{
try
{
fizzy::parse({wasm_binary, wasm_binary_size});
return true;
}
catch (...)
{
return false;
}
}
}
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ set(fizzy_include_dir ${PROJECT_SOURCE_DIR}/lib/fizzy)
add_subdirectory(utils)
add_subdirectory(bench)
add_subdirectory(bench_internal)
add_subdirectory(compilation)
add_subdirectory(smoketests)
add_subdirectory(spectests)
add_subdirectory(testfloat)
Expand Down
15 changes: 15 additions & 0 deletions test/compilation/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# EVMC: Ethereum Client-VM Connector API.
# Copyright 2018-2020 The EVMC Authors.
# Licensed under the Apache License, Version 2.0.

# This CMake script creates multiple additional targets to test the compilation of public headers
# with different C standards.

set(STANDARDS c_std_99 c_std_11)

foreach(STANDARD 99 11)
set(target test-compile-c${STANDARD})
add_library(${target} OBJECT compilation_test.c)
target_link_libraries(${target} PRIVATE fizzy::fizzy)
set_target_properties(${target} PROPERTIES C_STANDARD ${STANDARD} C_EXTENSIONS OFF C_STANDARD_REQUIRED TRUE)
endforeach()
12 changes: 12 additions & 0 deletions test/compilation/compilation_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Fizzy: A fast WebAssembly interpreter
// Copyright 2020 The Fizzy Authors.
// SPDX-License-Identifier: Apache-2.0

#include <fizzy/fizzy.h>

bool dummy(void);

bool dummy()
{
return fizzy_validate(NULL, 0);
}
1 change: 1 addition & 0 deletions test/unittests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ target_sources(
fizzy-unittests PRIVATE
api_test.cpp
bit_cast_test.cpp
capi_test.cpp
constexpr_vector_test.cpp
end_to_end_test.cpp
execute_call_test.cpp
Expand Down
14 changes: 14 additions & 0 deletions test/unittests/capi_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Fizzy: A fast WebAssembly interpreter
// Copyright 2020 The Fizzy Authors.
// SPDX-License-Identifier: Apache-2.0

#include <fizzy/fizzy.h>
#include <gtest/gtest.h>

TEST(capi, validate)
{
uint8_t wasm_prefix[]{0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00};
EXPECT_TRUE(fizzy_validate(wasm_prefix, sizeof(wasm_prefix)));
wasm_prefix[7] = 1;
EXPECT_FALSE(fizzy_validate(wasm_prefix, sizeof(wasm_prefix)));
}