Skip to content

Commit

Permalink
Add fizzy_validate() C API function
Browse files Browse the repository at this point in the history
  • Loading branch information
chfast authored and gumb0 committed Sep 16, 2020
1 parent fced26a commit de60a34
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 1 deletion.
6 changes: 6 additions & 0 deletions include/fizzy/fizzy.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@
// SPDX-License-Identifier: Apache-2.0
#pragma once

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

#if __cplusplus
extern "C" {
#endif

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

#if __cplusplus
}
#endif
1 change: 1 addition & 0 deletions lib/fizzy/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ target_sources(

PRIVATE
bytes.hpp
capi.cpp
constexpr_vector.hpp
exceptions.cpp
exceptions.hpp
Expand Down
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;
}
}
}
5 changes: 4 additions & 1 deletion test/compilation/compilation_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@

#include <fizzy/fizzy.h>

void dummy() {}
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 @@ -10,6 +10,7 @@ target_link_libraries(fizzy-unittests PRIVATE fizzy::fizzy fizzy::test-utils GTe
target_sources(
fizzy-unittests PRIVATE
api_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, fizzy_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)));
}

0 comments on commit de60a34

Please sign in to comment.