Trusted Software Excellence across Desktop and Embedded
Take a glance at the areas of expertise where KDAB excels ranging from swift troubleshooting, ongoing consulting and training to multi-year, large-scale software development projects.
Find out why customers from innovative industries rely on our extensive expertise, including Medical, Biotech, Science, Renewable Energy, Transportation, Mobility, Aviation, Automation, Electronics, Agriculture and Defense.
High-quality Embedded Engineering across the Stack
To successfully develop an embedded device that meets your expectations regarding quality, budget and time to market, all parts of the project need to fit perfectly together.
Learn more about KDAB's expertise in embedded software development.
Where the capabilities of modern mobile devices or web browsers fall short, KDAB engineers help you expertly architect and build high-functioning desktop and workstation applications.
Extensible, Safety-compliant Software for the Medical Sector
Create intelligent, patient-focused medical software and devices and stay ahead with technology that adapts to your needs.
KDAB offers you expertise in developing a broad spectrum of clinical and home-healthcare devices, including but not limited to, internal imaging systems, robotic surgery devices, ventilators and non-invasive monitoring systems.
Building digital dashboards and cockpits with fluid animations and gesture-controlled touchscreens is a big challenge.
In over two decades of developing intricate UI solutions for cars, trucks, tractors, scooters, ships, airplanes and more, the KDAB team has gained market leading expertise in this realm.
Build on Advanced Expertise when creating Modern UIs
KDAB assists you in the creation of user-friendly interfaces designed specifically for industrial process control, manufacturing, and fabrication.
Our specialties encompass the custom design and development of HMIs, enabling product accessibility from embedded systems, remote desktops, and mobile devices on the move.
Legacy software is a growing but often ignored problem across all industries. KDAB helps you elevate your aging code base to meet the dynamic needs of the future.
Whether you want to migrate from an old to a modern GUI toolkit, update to a more recent version, or modernize your code base, you can rely on over 25 years of modernization experience.
KDAB offers a wide range of services to address your software needs including consulting, development, workshops and training tailored to your requirements.
Our expertise spans cross-platform desktop, embedded and 3D application development, using the proven technologies for the job.
When working with KDAB, the first-ever Qt consultancy, you benefit from a deep understanding of Qt internals, that allows us to provide effective solutions, irrespective of the depth or scale of your Qt project.
Qt Services include developing applications, building runtimes, mixing native and web technologies, solving performance issues, and porting problems.
KDAB helps create commercial, scientific or industrial desktop applications from scratch, or update its code or framework to benefit from modern features.
Discover clean, efficient solutions that precisely meet your requirements.
Boost your team's programming skills with in-depth, constantly updated, hands-on training courses delivered by active software engineers who love to teach and share their knowledge.
Our courses cover Modern C++, Qt/QML, Rust, 3D programming, Debugging, Profiling and more.
The collective expertise of KDAB's engineering team is at your disposal to help you choose the software stack for your project or master domain-specific challenges.
Our particular focus is on software technologies you use for cross-platform applications or for embedded devices.
Since 1999, KDAB has been the largest independent Qt consultancy worldwide and today is a Qt Platinum partner. Our experts can help you with any aspect of software development with Qt and QML.
KDAB specializes in Modern C++ development, with a focus on desktop applications, GUI, embedded software, and operating systems.
Our experts are industry-recognized contributors and trainers, leveraging C++'s power and relevance across these domains to deliver high-quality software solutions.
KDAB can guide you incorporating Rust into your project, from as overlapping element to your existing C++ codebase to a complete replacement of your legacy code.
Unique Expertise for Desktop and Embedded Platforms
Whether you are using Linux, Windows, MacOS, Android, iOS or real-time OS, KDAB helps you create performance optimized applications on your preferred platform.
If you are planning to create projects with Slint, a lightweight alternative to standard GUI frameworks especially on low-end hardware, you can rely on the expertise of KDAB being one of the earliest adopters and official service partner of Slint.
KDAB has deep expertise in embedded systems, which coupled with Flutter proficiency, allows us to provide comprehensive support throughout the software development lifecycle.
Our engineers are constantly contributing to the Flutter ecosystem, for example by developing flutter-pi, one of the most used embedders.
KDAB invests significant time in exploring new software technologies to maintain its position as software authority. Benefit from this research and incorporate it eventually into your own project.
Start here to browse infos on the KDAB website(s) and take advantage of useful developer resources like blogs, publications and videos about Qt, C++, Rust, 3D technologies like OpenGL and Vulkan, the KDAB developer tools and more.
The KDAB Youtube channel has become a go-to source for developers looking for high-quality tutorial and information material around software development with Qt/QML, C++, Rust and other technologies.
Click to navigate the all KDAB videos directly on this website.
In over 25 years KDAB has served hundreds of customers from various industries, many of them having become long-term customers who value our unique expertise and dedication.
Learn more about KDAB as a company, understand why we are considered a trusted partner by many and explore project examples in which we have proven to be the right supplier.
The KDAB Group is a globally recognized provider for software consulting, development and training, specializing in embedded devices and complex cross-platform desktop applications.
Read more about the history, the values, the team and the founder of the company.
When working with KDAB you can expect quality software and the desired business outcomes thanks to decades of experience gathered in hundreds of projects of different sizes in various industries.
Have a look at selected examples where KDAB has helped customers to succeed with their projects.
KDAB is committed to developing high-quality and high-performance software, and helping other developers deliver to the same high standards.
We create software with pride to improve your engineering and your business, making your products more resilient and maintainable with better performance.
KDAB has been the first certified Qt consulting and software development company in the world, and continues to deliver quality processes that meet or exceed the highest expectations.
In KDAB we value practical software development experience and skills higher than academic degrees. We strive to ensure equal treatment of all our employees regardless of age, ethnicity, gender, sexual orientation, nationality.
Interested? Read more about working at KDAB and how to apply for a job in software engineering or business administration.
The upcoming version of the C++ Standard (C++2a) is proposing to deprecate certain usages of the volatile keyword, by adopting the P1152 proposal (Deprecating volatile).
Despite the somewhat "flamboyant" title, the actual deprecated parts are very limited and indeed the paper limits the deprecation to somehow language/library corner cases and/or dangerous antipatterns.
For instance certain read-modify-write operations on a variable declared volatile are now deprecated, like:
volatileint i =42;++i;// deprecatedi *=2;// also deprecated
In any case, I took the occasion for digging a bit into Qt's own usages of volatile. I didn't do a thorough search through the entirety of Qt because I've also tried to fix the mis-usages...
In this blog post I am going to share some of my discoveries.
volatile foo used in place of std::atomic<foo>
As usual, the problem here is that volatile in C++ has nothing to do with synchronization/atomicity. (Ok, that's not entirely true on all systems, but it's true enough.)
There are at least two big offenders I found this way.
The incubateWhile call takes a pointer to volatile bool. The idea is that the pointed boolean gets periodically checked by the incubator controller; another thread can tell the controller to stop incubating by flipping that bool.
To be honest, I'm not 100% convinced by the soundness of this API. For instance, it could've accepted a function object to poll, moving the problem of the eventual synchronization from another thread to the user's domain. When in doubt, making it someone else's problem is usually a good choice.
But I don't want to redesign the QML engine APIs here, so I've just attempted a fix by porting to std::atomic<bool> instead.
The fix will appear in Qt 5.15.
2: QObjectPrivate::threadData
QObjectPrivate::threadData is a raw pointer to QThreadData (i.e. QThreadData *), basically storing the thread affinity of a given QObject, by holding a pointer to that thread's private data.
The problem is that that variable is read and written by multiple threads without synchronization. For instance it's written from here:
voidQObjectPrivate::setThreadData_helper(QThreadData *currentData, QThreadData *targetData){...// set new thread data targetData->ref(); threadData->deref(); threadData = targetData;// <-- the write...}
These calls can happen from different threads, without synchronization — postEvent is documented to be thread-safe. Therefore, we have a data race, and data races are undefined behavior.
This ended up being super-tricky to fix (and I'm still not sure of it).
Sure, the idea is to make QObjectPrivate::threadData an atomic pointer to QThreadData, but then how to fix all the usage points?
The big struggle is that QObject is merely reentrant, and not thread safe, so most cases don't require any synchronization (and can be turned into relaxed loads) because you're not supposed to be modifying that variable by multiple threads without synchronization. Big exception: the cases that involve code paths that are documented to be thread-safe, like the postEvent code path above, which thus require acquire loads.
The most interesting part is probably the old loop in postEvent(), trying to lock receiver's thread event queue:
QThreadData *volatile* pdata =&receiver->d_func()->threadData; QThreadData *data =*pdata;if(!data){// posting during destruction? just delete the event to prevent a leakdelete event;return;}// lock the post event mutex data->postEventList.mutex.lock();// if object has moved to another thread, follow itwhile(data !=*pdata){ data->postEventList.mutex.unlock(); data =*pdata;if(!data){// posting during destruction? just delete the event to prevent a leakdelete event;return;} data->postEventList.mutex.lock();}
This loop used volatile once more to "cheat" synchronization: the code needs to get the threadData of an object and lock its event queue. Before that's done, however, the affinity of the object could've been changed, so we need to unlock and try again. The idea is sound, but volatile does not help at all here as far as C++ is concerned; it seems to work because the compiler actually reloads the value of receiver's threadData, since its access happens through a volatile pointer.
As a side note: the same loop was completely missing from other places, like QCoreApplication::removePostedEvents, that also need to lock an object's event queue (oops!). So I fixed that too, as a drive-by change...
volatile as a way to tell the compiler to... stop thinking
The QLibraryInfo class stores, amongst other things, the path to Qt's own installed "parts" (plugins, translations, etc.). This path is hardcoded at build time; it's one of the reasons why a Qt installation cannot be easily relocated.
#ifndefQT_BUILD_QMAKE_BOOTSTRAPif(!fromConf){constchar*volatile path =0;if(loc == PrefixPath){ path =getPrefix(#ifdefQT_BUILD_QMAKE group
#endif);}elseif(unsigned(loc)<=sizeof(qt_configure_str_offsets)/sizeof(qt_configure_str_offsets[0])){ path = qt_configure_strs + qt_configure_str_offsets[loc -1];#ifndefQ_OS_WIN // On Windows we use the registry}elseif(loc == SettingsPath){ path = QT_CONFIGURE_SETTINGS_PATH;#endif#ifdefQT_BUILD_QMAKE}elseif(loc == HostPrefixPath){staticconst QByteArray hostPrefixPath =getHostPrefixFromHostBinDir().toLatin1(); path = hostPrefixPath.constData();#endif}if(path) ret =QString::fromLocal8Bit(path);}#endif
What's it for? It turned out to be a complete hack.
In order to achieve relocation of Qt libraries (for instance, to allow the binary Qt installers to install Qt in any path specified by the user), Qt can be built with a dummy installation path. The installer will then binary patch Qt, replacing this dummy path with the actual installation path.
What's the hack, then? The hack is to prevent the compiler to aggressively inline the call to QString::fromLocal8Bit below, which involves a call to strlen(path).
With path set to a compile-time string literal, strlen(path) would also be fully evaluated at compile time, and that would make the binary patch not work -- it would end up with path and its length not matching, and making fromLocal8Bit return broken data or crash horribly. This has actually happened and has been reported as QTBUG-45307.
How to stop the compiler from calling strlen at compile-time? There you have it, don't use a "const char *", use a "const char * volatile" (a volatile pointer to const char). I have no idea why it actually works!
Didn't fix it, but left a comment for the future reader.
volatile for setjmp/longjmp protection
Now we go into the legitimate use cases for volatile. C (and thus C++) define the setjmp and longjmp library functions. They do non-local jumps: longjmp jumps to a setjmp done somewhere before in the call stack.
In C they're used as a poor man's exception handling mechanism -- return from a deep call stack up directly to a caller, unwinding the stack in the process. Since it's C, every object is trivially destructible, so there's nothing special to do to perform the unwinding. In C++ performing a longjmp that would destroy non-trivially destructible objects yields undefined behavior.
volatile has an interesting interaction with setjmp/longjmp: it's used to give well-defined semantics when using function local variables across the jump.
For instance, a snippet like this:
volatileint i =0;jmp_buf buf;if(!setjmp(buf)){// first time, enter here i =42;longjmp(buf,1);}// then go here after the longjmpstd::cout << i;
exhibits well-defined behavior (and prints 42) specifically because i is volatile. Without the volatile, i would have indeterminate value, and producing such an indeterminate value in this context is undefined behavior (I'm sensing a pattern here...). In other words: any local variable that is set after a setjmp and read after the longjmp must be declared volatile.
In layman's terms: this volatile is telling the compiler something like "do not put i in a register and don't optimize it out in any way; always keep it spilled on the stack, so it's safe in case of a longjmp".
Qt has a couple of places where there are setjmp/longjmp calls, namely its image handlers. PNG and JPEG use the respective C libraries (libpng and libjpeg), which seem to be designed with using setjmp/longjmp as a way to notify users of errors. That's also where there are more usages of volatile.
PNG handler
In the case of the PNG image handler, a local variable was declared volatile, but that local was actually never accessed after the longjmp; so volatile wasn't even needed in the first place. This has been fixed here.
JPEG handler
For the JPEG image handler, the situation was a bit more spicy. Sure thing, a local variable was again unnecessarily declared volatile (fix).
However the same code also was showing undefined behavior by not protecting some locals with volatile. For instance here, simplifying the code, we have this pattern:
The row_pointer local variable was not volatile; it was written after the setjmp (line 615) and then read again after the longjmp (line 716).
The fix in this case couldn't be to simply mark it as volatile: the object in question is passed to libjpeg's API, which simply want a pointer-to-object, and not a pointer-to-volatile-object (d'oh!). Marking an object with volatile, then const_cast'ing volatileness away and accessing the object is an immediate undefined behavior, so don't even think about doing it...
Also, libjpeg's error handling requires the user to use setjmp/longjmp; therefore, the code had to keep the same structure (including the jumps). Basically, in libjpeg, one installs a custom error handling function; once libjpeg calls that handling function, it expects it not to return. So it must either exit the entire process, or longjmp out.
My fix was to make those variables non-local to the function calling setjmp, thus recovering well-defined behavior. You can read more about it in the fix' commit message here.
longjmps, longjmps everywhere
Now the really interesting bit is that the very same code in the very same shape (and with the same very bug) is present everywhere.
For instance, it's present in the equivalent GTK code in GDK that loads JPEG images here:
It turns out that this kind of code comes all the way back from libjpeg version 6b's own examples, dated 1998!
Those examples showcased the non-local return via setjmp/longjmp, and were affected by the same undefined behavior. You can still download them here (check out the sources for libjpeg 6b).
// 1. declare a non-volatile objectstructjpeg_decompress_struct cinfo;if(setjmp(jerr.setjmp_buffer)){// 3: and access it after the longjmp.jpeg_destroy_decompress(&cinfo);}// 2. access it after the setjmpjpeg_create_decompress(&cinfo);
I opened an issue asking for this to be fixed; the fix landed in this commit.
Conclusions
To conclude, let's celebrate the longevity of this problem in all the codebases in the world. In perfect security-drama design: the most important part of an issue is its logo.
Therefore, here's the official logo of the volatile handling in JPEG fiasco, of course made in 1998 style:
Thanks for reading!
About KDAB
Trusted software excellence across embedded and desktop platforms
The KDAB Group is a globally recognized provider for software consulting, development and training, specializing in embedded devices and complex cross-platform desktop applications. In addition to being leading experts in Qt, C++ and 3D technologies for over two decades, KDAB provides deep expertise across the stack, including Linux, Rust and modern UI frameworks. With 100+ employees from 20 countries and offices in Sweden, Germany, USA, France and UK, we serve clients around the world.
Great article. I'm loving how "volatile.jpeg" is actually a PNG
21 - Mar - 2020
Stephen
That was a fascinating read! Thank You!
Giuseppe D’Angelo
Senior Software Engineer
Senior Software Engineer at KDAB. Giuseppe is a long-time contributor to Qt, having used Qt and C++ since 2000, and is an Approver in the Qt Project. His contributions in Qt range from containers and regular expressions to GUI, Widgets, and OpenGL. A free software passionate and UNIX specialist, before joining KDAB, he organized conferences on opensource around Italy. He holds a BSc in Computer Science.
Our hands-on Modern C++ training courses are designed to quickly familiarize newcomers with the language. They also update professional C++ developers on the latest changes in the language and standard library introduced in recent C++ editions.
3 Comments
5 - Mar - 2020
chris
Great post, thanks a lot!
6 - Mar - 2020
Paul Belanger
Great article. I'm loving how "volatile.jpeg" is actually a PNG
21 - Mar - 2020
Stephen
That was a fascinating read! Thank You!