Giter Club home page Giter Club logo

ffi's Introduction

FFI for kdb+

FFI (foreign function interface) is a mechanism by which a program written in one programming language can call routines or make use of services written in another. Some programs may not know at the time of compilation what arguments are to be passed to a function. For instance, an interpreter may be told at run-time about the number and types of arguments used to call a given function. Libffi can be used in such programs to provide a bridge from the interpreter program to compiled code.

ffikdb is an extension to kdb+ for loading and calling dynamic libraries using pure q.

The main purpose of the library is to build stable interfaces on top of external libraries, or to interact with the operating system from q. No compiler toolchain or writing C/C++ code is required to use this library.

You don't need to write C code, but you do need to know what you are doing. You can easily crash the kdb+ process or corrupt in-memory data structures with no hope of finding out what happened. For example, when q callback function is passed to foreign function and the q function failed, user might be able to see error message in console but as the foreign function cannot handle q error, the execution crashes and leads to entire application crash. Or if user passed valid but wrong tye characters to q callback, interna conversion failure of q cannot be handed and application crashes without any error message.

No support is offered for crashes caused by use of this library.

We are grateful to @abalkin for allowing us to adapt and expand on his original codebase.

Installation Documentation

๐Ÿ‘‰ Install guide

API Documentation

๐Ÿ‘‰ API reference

Examples

๐Ÿ‘‰ Example guide

Status

The FFI interface is provided here under an Apache 2.0 license.

If you find issues with the interface or have feature requests please consider raising an issue here.

If you wish to contribute to this project please follow the contributing guide here.

Unsupported Functionality

Foreign functions taking a struct do not work properly (can cause crash). For example, a function taking K pointer does not work.

ffi's People

Contributors

5jt avatar abalkin avatar charlieskelton-kx avatar mshimizu-kx avatar sshanks-kx avatar sv avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ffi's Issues

Accessing the K Object length

extern "C" void g(void* ptr)
{
printf("%s: (%p)\n", PRETTY_FUNCTION, ptr);
K k = static_cast(ptr);
printf("k = %p\n", k);
printf("%p->t = %d\n", k, k->t);
printf("%p->n = %lld\n", k, k->n);
printf("---\n");

q)g[enlist ([]til 10)]
void g(void*): (0x7f7929901bd0)
k = 0x7f7929901bd0
0x7f7929901bd0->t = 98
0x7f7929901bd0->n = 140158365392032

For some reason n is coming as some nonsensical number. The type t is correct. I would expect n to be 10 or 1 if it is the number of columns.

Is there a way of passing the raw k object using ffi?

Errors building without setting various cmake options

I had some troubles building this library, and with help from Simon managed to get it built by adding the below to CMakeLists.txt. This should probably be included by default when I clone the repo.

set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99 -D_GNU_SOURCE")

Is it possible to call a function like execve?

I'm having a pickle of a time trying to call execve. I can't for the life of me figure out the right way to construct and pass the argv and envp arguments to the function so that they are arrays of pointers.

Is this possible? How are you supposed to build the array of character pointers?

Segfault error when calling bound function twice

Hi,
Whenever the q function bound to isConnected() function from this c++ project is called for the second time, q crashes with segmentation fault error:

$ q
KDB+ 3.6 2018.06.14 Copyright (C) 1993-2018 Kx Systems
l64/ 16(16)core 64537MB dk s110519 208.110.70.170 EXPIRE 2019.11.06 daniel.krizian@gmail.com KOD #4162453

q)\l ffi.q;

q)lib:{`qib.0.0.1.so,x};

q)bind:{[f;a;r] '[.ffi.bind[ lib f;a;r],[;::]::;enlist]};

q)isConnected:bind[ ;"";"b"] `$"_ZNK7EClient11isConnectedEv";

q)isConnected[]
0b

q)isConnected[]
Sorry, this application or an associated library has encountered a fatal error and will exit.
If known, please email the steps to reproduce this error to tech@kx.com
with a copy of the kdb+ startup banner and the info printed below.
Thank you.
SIGSEGV: Fault address 0x40
/opt/conda/envs/q/q/l64/q() [0x48483b]
/lib/x86_64-linux-gnu/libpthread.so.0(+0xf890) [0x7f1a752a9890]
/usr/lib/qib.0.0.1.so(_ZNK7EClient11isConnectedEv+0) [0x7f1a75fa1400]
/opt/conda/envs/q/q/l64/../../lib/libffi.so.6(ffi_call_unix64+0x4c) [0x7f1a6e949ec0]
/opt/conda/envs/q/q/l64/../../lib/libffi.so.6(ffi_call+0x22d) [0x7f1a6e94987d]
/opt/conda/envs/q/q/l64/ffi.so(call+0x296) [0x7f1a6eb4ef67]
/opt/conda/envs/q/q/l64/q() [0x483bf9]
/opt/conda/envs/q/q/l64/q() [0x4441a6]
/opt/conda/envs/q/q/l64/q() [0x444185]
/opt/conda/envs/q/q/l64/q() [0x4445d5]
/opt/conda/envs/q/q/l64/q() [0x41da94]
/opt/conda/envs/q/q/l64/q() [0x443eee]
/opt/conda/envs/q/q/l64/q() [0x4445d5]
/opt/conda/envs/q/q/l64/q() [0x41da94]
/opt/conda/envs/q/q/l64/q() [0x443eee]
/opt/conda/envs/q/bin/q: line 3: 22054 Segmentation fault      numactl --physcpubind=0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 $QHOME/l64/q "$@"

qib.0.0.1.so library is compiled from this source (https://github.com/danielkrizian/qib/), which is latest version of the Interactive Brokers TWS API.

Note: _ZNK7EClient11isConnectedEv is mangled c++ symbol of the function EClient::isConnected():

$ nm  /usr/lib/qib.0.0.1.so | c++filt | grep isConnected
0000000000017400 T EClient::isConnected() const

$ nm  /usr/lib/qib.0.0.1.so | grep _ZNK7EClient11isConnectedEv
0000000000017400 T _ZNK7EClient11isConnectedEv
Compilation details

Snippet from my CMakeCache.txt :

//CXX compiler
CMAKE_CXX_COMPILER:FILEPATH=/opt/conda/envs/q/bin/x86_64-conda_cos6-linux-gnu-c++

//Flags used by the CXX compiler during all build types.
CMAKE_CXX_FLAGS:STRING=-fvisibility-inlines-hidden -std=c++17 -fmessage-length=0 -march=nocona -mtune=haswell -ftree-vectorize -fPIC -fstack-protector-strong -fno-plt -O2 -ffunction-sections -pipe

//C compiler
CMAKE_C_COMPILER:FILEPATH=/opt/conda/envs/q/bin/x86_64-conda_cos6-linux-gnu-cc

//Flags used by the C compiler during all build types.
CMAKE_C_FLAGS:STRING=-march=nocona -mtune=haswell -ftree-vectorize -fPIC -fstack-protector-strong -fno-plt -O2 -ffunction-sections -pipe

//Flags used by the linker during all build types.
CMAKE_EXE_LINKER_FLAGS:STRING=-Wl,-O2 -Wl,--sort-common -Wl,--as-needed -Wl,-z,relro -Wl,-z,now -Wl,--disable-new-dtags -Wl,--gc-sections

//Flags used by the linker during the creation of shared libraries
// during all build types.
CMAKE_SHARED_LINKER_FLAGS:STRING=-Wl,-O2 -Wl,--sort-common -Wl,--as-needed -Wl,-z,relro -Wl,-z,now -Wl,--disable-new-dtags -Wl,--gc-sections

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.