# Packing shared object in mod for ffi

**URL:** <https://forum.beamng.tech/t/packing-shared-object-in-mod-for-ffi/190>\
**Category:** General\
**Tags:** mods\
**Created:** [19 May 2025 13:11 UTC](https://forum.beamng.tech/t/packing-shared-object-in-mod-for-ffi/190 "2025-05-19T13:11:27Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![comejv](https://forum.beamng.tech/user_avatar/forum.beamng.tech/comejv/32/75_2.png) [@comejv](https://forum.beamng.tech/u/comejv)\
**Post date:** [19 May 2025 13:11 UTC](https://forum.beamng.tech/t/packing-shared-object-in-mod-for-ffi/190/1 "2025-05-19T13:11:27Z")

</div>

Hello everyone.  
I have the need to run inference on neural networks inside of a vehicle controller. We have decided to write a small library in C to run the models. The library will be loaded with ffi.load. All of our codebase is packaged as a BNG mod.

My issue is that I cannot seem to find the proper path to give ffi for it to find the shared object I have in my mod. An assertion with FS:fileExists shows that the shared object is accessible, but when using the same path in ffi.load I get not found errors.

The only fix I have found is to reference the source shared object (not the one packaged in the mod) with an absolute path.

Is this expected?

Thank you for your help,  
Côme

---

<div class="post-metadata">

**Author:** ![aivora](https://forum.beamng.tech/user_avatar/forum.beamng.tech/aivora/32/13_2.png) [@aivora](https://forum.beamng.tech/u/aivora)\
**Post date:** [20 May 2025 07:50 UTC](https://forum.beamng.tech/t/packing-shared-object-in-mod-for-ffi/190/2 "2025-05-20T07:50:00Z")

</div>

Hi Côme,  
`ffi.load` uses the native operating system path as the argument. This is expected, as the filesystem virtualization we provide exists mainly for untrusted code security, and `ffi.load` is inherently not secure. If your shared object is located in the BeamNG virtual filesystem (VFS), you can use the `FS:virtual2Native('/virtual/path/to/file')` function to get the absolute path.

---

<div class="post-metadata">

**Author:** ![comejv](https://forum.beamng.tech/user_avatar/forum.beamng.tech/comejv/32/75_2.png) [@comejv](https://forum.beamng.tech/u/comejv)\
**Post date:** [20 May 2025 15:09 UTC](https://forum.beamng.tech/t/packing-shared-object-in-mod-for-ffi/190/3 "2025-05-20T15:09:51Z")

</div>

Thank you for your answer!  
Do we have access to this method in the VELUA VM? I get this error:  
lua/vehicle/controller/xlab/lib/nn.lua:111: attempt to call method ‘virtual2Native’ (a nil value)

```lua
    assert(FS:fileExists('lua/vehicle/controller/xlab/lib/libnn.so'))
    libnn = ffi.load(FS:virtual2Native('lua/common/extensions/xlab/libnn.so'))

```

---

<div class="post-metadata">

**Author:** ![aivora](https://forum.beamng.tech/user_avatar/forum.beamng.tech/aivora/32/13_2.png) [@aivora](https://forum.beamng.tech/u/aivora)\
**Post date:** [21 May 2025 08:12 UTC](https://forum.beamng.tech/t/packing-shared-object-in-mod-for-ffi/190/4 "2025-05-21T08:12:37Z")

</div>

The function is only available in GELua, but you can use [intra-VM communication](https://documentation.beamng.com/modding/programming/virtualmachines/#communication) to send this data to VLua:

GELua:

```lua
assert(FS:fileExists('lua/vehicle/controller/xlab/lib/libnn.so'))
local libnnPath = FS:virtual2Native('lua/common/extensions/xlab/libnn.so')
be:sendToMailbox('libnnPath', libnnPath) 

```

VLua:

```lua
local libnnPath = obj:getLastMailbox('libnnPath')
local libnn = ffi.load(libnnPath)

```

---

<div class="post-metadata">

**Author:** ![comejv](https://forum.beamng.tech/user_avatar/forum.beamng.tech/comejv/32/75_2.png) [@comejv](https://forum.beamng.tech/u/comejv)\
**Post date:** [21 May 2025 21:32 UTC](https://forum.beamng.tech/t/packing-shared-object-in-mod-for-ffi/190/6 "2025-05-21T21:32:48Z")

</div>

That worked, thank you! But because my mod is still in zip form in the virtual FS I had to copy it to the native FS unzipped:

```lua
    local mod_libpath = 'lua/vehicle/controller/xlab/lib/libnn.so'
    local fs_libpath = 'tmp/libnn.so'
    copyfile(mod_libpath, fs_libpath)

    be:sendToMailbox('libnnPath', FS:virtual2Native(fs_libpath))

```

It’s a bit clunky I guess but it works. If you have something better please do share!

(thanks to @Neverless for the help!)

---

<div class="post-metadata">

**Author:** ![aivora](https://forum.beamng.tech/user_avatar/forum.beamng.tech/aivora/32/13_2.png) [@aivora](https://forum.beamng.tech/u/aivora)\
**Post date:** [22 May 2025 08:20 UTC](https://forum.beamng.tech/t/packing-shared-object-in-mod-for-ffi/190/7 "2025-05-22T08:20:32Z")

</div>

Hello,  
I think that is a good solution, the `ffi.load` function does not have access to the virtual filesystem and therefore cannot access .zip contents. Glad that you got it working 🙂
