Skip to content

Add low-level APIs for running Emscripten programs in xterm.js - #19499

Merged
sbc100 merged 12 commits into
emscripten-core:mainfrom
mame:apis-for-xterm-pty
Jun 20, 2023
Merged

sbc100 merged 12 commits into
emscripten-core:mainfrom
mame:apis-for-xterm-pty

Conversation

@mame

@mame mame commented Jun 1, 2023

Copy link
Copy Markdown
Contributor

Background: #16352

The main purpose of this changeset is to make (some kinds of) ioctl syscall hookable:

  • ioctl(TCGETS) calls TTY.default_tty_ops.ioctl_tcgets
  • ioctl(TCSETS), ioctl(TCSETSW) and ioctl(TCSETSF) call TTY.default_tty_ops.ioctl_tcsets
  • ioctl(TCIOCGWINSZ) calls TTY.default_tty_ops.ioctl_tiocgwinsz

So we can hook the events by defining TTY.default_tty_ops.ioctl_tcgets, etc.

These hooks serve as low-level APIs to run an Emscripten CUI program on xterm.js.
Currently, xterm-pty uses very dirty hack to hook the syscall (by redefining wasmImports).
This hack is very fragile and will stop working if the Emscripten runtime changes (for example, asmLibraryArg was renamed to wasmImports).

The proposed ioctl hooks allow (relatively) clean integration between xterm-pty and the Emscripten runtime.
What do you think?

mame added 5 commits June 2, 2023 00:02
When an Emscripten program calls `ioctl(fd, TCGETS, argp)` for TTY,
`stream.tty.ops.ioctl_tcgets` is called if defined. This allows a user
to hook this event by defining `TTY.default_tty_ops.ioctl_tcgets`.

`stream.tty.ops.ioctl_tcgets` should return an object like the following
format:

```
{
  c_iflag: number,
  c_oflag: number,
  c_cflag: number,
  c_lflag: number,
  c_cc: number[32],
}
```
When an Emscripten program calls `ioctl(fd, TCSETS, argp)` for TTY,
`stream.tty.ops.ioctl_tcsets` is called if defined. This allows a user
to hook this event by defining `TTY.default_tty_ops.ioctl_tcsets`.

`TCSETSW` and `TCSETSF` are handled in the same way.

`stream.tty.ops.ioctl_tcsets` should accept the command (`""` for
`TCSETS`, `"W"` for `TCSETSW`, and `"F"` for `TCSETSF`) and a termios
object like the following format

```
{
  c_iflag: number,
  c_oflag: number,
  c_cflag: number,
  c_lflag: number,
  c_cc: number[32],
}
```

Also, `stream.tty.ops.ioctl_tcsets` should return 0 on success (or -1 on
error).
When an Emscripten program calls `ioctl(fd, TIOCGWINSZ, argp)` for TTY,
`stream.tty.ops.ioctl_tiocgwinsz` is called if defined. This allows a
user to hook this event by defining
`TTY.default_tty_ops.ioctl_tiocgwinsz`.

`stream.tty.ops.ioctl_tiocgwinsz` should return [row, col].
Previously, the timeout argument of select syscall was just ignored.

