Skip to content

feat(ses): Shim compatible with Hermes compiler#2334

Merged
leotm merged 13 commits into
endojs:masterfrom
leotm:ses-hermes
Mar 5, 2025
Merged

feat(ses): Shim compatible with Hermes compiler#2334
leotm merged 13 commits into
endojs:masterfrom
leotm:ses-hermes

Conversation

@leotm

@leotm leotm commented Jun 25, 2024

Copy link
Copy Markdown
Contributor

Description

Add lockdown shim compatible with Hermes
Make current shim compatible with Hermes compiler
for building React Native (RN) prod apps with SES

Generating the release AAB (Android App Bundle)
i.e. npx react-native build-android --mode=release via RN CLI
calls Gradle's bundleRelease task under the hood (bundle build task on release variant)
which calls RNGP (React Native Gradle Plugin) React task createBundleReleaseJsAndAssets and fails
after Metro finishes writing the release bundle (bundle, sourcemaps, assets)

Gradle emits these Hermes errors

(at runtime we can see both are SyntaxErrors)
Resulting in vague java.lang.StackOverflowError (no error message)

The try/catch approach testing language ft support via a new fn works
since RNGP no longer emits the Hermes errors in the task after Metro bundles
and we're conditionally using parts of SES compatible with the Hermes engine

The initial approach involved building a new shim via an env var
which involved a lot of duplicates /src files

Nb: clean before bundling to see changes reflected (avoid cache)
i.e. ./gradlew clean :app:bundleRelease

Nb: async function* a() {}; alone won't emit an error
but using/referencing it and beyond const b = a; will

Nb: eventually we hit RNGP BundleHermesCTask.kt > run > detectedHermesCommand > detectOSAwareHermesCommand from PathUtils.kt, which calls the Hermes compiler default command hermesc - the path of the binary file, to create the optimised bytecode bundle to load/exec at runtime

TODO

  • use standard async functions without arrows (module-load.js)
  • remove async generators from whitelist (permits.js)
  • remove async generators from intrinsics (get-anonymous-intrinsics-hermes.js)
  • remove repairing async generators during taming (tame-function-constructors.js)
  • remove async generator tests (ses, harden, tame-function-unit)
  • ensure 379 ses tests passing
  • test new shim in React Native repro
    • test android bundle via gradle: ./gradlew :app:createBundleReleaseJsAndAssets
    • test android build/runtime via gradle: ./gradlew :app:installRelease -PreactNativeArchitectures=arm64-v8a
    • test android/ios build/runtime via RN CLI: yarn <android/ios> --mode release
  • add new entry points to hook into get-anonymous-intrinsics-hermes.js
    • original: bundle.js > index.js > lockdown-shim.js > lockdown.js
    • hermes: bundle.js > index-hermes.js > lockdown-shim-hermes.js > lockdown-hermes.js
  • support env var to bundle new ses/lockdown shim bundle/terse variants (bundle.js)
  • bundle new shims in prepare npm lifecycle script (package.json)
  • add new shim exports entry points (package.json)
  • conditionally get intrinsics (get-anonymous-intrinsics.js)
  • conditionally repair async generators (tame-function-constructors.js)
  • conditionally add async generators to whitelist (permits.js)
  • revert new publishing changes
  • revert new entry points
  • revert new bundling with env var changes
  • ensure 379 ses tests still passing
  • restore original tests
  • suggested change
  • revert permits.js changes
  • asyncTrampoline non-arrow async function
  • console info msg x2
  • improved catch block
    • do nothing only on the SyntaxError
    • otherwise re-throw
  • add to commons.js and use
    • AsyncGeneratorFunctionInstance
    • AsyncArrowFunctionInstance?
  • CI (new PR, stacked)

Follow-up, CI testing options discussed

  • ubuntu: RN app + SES, cd android && ./gradlew :app:bundleRelease
    • add Java CI docs if included
  • react-native-community/docker-android is available to use
  • CI(macos): RN app test + SES, Xcode release
    • not needed, Xcode does not emit Hermes errors like Gradle
  • test262:hermes script (like test262:xs)
  • Hermes maintains test262 tests (testsuite.py, testsuite_skiplist.py)
  • (eshost) > Hermes CLI v0.12.0 (few years old, newer being discussed)
  • or build and run Hermes (and/or Static Hermes) as a standalone compiler and VM
    • add docs or at least link to Hermes docs

Security Considerations

Does this change introduce new assumptions or dependencies that, if violated, could introduce security vulnerabilities? How does this PR change the boundaries between mutually-suspicious components? What new authorities are introduced by this change, perhaps by new API calls?

Scaling Considerations

Does this change require or encourage significant increase in consumption of CPU cycles, RAM, on-chain storage, message exchanges, or other scarce resources? If so, can that be prevented or mitigated?

Documentation Considerations

Give our docs folks some hints about what needs to be described to downstream users. Backwards compatibility: what happens to existing data or deployments when this code is shipped? Do we need to instruct users to do something to upgrade their saved data? If there is no upgrade path possible, how bad will that be for users?

Testing Considerations

Every PR should of course come with tests of its own functionality. What additional tests are still needed beyond those unit tests? How does this affect CI, other test automation, or the testnet?

Compatibility Considerations

Does this change break any prior usage patterns? Does this change allow usage patterns to evolve?

Upgrade Considerations

What aspects of this PR are relevant to upgrading live production systems, and how should they be addressed?

Include *BREAKING*: in the commit message with migration instructions for any breaking change.

Update NEWS.md for user-facing changes.

Delete guidance from pull request description before merge (including this!)

Comment thread packages/ses/src/permits.js Outdated
Comment thread packages/ses/src/tame-function-constructors.js Outdated
Comment thread packages/ses/index-hermes.js Outdated
Comment thread packages/ses/lockdown-hermes.js Outdated
Comment thread packages/ses/package.json
Comment thread packages/ses/scripts/bundle.js
Comment thread packages/ses/src/lockdown-hermes.js Outdated
Comment thread packages/ses/src/lockdown-shim-hermes.js Outdated
Comment thread packages/ses/src/module-load.js Outdated
Comment thread packages/ses/src/module-load.js Outdated
Comment thread packages/ses/src/permits.js Outdated
Comment thread packages/ses/src/tame-function-constructors.js Outdated
Comment thread packages/ses/src/get-anonymous-intrinsics.js Outdated
Comment thread packages/ses/src/tame-function-constructors.js Outdated
Comment thread packages/ses/src/permits.js Outdated
Comment thread packages/ses/src/permits.js Outdated
Comment thread packages/ses/src/permits.js Outdated
@leotm leotm marked this pull request as ready for review July 3, 2024 12:11
@leotm leotm marked this pull request as draft July 3, 2024 12:49
@leotm leotm changed the title feat(ses): Hermes compatible shim variant feat(ses): Shim compatible with Hermes Jul 3, 2024
@leotm leotm marked this pull request as ready for review July 10, 2024 11:56
@leotm

leotm commented Jul 10, 2024

Copy link
Copy Markdown
Contributor Author

ready for review ^ tackling the testing strategy separately in a follow-up PR, to keep this change small

@kriskowal kriskowal left a comment

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.

This is indeed far more surgical than I expected.

I only request that we capture the async generator function instance in a single try{eval}catch in commons.js so we can just use if blocks to decide whether to include async generators in the various points you’re currently using try/catch.

I would like to make sure @erights looks at these changes as well.

Comment thread packages/ses/src/get-anonymous-intrinsics.js Outdated
Comment thread packages/ses/src/get-anonymous-intrinsics.js Outdated
Comment thread packages/ses/src/tame-function-constructors.js Outdated
@kriskowal

Copy link
Copy Markdown
Member

ready for review ^ tackling the testing strategy separately in a follow-up PR, to keep this change small

Are you familiar with stacked PRs? You can propose the test changes in a PR based on this branch so they can be reviewed together. I would hesitate to approve code that doesn’t come with tests in the same merge, though I’m fine with reviewing them separately. Much depends on your workflow.

One workflow that I like is individually reviewable commits in a single PR. That does require more commit grooming, and it looks like you intend to squash the 21 commits here when you merge.

Another workflow is to create “stacked” PRs for each review artifact, then merge them top to bottom, so that ultimately the feature and its tests arrive in master with a single merge commit.

@erights

erights commented Jul 10, 2024

Copy link
Copy Markdown
Contributor

Glad to see this!

I would like to make sure @erights looks at these changes as well.

Hopefully tomorrow.

@erights erights self-requested a review July 14, 2024 08:40
Comment thread packages/ses/src/module-load.js Outdated

@kriskowal kriskowal left a comment

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’m poised to approve these changes. The functional changes look good, with some polish desired.

I verified the Hermes test locally. It’s a great start.

Blocking: Please choose lexical compartment instead of this. I disprefer functionBind used on an arrow function as that is confusing and I believe unnecessary. Do let me know if there’s more to that.

Blocking: Please clear up the TODOs in the test. Those should either be DONETO or further work captured as a link to an issue.

Blocking: Please use the new reporters for logging SES Skipping async generators. The reporter is configurable and can paper over whether console or print capabilities are present in the host environment.

Not blocking: I would like a SES-specific lint rule, like the we have for @endo/polymorphic*, so that we get a lint error if we use this inside an arrow function, since evidently that will break the Hermes version.

Comment thread packages/ses/test/_hermes-smoke.js Outdated
Comment thread packages/ses/test/_hermes-smoke.js Outdated
Comment thread packages/ses/test/_hermes-smoke.js Outdated
Comment thread packages/ses/test/_hermes-smoke.js Outdated
Comment thread packages/ses/src/compartment.js Outdated
const { execute, exportsProxy } = link(
privateFields,
moduleAliases,
compartment,

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 see that this changes this to the lexical compartment. That seems to obviate the functionBind. But also, async arrow functions do not rebind this. Is this change needed because of a the shift in semantics caused by the transformation? Do we need both accommodations or just one?

My preference would be to just use the compartment named in lexical scope since that doesn’t require the async function transform for explanation.

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.

the change is needed since the current hermes transform only replaces the async arrow fn with a fn expression with babel (without modifying this)

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.

without functionBind our 4 (compartment-mapper) optionalDependencies/esm tests fail
(@naugtur do you remember the exact reason)

compartment-mapper
  optional › optionalDependencies/esm / loadLocation

  test/optional.test.js:25

   24:     const result = await tryOptionalDeps();
   25:     t.deepEqual(
   26:       Reflect.ownKeys(result.exports),

  expected exports

  Difference (- actual, + expected):

    [
  +   'alpha',
  +   'beta',
    ]

  › scaffold.knownArchiveFailure (file://test/optional.test.js:25:7)



  optional › optionalDependencies/esm / mapNodeModules / importFromMap

  test/optional.test.js:25

   24:     const result = await tryOptionalDeps();
   25:     t.deepEqual(
   26:       Reflect.ownKeys(result.exports),

  expected exports

  Difference (- actual, + expected):

    [
  +   'alpha',
  +   'beta',
    ]

  › scaffold.knownArchiveFailure (file://test/optional.test.js:25:7)



  optional › optionalDependencies/esm / mapNodeModules / loadFromMap / import

  test/optional.test.js:25

   24:     const result = await tryOptionalDeps();
   25:     t.deepEqual(
   26:       Reflect.ownKeys(result.exports),

  expected exports

  Difference (- actual, + expected):

    [
  +   'alpha',
  +   'beta',
    ]

  › scaffold.knownArchiveFailure (file://test/optional.test.js:25:7)



  optional › optionalDependencies/esm / importLocation

  test/optional.test.js:25

   24:     const result = await tryOptionalDeps();
   25:     t.deepEqual(
   26:       Reflect.ownKeys(result.exports),

  expected exports

  Difference (- actual, + expected):

    [
  +   'alpha',
  +   'beta',
    ]

  › scaffold.knownArchiveFailure (file://test/optional.test.js:25:7)



  4 tests failed
</details>

Comment thread packages/ses/src/compartment.js Outdated
Comment thread packages/ses/src/commons.js Outdated
Comment thread packages/ses/scripts/bundle.js Outdated
@leotm leotm marked this pull request as draft March 5, 2025 13:31
auto-merge was automatically disabled March 5, 2025 13:31

Pull request was converted to draft

@leotm

leotm commented Mar 5, 2025

Copy link
Copy Markdown
Contributor Author

Not blocking: I would like a SES-specific lint rule, like the we have for @endo/polymorphic*, so that we get a lint error if we use this inside an arrow function, since evidently that will break the Hermes version.

added to tracking issue under 'Non blocking' header

endo sync: the current error in the transform that provides feedback in CI is adequate

@kriskowal kriskowal left a comment

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.

One last round. Let’s obviate functionBind again, and also take on the explicit devDependencies on the the same versions of Babel used elsewhere in the project.

Comment thread packages/ses/package.json
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-eslint-comments": "^3.2.0",
"eslint-plugin-import": "^2.29.1",
"hermes-engine-cli": "^0.12.0",

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.

Let’s add explicit dependencies on the relevant Babel packages.

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.

note: added the same versions to packages/ses for parity

"dependencies": {
"@agoric/babel-generator": "^7.17.6",
"@babel/parser": "^7.23.6",
"@babel/traverse": "^7.23.6",
"@babel/types": "^7.24.0",
"ses": "workspace:^"
},

"dependencies": {
"@agoric/babel-generator": "^7.17.6",
"@babel/parser": "^7.23.6",
"@babel/traverse": "^7.23.6",
"source-map-js": "^1.2.0"
}

as devDepencencies since they're not used in any packages/ses/src code
only packages/ses/scripts/hermes-transforms.js to bundle ses itself

Comment thread packages/ses/src/compartment.js Outdated
@mhofman

mhofman commented Mar 5, 2025

Copy link
Copy Markdown
Contributor

I think there is a risk under maintenance that we add a new syntactic async generator in the SES-shim, that would be unexpectedly transformed under Hermes.

Let's add a lint rule that warn on async generator usages that Hermes requires special handling, and silence the lint warning in the EvalError fallback case, which has the special handling.

@leotm

leotm commented Mar 5, 2025

Copy link
Copy Markdown
Contributor Author

I think there is a risk under maintenance that we add a new syntactic async generator in the SES-shim, that would be unexpectedly transformed under Hermes.

Let's add a lint rule that warn on async generator usages that Hermes requires special handling, and silence the lint warning in the EvalError fallback case, which has the special handling.

added to tracking issue under 'Non blocking' header

@kriskowal kriskowal left a comment

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.

Just a nit. Otherwise, good to go.

Comment thread packages/ses/src/compartment.js Outdated
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.

Support Windows in Hermes scripts

8 participants