Skip to content

Fix WASM global vars #2006

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tangy-clowns-thank.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphprotocol/graph-ts': patch
---

fix global variables in wasm
1 change: 1 addition & 0 deletions packages/ts/chain/arweave.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import '../common/eager_offset';
import { Bytes } from '../common/collections';

// Most types from this namespace are direct mappings or adaptations from:
Expand Down
1 change: 1 addition & 0 deletions packages/ts/chain/cosmos.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import '../common/eager_offset';
import { Bytes } from '../common/collections';

export namespace cosmos {
Expand Down
1 change: 1 addition & 0 deletions packages/ts/chain/ethereum.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import '../common/eager_offset';
import { Bytes, Wrapped } from '../common/collections';
import { Address, BigInt } from '../common/numbers';

Expand Down
1 change: 1 addition & 0 deletions packages/ts/chain/near.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import '../common/eager_offset';
import { Bytes } from '../common/collections';
import { BigInt } from '../common/numbers';

Expand Down
1 change: 1 addition & 0 deletions packages/ts/chain/starknet.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import '../common/eager_offset';
import { Bytes } from '../common/collections';
import { BigInt } from '../common/numbers';

Expand Down
1 change: 1 addition & 0 deletions packages/ts/common/conversion.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import './eager_offset';
import { Bytes } from './collections';

/** Host type conversion interface */
Expand Down
1 change: 1 addition & 0 deletions packages/ts/common/datasource.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import './eager_offset';
import { Entity } from './collections';
import { Address } from './numbers';

Expand Down
42 changes: 42 additions & 0 deletions packages/ts/common/eager_offset.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// # What is this file?
// This file is a "hack" to allow global variables in subgraphs and
// on this library (`graph-ts`).
//
// # Why is it needed?
// It's necessary because of one of the features of the AssemblyScript
// compiler we use, the stub runtime.
//
// The problem happens because we call the stub runtime allocation
// (`__alloc`) directly on Rust side (`graph-node`), and that doesn't
// trigger some AssemblyScript aspects of the code.
//
// If you take a look at the stub runtime's code, you'll see that the
// `__alloc` function uses a variable named `offset` tagged as `lazy`.
// Like said above, since we call it on Rust side, this variable is not
// "triggered" to be used, then it's declared below the `__alloc` call
// in the compiled WASM code.
//
// That makes the `graph-node` WASM runtime break because of this out
// of order variable usage.
//
// # How does this fix the issue?
// The way this workaround works is by calling the `__alloc` function
// before everything in the AssemblyScript side. This makes the `offset`
// `lazy` variable be eagerly evaluated when the mappings are compiled
// (since they always import `graph-ts`).
//
// So when we're on Rust side calling `__alloc` it will be fine, because
// the `offset` is declared before call (order fixed because of this file).
//
// The 0 argument to the function call is just because we need no memory
// to be allocated.
//
// # IMPORTANT
// This should be imported in EVERY file which uses external namespaces (`graph-node` host-exports code),
// just to make sure no one imports a file directly and gets an error on global variables.
//
// # Reference
// - Runtimes in AS: https://www.assemblyscript.org/garbage-collection.html#runtime-variants
// - `offset` variable in question: https://github.com/AssemblyScript/assemblyscript/blob/f4091b8f3b6b029d30cd917cf84d97421faadeeb/std/assembly/rt/stub.ts#L9
// @ts-expect-error We do not want to expose __alloc, hence why we just ignore the error
__alloc(0);
1 change: 1 addition & 0 deletions packages/ts/common/json.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import './eager_offset';
import { Bytes, Result } from './collections';
import { BigInt } from './numbers';
import { JSONValue } from './value';
Expand Down
1 change: 1 addition & 0 deletions packages/ts/common/numbers.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import './eager_offset';
import { ByteArray, Bytes } from './collections';
import { typeConversion } from './conversion';

Expand Down
1 change: 1 addition & 0 deletions packages/ts/common/value.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import './eager_offset';
import { Bytes, TypedMap } from './collections';
import { json } from './json';
import { Address, BigDecimal, BigInt } from './numbers';
Expand Down
3 changes: 2 additions & 1 deletion packages/ts/common/yaml.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import './eager_offset';
import { Bytes, Result, TypedMap } from './collections';
import { BigInt } from './numbers';

Expand Down Expand Up @@ -294,6 +295,6 @@ export class YAMLTaggedValue {
return true;
}

return !(a! == b!);
return !(a == b);
}
}
2 changes: 2 additions & 0 deletions packages/ts/test/test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ async function main() {
fs.copyFileSync('common/collections.ts', 'test/temp_lib/common/collections.ts');
fs.copyFileSync('common/conversion.ts', 'test/temp_lib/common/conversion.ts');
fs.copyFileSync('common/datasource.ts', 'test/temp_lib/common/datasource.ts');
fs.copyFileSync('common/eager_offset.ts', 'test/temp_lib/common/eager_offset.ts');
fs.copyFileSync('common/json.ts', 'test/temp_lib/common/json.ts');
fs.copyFileSync('common/numbers.ts', 'test/temp_lib/common/numbers.ts');
fs.copyFileSync('common/value.ts', 'test/temp_lib/common/value.ts');
Expand All @@ -50,6 +51,7 @@ async function main() {
fs.unlinkSync('test/temp_lib/common/collections.ts');
fs.unlinkSync('test/temp_lib/common/conversion.ts');
fs.unlinkSync('test/temp_lib/common/datasource.ts');
fs.unlinkSync('test/temp_lib/common/eager_offset.ts');
fs.unlinkSync('test/temp_lib/common/json.ts');
fs.unlinkSync('test/temp_lib/common/numbers.ts');
fs.unlinkSync('test/temp_lib/common/value.ts');
Expand Down