emscripten icon indicating copy to clipboard operation
emscripten copied to clipboard

Embind overload table containing 'undefined' entry

Open AlexPerrot opened this issue 11 years ago • 11 comments

Hi,

I have a strange issue with the overload tables used by embind. I am binding simple overloads to get and set the value of an attribute, as in this code, but with more functions :

class Foo {
  float _x;

public:
  const float x() const {
    return _x;
  }

  Foo& x(const float x) {
    _x = x;
    return *this;
  }
};

EMSCRIPTEN_BINDINGS(overload) {
  class_<Foo>("Foo")
    .function("x", select_overload<Foo&(const float)>(&Foo::x))
    .function("x", select_overload<const float(void)const>(&Foo::x)) ;
}

This creates an overload table with two entries named '0' and '1' as expected. The issue I face is that when dealing with a bigger codebase, the first entry added in the overload table has the key 'undefined'. Even when interverting the two bindings, it is always the first one added that gets an 'undefined' key. However, I was not able to recreate this with a minimal code, so I suspect that the size of the codebase as something to do with triggering this bug.

I can also add that this always happens with functions using native types. However, with functions using user-defined types, this issue only happens if I put the binding of the type before the binding of the overloaded functions using it.

I will dig the embind code to try and find what's going on...

AlexPerrot avatar May 04 '15 18:05 AlexPerrot

So, digging a little in the embind code, this issue seems to come from line 98 of embind.js :

proto[methodName].overloadTable[prevFunc.argCount] = prevFunc;

Sometimes, prevFunc.argCount can be undefined. This explains the overload table entry...

AlexPerrot avatar May 04 '15 19:05 AlexPerrot

I have found what's going wrong here, but not sure why it happens and I still can't reproduce it in a minimal code example.

Anyway, here's the explanation : When registering multiple overloads for a function, several things need to happen :

  1. When the initial function is registered, an unboundTypesHandler is set as prototype[methodName]. Its argCount is set to its correct value.
  2. When an overload is registered for this function, ensureOverloadTable is called to create the overload table. This will take the initially registered function and use its argCount property to put it at the right place in the newly created overload table. It then proceeds to insert the desired overload in the table.
  3. Finally, the unboundTypesHandler are replaced by the correct member functions, either directly or in the overload table if it exists. Notice that the argCount property of this member function is not set and so, remains undefined.

In a normal situation, 1, 2 and 3 happen in this order and everything works correctly. However, in my code, if registering two overloads a and b, the order is 1a, 3a, 2b, 3b. This means that when ensureOverloadTable is called at step 2, the initial unboundTypesHandler and its argCount property have already been replaced by the correct member function whose argCount is undefined.

Systematically setting argCount on the member function in step 3 fixes the problem, but I don't know if it is the right thing to do or if it would be better to try and prevent those steps from running in the wrong order.

AlexPerrot avatar May 05 '15 08:05 AlexPerrot

I think it would be good to understand why this is happening in the wrong order.

waywardmonkeys avatar May 08 '15 11:05 waywardmonkeys

From what I understand, in a normal execution, the only thing that makes step 3 to run after step 2 is that the native types (in the example of my first post, float and void) are not yet registered.

In the case of my larger codebase, when step 2 runs for the first overload, the native types are already registered, so step 3 runs immediately.

So far, that is the behaviour I can't reproduce with a minimal example.

AlexPerrot avatar May 08 '15 12:05 AlexPerrot

Some news about this issue, I have changed the way I link the bindings file (from a normal object file to a static .bc library) and the issue disappeared.

I will close this issue for now, hoping it will not happen again.

AlexPerrot avatar May 11 '15 14:05 AlexPerrot

The issue reappeared on another target this morning. This does not seem to be a compiling/linking issue, since I link the exact same bindings file with the exact same compiling process, only with two different applications.

Also, I have been able to reproduce my issue with a minimal code :

#include <emscripten/bind.h>

using namespace emscripten;

struct Bar {
  Bar() {}
};

struct Foo {
  Bar bar() {return Bar();}
  Bar bar(Bar b) {return b;}
};

EMSCRIPTEN_BINDINGS(overload) {

  class_<Bar>("Bar").constructor();

  class_<Foo>("Foo")
    .constructor()
    .function("bar", select_overload<Bar()>(&Foo::bar))
    .function("bar", select_overload<Bar(Bar)>(&Foo::bar))
    ;
}

