Skip to content

Commit 6caab30

Browse files
lewingCopilot
andauthored
[wasi] Pass runtimeconfig properties to the runtime in wasihost (#134964)
The CoreCLR WASI host (`wasihost`) initializes the runtime with only `TRUSTED_PLATFORM_ASSEMBLIES`, `APP_PATHS` and the host runtime contract. It never reads the app's runtime configuration, so none of the `configProperties` from `<app>.runtimeconfig.json` reach `AppContext`: feature switches, `RuntimeHostConfigurationOption` items and so on. For example, `System.Text.Encoding.EnableUnsafeUTF7Encoding` never arrives, so UTF-7 tests throw `NotSupportedException : Support for UTF-7 is disabled`. When trimming, `System.Data.DataSet.XmlSerializationIsSupported` is seen as missing by test `PlatformDetection`. ## Change - `src/native/corehost/wasihost/wasihost.cpp`: read `runtimeconfig.bin` from next to the entry assembly and append its properties to the init properties passed to `coreclr_initialize`. Properties the host sets itself take precedence. `get_runtime_property` already serves from the same vectors, so the contract callback returns the same values. A missing file is ignored, and a malformed one fails startup with a message. - `src/mono/wasi/build/WasiApp.CoreCLR.targets`: copy `runtimeconfig.bin` into `managed/`, next to the entry assembly, the framework, `icudt.dat` and the host. `WasiAppBuilder` places it at the bundle root, which `run-wasmtime.sh` (`cd managed && wasmtime --dir . ...`) does not preopen. `runtimeconfig.bin` is the file `RuntimeConfigParserTask` already produces for WASI apps, and Mono's WASI driver consumes the same file. Its format is an ECMA-335 compressed count followed by key/value serialized strings, so the host needs no JSON parser. The browser CoreCLR host follows the same model: `configProperties` first, with host-set properties overriding them. ## Validation Local, macOS arm64 host, wasi-sdk 33, wasmtime 45. - `./build.sh -os wasi -arch wasm -rf CoreCLR -s clr+libs+host -c Release` on clean upstream `main` → succeeded, 0 warnings, 0 errors. - `./build.sh -os wasi -arch wasm -rf CoreCLR -s host.native -c Release` with the change → succeeded; `wasihost.cpp` compiles with no warnings. **System.Text.Encoding.Tests** (untrimmed, `./dotnet.sh build src/libraries/System.Runtime/tests/System.Text.Encoding.Tests/System.Text.Encoding.Tests.csproj -f net11.0 /t:Test /p:TargetOS=wasi /p:TargetArchitecture=wasm /p:RuntimeFlavor=CoreCLR /p:Configuration=Release`) On `main`, the full suite hangs in `TranscodingStreamTests.ReadAsync_LoopsWhenPartialDataReceived`, a blocking wait on single-threaded WASI that is handled in #134813. So I compared the UTF-7-related classes directly with wasmtime on the bundle (`-class` for EncodingMiscTests, EncodingGetEncodingTest, NegativeEncodingTests and the seven `UTF7Encoding*` classes): | | Tests run | Passed | Failed | |---|---|---|---| | Without fix | 1692 | 1654 | **38** (all `Support for UTF-7 is disabled`) | | With fix | 1720 | 1720 | **0** | Full suite with the fix, with the hanging test skipped locally only (not part of this PR): 14614 run, 14605 passed, 4 failed, 5 skipped, 0 UTF-7 failures. The 4 failures are unrelated: `FromUtf16Tests`/`ToUtf16Tests.SomeNonAsciiInput` need System.Security.Cryptography (#99126), and `TranscodingStreamTests.ReadApm`/`WriteApm` throw `PlatformNotSupportedException` from `TaskToAsyncResult.Begin`. **System.Data.Common.Tests** (untrimmed, same command with the System.Data.Common.Tests project) - With fix: 11734 run, 11658 passed, 67 failed, 9 skipped. The same bundle run with `managed/runtimeconfig.bin` removed, which reproduces the pre-fix behavior, gives the identical 67 failures. So there are no regressions. 54 of the failures are `IOException : Success : '/tmp/'` (#134958), and the rest are DbProviderFactories and serialization-guard failures unrelated to runtimeconfig. - The DataSet switch only lands in runtimeconfig when trimming, and trimmed CoreCLR-WASI test builds don't work on `main` yet (ILLink can't resolve System.Private.CoreLib; #134813 sets that up). To confirm the switch now reaches both the product and `PlatformDetection`, I added `System.Data.DataSet.XmlSerializationIsSupported=false` to the bundle's `runtimeconfig.bin` and ran `-class System.Data.Tests.DataSetTest -class System.Data.Tests.DataTableTest5`. The original config gave 63 run, 48 passed, 14 failed (`/tmp`), 1 skipped. With the switch it gave 63 run, 44 passed, 0 failed, 19 skipped: the guarded tests skip and nothing fails from a product/test mismatch. This is split out from the WASI R2R test-standup work in #134813, where the missing properties account for the 38 UTF-7 failures and about 360 System.Data.Common failures in trimmed runs. Resolves #134954 > [!NOTE] > This PR was authored with assistance from GitHub Copilot. Co-authored-by: Copilot App <[email protected]>
1 parent fcaee07 commit 6caab30

2 files changed

Lines changed: 117 additions & 0 deletions

File tree

‎src/mono/wasi/build/WasiApp.CoreCLR.targets‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,12 @@
217217
SourceFiles="$(MicrosoftNetCoreAppRuntimePackRidNativeDir)icudt.dat"
218218
DestinationFolder="$(WasmAppDir)managed"
219219
SkipUnchangedFiles="true" />
220+
<!-- The wasihost corehost reads the app's runtimeconfig properties from runtimeconfig.bin
221+
next to the entry assembly and passes them to the runtime. -->
222+
<Copy Condition="'$(_ParsedRuntimeConfigFilePath)' != '' and Exists('$(_ParsedRuntimeConfigFilePath)')"
223+
SourceFiles="$(_ParsedRuntimeConfigFilePath)"
224+
DestinationFolder="$(WasmAppDir)managed"
225+
SkipUnchangedFiles="true" />
220226
</Target>
221227

222228
<!-- ============================ Per-app wasihost link ============================ -->

‎src/native/corehost/wasihost/wasihost.cpp‎

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
// real wasi:cli/run main() instead of a JS driver.
77
// See https://github.com/dotnet/runtime/issues/130129.
88

9+
#include <algorithm>
910
#include <cstdint>
1011
#include <cstdio>
1112
#include <cstdlib>
@@ -158,6 +159,110 @@ static string_t build_tpa(const string_t& core_root, const string_t& core_librar
158159
return tpa_list.str();
159160
}
160161

162+
// ECMA-335 compressed unsigned integer (II.23.2), as written by BlobBuilder.WriteCompressedInteger.
163+
static bool read_compressed_uint(const uint8_t*& cur, const uint8_t* end, uint32_t& value)
164+
{
165+
if (cur >= end)
166+
return false;
167+
168+
uint8_t b = cur[0];
169+
if ((b & 0x80) == 0)
170+
{
171+
value = b;
172+
cur += 1;
173+
return true;
174+
}
175+
176+
if ((b & 0xC0) == 0x80)
177+
{
178+
if (end - cur < 2)
179+
return false;
180+
181+
value = (static_cast<uint32_t>(b & 0x3F) << 8) | cur[1];
182+
cur += 2;
183+
return true;
184+
}
185+
186+
if ((b & 0xE0) == 0xC0)
187+
{
188+
if (end - cur < 4)
189+
return false;
190+
191+
value = (static_cast<uint32_t>(b & 0x1F) << 24) | (static_cast<uint32_t>(cur[1]) << 16) | (static_cast<uint32_t>(cur[2]) << 8) | cur[3];
192+
cur += 4;
193+
return true;
194+
}
195+
196+
return false;
197+
}
198+
199+
// Length-prefixed UTF-8 string, as written by BlobBuilder.WriteSerializedString.
200+
static bool read_serialized_string(const uint8_t*& cur, const uint8_t* end, std::string& value)
201+
{
202+
uint32_t len;
203+
if (!read_compressed_uint(cur, end, len) || static_cast<size_t>(end - cur) < len)
204+
return false;
205+
206+
value.assign(reinterpret_cast<const char*>(cur), len);
207+
cur += len;
208+
return true;
209+
}
210+
211+
// Appends the app's runtimeconfig configProperties (feature switches, RuntimeHostConfigurationOption
212+
// items, ...) to the init properties. Reads the runtimeconfig.bin that RuntimeConfigParserTask writes
213+
// (the same file Mono's WASI driver consumes): a compressed count followed by that many key/value
214+
// serialized strings. Properties already set by the host take precedence. A missing file is not an
215+
// error.
216+
static bool add_runtimeconfig_properties(const string_t& path)
217+
{
218+
FILE* file = std::fopen(path.c_str(), "rb");
219+
if (file == nullptr)
220+
return true;
221+
222+
std::vector<uint8_t> data;
223+
uint8_t buffer[4096];
224+
size_t read;
225+
while ((read = std::fread(buffer, 1, sizeof(buffer), file)) > 0)
226+
data.insert(data.end(), buffer, buffer + read);
227+
228+
bool read_error = std::ferror(file) != 0;
229+
std::fclose(file);
230+
231+
const uint8_t* cur = data.data();
232+
const uint8_t* end = cur + data.size();
233+
uint32_t count = 0;
234+
bool valid = !read_error && read_compressed_uint(cur, end, count);
235+
std::vector<std::string> keys;
236+
std::vector<std::string> values;
237+
for (uint32_t i = 0; valid && i < count; ++i)
238+
{
239+
std::string key;
240+
std::string value;
241+
valid = read_serialized_string(cur, end, key) && read_serialized_string(cur, end, value);
242+
keys.push_back(std::move(key));
243+
values.push_back(std::move(value));
244+
}
245+
246+
if (!valid)
247+
{
248+
std::fprintf(stderr, "Failed to read runtime configuration from '%s'\n", path.c_str());
249+
return false;
250+
}
251+
252+
size_t host_property_count = s_property_keys.size();
253+
for (size_t i = 0; i < keys.size(); ++i)
254+
{
255+
auto host_keys_end = s_property_keys.begin() + host_property_count;
256+
if (std::find(s_property_keys.begin(), host_keys_end, keys[i]) != host_keys_end)
257+
continue;
258+
259+
s_property_keys.push_back(std::move(keys[i]));
260+
s_property_values.push_back(std::move(values[i]));
261+
}
262+
263+
return true;
264+
}
265+
161266
static size_t HOST_CONTRACT_CALLTYPE get_runtime_property(
162267
const char* key,
163268
/*out*/ char* value_buffer,
@@ -248,6 +353,12 @@ int main(int argc, char* argv[])
248353
s_property_values.push_back(ss.str());
249354
}
250355

356+
// The app bundle places runtimeconfig.bin next to the entry assembly.
357+
string_t runtimeconfig_path = app_path;
358+
runtimeconfig_path.append(W("runtimeconfig.bin"));
359+
if (!add_runtimeconfig_properties(runtimeconfig_path))
360+
return -1;
361+
251362
std::vector<const char*> property_keys;
252363
std::vector<const char*> property_values;
253364
for (const std::string& key : s_property_keys)

0 commit comments

Comments
 (0)