Intellijel should do a module called the Spectre of Meltdown. It would imho have to be a delay.

1 Like

We might be looking at the real possibility that every shared library will need to be recompiled with a “retpoline” style defence against Spectre. So it might not be that hyperbolic.

The CPU manufacturers have a real problem with branch prediction, at the moment branch fails leave observable state changes in the CPU, in the future they’re going to need a method to unwind a lot more on branch fail.

From the Spectre paper:

• The branch predictor learns from jumps to illegal
destinations. Although an exception is triggered in
the attacker’s process, this can be caught easily (e.g.
using try…catch in C++). The branch predictor
will then make predictions that send other processes
to the illegal destination.

I’ve seen a few other allusion on the net that it may be possible to read memory from another process. Ultimately this is a whole new class of security vulnerability, I don’t think we really know the full implications yet.

edit: see variant 2 (which is a Spectre attack – tricking the branch predictor) on the Google Security Blog:

This attack variant uses the ability of one process to influence the speculative execution behavior of code in another security context (i.e., guest/host mode, CPU ring, or process) running on the same physical CPU core.

and

Mitigating this attack variant requires either installing and enabling a CPU microcode update from the CPU vendor (e.g., Intel’s IBRS microcode), or applying a software mitigation (e.g., Google’s Retpoline) to the hypervisor, operating system kernel, system programs and libraries, and user applications.

I don’t think the microcode updates are out yet, and whether they are going to cover all CPUs going back the last 20 years…

2 Likes

Yeah that’s all interesting, I was just suggesting the discussion of these specifics be moved to a thread about these vulnerabilities. But maybe I’m wrong and there’s some tips and tricks to discuss about it now.

1 Like

Some concrete advice on dealing with Spectre and Meltdown:

http://www.daemonology.net/blog/2018-01-17-some-thoughts-on-spectre-and-meltdown.html

I only realised the enormous power of gcc’s dynamic linking very recently - allows to write C programs that recompile bits of themselves! https://gist.github.com/ranch-verdin/1c794b778f61cce633e209380692a2d0

I guess this kind of hackery might be ‘considered harmful’ but have managed to use the technique in what I think is a cool way on a project of mine.

Had somehow got it into my head that ‘dynamic partial recompilation of a C program’ was only supported by llvm (which is clearly not strictly true, as shown in the above example). Anyone on here hip to how llvm differs from gcc in this regard? (i.e the possibilities for modifying C programs on the fly whilst they run)

Does seem that many people choose llvm over gcc for this kind of task (e.g faustlive). Is this just a fad or is there more to it?

1 Like

