TweetFollow Us on Twitter

December 93 - KON & BAL'S PUZZLE PAGE

KON & BAL'S PUZZLE PAGE

SOUNDS LIKE TROUBLE

KONSTANTIN OTHMER AND BRUCE LEAK

[IMAGE Puzzle_Page_final_rev1.GIF]

See if you can solve this programming puzzle, presented in the form of a dialog between Konstantin Othmer (KON) and Bruce Leak (BAL) -- and a special guest, Apple summer intern Mike Dodd. The dialog gives clues to help you. Keep guessing until you're done; your score is the number to the left of the clue that gave you the correct answer. These problems are supposed to be tough. If you don't get a high score, at least you'll learn interesting Macintosh trivia.

Mike Hey, guys. I've got one for you I bet you can't get.

KON Well, I haven't been able to get you over to the poker game; maybe I can beat you here. Wanna put your summer salary on it, schoolboy?

BAL Careful, Mike. You're talking to trained professionals here.

Mike OK. We have this true multimedia application that does lots of things: plays movies and sounds and does some GWorld stuff. After a while it crashes with a corrupt heap, usually SysError 33, and sometimes with a bus error. I'm using the latest QuickTime, and I have that snazzy PowerPC QuickDraw extension that installs PowerPC native QuickDraw on 68K-class machines.

BAL Sounds like some quality software you're running there. I doubt it's QuickTime's fault.

KON Yeah, and the PowerPC stuff is pretty awesome. You wouldn't stick us with some stupid application bug, so it's probably a problem with the Sound Manager. Is this that MoveHHi Sound Manager problem? MoveHHi snags the whole stack, and when the sound interrupts come in, the stack overflows into the application space, corrupting the heap. Since the Sound Manager is at a higher interrupt level than the stack sniffer VBL, it never detects the problem. Unsolved Mysteries: Solved!

100 Mike I'm using the new Sound Manager, version 3.0.

BAL I guess Reekes did a good job with compatibility on the new Sound Manager. He even maintained all the bugs!

Mike Reekes swears there's not a line of code the same between the new and old Sound Managers. Besides, the new Sound Manager patches MoveHHi to not use as much stack.

KON Does it happen with the old Sound Manager?

90 Mike Yep. Same thing.

BAL Hmmm. So what are the circumstances around the crash? Do you have a reproducible case?

80 Mike It seems to happen fairly randomly. But it generally occurs when I push a button that plays a sound. Sometimes it happens the first time I push the button; other times I push the button over a hundred times before it crashes.

KON Turn on heap scrambling in the application and system heaps and run something like the MemHell extension, which forces a worst-case memory scenario. That should bring the problem out more frequently. Maybe you can get a reproducible case.

BAL Yeah, and turn on A-trap recording and heap checking so that we can narrow down the problem area.

70 Mike The application is running really slowly now, but the problem doesn't happen any more frequently. Every time you crash, you notice the last trap that the application called was SndNewChannel.

KON Wait a second. The application calls SndNewChannel every time it plays a sound? It should just call SndNewChannel once at startup for each channel it needs and then keep reusing those.

BAL What happens if you fix the application?

65 Mike The problem goes away. But you haven't found the bug yet. Although calling SndNewChannel all the time may slow you down, it isn't illegal and shouldn't cause heap corruption.

BAL Is anything else going on while the sound is being played?

60 Mike The problem seems to happen only while a movie is playing. The application calls SndNewChannel, SndPlay, and SndDisposeChannel every time it wants to make a sound, but why the crash?

BAL Does the movie have sound? What happens if you turn off the sound in the movie?

55 Mike The problem goes away.

KON What's the last trap called inside SndNewChannel before the crash?

50 Mike MoveHHi.

KON What if you don't play the video?

45 Mike It crashes.

BAL Change the button that's doing the SndNewChannel, SndPlay, and all that other stuff; make it so it creates a bunch of handles and calls MoveHHi on them instead.

40 Mike Now the machine crashes more frequently.

BAL So now we know that MoveHHi and playing the sound in the movie have something to do with it. Make it so that when you push the button, the movie starts playing at the beginning; then do NewHandle, MoveHHi, and DisposeHandle in a loop with a counter, and keep the loop counter at location 0 so that when you crash you can see which iteration you're on. You might have to make the size of the handles vary in case the failure depends on block size or position, since that'll help spread the allocations throughout the heap.

35 Mike It seems to happen consistently on the sixty-ninth iteration of the loop, reproducibly, if I start from launching the program.

KON Go into MacsBug and put a breakpoint in the loop when the loop counter is 69.

30 Mike You hit your breakpoint and trace over the call to MoveHHi, and it works fine. If you say go, you crash a hundred iterations later.

KON Rather than use MacsBug, change the code to break on the sixty-ninth iteration. Then what happens when you trace over MoveHHi?

