2017年4月11日 星期二

【谨慎使用】解决CTP在Linux开发中double free

原创文章,转载请注明: 转载自勤奋的小青蛙
本文链接地址: 【谨慎使用】解决CTP在Linux开发中double free
感谢网友:∮f(x)dx,徐冠鹏 提供的解决方法。
这份代码用于演示如何利用 dlopen 来避免 CTP GNU/Linux API 中 trader 和 mduser 两个库同时在一个进程中加载导致的 double free 问题。请注意,它只
用于演示如何避免 double free,其余为了能使这个程序能运行起来的代码没有任何实际意义,且不适合用于生产。所以这份代码仅供参考,因使用此代码引起
的任何损失,我不承担任何责任。
精力所限,下述的步骤的测试环境包含:
- 64 位 Gentoo Linux,内核版本 3.17.7-gentoo
- GCC Gentoo 4.8.3 p1.1, pie-0.5.9
- CTP GNU/Linux 版 6.3.0_20140811
1. 下载 http://www.sfit.com.cn/DocumentDown/api/6.3.0_20140811_traderapi_linux.tar并解压至工作目录:
?
1
2
3
4
5
6
7
$ mkdir work
$ cd work
work
$ wget http://www.sfit.com.cn/DocumentDown/api/6.3.0_20140811_traderapi_linux.tar
work $ tar xf 6.3.0_20140811_traderapi_linux.tar
work $ ls
6.3.0_20140811_traderapi_linux.tar  6.3.0_20140811_traderapi_linux32  6.3.0_20140811_traderapi_linux64
2. 将 Makefile、md_test.cc、trader_test.cc、x.cc 复制至包含 CTP 头文件及共享库的目录。这里是 64 位 GNU/Linux 的目录:
?
1
2
$ cd 6.3.0_20140811_traderapi_linux64
$ cp .../{Makefile,md_test.cc,trader_test.cc,x.cc} .
这里需要把 "..." 换成包含这些文件的目录。
3. 重命名共享库文件:
?
1
2
$ mv thostmduserapi.so libthostmduserapi.so
$ mv thosttraderapi.so libthosttraderapi.so
4. 编译。这可以用 make 完成:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$ make
g++ -g -fPIC   -c -o x.o x.cc
c++ -g -fPIC -o x x.o -ldl
g++ -g -fPIC   -c -o trader_test.o trader_test.cc
trader_test.cc: In function 'void init()':
trader_test.cc:69:88: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
                                                                                        ^
g++ -fPIC -shared -o libtrader_test.so trader_test.o -L. -lthosttraderapi -ldl
g++ -g -fPIC   -c -o md_test.o md_test.cc
md_test.cc: In function 'void init()':
md_test.cc:75:88: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
                                                                                        ^
g++ -fPIC -shared -o libmd_test.so md_test.o -L. -lthostmduserapi -ldl
5. 运行:
?
1
2
3
4
5
6
7
8
9
$ LD_LIBRARY_PATH=. ./x
trader front connected
wlp3s0: Success
md front connected
wlp3s0: Success
TRADER: 3: 综合交易平台:不合法的登录
MD: 3: 综合交易平台:不合法的登录
CThostFtdcUserApiImplBase::OnSessionDisconnected[0x7f80300008c8][1510735873][    0]
CThostFtdcUserApiImplBase::OnSessionDisconnected[0x7f802c0008c8][1510735873][    0]
6. 说明:
trader_test.cc 中包含一个 trader SPI 的实现,编译后生成libtrader_test.so。md_test.cc 中包含一个 md SPI 的实现,编译后生成libmd_test.so。这两个文件各自使用 dlopen 加载对应的 CTP 共享库,并用RTLD_DEEPBIND 隔离两个共享库中冲突的符号。
x.cc 通过加载 libtrader_test.so 和 libmd_test.so 来使用 CTP 的功能。运行时,需要确保所有的共享库存在于系统搜索共享库的目录中,这可以通过配置ld.so.conf 或设置 LD_LIBRARY_PATH 环境变量来实现。配置 ld.so.conf 的方法请参考各发行版的手册,LD_LIBRARY_PATH 的设置方法见上面的例子。
Makefile
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
CXXFLAGS += -g -fPIC
 
all:    x libtrader_test.so libmd_test.so
 
x:  x.o
    c++ $(CXXFLAGS) -o x x.o -ldl
 
