r/programming • u/liotier • Mar 25 '15
x86 is a high-level language
http://blog.erratasec.com/2015/03/x86-is-high-level-language.html361
u/cromulent_nickname Mar 25 '15
I think "x86 is a virtual machine" might be more accurate. It's still a machine language, just the machine is abstracted on the cpu.
83
u/BillWeld Mar 25 '15
Totally. What a weird high-level language though! How would you design an instruction set architecture nowadays if you got to start from scratch?
167
u/Poltras Mar 25 '15
ARM is actually pretty close to an answer to your question.
→ More replies (53)74
u/PstScrpt Mar 25 '15
No, I'd want register windows. The original design from the Berkeley RISC 1 wastes registers, but AMD fixed that in their Am29000 chips by letting programs only shift by as many registers as they actually need.
Unfortunately, AMD couldn't afford to support that architecture, because they needed all the engineers to work on x86.
→ More replies (17)53
u/barsoap Mar 25 '15 edited Mar 26 '15
EDIT: Yesyes you can write timing side-channel safe code with that, it's got an explicit pipeline and instructions have to be scheduled by the assembler. Needs drilling further down to the hardware than a usual compiler would, but it's a piece of cake, compared to architectures that are too smart for their own good.
→ More replies (18)12
23
u/coder543 Mar 26 '15
RISC-V is the new, upcoming awesomeness
Itanium was awesome, it just happened before the necessary compiler technology happened, and Intel has never reduced the price to anything approaching attractiveness for an architecture that isn't popular enough to warrant the sky-high price.
There's always that Mill architecture that's been floating around in the tech news.
ARM and especially ARM's Thumb instruction set is pretty cool.
Not a huge fan of x86 of any flavor, but I was really impressed with AMD's Jaguar for a variety of technical reasons, but they never brought it to its fullest potential. They absolutely should have released the 8-core + big GPU chip that they put in the PS4 as a general market chip, and released a 16-core + full-size GPU version as well. It would have been awesome and relatively inexpensive. But, they haven't hired me to plan their chip strategy, so that didn't happen.
→ More replies (3)19
u/cogman10 Mar 25 '15
TBH, I feel like Intel's IA64 architecture never really got a fair shake. The concept of "do most optimizations in the compiler" really rings true to where compiler tech has started going to now-a-days. The problem with it is that compilers weren't there yet, x86 had too strong of a hold on everything, and the x86 to IA64 translation resulted in applications with anywhere from 10%->50% performance penalties.
→ More replies (2)30
u/Rusky Mar 25 '15
Itanium was honestly just a really hard architecture to write a compiler for. It tried to go a good direction, but it didn't go far enough- it still did register renaming and out of order execution underneath all the explicit parallelism.
Look at DSPs for an example of taking that idea to the extreme. For the type of workloads they're designed for, they absolutely destroy a typical superscalar/OoO CPU. Also, obligatory Mill reference.
→ More replies (9)4
u/Condorcet_Winner Mar 26 '15
Honestly, as a compiler writer x86 is perfectly pleasant to deal with. It's very easy actually. ARM is a bit annoying because it is verbose, but otherwise is ok. Some level of abstraction is necessary to allow chipmakers to make perf improvements without requiring different binaries. Adding new instructions takes a very long time. Compiling with sse2 is only starting to happen now, despite sse2 coming out well over a decade ago.
→ More replies (5)→ More replies (17)8
4
u/PurpleOrangeSkies Mar 25 '15
I don't know that you can truly call x86 assembly a machine language. There are 9 different opcodes for
add. A naive assembler couldn't handle that.
179
u/rhapsblu Mar 25 '15
Every time I think I'm starting to understand how a computer works someone posts something like this.
104
u/psuwhammy Mar 25 '15
Abstraction is a beautiful thing. Every time you think you've figured it out, you get a little glimpse of the genius built into what you take for granted.
→ More replies (1)116
u/Intrexa Mar 25 '15
To code a program from scratch, you must first create the universe.
→ More replies (3)34
u/Netzapper Mar 25 '15
Honestly? Just don't sweat it. Read the article, enjoy your new-found understanding, with the additional understanding that whatever you understand now will be wrong in a week.
Just focus on algorithmic efficiency. Once you've got your asymptotic time as small as theoretically possible, then focus on which instruction takes how many clock cycles.
Make it work. Make it work right. Make it work fast.
15
u/IJzerbaard Mar 25 '15
It doesn't change that fast really. OoOE has been around since the 60's, though it wasn't nearly as powerful back then (no register renaming yet). The split front-end/back-end (you can always draw a line I suppose, but a real split with µops) of modern x86 microarchs has been around since PPro. What has changed is scale - bigger physical register files, bigger execution windows, more tricks in the front-end, more execution units, wider SIMD and more special instructions.
But not much has changed fundamentally in a long time, a week from now surely nothing will have changed.
→ More replies (2)9
→ More replies (1)5
u/bstamour Mar 25 '15
Be careful with asymptotics though... A linear search through a vector will typically blow a binary search out of the water on anything that can fit inside your L1-cache. I'd say pay attention to things such as asymptotic complexity but never neglect to actually measure things.
→ More replies (4)12
→ More replies (3)2
u/randomguy186 Mar 25 '15
Don't worry about it. I doubt that anyone here can explain the quantum physics of the field effect or the NP / PN junctions. If you don't understand the physics, you don't understand how transistors work, which means you don't understand how logic gates work, which means you don't understand digital circuits, etc. There are very few people in the world who really understand how a computer works.
→ More replies (2)
229
u/deadstone Mar 25 '15
I've been thinking about this for a while; How there's physically no way to get lowest-level machine access any more. It's strange.
117
u/salgat Mar 25 '15
After reading this article, I was surprised at how abstract even machine code is. It really is quite strange.
184
u/DSMan195276 Mar 25 '15
At this point the machine-code language for x86 is mostly just still there for compatibility. It's not practical to change the machine-code language for x86, the only real option for updating is to add new opcodes. I bet that if you go back to the 8086, x86 machine code probably maps extremely well to what the CPU is actually doing. But, at this point CPU's are so far removed from the 8086 that newer Intel CPU's are basically just 'emulating' x86 code on a better instruction set. The big advantage to keeping it a secret instruction set is that Intel is free to make any changes they want to the underlying instruction set to fit it to the hardware design and speed things up, and the computer won't see anything different.
→ More replies (16)24
u/HowieCameUnglued Mar 25 '15 edited Mar 25 '15
Yup that's why AMD64 beat IA64 so handily (well, that and it's extremely difficult to write a good compiler targeting IA64). Backwards compatibility is huge.
→ More replies (1)95
u/tralfaz66 Mar 25 '15
The CPU is better at optimizing the CPU than you.
45
→ More replies (2)13
u/deelowe Mar 25 '15
The algorithm behind branch prediction how much much of a difference it made in speed when it was implemented always amazes me.
20
Mar 25 '15 edited Mar 25 '15
with things like pipelining and multi core architectures, it's probably for the best that most programmers dont get access to micro code. Most programmers don't even have a clue how the processor works let alone how pipelining works and how to handle the different types of hazards.
26
u/Prometh3u5 Mar 25 '15 edited Mar 25 '15
With out of order and all the reordering going on, plus all the optimization to prevent stalls due to cache accesses and other hazards, it would be an absolute disaster for programmers trying to code at such a low level on modern CPUs. It would be a huge step back.
→ More replies (1)11
u/Bedeone Mar 25 '15
For the very vast majority of programmers (myself absolutely included), I agree. But there are some people out there who excel at that kind of stuff. They'd be having loads of fun.
→ More replies (1)30
u/jediknight Mar 25 '15
How there's physically no way to get lowest-level machine access any more.
Regular programmers might be denied access but isn't the micro-code that's running inside the processors working at that lowest-level?
72
u/tyfighter Mar 25 '15
Sure, but when you start thinking about that, personally I always begin to wonder, "I'll bet I could do this better in Verilog on an FPGA". But, not everyone likes that low of a level.
28
u/softwaredev Mar 25 '15
Skip Verilog, make your webpage from discrete transistors.
→ More replies (4)12
u/jared314 Mar 25 '15 edited Mar 25 '15
There is a community around open processor designs at Open Cores that can be written to FPGAs. The Amber CPU might be a good starting point to add your own processor extensions.
→ More replies (28)5
u/hrjet Mar 25 '15
The micro-code gets subjected to out-of-order execution, so it doesn't really help with the OP's problem of predictability.
→ More replies (12)6
u/chuckDontSurf Mar 25 '15
I'm not sure exactly what you mean by "lowest-level machine access." Processors have pretty much always tried to hide microarchitectural details from the software (e.g., cache hierarchy--software doesn't get direct access to any particular cache, although there are "helpers" like prefetching). Can you give me an example?
→ More replies (1)5
u/lordstith Mar 25 '15
It seems people are referring to back-in-the-day when x86 was just the 8086. No such thing as cache in an MPU setting at that point.
→ More replies (1)
26
u/OverBiasedAndroid6l6 Mar 25 '15
I understood this after taking a class on programing for the 8086. I had taken a class using a crippled 16 bit microcontroller board using assembly the semester before. When I found out that you can do in line multiplication in x86, I audibly exclaimed "WHAAAA?". I realized how far from true low level I was working.
29
u/SarahC Mar 25 '15
You can do floating point inline multiplication!
That took a program on the Z80!
11
u/lordstith Mar 25 '15
Psh. What, were you too broke of a schlub to afford installing a whole separate FPU into your system just to handle this stuff?
Jesus, there was a day where MMUs were an actual physical addon. We're in the crazy future.
7
u/OverBiasedAndroid6l6 Mar 25 '15
And with loops in tandem with that, who needs C!
I do, just so you ^know.
10
u/PurpleOrangeSkies Mar 25 '15
Multiplication isn't too hard to implement in hardware. Now division, on the other hand, is something I can't figure out how they did it for the life of me.
14
u/bo1024 Mar 25 '15
I think the point is "inline", meaning that in your code you can just write something like 4*eax and the computer will multiply 4 by the register eax for you (or something like that).
This is very weird when you consider that in assembly language you are supposedly controlling each step of what the CPU does, so who does this extra multiplication?
→ More replies (1)7
u/sandwich_today Mar 26 '15
The multiplications are only small powers of two, so they're implemented as bit shifts in simple hardware. Some early x86 processors had dedicated address calculation units, separate from the ALU. This made the LEA (load effective address) instruction a lot faster than performing the same operations with adds and shifts, so a lot of assembly code used LEA for general-purpose calculation.
→ More replies (5)2
u/lordstith Mar 25 '15
Yup, ISA before the 80s or so were actually developed with the intention of being written in by humans. It's crazy to think about. For example, the way arrays work in C was originally basically a thin veneer over one of the addressing modes of the PDP-11.
→ More replies (3)
23
u/YourFavoriteBandSux Mar 25 '15
I'm going to go ahead and not send this to my sophomore Assembly Language students. They're having enough trouble keeping track of the stack during procedure calls; I think this will drive them right to drinking.
15
44
u/Minhaul Mar 25 '15
As a computer architect, I don't completely agree or disagree with the title of this article. But reading it, the author is arguing that the underlying microarchitecture of most x86 processors is complex, but microarchitecture is completely separate from the x86 ISA. And just about any modern processor has the same complicated underlying microarchitecture to implement the ISA efficiently.
→ More replies (1)16
Mar 26 '15
Indeed, I'm also a (former) computer architect here with a similar experience: tons of people, mainly programmers, I have had to work with do not understand that ISA and microarchitecture refer to 2 (very) different things.
After reading the article, I wanna smack the author with a wet sock though.
5
u/nullparty Mar 26 '15
It would be interesting to hear your gripes about this article.
10
Mar 26 '15 edited Mar 26 '15
I found the "reasoning" the author used to reach the conclusion to be baffling, to say the least. Basically any interface to an out-of-order superscalar machine is a "high level language."
Instructions in the ISA do exactly what they say with respect to their retirement. I have no idea what the author is specifically referring to by "smooth" or "predictable" execution, but neither of those seem to be exclusive issues to modern aggressively out-of-order designs. Which made the whole "side-channel" attack claim not very well substantiated IMO.
→ More replies (5)→ More replies (1)2
u/bakuretsu Mar 26 '15
A lot has happened in processor design while I've not been paying attention (I am but a mere web programmer for whom processor opcodes are a passing interest).
Is it safer to say that x86 itself is an API that the processor is free to implement as it wishes?
Is x86 itself ever expanded? At some point abstractions become more costly than direct access in certain situations, so do some of those bubble up into the spec for kernel and driver programmers to take advantage of?
→ More replies (2)
12
u/snarkyxanf Mar 25 '15 edited Mar 25 '15
In the context of cryptography, one of the NSA's jobs is to create encryption hardware and keys for other government agencies. They prefer really predictable technology, for example this thing that reads keys from punched paper tape.
Cryptosystems are built around a small set of primitives with fairly stable design. Maybe it's time to start shipping coprocessors/built in functional units that implement the primitives?
5
u/P1h3r1e3d13 Mar 25 '15
That's what I came here to ask. Is it feasible to have dedicated circuitry, optimized for crypto calculation. Presumably you could get benefits in speed, predictability, and reliability.
3
u/rcxdude Mar 25 '15
The ARM chip inside the beaglebone has some interesting real-time co-processors which are designed for extremely predictable execution. I'm not sure how good they are at cryptography though.
2
u/pinealservo Mar 26 '15
The chip inside the beaglebone is a TI Sitara processor SoC, which happens to have an ARM Cortex A8 processor in it along with a whole pile of other things generally unrelated to ARM. The co-processors you're referring to are called PRU-ICSS, or "Programmable Real-time Unit--Industrial Communication SubSystem". As the ICSS part of the name implies, they're primarily there to implement industrial control protocols like EtherCAT, PROFIBUS, etc.; there are a whole bunch of them and they require a lot of high-speed deterministic protocol state transitions; you'd usually implement them in hardware, but this solution is far more flexible and makes it easy to support new industrial protocols without spinning a new chip.
So, they're really designed to shunt data around and bit-bang wire-level protocols rather than do complex calculations, though if they can do the math you need for your crypto they'll definitely be easy to get deterministic (if not fast) results from.
On the other hand, the Sitara also has a co-processor specifically designd for crypto acceleration. That might be a better choice, though I guess it could have some flaws I'm unaware of.
3
Mar 26 '15
Intel's AES instructions are a good start; no more worrying about those god damn S-boxes being assholes.
2
u/LittleHelperRobot Mar 25 '15
Non-mobile: this thing
That's why I'm here, I don't judge you. PM /u/xl0 if I'm causing any trouble. ^WUT?
→ More replies (1)1
u/green_meklar Mar 25 '15
Or maybe we should start shipping processors with a built-in feature for spamming a bunch of random noise on top of your cryptographic algorithm...
→ More replies (2)6
u/rcxdude Mar 25 '15
Constant time/power is a far better defence against side-channel attacks than adding random noise: all random noise means is that the attacked needs to collect more samples (and not an exponentially increasing amount), while constant time removes the attack entirely.
10
u/aiij Mar 25 '15
I wouldn't call it a high-level language, although there are certainly more layers below it than there used to be...
→ More replies (1)
10
9
u/jib Mar 25 '15
x86 is complicated and executes out of order, but I disagree with the article's implication that this makes side-channel attacks unavoidable.
Out-of-order execution makes execution time depend on where things are in the cache and what code was executed previously, but the execution time is still usually independent of the actual data values.
e.g. if you add two numbers together, timing may reveal information about the state of the cache etc, but it won't tell you anything about what the two numbers are.
And if you write code correctly, making your sequence of instructions and sequence of accessed addresses independent of any secret information, then you won't leak any secret information through timing.
2
u/zefcfd Mar 29 '15
x86 is complicated and executes out of order
is it really the x86 instruction set managing this, or the microarchitecture underneath it?
→ More replies (1)
26
u/atakomu Mar 25 '15
There is a great talk by Martin Thompson about Myths in computers. (That RAM/HDD is random access that CPU's are slowing down etc.) Mythbusting modern hardware.
And because CPU's aren't in order anymore you can get "strange" results like sorting an array makes algorithm 10 times faster.
42
u/happyscrappy Mar 25 '15
Actually that sorting thing happens because of branch prediction techniques instead of out of order execution.
→ More replies (4)2
u/sirin3 Mar 26 '15
Reminds me of this recent German thread
Someone wanted a map type, but Pascal does not really have a good one atm. He tried to implement one / modifying the existing one, and noticed that most of the time is spend comparing the keys.
Now Ruewa worked the last months to find an efficient way to compare two strings for equality.
Seems, inserting random NOPs in the comparison loop can make it three times faster. One some CPUs. On others this makes it slower?
Such a comparison is an extremely complicated problem, but crucially to solve it, if you ever want to use a map for anything...
44
u/exscape Mar 25 '15
High-level? I understand the point, but I wouldn't call it that. Hell, I don't consider C high level.
85
u/ctcampbell Mar 25 '15
'Contains a layer of abstraction' would probably be a better phrase.
→ More replies (4)47
u/frezik Mar 25 '15
Defining "high-level" is more a matter of perspective than anything strictly defined. If you're fooling around with logic gates, then machine code is "high-level".
22
24
u/Darkmere Mar 25 '15
C is a high level language for close-to-hardware people. And a low-level language for CS students.
It depends on your background and concepts.
( Good luck writing cache-aware software in F# ;)
→ More replies (4)1
u/RainbowNowOpen Mar 25 '15
"Higher level" is something we can all probably agree on. x86 is clearly higher level than the μops used to implement instructions like "XOR EAX, EAX" which do not actually execute as many programmers imagine. A macro-instruction like that will be modified or completely optimized away by the CPU in a manner similar to what a compiler or interpreter might do.
21
u/Bedeone Mar 25 '15
Speeding up processors with transparent techniques such as out of order execution, pipe lining, and the associated branch prediction will indeed never be a constant advantage. Sometimes even a disadvantage. x86 is still backwards compatible, instructions don't disappear.
As a result, you can treat a subset of the x86 instruction set as a RISC architecture, only using ~30 basic instructions, and none of the fancy uncertainties will affect you too much. But you also miss out on the possible speed increases.
With that being said, machine instructions still map to a list of microcode instructions. So in a sense, machine code has always been high-level.
10
u/tending Mar 25 '15
What ~30 instruction subset?
→ More replies (16)3
u/Bedeone Mar 25 '15
I couldn't tell you because I don't write x86 assembler, I write z/Architecture assembler (z/Arch is also CISC). But basically a couple instructions to load and store registers (RX-RX and RX-ST), a couple to load and store addresses (RX-RX and RX-ST) again. Basic arithmetic, basic conditional branching, etc.
You don't use all of the auto-iterative instructions. For example in z/Arch; MVI moves one byte, MVC moves multiple bytes. But in the background (processor level, it's still one machine instruction), MVC just iterates MVI's.
Perhaps a bit of a bad example. MVC is useful, and you are still very much in control, even though stuff happens in the background. But you don't need it. You'd otherwise write ~7 instructions to iterate over an MVI instruction to get the same effect.
7
u/lordstith Mar 25 '15
Is it weird that I think it's fucking badass that you specialize in the internals of a system that harkens back to twenty years before x86 was even a thing?
→ More replies (8)8
u/Rusky Mar 25 '15
Dropping all those instructions might save some die space but it might not bring as much of a performance increase as you would hope.
RISC was originally a boost because it enabled pipelining, and CISC CPUs took a long time to catch up. Now that clock speeds are so much higher, the bottleneck is memory access, and more compact instruction encodings (i.e. CISC) have the advantage.
Ideally we'd have a more compact instruction encoding where the instructions are still easily pipelined internally- x86 certainly isn't optimal here, but it definitely takes advantage of pipelining and out-of-order execution.
→ More replies (11)2
u/websnarf Mar 25 '15
What speed increases? Remember Alpha, PA-RISC, MIPS, PowerPC, and Sparc all had their opportunity to show just how wrong Intel was. And where are they now?
→ More replies (1)1
u/lordstith Mar 25 '15
That's not exactly true. Microcode wasn't a thing prior to the System/360.
→ More replies (5)
6
u/kindall Mar 25 '15 edited Mar 25 '15
Ever look at the assembly language for a "classic" IBM mainframe, like the 360 or 370? Those mofos have opcodes for formatting numbers according to a template. A single instruction (EDMK) not only converts the number to a string, but inserts commas and decimal points and like that, and then leaves the address of the first digit in a register so you can easily insert a floating currency symbol. If you look at the COBOL language, it maps well to these high-level assembly instructions: the assembly language is basically the pieces of COBOL.
How much of this was ever actually implemented in hardware, I don't know. Possibly these instructions were trapped and actually ran in software from the get-go; they were almost certainly microcoded even initially. (They remained supported in later systems for many years and probably still are, and they are almost certainly emulated in software now.)
Compared to that, I wouldn't really say x86 assembly is high-level at all.
6
31
u/jhaluska Mar 25 '15
Just because the CPU isn't executing it with constant time constraints doesn't make it not meet the criteria of a low-level language.
Good content, but lousy conclusion.
6
Mar 25 '15
The very amount of translation done from x86 machine code to the actual mOPs executed by the core makes it significantly higher level than a classic, directly executed RISC or VLIW.
6
2
u/UsingYourWifi Mar 25 '15 edited Mar 25 '15
It's just the author exercising some artistic license with the term "high-level language."
Good content, good conclusion worded in a way that irritates the excessively pedantic (aka everyone that reads this subreddit).
0
Mar 25 '15
[deleted]
5
Mar 25 '15
Did jhaluska conflate the points or did the article author?
4
u/jhaluska Mar 25 '15
The upshot is this: Intel's x86 is a high-level language. Coding everything up according to Agner Fog's instruction timings still won't produce the predictable, constant-time code you are looking for.
I'd argue the article author implied it, but even if I did conflate the points, I just wanted to point out that the ISA itself is implementation agnostic. Implementations don't always give you exact timings. Throw in multi threading and you're in even more trouble. However the trade off for this variable execution time is a drastic increase in overall performance.
In the other case, just because there are abstraction layers beneath it, doesn't mean it's a HLL from the CS definition. The content was good, the conclusion was bad.
→ More replies (1)
4
Mar 25 '15
When we got to x86 in our systems course, my world was shattered.
I thought "binary code," all those zeroes and ones, were complex circuit instructions!
I didn't know they encoded high level instructions such as "do a * b + c," all in one instruction.
→ More replies (2)
6
3
u/websnarf Mar 25 '15
This is just an argument for saying x86 specifies the operations, but does not dictate the implementation. That's a very different thing from saying it is a high-level language. What support does it have for user defined abstract data types for example? Does it support recursion?
7
Mar 25 '15
What an idiotic title. The sky is blue. My car is also blue. Therefore, my car is the sky.
1
Mar 28 '15
If we're going with this metaphor...
Can you prove that your car isn't essentially the same thing as the sky? Birds use the sky to fly to places, and you use the car to drive to places.
Same thing.
→ More replies (1)
5
u/flat5 Mar 25 '15
Glad to have learned assembly on 68k processors. x86 is a horror show that I could never stomach long enough to really learn it.
4
u/mscman Mar 25 '15
This is why MIPS is still used pretty heavily to teach basic assembly and computer architecture. Trying to teach it starting with x86 leads to a ton of corner cases and optimization techniques which, while applicable to today's technologies, can get in the way of the underlying theory of why things are the way they are today.
4
Mar 26 '15
MIPS is popular in academia mainly because lots of schools use the same Patterson/Hennessy architecture book, which uses that ISA prolifically for its examples.
1
u/jurniss Mar 25 '15
I dunno, I have a pretty easy time understanding x86 assembly code and have written some on occasion. It's a lot better in 64-bit where you get 8 more integer registers. But its binary instruction format is a hellish nightmare. I feel like x86-64 would be OK for learning assembly but extremely difficult to target with a compiler. So it makes sense that universities use a different architecture because presumably students will use it as the target architecture in their compiler class next year.
5
u/EasilyAnnoyed Mar 25 '15
Consider registers, for example. Everyone knows that the 32-bit x86 was limited to 8 registers, while 64-bit expanded that to 16 registers.
Uh, yeah... Everyone knows this.... Especially me!
4
Mar 25 '15
The article is poorly documented and badly written. The worst is probably "Everyone knows that"...
2
u/dukey Mar 25 '15
If the processor didn't have 'virtual' registers, x86 performance would have been pretty terrible compared to what it could have been with a better instruction set.
2
u/CompellingProtagonis Mar 25 '15
One thing that strikes me when reading this is whether it would make a difference for programmers, in practice, if x86 wasn't a high level language. For very specific extremely high-budget applications like security for DoD or major corporations it might make a difference to have this option, but for the vast majority of applications it might be a Crystal Skull type situation. I mean every new processor architecture would require god knows how many man-hours to research the new architecture and figure out how best to use it otherwise you risk performance penalties with newer hardware. That being said, this would be an absolutely amazing thing for something like Raspberry Pi, if they don't already do this.
2
2
Mar 25 '15
Would this solve the timing-attack problem? Use the CPU's clock cycle counter after the work has been done to ensure it took exactly a defined amount of time?
I don't see why the author considers this an intractable problem.
1
u/aiij Mar 26 '15
That's exactly the problem. What do you do if it didn't? You can't unleak the information you just leaked.
2
u/immibis Mar 25 '15
Not only is it high-level in the sense that it's translated into something lower-level, but it's high-level in the sense that it was designed to make it easier for programmers to write things.
Hence things like rep stosb being a one-instruction memset.
2
u/RainbowNowOpen Mar 25 '15
Low-level programming on modern Intel CPUs is available only via μops (micro-operations).
x86 mnemonics are higher-level macro-instructions, implemented in terms of μops.
2
u/northrupthebandgeek Mar 26 '15
The title made it sound like this was a salvo fired by the RISC side of the RISC v. CISC flamewar of old.
I was sorely disappointed.
2
u/fuckthiscode Mar 26 '15
Um, duh?
Coding everything up according to Agner Fog's instruction timings still won't produce the predictable, constant-time code you are looking for.
This is by design, and any out of order processor is going to behave this way. Hell, anything with a cache is going to be non-deterministic in execution time.
The only way you wouldn't know this already is if you never ever bothered to look into any modern computer architecture in which you were programming. Can OpenSSL seriously not securely deal with an architecture algorithm that was developed in 1967?
3
Mar 25 '15
I really need to learn assembly.
1
1
u/berlinbrown Mar 26 '15
What they are saying, it really isn't that difficult depending on the compiler and environment you are working with. A lot of that is abstracted away by the compiler and the the os and a host of other things.
If you know C you can easily work with the high-level x86 machine language that they are talking about in the article.
....
Now debugging OS code, writing code that might actually go in the linux kernel, that is a different beast.1
3
1
u/poizan42 Mar 25 '15
If you only care about constant time and not performance you could just execute a serializing instruction after every instruction, such as CPUID.
1
u/hatessw Mar 25 '15
Can this be (reasonably) relied on even for future processors? Why?
2
u/poizan42 Mar 26 '15
Yes, because it's documented as such:
CPUID can be executed at any privilege level to serialize instruction execution. Serializing instruction execution guarantees that any modifications to flags, registers, and memory for previous instructions are completed before the next instruction is fetched and executed.
This is cited from Intel® 64 and IA-32 Architectures Software Developer’s Manual Volume 2A. You can find it at http://www.intel.com/content/www/us/en/processors/architectures-software-developer-manuals.html
1
u/afriendtosell Mar 25 '15
Those surprised by the "x86 is a high level language" article will be really shocked when they learn ARM/PPC/etc have been for 10+ years too.
Like, sure, the ISA sometimes maps directly to CPU operations -- on tiny 100mhz embedded chips used in microcontrollers.
1
u/Randosity42 Mar 25 '15
I don't know much about x86 tbh, but it seems like the author is arguing that the language is becoming higher level just based on the microcode beneath.
Kinda bullshit imo, if I write a C compiler in python it doesn't mean C is a high level language, it's still C.
1
u/romcgb Mar 25 '15
Both Intel and AMD x86 CPUs have an internal RISC backend. The cpu's frontend converts the macro-operations (eax, mov, add,...) into micro-operations of type RISC. The internal instructions set of AMD's cpu is RISC86, it's described in the patent US 6,336,178 B1. The Pentium Pro (1995) was the first Intel's CPU to have a RISC backend.
1
u/hatessw Mar 25 '15
Consider "mov eax, ebx". Again, this doesn't do anything, except rename the register as far as the processor is concerned, so that from this point on, what was referred to as ebx is now eax.
What does that mean for the contents of ebx? Is this undefined behavior? Or can the previous contents still be accessed through it? And what happens if eax is changed - is ebx then changed simultaneously?
1
u/wretcheddawn Mar 26 '15
Seems like the answer to the constant time problem is to add random delays once the operation is complete. Do a benchmark, figure out how long it takes, add 10%, and randomize. When the operation finishes quicker, spin until the randomized delay is complete (or better, switch threads), then resume.
1
Mar 26 '15
Most awesome comment on that page:
Anyone can write a cryptosystem strong enough that they can't break it.
~Gregory Maxwell
1
u/ReversedGif Mar 26 '15
Constant time compares are definitely possible if you try hard enough. Something like H(a ^ b) == x, where H is a cryptographic hash function and x is (precomputed) H(0) is practically guaranteed to never be optimized at all, since any possible optimization would require finding the set of hash collisions for H(0).
1
u/andrewjsledge Mar 26 '15
"That means "side-channel" attacks on x86 leaking software crypto secrets may always be with us."
The next time an undergrad complains about coding on SPARC, show them this.
1
Mar 26 '15
Perhaps we need dedicated security processor cores. It seems general-purpose performance processors are insufficient.
1
u/whenido Mar 26 '15
" Everyone knows that the 32-bit x86 was limited to 8 registers, while 64-bit expanded that to 16 registers." So false.
1
u/teiman Mar 26 '15
A way to generate code that take a random time to process with different data. Is to add a random number of "garbage" instructions betwen normal instructions. How much "garbage" is needed for each usefull instructions is hard. Maybe it would required to be calculated and "calibrated" making the whole very hard. I would write code that do 99 things, and 1 thing the cryptographical work, so after 200 runs has completed the cryptografical work and 19800 garbage operations (neligible calculations that are not used for anything but that are complex enough so the CPU can't just ignore them)
1
u/vattenpuss Mar 25 '15
Are there seriously people out there who thought they knew how to program a modern CPU with all that jazz going on under the hood?
Being a person who's first programming language ever was the super high level ML, I have known this since the first time I read about computer architecture (that course started about five weeks after the SML programming started and had us implement Russian multiplication in assembly and construct arithmetic circuits from transistors).
1
u/hatessw Mar 25 '15
Given that crypto is used by just about everyone, I'm not shocked about the high level (i.e. abstraction between hardware and software) nature of assembler on a modern CPU, what I'm surprised at is the difficulty involved with bypassing this behavior.
It seems like a wide opening for any well-funded attacker, and I'd rather this be made more difficult, for instance by being able to selectively turn on constant-time execution for a given process, at the cost of crypto performance of course. It seems to me this would need to be on a hardware level.
0
-24
Mar 25 '15
Everyone knows that the 32-bit x86 was limited to 8 registers, while 64-bit expanded that to 16 registers. This isn't actually true.
Yea, everyone DUUUUHHHHH
0
Mar 25 '15
[deleted]
35
u/bstamour Mar 25 '15
Word size has nothing to do with the amount of registers.
-1
Mar 25 '15
It kinda does. If your instruction size is limited then you can only address so many registers.
4
u/bstamour Mar 25 '15
You're technically correct, which is the best kind of correct. However my point still stands, all you need to do is look at SPARC for a counterexample that 32 bit = 8 registers and 64 bit = 16.
1
u/real_jeeger Mar 25 '15
Yes, a maximum of 232 registers, disregarding multi-byte instructions. So no, not really.
1
Mar 25 '15
Um, no? You have to have space for opcodes, option flags, source registers, destination registers, etc. in your instructions. x86-64 gives 4 bits for this.
5
u/FredFnord Mar 25 '15
What? The 32-bit 680x0 family had 16 general-purpose registers, and the earlier 32-bit implementations of the PowerPC (well, the ones used for computers) had 32 GPRs and 32 floating-point registers, and the later 32-bit had 32 GPRs, 32 FPRs, 32 128-bit vector registers, and 6 vector rename registers.
EVERYONE knows THAT, right? Or maybe that's what RockMeetHardPlaces was suggesting: not everyone in the world knows the implementation details of a given microprocessor.
→ More replies (1)2
0
Mar 25 '15
[deleted]
12
u/xXxDeAThANgEL99xXx Mar 25 '15
I have a hard time calling a language that doesn't even define function call semantics 'high-level'
It certainly defines some stuff, like
push,pop,call,ret, and evenleave(likeretbut also adjustsSP).Fun fact: going all pedantic is weird in this context because this is about as much function call semantics as Forth defines, and Forth is supposed to be higher-level than the usual C/C++/Java bunch.
7
Mar 25 '15
I'm amazed at how many comments are posted purely to take issue with this one throw-away term, instead of looking at the meat of the article.
8
u/Hadrosauroidea Mar 25 '15
To be fair, that throw-away is the title of the article. And a key part of the introduction. And the conclusion. Hmm, maybe it wasn't a complete throw-away in the first place.
It might have been more accurate to call it 'highly abstracted' instead, but I agree that the point about the futility of attempting to code constant-time calculations is clear enough either way.
1
u/Bobshayd Mar 25 '15
What, exactly, does a high-level language have that a low-level language does not? I'd say, if pressed for an answer, that "abstraction" is a pretty good answer to that question.
0
Mar 25 '15
[deleted]
3
Mar 25 '15
I didn't have any trouble understanding his meaning once I got past the title, mostly because I'm far too familiar with the things he was talking about, but that doesn't mean others won't be confused or left with a flawed understanding of what the author is intending to describe.
I'd have to agree with Hadrosauroidea, when the meat of what the author intended to say is wrapped in the wrong package and comes to a seemingly inexplicable conclusion you question whether or not even the author truly understands what it is they're trying to say. A lot of people who already understand what's going on will no doubt circle back to the title and say "hey, the sign on the front door doesn't match what's in the house"
1
u/hatessw Mar 25 '15
mostly because I'm far too familiar with the things he was talking about
I'm not very familiar with assembler, and I thought it was a fairly concise, understandable and well-written piece. The clarity of the writing IMHO ensures that the odds of what you describe happening should be low (but are never zero anyway when dealing with potentially conflicting definitions).
0
Mar 25 '15
So you agree with the author that what he's describing is sufficient to classify x86 assembly a "high level language"?
1
u/hatessw Mar 25 '15
I think it's a fairly logical distinction to make, and I think it's clear from the article what was meant. I find it an intuitive use of the term, personally, so I do agree.
Other commenters seem to disagree with this definition of the phrase entirely, at which point the rest of the article may become meaningless to them. Shared definitions, to the best of my knowledge, are absolutely required for meaningful discourse.
That said, the author could just replace the term throughout the document with something like 'abstracted language' or some other term, and as long as it's generally agreed on, the article would have meaning.
The problem is that definitions are political in nature. They are not factual. You have to make choices in defining terms, and you'll probably always have conflict about what they mean. I am in a homogenous environment, and I find that most arguments I'm a part of can eventually be reduced to conflicting definitions. Once you find out where that stems from (i.e. what phrase or word is at the very core of the conflict) it may be possible to address it by choosing shared terminology.
1
Mar 25 '15
So, in effect, you're saying the term "high level language", when considered in a vacuum and free of any prior historical meaning, is a sufficient descriptor for the fact that today's x86 micro-architectures don't do what one might naively expect nor do they do it in a predictable (timing-wise) fashion.
The problem is that the prior historical meaning of high level language has never included micro-architectural details, and for good reason. It makes no sense to do so because you'll find that practically all assembly languages out there are high level by this definition and there are very few, if any, "lower level languages" and the classification is therefore useless.
It also doesn't make sense to do so because you'll end up in the following pickle: If today's x86 assembly is a high level language, does that mean that older x86 processors with simpler micro-architectures, were lower level languages? If so, how can that be an accurate description of the language? It's the same language after all, the same instructions, in the same order. It doesn't make sense to conflate the micro-architecture of the processor with the expressiveness of the language, one is separate from the other and the "level" of a language has always been used to describe and categorize languages based on things like expressiveness.
1
u/hatessw Mar 25 '15
Not at all - that's not my argument. I'm saying no term can be automatically relied on to have a shared meaning and my opinion (and that of others) are not universally shared. Historical details don't change that, and the fact that we don't seem to be in agreement about how logical the term is proves that meanings of phrases can't be relied on without synchronizing these meanings.
Even if you don't agree with the term, the point of the author still stands had he used a different phrase to describe the phenomenon.
If so, how can that be an accurate description of the language?
It cannot, universally, because it is not an innate property of a language, but a property within a context. Any sequential language that could have constant-time execution of instructions can be executed in breach of that property as well (not sure if the reverse holds).
It doesn't make sense to conflate the micro-architecture of the processor with the expressiveness of the language, one is separate from the other and the "level" of a language has always been used to describe and categorize languages based on things like expressiveness.
I'm unconvinced that use of the term is better defined then given the vague nature of 'expressiveness', even if there are ways to attempt to estimate it. But as I said; definitions are political, not factual. They are choices. I cannot logically claim definitions not to be factual while simultaneously arguing against your chosen definitions.
The better question is, aside from the terms used, are we in disagreement about the underlying subject, the consequences of the inability to rely on constant-time execution in assembler? From what I can tell, we aren't. Is that right?
→ More replies (0)
128
u/Sting3r Mar 25 '15
As a CS student currently taking an x86 course, I finally understood an entire /r/programming link! I might not quite follow all the C++ or Python talk, and stuff over at /r/java might be too advanced, but today I actually feel like I belong in these subreddits instead of just an outsider looking in.
Thanks OP!