Skip to content

Commit

Permalink
Test errors for out-of-range limit requests (#3132)
Browse files Browse the repository at this point in the history
  • Loading branch information
kainino0x authored Nov 3, 2023
1 parent 41f89e7 commit 3382e0d
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
59 changes: 59 additions & 0 deletions src/webgpu/api/operation/adapter/requestDevice.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,65 @@ g.test('limit,better_than_supported')
t.shouldReject('OperationError', adapter.requestDevice({ requiredLimits }));
});

g.test('limit,out_of_range')
.desc(
`
Test that specifying limits that are out of range (<0, >MAX_SAFE_INTEGER, >2**31-2 for 32-bit
limits, =0 for alignment limits) produce the appropriate error (TypeError or OperationError).
`
)
.params(u =>
u
.combine('limit', kLimits)
.beginSubcases()
.expand('value', function* () {
yield -(2 ** 64);
yield Number.MIN_SAFE_INTEGER - 3;
yield Number.MIN_SAFE_INTEGER - 1;
yield Number.MIN_SAFE_INTEGER;
yield -(2 ** 32);
yield -1;
yield 0;
yield 2 ** 32 - 2;
yield 2 ** 32 - 1;
yield 2 ** 32;
yield 2 ** 32 + 1;
yield 2 ** 32 + 2;
yield Number.MAX_SAFE_INTEGER;
yield Number.MAX_SAFE_INTEGER + 1;
yield Number.MAX_SAFE_INTEGER + 3;
yield 2 ** 64;
yield Number.MAX_VALUE;
})
)
.fn(async t => {
const { limit, value } = t.params;

const gpu = getGPU(t.rec);
const adapter = await gpu.requestAdapter();
assert(adapter !== null);
const limitInfo = getDefaultLimitsForAdapter(adapter)[limit];

const requiredLimits = {
[limit]: value,
};

const errorName =
value < 0 || value > Number.MAX_SAFE_INTEGER
? 'TypeError'
: limitInfo.class === 'maximum' && value > adapter.limits[limit]
? 'OperationError'
: limitInfo.class === 'alignment' && (value > 2 ** 31 || !isPowerOfTwo(value))
? 'OperationError'
: false;

if (errorName) {
t.shouldReject(errorName, adapter.requestDevice({ requiredLimits }));
} else {
await adapter.requestDevice({ requiredLimits });
}
});

g.test('limit,worse_than_default')
.desc(
`
Expand Down
1 change: 1 addition & 0 deletions src/webgpu/listing_meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"webgpu:api,operation,adapter,requestDevice:features,unknown:*": { "subcaseMS": 13.600 },
"webgpu:api,operation,adapter,requestDevice:invalid:*": { "subcaseMS": 27.801 },
"webgpu:api,operation,adapter,requestDevice:limit,better_than_supported:*": { "subcaseMS": 3.614 },
"webgpu:api,operation,adapter,requestDevice:limit,out_of_range:*": { "subcaseMS": 1.000 },
"webgpu:api,operation,adapter,requestDevice:limit,worse_than_default:*": { "subcaseMS": 6.711 },
"webgpu:api,operation,adapter,requestDevice:limits,supported:*": { "subcaseMS": 4.579 },
"webgpu:api,operation,adapter,requestDevice:limits,unknown:*": { "subcaseMS": 0.601 },
Expand Down
1 change: 1 addition & 0 deletions src/webgpu/util/math.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2056,6 +2056,7 @@ export function isPowerOfTwo(n: number): boolean {
if (!Number.isInteger(n)) {
return false;
}
assert((n | 0) === n, 'isPowerOfTwo only supports 32-bit numbers');
return n !== 0 && (n & (n - 1)) === 0;
}

Expand Down

0 comments on commit 3382e0d

Please sign in to comment.