libtrader_test.so:  trader_test.o
    $(CXX) -fPIC -shared -o libtrader_test.so trader_test.o -L. -lthosttraderapi -ldl
 
libmd_test.so:  md_test.o
    $(CXX) -fPIC -shared -o libmd_test.so md_test.o -L. -lthostmduserapi -ldl
 
clean:
    rm -f x *.o
md_test.cc
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <unistd.h>
#include <dlfcn.h>
 
#include "ThostFtdcMdApi.h"
 
using namespace std;
 
volatile int child_finished = 0;
 
volatile int logging_in = 0;
 
class SimpleHandler: public CThostFtdcMdSpi
{
  CThostFtdcMdApi *user_api;
 
public:
 
  SimpleHandler (CThostFtdcMdApi *user_api): user_api (user_api) { }
 
  ~SimpleHandler () { }
 
  virtual void OnFrontConnected ()
  {
    if (!logging_in++)
      {
    printf ("md front connected\n");
    CThostFtdcReqUserLoginField user_login_req;
    strcpy (user_login_req.BrokerID, "test_uid");
    strcpy (user_login_req.UserID, "test_uid");
    strcpy (user_login_req.Password, "password");
    user_api->ReqUserLogin (&user_login_req, 0);
      }
  }
 
  virtual void OnFrontDisconnected (int reason)
  {
    printf ("md front disconnected: %d\n", reason);
  }
 
  virtual void OnRspUserLogin (CThostFtdcRspUserLoginField *resp,
                   CThostFtdcRspInfoField *info,
                   int req_id, bool is_last)
  {
    printf ("MD: %d: %s\n", info->ErrorID, info->ErrorMsg);
    if (info->ErrorID != 0)
      child_finished = 1;
  }
};
 
CThostFtdcMdApi *user_api;
 
SimpleHandler *simple_handler;
 
extern "C"
{
 
void
init ()
{
  void *trader_user_handle = dlopen ("libthostmduserapi.so",
                     RTLD_LAZY | RTLD_DEEPBIND);
  if (trader_user_handle == 0)
    {
      fprintf (stderr, "Cannot load libthostusermdapi.so\n");
      exit (-1);
    }
 
  user_api = CThostFtdcMdApi::CreateFtdcMdApi ();
 
  simple_handler = new SimpleHandler (user_api);
  user_api->RegisterSpi (simple_handler);
  user_api->Init ();
}
 
void
finalize ()
{
  user_api->RegisterSpi (0);
  user_api->Release ();
  delete simple_handler;
  simple_handler = 0;
}
 
}
trader_test.cc
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <unistd.h>
#include <dlfcn.h>
 
#include "ThostFtdcTraderApi.h"
 
using namespace std;
 
volatile int child_finished = 0;
 
class SimpleHandler: public CThostFtdcTraderSpi
{
  CThostFtdcTraderApi *user_api;
 
public:
 
  SimpleHandler (CThostFtdcTraderApi *user_api): user_api (user_api) { }
 
  ~SimpleHandler () { }
 
  virtual void OnFrontConnected ()
  {
    printf ("trader front connected\n");
    CThostFtdcReqUserLoginField user_login_req;
    strcpy (user_login_req.BrokerID, "test_uid");
    strcpy (user_login_req.UserID, "test_uid");
    strcpy (user_login_req.Password, "password");
    user_api->ReqUserLogin (&user_login_req, 0);
  }
 
  virtual void OnFrontDisconnected (int reason)
  {
    printf ("trader front disconnected: %d\n", reason);
  }
 
  virtual void OnRspUserLogin (CThostFtdcRspUserLoginField *resp,
                   CThostFtdcRspInfoField *info,
                   int req_id, bool is_last)
  {
    printf ("TRADER: %d: %s\n", info->ErrorID, info->ErrorMsg);
  }
};
 
CThostFtdcTraderApi *user_api;
SimpleHandler *simple_handler;
 
extern "C"
{
 
void
init ()
{
  void *trader_user_handle = dlopen ("libthosttraderapi.so",
                     RTLD_LAZY | RTLD_DEEPBIND);
  if (trader_user_handle == 0)
    {
      fprintf (stderr, "Cannot load libthosttraderapi.so\n");
      exit (-1);
    }
 
  user_api = CThostFtdcTraderApi::CreateFtdcTraderApi ();
 
  simple_handler = new SimpleHandler (user_api);
  user_api->RegisterSpi (simple_handler);
  user_api->SubscribePrivateTopic (THOST_TERT_RESUME);
  user_api->SubscribePublicTopic (THOST_TERT_RESUME);
  user_api->Init ();
}
 
void
finalize ()
{
  user_api->RegisterSpi (0);
  user_api->Release ();
  delete simple_handler;
  simple_handler = 0;
}
 
}
x.cc
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <unistd.h>
#include <dlfcn.h>
 
