Skip to content

Commit 6a5bec6

Browse files
committed
Add fizzy_validate() C API function
1 parent 983a349 commit 6a5bec6

6 files changed

Lines changed: 49 additions & 1 deletion

File tree

include/fizzy/fizzy.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,16 @@
33
// SPDX-License-Identifier: Apache-2.0
44
#pragma once
55

6+
#include <stdbool.h>
7+
#include <stddef.h>
8+
#include <stdint.h>
9+
610
#ifdef __cplusplus
711
extern "C" {
812
#endif
913

14+
bool fizzy_validate(const uint8_t* wasm_binary, size_t wasm_binary_size);
15+
1016
#ifdef __cplusplus
1117
}
1218
#endif

lib/fizzy/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ target_sources(
1313
cxx20/bit.hpp
1414
cxx20/span.hpp
1515
bytes.hpp
16+
capi.cpp
1617
constexpr_vector.hpp
1718
exceptions.cpp
1819
exceptions.hpp

lib/fizzy/capi.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Fizzy: A fast WebAssembly interpreter
2+
// Copyright 2020 The Fizzy Authors.
3+
// SPDX-License-Identifier: Apache-2.0
4+
5+
#include "parser.hpp"
6+
#include <fizzy/fizzy.h>
7+
8+
extern "C" {
9+
bool fizzy_validate(const uint8_t* wasm_binary, size_t wasm_binary_size)
10+
{
11+
try
12+
{
13+
fizzy::parse({wasm_binary, wasm_binary_size});
14+
return true;
15+
}
16+
catch (...)
17+
{
18+
return false;
19+
}
20+
}
21+
}

test/compilation/compilation_test.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,9 @@
44

55
#include <fizzy/fizzy.h>
66

7-
void dummy() {}
7+
bool dummy(void);
8+
9+
bool dummy()
10+
{
11+
return fizzy_validate(NULL, 0);
12+
}

test/unittests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ target_sources(
1111
fizzy-unittests PRIVATE
1212
api_test.cpp
1313
bit_cast_test.cpp
14+
capi_test.cpp
1415
constexpr_vector_test.cpp
1516
end_to_end_test.cpp
1617
execute_call_test.cpp

test/unittests/capi_test.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Fizzy: A fast WebAssembly interpreter
2+
// Copyright 2020 The Fizzy Authors.
3+
// SPDX-License-Identifier: Apache-2.0
4+
5+
#include <fizzy/fizzy.h>
6+
#include <gtest/gtest.h>
7+
8+
TEST(capi, validate)
9+
{
10+
uint8_t wasm_prefix[]{0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00};
11+
EXPECT_TRUE(fizzy_validate(wasm_prefix, sizeof(wasm_prefix)));
12+
wasm_prefix[7] = 1;
13+
EXPECT_FALSE(fizzy_validate(wasm_prefix, sizeof(wasm_prefix)));
14+
}

0 commit comments

Comments
 (0)