25 Mike It works fine.

BAL OK. Change the code to break on the seventieth iteration.

20 Mike You get to the breakpoint at 70 and everything is fine.

KON So somehow this thing is timing sensitive. Have the program compare with a really big number and see when the heap goes bad. Then change the number so that it breaks right before the problem code.

15 Mike When you break and trace, it doesn't happen.

BAL What if I turn off interrupts during my MoveHHi loop?

10 Mike It works fine, but you only hear the first half second of sound from the movie. Since interrupts are blocked, the Sound Manager can't call back to QuickTime to get the next piece, which QuickTime has queued up in the mean time.

KON Great, so what you're saying is we can now break right before the MoveHHi that would cause it to happen if we didn't break there.

BAL Right, so go ahead and break there. Dump the state of the heap and log it to a file. Then do the same thing, this time comparing with a higher number and letting it trash itself, and dump the heap again. Compare the heaps and figure out what's going on.

[IMAGE Puzzle_Page_final_rev2.GIF]

5 Mike The interesting part of the heap before and after the MoveHHi call is shown in the figure. Before MoveHHi there was a locked block, labeled A in the figure, which is marked as relocatable afterward. The relocatable block just below locked block B is getting overwritten by the block we're calling MoveHHi on.

KON MoveHHi works by first saving the contents of the block that you're moving, then marking the block as free. Then it calls CompactMem on the heap, which bubbles all the free space up to any islands and all relocatable blocks down. Then it copies the block to the free block just before the island.

BAL And someone is coming in at interrupt time and unlocking the island, block A in the figure. Instead of remembering the location of the island, MoveHHi searches for it after the CompactMem call. Since that block was unlocked by an interrupt after CompactMem, a different block is found the second time. When MoveHHi backs up to the previous, presumably free, block and starts copying data, the heap gets trashed.

Mike Yeah, that interrupt unlocking the block was QuickTime, BAL. It turns out the Sound Manager does the same thing. Apparently the "system architects" at the time thought it was OK to call HUnlock on a locked block during an interrupt. Not! We fixed it by deferring all HUnlock calls until MoveHHi finishes. This was the cleanest fix; it keeps us from patching out huge parts of the Memory Manager. But we were stumped for quite a while.

KON Nasty.

BAL Yeah.

SCORING

  • 75-100 We'd award you a copy of Debugging Macintosh Software, but obviously you don't need it.
  • 50-70 We'd award you a copy of Debugging Macintosh Software, but then KON would miss out on his royalties.
  • 25-45 Only scores from your first reading count.
  • 5-20 Outfoxed by a summer intern. Care to join our poker game? *

KONSTANTIN OTHMER AND BRUCE LEAK have been awarded a subcontract to debug the Ada software for the cost-reduced space station backup project, code named BALKON-4. While BAL has found that rocket science makes him nauseous, KON has taken to weightlessness like a bug to code and is thinking of opening KONstellation, the first casino in space.*

Mike DODD is the official QuickTime perpetual summer hire, just finishing his fourth summer with Apple. He claims that someday he'll actually graduate from the University of Tennessee and get a full-time job. Mike spends a lot of his time inside MacsBug trying to make QuickTime crash less, or at least finding cool bugs to try to stump KON and BAL with. *

The MemHell extension is on this issue's CD. *

Thanks to Gary Davidian, Peter Hoddie, and Jim Reekes for reviewing this column. *

 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

