From 84e44f4ae0b9564bf05f88590231e9a1568e6c40 Mon Sep 17 00:00:00 2001 From: Jean Privat Date: Fri, 7 Dec 2012 15:25:38 -0500 Subject: [PATCH] nitg: implements implicit casts for unsafe nexpr add the option --no-check-autocast to disable it. Signed-off-by: Jean Privat --- src/global_compiler.nit | 12 +++++++++++- src/rapid_type_analysis.nit | 4 ++++ src/run_bench.sh | 2 +- tests/sav/nitg.sav | 1 + 4 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/global_compiler.nit b/src/global_compiler.nit index 66969f7..2f63d20 100644 --- a/src/global_compiler.nit +++ b/src/global_compiler.nit @@ -47,6 +47,9 @@ redef class ToolContext # --no-check-assert var opt_no_check_assert: OptionBool = new OptionBool("Disable the evaluation of explicit 'assert' and 'as' (dangerous)", "--no-check-assert") + # --no-check-autocast + var opt_no_check_autocast: OptionBool = new OptionBool("Disable implicit casts on unsafe expression usage (dangerous)", "--no-check-autocast") + # --no-check-other var opt_no_check_other: OptionBool = new OptionBool("Disable implicit tests: unset attribute, null receiver (dangerous)", "--no-check-other") @@ -54,7 +57,7 @@ redef class ToolContext do super self.option_context.add_option(self.opt_output, self.opt_no_cc, self.opt_hardening) - self.option_context.add_option(self.opt_no_check_covariance, self.opt_no_check_initialization, self.opt_no_check_assert, self.opt_no_check_other) + self.option_context.add_option(self.opt_no_check_covariance, self.opt_no_check_initialization, self.opt_no_check_assert, self.opt_no_check_autocast, self.opt_no_check_other) end end @@ -951,6 +954,13 @@ class GlobalCompilerVisitor mtype = self.anchor(mtype) res = self.autobox(res, mtype) end + var implicit_cast_to = nexpr.implicit_cast_to + if implicit_cast_to != null and not self.compiler.modelbuilder.toolcontext.opt_no_check_autocast.value then + var castres = self.type_test(res, implicit_cast_to) + self.add("if (!{castres}) \{") + self.add_abort("Cast failed") + self.add("\}") + end self.current_node = old return res end diff --git a/src/rapid_type_analysis.nit b/src/rapid_type_analysis.nit index 2a89d11..fdea6f9 100644 --- a/src/rapid_type_analysis.nit +++ b/src/rapid_type_analysis.nit @@ -398,6 +398,10 @@ private class RapidTypeVisitor do if node == null then return node.accept_rapid_type_vistor(self) + if node isa AExpr then + var implicit_cast_to = node.implicit_cast_to + if implicit_cast_to != null then self.add_cast_type(implicit_cast_to) + end node.visit_all(self) end diff --git a/src/run_bench.sh b/src/run_bench.sh index 369cdcf..c44de47 100755 --- a/src/run_bench.sh +++ b/src/run_bench.sh @@ -306,7 +306,7 @@ function bench_nitg_options() plot "$name.gnu" } -bench_nitg_options --hardening --no-check-covariance --no-check-initialization --no-check-assert --no-check-other +bench_nitg_options --hardening --no-check-covariance --no-check-initialization --no-check-assert --no-check-autocast --no-check-other function bench_nitg-e_options() { diff --git a/tests/sav/nitg.sav b/tests/sav/nitg.sav index a46fad5..2d1fbc2 100644 --- a/tests/sav/nitg.sav +++ b/tests/sav/nitg.sav @@ -16,6 +16,7 @@ --no-check-covariance Disable type tests of covariant parameters (dangerous) --no-check-initialization Disable isset tests at the end of constructors (dangerous) --no-check-assert Disable the evaluation of explicit 'assert' and 'as' (dangerous) + --no-check-autocast Disable implicit casts on unsafe expression usage (dangerous) --no-check-other Disable implicit tests: unset attribute, null receiver (dangerous) --separate Use separate compilation --no-inline-intern Do not inline call to intern methods -- 1.7.9.5