Skip to content

Fix WASI struct sizes #1245

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 1 commit into from
Apr 29, 2020
Merged
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
40 changes: 30 additions & 10 deletions std/assembly/bindings/wasi_snapshot_preview1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -959,8 +959,7 @@ export namespace errno {
}
export type errno = u16;

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

/** An event that occurred. */
@unmanaged export abstract class event extends $event {
private __padding1: u64;
private __padding2: u64;
}

/** An event that occurred when type is `eventtype.FD_READ` or `eventtype.FD_WRITE`. */
@unmanaged export class event_fd_readwrite extends event {
@unmanaged export class event_fd_readwrite extends $event {
/* The number of bytes available for reading or writing. */
nbytes: filesize;
/* The state of the file descriptor. */
Expand Down Expand Up @@ -1187,14 +1192,18 @@ export namespace preopentype {
}
export type preopentype = u8;

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

/* Information about a pre-opened capability. */
@unmanaged export abstract class prestat extends $prestat {
private __padding0: usize;
}

/** The contents of a $prestat when type is `preopentype.DIR`. */
export class prestat_dir extends prestat {
@unmanaged export class prestat_dir extends $prestat {
/** The length of the directory name for use with `fd_prestat_dir_name`. */
name_len: usize;
}
Expand Down Expand Up @@ -1491,8 +1500,7 @@ export namespace subclockflags {
}
export type subclockflags = u16;

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

/** Subscription to an event. */
@unmanaged export abstract class subscription extends $subscription {
private __padding1: u64;
private __padding2: u64;
private __padding3: u64;
private __padding4: u64;
}

/* Subscription to an event of type `eventtype.CLOCK`.**/
@unmanaged export class subscription_clock extends subscription {
@unmanaged export class subscription_clock extends $subscription {
/** The clock against which to compare the timestamp. */
clock_id: clockid;
/** The absolute or relative timestamp. */
Expand All @@ -1516,9 +1532,13 @@ export type subclockflags = u16;
}

/* Subscription to an event of type `eventtype.FD_READ` or `eventtype.FD_WRITE`.**/
@unmanaged export class subscription_fd_readwrite extends subscription {
@unmanaged export class subscription_fd_readwrite extends $subscription {
/** The file descriptor on which to wait for it to become ready for reading or writing. */
file_descriptor: fd;

private __padding1: u64;
private __padding2: u64;
private __padding3: u64;
}

/** Timestamp in nanoseconds. */
Expand Down
11 changes: 10 additions & 1 deletion tests/compiler/wasi/snapshot_preview1.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import {
dirent,
event,
event_fd_readwrite,
fdstat,
filestat,
iovec,
subscription,
subscription_clock,
subscription_fd_readwrite,
signal,
prestat,
prestat_dir
} from "bindings/wasi";

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

assert(offsetof<event>() == 32);

assert(offsetof<fdstat>("filetype") == 0);
assert(offsetof<fdstat>("flags") == 2);
assert(offsetof<fdstat>("rights_base") == 8);
Expand Down Expand Up @@ -63,15 +68,19 @@ assert(offsetof<subscription_clock>() == 48);
assert(offsetof<subscription_fd_readwrite>("userdata") == 0);
assert(offsetof<subscription_fd_readwrite>("type") == 8);
assert(offsetof<subscription_fd_readwrite>("file_descriptor") == 16);
assert(offsetof<subscription_fd_readwrite>() == 20);
assert(offsetof<subscription_fd_readwrite>() == 48);

assert(offsetof<subscription>() == 48);

assert(offsetof<prestat_dir>("type") == 0);
if (ASC_TARGET == Target.WASM32) {
assert(offsetof<prestat_dir>("name_len") == 4);
assert(offsetof<prestat_dir>() == 8);
assert(offsetof<prestat>() == 8);
} else if (ASC_TARGET == Target.WASM64) {
assert(offsetof<prestat_dir>("name_len") == 8);
assert(offsetof<prestat_dir>() == 16);
assert(offsetof<prestat>() == 16);
} else {
assert(false);
}
Expand Down
16 changes: 14 additions & 2 deletions tests/compiler/wasi/snapshot_preview1.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@
i32.const 32
i32.eq
drop
i32.const 32
i32.const 32
i32.eq
drop
i32.const 0
i32.const 0
i32.eq
Expand Down Expand Up @@ -166,8 +170,12 @@
i32.const 16
i32.eq
drop
i32.const 20
i32.const 20
i32.const 48
i32.const 48
i32.eq
drop
i32.const 48
i32.const 48
i32.eq
drop
i32.const 0
Expand All @@ -186,6 +194,10 @@
i32.const 8
i32.eq
drop
i32.const 8
i32.const 8
i32.eq
drop
i32.const 9
global.set $wasi/snapshot_preview1/sig
)
Expand Down