Re: Get the current function name in its C/C++ method body

classic Classic list List threaded Threaded
17 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Get the current function name in its C/C++ method body

Boris Zbarsky
On 1/24/16 12:04 PM, [hidden email] wrote:
> Hello,
>
> Using the following construction
>
> JS_FN("MethodName", ClassName::MethodName, 0, 0),
>
> how to get the function name, "MethodName", in the ClassName::MethodName method body?

Assuming you already set up your CallArgs:

    JS::RootedFunction func(cx, JS_ValueToFunction(cx, args.calleev()));
    JS::RootedString funcName(cx, JS_GetFunctionDisplayId(func));

should work.

-Boris
_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Get the current function name in its C/C++ method body

Boris Zbarsky
On 1/25/16 6:33 AM, [hidden email] wrote:
>          JSAutoByteString bytes;
> char* fn = bytes.encodeUtf8(cx, funcName); /* calls JS_EncodeStringToUTF8 internally */
> if (!fn)
>              return false;
>
> but it seems it tries to free some memory twice, so I get an error when the scope ends. What am I doing wrong?

This should generally work, as should this:

   JSAutoByteString bytes(cx, funcName);
   char* fn = bytes.ptr();

What, if anything, are you doing with that char* pointer once you have it?

-Boris
_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Get the current function name in its C/C++ method body

Kent Williams
More to the point, why don't you know the name of the JSNative function
in that function?

The only reason I can think of is the same C++ function handles multiple
JSNative entry points, but for me that seems like more effort than it's
worth.

On 01/25/2016 08:43 AM, Boris Zbarsky wrote:

> On 1/25/16 6:33 AM, [hidden email] wrote:
>>          JSAutoByteString bytes;
>>         char* fn = bytes.encodeUtf8(cx, funcName); /* calls
>> JS_EncodeStringToUTF8 internally */
>>         if (!fn)
>>              return false;
>>
>> but it seems it tries to free some memory twice, so I get an error
>> when the scope ends. What am I doing wrong?
>
> This should generally work, as should this:
>
>   JSAutoByteString bytes(cx, funcName);
>   char* fn = bytes.ptr();
>
> What, if anything, are you doing with that char* pointer once you have
> it?
>
> -Boris
> _______________________________________________
> dev-tech-js-engine mailing list
> [hidden email]
> https://lists.mozilla.org/listinfo/dev-tech-js-engine

--

*Kent Williams*| C++ Developer
*CourseLeaf from Leepfrog Technologies *

/“The Process of Academic Change”
/319-337-3877 | courseleaf.com <http://www.courseleaf.com/>

/This message contains confidential information and is intended only for
the individual named. If you are not the intended recipient of this
transmission or a person responsible for delivering it to the intended
recipient, any disclosure, copying, distribution, or other use of any of
the information in this transmission is strictly prohibited. Please
notify the sender immediately by e-mail if you have received this e-mail
by mistake and delete this e-mail from your system./

_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Get the current function name in its C/C++ method body

Boris Zbarsky
In reply to this post by Boris Zbarsky
On 1/25/16 10:45 AM, Kent Williams wrote:
> More to the point, why don't you know the name of the JSNative
> function in that function?
>
> The only reason I can think of is the same C++ function handles
> multiple JSNative entry points, but for me that seems like more
> effort than it's worth.

There can be various use cases for this.  For example, the Firefox
binding code shares a single JSNative across many JSFunctions, to save
codesize (because otherwise we'd need literally thousands of JSNatives
instead of just one).  The code I sent using JS_GetFunctionDisplayId is
what our bindings use to display the function name in exceptions from
DOM stuff.

-Boris
_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Get the current function name in its C/C++ method body

Boris Zbarsky
In reply to this post by Boris Zbarsky
On 1/25/16 1:19 PM, [hidden email] wrote:
> I usually need to see its value at debug time, but would be useful to pass it as info for an exception.

No, I mean, do you actually free it or anything?  What, exactly, does
your code look like?

-Boris

_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Get the current function name in its C/C++ method body

Boris Zbarsky
On 1/25/16 2:16 PM, [hidden email] wrote:
>          JSAutoByteString fn(cx, funcName);
>          if (!fn)
>              return false;

OK, and never used after that point?

That's pretty odd.  You might need to step through here and debug what's
going on, because I don't quite see how this can go awry.  Assuming
you're creating your JSFunctions with a name, of course.

-Boris
_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Get the current function name in its C/C++ method body

