Skip to content

Commit

Permalink
Merge pull request #22 from extism/fix/mono-initialize
Browse files Browse the repository at this point in the history
fix: ensure mono is only initialized once
  • Loading branch information
mhmd-azeez authored Oct 16, 2023
2 parents 5bdb03f + 386b1f9 commit df3dc6e
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 13 deletions.
6 changes: 0 additions & 6 deletions src/Extism.Pdk.MSBuild/FFIGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,8 @@ private FileEntry GenerateExports(string assemblyFileName, MethodDefinition[] ex
WASI_AFTER_RUNTIME_LOADED_DECLARATIONS
#endif

bool mono_runtime_initialized = false;
void initialize_runtime() {
if (mono_runtime_initialized) {
return;
}

mono_wasm_load_runtime("", 0);
mono_runtime_initialized = true;
}

// end of _initialize
Expand Down
2 changes: 1 addition & 1 deletion src/Extism.Pdk/Extism.Pdk.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

<PropertyGroup>
<PackageId>Extism.Pdk</PackageId>
<Version>1.0.0-rc2</Version>
<Version>1.0.0-rc3</Version>
<Authors>Extism Contributors</Authors>
<Description>Extism PDK that allows compiling .NET apps into wasm Extism plugins.</Description>
<Tags>extism, wasm, plugin</Tags>
Expand Down
3 changes: 3 additions & 0 deletions src/Extism.Pdk/build/Extism.Pdk.targets
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
<!-- Wrap mono_runtime_run_main because we have to make sure at least one argument is passed in to Mono -->
<!-- See native/env.c for the implementation -->
<_WasiSdkClangArgs Include="-Wl,--wrap=mono_runtime_run_main" />

<!-- Wrap mono_wasm_load_runtime to ensure mono is loaded only once when _start is called after an exported function -->
<_WasiSdkClangArgs Include="-Wl,--wrap=mono_wasm_load_runtime" />
</ItemGroup>
</Target>
</Project>
13 changes: 13 additions & 0 deletions src/Extism.Pdk/native/env.c
Original file line number Diff line number Diff line change
Expand Up @@ -232,4 +232,17 @@ int __wrap_mono_runtime_run_main(MonoMethod *method, int argc, char *argv[], Mon
}

return __real_mono_runtime_run_main(method, argc, argv, exc);
}

// Wrap mono_wasm_load_runtime to make sure we don't initialize mono more than once
void __real_mono_wasm_load_runtime(const char* unused, int debug_level);

bool mono_runtime_initialized = false;
void __wrap_mono_wasm_load_runtime(const char* unused, int debug_level) {
if (mono_runtime_initialized) {
return;
}

__real_mono_wasm_load_runtime(unused, debug_level);
mono_runtime_initialized = true;
}
6 changes: 0 additions & 6 deletions tests/Extism.Pdk.MsBuild.Tests/snapshots/exports.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,8 @@ void mono_wasm_load_runtime(const char* unused, int debug_level);
WASI_AFTER_RUNTIME_LOADED_DECLARATIONS
#endif

bool mono_runtime_initialized = false;
void initialize_runtime() {
if (mono_runtime_initialized) {
return;
}

mono_wasm_load_runtime("", 0);
mono_runtime_initialized = true;
}

// end of _initialize
Expand Down

0 comments on commit df3dc6e

Please sign in to comment.