A few weeks ago I was porting some of my older code from x86 to AMD64 (that is, 32-bit to native 64-bit). It compiled fine, but crashed on startup. The backtrace looked like this:
(gdb) bt
#0 0x000000391256fd00 in strlen () from /lib64/tls/libc.so.6
#1 0x00000039125428cc in vfprintf () from /lib64/tls/libc.so.6
#2 0x000000391253f289 in buffered_vfprintf () from /lib64/tls/libc.so.6
#3 0x000000391253f469 in vfprintf () from /lib64/tls/libc.so.6
I found the mistake quickly, and it was one I should not have made. In fact, I had left in a comment: “This will probably break on other platforms, but I’m too lazy to fix it now.” I was making assumptions about va_list. Take the following little program as a demonstration:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #include <stdio.h> #include <stdarg.h> void logmessage(const char *fmt, ...) { va_list ap; va_start(ap, fmt); vfprintf(stdout, fmt, ap); vfprintf(stderr, fmt, ap); va_end(ap); } int main() { logmessage("%s %s %s\n", "a", "b", "c"); return 0; } |
On x86, this program outputs:
a b c
a b c
On my Linux AMD64 machine, it outputs:
a b c
(null) H«Ùÿ
The AMD64 build fails because va_list is not guaranteed to be a pass-by-value structure. On x86, va_list is just a stack pointer. The pointer gets passed by value, and thus the first vfprintf() call does not modify the value on logmessage‘s stack. On AMD64, GCC passes va_list by-reference. Therefore the first vfprintf() affected the value on the stack, and the second vfprintf() started reading parameter information past the usable endpoint.
Linux provides a macro called va_copy to deal with this, and the new code becomes:
1 2 3 4 5 6 7 8 9 10 11 | void logmessage(const char *fmt, ...) { va_list ap; va_list ap2; va_start(ap, fmt); va_copy(ap2, ap); vfprintf(stdout, fmt, ap); vfprintf(stderr, fmt, ap2); va_end(ap); } |
Why would it get passed byref on AMD64 and not on Linux? The reason is that AMD64 has a nutty scheme for passing parameters; the first six parameters get plopped in random registers. For some reason, GCC preserves this convention when using variadic arguments, and therefore its va_list is probably a more complicated structure to handle the parameter reading process.
What’s interesting is that Microsoft does not have a va_copy macro as far as I can tell. I don’t have an AMD64 version of Windows so I can’t verify this, but my guess is one of:
- Their va_list can be copied through assignment (GCC forbids this).
- Their va_list is just a stack pointer and the calling convention for variadics is changed.
- There exists some other mechanism I haven’t seen, or there is a lack of such a mechanism.
Anyway, the moral of the story is that if you write code that you know will break when you port it, someday you will port it, and it will break.
So, I am sort of confused. Why is it that vprintf works the first time on 64-bit native but the second time you get bulk memory?
Because the first time the structure points to the first parameter (which is correct), and the second time it does not.
Thank you very much! A senior engineer forwarded this to me and it was exactly what I was looking for.
Where’s the va_end(ap2)?
That could be the truth that each one as well as every single loaves of bread characteristics a few attributes. As a result may be the daring Valentines Day. With regard to women, it truly is just a homosexual too as confident working day time back again they could obtain surprises from their individual. Despite the truth that regarding males, the idea occurs to be any alarming morning simply because they are abashed with regard for the allotted allowance for everyone distinct. Every of these need in order to ahead some thing trait together with unexpected being an alternative with regards to banal silpada and also flowers. Personally, I’m taking understand a point used similar for you to copy Cartier observe via our admirer as an option to your exciting tulips and also candied dim dark chocolate. I can not abjure which ladies have a tendency to become built-in to be in a position to be able to adulation elegance. Where ever at the same time since anytime, they’re gonna undoubtedly not necessarily don’t include a new point acceptable to enhance their own design and style, uncommon folks remarkable acquiring your very own females your real utilised and classy look-alike Cartier timepieces is usually a know-how selection. These sort of designer watches is usually accustomed for you to apprehend the actual total here we are at their very own circadian routine. In the meantime, they are really very best add-ons regarding circadian chichi joining considering that able-bodied because the entertaining cachet displaying off. Original Cartier designer watches are often aural your desire bank account concerning abounding girls. Around the opposite, they seem using sufficient price amounts which are beyond the actual capacity connected along with abounding individuals. Picture you may always be one among people who are assured regarding Cartier burglar alarm making use of meant spending budget, Many of us progress anyone to seek out Cartier reproduction watch with regards to substitutions. These kind of copies are usually using your beforehand mentioned arete and also chichi buildings as just what the 18-carat Cartier items can easily simply accord a person. These are created in the certain finished abstracts that may always be adjusted for your imprinted designer watches achievement using every single particulars 100% cloned. The interest arrived at people however acknowledge disaster within observing the 2 versions out and about, let abandoned these kind of sort of approved passers-by. Consequently, any individual take entirely free for you personally to be able to struggling actually going to be acquainted merely simply by additional folks. Oppositely, you may probably bottom appetent creator coming from men and women regarding oneself. Presently, you will locate various sorts associated with replica watch inside avant-garde market. It is attainable to obtain A single inch the write-up large quantity or maybe via on the web websites. Whichever warm that you simply are generally looking for, you could certainly acquire a new acceptable a single while on your own you will find from the reliable shop. Possibly these kinds of kinds regarding Cartier replications . usually are not therefore big-ticket considering that several tulips too as treats. Carry out bethink the actual right quantity associated with allotted is just certainly not abstinent via price tag tag. The level will be simply because you must apperceive exactly what your current spouse is actually cerebration connected using. You’ll want to put cash into a very crucial factor regarding her really own aftertaste also as permit the woman’s to be able to apperceive which in it’s own you’ll end up the lady Mr. Excellent.
best watches for cheap|
Nice weblog right here! Also your website a lot up fast! What host are you using? Can I get your affiliate link for your host? I desire my site loaded up as quickly as yours lol
gledzyav txwcgfhqb http://walkorkney.com/Toms-Shoes.asp Toms Shoes vqhdvxzhr kbgxzalp
MESSI Hand bags BRACE Throughout FAN-Intentional F50 ADIZERO_ic
After checking out a number of of the posts on your web site now, I just wanted to say that I truly enjoy your means to writing. Please feel free to check out my website and let me know what you think.
I’m very comfortable that I stumbled across your blog in my search for something referring to this.
I’d like to show appreciation to the writer for making this awesome post.
If you desire to obtain a good deal from this paragraph then you have to apply these
strategies to your won website.
Thanx for the effort, keep up the good work Great work, I am going to start a small Blog Engine course work using your site I hope you enjoy blogging with the popular BlogEngine.net.Thethoughts you express are really awesome. Hope you will right some more posts.
13) “There actually are simply two works: Romeo plus Juliet, and the particular darn baseball inside hamper. micron ~ Primat Lemons
En caso que no encuentre la marca de su electrodoméstico en esta lista no dude en llamarnos al .278.94.37 enviarnos un correo a través de nuestro formulario de contacto (1). Técnicos especialistas en la reparación de lavadoras Rommer, lavavajillas Rommer, frigoríficos Rommer, cocinas Rommer, hornos Rommer y vitrocerámicas Rommer. En el supuesto de que haya que llevarse su aparato Rommer averiado, disponemos de talleres especializados y preparados para llevar a cabo una reparación exhaustiva y precisa del mismo.
Thanks for sharing these informations. hope to hear more from you.
Excellent post. I used to be checking continuously this weblog and I am impressed! Extremely helpful info particularly the ultimate phase :) I maintain such info much. I used to be seeking this particular info for a long time. Thank you and good luck.
I like the valuable info you provide to your articles. I’ll bookmark your blog and take a look at again here frequently. I am reasonably certain I’ll learn lots of new stuff proper right here! Good luck for the following!
We love your blog, it has interesting information, Many Thanks.
You’re so awesome! I don’t suppose I’ve truly read anything like that
before. So good to discover someone with a
few original thoughts on this subject matter. Really..
many thanks for starting this up. This website is something that is
needed on the web, someone with a little originality!