From 2c72af935ad545f269c7c0726e7c5181c9d70f6f Mon Sep 17 00:00:00 2001 From: Niklas Eiling Date: Sat, 10 Feb 2024 16:18:57 +0100 Subject: [PATCH] ips/i2c: move deinit to stop instead of destructor accessing register space from destructor can cause use after free errors Signed-off-by: Niklas Eiling --- fpga/include/villas/fpga/ips/i2c.hpp | 1 + fpga/lib/ips/i2c.cpp | 6 +++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/fpga/include/villas/fpga/ips/i2c.hpp b/fpga/include/villas/fpga/ips/i2c.hpp index 132e88f9e..b76be99ab 100644 --- a/fpga/include/villas/fpga/ips/i2c.hpp +++ b/fpga/include/villas/fpga/ips/i2c.hpp @@ -34,6 +34,7 @@ public: virtual bool init() override; virtual bool check() override; virtual bool reset() override; + virtual bool stop() override; bool write(u8 address, std::vector &data); bool read(u8 address, std::vector &data, size_t max_read); bool readRegister(u8 address, u8 reg, std::vector &data, size_t max_read); diff --git a/fpga/lib/ips/i2c.cpp b/fpga/lib/ips/i2c.cpp index f2f71cc3e..f5b55699f 100644 --- a/fpga/lib/ips/i2c.cpp +++ b/fpga/lib/ips/i2c.cpp @@ -21,7 +21,7 @@ I2c::I2c() xConfig(), hwLock(), configDone(false), initDone(false), polling(false), switchInstance(nullptr) {} -I2c::~I2c() { I2c::reset(); } +I2c::~I2c() {} static void SendHandler(I2c *i2c, __attribute__((unused)) int bytesSend) { i2c->transmitIntrs++; @@ -43,6 +43,7 @@ bool I2c::init() { return true; } xConfig.BaseAddress = getBaseAddr(registerMemory); + logger->debug("I2C base address: {:#x}", xConfig.BaseAddress); hwLock.lock(); ret = XIic_CfgInitialize(&xIic, &xConfig, xConfig.BaseAddress); if (ret != XST_SUCCESS) { @@ -67,7 +68,10 @@ bool I2c::check() { return getSwitch().selfTest(); } +bool I2c::stop() { return reset(); } + bool I2c::reset() { + logger->debug("I2C reset"); // we cannot lock here because this may be called in a destructor XIic_Reset(&xIic); irqs[i2cInterrupt].irqController->disableInterrupt(irqs[i2cInterrupt]);