Boris Zbarsky
In reply to this post by Boris Zbarsky
On 1/25/16 3:00 PM, [hidden email] wrote:
> At the return true; step (last instruction) it will try to free something already deleted by js_free method of JSAutoByteString and the error occurs.

At the return method, I would expect a js_free() call from the
JSAutoByteString destructor and no other js_free calls.

Try breakpointing in js_free and seeing whether there is in fact more
than one call and if so what the stacks look like?

If there is only one call, see what value is being passed to it?

-Boris


_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Get the current function name in its C/C++ method body

Boris Zbarsky
On 1/25/16 11:08 PM, [hidden email] wrote:
> It is called once, crashes from the first time.

OK...

> The value is the right one.

"right" in what sense?

> Probably some shared memory collision occurs, looking in te code it seems no copy of the string is done, but reference, so it is possible to try to free some other's object member?

JS_EncodeStringToUTF8 certainly makes a copy, by calling
StringToNewUTF8CharsZ which calls CharsToNewUTF8CharsZ which very much
copies in both its overloads.  That's if you're using encodeUtf8().

If you're using encodeLatin1 or the JSAutoByteString constructor that
takes a JSString*, that calls JS_EncodeString which calls EncodeLatin1
which allocates a new buffer and copies into it.

So in both cases the char* that ends up in the AutoByteString has just
been allocated and should be freed by js_free.

Is it possible that in your case SpiderMonkey is in one DLL and your
calling code is in another and they have separate heaps (e.g. on Windows
this is not uncommon)?  It looks like js_free as called by
JSAutoByteString is inlined and just calls free(), which will call the
free() of the API consumer DLL.  But the allocation happens in
out-of-line SpiderMonkey code, which may use a different allocator, in
general...  Seems like a bug in JSAutoByteString if this is what's going on.

-Boris
_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Get the current function name in its C/C++ method body

Mihai Dobrescu
I think I've found already a bug report on this: https://bugzilla.mozilla.org/show_bug.cgi?id=1007136
_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Get the current function name in its C/C++ method body

Boris Zbarsky
In reply to this post by Boris Zbarsky
On 1/26/16 1:55 AM, [hidden email] wrote:
> If I use JS_EncodeStringToUTF8 instead of JSAutoByteString, I get no error when I call JS_free for the newly allocated string...

That's JS_free.  That's out-of-line.

JSAutoByteString uses js_free.  Do you get the crash if you use js_free?
  I expect you do.

-Boris

_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Get the current function name in its C/C++ method body

Boris Zbarsky
In reply to this post by Boris Zbarsky
On 1/26/16 6:00 AM, [hidden email] wrote:
> The separate heaps might be the cause, but I don't see why this happens only in this type of object, if true.

Because other places don't use js_free as part of inlined public API.

-Boris
_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Get the current function name in its C/C++ method body

Boris Zbarsky
In reply to this post by Boris Zbarsky
On 1/26/16 10:29 AM, Boris Zbarsky wrote:
> On 1/26/16 1:55 AM, [hidden email] wrote:
>> If I use JS_EncodeStringToUTF8 instead of JSAutoByteString, I get no
>> error when I call JS_free for the newly allocated string...
>
> That's JS_free.  That's out-of-line.
>
> JSAutoByteString uses js_free.  Do you get the crash if you use js_free?
>   I expect you do.

And do you stop crashing if you change JSAutoByteString::clear to use
JS_free?

-Boris
_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Get the current function name in its C/C++ method body

Boris Zbarsky
On 1/27/16 1:33 AM, [hidden email] wrote:
> Let's say I take it as it is, after reading myself some theory on how things work regarding heaps and inline functions, but please give me some clue on why calling the standard free function leads to the same crash?

The short summary is that when you call free you're calling a function
provided by the C or C++ standard library.  But _which_ standard
library?  In general, DLLs can link to different standard libraries.
And even if you're using the same standard library you may still have
separate per-DLL heaps. The answers on
http://stackoverflow.com/questions/10820114/do-statically-linked-dlls-use-a-different-heap-than-the-main-program 
describe the setup fairly well.  I think in this case it's just a matter
of different standard libraries...

Anyway, the upshot is that given how you're compiling/linking your code
there are effectively two separate memory allocators: the one in
SpiderMonkey and the one the rest of your program is using.  They both
allocate and deallocate using functions named malloc and free, but which
allocator is used depends on which library the call is happening in.

