Browse Source

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

Nikolay Nikolov 2 years ago
parent
commit
20a1f0f49a

+ 5 - 3
compiler/msg/errore.msg

@@ -1656,8 +1656,8 @@ parser_e_suspending_externals_not_supported_on_current_platform=03368_E_Declarin
 %
 # Type Checking
 #
-# 04133 is the last used one
-# type_w_dangerous_type_conversion=04133
+# 04134 is the last used one
+# type_w_dangerous_type_conversion=04134
 #
 % \section{Type checking errors}
 % This section lists all errors that can occur when type checking is
@@ -1882,7 +1882,7 @@ type_e_operator_not_supported_for_types=04053_E_Operation "$1" not supported for
 type_e_illegal_type_conversion=04054_E_Illegal type conversion: "$1" to "$2"
 % When doing a type-cast, you must take care that the sizes of the variable and
 % the destination type are the same.
-type_w_dangerous_type_conversion=04133_W_Type conversion between unrelated/incompatible types: "$1" to "$2"
+type_w_dangerous_type_conversion=04134_W_Type conversion between unrelated/incompatible types: "$1" to "$2"
 % The typecast is allowed for compatibility but types are unrelated or not compatible
 type_h_pointer_to_longint_conv_not_portable=04055_H_Conversion between ordinals and pointers is not portable
 % If you typecast a pointer to a longint (or vice-versa), this code will not compile
@@ -2117,6 +2117,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}
 #

+ 4 - 3
compiler/msgidx.inc

@@ -536,7 +536,7 @@ const
   type_e_constant_expr_expected=04052;
   type_e_operator_not_supported_for_types=04053;
   type_e_illegal_type_conversion=04054;
-  type_w_dangerous_type_conversion=04133;
+  type_w_dangerous_type_conversion=04134;
   type_h_pointer_to_longint_conv_not_portable=04055;
   type_w_pointer_to_longint_conv_not_portable=04056;
   type_e_cant_choose_overload_function=04057;
@@ -606,6 +606,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;
@@ -1167,9 +1168,9 @@ const
   option_info=11024;
   option_help_pages=11025;
 
-  MsgTxtSize = 91547;
+  MsgTxtSize = 91613;
 
   MsgIdxMax : array[1..20] of longint=(
-    28,109,369,134,102,63,148,38,223,71,
+    28,109,369,135,102,63,148,38,223,71,
     68,20,30,1,1,1,1,1,1,1
   );

File diff suppressed because it is too large
+ 302 - 299
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