off the top of my head, some reasons clang/llvm has been a prominent tool for this kinda application:

  • it’s designed from the ground up to be a modular compiler infrastructure, with an API, instead of a monolithic compiler program.
  • it’s BSD-licensed. (these last 2 features make it much more attractive as an embedded component for a commercial IDE.)
  • it’s a modern codebase that is much easier to adapt to different languages / targets (https://llvm.org/docs/CodeGenerator.html)
  • it’s quite a bit faster and more memory-efficient

but inherently i don’t think there’s anything too crazy about compiling and linking dynamic libraries at runtime, regardless of the compiler.

1 Like

any quick recs for starting points for c for the object-oriented language oriented ? either web reference or a book I can find at the library or buy cheap-ish.

I found Essential C somewhere in this thread which seems pretty decent.

4 Likes

so I kinda just realized how much of a funny question that was - I sat down with that pdf and “learned C” in like two hours

cuz that’s the whole thing right, it’s “”“simple”"", as in barely any syntax, but complex to execute. so “learning C” is going to be much more about reading code in the style that I need to use it. that’s a big step away from how learning other programming languages work.

idk just a funny/neat realization at 9:40 pm

7 Likes

Syntactically, C is fairly simple. The bigger gotchas are things like memory leaks, segmentation/bus faults, and undefined behavior. Those really suck because you can have code that looks completely innocent but will actually crash when you try to run it. It is feature of C that makes it very special :wink:

In fact, there is a whole contest for doing just this: https://en.wikipedia.org/wiki/Underhanded_C_Contest

3 Likes

4 posts were merged into an existing topic: Lower-level audio programming

I haven’t read through all of it yet, and as always with these things you may not agree with everything…

But I’ve already found out about -Wdouble-promotion which is pretty useful on embedded devices!

e.g. from the article

bool float_promotion_example(float val) {
  return val > 2.6;
}

Will cause val to be promoted to a double for the comparison (fixed with a 2.6f instead). That’s just the kind of silly mistake that I’d make all the time!

Sadly the -Wdouble-promotion is too new to be included in avr32-gcc

3 Likes

I’m wondering if there’s an easy way to set one C++ array equal to another…
Something like

float array1[16] = {1, 2, 3, 4, 5,…}, array2[16];
array2 = array1;

Is there a simple way to do this? should I actually be using vectors? and so on

It’s been a while since I’ve done any C++, so AFAIK…

Using C arrays in C++ will give you C semantics, so array2 = array1 is just 2 pointers to the same array (aka aliasing), and not a copy.

If you want to copy C arrays then you need to use memcpy.

But… in C++ it’s better to use STL based data structures, so either vector or array. Have a look at this for the various copy methods.

When I did do C++ I always used to use https://en.cppreference.com/w/ to look stuff up (it’s good for the C standard library too), it’s not the easiest as a beginner… but it’s worth trying to figure out your way round.

Thanks a lot my friend. Now I can try and make my VCV module not crash Rack in its entirety…

just echoing with examples…

in C, use memcpy, and be careful:

#include <string.h>
#include <stdio.h>

#define SIZE 10 // can't initialize stack-allocated C-style arr with variable size

int main() {

    typedef float sample_t;
    sample_t a[SIZE] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    sample_t b[SIZE];
 
    memcpy(b, a, SIZE * sizeof(sample_t));

    // change stuff in `a`
    for (int i=0; i<SIZE; ++i) {
	a[i] = i * 2;
    }

    // print `b`
    for (int i=0; i<10; ++i) {
	printf("%f ", b[i]);
    }
    printf("\n");
}

in C++, prefer std::array (fixed size) or std::vector (mutable size), and built-in copy semantics:

#include <array>
#include <iostream>

constexpr size_t size = 10; // yay, `constexpr` exists
typedef float sample_t;

int main() {
    typedef std::array<sample_t, size> arr_t;
    arr_t a = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    arr_t b;
    b = a; // copies

    // change stuff in `a`
    for (int i=0; i<size; ++i) {
	a[i] = i * 2;
    }

    // print `b`:
    for (auto& x: b) {
	std::cout << x << " ";
    }
    std::cout << std::endl;
}

5 Likes

Thanks, that’s really helpful

Does anyone have a favourite data structures library for C? I’m guessing something macro based…?

In particular I’m looking for a linked list (single or double), and a queue. Heap allocated / malloc is fine, though it would be nice if the queue had the option of pre-allocating with a max size.

And along similar lines, anyone have any techniques for making tagged unions easy to work with?

Those are usually so simple it’s worth writing your own for; I rarely use libraries for things that aren’t highly complex (like an rtos or a vendor abstraction layer).

2 Likes

For linked lists I generally prefer to add my own code directly to the class/struct that’s being stored in them. It’s just a matter of adding next/previous pointers to the same class, and static front/back pointers.

For C++ I stick with the good ol’ Standard Template Library.

maybe sys/queue.h and sys/tree.h, from the BSD libc extensions? these are macro-based and most queue operations are O(1).

{ https://github.com/freebsd/freebsd/blob/master/sys/sys/queue.h }
{ https://github.com/freebsd/freebsd/blob/master/sys/sys/tree.h }

2 Likes