Skip to content

Commit 70e1578

Browse files
authored
Fix WASI struct sizes (AssemblyScript#1245)
1 parent 7b501b2 commit 70e1578

File tree

3 files changed

+54
-13
lines changed

3 files changed

+54
-13
lines changed

std/assembly/bindings/wasi_snapshot_preview1.ts

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -959,8 +959,7 @@ export namespace errno {
959959
}
960960
export type errno = u16;
961961

962-
/** An event that occurred. */
963-
@unmanaged export abstract class event {
962+
@unmanaged abstract class $event { // size=16/32
964963
/** User-provided value that got attached to `subscription#userdata`. */
965964
userdata: userdata;
966965
/** If non-zero, an error that occurred while processing the subscription request. */
@@ -971,8 +970,14 @@ export type errno = u16;
971970
private __padding0: u16;
972971
}
973972

973+
/** An event that occurred. */
974+
@unmanaged export abstract class event extends $event {
975+
private __padding1: u64;
976+
private __padding2: u64;
977+
}
978+
974979
/** An event that occurred when type is `eventtype.FD_READ` or `eventtype.FD_WRITE`. */
975-
@unmanaged export class event_fd_readwrite extends event {
980+
@unmanaged export class event_fd_readwrite extends $event {
976981
/* The number of bytes available for reading or writing. */
977982
nbytes: filesize;
978983
/* The state of the file descriptor. */
@@ -1187,14 +1192,18 @@ export namespace preopentype {
11871192
}
11881193
export type preopentype = u8;
11891194

1190-
/* Information about a pre-opened capability. */
1191-
export abstract class prestat {
1195+
@unmanaged abstract class $prestat { // WASM32: size=1/8, WASM64: size=1/16
11921196
/* The type of the pre-opened capability. */
11931197
type: preopentype;
11941198
}
11951199

1200+
/* Information about a pre-opened capability. */
1201+
@unmanaged export abstract class prestat extends $prestat {
1202+
private __padding0: usize;
1203+
}
1204+
11961205
/** The contents of a $prestat when type is `preopentype.DIR`. */
1197-
export class prestat_dir extends prestat {
1206+
@unmanaged export class prestat_dir extends $prestat {
11981207
/** The length of the directory name for use with `fd_prestat_dir_name`. */
11991208
name_len: usize;
12001209
}
@@ -1491,8 +1500,7 @@ export namespace subclockflags {
14911500
}
14921501
export type subclockflags = u16;
14931502

1494-
/** Subscription to an event. */
1495-
@unmanaged export abstract class subscription {
1503+
@unmanaged abstract class $subscription { // size=16/48
14961504
/** User-provided value that is attached to the subscription. */
14971505
userdata: userdata;
14981506
/** The type of the event to which to subscribe. */
@@ -1501,8 +1509,16 @@ export type subclockflags = u16;
15011509
private __padding0: u32;
15021510
}
15031511

1512+
/** Subscription to an event. */
1513+
@unmanaged export abstract class subscription extends $subscription {
1514+
private __padding1: u64;
1515+
private __padding2: u64;
1516+
private __padding3: u64;
1517+
private __padding4: u64;
1518+
}
1519+
15041520
/* Subscription to an event of type `eventtype.CLOCK`.**/
1505-
@unmanaged export class subscription_clock extends subscription {
1521+
@unmanaged export class subscription_clock extends $subscription {
15061522
/** The clock against which to compare the timestamp. */
15071523
clock_id: clockid;
15081524
/** The absolute or relative timestamp. */
@@ -1516,9 +1532,13 @@ export type subclockflags = u16;
15161532
}
15171533

15181534
/* Subscription to an event of type `eventtype.FD_READ` or `eventtype.FD_WRITE`.**/
1519-
@unmanaged export class subscription_fd_readwrite extends subscription {
1535+
@unmanaged export class subscription_fd_readwrite extends $subscription {
15201536
/** The file descriptor on which to wait for it to become ready for reading or writing. */
15211537
file_descriptor: fd;
1538+
1539+
private __padding1: u64;
1540+
private __padding2: u64;
1541+
private __padding3: u64;
15221542
}
15231543

15241544
/** Timestamp in nanoseconds. */

tests/compiler/wasi/snapshot_preview1.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import {
22
dirent,
3+
event,
34
event_fd_readwrite,
45
fdstat,
56
filestat,
67
iovec,
8+
subscription,
79
subscription_clock,
810
subscription_fd_readwrite,
911
signal,
12+
prestat,
1013
prestat_dir
1114
} from "bindings/wasi";
1215

@@ -25,6 +28,8 @@ assert(offsetof<event_fd_readwrite>("nbytes") == 16);
2528
assert(offsetof<event_fd_readwrite>("flags") == 24);
2629
assert(offsetof<event_fd_readwrite>() == 32);
2730

31+
assert(offsetof<event>() == 32);
32+
2833
assert(offsetof<fdstat>("filetype") == 0);
2934
assert(offsetof<fdstat>("flags") == 2);
3035
assert(offsetof<fdstat>("rights_base") == 8);
@@ -63,15 +68,19 @@ assert(offsetof<subscription_clock>() == 48);
6368
assert(offsetof<subscription_fd_readwrite>("userdata") == 0);
6469
assert(offsetof<subscription_fd_readwrite>("type") == 8);
6570
assert(offsetof<subscription_fd_readwrite>("file_descriptor") == 16);
66-
assert(offsetof<subscription_fd_readwrite>() == 20);
71+
assert(offsetof<subscription_fd_readwrite>() == 48);
72+
73+
assert(offsetof<subscription>() == 48);
6774

6875
assert(offsetof<prestat_dir>("type") == 0);
6976
if (ASC_TARGET == Target.WASM32) {
7077
assert(offsetof<prestat_dir>("name_len") == 4);
7178
assert(offsetof<prestat_dir>() == 8);
79+
assert(offsetof<prestat>() == 8);
7280
} else if (ASC_TARGET == Target.WASM64) {
7381
assert(offsetof<prestat_dir>("name_len") == 8);
7482
assert(offsetof<prestat_dir>() == 16);
83+
assert(offsetof<prestat>() == 16);
7584
} else {
7685
assert(false);
7786
}

tests/compiler/wasi/snapshot_preview1.untouched.wat

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@
5454
i32.const 32
5555
i32.eq
5656
drop
57+
i32.const 32
58+
i32.const 32
59+
i32.eq
60+
drop
5761
i32.const 0
5862
i32.const 0
5963
i32.eq
@@ -166,8 +170,12 @@
166170
i32.const 16
167171
i32.eq
168172
drop
169-
i32.const 20
170-
i32.const 20
173+
i32.const 48
174+
i32.const 48
175+
i32.eq
176+
drop
177+
i32.const 48
178+
i32.const 48
171179
i32.eq
172180
drop
173181
i32.const 0
@@ -186,6 +194,10 @@
186194
i32.const 8
187195
i32.eq
188196
drop
197+
i32.const 8
198+
i32.const 8
199+
i32.eq
200+
drop
189201
i32.const 9
190202
global.set $wasi/snapshot_preview1/sig
191203
)

0 commit comments

Comments
 (0)