Skip to content

[wasm2c] Write float constants independent of the host locale - #2768

Merged
sbc100 merged 1 commit into
WebAssembly:mainfrom
aizu-m:wasm2c-float-locale
Jun 24, 2026
Merged

sbc100 merged 1 commit into
WebAssembly:mainfrom
aizu-m:wasm2c-float-locale

Conversation

@aizu-m

@aizu-m aizu-m commented Jun 22, 2026

Copy link
Copy Markdown
Contributor

Noticed while running wasm2c with the embedder's locale set to de_DE, where LC_NUMERIC makes the radix ','. The constant writer produced:

instance->w2c_g0 = 1,5;

snprintf("%.9g"/"%.17g") in CWriter::Write(const Const&) takes the radix from the active locale, but the emitted C must use '.'. The comma is either a compile error or, in argument/initialiser position, a comma expression that quietly drops the fractional part. The f64 path is worse: the following strchr(buf, '.') no longer finds the radix, so it also appends ".0" and writes 1,5.0.

The .wat writer sidesteps this by emitting hex floats, and the parsing direction was already made locale-independent in literal.cc. This is the output side. The fix formats as before, then rewrites the locale's radix back to '.', so the generated C is byte-identical regardless of the locale the caller set. Output under the C locale is unchanged.

Added a unit test that writes a module with f32/f64 constants under a comma locale and checks the result uses '.'. It skips when no comma locale is installed, matching the existing parser test.

Comment thread src/c-writer.cc
} else {
Writef("%.9g", Bitcast<float>(f32_bits));
char buf[128];
snprintf(buf, sizeof(buf), "%.9g", Bitcast<float>(f32_bits));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we instead use sprintf_l like we already use the _l flavors of other libc functions?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Switched to the c_locale approach from literal.cc. One snag: glibc and musl only ship the strto*_l family, not the printf side, so there's no snprintf_l/sprintf_l there. On POSIX I format through the same C locale_t via uselocale; Windows uses _snprintf_l. That drops the radix-rewriting helper and keeps the C-locale output byte-identical.

@aizu-m
aizu-m force-pushed the wasm2c-float-locale branch from 5a5bb75 to ccba3a8 Compare June 22, 2026 17:28
@keithw

keithw commented Jun 22, 2026

Copy link
Copy Markdown
Member

No objection to this, but wondering if users might prefer to do #2389 . The benefit is we really don't have to think about floating-point text representations at all, but the downside is it makes the generated C code a little less comprehensible (which is why we never merged it the first time). @aizu-m what do you think?

Comment thread src/c-writer.cc Outdated
#else
locale_t prev = uselocale(c_locale);
int result = snprintf(buf, size, format, value);
uselocale(prev);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think sadly this is not thread safe, right?

So if we can't have snprintf_l then maybe we have to do with the original approach.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. uselocale is per-thread so it wouldn't race other threads, but it still swaps thread state, and glibc/musl don't ship the _l printf side anyway. Reverted to the NormalizeFloatRadix version: format with snprintf, then rewrite localeconv()->decimal_point back to '.'. No locale state touched, and the C-locale output stays byte-identical. Test still passes under de_DE.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, TIL that uselocale is per-thread.

@aizu-m
aizu-m force-pushed the wasm2c-float-locale branch from ccba3a8 to 385c41a Compare June 23, 2026 07:27
@aizu-m

aizu-m commented Jun 23, 2026

Copy link
Copy Markdown
Contributor Author

@keithw my vote is to land this as the targeted fix. It's a few lines and the emitted constants stay readable. #2389 is the more complete answer since it drops decimal text entirely, but it bloats the output and reads less clearly, which is why it stalled last time. The two don't really conflict: this stops the comma-locale miscompile now, and #2389 can still land later on its own merits if the readability tradeoff is acceptable. Happy to follow your call though.

Comment thread src/test-c-writer.cc Outdated
@@ -0,0 +1,103 @@
/*
* Copyright 2024 WebAssembly Community Group participants

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, bumped to 2026.

Comment thread src/c-writer.cc

#include <cctype>
#include <cinttypes>
#include <clocale>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe not needed anymore?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still needed, I'm afraid. NormalizeFloatRadix calls localeconv(), which is declared in , so I left the include in.

snprintf renders the radix character according to LC_NUMERIC, so under a comma-decimal locale CWriter::Write emitted constants like 100,125: invalid C, or silently a comma expression with a different value. Normalise the radix produced by %g back to '.' so the generated C does not depend on the locale the embedder set.
@aizu-m
aizu-m force-pushed the wasm2c-float-locale branch from 385c41a to a03af2f Compare June 24, 2026 05:50
@sbc100

sbc100 commented Jun 24, 2026

Copy link
Copy Markdown
Member

Landing this for now. We can always land #2389 later if we decide that is the way to go.

@sbc100
sbc100 merged commit 5d0a3a7 into WebAssembly:main Jun 24, 2026
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants