misc: check address range in the SystemC example
authorChristian Menard <christian.menard@tu-dresden.de>
Tue, 23 May 2017 16:34:12 +0000 (18:34 +0200)
committerChristian Menard <christian.menard@tu-dresden.de>
Tue, 30 May 2017 10:47:54 +0000 (10:47 +0000)
Using a wrong offset or memory size may lead to segmentation faults.
This patch adds an address range check and produces an error message.

Change-Id: I79a72c05879266daf61a83367fe4ae386d1958a4
Reviewed-on: https://gem5-review.googlesource.com/3482
Maintainer: Jason Lowe-Power <jason@lowepower.com>
Reviewed-by: Jason Lowe-Power <jason@lowepower.com>
Reviewed-by: Matthias Jung <jungma@eit.uni-kl.de>
util/tlm/examples/slave_port/sc_target.cc
util/tlm/examples/slave_port/sc_target.hh

index c44a271496a660491d0d0a3dcf6af62fb07f225f..152d99ae624925cf63ad0029a997c9597e39cb1e 100644 (file)
@@ -65,6 +65,14 @@ Target::Target(sc_core::sc_module_name name,
     dont_initialize();
 }
 
+void
+Target::check_address(unsigned long long int addr)
+{
+    if (addr < offset || addr >= offset + size)
+        SC_REPORT_FATAL("Target", "Address out of range. Did you set an "
+                                  "appropriate size and offset?");
+}
+
 void
 Target::b_transport(tlm::tlm_generic_payload& trans, sc_time& delay)
 {
@@ -75,6 +83,8 @@ Target::b_transport(tlm::tlm_generic_payload& trans, sc_time& delay)
 unsigned int
 Target::transport_dbg(tlm::tlm_generic_payload& trans)
 {
+    check_address(trans.get_address());
+
     tlm::tlm_command cmd = trans.get_command();
     sc_dt::uint64    adr = trans.get_address() - offset;
     unsigned char*   ptr = trans.get_data_ptr();
@@ -203,6 +213,8 @@ Target::execute_transaction_process()
 void
 Target::execute_transaction(tlm::tlm_generic_payload& trans)
 {
+    check_address(trans.get_address());
+
     tlm::tlm_command cmd = trans.get_command();
     sc_dt::uint64    adr = trans.get_address() - offset;
     unsigned char*   ptr = trans.get_data_ptr();
index 7a2d075d9e866ff3409ec6742af3c7e794d53bcd..a10fb492d3acb167a09fa0f67b124eeb7e337a6a 100644 (file)
@@ -91,6 +91,9 @@ struct Target: sc_module
     /** Method process that runs on target_done_event */
     void execute_transaction_process();
 
+    /** Helping function that checks if a requested address is with range */
+    void check_address(unsigned long long int addr);
+
     /** Helping Variables **/
     unsigned long long int size;
     unsigned offset;