Open Bug 500874 Opened 15 years ago Updated 2 months ago

Static analysis to find heap allocations that could be stack allocations

Categories

(Developer Infrastructure :: Source Code Analysis, enhancement)

enhancement

Tracking

(Not tracked)

People

(Reporter: jruderman, Unassigned)

References

Details

(Keywords: perf)

Attachments

(2 files, 1 obsolete file)

new & delete in same function --> probably safe to use a stack variable or alloca()

(suggested by pav when i asked "what static analyses could we do to find patterns that are common performance issues?")
Don't we already have problems with blowing the stack?
Dynamically-sized (alloca) stack allocations require case-by-case reasoning about whether it's safe.  Other than that, I believe there's plenty of room on the stack.
Keywords: perf
(In reply to comment #2)
> Dynamically-sized (alloca) stack allocations require case-by-case reasoning
> about whether it's safe.  Other than that, I believe there's plenty of room on
> the stack.

is that portable? Or should this be about looking for malloc/free with a constant size?
We can start by just looking at constant-size mallocs.  If fixing those goes well, we can try variable-size mallocs and see how many of them are safe to convert to alloca.
> safe to use a stack variable or alloca()

i don't think this is good idea. alloca(rand()) crashes. dynamic alloca is dangerous - no overflowing check.

> I believe there's plenty of room on the stack.

don't think so. i have seen crashes caused by recursion - to fix them i needed |ulimit -s BIG| <-- this sets the stack size.
I, Sunita E, am a student of SJCE,Mysore ,India and would like to take up this bug.
Thanks, Sunita.  I suggest reading https://developer.mozilla.org/en/Dehydra and asking any questions you have in irc.mozilla.org #static.
Assignee: nobody → sunsai16
please take a look at the attachment.I need guidance to go further.
Attachment #441706 - Attachment mime type: application/octet-stream → text/plain
(In reply to comment #8)
> Created an attachment (id=441706) [details]
> script to match new and delete calls in a function
> 
> please take a look at the attachment.I need guidance to go further.

Look for new* calls that take a constant as an argument.
Changes:
Added check for arguments:
if(rhs[0].arguments[0].value) { 
//...
}
This makes sure that the values of arguments are constant or in other words available at compile time.
Could you please go through the latest attachment.
If its acceptable, I shall add the necessary warning statements.
Sunita, you need to request review/feedback from a particular person, probably dwitte or taras.
(In reply to comment #12)
> Sunita, you need to request review/feedback from a particular person, probably
> dwitte or taras.

Sure.Thank you.
Attachment #441706 - Attachment is obsolete: true
Attachment #443297 - Attachment is patch: true
Attachment #443297 - Attachment mime type: application/javascript → text/plain
Attachment #443297 - Flags: feedback?(tglek)
Attachment #443297 - Flags: feedback?(tglek)
Comment on attachment 443297 [details] [diff] [review]
Looks for new calls that have constant argument

>var allocs = []
>
>function process_function(decl, body) {
>
>    for (var i in body) {
>        for (var j in body[i].statements) {

Instead of looping directly over body, use iterate_vars() like in bug 522776. 

Otherwise the code doesn't matter, it's just a tool to produce a report. Run it on mozilla, see what sort of code it flags and refine the code until it seems to flag reasonable problems.

To proceed with the bug I recommend that you run this on mozilla, produce a report. Then inspect it to see if the analysis produces too many false positives, if so refine the script. Once you are sure that the analysis might be showing up likely candidates, attach a report to this bug.
Almost all the warnings have been generated in cases with conditions i.e within if statements.I would like to know what should/can be done.
Attachment #458281 - Flags: review?(tglek)
Attachment #458281 - Flags: review?(tglek)
Product: Core → Firefox Build System
Assignee: sunsai16 → nobody
Product: Firefox Build System → Developer Infrastructure
Severity: normal → S3
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Creator:
Created:
Updated:
Size: