Comments (9)
The problem is with the newudata
binding, used to allocate a file watcher object. _Alignof(bee::filewatch::watch) == 16
, and the constructor relies on this property:
(gdb) disas
Dump of assembler code for function _ZN3bee9filewatch5watchC2Ev:
0x0000000000452470 <+0>: push %rbp
0x0000000000452471 <+1>: mov %rsp,%rbp
0x0000000000452474 <+4>: push %rbx
0x0000000000452475 <+5>: push %rax
0x0000000000452476 <+6>: mov %rdi,%rbx
0x0000000000452479 <+9>: xorps %xmm0,%xmm0
=> 0x000000000045247c <+12>: movaps %xmm0,0x20(%rdi) <-- assumes %rdi is 16-bit aligned
0x0000000000452480 <+16>: movaps %xmm0,0x10(%rdi)
In particular, it's using SIMD instructions to zero the object, and this assumes 16-byte alignment. However, the value in %rdi is only 8-byte aligned.
The object was allocated with:
217 template <typename T, typename... Args>
218 T& newudata(lua_State* L, void (*init_metatable)(lua_State*), Args&&... args) {
219 static_assert(udata_has_name<T>::value);
220 int nupvalue = 0;
221 if constexpr (udata_has_nupvalue<T>::value) {
222 nupvalue = udata<T>::nupvalue;
223 }
224 T* o = static_cast<T*>(lua_newuserdatauv(L, sizeof(T), nupvalue));
...
234 return *o;
235 }
i.e., we're taking some udata block from lua and stuffing an object into it. But this assumes that lua's udata allocator is returning sufficiently aligned memory, and that assumption is wrong. (Lua is using realloc() under the hood, which does return 16-byte aligned memory, but lua cuts off some of that for its own use, resulting in misalignment.)
To demonstrate, if I change the code to do this, then I get SIGABRT instead:
...
224 //T* o = static_cast<T*>(lua_newuserdatauv(L, sizeof(T), nupvalue));
225 void *ret = lua_newuserdatauv(L, sizeof(T), nupvalue);
226 if (((uintptr_t)ret & (_Alignof(T) - 1)) != 0) abort();
227 T *o = static_cast<T*>(ret);
...
I'm not sure how best to fix it. I believe the bug is in bee: it's assuming too much of the underlying udata allocator, which it doesn't implement. Probably it should be storing a pointer in udata instead of the whole object.
from bee.lua.
filewatch on freebsd is incomplete. filewatch depends on libinotify-kqueue, but unfortunately libinotify-kqueue doesn't work correctly.
from bee.lua.
It may be incomplete, but I think that doesn't explain the crash. For example, the following C++ program using bee's C++ library works fine and prints the events:
#include <chrono>
#include <iostream>
#include <thread>
#include "filewatch.h"
int main(void)
{
bee::filewatch::watch watch;
watch.set_recursive(true);
watch.set_follow_symlinks(true);
watch.add("/tmp/foo");
for (int i = 1; i <= 10; ++i) {
watch.update();
while (true) {
auto event = watch.select();
if (event.has_value()) {
std::cout << "event for: " << event.value().path << std::endl;
} else {
std::cout << "no events" << std::endl;
break;
}
}
std::this_thread::sleep_for(std::chrono::seconds(1));
}
return 0;
}
This makes me think it's actually an issue with Lua.
from bee.lua.
I don't have a test environment, you can try to fix it.
from bee.lua.
@markjdb I don't know why freebsd's clang assumes filewatch::watch is 16-byte aligned. Maybe using alignas will make clang aware that filewatch::watch is not 16-byte aligned.
from bee.lua.
@markjdb I don't know why freebsd's clang assumes filewatch::watch is 16-byte aligned. Maybe using alignas will make clang aware that filewatch::watch is not 16-byte aligned.
clang is what decides that the class is 16-byte aligned. It raises an error if I try to lower the alignment to 8, but doesn't say why. And the problem affects all classes which are used as udata with an odd number of user values.
from bee.lua.
Well. You can change filewatch's uservalue to 2 if it makes clang work.
from bee.lua.
Well. You can change filewatch's uservalue to 2 if it makes clang work.
It turns out that the problem is there no matter how many uservalues there are. Since bee.lua provides its own lua runtime, I think it might be acceptable to force lua to provide required alignment. I submitted a PR to do that, please let me know if it's acceptable.
from bee.lua.
Fixed.
from bee.lua.
Related Issues (20)
- Improve C++ backward compatibility HOT 1
- Can it be used, or how it can be used for Lua 5.1, LuJit ..? HOT 1
- some tests are failing HOT 2
- Android support? HOT 7
- MacOS filesystem test fails on Linux HOT 3
- Statically linking libstdc++ causes lua-language-server to fail at build HOT 2
- 无法包含头文件
- [Bug Report] test_filesystem::test_appdata_path() failed when building from Homebrew HOT 2
- MacOS filesystem test (filesystem.test_appdata_path) fails on MacPorts HOT 6
- Please add arm architecture HOT 8
- How to use bee with native lua? HOT 1
- time.time()得到负值 HOT 7
- subprocess的使用问题 HOT 4
- Documentation HOT 2
- Fails to build on Fedora 38/Rawhide HOT 2
- 加载dll报错: error loading module 'bee' from file HOT 9
- lua_error linux下是longjump, 会跳过调用cpp对象的析构函数。windows下也容易出现double free UB HOT 5
- filewatch对重复路径有判断吗? HOT 2
- Test fails during compilation on WSL HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from bee.lua.