You are viewing udrepper

Ulrich Drepper - So close, but no cigar [entries|archive|friends|userinfo]
Ulrich Drepper

[ website | My Website ]
[ userinfo | livejournal userinfo ]
[ archive | journal archive ]

So close, but no cigar [Jan. 30th, 2007|08:43 am]
Previous Entry Share Next Entry
[Tags|]

It's nice to see some people actually look at their DSO's and rewrite them to not be resource hogs. One late example is this PCRE code and the optimization done by one Marco Barisione who should be applauded for starting the work. But then this:

const char *_pcre_ucp_names =
  "Any\0"
  "Arabic\0"
  "Armenian\0"
  ...
  "Zs";


This is a global variable. Anybody seeing what is wrong?

What this does is define a variable in .data (it's modifiable) which points to a constant string. This means


  1. An additional variable

  2. More attack points, the variable is writable

  3. An additional relocation

  4. Getting the string address requires a memory load and accessing the string itself requires two memory loads



People, think before writing code! All that is needed here is name for the memory area containing the constant string. I.e.:

const char _pcre_ucp_names[] =
  "Any\0"
  "Arabic\0"
  "Armenian\0"
  ...
  "Zs";


See the difference? This one character removed and two added make all the difference in the world. The binary is smaller (at least 32 bytes on x86-64, more counting the simpler memory access in the actual code), one less relative relocation, faster code at runtime since the code to compute the string address needs no memory access.
linkReply

Comments:
From: (Anonymous)
2007-01-31 11:48 am (UTC)

Fixed

(Link)

Fixed, thanks.

-- Marco Barisione (http://www.barisione.org/)
From: (Anonymous)
2007-02-01 12:11 am (UTC)

(Link)

Use const char* const. Problem solved.
From: udrepper
2007-02-01 09:15 pm (UTC)

No

(Link)

I suggest you pick up a book on compiler construction.