Browse Source

+ disallow using sizeof() and bitsizeof() on WebAssembly reference types

Nikolay Nikolov 2 years ago
parent
commit
5c792c438d

+ 3 - 1
compiler/msg/errore.msg

@@ -1656,7 +1656,7 @@ parser_e_suspending_externals_not_supported_on_current_platform=03368_E_Declarin
 %
 # Type Checking
 #
-# 04132 is the last used one
+# 04133 is the last used one
 #
 % \section{Type checking errors}
 % This section lists all errors that can occur when type checking is
@@ -2114,6 +2114,8 @@ type_e_nested_procvar_to_funcref=04131_E_A nested function variable can not be a
 % in design assigning a nested function variable to a function reference is forbidden.
 type_e_cannot_take_address_of_wasm_externref=04132_E_Cannot take the address of a WebAssembly externref
 % WebAssembly externref types don't have an in-memory representation and therefore, their address cannot be taken.
+type_e_cannot_determine_size_of_wasm_reference_type=04133_E_WebAssembly reference types don't have an observable size
+% WebAssembly reference types are opaque, meaning neither their size, nor their bit pattern can be observed.
 %
 % \end{description}
 #

+ 3 - 2
compiler/msgidx.inc

@@ -605,6 +605,7 @@ const
   type_w_array_size_does_not_match_size_of_constant_string=04130;
   type_e_nested_procvar_to_funcref=04131;
   type_e_cannot_take_address_of_wasm_externref=04132;
+  type_e_cannot_determine_size_of_wasm_reference_type=04133;
   sym_e_id_not_found=05000;
   sym_f_internal_error_in_symtablestack=05001;
   sym_e_duplicate_id=05002;
@@ -1166,9 +1167,9 @@ const
   option_info=11024;
   option_help_pages=11025;
 
-  MsgTxtSize = 91472;
+  MsgTxtSize = 91538;
 
   MsgIdxMax : array[1..20] of longint=(
-    28,109,369,133,102,63,148,38,223,71,
+    28,109,369,134,102,63,148,38,223,71,
     68,20,30,1,1,1,1,1,1,1
   );

File diff suppressed because it is too large
+ 311 - 310
compiler/msgtxt.inc


+ 5 - 0
compiler/pexpr.pas

@@ -457,8 +457,13 @@ implementation
                  { allow helpers for SizeOf and BitSizeOf }
                  if p1.nodetype=typen then
                    ttypenode(p1).helperallowed:=true;
+                 //Writeln(p1.nodetype, p1.resultdef.typ);
                  if (p1.resultdef.typ=forwarddef) then
                    Message1(type_e_type_is_not_completly_defined,tforwarddef(p1.resultdef).tosymname^);
+{$ifdef wasm}
+                 if is_wasm_reference_type(p1.resultdef) then
+                   Message(type_e_cannot_determine_size_of_wasm_reference_type);
+{$endif wasm}
                  if (l = in_sizeof_x) or
                     (not((p1.nodetype = vecn) and
                          is_packed_array(tvecnode(p1).left.resultdef)) and

+ 11 - 0
tests/test/wasm/twasmexternref6.pp

@@ -0,0 +1,11 @@
+{ %cpu=wasm32 }
+{ %fail }
+
+program twasmexternref6;
+
+const
+  { Cannot take the size of WebAssembly reference types }
+  Q = SizeOf(WasmExternRef);
+
+begin
+end.

+ 11 - 0
tests/test/wasm/twasmexternref6a.pp

@@ -0,0 +1,11 @@
+{ %cpu=wasm32 }
+{ %fail }
+
+program twasmexternref6a;
+
+const
+  { Cannot take the size of WebAssembly reference types }
+  Q = BitSizeOf(WasmExternRef);
+
+begin
+end.

+ 15 - 0
tests/test/wasm/twasmexternref6b.pp

@@ -0,0 +1,15 @@
+{ %cpu=wasm32 }
+{ %fail }
+
+program twasmexternref6b;
+
+procedure test(para: WasmExternRef);
+var
+  a: longint;
+begin
+  { Cannot take the size of WebAssembly reference types }
+  a := SizeOf(para);
+end;
+
+begin
+end.

+ 14 - 0
tests/test/wasm/twasmfuncref3.pp

@@ -0,0 +1,14 @@
+{ %cpu=wasm32 }
+{ %fail }
+
+program twasmfuncref3;
+
+type
+  TWasmFuncRef = function(a: longint; b: int64): longint; WasmFuncRef;
+
+const
+  { Cannot take the size of WebAssembly reference types }
+  Q = SizeOf(TWasmFuncRef);
+
+begin
+end.

+ 14 - 0
tests/test/wasm/twasmfuncref3a.pp

@@ -0,0 +1,14 @@
+{ %cpu=wasm32 }
+{ %fail }
+
+program twasmfuncref3a;
+
+type
+  TWasmFuncRef = function(a: longint; b: int64): longint; WasmFuncRef;
+
+const
+  { Cannot take the size of WebAssembly reference types }
+  Q = BitSizeOf(TWasmFuncRef);
+
+begin
+end.

+ 18 - 0
tests/test/wasm/twasmfuncref3b.pp

@@ -0,0 +1,18 @@
+{ %cpu=wasm32 }
+{ %fail }
+
+program twasmfuncref3b;
+
+type
+  TWasmFuncRef = function(a: longint; b: int64): longint; WasmFuncRef;
+
+procedure test(para: TWasmFuncRef);
+var
+  a: longint;
+begin
+  { Cannot take the size of WebAssembly reference types }
+  a := SizeOf(para);
+end;
+
+begin
+end.

Some files were not shown because too many files changed in this diff