Skip to content

Commit e121c47

Browse files
author
Mark McCaskey
committed
Generate C preprocessor code to hide things not on Windows
1 parent 2343075 commit e121c47

File tree

5 files changed

+101
-1
lines changed

5 files changed

+101
-1
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## **[Unreleased]**
44

5+
- [#952](https://github.com/wasmerio/wasmer/pull/952) Use C preprocessor to properly hide trampoline functions on Windowns and non-x86_64 targets.
6+
57
## 0.10.0 - 2019-11-11
68

79
Special thanks to [@newpavlov](https://github.com/newpavlov) and [@Maxgy](https://github.com/Maxgy) for their contributions!

lib/runtime-c-api/build.rs

+22
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,31 @@ fn main() {
1212
let mut out_wasmer_header_file = PathBuf::from(&out_dir);
1313
out_wasmer_header_file.push("wasmer");
1414

15+
const WASMER_PRE_HEADER: &str = r#"
16+
#ifndef WASMER_H_MACROS
17+
#define WASMER_H_MACROS
18+
#if MSVC
19+
#ifdef _M_AMD64
20+
#define ARCH_X86_64
21+
#endif
22+
#endif
23+
24+
#if GCC
25+
#ifdef __x86_64__
26+
#define ARCH_X86_64
27+
#endif
28+
#endif
29+
#endif // WASMER_H_MACROS
30+
"#;
1531
// Generate the C bindings in the `OUT_DIR`.
1632
out_wasmer_header_file.set_extension("h");
1733
Builder::new()
1834
.with_crate(crate_dir.clone())
1935
.with_language(Language::C)
2036
.with_include_guard("WASMER_H")
37+
.with_header(WASMER_PRE_HEADER)
38+
.with_define("target_family", "windows", "_WIN32")
39+
.with_define("target_arch", "x86_64", "ARCH_X86_64")
2140
.generate()
2241
.expect("Unable to generate C bindings")
2342
.write_to_file(out_wasmer_header_file.as_path());
@@ -28,6 +47,9 @@ fn main() {
2847
.with_crate(crate_dir)
2948
.with_language(Language::Cxx)
3049
.with_include_guard("WASMER_H")
50+
.with_header(WASMER_PRE_HEADER)
51+
.with_define("target_family", "windows", "_WIN32")
52+
.with_define("target_arch", "x86_64", "ARCH_X86_64")
3153
.generate()
3254
.expect("Unable to generate C++ bindings")
3355
.write_to_file(out_wasmer_header_file.as_path());

lib/runtime-c-api/src/lib.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,9 @@ pub mod instance;
101101
pub mod memory;
102102
pub mod module;
103103
pub mod table;
104-
#[cfg(all(unix, target_arch = "x86_64"))]
104+
// `not(target_family = "windows")` is simpler than `unix`. See build.rs
105+
// if you want to change the meaning of these `cfg`s in the header file.
106+
#[cfg(all(not(target_family = "windows"), target_arch = "x86_64"))]
105107
pub mod trampoline;
106108
pub mod value;
107109

lib/runtime-c-api/wasmer.h

+37
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
2+
#ifndef WASMER_H_MACROS
3+
#define WASMER_H_MACROS
4+
#if MSVC
5+
#ifdef _M_AMD64
6+
#define ARCH_X86_64
7+
#endif
8+
#endif
9+
10+
#if GCC
11+
#ifdef __x86_64__
12+
#define ARCH_X86_64
13+
#endif
14+
#endif
15+
#endif // WASMER_H_MACROS
16+
17+
118
#ifndef WASMER_H
219
#define WASMER_H
320

@@ -162,17 +179,23 @@ typedef struct {
162179

163180
} wasmer_serialized_module_t;
164181

182+
#if (!defined(_WIN32) && defined(ARCH_X86_64))
165183
typedef struct {
166184

167185
} wasmer_trampoline_buffer_builder_t;
186+
#endif
168187

188+
#if (!defined(_WIN32) && defined(ARCH_X86_64))
169189
typedef struct {
170190

171191
} wasmer_trampoline_callable_t;
192+
#endif
172193

194+
#if (!defined(_WIN32) && defined(ARCH_X86_64))
173195
typedef struct {
174196

175197
} wasmer_trampoline_buffer_t;
198+
#endif
176199

177200
/**
178201
* Opens a directory that's visible to the WASI module as `alias` but
@@ -780,46 +803,60 @@ uint32_t wasmer_table_length(wasmer_table_t *table);
780803
*/
781804
wasmer_result_t wasmer_table_new(wasmer_table_t **table, wasmer_limits_t limits);
782805

806+
#if (!defined(_WIN32) && defined(ARCH_X86_64))
783807
/**
784808
* Adds a callinfo trampoline to the builder.
785809
*/
786810
uintptr_t wasmer_trampoline_buffer_builder_add_callinfo_trampoline(wasmer_trampoline_buffer_builder_t *builder,
787811
const wasmer_trampoline_callable_t *func,
788812
const void *ctx,
789813
uint32_t num_params);
814+
#endif
790815

816+
#if (!defined(_WIN32) && defined(ARCH_X86_64))
791817
/**
792818
* Adds a context trampoline to the builder.
793819
*/
794820
uintptr_t wasmer_trampoline_buffer_builder_add_context_trampoline(wasmer_trampoline_buffer_builder_t *builder,
795821
const wasmer_trampoline_callable_t *func,
796822
const void *ctx);
823+
#endif
797824

825+
#if (!defined(_WIN32) && defined(ARCH_X86_64))
798826
/**
799827
* Finalizes the trampoline builder into an executable buffer.
800828
*/
801829
wasmer_trampoline_buffer_t *wasmer_trampoline_buffer_builder_build(wasmer_trampoline_buffer_builder_t *builder);
830+
#endif
802831

832+
#if (!defined(_WIN32) && defined(ARCH_X86_64))
803833
/**
804834
* Creates a new trampoline builder.
805835
*/
806836
wasmer_trampoline_buffer_builder_t *wasmer_trampoline_buffer_builder_new(void);
837+
#endif
807838

839+
#if (!defined(_WIN32) && defined(ARCH_X86_64))
808840
/**
809841
* Destroys the trampoline buffer if not null.
810842
*/
811843
void wasmer_trampoline_buffer_destroy(wasmer_trampoline_buffer_t *buffer);
844+
#endif
812845

846+
#if (!defined(_WIN32) && defined(ARCH_X86_64))
813847
/**
814848
* Returns the callable pointer for the trampoline with index `idx`.
815849
*/
816850
const wasmer_trampoline_callable_t *wasmer_trampoline_buffer_get_trampoline(const wasmer_trampoline_buffer_t *buffer,
817851
uintptr_t idx);
852+
#endif
818853

854+
#if (!defined(_WIN32) && defined(ARCH_X86_64))
819855
/**
820856
* Returns the context added by `add_context_trampoline`, from within the callee function.
821857
*/
822858
void *wasmer_trampoline_get_context(void);
859+
#endif
823860

824861
/**
825862
* Returns true for valid wasm bytes and false for invalid bytes

lib/runtime-c-api/wasmer.hh

+37
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
2+
#ifndef WASMER_H_MACROS
3+
#define WASMER_H_MACROS
4+
#if MSVC
5+
#ifdef _M_AMD64
6+
#define ARCH_X86_64
7+
#endif
8+
#endif
9+
10+
#if GCC
11+
#ifdef __x86_64__
12+
#define ARCH_X86_64
13+
#endif
14+
#endif
15+
#endif // WASMER_H_MACROS
16+
17+
118
#ifndef WASMER_H
219
#define WASMER_H
320

@@ -146,17 +163,23 @@ struct wasmer_serialized_module_t {
146163

147164
};
148165

166+
#if (!defined(_WIN32) && defined(ARCH_X86_64))
149167
struct wasmer_trampoline_buffer_builder_t {
150168

151169
};
170+
#endif
152171

172+
#if (!defined(_WIN32) && defined(ARCH_X86_64))
153173
struct wasmer_trampoline_callable_t {
154174

155175
};
176+
#endif
156177

178+
#if (!defined(_WIN32) && defined(ARCH_X86_64))
157179
struct wasmer_trampoline_buffer_t {
158180

159181
};
182+
#endif
160183

161184
/// Opens a directory that's visible to the WASI module as `alias` but
162185
/// is backed by the host file at `host_file_path`
@@ -612,32 +635,46 @@ uint32_t wasmer_table_length(wasmer_table_t *table);
612635
/// and `wasmer_last_error_message` to get an error message.
613636
wasmer_result_t wasmer_table_new(wasmer_table_t **table, wasmer_limits_t limits);
614637

638+
#if (!defined(_WIN32) && defined(ARCH_X86_64))
615639
/// Adds a callinfo trampoline to the builder.
616640
uintptr_t wasmer_trampoline_buffer_builder_add_callinfo_trampoline(wasmer_trampoline_buffer_builder_t *builder,
617641
const wasmer_trampoline_callable_t *func,
618642
const void *ctx,
619643
uint32_t num_params);
644+
#endif
620645

646+
#if (!defined(_WIN32) && defined(ARCH_X86_64))
621647
/// Adds a context trampoline to the builder.
622648
uintptr_t wasmer_trampoline_buffer_builder_add_context_trampoline(wasmer_trampoline_buffer_builder_t *builder,
623649
const wasmer_trampoline_callable_t *func,
624650
const void *ctx);
651+
#endif
625652

653+
#if (!defined(_WIN32) && defined(ARCH_X86_64))
626654
/// Finalizes the trampoline builder into an executable buffer.
627655
wasmer_trampoline_buffer_t *wasmer_trampoline_buffer_builder_build(wasmer_trampoline_buffer_builder_t *builder);
656+
#endif
628657

658+
#if (!defined(_WIN32) && defined(ARCH_X86_64))
629659
/// Creates a new trampoline builder.
630660
wasmer_trampoline_buffer_builder_t *wasmer_trampoline_buffer_builder_new();
661+
#endif
631662

663+
#if (!defined(_WIN32) && defined(ARCH_X86_64))
632664
/// Destroys the trampoline buffer if not null.
633665
void wasmer_trampoline_buffer_destroy(wasmer_trampoline_buffer_t *buffer);
666+
#endif
634667

668+
#if (!defined(_WIN32) && defined(ARCH_X86_64))
635669
/// Returns the callable pointer for the trampoline with index `idx`.
636670
const wasmer_trampoline_callable_t *wasmer_trampoline_buffer_get_trampoline(const wasmer_trampoline_buffer_t *buffer,
637671
uintptr_t idx);
672+
#endif
638673

674+
#if (!defined(_WIN32) && defined(ARCH_X86_64))
639675
/// Returns the context added by `add_context_trampoline`, from within the callee function.
640676
void *wasmer_trampoline_get_context();
677+
#endif
641678

642679
/// Returns true for valid wasm bytes and false for invalid bytes
643680
bool wasmer_validate(const uint8_t *wasm_bytes, uint32_t wasm_bytes_len);

0 commit comments

Comments
 (0)