Skip to content

Commit

Permalink
wasm2c: Add segue option to make exclusive use of the segment register
Browse files Browse the repository at this point in the history
  • Loading branch information
shravanrn committed Sep 6, 2024
1 parent c6853eb commit 8f7a227
Show file tree
Hide file tree
Showing 14 changed files with 209 additions and 55 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ jobs:
env:
USE_NINJA: "1"
WASM2C_CC: "clang"
WASM2C_CFLAGS: "-DWASM_RT_USE_MMAP=1 -DWASM_RT_SKIP_SIGNAL_RECOVERY=1 -DWASM_RT_NONCONFORMING_UNCHECKED_STACK_EXHAUSTION=1 -DWASM2C_TEST_EMBEDDER_SIGNAL_HANDLING -DWASM_RT_ALLOW_SEGUE=1 -mfsgsbase -DWASM_RT_SANITY_CHECKS=1 -Wno-pass-failed"
WASM2C_CFLAGS: "-DWASM_RT_USE_MMAP=1 -DWASM_RT_SKIP_SIGNAL_RECOVERY=1 -DWASM_RT_NONCONFORMING_UNCHECKED_STACK_EXHAUSTION=1 -DWASM2C_TEST_EMBEDDER_SIGNAL_HANDLING -DWASM_RT_ALLOW_SEGUE=1 -DWASM_RT_SEGUE_FREE_SEGMENT=1 -mfsgsbase -DWASM_RT_SANITY_CHECKS=1 -Wno-pass-failed"
steps:
- uses: actions/setup-python@v1
with:
Expand Down
6 changes: 5 additions & 1 deletion src/c-writer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2449,7 +2449,9 @@ bool CWriter::IsSingleUnsharedMemory() {
void CWriter::InstallSegueBase(Memory* memory, bool save_old_value) {
NonIndented([&] { Write("#if WASM_RT_USE_SEGUE", Newline()); });
if (save_old_value) {
NonIndented([&] { Write("#if !WASM_RT_SEGUE_FREE_SEGMENT", Newline()); });
Write("void* segue_saved_base = wasm_rt_segue_read_base();", Newline());
NonIndented([&] { Write("#endif", Newline()); });
}
auto primary_memory =
ExternalInstanceRef(ModuleFieldType::Memory, memory->name);
Expand All @@ -2458,7 +2460,9 @@ void CWriter::InstallSegueBase(Memory* memory, bool save_old_value) {
}

void CWriter::RestoreSegueBase() {
NonIndented([&] { Write("#if WASM_RT_USE_SEGUE", Newline()); });
NonIndented([&] {
Write("#if WASM_RT_USE_SEGUE && !WASM_RT_SEGUE_FREE_SEGMENT", Newline());
});
Write("wasm_rt_segue_write_base(segue_saved_base);", Newline());
NonIndented([&] { Write("#endif", Newline()); });
}
Expand Down
27 changes: 24 additions & 3 deletions src/prebuilt/wasm2c_source_declarations.cc
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,34 @@ R"w2c_template(}
)w2c_template"
R"w2c_template(static inline void wasm_rt_segue_write_base(void* base) {
)w2c_template"
R"w2c_template( if (wasm_rt_fsgsbase_inst_supported) {
R"w2c_template( bool should_set = true;
)w2c_template"
R"w2c_template(#if WASM_RT_SEGUE_FREE_SEGMENT
)w2c_template"
R"w2c_template( __builtin_ia32_wrgsbase64((uintptr_t)base);
R"w2c_template( if (wasm_rt_last_segment_val == base) {
)w2c_template"
R"w2c_template( should_set = false;
)w2c_template"
R"w2c_template( } else {
)w2c_template"
R"w2c_template( wasm_rt_syscall_set_segue_base(base);
R"w2c_template( wasm_rt_last_segment_val = base;
)w2c_template"
R"w2c_template( }
)w2c_template"
R"w2c_template(#endif
)w2c_template"
R"w2c_template(
if (should_set) {
)w2c_template"
R"w2c_template( if (wasm_rt_fsgsbase_inst_supported) {
)w2c_template"
R"w2c_template( __builtin_ia32_wrgsbase64((uintptr_t)base);
)w2c_template"
R"w2c_template( } else {
)w2c_template"
R"w2c_template( wasm_rt_syscall_set_segue_base(base);
)w2c_template"
R"w2c_template( }
)w2c_template"
R"w2c_template( }
)w2c_template"
Expand Down
17 changes: 14 additions & 3 deletions src/template/wasm2c.declarations.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,21 @@ static inline void* wasm_rt_segue_read_base() {
}
}
static inline void wasm_rt_segue_write_base(void* base) {
if (wasm_rt_fsgsbase_inst_supported) {
__builtin_ia32_wrgsbase64((uintptr_t)base);
bool should_set = true;
#if WASM_RT_SEGUE_FREE_SEGMENT
if (wasm_rt_last_segment_val == base) {
should_set = false;
} else {
wasm_rt_syscall_set_segue_base(base);
wasm_rt_last_segment_val = base;
}
#endif

if (should_set) {
if (wasm_rt_fsgsbase_inst_supported) {
__builtin_ia32_wrgsbase64((uintptr_t)base);
} else {
wasm_rt_syscall_set_segue_base(base);
}
}
}
#define MEM_ADDR_MEMOP(mem, addr, n) ((uint8_t __seg_gs*)(uintptr_t)addr)
Expand Down
17 changes: 14 additions & 3 deletions test/wasm2c/add.txt
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,21 @@ static inline void* wasm_rt_segue_read_base() {
}
}
static inline void wasm_rt_segue_write_base(void* base) {
if (wasm_rt_fsgsbase_inst_supported) {
__builtin_ia32_wrgsbase64((uintptr_t)base);
bool should_set = true;
#if WASM_RT_SEGUE_FREE_SEGMENT
if (wasm_rt_last_segment_val == base) {
should_set = false;
} else {
wasm_rt_syscall_set_segue_base(base);
wasm_rt_last_segment_val = base;
}
#endif
if (should_set) {
if (wasm_rt_fsgsbase_inst_supported) {
__builtin_ia32_wrgsbase64((uintptr_t)base);
} else {
wasm_rt_syscall_set_segue_base(base);
}
}
}
#define MEM_ADDR_MEMOP(mem, addr, n) ((uint8_t __seg_gs*)(uintptr_t)addr)
Expand Down
21 changes: 17 additions & 4 deletions test/wasm2c/check-imports.txt
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,21 @@ static inline void* wasm_rt_segue_read_base() {
}
}
static inline void wasm_rt_segue_write_base(void* base) {
if (wasm_rt_fsgsbase_inst_supported) {
__builtin_ia32_wrgsbase64((uintptr_t)base);
bool should_set = true;
#if WASM_RT_SEGUE_FREE_SEGMENT
if (wasm_rt_last_segment_val == base) {
should_set = false;
} else {
wasm_rt_syscall_set_segue_base(base);
wasm_rt_last_segment_val = base;
}
#endif
if (should_set) {
if (wasm_rt_fsgsbase_inst_supported) {
__builtin_ia32_wrgsbase64((uintptr_t)base);
} else {
wasm_rt_syscall_set_segue_base(base);
}
}
}
#define MEM_ADDR_MEMOP(mem, addr, n) ((uint8_t __seg_gs*)(uintptr_t)addr)
Expand Down Expand Up @@ -825,11 +836,13 @@ void wasm2c_test_instantiate(w2c_test* instance, struct w2c_env* w2c_env_instanc
init_tables(instance);
init_memories(instance);
#if WASM_RT_USE_SEGUE
#if !WASM_RT_SEGUE_FREE_SEGMENT
void* segue_saved_base = wasm_rt_segue_read_base();
#endif
wasm_rt_segue_write_base((*instance->w2c_env_0x5F_linear_memory).data);
#endif
init_elem_instances(instance);
#if WASM_RT_USE_SEGUE
#if WASM_RT_USE_SEGUE && !WASM_RT_SEGUE_FREE_SEGMENT
wasm_rt_segue_write_base(segue_saved_base);
#endif
}
Expand Down
41 changes: 32 additions & 9 deletions test/wasm2c/export-names.txt
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,21 @@ static inline void* wasm_rt_segue_read_base() {
}
}
static inline void wasm_rt_segue_write_base(void* base) {
if (wasm_rt_fsgsbase_inst_supported) {
__builtin_ia32_wrgsbase64((uintptr_t)base);
bool should_set = true;
#if WASM_RT_SEGUE_FREE_SEGMENT
if (wasm_rt_last_segment_val == base) {
should_set = false;
} else {
wasm_rt_syscall_set_segue_base(base);
wasm_rt_last_segment_val = base;
}
#endif
if (should_set) {
if (wasm_rt_fsgsbase_inst_supported) {
__builtin_ia32_wrgsbase64((uintptr_t)base);
} else {
wasm_rt_syscall_set_segue_base(base);
}
}
}
#define MEM_ADDR_MEMOP(mem, addr, n) ((uint8_t __seg_gs*)(uintptr_t)addr)
Expand Down Expand Up @@ -796,59 +807,69 @@ static void init_memories(w2c_test* instance) {
/* export: '' */
void w2c_test_(w2c_test* instance) {
#if WASM_RT_USE_SEGUE
#if !WASM_RT_SEGUE_FREE_SEGMENT
void* segue_saved_base = wasm_rt_segue_read_base();
#endif
wasm_rt_segue_write_base((*instance->w2c_0x5Cmodule_import0x200x2A0x2F).data);
#endif
w2c_test__0(instance);
#if WASM_RT_USE_SEGUE
#if WASM_RT_USE_SEGUE && !WASM_RT_SEGUE_FREE_SEGMENT
wasm_rt_segue_write_base(segue_saved_base);
#endif
}
/* export: '*\2F' */
void w2c_test_0x2A0x2F(w2c_test* instance) {
#if WASM_RT_USE_SEGUE
#if !WASM_RT_SEGUE_FREE_SEGMENT
void* segue_saved_base = wasm_rt_segue_read_base();
#endif
wasm_rt_segue_write_base((*instance->w2c_0x5Cmodule_import0x200x2A0x2F).data);
#endif
w2c_test__0(instance);
#if WASM_RT_USE_SEGUE
#if WASM_RT_USE_SEGUE && !WASM_RT_SEGUE_FREE_SEGMENT
wasm_rt_segue_write_base(segue_saved_base);
#endif
}
/* export: '\3F\3F\2F' */
void w2c_test_0x3F0x3F0x2F(w2c_test* instance) {
#if WASM_RT_USE_SEGUE
#if !WASM_RT_SEGUE_FREE_SEGMENT
void* segue_saved_base = wasm_rt_segue_read_base();
#endif
wasm_rt_segue_write_base((*instance->w2c_0x5Cmodule_import0x200x2A0x2F).data);
#endif
w2c_test__0(instance);
#if WASM_RT_USE_SEGUE
#if WASM_RT_USE_SEGUE && !WASM_RT_SEGUE_FREE_SEGMENT
wasm_rt_segue_write_base(segue_saved_base);
#endif
}
/* export: '\0A' */
void w2c_test_0x0A(w2c_test* instance) {
#if WASM_RT_USE_SEGUE
#if !WASM_RT_SEGUE_FREE_SEGMENT
void* segue_saved_base = wasm_rt_segue_read_base();
#endif
wasm_rt_segue_write_base((*instance->w2c_0x5Cmodule_import0x200x2A0x2F).data);
#endif
w2c_test__0(instance);
#if WASM_RT_USE_SEGUE
#if WASM_RT_USE_SEGUE && !WASM_RT_SEGUE_FREE_SEGMENT
wasm_rt_segue_write_base(segue_saved_base);
#endif
}
/* export: '\E2\9D\A4\EF\B8\8F' */
void w2c_test_0xE20x9D0xA40xEF0xB80x8F(w2c_test* instance) {
#if WASM_RT_USE_SEGUE
#if !WASM_RT_SEGUE_FREE_SEGMENT
void* segue_saved_base = wasm_rt_segue_read_base();
#endif
wasm_rt_segue_write_base((*instance->w2c_0x5Cmodule_import0x200x2A0x2F).data);
#endif
w2c_test__0(instance);
#if WASM_RT_USE_SEGUE
#if WASM_RT_USE_SEGUE && !WASM_RT_SEGUE_FREE_SEGMENT
wasm_rt_segue_write_base(segue_saved_base);
#endif
}
Expand All @@ -866,10 +887,12 @@ void wasm2c_test_instantiate(w2c_test* instance, struct w2c_0x5Cmodule* w2c_0x5C
init_instance_import(instance, w2c_0x5Cmodule_instance);
init_memories(instance);
#if WASM_RT_USE_SEGUE
#if !WASM_RT_SEGUE_FREE_SEGMENT
void* segue_saved_base = wasm_rt_segue_read_base();
#endif
wasm_rt_segue_write_base((*instance->w2c_0x5Cmodule_import0x200x2A0x2F).data);
#endif
#if WASM_RT_USE_SEGUE
#if WASM_RT_USE_SEGUE && !WASM_RT_SEGUE_FREE_SEGMENT
wasm_rt_segue_write_base(segue_saved_base);
#endif
}
Expand Down
25 changes: 20 additions & 5 deletions test/wasm2c/hello.txt
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,21 @@ static inline void* wasm_rt_segue_read_base() {
}
}
static inline void wasm_rt_segue_write_base(void* base) {
if (wasm_rt_fsgsbase_inst_supported) {
__builtin_ia32_wrgsbase64((uintptr_t)base);
bool should_set = true;
#if WASM_RT_SEGUE_FREE_SEGMENT
if (wasm_rt_last_segment_val == base) {
should_set = false;
} else {
wasm_rt_syscall_set_segue_base(base);
wasm_rt_last_segment_val = base;
}
#endif
if (should_set) {
if (wasm_rt_fsgsbase_inst_supported) {
__builtin_ia32_wrgsbase64((uintptr_t)base);
} else {
wasm_rt_syscall_set_segue_base(base);
}
}
}
#define MEM_ADDR_MEMOP(mem, addr, n) ((uint8_t __seg_gs*)(uintptr_t)addr)
Expand Down Expand Up @@ -833,11 +844,13 @@ wasm_rt_memory_t* w2c_test_memory(w2c_test* instance) {
/* export: '_start' */
void w2c_test_0x5Fstart(w2c_test* instance) {
#if WASM_RT_USE_SEGUE
#if !WASM_RT_SEGUE_FREE_SEGMENT
void* segue_saved_base = wasm_rt_segue_read_base();
#endif
wasm_rt_segue_write_base(instance->w2c_memory.data);
#endif
w2c_test_0x5Fstart_0(instance);
#if WASM_RT_USE_SEGUE
#if WASM_RT_USE_SEGUE && !WASM_RT_SEGUE_FREE_SEGMENT
wasm_rt_segue_write_base(segue_saved_base);
#endif
}
Expand All @@ -852,12 +865,14 @@ void wasm2c_test_instantiate(w2c_test* instance, struct w2c_wasi__snapshot__prev
init_tables(instance);
init_memories(instance);
#if WASM_RT_USE_SEGUE
#if !WASM_RT_SEGUE_FREE_SEGMENT
void* segue_saved_base = wasm_rt_segue_read_base();
#endif
wasm_rt_segue_write_base(instance->w2c_memory.data);
#endif
init_elem_instances(instance);
init_data_instances(instance);
#if WASM_RT_USE_SEGUE
#if WASM_RT_USE_SEGUE && !WASM_RT_SEGUE_FREE_SEGMENT
wasm_rt_segue_write_base(segue_saved_base);
#endif
}
Expand Down
17 changes: 14 additions & 3 deletions test/wasm2c/minimal.txt
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,21 @@ static inline void* wasm_rt_segue_read_base() {
}
}
static inline void wasm_rt_segue_write_base(void* base) {
if (wasm_rt_fsgsbase_inst_supported) {
__builtin_ia32_wrgsbase64((uintptr_t)base);
bool should_set = true;
#if WASM_RT_SEGUE_FREE_SEGMENT
if (wasm_rt_last_segment_val == base) {
should_set = false;
} else {
wasm_rt_syscall_set_segue_base(base);
wasm_rt_last_segment_val = base;
}
#endif
if (should_set) {
if (wasm_rt_fsgsbase_inst_supported) {
__builtin_ia32_wrgsbase64((uintptr_t)base);
} else {
wasm_rt_syscall_set_segue_base(base);
}
}
}
#define MEM_ADDR_MEMOP(mem, addr, n) ((uint8_t __seg_gs*)(uintptr_t)addr)
Expand Down
17 changes: 14 additions & 3 deletions test/wasm2c/tail-calls.txt
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,21 @@ static inline void* wasm_rt_segue_read_base() {
}
}
static inline void wasm_rt_segue_write_base(void* base) {
if (wasm_rt_fsgsbase_inst_supported) {
__builtin_ia32_wrgsbase64((uintptr_t)base);
bool should_set = true;
#if WASM_RT_SEGUE_FREE_SEGMENT
if (wasm_rt_last_segment_val == base) {
should_set = false;
} else {
wasm_rt_syscall_set_segue_base(base);
wasm_rt_last_segment_val = base;
}
#endif
if (should_set) {
if (wasm_rt_fsgsbase_inst_supported) {
__builtin_ia32_wrgsbase64((uintptr_t)base);
} else {
wasm_rt_syscall_set_segue_base(base);
}
}
}
#define MEM_ADDR_MEMOP(mem, addr, n) ((uint8_t __seg_gs*)(uintptr_t)addr)
Expand Down
Loading

0 comments on commit 8f7a227

Please sign in to comment.