This changeset passes the argument to `poll` hook, which allows a user
to handle the argument. This is useful for
[xterm-pty](https://github.com/mame/xterm-pty/).
@mame

mame commented Jun 1, 2023

Copy link
Copy Markdown
Contributor Author

Note that this PR contains two more minor changes for xterm-pty.

  • ignore ioctl(TCFLSH) and treat it as a success
  • Passing the timeout argument of select to the poll hook.

Please let me know if you prefer to separate the PRs.

@mame

mame commented Jun 1, 2023

Copy link
Copy Markdown
Contributor Author

I have not written the test yet because I have no idea how to write it. Please let me know if there is an existing test that might be helpful to write it.

@sbc100 sbc100 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Looks great!

Is there some way we can test this here in the emscripten repo to avoid regressions?

Comment thread src/library_tty.js Outdated
ioctl_tcgets: function(tty) {
// typical setting
return {
c_iflag: 0x6500, // ICRNL | IXON | IMAXBEL | IUTF8

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Can you use {{{ cDefs.ICRNL | cDefs.IXON | cDefs.IMAXBEL | cDefs.IUTF8 }}} here instead of the hex number? Or is that harder to read?

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 at 1145e39

Comment thread src/library_syscall.js Outdated
var timeoutInMillis = -1;
if (timeout) {
var timeoutLow = (readfds ? {{{ makeGetValue('timeout', 0, 'i32') }}} : 0),
timeoutHigh = (readfds ? {{{ makeGetValue('timeout', 4, 'i32') }}} : 0);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I think you can use var timeout = {{{ makeGetValue('timeout', 4, 'i53') }}} instead of reading the hi/low pair yourself.

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.

Oh sorry, this is intentional here. The code reads the following two fields of struct timeval.

struct timeval {
  long tv_sec;   // seconds
  long tv_usec; // microseconds
};

The names timeoutLow and timeoutHigh were bad. I'll change them as timeoutSeconds and timeoutMicroseconds.

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.

I changed the variable names d8efb50

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

In that case I wonder if its worth using timeval.tv_sec and timeval.tv_usec here rather than hardcoding 0 and 4? Maybe not worth it?

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.

You are absolutely right. Why didn't I notice it? Done at 8fe4bb8

Comment thread src/library_syscall.js Outdated
case {{{ cDefs.TCSETSF }}}: {
if (!stream.tty) return -{{{ cDefs.ENOTTY }}};
if (stream.tty.ops.ioctl_tcsets) {
var optional_actions = op == {{{ cDefs.TCSETS }}} ? "" : op == {{{ cDefs.TCSETSW }}} ? "W" : "F";

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Just single quotes in JS code

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

What do these "W" and "F" strings mean?

Can you move this line down to line 258 where optional_actions is first used?

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.

It is an argument to distinguish the event types: "" stands for TCSETS, "W" does for TCSETSW, and "F" does for TCSETSF.

I am open to change the values. Do you like to pass the numbers like cDefs.TCSETS or cDefs.TCSETSW? Or pass a string like "TCSETS" or "TCSETSW"?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Maybe just stick with cDefs.TCSETS since they already exist?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Maybe just stick with cDefs.TCSETS since they already exist?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Just FYI: The reason why I did something a little different is that the constants such as cDefs.TCSETS cannot be referred from xterm-pty which is outside of Emscripten. But, the values of the constants probably won't change easily, so it's not a big deal. I think we should aim to incorporate xterm-pty into Emscripten in the end.

BYW if you write the glue code between xterm.js and emscripten as a JS library file you can access all the macros. e.g. --js-library=library_xterm.js .. can code than interpret the emscripten requests and make calls into the external xterm.js library.

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.

Yes, I would like to challenge that next.

However, I think it may be difficult to get everything working nice with just --js-library=library_xterm.js.

This is because getchar function needs to synchronously wait for user input. The default implementation of getchar waits synchornously by using prompt function, but we can't do the same for xterm.js. So, xterm-pty requires to run the Emscripten runtime in a Web Worker, and waits for user input via SharedArrayBuffer.

Therefore, emcc needs to generate two JS files, one for the main thread and one for the Web Worker. I wonder if this is beyond the scope of the --js-library=library_xterm.js option.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

We have an option called PROXY_TO_THREAD that puts the main application code in worker. When getchar is called from a worker it should be able to block.. We also have -sASYNCIFY and -sASYNCIFY=2 (A.K.A JSPI) which allow functions to suspend/block, even when running on the main thread.

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.

Great to know PROXY_TO_THREAD, thanks! I will read the code.

We also have -sASYNCIFY and -sASYNCIFY=2 (A.K.A JSPI) which allow functions to suspend/block, even when running on the main thread.

I'm not sure about this. As long as getchar is not an async function, I thought it could not wait synchronously in the main thread. Maybe I need to learn JSPI.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

-sASYNCIFY and -sASYNCIFY=2 allow the wasm module to suspend its computation and resume later. For example, after some async work is done. From the POV of the code being suspended it appears like a blocking call.

... by ./tools/gen_struct_info.py --wasm64
@mame

mame commented Jun 1, 2023

Copy link
Copy Markdown
Contributor Author

Looks great!

Thank you!

Is there some way we can test this here in the emscripten repo to avoid regressions?

Sure, I guess the last additional commit should fix the issue.

(It may be good to write "run . /tools/gen_struct_info.py --wasm64" in this section of the documentation.)

mame added a commit to mame/emscripten that referenced this pull request Jun 2, 2023
I needed to run not only `./tools/gen_struct_info.py` but also
`./tools/gen_struct_info.py --wasm64`.

emscripten-core#19499 (comment)
mame added a commit to mame/emscripten that referenced this pull request Jun 2, 2023
I needed to run not only `./tools/gen_struct_info.py` but also
`./tools/gen_struct_info.py --wasm64`.

emscripten-core#19499 (comment)
@mame

mame commented Jun 2, 2023

Copy link
Copy Markdown
Contributor Author

@sbc100 Thank you, I think I've addressed all your review comments and the CI has passed. Do you want me to tidy up the commits by rebase? Please feel free to let me know.

@sbc100

sbc100 commented Jun 2, 2023

Copy link
Copy Markdown
Collaborator

In terms of testing, perhaps you could modify test/other/test_ioctl.c?

sbc100 pushed a commit that referenced this pull request Jun 2, 2023
I needed to run not only `./tools/gen_struct_info.py` but also
`./tools/gen_struct_info.py --wasm64`.

#19499 (comment)
mame added 2 commits June 3, 2023 03:20
... because ioctl syscall is not well implemented yet on wasmfs
@mame

mame commented Jun 3, 2023

Copy link
Copy Markdown
Contributor Author

In terms of testing, perhaps you could modify test/other/test_ioctl.c?

Thanks! I have added test/other/test_ioctl_termios.c.

@sbc100
sbc100 enabled auto-merge (squash) June 3, 2023 00:43
auto-merge was automatically disabled June 3, 2023 02:15

Head branch was pushed to by a user without write access

@mame
mame force-pushed the apis-for-xterm-pty branch from 1a83838 to efa91ab Compare June 3, 2023 02:23
@appden

appden commented Jun 16, 2023

Copy link
Copy Markdown

Excited to see this land. Looks like this was ready to auto-merge then the force push cancelled it.

@sbc100
sbc100 merged commit fe0db79 into emscripten-core:main Jun 20, 2023
@mame
mame deleted the apis-for-xterm-pty branch June 20, 2023 03:42
@RReverser RReverser mentioned this pull request Oct 16, 2023
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