{"id":4331,"date":"2020-12-13T14:43:06","date_gmt":"2020-12-13T20:43:06","guid":{"rendered":"https:\/\/coderrect.com\/?post_type=docs&p=4331"},"modified":"2021-01-06T15:14:00","modified_gmt":"2021-01-06T21:14:00","slug":"typical-cases","status":"publish","type":"docs","link":"https:\/\/coderrect.com\/docs\/typical-cases\/","title":{"rendered":"Typical Cases"},"content":{"rendered":"\n

Typical Cases<\/strong><\/h1>\n\n\n\n

<\/p>\n\n\n\n

Detect races in an open source project Memcached<\/h2>\n\n\n\n

This tutorial assumes that you have gone through one of the three starter case tutorials<\/a> and have successfully run Coderrect.<\/p>\n\n\n\n

Coderrect detected\u00a03 new races<\/a>\u00a0(2 of them were confirmed) in\u00a0memcached<\/a>. To detect the reported bugs using coderrect, download and checkout the buggy version of memcached using the following commands.<\/p>\n\n\n\n

$ git clone https:\/\/github.com\/memcached\/memcached.git\n$ cd memcached\n$ git checkout 82029ecc9b3dd0f57b3f9ab9761f44714cceed6f<\/pre>\n\n\n\n
\n\n\n\n

Detect the race<\/h3>\n\n\n\n
  1. Build\u00a0memcached<\/em>\u00a0using\u00a0coderrect<\/em><\/li><\/ol>\n\n\n\n
    # install dependencies\n$ apt install libevent-dev\n# configure memcached\n$ .\/autogen.sh && .\/configure\n# build memcached using coderrect\n$ coderrect -t make<\/pre>\n\n\n\n

    The\u00a0coderrect -t make<\/em><\/strong>\u00a0command will compile and analyze the program automatically.\u00a0<\/p>\n\n\n\n

    1. Detect races using coderrect<\/em><\/li><\/ol>\n\n\n\n

      After compilation, coderrect automatically detects and lists all the potential targets to analyzed as follows:\u00a0<\/p>\n\n\n\n

      1) timedrun\n2) sizes\n3) memcached-debug\n4) memcached\n5) testapp\nPlease select binaries by entering their ordinal numbers (e.g. 1,2,6):<\/pre>\n\n\n\n

      Select 3)\u00a0memcached-debug<\/em><\/strong>\u00a0as the target to detect races on the debug version of memcached.<\/p>\n\n\n\n


      \n\n\n\n

      Interpret the Results<\/h3>\n\n\n\n

      The\u00a0coderrect<\/em><\/strong>\u00a0tool generates a comprehensive report that can be viewed in a browser.\u00a0<\/p>\n\n\n\n

      HTML Report<\/h4>\n\n\n\n

      To view the full report, open \u2018.coderrect\/report\/index.htm<\/em><\/strong>l\u2018 in your browser.<\/p>\n\n\n\n

      The HTML report looks like the following picture.<\/p>\n\n\n\n

      \"\"<\/figure>\n\n\n\n

      Terminal Report<\/h4>\n\n\n\n

      To get a quick overview of the detected races, coderrect can also report a summary of the most interesting races in the terminal (with\u00a0-t<\/em><\/strong>\u00a0flag, checkout\u00a0all coderrect options<\/a>). The terminal races report looks like the following:<\/p>\n\n\n\n

      ==== Found a race between: \nline 162, column 5 in crawler.c AND line 1464, column 16 in items.c\nShared variable:\n at line 1577 of items.c\n 1577|        calloc(1, sizeof(struct crawler_expired_data));\nThread 1:\n 160|    pthread_mutex_lock(&d->lock);\n 161|    d->crawlerstats[slab_cls].end_time = current_time;\n>162|    d->crawlerstats[slab_cls].run_complete = true;\n 163|    pthread_mutex_unlock(&d->lock);\n 164|}\n>>>Stacktrace:\n>>>pthread_create [crawler.c:505]\n>>>  item_crawler_thread [crawler.c:505]\n>>>    lru_crawler_class_done [crawler.c:378]\n>>>      crawler_expired_doneclass [crawler.c:350]\nThread 2:\n 1462|        crawlerstats_t *s = &cdata->crawlerstats[i];\n 1463|        \/* We've not successfully kicked off a crawl yet. *\/\n>1464|        if (s->run_complete) {\n 1465|            char *lru_name = \"na\";\n 1466|            pthread_mutex_lock(&cdata->lock);\n>>>Stacktrace:\n>>>pthread_create [items.c:1703]\n>>>  lru_maintainer_thread [items.c:1703]\n>>>    lru_maintainer_crawler_check [items.c:1647]<\/pre>\n\n\n\n

      Each reported race starts with a summary of where the race was found.<\/p>\n\n\n\n

      ==== Found a race between: \nline 162, column 5 in crawler.c AND line 1464, column 16 in items.c<\/pre>\n\n\n\n

      Next the report shows the name and location of the variable on which the race occurs.<\/p>\n\n\n\n

      Shared variable:\n at line 1577 of items.c\n 1577|        calloc(1, sizeof(struct crawler_expired_data));<\/pre>\n\n\n\n

      For example, the above result shows that the race occurs on the variable\u00a0allocated at line 1577 in\u00a0items.c<\/em><\/strong>\u00a0file.<\/p>\n\n\n\n

      Next the tool reports information about the two unsynchronized accesses to the shared variable. For each of the two accesses, the code snippet, and the stack trace is shown.<\/p>\n\n\n\n

      Since finding the reported location in the code may be a little tedious, the report shows a preview of the file at that location.<\/p>\n\n\n\n

      Thread 1:\n 160|    pthread_mutex_lock(&d->lock);\n 161|    d->crawlerstats[slab_cls].end_time = current_time;\n>162|    d->crawlerstats[slab_cls].run_complete = true;\n 163|    pthread_mutex_unlock(&d->lock);\n 164|}<\/pre>\n\n\n\n

      The code snippet shows that the race is on variable\u00a0crawlerstats[slab_cls].run_complete<\/em><\/strong>. Coderrect also shows the stack trace that triggers the race to make validation on the race simpler.<\/p>\n\n\n\n

      >>>Stacktrace:\n>>>pthread_create [crawler.c:505]\n>>>  item_crawler_thread [crawler.c:505]\n>>>    lru_crawler_class_done [crawler.c:378]\n>>>      crawler_expired_doneclass [crawler.c:350]<\/pre>\n\n\n\n
      \n\n\n\n

      Detecting Races in Redis<\/h2>\n\n\n\n

      Redis<\/a>\u00a0is a popular in-memory database that persists on disk. Redis has been heavily adopted and is widely used. The\u00a0GitHub repository<\/a>\u00a0has over 43,000 stars, over 18,000 questions have been tagged with\u00a0[redis]<\/em><\/strong>\u00a0on\u00a0StackOverflow<\/a>, and there are various meetups and conferences for Redis\u00a0every year<\/a>.<\/p>\n\n\n\n

      This tutorial assumes that you have gone through one of the three starter case tutorials<\/a> and have successfully run Coderrect.<\/p>\n\n\n\n


      \n\n\n\n

      Detect the race<\/h3>\n\n\n\n

      Redis is easy to build. To detect races on redis, simply run the following:<\/p>\n\n\n\n

      git clone -b 6.0.0 https:\/\/github.com\/antirez\/redis.git\ncd redis\ncoderrect -t -o report -e redis-server,redis-benchmark make -j<\/pre>\n\n\n\n