Skip to content

Commit ca2475f

Browse files
authored
Fix build on Windows. (#260)
Signed-off-by: Piotr Sikora <[email protected]>
1 parent d75bfa4 commit ca2475f

File tree

11 files changed

+41
-30
lines changed

11 files changed

+41
-30
lines changed

.bazelrc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ build:linux --linkopt=-ldl
3535

3636
build:macos --cxxopt=-std=c++17
3737

38-
# TODO(mathetake): Windows build is not verified yet.
39-
# build:windows --cxxopt="/std:c++17"
38+
build:windows --enable_runfiles
39+
build:windows --cxxopt="/std:c++17"
4040
# See https://bytecodealliance.github.io/wasmtime/c-api/
4141
# build:windows --linkopt="ws2_32.lib advapi32.lib userenv.lib ntdll.lib shell32.lib ole32.lib"

.github/workflows/cpp.yml

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,11 @@ jobs:
123123
arch: x86_64
124124
action: test
125125
flags: --config=gcc
126+
- name: 'NullVM on Windows/x86_64'
127+
engine: 'null'
128+
os: windows-2019
129+
arch: x86_64
130+
action: test
126131
- name: 'V8 on Linux/x86_64'
127132
engine: 'v8'
128133
repo: 'v8'
@@ -233,11 +238,12 @@ jobs:
233238
path: test/test_data/
234239

235240
- name: Mangle build rules to use existing test data
236-
run: >
237-
sed 's/\.wasm//g' test/BUILD > test/BUILD.tmp && mv test/BUILD.tmp test/BUILD;
238-
echo "package(default_visibility = [\"//visibility:public\"])" > test/test_data/BUILD;
239-
for i in $(cd test/test_data && ls -1 *.wasm | sed 's/\.wasm$//g');
240-
do echo "filegroup(name = \"$i\", srcs = [\"$i.wasm\"])" >> test/test_data/BUILD;
241+
shell: bash
242+
run: |
243+
sed 's/\.wasm//g' test/BUILD > test/BUILD.tmp && mv test/BUILD.tmp test/BUILD
244+
echo "package(default_visibility = [\"//visibility:public\"])" > test/test_data/BUILD
245+
for i in $(cd test/test_data && ls -1 *.wasm | sed 's/\.wasm$//g'); do \
246+
echo "filegroup(name = \"$i\", srcs = [\"$i.wasm\"])" >> test/test_data/BUILD; \
241247
done
242248
243249
- name: Bazel build/test

include/proxy-wasm/exports.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,12 @@ template <typename Pairs> size_t pairsSize(const Pairs &result) {
7474

7575
template <typename Pairs> void marshalPairs(const Pairs &result, char *buffer) {
7676
char *b = buffer;
77-
*reinterpret_cast<uint32_t *>(b) = htole32(result.size());
77+
*reinterpret_cast<uint32_t *>(b) = htowasm(result.size());
7878
b += sizeof(uint32_t);
7979
for (auto &p : result) {
80-
*reinterpret_cast<uint32_t *>(b) = htole32(p.first.size());
80+
*reinterpret_cast<uint32_t *>(b) = htowasm(p.first.size());
8181
b += sizeof(uint32_t);
82-
*reinterpret_cast<uint32_t *>(b) = htole32(p.second.size());
82+
*reinterpret_cast<uint32_t *>(b) = htowasm(p.second.size());
8383
b += sizeof(uint32_t);
8484
}
8585
for (auto &p : result) {

include/proxy-wasm/word.h

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,20 @@
1717

1818
#include <iostream>
1919

20-
#ifdef __APPLE__
21-
#define htole32(x) (x)
22-
#define le32toh(x) (x)
23-
#endif
24-
2520
namespace proxy_wasm {
2621

2722
#include "proxy_wasm_common.h"
2823

24+
// Use byteswap functions only when compiling for big-endian platforms.
25+
#if defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && \
26+
__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
27+
#define htowasm(x) __builtin_bswap32(x)
28+
#define wasmtoh(x) __builtin_bswap32(x)
29+
#else
30+
#define htowasm(x) (x)
31+
#define wasmtoh(x) (x)
32+
#endif
33+
2934
// Represents a Wasm-native word-sized datum. On 32-bit VMs, the high bits are always zero.
3035
// The Wasm/VM API treats all bits as significant.
3136
struct Word {

src/exports.cc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,16 @@ Pairs toPairs(std::string_view buffer) {
6464
if (buffer.size() < sizeof(uint32_t)) {
6565
return {};
6666
}
67-
auto size = le32toh(*reinterpret_cast<const uint32_t *>(b));
67+
auto size = wasmtoh(*reinterpret_cast<const uint32_t *>(b));
6868
b += sizeof(uint32_t);
6969
if (sizeof(uint32_t) + size * 2 * sizeof(uint32_t) > buffer.size()) {
7070
return {};
7171
}
7272
result.resize(size);
7373
for (uint32_t i = 0; i < size; i++) {
74-
result[i].first = std::string_view(nullptr, le32toh(*reinterpret_cast<const uint32_t *>(b)));
74+
result[i].first = std::string_view(nullptr, wasmtoh(*reinterpret_cast<const uint32_t *>(b)));
7575
b += sizeof(uint32_t);
76-
result[i].second = std::string_view(nullptr, le32toh(*reinterpret_cast<const uint32_t *>(b)));
76+
result[i].second = std::string_view(nullptr, wasmtoh(*reinterpret_cast<const uint32_t *>(b)));
7777
b += sizeof(uint32_t);
7878
}
7979
for (auto &p : result) {
@@ -712,8 +712,8 @@ Word writevImpl(Word fd, Word iovs, Word iovs_len, Word *nwritten_ptr) {
712712
}
713713
const uint32_t *iovec = reinterpret_cast<const uint32_t *>(memslice.value().data());
714714
if (iovec[1] /* buf_len */) {
715-
memslice = context->wasmVm()->getMemory(le32toh(iovec[0]) /* buf */,
716-
le32toh(iovec[1]) /* buf_len */);
715+
memslice = context->wasmVm()->getMemory(wasmtoh(iovec[0]) /* buf */,
716+
wasmtoh(iovec[1]) /* buf_len */);
717717
if (!memslice) {
718718
return 21; // __WASI_EFAULT
719719
}

src/signature_util.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ bool SignatureUtil::verifySignature(std::string_view bytecode, std::string &mess
8585

8686
uint32_t alg_id;
8787
std::memcpy(&alg_id, payload.data(), sizeof(uint32_t));
88-
alg_id = le32toh(alg_id);
88+
alg_id = wasmtoh(alg_id);
8989

9090
if (alg_id != 2) {
9191
message = "Signature has a wrong alg_id (want: 2, is: " + std::to_string(alg_id) + ")";

src/v8/v8.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ bool V8::getWord(uint64_t pointer, Word *word) {
475475
}
476476
uint32_t word32;
477477
::memcpy(&word32, memory_->data() + pointer, size);
478-
word->u64_ = le32toh(word32);
478+
word->u64_ = wasmtoh(word32);
479479
return true;
480480
}
481481

@@ -484,7 +484,7 @@ bool V8::setWord(uint64_t pointer, Word word) {
484484
if (pointer + size > memory_->data_size()) {
485485
return false;
486486
}
487-
uint32_t word32 = htole32(word.u32());
487+
uint32_t word32 = htowasm(word.u32());
488488
::memcpy(memory_->data() + pointer, &word32, size);
489489
return true;
490490
}

src/wamr/wamr.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ bool Wamr::getWord(uint64_t pointer, Word *word) {
365365

366366
uint32_t word32;
367367
::memcpy(&word32, wasm_memory_data(memory_.get()) + pointer, size);
368-
word->u64_ = le32toh(word32);
368+
word->u64_ = wasmtoh(word32);
369369
return true;
370370
}
371371

@@ -374,7 +374,7 @@ bool Wamr::setWord(uint64_t pointer, Word word) {
374374
if (pointer + size > wasm_memory_data_size(memory_.get())) {
375375
return false;
376376
}
377-
uint32_t word32 = htole32(word.u32());
377+
uint32_t word32 = htowasm(word.u32());
378378
::memcpy(wasm_memory_data(memory_.get()) + pointer, &word32, size);
379379
return true;
380380
}

src/wasmtime/wasmtime.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ bool Wasmtime::getWord(uint64_t pointer, Word *word) {
392392

393393
uint32_t word32;
394394
::memcpy(&word32, wasm_memory_data(memory_.get()) + pointer, size);
395-
word->u64_ = le32toh(word32);
395+
word->u64_ = wasmtoh(word32);
396396
return true;
397397
}
398398

@@ -401,7 +401,7 @@ bool Wasmtime::setWord(uint64_t pointer, Word word) {
401401
if (pointer + size > wasm_memory_data_size(memory_.get())) {
402402
return false;
403403
}
404-
uint32_t word32 = htole32(word.u32());
404+
uint32_t word32 = htowasm(word.u32());
405405
::memcpy(wasm_memory_data(memory_.get()) + pointer, &word32, size);
406406
return true;
407407
}

src/wavm/wavm.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -390,12 +390,12 @@ bool Wavm::getWord(uint64_t pointer, Word *data) {
390390
auto p = reinterpret_cast<char *>(memory_base_ + pointer);
391391
uint32_t data32;
392392
memcpy(&data32, p, sizeof(uint32_t));
393-
data->u64_ = le32toh(data32);
393+
data->u64_ = wasmtoh(data32);
394394
return true;
395395
}
396396

397397
bool Wavm::setWord(uint64_t pointer, Word data) {
398-
uint32_t data32 = htole32(data.u32());
398+
uint32_t data32 = htowasm(data.u32());
399399
return setMemory(pointer, sizeof(uint32_t), &data32);
400400
}
401401

test/runtime_test.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ TEST_P(TestVM, Memory) {
5757
ASSERT_TRUE(vm_->getWord(0x2000, &word));
5858
ASSERT_EQ(100, word.u64_);
5959

60-
uint32_t data[2] = {htole32(static_cast<uint32_t>(-1)), htole32(200)};
60+
uint32_t data[2] = {htowasm(static_cast<uint32_t>(-1)), htowasm(200)};
6161
ASSERT_TRUE(vm_->setMemory(0x200, sizeof(int32_t) * 2, static_cast<void *>(data)));
6262
ASSERT_TRUE(vm_->getWord(0x200, &word));
6363
ASSERT_EQ(-1, static_cast<int32_t>(word.u64_));

0 commit comments

Comments
 (0)