using namespace std;
 
int
main (int argc, char *argv[])
{
  void *trader_handle = dlopen ("libtrader_test.so", RTLD_LAZY);
  if (trader_handle == 0)
    {
      fprintf (stderr, "Cannot load libtrader_test.so\n");
      exit (-1);
    }
 
  void (*trader_init)() = (void (*)()) dlsym (trader_handle, "init");
  void (*trader_finalize)() = (void (*)()) dlsym (trader_handle, "finalize");
  trader_init ();
 
  void *md_handle = dlopen ("libmd_test.so", RTLD_LAZY);
  if (md_handle == 0)
    {
      fprintf (stderr, "Cannot load libmd_test.so\n");
      exit (-1);
    }
 
  void (*md_init)() = (void (*)()) dlsym (md_handle, "init");
  void (*md_finalize)() = (void (*)()) dlsym (md_handle, "finalize");
  md_init ();
 
  sleep (3);
 
  md_finalize ();
  trader_finalize ();
 
  return 0;
}
原创文章,转载请注明: 转载自勤奋的小青蛙
本文链接地址: 【谨慎使用】解决CTP在Linux开发中double free

2017年3月28日 星期二

Using systemd to restart core dump C++ program for debugging in CentOS 7


Core Dump Generation

How to write a crashing C++ program?
vi a.cpp
#include <string.h>
#include <chrono>
#include <thread>
int main() {
    std::this_thread::sleep_for(std::chrono::seconds(10)); // 10 or 120
    char* p = NULL;
    strcpy(p, "copy string to null pointer and cause crash");
    return 0;
} // g++ -g a.cpp -o a.out -std=c++11
Segmentation fault (core dumped)

Why there is no core dump file generated?
ulimit -c
0
ulimit -a | grep core
core file size          (blocks, -c) 0

How to enable core file size?
ulimit -c unlimited
ulimit -a | grep core
core file size          (blocks, -c) unlimited

What is the file size of the core dump?
417KB
./a.out
ll core.*
rw-------. 1 ricky ricky 417792 Mar 28 11:06 core.1258





Core Dump Analysis



How to setup dbg for debugging?

sudo debuginfo-install glibc-2.17-106.el7_2.8.x86_64 libgcc-4.8.5-11.el7.x86_64 libstdc++-4.8.5-11.el7.x86_64




How to analyse the core dump file?
gdb a.out core.1258
bt

GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-80.el7

Copyright (C) 2013 Free Software Foundation, Inc.

License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software: you are free to change and redistribute it.

There is NO WARRANTY, to the extent permitted by law.  Type "show copying"

and "show warranty" for details.

This GDB was configured as "x86_64-redhat-linux-gnu".

For bug reporting instructions, please see:

<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/ricky/crash/a.out...done.