Aether Gazer unveils Chapter 16 of its m...
After a bit of maintenance, Aether Gazer has released Chapter 16 of its main storyline, titled Night Parade of the Beasts. This big update brings a new character, a special outfit, some special limited-time events, and, of course, an engaging... | Read more »
Challenge those pesky wyverns to a dance...
After recently having you do battle against your foes by wildly flailing Hello Kitty and friends at them, GungHo Online has whipped out another surprising collaboration for Puzzle & Dragons. It is now time to beat your opponents by cha-cha... | Read more »
Pack a magnifying glass and practice you...
Somehow it has already been a year since Torchlight: Infinite launched, and XD Games is celebrating by blending in what sounds like a truly fantastic new update. Fans of Cthulhu rejoice, as Whispering Mist brings some horror elements, and tests... | Read more »
Summon your guild and prepare for war in...
Netmarble is making some pretty big moves with their latest update for Seven Knights Idle Adventure, with a bunch of interesting additions. Two new heroes enter the battle, there are events and bosses abound, and perhaps most interesting, a huge... | Read more »
Make the passage of time your plaything...
While some of us are still waiting for a chance to get our hands on Ash Prime - yes, don’t remind me I could currently buy him this month I’m barely hanging on - Digital Extremes has announced its next anticipated Prime Form for Warframe. Starting... | Read more »
If you can find it and fit through the d...
The holy trinity of amazing company names have come together, to release their equally amazing and adorable mobile game, Hamster Inn. Published by HyperBeard Games, and co-developed by Mum Not Proud and Little Sasquatch Studios, it's time to... | Read more »
Amikin Survival opens for pre-orders on...
Join me on the wonderful trip down the inspiration rabbit hole; much as Palworld seemingly “borrowed” many aspects from the hit Pokemon franchise, it is time for the heavily armed animal survival to also spawn some illegitimate children as Helio... | Read more »
PUBG Mobile teams up with global phenome...
Since launching in 2019, SpyxFamily has exploded to damn near catastrophic popularity, so it was only a matter of time before a mobile game snapped up a collaboration. Enter PUBG Mobile. Until May 12th, players will be able to collect a host of... | Read more »
Embark into the frozen tundra of certain...
Chucklefish, developers of hit action-adventure sandbox game Starbound and owner of one of the cutest logos in gaming, has released their roguelike deck-builder Wildfrost. Created alongside developers Gaziter and Deadpan Games, Wildfrost will... | Read more »
MoreFun Studios has announced Season 4,...
Tension has escalated in the ever-volatile world of Arena Breakout, as your old pal Randall Fisher and bosses Fred and Perrero continue to lob insults and explosives at each other, bringing us to a new phase of warfare. Season 4, Into The Fog of... | Read more »

Price Scanner via MacPrices.net

New today at Apple: Series 9 Watches availabl...
Apple is now offering Certified Refurbished Apple Watch Series 9 models on their online store for up to $80 off MSRP, starting at $339. Each Watch includes Apple’s standard one-year warranty, a new... Read more
The latest Apple iPhone deals from wireless c...
We’ve updated our iPhone Price Tracker with the latest carrier deals on Apple’s iPhone 15 family of smartphones as well as previous models including the iPhone 14, 13, 12, 11, and SE. Use our price... Read more
Boost Mobile will sell you an iPhone 11 for $...
Boost Mobile, an MVNO using AT&T and T-Mobile’s networks, is offering an iPhone 11 for $149.99 when purchased with their $40 Unlimited service plan (12GB of premium data). No trade-in is required... Read more
Free iPhone 15 plus Unlimited service for $60...
Boost Infinite, part of MVNO Boost Mobile using AT&T and T-Mobile’s networks, is offering a free 128GB iPhone 15 for $60 per month including their Unlimited service plan (30GB of premium data).... Read more
$300 off any new iPhone with service at Red P...
Red Pocket Mobile has new Apple iPhones on sale for $300 off MSRP when you switch and open up a new line of service. Red Pocket Mobile is a nationwide MVNO using all the major wireless carrier... Read more
Clearance 13-inch M1 MacBook Airs available a...
Apple has clearance 13″ M1 MacBook Airs, Certified Refurbished, available for $759 for 8-Core CPU/7-Core GPU/256GB models and $929 for 8-Core CPU/8-Core GPU/512GB models. Apple’s one-year warranty is... Read more
Updated Apple MacBook Price Trackers
Our Apple award-winning MacBook Price Trackers are continually updated with the latest information on prices, bundles, and availability for 16″ and 14″ MacBook Pros along with 13″ and 15″ MacBook... Read more
Every model of Apple’s 13-inch M3 MacBook Air...
Best Buy has Apple 13″ MacBook Airs with M3 CPUs in stock and on sale today for $100 off MSRP. Prices start at $999. Their prices are the lowest currently available for new 13″ M3 MacBook Airs among... Read more
Sunday Sale: Apple iPad Magic Keyboards for 1...
Walmart has Apple Magic Keyboards for 12.9″ iPad Pros, in Black, on sale for $150 off MSRP on their online store. Sale price for online orders only, in-store price may vary. Order online and choose... Read more
Apple Watch Ultra 2 now available at Apple fo...
Apple has, for the first time, begun offering Certified Refurbished Apple Watch Ultra 2 models in their online store for $679, or $120 off MSRP. Each Watch includes Apple’s standard one-year warranty... Read more

Jobs Board

DMR Technician - *Apple* /iOS Systems - Haml...
…relevant point-of-need technology self-help aids are available as appropriate. ** Apple Systems Administration** **:** Develops solutions for supporting, deploying, Read more
Omnichannel Associate - *Apple* Blossom Mal...
Omnichannel Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Operations Associate - *Apple* Blossom Mall...
Operations Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Cashier - *Apple* Blossom Mall - JCPenney (...
Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Mall Read more
IT Systems Engineer ( *Apple* Platforms) - S...
IT Systems Engineer ( Apple Platforms) at SpaceX Hawthorne, CA SpaceX was founded under the belief that a future where humanity is out exploring the stars is Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.