From 93328ad8ee6e29ccca65e28e3a6c9708da339e25 Mon Sep 17 00:00:00 2001 From: pca006132 Date: Thu, 27 Jan 2022 23:03:28 +0800 Subject: [PATCH] compiler: only allow constant exception messages Otherwise, the exception message might be allocated on a stack, and will become a dangling pointer when the exception is raised. This will break some code that constructs exceptions with a function by passing the message as a parameter because we cannot know if the parameter is a constant. A way to mitigate this would be to defer this check to LLVM IR codegen stage, and do inlining first for those exception allocation functions, but I am not sure if we will guarantee inlining for certain functions, and whether this is really needed. --- artiq/compiler/transforms/artiq_ir_generator.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/artiq/compiler/transforms/artiq_ir_generator.py b/artiq/compiler/transforms/artiq_ir_generator.py index 40f50718a..9062b6806 100644 --- a/artiq/compiler/transforms/artiq_ir_generator.py +++ b/artiq/compiler/transforms/artiq_ir_generator.py @@ -2198,7 +2198,8 @@ class ARTIQIRGenerator(algorithm.Visitor): return phi # Keep this function with builtins.TException.attributes. - def alloc_exn(self, typ, message=None, param0=None, param1=None, param2=None): + def alloc_exn(self, typ, message=None, param0=None, param1=None, + param2=None, nomsgcheck=False): typ = typ.find() name = "{}:{}".format(typ.id, typ.name) name_id = self.embedding_map.store_str(name) @@ -2212,8 +2213,16 @@ class ARTIQIRGenerator(algorithm.Visitor): if message is None: attributes.append(ir.Constant(typ.name, builtins.TStr())) + elif isinstance(message, ir.Constant) or nomsgcheck: + attributes.append(message) # message else: - attributes.append(message) # message + diag = diagnostic.Diagnostic( + "error", + "only constant exception messages are supported", + {}, + self.current_loc if message.loc is None else message.loc + ) + self.engine.process(diag) param_type = builtins.TInt64() for param in [param0, param1, param2]: @@ -2674,7 +2683,8 @@ class ARTIQIRGenerator(algorithm.Visitor): old_final_branch, self.final_branch = self.final_branch, None old_unwind, self.unwind_target = self.unwind_target, None - exn = self.alloc_exn(builtins.TException("AssertionError"), message=msg) + exn = self.alloc_exn(builtins.TException("AssertionError"), + message=msg, nomsgcheck=True) self.append(ir.SetAttr(exn, "#__file__", file)) self.append(ir.SetAttr(exn, "#__line__", line)) self.append(ir.SetAttr(exn, "#__col__", col))