[New LWP 1258]
Core was generated by `./a.out'.
Program terminated with signal 11, Segmentation fault.
#0  0x00000000004005cb in main () at crashing.cpp:6
6           strcpy(p, "copy string to null pointer and cause crash");


(gdb) print p

$1 = 0x0

Background of systemd

What is init?
init is the default PID 1 in CentOS 6
cat /etc/*release
CentOS release 6.6
ps -ef | head -2
UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0  2016 ?        00:01:03 /sbin/init

What is systemd?
systemd is the default PID 1 in CentOS 7
cat /etc/*release
CentOS Linux release 7.2.1511
ps -ef | head -2
UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0  2016 ?        00:09:54 /usr/lib/systemd/systemd --system --deserialize 26

Restarting the crashing program with systemd

How to create a daemon?
sudo vim /etc/systemd/system/a.service
[Unit]
Description=service a.service here
[Service]
Type=simple
Restart=always
WorkingDirectory=/home/ricky/a/
ExecStart=/home/ricky/a/a.out
[Install]
WantedBy=multi-user.target

How to start a daemon?
sudo systemctl daemon-reload
sudo systemctl status -l a
● a.service
   Loaded: loaded (/etc/systemd/system/a.service; disabled; vendor preset: disabled)
   Active: inactive (dead)
sudo systemctl start a
sudo systemctl status -l a
● a.service
   Loaded: loaded (/etc/systemd/system/a.service; disabled; vendor preset: disabled)
   Active: active (running) since Tue 2017-03-28 11:46:36 HKT; 108ms ago
 Main PID: 2056 (a.out)
   CGroup: /system.slice/a.service
           └─2056 /home/ricky/a/a.out
ps -ef | grep a.out

How to enable core dump for systemd daemon?
sudo vi /etc/systemd/system.conf
DefaultLimitCORE=infinity
systemctl daemon-reexec



How to make a b.service that requires a.service?
sudo vim /etc/systemd/system/b.service
[Unit]
Description=service b.service here
Requires=a.service
[Service]
Type=simple
Restart=always
WorkingDirectory=/home/ricky/b/
ExecStart=/home/ricky/b/b.out
ExecStartPre=/usr/bin/date
ExecStartPost=/usr/bin/date
ExecReload=/usr/bin/date
ExecStop=/usr/bin/date
ExecStopPost=/usr/bin/date
[Install]
WantedBy=multi-user.target

sudo systemctl daemon-reload
sudo systemctl status -l b
sudo systemctl start b

sudo systemctl status -l a
sudo systemctl status -l b
ls && ps -ef | grep b.out


sudo systemctl stop a
sudo systemctl stop b

sudo systemctl start a
sudo systemctl start b

sudo systemctl status -l a
sudo systemctl status -l b


ExecStartPre=/bin/bash -c 'pwd; ulimit -c unlimited; rm ./*.log; python -c "print(123)" '?
Process: 23069 ExecStartPre=/bin/bash -c pwd; ulimit -c unlimited; rm ./*.log; python -c "print(123)"  (code=exited, status=0/SUCCESS)
Apr 07 12:41:05 pc009 bash[23069]: /home/ricky/b
Apr 07 12:41:05 pc009 bash[23069]: rm: cannot remove ‘./*.log’: No such file or directory
Apr 07 12:41:05 pc009 bash[23069]: 123

ExecStartPre=/usr/bin/sleep 5?
Yes. It will be exited with status 0/SUCCESS.
Process: 20924 ExecStartPre=/usr/bin/sleep 5 (code=exited, status=0/SUCCESS)

ExecStartPre=/usr/bin/sleep 1; /usr/bin/sleep 1; /usr/bin/sleep 3?
No. Multiple commands not allowed.
Process: 21025 ExecStartPre=/usr/bin/sleep 1; /usr/bin/sleep 1; /usr/bin/sleep 3 (code=exited, status=1/FAILURE)

ExecStartPre=rm /home/ricky/b/md/*.con ?
No. It is because rm is not absolute. You will see below error.
[/etc/systemd/system/b.service:8] Executable path is not absolute, ignoring: rm /home/ricky/b/md/*.con

ExecStartPre=/usr/bin/rm /home/ricky/b/md/*.con ?
No. It will be exited with status 1/FAILURE
Process: 20827 ExecStartPre=/usr/bin/rm /home/ricky/b/md/*.con (code=exited, status=1/FAILURE)


How to make a sleeping service?
sudo vim /etc/systemd/system/sleeping.service
[Unit]
Description=sleeping service
[Service]
Type=simple
User=ricky
Restart=always
WorkingDirectory=/home/ricky/
ExecStart=/usr/bin/sleep 10
[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload && sudo systemctl restart sleeping && sudo systemctl status sleeping
● sleeping.service - sleeping service
   Loaded: loaded (/etc/systemd/system/sleeping.service; disabled; vendor preset: disabled)
   Active: active (running) since Fri 2017-04-21 16:43:19 HKT; 31ms ago
 Main PID: 28266 (sleep)
   CGroup: /system.slice/sleeping.service
           └─28266 /usr/bin/sleep 10

Apr 21 16:43:19 pc009 systemd[1]: Started sleeping service.
Apr 21 16:43:19 pc009 systemd[1]: Starting sleeping service...


abcdefghijklmnopqrstuwxyz
abcdefghijklmnopqrstuwxyz
abcdefghijklmnopqrstuwxyz
abcdefghijklmnopqrstuwxyz
abcdefghijklmnopqrstuwxyz
abcdefghijklmnopqrstuwxyz



2007 to 2023 HP and Dell Servers Comparison

  HP Gen5 to Gen11  using ChatGPT HP ProLiant Gen Active Years CPU Socket Popular HP CPUs Cores Base Clock Max RAM Capacity Comparable Dell ...