js_free is an inline function that just calls free.  So if you call
js_free at some code location, it's the same as if you just called free
at that same code location, in terms of which DLL the call ends up being
in.  Which is why they have identical behavior.

Does that help?

-Boris
_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Get the current function name in its C/C++ method body

Boris Zbarsky
On 1/27/16 1:58 PM, [hidden email] wrote:
> Sir, thank you but JS_free calls js_free, I understand it's another point where is called that makes the difference

Yep, exactly.

> So, now, I wish I could control the allocation and deallocation in such cases and be able to say when I forget to cleanup some memory versus trying cleaning it up twice versus cleaning it up from the wrong heap... How could I tell?

You can't, except via API contracts....

The API contract for SpiderMonkey is the following:

1)  All memory allocated by SpiderMonkey that is expected to be freed by
its API consumers is allocated via JS_malloc or equivalent (i.e.
malloc() calls from inside the SpiderMonkey DLL).  Consumers must use
JS_free to free the memory.

2)  All memory freed by SpiderMonkey that is expected to be allocated by
API consumers must be freed by JS_free or equivalent (i.e. free() calls
from inside the SpiderMonkey DLL).  Consumers must use JS_malloc to
allocate the memory.

In this context "consumers" includes any code outside the SpiderMonkey
DLL, so it includes both embedder code and inlined SpiderMonkey APIs
that do allocation or deallocation.

The problem you're running into is that some things got added to
SpiderMonkey that violate this API contract.  This wasn't caught in
testing because the normal situations in which SpiderMonkey is tested in
our tree are either standalone or as part of Firefox, and in both cases
there is only one heap involved...

The right thing here is for us to fix SpiderMonkey to actually follow
its API contract.

-Boris

_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Get the current function name in its C/C++ method body

Mihai Dobrescu
In reply to this post by Boris Zbarsky
On Tuesday, January 26, 2016 at 5:41:29 PM UTC+2, Boris Zbarsky wrote:

> On 1/26/16 10:29 AM, Boris Zbarsky wrote:
> > On 1/26/16 1:55 AM, [hidden email] wrote:
> >> If I use JS_EncodeStringToUTF8 instead of JSAutoByteString, I get no
> >> error when I call JS_free for the newly allocated string...
> >
> > That's JS_free.  That's out-of-line.
> >
> > JSAutoByteString uses js_free.  Do you get the crash if you use js_free?
> >   I expect you do.
>
> And do you stop crashing if you change JSAutoByteString::clear to use
> JS_free?
>
> -Boris

Bad bad bad! The issue returned in SpiderMonkey 45, taken from https://people.mozilla.org/~sfink/mozjs-45.0.2.tar.bz2 (refered by https://developer.mozilla.org/en-US/docs/Mozilla/Projects/SpiderMonkey/Releases/45).
_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Get the current function name in its C/C++ method body

Boris Zbarsky
On 5/4/16 11:58 AM, Mihai Dobrescu wrote:
> Bad bad bad! The issue returned in SpiderMonkey 45, taken from https://people.mozilla.org/~sfink/mozjs-45.0.2.tar.bz2 (refered by https://developer.mozilla.org/en-US/docs/Mozilla/Projects/SpiderMonkey/Releases/45).

"Returned" in what sense?

https://bugzilla.mozilla.org/show_bug.cgi?id=1007136 was fixed in
Firefox 47.  The fix was not backported anywhere, so the issue was never
fixed in 45 or any earlier release.

If you were using a locally changed version to work around the bug and
then got a clean upstream version, then the bug might have "returned" in
that sense...

-Boris
_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Get the current function name in its C/C++ method body

Mihai Dobrescu
On Wednesday, May 4, 2016 at 7:05:59 PM UTC+3, Boris Zbarsky wrote:

> On 5/4/16 11:58 AM, Mihai Dobrescu wrote:
> > Bad bad bad! The issue returned in SpiderMonkey 45, taken from https://people.mozilla.org/~sfink/mozjs-45.0.2.tar.bz2 (refered by https://developer.mozilla.org/en-US/docs/Mozilla/Projects/SpiderMonkey/Releases/45).
>
> "Returned" in what sense?
>
> https://bugzilla.mozilla.org/show_bug.cgi?id=1007136 was fixed in
> Firefox 47.  The fix was not backported anywhere, so the issue was never
> fixed in 45 or any earlier release.
>
> If you were using a locally changed version to work around the bug and
> then got a clean upstream version, then the bug might have "returned" in
> that sense...
>
> -Boris

Right.
_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine
Loading...