With this code, the two overloads of Foo::bar appear in the overload table. The first one has undefined as its key and the second one has 1. When moving the binding of Bar after the binding of Foo, everything works fine.

I hope the results will be consistent for other people, as this issue seems hard to reproduce...

AlexPerrot avatar May 12 '15 08:05 AlexPerrot

I made a pull request with the only fix I could find : #3450.

Maybe the current embind code relies on all overloads being registered before every dependent type is registered, but this might not always be the case.

AlexPerrot avatar May 12 '15 09:05 AlexPerrot

Just a quick update to let you know that the issue is still present for me with latest incoming version (1.36.9).

AlexPerrot avatar Sep 06 '16 13:09 AlexPerrot

This issue came back to bite me a few days back, so I did further research on the subject. I was able to gather a few new elements. Turns out there are actually kind of two issues here.

The general problem

As explained before, the main issue happens when registering overloaded functions which only depend on types that are already registered. Let's say there two functions a that depend on type B (but not any built-in type, not even void) :

 #include <emscripten/bind.h>

 using namespace emscripten;

 struct B {
 };

 B a(B b) {return b;};
 B a() {return B();};

 EMSCRIPTEN_BINDINGS(overloadbinding) {

   class_<B>("B").constructor();

   function("a", select_overload<B(B)>(&a));
   function("a", select_overload<B()>(&a));
 }

In this situation, when the first a is registered, all of its dependent types (i.e. only B) are already registered.

Looking at embind.cpp to see what happens : exposePublicSymbol on line 976 creates Module[a] with an unbound types handler and Module[a].numArguments to remember it (although it will not be used later). Then whenDependentTypesAreResolved is called on line 980. All the dependent types have been registered so replacePublicSymbol (line 982) is called immediately. replacePublicSymbol then proceeds to replace the unbound type handler at Module[a] with the real function, since no other overloads are registered. At this point, Module[a].numArguments has been removed.

Now here is what happens when the second a is registered. In exposePublicSymbol : Module[a] already exists, so ensureOverloadTable is called. On lines 97 and 98, it tries to put the previous function prevFunc into the overload table based on prevFunc.argCount, which was never defined !

In this simple case, moving the registration of B after a fixes the problem. However, I think this is should be addressed by embind.

When B is a built-in type

When B is a user-defined type, the user can change the order of registration to get rid of the problem. But B can also be a built-in type (void, int, etc...), which are registered by embind in bind.cpp. If the built-in types are registered before the user bindings, the same issue happens. In the js output, the type registrations from bind.cpp are contained in a function called __GLOBAL__sub_I_bind_cpp, and the user bindings in __GLOBAL__sub_I_<NAME-OF-THE-FILE>_cpp. The order in which those two functions are called depends on their order in the __ATINIT__ array.

The "right" order is __ATINIT__.push({ func: function() { __GLOBAL__sub_I_userbindings_cpp() } }, { func: function() { __GLOBAL__sub_I_bind_cpp() } });. This order is pulled directly from metadata generated by LLVM. When there are more function in __ATINIT__, the order can be changed, thus triggering the issue. I still don't know why LLVM would change the order in which it defines these functions. There was no difference in the building process between a successful and failing build.

When this happens, the user has no way to fix the issue from his code.

Possible solutions

Here are the possible solutions I see :

  • Modify LLVM to get a consistent order in __ATINIT__. I couldn't tell where modifications would be needed, and it seems too deep into LLVM for an issue this simple, so I don't think this is a good solution.
  • Modify emscripten.py to manually move __GLOBAL__sub_I_bind_cpp at the end of __ATINIT__ if it is detected. Slightly better, but still quite a hack IMO.
  • Finally, use the fix I made in PR #3450 (might need to be updated a little, though). Now that I have dug deeper into this issue, I am convinced this was overlooked in embind.js, and should be added.

I hope this was clear enough, don't hesitate to ask me to clarify if needed. I would really like to have news on this matter, as this is very blocking for me.

AlexPerrot avatar Sep 09 '16 13:09 AlexPerrot

Found some time to implement my second suggestion in PR #4717

AlexPerrot avatar Nov 10 '16 15:11 AlexPerrot

Closing, the solution in #3450 is now merged.

AlexPerrot avatar Dec 09 '16 13:12 AlexPerrot