Skip to content

Fix signedness/type mismatches when marshaling stat-related structs #24772

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Aug 1, 2025

Conversation

kleisauke
Copy link
Collaborator

No description provided.

@sbc100
Copy link
Collaborator

sbc100 commented Jul 23, 2025

lgtm, but I'm fine with #24769 landing first too.

@@ -60,20 +60,20 @@ var SyscallsLibrary = {
{{{ makeSetValue('buf', C_STRUCTS.stat.st_mtim.tv_nsec, '(mtime % 1000) * 1000 * 1000', SIZE_TYPE) }}};
{{{ makeSetValue('buf', C_STRUCTS.stat.st_ctim.tv_sec, 'Math.floor(ctime / 1000)', 'i64') }}};
{{{ makeSetValue('buf', C_STRUCTS.stat.st_ctim.tv_nsec, '(ctime % 1000) * 1000 * 1000', SIZE_TYPE) }}};
{{{ makeSetValue('buf', C_STRUCTS.stat.st_ino, 'stat.ino', 'i64') }}};
{{{ makeSetValue('buf', C_STRUCTS.stat.st_ino, 'stat.ino', 'u64') }}};
Copy link
Collaborator

Choose a reason for hiding this comment

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

IIRC the signed-ness of the type only really matter for makeGetValue.. is that right? i.e. its when reading the bits that its important to interpret them.

Supporting evidence for this is that the makeSetValue code doesn't even have code paths for handling u64 and u53 here:

if (type == 'i64' && !WASM_BIGINT) {
// If we lack BigInt support we must fall back to an reading a pair of I32
// values.
// prettier-ignore
return '(tempI64 = [' + splitI64(value) + '], ' +
makeSetValueImpl(ptr, pos, 'tempI64[0]', 'i32') + ',' +
makeSetValueImpl(ptr, getFastValue(pos, '+', getNativeTypeSize('i32')), 'tempI64[1]', 'i32') + ')';
}
const offset = calcFastOffset(ptr, pos);
if (type === 'i53') {
return `writeI53ToI64(${offset}, ${value})`;
}

I think maybe just always use the signed array views works for setting values?

I'm not sure what happens if we write a negative number to an unsigned typed array, but I guess its undefined behaviour from our POV and we should assert in debug build.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

According to the failing tests, there's seems to be indeed support missing for -sWASM_BIGINT=0 and makeSetValue(x, y, z, 'u64'). Commit 5511235 reverts that part.

I'm not sure if this signedness mismatch only affects makeGetValue(), since getHeapForType() is also used in makeSetValue() (e.g. HEAP32 versus HEAPU32).

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm not sure if this signedness mismatch only affects makeGetValue(), since getHeapForType() is also used in makeSetValue() (e.g. HEAP32 versus HEAPU32).

I'm not sure either, but I would say that seems like signedness is critical when reading values (since the bytes need to be interpreted), but maybe not critical when writing them.

This is an automatic change generated by tools/maint/rebaseline_tests.py.

The following (1) test expectation files were updated by
running the tests with `--rebaseline`:

```
code_size/test_codesize_hello_dylink.json: 45583 => 45582 [-1 bytes / -0.00%]

Average change: -0.00% (-0.00% - -0.00%)
```
This is an automatic change generated by tools/maint/rebaseline_tests.py.

The following (3) test expectation files were updated by
running the tests with `--rebaseline`:

```
code_size/test_codesize_cxx_wasmfs.json: 176935 => 176938 [+3 bytes / +0.00%]
code_size/test_codesize_files_wasmfs.json: 55778 => 55781 [+3 bytes / +0.01%]
code_size/test_codesize_hello_dylink_all.json: 844699 => 844697 [-2 bytes / -0.00%]

Average change: +0.00% (-0.00% - +0.01%)
```
@kleisauke
Copy link
Collaborator Author

kleisauke commented Jul 31, 2025

The codesize tests appear to be quite sensitive, especially other.test_codesize_hello_dylink_all. IIRC, we allowed some slop/slack factor in the past, but it seems that's no longer configurable.

@sbc100
Copy link
Collaborator

sbc100 commented Jul 31, 2025

Yes, I apologize for that. I'm not sure if we should remove that test again.. maybe? Or we should have it auto-update as part of the review process.

@kleisauke
Copy link
Collaborator Author

I'm not certain what the ideal solution is, but one possibility is to create a bot that automatically updates test expectations whenever a specific label (perhaps reuse code size or introduce "needs-rebaseline"?) is applied to a PR. Once the bot completes the update, it would remove the label.

Using labels helps prevent potential abuse, since applying them requires triage access or higher (this idea is also somewhat inspired by LLVM's convenient /cherry-pick command, which also requires proper permissions, such as the ability to edit the milestone).

@kleisauke
Copy link
Collaborator Author

... ah, I forgot that this only works when the "Allow edits by maintainers" option is enabled, though I believe it's enabled by default.

@sbc100
Copy link
Collaborator

sbc100 commented Jul 31, 2025

I'm not certain what the ideal solution is, but one possibility is to create a bot that automatically updates test expectations whenever a specific label (perhaps reuse code size or introduce "needs-rebaseline"?) is applied to a PR. Once the bot completes the update, it would remove the label.

Using labels helps prevent potential abuse, since applying them requires triage access or higher (this idea is also somewhat inspired by LLVM's convenient /cherry-pick command, which also requires proper permissions, such as the ability to edit the milestone).

Yes, we already have an action that will create rebasline CLs for main: https://github.com/emscripten-core/emscripten/actions/workflows/rebaseline-tests.yml

Figuring out how to make this work PRs in flight is what I'd like to do.

I've not get written a bot that responds to labels of commands but it seems possible.

@sbc100 sbc100 merged commit 14da0a2 into emscripten-core:main Aug 1, 2025
28 of 30 checks passed
@kleisauke kleisauke deleted the stat-syscall-correct-types branch August 1, 2025 18:44
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.

2 participants