The Boston Diaries

The ongoing saga of a programmer who doesn't live in Boston, nor does he even like Boston, but yet named his weblog/journal “The Boston Diaries.”

Go figure.

Thursday, Debtember 01, 2016

Meow meow meow meow …

Ah, Debtember 1st! Time once again for missing National Novel Generation Month.

I never did come up with decent idea for a generated novel. And I never did get around to doing that English As She Is Spoke idea. I could have written a program to write the word “meow” 50,000 times, and while it's within the letter of the rules it does feel like cheating. Then again, this year's submissions were not up to the caliber of last year's submissions. Perhaps the idea of computer generated novels has run its course? Who knows?

There's always next year.

Saturday, Debtember 17, 2016

Really, POSIX? Really? memset() isn't async-signal-safe? How is it not safe? … oh … that's why

Just because, I found myself rewriting some code that dumps memory. The current version is too limiting because it only dumps to a file, and there have been several times I've wanted to dump memory to something other than a file, like to syslog() (for instance, my crashreport() function which dumps some memory as part of its report).

So I got the new code written, and the core of it is “async-signal-safe.” This is important because functions that are not async-signal-safe can not be called from a signal handler (the cause of my hardest-to-find bug yet). I got to the point where I needed to add some padding to the output and the easiest way to do that is to call memset().

Now a curious thing about memset()—if you check the list of async-signal-safe functions one can call, you will not find memset() among the listed functions. Which is odd, because the function itself does very little, little more than:

void memset(void *s,int c,size_t n)
{
  unsigned char *m = s;

  while(n--)
    *m++ = c;

  return s;
}

This isn't like malloc(), which could be interrupted as it's working and leave critical data structures in an indeterminate state such that a subsequent call to malloc() from within the signal handler could blow up. No, it's self contained and a call to memset() won't interfere with an already interrupted call to memset(). It's curious that memset() isn't considered async-signal-safe (along with memcpy(), memmove() and strcpy()). It just doesn't make sense.

Until it does.

Here's the upshot: since memset() is a standard C function, a C compiler is free to do anything it wants as long as the end result is the same—the memory is set to a given value. So, assuming a standard Intel CPU, it can compile this:

int array[2];
memset(array,0,sizeof(array));

into the following assembly language:

	xor	eax,eax		; set EAX to 0
	mov	[array],eax	; zero out array[0]
	mov	[array+4],eax	; zero out array[1]

or (and this is getting close to the issue at hand):

int array[1000];
memset(array,0,sizeof(array));

can be compiled into

	mov	edi,array	; point to array
	mov	ecx,1000	; there are 1000 entries
	xor	eax,eax		; each being set to 0
	rep	stosd		; now do it

The problem is memmove(), which can handle copying memory from overlapping regions. Typically, you just copy memory from low memory to high but when the regions overlap, you can't do that. Instead, you have to copy from high memory to low. Again, the Intel CPU can deal. So, code like:

int array[1000];
memmove(&array[100],&array[0],sizeof(int) * 900);

could turn into:

	mov	esi,array + 999 * 4	; point to last element in array
	mov	edi,array +  99 * 4	; point to final destination in array
	mov	ecx,900			; this many integers
	std				; !!! make sure we copy from high to low
					; HERE BE DRAGONS!
	rep	movsd			; copy data
	cld				; clear direction flag

And the issue shows itself. There's a flag in the Intel CPU that tells it which way to copy memory. If the flag is not set, then any memory copy (or memory setting) goes from low to high (the index registers ESI and EDI are incremented); otherwise if the flag is set, then any memory copy (or memory setting) goes from high to low (the index registers are decremented). Generally, the flag is usually cleared except for the few cases where it's required to be set. After the STD instruction is executed but prior to the CLD instruction being executed, if a signal is delivered to the program, the kernel will interrupt the program and call a signal handler. And if in the signal handler, memset() is called, the code is probably expecting the direction flag to not be set, so the memset() code will now run in the reverse direction.

The real issue here is that the program state (including the direction flag) is saved upon entering the kernel. When the kernel transitions back to usercode, that state is then restored. The signal handler isn't technically a thread (although it executes asynchronously as a thread) so it doesn't have its own state (although in POSIX, a signal handler can have its own stack, but that's entirely optional and I'm digressing). You could argue that a program (technically a process or thread) could have two states—a normal state and a signal handler state, but the problem there is that signal handlers can be interrupted by yet another signal handler and the issue rears its ugly head yet again.

I couldn't find any current information about this problem (and where is the bug? Is it in the compiler? The operating system? The standards bodies?) and so alas, even a simple function like memset() might not be async-signal-safe.

Sigh.

Update on January 3rd, 2017

Wednesday, Debtember 21, 2016

Rogue One: A Star Wars Story

Bunny and I went to see “Rogue One: A Star Wars Story”. Unlike the other Star Wars movies, there's no text crawl at the beginning (it surprised me). You get the Lucas Film logo, the words “Star Wars” and the standard “A long long time ago in a galaxy far away” but no text crawl. I think this is intentional as it's set in the same universe but outside the main story line we know and love.

It's actually another another prequel, taking place just a day or two prior to the 1977 release of “Star Wars” (or “Star Wars: A New Hope” if you want to be pedantic) and concerns itself with the rebels aquiring the plans to the Death Star (and in the process, explains the flaw that ended up destroying it).

Visually, the movie is stunning (like all Star Wars movies), but I found the first half a bit slow moving and the music absolutely forgettable—vaguely … Star Warsish … but … not? Forest Whitaker was wasted, as far as I'm concerned, as we got none of his character's backstory, or the backstory between his character and the main character Jyn Erso (Felicity Jones). What we do learn is told to us, not shown.

Unfortunately, most of the characters are not given any backstory. It's a shame, especially given Chirrut Îmwe (played by Donnie Yen), a blind monk who believes in the Force (and is not a Jedi), and his friend Baze Malbus (played by Wen Jiang) a large gun toting soldier. These are fantastic characters, but again, not much is said about them.

Now, the second half? Once the actual plot kicks in? Oh my God! It's totally worth slugging through the first half. The battle to extract the Death Star plans is incredible. And dark. Make no mistake, this is a “war film” that just happens to be set in the Star Wars Universe. It's the darkest Star Wars movie yet (and mild spoilers if you can read between the lines and think a bit about why we never heard about these characters before).

Darth Vader does make an appearance. He's in the movie maybe a total of three minutes. The first minute (about half way through) is menancing. His final two minutes? This is a Sith Lord. Someone you do not want to meet, period. This is the monster we're told about in the other movies.

So yes, it's worth seeing. Come for the battle. Stay for Darth Vader. And just be glad he's a fictional character.


Notes on an overheard conversation while in a car listening to Christmas music

♫You better watch out
You better not cry
Better not pout
I'm telling you why
Santa Claus is coming to town♫

“Oh, it's Pat Boone!”

“Sounds like it was prior to his heavy metal album.”

“What?”

“Yes. Heavy metal album.”

“Are you yanking my chain again?”

“Nope. Look it up.”

“Okay, I will … Pat Boone … heavy metal … I hate you.”

Saturday, Debtember 24, 2016

Notes on an overheard conversation about an unfortunate event

“Oh dear!”

“What?”

“A 91 year old man was tased by police.”

“Oh man … ”

“And he had Alzheimer's.”

“Well, at least he won't remember it.”

“You are bad! Bad! No Christmas cookies for you!”

Monday, Debtember 26, 2016

Kris Kringle is down! I repeat, Kris Kringle is down!

I was talking with Bunny when my hand came down and knocked one of the many Santas she had on display. We both saw it falling, and while the entire event played out in slow motion, with seconds stretching out to minutes, there was nothing we could do to move fast enough save Santa from crashing onto the floor.

His left leg snapped off at the knee, while his right leg shattered into splintered shards across the floor.

[Ouch!  That had to hurt!]

Bunny said not to worry about it, accidents happen. But I felt bad about breaking Santa. Who wants to break Santa?

So I spent the next few hours putting the 3-D jigsaw puzzle of a leg back together again. It took time because I had to not only figure out what piece went where, but glue the piece in, and wait until it dried hard enough to handle for the next piece.

[“Yeah, I still can't feel muh legs, but thanks anyway!”]

And there, good as new. Nothing a little black paint can't hide.

Thursday, Debtember 29, 2016

Notes on an overheard conversation while in a car listening to Christmas music, part II

[Music is playing on the car radio]

♫Sleigh bells ring
are you listening
in the lane
snow is glistening
A beautiful sight
we're happy tonight
walking in a winter wonderland

Needs more cowbell.”

Saturday, Debtember 31, 2016

A critical obituary for a rebel general

Leia Organa, the politician and revolutionary who led the defeat of the Galactic Empire, died after a short illness. She was 60 years old. Hers was a life laced with controversy concerning everything from her tactics to her very ancestry, but her intelligence, commitment to the Republican cause, and place at the heart of the Rebellion, and later the Resistance against Neo-Imperialism, remains the indisputable core of her legacy.

Via inks, Leia Organa: A Critical Obituary – you're always being judged

Sigh … as if 2016 wasn't bad enough with the death of Carrie Fisher. Now you have to take Princess Leia as well?

Good riddance, 2016!


Notes on a conversation that must be going on outside while the author cowers in fear in the back of the closet under a pile of blankets and a fire extinguisher in hand as the neighbors light off huge fireworks

“Hold my beer! Now watch this!

Obligatory Picture

[Don't hate me for my sock monkey headphones.]

Obligatory Links

Obligatory Miscellaneous

You have my permission to link freely to any entry here. Go ahead, I won't bite. I promise.

The dates are the permanent links to that day's entries (or entry, if there is only one entry). The titles are the permanent links to that entry only. The format for the links are simple: Start with the base link for this site: http://boston.conman.org/, then add the date you are interested in, say 2000/08/01, so that would make the final URL:

http://boston.conman.org/2000/08/01

You can also specify the entire month by leaving off the day portion. You can even select an arbitrary portion of time.

You may also note subtle shading of the links and that's intentional: the “closer” the link is (relative to the page) the “brighter” it appears. It's an experiment in using color shading to denote the distance a link is from here. If you don't notice it, don't worry; it's not all that important.

It is assumed that every brand name, slogan, corporate name, symbol, design element, et cetera mentioned in these pages is a protected and/or trademarked entity, the sole property of its owner(s), and acknowledgement of this status is implied.

Copyright © 1999-2017 by Sean Conner. All Rights Reserved.