News for hackers. Stuff that matters.
Re:A Guide to Porting C/C++ to Rust by geezerjay on Sunday January 29, 2017 @10:16AM (#13514439)
> Those things help but it's still possible to return a pointer to an inner part of an allocation(say an element in a vector) and then have the vector go out of scope.

That's something that the programmer needs to specifically ask for, and I'm not aware of any C++ compiler which doesn't throw a bunch of warnings regarding an object's scope and lifetime.

That means your point boils down to arguing that if a programmer really wants to, he can still shoot himself in the proverbial foot by writing C++ code, but in order for that to happen he needs to have a death wish and intentionally avoid all warnings and safe practices to begin with.

Re:A Guide to Porting C/C++ to Rust by vvanders on Sunday January 29, 2017 @10:26AM (#13514478)
I've seen variations of the below in just about every codebase I've worked in. Just ran it through GCC, Clang and MSCV(W4) with no warnings ;).

  #include <vector>

  int main()
  {
      struct Foo {
          int bar;
      };
    
      Foo* bar;
    
      {
          std::vector<Foo> foos;
        
          foos.emplace_back(Foo{1});
          foos.emplace_back(Foo{2});
        
          bar = &foos[1];
      }
      
      bar->bar = 2;
  }
Re:A Guide to Porting C/C++ to Rust by Const-me on Sunday January 29, 2017 @11:01AM (#13514699)
True, found similar bugs in my own C++ code, and I read and fix all warnings.

However, I never spent more than 10 minutes fixing that.

Debug version of MS C runtime fills freed heap memory with a magic number 0xFEEEFEEE. You’ll get exception in runtime, and you’ll immediately have the idea what’s wrong with the code.

Re:A Guide to Porting C/C++ to Rust by vvanders on Sunday January 29, 2017 @11:25AM (#13514840)
Yeah, this is a trivial example. It becomes trickier when you start dealing with things across library boundaries where you might not have source code.

I've seen 5 minute fixes and things that took weeks to track down(yay for non deterministic repros).

Re:A Guide to Porting C/C++ to Rust by pjmlp on Sunday January 29, 2017 @12:00PM (#13515047)
Back when I was doing C coding in the early 2000's I have spent around one week tracking down a server crash caused by a bad pointer.

Technical support guys were not that happy with the customer regularly calling them to know when the fix would be delivered.

Have other war stories about tracking down leaks as well.

Re:A Guide to Porting C/C++ to Rust by Const-me on Sunday January 29, 2017 @01:02PM (#13515565)
It’s hard to debug a library without the source code. This is irrelevant to what language was that source code in, C++ or Rust.
Re:A Guide to Porting C/C++ to Rust by vvanders on Sunday January 29, 2017 @01:14PM (#13515654)
Yeah but we're talking about lifetime bugs here. In the case of Rust they are encoded in the function signature so you don't have to rely on convention or documentation.
Re:A Guide to Porting C/C++ to Rust by Const-me on Sunday January 29, 2017 @01:55PM (#13515869)
In most cases you have the source code you’re building. Combined with some stuff in C runtime, this pinpoints the C++ problem nearly as accurately as Rust’s compiler would.

Most commercial libraries are written in C or C++. The reasons include performance, smaller runtime, and ease of use (all OS kernels are written in C, so the majority of languages already feature decent C interop). Even if you code Rust, you won’t have that compile-time checks inside proprietary third-party libraries.

Re:A Guide to Porting C/C++ to Rust by vvanders on Sunday January 29, 2017 @03:21PM (#13516359)
So if I can summarize: If you don't use Rust you won't see the benefits of Rust? ;)

FWIW it's getting pretty common in the Rust ecosystem to wrap 3rd party libs with proper lifetime annotations with opaque types when possible. Things like OpenGL, JNI bindings and whatnot can still benefit from lifetime annotations the compiler provides.

Re:A Guide to Porting C/C++ to Rust by Const-me on Sunday January 29, 2017 @03:44PM (#13516513)
I’d summarize in a different way.

Rust proponents often overestimate practical impart of those C++ safely problems fixed by Rust.

At the same time, they often underestimate or ignore Rust’s problems caused by the same safety thing. The main of which is, it’s hard to implement some widely used data structures in Rust. Here’s my old thread where I discuss the topic in details:

https://news.ycombinator.com/item?id=11977247

Re:A Guide to Porting C/C++ to Rust by pcwalton on Sunday January 29, 2017 @06:52PM (#13517595)
All the exploitable UAFs in shipping browser engines for years and years are a strong counterexample to the idea that UAF isn't a problem in modern C++.
Re:A Guide to Porting C/C++ to Rust by dman on Sunday January 29, 2017 @06:59PM (#13517629)
Out of curiosity - do the codebases for modern browser engines routinely get subjected to static analysis tools and runtime tools like asan (Address sanitizer) / valgrind?

Are there categories of UAF that would not be caught with valgrind / asan? Or is it the case that the test suites do not have sufficient coverage to exhibit the issues?

Re:A Guide to Porting C/C++ to Rust by catnaroek on Sunday January 29, 2017 @10:20AM (#13514452)
I don't see any C++ compilers raising warnings because you use shared mutable objects without grabbing the right lock first.
Re:A Guide to Porting C/C++ to Rust by MaulingMonkey on Sunday January 29, 2017 @04:45PM (#13516891)
Check out https://clang.llvm.org/docs/ThreadSafetyAnalysis.html

Requires manual annotation and isn't nearly as systematic and comprehensive as the Rust compiler, but I've found threading bugs with this in existing code that I was about to modify.

Re:A Guide to Porting C/C++ to Rust by catnaroek on Sunday January 29, 2017 @05:48PM (#13517256)
Ah, this is very nice.
Re:A Guide to Porting C/C++ to Rust by pcwalton on Sunday January 29, 2017 @06:52PM (#13517590)
> That's something that the programmer needs to specifically ask for

No, it's not.

Use-after-free is very easy. Just push onto a vector while you're iterating over it, to name one obvious example that comes up a lot.

Re:A Guide to Porting C/C++ to Rust by tomjakubowski on Sunday January 29, 2017 @02:42PM (#13516141)
Let me share a sanitized C++ memory corruption story.

We were testing our application the night before a big demo for our client. Ours was a C++ OpenGL application with several "phases" the user could enter from a "main menu" type screen. Our demo explored these phases in a pre-planned order. All went well, until somebody decided to try doing the demo in a slightly different order. Once the tester visited a certain phase of the application, the thing crashed and burned. The stack trace was littered with some incomprehensible container-implementation-detail junk. Debuggers weren't pointing at an obvious sign, other than that something was dereferencing a garbage pointer.

We noticed that if we went back to the original canned order of operations (this was a demo, after all), the application didn't crash. But this still made us nervous. We found that we could sometimes swap a few phases early in the application's run with no ill effect.

Here's part of the class responsible for some of the higher layers of the stack trace, from one of the lower level libraries we build all of our applications on (this is important because, for hysterical raisins, we can't source-step debug those libraries in production). Can you spot the bug?

    #include <map>
    #include <string>
    #include <vector>
    
    struct Message;
    
    class MessageCentral {
    public:
      void HandleMessages() {
        // messages were collected somewhere and organized by their queue.
        const std::vector<std::string> queues_with_new_messages;
        for (const std::string &queue : queues_with_new_messages) {
          const std::vector<Message> msgs; // = messages_for_queue(queue);
          const auto &handlers = handlers_[queue];
          for (auto &h : handlers) {
            for (const auto &msg : msgs) {
              h(msg);
            }
          }
        }
      }
      void RegisterMessageHandler(const std::string &queue_name, std::function<void(Message)> handler) {
        handlers_[queue_name].push_back(handler);
      }
    private:
      std::map<std::string, std::vector<std::function<void(Message)>>> handlers_;
    };
    
(Note: this is probably way easier to figure out with all of the other code needed to implement MessageCentral taken out! It took us three engineers working for about 2.5-3 hours each, with lots and lots of gdb and printf debugging, to nail this one down. This particular crash would have been impossible in Rust, because the compiler would have rejected the Rust equivalent of the above C++ code.)

(oh, by the way, we couldn't use runtime memory sanitizer tools like ASan or valgrind because the application linked to CEF. In retrospect, we could have dropped in a fake CEF that didn't interfere with ASan or valgrind, but that would have required build resources we didn't have available in the frenzied night before a big demo.)

Re:A Guide to Porting C/C++ to Rust by MaulingMonkey on Sunday January 29, 2017 @04:27PM (#13516798)
> Can you spot the bug?

I assume one of your handlers invokes RegisterMessageHandler invalidates the iterators to handlers_[some_queue_name]. As such, the following line exhibits UB:

    for (auto &h : handlers) {
In practice, the exact behavior is likely to depend on if the vector resized it's underlying storage or not.

EDIT: I'll also note that using a debug build of the SC++L with iterator debugging enabled should give you a useful diagnostic here - given a modern SC++L implementation. Whether or not your application runs well enough, to actually get to the point of being able to reproduce the bug with iterator debugging enabled, is another matter entirely of course.

EDIT x2: I'll further note I'm cheating here by having encountered and fixed this same bug on a different codebase.