diff --git a/Changes b/Changes index 643d0c47a..34e69f689 100644 --- a/Changes +++ b/Changes @@ -25,6 +25,7 @@ Verilator 5.041 devel * Add error on `virtual new` (#6486). [Alex Solomatnikov] * Add error on ranges with tristate values (#6534). [Alex Solomatnikov] * Add NORETURN warning on functions without return values (#6534). [Alex Solomatnikov] +* Add `--aslr` and `--no-aslr` options. * Deprecate sensitivity list on public_flat_rw attributes (#6443). [Geza Lore] * Deprecate clocker attribute and --clk option (#6463). [Geza Lore] * Deprecate '--make cmake' option (#6540). [Geza Lore] diff --git a/bin/verilator b/bin/verilator index e17db72cf..856d2122f 100755 --- a/bin/verilator +++ b/bin/verilator @@ -28,6 +28,7 @@ autoflush STDOUT 1; autoflush STDERR 1; $Debug = 0; +my $opt_aslr; my $opt_gdb; my $opt_rr; my $opt_gdbbt; @@ -56,6 +57,7 @@ if (! GetOptions( "debug" => \&debug, # "version!" => \&version, # Also passthru'ed # Switches + "aslr!" => \$opt_aslr, "gdb!" => \$opt_gdb, "gdbbt!" => \$opt_gdbbt, "quiet!" => \$opt_quiet_exit, # As -quiet implies -quiet-exit @@ -97,7 +99,7 @@ my @quoted_sw = map { sh_escape($_) } @Opt_Verilator_Sw; if ($opt_gdb) { # Generic GDB interactive run (ulimit_stack_unlimited() - . aslr_off() + . aslr(0) . ($ENV{VERILATOR_GDB} || "gdb") . " " . verilator_bin() # Note, uncomment to set breakpoints before running: @@ -114,13 +116,13 @@ if ($opt_gdb) { } elsif ($opt_rr) { # Record with rr run (ulimit_stack_unlimited() - . aslr_off() + . aslr(0) . "rr record " . verilator_bin() . " " . join(' ', @quoted_sw)); } elsif ($opt_gdbbt && $Debug) { # Run under GDB to get gdbbt run (ulimit_stack_unlimited() - . aslr_off() + . aslr(0) . "gdb" . " " . verilator_bin() . " --batch --quiet --return-child-result" @@ -135,19 +137,19 @@ if ($opt_gdb) { ); run (ulimit_stack_unlimited() - . aslr_off() + . aslr(0) . $valgrind_bin . " " . verilator_bin() . " " . join(' ', @quoted_sw)); } elsif ($Debug) { # Debug run(ulimit_stack_unlimited() - . aslr_off() + . aslr(0) . verilator_bin() . " " . join(' ', @quoted_sw)); } else { # Normal, non gdb - run(ulimit_stack_unlimited() . verilator_bin() . " " . join(' ', @quoted_sw)); + run(ulimit_stack_unlimited() . aslr(1) . verilator_bin() . " " . join(' ', @quoted_sw)); } #---------------------------------------------------------------------- @@ -192,13 +194,16 @@ sub gdb_works { return $status == 0; } -sub aslr_off { - my $ok = `setarch --addr-no-randomize echo ok 2>/dev/null` || ""; - if ($ok =~ /ok/) { - return "setarch --addr-no-randomize "; - } else { - return ""; +sub aslr { + my $want_on = shift; + $want_on = $opt_aslr if defined $opt_aslr; + if (!$want_on) { + my $ok = `setarch --addr-no-randomize echo ok 2>/dev/null` || ""; + if ($ok =~ /ok/) { + return "setarch --addr-no-randomize "; + } } + return ""; } sub ulimit_stack_unlimited { @@ -325,6 +330,7 @@ detailed descriptions of these arguments. +1800-2012ext+ Use SystemVerilog 2012 with file extension +1800-2017ext+ Use SystemVerilog 2017 with file extension +1800-2023ext+ Use SystemVerilog 2023 with file extension + --no-aslr Disable address space layout randomization --no-assert Disable all assertions --no-assert-case Disable unique/unique0/priority-case assertions --autoflush Flush streams after all $displays diff --git a/docs/guide/exe_verilator.rst b/docs/guide/exe_verilator.rst index 465a7e367..f6fc143fa 100644 --- a/docs/guide/exe_verilator.rst +++ b/docs/guide/exe_verilator.rst @@ -88,12 +88,23 @@ Summary: grammar and other semantic extensions which might not be legal when set to an older standard. +.. option:: --aslr + +.. option:: --no-aslr + + Rarely needed - for developer use. With `--aslr`, do not change the + system default as to using Linux address space layout randomization + (ASLR). With `--no-aslr` attempt to disable ASLR. If not specified, + ASLR will be disabled only when using :vlopt:`--debug` (or similar + debug-related options), so that pointers have more deterministic values, + aiding repeatability. + .. option:: --no-assert Disable all assertions. Implies :vlopt:`--no-assert-case`. - In versions before 5.038, these were disabled by default, and `--assert` - was required to enable assertions. + In versions before 5.038, assertions were disabled by default, and + `--assert` was required to enable assertions. .. option:: --no-assert-case diff --git a/docs/spelling.txt b/docs/spelling.txt index 4483943cb..9ff29b9f6 100644 --- a/docs/spelling.txt +++ b/docs/spelling.txt @@ -531,6 +531,7 @@ architected args arrarys asan +aslr assertOn assertcontrol astgen diff --git a/src/V3Options.cpp b/src/V3Options.cpp index 4b44706c3..ec431c3c6 100644 --- a/src/V3Options.cpp +++ b/src/V3Options.cpp @@ -1237,6 +1237,7 @@ void V3Options::parseOptsList(FileLine* fl, const string& optdir, int argc, [this](const char* optp) { addLangExt(optp, V3LangCode::L1800_2023); }); // Minus options + DECL_OPTION("-aslr", CbOnOff, [](bool) {}); // Processed only in bin/verilator shell DECL_OPTION("-assert", CbOnOff, [this](bool flag) { m_assert = flag; m_assertCase = flag; diff --git a/test_regress/t/t_flag_aslr.py b/test_regress/t/t_flag_aslr.py new file mode 100755 index 000000000..eb91ad9bc --- /dev/null +++ b/test_regress/t/t_flag_aslr.py @@ -0,0 +1,17 @@ +#!/usr/bin/env python3 +# DESCRIPTION: Verilator: Verilog Test driver/expect definition +# +# Copyright 2024 by Wilson Snyder. This program is free software; you +# can redistribute it and/or modify it under the terms of either the GNU +# Lesser General Public License Version 3 or the Perl Artistic License +# Version 2.0. +# SPDX-License-Identifier: LGPL-3.0-only OR Artistic-2.0 + +import vltest_bootstrap + +test.scenarios('vlt') +test.top_filename = 't/t_EXAMPLE.v' + +test.lint(v_flags2=["--aslr"]) + +test.passes() diff --git a/test_regress/t/t_flag_aslr_no.py b/test_regress/t/t_flag_aslr_no.py new file mode 100755 index 000000000..9e3e11aa1 --- /dev/null +++ b/test_regress/t/t_flag_aslr_no.py @@ -0,0 +1,17 @@ +#!/usr/bin/env python3 +# DESCRIPTION: Verilator: Verilog Test driver/expect definition +# +# Copyright 2024 by Wilson Snyder. This program is free software; you +# can redistribute it and/or modify it under the terms of either the GNU +# Lesser General Public License Version 3 or the Perl Artistic License +# Version 2.0. +# SPDX-License-Identifier: LGPL-3.0-only OR Artistic-2.0 + +import vltest_bootstrap + +test.scenarios('vlt') +test.top_filename = 't/t_EXAMPLE.v' + +test.lint(v_flags2=["--no-aslr"]) + +test.passes()