[llvm] [WebAssembly] Protect memory.fill and memory.copy from zero-length ranges. (PR #112617)

Heejin Ahn via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 17 17:15:53 PDT 2024


================
@@ -21,22 +21,37 @@ multiclass BULK_I<dag oops_r, dag iops_r, dag oops_s, dag iops_s,
 }
 
 // Bespoke types and nodes for bulk memory ops
-def wasm_memcpy_t : SDTypeProfile<0, 5,
+
+def wasm_memcpylike_t : SDTypeProfile<0, 5,
   [SDTCisInt<0>, SDTCisInt<1>, SDTCisPtrTy<2>, SDTCisPtrTy<3>, SDTCisInt<4>]
 >;
-def wasm_memcpy : SDNode<"WebAssemblyISD::MEMORY_COPY", wasm_memcpy_t,
-                         [SDNPHasChain, SDNPMayLoad, SDNPMayStore]>;
-
-def wasm_memset_t : SDTypeProfile<0, 4,
+def wasm_memsetlike_t : SDTypeProfile<0, 4,
   [SDTCisInt<0>, SDTCisPtrTy<1>, SDTCisInt<2>, SDTCisInt<3>]
 >;
-def wasm_memset : SDNode<"WebAssemblyISD::MEMORY_FILL", wasm_memset_t,
+
+// memory.copy (may trap on empty ranges)
+def wasm_memory_copy : SDNode<"WebAssemblyISD::MEMORY_COPY", wasm_memcpylike_t,
+                              [SDNPHasChain, SDNPMayLoad, SDNPMayStore]>;
----------------
aheejin wrote:

Can you elaborate a little more, like
```
may trap on empty ranges when pointers are out of bounds
```
?

But come to think of it, do we need `wasm_memory_copy`/`wasm_memory_fill` nodes and `WebAssemblyISD::MEMORY_COPY`/`WebAssemblyISD::MEMORY_FILL` at all? Can't they just be normal instructions without ISD nodes?
https://github.com/llvm/llvm-project/blob/5033ea73bb01061feb09b3216c74619e1fbefdeb/llvm/lib/Target/WebAssembly/WebAssemblyISD.def#L45-L46

https://github.com/llvm/llvm-project/pull/112617


More information about the llvm-commits mailing list